Simple tools for networking / objects in plain C Snapshot
|
00001 #ifndef __ADDRUTIL_H__ 00002 #define __ADDRUTIL_H__ 00003 00004 #include <netinet/in.h> 00005 #include <cutils/base.h> 00006 00007 /** 00008 * @defgroup IPADDRESS 00009 * @brief class for transparent treatment of ip addresses. encapsulates either ipv4 or ipv6 address. 00010 * @{ 00011 */ 00012 typedef struct { 00013 int family; 00014 union { 00015 struct in_addr ipv4; 00016 struct in6_addr ipv6; 00017 } addr; 00018 } 00019 IPADDRESS; 00020 00021 int IPADDRESS_any( IPADDRESS *addr, int family ); 00022 00023 int IPADDRESS_loopback( IPADDRESS *addr, int family ); 00024 00025 void IPADDRESS_broadcast_ipv4( IPADDRESS *addr ); 00026 00027 int IPADDRESS_parse_string( IPADDRESS *addr, const char *str ); 00028 00029 char * IPADDRESS_to_string( IPADDRESS *addr ); 00030 00031 M_INLINE int IPADDRESS_family( IPADDRESS *addr) { 00032 return addr->family; 00033 } 00034 00035 /** 00036 * @} 00037 */ 00038 00039 /** 00040 * @defgroup SOCKADDR 00041 * @brief class for transparent treatment of port / ip address pairs. enacapsulates either sockaddr_in or sockaddr_in6 00042 * 00043 * @{ 00044 */ 00045 typedef union { 00046 struct sockaddr_in6 addr_ipv6; 00047 struct sockaddr_in addr_ipv4; 00048 } SOCKADDR; 00049 00050 00051 /** 00052 * @brief initialise ip address / port pair. 00053 */ 00054 int SOCKADDR_init( SOCKADDR *saddr, IPADDRESS *addr, uint16_t port ); 00055 00056 /** 00057 * @brief return addressing family of object (AF_INET or AF_INET6) 00058 */ 00059 int SOCKADDR_family( SOCKADDR *addr ); 00060 00061 /** 00062 * @brief return address of ip address portion of address / port pair. 00063 */ 00064 void * SOCKADDR_ipaddr( SOCKADDR * addr ); 00065 00066 00067 /* 00068 * @brief friendly cast to struct sockaddr 00069 */ 00070 M_INLINE struct sockaddr *SOCKADDR_saddr( SOCKADDR *addr ) 00071 { 00072 return (struct sockaddr *) addr; 00073 } 00074 00075 /** 00076 * @brief return address of address / port pair (depends on addressing family) 00077 */ 00078 socklen_t SOCKADDR_length( SOCKADDR * addr ); 00079 00080 /** 00081 * @brief formats objec as string 00082 */ 00083 char * SOCKADDR_to_string( SOCKADDR *saddr ); 00084 00085 /** 00086 * @} 00087 */ 00088 00089 #endif 00090 00091 00092