gnutls-cli-debug uses server name indication.
[gnutls.git] / tests / mini-emsgsize-dtls.c
blobb27f8c7ec6f4c1da8af8d4cf38d14752e748355b
1 /*
2 * Copyright (C) 2008-2012 Free Software Foundation, Inc.
4 * Author: Simon Josefsson, 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>
30 #include <errno.h>
31 #include <gnutls/gnutls.h>
32 #include <gnutls/dtls.h>
33 #include <gnutls/crypto.h>
34 #include "utils.h"
35 #define RANDOMIZE
36 #define IGNORE_PUSH
37 #include "eagain-common.h"
39 const char* side = "";
41 static void
42 tls_log_func (int level, const char *str)
44 fprintf (stderr, "%s|<%d>| %s", side, level, str);
47 static int handshake = 0;
49 #define MAX_BUF 1024
50 #define MSG "Hello TLS, and hi and how are you and more data here... and more... and even more and even more more data..."
52 static ssize_t
53 client_push_300 (gnutls_transport_ptr_t tr, const void *data, size_t len)
55 size_t newlen;
57 if (len > 300)
59 gnutls_transport_set_errno ((gnutls_session_t)tr, EMSGSIZE);
60 return -1;
63 len = min(len, sizeof(to_server)-to_server_len);
65 newlen = to_server_len + len;
66 memcpy (to_server + to_server_len, data, len);
67 to_server_len = newlen;
68 #ifdef EAGAIN_DEBUG
69 fprintf(stderr, "eagain: pushed %d bytes to server (avail: %d)\n", (int)len, (int)to_server_len);
70 #endif
71 return len;
74 static ssize_t
75 server_push_300 (gnutls_transport_ptr_t tr, const void *data, size_t len)
77 size_t newlen;
79 if (len > 300)
81 gnutls_transport_set_errno ((gnutls_session_t)tr, EMSGSIZE);
82 return -1;
85 len = min(len, sizeof(to_client)-to_client_len);
87 newlen = to_client_len + len;
88 memcpy (to_client + to_client_len, data, len);
89 to_client_len = newlen;
90 #ifdef EAGAIN_DEBUG
91 fprintf(stderr, "eagain: pushed %d bytes to client (avail: %d)\n", (int)len, (int)to_client_len);
92 #endif
94 return len;
97 void
98 doit (void)
100 /* Server stuff. */
101 gnutls_anon_server_credentials_t s_anoncred;
102 const gnutls_datum_t p3 = { (void *) pkcs3, strlen (pkcs3) };
103 static gnutls_dh_params_t dh_params;
104 gnutls_session_t server;
105 int sret, cret;
106 /* Client stuff. */
107 gnutls_anon_client_credentials_t c_anoncred;
108 gnutls_session_t client;
109 /* Need to enable anonymous KX specifically. */
110 char buffer[MAX_BUF + 1];
111 ssize_t ns;
112 int ret, transferred = 0, msglen;
114 /* General init. */
115 gnutls_global_init ();
116 gnutls_global_set_log_function (tls_log_func);
117 if (debug)
118 gnutls_global_set_log_level (99);
120 /* Init server */
121 gnutls_anon_allocate_server_credentials (&s_anoncred);
122 gnutls_dh_params_init (&dh_params);
123 gnutls_dh_params_import_pkcs3 (dh_params, &p3, GNUTLS_X509_FMT_PEM);
124 gnutls_anon_set_server_dh_params (s_anoncred, dh_params);
125 gnutls_init (&server, GNUTLS_SERVER|GNUTLS_DATAGRAM|GNUTLS_NONBLOCK);
126 ret = gnutls_priority_set_direct (server, "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-DH", NULL);
127 if (ret < 0)
128 exit(1);
129 gnutls_credentials_set (server, GNUTLS_CRD_ANON, s_anoncred);
130 gnutls_dh_set_prime_bits (server, 1024);
131 gnutls_transport_set_push_function (server, server_push_300);
132 gnutls_transport_set_pull_function (server, server_pull);
133 gnutls_transport_set_pull_timeout_function (server, server_pull_timeout_func);
134 gnutls_transport_set_ptr (server, (gnutls_transport_ptr_t)server);
136 /* Init client */
137 gnutls_anon_allocate_client_credentials (&c_anoncred);
138 gnutls_init (&client, GNUTLS_CLIENT|GNUTLS_DATAGRAM|GNUTLS_NONBLOCK);
139 cret = gnutls_priority_set_direct (client, "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-DH", NULL);
140 if (cret < 0)
141 exit(1);
142 gnutls_credentials_set (client, GNUTLS_CRD_ANON, c_anoncred);
143 gnutls_transport_set_push_function (client, client_push_300);
144 gnutls_transport_set_pull_function (client, client_pull);
145 gnutls_transport_set_pull_timeout_function (client, client_pull_timeout_func);
146 gnutls_transport_set_ptr (client, (gnutls_transport_ptr_t)client);
148 handshake = 1;
149 HANDSHAKE_DTLS(client, server);
151 handshake = 0;
152 if (debug)
153 success ("Handshake established\n");
157 ret = gnutls_record_send (client, MSG, strlen (MSG));
159 while(ret == GNUTLS_E_AGAIN);
160 //success ("client: sent %d\n", ns);
162 msglen = strlen(MSG);
163 TRANSFER(client, server, MSG, msglen, buffer, MAX_BUF);
165 if (debug)
166 fputs ("\n", stdout);
168 gnutls_bye (client, GNUTLS_SHUT_WR);
169 gnutls_bye (server, GNUTLS_SHUT_WR);
171 gnutls_deinit (client);
172 gnutls_deinit (server);
174 gnutls_anon_free_client_credentials (c_anoncred);
175 gnutls_anon_free_server_credentials (s_anoncred);
177 gnutls_dh_params_deinit (dh_params);
179 gnutls_global_deinit ();