- struct addrinfo *local = NULL, *src = NULL, *remote = NULL, *dst = NULL;
- int rc, on = 1, sockfd = -1;
-
- rc = lookup_address(l4type, passive, host, port_number, passive?
- &local : &remote);
- if (rc < 0)
- goto out;
- /* 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,
- sock_type(l4type), 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));
- rc = -ERRNO_TO_PARA_ERROR(rc);
- break;
- }
- flowopt_setopts(sockfd, fo);