summaryrefslogtreecommitdiff
path: root/lib/tevent/echo_server.c
blob: a1da0d801bc0f188a07338ab3c0141f6ca065521 (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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/**
 ** NOTE! The following liberal license applies to this sample file only.
 ** This does NOT imply that all of Samba is released under this license.
 **
 ** This file is meant as a starting point for libtevent users to be used
 ** in any program linking against the LGPL licensed libtevent.
 **/

/*
 * This file is being made available by the Samba Team under the following
 * license:
 *
 * Permission to use, copy, modify, and distribute this sample file for any
 * purpose is hereby granted without fee.
 *
 * This work is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <unistd.h>
#include "tevent.h"
#include "talloc.h"

/**
 * @brief Helper function to get a useful unix error from tevent_req
 */

static bool tevent_req_is_unix_error(struct tevent_req *req, int *perrno)
{
	enum tevent_req_state state;
	uint64_t err;

	if (!tevent_req_is_error(req, &state, &err)) {
		return false;
	}
	switch (state) {
	case TEVENT_REQ_TIMED_OUT:
		*perrno = ETIMEDOUT;
		break;
	case TEVENT_REQ_NO_MEMORY:
		*perrno = ENOMEM;
		break;
	case TEVENT_REQ_USER_ERROR:
		*perrno = err;
		break;
	default:
		*perrno = EINVAL;
		break;
	}
	return true;
}

/**
 * @brief Wrapper around accept(2)
 */

struct accept_state {
	struct tevent_fd *fde;
	int listen_sock;
	socklen_t addrlen;
	struct sockaddr addr;
	int sock;
};

static void accept_handler(struct tevent_context *ev, struct tevent_fd *fde,
			   uint16_t flags, void *private_data);

static struct tevent_req *accept_send(TALLOC_CTX *mem_ctx,
				      struct tevent_context *ev,
				      int listen_sock)
{
	struct tevent_req *req;
	struct accept_state *state;

	req = tevent_req_create(mem_ctx, &state, struct accept_state);
	if (req == NULL) {
		return NULL;
	}

	state->listen_sock = listen_sock;

	state->fde = tevent_add_fd(ev, state, listen_sock, TEVENT_FD_READ,
				   accept_handler, req);
	if (tevent_req_nomem(state->fde, req)) {
		return tevent_req_post(req, ev);
	}
	return req;
}

static void accept_handler(struct tevent_context *ev, struct tevent_fd *fde,
			   uint16_t flags, void *private_data)
{
	struct tevent_req *req = talloc_get_type_abort(
		private_data, struct tevent_req);
	struct accept_state *state = tevent_req_data(req, struct accept_state);
	int ret;

	TALLOC_FREE(state->fde);

	if ((flags & TEVENT_FD_READ) == 0) {
		tevent_req_error(req, EIO);
		return;
	}
	state->addrlen = sizeof(state->addr);

	ret = accept(state->listen_sock, &state->addr, &state->addrlen);
	if (ret == -1) {
		tevent_req_error(req, errno);
		return;
	}
	state->sock = ret;
	tevent_req_done(req);
}

static int accept_recv(struct tevent_req *req, struct sockaddr *paddr,
		       socklen_t *paddrlen, int *perr)
{
	struct accept_state *state = tevent_req_data(req, struct accept_state);
	int err;

	if (tevent_req_is_unix_error(req, &err)) {
		if (perr != NULL) {
			*perr = err;
		}
		return -1;
	}
	if (paddr != NULL) {
		*paddr = state->addr;
	}
	if (paddrlen != NULL) {
		*paddrlen = state->addrlen;
	}
	return state->sock;
}

/**
 * @brief Wrapper around read(2)
 */

struct read_state {
	struct tevent_fd *fde;
	int fd;
	void *buf;
	size_t count;

	ssize_t nread;
};

static void read_handler(struct tevent_context *ev, struct tevent_fd *fde,
			 uint16_t flags, void *private_data);

static struct tevent_req *read_send(TALLOC_CTX *mem_ctx,
				    struct tevent_context *ev,
				    int fd, void *buf, size_t count)
{
	struct tevent_req *req;
	struct read_state *state;

	req = tevent_req_create(mem_ctx, &state, struct read_state);
	if (req == NULL) {
		return NULL;
	}

	state->fd = fd;
	state->buf = buf;
	state->count = count;

	state->fde = tevent_add_fd(ev, state, fd, TEVENT_FD_READ,
				   read_handler, req);
	if (tevent_req_nomem(state->fde, req)) {
		return tevent_req_post(req, ev);
	}
	return req;
}

static void read_handler(struct tevent_context *ev, struct tevent_fd *fde,
			 uint16_t flags, void *private_data)
{
	struct tevent_req *req = talloc_get_type_abort(
		private_data, struct tevent_req);
	struct read_state *state = tevent_req_data(req, struct read_state);
	ssize_t ret;

	TALLOC_FREE(state->fde);

	if ((flags & TEVENT_FD_READ) == 0) {
		tevent_req_error(req, EIO);
		return;
	}

	ret = read(state->fd, state->buf, state->count);
	if (ret == -1) {
		tevent_req_error(req, errno);
		return;
	}
	state->nread = ret;
	tevent_req_done(req);
}

static ssize_t read_recv(struct tevent_req *req, int *perr)
{
	struct read_state *state = tevent_req_data(req, struct read_state);
	int err;

	if (tevent_req_is_unix_error(req, &err)) {
		if (perr != NULL) {
			*perr = err;
		}
		return -1;
	}
	return state->nread;
}

/**
 * @brief Wrapper around write(2)
 */

struct write_state {
	struct tevent_fd *fde;
	int fd;
	const void *buf;
	size_t count;

	ssize_t nwritten;
};

static void write_handler(struct tevent_context *ev, struct tevent_fd *fde,
			 uint16_t flags, void *private_data);

static struct tevent_req *write_send(TALLOC_CTX *mem_ctx,
				    struct tevent_context *ev,
				    int fd, const void *buf, size_t count)
{
	struct tevent_req *req;
	struct write_state *state;

	req = tevent_req_create(mem_ctx, &state, struct write_state);
	if (req == NULL) {
		return NULL;
	}

	state->fd = fd;
	state->buf = buf;
	state->count = count;

	state->fde = tevent_add_fd(ev, state, fd, TEVENT_FD_WRITE,
				   write_handler, req);
	if (tevent_req_nomem(state->fde, req)) {
		return tevent_req_post(req, ev);
	}
	return req;
}

static void write_handler(struct tevent_context *ev, struct tevent_fd *fde,
			 uint16_t flags, void *private_data)
{
	struct tevent_req *req = talloc_get_type_abort(
		private_data, struct tevent_req);
	struct write_state *state = tevent_req_data(req, struct write_state);
	ssize_t ret;

	TALLOC_FREE(state->fde);

	if ((flags & TEVENT_FD_WRITE) == 0) {
		tevent_req_error(req, EIO);
		return;
	}

	ret = write(state->fd, state->buf, state->count);
	if (ret == -1) {
		tevent_req_error(req, errno);
		return;
	}
	state->nwritten = ret;
	tevent_req_done(req);
}

static ssize_t write_recv(struct tevent_req *req, int *perr)
{
	struct write_state *state = tevent_req_data(req, struct write_state);
	int err;

	if (tevent_req_is_unix_error(req, &err)) {
		if (perr != NULL) {
			*perr = err;
		}
		return -1;
	}
	return state->nwritten;
}

/**
 * @brief Wrapper function that deals with short writes
 */

struct writeall_state {
	struct tevent_context *ev;
	int fd;
	const void *buf;
	size_t count;
	size_t nwritten;
};

static void writeall_done(struct tevent_req *subreq);

static struct tevent_req *writeall_send(TALLOC_CTX *mem_ctx,
					struct tevent_context *ev,
					int fd, const void *buf, size_t count)
{
	struct tevent_req *req, *subreq;
	struct writeall_state *state;

	req = tevent_req_create(mem_ctx, &state, struct writeall_state);
	if (req == NULL) {
		return NULL;
	}
	state->ev = ev;
	state->fd = fd;
	state->buf = buf;
	state->count = count;
	state->nwritten = 0;

	subreq = write_send(state, state->ev, state->fd,
			    ((char *)state->buf)+state->nwritten,
			    state->count - state->nwritten);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, writeall_done, req);
	return req;
}

static void writeall_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct writeall_state *state = tevent_req_data(
		req, struct writeall_state);
	ssize_t nwritten;
	int err = 0;

	nwritten = write_recv(subreq, &err);
	TALLOC_FREE(subreq);
	if (nwritten == -1) {
		tevent_req_error(req, err);
		return;
	}

	state->nwritten += nwritten;

	if (state->nwritten < state->count) {
		subreq = write_send(state, state->ev, state->fd,
				    ((char *)state->buf)+state->nwritten,
				    state->count - state->nwritten);
		if (tevent_req_nomem(subreq, req)) {
			return;
		}
		tevent_req_set_callback(subreq, writeall_done, req);
		return;
	}
	tevent_req_done(req);
}

static ssize_t writeall_recv(struct tevent_req *req, int *perr)
{
	struct writeall_state *state = tevent_req_data(
		req, struct writeall_state);
	int err;

	if (tevent_req_is_unix_error(req, &err)) {
		if (perr != NULL) {
			*perr = err;
		}
		return -1;
	}
	return state->nwritten;
}

/**
 * @brief Async echo handler code dealing with one client
 */

struct echo_state {
	struct tevent_context *ev;
	int fd;
	uint8_t *buf;
};

static int echo_state_destructor(struct echo_state *s);
static void echo_read_done(struct tevent_req *subreq);
static void echo_writeall_done(struct tevent_req *subreq);

static struct tevent_req *echo_send(TALLOC_CTX *mem_ctx,
				    struct tevent_context *ev,
				    int fd, size_t bufsize)
{
	struct tevent_req *req, *subreq;
	struct echo_state *state;

	req = tevent_req_create(mem_ctx, &state, struct echo_state);
	if (req == NULL) {
		return NULL;
	}
	state->ev = ev;
	state->fd = fd;

	talloc_set_destructor(state, echo_state_destructor);

	state->buf = talloc_array(state, uint8_t, bufsize);
	if (tevent_req_nomem(state->buf, req)) {
		return tevent_req_post(req, ev);
	}

	subreq = read_send(state, state->ev, state->fd,
			   state->buf, talloc_get_size(state->buf));
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, echo_read_done, req);
	return req;
}

static int echo_state_destructor(struct echo_state *s)
{
	if (s->fd != -1) {
		printf("Closing client fd %d\n", s->fd);
		close(s->fd);
		s->fd = -1;
	}
	return 0;
}

static void echo_read_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct echo_state *state = tevent_req_data(
		req, struct echo_state);
	ssize_t nread;
	int err;

	nread = read_recv(subreq, &err);
	TALLOC_FREE(subreq);
	if (nread == -1) {
		tevent_req_error(req, err);
		return;
	}
	if (nread == 0) {
		tevent_req_done(req);
		return;
	}

	subreq = writeall_send(state, state->ev, state->fd, state->buf, nread);
	if (tevent_req_nomem(subreq, req)) {
		return;
	}
	tevent_req_set_callback(subreq, echo_writeall_done, req);
}

static void echo_writeall_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct echo_state *state = tevent_req_data(
		req, struct echo_state);
	ssize_t nwritten;
	int err;

	nwritten = writeall_recv(subreq, &err);
	TALLOC_FREE(subreq);
	if (nwritten == -1) {
		if (err == EPIPE) {
			tevent_req_done(req);
			return;
		}
		tevent_req_error(req, err);
		return;
	}

	subreq = read_send(state, state->ev, state->fd,
			   state->buf, talloc_get_size(state->buf));
	if (tevent_req_nomem(subreq, req)) {
		return;
	}
	tevent_req_set_callback(subreq, echo_read_done, req);
}

static bool echo_recv(struct tevent_req *req, int *perr)
{
	int err;

	if (tevent_req_is_unix_error(req, &err)) {
		*perr = err;
		return false;
	}
	return true;
}

/**
 * @brief Full echo handler code accepting and handling clients
 */

struct echo_server_state {
	struct tevent_context *ev;
	int listen_sock;
};

static void echo_server_accepted(struct tevent_req *subreq);
static void echo_server_client_done(struct tevent_req *subreq);

static struct tevent_req *echo_server_send(TALLOC_CTX *mem_ctx,
					   struct tevent_context *ev,
					   int listen_sock)
{
	struct tevent_req *req, *subreq;
	struct echo_server_state *state;

	req = tevent_req_create(mem_ctx, &state,
				struct echo_server_state);
	if (req == NULL) {
		return NULL;
	}
	state->ev = ev;
	state->listen_sock = listen_sock;

	subreq = accept_send(state, state->ev, state->listen_sock);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, echo_server_accepted, req);
	return req;
}

static void echo_server_accepted(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct echo_server_state *state = tevent_req_data(
		req, struct echo_server_state);
	int sock, err;

	sock = accept_recv(subreq, NULL, NULL, &err);
	TALLOC_FREE(subreq);
	if (sock == -1) {
		tevent_req_error(req, err);
		return;
	}

	printf("new client fd %d\n", sock);

	subreq = echo_send(state, state->ev, sock, 100);
	if (tevent_req_nomem(subreq, req)) {
		return;
	}
	tevent_req_set_callback(subreq, echo_server_client_done, req);

	subreq = accept_send(state, state->ev, state->listen_sock);
	if (tevent_req_nomem(subreq, req)) {
		return;
	}
	tevent_req_set_callback(subreq, echo_server_accepted, req);
}

static void echo_server_client_done(struct tevent_req *subreq)
{
	bool ret;
	int err;

	ret = echo_recv(subreq, &err);
	TALLOC_FREE(subreq);

	if (ret) {
		printf("Client done\n");
	} else {
		printf("Client failed: %s\n", strerror(err));
	}
}

static bool echo_server_recv(struct tevent_req *req, int *perr)
{
	int err;

	if (tevent_req_is_unix_error(req, &err)) {
		*perr = err;
		return false;
	}
	return true;
}

int main(int argc, const char **argv)
{
	int ret, port, listen_sock, err;
	struct tevent_context *ev;
	struct sockaddr_in addr;
	struct tevent_req *req;
	bool result;

	if (argc != 2) {
		fprintf(stderr, "Usage: %s <port>\n", argv[0]);
		exit(1);
	}

	port = atoi(argv[1]);

	printf("listening on port %d\n", port);

	listen_sock = socket(AF_INET, SOCK_STREAM, 0);

	if (listen_sock == -1) {
		perror("socket() failed");
		exit(1);
	}

	memset(&addr, 0, sizeof(addr));

	addr.sin_family = AF_INET;
	addr.sin_port = htons(port);

	ret = bind(listen_sock, (struct sockaddr *)&addr, sizeof(addr));
	if (ret == -1) {
		perror("bind() failed");
		exit(1);
	}

	ret = listen(listen_sock, 5);
	if (ret == -1) {
		perror("listen() failed");
		exit(1);
	}

	ev = tevent_context_init(NULL);
	if (ev == NULL) {
		fprintf(stderr, "tevent_context_init failed\n");
		exit(1);
	}

	req = echo_server_send(ev, ev, listen_sock);
	if (req == NULL) {
		fprintf(stderr, "echo_server_send failed\n");
		exit(1);
	}

	if (!tevent_req_poll(req, ev)) {
		perror("tevent_req_poll() failed");
		exit(1);
	}

	result = echo_server_recv(req, &err);
	TALLOC_FREE(req);
	if (!result) {
		fprintf(stderr, "echo_server failed: %s\n", strerror(err));
		exit(1);
	}

	return 0;
}