Browse Source

core: attempt harder to filter loopback addresses

- on startup detect not only loopback interfaces (e.g. -l lo) but also
ipv4 and ipv6 loopback addresses (e.g. 127.0.0.1, ::1). This helps filtering
 them out when choosing the default socket for each of the protocols.
Andrei Pelinescu-Onciul 17 years ago
parent
commit
1571613cc0
3 changed files with 18 additions and 1 deletions
  1. 1 1
      ip_addr.c
  2. 15 0
      ip_addr.h
  3. 2 0
      socket_info.c

+ 1 - 1
ip_addr.c

@@ -190,7 +190,7 @@ int is_mcast(struct ip_addr* ip)
 		return IN_MULTICAST(htonl(ip->u.addr32[0]));
 #ifdef USE_IPV6
 	} else if (ip->af==AF_INET6){
-		return IN6_IS_ADDR_MULTICAST((struct in6_addr *)ip->u.addr);
+		return IN6_IS_ADDR_MULTICAST((struct in6_addr *)&ip->u.addr);
 #endif /* USE_IPV6 */
 	} else {
 		LOG(L_ERR, "ERROR: is_mcast: Unsupported protocol family\n");

+ 15 - 0
ip_addr.h

@@ -234,6 +234,21 @@ inline static int ip_addr_any(struct ip_addr* ip)
 
 
 
+/* returns 1 if the given ip address is a loopback address
+ * 0 otherwise */
+inline static int ip_addr_loopback(struct ip_addr* ip)
+{
+	if (ip->af==AF_INET){
+		return ip->u.addr32[0]==htonl(INADDR_LOOPBACK);
+#ifdef USE_IPV6
+	} else if (ip->af==AF_INET6)
+		return IN6_IS_ADDR_LOOPBACK((struct in6_addr *)&ip->u.addr32);
+#endif /* USE_IPV6 */
+	return 0;
+}
+
+
+
 /* creates an ANY ip_addr (filled with 0, af and len properly set) */
 inline static void ip_addr_mk_any(int af, struct ip_addr* ip)
 {

+ 2 - 0
socket_info.c

@@ -849,6 +849,8 @@ static int fix_hostname(str* name, struct ip_addr* address, str* address_str,
 	/* check if INADDR_ANY */
 	if (ip_addr_any(address))
 		*flags|=SI_IS_ANY;
+	else if (ip_addr_loopback(address)) /* check for loopback */
+		*flags|=SI_IS_LO;
 	
 	return 0;
 error: