tsocket: tdgram move input checks to the common code as there're needed for all backends
authorStefan Metzmacher <metze@samba.org>
Tue, 14 Apr 2009 08:44:25 +0000 (10:44 +0200)
committerStefan Metzmacher <metze@samba.org>
Fri, 1 May 2009 15:38:25 +0000 (17:38 +0200)
We need to make sure that we free the sendto and recvfrom requests before
the tdgram_context

metze

tsocket/tsocket.c
tsocket/tsocket_bsd.c

index 11ae6ce9ed194ec863ec0cf161fe8536fa889102..606642e98392e82031fe54031f5a1a785a79e66c 100644 (file)
@@ -73,8 +73,24 @@ struct tdgram_context {
        const char *location;
        const struct tdgram_context_ops *ops;
        void *private_data;
+
+       struct tevent_req *recvfrom_req;
+       struct tevent_req *sendto_req;
 };
 
+static int tdgram_context_destructor(struct tdgram_context *dgram)
+{
+       if (dgram->recvfrom_req) {
+               tevent_req_received(dgram->recvfrom_req);
+       }
+
+       if (dgram->sendto_req) {
+               tevent_req_received(dgram->sendto_req);
+       }
+
+       return 0;
+}
+
 struct tdgram_context *_tdgram_context_create(TALLOC_CTX *mem_ctx,
                                        const struct tdgram_context_ops *ops,
                                        void *pstate,
@@ -90,8 +106,10 @@ struct tdgram_context *_tdgram_context_create(TALLOC_CTX *mem_ctx,
        if (dgram == NULL) {
                return NULL;
        }
-       dgram->location = location;
-       dgram->ops      = ops;
+       dgram->location         = location;
+       dgram->ops              = ops;
+       dgram->recvfrom_req     = NULL;
+       dgram->sendto_req       = NULL;
 
        state = talloc_size(dgram, psize);
        if (state == NULL) {
@@ -102,6 +120,8 @@ struct tdgram_context *_tdgram_context_create(TALLOC_CTX *mem_ctx,
 
        dgram->private_data = state;
 
+       talloc_set_destructor(dgram, tdgram_context_destructor);
+
        *ppstate = state;
        return dgram;
 }
@@ -113,11 +133,21 @@ void *_tdgram_context_data(struct tdgram_context *dgram)
 
 struct tdgram_recvfrom_state {
        const struct tdgram_context_ops *ops;
+       struct tdgram_context *dgram;
        uint8_t *buf;
        size_t len;
        struct tsocket_address *src;
 };
 
+static int tdgram_recvfrom_destructor(struct tdgram_recvfrom_state *state)
+{
+       if (state->dgram) {
+               state->dgram->recvfrom_req = NULL;
+       }
+
+       return 0;
+}
+
 static void tdgram_recvfrom_done(struct tevent_req *subreq);
 
 struct tevent_req *tdgram_recvfrom_send(TALLOC_CTX *mem_ctx,
@@ -135,6 +165,18 @@ struct tevent_req *tdgram_recvfrom_send(TALLOC_CTX *mem_ctx,
        }
 
        state->ops = dgram->ops;
+       state->dgram = dgram;
+       state->buf = NULL;
+       state->len = 0;
+       state->src = NULL;
+
+       if (dgram->recvfrom_req) {
+               tevent_req_error(req, EBUSY);
+               goto post;
+       }
+       dgram->recvfrom_req = req;
+
+       talloc_set_destructor(state, tdgram_recvfrom_destructor);
 
        subreq = state->ops->recvfrom_send(state, ev, dgram);
        if (tevent_req_nomem(subreq, req)) {
@@ -195,9 +237,19 @@ ssize_t tdgram_recvfrom_recv(struct tevent_req *req,
 
 struct tdgram_sendto_state {
        const struct tdgram_context_ops *ops;
+       struct tdgram_context *dgram;
        ssize_t ret;
 };
 
+static int tdgram_sendto_destructor(struct tdgram_sendto_state *state)
+{
+       if (state->dgram) {
+               state->dgram->sendto_req = NULL;
+       }
+
+       return 0;
+}
+
 static void tdgram_sendto_done(struct tevent_req *subreq);
 
 struct tevent_req *tdgram_sendto_send(TALLOC_CTX *mem_ctx,
@@ -217,6 +269,7 @@ struct tevent_req *tdgram_sendto_send(TALLOC_CTX *mem_ctx,
        }
 
        state->ops = dgram->ops;
+       state->dgram = dgram;
        state->ret = -1;
 
        if (len == 0) {
@@ -224,6 +277,14 @@ struct tevent_req *tdgram_sendto_send(TALLOC_CTX *mem_ctx,
                goto post;
        }
 
+       if (dgram->sendto_req) {
+               tevent_req_error(req, EBUSY);
+               goto post;
+       }
+       dgram->sendto_req = req;
+
+       talloc_set_destructor(state, tdgram_sendto_destructor);
+
        subreq = state->ops->sendto_send(state, ev, dgram,
                                         buf, len, dst);
        if (tevent_req_nomem(subreq, req)) {
@@ -296,6 +357,11 @@ struct tevent_req *tdgram_disconnect_send(TALLOC_CTX *mem_ctx,
 
        state->ops = dgram->ops;
 
+       if (dgram->recvfrom_req || dgram->sendto_req) {
+               tevent_req_error(req, EBUSY);
+               goto post;
+       }
+
        subreq = state->ops->disconnect_send(state, ev, dgram);
        if (tevent_req_nomem(subreq, req)) {
                goto post;
index 52cc5cc1cfba5e7b15a51f7178e270e9d92aee47..34ae60ee09050bb493c1b858330e64d3773a9734 100644 (file)
@@ -557,9 +557,6 @@ struct tdgram_bsd {
        void (*readable_handler)(void *private_data);
        void *writeable_private;
        void (*writeable_handler)(void *private_data);
-
-       struct tevent_req *read_req;
-       struct tevent_req *write_req;
 };
 
 static void tdgram_bsd_fde_handler(struct tevent_context *ev,
@@ -698,7 +695,6 @@ static int tdgram_bsd_recvfrom_destructor(struct tdgram_bsd_recvfrom_state *stat
        struct tdgram_bsd *bsds = tdgram_context_data(state->dgram,
                                  struct tdgram_bsd);
 
-       bsds->read_req = NULL;
        tdgram_bsd_set_readable_handler(bsds, NULL, NULL, NULL);
 
        return 0;
@@ -726,12 +722,6 @@ static struct tevent_req *tdgram_bsd_recvfrom_send(TALLOC_CTX *mem_ctx,
        state->len      = 0;
        state->src      = NULL;
 
-       if (bsds->read_req) {
-               tevent_req_error(req, EBUSY);
-               goto post;
-       }
-       bsds->read_req = req;
-
        talloc_set_destructor(state, tdgram_bsd_recvfrom_destructor);
 
        if (bsds->fd == -1) {
@@ -876,8 +866,8 @@ static int tdgram_bsd_sendto_destructor(struct tdgram_bsd_sendto_state *state)
        struct tdgram_bsd *bsds = tdgram_context_data(state->dgram,
                                  struct tdgram_bsd);
 
-       bsds->write_req = NULL;
        tdgram_bsd_set_writeable_handler(bsds, NULL, NULL, NULL);
+
        return 0;
 }
 
@@ -907,12 +897,6 @@ static struct tevent_req *tdgram_bsd_sendto_send(TALLOC_CTX *mem_ctx,
        state->dst      = dst;
        state->ret      = -1;
 
-       if (bsds->write_req) {
-               tevent_req_error(req, EBUSY);
-               goto post;
-       }
-       bsds->write_req = req;
-
        talloc_set_destructor(state, tdgram_bsd_sendto_destructor);
 
        if (bsds->fd == -1) {
@@ -1026,11 +1010,6 @@ static struct tevent_req *tdgram_bsd_disconnect_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       if (bsds->read_req || bsds->write_req) {
-               tevent_req_error(req, EBUSY);
-               goto post;
-       }
-
        if (bsds->fd == -1) {
                tevent_req_error(req, ENOTCONN);
                goto post;