1
0
mirror of https://github.com/samba-team/samba.git synced 2025-02-02 09:47:23 +03:00

Convert open_socket_out_defer to tevent_req

This commit is contained in:
Volker Lendecke 2009-03-16 19:15:26 +01:00
parent cb9effdc63
commit 20cee26a3d
3 changed files with 73 additions and 58 deletions

View File

@ -1396,13 +1396,13 @@ struct tevent_req *open_socket_out_send(TALLOC_CTX *mem_ctx,
uint16_t port,
int timeout);
NTSTATUS open_socket_out_recv(struct tevent_req *req, int *pfd);
struct async_req *open_socket_out_defer_send(TALLOC_CTX *mem_ctx,
struct event_context *ev,
struct timeval wait_time,
const struct sockaddr_storage *pss,
uint16_t port,
int timeout);
NTSTATUS open_socket_out_defer_recv(struct async_req *req, int *pfd);
struct tevent_req *open_socket_out_defer_send(TALLOC_CTX *mem_ctx,
struct event_context *ev,
struct timeval wait_time,
const struct sockaddr_storage *pss,
uint16_t port,
int timeout);
NTSTATUS open_socket_out_defer_recv(struct tevent_req *req, int *pfd);
bool open_any_socket_out(struct sockaddr_storage *addrs, int num_addrs,
int timeout, int *fd_index, int *fd);
int open_udp_socket(const char *host, int port);

View File

@ -1155,19 +1155,19 @@ struct open_socket_out_defer_state {
static void open_socket_out_defer_waited(struct tevent_req *subreq);
static void open_socket_out_defer_connected(struct tevent_req *subreq);
struct async_req *open_socket_out_defer_send(TALLOC_CTX *mem_ctx,
struct event_context *ev,
struct timeval wait_time,
const struct sockaddr_storage *pss,
uint16_t port,
int timeout)
struct tevent_req *open_socket_out_defer_send(TALLOC_CTX *mem_ctx,
struct event_context *ev,
struct timeval wait_time,
const struct sockaddr_storage *pss,
uint16_t port,
int timeout)
{
struct async_req *result;
struct tevent_req *subreq;
struct tevent_req *req, *subreq;
struct open_socket_out_defer_state *state;
if (!async_req_setup(mem_ctx, &result, &state,
struct open_socket_out_defer_state)) {
req = tevent_req_create(mem_ctx, &state,
struct open_socket_out_defer_state);
if (req == NULL) {
return NULL;
}
state->ev = ev;
@ -1181,31 +1181,31 @@ struct async_req *open_socket_out_defer_send(TALLOC_CTX *mem_ctx,
if (subreq == NULL) {
goto fail;
}
tevent_req_set_callback(subreq, open_socket_out_defer_waited, result);
return result;
tevent_req_set_callback(subreq, open_socket_out_defer_waited, req);
return req;
fail:
TALLOC_FREE(result);
TALLOC_FREE(req);
return NULL;
}
static void open_socket_out_defer_waited(struct tevent_req *subreq)
{
struct async_req *req = tevent_req_callback_data(
subreq, struct async_req);
struct open_socket_out_defer_state *state = talloc_get_type_abort(
req->private_data, struct open_socket_out_defer_state);
struct tevent_req *req = tevent_req_callback_data(
subreq, struct tevent_req);
struct open_socket_out_defer_state *state = tevent_req_data(
req, struct open_socket_out_defer_state);
bool ret;
ret = tevent_wakeup_recv(subreq);
TALLOC_FREE(subreq);
if (!ret) {
async_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
return;
}
subreq = open_socket_out_send(state, state->ev, &state->ss,
state->port, state->timeout);
if (async_req_nomem(subreq, req)) {
if (tevent_req_nomem(subreq, req)) {
return;
}
tevent_req_set_callback(subreq, open_socket_out_defer_connected, req);
@ -1213,28 +1213,28 @@ static void open_socket_out_defer_waited(struct tevent_req *subreq)
static void open_socket_out_defer_connected(struct tevent_req *subreq)
{
struct async_req *req =
tevent_req_callback_data(subreq, struct async_req);
struct open_socket_out_defer_state *state = talloc_get_type_abort(
req->private_data, struct open_socket_out_defer_state);
struct tevent_req *req = tevent_req_callback_data(
subreq, struct tevent_req);
struct open_socket_out_defer_state *state = tevent_req_data(
req, struct open_socket_out_defer_state);
NTSTATUS status;
status = open_socket_out_recv(subreq, &state->fd);
TALLOC_FREE(subreq);
if (!NT_STATUS_IS_OK(status)) {
async_req_nterror(req, status);
tevent_req_nterror(req, status);
return;
}
async_req_done(req);
tevent_req_done(req);
}
NTSTATUS open_socket_out_defer_recv(struct async_req *req, int *pfd)
NTSTATUS open_socket_out_defer_recv(struct tevent_req *req, int *pfd)
{
struct open_socket_out_defer_state *state = talloc_get_type_abort(
req->private_data, struct open_socket_out_defer_state);
struct open_socket_out_defer_state *state = tevent_req_data(
req, struct open_socket_out_defer_state);
NTSTATUS status;
if (async_req_is_nterror(req, &status)) {
if (tevent_req_is_nterror(req, &status)) {
return status;
}
*pfd = state->fd;

View File

@ -1785,15 +1785,20 @@ bool cli_session_request(struct cli_state *cli,
return(True);
}
static void smb_sock_connected(struct async_req *req)
struct fd_struct {
int fd;
};
static void smb_sock_connected(struct tevent_req *req)
{
int *pfd = (int *)req->async.priv;
struct fd_struct *pfd = tevent_req_callback_data(
req, struct fd_struct);
int fd;
NTSTATUS status;
status = open_socket_out_defer_recv(req, &fd);
if (NT_STATUS_IS_OK(status)) {
*pfd = fd;
pfd->fd = fd;
}
}
@ -1801,10 +1806,9 @@ static NTSTATUS open_smb_socket(const struct sockaddr_storage *pss,
uint16_t *port, int timeout, int *pfd)
{
struct event_context *ev;
struct async_req *r139, *r445;
int fd139 = -1;
int fd445 = -1;
NTSTATUS status;
struct tevent_req *r139, *r445;
struct fd_struct *fd139, *fd445;
NTSTATUS status = NT_STATUS_NO_MEMORY;
if (*port != 0) {
return open_socket_out(pss, *port, timeout, pfd);
@ -1815,43 +1819,54 @@ static NTSTATUS open_smb_socket(const struct sockaddr_storage *pss,
return NT_STATUS_NO_MEMORY;
}
fd139 = talloc(ev, struct fd_struct);
if (fd139 == NULL) {
goto done;
}
fd139->fd = -1;
fd445 = talloc(ev, struct fd_struct);
if (fd445 == NULL) {
goto done;
}
fd445->fd = -1;
r445 = open_socket_out_defer_send(ev, ev, timeval_set(0, 0),
pss, 445, timeout);
r139 = open_socket_out_defer_send(ev, ev, timeval_set(0, 3000),
pss, 139, timeout);
if ((r445 == NULL) || (r139 == NULL)) {
status = NT_STATUS_NO_MEMORY;
goto done;
}
r445->async.fn = smb_sock_connected;
r445->async.priv = &fd445;
r139->async.fn = smb_sock_connected;
r139->async.priv = &fd139;
tevent_req_set_callback(r445, smb_sock_connected, fd445);
tevent_req_set_callback(r139, smb_sock_connected, fd139);
while ((fd139 == -1) && (r139->state < ASYNC_REQ_DONE)
&& (fd445 == -1) && (r445->state < ASYNC_REQ_DONE)) {
while ((fd139->fd == -1)
&& tevent_req_is_in_progress(r139)
&& (fd445->fd == -1)
&& tevent_req_is_in_progress(r445)) {
event_loop_once(ev);
}
if ((fd139 != -1) && (fd445 != -1)) {
close(fd139);
fd139 = -1;
if ((fd139->fd != -1) && (fd445->fd != -1)) {
close(fd139->fd);
fd139->fd = -1;
}
if (fd445 != -1) {
if (fd445->fd != -1) {
*port = 445;
*pfd = fd445;
*pfd = fd445->fd;
status = NT_STATUS_OK;
goto done;
}
if (fd139 != -1) {
if (fd139->fd != -1) {
*port = 139;
*pfd = fd139;
*pfd = fd139->fd;
status = NT_STATUS_OK;
goto done;
}
status = open_socket_out_defer_recv(r445, &fd445);
status = open_socket_out_defer_recv(r445, &fd445->fd);
done:
TALLOC_FREE(ev);
return status;