From f55ea8bb3dca868e21663cd90eaea7a35cd7886c Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Mon, 9 Jan 2006 22:12:53 +0000 Subject: r12804: This patch reworks the Samba4 sockets layer to use a socket_address structure that is more generic than just 'IP/port'. It now passes make test, and has been reviewed and updated by metze. (Thankyou *very* much). This passes 'make test' as well as kerberos use (not currently in the testsuite). The original purpose of this patch was to have Samba able to pass a socket address stucture from the BSD layer into the kerberos routines and back again. It also removes nbt_peer_addr, which was being used for a similar purpose. It is a large change, but worthwhile I feel. Andrew Bartlett (This used to be commit 88198c4881d8620a37086f80e4da5a5b71c5bbb2) --- source4/libcli/nbt/libnbt.h | 14 ++++---------- source4/libcli/nbt/namequery.c | 24 +++++++++++++----------- source4/libcli/nbt/namerefresh.c | 12 +++++++----- source4/libcli/nbt/nameregister.c | 12 +++++++----- source4/libcli/nbt/namerelease.c | 12 +++++++----- source4/libcli/nbt/nbtsocket.c | 39 ++++++++++++++++++--------------------- 6 files changed, 56 insertions(+), 57 deletions(-) (limited to 'source4/libcli/nbt') diff --git a/source4/libcli/nbt/libnbt.h b/source4/libcli/nbt/libnbt.h index 7c9e3e9651..b4411d6fe6 100644 --- a/source4/libcli/nbt/libnbt.h +++ b/source4/libcli/nbt/libnbt.h @@ -34,12 +34,6 @@ enum nbt_request_state {NBT_REQUEST_SEND, NBT_REQUEST_TIMEOUT, NBT_REQUEST_ERROR}; -/* where to send the request/replies */ -struct nbt_peer_socket { - const char *addr; - int port; -}; - /* a nbt name request */ @@ -54,7 +48,7 @@ struct nbt_name_request { struct nbt_name_socket *nbtsock; /* where to send the request */ - struct nbt_peer_socket dest; + struct socket_address *dest; /* timeout between retries */ int timeout; @@ -83,7 +77,7 @@ struct nbt_name_request { uint_t num_replies; struct nbt_name_reply { struct nbt_name_packet *packet; - struct nbt_peer_socket dest; + struct socket_address *dest; } *replies; /* information on what to do on completion */ @@ -117,14 +111,14 @@ struct nbt_name_socket { /* what to do with incoming request packets */ struct { void (*handler)(struct nbt_name_socket *, struct nbt_name_packet *, - const struct nbt_peer_socket *); + struct socket_address *); void *private; } incoming; /* what to do with unexpected replies */ struct { void (*handler)(struct nbt_name_socket *, struct nbt_name_packet *, - const struct nbt_peer_socket *); + struct socket_address *); void *private; } unexpected; }; diff --git a/source4/libcli/nbt/namequery.c b/source4/libcli/nbt/namequery.c index 38fb2f966d..6566e48a5a 100644 --- a/source4/libcli/nbt/namequery.c +++ b/source4/libcli/nbt/namequery.c @@ -22,7 +22,7 @@ #include "includes.h" #include "libcli/nbt/libnbt.h" - +#include "lib/socket/socket.h" /* send a nbt name query */ @@ -31,7 +31,7 @@ struct nbt_name_request *nbt_name_query_send(struct nbt_name_socket *nbtsock, { struct nbt_name_request *req; struct nbt_name_packet *packet; - struct nbt_peer_socket dest; + struct socket_address *dest; packet = talloc_zero(nbtsock, struct nbt_name_packet); if (packet == NULL) return NULL; @@ -52,9 +52,10 @@ struct nbt_name_request *nbt_name_query_send(struct nbt_name_socket *nbtsock, packet->questions[0].question_type = NBT_QTYPE_NETBIOS; packet->questions[0].question_class = NBT_QCLASS_IP; - dest.port = lp_nbt_port(); - dest.addr = io->in.dest_addr; - req = nbt_name_request_send(nbtsock, &dest, packet, + dest = socket_address_from_strings(packet, nbtsock->sock->backend_name, + io->in.dest_addr, lp_nbt_port()); + if (dest == NULL) goto failed; + req = nbt_name_request_send(nbtsock, dest, packet, io->in.timeout, io->in.retries, False); if (req == NULL) goto failed; @@ -84,7 +85,7 @@ NTSTATUS nbt_name_query_recv(struct nbt_name_request *req, } packet = req->replies[0].packet; - io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest.addr); + io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest->addr); if ((packet->operation & NBT_RCODE) != 0) { status = nbt_rcode_to_ntstatus(packet->operation & NBT_RCODE); @@ -140,7 +141,7 @@ struct nbt_name_request *nbt_name_status_send(struct nbt_name_socket *nbtsock, { struct nbt_name_request *req; struct nbt_name_packet *packet; - struct nbt_peer_socket dest; + struct socket_address *dest; packet = talloc_zero(nbtsock, struct nbt_name_packet); if (packet == NULL) return NULL; @@ -155,9 +156,10 @@ struct nbt_name_request *nbt_name_status_send(struct nbt_name_socket *nbtsock, packet->questions[0].question_type = NBT_QTYPE_STATUS; packet->questions[0].question_class = NBT_QCLASS_IP; - dest.port = lp_nbt_port(); - dest.addr = io->in.dest_addr; - req = nbt_name_request_send(nbtsock, &dest, packet, + dest = socket_address_from_strings(packet, nbtsock->sock->backend_name, + io->in.dest_addr, lp_nbt_port()); + if (dest == NULL) goto failed; + req = nbt_name_request_send(nbtsock, dest, packet, io->in.timeout, io->in.retries, False); if (req == NULL) goto failed; @@ -187,7 +189,7 @@ NTSTATUS nbt_name_status_recv(struct nbt_name_request *req, } packet = req->replies[0].packet; - io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest.addr); + io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest->addr); if ((packet->operation & NBT_RCODE) != 0) { status = nbt_rcode_to_ntstatus(packet->operation & NBT_RCODE); diff --git a/source4/libcli/nbt/namerefresh.c b/source4/libcli/nbt/namerefresh.c index e6464885f7..8940fbb95e 100644 --- a/source4/libcli/nbt/namerefresh.c +++ b/source4/libcli/nbt/namerefresh.c @@ -23,6 +23,7 @@ #include "includes.h" #include "libcli/nbt/libnbt.h" #include "libcli/composite/composite.h" +#include "lib/socket/socket.h" /* send a nbt name refresh request @@ -32,7 +33,7 @@ struct nbt_name_request *nbt_name_refresh_send(struct nbt_name_socket *nbtsock, { struct nbt_name_request *req; struct nbt_name_packet *packet; - struct nbt_peer_socket dest; + struct socket_address *dest; packet = talloc_zero(nbtsock, struct nbt_name_packet); if (packet == NULL) return NULL; @@ -66,9 +67,10 @@ struct nbt_name_request *nbt_name_refresh_send(struct nbt_name_socket *nbtsock, packet->additional[0].rdata.netbios.addresses[0].ipaddr = talloc_strdup(packet->additional, io->in.address); - dest.port = lp_nbt_port(); - dest.addr = io->in.dest_addr; - req = nbt_name_request_send(nbtsock, &dest, packet, + dest = socket_address_from_strings(nbtsock, nbtsock->sock->backend_name, + io->in.dest_addr, lp_nbt_port()); + if (dest == NULL) goto failed; + req = nbt_name_request_send(nbtsock, dest, packet, io->in.timeout, io->in.retries, False); if (req == NULL) goto failed; @@ -97,7 +99,7 @@ NTSTATUS nbt_name_refresh_recv(struct nbt_name_request *req, } packet = req->replies[0].packet; - io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest.addr); + io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest->addr); if (packet->ancount != 1 || packet->answers[0].rr_type != NBT_QTYPE_NETBIOS || diff --git a/source4/libcli/nbt/nameregister.c b/source4/libcli/nbt/nameregister.c index 4f3e046274..5c7e86367f 100644 --- a/source4/libcli/nbt/nameregister.c +++ b/source4/libcli/nbt/nameregister.c @@ -23,6 +23,7 @@ #include "includes.h" #include "libcli/nbt/libnbt.h" #include "libcli/composite/composite.h" +#include "lib/socket/socket.h" /* send a nbt name registration request @@ -32,7 +33,7 @@ struct nbt_name_request *nbt_name_register_send(struct nbt_name_socket *nbtsock, { struct nbt_name_request *req; struct nbt_name_packet *packet; - struct nbt_peer_socket dest; + struct socket_address *dest; packet = talloc_zero(nbtsock, struct nbt_name_packet); if (packet == NULL) return NULL; @@ -74,9 +75,10 @@ struct nbt_name_request *nbt_name_register_send(struct nbt_name_socket *nbtsock, talloc_strdup(packet->additional, io->in.address); if (packet->additional[0].rdata.netbios.addresses[0].ipaddr == NULL) goto failed; - dest.port = lp_nbt_port(); - dest.addr = io->in.dest_addr; - req = nbt_name_request_send(nbtsock, &dest, packet, + dest = socket_address_from_strings(packet, nbtsock->sock->backend_name, + io->in.dest_addr, lp_nbt_port()); + if (dest == NULL) goto failed; + req = nbt_name_request_send(nbtsock, dest, packet, io->in.timeout, io->in.retries, False); if (req == NULL) goto failed; @@ -105,7 +107,7 @@ NTSTATUS nbt_name_register_recv(struct nbt_name_request *req, } packet = req->replies[0].packet; - io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest.addr); + io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest->addr); if (packet->ancount != 1 || packet->answers[0].rr_type != NBT_QTYPE_NETBIOS || diff --git a/source4/libcli/nbt/namerelease.c b/source4/libcli/nbt/namerelease.c index 0a0cd45276..e8dc175b36 100644 --- a/source4/libcli/nbt/namerelease.c +++ b/source4/libcli/nbt/namerelease.c @@ -22,6 +22,7 @@ #include "includes.h" #include "libcli/nbt/libnbt.h" +#include "lib/socket/socket.h" /* send a nbt name release request @@ -31,7 +32,7 @@ struct nbt_name_request *nbt_name_release_send(struct nbt_name_socket *nbtsock, { struct nbt_name_request *req; struct nbt_name_packet *packet; - struct nbt_peer_socket dest; + struct socket_address *dest; packet = talloc_zero(nbtsock, struct nbt_name_packet); if (packet == NULL) return NULL; @@ -65,9 +66,10 @@ struct nbt_name_request *nbt_name_release_send(struct nbt_name_socket *nbtsock, packet->additional[0].rdata.netbios.addresses[0].ipaddr = talloc_strdup(packet->additional, io->in.address); - dest.port = lp_nbt_port(); - dest.addr = io->in.dest_addr; - req = nbt_name_request_send(nbtsock, &dest, packet, + dest = socket_address_from_strings(packet, nbtsock->sock->backend_name, + io->in.dest_addr, lp_nbt_port()); + if (dest == NULL) goto failed; + req = nbt_name_request_send(nbtsock, dest, packet, io->in.timeout, io->in.retries, False); if (req == NULL) goto failed; @@ -96,7 +98,7 @@ NTSTATUS nbt_name_release_recv(struct nbt_name_request *req, } packet = req->replies[0].packet; - io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest.addr); + io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest->addr); if (packet->ancount != 1 || packet->answers[0].rr_type != NBT_QTYPE_NETBIOS || diff --git a/source4/libcli/nbt/nbtsocket.c b/source4/libcli/nbt/nbtsocket.c index 1c41b80fdb..42b92b14cc 100644 --- a/source4/libcli/nbt/nbtsocket.c +++ b/source4/libcli/nbt/nbtsocket.c @@ -73,7 +73,7 @@ static void nbt_name_socket_send(struct nbt_name_socket *nbtsock) len = req->encoded.length; status = socket_sendto(nbtsock->sock, &req->encoded, &len, 0, - req->dest.addr, req->dest.port); + req->dest); if (NT_STATUS_IS_ERR(status)) goto failed; if (!NT_STATUS_IS_OK(status)) { @@ -153,7 +153,7 @@ static void nbt_name_socket_recv(struct nbt_name_socket *nbtsock) { TALLOC_CTX *tmp_ctx = talloc_new(nbtsock); NTSTATUS status; - struct nbt_peer_socket src; + struct socket_address *src; DATA_BLOB blob; size_t nread, dsize; struct nbt_name_packet *packet; @@ -172,13 +172,11 @@ static void nbt_name_socket_recv(struct nbt_name_socket *nbtsock) } status = socket_recvfrom(nbtsock->sock, blob.data, blob.length, &nread, 0, - &src.addr, &src.port); + tmp_ctx, &src); if (!NT_STATUS_IS_OK(status)) { talloc_free(tmp_ctx); return; } - talloc_steal(tmp_ctx, src.addr); - blob.length = nread; packet = talloc(tmp_ctx, struct nbt_name_packet); if (packet == NULL) { @@ -198,7 +196,7 @@ static void nbt_name_socket_recv(struct nbt_name_socket *nbtsock) if (DEBUGLVL(10)) { DEBUG(10,("Received nbt packet of length %d from %s:%d\n", - (int)blob.length, src.addr, src.port)); + (int)blob.length, src->addr, src->port)); NDR_PRINT_DEBUG(nbt_name_packet, packet); } @@ -206,7 +204,7 @@ static void nbt_name_socket_recv(struct nbt_name_socket *nbtsock) handler, if any */ if (!(packet->operation & NBT_FLAG_REPLY)) { if (nbtsock->incoming.handler) { - nbtsock->incoming.handler(nbtsock, packet, &src); + nbtsock->incoming.handler(nbtsock, packet, src); } talloc_free(tmp_ctx); return; @@ -216,7 +214,7 @@ static void nbt_name_socket_recv(struct nbt_name_socket *nbtsock) req = idr_find(nbtsock->idr, packet->name_trn_id); if (req == NULL) { if (nbtsock->unexpected.handler) { - nbtsock->unexpected.handler(nbtsock, packet, &src); + nbtsock->unexpected.handler(nbtsock, packet, src); } else { DEBUG(2,("Failed to match request for incoming name packet id 0x%04x on %p\n", packet->name_trn_id, nbtsock)); @@ -258,9 +256,10 @@ static void nbt_name_socket_recv(struct nbt_name_socket *nbtsock) goto done; } - req->replies[req->num_replies].dest.addr = talloc_steal(req, src.addr); - req->replies[req->num_replies].dest.port = src.port; - req->replies[req->num_replies].packet = talloc_steal(req, packet); + talloc_steal(req, src); + req->replies[req->num_replies].dest = src; + talloc_steal(req, packet); + req->replies[req->num_replies].packet = packet; req->num_replies++; /* if we don't want multiple replies then we are done */ @@ -348,7 +347,7 @@ failed: send off a nbt name request */ struct nbt_name_request *nbt_name_request_send(struct nbt_name_socket *nbtsock, - const struct nbt_peer_socket *dest, + struct socket_address *dest, struct nbt_name_packet *request, int timeout, int retries, BOOL allow_multiple_replies) @@ -366,9 +365,8 @@ struct nbt_name_request *nbt_name_request_send(struct nbt_name_socket *nbtsock, req->is_reply = False; req->timeout = timeout; req->num_retries = retries; - req->dest.port = dest->port; - req->dest.addr = talloc_strdup(req, dest->addr); - if (req->dest.addr == NULL) goto failed; + req->dest = dest; + if (talloc_reference(req, dest) == NULL) goto failed; /* we select a random transaction id unless the user supplied one */ if (request->name_trn_id == 0) { @@ -397,7 +395,7 @@ struct nbt_name_request *nbt_name_request_send(struct nbt_name_socket *nbtsock, if (DEBUGLVL(10)) { DEBUG(10,("Queueing nbt packet to %s:%d\n", - req->dest.addr, req->dest.port)); + req->dest->addr, req->dest->port)); NDR_PRINT_DEBUG(nbt_name_packet, request); } @@ -415,7 +413,7 @@ failed: send off a nbt name reply */ NTSTATUS nbt_name_reply_send(struct nbt_name_socket *nbtsock, - const struct nbt_peer_socket *dest, + struct socket_address *dest, struct nbt_name_packet *request) { struct nbt_name_request *req; @@ -425,9 +423,8 @@ NTSTATUS nbt_name_reply_send(struct nbt_name_socket *nbtsock, NT_STATUS_HAVE_NO_MEMORY(req); req->nbtsock = nbtsock; - req->dest.port = dest->port; - req->dest.addr = talloc_strdup(req, dest->addr); - if (req->dest.addr == NULL) goto failed; + req->dest = dest; + if (talloc_reference(req, dest) == NULL) goto failed; req->state = NBT_REQUEST_SEND; req->is_reply = True; @@ -480,7 +477,7 @@ NTSTATUS nbt_name_request_recv(struct nbt_name_request *req) */ NTSTATUS nbt_set_incoming_handler(struct nbt_name_socket *nbtsock, void (*handler)(struct nbt_name_socket *, struct nbt_name_packet *, - const struct nbt_peer_socket *), + struct socket_address *), void *private) { nbtsock->incoming.handler = handler; -- cgit