async_send->sendto, async_recv->recvfrom
authorVolker Lendecke <vl@samba.org>
Sun, 26 Dec 2010 15:03:58 +0000 (16:03 +0100)
committerVolker Lendecke <vl@samba.org>
Tue, 28 Dec 2010 11:59:11 +0000 (12:59 +0100)
lib/async_req/async_sock.c
lib/async_req/async_sock.h

index 18adb42a0cb6aeeedbaf26429155db50ed3d1cd8..9b2a625bbe86a3c4ac2268202ffe3e866d20b3b3 100644 (file)
 #define TALLOC_FREE(ctx) do { talloc_free(ctx); ctx=NULL; } while(0)
 #endif
 
-struct async_send_state {
+struct sendto_state {
        int fd;
        const void *buf;
        size_t len;
        int flags;
+       const struct sockaddr *addr;
+       socklen_t addr_len;
        ssize_t sent;
 };
 
-static void async_send_handler(struct tevent_context *ev,
+static void sendto_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 *sendto_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
+                              int fd, const void *buf, size_t len, int flags,
+                              const struct sockaddr *addr,
+                              socklen_t addr_len)
 {
        struct tevent_req *result;
-       struct async_send_state *state;
+       struct sendto_state *state;
        struct tevent_fd *fde;
 
-       result = tevent_req_create(mem_ctx, &state, struct async_send_state);
+       result = tevent_req_create(mem_ctx, &state, struct sendto_state);
        if (result == NULL) {
                return result;
        }
@@ -65,8 +67,10 @@ struct tevent_req *async_send_send(TALLOC_CTX *mem_ctx,
        state->buf = buf;
        state->len = len;
        state->flags = flags;
+       state->addr = addr;
+       state->addr_len = addr_len;
 
-       fde = tevent_add_fd(ev, state, fd, TEVENT_FD_WRITE, async_send_handler,
+       fde = tevent_add_fd(ev, state, fd, TEVENT_FD_WRITE, sendto_handler,
                            result);
        if (fde == NULL) {
                TALLOC_FREE(result);
@@ -75,16 +79,17 @@ struct tevent_req *async_send_send(TALLOC_CTX *mem_ctx,
        return result;
 }
 
-static void async_send_handler(struct tevent_context *ev,
+static void sendto_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);
+       struct sendto_state *state =
+               tevent_req_data(req, struct sendto_state);
 
-       state->sent = send(state->fd, state->buf, state->len, state->flags);
+       state->sent = sendto(state->fd, state->buf, state->len, state->flags,
+                            state->addr, state->addr_len);
        if ((state->sent == -1) && (errno == EINTR)) {
                /* retry */
                return;
@@ -96,10 +101,10 @@ static void async_send_handler(struct tevent_context *ev,
        tevent_req_done(req);
 }
 
-ssize_t async_send_recv(struct tevent_req *req, int *perrno)
+ssize_t sendto_recv(struct tevent_req *req, int *perrno)
 {
-       struct async_send_state *state =
-               tevent_req_data(req, struct async_send_state);
+       struct sendto_state *state =
+               tevent_req_data(req, struct sendto_state);
 
        if (tevent_req_is_unix_error(req, perrno)) {
                return -1;
@@ -107,27 +112,30 @@ ssize_t async_send_recv(struct tevent_req *req, int *perrno)
        return state->sent;
 }
 
-struct async_recv_state {
+struct recvfrom_state {
        int fd;
        void *buf;
        size_t len;
        int flags;
+       struct sockaddr *addr;
+       socklen_t *addr_len;
        ssize_t received;
 };
 
-static void async_recv_handler(struct tevent_context *ev,
+static void recvfrom_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 *recvfrom_send(TALLOC_CTX *mem_ctx,
+                                struct tevent_context *ev,
+                                int fd, void *buf, size_t len, int flags,
+                                struct sockaddr *addr, socklen_t *addr_len)
 {
        struct tevent_req *result;
-       struct async_recv_state *state;
+       struct recvfrom_state *state;
        struct tevent_fd *fde;
 
-       result = tevent_req_create(mem_ctx, &state, struct async_recv_state);
+       result = tevent_req_create(mem_ctx, &state, struct recvfrom_state);
        if (result == NULL) {
                return result;
        }
@@ -135,8 +143,10 @@ struct tevent_req *async_recv_send(TALLOC_CTX *mem_ctx,
        state->buf = buf;
        state->len = len;
        state->flags = flags;
+       state->addr = addr;
+       state->addr_len = addr_len;
 
-       fde = tevent_add_fd(ev, state, fd, TEVENT_FD_READ, async_recv_handler,
+       fde = tevent_add_fd(ev, state, fd, TEVENT_FD_READ, recvfrom_handler,
                            result);
        if (fde == NULL) {
                TALLOC_FREE(result);
@@ -145,17 +155,17 @@ struct tevent_req *async_recv_send(TALLOC_CTX *mem_ctx,
        return result;
 }
 
-static void async_recv_handler(struct tevent_context *ev,
+static void recvfrom_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);
+       struct recvfrom_state *state =
+               tevent_req_data(req, struct recvfrom_state);
 
-       state->received = recv(state->fd, state->buf, state->len,
-                              state->flags);
+       state->received = recvfrom(state->fd, state->buf, state->len,
+                                  state->flags, state->addr, state->addr_len);
        if ((state->received == -1) && (errno == EINTR)) {
                /* retry */
                return;
@@ -171,10 +181,10 @@ static void async_recv_handler(struct tevent_context *ev,
        tevent_req_done(req);
 }
 
-ssize_t async_recv_recv(struct tevent_req *req, int *perrno)
+ssize_t recvfrom_recv(struct tevent_req *req, int *perrno)
 {
-       struct async_recv_state *state =
-               tevent_req_data(req, struct async_recv_state);
+       struct recvfrom_state *state =
+               tevent_req_data(req, struct recvfrom_state);
 
        if (tevent_req_is_unix_error(req, perrno)) {
                return -1;
index e7ddff8c92a9cd0024203ce2cede854cef802f13..d87b2c144521992257f87b1f227052f822f59196 100644 (file)
 #include <talloc.h>
 #include <tevent.h>
 
-struct tevent_req *async_send_send(TALLOC_CTX *mem_ctx,
-                                  struct tevent_context *ev,
-                                  int fd, const void *buf, size_t len,
-                                  int flags);
-ssize_t async_send_recv(struct tevent_req *req, int *perrno);
-
-struct tevent_req *async_recv_send(TALLOC_CTX *mem_ctx,
-                                  struct tevent_context *ev,
-                                  int fd, void *buf, size_t len, int flags);
-ssize_t async_recv_recv(struct tevent_req *req, int *perrno);
+struct tevent_req *sendto_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
+                              int fd, const void *buf, size_t len, int flags,
+                              const struct sockaddr *addr,
+                              socklen_t addr_len);
+ssize_t sendto_recv(struct tevent_req *req, int *perrno);
+
+struct tevent_req *recvfrom_send(TALLOC_CTX *mem_ctx,
+                                struct tevent_context *ev,
+                                int fd, void *buf, size_t len, int flags,
+                                struct sockaddr *addr, socklen_t *addr_len);
+ssize_t recvfrom_recv(struct tevent_req *req, int *perrno);
 
 struct tevent_req *async_connect_send(TALLOC_CTX *mem_ctx,
                                      struct tevent_context *ev,