]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/CyaSSL/examples/echoclient/echoclient.c
d93ca3fe26077534523a3a7668f7bbf6f6e15f1f
[freertos] / FreeRTOS-Plus / CyaSSL / examples / echoclient / echoclient.c
1 /* echoclient.c
2  *
3  * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
4  *
5  * This file is part of CyaSSL.
6  *
7  * CyaSSL is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * CyaSSL is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20  */
21
22 #ifdef HAVE_CONFIG_H
23     #include <config.h>
24 #endif
25
26 #include <cyassl/openssl/ssl.h>
27 #include <cyassl/test.h>
28
29
30 void echoclient_test(void* args)
31 {
32     SOCKET_T sockfd = 0;
33
34     FILE* fin  = stdin;
35     FILE* fout = stdout;
36
37     int inCreated  = 0;
38     int outCreated = 0;
39
40     char send[1024];
41     char reply[1024];
42
43     SSL_METHOD* method = 0;
44     SSL_CTX*    ctx    = 0;
45     SSL*        ssl    = 0;
46
47     int doDTLS = 0;
48     int sendSz;
49     int argc    = 0;
50     char** argv = 0;
51
52     ((func_args*)args)->return_code = -1; /* error state */
53     argc = ((func_args*)args)->argc;
54     argv = ((func_args*)args)->argv;
55
56     if (argc >= 2) {
57         fin  = fopen(argv[1], "r"); 
58         inCreated = 1;
59     }
60     if (argc >= 3) {
61         fout = fopen(argv[2], "w");
62         outCreated = 1;
63     }
64
65     if (!fin)  err_sys("can't open input file");
66     if (!fout) err_sys("can't open output file");
67
68 #ifdef CYASSL_DTLS
69     doDTLS  = 1;
70 #endif
71
72     tcp_connect(&sockfd, yasslIP, yasslPort, doDTLS);
73
74 #if defined(CYASSL_DTLS)
75     method  = DTLSv1_client_method();
76 #elif  !defined(NO_TLS)
77     method = CyaSSLv23_client_method();
78 #else
79     method = SSLv3_client_method();
80 #endif
81     ctx    = SSL_CTX_new(method);
82
83 #ifndef NO_FILESYSTEM
84     if (SSL_CTX_load_verify_locations(ctx, caCert, 0) != SSL_SUCCESS)
85         err_sys("can't load ca file, Please run from CyaSSL home dir");
86     #ifdef HAVE_ECC
87         if (SSL_CTX_load_verify_locations(ctx, eccCert, 0) != SSL_SUCCESS)
88             err_sys("can't load ca file, Please run from CyaSSL home dir");
89     #endif
90 #else
91     load_buffer(ctx, caCert, CYASSL_CA);
92 #endif
93
94 #if defined(CYASSL_SNIFFER) && !defined(HAVE_NTRU) && !defined(HAVE_ECC)
95     /* don't use EDH, can't sniff tmp keys */
96     SSL_CTX_set_cipher_list(ctx, "AES256-SHA");
97 #endif
98
99 #ifdef OPENSSL_EXTRA
100     SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
101 #endif
102     ssl = SSL_new(ctx);
103
104     SSL_set_fd(ssl, sockfd);
105 #if defined(USE_WINDOWS_API) && defined(CYASSL_DTLS) && defined(NO_MAIN_DRIVER)
106     /* let echoserver bind first, TODO: add Windows signal like pthreads does */
107     Sleep(100);
108 #endif
109     if (SSL_connect(ssl) != SSL_SUCCESS) err_sys("SSL_connect failed");
110
111     while (fgets(send, sizeof(send), fin)) {
112
113         sendSz = (int)strlen(send);
114
115         if (SSL_write(ssl, send, sendSz) != sendSz)
116             err_sys("SSL_write failed");
117
118         if (strncmp(send, "quit", 4) == 0) {
119             fputs("sending server shutdown command: quit!\n", fout);
120             break;
121         }
122
123         if (strncmp(send, "break", 5) == 0) {
124             fputs("sending server session close: break!\n", fout);
125             break;
126         }
127
128         while (sendSz) {
129             int got;
130             if ( (got = SSL_read(ssl, reply, sizeof(reply))) > 0) {
131                 reply[got] = 0;
132                 fputs(reply, fout);
133                 sendSz -= got;
134             }
135             else
136                 break;
137         }
138     }
139
140 #ifdef CYASSL_DTLS
141     strncpy(send, "break", 6);
142     sendSz = (int)strlen(send);
143     /* try to tell server done */
144     SSL_write(ssl, send, sendSz);
145 #else
146     SSL_shutdown(ssl);
147 #endif
148
149     SSL_free(ssl);
150     SSL_CTX_free(ctx);
151
152     fflush(fout);
153     if (inCreated)  fclose(fin);
154     if (outCreated) fclose(fout);
155
156     CloseSocket(sockfd);
157     ((func_args*)args)->return_code = 0; 
158 }
159
160
161 /* so overall tests can pull in test function */
162 #ifndef NO_MAIN_DRIVER
163
164     int main(int argc, char** argv)
165     {
166         func_args args;
167
168         StartTCP();
169
170         args.argc = argc;
171         args.argv = argv;
172
173         CyaSSL_Init();
174 #ifdef DEBUG_CYASSL
175         CyaSSL_Debugging_ON();
176 #endif
177         if (CurrentDir("echoclient") || CurrentDir("build"))
178             ChangeDirBack(2);
179         echoclient_test(&args);
180         CyaSSL_Cleanup();
181
182         return args.return_code;
183     }
184
185     int myoptind = 0;
186     char* myoptarg = NULL;
187
188 #endif /* NO_MAIN_DRIVER */
189
190
191