Simple tools for networking / objects in plain C Snapshot
Classes | Functions
IPADDRESS

class for transparent treatment of ip addresses. encapsulates either ipv4 or ipv6 address. More...

Classes

struct  IPADDRESS

Functions

int IPADDRESS_any (IPADDRESS *addr, int family)
int IPADDRESS_loopback (IPADDRESS *addr, int family)
void IPADDRESS_broadcast_ipv4 (IPADDRESS *addr)
int IPADDRESS_parse_string (IPADDRESS *addr, const char *str)
char * IPADDRESS_to_string (IPADDRESS *addr)
M_INLINE int IPADDRESS_family (IPADDRESS *addr)

Detailed Description

class for transparent treatment of ip addresses. encapsulates either ipv4 or ipv6 address.


Function Documentation

int IPADDRESS_any ( IPADDRESS addr,
int  family 
)

Definition at line 7 of file addrutil.c.

{
  switch(family) {
    case AF_INET:
      addr->family = AF_INET;
      addr->addr.ipv4.s_addr = INADDR_ANY;
      break;
    case AF_INET6:
      addr->family = AF_INET6;
      addr->addr.ipv6 = in6addr_any; 
      break;
    default:
      return -1;
  }
  return 0;
}
void IPADDRESS_broadcast_ipv4 ( IPADDRESS addr)

Definition at line 24 of file addrutil.c.

{
  addr->family = AF_INET;
  addr->addr.ipv4.s_addr = htonl(INADDR_BROADCAST);
}
M_INLINE int IPADDRESS_family ( IPADDRESS addr)

Definition at line 31 of file addrutil.h.

                                                {
  return addr->family;
}
int IPADDRESS_loopback ( IPADDRESS addr,
int  family 
)

Definition at line 31 of file addrutil.c.

{
  switch(family) {
    case AF_INET:
      addr->family = AF_INET;
      addr->addr.ipv4.s_addr = htonl(INADDR_LOOPBACK);
      break;
    case AF_INET6:
      addr->family = AF_INET6;
      addr->addr.ipv6 = in6addr_loopback;
      break;
    default:
      return -1;
  }
  return 0;
}
int IPADDRESS_parse_string ( IPADDRESS addr,
const char *  str 
)

Definition at line 50 of file addrutil.c.

{
  if (inet_pton( AF_INET, str, &addr->addr.ipv4 )) {
     addr->family = AF_INET;
     return 0;
  } else if (inet_pton( AF_INET6, str, &addr->addr.ipv6 )) { 
     addr->family = AF_INET6;
     return 0;
  } 
  return -1;
}
char* IPADDRESS_to_string ( IPADDRESS addr)

Definition at line 62 of file addrutil.c.

{
  char sbuf[100];

  if (! inet_ntop( addr->family, (const void *) &addr->addr, sbuf, sizeof(sbuf) ) ) {
    return 0; 
  }
  return strdup( sbuf );
}