From 6f2252dace1629d7b5c5637b103caa28d2c89b07 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Tue, 11 Dec 2007 22:23:14 +0100 Subject: r26401: Don't cache interfaces context in libnetif. (This used to be commit 9f975417cc66bfd4589da38bfd23731dbe0e6153) --- source4/lib/socket/interface.c | 73 ++++++++++++++---------------------------- source4/lib/socket/netif.h | 2 ++ source4/lib/socket/testsuite.c | 15 ++++++--- 3 files changed, 36 insertions(+), 54 deletions(-) (limited to 'source4/lib') diff --git a/source4/lib/socket/interface.c b/source4/lib/socket/interface.c index 309ed80744..d21923d972 100644 --- a/source4/lib/socket/interface.c +++ b/source4/lib/socket/interface.c @@ -35,8 +35,6 @@ struct interface { const char *nmask_s; }; -static struct interface *local_interfaces; - #define ALLONES ((uint32_t)0xFFFFFFFF) /* address construction based on a patch from fred@datalync.com @@ -114,7 +112,8 @@ This handles the following different forms: **/ static void interpret_interface(const char *token, struct iface_struct *probed_ifaces, - int total_probed) + int total_probed, + struct interface **local_interfaces) { struct in_addr ip, nmask; char *p; @@ -128,7 +127,7 @@ static void interpret_interface(const char *token, if (gen_fnmatch(token, probed_ifaces[i].name) == 0) { add_interface(probed_ifaces[i].ip, probed_ifaces[i].netmask, - &local_interfaces); + local_interfaces); added = 1; } } @@ -146,7 +145,7 @@ static void interpret_interface(const char *token, if (ip.s_addr == probed_ifaces[i].ip.s_addr) { add_interface(probed_ifaces[i].ip, probed_ifaces[i].netmask, - &local_interfaces); + local_interfaces); return; } } @@ -171,7 +170,7 @@ static void interpret_interface(const char *token, for (i=0;inext) + for (i=ifaces;i;i=i->next) ret++; return ret; } @@ -255,13 +240,11 @@ int iface_count(struct loadparm_context *lp_ctx) /** return IP of the Nth interface **/ -const char *iface_n_ip(struct loadparm_context *lp_ctx, int n) +const char *iface_n_ip(struct interface *ifaces, int n) { struct interface *i; - load_interfaces(lp_interfaces(lp_ctx)); - - for (i=local_interfaces;i && n;i=i->next) + for (i=ifaces;i && n;i=i->next) n--; if (i) { @@ -273,13 +256,11 @@ const char *iface_n_ip(struct loadparm_context *lp_ctx, int n) /** return bcast of the Nth interface **/ -const char *iface_n_bcast(struct loadparm_context *lp_ctx, int n) +const char *iface_n_bcast(struct interface *ifaces, int n) { struct interface *i; - load_interfaces(lp_interfaces(lp_ctx)); - - for (i=local_interfaces;i && n;i=i->next) + for (i=ifaces;i && n;i=i->next) n--; if (i) { @@ -291,13 +272,11 @@ const char *iface_n_bcast(struct loadparm_context *lp_ctx, int n) /** return netmask of the Nth interface **/ -const char *iface_n_netmask(struct loadparm_context *lp_ctx, int n) +const char *iface_n_netmask(struct interface *ifaces, int n) { struct interface *i; - load_interfaces(lp_interfaces(lp_ctx)); - - for (i=local_interfaces;i && n;i=i->next) + for (i=ifaces;i && n;i=i->next) n--; if (i) { @@ -310,32 +289,28 @@ const char *iface_n_netmask(struct loadparm_context *lp_ctx, int n) return the local IP address that best matches a destination IP, or our first interface if none match */ -const char *iface_best_ip(struct loadparm_context *lp_ctx, const char *dest) +const char *iface_best_ip(struct interface *ifaces, const char *dest) { struct interface *iface; struct in_addr ip; - load_interfaces(lp_interfaces(lp_ctx)); - ip.s_addr = interpret_addr(dest); - iface = iface_find(local_interfaces, ip, true); + iface = iface_find(ifaces, ip, true); if (iface) { return iface->ip_s; } - return iface_n_ip(lp_ctx, 0); + return iface_n_ip(ifaces, 0); } /** return true if an IP is one one of our local networks */ -bool iface_is_local(struct loadparm_context *lp_ctx, const char *dest) +bool iface_is_local(struct interface *ifaces, const char *dest) { struct in_addr ip; - load_interfaces(lp_interfaces(lp_ctx)); - ip.s_addr = interpret_addr(dest); - if (iface_find(local_interfaces, ip, true)) { + if (iface_find(ifaces, ip, true)) { return true; } return false; diff --git a/source4/lib/socket/netif.h b/source4/lib/socket/netif.h index 4855f4bd73..417c6e074f 100644 --- a/source4/lib/socket/netif.h +++ b/source4/lib/socket/netif.h @@ -27,6 +27,8 @@ struct iface_struct { struct in_addr netmask; }; +struct interface; + #define MAX_INTERFACES 128 #ifndef AUTOCONF_TEST diff --git a/source4/lib/socket/testsuite.c b/source4/lib/socket/testsuite.c index edbe617aa0..a99ae078b5 100644 --- a/source4/lib/socket/testsuite.c +++ b/source4/lib/socket/testsuite.c @@ -40,6 +40,9 @@ static bool test_udp(struct torture_context *tctx) DATA_BLOB blob, blob2; size_t sent, nread; TALLOC_CTX *mem_ctx = tctx; + struct interface *ifaces; + + load_interfaces(lp_interfaces(tctx->lp_ctx), &ifaces); status = socket_create("ip", SOCKET_TYPE_DGRAM, &sock1, 0); torture_assert_ntstatus_ok(tctx, status, "creating DGRAM IP socket 1"); @@ -50,7 +53,7 @@ static bool test_udp(struct torture_context *tctx) talloc_steal(mem_ctx, sock2); localhost = socket_address_from_strings(sock1, sock1->backend_name, - iface_best_ip(tctx->lp_ctx, "127.0.0.1"), 0); + iface_best_ip(ifaces, "127.0.0.1"), 0); torture_assert(tctx, localhost, "Localhost not found"); @@ -59,10 +62,10 @@ static bool test_udp(struct torture_context *tctx) srv_addr = socket_get_my_addr(sock1, mem_ctx); torture_assert(tctx, srv_addr != NULL && - strcmp(srv_addr->addr, iface_best_ip(tctx->lp_ctx, "127.0.0.1")) == 0, + strcmp(srv_addr->addr, iface_best_ip(ifaces, "127.0.0.1")) == 0, talloc_asprintf(tctx, "Expected server address of %s but got %s", - iface_best_ip(tctx->lp_ctx, "127.0.0.1"), srv_addr ? srv_addr->addr : NULL)); + iface_best_ip(ifaces, "127.0.0.1"), srv_addr ? srv_addr->addr : NULL)); torture_comment(tctx, "server port is %d\n", srv_addr->port); @@ -122,6 +125,7 @@ static bool test_tcp(struct torture_context *tctx) size_t sent, nread; TALLOC_CTX *mem_ctx = tctx; struct event_context *ev = event_context_init(mem_ctx); + struct interface *ifaces; status = socket_create("ip", SOCKET_TYPE_STREAM, &sock1, 0); torture_assert_ntstatus_ok(tctx, status, "creating IP stream socket 1"); @@ -131,8 +135,9 @@ static bool test_tcp(struct torture_context *tctx) torture_assert_ntstatus_ok(tctx, status, "creating IP stream socket 1"); talloc_steal(mem_ctx, sock2); + load_interfaces(lp_interfaces(tctx->lp_ctx), &ifaces); localhost = socket_address_from_strings(sock1, sock1->backend_name, - iface_best_ip(tctx->lp_ctx, "127.0.0.1"), 0); + iface_best_ip(ifaces, "127.0.0.1"), 0); torture_assert(tctx, localhost, "Localhost not found"); status = socket_listen(sock1, localhost, 0, 0); @@ -142,7 +147,7 @@ static bool test_tcp(struct torture_context *tctx) torture_assert(tctx, srv_addr && srv_addr->addr, "Unexpected socket_get_my_addr NULL\n"); - torture_assert_str_equal(tctx, srv_addr->addr, iface_best_ip(tctx->lp_ctx, "127.0.0.1"), + torture_assert_str_equal(tctx, srv_addr->addr, iface_best_ip(ifaces, "127.0.0.1"), "Unexpected server address"); torture_comment(tctx, "server port is %d\n", srv_addr->port); -- cgit