+sockaddr_set_data (struct sockaddr *sa, const ip_address *ip, int port)
+{
+ switch (ip->family)
+ {
+ case AF_INET:
+ {
+ struct sockaddr_in *sin = (struct sockaddr_in *)sa;
+ xzero (*sin);
+ sin->sin_family = AF_INET;
+ sin->sin_port = htons (port);
+ sin->sin_addr = ip->data.d4;
+ break;
+ }
+#ifdef ENABLE_IPV6
+ case AF_INET6:
+ {
+ struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
+ xzero (*sin6);
+ sin6->sin6_family = AF_INET6;
+ sin6->sin6_port = htons (port);
+ sin6->sin6_addr = ip->data.d6;
+#ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
+ sin6->sin6_scope_id = ip->ipv6_scope;
+#endif
+ break;
+ }
+#endif /* ENABLE_IPV6 */
+ default:
+ abort ();
+ }
+}
+
+/* Get the data of SA, specifically the IP address and the port. If
+ you're not interested in one or the other information, pass NULL as
+ the pointer. */
+
+static void
+sockaddr_get_data (const struct sockaddr *sa, ip_address *ip, int *port)
+{
+ switch (sa->sa_family)
+ {
+ case AF_INET:
+ {
+ struct sockaddr_in *sin = (struct sockaddr_in *)sa;
+ if (ip)
+ {
+ ip->family = AF_INET;
+ ip->data.d4 = sin->sin_addr;
+ }
+ if (port)
+ *port = ntohs (sin->sin_port);
+ break;
+ }
+#ifdef ENABLE_IPV6
+ case AF_INET6:
+ {
+ struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
+ if (ip)
+ {
+ ip->family = AF_INET6;
+ ip->data.d6 = sin6->sin6_addr;
+#ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
+ ip->ipv6_scope = sin6->sin6_scope_id;
+#endif
+ }
+ if (port)
+ *port = ntohs (sin6->sin6_port);
+ break;
+ }
+#endif
+ default:
+ abort ();
+ }
+}
+
+/* Return the size of the sockaddr structure depending on its
+ family. */
+
+static socklen_t
+sockaddr_size (const struct sockaddr *sa)
+{
+ switch (sa->sa_family)
+ {
+ case AF_INET:
+ return sizeof (struct sockaddr_in);
+#ifdef ENABLE_IPV6
+ case AF_INET6:
+ return sizeof (struct sockaddr_in6);
+#endif
+ default:
+ abort ();
+ }
+}
+\f
+/* Resolve the bind address specified via --bind-address and store it
+ to SA. The resolved value is stored in a static variable and
+ reused after the first invocation of this function.
+
+ Returns true on success, false on failure. */
+
+static bool
+resolve_bind_address (struct sockaddr *sa)