(addr)->superfamily == UNIX ? AF_UNIX : \
(step).ai ? (step).ai->ai_family : AF_INET)
#else
+/* Here we gratuitously reference 'step' to avoid gcc warnings about
+ * 'set but not used' when compiling -DNO_IPV6 */
#define SOCKADDR_FAMILY(addr, step) \
((addr)->superfamily == UNRESOLVED ? AF_UNSPEC : \
- (addr)->superfamily == UNIX ? AF_UNIX : AF_INET)
+ (addr)->superfamily == UNIX ? AF_UNIX : \
+ (step).curraddr ? AF_INET : AF_INET)
#endif
/*
hints.ai_addr = NULL;
hints.ai_canonname = NULL;
hints.ai_next = NULL;
- err = getaddrinfo(host, NULL, &hints, &ret->ais);
+ {
+ char *trimmed_host = host_strduptrim(host); /* strip [] on literals */
+ err = getaddrinfo(trimmed_host, NULL, &hints, &ret->ais);
+ sfree(trimmed_host);
+ }
if (err != 0) {
ret->error = gai_strerror(err);
return ret;
}
}
+int sk_addr_needs_port(SockAddr addr)
+{
+ if (addr->superfamily == UNRESOLVED || addr->superfamily == UNIX) {
+ return FALSE;
+ } else {
+ return TRUE;
+ }
+}
+
int sk_hostname_is_local(const char *name)
{
return !strcmp(name, "localhost") ||
{
int s;
#ifndef NO_IPV6
- struct addrinfo hints, *ai;
+ struct addrinfo hints, *ai = NULL;
char portstr[6];
#endif
union sockaddr_union u;
hints.ai_next = NULL;
assert(port >= 0 && port <= 99999);
sprintf(portstr, "%d", port);
- retcode = getaddrinfo(srcaddr, portstr, &hints, &ai);
+ {
+ char *trimmed_addr = host_strduptrim(srcaddr);
+ retcode = getaddrinfo(trimmed_addr, portstr, &hints, &ai);
+ sfree(trimmed_addr);
+ }
if (retcode == 0) {
addr = (union sockaddr_union *)ai->ai_addr;
addrlen = ai->ai_addrlen;
}
retcode = bind(s, &addr->sa, addrlen);
+
+#ifndef NO_IPV6
+ if (ai)
+ freeaddrinfo(ai);
+#endif
+
if (retcode < 0) {
close(s);
ret->error = strerror(errno);