PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
ifaddr.h File Reference
#include "libpq/pqcomm.h"
Include dependency graph for ifaddr.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

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

Functions

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)
 

Typedef Documentation

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

Definition at line 17 of file ifaddr.h.

Function Documentation

int pg_foreach_ifaddr ( PgIfAddrCallback  callback,
void *  cb_data 
)

Definition at line 562 of file ifaddr.c.

References pg_sockaddr_cidr_mask(), and run_ifaddr_callback().

Referenced by check_same_host_or_net(), and main().

563 {
564  struct sockaddr_in addr;
565  struct sockaddr_storage mask;
566 
567 #ifdef HAVE_IPV6
568  struct sockaddr_in6 addr6;
569 #endif
570 
571  /* addr 127.0.0.1/8 */
572  memset(&addr, 0, sizeof(addr));
573  addr.sin_family = AF_INET;
574  addr.sin_addr.s_addr = ntohl(0x7f000001);
575  memset(&mask, 0, sizeof(mask));
576  pg_sockaddr_cidr_mask(&mask, "8", AF_INET);
577  run_ifaddr_callback(callback, cb_data,
578  (struct sockaddr *) & addr,
579  (struct sockaddr *) & mask);
580 
581 #ifdef HAVE_IPV6
582  /* addr ::1/128 */
583  memset(&addr6, 0, sizeof(addr6));
584  addr6.sin6_family = AF_INET6;
585  addr6.sin6_addr.s6_addr[15] = 1;
586  memset(&mask, 0, sizeof(mask));
587  pg_sockaddr_cidr_mask(&mask, "128", AF_INET6);
588  run_ifaddr_callback(callback, cb_data,
589  (struct sockaddr *) & addr6,
590  (struct sockaddr *) & mask);
591 #endif
592 
593  return 0;
594 }
static void run_ifaddr_callback(PgIfAddrCallback callback, void *cb_data, struct sockaddr *addr, struct sockaddr *mask)
Definition: ifaddr.c:193
int pg_sockaddr_cidr_mask(struct sockaddr_storage *mask, char *numbits, int family)
Definition: ifaddr.c:116
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:49
int pg_range_sockaddr ( const struct sockaddr_storage addr,
const struct sockaddr_storage netaddr,
const struct sockaddr_storage netmask 
)

Definition at line 54 of file ifaddr.c.

References range_sockaddr_AF_INET().

Referenced by check_ip().

57 {
58  if (addr->ss_family == AF_INET)
59  return range_sockaddr_AF_INET((const struct sockaddr_in *) addr,
60  (const struct sockaddr_in *) netaddr,
61  (const struct sockaddr_in *) netmask);
62 #ifdef HAVE_IPV6
63  else if (addr->ss_family == AF_INET6)
64  return range_sockaddr_AF_INET6((const struct sockaddr_in6 *) addr,
65  (const struct sockaddr_in6 *) netaddr,
66  (const struct sockaddr_in6 *) netmask);
67 #endif
68  else
69  return 0;
70 }
static int range_sockaddr_AF_INET(const struct sockaddr_in *addr, const struct sockaddr_in *netaddr, const struct sockaddr_in *netmask)
Definition: ifaddr.c:73
int pg_sockaddr_cidr_mask ( struct sockaddr_storage mask,
char *  numbits,
int  family 
)

Definition at line 116 of file ifaddr.c.

References i, and NULL.

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

117 {
118  long bits;
119  char *endptr;
120 
121  if (numbits == NULL)
122  {
123  bits = (family == AF_INET) ? 32 : 128;
124  }
125  else
126  {
127  bits = strtol(numbits, &endptr, 10);
128  if (*numbits == '\0' || *endptr != '\0')
129  return -1;
130  }
131 
132  switch (family)
133  {
134  case AF_INET:
135  {
136  struct sockaddr_in mask4;
137  long maskl;
138 
139  if (bits < 0 || bits > 32)
140  return -1;
141  memset(&mask4, 0, sizeof(mask4));
142  /* avoid "x << 32", which is not portable */
143  if (bits > 0)
144  maskl = (0xffffffffUL << (32 - (int) bits))
145  & 0xffffffffUL;
146  else
147  maskl = 0;
148  mask4.sin_addr.s_addr = htonl(maskl);
149  memcpy(mask, &mask4, sizeof(mask4));
150  break;
151  }
152 
153 #ifdef HAVE_IPV6
154  case AF_INET6:
155  {
156  struct sockaddr_in6 mask6;
157  int i;
158 
159  if (bits < 0 || bits > 128)
160  return -1;
161  memset(&mask6, 0, sizeof(mask6));
162  for (i = 0; i < 16; i++)
163  {
164  if (bits <= 0)
165  mask6.sin6_addr.s6_addr[i] = 0;
166  else if (bits >= 8)
167  mask6.sin6_addr.s6_addr[i] = 0xff;
168  else
169  {
170  mask6.sin6_addr.s6_addr[i] =
171  (0xff << (8 - (int) bits)) & 0xff;
172  }
173  bits -= 8;
174  }
175  memcpy(mask, &mask6, sizeof(mask6));
176  break;
177  }
178 #endif
179  default:
180  return -1;
181  }
182 
183  mask->ss_family = family;
184  return 0;
185 }
#define NULL
Definition: c.h:226
int i