Added gnutls-dane.pc.
[gnutls.git] / tests / mini-loss-time.c
blobc15b53ba68695ea00e2fdd994aee30ce6f1093a9
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>
29 #include <string.h>
31 #if defined(_WIN32)
33 int main()
35 exit(77);
38 #else
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>
48 #include <signal.h>
50 #include "utils.h"
52 /* This program tests whether a DTLS handshake would timeout
53 * in a minute.
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 static int counter;
99 static int packet_to_lose;
100 gnutls_session_t session;
102 static ssize_t
103 push (gnutls_transport_ptr_t tr, const void *data, size_t len)
105 int fd = (long int)tr;
107 counter++;
109 if (packet_to_lose != -1 && packet_to_lose == counter) {
110 if (debug)
112 fprintf(stderr, "Discarding packet %d: ", counter);
113 print_type(data, len);
116 packet_to_lose = 1;
117 counter = 0;
118 return len;
120 return send(fd, data, len, 0);
123 static void
124 client (int fd, int packet)
126 int ret;
127 gnutls_anon_client_credentials_t anoncred;
128 /* Need to enable anonymous KX specifically. */
130 gnutls_global_init ();
132 if (debug)
134 gnutls_global_set_log_function (client_log_func);
135 gnutls_global_set_log_level (4711);
138 gnutls_anon_allocate_client_credentials (&anoncred);
140 /* Initialize TLS session
142 gnutls_init (&session, GNUTLS_CLIENT|GNUTLS_DATAGRAM);
143 gnutls_dtls_set_mtu( session, 1500);
145 /* Use default priorities */
146 gnutls_priority_set_direct (session, "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL", NULL);
148 /* put the anonymous credentials to the current session
150 gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
152 counter = 0;
153 packet_to_lose = packet;
155 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) fd);
156 gnutls_transport_set_push_function (session, push);
158 /* Perform the TLS handshake
162 ret = gnutls_handshake (session);
164 while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
166 gnutls_deinit(session);
167 gnutls_global_deinit();
169 if (ret < 0)
171 if (ret == GNUTLS_E_TIMEDOUT) return;
172 fail ("client: Handshake failed\n");
173 gnutls_perror (ret);
174 exit(1);
176 else
178 if (debug)
179 success ("client: Handshake was completed\n");
182 exit(1);
186 /* These are global */
187 gnutls_anon_server_credentials_t anoncred;
188 pid_t child;
190 static gnutls_session_t
191 initialize_tls_session (void)
193 gnutls_session_t session;
195 gnutls_init (&session, GNUTLS_SERVER|GNUTLS_DATAGRAM);
196 gnutls_dtls_set_mtu( session, 1500);
198 /* avoid calling all the priority functions, since the defaults
199 * are adequate.
201 gnutls_priority_set_direct (session, "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL", NULL);
203 gnutls_credentials_set (session, GNUTLS_CRD_ANON, anoncred);
205 return session;
208 static void
209 server (int fd, int packet)
211 int ret;
212 /* this must be called once in the program
214 gnutls_global_init ();
216 if (debug)
218 gnutls_global_set_log_function (server_log_func);
219 gnutls_global_set_log_level (4711);
222 gnutls_anon_allocate_server_credentials (&anoncred);
224 session = initialize_tls_session ();
226 counter = 0;
227 packet_to_lose = packet;
229 gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) fd);
230 gnutls_transport_set_push_function (session, push);
234 ret = gnutls_handshake (session);
236 while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
238 gnutls_deinit (session);
239 gnutls_global_deinit();
241 if (ret < 0)
243 return;
247 static void start (int server_packet, int client_packet)
249 int fd[2];
250 int ret;
252 if (debug)
253 fprintf(stderr, "\nWill discard %s packet %d\n",
254 (client_packet!=-1)?"client":"server", (client_packet!=-1)?client_packet:server_packet);
256 ret = socketpair(AF_UNIX, SOCK_DGRAM, 0, fd);
257 if (ret < 0)
259 perror("socketpair");
260 exit(1);
263 child = fork ();
264 if (child < 0)
266 perror ("fork");
267 fail ("fork");
268 exit(1);
271 if (child)
273 /* parent */
274 close(fd[1]);
275 server (fd[0], server_packet);
276 close(fd[0]);
277 kill(child, SIGTERM);
279 else
281 close(fd[0]);
282 client (fd[1], client_packet);
283 close(fd[1]);
284 exit(0);
288 static void ch_handler(int sig)
290 int status;
291 wait(&status);
292 if (WEXITSTATUS(status) != 0)
293 fail("Child died with status %d\n", WEXITSTATUS(status));
294 return;
297 void
298 doit (void)
300 time_t tstart, tstop;
302 signal(SIGCHLD, ch_handler);
304 tstart = time(0);
305 start(2, -1);
307 tstop = time(0);
309 tstop = tstop - tstart;
311 if (!(tstop < 70 && tstop > 55))
312 fail("Time difference: %u\n", (unsigned)tstop);
315 #endif /* _WIN32 */