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/torture/nbt/dgram.c | 116 +++++++++++++++++++++++++--------- source4/torture/nbt/register.c | 30 +++++++-- source4/torture/nbt/wins.c | 15 ++++- source4/torture/nbt/winsbench.c | 6 +- source4/torture/nbt/winsreplication.c | 59 +++++++++++------ 5 files changed, 169 insertions(+), 57 deletions(-) (limited to 'source4/torture/nbt') diff --git a/source4/torture/nbt/dgram.c b/source4/torture/nbt/dgram.c index 64d4825edc..821a169314 100644 --- a/source4/torture/nbt/dgram.c +++ b/source4/torture/nbt/dgram.c @@ -33,7 +33,7 @@ */ static void netlogon_handler(struct dgram_mailslot_handler *dgmslot, struct nbt_dgram_packet *packet, - const struct nbt_peer_socket *src) + struct socket_address *src) { NTSTATUS status; struct nbt_netlogon_packet netlogon; @@ -60,7 +60,7 @@ static BOOL nbt_test_netlogon(TALLOC_CTX *mem_ctx, { struct dgram_mailslot_handler *dgmslot; struct nbt_dgram_socket *dgmsock = nbt_dgram_socket_init(mem_ctx, NULL); - struct nbt_peer_socket dest; + struct socket_address *dest; const char *myaddress = talloc_strdup(dgmsock, iface_best_ip(address)); struct nbt_netlogon_packet logon; struct nbt_name myname; @@ -68,13 +68,28 @@ static BOOL nbt_test_netlogon(TALLOC_CTX *mem_ctx, struct timeval tv = timeval_current(); int replies = 0; + struct socket_address *socket_address; + + socket_address = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, + myaddress, lp_dgram_port()); + if (!socket_address) { + return False; + } + /* try receiving replies on port 138 first, which will only work if we are root and smbd/nmbd are not running - fall back to listening on any port, which means replies from some windows versions won't be seen */ - status = socket_listen(dgmsock->sock, myaddress, lp_dgram_port(), 0, 0); + status = socket_listen(dgmsock->sock, socket_address, 0, 0); if (!NT_STATUS_IS_OK(status)) { - socket_listen(dgmsock->sock, myaddress, 0, 0, 0); + talloc_free(socket_address); + socket_address = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, + myaddress, 0); + if (!socket_address) { + return False; + } + + socket_listen(dgmsock->sock, socket_address, 0, 0); } /* setup a temporary mailslot listener for replies */ @@ -92,9 +107,13 @@ static BOOL nbt_test_netlogon(TALLOC_CTX *mem_ctx, make_nbt_name_client(&myname, TEST_NAME); - dest.port = 0; - dest.addr = address; - status = dgram_mailslot_netlogon_send(dgmsock, &name, &dest, + dest = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, + address, 0); + if (!dest) { + return False; + } + + status = dgram_mailslot_netlogon_send(dgmsock, &name, dest, &myname, &logon); if (!NT_STATUS_IS_OK(status)) { printf("Failed to send netlogon request - %s\n", nt_errstr(status)); @@ -120,7 +139,7 @@ static BOOL nbt_test_netlogon2(TALLOC_CTX *mem_ctx, { struct dgram_mailslot_handler *dgmslot; struct nbt_dgram_socket *dgmsock = nbt_dgram_socket_init(mem_ctx, NULL); - struct nbt_peer_socket dest; + struct socket_address *dest; const char *myaddress = talloc_strdup(dgmsock, iface_best_ip(address)); struct nbt_netlogon_packet logon; struct nbt_name myname; @@ -128,13 +147,28 @@ static BOOL nbt_test_netlogon2(TALLOC_CTX *mem_ctx, struct timeval tv = timeval_current(); int replies = 0; + struct socket_address *socket_address; + + socket_address = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, + myaddress, lp_dgram_port()); + if (!socket_address) { + return False; + } + /* try receiving replies on port 138 first, which will only work if we are root and smbd/nmbd are not running - fall back to listening on any port, which means replies from some windows versions won't be seen */ - status = socket_listen(dgmsock->sock, myaddress, lp_dgram_port(), 0, 0); + status = socket_listen(dgmsock->sock, socket_address, 0, 0); if (!NT_STATUS_IS_OK(status)) { - socket_listen(dgmsock->sock, myaddress, 0, 0, 0); + talloc_free(socket_address); + socket_address = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, + myaddress, 0); + if (!socket_address) { + return False; + } + + socket_listen(dgmsock->sock, socket_address, 0, 0); } /* setup a temporary mailslot listener for replies */ @@ -154,9 +188,12 @@ static BOOL nbt_test_netlogon2(TALLOC_CTX *mem_ctx, make_nbt_name_client(&myname, TEST_NAME); - dest.port = 0; - dest.addr = address; - status = dgram_mailslot_netlogon_send(dgmsock, &name, &dest, + dest = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, + address, 0); + if (!dest) { + goto failed; + } + status = dgram_mailslot_netlogon_send(dgmsock, &name, dest, &myname, &logon); if (!NT_STATUS_IS_OK(status)) { printf("Failed to send netlogon request - %s\n", nt_errstr(status)); @@ -182,7 +219,7 @@ failed: */ static void ntlogon_handler(struct dgram_mailslot_handler *dgmslot, struct nbt_dgram_packet *packet, - const struct nbt_peer_socket *src) + struct socket_address *src) { NTSTATUS status; struct nbt_ntlogon_packet ntlogon; @@ -209,36 +246,52 @@ static BOOL nbt_test_ntlogon(TALLOC_CTX *mem_ctx, { struct dgram_mailslot_handler *dgmslot; struct nbt_dgram_socket *dgmsock = nbt_dgram_socket_init(mem_ctx, NULL); - struct nbt_peer_socket dest; + struct socket_address *dest; + struct test_join *join_ctx; + struct cli_credentials *machine_credentials; + const char *dom_sid; + const char *myaddress = talloc_strdup(dgmsock, iface_best_ip(address)); struct nbt_ntlogon_packet logon; struct nbt_name myname; NTSTATUS status; struct timeval tv = timeval_current(); int replies = 0; - struct test_join *join_ctx; - struct cli_credentials *machine_credentials; - const char *dom_sid; - join_ctx = torture_join_domain(TEST_NAME, - ACB_WSTRUST, &machine_credentials); - if (join_ctx == NULL) { - printf("Failed to join domain %s as %s\n", lp_workgroup(), TEST_NAME); - talloc_free(dgmsock); + struct socket_address *socket_address; + + socket_address = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, + myaddress, lp_dgram_port()); + if (!socket_address) { return False; } - dom_sid = torture_join_sid(join_ctx); - /* try receiving replies on port 138 first, which will only work if we are root and smbd/nmbd are not running - fall back to listening on any port, which means replies from some windows versions won't be seen */ - status = socket_listen(dgmsock->sock, myaddress, lp_dgram_port(), 0, 0); + status = socket_listen(dgmsock->sock, socket_address, 0, 0); if (!NT_STATUS_IS_OK(status)) { - socket_listen(dgmsock->sock, myaddress, 0, 0, 0); + talloc_free(socket_address); + socket_address = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, + myaddress, 0); + if (!socket_address) { + return False; + } + + socket_listen(dgmsock->sock, socket_address, 0, 0); + } + + join_ctx = torture_join_domain(TEST_NAME, + ACB_WSTRUST, &machine_credentials); + if (join_ctx == NULL) { + printf("Failed to join domain %s as %s\n", lp_workgroup(), TEST_NAME); + talloc_free(dgmsock); + return False; } + dom_sid = torture_join_sid(join_ctx); + /* setup a temporary mailslot listener for replies */ dgmslot = dgram_mailslot_temp(dgmsock, NBT_MAILSLOT_GETDC, ntlogon_handler, &replies); @@ -258,10 +311,13 @@ static BOOL nbt_test_ntlogon(TALLOC_CTX *mem_ctx, make_nbt_name_client(&myname, TEST_NAME); - dest.port = 0; - dest.addr = address; + dest = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, + address, 0); + if (!dest) { + goto failed; + } status = dgram_mailslot_ntlogon_send(dgmsock, DGRAM_DIRECT_UNIQUE, - &name, &dest, &myname, &logon); + &name, dest, &myname, &logon); if (!NT_STATUS_IS_OK(status)) { printf("Failed to send ntlogon request - %s\n", nt_errstr(status)); goto failed; diff --git a/source4/torture/nbt/register.c b/source4/torture/nbt/register.c index 8cbceb8e30..cd7d3c7624 100644 --- a/source4/torture/nbt/register.c +++ b/source4/torture/nbt/register.c @@ -48,8 +48,19 @@ static BOOL nbt_register_own(TALLOC_CTX *mem_ctx, struct nbt_name *name, struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL); BOOL ret = True; const char *myaddress = iface_best_ip(address); + struct socket_address *socket_address; - socket_listen(nbtsock->sock, myaddress, 0, 0, 0); + socket_address = socket_address_from_strings(mem_ctx, nbtsock->sock->backend_name, + myaddress, 0); + if (!socket_address) { + return False; + } + + status = socket_listen(nbtsock->sock, socket_address, 0, 0); + if (!NT_STATUS_IS_OK(status)) { + printf("socket_listen for nbt_register_own failed: %s\n", nt_errstr(status)); + return False; + } printf("Testing name defense to name registration\n"); @@ -85,11 +96,11 @@ static BOOL nbt_register_own(TALLOC_CTX *mem_ctx, struct nbt_name *name, status = nbt_name_register(nbtsock, mem_ctx, &io); if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) { - printf("No response from %s for name register\n", address); + printf("No response from %s for name register demand\n", address); return False; } if (!NT_STATUS_IS_OK(status)) { - printf("Bad response from %s for name register - %s\n", + printf("Bad response from %s for name register demand - %s\n", address, nt_errstr(status)); return False; } @@ -113,8 +124,19 @@ static BOOL nbt_refresh_own(TALLOC_CTX *mem_ctx, struct nbt_name *name, struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL); BOOL ret = True; const char *myaddress = iface_best_ip(address); + struct socket_address *socket_address; + + socket_address = socket_address_from_strings(mem_ctx, nbtsock->sock->backend_name, + myaddress, 0); + if (!socket_address) { + return False; + } - socket_listen(nbtsock->sock, myaddress, 0, 0, 0); + status = socket_listen(nbtsock->sock, socket_address, 0, 0); + if (!NT_STATUS_IS_OK(status)) { + printf("socket_listen for nbt_referesh_own failed: %s\n", nt_errstr(status)); + return False; + } printf("Testing name defense to name refresh\n"); diff --git a/source4/torture/nbt/wins.c b/source4/torture/nbt/wins.c index 37e9688a60..bcb52dc9b8 100644 --- a/source4/torture/nbt/wins.c +++ b/source4/torture/nbt/wins.c @@ -59,10 +59,23 @@ static BOOL nbt_test_wins_name(TALLOC_CTX *mem_ctx, const char *address, struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL); BOOL ret = True; const char *myaddress = talloc_strdup(mem_ctx, iface_best_ip(address)); + struct socket_address *socket_address; + + socket_address = socket_address_from_strings(mem_ctx, + nbtsock->sock->backend_name, + myaddress, 0); + if (!socket_address) { + return False; + } /* we do the listen here to ensure the WINS server receives the packets from the right IP */ - socket_listen(nbtsock->sock, myaddress, 0, 0, 0); + status = socket_listen(nbtsock->sock, socket_address, 0, 0); + if (!NT_STATUS_IS_OK(status)) { + printf("socket_listen for WINS failed: %s\n", nt_errstr(status)); + return False; + } + talloc_free(socket_address); printf("Testing name registration to WINS with name %s at %s nb_flags=0x%x\n", nbt_name_string(mem_ctx, name), myaddress, nb_flags); diff --git a/source4/torture/nbt/winsbench.c b/source4/torture/nbt/winsbench.c index 65f494184f..5aa97f9ade 100644 --- a/source4/torture/nbt/winsbench.c +++ b/source4/torture/nbt/winsbench.c @@ -223,6 +223,7 @@ static BOOL bench_wins(TALLOC_CTX *mem_ctx, struct nbt_name *name, const char *a int timelimit = lp_parm_int(-1, "torture", "timelimit", 10); struct wins_state *state; extern int torture_entries; + struct socket_address *my_ip; state = talloc_zero(nbtsock, struct wins_state); @@ -232,7 +233,10 @@ static BOOL bench_wins(TALLOC_CTX *mem_ctx, struct nbt_name *name, const char *a state->my_ip = talloc_strdup(mem_ctx, iface_best_ip(address)); state->ttl = timelimit; - socket_listen(nbtsock->sock, state->my_ip, 0, 0, 0); + my_ip = socket_address_from_strings(nbtsock, nbtsock->sock->backend_name, + state->my_ip, 0); + + socket_listen(nbtsock->sock, my_ip, 0, 0); printf("Running for %d seconds\n", timelimit); while (timeval_elapsed(&tv) < timelimit) { diff --git a/source4/torture/nbt/winsreplication.c b/source4/torture/nbt/winsreplication.c index f7f9665a6d..a6089fc2db 100644 --- a/source4/torture/nbt/winsreplication.c +++ b/source4/torture/nbt/winsreplication.c @@ -338,8 +338,8 @@ struct test_wrepl_conflict_conn { struct wrepl_wins_owner a, b, c, x; - const char *myaddr; - const char *myaddr2; + struct socket_address *myaddr; + struct socket_address *myaddr2; struct nbt_name_socket *nbtsock; struct nbt_name_socket *nbtsock2; @@ -551,6 +551,7 @@ static struct test_wrepl_conflict_conn *test_create_conflict_ctx(TALLOC_CTX *mem struct test_wrepl_conflict_conn *ctx; struct wrepl_associate associate; struct wrepl_pull_table pull_table; + struct socket_address *nbt_srv_addr; NTSTATUS status; uint32_t i; @@ -615,27 +616,34 @@ static struct test_wrepl_conflict_conn *test_create_conflict_ctx(TALLOC_CTX *mem talloc_free(pull_table.out.partners); - ctx->myaddr = talloc_strdup(mem_ctx, iface_best_ip(address)); + ctx->nbtsock = nbt_name_socket_init(ctx, NULL); + if (!ctx->nbtsock) return NULL; + + ctx->myaddr = socket_address_from_strings(mem_ctx, ctx->nbtsock->sock->backend_name, iface_best_ip(address), 0); if (!ctx->myaddr) return NULL; for (i = 0; i < iface_count(); i++) { - if (strcmp(ctx->myaddr, iface_n_ip(i)) == 0) continue; - ctx->myaddr2 = talloc_strdup(mem_ctx, iface_n_ip(i)); + if (strcmp(ctx->myaddr->addr, iface_n_ip(i)) == 0) continue; + ctx->myaddr2 = socket_address_from_strings(mem_ctx, ctx->nbtsock->sock->backend_name, iface_n_ip(i), 0); if (!ctx->myaddr2) return NULL; break; } - ctx->nbtsock = nbt_name_socket_init(ctx, NULL); - if (!ctx->nbtsock) return NULL; - - status = socket_listen(ctx->nbtsock->sock, ctx->myaddr, 0, 0, 0); + status = socket_listen(ctx->nbtsock->sock, ctx->myaddr, 0, 0); if (!NT_STATUS_IS_OK(status)) return NULL; ctx->nbtsock_srv = nbt_name_socket_init(ctx, NULL); if (!ctx->nbtsock_srv) return NULL; - status = socket_listen(ctx->nbtsock_srv->sock, ctx->myaddr, lp_nbt_port(), 0, 0); + /* Make a port 137 version of ctx->myaddr */ + nbt_srv_addr = socket_address_from_strings(mem_ctx, ctx->nbtsock_srv->sock->backend_name, ctx->myaddr->addr, lp_nbt_port()); + if (!nbt_srv_addr) return NULL; + + /* And if possible, bind to it. This won't work unless we are root or in sockewrapper */ + status = socket_listen(ctx->nbtsock_srv->sock, nbt_srv_addr, 0, 0); + talloc_free(nbt_srv_addr); if (!NT_STATUS_IS_OK(status)) { + /* this isn't fatal */ talloc_free(ctx->nbtsock_srv); ctx->nbtsock_srv = NULL; } @@ -644,14 +652,23 @@ static struct test_wrepl_conflict_conn *test_create_conflict_ctx(TALLOC_CTX *mem ctx->nbtsock2 = nbt_name_socket_init(ctx, NULL); if (!ctx->nbtsock2) return NULL; - status = socket_listen(ctx->nbtsock2->sock, ctx->myaddr2, 0, 0, 0); + status = socket_listen(ctx->nbtsock2->sock, ctx->myaddr2, 0, 0); if (!NT_STATUS_IS_OK(status)) return NULL; ctx->nbtsock_srv2 = nbt_name_socket_init(ctx, ctx->nbtsock_srv->event_ctx); if (!ctx->nbtsock_srv2) return NULL; - status = socket_listen(ctx->nbtsock_srv2->sock, ctx->myaddr2, lp_nbt_port(), 0, 0); + /* Make a port 137 version of ctx->myaddr2 */ + nbt_srv_addr = socket_address_from_strings(mem_ctx, + ctx->nbtsock_srv->sock->backend_name, + ctx->myaddr2->addr, lp_nbt_port()); + if (!nbt_srv_addr) return NULL; + + /* And if possible, bind to it. This won't work unless we are root or in sockewrapper */ + status = socket_listen(ctx->nbtsock_srv2->sock, ctx->myaddr2, 0, 0); + talloc_free(nbt_srv_addr); if (!NT_STATUS_IS_OK(status)) { + /* this isn't fatal */ talloc_free(ctx->nbtsock_srv2); ctx->nbtsock_srv2 = NULL; } @@ -661,7 +678,7 @@ static struct test_wrepl_conflict_conn *test_create_conflict_ctx(TALLOC_CTX *mem ctx->addresses_best = talloc_array(ctx, struct wrepl_ip, ctx->addresses_best_num); if (!ctx->addresses_best) return NULL; ctx->addresses_best[0].owner = ctx->b.address; - ctx->addresses_best[0].ip = ctx->myaddr; + ctx->addresses_best[0].ip = ctx->myaddr->addr; ctx->addresses_all_num = iface_count(); ctx->addresses_all = talloc_array(ctx, struct wrepl_ip, ctx->addresses_all_num); @@ -677,15 +694,15 @@ static struct test_wrepl_conflict_conn *test_create_conflict_ctx(TALLOC_CTX *mem ctx->addresses_best2 = talloc_array(ctx, struct wrepl_ip, ctx->addresses_best2_num); if (!ctx->addresses_best2) return NULL; ctx->addresses_best2[0].owner = ctx->b.address; - ctx->addresses_best2[0].ip = ctx->myaddr2; + ctx->addresses_best2[0].ip = ctx->myaddr2->addr; ctx->addresses_mhomed_num = 2; ctx->addresses_mhomed = talloc_array(ctx, struct wrepl_ip, ctx->addresses_mhomed_num); if (!ctx->addresses_mhomed) return NULL; ctx->addresses_mhomed[0].owner = ctx->b.address; - ctx->addresses_mhomed[0].ip = ctx->myaddr; + ctx->addresses_mhomed[0].ip = ctx->myaddr->addr; ctx->addresses_mhomed[1].owner = ctx->b.address; - ctx->addresses_mhomed[1].ip = ctx->myaddr2; + ctx->addresses_mhomed[1].ip = ctx->myaddr2->addr; } return ctx; @@ -6545,7 +6562,7 @@ struct test_conflict_owned_active_vs_replica_struct { static void test_conflict_owned_active_vs_replica_handler(struct nbt_name_socket *nbtsock, struct nbt_name_packet *req_packet, - const struct nbt_peer_socket *src); + struct socket_address *src); static BOOL test_conflict_owned_active_vs_replica(struct test_wrepl_conflict_conn *ctx) { @@ -9220,7 +9237,7 @@ static BOOL test_conflict_owned_active_vs_replica(struct test_wrepl_conflict_con for (j=0; j < count; j++) { struct nbt_name_socket *nbtsock = ctx->nbtsock; - if (ctx->myaddr2 && strcmp(records[i].wins.ips[j].ip, ctx->myaddr2) == 0) { + if (ctx->myaddr2 && strcmp(records[i].wins.ips[j].ip, ctx->myaddr2->addr) == 0) { nbtsock = ctx->nbtsock2; } @@ -9312,7 +9329,7 @@ done: static void test_conflict_owned_active_vs_replica_handler_query(struct nbt_name_socket *nbtsock, struct nbt_name_packet *req_packet, - const struct nbt_peer_socket *src) + struct socket_address *src) { struct nbt_name *name; struct nbt_name_packet *rep_packet; @@ -9407,7 +9424,7 @@ static void test_conflict_owned_active_vs_replica_handler_query(struct nbt_name_ static void test_conflict_owned_active_vs_replica_handler_release(struct nbt_name_socket *nbtsock, struct nbt_name_packet *req_packet, - const struct nbt_peer_socket *src) + struct socket_address *src) { struct nbt_name *name; struct nbt_name_packet *rep_packet; @@ -9460,7 +9477,7 @@ static void test_conflict_owned_active_vs_replica_handler_release(struct nbt_nam static void test_conflict_owned_active_vs_replica_handler(struct nbt_name_socket *nbtsock, struct nbt_name_packet *req_packet, - const struct nbt_peer_socket *src) + struct socket_address *src) { struct test_conflict_owned_active_vs_replica_struct *rec = nbtsock->incoming.private; -- cgit