Internet address conversion


Detailed Description

The addr_htos(), addr_hton(), addr_stoh() and addr_ntoh() functions convert from host- to network-byteorder, and vice versa, respectively.

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

uint16_t addr_htos (uint16_t addr)
 convert address from host to network byte order
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
uint16_t addr_stoh (uint16_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


Function Documentation

uint16_t addr_htos ( uint16_t  addr  ) 

convert address from host to network byte order

Parameters:
[in] addr address in host byte order
Returns:
address in network byte order

Definition at line 26 of file addr_htos.c.

Referenced by addr_stoh(), and tcp_listen().

00027 {
00028         if (islitend())
00029                 return ((addr >> 8) & 0xFF) | (addr << 8);
00030         else
00031                 return addr;
00032 }

uint32_t addr_hton ( uint32_t  addr  ) 

convert address from host to network byte order

Parameters:
[in] addr address in host byte order
Returns:
address in network byte order

Definition at line 26 of file addr_hton.c.

Referenced by addr_from_str(), and addr_ntoh().

00027 {
00028         if (islitend())
00029                 return (addr >> 24) |
00030                         ((addr & 0xff0000) >> 8) |
00031                         ((addr & 0xff00  ) << 8) |
00032                         (addr << 24);
00033         else
00034                 return addr;
00035 }

uint32_t addr_ntoh ( uint32_t  addr  ) 

convert address from network to host byte order

Parameters:
[in] addr address in network byte order
Returns:
address in host byte order

Definition at line 19 of file addr_ntoh.c.

References addr_hton().

00020 {
00021         return addr_hton(addr);
00022 }

uint16_t addr_stoh ( uint16_t  addr  ) 

convert address from network to host byte order

Parameters:
[in] addr address in network byte order
Returns:
address in host byte order

Definition at line 19 of file addr_stoh.c.

References addr_htos().

00020 {
00021         return addr_htos(addr);
00022 }

int addr_from_str ( const char *  str,
uint32_t *  ip,
uint32_t *  mask 
)

convert string to IP address and netmask

Parameters:
[in] str string in CIDR or netmask notation
[out] ip pointer to store IP address in network byte order
[out] mask pointer to store netmask in network byte order
Returns:
0 if no argument was converted, 1 if ip was converted, 2 for mask and 3 for both.

Definition at line 21 of file addr_from_str.c.

References _lucid_sscanf(), addr_hton(), str_chr(), str_isdigit, str_isempty, and str_len().

Referenced by tcp_connect(), and tcp_listen().

00022 {
00023         int rc = 0;
00024         int cidr;
00025 
00026         union {
00027                 uint8_t  b[4];
00028                 uint32_t l;
00029         } u;
00030 
00031         const char *p = str_chr(str, '/', str_len(str));
00032 
00033         /* ip address */
00034         if (!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                         if (ip)
00038                                 *ip = u.l;
00039 
00040                         rc = 1;
00041                 }
00042         }
00043 
00044         if (!p)
00045                 return rc;
00046 
00047         p++;
00048 
00049         /* netmask in CIDR notation */
00050         if (!str_isempty(p) && str_isdigit(p)) {
00051                 if (_lucid_sscanf(p, "%d", &cidr) == 1 && (cidr > 0 && cidr <= 32)) {
00052                         if (mask)
00053                                 *mask = addr_hton(0xffffffff & ~((1 << (32 - cidr)) - 1));
00054 
00055                         rc   += 2;
00056                 }
00057         }
00058 
00059         /* netmask in ip notation */
00060         if (!str_isempty(p)) {
00061                 if (_lucid_sscanf(p, "%hhu.%hhu.%hhu.%hhu",
00062                                         &u.b[0], &u.b[1], &u.b[2], &u.b[3]) == 4) {
00063                         if (mask)
00064                                 *mask = u.l;
00065 
00066                         rc += 2;
00067                 }
00068         }
00069 
00070         return rc;
00071 }

char* addr_to_str ( uint32_t  ip,
uint32_t  mask 
)

convert IP address and netmask to string

Parameters:
[in] ip IP adress to convert in network byte order
[in] mask netmask to convert in network byte order
Returns:
string in netmask notation (obtained with malloc(3))
Note:
The caller should free obtained memory using free(3)
See also:
malloc(3)

free(3)

Definition at line 20 of file addr_to_str.c.

References _lucid_asprintf().

00021 {
00022         char *buf;
00023 
00024         char *ipp   = (char *) &ip;
00025         char *maskp = (char *) &mask;
00026 
00027         if (mask)
00028                 _lucid_asprintf(&buf, "%hhu.%hhu.%hhu.%hhu/%hhu.%hhu.%hhu.%hhu",
00029                                 ipp[0],   ipp[1],   ipp[2],   ipp[3],
00030                                 maskp[0], maskp[1], maskp[2], maskp[3]);
00031 
00032         else
00033                 _lucid_asprintf(&buf, "%hhu.%hhu.%hhu.%hhu",
00034                                 ipp[0],  ipp[1],  ipp[2],  ipp[3]);
00035 
00036         return buf;
00037 }


Generated on Tue Jun 19 20:38:44 2007 for lucid by  doxygen 1.5.2