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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
|
/*
Unix SMB/CIFS implementation.
async socket syscalls
Copyright (C) Volker Lendecke 2008
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program 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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
/**
* Discriminator for async_syscall_state
*/
enum async_syscall_type {
ASYNC_SYSCALL_SEND,
ASYNC_SYSCALL_SENDALL,
ASYNC_SYSCALL_RECV,
ASYNC_SYSCALL_RECVALL,
ASYNC_SYSCALL_CONNECT
};
/**
* Holder for syscall arguments and the result
*/
struct async_syscall_state {
enum async_syscall_type syscall_type;
struct fd_event *fde;
union {
struct param_send {
int fd;
const void *buffer;
size_t length;
int flags;
} param_send;
struct param_sendall {
int fd;
const void *buffer;
size_t length;
int flags;
size_t sent;
} param_sendall;
struct param_recv {
int fd;
void *buffer;
size_t length;
int flags;
} param_recv;
struct param_recvall {
int fd;
void *buffer;
size_t length;
int flags;
size_t received;
} param_recvall;
struct param_connect {
/**
* connect needs to be done on a nonblocking
* socket. Keep the old flags around
*/
long old_sockflags;
int fd;
const struct sockaddr *address;
socklen_t address_len;
} param_connect;
} param;
union {
ssize_t result_ssize_t;
size_t result_size_t;
int result_int;
} result;
int sys_errno;
};
/**
* @brief Create a new async syscall req
* @param[in] mem_ctx The memory context to hang the result off
* @param[in] ev The event context to work from
* @param[in] type Which syscall will this be
* @param[in] pstate Where to put the newly created private_data state
* @retval The new request
*
* This is a helper function to prepare a new struct async_req with an
* associated struct async_syscall_state. The async_syscall_state will be put
* into the async_req as private_data.
*/
static struct async_req *async_syscall_new(TALLOC_CTX *mem_ctx,
struct event_context *ev,
enum async_syscall_type type,
struct async_syscall_state **pstate)
{
struct async_req *result;
struct async_syscall_state *state;
result = async_req_new(mem_ctx);
if (result == NULL) {
return NULL;
}
state = talloc(result, struct async_syscall_state);
if (state == NULL) {
TALLOC_FREE(result);
return NULL;
}
state->syscall_type = type;
result->private_data = state;
*pstate = state;
return result;
}
/**
* @brief Create a new async syscall req based on a fd
* @param[in] mem_ctx The memory context to hang the result off
* @param[in] ev The event context to work from
* @param[in] type Which syscall will this be
* @param[in] fd The file descriptor we work on
* @param[in] fde_flags EVENT_FD_READ/WRITE -- what are we interested in?
* @param[in] fde_cb The callback function for the file descriptor event
* @param[in] pstate Where to put the newly created private_data state
* @retval The new request
*
* This is a helper function to prepare a new struct async_req with an
* associated struct async_syscall_state and an associated file descriptor
* event.
*/
static struct async_req *async_fde_syscall_new(
TALLOC_CTX *mem_ctx,
struct event_context *ev,
enum async_syscall_type type,
int fd,
uint16_t fde_flags,
void (*fde_cb)(struct event_context *ev,
struct fd_event *fde, uint16_t flags,
void *priv),
struct async_syscall_state **pstate)
{
struct async_req *result;
struct async_syscall_state *state;
result = async_syscall_new(mem_ctx, ev, type, &state);
if (result == NULL) {
return NULL;
}
state->fde = event_add_fd(ev, state, fd, fde_flags, fde_cb, result);
if (state->fde == NULL) {
TALLOC_FREE(result);
return NULL;
}
*pstate = state;
return result;
}
/**
* Retrieve a ssize_t typed result from an async syscall
* @param[in] req The syscall that has just finished
* @param[out] perrno Where to put the syscall's errno
* @retval The return value from the asynchronously called syscall
*/
ssize_t async_syscall_result_ssize_t(struct async_req *req, int *perrno)
{
struct async_syscall_state *state = talloc_get_type_abort(
req->private_data, struct async_syscall_state);
*perrno = state->sys_errno;
return state->result.result_ssize_t;
}
/**
* Retrieve a size_t typed result from an async syscall
* @param[in] req The syscall that has just finished
* @param[out] perrno Where to put the syscall's errno
* @retval The return value from the asynchronously called syscall
*/
size_t async_syscall_result_size_t(struct async_req *req, int *perrno)
{
struct async_syscall_state *state = talloc_get_type_abort(
req->private_data, struct async_syscall_state);
*perrno = state->sys_errno;
return state->result.result_size_t;
}
/**
* Retrieve a int typed result from an async syscall
* @param[in] req The syscall that has just finished
* @param[out] perrno Where to put the syscall's errno
* @retval The return value from the asynchronously called syscall
*/
int async_syscall_result_int(struct async_req *req, int *perrno)
{
struct async_syscall_state *state = talloc_get_type_abort(
req->private_data, struct async_syscall_state);
*perrno = state->sys_errno;
return state->result.result_int;
}
/**
* fde event handler for the "send" syscall
* @param[in] ev The event context that sent us here
* @param[in] fde The file descriptor event associated with the send
* @param[in] flags Can only be EVENT_FD_WRITE here
* @param[in] priv private data, "struct async_req *" in this case
*/
static void async_send_callback(struct event_context *ev,
struct fd_event *fde, uint16_t flags,
void *priv)
{
struct async_req *req = talloc_get_type_abort(
priv, struct async_req);
struct async_syscall_state *state = talloc_get_type_abort(
req->private_data, struct async_syscall_state);
struct param_send *p = &state->param.param_send;
if (state->syscall_type != ASYNC_SYSCALL_SEND) {
async_req_error(req, NT_STATUS_INTERNAL_ERROR);
return;
}
state->result.result_ssize_t = send(p->fd, p->buffer, p->length,
p->flags);
state->sys_errno = errno;
TALLOC_FREE(state->fde);
async_req_done(req);
}
/**
* Async version of send(2)
* @param[in] mem_ctx The memory context to hang the result off
* @param[in] ev The event context to work from
* @param[in] fd The socket to send to
* @param[in] buffer The buffer to send
* @param[in] length How many bytes to send
* @param[in] flags flags passed to send(2)
*
* This function is a direct counterpart of send(2)
*/
struct async_req *async_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
int fd, const void *buffer, size_t length,
int flags)
{
struct async_req *result;
struct async_syscall_state *state;
result = async_fde_syscall_new(
mem_ctx, ev, ASYNC_SYSCALL_SEND,
fd, EVENT_FD_WRITE, async_send_callback,
&state);
if (result == NULL) {
return NULL;
}
state->param.param_send.fd = fd;
state->param.param_send.buffer = buffer;
state->param.param_send.length = length;
state->param.param_send.flags = flags;
return result;
}
/**
* fde event handler for the "sendall" syscall group
* @param[in] ev The event context that sent us here
* @param[in] fde The file descriptor event associated with the send
* @param[in] flags Can only be EVENT_FD_WRITE here
* @param[in] priv private data, "struct async_req *" in this case
*/
static void async_sendall_callback(struct event_context *ev,
struct fd_event *fde, uint16_t flags,
void *priv)
{
struct async_req *req = talloc_get_type_abort(
priv, struct async_req);
struct async_syscall_state *state = talloc_get_type_abort(
req->private_data, struct async_syscall_state);
struct param_sendall *p = &state->param.param_sendall;
if (state->syscall_type != ASYNC_SYSCALL_SENDALL) {
async_req_error(req, NT_STATUS_INTERNAL_ERROR);
return;
}
state->result.result_ssize_t = send(p->fd, (char *)p->buffer + p->sent,
p->length - p->sent, p->flags);
state->sys_errno = errno;
if (state->result.result_ssize_t == -1) {
async_req_error(req, map_nt_error_from_unix(state->sys_errno));
return;
}
if (state->result.result_ssize_t == 0) {
async_req_error(req, NT_STATUS_END_OF_FILE);
return;
}
p->sent += state->result.result_ssize_t;
if (p->sent > p->length) {
async_req_error(req, NT_STATUS_INTERNAL_ERROR);
return;
}
if (p->sent == p->length) {
TALLOC_FREE(state->fde);
async_req_done(req);
}
}
/**
* @brief Send all bytes to a socket
* @param[in] mem_ctx The memory context to hang the result off
* @param[in] ev The event context to work from
* @param[in] fd The socket to send to
* @param[in] buffer The buffer to send
* @param[in] length How many bytes to send
* @param[in] flags flags passed to send(2)
*
* async_sendall calls send(2) as long as it is necessary to send all of the
* "length" bytes
*/
struct async_req *sendall_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
int fd, const void *buffer, size_t length,
int flags)
{
struct async_req *result;
struct async_syscall_state *state;
result = async_fde_syscall_new(
mem_ctx, ev, ASYNC_SYSCALL_SENDALL,
fd, EVENT_FD_WRITE, async_sendall_callback,
&state);
if (result == NULL) {
return NULL;
}
state->param.param_sendall.fd = fd;
state->param.param_sendall.buffer = buffer;
state->param.param_sendall.length = length;
state->param.param_sendall.flags = flags;
state->param.param_sendall.sent = 0;
return result;
}
NTSTATUS sendall_recv(struct async_req *req)
{
return async_req_simple_recv(req);
}
/**
* fde event handler for the "recv" syscall
* @param[in] ev The event context that sent us here
* @param[in] fde The file descriptor event associated with the recv
* @param[in] flags Can only be EVENT_FD_READ here
* @param[in] priv private data, "struct async_req *" in this case
*/
static void async_recv_callback(struct event_context *ev,
struct fd_event *fde, uint16_t flags,
void *priv)
{
struct async_req *req = talloc_get_type_abort(
priv, struct async_req);
struct async_syscall_state *state = talloc_get_type_abort(
req->private_data, struct async_syscall_state);
struct param_recv *p = &state->param.param_recv;
if (state->syscall_type != ASYNC_SYSCALL_RECV) {
async_req_error(req, NT_STATUS_INTERNAL_ERROR);
return;
}
state->result.result_ssize_t = recv(p->fd, p->buffer, p->length,
p->flags);
state->sys_errno = errno;
TALLOC_FREE(state->fde);
async_req_done(req);
}
/**
* Async version of recv(2)
* @param[in] mem_ctx The memory context to hang the result off
* @param[in] ev The event context to work from
* @param[in] fd The socket to recv from
* @param[in] buffer The buffer to recv into
* @param[in] length How many bytes to recv
* @param[in] flags flags passed to recv(2)
*
* This function is a direct counterpart of recv(2)
*/
struct async_req *async_recv(TALLOC_CTX *mem_ctx, struct event_context *ev,
int fd, void *buffer, size_t length,
int flags)
{
struct async_req *result;
struct async_syscall_state *state;
result = async_fde_syscall_new(
mem_ctx, ev, ASYNC_SYSCALL_RECV,
fd, EVENT_FD_READ, async_recv_callback,
&state);
if (result == NULL) {
return NULL;
}
state->param.param_recv.fd = fd;
state->param.param_recv.buffer = buffer;
state->param.param_recv.length = length;
state->param.param_recv.flags = flags;
return result;
}
/**
* fde event handler for the "recvall" syscall group
* @param[in] ev The event context that sent us here
* @param[in] fde The file descriptor event associated with the recv
* @param[in] flags Can only be EVENT_FD_READ here
* @param[in] priv private data, "struct async_req *" in this case
*/
static void async_recvall_callback(struct event_context *ev,
struct fd_event *fde, uint16_t flags,
void *priv)
{
struct async_req *req = talloc_get_type_abort(
priv, struct async_req);
struct async_syscall_state *state = talloc_get_type_abort(
req->private_data, struct async_syscall_state);
struct param_recvall *p = &state->param.param_recvall;
if (state->syscall_type != ASYNC_SYSCALL_RECVALL) {
async_req_error(req, NT_STATUS_INTERNAL_ERROR);
return;
}
state->result.result_ssize_t = recv(p->fd,
(char *)p->buffer + p->received,
p->length - p->received, p->flags);
state->sys_errno = errno;
if (state->result.result_ssize_t == -1) {
async_req_error(req, map_nt_error_from_unix(state->sys_errno));
return;
}
if (state->result.result_ssize_t == 0) {
async_req_error(req, NT_STATUS_END_OF_FILE);
return;
}
p->received += state->result.result_ssize_t;
if (p->received > p->length) {
async_req_error(req, NT_STATUS_INTERNAL_ERROR);
return;
}
if (p->received == p->length) {
TALLOC_FREE(state->fde);
async_req_done(req);
}
}
/**
* Receive a specified number of bytes from a socket
* @param[in] mem_ctx The memory context to hang the result off
* @param[in] ev The event context to work from
* @param[in] fd The socket to recv from
* @param[in] buffer The buffer to recv into
* @param[in] length How many bytes to recv
* @param[in] flags flags passed to recv(2)
*
* async_recvall will call recv(2) until "length" bytes are received
*/
struct async_req *recvall_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
int fd, void *buffer, size_t length,
int flags)
{
struct async_req *result;
struct async_syscall_state *state;
result = async_fde_syscall_new(
mem_ctx, ev, ASYNC_SYSCALL_RECVALL,
fd, EVENT_FD_READ, async_recvall_callback,
&state);
if (result == NULL) {
return NULL;
}
state->param.param_recvall.fd = fd;
state->param.param_recvall.buffer = buffer;
state->param.param_recvall.length = length;
state->param.param_recvall.flags = flags;
state->param.param_recvall.received = 0;
return result;
}
NTSTATUS recvall_recv(struct async_req *req)
{
return async_req_simple_recv(req);
}
/**
* fde event handler for connect(2)
* @param[in] ev The event context that sent us here
* @param[in] fde The file descriptor event associated with the connect
* @param[in] flags Indicate read/writeability of the socket
* @param[in] priv private data, "struct async_req *" in this case
*/
static void async_connect_callback(struct event_context *ev,
struct fd_event *fde, uint16_t flags,
void *priv)
{
struct async_req *req = talloc_get_type_abort(
priv, struct async_req);
struct async_syscall_state *state = talloc_get_type_abort(
req->private_data, struct async_syscall_state);
struct param_connect *p = &state->param.param_connect;
if (state->syscall_type != ASYNC_SYSCALL_CONNECT) {
async_req_error(req, NT_STATUS_INTERNAL_ERROR);
return;
}
TALLOC_FREE(state->fde);
/*
* Stevens, Network Programming says that if there's a
* successful connect, the socket is only writable. Upon an
* error, it's both readable and writable.
*/
if ((flags & (EVENT_FD_READ|EVENT_FD_WRITE))
== (EVENT_FD_READ|EVENT_FD_WRITE)) {
int sockerr;
socklen_t err_len = sizeof(sockerr);
if (getsockopt(p->fd, SOL_SOCKET, SO_ERROR,
(void *)&sockerr, &err_len) == 0) {
errno = sockerr;
}
state->sys_errno = errno;
DEBUG(10, ("connect returned %s\n", strerror(errno)));
sys_fcntl_long(p->fd, F_SETFL, p->old_sockflags);
async_req_error(req, map_nt_error_from_unix(state->sys_errno));
return;
}
sys_fcntl_long(p->fd, F_SETFL, p->old_sockflags);
state->result.result_int = 0;
state->sys_errno = 0;
async_req_done(req);
}
/**
* @brief async version of connect(2)
* @param[in] mem_ctx The memory context to hang the result off
* @param[in] ev The event context to work from
* @param[in] fd The socket to recv from
* @param[in] address Where to connect?
* @param[in] address_len Length of *address
* @retval The async request
*
* This function sets the socket into non-blocking state to be able to call
* connect in an async state. This will be reset when the request is finished.
*/
struct async_req *async_connect(TALLOC_CTX *mem_ctx, struct event_context *ev,
int fd, const struct sockaddr *address,
socklen_t address_len)
{
struct async_req *result;
struct async_syscall_state *state;
struct param_connect *p;
result = async_syscall_new(mem_ctx, ev, ASYNC_SYSCALL_CONNECT, &state);
if (result == NULL) {
return NULL;
}
p = &state->param.param_connect;
/**
* We have to set the socket to nonblocking for async connect(2). Keep
* the old sockflags around.
*/
p->old_sockflags = sys_fcntl_long(fd, F_GETFL, 0);
if (p->old_sockflags == -1) {
if (async_post_status(result, ev,
map_nt_error_from_unix(errno))) {
return result;
}
TALLOC_FREE(result);
return NULL;
}
set_blocking(fd, false);
state->result.result_int = connect(fd, address, address_len);
if (state->result.result_int == 0) {
state->sys_errno = 0;
if (async_post_status(result, ev, NT_STATUS_OK)) {
return result;
}
sys_fcntl_long(fd, F_SETFL, p->old_sockflags);
TALLOC_FREE(result);
return NULL;
}
/**
* A number of error messages show that something good is progressing
* and that we have to wait for readability.
*
* If none of them are present, bail out.
*/
if (!(errno == EINPROGRESS || errno == EALREADY ||
#ifdef EISCONN
errno == EISCONN ||
#endif
errno == EAGAIN || errno == EINTR)) {
state->sys_errno = errno;
if (async_post_status(result, ev,
map_nt_error_from_unix(errno))) {
return result;
}
sys_fcntl_long(fd, F_SETFL, p->old_sockflags);
TALLOC_FREE(result);
return NULL;
}
state->fde = event_add_fd(ev, state, fd,
EVENT_FD_READ | EVENT_FD_WRITE,
async_connect_callback, result);
if (state->fde == NULL) {
sys_fcntl_long(fd, F_SETFL, p->old_sockflags);
TALLOC_FREE(result);
return NULL;
}
result->private_data = state;
state->param.param_connect.fd = fd;
state->param.param_connect.address = address;
state->param.param_connect.address_len = address_len;
return result;
}
|