2 Unix SMB/CIFS implementation.
4 Copyright (C) Volker Lendecke 2008
6 ** NOTE! The following LGPL license applies to the async_sock
7 ** library. This does NOT imply that all of Samba is released
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 3 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Library General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "system/network.h"
26 #include "system/filesys.h"
29 #include "lib/async_req/async_sock.h"
31 /* Note: lib/util/ is currently GPL */
32 #include "lib/util/tevent_unix.h"
33 #include "lib/util/util.h"
36 #define TALLOC_FREE(ctx) do { talloc_free(ctx); ctx=NULL; } while(0)
39 struct async_send_state
{
47 static void async_send_handler(struct tevent_context
*ev
,
48 struct tevent_fd
*fde
,
49 uint16_t flags
, void *private_data
);
51 struct tevent_req
*async_send_send(TALLOC_CTX
*mem_ctx
,
52 struct tevent_context
*ev
,
53 int fd
, const void *buf
, size_t len
,
56 struct tevent_req
*result
;
57 struct async_send_state
*state
;
58 struct tevent_fd
*fde
;
60 result
= tevent_req_create(mem_ctx
, &state
, struct async_send_state
);
69 fde
= tevent_add_fd(ev
, state
, fd
, TEVENT_FD_WRITE
, async_send_handler
,
78 static void async_send_handler(struct tevent_context
*ev
,
79 struct tevent_fd
*fde
,
80 uint16_t flags
, void *private_data
)
82 struct tevent_req
*req
= talloc_get_type_abort(
83 private_data
, struct tevent_req
);
84 struct async_send_state
*state
=
85 tevent_req_data(req
, struct async_send_state
);
87 state
->sent
= send(state
->fd
, state
->buf
, state
->len
, state
->flags
);
88 if ((state
->sent
== -1) && (errno
== EINTR
)) {
92 if (state
->sent
== -1) {
93 tevent_req_error(req
, errno
);
99 ssize_t
async_send_recv(struct tevent_req
*req
, int *perrno
)
101 struct async_send_state
*state
=
102 tevent_req_data(req
, struct async_send_state
);
104 if (tevent_req_is_unix_error(req
, perrno
)) {
110 struct async_recv_state
{
118 static void async_recv_handler(struct tevent_context
*ev
,
119 struct tevent_fd
*fde
,
120 uint16_t flags
, void *private_data
);
122 struct tevent_req
*async_recv_send(TALLOC_CTX
*mem_ctx
,
123 struct tevent_context
*ev
,
124 int fd
, void *buf
, size_t len
, int flags
)
126 struct tevent_req
*result
;
127 struct async_recv_state
*state
;
128 struct tevent_fd
*fde
;
130 result
= tevent_req_create(mem_ctx
, &state
, struct async_recv_state
);
131 if (result
== NULL
) {
137 state
->flags
= flags
;
139 fde
= tevent_add_fd(ev
, state
, fd
, TEVENT_FD_READ
, async_recv_handler
,
148 static void async_recv_handler(struct tevent_context
*ev
,
149 struct tevent_fd
*fde
,
150 uint16_t flags
, void *private_data
)
152 struct tevent_req
*req
= talloc_get_type_abort(
153 private_data
, struct tevent_req
);
154 struct async_recv_state
*state
=
155 tevent_req_data(req
, struct async_recv_state
);
157 state
->received
= recv(state
->fd
, state
->buf
, state
->len
,
159 if ((state
->received
== -1) && (errno
== EINTR
)) {
163 if (state
->received
== -1) {
164 tevent_req_error(req
, errno
);
167 tevent_req_done(req
);
170 ssize_t
async_recv_recv(struct tevent_req
*req
, int *perrno
)
172 struct async_recv_state
*state
=
173 tevent_req_data(req
, struct async_recv_state
);
175 if (tevent_req_is_unix_error(req
, perrno
)) {
178 return state
->received
;
181 struct async_connect_state
{
186 socklen_t address_len
;
187 struct sockaddr_storage address
;
190 static void async_connect_connected(struct tevent_context
*ev
,
191 struct tevent_fd
*fde
, uint16_t flags
,
195 * @brief async version of connect(2)
196 * @param[in] mem_ctx The memory context to hang the result off
197 * @param[in] ev The event context to work from
198 * @param[in] fd The socket to recv from
199 * @param[in] address Where to connect?
200 * @param[in] address_len Length of *address
201 * @retval The async request
203 * This function sets the socket into non-blocking state to be able to call
204 * connect in an async state. This will be reset when the request is finished.
207 struct tevent_req
*async_connect_send(TALLOC_CTX
*mem_ctx
,
208 struct tevent_context
*ev
,
209 int fd
, const struct sockaddr
*address
,
210 socklen_t address_len
)
212 struct tevent_req
*result
;
213 struct async_connect_state
*state
;
214 struct tevent_fd
*fde
;
216 result
= tevent_req_create(
217 mem_ctx
, &state
, struct async_connect_state
);
218 if (result
== NULL
) {
223 * We have to set the socket to nonblocking for async connect(2). Keep
224 * the old sockflags around.
228 state
->sys_errno
= 0;
230 state
->old_sockflags
= fcntl(fd
, F_GETFL
, 0);
231 if (state
->old_sockflags
== -1) {
235 state
->address_len
= address_len
;
236 if (address_len
> sizeof(state
->address
)) {
240 memcpy(&state
->address
, address
, address_len
);
242 set_blocking(fd
, false);
244 state
->result
= connect(fd
, address
, address_len
);
245 if (state
->result
== 0) {
246 tevent_req_done(result
);
251 * A number of error messages show that something good is progressing
252 * and that we have to wait for readability.
254 * If none of them are present, bail out.
257 if (!(errno
== EINPROGRESS
|| errno
== EALREADY
||
261 errno
== EAGAIN
|| errno
== EINTR
)) {
262 state
->sys_errno
= errno
;
266 fde
= tevent_add_fd(ev
, state
, fd
, TEVENT_FD_READ
| TEVENT_FD_WRITE
,
267 async_connect_connected
, result
);
269 state
->sys_errno
= ENOMEM
;
275 tevent_req_error(result
, state
->sys_errno
);
277 fcntl(fd
, F_SETFL
, state
->old_sockflags
);
278 return tevent_req_post(result
, ev
);
282 * fde event handler for connect(2)
283 * @param[in] ev The event context that sent us here
284 * @param[in] fde The file descriptor event associated with the connect
285 * @param[in] flags Indicate read/writeability of the socket
286 * @param[in] priv private data, "struct async_req *" in this case
289 static void async_connect_connected(struct tevent_context
*ev
,
290 struct tevent_fd
*fde
, uint16_t flags
,
293 struct tevent_req
*req
= talloc_get_type_abort(
294 priv
, struct tevent_req
);
295 struct async_connect_state
*state
=
296 tevent_req_data(req
, struct async_connect_state
);
299 * Stevens, Network Programming says that if there's a
300 * successful connect, the socket is only writable. Upon an
301 * error, it's both readable and writable.
303 if ((flags
& (TEVENT_FD_READ
|TEVENT_FD_WRITE
))
304 == (TEVENT_FD_READ
|TEVENT_FD_WRITE
)) {
307 ret
= connect(state
->fd
,
308 (struct sockaddr
*)(void *)&state
->address
,
312 tevent_req_done(req
);
316 if (errno
== EINPROGRESS
) {
317 /* Try again later, leave the fde around */
321 tevent_req_error(req
, errno
);
325 state
->sys_errno
= 0;
326 tevent_req_done(req
);
329 int async_connect_recv(struct tevent_req
*req
, int *perrno
)
331 struct async_connect_state
*state
=
332 tevent_req_data(req
, struct async_connect_state
);
335 fcntl(state
->fd
, F_SETFL
, state
->old_sockflags
);
337 if (tevent_req_is_unix_error(req
, &err
)) {
342 if (state
->sys_errno
== 0) {
346 *perrno
= state
->sys_errno
;
350 struct writev_state
{
351 struct tevent_context
*ev
;
359 static void writev_trigger(struct tevent_req
*req
, void *private_data
);
360 static void writev_handler(struct tevent_context
*ev
, struct tevent_fd
*fde
,
361 uint16_t flags
, void *private_data
);
363 struct tevent_req
*writev_send(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
364 struct tevent_queue
*queue
, int fd
,
365 bool err_on_readability
,
366 struct iovec
*iov
, int count
)
368 struct tevent_req
*req
;
369 struct writev_state
*state
;
371 req
= tevent_req_create(mem_ctx
, &state
, struct writev_state
);
377 state
->total_size
= 0;
378 state
->count
= count
;
379 state
->iov
= (struct iovec
*)talloc_memdup(
380 state
, iov
, sizeof(struct iovec
) * count
);
381 if (state
->iov
== NULL
) {
384 state
->flags
= TEVENT_FD_WRITE
;
385 if (err_on_readability
) {
386 state
->flags
|= TEVENT_FD_READ
;
390 struct tevent_fd
*fde
;
391 fde
= tevent_add_fd(state
->ev
, state
, state
->fd
,
392 state
->flags
, writev_handler
, req
);
393 if (tevent_req_nomem(fde
, req
)) {
394 return tevent_req_post(req
, ev
);
399 if (!tevent_queue_add(queue
, ev
, req
, writev_trigger
, NULL
)) {
408 static void writev_trigger(struct tevent_req
*req
, void *private_data
)
410 struct writev_state
*state
= tevent_req_data(req
, struct writev_state
);
411 struct tevent_fd
*fde
;
413 fde
= tevent_add_fd(state
->ev
, state
, state
->fd
, state
->flags
,
414 writev_handler
, req
);
416 tevent_req_error(req
, ENOMEM
);
420 static void writev_handler(struct tevent_context
*ev
, struct tevent_fd
*fde
,
421 uint16_t flags
, void *private_data
)
423 struct tevent_req
*req
= talloc_get_type_abort(
424 private_data
, struct tevent_req
);
425 struct writev_state
*state
=
426 tevent_req_data(req
, struct writev_state
);
427 size_t to_write
, written
;
432 if ((state
->flags
& TEVENT_FD_READ
) && (flags
& TEVENT_FD_READ
)) {
433 tevent_req_error(req
, EPIPE
);
437 for (i
=0; i
<state
->count
; i
++) {
438 to_write
+= state
->iov
[i
].iov_len
;
441 written
= writev(state
->fd
, state
->iov
, state
->count
);
442 if ((written
== -1) && (errno
== EINTR
)) {
447 tevent_req_error(req
, errno
);
451 tevent_req_error(req
, EPIPE
);
454 state
->total_size
+= written
;
456 if (written
== to_write
) {
457 tevent_req_done(req
);
462 * We've written less than we were asked to, drop stuff from
466 while (written
> 0) {
467 if (written
< state
->iov
[0].iov_len
) {
468 state
->iov
[0].iov_base
=
469 (char *)state
->iov
[0].iov_base
+ written
;
470 state
->iov
[0].iov_len
-= written
;
473 written
-= state
->iov
[0].iov_len
;
479 ssize_t
writev_recv(struct tevent_req
*req
, int *perrno
)
481 struct writev_state
*state
=
482 tevent_req_data(req
, struct writev_state
);
484 if (tevent_req_is_unix_error(req
, perrno
)) {
487 return state
->total_size
;
490 struct read_packet_state
{
494 ssize_t (*more
)(uint8_t *buf
, size_t buflen
, void *private_data
);
498 static void read_packet_handler(struct tevent_context
*ev
,
499 struct tevent_fd
*fde
,
500 uint16_t flags
, void *private_data
);
502 struct tevent_req
*read_packet_send(TALLOC_CTX
*mem_ctx
,
503 struct tevent_context
*ev
,
504 int fd
, size_t initial
,
505 ssize_t (*more
)(uint8_t *buf
,
510 struct tevent_req
*result
;
511 struct read_packet_state
*state
;
512 struct tevent_fd
*fde
;
514 result
= tevent_req_create(mem_ctx
, &state
, struct read_packet_state
);
515 if (result
== NULL
) {
521 state
->private_data
= private_data
;
523 state
->buf
= talloc_array(state
, uint8_t, initial
);
524 if (state
->buf
== NULL
) {
528 fde
= tevent_add_fd(ev
, state
, fd
, TEVENT_FD_READ
, read_packet_handler
,
539 static void read_packet_handler(struct tevent_context
*ev
,
540 struct tevent_fd
*fde
,
541 uint16_t flags
, void *private_data
)
543 struct tevent_req
*req
= talloc_get_type_abort(
544 private_data
, struct tevent_req
);
545 struct read_packet_state
*state
=
546 tevent_req_data(req
, struct read_packet_state
);
547 size_t total
= talloc_get_size(state
->buf
);
551 nread
= recv(state
->fd
, state
->buf
+state
->nread
, total
-state
->nread
,
553 if ((nread
== -1) && (errno
== EINTR
)) {
558 tevent_req_error(req
, errno
);
562 tevent_req_error(req
, EPIPE
);
566 state
->nread
+= nread
;
567 if (state
->nread
< total
) {
568 /* Come back later */
573 * We got what was initially requested. See if "more" asks for -- more.
575 if (state
->more
== NULL
) {
576 /* Nobody to ask, this is a async read_data */
577 tevent_req_done(req
);
581 more
= state
->more(state
->buf
, total
, state
->private_data
);
583 /* We got an invalid packet, tell the caller */
584 tevent_req_error(req
, EIO
);
588 /* We're done, full packet received */
589 tevent_req_done(req
);
593 tmp
= talloc_realloc(state
, state
->buf
, uint8_t, total
+more
);
594 if (tevent_req_nomem(tmp
, req
)) {
600 ssize_t
read_packet_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
601 uint8_t **pbuf
, int *perrno
)
603 struct read_packet_state
*state
=
604 tevent_req_data(req
, struct read_packet_state
);
606 if (tevent_req_is_unix_error(req
, perrno
)) {
609 *pbuf
= talloc_move(mem_ctx
, &state
->buf
);
610 return talloc_get_size(*pbuf
);