- memset(hints, 0, sizeof hints);
- hints->ai_family = na->af;
- hints->ai_protocol = protocol;
- hints->ai_flags = passive ? AI_PASSIVE : 0;
- snprintf(service, sizeof service, "%d", na->port);
- rc = getaddrinfo(na->address, service, hints, &res);
- if(rc) {
- error(0, "getaddrinfo %s %d: %s",
- na->address ? na->address : "*",
- na->port, gai_strerror(rc));
- return NULL;
+#if HAVE_SYS_UN_H
+ if (na->af == AF_UNIX) {
+ /* `getaddrinfo' won't work, so we make our own one */
+ raddr = xmalloc(sizeof(*raddr));
+ raddr->len = offsetof(struct sockaddr_un, sun_path) +
+ strlen(na->address) + 1;
+ sun = xmalloc_noptr(raddr->len);
+ sun->sun_family = AF_UNIX;
+ strcpy(sun->sun_path, na->address);
+ raddr->sa = (struct sockaddr *)sun;
+ nraddr = 1;
+ } else
+#endif
+ {
+ /* get the system to do the heavy lifting */
+ memset(hints, 0, sizeof hints);
+ hints->ai_family = na->af;
+ hints->ai_socktype = type;
+ hints->ai_flags = passive ? AI_PASSIVE : 0;
+ byte_snprintf(service, sizeof service, "%d", na->port);
+ rc = getaddrinfo(na->address, service, hints, &res);
+ if(rc) {
+ disorder_error(0, "getaddrinfo %s %d: %s",
+ na->address ? na->address : "*",
+ na->port,
+ format_error(ec_getaddrinfo, rc, errbuf, sizeof errbuf));
+ return -1;
+ }
+ /* copy the addresses into an output vector */
+ for(ai = res, nraddr = 0; ai; ai = ai->ai_next, nraddr++);
+ raddr = xmalloc(nraddr*sizeof(*raddr));
+ for(ai = res, i = 0; ai; ai = ai->ai_next, i++) {
+ raddr[i].sa = xmalloc_noptr(ai->ai_addrlen);
+ raddr[i].len = ai->ai_addrlen;
+ memcpy(raddr[i].sa, ai->ai_addr, ai->ai_addrlen);
+ }
+ freeaddrinfo(res);