openbmc/build/tmp/deploy/sdk/witherspoon-2019-08-08/sysroots/armv6-openbmc-linux-gnueabi/usr/src/debug/dbus/1.12.16-r0/dbus-1.12.16/dbus/dbus-sysdeps-unix.c
int _dbus_listen_tcp_socket(const char* host, const char* port,
                            const char* family, DBusString* retport,
                            DBusSocket** fds_p, DBusError* error)
{
    int saved_errno;
    int nlisten_fd = 0, res, i;
    DBusSocket* listen_fd = NULL;
    struct addrinfo hints;
    struct addrinfo *ai, *tmp;
    unsigned int reuseaddr;

    *fds_p = NULL;
    _DBUS_ASSERT_ERROR_IS_CLEAR(error);

    _DBUS_ZERO(hints);

    if (!family)
        hints.ai_family = AF_UNSPEC;
    else if (!strcmp(family, "ipv4"))
        hints.ai_family = AF_INET;
    else if (!strcmp(family, "ipv6"))
        hints.ai_family = AF_INET6;
    else
    {
        dbus_set_error(error, DBUS_ERROR_BAD_ADDRESS,
                       "Unknown address family %s", family);
        return -1;
    }

    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;

redo_lookup_with_port:
    ai = NULL;
    if ((res = getaddrinfo(host, port, &hints, &ai)) != 0 || !ai)
    {
        dbus_set_error(error, _dbus_error_from_gai(res, errno),
                       "Failed to lookup host/port: \"%s:%s\": %s (%d)",
                       host ? host : "*", port, gai_strerror(res), res);
        goto failed;
    }

    tmp = ai;
    while (tmp)
    {
        int fd = -1, tcp_nodelay_on;
        DBusSocket* newlisten_fd;

        if (!_dbus_open_socket(&fd, tmp->ai_family, SOCK_STREAM, 0, error))
        {
            _DBUS_ASSERT_ERROR_IS_SET(error);
            goto failed;
        }
        _DBUS_ASSERT_ERROR_IS_CLEAR(error);

        reuseaddr = 1;
        if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr,
                       sizeof(reuseaddr)) == -1)
        {
            _dbus_warn("Failed to set socket option \"%s:%s\": %s",
                       host ? host : "*", port, _dbus_strerror(errno));
        }

        /* Nagle's algorithm imposes a huge delay on the initial messages
           going over TCP. */
        tcp_nodelay_on = 1;
        if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &tcp_nodelay_on,
                       sizeof(tcp_nodelay_on)) == -1)
        {
            _dbus_warn("Failed to set TCP_NODELAY socket option \"%s:%s\": %s",
                       host ? host : "*", port, _dbus_strerror(errno));
        }

        if (bind(fd, (struct sockaddr*)tmp->ai_addr, tmp->ai_addrlen) < 0)
        {
            saved_errno = errno;
            _dbus_close(fd, NULL);
            if (saved_errno == EADDRINUSE)
            {
                /* Depending on kernel policy, binding to an IPv6 address
                   might implicitly bind to a corresponding IPv4
                   address or vice versa, resulting in EADDRINUSE for the
                   other one (e.g. bindv6only=0 on Linux).

                   Also, after we "goto redo_lookup_with_port" after binding
                   a port on one of the possible addresses, we will
                   try to bind that same port on every address, including the
                   same address again for a second time; that one will
                   also fail with EADDRINUSE.

                   For both those reasons, ignore EADDRINUSE here */
                tmp = tmp->ai_next;
                continue;
            }
            dbus_set_error(error, _dbus_error_from_errno(saved_errno),
                           "Failed to bind socket \"%s:%s\": %s",
                           host ? host : "*", port,
                           _dbus_strerror(saved_errno));
            goto failed;
        }

        if (listen(fd, 30 /* backlog */) < 0)
        {
            saved_errno = errno;
            _dbus_close(fd, NULL);
            dbus_set_error(error, _dbus_error_from_errno(saved_errno),
                           "Failed to listen on socket \"%s:%s\": %s",
                           host ? host : "*", port,
                           _dbus_strerror(saved_errno));
            goto failed;
        }

        newlisten_fd =
            dbus_realloc(listen_fd, sizeof(DBusSocket) * (nlisten_fd + 1));
        if (!newlisten_fd)
        {
            _dbus_close(fd, NULL);
            dbus_set_error(error, DBUS_ERROR_NO_MEMORY,
                           "Failed to allocate file handle array");
            goto failed;
        }
        listen_fd = newlisten_fd;
        listen_fd[nlisten_fd].fd = fd;
        nlisten_fd++;

        if (!_dbus_string_get_length(retport))
        {
            /* If the user didn't specify a port, or used 0, then
               the kernel chooses a port. After the first address
               is bound to, we need to force all remaining addresses
               to use the same port */
            if (!port || !strcmp(port, "0"))
            {
                int result;
                struct sockaddr_storage addr;
                socklen_t addrlen;
                char portbuf[50];

                addrlen = sizeof(addr);
                result = getsockname(fd, (struct sockaddr*)&addr, &addrlen);

                if (result == -1)
                {
                    saved_errno = errno;
                    dbus_set_error(
                        error, _dbus_error_from_errno(saved_errno),
                        "Failed to retrieve socket name for \"%s:%s\": %s",
                        host ? host : "*", port, _dbus_strerror(saved_errno));
                    goto failed;
                }

                if ((res = getnameinfo((struct sockaddr*)&addr, addrlen, NULL,
                                       0, portbuf, sizeof(portbuf),
                                       NI_NUMERICHOST | NI_NUMERICSERV)) != 0)
                {
                    saved_errno = errno;
                    dbus_set_error(
                        error, _dbus_error_from_gai(res, saved_errno),
                        "Failed to resolve port \"%s:%s\": %s (%d)",
                        host ? host : "*", port, gai_strerror(res), res);
                    goto failed;
                }

                if (!_dbus_string_append(retport, portbuf))
                {
                    dbus_set_error(error, DBUS_ERROR_NO_MEMORY, NULL);
                    goto failed;
                }

                /* Release current address list & redo lookup */
                port = _dbus_string_get_const_data(retport);
                freeaddrinfo(ai);
                goto redo_lookup_with_port;
            }
            else
            {
                if (!_dbus_string_append(retport, port))
                {
                    dbus_set_error(error, DBUS_ERROR_NO_MEMORY, NULL);
                    goto failed;
                }
            }
        }

        tmp = tmp->ai_next;
    }
    freeaddrinfo(ai);
    ai = NULL;

    if (!nlisten_fd)
    {
        errno = EADDRINUSE;
        dbus_set_error(error, _dbus_error_from_errno(errno),
                       "Failed to bind socket \"%s:%s\": %s", host ? host : "*",
                       port, _dbus_strerror(errno));
        goto failed;
    }

    for (i = 0; i < nlisten_fd; i++)
    {
        if (!_dbus_set_fd_nonblocking(listen_fd[i].fd, error))
        {
            goto failed;
        }
    }

    *fds_p = listen_fd;

    return nlisten_fd;

failed:
    if (ai)
        freeaddrinfo(ai);
    for (i = 0; i < nlisten_fd; i++)
        _dbus_close(listen_fd[i].fd, NULL);
    dbus_free(listen_fd);
    return -1;
}
/**
 * Creates a socket and binds it to the given path, then listens on
 * the socket. The socket is set to be nonblocking.  In case of port=0
 * a random free port is used and returned in the port parameter.
 * If inaddr_any is specified, the hostname is ignored.
 *
 * This will set FD_CLOEXEC for the socket returned
 *
 * @param host the host name to listen on
 * @param port the port to listen on, if zero a free port will be used
 * @param family the address family to listen on, NULL for all
 * @param retport string to return the actual port listened on
 * @param fds_p location to store returned file descriptors
 * @param error return location for errors
 * @returns the number of listening file descriptors or -1 on error
 */
Creates a socket and binds it to the given path, then listens on
the socket. The socket is set to be nonblocking.  In case of port=0
a random free port is used and returned in the port parameter.
If inaddr_any is specified, the hostname is ignored.

This will set FD_CLOEXEC for the socket returned


Creates a socket and binds it to the given path then listens on the socket . The socket is set to be nonblocking . In case of port = 0 a random free port is used and returned in the port parameter . If inaddr_any is specified the hostname is ignored . This will set FD_CLOEXEC for the socket returned
int _dbus_listen_tcp_socket(const char* host, const char* port,
                            const char* family, DBusString* retport,
                            DBusSocket** fds_p, DBusError* error)
{
    int saved_errno;
    int nlisten_fd = 0, res, i;
    DBusSocket* listen_fd = NULL;
    struct addrinfo hints;
    struct addrinfo *ai, *tmp;
    unsigned int reuseaddr;
    *fds_p = NULL;
    _DBUS_ASSERT_ERROR_IS_CLEAR(error);
    _DBUS_ZERO(hints);
    if (!family)
        hints.ai_family = AF_UNSPEC;
    else if (!strcmp(family, "ipv4"))
        hints.ai_family = AF_INET;
    else if (!strcmp(family, "ipv6"))
        hints.ai_family = AF_INET6;
    else
    {
        dbus_set_error(error, DBUS_ERROR_BAD_ADDRESS,
                       "Unknown address family %s", family);
        return -1;
    }
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
redo_lookup_with_port:
    ai = NULL;
    if ((res = getaddrinfo(host, port, &hints, &ai)) != 0 || !ai)
    {
        dbus_set_error(error, _dbus_error_from_gai(res, errno),
                       "Failed to lookup host/port: \"%s:%s\": %s (%d)",
                       host ? host : "*", port, gai_strerror(res), res);
        goto failed;
    }
    tmp = ai;
    while (tmp)
    {
        int fd = -1, tcp_nodelay_on;
        DBusSocket* newlisten_fd;
        if (!_dbus_open_socket(&fd, tmp->ai_family, SOCK_STREAM, 0, error))
        {
            _DBUS_ASSERT_ERROR_IS_SET(error);
            goto failed;
        }
        _DBUS_ASSERT_ERROR_IS_CLEAR(error);
        reuseaddr = 1;
        if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr,
                       sizeof(reuseaddr)) == -1)
        {
            _dbus_warn("Failed to set socket option \"%s:%s\": %s",
                       host ? host : "*", port, _dbus_strerror(errno));
        }
        tcp_nodelay_on = 1;
        if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &tcp_nodelay_on,
                       sizeof(tcp_nodelay_on)) == -1)
        {
            _dbus_warn("Failed to set TCP_NODELAY socket option \"%s:%s\": %s",
                       host ? host : "*", port, _dbus_strerror(errno));
        }
        if (bind(fd, (struct sockaddr*)tmp->ai_addr, tmp->ai_addrlen) < 0)
        {
            saved_errno = errno;
            _dbus_close(fd, NULL);
            if (saved_errno == EADDRINUSE)
            {
                tmp = tmp->ai_next;
                continue;
            }
            dbus_set_error(error, _dbus_error_from_errno(saved_errno),
                           "Failed to bind socket \"%s:%s\": %s",
                           host ? host : "*", port,
                           _dbus_strerror(saved_errno));
            goto failed;
        }
        if (listen(fd, 30 ) < 0)
        {
            saved_errno = errno;
            _dbus_close(fd, NULL);
            dbus_set_error(error, _dbus_error_from_errno(saved_errno),
                           "Failed to listen on socket \"%s:%s\": %s",
                           host ? host : "*", port,
                           _dbus_strerror(saved_errno));
            goto failed;
        }
        newlisten_fd =
            dbus_realloc(listen_fd, sizeof(DBusSocket) * (nlisten_fd + 1));
        if (!newlisten_fd)
        {
            _dbus_close(fd, NULL);
            dbus_set_error(error, DBUS_ERROR_NO_MEMORY,
                           "Failed to allocate file handle array");
            goto failed;
        }
        listen_fd = newlisten_fd;
        listen_fd[nlisten_fd].fd = fd;
        nlisten_fd++;
        if (!_dbus_string_get_length(retport))
        {
            if (!port || !strcmp(port, "0"))
            {
                int result;
                struct sockaddr_storage addr;
                socklen_t addrlen;
                char portbuf[50];
                addrlen = sizeof(addr);
                result = getsockname(fd, (struct sockaddr*)&addr, &addrlen);
                if (result == -1)
                {
                    saved_errno = errno;
                    dbus_set_error(
                        error, _dbus_error_from_errno(saved_errno),
                        "Failed to retrieve socket name for \"%s:%s\": %s",
                        host ? host : "*", port, _dbus_strerror(saved_errno));
                    goto failed;
                }
                if ((res = getnameinfo((struct sockaddr*)&addr, addrlen, NULL,
                                       0, portbuf, sizeof(portbuf),
                                       NI_NUMERICHOST | NI_NUMERICSERV)) != 0)
                {
                    saved_errno = errno;
                    dbus_set_error(
                        error, _dbus_error_from_gai(res, saved_errno),
                        "Failed to resolve port \"%s:%s\": %s (%d)",
                        host ? host : "*", port, gai_strerror(res), res);
                    goto failed;
                }
                if (!_dbus_string_append(retport, portbuf))
                {
                    dbus_set_error(error, DBUS_ERROR_NO_MEMORY, NULL);
                    goto failed;
                }
                port = _dbus_string_get_const_data(retport);
                freeaddrinfo(ai);
                goto redo_lookup_with_port;
            }
            else
            {
                if (!_dbus_string_append(retport, port))
                {
                    dbus_set_error(error, DBUS_ERROR_NO_MEMORY, NULL);
                    goto failed;
                }
            }
        }
        tmp = tmp->ai_next;
    }
    freeaddrinfo(ai);
    ai = NULL;
    if (!nlisten_fd)
    {
        errno = EADDRINUSE;
        dbus_set_error(error, _dbus_error_from_errno(errno),
                       "Failed to bind socket \"%s:%s\": %s", host ? host : "*",
                       port, _dbus_strerror(errno));
        goto failed;
    }
    for (i = 0; i < nlisten_fd; i++)
    {
        if (!_dbus_set_fd_nonblocking(listen_fd[i].fd, error))
        {
            goto failed;
        }
    }
    *fds_p = listen_fd;
    return nlisten_fd;
failed:
    if (ai)
        freeaddrinfo(ai);
    for (i = 0; i < nlisten_fd; i++)
        _dbus_close(listen_fd[i].fd, NULL);
    dbus_free(listen_fd);
    return -1;
}
int _dbus_listen_tcp_socket ( const char * host , const char * port , const char * family , DBusString * retport , DBusSocket * * fds_p , DBusError * error ) { int saved_errno ; int nlisten_fd = 0 , res , i ; DBusSocket * listen_fd = NULL ; struct addrinfo hints ; struct addrinfo * ai , * tmp ; unsigned int reuseaddr ; * fds_p = NULL ; _DBUS_ASSERT_ERROR_IS_CLEAR ( error ) ; _DBUS_ZERO ( hints ) ; if ( ! family ) hints . ai_family = AF_UNSPEC ; else if ( ! strcmp ( family , " " ) ) hints . ai_family = AF_INET ; else if ( ! strcmp ( family , " " ) ) hints . ai_family = AF_INET6 ; else { dbus_set_error ( error , DBUS_ERROR_BAD_ADDRESS , " " , family ) ; return -1 ; } hints . ai_protocol = IPPROTO_TCP ; hints . ai_socktype = SOCK_STREAM ; hints . ai_flags = AI_ADDRCONFIG | AI_PASSIVE ; redo_lookup_with_port : ai = NULL ; if ( ( res = getaddrinfo ( host , port , & hints , & ai ) ) != 0 || ! ai ) { dbus_set_error ( error , _dbus_error_from_gai ( res , errno ) , " \" \" " , host ? host : " " , port , gai_strerror ( res ) , res ) ; goto failed ; } tmp = ai ; while ( tmp ) { int fd = -1 , tcp_nodelay_on ; DBusSocket * newlisten_fd ; if ( ! _dbus_open_socket ( & fd , tmp -> ai_family , SOCK_STREAM , 0 , error ) ) { _DBUS_ASSERT_ERROR_IS_SET ( error ) ; goto failed ; } _DBUS_ASSERT_ERROR_IS_CLEAR ( error ) ; reuseaddr = 1 ; if ( setsockopt ( fd , SOL_SOCKET , SO_REUSEADDR , & reuseaddr , sizeof ( reuseaddr ) ) == -1 ) { _dbus_warn ( " \" \" " , host ? host : " " , port , _dbus_strerror ( errno ) ) ; } tcp_nodelay_on = 1 ; if ( setsockopt ( fd , IPPROTO_TCP , TCP_NODELAY , & tcp_nodelay_on , sizeof ( tcp_nodelay_on ) ) == -1 ) { _dbus_warn ( " \" \" " , host ? host : " " , port , _dbus_strerror ( errno ) ) ; } if ( bind ( fd , ( struct sockaddr * ) tmp -> ai_addr , tmp -> ai_addrlen ) < 0 ) { saved_errno = errno ; _dbus_close ( fd , NULL ) ; if ( saved_errno == EADDRINUSE ) { tmp = tmp -> ai_next ; continue ; } dbus_set_error ( error , _dbus_error_from_errno ( saved_errno ) , " \" \" " , host ? host : " " , port , _dbus_strerror ( saved_errno ) ) ; goto failed ; } if ( listen ( fd , 30 ) < 0 ) { saved_errno = errno ; _dbus_close ( fd , NULL ) ; dbus_set_error ( error , _dbus_error_from_errno ( saved_errno ) , " \" \" " , host ? host : " " , port , _dbus_strerror ( saved_errno ) ) ; goto failed ; } newlisten_fd = dbus_realloc ( listen_fd , sizeof ( DBusSocket ) * ( nlisten_fd + 1 ) ) ; if ( ! newlisten_fd ) { _dbus_close ( fd , NULL ) ; dbus_set_error ( error , DBUS_ERROR_NO_MEMORY , " " ) ; goto failed ; } listen_fd = newlisten_fd ; listen_fd [ nlisten_fd ] . fd = fd ; nlisten_fd ++ ; if ( ! _dbus_string_get_length ( retport ) ) { if ( ! port || ! strcmp ( port , " " ) ) { int result ; struct sockaddr_storage addr ; socklen_t addrlen ; char portbuf [ 50 ] ; addrlen = sizeof ( addr ) ; result = getsockname ( fd , ( struct sockaddr * ) & addr , & addrlen ) ; if ( result == -1 ) { saved_errno = errno ; dbus_set_error ( error , _dbus_error_from_errno ( saved_errno ) , " \" \" " , host ? host : " " , port , _dbus_strerror ( saved_errno ) ) ; goto failed ; } if ( ( res = getnameinfo ( ( struct sockaddr * ) & addr , addrlen , NULL , 0 , portbuf , sizeof ( portbuf ) , NI_NUMERICHOST | NI_NUMERICSERV ) ) != 0 ) { saved_errno = errno ; dbus_set_error ( error , _dbus_error_from_gai ( res , saved_errno ) , " \" \" " , host ? host : " " , port , gai_strerror ( res ) , res ) ; goto failed ; } if ( ! _dbus_string_append ( retport , portbuf ) ) { dbus_set_error ( error , DBUS_ERROR_NO_MEMORY , NULL ) ; goto failed ; } port = _dbus_string_get_const_data ( retport ) ; freeaddrinfo ( ai ) ; goto redo_lookup_with_port ; } else { if ( ! _dbus_string_append ( retport , port ) ) { dbus_set_error ( error , DBUS_ERROR_NO_MEMORY , NULL ) ; goto failed ; } } } tmp = tmp -> ai_next ; } freeaddrinfo ( ai ) ; ai = NULL ; if ( ! nlisten_fd ) { errno = EADDRINUSE ; dbus_set_error ( error , _dbus_error_from_errno ( errno ) , " \" \" " , host ? host : " " , port , _dbus_strerror ( errno ) ) ; goto failed ; } for ( i = 0 ; i < nlisten_fd ; i ++ ) { if ( ! _dbus_set_fd_nonblocking ( listen_fd [ i ] . fd , error ) ) { goto failed ; } } * fds_p = listen_fd ; return nlisten_fd ; failed : if ( ai ) freeaddrinfo ( ai ) ; for ( i = 0 ; i < nlisten_fd ; i ++ ) _dbus_close ( listen_fd [ i ] . fd , NULL ) ; dbus_free ( listen_fd ) ; return -1 ; }
Creates a socket and binds it to the given path, then listens on
 the socket.
Creates a socket and binds it to the given path then listens on the socket .
/* Nagle's algorithm imposes a huge delay on the initial messages
           going over TCP. */ /* Depending on kernel policy, binding to an IPv6 address
                   might implicitly bind to a corresponding IPv4
                   address or vice versa, resulting in EADDRINUSE for the
                   other one (e.g. bindv6only=0 on Linux).

                   Also, after we "goto redo_lookup_with_port" after binding
                   a port on one of the possible addresses, we will
                   try to bind that same port on every address, including the
                   same address again for a second time; that one will
                   also fail with EADDRINUSE.

                   For both those reasons, ignore EADDRINUSE here */ /* backlog */ /* If the user didn't specify a port, or used 0, then
               the kernel chooses a port. After the first address
               is bound to, we need to force all remaining addresses
               to use the same port */ /* Release current address list & redo lookup */