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 "lib/talloc/talloc.h"
26 #include "lib/tevent/tevent.h"
27 #include "lib/async_req/async_sock.h"
28 #include "lib/util/tevent_unix.h"
32 #define TALLOC_FREE(ctx) do { talloc_free(ctx); ctx=NULL; } while(0)
35 struct async_send_state {
43 static void async_send_handler(struct tevent_context *ev,
44 struct tevent_fd *fde,
45 uint16_t flags, void *private_data);
47 struct tevent_req *async_send_send(TALLOC_CTX *mem_ctx,
48 struct tevent_context *ev,
49 int fd, const void *buf, size_t len,
52 struct tevent_req *result;
53 struct async_send_state *state;
54 struct tevent_fd *fde;
56 result = tevent_req_create(mem_ctx, &state, struct async_send_state);
65 fde = tevent_add_fd(ev, state, fd, TEVENT_FD_WRITE, async_send_handler,
74 static void async_send_handler(struct tevent_context *ev,
75 struct tevent_fd *fde,
76 uint16_t flags, void *private_data)
78 struct tevent_req *req = talloc_get_type_abort(
79 private_data, struct tevent_req);
80 struct async_send_state *state =
81 tevent_req_data(req, struct async_send_state);
83 state->sent = send(state->fd, state->buf, state->len, state->flags);
84 if ((state->sent == -1) && (errno == EINTR)) {
88 if (state->sent == -1) {
89 tevent_req_error(req, errno);
95 ssize_t async_send_recv(struct tevent_req *req, int *perrno)
97 struct async_send_state *state =
98 tevent_req_data(req, struct async_send_state);
100 if (tevent_req_is_unix_error(req, perrno)) {
106 struct async_recv_state {
114 static void async_recv_handler(struct tevent_context *ev,
115 struct tevent_fd *fde,
116 uint16_t flags, void *private_data);
118 struct tevent_req *async_recv_send(TALLOC_CTX *mem_ctx,
119 struct tevent_context *ev,
120 int fd, void *buf, size_t len, int flags)
122 struct tevent_req *result;
123 struct async_recv_state *state;
124 struct tevent_fd *fde;
126 result = tevent_req_create(mem_ctx, &state, struct async_recv_state);
127 if (result == NULL) {
133 state->flags = flags;
135 fde = tevent_add_fd(ev, state, fd, TEVENT_FD_READ, async_recv_handler,
144 static void async_recv_handler(struct tevent_context *ev,
145 struct tevent_fd *fde,
146 uint16_t flags, void *private_data)
148 struct tevent_req *req = talloc_get_type_abort(
149 private_data, struct tevent_req);
150 struct async_recv_state *state =
151 tevent_req_data(req, struct async_recv_state);
153 state->received = recv(state->fd, state->buf, state->len,
155 if ((state->received == -1) && (errno == EINTR)) {
159 if (state->received == -1) {
160 tevent_req_error(req, errno);
163 tevent_req_done(req);
166 ssize_t async_recv_recv(struct tevent_req *req, int *perrno)
168 struct async_recv_state *state =
169 tevent_req_data(req, struct async_recv_state);
171 if (tevent_req_is_unix_error(req, perrno)) {
174 return state->received;
177 struct async_connect_state {
182 socklen_t address_len;
183 struct sockaddr_storage address;
186 static void async_connect_connected(struct tevent_context *ev,
187 struct tevent_fd *fde, uint16_t flags,
191 * @brief async version of connect(2)
192 * @param[in] mem_ctx The memory context to hang the result off
193 * @param[in] ev The event context to work from
194 * @param[in] fd The socket to recv from
195 * @param[in] address Where to connect?
196 * @param[in] address_len Length of *address
197 * @retval The async request
199 * This function sets the socket into non-blocking state to be able to call
200 * connect in an async state. This will be reset when the request is finished.
203 struct tevent_req *async_connect_send(TALLOC_CTX *mem_ctx,
204 struct tevent_context *ev,
205 int fd, const struct sockaddr *address,
206 socklen_t address_len)
208 struct tevent_req *result;
209 struct async_connect_state *state;
210 struct tevent_fd *fde;
212 result = tevent_req_create(
213 mem_ctx, &state, struct async_connect_state);
214 if (result == NULL) {
219 * We have to set the socket to nonblocking for async connect(2). Keep
220 * the old sockflags around.
224 state->sys_errno = 0;
226 state->old_sockflags = fcntl(fd, F_GETFL, 0);
227 if (state->old_sockflags == -1) {
231 state->address_len = address_len;
232 if (address_len > sizeof(state->address)) {
236 memcpy(&state->address, address, address_len);
238 set_blocking(fd, false);
240 state->result = connect(fd, address, address_len);
241 if (state->result == 0) {
242 tevent_req_done(result);
247 * A number of error messages show that something good is progressing
248 * and that we have to wait for readability.
250 * If none of them are present, bail out.
253 if (!(errno == EINPROGRESS || errno == EALREADY ||
257 errno == EAGAIN || errno == EINTR)) {
258 state->sys_errno = errno;
262 fde = tevent_add_fd(ev, state, fd, TEVENT_FD_READ | TEVENT_FD_WRITE,
263 async_connect_connected, result);
265 state->sys_errno = ENOMEM;
271 tevent_req_error(result, state->sys_errno);
273 fcntl(fd, F_SETFL, state->old_sockflags);
274 return tevent_req_post(result, ev);
278 * fde event handler for connect(2)
279 * @param[in] ev The event context that sent us here
280 * @param[in] fde The file descriptor event associated with the connect
281 * @param[in] flags Indicate read/writeability of the socket
282 * @param[in] priv private data, "struct async_req *" in this case
285 static void async_connect_connected(struct tevent_context *ev,
286 struct tevent_fd *fde, uint16_t flags,
289 struct tevent_req *req = talloc_get_type_abort(
290 priv, struct tevent_req);
291 struct async_connect_state *state =
292 tevent_req_data(req, struct async_connect_state);
295 * Stevens, Network Programming says that if there's a
296 * successful connect, the socket is only writable. Upon an
297 * error, it's both readable and writable.
299 if ((flags & (TEVENT_FD_READ|TEVENT_FD_WRITE))
300 == (TEVENT_FD_READ|TEVENT_FD_WRITE)) {
303 ret = connect(state->fd,
304 (struct sockaddr *)(void *)&state->address,
308 tevent_req_done(req);
312 if (errno == EINPROGRESS) {
313 /* Try again later, leave the fde around */
317 tevent_req_error(req, errno);
321 state->sys_errno = 0;
322 tevent_req_done(req);
325 int async_connect_recv(struct tevent_req *req, int *perrno)
327 struct async_connect_state *state =
328 tevent_req_data(req, struct async_connect_state);
331 fcntl(state->fd, F_SETFL, state->old_sockflags);
333 if (tevent_req_is_unix_error(req, &err)) {
338 if (state->sys_errno == 0) {
342 *perrno = state->sys_errno;
346 struct writev_state {
347 struct tevent_context *ev;
355 static void writev_trigger(struct tevent_req *req, void *private_data);
356 static void writev_handler(struct tevent_context *ev, struct tevent_fd *fde,
357 uint16_t flags, void *private_data);
359 struct tevent_req *writev_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
360 struct tevent_queue *queue, int fd,
361 bool err_on_readability,
362 struct iovec *iov, int count)
364 struct tevent_req *req;
365 struct writev_state *state;
367 req = tevent_req_create(mem_ctx, &state, struct writev_state);
373 state->total_size = 0;
374 state->count = count;
375 state->iov = (struct iovec *)talloc_memdup(
376 state, iov, sizeof(struct iovec) * count);
377 if (state->iov == NULL) {
380 state->flags = TEVENT_FD_WRITE;
381 if (err_on_readability) {
382 state->flags |= TEVENT_FD_READ;
386 struct tevent_fd *fde;
387 fde = tevent_add_fd(state->ev, state, state->fd,
388 state->flags, writev_handler, req);
389 if (tevent_req_nomem(fde, req)) {
390 return tevent_req_post(req, ev);
395 if (!tevent_queue_add(queue, ev, req, writev_trigger, NULL)) {
404 static void writev_trigger(struct tevent_req *req, void *private_data)
406 struct writev_state *state = tevent_req_data(req, struct writev_state);
407 struct tevent_fd *fde;
409 fde = tevent_add_fd(state->ev, state, state->fd, state->flags,
410 writev_handler, req);
412 tevent_req_error(req, ENOMEM);
416 static void writev_handler(struct tevent_context *ev, struct tevent_fd *fde,
417 uint16_t flags, void *private_data)
419 struct tevent_req *req = talloc_get_type_abort(
420 private_data, struct tevent_req);
421 struct writev_state *state =
422 tevent_req_data(req, struct writev_state);
423 size_t to_write, written;
428 if ((state->flags & TEVENT_FD_READ) && (flags & TEVENT_FD_READ)) {
429 tevent_req_error(req, EPIPE);
433 for (i=0; i<state->count; i++) {
434 to_write += state->iov[i].iov_len;
437 written = writev(state->fd, state->iov, state->count);
438 if ((written == -1) && (errno = EINTR)) {
443 tevent_req_error(req, errno);
447 tevent_req_error(req, EPIPE);
450 state->total_size += written;
452 if (written == to_write) {
453 tevent_req_done(req);
458 * We've written less than we were asked to, drop stuff from
462 while (written > 0) {
463 if (written < state->iov[0].iov_len) {
464 state->iov[0].iov_base =
465 (char *)state->iov[0].iov_base + written;
466 state->iov[0].iov_len -= written;
469 written -= state->iov[0].iov_len;
475 ssize_t writev_recv(struct tevent_req *req, int *perrno)
477 struct writev_state *state =
478 tevent_req_data(req, struct writev_state);
480 if (tevent_req_is_unix_error(req, perrno)) {
483 return state->total_size;
486 struct read_packet_state {
490 ssize_t (*more)(uint8_t *buf, size_t buflen, void *private_data);
494 static void read_packet_handler(struct tevent_context *ev,
495 struct tevent_fd *fde,
496 uint16_t flags, void *private_data);
498 struct tevent_req *read_packet_send(TALLOC_CTX *mem_ctx,
499 struct tevent_context *ev,
500 int fd, size_t initial,
501 ssize_t (*more)(uint8_t *buf,
506 struct tevent_req *result;
507 struct read_packet_state *state;
508 struct tevent_fd *fde;
510 result = tevent_req_create(mem_ctx, &state, struct read_packet_state);
511 if (result == NULL) {
517 state->private_data = private_data;
519 state->buf = talloc_array(state, uint8_t, initial);
520 if (state->buf == NULL) {
524 fde = tevent_add_fd(ev, state, fd, TEVENT_FD_READ, read_packet_handler,
535 static void read_packet_handler(struct tevent_context *ev,
536 struct tevent_fd *fde,
537 uint16_t flags, void *private_data)
539 struct tevent_req *req = talloc_get_type_abort(
540 private_data, struct tevent_req);
541 struct read_packet_state *state =
542 tevent_req_data(req, struct read_packet_state);
543 size_t total = talloc_get_size(state->buf);
547 nread = recv(state->fd, state->buf+state->nread, total-state->nread,
549 if ((nread == -1) && (errno == EINTR)) {
554 tevent_req_error(req, errno);
558 tevent_req_error(req, EPIPE);
562 state->nread += nread;
563 if (state->nread < total) {
564 /* Come back later */
569 * We got what was initially requested. See if "more" asks for -- more.
571 if (state->more == NULL) {
572 /* Nobody to ask, this is a async read_data */
573 tevent_req_done(req);
577 more = state->more(state->buf, total, state->private_data);
579 /* We got an invalid packet, tell the caller */
580 tevent_req_error(req, EIO);
584 /* We're done, full packet received */
585 tevent_req_done(req);
589 tmp = talloc_realloc(state, state->buf, uint8_t, total+more);
590 if (tevent_req_nomem(tmp, req)) {
596 ssize_t read_packet_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
597 uint8_t **pbuf, int *perrno)
599 struct read_packet_state *state =
600 tevent_req_data(req, struct read_packet_state);
602 if (tevent_req_is_unix_error(req, perrno)) {
605 *pbuf = talloc_move(mem_ctx, &state->buf);
606 return talloc_get_size(*pbuf);