+ char port_buff [16];
+ struct addrinfo hints;
+ struct addrinfo* addrinf;
+ int err;
+
+ dpsnprintf (port_buff, sizeof (port_buff), "%d", port);
+ port_buff[sizeof (port_buff) - 1] = '\0';
+
+ memset(&hints, 0, sizeof (hints));
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_DGRAM;
+ //hints.ai_flags = AI_PASSIVE;
+
+ err = getaddrinfo(name, port_buff, &hints, &addrinf);
+ if (err != 0 || addrinf == NULL)
+ return 0;
+ if (addrinf->ai_addr->sa_family != AF_INET6 && addrinf->ai_addr->sa_family != AF_INET)
+ {
+ freeaddrinfo (addrinf);
+ return 0;
+ }
+
+ // great it worked
+ if (addrinf->ai_addr->sa_family == AF_INET6)
+ {
+ address->addresstype = LHNETADDRESSTYPE_INET6;
+ memcpy(&address->addr.in6, addrinf->ai_addr, sizeof(address->addr.in6));
+ }
+ else
+ {
+ address->addresstype = LHNETADDRESSTYPE_INET4;
+ memcpy(&address->addr.in, addrinf->ai_addr, sizeof(address->addr.in));
+ }
+ address->port = port;
+
+ freeaddrinfo (addrinf);
+ return 1;
+}
+
+int LHNETADDRESS_FromString(lhnetaddress_t *vaddress, const char *string, int defaultport)
+{
+ lhnetaddressnative_t *address = (lhnetaddressnative_t *)vaddress;
+ int i, port, d1, d2, d3, d4, resolved;
+ size_t namelen;
+ unsigned char *a;
+ char name[128];
+#ifdef STANDALONETEST
+ char string2[128];
+#endif
+ const char* addr_start;
+ const char* addr_end = NULL;
+ const char* port_name = NULL;
+ int addr_family = AF_UNSPEC;
+
+ if (!address || !string || !*string)
+ return 0;
+ memset(address, 0, sizeof(*address));
+ address->addresstype = LHNETADDRESSTYPE_NONE;
+ port = 0;
+
+ // If it's a bracketed IPv6 address
+ if (string[0] == '[')
+ {
+ const char* end_bracket = strchr(string, ']');
+
+ if (end_bracket == NULL)
+ return 0;
+
+ if (end_bracket[1] == ':')
+ port_name = end_bracket + 2;
+ else if (end_bracket[1] != '\0')
+ return 0;
+
+ addr_family = AF_INET6;
+ addr_start = &string[1];
+ addr_end = end_bracket;
+ }
+ else
+ {
+ const char* first_colon;
+
+ addr_start = string;
+
+ // If it's a numeric non-bracket IPv6 address (-> no port),
+ // or it's a numeric IPv4 address, or a name, with a port
+ first_colon = strchr(string, ':');
+ if (first_colon != NULL)
+ {
+ const char* last_colon = strrchr(first_colon + 1, ':');
+
+ // If it's an numeric IPv4 address, or a name, with a port
+ if (last_colon == NULL)
+ {
+ addr_end = first_colon;
+ port_name = first_colon + 1;
+ }
+ else
+ addr_family = AF_INET6;
+ }
+ }
+
+ if (addr_end != NULL)
+ namelen = addr_end - addr_start;
+ else
+ namelen = strlen (addr_start);
+
+ if (namelen >= sizeof(name))
+ namelen = sizeof(name) - 1;
+ memcpy (name, addr_start, namelen);
+ name[namelen] = 0;
+
+ if (port_name)
+ port = atoi(port_name);
+
+ if (port == 0)
+ port = defaultport;
+
+ // handle loopback
+ if (!strcmp(name, "local"))
+ {
+ address->addresstype = LHNETADDRESSTYPE_LOOP;
+ address->port = port;
+ return 1;
+ }
+ // try to parse as dotted decimal ipv4 address first
+ // note this supports partial ip addresses
+ d1 = d2 = d3 = d4 = 0;
+#if _MSC_VER >= 1400
+#define sscanf sscanf_s
+#endif
+ if (addr_family != AF_INET6 &&
+ sscanf(name, "%d.%d.%d.%d", &d1, &d2, &d3, &d4) >= 1 && (unsigned int)d1 < 256 && (unsigned int)d2 < 256 && (unsigned int)d3 < 256 && (unsigned int)d4 < 256)
+ {
+ // parsed a valid ipv4 address
+ address->addresstype = LHNETADDRESSTYPE_INET4;
+ address->port = port;
+ address->addr.in.sin_family = AF_INET;
+ address->addr.in.sin_port = htons((unsigned short)port);
+ a = (unsigned char *)&address->addr.in.sin_addr;
+ a[0] = d1;
+ a[1] = d2;
+ a[2] = d3;
+ a[3] = d4;
+#ifdef STANDALONETEST
+ LHNETADDRESS_ToString(address, string2, sizeof(string2), 1);
+ printf("manual parsing of ipv4 dotted decimal address \"%s\" successful: %s\n", string, string2);
+#endif
+ return 1;
+ }
+ for (i = 0;i < MAX_NAMECACHE;i++)
+ if (!strcmp(namecache[i].name, name))
+ break;
+#ifdef STANDALONETEST
+ if (i < MAX_NAMECACHE)
+#else
+ if (i < MAX_NAMECACHE && realtime < namecache[i].expirationtime)
+#endif
+ {
+ *address = namecache[i].address;
+ address->port = port;
+ if (address->addresstype == LHNETADDRESSTYPE_INET6)
+ {
+ address->addr.in6.sin6_port = htons((unsigned short)port);
+ return 1;
+ }
+ else if (address->addresstype == LHNETADDRESSTYPE_INET4)
+ {
+ address->addr.in.sin_port = htons((unsigned short)port);
+ return 1;
+ }
+ return 0;
+ }
+
+ for (i = 0;i < (int)sizeof(namecache[namecacheposition].name)-1 && name[i];i++)
+ namecache[namecacheposition].name[i] = name[i];
+ namecache[namecacheposition].name[i] = 0;
+#ifndef STANDALONETEST
+ namecache[namecacheposition].expirationtime = realtime + 12 * 3600; // 12 hours
+#endif
+
+ // try resolving the address (handles dns and other ip formats)
+ resolved = LHNETADDRESS_Resolve(address, name, port);
+ if (resolved)
+ {
+#ifdef STANDALONETEST
+ const char *protoname;
+
+ switch (address->addresstype)
+ {
+ case LHNETADDRESSTYPE_INET6:
+ protoname = "ipv6";
+ break;
+ case LHNETADDRESSTYPE_INET4:
+ protoname = "ipv4";
+ break;
+ default:
+ protoname = "UNKNOWN";
+ break;
+ }
+ LHNETADDRESS_ToString(vaddress, string2, sizeof(string2), 1);
+ Con_Printf("LHNETADDRESS_Resolve(\"%s\") returned %s address %s\n", string, protoname, string2);
+#endif
+ namecache[namecacheposition].address = *address;
+ }
+ else
+ {
+#ifdef STANDALONETEST
+ printf("name resolution failed on address \"%s\"\n", name);
+#endif
+ namecache[namecacheposition].address.addresstype = LHNETADDRESSTYPE_NONE;
+ }
+
+ namecacheposition = (namecacheposition + 1) % MAX_NAMECACHE;
+ return resolved;
+}
+#else
+int LHNETADDRESS_FromString(lhnetaddress_t *vaddress, const char *string, int defaultport)
+{
+ lhnetaddressnative_t *address = (lhnetaddressnative_t *)vaddress;
+ int i, port, namelen, d1, d2, d3, d4;