]> git.seodisparate.com - UDPConnection/commitdiff
Attempt to make code platform agnostic
authorStephen Seo <seo.disparate@gmail.com>
Thu, 19 Sep 2019 01:35:22 +0000 (10:35 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Thu, 19 Sep 2019 01:35:22 +0000 (10:35 +0900)
Getting scope_id from device name from ipv6 link-local address was
previously using only a unix supported api. Added (untested) code to do
the same for windows machines.

cpp_impl/src/UDPConnection.cpp

index cb55bdf78af48381651a8fe5e5f6f66b96bd1a41..d97152a6a10efc2f93c1d1589135787adb8e2955 100644 (file)
 #include <ios>
 #include <iomanip>
 #include <regex>
+
+#if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS
+#include <netioapi.h>
+#elif UDPC_PLATFORM == UDPC_PLATFORM_MAC || UDPC_PLATFORM == UDPC_PLATFORM_LINUX
 #include <sys/ioctl.h>
 #include <net/if.h>
+#endif
 
 //static const std::regex ipv6_regex = std::regex(R"d((([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])))d");
 static const std::regex ipv6_regex_nolink = std::regex(R"d((([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])))d");
@@ -1605,6 +1610,15 @@ struct in6_addr UDPC_strtoa_link(const char *addrStr, uint32_t *linkId_out) {
             }
         }
     }
+
+    uint32_t scope_id;
+#if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS
+    scope_id = if_nametoindex(linkName);
+    if(scope_id == 0) {
+        checkSetOut(0);
+        return in6addr_loopback;
+    }
+#elif UDPC_PLATFORM == UDPC_PLATFORM_MAC || UDPC_PLATFORM == UDPC_PLATFORM_LINUX
     struct ifreq req{{0}, 0, 0};
     std::strncpy(req.ifr_name, linkName, IFNAMSIZ);
     int socketHandle = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
@@ -1613,8 +1627,10 @@ struct in6_addr UDPC_strtoa_link(const char *addrStr, uint32_t *linkId_out) {
         checkSetOut(0);
         return in6addr_loopback;
     }
+    scope_id = req.ifr_ifindex;
+#endif
 
     CleanupSocket(socketHandle);
-    checkSetOut(req.ifr_ifindex);
+    checkSetOut(scope_id);
     return result;
 }