#include <talloc.h>
#include <tevent.h>
#include "lib/async_req/async_sock.h"
+#include "lib/util/iov_buf.h"
/* Note: lib/util/ is currently GPL */
#include "lib/util/tevent_unix.h"
-#include "lib/util/util.h"
-
-#ifndef TALLOC_FREE
-#define TALLOC_FREE(ctx) do { talloc_free(ctx); ctx=NULL; } while(0)
-#endif
-
-struct async_send_state {
- int fd;
- const void *buf;
- size_t len;
- int flags;
- ssize_t sent;
-};
-
-static void async_send_handler(struct tevent_context *ev,
- struct tevent_fd *fde,
- uint16_t flags, void *private_data);
-
-struct tevent_req *async_send_send(TALLOC_CTX *mem_ctx,
- struct tevent_context *ev,
- int fd, const void *buf, size_t len,
- int flags)
-{
- struct tevent_req *result;
- struct async_send_state *state;
- struct tevent_fd *fde;
-
- result = tevent_req_create(mem_ctx, &state, struct async_send_state);
- if (result == NULL) {
- return result;
- }
- state->fd = fd;
- state->buf = buf;
- state->len = len;
- state->flags = flags;
-
- fde = tevent_add_fd(ev, state, fd, TEVENT_FD_WRITE, async_send_handler,
- result);
- if (fde == NULL) {
- TALLOC_FREE(result);
- return NULL;
- }
- return result;
-}
-
-static void async_send_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 async_send_state *state =
- tevent_req_data(req, struct async_send_state);
-
- state->sent = send(state->fd, state->buf, state->len, state->flags);
- if ((state->sent == -1) && (errno == EINTR)) {
- /* retry */
- return;
- }
- if (state->sent == -1) {
- tevent_req_error(req, errno);
- return;
- }
- tevent_req_done(req);
-}
-
-ssize_t async_send_recv(struct tevent_req *req, int *perrno)
-{
- struct async_send_state *state =
- tevent_req_data(req, struct async_send_state);
-
- if (tevent_req_is_unix_error(req, perrno)) {
- return -1;
- }
- return state->sent;
-}
-
-struct async_recv_state {
- int fd;
- void *buf;
- size_t len;
- int flags;
- ssize_t received;
-};
-
-static void async_recv_handler(struct tevent_context *ev,
- struct tevent_fd *fde,
- uint16_t flags, void *private_data);
-
-struct tevent_req *async_recv_send(TALLOC_CTX *mem_ctx,
- struct tevent_context *ev,
- int fd, void *buf, size_t len, int flags)
-{
- struct tevent_req *result;
- struct async_recv_state *state;
- struct tevent_fd *fde;
-
- result = tevent_req_create(mem_ctx, &state, struct async_recv_state);
- if (result == NULL) {
- return result;
- }
- state->fd = fd;
- state->buf = buf;
- state->len = len;
- state->flags = flags;
-
- fde = tevent_add_fd(ev, state, fd, TEVENT_FD_READ, async_recv_handler,
- result);
- if (fde == NULL) {
- TALLOC_FREE(result);
- return NULL;
- }
- return result;
-}
-
-static void async_recv_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 async_recv_state *state =
- tevent_req_data(req, struct async_recv_state);
-
- state->received = recv(state->fd, state->buf, state->len,
- state->flags);
- if ((state->received == -1) && (errno == EINTR)) {
- /* retry */
- return;
- }
- if (state->received == -1) {
- tevent_req_error(req, errno);
- return;
- }
- tevent_req_done(req);
-}
-
-ssize_t async_recv_recv(struct tevent_req *req, int *perrno)
-{
- struct async_recv_state *state =
- tevent_req_data(req, struct async_recv_state);
-
- if (tevent_req_is_unix_error(req, perrno)) {
- return -1;
- }
- return state->received;
-}
+#include "lib/util/samba_util.h"
struct async_connect_state {
int fd;
+ struct tevent_fd *fde;
int result;
- int sys_errno;
long old_sockflags;
socklen_t address_len;
struct sockaddr_storage address;
+
+ void (*before_connect)(void *private_data);
+ void (*after_connect)(void *private_data);
+ void *private_data;
};
+static void async_connect_cleanup(struct tevent_req *req,
+ enum tevent_req_state req_state);
static void async_connect_connected(struct tevent_context *ev,
struct tevent_fd *fde, uint16_t flags,
void *priv);
* connect in an async state. This will be reset when the request is finished.
*/
-struct tevent_req *async_connect_send(TALLOC_CTX *mem_ctx,
- struct tevent_context *ev,
- int fd, const struct sockaddr *address,
- socklen_t address_len)
+struct tevent_req *async_connect_send(
+ TALLOC_CTX *mem_ctx, struct tevent_context *ev, int fd,
+ const struct sockaddr *address, socklen_t address_len,
+ void (*before_connect)(void *private_data),
+ void (*after_connect)(void *private_data),
+ void *private_data)
{
- struct tevent_req *result;
+ struct tevent_req *req;
struct async_connect_state *state;
- struct tevent_fd *fde;
- result = tevent_req_create(
- mem_ctx, &state, struct async_connect_state);
- if (result == NULL) {
+ req = tevent_req_create(mem_ctx, &state, struct async_connect_state);
+ if (req == NULL) {
return NULL;
}
*/
state->fd = fd;
- state->sys_errno = 0;
+ state->before_connect = before_connect;
+ state->after_connect = after_connect;
+ state->private_data = private_data;
state->old_sockflags = fcntl(fd, F_GETFL, 0);
if (state->old_sockflags == -1) {
- goto post_errno;
+ tevent_req_error(req, errno);
+ return tevent_req_post(req, ev);
}
+ tevent_req_set_cleanup_fn(req, async_connect_cleanup);
+
state->address_len = address_len;
if (address_len > sizeof(state->address)) {
- errno = EINVAL;
- goto post_errno;
+ tevent_req_error(req, EINVAL);
+ return tevent_req_post(req, ev);
}
memcpy(&state->address, address, address_len);
set_blocking(fd, false);
+ if (state->before_connect != NULL) {
+ state->before_connect(state->private_data);
+ }
+
state->result = connect(fd, address, address_len);
+
+ if (state->after_connect != NULL) {
+ state->after_connect(state->private_data);
+ }
+
if (state->result == 0) {
- tevent_req_done(result);
- goto done;
+ tevent_req_done(req);
+ return tevent_req_post(req, ev);
}
/**
errno == EISCONN ||
#endif
errno == EAGAIN || errno == EINTR)) {
- state->sys_errno = errno;
- goto post_errno;
+ tevent_req_error(req, errno);
+ return tevent_req_post(req, ev);
}
- fde = tevent_add_fd(ev, state, fd, TEVENT_FD_READ | TEVENT_FD_WRITE,
- async_connect_connected, result);
- if (fde == NULL) {
- state->sys_errno = ENOMEM;
- goto post_errno;
+ state->fde = tevent_add_fd(ev, state, fd,
+ TEVENT_FD_READ | TEVENT_FD_WRITE,
+ async_connect_connected, req);
+ if (state->fde == NULL) {
+ tevent_req_error(req, ENOMEM);
+ return tevent_req_post(req, ev);
}
- return result;
+ return req;
+}
+
+static void async_connect_cleanup(struct tevent_req *req,
+ enum tevent_req_state req_state)
+{
+ struct async_connect_state *state =
+ tevent_req_data(req, struct async_connect_state);
- post_errno:
- tevent_req_error(result, state->sys_errno);
- done:
- fcntl(fd, F_SETFL, state->old_sockflags);
- return tevent_req_post(result, ev);
+ TALLOC_FREE(state->fde);
+ if (state->fd != -1) {
+ fcntl(state->fd, F_SETFL, state->old_sockflags);
+ state->fd = -1;
+ }
}
/**
priv, struct tevent_req);
struct async_connect_state *state =
tevent_req_data(req, struct async_connect_state);
+ int ret;
- /*
- * 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 & (TEVENT_FD_READ|TEVENT_FD_WRITE))
- == (TEVENT_FD_READ|TEVENT_FD_WRITE)) {
- int ret;
-
- ret = connect(state->fd,
- (struct sockaddr *)(void *)&state->address,
- state->address_len);
- if (ret == 0) {
- TALLOC_FREE(fde);
- tevent_req_done(req);
- return;
- }
+ if (state->before_connect != NULL) {
+ state->before_connect(state->private_data);
+ }
- if (errno == EINPROGRESS) {
- /* Try again later, leave the fde around */
- return;
- }
- TALLOC_FREE(fde);
- tevent_req_error(req, errno);
- return;
+ ret = connect(state->fd, (struct sockaddr *)(void *)&state->address,
+ state->address_len);
+
+ if (state->after_connect != NULL) {
+ state->after_connect(state->private_data);
}
- state->sys_errno = 0;
- tevent_req_done(req);
+ if (ret == 0) {
+ tevent_req_done(req);
+ return;
+ }
+ if (errno == EINPROGRESS) {
+ /* Try again later, leave the fde around */
+ return;
+ }
+ tevent_req_error(req, errno);
+ return;
}
int async_connect_recv(struct tevent_req *req, int *perrno)
{
- struct async_connect_state *state =
- tevent_req_data(req, struct async_connect_state);
- int err;
-
- fcntl(state->fd, F_SETFL, state->old_sockflags);
+ int err = tevent_req_simple_recv_unix(req);
- if (tevent_req_is_unix_error(req, &err)) {
+ if (err != 0) {
*perrno = err;
return -1;
}
- if (state->sys_errno == 0) {
- return 0;
- }
-
- *perrno = state->sys_errno;
- return -1;
+ return 0;
}
struct writev_state {
struct tevent_context *ev;
int fd;
+ struct tevent_fd *fde;
struct iovec *iov;
int count;
size_t total_size;
uint16_t flags;
+ bool err_on_readability;
};
+static void writev_cleanup(struct tevent_req *req,
+ enum tevent_req_state req_state);
static void writev_trigger(struct tevent_req *req, void *private_data);
static void writev_handler(struct tevent_context *ev, struct tevent_fd *fde,
uint16_t flags, void *private_data);
state->count = count;
state->iov = (struct iovec *)talloc_memdup(
state, iov, sizeof(struct iovec) * count);
- if (state->iov == NULL) {
- goto fail;
- }
- state->flags = TEVENT_FD_WRITE;
- if (err_on_readability) {
- state->flags |= TEVENT_FD_READ;
+ if (tevent_req_nomem(state->iov, req)) {
+ return tevent_req_post(req, ev);
}
+ state->flags = TEVENT_FD_WRITE|TEVENT_FD_READ;
+ state->err_on_readability = err_on_readability;
+
+ tevent_req_set_cleanup_fn(req, writev_cleanup);
if (queue == NULL) {
- struct tevent_fd *fde;
- fde = tevent_add_fd(state->ev, state, state->fd,
+ state->fde = tevent_add_fd(state->ev, state, state->fd,
state->flags, writev_handler, req);
- if (tevent_req_nomem(fde, req)) {
+ if (tevent_req_nomem(state->fde, req)) {
return tevent_req_post(req, ev);
}
return req;
}
if (!tevent_queue_add(queue, ev, req, writev_trigger, NULL)) {
- goto fail;
+ tevent_req_nomem(NULL, req);
+ return tevent_req_post(req, ev);
}
return req;
- fail:
- TALLOC_FREE(req);
- return NULL;
+}
+
+static void writev_cleanup(struct tevent_req *req,
+ enum tevent_req_state req_state)
+{
+ struct writev_state *state = tevent_req_data(req, struct writev_state);
+
+ TALLOC_FREE(state->fde);
}
static void writev_trigger(struct tevent_req *req, void *private_data)
{
struct writev_state *state = tevent_req_data(req, struct writev_state);
- struct tevent_fd *fde;
- fde = tevent_add_fd(state->ev, state, state->fd, state->flags,
+ state->fde = tevent_add_fd(state->ev, state, state->fd, state->flags,
writev_handler, req);
- if (fde == NULL) {
- tevent_req_error(req, ENOMEM);
+ if (tevent_req_nomem(state->fde, req)) {
+ return;
}
}
private_data, struct tevent_req);
struct writev_state *state =
tevent_req_data(req, struct writev_state);
- size_t to_write, written;
- int i;
-
- to_write = 0;
+ size_t written;
+ bool ok;
if ((state->flags & TEVENT_FD_READ) && (flags & TEVENT_FD_READ)) {
- tevent_req_error(req, EPIPE);
- return;
- }
+ int ret, value;
+
+ if (state->err_on_readability) {
+ /* Readable and the caller wants an error on read. */
+ tevent_req_error(req, EPIPE);
+ return;
+ }
- for (i=0; i<state->count; i++) {
- to_write += state->iov[i].iov_len;
+ /* Might be an error. Check if there are bytes to read */
+ ret = ioctl(state->fd, FIONREAD, &value);
+ /* FIXME - should we also check
+ for ret == 0 and value == 0 here ? */
+ if (ret == -1) {
+ /* There's an error. */
+ tevent_req_error(req, EPIPE);
+ return;
+ }
+ /* A request for TEVENT_FD_READ will succeed from now and
+ forevermore until the bytes are read so if there was
+ an error we'll wait until we do read, then get it in
+ the read callback function. Until then, remove TEVENT_FD_READ
+ from the flags we're waiting for. */
+ state->flags &= ~TEVENT_FD_READ;
+ TEVENT_FD_NOT_READABLE(fde);
+
+ /* If not writable, we're done. */
+ if (!(flags & TEVENT_FD_WRITE)) {
+ return;
+ }
}
written = writev(state->fd, state->iov, state->count);
- if ((written == -1) && (errno = EINTR)) {
+ if ((written == -1) && (errno == EINTR)) {
/* retry */
return;
}
}
state->total_size += written;
- if (written == to_write) {
- tevent_req_done(req);
+ ok = iov_advance(&state->iov, &state->count, written);
+ if (!ok) {
+ tevent_req_error(req, EIO);
return;
}
- /*
- * We've written less than we were asked to, drop stuff from
- * state->iov.
- */
-
- while (written > 0) {
- if (written < state->iov[0].iov_len) {
- state->iov[0].iov_base =
- (char *)state->iov[0].iov_base + written;
- state->iov[0].iov_len -= written;
- break;
- }
- written -= state->iov[0].iov_len;
- state->iov += 1;
- state->count -= 1;
+ if (state->count == 0) {
+ tevent_req_done(req);
+ return;
}
}
{
struct writev_state *state =
tevent_req_data(req, struct writev_state);
+ ssize_t ret;
if (tevent_req_is_unix_error(req, perrno)) {
+ tevent_req_received(req);
return -1;
}
- return state->total_size;
+ ret = state->total_size;
+ tevent_req_received(req);
+ return ret;
}
struct read_packet_state {
void *private_data),
void *private_data)
{
- struct tevent_req *result;
+ struct tevent_req *req;
struct read_packet_state *state;
struct tevent_fd *fde;
- result = tevent_req_create(mem_ctx, &state, struct read_packet_state);
- if (result == NULL) {
+ req = tevent_req_create(mem_ctx, &state, struct read_packet_state);
+ if (req == NULL) {
return NULL;
}
state->fd = fd;
}
fde = tevent_add_fd(ev, state, fd, TEVENT_FD_READ, read_packet_handler,
- result);
+ req);
if (fde == NULL) {
goto fail;
}
- return result;
+ return req;
fail:
- TALLOC_FREE(result);
+ TALLOC_FREE(req);
return NULL;
}
nread = recv(state->fd, state->buf+state->nread, total-state->nread,
0);
+ if ((nread == -1) && (errno == ENOTSOCK)) {
+ nread = read(state->fd, state->buf+state->nread,
+ total-state->nread);
+ }
if ((nread == -1) && (errno == EINTR)) {
/* retry */
return;
return;
}
+ if (total + more < total) {
+ tevent_req_error(req, EMSGSIZE);
+ return;
+ }
+
tmp = talloc_realloc(state, state->buf, uint8_t, total+more);
if (tevent_req_nomem(tmp, req)) {
return;
*pbuf = talloc_move(mem_ctx, &state->buf);
return talloc_get_size(*pbuf);
}
+
+struct wait_for_read_state {
+ struct tevent_req *req;
+ struct tevent_fd *fde;
+};
+
+static void wait_for_read_done(struct tevent_context *ev,
+ struct tevent_fd *fde,
+ uint16_t flags,
+ void *private_data);
+
+struct tevent_req *wait_for_read_send(TALLOC_CTX *mem_ctx,
+ struct tevent_context *ev,
+ int fd)
+{
+ struct tevent_req *req;
+ struct wait_for_read_state *state;
+
+ req = tevent_req_create(mem_ctx, &state, struct wait_for_read_state);
+ if (req == NULL) {
+ return NULL;
+ }
+ state->req = req;
+ state->fde = tevent_add_fd(ev, state, fd, TEVENT_FD_READ,
+ wait_for_read_done, state);
+ if (tevent_req_nomem(state->fde, req)) {
+ return tevent_req_post(req, ev);
+ }
+ return req;
+}
+
+static void wait_for_read_done(struct tevent_context *ev,
+ struct tevent_fd *fde,
+ uint16_t flags,
+ void *private_data)
+{
+ struct wait_for_read_state *state = talloc_get_type_abort(
+ private_data, struct wait_for_read_state);
+
+ if (flags & TEVENT_FD_READ) {
+ TALLOC_FREE(state->fde);
+ tevent_req_done(state->req);
+ }
+}
+
+bool wait_for_read_recv(struct tevent_req *req, int *perr)
+{
+ int err;
+
+ if (tevent_req_is_unix_error(req, &err)) {
+ *perr = err;
+ return false;
+ }
+ return true;
+}