]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/CyaSSL/examples/echoserver/echoserver.c
Prepare for V7.2.0 release.
[freertos] / FreeRTOS-Plus / CyaSSL / examples / echoserver / echoserver.c
1 /* echoserver.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/ssl.h>
27 #include <cyassl/test.h>
28
29 #ifndef NO_MAIN_DRIVER
30     #define ECHO_OUT
31 #endif
32
33
34 #ifdef SESSION_STATS
35     CYASSL_API void PrintSessionStats(void);
36 #endif
37
38
39 static void SignalReady(void* args)
40 {
41 #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER)
42     /* signal ready to tcp_accept */
43     func_args* server_args = (func_args*)args;
44     tcp_ready* ready = server_args->signal;
45     pthread_mutex_lock(&ready->mutex);
46     ready->ready = 1;
47     pthread_cond_signal(&ready->cond);
48     pthread_mutex_unlock(&ready->mutex);
49 #endif
50 }
51
52
53 THREAD_RETURN CYASSL_THREAD echoserver_test(void* args)
54 {
55     SOCKET_T       sockfd = 0;
56     CYASSL_METHOD* method = 0;
57     CYASSL_CTX*    ctx    = 0;
58
59     int    doDTLS = 0;
60     int    outCreated = 0;
61     int    shutdown = 0;
62     int    useAnyAddr = 0;
63     int    argc = ((func_args*)args)->argc;
64     char** argv = ((func_args*)args)->argv;
65
66 #ifdef ECHO_OUT
67     FILE* fout = stdout;
68     if (argc >= 2) {
69         fout = fopen(argv[1], "w");
70         outCreated = 1;
71     }
72     if (!fout) err_sys("can't open output file");
73 #endif
74
75     ((func_args*)args)->return_code = -1; /* error state */
76
77 #ifdef CYASSL_DTLS
78     doDTLS  = 1;
79 #endif
80
81     tcp_listen(&sockfd, yasslPort, useAnyAddr, doDTLS);
82
83 #if defined(CYASSL_DTLS)
84     method  = CyaDTLSv1_server_method();
85 #elif  !defined(NO_TLS)
86     method = CyaSSLv23_server_method();
87 #else
88     method = CyaSSLv3_server_method();
89 #endif
90     ctx    = CyaSSL_CTX_new(method);
91     /* CyaSSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); */
92
93 #ifdef OPENSSL_EXTRA
94     CyaSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
95 #endif
96
97 #ifndef NO_FILESYSTEM
98     #ifdef HAVE_NTRU
99         /* ntru */
100         if (CyaSSL_CTX_use_certificate_file(ctx, ntruCert, SSL_FILETYPE_PEM)
101                 != SSL_SUCCESS)
102             err_sys("can't load ntru cert file, "
103                     "Please run from CyaSSL home dir");
104
105         if (CyaSSL_CTX_use_NTRUPrivateKey_file(ctx, ntruKey)
106                 != SSL_SUCCESS)
107             err_sys("can't load ntru key file, "
108                     "Please run from CyaSSL home dir");
109     #elif HAVE_ECC
110         /* ecc */
111         if (CyaSSL_CTX_use_certificate_file(ctx, eccCert, SSL_FILETYPE_PEM)
112                 != SSL_SUCCESS)
113             err_sys("can't load server cert file, "
114                     "Please run from CyaSSL home dir");
115
116         if (CyaSSL_CTX_use_PrivateKey_file(ctx, eccKey, SSL_FILETYPE_PEM)
117                 != SSL_SUCCESS)
118             err_sys("can't load server key file, "
119                     "Please run from CyaSSL home dir");
120     #else
121         /* normal */
122         if (CyaSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM)
123                 != SSL_SUCCESS)
124             err_sys("can't load server cert file, "
125                     "Please run from CyaSSL home dir");
126
127         if (CyaSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM)
128                 != SSL_SUCCESS)
129             err_sys("can't load server key file, "
130                     "Please run from CyaSSL home dir");
131     #endif
132 #else
133     load_buffer(ctx, svrCert, CYASSL_CERT);
134     load_buffer(ctx, svrKey,  CYASSL_KEY);
135 #endif
136
137 #if defined(CYASSL_SNIFFER) && !defined(HAVE_NTRU) && !defined(HAVE_ECC)
138     /* don't use EDH, can't sniff tmp keys */
139     CyaSSL_CTX_set_cipher_list(ctx, "AES256-SHA");
140 #endif
141
142     SignalReady(args);
143
144     while (!shutdown) {
145         CYASSL* ssl = 0;
146         char    command[1024];
147         int     echoSz = 0;
148         int     clientfd;
149         int     firstRead = 1;
150         int     gotFirstG = 0;
151                 
152 #ifndef CYASSL_DTLS 
153         SOCKADDR_IN_T client;
154         socklen_t     client_len = sizeof(client);
155         clientfd = accept(sockfd, (struct sockaddr*)&client,
156                          (ACCEPT_THIRD_T)&client_len);
157 #else
158         clientfd = udp_read_connect(sockfd);
159 #endif
160         if (clientfd == -1) err_sys("tcp accept failed");
161
162         ssl = CyaSSL_new(ctx);
163         if (ssl == NULL) err_sys("SSL_new failed");
164         CyaSSL_set_fd(ssl, clientfd);
165         #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA)
166             CyaSSL_SetTmpDH_file(ssl, dhParam, SSL_FILETYPE_PEM);
167         #else
168             SetDH(ssl);  /* will repick suites with DHE, higher than PSK */
169         #endif
170         if (CyaSSL_accept(ssl) != SSL_SUCCESS) {
171             printf("SSL_accept failed\n");
172             CyaSSL_free(ssl);
173             CloseSocket(clientfd);
174             continue;
175         }
176 #if defined(PEER_INFO)
177         showPeer(ssl);
178 #endif
179
180         while ( (echoSz = CyaSSL_read(ssl, command, sizeof(command))) > 0) {
181
182             if (firstRead == 1) {
183                 firstRead = 0;  /* browser may send 1 byte 'G' to start */
184                 if (echoSz == 1 && command[0] == 'G') {
185                     gotFirstG = 1;
186                     continue;
187                 }
188             }
189             else if (gotFirstG == 1 && strncmp(command, "ET /", 4) == 0) {
190                 strncpy(command, "GET", 4);
191                 /* fall through to normal GET */
192             }
193            
194             if ( strncmp(command, "quit", 4) == 0) {
195                 printf("client sent quit command: shutting down!\n");
196                 shutdown = 1;
197                 break;
198             }
199             if ( strncmp(command, "break", 5) == 0) {
200                 printf("client sent break command: closing session!\n");
201                 break;
202             }
203 #ifdef SESSION_STATS
204             if ( strncmp(command, "printstats", 10) == 0) {
205                 PrintSessionStats();
206                 break;
207             }
208 #endif
209             if ( strncmp(command, "GET", 3) == 0) {
210                 char type[]   = "HTTP/1.0 200 ok\r\nContent-type:"
211                                 " text/html\r\n\r\n";
212                 char header[] = "<html><body BGCOLOR=\"#ffffff\">\n<pre>\n";
213                 char body[]   = "greetings from CyaSSL\n";
214                 char footer[] = "</body></html>\r\n\r\n";
215             
216                 strncpy(command, type, sizeof(type));
217                 echoSz = sizeof(type) - 1;
218
219                 strncpy(&command[echoSz], header, sizeof(header));
220                 echoSz += sizeof(header) - 1;
221                 strncpy(&command[echoSz], body, sizeof(body));
222                 echoSz += sizeof(body) - 1;
223                 strncpy(&command[echoSz], footer, sizeof(footer));
224                 echoSz += sizeof(footer);
225
226                 if (CyaSSL_write(ssl, command, echoSz) != echoSz)
227                     err_sys("SSL_write failed");
228                 break;
229             }
230             command[echoSz] = 0;
231
232             #ifdef ECHO_OUT
233                 fputs(command, fout);
234             #endif
235
236             if (CyaSSL_write(ssl, command, echoSz) != echoSz)
237                 err_sys("SSL_write failed");
238         }
239 #ifndef CYASSL_DTLS
240         CyaSSL_shutdown(ssl);
241 #endif
242         CyaSSL_free(ssl);
243         CloseSocket(clientfd);
244 #ifdef CYASSL_DTLS
245         tcp_listen(&sockfd, yasslPort, useAnyAddr, doDTLS);
246         SignalReady(args);
247 #endif
248     }
249
250     CloseSocket(sockfd);
251     CyaSSL_CTX_free(ctx);
252
253 #ifdef ECHO_OUT
254     if (outCreated)
255         fclose(fout);
256 #endif
257
258     ((func_args*)args)->return_code = 0;
259     return 0;
260 }
261
262
263 /* so overall tests can pull in test function */
264 #ifndef NO_MAIN_DRIVER
265
266     int main(int argc, char** argv)
267     {
268         func_args args;
269
270         StartTCP();
271
272         args.argc = argc;
273         args.argv = argv;
274
275         CyaSSL_Init();
276 #ifdef DEBUG_CYASSL
277         CyaSSL_Debugging_ON();
278 #endif
279         if (CurrentDir("echoserver") || CurrentDir("build"))
280             ChangeDirBack(2);
281         echoserver_test(&args);
282         CyaSSL_Cleanup();
283
284         return args.return_code;
285     }
286
287     int myoptind = 0;
288     char* myoptarg = NULL;
289
290 #endif /* NO_MAIN_DRIVER */
291
292
293