diff options
author | Andrew Bartlett <abartlet@samba.org> | 2011-05-02 12:09:19 +1000 |
---|---|---|
committer | Andrew Tridgell <tridge@samba.org> | 2011-05-08 12:57:03 +0200 |
commit | 5a8b6ac4c0ea88bdab12349830985560c69cbf8c (patch) | |
tree | c9d71555f2ae3c0211c509ec6214dafa5245d0e8 /source4 | |
parent | 45e26fdeec4c6d3c4e75d648399ce2a8b3e26364 (diff) | |
download | samba-5a8b6ac4c0ea88bdab12349830985560c69cbf8c.tar.gz samba-5a8b6ac4c0ea88bdab12349830985560c69cbf8c.tar.bz2 samba-5a8b6ac4c0ea88bdab12349830985560c69cbf8c.zip |
s4-lib merge get_interfaces() from Samba3 to Samba4
Diffstat (limited to 'source4')
-rw-r--r-- | source4/lib/socket/interface.c | 244 | ||||
-rw-r--r-- | source4/lib/socket/netif.c | 225 | ||||
-rw-r--r-- | source4/lib/socket/netif.h | 6 |
3 files changed, 375 insertions, 100 deletions
diff --git a/source4/lib/socket/interface.c b/source4/lib/socket/interface.c index c4411b623c..abeca8ba1e 100644 --- a/source4/lib/socket/interface.c +++ b/source4/lib/socket/interface.c @@ -25,11 +25,14 @@ #include "../lib/util/util_net.h" #include "../lib/util/dlinklist.h" -/** used for network interfaces */ +/* used for network interfaces */ struct interface { struct interface *next, *prev; - struct in_addr ip; - struct in_addr nmask; + char *name; + int flags; + struct sockaddr_storage ip; + struct sockaddr_storage netmask; + struct sockaddr_storage bcast; const char *ip_s; const char *bcast_s; const char *nmask_s; @@ -46,30 +49,45 @@ struct interface { Try and find an interface that matches an ip. If we cannot, return NULL **************************************************************************/ static struct interface *iface_find(struct interface *interfaces, - struct in_addr ip, bool CheckMask) + const struct sockaddr *ip, + bool check_mask) { struct interface *i; - if (is_zero_ip_v4(ip)) return interfaces; - for (i=interfaces;i;i=i->next) - if (CheckMask) { - if (same_net_v4(i->ip,ip,i->nmask)) return i; - } else if (i->ip.s_addr == ip.s_addr) return i; + if (is_address_any(ip)) { + return interfaces; + } + + for (i=interfaces;i;i=i->next) { + if (check_mask) { + if (same_net(ip, (struct sockaddr *)&i->ip, (struct sockaddr *)&i->netmask)) { + return i; + } + } else if (sockaddr_equal((struct sockaddr *)&i->ip, ip)) { + return i; + } + } return NULL; } - /**************************************************************************** add an interface to the linked list of interfaces ****************************************************************************/ -static void add_interface(TALLOC_CTX *mem_ctx, struct in_addr ip, struct in_addr nmask, struct interface **interfaces) +static void add_interface(TALLOC_CTX *mem_ctx, const struct iface_struct *ifs, struct interface **interfaces) { + char addr[INET6_ADDRSTRLEN]; struct interface *iface; - struct in_addr bcast; - if (iface_find(*interfaces, ip, false)) { - DEBUG(3,("not adding duplicate interface %s\n",inet_ntoa(ip))); + if (iface_find(*interfaces, (const struct sockaddr *)&ifs->ip, false)) { + DEBUG(3,("add_interface: not adding duplicate interface %s\n", + print_sockaddr(addr, sizeof(addr), &ifs->ip) )); + return; + } + + if (!(ifs->flags & (IFF_BROADCAST|IFF_LOOPBACK))) { + DEBUG(3,("not adding non-broadcast interface %s\n", + ifs->name )); return; } @@ -79,26 +97,36 @@ static void add_interface(TALLOC_CTX *mem_ctx, struct in_addr ip, struct in_addr ZERO_STRUCTPN(iface); - iface->ip = ip; - iface->nmask = nmask; - bcast.s_addr = MKBCADDR(iface->ip.s_addr, iface->nmask.s_addr); + iface->name = talloc_strdup(iface, ifs->name); + if (!iface->name) { + SAFE_FREE(iface); + return; + } + iface->flags = ifs->flags; + iface->ip = ifs->ip; + iface->netmask = ifs->netmask; + iface->bcast = ifs->bcast; /* keep string versions too, to avoid people tripping over the implied static in inet_ntoa() */ - iface->ip_s = talloc_strdup(iface, inet_ntoa(iface->ip)); - iface->nmask_s = talloc_strdup(iface, inet_ntoa(iface->nmask)); - - if (nmask.s_addr != ~0) { - iface->bcast_s = talloc_strdup(iface, inet_ntoa(bcast)); - } - - DLIST_ADD_END(*interfaces, iface, struct interface *); - - DEBUG(3,("added interface ip=%s nmask=%s\n", iface->ip_s, iface->nmask_s)); + print_sockaddr(addr, sizeof(addr), &iface->ip); + DEBUG(2,("added interface %s ip=%s ", + iface->name, addr)); + iface->ip_s = talloc_strdup(iface, addr); + + print_sockaddr(addr, sizeof(addr), + &iface->bcast); + DEBUG(2,("bcast=%s ", addr)); + iface->bcast_s = talloc_strdup(iface, addr); + + print_sockaddr(addr, sizeof(addr), + &iface->netmask); + DEBUG(2,("netmask=%s\n", addr)); + iface->nmask_s = talloc_strdup(iface, addr); + + DLIST_ADD(*interfaces, iface); } - - /** interpret a single element from a interfaces= config line @@ -116,77 +144,134 @@ static void interpret_interface(TALLOC_CTX *mem_ctx, int total_probed, struct interface **local_interfaces) { - struct in_addr ip, nmask; + struct sockaddr_storage ss; + struct sockaddr_storage ss_mask; + struct sockaddr_storage ss_net; + struct sockaddr_storage ss_bcast; + struct iface_struct ifs; char *p; - char *address; - int i, added=0; + int i; + bool added=false; + bool goodaddr = false; - ip.s_addr = 0; - nmask.s_addr = 0; - /* first check if it is an interface name */ for (i=0;i<total_probed;i++) { if (gen_fnmatch(token, probed_ifaces[i].name) == 0) { - add_interface(mem_ctx, probed_ifaces[i].ip, - probed_ifaces[i].netmask, + add_interface(mem_ctx, &probed_ifaces[i], local_interfaces); - added = 1; + added = true; } } - if (added) return; + if (added) { + return; + } /* maybe it is a DNS name */ p = strchr_m(token,'/'); - if (!p) { - /* don't try to do dns lookups on wildcard names */ - if (strpbrk(token, "*?") != NULL) { + if (p == NULL) { + if (!interpret_string_addr(&ss, token, 0)) { + DEBUG(2, ("interpret_interface: Can't find address " + "for %s\n", token)); return; } - ip.s_addr = interpret_addr2(token).s_addr; + for (i=0;i<total_probed;i++) { - if (ip.s_addr == probed_ifaces[i].ip.s_addr) { - add_interface(mem_ctx, probed_ifaces[i].ip, - probed_ifaces[i].netmask, + if (sockaddr_equal((struct sockaddr *)&ss, (struct sockaddr *)&probed_ifaces[i].ip)) { + add_interface(mem_ctx, &probed_ifaces[i], local_interfaces); return; } } - DEBUG(2,("can't determine netmask for %s\n", token)); + DEBUG(2,("interpret_interface: " + "can't determine interface for %s\n", + token)); return; } - address = talloc_strdup(mem_ctx, token); - p = strchr_m(address,'/'); - /* parse it into an IP address/netmasklength pair */ - *p++ = 0; - - ip.s_addr = interpret_addr2(address).s_addr; + *p = 0; + goodaddr = interpret_string_addr(&ss, token, 0); + *p++ = '/'; + + if (!goodaddr) { + DEBUG(2,("interpret_interface: " + "can't determine interface for %s\n", + token)); + return; + } if (strlen(p) > 2) { - nmask.s_addr = interpret_addr2(p).s_addr; + goodaddr = interpret_string_addr(&ss_mask, p, 0); + if (!goodaddr) { + DEBUG(2,("interpret_interface: " + "can't determine netmask from %s\n", + p)); + return; + } } else { - nmask.s_addr = htonl(((ALLONES >> atoi(p)) ^ ALLONES)); + char *endp = NULL; + unsigned long val = strtoul(p, &endp, 0); + if (p == endp || (endp && *endp != '\0')) { + DEBUG(2,("interpret_interface: " + "can't determine netmask value from %s\n", + p)); + return; + } + if (!make_netmask(&ss_mask, &ss, val)) { + DEBUG(2,("interpret_interface: " + "can't apply netmask value %lu from %s\n", + val, + p)); + return; + } } - /* maybe the first component was a broadcast address */ - if (ip.s_addr == MKBCADDR(ip.s_addr, nmask.s_addr) || - ip.s_addr == MKNETADDR(ip.s_addr, nmask.s_addr)) { + make_bcast(&ss_bcast, &ss, &ss_mask); + make_net(&ss_net, &ss, &ss_mask); + + /* Maybe the first component was a broadcast address. */ + if (sockaddr_equal((struct sockaddr *)&ss_bcast, (struct sockaddr *)&ss) || + sockaddr_equal((struct sockaddr *)&ss_net, (struct sockaddr *)&ss)) { for (i=0;i<total_probed;i++) { - if (same_net_v4(ip, probed_ifaces[i].ip, nmask)) { - add_interface(mem_ctx, probed_ifaces[i].ip, nmask, + if (same_net((struct sockaddr *)&ss, + (struct sockaddr *)&probed_ifaces[i].ip, + (struct sockaddr *)&ss_mask)) { + /* Temporarily replace netmask on + * the detected interface - user knows + * best.... */ + struct sockaddr_storage saved_mask = + probed_ifaces[i].netmask; + probed_ifaces[i].netmask = ss_mask; + DEBUG(2,("interpret_interface: " + "using netmask value %s from " + "config file on interface %s\n", + p, + probed_ifaces[i].name)); + add_interface(mem_ctx, &probed_ifaces[i], local_interfaces); - talloc_free(address); + probed_ifaces[i].netmask = saved_mask; return; } } - DEBUG(2,("Can't determine ip for broadcast address %s\n", address)); - talloc_free(address); + DEBUG(2,("interpret_interface: Can't determine ip for " + "broadcast address %s\n", + token)); return; } - add_interface(mem_ctx, ip, nmask, local_interfaces); - talloc_free(address); + /* Just fake up the interface definition. User knows best. */ + + DEBUG(2,("interpret_interface: Adding interface %s\n", + token)); + + ZERO_STRUCT(ifs); + (void)strlcpy(ifs.name, token, sizeof(ifs.name)); + ifs.flags = IFF_BROADCAST; + ifs.ip = ss; + ifs.netmask = ss_mask; + ifs.bcast = ss_bcast; + add_interface(mem_ctx, &ifs, + local_interfaces); } @@ -197,16 +282,13 @@ void load_interfaces(TALLOC_CTX *mem_ctx, const char **interfaces, struct interf { const char **ptr = interfaces; int i; - struct iface_struct ifaces[MAX_INTERFACES]; - struct in_addr loopback_ip; + struct iface_struct *ifaces; int total_probed; *local_interfaces = NULL; - loopback_ip = interpret_addr2("127.0.0.1"); - /* probe the kernel for interfaces */ - total_probed = get_interfaces(ifaces, MAX_INTERFACES); + total_probed = get_interfaces(mem_ctx, &ifaces); /* if we don't have a interfaces line then use all interfaces except loopback */ @@ -215,9 +297,8 @@ void load_interfaces(TALLOC_CTX *mem_ctx, const char **interfaces, struct interf DEBUG(0,("ERROR: Could not determine network interfaces, you must use a interfaces config line\n")); } for (i=0;i<total_probed;i++) { - if (ifaces[i].ip.s_addr != loopback_ip.s_addr) { - add_interface(mem_ctx, ifaces[i].ip, - ifaces[i].netmask, local_interfaces); + if (!is_loopback_addr(&ifaces[i].ip)) { + add_interface(mem_ctx, &ifaces[i], local_interfaces); } } } @@ -230,6 +311,7 @@ void load_interfaces(TALLOC_CTX *mem_ctx, const char **interfaces, struct interf if (!*local_interfaces) { DEBUG(0,("WARNING: no network interfaces found\n")); } + talloc_free(ifaces); } /** @@ -300,10 +382,12 @@ const char *iface_n_netmask(struct interface *ifaces, int n) const char *iface_best_ip(struct interface *ifaces, const char *dest) { struct interface *iface; - struct in_addr ip; + struct sockaddr_storage ss; - ip.s_addr = interpret_addr(dest); - iface = iface_find(ifaces, ip, true); + if (!interpret_string_addr(&ss, dest, AI_NUMERICHOST)) { + return iface_n_ip(ifaces, 0); + } + iface = iface_find(ifaces, (const struct sockaddr *)&ss, true); if (iface) { return iface->ip_s; } @@ -315,10 +399,12 @@ const char *iface_best_ip(struct interface *ifaces, const char *dest) */ bool iface_is_local(struct interface *ifaces, const char *dest) { - struct in_addr ip; + struct sockaddr_storage ss; - ip.s_addr = interpret_addr(dest); - if (iface_find(ifaces, ip, true)) { + if (!interpret_string_addr(&ss, dest, AI_NUMERICHOST)) { + return false; + } + if (iface_find(ifaces, (const struct sockaddr *)&ss, true)) { return true; } return false; diff --git a/source4/lib/socket/netif.c b/source4/lib/socket/netif.c index e36f268bde..2846813d3f 100644 --- a/source4/lib/socket/netif.c +++ b/source4/lib/socket/netif.c @@ -37,6 +37,101 @@ #include "lib/util/tsort.h" /**************************************************************************** + Create a struct sockaddr_storage with the netmask bits set to 1. +****************************************************************************/ + +bool make_netmask(struct sockaddr_storage *pss_out, + const struct sockaddr_storage *pss_in, + unsigned long masklen) +{ + *pss_out = *pss_in; + /* Now apply masklen bits of mask. */ +#if defined(HAVE_IPV6) + if (pss_in->ss_family == AF_INET6) { + char *p = (char *)&((struct sockaddr_in6 *)pss_out)->sin6_addr; + unsigned int i; + + if (masklen > 128) { + return false; + } + for (i = 0; masklen >= 8; masklen -= 8, i++) { + *p++ = 0xff; + } + /* Deal with the partial byte. */ + *p++ &= (0xff & ~(0xff>>masklen)); + i++; + for (;i < sizeof(struct in6_addr); i++) { + *p++ = '\0'; + } + return true; + } +#endif + if (pss_in->ss_family == AF_INET) { + if (masklen > 32) { + return false; + } + ((struct sockaddr_in *)pss_out)->sin_addr.s_addr = + htonl(((0xFFFFFFFFL >> masklen) ^ 0xFFFFFFFFL)); + return true; + } + return false; +} + +/**************************************************************************** + Create a struct sockaddr_storage set to the broadcast or network adress from + an incoming sockaddr_storage. +****************************************************************************/ + +static void make_bcast_or_net(struct sockaddr_storage *pss_out, + const struct sockaddr_storage *pss_in, + const struct sockaddr_storage *nmask, + bool make_bcast_p) +{ + unsigned int i = 0, len = 0; + char *pmask = NULL; + char *p = NULL; + *pss_out = *pss_in; + + /* Set all zero netmask bits to 1. */ +#if defined(HAVE_IPV6) + if (pss_in->ss_family == AF_INET6) { + p = (char *)&((struct sockaddr_in6 *)pss_out)->sin6_addr; + pmask = discard_const_p(char, &((struct sockaddr_in6 *)nmask)->sin6_addr); + len = 16; + } +#endif + if (pss_in->ss_family == AF_INET) { + p = (char *)&((struct sockaddr_in *)pss_out)->sin_addr; + pmask = discard_const_p(char, &((struct sockaddr_in *)nmask)->sin_addr); + len = 4; + } + + for (i = 0; i < len; i++, p++, pmask++) { + if (make_bcast_p) { + *p = (*p & *pmask) | (*pmask ^ 0xff); + } else { + /* make_net */ + *p = (*p & *pmask); + } + } +} + +void make_bcast(struct sockaddr_storage *pss_out, + const struct sockaddr_storage *pss_in, + const struct sockaddr_storage *nmask) +{ + make_bcast_or_net(pss_out, pss_in, nmask, true); +} + +void make_net(struct sockaddr_storage *pss_out, + const struct sockaddr_storage *pss_in, + const struct sockaddr_storage *nmask) +{ + make_bcast_or_net(pss_out, pss_in, nmask, false); +} + + +/**************************************************************************** Try the "standard" getifaddrs/freeifaddrs interfaces. Also gets IPv6 interfaces. ****************************************************************************/ @@ -45,22 +140,38 @@ Get the netmask address for a local interface. ****************************************************************************/ -static int _get_interfaces(struct iface_struct *ifaces, int max_interfaces) +static int _get_interfaces(TALLOC_CTX *mem_ctx, struct iface_struct **pifaces) { + struct iface_struct *ifaces; struct ifaddrs *iflist = NULL; struct ifaddrs *ifptr = NULL; + int count; int total = 0; + size_t copy_size; if (getifaddrs(&iflist) < 0) { return -1; } - /* Loop through interfaces, looking for given IP address */ - for (ifptr = iflist, total = 0; - ifptr != NULL && total < max_interfaces; - ifptr = ifptr->ifa_next) { + count = 0; + for (ifptr = iflist; ifptr != NULL; ifptr = ifptr->ifa_next) { + if (!ifptr->ifa_addr || !ifptr->ifa_netmask) { + continue; + } + if (!(ifptr->ifa_flags & IFF_UP)) { + continue; + } + count += 1; + } - memset(&ifaces[total], '\0', sizeof(ifaces[total])); + ifaces = talloc_array(mem_ctx, struct iface_struct, count); + if (ifaces == NULL) { + errno = ENOMEM; + return -1; + } + + /* Loop through interfaces, looking for given IP address */ + for (ifptr = iflist; ifptr != NULL; ifptr = ifptr->ifa_next) { if (!ifptr->ifa_addr || !ifptr->ifa_netmask) { continue; @@ -71,13 +182,33 @@ static int _get_interfaces(struct iface_struct *ifaces, int max_interfaces) continue; } - /* We don't support IPv6 *yet* */ - if (ifptr->ifa_addr->sa_family != AF_INET) { - continue; + memset(&ifaces[total], '\0', sizeof(ifaces[total])); + + copy_size = sizeof(struct sockaddr_in); + + ifaces[total].flags = ifptr->ifa_flags; + +#if defined(HAVE_IPV6) + if (ifptr->ifa_addr->sa_family == AF_INET6) { + copy_size = sizeof(struct sockaddr_in6); } +#endif + + memcpy(&ifaces[total].ip, ifptr->ifa_addr, copy_size); + memcpy(&ifaces[total].netmask, ifptr->ifa_netmask, copy_size); - ifaces[total].ip = ((struct sockaddr_in *)ifptr->ifa_addr)->sin_addr; - ifaces[total].netmask = ((struct sockaddr_in *)ifptr->ifa_netmask)->sin_addr; + if (ifaces[total].flags & (IFF_BROADCAST|IFF_LOOPBACK)) { + make_bcast(&ifaces[total].bcast, + &ifaces[total].ip, + &ifaces[total].netmask); + } else if ((ifaces[total].flags & IFF_POINTOPOINT) && + ifptr->ifa_dstaddr ) { + memcpy(&ifaces[total].bcast, + ifptr->ifa_dstaddr, + copy_size); + } else { + continue; + } strlcpy(ifaces[total].name, ifptr->ifa_name, sizeof(ifaces[total].name)); @@ -86,27 +217,82 @@ static int _get_interfaces(struct iface_struct *ifaces, int max_interfaces) freeifaddrs(iflist); + *pifaces = ifaces; return total; } static int iface_comp(struct iface_struct *i1, struct iface_struct *i2) { int r; - r = strcmp(i1->name, i2->name); - if (r) return r; - r = ntohl(i1->ip.s_addr) - ntohl(i2->ip.s_addr); - if (r) return r; - r = ntohl(i1->netmask.s_addr) - ntohl(i2->netmask.s_addr); - return r; + +#if defined(HAVE_IPV6) + /* + * If we have IPv6 - sort these interfaces lower + * than any IPv4 ones. + */ + if (i1->ip.ss_family == AF_INET6 && + i2->ip.ss_family == AF_INET) { + return -1; + } else if (i1->ip.ss_family == AF_INET && + i2->ip.ss_family == AF_INET6) { + return 1; + } + + if (i1->ip.ss_family == AF_INET6) { + struct sockaddr_in6 *s1 = (struct sockaddr_in6 *)&i1->ip; + struct sockaddr_in6 *s2 = (struct sockaddr_in6 *)&i2->ip; + + r = memcmp(&s1->sin6_addr, + &s2->sin6_addr, + sizeof(struct in6_addr)); + if (r) { + return r; + } + + s1 = (struct sockaddr_in6 *)&i1->netmask; + s2 = (struct sockaddr_in6 *)&i2->netmask; + + r = memcmp(&s1->sin6_addr, + &s2->sin6_addr, + sizeof(struct in6_addr)); + if (r) { + return r; + } + } +#endif + + /* AIX uses __ss_family instead of ss_family inside of + sockaddr_storage. Instead of trying to figure out which field to + use, we can just cast it to a sockaddr. + */ + + if (((struct sockaddr *)&i1->ip)->sa_family == AF_INET) { + struct sockaddr_in *s1 = (struct sockaddr_in *)&i1->ip; + struct sockaddr_in *s2 = (struct sockaddr_in *)&i2->ip; + + r = ntohl(s1->sin_addr.s_addr) - + ntohl(s2->sin_addr.s_addr); + if (r) { + return r; + } + + s1 = (struct sockaddr_in *)&i1->netmask; + s2 = (struct sockaddr_in *)&i2->netmask; + + return ntohl(s1->sin_addr.s_addr) - + ntohl(s2->sin_addr.s_addr); + } + return 0; } /* this wrapper is used to remove duplicates from the interface list generated above */ -int get_interfaces(struct iface_struct *ifaces, int max_interfaces) +int get_interfaces(TALLOC_CTX *mem_ctx, struct iface_struct **pifaces) { + struct iface_struct *ifaces; int total, i, j; - total = _get_interfaces(ifaces, max_interfaces); + total = _get_interfaces(mem_ctx, &ifaces); if (total <= 0) return total; /* now we need to remove duplicates */ @@ -123,5 +309,6 @@ int get_interfaces(struct iface_struct *ifaces, int max_interfaces) } } + *pifaces = ifaces; return total; } diff --git a/source4/lib/socket/netif.h b/source4/lib/socket/netif.h index 417c6e074f..6a06c4bf15 100644 --- a/source4/lib/socket/netif.h +++ b/source4/lib/socket/netif.h @@ -23,8 +23,10 @@ struct iface_struct { char name[16]; - struct in_addr ip; - struct in_addr netmask; + int flags; + struct sockaddr_storage ip; + struct sockaddr_storage netmask; + struct sockaddr_storage bcast; }; struct interface; |