2 * Copyright (C) 2013 Nikos Mavrogiannopoulos
4 * This file is part of GnuTLS.
6 * GnuTLS is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuTLS is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GnuTLS; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
28 #if defined(_WIN32) || !defined(ENABLE_HEARTBEAT)
38 #include <sys/types.h>
39 #include <netinet/in.h>
40 #include <sys/socket.h>
42 #include <arpa/inet.h>
44 #include <gnutls/gnutls.h>
45 #include <gnutls/dtls.h>
49 static void terminate(void);
51 /* This program tests large packet sending in DTLS
54 static void server_log_func(int level, const char *str)
56 fprintf(stderr, "server|<%d>| %s", level, str);
59 static void client_log_func(int level, const char *str)
61 fprintf(stderr, "client|<%d>| %s", level, str);
64 /* These are global */
67 /* A very basic DTLS client, with anonymous authentication, that exchanges heartbeats.
70 #define MAX_BUF 24*1024
71 #define MAX_MTU 20*1024
73 static void client(int fd)
75 gnutls_session_t session;
77 char buffer[MAX_BUF + 1];
78 gnutls_anon_client_credentials_t anoncred;
79 /* Need to enable anonymous KX specifically. */
84 gnutls_global_set_log_function(client_log_func);
85 gnutls_global_set_log_level(4711);
88 gnutls_anon_allocate_client_credentials(&anoncred);
90 /* Initialize TLS session
92 gnutls_init(&session, GNUTLS_CLIENT | GNUTLS_DATAGRAM);
93 gnutls_heartbeat_enable(session, GNUTLS_HB_PEER_ALLOWED_TO_SEND);
94 gnutls_dtls_set_mtu(session, 1500);
96 /* Use default priorities */
97 gnutls_priority_set_direct(session,
98 "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL",
101 /* put the anonymous credentials to the current session
103 gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred);
105 gnutls_transport_set_int(session, fd);
107 /* Perform the TLS handshake
110 ret = gnutls_handshake(session);
112 while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
115 fail("client: Handshake failed\n");
120 success("client: Handshake was completed\n");
124 success("client: DTLS version is: %s\n",
125 gnutls_protocol_get_name
126 (gnutls_protocol_get_version(session)));
129 ret = gnutls_record_recv(session, buffer, sizeof(buffer));
131 while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED || ret > 0);
134 fail("recv: %s\n", gnutls_strerror(ret));
140 gnutls_deinit(session);
142 gnutls_anon_free_client_credentials(anoncred);
144 gnutls_global_deinit();
147 static void terminate(void)
151 kill(child, SIGTERM);
155 static void server(int fd)
158 char buffer[MAX_BUF + 1];
159 gnutls_session_t session;
160 gnutls_anon_server_credentials_t anoncred;
161 /* this must be called once in the program
166 gnutls_global_set_log_function(server_log_func);
167 gnutls_global_set_log_level(4711);
170 gnutls_anon_allocate_server_credentials(&anoncred);
172 gnutls_init(&session, GNUTLS_SERVER | GNUTLS_DATAGRAM);
173 gnutls_dtls_set_mtu(session, 1500);
175 /* avoid calling all the priority functions, since the defaults
178 gnutls_priority_set_direct(session,
179 "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-ECDH:+CURVE-ALL",
182 gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred);
184 gnutls_transport_set_int(session, fd);
187 ret = gnutls_handshake(session);
189 while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
192 gnutls_deinit(session);
194 fail("server: Handshake has failed (%s)\n\n",
195 gnutls_strerror(ret));
198 success("server: Handshake was completed\n");
201 success("server: TLS version is: %s\n",
202 gnutls_protocol_get_name
203 (gnutls_protocol_get_version(session)));
205 /* see the Getting peer's information example */
206 /* print_info(session); */
208 /* avoid uninitialized warnings */
209 memset(buffer, 1, sizeof(buffer));
212 gnutls_record_send(session, buffer,
213 gnutls_dtls_get_data_mtu(session) + 12);
214 if (ret != GNUTLS_E_LARGE_PACKET) {
216 fail("send[%d]: %s\n", __LINE__, gnutls_strerror(ret));
220 gnutls_record_send(session, buffer,
221 gnutls_dtls_get_data_mtu(session) + 5048);
222 if (ret != GNUTLS_E_LARGE_PACKET) {
224 fail("send[%d]: %s\n", __LINE__, gnutls_strerror(ret));
228 gnutls_record_send(session, buffer,
229 gnutls_dtls_get_data_mtu(session));
232 fail("send[%d]: %s\n", __LINE__, gnutls_strerror(ret));
235 gnutls_dtls_set_mtu(session, MAX_MTU);
237 gnutls_record_send(session, buffer,
238 gnutls_dtls_get_data_mtu(session) + 12);
239 if (ret != GNUTLS_E_LARGE_PACKET) {
241 fail("send[%d]: %s\n", __LINE__, gnutls_strerror(ret));
245 gnutls_record_send(session, buffer,
246 gnutls_dtls_get_data_mtu(session) + 5048);
247 if (ret != GNUTLS_E_LARGE_PACKET) {
249 fail("send[%d]: %s\n", __LINE__, gnutls_strerror(ret));
253 gnutls_record_send(session, buffer,
254 gnutls_dtls_get_data_mtu(session));
255 if (ret > 16384 || ret < 0) {
257 fail("send[%d]: %s\n", __LINE__, gnutls_strerror(ret));
260 /* test cork and uncork */
261 gnutls_record_cork(session);
264 gnutls_record_send(session, buffer,
265 gnutls_dtls_get_data_mtu(session));
268 fail("send[%d]: %s\n", __LINE__, gnutls_strerror(ret));
271 ret = gnutls_record_uncork(session, 0);
274 fail("send[%d]: %s\n", __LINE__, gnutls_strerror(ret));
277 gnutls_record_cork(session);
280 gnutls_record_send(session, buffer,
281 gnutls_dtls_get_data_mtu(session) - 16);
284 fail("send[%d]: %s\n", __LINE__, gnutls_strerror(ret));
288 gnutls_record_send(session, buffer,
289 gnutls_dtls_get_data_mtu(session));
290 if (ret != GNUTLS_E_LARGE_PACKET) {
292 fail("send[%d]: %s\n", __LINE__, gnutls_strerror(ret));
295 ret = gnutls_record_uncork(session, GNUTLS_RECORD_WAIT);
298 fail("send[%d]: %s\n", __LINE__, gnutls_strerror(ret));
301 /* do not wait for the peer to close the connection.
303 gnutls_bye(session, GNUTLS_SHUT_WR);
306 gnutls_deinit(session);
308 gnutls_anon_free_server_credentials(anoncred);
310 gnutls_global_deinit();
313 success("server: finished\n");
316 static void start(void)
321 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
323 perror("socketpair");
340 if (WEXITSTATUS(status) != 0)
341 fail("Child died with status %d\n",
342 WEXITSTATUS(status));