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 559 of file ifaddr.c.

References pg_sockaddr_cidr_mask(), and run_ifaddr_callback().

Referenced by check_same_host_or_net(), and main().

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

Definition at line 53 of file ifaddr.c.

References range_sockaddr_AF_INET().

Referenced by check_ip().

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

Definition at line 115 of file ifaddr.c.

References i.

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

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