GENERIC_SERVER  0.0.0.9
A light-weight, cross-platform, pluggable, extensible and secure framework for deploying C++ plug-ins.
 All Classes Files Functions Variables Typedefs Pages
sample_client2.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2013 Broadcom Corporation
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License version 2.1 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Lesser General Public License for more details.
12 
13  You should have received a copy of the GNU Lesser General Public
14  License along with this library; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
26 #include "util.h"
27 #define SET_NAME 1
28 #define FILE_DATA 2
29 #define CLOSE_FILE 3
30 void error(const char *msg)
31 {
32  perror(msg);
33  exit(0);
34 }
35 /***********************************************************************************/
36 
37 int main(int argc, char *argv[])
38 {
39  char buff[MAX_SZ],read_buff[MAX_SZ];
40  SOCKET sock;
41  unsigned int portno,size,sess_type;
42  int n;
43  string plugin_name,message,fname;
44  SSL *ssl_client;
45  SSL_CTX *ctx;
46  ssl_client = NULL;
47  ctx = NULL;
48 
49 #ifdef WINDOWS
50  char win_fname[1024],win_ext[10];
51  sock_init();
52  memset(win_fname,0,1024);
53  memset(win_ext,0,10);
54 #endif
55  memset(buff,0,MAX_SZ);
56  if (argc < 6)
57  error("Usage: sample_client2 <ip_address> <port> <plugin_name> <filename> <1=SSL|0=RAW>");
58  portno = atoi(argv[2]);
59  plugin_name = string(argv[3]);
60  fname = string(argv[4]);
61  sess_type = atoi(argv[5]);
62  if(sess_type == 1)
63  {
64  if(!ssl_init(&ctx))
65  error("ERROR SSL init");
66  if(!ssl_connect((unsigned char *) argv[1], portno,sock,&ssl_client,&ctx))
67  error("ERROR SSL connecting");
68  }
69  else
70  {
71  if(!sock_connect((unsigned char *)argv[1],portno,sock))
72  error("ERROR connecting");
73  }
74  setblocking(sock);
75  memset(buff,0,MAX_SZ);
76  // send filename to server
77 #ifdef WINDOWS
78  _splitpath_s((char *)fname.c_str(), NULL,0, NULL,0, win_fname,1024, win_ext,10);
79  strcat_s(win_fname,1024,win_ext);
80  size = create_message(plugin_name,SET_NAME,win_fname,buff);
81 #else
82  size = create_message(plugin_name,SET_NAME,basename((char *)fname.c_str()),buff);
83 #endif
84  if(sess_type == 1)
85  SSL_write(ssl_client, buff,size);
86  else
87  send(sock,buff,size,0);
88 
89  // open file and read in data
90  ifstream ifs(fname.c_str(), std::ios::binary);
91  while(!ifs.eof())
92  {
93  ifs.read(buff, MAX_SZ-(2*MSG_HDR_SIZE));
94  message = string(buff,(unsigned int)ifs.gcount());
95  // send 1 block of data to server
96  size = create_message(plugin_name,FILE_DATA,message,buff);
97  if(sess_type == 1)
98  n = SSL_write(ssl_client, buff,size);
99  else
100  n = send(sock,buff,size,0);
101  if (n < 0)
102  error("write ERROR ..");
103  if(sess_type == 1)
104  n = ssl_read_socket((unsigned char *)read_buff,sock,&ssl_client);
105  else
106  n = read_socket((unsigned char *)read_buff,sock);
107  if (n < 0)
108  error("read ERROR..");
109 
110  }
111  // close file and let server know
112  ifs.close();
113  message = string("TERMINATE");
114  size = create_message(plugin_name,CLOSE_FILE,message,buff);
115  if(sess_type == 1)
116  {
117  SSL_write(ssl_client, buff,size);
118  while((SSL_shutdown(ssl_client)) == 0);
119  }
120  else
121  send(sock,buff,size,0);
122 #ifdef WINDOWS
123  closesocket(sock);
124 #else
125  close(sock);
126 #endif
127  return 0;
128 }
129 /***********************************************************************************/
130