ipv6: support interface name for scope lookup 21/235621/2 accepted/tizen_6.0_unified_hotfix tizen_6.0_hotfix accepted/tizen/6.0/unified/20201030.110007 accepted/tizen/6.0/unified/hotfix/20201102.235050 accepted/tizen/6.0/unified/hotfix/20201103.051054 accepted/tizen/unified/20200609.153644 submit/tizen/20200608.091544 submit/tizen_6.0/20201029.205502 submit/tizen_6.0_hotfix/20201102.192902 submit/tizen_6.0_hotfix/20201103.115102 tizen_6.0.m2_release
authorSeonah Moon <seonah1.moon@samsung.com>
Mon, 8 Jun 2020 06:20:12 +0000 (15:20 +0900)
committerSeonah Moon <seonah1.moon@samsung.com>
Mon, 8 Jun 2020 06:23:08 +0000 (15:23 +0900)
Currently for ipv6, we look up the scope assuming we were given an interface ip for
binding.  But with ipv4, we also support giving an interface name.

Check it against an interface name first and return that if valid.

https://github.com/warmcat/libwebsockets/commit/280584ff8f147832f008b299d0e32fd4273024db

Change-Id: I9bd36dd680a90dce88e8b58547d0922a4f311122

lib/core-net/network.c

index e487369..28b778a 100644 (file)
@@ -416,14 +416,40 @@ lws_retry_get_delay_ms(struct lws_context *context,
 
 #if defined(LWS_WITH_IPV6)
 LWS_EXTERN unsigned long
-lws_get_addr_scope(const char *ipaddr)
+lws_get_addr_scope(const char *ifname_or_ipaddr)
 {
-       unsigned long scope = 0;
+       unsigned long scope;
 
-#ifndef WIN32
-       struct ifaddrs *addrs, *addr;
        char ip[NI_MAXHOST];
        unsigned int i;
+#if !defined(WIN32)
+       struct ifaddrs *addrs, *addr;
+#else
+       PIP_ADAPTER_ADDRESSES adapter, addrs = NULL;
+       PIP_ADAPTER_UNICAST_ADDRESS addr;
+       struct sockaddr_in6 *sockaddr;
+       ULONG size = 0;
+       int found = 0;
+       DWORD ret;
+#endif
+
+       /*
+        * First see if we can look the string up as a network interface name...
+        * windows vista+ also has this
+        */
+
+       scope = if_nametoindex(ifname_or_ipaddr);
+       if (scope > 0)
+               /* we found it from the interface name lookup */
+               return scope;
+
+       /*
+        * if not, try to look it up as an IP -> interface -> interface index
+        */
+
+       scope = 0;
+
+#if !defined(WIN32)
 
        getifaddrs(&addrs);
        for (addr = addrs; addr; addr = addr->ifa_next) {
@@ -431,6 +457,7 @@ lws_get_addr_scope(const char *ipaddr)
                        addr->ifa_addr->sa_family != AF_INET6)
                        continue;
 
+               ip[0] = '\0';
                getnameinfo(addr->ifa_addr,
                                sizeof(struct sockaddr_in6),
                                ip, sizeof(ip),
@@ -443,21 +470,13 @@ lws_get_addr_scope(const char *ipaddr)
                                break;
                        }
 
-               if (!strcmp(ip, ipaddr)) {
+               if (!strcmp(ip, ifname_or_ipaddr)) {
                        scope = if_nametoindex(addr->ifa_name);
                        break;
                }
        }
        freeifaddrs(addrs);
 #else
-       PIP_ADAPTER_ADDRESSES adapter, addrs = NULL;
-       PIP_ADAPTER_UNICAST_ADDRESS addr;
-       ULONG size = 0;
-       DWORD ret;
-       struct sockaddr_in6 *sockaddr;
-       char ip[NI_MAXHOST];
-       unsigned int i;
-       int found = 0;
 
        for (i = 0; i < 5; i++)
        {
@@ -496,7 +515,7 @@ lws_get_addr_scope(const char *ipaddr)
                                                        &sockaddr->sin6_addr,
                                                        ip, sizeof(ip));
 
-                                       if (!strcmp(ip, ipaddr)) {
+                                       if (!strcmp(ip, ifname_or_ipaddr)) {
                                                scope = sockaddr->sin6_scope_id;
                                                found = 1;
                                                break;