diff options
author | Jelmer Vernooij <jelmer@samba.org> | 2009-01-04 22:48:23 +0100 |
---|---|---|
committer | Jelmer Vernooij <jelmer@samba.org> | 2009-01-04 22:48:23 +0100 |
commit | af744e0954bbe9ddfa2e3da173e79de65e640a4c (patch) | |
tree | 78b98c28e6d54c5339c12f2f942883d5088fda84 /source3/lib/async_sock.c | |
parent | ce47b69d8e318bbb3642d27aa0451e2914c92be7 (diff) | |
parent | 2c0faaf5d921fe57a88d3b999067458e8774c6f6 (diff) | |
download | samba-af744e0954bbe9ddfa2e3da173e79de65e640a4c.tar.gz samba-af744e0954bbe9ddfa2e3da173e79de65e640a4c.tar.bz2 samba-af744e0954bbe9ddfa2e3da173e79de65e640a4c.zip |
Merge branch 'master' of ssh://git.samba.org/data/git/samba
Diffstat (limited to 'source3/lib/async_sock.c')
-rw-r--r-- | source3/lib/async_sock.c | 245 |
1 files changed, 140 insertions, 105 deletions
diff --git a/source3/lib/async_sock.c b/source3/lib/async_sock.c index 225cc7b195..bb89a1353a 100644 --- a/source3/lib/async_sock.c +++ b/source3/lib/async_sock.c @@ -106,7 +106,7 @@ static struct async_req *async_syscall_new(TALLOC_CTX *mem_ctx, struct async_req *result; struct async_syscall_state *state; - result = async_req_new(mem_ctx, ev); + result = async_req_new(mem_ctx); if (result == NULL) { return NULL; } @@ -209,7 +209,7 @@ size_t async_syscall_result_size_t(struct async_req *req, int *perrno) * @retval The return value from the asynchronously called syscall */ -ssize_t async_syscall_result_int(struct async_req *req, int *perrno) +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); @@ -236,7 +236,10 @@ static void async_send_callback(struct event_context *ev, req->private_data, struct async_syscall_state); struct param_send *p = &state->param.param_send; - SMB_ASSERT(state->syscall_type == ASYNC_SYSCALL_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); @@ -300,7 +303,10 @@ static void async_sendall_callback(struct event_context *ev, req->private_data, struct async_syscall_state); struct param_sendall *p = &state->param.param_sendall; - SMB_ASSERT(state->syscall_type == ASYNC_SYSCALL_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); @@ -317,7 +323,10 @@ static void async_sendall_callback(struct event_context *ev, } p->sent += state->result.result_ssize_t; - SMB_ASSERT(p->sent <= p->length); + if (p->sent > p->length) { + async_req_error(req, NT_STATUS_INTERNAL_ERROR); + return; + } if (p->sent == p->length) { TALLOC_FREE(state->fde); @@ -385,7 +394,10 @@ static void async_recv_callback(struct event_context *ev, req->private_data, struct async_syscall_state); struct param_recv *p = &state->param.param_recv; - SMB_ASSERT(state->syscall_type == ASYNC_SYSCALL_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); @@ -450,7 +462,10 @@ static void async_recvall_callback(struct event_context *ev, req->private_data, struct async_syscall_state); struct param_recvall *p = &state->param.param_recvall; - SMB_ASSERT(state->syscall_type == ASYNC_SYSCALL_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, @@ -468,7 +483,10 @@ static void async_recvall_callback(struct event_context *ev, } p->received += state->result.result_ssize_t; - SMB_ASSERT(p->received <= p->length); + if (p->received > p->length) { + async_req_error(req, NT_STATUS_INTERNAL_ERROR); + return; + } if (p->received == p->length) { TALLOC_FREE(state->fde); @@ -517,60 +535,16 @@ 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; - - SMB_ASSERT(state->syscall_type == ASYNC_SYSCALL_CONNECT); - - 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; +struct async_connect_state { + int fd; + int result; + int sys_errno; + long old_sockflags; +}; - async_req_done(req); -} +static void async_connect_connected(struct event_context *ev, + struct fd_event *fde, uint16_t flags, + void *priv); /** * @brief async version of connect(2) @@ -585,47 +559,46 @@ static void async_connect_callback(struct event_context *ev, * 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 *async_connect_send(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; + struct async_connect_state *state; + struct fd_event *fde; + NTSTATUS status; - result = async_syscall_new(mem_ctx, ev, ASYNC_SYSCALL_CONNECT, &state); + result = async_req_new(mem_ctx); if (result == NULL) { return NULL; } - p = &state->param.param_connect; + state = talloc(result, struct async_connect_state); + if (state == NULL) { + goto fail; + } + result->private_data = state; /** * 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); + state->fd = fd; + state->sys_errno = 0; - if (p->old_sockflags == -1) { - if (async_post_status(result, map_nt_error_from_unix(errno))) { - return result; - } - TALLOC_FREE(result); - return NULL; + state->old_sockflags = sys_fcntl_long(fd, F_GETFL, 0); + if (state->old_sockflags == -1) { + goto post_errno; } - set_blocking(fd, true); + set_blocking(fd, false); - state->result.result_int = connect(fd, address, address_len); - - if (state->result.result_int == 0) { + state->result = connect(fd, address, address_len); + if (state->result == 0) { state->sys_errno = 0; - if (async_post_status(result, NT_STATUS_OK)) { - return result; - } - sys_fcntl_long(fd, F_SETFL, p->old_sockflags); - TALLOC_FREE(result); - return NULL; + status = NT_STATUS_OK; + goto post_status; } /** @@ -640,31 +613,93 @@ struct async_req *async_connect(TALLOC_CTX *mem_ctx, struct event_context *ev, errno == EISCONN || #endif errno == EAGAIN || errno == EINTR)) { + goto post_errno; + } - state->sys_errno = errno; - - if (async_post_status(result, map_nt_error_from_unix(errno))) { - return result; - } - sys_fcntl_long(fd, F_SETFL, p->old_sockflags); - TALLOC_FREE(result); - return NULL; + fde = event_add_fd(ev, state, fd, EVENT_FD_READ | EVENT_FD_WRITE, + async_connect_connected, result); + if (fde == NULL) { + status = NT_STATUS_NO_MEMORY; + goto post_status; } + return result; - 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; + post_errno: + state->sys_errno = errno; + status = map_nt_error_from_unix(state->sys_errno); + post_status: + sys_fcntl_long(fd, F_SETFL, state->old_sockflags); + if (!async_post_status(result, ev, status)) { + goto fail; } - result->private_data = state; + return result; + fail: + TALLOC_FREE(result); + return NULL; +} + +/** + * 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 + */ - state->param.param_connect.fd = fd; - state->param.param_connect.address = address; - state->param.param_connect.address_len = address_len; +static void async_connect_connected(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_connect_state *state = talloc_get_type_abort( + req->private_data, struct async_connect_state); - return result; + TALLOC_FREE(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(state->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(state->fd, F_SETFL, state->old_sockflags); + async_req_error(req, map_nt_error_from_unix(state->sys_errno)); + return; + } + + state->sys_errno = 0; + async_req_done(req); } +NTSTATUS async_connect_recv(struct async_req *req, int *perrno) +{ + struct async_connect_state *state = talloc_get_type_abort( + req->private_data, struct async_connect_state); + NTSTATUS status; + + sys_fcntl_long(state->fd, F_SETFL, state->old_sockflags); + + *perrno = state->sys_errno; + + if (async_req_is_error(req, &status)) { + return status; + } + if (state->sys_errno == 0) { + return NT_STATUS_OK; + } + return map_nt_error_from_unix(state->sys_errno); +} |