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
dbus_bool_t _dbus_append_address_from_socket(DBusSocket fd, DBusString* address,
                                             DBusError* error)
{
    union
    {
        struct sockaddr sa;
        struct sockaddr_storage storage;
        struct sockaddr_un un;
        struct sockaddr_in ipv4;
        struct sockaddr_in6 ipv6;
    } socket;
    char hostip[INET6_ADDRSTRLEN];
    socklen_t size = sizeof(socket);
    DBusString path_str;

    if (getsockname(fd.fd, &socket.sa, &size))
        goto err;

    switch (socket.sa.sa_family)
    {
        case AF_UNIX:
            if (socket.un.sun_path[0] == '\0')
            {
                _dbus_string_init_const(&path_str, &(socket.un.sun_path[1]));
                if (_dbus_string_append(address, "unix:abstract=") &&
                    _dbus_address_append_escaped(address, &path_str))
                    return TRUE;
            }
            else
            {
                _dbus_string_init_const(&path_str, socket.un.sun_path);
                if (_dbus_string_append(address, "unix:path=") &&
                    _dbus_address_append_escaped(address, &path_str))
                    return TRUE;
            }
            break;
        case AF_INET:
            if (inet_ntop(AF_INET, &socket.ipv4.sin_addr, hostip,
                          sizeof(hostip)))
                if (_dbus_string_append_printf(
                        address, "tcp:family=ipv4,host=%s,port=%u", hostip,
                        ntohs(socket.ipv4.sin_port)))
                    return TRUE;
            break;
#ifdef AF_INET6
        case AF_INET6:
            _dbus_string_init_const(&path_str, hostip);
            if (inet_ntop(AF_INET6, &socket.ipv6.sin6_addr, hostip,
                          sizeof(hostip)))
                if (_dbus_string_append_printf(address,
                                               "tcp:family=ipv6,port=%u,host=",
                                               ntohs(socket.ipv6.sin6_port)) &&
                    _dbus_address_append_escaped(address, &path_str))
                    return TRUE;
            break;
#endif
        default:
            dbus_set_error(
                error, _dbus_error_from_errno(EINVAL),
                "Failed to read address from socket: Unknown socket type.");
            return FALSE;
    }
err:
    dbus_set_error(error, _dbus_error_from_errno(errno),
                   "Failed to open socket: %s", _dbus_strerror(errno));
    return FALSE;
}
/**
 * Read the address from the socket and append it to the string
 *
 * @param fd the socket
 * @param address
 * @param error return location for error code
 */
Read the address from the socket and append it to the string
@param fd the socket
@param address
@param error return location for error code
Read the address from the socket and append it to the string @param fd the socket @param address @param error return location for error code
dbus_bool_t _dbus_append_address_from_socket(DBusSocket fd, DBusString* address,
                                             DBusError* error)
{
    union
    {
        struct sockaddr sa;
        struct sockaddr_storage storage;
        struct sockaddr_un un;
        struct sockaddr_in ipv4;
        struct sockaddr_in6 ipv6;
    } socket;
    char hostip[INET6_ADDRSTRLEN];
    socklen_t size = sizeof(socket);
    DBusString path_str;
    if (getsockname(fd.fd, &socket.sa, &size))
        goto err;
    switch (socket.sa.sa_family)
    {
        case AF_UNIX:
            if (socket.un.sun_path[0] == '\0')
            {
                _dbus_string_init_const(&path_str, &(socket.un.sun_path[1]));
                if (_dbus_string_append(address, "unix:abstract=") &&
                    _dbus_address_append_escaped(address, &path_str))
                    return TRUE;
            }
            else
            {
                _dbus_string_init_const(&path_str, socket.un.sun_path);
                if (_dbus_string_append(address, "unix:path=") &&
                    _dbus_address_append_escaped(address, &path_str))
                    return TRUE;
            }
            break;
        case AF_INET:
            if (inet_ntop(AF_INET, &socket.ipv4.sin_addr, hostip,
                          sizeof(hostip)))
                if (_dbus_string_append_printf(
                        address, "tcp:family=ipv4,host=%s,port=%u", hostip,
                        ntohs(socket.ipv4.sin_port)))
                    return TRUE;
            break;
#ifdef AF_INET6
        case AF_INET6:
            _dbus_string_init_const(&path_str, hostip);
            if (inet_ntop(AF_INET6, &socket.ipv6.sin6_addr, hostip,
                          sizeof(hostip)))
                if (_dbus_string_append_printf(address,
                                               "tcp:family=ipv6,port=%u,host=",
                                               ntohs(socket.ipv6.sin6_port)) &&
                    _dbus_address_append_escaped(address, &path_str))
                    return TRUE;
            break;
#endif
        default:
            dbus_set_error(
                error, _dbus_error_from_errno(EINVAL),
                "Failed to read address from socket: Unknown socket type.");
            return FALSE;
    }
err:
    dbus_set_error(error, _dbus_error_from_errno(errno),
                   "Failed to open socket: %s", _dbus_strerror(errno));
    return FALSE;
}
dbus_bool_t _dbus_append_address_from_socket ( DBusSocket fd , DBusString * address , DBusError * error ) { union { struct sockaddr sa ; struct sockaddr_storage storage ; struct sockaddr_un un ; struct sockaddr_in ipv4 ; struct sockaddr_in6 ipv6 ; } socket ; char hostip [ INET6_ADDRSTRLEN ] ; socklen_t size = sizeof ( socket ) ; DBusString path_str ; if ( getsockname ( fd . fd , & socket . sa , & size ) ) goto err ; switch ( socket . sa . sa_family ) { case AF_UNIX : if ( socket . un . sun_path [ 0 ] == ' \0 ' ) { _dbus_string_init_const ( & path_str , & ( socket . un . sun_path [ 1 ] ) ) ; if ( _dbus_string_append ( address , " " ) && _dbus_address_append_escaped ( address , & path_str ) ) return TRUE ; } else { _dbus_string_init_const ( & path_str , socket . un . sun_path ) ; if ( _dbus_string_append ( address , " " ) && _dbus_address_append_escaped ( address , & path_str ) ) return TRUE ; } break ; case AF_INET : if ( inet_ntop ( AF_INET , & socket . ipv4 . sin_addr , hostip , sizeof ( hostip ) ) ) if ( _dbus_string_append_printf ( address , " " , hostip , ntohs ( socket . ipv4 . sin_port ) ) ) return TRUE ; break ; #ifdef AF_INET6 case AF_INET6 : _dbus_string_init_const ( & path_str , hostip ) ; if ( inet_ntop ( AF_INET6 , & socket . ipv6 . sin6_addr , hostip , sizeof ( hostip ) ) ) if ( _dbus_string_append_printf ( address , " " , ntohs ( socket . ipv6 . sin6_port ) ) && _dbus_address_append_escaped ( address , & path_str ) ) return TRUE ; break ; #endif default : dbus_set_error ( error , _dbus_error_from_errno ( EINVAL ) , " " ) ; return FALSE ; } err : dbus_set_error ( error , _dbus_error_from_errno ( errno ) , " " , _dbus_strerror ( errno ) ) ; return FALSE ; }
Read the address from the socket and append it to the string
 @param fd the socket
 @param address
 @param error return location for error code
Read the address from the socket and append it to the string @param fd the socket @param address @param error return location for error code