From d6a5476ee7af464a381bbeeec576ee58f3650a43 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Thu, 23 Oct 2008 19:53:15 +0200 Subject: Use sockaddr_storage only where we rely on the size, use sockaddr otherwise (to clarify we can also pass in structs smaller than sockaddr_storage, such as sockaddr_in). --- source3/lib/access.c | 2 +- source3/lib/interface.c | 41 ++++++++++++----------- source3/lib/util_sock.c | 89 ++++++++++++++++++++++++------------------------- 3 files changed, 67 insertions(+), 65 deletions(-) (limited to 'source3/lib') diff --git a/source3/lib/access.c b/source3/lib/access.c index 966d8ce87c..0b09e83ce3 100644 --- a/source3/lib/access.c +++ b/source3/lib/access.c @@ -66,7 +66,7 @@ static bool masked_match(const char *tok, const char *slash, const char *s) } } - return same_net(&ss_host, &ss_tok, &ss_mask); + return same_net((struct sockaddr *)&ss_host, (struct sockaddr *)&ss_tok, (struct sockaddr *)&ss_mask); } /* string_match - match string s against token tok */ diff --git a/source3/lib/interface.c b/source3/lib/interface.c index 2e7c2706a0..4536990d28 100644 --- a/source3/lib/interface.c +++ b/source3/lib/interface.c @@ -29,11 +29,11 @@ static struct interface *local_interfaces; Check if an IP is one of mine. **************************************************************************/ -bool ismyaddr(const struct sockaddr_storage *ip) +bool ismyaddr(const struct sockaddr *ip) { struct interface *i; for (i=local_interfaces;i;i=i->next) { - if (addr_equal(&i->ip,ip)) { + if (addr_equal((struct sockaddr *)&i->ip,ip)) { return true; } } @@ -44,14 +44,14 @@ bool ismyip_v4(struct in_addr ip) { struct sockaddr_storage ss; in_addr_to_sockaddr_storage(&ss, ip); - return ismyaddr(&ss); + return ismyaddr((struct sockaddr *)&ss); } /**************************************************************************** Try and find an interface that matches an ip. If we cannot, return NULL. **************************************************************************/ -static struct interface *iface_find(const struct sockaddr_storage *ip, +static struct interface *iface_find(const struct sockaddr *ip, bool check_mask) { struct interface *i; @@ -62,10 +62,10 @@ static struct interface *iface_find(const struct sockaddr_storage *ip, for (i=local_interfaces;i;i=i->next) { if (check_mask) { - if (same_net(ip, &i->ip, &i->netmask)) { + if (same_net(ip, (struct sockaddr *)&i->ip, (struct sockaddr *)&i->netmask)) { return i; } - } else if (addr_equal(&i->ip, ip)) { + } else if (addr_equal((struct sockaddr *)&i->ip, ip)) { return i; } } @@ -77,11 +77,11 @@ static struct interface *iface_find(const struct sockaddr_storage *ip, Check if a packet is from a local (known) net. **************************************************************************/ -bool is_local_net(const struct sockaddr_storage *from) +bool is_local_net(const struct sockaddr *from) { struct interface *i; for (i=local_interfaces;i;i=i->next) { - if (same_net(from, &i->ip, &i->netmask)) { + if (same_net(from, (struct sockaddr *)&i->ip, (struct sockaddr *)&i->netmask)) { return true; } } @@ -89,11 +89,11 @@ bool is_local_net(const struct sockaddr_storage *from) } #if defined(HAVE_IPV6) -void setup_linklocal_scope_id(struct sockaddr_storage *pss) +void setup_linklocal_scope_id(struct sockaddr *pss) { struct interface *i; for (i=local_interfaces;i;i=i->next) { - if (addr_equal(&i->ip,pss)) { + if (addr_equal((struct sockaddr *)&i->ip,pss)) { struct sockaddr_in6 *psa6 = (struct sockaddr_in6 *)pss; psa6->sin6_scope_id = if_nametoindex(i->name); @@ -112,7 +112,7 @@ bool is_local_net_v4(struct in_addr from) struct sockaddr_storage ss; in_addr_to_sockaddr_storage(&ss, from); - return is_local_net(&ss); + return is_local_net((struct sockaddr *)&ss); } /**************************************************************************** @@ -140,7 +140,7 @@ int iface_count_v4_nl(void) struct interface *i; for (i=local_interfaces;i;i=i->next) { - if (is_loopback_addr(&i->ip)) { + if (is_loopback_addr((struct sockaddr *)&i->ip)) { continue; } if (i->ip.ss_family == AF_INET) { @@ -265,7 +265,7 @@ const struct sockaddr_storage *iface_n_bcast(int n) an appropriate interface they return the requested field of the first known interface. */ -const struct sockaddr_storage *iface_ip(const struct sockaddr_storage *ip) +const struct sockaddr_storage *iface_ip(const struct sockaddr *ip) { struct interface *i = iface_find(ip, true); if (i) { @@ -276,7 +276,7 @@ const struct sockaddr_storage *iface_ip(const struct sockaddr_storage *ip) * matching address family. */ for (i=local_interfaces;i;i=i->next) { - if (i->ip.ss_family == ip->ss_family) { + if (i->ip.ss_family == ip->sa_family) { return &i->ip; } } @@ -287,7 +287,7 @@ const struct sockaddr_storage *iface_ip(const struct sockaddr_storage *ip) return True if a IP is directly reachable on one of our interfaces */ -bool iface_local(const struct sockaddr_storage *ip) +bool iface_local(const struct sockaddr *ip) { return iface_find(ip, True) ? true : false; } @@ -301,7 +301,7 @@ static void add_interface(const struct iface_struct *ifs) char addr[INET6_ADDRSTRLEN]; struct interface *iface; - if (iface_find(&ifs->ip, False)) { + if (iface_find((struct sockaddr *)&ifs->ip, False)) { DEBUG(3,("add_interface: not adding duplicate interface %s\n", print_sockaddr(addr, sizeof(addr), &ifs->ip) )); return; @@ -388,7 +388,7 @@ static void interpret_interface(char *token) } for (i=0;iss_family == AF_INET6) { + if (pss->sa_family == AF_INET6) { const struct in6_addr *sin6 = &((const struct sockaddr_in6 *)pss)->sin6_addr; return IN6_IS_ADDR_MULTICAST(sin6); } #endif - if (pss->ss_family == AF_INET) { + if (pss->sa_family == AF_INET) { uint32_t addr = ntohl(((const struct sockaddr_in *)pss)->sin_addr.s_addr); return addr == INADDR_BROADCAST; @@ -271,19 +271,19 @@ bool is_loopback_ip_v4(struct in_addr ip) } /******************************************************************* - Check if a struct sockaddr_storage is the loopback address. + Check if a struct sockaddr is the loopback address. ******************************************************************/ -bool is_loopback_addr(const struct sockaddr_storage *pss) +bool is_loopback_addr(const struct sockaddr *pss) { #if defined(HAVE_IPV6) - if (pss->ss_family == AF_INET6) { + if (pss->sa_family == AF_INET6) { struct in6_addr *pin6 = &((struct sockaddr_in6 *)pss)->sin6_addr; return IN6_IS_ADDR_LOOPBACK(pin6); } #endif - if (pss->ss_family == AF_INET) { + if (pss->sa_family == AF_INET) { struct in_addr *pin = &((struct sockaddr_in *)pss)->sin_addr; return is_loopback_ip_v4(*pin); } @@ -302,19 +302,19 @@ bool is_zero_ip_v4(struct in_addr ip) } /******************************************************************* - Check if a struct sockaddr_storage has an unspecified address. + Check if a struct sockaddr has an unspecified address. ******************************************************************/ -bool is_zero_addr(const struct sockaddr_storage *pss) +bool is_zero_addr(const struct sockaddr *pss) { #if defined(HAVE_IPV6) - if (pss->ss_family == AF_INET6) { + if (pss->sa_family == AF_INET6) { struct in6_addr *pin6 = &((struct sockaddr_in6 *)pss)->sin6_addr; return IN6_IS_ADDR_UNSPECIFIED(pin6); } #endif - if (pss->ss_family == AF_INET) { + if (pss->sa_family == AF_INET) { struct in_addr *pin = &((struct sockaddr_in *)pss)->sin_addr; return is_zero_ip_v4(*pin); } @@ -388,17 +388,17 @@ void in_addr_to_sockaddr_storage(struct sockaddr_storage *ss, Are two IPs on the same subnet? ********************************************************************/ -bool same_net(const struct sockaddr_storage *ip1, - const struct sockaddr_storage *ip2, - const struct sockaddr_storage *mask) +bool same_net(const struct sockaddr *ip1, + const struct sockaddr *ip2, + const struct sockaddr *mask) { - if (ip1->ss_family != ip2->ss_family) { + if (ip1->sa_family != ip2->sa_family) { /* Never on the same net. */ return false; } #if defined(HAVE_IPV6) - if (ip1->ss_family == AF_INET6) { + if (ip1->sa_family == AF_INET6) { struct sockaddr_in6 ip1_6 = *(struct sockaddr_in6 *)ip1; struct sockaddr_in6 ip2_6 = *(struct sockaddr_in6 *)ip2; struct sockaddr_in6 mask_6 = *(struct sockaddr_in6 *)mask; @@ -417,7 +417,7 @@ bool same_net(const struct sockaddr_storage *ip1, sizeof(struct in6_addr)) == 0); } #endif - if (ip1->ss_family == AF_INET) { + if (ip1->sa_family == AF_INET) { return same_net_v4(((const struct sockaddr_in *)ip1)->sin_addr, ((const struct sockaddr_in *)ip2)->sin_addr, ((const struct sockaddr_in *)mask)->sin_addr); @@ -426,25 +426,25 @@ bool same_net(const struct sockaddr_storage *ip1, } /******************************************************************* - Are two sockaddr_storage's the same family and address ? Ignore port etc. + Are two sockaddr 's the same family and address ? Ignore port etc. ********************************************************************/ -bool addr_equal(const struct sockaddr_storage *ip1, - const struct sockaddr_storage *ip2) +bool addr_equal(const struct sockaddr *ip1, + const struct sockaddr *ip2) { - if (ip1->ss_family != ip2->ss_family) { + if (ip1->sa_family != ip2->sa_family) { /* Never the same. */ return false; } #if defined(HAVE_IPV6) - if (ip1->ss_family == AF_INET6) { + if (ip1->sa_family == AF_INET6) { return (memcmp(&((const struct sockaddr_in6 *)ip1)->sin6_addr, &((const struct sockaddr_in6 *)ip2)->sin6_addr, sizeof(struct in6_addr)) == 0); } #endif - if (ip1->ss_family == AF_INET) { + if (ip1->sa_family == AF_INET) { return (memcmp(&((const struct sockaddr_in *)ip1)->sin_addr, &((const struct sockaddr_in *)ip2)->sin_addr, sizeof(struct in_addr)) == 0); @@ -456,10 +456,10 @@ bool addr_equal(const struct sockaddr_storage *ip1, Is an IP address the INADDR_ANY or in6addr_any value ? ****************************************************************************/ -bool is_address_any(const struct sockaddr_storage *psa) +bool is_address_any(const struct sockaddr *psa) { #if defined(HAVE_IPV6) - if (psa->ss_family == AF_INET6) { + if (psa->sa_family == AF_INET6) { struct sockaddr_in6 *si6 = (struct sockaddr_in6 *)psa; if (memcmp(&in6addr_any, &si6->sin6_addr, @@ -469,7 +469,7 @@ bool is_address_any(const struct sockaddr_storage *psa) return false; } #endif - if (psa->ss_family == AF_INET) { + if (psa->sa_family == AF_INET) { struct sockaddr_in *si = (struct sockaddr_in *)psa; if (si->sin_addr.s_addr == INADDR_ANY) { return true; @@ -508,13 +508,13 @@ uint16_t get_sockaddr_port(const struct sockaddr_storage *pss) static char *print_sockaddr_len(char *dest, size_t destlen, - const struct sockaddr_storage *psa, + const struct sockaddr *psa, socklen_t psalen) { if (destlen > 0) { dest[0] = '\0'; } - (void)sys_getnameinfo((const struct sockaddr *)psa, + (void)sys_getnameinfo(psa, psalen, dest, destlen, NULL, 0, @@ -530,7 +530,7 @@ char *print_sockaddr(char *dest, size_t destlen, const struct sockaddr_storage *psa) { - return print_sockaddr_len(dest, destlen, psa, + return print_sockaddr_len(dest, destlen, (struct sockaddr *)psa, sizeof(struct sockaddr_storage)); } @@ -596,7 +596,7 @@ static const char *get_socket_addr(int fd, char *addr_buf, size_t addr_len) return addr_buf; } - return print_sockaddr_len(addr_buf, addr_len, &sa, length); + return print_sockaddr_len(addr_buf, addr_len, (struct sockaddr *)&sa, length); } #if 0 @@ -1313,7 +1313,7 @@ int open_socket_out(int type, psa6->sin6_port = htons(port); if (psa6->sin6_scope_id == 0 && IN6_IS_ADDR_LINKLOCAL(&psa6->sin6_addr)) { - setup_linklocal_scope_id(&sock_out); + setup_linklocal_scope_id((struct sockaddr *)&sock_out); } } #endif @@ -1583,7 +1583,7 @@ int open_udp_socket(const char *host, int port) static const char *get_peer_addr_internal(int fd, char *addr_buf, size_t addr_buf_len, - struct sockaddr_storage *pss, + struct sockaddr *pss, socklen_t *plength) { struct sockaddr_storage ss; @@ -1596,9 +1596,7 @@ static const char *get_peer_addr_internal(int fd, } if (pss == NULL) { - pss = &ss; - } - if (plength == NULL) { + pss = (struct sockaddr *)&ss; plength = &length; } @@ -1621,7 +1619,7 @@ static const char *get_peer_addr_internal(int fd, ******************************************************************/ static bool matchname(const char *remotehost, - const struct sockaddr_storage *pss, + const struct sockaddr *pss, socklen_t len) { struct addrinfo *res = NULL; @@ -1659,8 +1657,8 @@ static bool matchname(const char *remotehost, if (!res->ai_addr) { continue; } - if (addr_equal((const struct sockaddr_storage *)res->ai_addr, - pss)) { + if (addr_equal((const struct sockaddr *)res->ai_addr, + (struct sockaddr *)pss)) { freeaddrinfo(ailist); return true; } @@ -1760,7 +1758,7 @@ const char *get_peer_name(int fd, bool force_lookup) if (!lp_hostname_lookups() && (force_lookup == false)) { length = sizeof(nc.ss); nc.name = get_peer_addr_internal(fd, addr_buf, sizeof(addr_buf), - &nc.ss, &length); + (struct sockaddr *)&nc.ss, &length); store_nc(&nc); lookup_nc(&nc); return nc.name ? nc.name : "UNKNOWN"; @@ -1769,10 +1767,10 @@ const char *get_peer_name(int fd, bool force_lookup) lookup_nc(&nc); memset(&ss, '\0', sizeof(ss)); - p = get_peer_addr_internal(fd, addr_buf, sizeof(addr_buf), &ss, &length); + p = get_peer_addr_internal(fd, addr_buf, sizeof(addr_buf), (struct sockaddr *)&ss, &length); /* it might be the same as the last one - save some DNS work */ - if (addr_equal(&ss, &nc.ss)) { + if (addr_equal((struct sockaddr *)&ss, (struct sockaddr *)&nc.ss)) { return nc.name ? nc.name : "UNKNOWN"; } @@ -1797,7 +1795,7 @@ const char *get_peer_name(int fd, bool force_lookup) gai_strerror(ret))); strlcpy(name_buf, p, sizeof(name_buf)); } else { - if (!matchname(name_buf, &ss, length)) { + if (!matchname(name_buf, (struct sockaddr *)&ss, length)) { DEBUG(0,("Matchname failed on %s %s\n",name_buf,p)); strlcpy(name_buf,"UNKNOWN",sizeof(name_buf)); } @@ -2080,7 +2078,8 @@ bool is_myname_or_ipaddr(const char *s) return false; } - if (is_zero_addr(&ss) || is_loopback_addr(&ss)) { + if (is_zero_addr((struct sockaddr *)&ss) || + is_loopback_addr((struct sockaddr *)&ss)) { return false; } @@ -2091,7 +2090,7 @@ bool is_myname_or_ipaddr(const char *s) } n = get_interfaces(nics, MAX_INTERFACES); for (i=0; i