inet.h File Reference

INET_ADDRSTRLEN

INET6_ADDRSTRLEN

Typedef uint16_t

typedef __uint16_t uint16_t

denotes a unsigned integer type with a width of exactly 16 bits.

_UINT16_T_DECLARED

Typedef uint32_t

typedef __uint32_t uint32_t

Unsigned long int

denotes a unsigned integer type with a width of exactly 32 bits.

_UINT32_T_DECLARED

Typedef in_addr_t

typedef uint32_t in_addr_t

Internet address manipulation

32 bit unsigned int address

_IN_ADDR_T_DECLARED

Typedef in_port_t

typedef uint16_t in_port_t

Internet port manipulation

16bit unsigned int

_IN_PORT_T_DECLARED

Typedef socklen_t

typedef __socklen_t socklen_t

Socket address length type.

_SOCKLEN_T_DECLARED

_STRUCT_IN_ADDR_DECLARED

_BYTEORDER_PROTOTYPED

htonl ( uint32_t )

IMPORT_C uint32_thtonl(uint32_t)

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()

htons ( uint16_t )

IMPORT_C uint16_thtons(uint16_t)
Parameters
Refer to htonl() for the documentation

ntohl

ntohs

inet_addr ( const char * )

IMPORT_C in_addr_tinet_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()

Examples:
#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 passed
The 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()

inet_ntoa ( struct in_addr )

IMPORT_C char *inet_ntoa(struct in_addr)

See also: gethostbyname()

Parameters
Refer to inet_addr() for the documentation

inet_ntop ( int, const void *, char *, socklen_t )

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

inet_pton ( int, const char *, void * )

IMPORT_C intinet_pton(int,
const char *__restrict,
void *__restrict
)

See also: gethostbyname()

Parameters
__restrictRefer to inet_addr() for the documentation

_BYTEORDER_FUNC_DEFINED

htonl

htons