sctp_lib: explicitly test for ipv6 support

Change-Id: I662138e6c9ea71cad751e2a057a642b85e6aabc7
Signed-off-by: Austin Schuh <austin.schuh@bluerivertech.com>
diff --git a/aos/network/sctp_lib.cc b/aos/network/sctp_lib.cc
index 7822e8e..99ead37 100644
--- a/aos/network/sctp_lib.cc
+++ b/aos/network/sctp_lib.cc
@@ -13,7 +13,7 @@
 
 #include "aos/util/file.h"
 
-DEFINE_string(interface, "", "ipv6 interface");
+DEFINE_string(interface, "", "network interface");
 DEFINE_bool(disable_ipv6, false, "disable ipv6");
 
 namespace aos {
@@ -31,19 +31,40 @@
 
 }  // namespace
 
-struct sockaddr_storage ResolveSocket(std::string_view host, int port) {
+bool Ipv6Enabled() {
+  if (FLAGS_disable_ipv6) {
+    return false;
+  }
+  int fd = socket(AF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP);
+  if (fd != -1) {
+    close(fd);
+    return true;
+  }
+  switch (errno) {
+    case EAFNOSUPPORT:
+    case EINVAL:
+    case EPROTONOSUPPORT:
+      PLOG(INFO) << "no ipv6";
+      return false;
+    default:
+      PLOG(FATAL) << "Open socket failed";
+      return false;
+  };
+}
+
+struct sockaddr_storage ResolveSocket(std::string_view host, int port,
+                                      bool use_ipv6) {
   struct sockaddr_storage result;
   struct addrinfo *addrinfo_result;
   struct sockaddr_in *t_addr = (struct sockaddr_in *)&result;
   struct sockaddr_in6 *t_addr6 = (struct sockaddr_in6 *)&result;
   struct addrinfo hints;
   memset(&hints, 0, sizeof(hints));
-  if (FLAGS_disable_ipv6) {
+  if (!use_ipv6) {
     hints.ai_family = AF_INET;
   } else {
-    // IPv6 can handle IPv4 through IPv4-mapped IPv6 addresses
-    // but IPv4 can't handle IPv6 connections.
-    // The default, if unspecified, is to use IPv4.
+    // Default to IPv6 as the clearly superior protocol, since it also handles
+    // IPv4.
     hints.ai_family = AF_INET6;
   }
   hints.ai_socktype = SOCK_SEQPACKET;
@@ -55,11 +76,6 @@
   hints.ai_flags = AI_PASSIVE | AI_V4MAPPED | AI_NUMERICSERV;
   int ret = getaddrinfo(host.empty() ? nullptr : std::string(host).c_str(),
                         std::to_string(port).c_str(), &hints, &addrinfo_result);
-  if (ret) {
-    hints.ai_family = AF_INET;
-    ret = getaddrinfo(host.empty() ? nullptr : std::string(host).c_str(),
-                      std::to_string(port).c_str(), &hints, &addrinfo_result);
-  }
   if (ret == EAI_SYSTEM) {
     PLOG(FATAL) << "getaddrinfo failed to look up '" << host << "'";
   } else if (ret != 0) {