Header And Logo

PostgreSQL
| The world's most advanced open source database.

Defines | Typedefs | Functions

ip.h File Reference

#include "getaddrinfo.h"
#include "libpq/pqcomm.h"
Include dependency graph for ip.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define IS_AF_UNIX(fam)   (0)

Typedefs

typedef void(* PgIfAddrCallback )(struct sockaddr *addr, struct sockaddr *netmask, void *cb_data)

Functions

int pg_getaddrinfo_all (const char *hostname, const char *servname, const struct addrinfo *hintp, struct addrinfo **result)
void pg_freeaddrinfo_all (int hint_ai_family, struct addrinfo *ai)
int pg_getnameinfo_all (const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
int pg_range_sockaddr (const struct sockaddr_storage *addr, const struct sockaddr_storage *netaddr, const struct sockaddr_storage *netmask)
int pg_sockaddr_cidr_mask (struct sockaddr_storage *mask, char *numbits, int family)
int pg_foreach_ifaddr (PgIfAddrCallback callback, void *cb_data)

Define Documentation

#define IS_AF_UNIX (   fam  )     (0)

Typedef Documentation

typedef void(* PgIfAddrCallback)(struct sockaddr *addr, struct sockaddr *netmask, void *cb_data)

Definition at line 28 of file ip.h.


Function Documentation

int pg_foreach_ifaddr ( PgIfAddrCallback  callback,
void *  cb_data 
)

Definition at line 857 of file ip.c.

References pg_sockaddr_cidr_mask(), and run_ifaddr_callback().

Referenced by check_same_host_or_net(), and main().

{
    struct sockaddr_in addr;
    struct sockaddr_storage mask;

#ifdef HAVE_IPV6
    struct sockaddr_in6 addr6;
#endif

    /* addr 127.0.0.1/8 */
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = ntohl(0x7f000001);
    memset(&mask, 0, sizeof(mask));
    pg_sockaddr_cidr_mask(&mask, "8", AF_INET);
    run_ifaddr_callback(callback, cb_data,
                        (struct sockaddr *) & addr,
                        (struct sockaddr *) & mask);

#ifdef HAVE_IPV6
    /* addr ::1/128 */
    memset(&addr6, 0, sizeof(addr6));
    addr6.sin6_family = AF_INET6;
    addr6.sin6_addr.s6_addr[15] = 1;
    memset(&mask, 0, sizeof(mask));
    pg_sockaddr_cidr_mask(&mask, "128", AF_INET6);
    run_ifaddr_callback(callback, cb_data,
                        (struct sockaddr *) & addr6,
                        (struct sockaddr *) & mask);
#endif

    return 0;
}

void pg_freeaddrinfo_all ( int  hint_ai_family,
struct addrinfo ai 
)

Definition at line 95 of file ip.c.

References addrinfo::ai_addr, addrinfo::ai_next, free, freeaddrinfo, and NULL.

Referenced by CheckRADIUSAuth(), closePGconn(), connectDBStart(), ident_inet(), parse_hba_auth_opt(), parse_hba_line(), pgstat_init(), PQconnectPoll(), and StreamServerPort().

{
#ifdef HAVE_UNIX_SOCKETS
    if (hint_ai_family == AF_UNIX)
    {
        /* struct was built by getaddrinfo_unix (see pg_getaddrinfo_all) */
        while (ai != NULL)
        {
            struct addrinfo *p = ai;

            ai = ai->ai_next;
            free(p->ai_addr);
            free(p);
        }
    }
    else
#endif   /* HAVE_UNIX_SOCKETS */
    {
        /* struct was built by getaddrinfo() */
        if (ai != NULL)
            freeaddrinfo(ai);
    }
}

int pg_getaddrinfo_all ( const char *  hostname,
const char *  servname,
const struct addrinfo hintp,
struct addrinfo **  result 
)

Definition at line 64 of file ip.c.

References addrinfo::ai_family, getaddrinfo, and NULL.

Referenced by CheckRADIUSAuth(), connectDBStart(), ident_inet(), parse_hba_auth_opt(), parse_hba_line(), pgstat_init(), and StreamServerPort().

{
    int         rc;

    /* not all versions of getaddrinfo() zero *result on failure */
    *result = NULL;

#ifdef HAVE_UNIX_SOCKETS
    if (hintp->ai_family == AF_UNIX)
        return getaddrinfo_unix(servname, hintp, result);
#endif

    /* NULL has special meaning to getaddrinfo(). */
    rc = getaddrinfo((!hostname || hostname[0] == '\0') ? NULL : hostname,
                     servname, hintp, result);

    return rc;
}

int pg_getnameinfo_all ( const struct sockaddr_storage addr,
int  salen,
char *  node,
int  nodelen,
char *  service,
int  servicelen,
int  flags 
)

Definition at line 129 of file ip.c.

References getnameinfo, and strlcpy().

Referenced by BackendInitialize(), check_hostname(), ClientAuthentication(), connectFailureMessage(), ident_inet(), inet_client_addr(), inet_client_port(), inet_server_addr(), inet_server_port(), pg_stat_get_activity(), pg_stat_get_backend_client_addr(), and pg_stat_get_backend_client_port().

{
    int         rc;

#ifdef HAVE_UNIX_SOCKETS
    if (addr && addr->ss_family == AF_UNIX)
        rc = getnameinfo_unix((const struct sockaddr_un *) addr, salen,
                              node, nodelen,
                              service, servicelen,
                              flags);
    else
#endif
        rc = getnameinfo((const struct sockaddr *) addr, salen,
                         node, nodelen,
                         service, servicelen,
                         flags);

    if (rc != 0)
    {
        if (node)
            strlcpy(node, "???", nodelen);
        if (service)
            strlcpy(service, "???", servicelen);
    }

    return rc;
}

int pg_range_sockaddr ( const struct sockaddr_storage addr,
const struct sockaddr_storage netaddr,
const struct sockaddr_storage netmask 
)

Definition at line 276 of file ip.c.

References range_sockaddr_AF_INET().

Referenced by check_ip().

{
    if (addr->ss_family == AF_INET)
        return range_sockaddr_AF_INET((const struct sockaddr_in *) addr,
                                      (const struct sockaddr_in *) netaddr,
                                      (const struct sockaddr_in *) netmask);
#ifdef HAVE_IPV6
    else if (addr->ss_family == AF_INET6)
        return range_sockaddr_AF_INET6((const struct sockaddr_in6 *) addr,
                                       (const struct sockaddr_in6 *) netaddr,
                                       (const struct sockaddr_in6 *) netmask);
#endif
    else
        return 0;
}

int pg_sockaddr_cidr_mask ( struct sockaddr_storage mask,
char *  numbits,
int  family 
)

Definition at line 338 of file ip.c.

References NULL.

Referenced by check_network_callback(), parse_hba_line(), pg_foreach_ifaddr(), and run_ifaddr_callback().

{
    long        bits;
    char       *endptr;

    if (numbits == NULL)
    {
        bits = (family == AF_INET) ? 32 : 128;
    }
    else
    {
        bits = strtol(numbits, &endptr, 10);
        if (*numbits == '\0' || *endptr != '\0')
            return -1;
    }

    switch (family)
    {
        case AF_INET:
            {
                struct sockaddr_in mask4;
                long        maskl;

                if (bits < 0 || bits > 32)
                    return -1;
                memset(&mask4, 0, sizeof(mask4));
                /* avoid "x << 32", which is not portable */
                if (bits > 0)
                    maskl = (0xffffffffUL << (32 - (int) bits))
                        & 0xffffffffUL;
                else
                    maskl = 0;
                mask4.sin_addr.s_addr = htonl(maskl);
                memcpy(mask, &mask4, sizeof(mask4));
                break;
            }

#ifdef HAVE_IPV6
        case AF_INET6:
            {
                struct sockaddr_in6 mask6;
                int         i;

                if (bits < 0 || bits > 128)
                    return -1;
                memset(&mask6, 0, sizeof(mask6));
                for (i = 0; i < 16; i++)
                {
                    if (bits <= 0)
                        mask6.sin6_addr.s6_addr[i] = 0;
                    else if (bits >= 8)
                        mask6.sin6_addr.s6_addr[i] = 0xff;
                    else
                    {
                        mask6.sin6_addr.s6_addr[i] =
                            (0xff << (8 - (int) bits)) & 0xff;
                    }
                    bits -= 8;
                }
                memcpy(mask, &mask6, sizeof(mask6));
                break;
            }
#endif
        default:
            return -1;
    }

    mask->ss_family = family;
    return 0;
}