typedef __uint16_t | uint16_t |
denotes a unsigned integer type with a width of exactly 16 bits.
typedef __uint32_t | uint32_t |
Unsigned long int
denotes a unsigned integer type with a width of exactly 32 bits.
typedef uint32_t | in_addr_t |
Internet address manipulation
32 bit unsigned int address
These routines convert 16 and 32 bit quantities between network byte order and host byte order. On machines which have a byte order which is the same as the network order, routines are defined as null macros.
These routines are most often used in conjunction with Internet addresses and ports as returned by gethostbyname and getservent .
See also: gethostbyaddr() getservent()
Bugs:
On the VAX bytes are handled backwards from most everyone else in the world.
Parameters | |
---|---|
Note: This description also covers the following functions - htons() ntohl() ntohs() |
See also: gethostbyaddr() getservent()
Parameters | |
---|---|
Refer to htonl() for the documentation |
IMPORT_C in_addr_t | inet_addr | ( | const char * | ) |
struct in_addr or some other internal binary representation, in network byte order). It returns 1 if the address was valid for the specified address family, or 0 if the address was not parseable in the specified address family, or -1 if some system error occurred. This function is presently valid for AF_INET and AF_INET6.
struct in_addr or some other binary form, in network byte order) to presentation format (suitable for external display purposes). The size argument specifies the size, in bytes, of the buffer *dst It returns NULL if a system error occurs (in which case, errno will have been set), or it returns a pointer to the destination string. This function is presently valid for AF_INET and AF_INET6.
The routines inet_addr and inet_aton interpret character strings representing numbers expressed in the Internet standard . notation.
The inet_pton function converts a presentation format address (that is, printable form as held in a character string) to network format (usually a struct in_addr or some other internal binary representation, in network byte order). It returns 1 if the address was valid for the specified address family, or 0 if the address was not parseable in the specified address family, or -1 if some system error occurred. This function is presently valid for AF_INET and AF_INET6.
The inet_aton routine interprets the specified character string as an Internet address, placing the address into the structure provided. It returns 1 if the string was successfully interpreted, or 0 if the string is invalid. The inet_addr functions return numbers suitable for use as Internet addresses.
The function inet_ntop converts an address *src from network format (usually a struct in_addr or some other binary form, in network byte order) to presentation format (suitable for external display purposes). The size argument specifies the size, in bytes, of the buffer *dst It returns NULL if a system error occurs (in which case, errno will have been set), or it returns a pointer to the destination string. This function is presently valid for AF_INET and AF_INET6.
The routine inet_ntoa takes an Internet address and returns an ASCII string representing the address in ' . ' notation.
All Internet addresses are returned in network order (bytes ordered from left to right). All network numbers and local address parts are returned as machine byte order integer values.
Diagnostics:
The constant INADDR_NONE is returned by inet_addr for malformed requests.
See also: gethostbyname()
#include <stdio.h> #include <arpa/inet.h> #include <netinet/in.h> #define IPV6ADDRSIZE 48 int main() { unsigned int nbo_value; char *ipaddrstring="1.2.3.4"; char *ipaddrholdr=NULL; char *ipv6addrstring="8000::123:4567:89AB:CDEF"; struct in_addr ipstruct; struct in6_addr ipv6struct; char result[IPV6ADDRSIZE]; int err; int size; const char* error; nbo_value=inet_addr(ipaddrstring); if(nbo_value == -1) { printf("inet_addr failed0); } else { printf("inet_addr passed0); } ipstruct.s_addr=nbo_value; ipaddrholdr=inet_ntoa(ipstruct); if(ipaddrholdr==NULL) { printf("inet_ntoa failed0); } else { printf("ipaddr is %s0,ipaddrholdr); } err=inet_pton(AF_INET6,ipv6addrstring ,&ipv6struct;); if(err ==0 || err==-1) printf("inet_pton Failed0); else printf("inet_pton passed0); size=sizeof(result); error=inet_ntop(AF_INET6,&ipv6struct.s6;_addr,result,size); if(error==NULL) { printf("inet_ntop failed"); } else { printf("inet_ntop passed"); } err=inet_aton(ipaddrstring,&ipstruct;); if(err==0) { printf("invalid address "); } else { printf("inet_aton passed "); } return 0; } Output: inet_addr passed ipaddr is 1.2.3.4 inet_pton passed inet_ntop passed inet_aton passedThe inet_ntop and inet_pton functions conform to -xns5.2. Note that inet_pton does not accept 1-, 2-, or 3-part dotted addresses; all four parts must be specified and are interpreted only as decimal values. This is a narrower input set than that accepted by inet_aton. These functions appeared in BSD 4.2.
Bugs:
The value INADDR_NONE (0xffffffff) is a valid broadcast address, but inet_addr cannot return that value without indicating failure. The newer inet_aton function does not share this problem. The problem of host byte ordering versus network byte ordering is confusing. The string returned by inet_ntoa resides in a static memory area. Inet_addr should return a struct in_addr.
Parameters | |
---|---|
Note: This description also covers the following functions - inet_aton() inet_ntoa() inet_ntop() inet_pton() |
IMPORT_C char * | inet_ntoa | ( | struct in_addr | ) |
See also: gethostbyname()
Parameters | |
---|---|
Refer to inet_addr() for the documentation |
IMPORT_C const char * | inet_ntop | ( | int | , |
const void * | __restrict, | |||
char * | __restrict, | |||
socklen_t | ||||
) |
See also: gethostbyname()
Parameters | |
---|---|
Refer to inet_addr() for the documentation |
IMPORT_C int | inet_pton | ( | int | , |
const char * | __restrict, | |||
void * | __restrict | |||
) |
See also: gethostbyname()
Parameters | |
---|---|
__restrict | Refer to inet_addr() for the documentation |