1 /*
2    Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program 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
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; see the file COPYING. If not, write to the
15    Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
16    MA  02110-1335  USA.
17 */
18 
19 /* echoclient.cpp  */
20 
21 #include "../../testsuite/test.hpp"
22 
23 
EchoClientError(SSL_CTX * ctx,SSL * ssl,SOCKET_T & sockfd,const char * msg)24 void EchoClientError(SSL_CTX* ctx, SSL* ssl, SOCKET_T& sockfd, const char* msg)
25 {
26     SSL_CTX_free(ctx);
27     SSL_free(ssl);
28     tcp_close(sockfd);
29     err_sys(msg);
30 }
31 
32 
echoclient_test(void * args)33 void echoclient_test(void* args)
34 {
35 #ifdef _WIN32
36     WSADATA wsd;
37     WSAStartup(0x0002, &wsd);
38 #endif
39 
40     SOCKET_T sockfd = 0;
41     int      argc = 0;
42     char**   argv = 0;
43 
44     FILE* fin  = stdin;
45     FILE* fout = stdout;
46 
47     bool inCreated  = false;
48     bool outCreated = false;
49 
50     set_args(argc, argv, *static_cast<func_args*>(args));
51     if (argc >= 2) {
52         fin  = fopen(argv[1], "r");
53         inCreated = true;
54     }
55     if (argc >= 3) {
56         fout = fopen(argv[2], "w");
57         outCreated = true;
58     }
59 
60     if (!fin)  err_sys("can't open input file");
61     if (!fout) err_sys("can't open output file");
62 
63     tcp_connect(sockfd);
64 
65     SSL_METHOD* method = SSLv23_client_method();
66     SSL_CTX*    ctx = SSL_CTX_new(method);
67     set_certs(ctx);
68     SSL*        ssl = SSL_new(ctx);
69 
70     SSL_set_fd(ssl, sockfd);
71 
72     if (SSL_connect(ssl) != SSL_SUCCESS)
73         EchoClientError(ctx, ssl, sockfd, "SSL_connect failed");
74 
75     char send[1024];
76     char reply[1024];
77 
78     while (fgets(send, sizeof(send), fin)) {
79 
80         int sendSz = (int)strlen(send) + 1;
81         if (SSL_write(ssl, send, sendSz) != sendSz)
82             EchoClientError(ctx, ssl, sockfd, "SSL_write failed");
83 
84         if (strncmp(send, "quit", 4) == 0) {
85             fputs("sending server shutdown command: quit!\n", fout);
86             break;
87         }
88 
89         if (SSL_read(ssl, reply, sizeof(reply)) > 0)
90             fputs(reply, fout);
91     }
92 
93     SSL_CTX_free(ctx);
94     SSL_free(ssl);
95     tcp_close(sockfd);
96 
97     fflush(fout);
98     if (inCreated)  fclose(fin);
99     if (outCreated) fclose(fout);
100 
101     ((func_args*)args)->return_code = 0;
102 }
103 
104 
105 #ifndef NO_MAIN_DRIVER
106 
main(int argc,char ** argv)107     int main(int argc, char** argv)
108     {
109         func_args args;
110 
111         args.argc = argc;
112         args.argv = argv;
113 
114         echoclient_test(&args);
115         yaSSL_CleanUp();
116 
117         return args.return_code;
118     }
119 
120 #endif // NO_MAIN_DRIVER
121