The addr_from_str() function converts the Internet host address in standard numbers-and-dots notation pointed to by the string str into binary data and stores result in the ip/mask pair of pointers. addr_from_str() returns 0 if no argument was converted, 1 if ip was converted, 2 for mask and 3 for both.
The addr_to_str() function converts the Internet host address given in the ip/mask pair of pointers to a string in standard numbers-and-dots notation. The returned string is obtained by malloc and should be free(3)'d by the caller.
Functions | |
uint32_t | addr_hton (uint32_t addr) |
convert address from host to network byte order | |
uint32_t | addr_ntoh (uint32_t addr) |
convert address from network to host byte order | |
int | addr_from_str (const char *str, uint32_t *ip, uint32_t *mask) |
convert string to IP address and netmask | |
char * | addr_to_str (uint32_t ip, uint32_t mask) |
convert IP address and netmask to string |
uint32_t addr_hton | ( | uint32_t | addr | ) |
convert address from host to network byte order
[in] | addr | address in host byte order |
Definition at line 27 of file addr_hton.c.
Referenced by addr_from_str(), and addr_ntoh().
00028 { 00029 if (islitend()) 00030 return (addr >> 24) | 00031 ((addr & 0xff0000) >> 8) | 00032 ((addr & 0xff00 ) << 8) | 00033 (addr << 24); 00034 else 00035 return addr; 00036 }
uint32_t addr_ntoh | ( | uint32_t | addr | ) |
convert address from network to host byte order
[in] | addr | address in network byte order |
Definition at line 20 of file addr_ntoh.c.
References addr_hton().
Referenced by addr_to_str().
00021 { 00022 return addr_hton(addr); 00023 }
int addr_from_str | ( | const char * | str, | |
uint32_t * | ip, | |||
uint32_t * | mask | |||
) |
convert string to IP address and netmask
[in] | str | string in CIDR or netmask notation |
[out] | ip | pointer to store IP address |
[out] | mask | pointer to store netmask |
Definition at line 22 of file addr_from_str.c.
References _lucid_sscanf(), addr_hton(), str_index(), str_isdigit, str_isempty, str_len(), and str_toumax().
Referenced by tcp_connect(), and tcp_listen().
00023 { 00024 int rc = 0; 00025 unsigned long long int cidr; 00026 00027 union { 00028 uint8_t b[4]; 00029 uint32_t l; 00030 } u; 00031 00032 const char *p = str_index(str, '/', str_len(str)); 00033 00034 if (ip && (!p || p - str > 0)) { 00035 if (_lucid_sscanf(str, "%hhu.%hhu.%hhu.%hhu", 00036 &u.b[0], &u.b[1], &u.b[2], &u.b[3]) == 4) { 00037 *ip = addr_hton(u.l); 00038 rc = 1; 00039 } 00040 } 00041 00042 if (!p || !mask) 00043 return rc; 00044 00045 p++; 00046 00047 /* CIDR notation */ 00048 if (!str_isempty(p) && str_isdigit(p)) { 00049 str_toumax(p, &cidr, 10, str_len(p)); 00050 00051 if (cidr > 0 && cidr <= 32) { 00052 *mask = 0xffffffff & ~((1 << (32 - cidr)) - 1); 00053 rc += 2; 00054 } 00055 } 00056 00057 if (!str_isempty(p)) { 00058 if (_lucid_sscanf(p, "%hhu.%hhu.%hhu.%hhu", 00059 &u.b[0], &u.b[1], &u.b[2], &u.b[3]) == 4) { 00060 *mask = addr_hton(u.l); 00061 rc += 2; 00062 } 00063 } 00064 00065 return rc; 00066 }
char* addr_to_str | ( | uint32_t | ip, | |
uint32_t | mask | |||
) |
convert IP address and netmask to string
[in] | ip | IP adress to convert |
[in] | mask | netmask to convert |
free(3)
Definition at line 21 of file addr_to_str.c.
References _lucid_asprintf(), and addr_ntoh().
00022 { 00023 char *buf; 00024 00025 ip = addr_ntoh(ip); 00026 mask = addr_ntoh(mask); 00027 00028 uint8_t *ipp = (uint8_t *) &ip; 00029 uint8_t *maskp = (uint8_t *) &mask; 00030 00031 if (mask) 00032 _lucid_asprintf(&buf, "%u.%u.%u.%u/%u.%u.%u.%u", 00033 ipp[0], ipp[1], ipp[2], ipp[3], 00034 maskp[0], maskp[1], maskp[2], maskp[3]); 00035 00036 else 00037 _lucid_asprintf(&buf, "%u.%u.%u.%u", ipp[0], ipp[1], ipp[2], ipp[3]); 00038 00039 return buf; 00040 }