summaryrefslogtreecommitdiffstats
path: root/tests/eagain-common.h
blob: 6b168cb684c4e2a2dc85ddff19339af0d295afe5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#ifndef GNUTLS_TESTS_EAGAIN_COMMON_H
#define GNUTLS_TESTS_EAGAIN_COMMON_H

#include <errno.h>
#include <time.h>
#include <stdio.h>

#define min(x,y) ((x)<(y)?(x):(y))

extern const char *side;

#ifdef USE_CMOCKA
# define failure() fail()
# define client_transfer_failure(r) {fprintf(stderr, "client transfer failure: %s\n", gnutls_strerror(r)); fail();}
# define server_transfer_failure(r) {fprintf(stderr, "server transfer failure: %s\n", gnutls_strerror(r)); fail();}
# define switch_side(str)
#else
# define failure() fail("Handshake failed\n")
# define client_transfer_failure(r) fail("client transfer failure: %s\n", gnutls_strerror(r))
# define server_transfer_failure(r) fail("client transfer failure: %s\n", gnutls_strerror(r))
# define switch_side(str) side = str
#endif

#define HANDSHAKE_EXPECT(c, s, clierr, serverr) \
  sret = cret = GNUTLS_E_AGAIN; \
  do \
    { \
      if (cret == GNUTLS_E_AGAIN) \
	{ \
	  switch_side("client"); \
	  cret = gnutls_handshake (c); \
	  if (cret == GNUTLS_E_INTERRUPTED) cret = GNUTLS_E_AGAIN; \
	} \
      if (sret == GNUTLS_E_AGAIN) \
	{ \
	  switch_side("server"); \
	  sret = gnutls_handshake (s); \
	  if (sret == GNUTLS_E_INTERRUPTED) sret = GNUTLS_E_AGAIN; \
	} \
    } \
  while ((cret == GNUTLS_E_AGAIN || (cret == 0 && sret == GNUTLS_E_AGAIN)) && (sret == GNUTLS_E_AGAIN || (sret == 0 && cret == GNUTLS_E_AGAIN))); \
  if ((clierr != -1 && cret != clierr) || (serverr != -1 && sret != serverr)) \
    { \
      fprintf(stderr, "client[%d]: %s\n", cret, gnutls_strerror(cret)); \
      fprintf(stderr, "server[%d]: %s\n", sret, gnutls_strerror(sret)); \
      failure(); \
    }

#define HANDSHAKE(c, s) \
  HANDSHAKE_EXPECT(c,s,0,0)

#define HANDSHAKE_DTLS_EXPECT(c, s, clierr, serverr) \
  sret = cret = GNUTLS_E_AGAIN; \
  do \
    { \
      if (cret == GNUTLS_E_LARGE_PACKET) \
	{ \
	  unsigned int mtu = gnutls_dtls_get_mtu(s); \
	  gnutls_dtls_set_mtu(s, mtu/2); \
	} \
      if (cret < 0 && gnutls_error_is_fatal(cret) == 0) \
	{ \
	  switch_side("client"); \
	  cret = gnutls_handshake (c); \
	} \
      if (sret == GNUTLS_E_LARGE_PACKET) \
	{ \
	  unsigned int mtu = gnutls_dtls_get_mtu(s); \
	  gnutls_dtls_set_mtu(s, mtu/2); \
	} \
      if (sret < 0 && gnutls_error_is_fatal(sret) == 0) \
	{ \
	  switch_side("server"); \
	  sret = gnutls_handshake (s); \
	} \
    } \
  while (((gnutls_error_is_fatal(cret) == 0 && gnutls_error_is_fatal(sret) == 0)) && (cret < 0 || sret < 0)); \
  if (cret != clierr || sret != serverr) \
    { \
      fprintf(stderr, "client: %s\n", gnutls_strerror(cret)); \
      fprintf(stderr, "server: %s\n", gnutls_strerror(sret)); \
      failure(); \
    }

#define HANDSHAKE_DTLS(c, s) \
  HANDSHAKE_DTLS_EXPECT(c,s,0,0)

#define HANDSHAKE(c, s) \
  HANDSHAKE_EXPECT(c,s,0,0)

#define TRANSFER2(c, s, msg, msglen, buf, buflen, retry_send_with_null) { \
  int _ret; \
  switch_side("client"); \
  _ret = record_send_loop (c, msg, msglen, retry_send_with_null); \
  \
  if (_ret < 0) client_transfer_failure(_ret); \
  \
  do \
    { \
      do \
	{ \
	  switch_side("server"); \
	  _ret = gnutls_record_recv (s, buf, buflen); \
	} \
      while(_ret == GNUTLS_E_AGAIN); \
      if (_ret <= 0) \
	{ \
	  server_transfer_failure(_ret); \
	} \
      else \
	{ \
	  transferred += _ret; \
	} \
      switch_side("server"); \
      _ret = record_send_loop (server, msg, msglen, retry_send_with_null); \
      if (_ret < 0) server_transfer_failure(_ret); \
      do \
	{ \
	  switch_side("client"); \
	  _ret = gnutls_record_recv (client, buf, buflen); \
	} \
      while(_ret == GNUTLS_E_AGAIN); \
      if (_ret <= 0) \
	{ \
	  client_transfer_failure(_ret); \
	} \
      else \
	{ \
	  if (msglen != _ret || memcmp (buf, msg, msglen) != 0) \
	    { \
	      failure(); \
	    } \
	  /* echo back */ \
	  switch_side("client"); \
	  _ret = record_send_loop (client, buf, msglen, retry_send_with_null); \
	  if (_ret < 0) client_transfer_failure(_ret); \
	  transferred += _ret; \
	} \
    } \
  while (transferred < 70000); \
  }

#define EMPTY_BUF(s, c, buf, buflen) \
    { \
      switch_side("client"); int _ret = 0; \
      while((_ret == GNUTLS_E_AGAIN && to_server_len > 0) || to_server_len > 0) \
	{ \
	  switch_side("server"); \
	  _ret = gnutls_record_recv (s, buf, buflen); \
	} \
      if (_ret < 0 && _ret !=GNUTLS_E_AGAIN) \
	{ \
	  server_transfer_failure(_ret); \
	} \
      switch_side("server"); _ret = 0; \
      while((to_client_len > 0 && _ret == GNUTLS_E_AGAIN) || to_client_len > 0) \
	{ \
	  switch_side("client"); \
	  _ret = gnutls_record_recv (client, buf, buflen); \
	} \
      if (_ret < 0 && _ret !=GNUTLS_E_AGAIN) \
	{ \
	  client_transfer_failure(_ret); \
	} \
    }

#define TRANSFER(c, s, msg, msglen, buf, buflen) \
  TRANSFER2(c, s, msg, msglen, buf, buflen, 0); \
  TRANSFER2(c, s, msg, msglen, buf, buflen, 1)

static char to_server[64 * 1024];
static size_t to_server_len = 0;

static char to_client[64 * 1024];
static size_t to_client_len = 0;


#ifdef RANDOMIZE
#define RETURN_RND_EAGAIN(session) \
  unsigned int rnd = time(0); \
  if (rnd++ % 3 == 0) \
    { \
      gnutls_transport_set_errno (session, EAGAIN); \
      return -1; \
    }
#else
#define RETURN_RND_EAGAIN(session)
#endif

#ifndef IGNORE_PUSH
static ssize_t
client_push(gnutls_transport_ptr_t tr, const void *data, size_t len)
{
	size_t newlen;
	RETURN_RND_EAGAIN(tr);

	len = min(len, sizeof(to_server) - to_server_len);

	newlen = to_server_len + len;
	memcpy(to_server + to_server_len, data, len);
	to_server_len = newlen;
#ifdef EAGAIN_DEBUG
	fprintf(stderr, "eagain: pushed %d bytes to server (avail: %d)\n",
		(int) len, (int) to_server_len);
#endif
	return len;
}

#endif

static ssize_t
client_pull(gnutls_transport_ptr_t tr, void *data, size_t len)
{
	RETURN_RND_EAGAIN(tr);

	if (to_client_len == 0) {
#ifdef EAGAIN_DEBUG
		fprintf(stderr,
			"eagain: Not enough data by server (asked for: %d, have: %d)\n",
			(int) len, (int) to_client_len);
#endif
		gnutls_transport_set_errno((gnutls_session_t) tr, EAGAIN);
		return -1;
	}

	len = min(len, to_client_len);

	memcpy(data, to_client, len);

	memmove(to_client, to_client + len, to_client_len - len);
	to_client_len -= len;
#ifdef EAGAIN_DEBUG
	fprintf(stderr, "eagain: pulled %d bytes by client (avail: %d)\n",
		(int) len, (int) to_client_len);
#endif
	return len;
}

static ssize_t
server_pull(gnutls_transport_ptr_t tr, void *data, size_t len)
{
	//success ("server_pull len %d has %d\n", len, to_server_len);
	RETURN_RND_EAGAIN(tr);

	if (to_server_len == 0) {
#ifdef EAGAIN_DEBUG
		fprintf(stderr,
			"eagain: Not enough data by client (asked for: %d, have: %d)\n",
			(int) len, (int) to_server_len);
#endif
		gnutls_transport_set_errno((gnutls_session_t) tr, EAGAIN);
		return -1;
	}

	len = min(len, to_server_len);
#ifdef EAGAIN_DEBUG
	fprintf(stderr, "eagain: pulled %d bytes by server (avail: %d)\n",
		(int) len, (int) to_server_len);
#endif
	memcpy(data, to_server, len);

	memmove(to_server, to_server + len, to_server_len - len);
	to_server_len -= len;

	return len;
}

#ifndef IGNORE_PUSH
static ssize_t
server_push(gnutls_transport_ptr_t tr, const void *data, size_t len)
{
	size_t newlen;
	RETURN_RND_EAGAIN(tr);

//  hexprint (data, len);

	len = min(len, sizeof(to_client) - to_client_len);

	newlen = to_client_len + len;
	memcpy(to_client + to_client_len, data, len);
	to_client_len = newlen;
#ifdef EAGAIN_DEBUG
	fprintf(stderr, "eagain: pushed %d bytes to client (avail: %d)\n",
		(int) len, (int) to_client_len);
#endif


#ifdef SERVER_PUSH_ADD
	SERVER_PUSH_ADD
#endif

	return len;
}

#endif

/* inline is used to avoid a gcc warning if used in mini-eagain */
inline static int server_pull_timeout_func(gnutls_transport_ptr_t ptr,
					   unsigned int ms)
{
	int ret;

	if (to_server_len > 0)
		ret = 1;	/* available data */
	else
		ret = 0;	/* timeout */

#ifdef EAGAIN_DEBUG
	fprintf(stderr,
		"eagain: server_pull_timeout: %d (avail: cli %d, serv %d)\n",
		ret, (int) to_client_len, (int) to_server_len);
#endif

	return ret;
}

inline static int client_pull_timeout_func(gnutls_transport_ptr_t ptr,
					   unsigned int ms)
{
	int ret;

	if (to_client_len > 0)
		ret = 1;
	else
		ret = 0;

#ifdef EAGAIN_DEBUG
	fprintf(stderr,
		"eagain: client_pull_timeout: %d (avail: cli %d, serv %d)\n",
		ret, (int) to_client_len, (int) to_server_len);
#endif

	return ret;
}

inline static void reset_buffers(void)
{
	to_server_len = 0;
	to_client_len = 0;
}

inline static int record_send_loop(gnutls_session_t session,
				   const void *data, size_t sizeofdata,
				   int use_null_on_retry)
{
	int ret;
	const void *retry_data;
	size_t retry_sizeofdata;

	if (use_null_on_retry) {
		retry_data = 0;
		retry_sizeofdata = 0;
	} else {
		retry_data = data;
		retry_sizeofdata = sizeofdata;
	}

	ret = gnutls_record_send(session, data, sizeofdata);
	while (ret == GNUTLS_E_AGAIN) {
		ret =
		    gnutls_record_send(session, retry_data,
					retry_sizeofdata);
	}

	return ret;
}

#endif /* GNUTLS_TESTS_EAGAIN_COMMON_H */