+ struct addrinfo *local = NULL, *src,
+ *remote = NULL, *dst, hints;
+ unsigned int l3type = AF_UNSPEC;
+ int rc, on = 1, sockfd = -1,
+ socktype = sock_type(l4type);
+ char port[6]; /* port number has at most 5 digits */
+
+ sprintf(port, "%u", port_number);
+ /* Set up address hint structure */
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = l3type;
+ hints.ai_socktype = socktype;
+ /*
+ * getaddrinfo does not support SOCK_DCCP, so for the sake of lookup
+ * (and only then) pretend to be UDP.
+ */
+ if (l4type == IPPROTO_DCCP)
+ hints.ai_socktype = SOCK_DGRAM;
+
+ /* only use addresses available on the host */
+ hints.ai_flags = AI_ADDRCONFIG;
+ if (l3type == AF_INET6)
+ /* use v4-mapped-v6 if no v6 addresses found */
+ hints.ai_flags |= AI_V4MAPPED | AI_ALL;
+
+ if (passive && host == NULL)
+ hints.ai_flags |= AI_PASSIVE;
+
+ /* Obtain local/remote address information */
+ if ((rc = getaddrinfo(host, port, &hints, passive ? &local : &remote))) {
+ PARA_ERROR_LOG("can not resolve %s address %s#%s: %s.\n",
+ layer4_name(l4type),
+ host? host : (passive? "[loopback]" : "[localhost]"),
+ port, gai_strerror(rc));
+ return -E_ADDRESS_LOOKUP;
+ }
+
+ /* Iterate over all src/dst combination, exhausting dst first */
+ for (src = local, dst = remote; src != NULL || dst != NULL; /* no op */ ) {
+ if (src && dst && src->ai_family == AF_INET
+ && dst->ai_family == AF_INET6)
+ goto get_next_dst; /* v4 -> v6 is not possible */
+
+ sockfd = socket(src ? src->ai_family : dst->ai_family,
+ socktype, l4type);
+ if (sockfd < 0)
+ goto get_next_dst;
+
+ /*
+ * Reuse the address on passive sockets to avoid failure on
+ * restart (protocols using listen()) and when creating
+ * multiple listener instances (UDP multicast).
+ */
+ if (passive && setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
+ &on, sizeof(on)) == -1) {
+ rc = errno;
+ close(sockfd);
+ PARA_ERROR_LOG("can not set SO_REUSEADDR: %s\n",
+ strerror(rc));
+ return -ERRNO_TO_PARA_ERROR(rc);
+ }
+ flowopt_setopts(sockfd, fo);
+
+ if (src) {
+ if (bind(sockfd, src->ai_addr, src->ai_addrlen) < 0) {
+ close(sockfd);
+ goto get_next_src;
+ }
+ if (!dst) /* bind-only completed successfully */
+ break;
+ }
+
+ if (dst && connect(sockfd, dst->ai_addr, dst->ai_addrlen) == 0)
+ break; /* connection completed successfully */
+ close(sockfd);
+get_next_dst:
+ if (dst && (dst = dst->ai_next))
+ continue;
+get_next_src:
+ if (src && (src = src->ai_next)) /* restart inner loop */
+ dst = remote;
+ }
+ if (local)
+ freeaddrinfo(local);
+ if (remote)
+ freeaddrinfo(remote);
+ flowopt_cleanup(fo);
+
+ if (src == NULL && dst == NULL) {
+ rc = errno;
+ PARA_ERROR_LOG("can not create %s socket %s#%s.\n",
+ layer4_name(l4type), host? host : (passive?
+ "[loopback]" : "[localhost]"), port);
+ return -ERRNO_TO_PARA_ERROR(rc);
+ }
+ return sockfd;
+}
+
+/**
+ * Create a passive / listening socket.
+ *
+ * \param l4type The transport-layer type (\p IPPROTO_xxx).
+ * \param port The decimal port number to listen on.
+ * \param fo Flowopts (if any) to set before starting to listen.
+ *
+ * \return Positive integer (socket descriptor) on success, negative value
+ * otherwise.
+ *
+ * \sa makesock(), ip(7), ipv6(7), bind(2), listen(2).
+ */
+int para_listen(unsigned l4type, uint16_t port, struct flowopts *fo)
+{
+ int ret, fd = makesock(l4type, 1, NULL, port, fo);
+
+ if (fd > 0) {
+ ret = listen(fd, BACKLOG);
+ if (ret < 0) {
+ ret = errno;
+ close(fd);
+ return -ERRNO_TO_PARA_ERROR(ret);
+ }
+ PARA_INFO_LOG("listening on %s port %u, fd %d\n",
+ layer4_name(l4type), port, fd);
+ }
+ return fd;
+}
+
+/**
+ * Determine IPv4/v6 socket address length.
+ * \param sa Container of IPv4 or IPv6 address.
+ * \return Address-family dependent address length.
+ */
+static socklen_t salen(const struct sockaddr *sa)
+{
+ assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
+
+ return sa->sa_family == AF_INET6
+ ? sizeof(struct sockaddr_in6)
+ : sizeof(struct sockaddr_in);
+}
+
+/**
+ * Process IPv4/v6 address, turn v6-mapped-v4 address into normal IPv4 address.
+ * \param ss Container of IPv4/6 address.
+ * \return Pointer to normalized address (may be static storage).
+ *
+ * \sa RFC 3493
+ */
+static const struct sockaddr *
+normalize_ip_address(const struct sockaddr_storage *ss)
+{
+ const struct sockaddr_in6 *ia6 = (const struct sockaddr_in6 *)ss;
+
+ assert(ss->ss_family == AF_INET || ss->ss_family == AF_INET6);
+
+ if (ss->ss_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&ia6->sin6_addr)) {
+ static struct sockaddr_in ia;
+
+ ia.sin_family = AF_INET;
+ ia.sin_port = ia6->sin6_port;
+ memcpy(&ia.sin_addr.s_addr, &(ia6->sin6_addr.s6_addr[12]), 4);
+ return (const struct sockaddr *)&ia;
+ }
+ return (const struct sockaddr *)ss;
+}
+
+/**
+ * Print numeric host and port number (beware - uses static char).
+ *
+ * \param sa The IPv4/IPv6 socket address to use.
+ *
+ * \sa getnameinfo(3), services(5), nsswitch.conf(5)
+ */
+static char *host_and_port(const struct sockaddr_storage *ss)
+{
+ const struct sockaddr *sa = normalize_ip_address(ss);
+ char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
+ static char output[sizeof(hbuf) + sizeof(sbuf) + 2];