updates for cryptodev. Require the COP_FLAG_RESET.
[gnutls.git] / tests / mini-loss.c
bloba541c29638b13357b1ce667ad13e763898a0f917
1 /*
2 * Copyright (C) 2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
8 * GnuTLS is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * GnuTLS is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with GnuTLS; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
27 #include <stdio.h>
28 #include <stdlib.h>
30 #if defined(_WIN32)
32 int main()
34 exit(77);
37 #else
39 #include <string.h>
40 #include <sys/types.h>
41 #include <netinet/in.h>
42 #include <sys/socket.h>
43 #include <sys/wait.h>
44 #include <arpa/inet.h>
45 #include <unistd.h>
46 #include <gnutls/gnutls.h>
47 #include <gnutls/dtls.h>
49 #include "utils.h"
51 /* This program simulates packet loss in DTLS datagrams using
52 * the blocking functions. Idea taken from test app of
53 * Sean Buckheister.
56 static void print_type(const unsigned char* buf, int size)
58 if (buf[0] == 22 && size >= 13) {
59 if (buf[13] == 1)
60 fprintf(stderr, "Client Hello\n");
61 else if (buf[13] == 2)
62 fprintf(stderr, "Server Hello\n");
63 else if (buf[13] == 12)
64 fprintf(stderr, "Server Key exchange\n");
65 else if (buf[13] == 14)
66 fprintf(stderr, "Server Hello Done\n");
67 else if (buf[13] == 11)
68 fprintf(stderr, "Certificate\n");
69 else if (buf[13] == 16)
70 fprintf(stderr, "Client Key Exchange\n");
71 else if (buf[4] == 1)
72 fprintf(stderr, "Finished\n");
73 else if (buf[13] == 11)
74 fprintf(stderr, "Server Hello Done\n");
75 else
76 fprintf(stderr, "Unknown handshake\n");
77 } else if (buf[0] == 20) {
78 fprintf(stderr, "Change Cipher Spec\n");
79 } else
80 fprintf(stderr, "Unknown\n");
83 static void
84 server_log_func (int level, const char *str)
86 fprintf (stderr, "server|<%d>| %s", level, str);
89 static void
90 client_log_func (int level, const char *str)
92 fprintf (stderr, "client|<%d>| %s", level, str);
95 /* A very basic TLS client, with anonymous authentication.
98 #define MAX_BUF 1024
99 #define MSG "Hello TLS"
101 static int counter;
102 static int packet_to_lose;
103 gnutls_session_t session;
105 static ssize_t
106 push (gnutls_transport_ptr_t tr, const void *data, size_t len)
108 int fd = (long int)tr;
110 counter++;
112 if (packet_to_lose != -1 && packet_to_lose == counter) {
113 if (debug)
115 fprintf(stderr, "Discarding packet %d: ", counter);
116 print_type(data, len);
118 return len;
120 return send(fd, data, len, 0);
123 static void
124 client (int fd, int packet)
126 int ret, ii;
127 char buffer[MAX_BUF + 1];
128 gnutls_anon_client_credentials_t anoncred;
129 /* Need to enable anonymous KX specifically. */
131 gnutls_global_init ();
133 if (debug)
135 gnutls_global_set_log_function (client_log_func);
136 gnutls_global_set_log_level (4711);
139 gnutls_anon_allocate_client_credentials (&anoncred);
141 /* Initialize TLS session
143 gnutls_init (&session, GNUTLS_CLIENT|GNUTLS_DATAGRAM);
144 gnutls_dtls_set_mtu( session, 1500);
146 /* Use default priorities */
147 gnutls_priority_set_direct (session, "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL", NULL);
149 /* put the anonymous credentials to the current session
151 gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
153 counter = 0;
154 packet_to_lose = packet;
156 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) fd);
157 gnutls_transport_set_push_function (session, push);
159 /* Perform the TLS handshake
163 ret = gnutls_handshake (session);
165 while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
167 if (ret < 0)
169 fail ("client: Handshake failed\n");
170 gnutls_perror (ret);
171 exit(1);
173 else
175 if (debug)
176 success ("client: Handshake was completed\n");
179 if (debug)
180 success ("client: TLS version is: %s\n",
181 gnutls_protocol_get_name (gnutls_protocol_get_version
182 (session)));
184 do {
185 ret = gnutls_record_send (session, MSG, strlen (MSG));
186 } while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
188 do {
189 ret = gnutls_record_recv (session, buffer, MAX_BUF);
190 } while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
192 if (ret == 0)
194 if (debug)
195 success ("client: Peer has closed the TLS connection\n");
196 goto end;
198 else if (ret < 0)
200 fail ("client: Error: %s\n", gnutls_strerror (ret));
201 exit(1);
204 if (debug)
206 printf ("- Received %d bytes: ", ret);
207 for (ii = 0; ii < ret; ii++)
209 fputc (buffer[ii], stdout);
211 fputs ("\n", stdout);
214 gnutls_bye (session, GNUTLS_SHUT_RDWR);
216 end:
218 close (fd);
220 gnutls_deinit (session);
222 gnutls_anon_free_client_credentials (anoncred);
224 gnutls_global_deinit ();
228 /* These are global */
229 gnutls_anon_server_credentials_t anoncred;
230 pid_t child;
232 static gnutls_session_t
233 initialize_tls_session (void)
235 gnutls_session_t session;
237 gnutls_init (&session, GNUTLS_SERVER|GNUTLS_DATAGRAM);
238 gnutls_dtls_set_mtu( session, 1500);
240 /* avoid calling all the priority functions, since the defaults
241 * are adequate.
243 gnutls_priority_set_direct (session, "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL", NULL);
245 gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
247 return session;
250 static void terminate(void)
252 int status;
254 kill(child, SIGTERM);
255 wait(&status);
256 exit(1);
259 static void
260 server (int fd, int packet)
262 int ret;
263 char buffer[MAX_BUF + 1];
264 /* this must be called once in the program
266 gnutls_global_init ();
268 if (debug)
270 gnutls_global_set_log_function (server_log_func);
271 gnutls_global_set_log_level (4711);
274 gnutls_anon_allocate_server_credentials (&anoncred);
276 session = initialize_tls_session ();
278 counter = 0;
279 packet_to_lose = packet;
281 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) fd);
282 gnutls_transport_set_push_function (session, push);
286 ret = gnutls_handshake (session);
288 while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
289 if (ret < 0)
291 close (fd);
292 gnutls_deinit (session);
293 fail ("server: Handshake has failed (%s)\n\n", gnutls_strerror (ret));
294 terminate();
296 if (debug)
297 success ("server: Handshake was completed\n");
299 if (debug)
300 success ("server: TLS version is: %s\n",
301 gnutls_protocol_get_name (gnutls_protocol_get_version
302 (session)));
304 /* see the Getting peer's information example */
305 /* print_info(session); */
307 for (;;)
309 memset (buffer, 0, MAX_BUF + 1);
311 do {
312 ret = gnutls_record_recv (session, buffer, MAX_BUF);
313 } while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
315 if (ret == 0)
317 if (debug)
318 success ("server: Peer has closed the GnuTLS connection\n");
319 break;
321 else if (ret < 0)
323 fail ("server: Received corrupted data(%d). Closing...\n", ret);
324 terminate();
326 else if (ret > 0)
328 /* echo data back to the client
330 do {
331 ret = gnutls_record_send (session, buffer, strlen (buffer));
332 } while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
335 /* do not wait for the peer to close the connection.
337 gnutls_bye (session, GNUTLS_SHUT_WR);
339 close (fd);
340 gnutls_deinit (session);
342 gnutls_anon_free_server_credentials (anoncred);
344 gnutls_global_deinit ();
346 if (debug)
347 success ("server: finished\n");
350 static void start (int server_packet, int client_packet)
352 int fd[2];
353 int ret;
355 if (debug)
356 fprintf(stderr, "\nWill discard %s packet %d\n",
357 (client_packet!=-1)?"client":"server", (client_packet!=-1)?client_packet:server_packet);
359 ret = socketpair(AF_LOCAL, SOCK_DGRAM, 0, fd);
360 if (ret < 0)
362 perror("socketpair");
363 exit(1);
366 child = fork ();
367 if (child < 0)
369 perror ("fork");
370 fail ("fork");
371 exit(1);
374 if (child)
376 int status;
377 /* parent */
378 server (fd[0], server_packet);
379 wait (&status);
380 if (WEXITSTATUS(status) != 0)
381 fail("Child died with status %d\n", WEXITSTATUS(status));
383 else
385 client (fd[1], client_packet);
386 exit(0);
390 void
391 doit (void)
393 start(-1, 1);
394 start(-1, 2);
395 start(-1, 3);
396 start(-1, 4);
398 start(1, -1);
399 start(2, -1);
400 start(3, -1);
401 start(4, -1);
402 start(5, -1);
405 #endif /* _WIN32 */