libnmb: Make nb_packet_read_recv return a talloc'ed pkt
authorVolker Lendecke <vl@samba.org>
Tue, 16 Jan 2018 14:50:19 +0000 (15:50 +0100)
committerJeremy Allison <jra@samba.org>
Tue, 23 Jan 2018 23:58:17 +0000 (00:58 +0100)
This saves a few explicit destructors only doing free_packet()

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
source3/libsmb/clidgram.c
source3/libsmb/namequery.c
source3/libsmb/unexpected.c
source3/libsmb/unexpected.h

index 8f0dba3..4ae57a3 100644 (file)
@@ -397,7 +397,7 @@ static void nbt_getdc_got_response(struct tevent_req *subreq)
        NTSTATUS status;
        bool ret;
 
-       status = nb_packet_read_recv(subreq, &p);
+       status = nb_packet_read_recv(subreq, state, &p);
        TALLOC_FREE(subreq);
        if (tevent_req_nterror(req, status)) {
                return;
@@ -406,7 +406,6 @@ static void nbt_getdc_got_response(struct tevent_req *subreq)
        ret = parse_getdc_response(p, state, state->domain_name,
                                   &state->nt_version, &state->dc_name,
                                   &state->samlogon_response);
-       free_packet(p);
        if (!ret) {
                tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
                return;
index 6107e8f..b616a64 100644 (file)
@@ -309,7 +309,6 @@ struct sock_packet_read_state {
        struct packet_struct *packet;
 };
 
-static int sock_packet_read_state_destructor(struct sock_packet_read_state *s);
 static void sock_packet_read_got_packet(struct tevent_req *subreq);
 static void sock_packet_read_got_socket(struct tevent_req *subreq);
 
@@ -331,7 +330,6 @@ static struct tevent_req *sock_packet_read_send(
        if (req == NULL) {
                return NULL;
        }
-       talloc_set_destructor(state, sock_packet_read_state_destructor);
        state->ev = ev;
        state->reader = reader;
        state->sock = sock;
@@ -359,15 +357,6 @@ static struct tevent_req *sock_packet_read_send(
        return req;
 }
 
-static int sock_packet_read_state_destructor(struct sock_packet_read_state *s)
-{
-       if (s->packet != NULL) {
-               free_packet(s->packet);
-               s->packet = NULL;
-       }
-       return 0;
-}
-
 static void sock_packet_read_got_packet(struct tevent_req *subreq)
 {
        struct tevent_req *req = tevent_req_callback_data(
@@ -376,7 +365,7 @@ static void sock_packet_read_got_packet(struct tevent_req *subreq)
                req, struct sock_packet_read_state);
        NTSTATUS status;
 
-       status = nb_packet_read_recv(subreq, &state->packet);
+       status = nb_packet_read_recv(subreq, state, &state->packet);
 
        TALLOC_FREE(state->reader_req);
 
@@ -398,8 +387,7 @@ static void sock_packet_read_got_packet(struct tevent_req *subreq)
            !state->validator(state->packet, state->private_data)) {
                DEBUG(10, ("validator failed\n"));
 
-               free_packet(state->packet);
-               state->packet = NULL;
+               TALLOC_FREE(state->packet);
 
                state->reader_req = nb_packet_read_send(state, state->ev,
                                                        state->reader);
@@ -460,8 +448,9 @@ static void sock_packet_read_got_socket(struct tevent_req *subreq)
                return;
        }
 
-       state->packet = parse_packet((char *)state->buf, received, state->type,
-                                    addr.sin.sin_addr, addr.sin.sin_port);
+       state->packet = parse_packet_talloc(
+               state, (char *)state->buf, received, state->type,
+               addr.sin.sin_addr, addr.sin.sin_port);
        if (state->packet == NULL) {
                DEBUG(10, ("parse_packet failed\n"));
                goto retry;
@@ -483,10 +472,7 @@ static void sock_packet_read_got_socket(struct tevent_req *subreq)
        return;
 
 retry:
-       if (state->packet != NULL) {
-               free_packet(state->packet);
-               state->packet = NULL;
-       }
+       TALLOC_FREE(state->packet);
        TALLOC_FREE(state->buf);
        TALLOC_FREE(state->addr);
 
@@ -499,6 +485,7 @@ retry:
 }
 
 static NTSTATUS sock_packet_read_recv(struct tevent_req *req,
+                                     TALLOC_CTX *mem_ctx,
                                      struct packet_struct **ppacket)
 {
        struct sock_packet_read_state *state = tevent_req_data(
@@ -508,8 +495,7 @@ static NTSTATUS sock_packet_read_recv(struct tevent_req *req,
        if (tevent_req_is_nterror(req, &status)) {
                return status;
        }
-       *ppacket = state->packet;
-       state->packet = NULL;
+       *ppacket = talloc_move(mem_ctx, &state->packet);
        return NT_STATUS_OK;
 }
 
@@ -532,7 +518,6 @@ struct nb_trans_state {
        struct packet_struct *packet;
 };
 
-static int nb_trans_state_destructor(struct nb_trans_state *s);
 static void nb_trans_got_reader(struct tevent_req *subreq);
 static void nb_trans_done(struct tevent_req *subreq);
 static void nb_trans_sent(struct tevent_req *subreq);
@@ -564,7 +549,6 @@ static struct tevent_req *nb_trans_send(
        if (req == NULL) {
                return NULL;
        }
-       talloc_set_destructor(state, nb_trans_state_destructor);
        state->ev = ev;
        state->buf = buf;
        state->buflen = buflen;
@@ -604,15 +588,6 @@ static struct tevent_req *nb_trans_send(
        return req;
 }
 
-static int nb_trans_state_destructor(struct nb_trans_state *s)
-{
-       if (s->packet != NULL) {
-               free_packet(s->packet);
-               s->packet = NULL;
-       }
-       return 0;
-}
-
 static void nb_trans_got_reader(struct tevent_req *subreq)
 {
        struct tevent_req *req = tevent_req_callback_data(
@@ -704,7 +679,7 @@ static void nb_trans_done(struct tevent_req *subreq)
                req, struct nb_trans_state);
        NTSTATUS status;
 
-       status = sock_packet_read_recv(subreq, &state->packet);
+       status = sock_packet_read_recv(subreq, state, &state->packet);
        TALLOC_FREE(subreq);
        if (tevent_req_nterror(req, status)) {
                return;
@@ -712,7 +687,7 @@ static void nb_trans_done(struct tevent_req *subreq)
        tevent_req_done(req);
 }
 
-static NTSTATUS nb_trans_recv(struct tevent_req *req,
+static NTSTATUS nb_trans_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
                              struct packet_struct **ppacket)
 {
        struct nb_trans_state *state = tevent_req_data(
@@ -722,8 +697,7 @@ static NTSTATUS nb_trans_recv(struct tevent_req *req,
        if (tevent_req_is_nterror(req, &status)) {
                return status;
        }
-       *ppacket = state->packet;
-       state->packet = NULL;
+       *ppacket = talloc_move(mem_ctx, &state->packet);
        return NT_STATUS_OK;
 }
 
@@ -740,8 +714,6 @@ struct node_status_query_state {
        struct packet_struct *packet;
 };
 
-static int node_status_query_state_destructor(
-       struct node_status_query_state *s);
 static bool node_status_query_validator(struct packet_struct *p,
                                        void *private_data);
 static void node_status_query_done(struct tevent_req *subreq);
@@ -762,7 +734,6 @@ struct tevent_req *node_status_query_send(TALLOC_CTX *mem_ctx,
        if (req == NULL) {
                return NULL;
        }
-       talloc_set_destructor(state, node_status_query_state_destructor);
 
        if (addr->ss_family != AF_INET) {
                /* Can't do node status to IPv6 */
@@ -837,16 +808,6 @@ static bool node_status_query_validator(struct packet_struct *p,
        return true;
 }
 
-static int node_status_query_state_destructor(
-       struct node_status_query_state *s)
-{
-       if (s->packet != NULL) {
-               free_packet(s->packet);
-               s->packet = NULL;
-       }
-       return 0;
-}
-
 static void node_status_query_done(struct tevent_req *subreq)
 {
        struct tevent_req *req = tevent_req_callback_data(
@@ -855,7 +816,7 @@ static void node_status_query_done(struct tevent_req *subreq)
                req, struct node_status_query_state);
        NTSTATUS status;
 
-       status = nb_trans_recv(subreq, &state->packet);
+       status = nb_trans_recv(subreq, state, &state->packet);
        TALLOC_FREE(subreq);
        if (tevent_req_nterror(req, status)) {
                return;
@@ -1501,7 +1462,7 @@ static void name_query_done(struct tevent_req *subreq)
        NTSTATUS status;
        struct packet_struct *p = NULL;
 
-       status = nb_trans_recv(subreq, &p);
+       status = nb_trans_recv(subreq, state, &p);
        TALLOC_FREE(subreq);
        if (tevent_req_nterror(req, status)) {
                return;
@@ -1510,13 +1471,6 @@ static void name_query_done(struct tevent_req *subreq)
                tevent_req_nterror(req, state->validate_error);
                return;
        }
-       if (p != NULL) {
-               /*
-                * Free the packet here, we've collected the response in the
-                * validator
-                */
-               free_packet(p);
-       }
        tevent_req_done(req);
 }
 
index 16d1f67..ac6c1cf 100644 (file)
@@ -715,7 +715,7 @@ static void nb_packet_read_done(struct tevent_req *subreq)
        tevent_req_done(req);
 }
 
-NTSTATUS nb_packet_read_recv(struct tevent_req *req,
+NTSTATUS nb_packet_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
                             struct packet_struct **ppacket)
 {
        struct nb_packet_read_state *state = tevent_req_data(
@@ -731,7 +731,8 @@ NTSTATUS nb_packet_read_recv(struct tevent_req *req,
 
        memcpy(&hdr, state->buf, sizeof(hdr));
 
-       packet = parse_packet(
+       packet = parse_packet_talloc(
+               mem_ctx,
                (char *)state->buf + sizeof(struct nb_packet_client_header),
                state->buflen - sizeof(struct nb_packet_client_header),
                state->hdr.type, state->hdr.ip, state->hdr.port);
@@ -739,6 +740,7 @@ NTSTATUS nb_packet_read_recv(struct tevent_req *req,
                tevent_req_received(req);
                return NT_STATUS_INVALID_NETWORK_RESPONSE;
        }
+
        *ppacket = packet;
        tevent_req_received(req);
        return NT_STATUS_OK;
index a40a507..270976b 100644 (file)
@@ -43,7 +43,7 @@ NTSTATUS nb_packet_reader_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
 struct tevent_req *nb_packet_read_send(TALLOC_CTX *mem_ctx,
                                       struct tevent_context *ev,
                                       struct nb_packet_reader *reader);
-NTSTATUS nb_packet_read_recv(struct tevent_req *req,
+NTSTATUS nb_packet_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
                             struct packet_struct **ppacket);
 
 #endif