diff --git a/libcli/nbt/nbtsocket.c b/libcli/nbt/nbtsocket.c index d7abb1bf30b..711e39cbdc5 100644 --- a/libcli/nbt/nbtsocket.c +++ b/libcli/nbt/nbtsocket.c @@ -339,13 +339,12 @@ _PUBLIC_ struct nbt_name_socket *nbt_name_socket_init(TALLOC_CTX *mem_ctx, nbtsock->event_ctx = event_ctx; if (nbtsock->event_ctx == NULL) goto failed; - status = socket_create("ip", SOCKET_TYPE_DGRAM, &nbtsock->sock, 0); + status = socket_create(nbtsock, "ip", SOCKET_TYPE_DGRAM, + &nbtsock->sock, 0); if (!NT_STATUS_IS_OK(status)) goto failed; socket_set_option(nbtsock->sock, "SO_BROADCAST", "1"); - talloc_steal(nbtsock, nbtsock->sock); - nbtsock->idr = idr_init(nbtsock); if (nbtsock->idr == NULL) goto failed; diff --git a/source4/auth/kerberos/krb5_init_context.c b/source4/auth/kerberos/krb5_init_context.c index e2c837abdce..5e771a87cc5 100644 --- a/source4/auth/kerberos/krb5_init_context.c +++ b/source4/auth/kerberos/krb5_init_context.c @@ -261,10 +261,14 @@ static krb5_error_code smb_krb5_send_and_recv_func_int(krb5_context context, status = NT_STATUS_INVALID_PARAMETER; switch (hi->proto) { case KRB5_KRBHST_UDP: - status = socket_create(name, SOCKET_TYPE_DGRAM, &smb_krb5->sock, 0); + status = socket_create(smb_krb5, name, + SOCKET_TYPE_DGRAM, + &smb_krb5->sock, 0); break; case KRB5_KRBHST_TCP: - status = socket_create(name, SOCKET_TYPE_STREAM, &smb_krb5->sock, 0); + status = socket_create(smb_krb5, name, + SOCKET_TYPE_STREAM, + &smb_krb5->sock, 0); break; case KRB5_KRBHST_HTTP: TALLOC_FREE(frame); @@ -275,8 +279,6 @@ static krb5_error_code smb_krb5_send_and_recv_func_int(krb5_context context, continue; } - talloc_steal(smb_krb5, smb_krb5->sock); - remote_addr = socket_address_from_sockaddr(smb_krb5, a->ai_addr, a->ai_addrlen); if (!remote_addr) { talloc_free(smb_krb5); diff --git a/source4/lib/socket/connect_multi.c b/source4/lib/socket/connect_multi.c index c8231b3cff3..b29fffb33b4 100644 --- a/source4/lib/socket/connect_multi.c +++ b/source4/lib/socket/connect_multi.c @@ -152,8 +152,9 @@ static void connect_multi_next_socket(struct composite_context *result) if (composite_nomem(state, result)) return; state->result = result; - result->status = socket_create(multi->server_address[multi->current_address]->family, - SOCKET_TYPE_STREAM, &state->sock, 0); + result->status = socket_create( + state, multi->server_address[multi->current_address]->family, + SOCKET_TYPE_STREAM, &state->sock, 0); if (!composite_is_ok(result)) return; state->addr = socket_address_copy(state, multi->server_address[multi->current_address]); @@ -161,8 +162,6 @@ static void connect_multi_next_socket(struct composite_context *result) socket_address_set_port(state->addr, multi->ports[multi->current_port]); - talloc_steal(state, state->sock); - creq = socket_connect_send(state->sock, NULL, state->addr, 0, result->event_ctx); diff --git a/source4/lib/socket/socket.c b/source4/lib/socket/socket.c index 42eb53abcef..98f796e3fed 100644 --- a/source4/lib/socket/socket.c +++ b/source4/lib/socket/socket.c @@ -96,7 +96,8 @@ _PUBLIC_ NTSTATUS socket_create_with_ops(TALLOC_CTX *mem_ctx, const struct socke return NT_STATUS_OK; } -_PUBLIC_ NTSTATUS socket_create(const char *name, enum socket_type type, +_PUBLIC_ NTSTATUS socket_create(TALLOC_CTX *mem_ctx, + const char *name, enum socket_type type, struct socket_context **new_sock, uint32_t flags) { const struct socket_ops *ops; @@ -106,7 +107,7 @@ _PUBLIC_ NTSTATUS socket_create(const char *name, enum socket_type type, return NT_STATUS_INVALID_PARAMETER; } - return socket_create_with_ops(NULL, ops, new_sock, type, flags); + return socket_create_with_ops(mem_ctx, ops, new_sock, type, flags); } _PUBLIC_ NTSTATUS socket_connect(struct socket_context *sock, diff --git a/source4/lib/socket/socket.h b/source4/lib/socket/socket.h index 50a20d90911..a492bc10367 100644 --- a/source4/lib/socket/socket.h +++ b/source4/lib/socket/socket.h @@ -133,7 +133,8 @@ struct tsocket_address; NTSTATUS socket_create_with_ops(TALLOC_CTX *mem_ctx, const struct socket_ops *ops, struct socket_context **new_sock, enum socket_type type, uint32_t flags); -NTSTATUS socket_create(const char *name, enum socket_type type, +NTSTATUS socket_create(TALLOC_CTX *mem_ctx, + const char *name, enum socket_type type, struct socket_context **new_sock, uint32_t flags); NTSTATUS socket_connect(struct socket_context *sock, const struct socket_address *my_address, diff --git a/source4/lib/socket/testsuite.c b/source4/lib/socket/testsuite.c index 6ebbd1227e8..1df96e3ccf7 100644 --- a/source4/lib/socket/testsuite.c +++ b/source4/lib/socket/testsuite.c @@ -45,13 +45,11 @@ static bool test_udp(struct torture_context *tctx) load_interface_list(tctx, tctx->lp_ctx, &ifaces); - status = socket_create("ip", SOCKET_TYPE_DGRAM, &sock1, 0); + status = socket_create(mem_ctx, "ip", SOCKET_TYPE_DGRAM, &sock1, 0); torture_assert_ntstatus_ok(tctx, status, "creating DGRAM IP socket 1"); - talloc_steal(mem_ctx, sock1); - status = socket_create("ip", SOCKET_TYPE_DGRAM, &sock2, 0); + status = socket_create(mem_ctx, "ip", SOCKET_TYPE_DGRAM, &sock2, 0); torture_assert_ntstatus_ok(tctx, status, "creating DGRAM IP socket 1"); - talloc_steal(mem_ctx, sock2); localhost = socket_address_from_strings(sock1, sock1->backend_name, iface_list_best_ip(ifaces, "127.0.0.1"), 0); @@ -128,13 +126,11 @@ static bool test_tcp(struct torture_context *tctx) struct tevent_context *ev = tctx->ev; struct interface *ifaces; - status = socket_create("ip", SOCKET_TYPE_STREAM, &sock1, 0); + status = socket_create(mem_ctx, "ip", SOCKET_TYPE_STREAM, &sock1, 0); torture_assert_ntstatus_ok(tctx, status, "creating IP stream socket 1"); - talloc_steal(mem_ctx, sock1); - status = socket_create("ip", SOCKET_TYPE_STREAM, &sock2, 0); + status = socket_create(mem_ctx, "ip", SOCKET_TYPE_STREAM, &sock2, 0); torture_assert_ntstatus_ok(tctx, status, "creating IP stream socket 1"); - talloc_steal(mem_ctx, sock2); load_interface_list(tctx, tctx->lp_ctx, &ifaces); localhost = socket_address_from_strings(sock1, sock1->backend_name, diff --git a/source4/libcli/dgram/dgramsocket.c b/source4/libcli/dgram/dgramsocket.c index b6e7dd12c7c..154a6670948 100644 --- a/source4/libcli/dgram/dgramsocket.c +++ b/source4/libcli/dgram/dgramsocket.c @@ -168,13 +168,12 @@ struct nbt_dgram_socket *nbt_dgram_socket_init(TALLOC_CTX *mem_ctx, dgmsock->event_ctx = event_ctx; if (dgmsock->event_ctx == NULL) goto failed; - status = socket_create("ip", SOCKET_TYPE_DGRAM, &dgmsock->sock, 0); + status = socket_create(dgmsock, "ip", SOCKET_TYPE_DGRAM, + &dgmsock->sock, 0); if (!NT_STATUS_IS_OK(status)) goto failed; socket_set_option(dgmsock->sock, "SO_BROADCAST", "1"); - talloc_steal(dgmsock, dgmsock->sock); - dgmsock->fde = tevent_add_fd(dgmsock->event_ctx, dgmsock, socket_get_fd(dgmsock->sock), 0, dgm_socket_handler, dgmsock); diff --git a/source4/libcli/ldap/ldap_client.c b/source4/libcli/ldap/ldap_client.c index b5f5da6fa00..1cbcd0d42d5 100644 --- a/source4/libcli/ldap/ldap_client.c +++ b/source4/libcli/ldap/ldap_client.c @@ -413,11 +413,12 @@ _PUBLIC_ struct composite_context *ldap_connect_send(struct ldap_connection *con struct socket_address *unix_addr; char path[1025]; char *end = NULL; - NTSTATUS status = socket_create("unix", SOCKET_TYPE_STREAM, &state->sock, 0); + NTSTATUS status = socket_create(state, "unix", + SOCKET_TYPE_STREAM, + &state->sock, 0); if (!NT_STATUS_IS_OK(status)) { return NULL; } - talloc_steal(state, state->sock); SMB_ASSERT(sizeof(protocol)>10); SMB_ASSERT(sizeof(path)>1024); diff --git a/source4/librpc/rpc/dcerpc_sock.c b/source4/librpc/rpc/dcerpc_sock.c index 6401534806e..e7ecca73e3c 100644 --- a/source4/librpc/rpc/dcerpc_sock.c +++ b/source4/librpc/rpc/dcerpc_sock.c @@ -151,11 +151,10 @@ static struct composite_context *dcerpc_pipe_open_socket_send(TALLOC_CTX *mem_ct if (composite_nomem(s->target_hostname, c)) return c; } - c->status = socket_create(server->family, SOCKET_TYPE_STREAM, &s->socket_ctx, 0); + c->status = socket_create(s, server->family, SOCKET_TYPE_STREAM, + &s->socket_ctx, 0); if (!composite_is_ok(c)) return c; - talloc_steal(s, s->socket_ctx); - conn_req = socket_connect_send(s->socket_ctx, s->localaddr, s->server, 0, c->event_ctx); composite_continue(c, conn_req, continue_socket_connect, c); diff --git a/source4/smbd/service_stream.c b/source4/smbd/service_stream.c index 545cd4315b3..fc996d942e6 100644 --- a/source4/smbd/service_stream.c +++ b/source4/smbd/service_stream.c @@ -315,10 +315,14 @@ NTSTATUS stream_setup_socket(TALLOC_CTX *mem_ctx, return NT_STATUS_NO_MEMORY; } - status = socket_create(socket_address->family, SOCKET_TYPE_STREAM, &stream_socket->sock, 0); + status = socket_create(stream_socket, socket_address->family, + SOCKET_TYPE_STREAM, + &stream_socket->sock, 0); NT_STATUS_NOT_OK_RETURN(status); } else { - status = socket_create(family, SOCKET_TYPE_STREAM, &stream_socket->sock, 0); + status = socket_create(stream_socket, family, + SOCKET_TYPE_STREAM, + &stream_socket->sock, 0); NT_STATUS_NOT_OK_RETURN(status); /* this is for non-IP sockets, eg. unix domain sockets */ @@ -329,8 +333,6 @@ NTSTATUS stream_setup_socket(TALLOC_CTX *mem_ctx, } - talloc_steal(stream_socket, stream_socket->sock); - stream_socket->lp_ctx = talloc_reference(stream_socket, lp_ctx); /* ready to listen */