1 #define min(x,y) ((x)<(y)?(x):(y))
3 extern const char* side
;
5 #define HANDSHAKE_EXPECT(c, s, clierr, serverr) \
6 sret = cret = GNUTLS_E_AGAIN; \
9 if (cret == GNUTLS_E_AGAIN) \
12 cret = gnutls_handshake (c); \
14 if (sret == GNUTLS_E_AGAIN) \
17 sret = gnutls_handshake (s); \
20 while ((cret == GNUTLS_E_AGAIN || (cret == 0 && sret == GNUTLS_E_AGAIN)) && (sret == GNUTLS_E_AGAIN || (sret == 0 && cret == GNUTLS_E_AGAIN))); \
21 if (cret != clierr || sret != serverr) \
23 fprintf(stderr, "client: %s\n", gnutls_strerror(cret)); \
24 fprintf(stderr, "server: %s\n", gnutls_strerror(sret)); \
25 fail("Handshake failed\n"); \
29 #define HANDSHAKE(c, s) \
30 HANDSHAKE_EXPECT(c,s,0,0)
32 #define HANDSHAKE_DTLS_EXPECT(c, s, clierr, serverr) \
33 sret = cret = GNUTLS_E_LARGE_PACKET; \
36 if (cret == GNUTLS_E_LARGE_PACKET) \
38 unsigned int mtu = gnutls_dtls_get_mtu(s); \
39 gnutls_dtls_set_mtu(s, mtu/2); \
41 if (cret < 0 && gnutls_error_is_fatal(cret) == 0) \
44 cret = gnutls_handshake (c); \
46 if (sret == GNUTLS_E_LARGE_PACKET) \
48 unsigned int mtu = gnutls_dtls_get_mtu(s); \
49 gnutls_dtls_set_mtu(s, mtu/2); \
51 if (sret < 0 && gnutls_error_is_fatal(sret) == 0) \
54 sret = gnutls_handshake (s); \
57 while (((gnutls_error_is_fatal(cret) == 0 && gnutls_error_is_fatal(sret) == 0)) && (cret < 0 || sret < 0)); \
58 if (cret != clierr || sret != serverr) \
60 fprintf(stderr, "client: %s\n", gnutls_strerror(cret)); \
61 fprintf(stderr, "server: %s\n", gnutls_strerror(sret)); \
62 fail("Handshake failed\n"); \
66 #define HANDSHAKE_DTLS(c, s) \
67 HANDSHAKE_DTLS_EXPECT(c,s,0,0)
69 #define HANDSHAKE(c, s) \
70 HANDSHAKE_EXPECT(c,s,0,0)
72 #define TRANSFER(c, s, msg, msglen, buf, buflen) \
76 ret = gnutls_record_send (c, msg, msglen); \
78 while(ret == GNUTLS_E_AGAIN); \
80 if (ret < 0) fail ("client send error: %s\n", gnutls_strerror (ret)); \
87 ret = gnutls_record_recv (s, buf, buflen); \
89 while(ret == GNUTLS_E_AGAIN); \
91 fail ("server: didn't receive any data\n"); \
94 fail ("server: error: %s\n", gnutls_strerror (ret)); \
103 ns = gnutls_record_send (server, msg, msglen); \
105 while (ns == GNUTLS_E_AGAIN); \
106 if (ns < 0) fail ("server send error: %s\n", gnutls_strerror (ret)); \
110 ret = gnutls_record_recv (client, buf, buflen); \
112 while(ret == GNUTLS_E_AGAIN); \
115 fail ("client: Peer has closed the TLS connection\n"); \
120 fputs ("!", stdout); \
121 fail ("client: Error: %s\n", gnutls_strerror (ret)); \
125 if (msglen != ret || memcmp (buf, msg, msglen) != 0) \
127 fail ("client: Transmitted data do not match\n"); \
133 ns = gnutls_record_send (client, buf, msglen); \
135 while (ns == GNUTLS_E_AGAIN); \
136 if (ns < 0) fail ("client send error: %s\n", gnutls_strerror (ret)); \
137 transferred += ret; \
139 fputs (".", stdout); \
142 while (transferred < 70000)
144 static char to_server
[64*1024];
145 static size_t to_server_len
= 0;
147 static char to_client
[64*1024];
148 static size_t to_client_len
= 0;
151 #define RETURN_RND_EAGAIN(session) \
152 static unsigned char rnd = 0; \
153 if (rnd++ % 2 == 0) \
155 gnutls_transport_set_errno (session, EAGAIN); \
159 #define RETURN_RND_EAGAIN(session)
164 client_push (gnutls_transport_ptr_t tr
, const void *data
, size_t len
)
167 RETURN_RND_EAGAIN(tr
);
169 len
= min(len
, sizeof(to_server
)-to_server_len
);
171 newlen
= to_server_len
+ len
;
172 memcpy (to_server
+ to_server_len
, data
, len
);
173 to_server_len
= newlen
;
175 fprintf(stderr
, "eagain: pushed %d bytes to server (avail: %d)\n", (int)len
, (int)to_server_len
);
183 client_pull (gnutls_transport_ptr_t tr
, void *data
, size_t len
)
185 RETURN_RND_EAGAIN(tr
);
187 if (to_client_len
== 0)
190 fprintf(stderr
, "eagain: Not enough data by server (asked for: %d, have: %d)\n", (int)len
, (int)to_client_len
);
192 gnutls_transport_set_errno ((gnutls_session_t
)tr
, EAGAIN
);
196 len
= min(len
, to_client_len
);
198 memcpy (data
, to_client
, len
);
200 memmove (to_client
, to_client
+ len
, to_client_len
- len
);
201 to_client_len
-= len
;
203 fprintf(stderr
, "eagain: pulled %d bytes by client (avail: %d)\n", (int)len
, (int)to_client_len
);
209 server_pull (gnutls_transport_ptr_t tr
, void *data
, size_t len
)
211 //success ("server_pull len %d has %d\n", len, to_server_len);
212 RETURN_RND_EAGAIN(tr
);
214 if (to_server_len
== 0)
217 fprintf(stderr
, "eagain: Not enough data by client (asked for: %d, have: %d)\n", (int)len
, (int)to_server_len
);
219 gnutls_transport_set_errno ((gnutls_session_t
)tr
, EAGAIN
);
223 len
= min(len
, to_server_len
);
225 fprintf(stderr
, "eagain: pulled %d bytes by server (avail: %d)\n", (int)len
, (int)to_server_len
);
227 memcpy (data
, to_server
, len
);
229 memmove (to_server
, to_server
+ len
, to_server_len
- len
);
230 to_server_len
-= len
;
237 server_push (gnutls_transport_ptr_t tr
, const void *data
, size_t len
)
240 RETURN_RND_EAGAIN(tr
);
242 // hexprint (data, len);
244 len
= min(len
, sizeof(to_client
)-to_client_len
);
246 newlen
= to_client_len
+ len
;
247 memcpy (to_client
+ to_client_len
, data
, len
);
248 to_client_len
= newlen
;
250 fprintf(stderr
, "eagain: pushed %d bytes to client (avail: %d)\n", (int)len
, (int)to_client_len
);
258 /* inline is used to avoid a gcc warning if used in mini-eagain */
259 inline static int server_pull_timeout_func(gnutls_transport_ptr_t ptr
, unsigned int ms
)
263 if (to_server_len
> 0)
264 ret
= 1; /* available data */
266 ret
= 0; /* timeout */
269 fprintf(stderr
, "eagain: server_pull_timeout: %d (avail: cli %d, serv %d)\n", ret
, (int)to_client_len
, (int)to_server_len
);
275 inline static int client_pull_timeout_func(gnutls_transport_ptr_t ptr
, unsigned int ms
)
279 if (to_client_len
> 0)
285 fprintf(stderr
, "eagain: client_pull_timeout: %d (avail: cli %d, serv %d)\n", ret
, (int)to_client_len
, (int)to_server_len
);
291 inline static void reset_buffers(void)