Simple tools for networking / objects in plain C Snapshot
|
class for transparent treatment of port / ip address pairs. enacapsulates either sockaddr_in or sockaddr_in6 More...
Classes | |
union | SOCKADDR |
Functions | |
int | SOCKADDR_init (SOCKADDR *saddr, IPADDRESS *addr, uint16_t port) |
initialise ip address / port pair. | |
int | SOCKADDR_family (SOCKADDR *addr) |
return addressing family of object (AF_INET or AF_INET6) | |
void * | SOCKADDR_ipaddr (SOCKADDR *addr) |
return address of ip address portion of address / port pair. | |
M_INLINE struct sockaddr * | SOCKADDR_saddr (SOCKADDR *addr) |
socklen_t | SOCKADDR_length (SOCKADDR *addr) |
return address of address / port pair (depends on addressing family) | |
char * | SOCKADDR_to_string (SOCKADDR *saddr) |
formats objec as string |
class for transparent treatment of port / ip address pairs. enacapsulates either sockaddr_in or sockaddr_in6
int SOCKADDR_family | ( | SOCKADDR * | addr | ) |
return addressing family of object (AF_INET or AF_INET6)
Definition at line 99 of file addrutil.c.
{ return addr->addr_ipv4.sin_family; }
initialise ip address / port pair.
Definition at line 73 of file addrutil.c.
{ memset( saddr, 0, sizeof( SOCKADDR ) ); saddr->addr_ipv4.sin_port = htons( port ); saddr->addr_ipv4.sin_family = addr->family; if (addr->family == AF_INET) { saddr->addr_ipv4.sin_addr = addr->addr.ipv4; } else if (addr->family == AF_INET6) { saddr->addr_ipv6.sin6_addr = addr->addr.ipv6; } else { return -1; } return 0; }
void* SOCKADDR_ipaddr | ( | SOCKADDR * | addr | ) |
socklen_t SOCKADDR_length | ( | SOCKADDR * | addr | ) |
return address of address / port pair (depends on addressing family)
Definition at line 89 of file addrutil.c.
M_INLINE struct sockaddr* SOCKADDR_saddr | ( | SOCKADDR * | addr | ) | [read] |
Definition at line 70 of file addrutil.h.
{ return (struct sockaddr *) addr; }
char* SOCKADDR_to_string | ( | SOCKADDR * | saddr | ) |
formats objec as string
Definition at line 114 of file addrutil.c.
{ char sbuf[100]; int nlen, remain; if (SOCKADDR_family(saddr) == AF_INET6) { sbuf[0] = '['; if (! inet_ntop( SOCKADDR_family( saddr ), (const void *) SOCKADDR_ipaddr( saddr ), sbuf + 1, sizeof(sbuf) - 1 ) ) { return 0; } nlen = strlen(sbuf); remain = sizeof(sbuf) - nlen; if (remain <= 0) { return 0; } snprintf(sbuf+strlen(sbuf),remain-1,"]:%hu", ntohs( saddr->addr_ipv4.sin_port ) ); } else { if (! inet_ntop( SOCKADDR_family( saddr ), (const void *) SOCKADDR_ipaddr( saddr ), sbuf , sizeof(sbuf) ) ) { return 0; } nlen = strlen(sbuf); remain = sizeof(sbuf) - nlen; if (remain <= 0) { return 0; } snprintf(sbuf+strlen(sbuf),remain-1,":%hu", ntohs( saddr->addr_ipv4.sin_port ) ); } return strdup(sbuf); }