Address Manipulation:inet_ntoa, inet_aton, inet_addr

Three important Internet Address manipulation routines are:

In the AF_INET domain, the address is specified using a structure called sockaddr_in , defined in netinet/in.h, which contains atleast three members. */

struct sockaddr_in { 
   short int family;                 /*AF_INET */ 
   unsigned short int sin_port;      /* Port Number */
   struct in_addr sin_addr;          /* Internet Address */
 };
struct in_addr {
   unsigned long int s_addr;
};

inet_ntoa():

  • This API is used for converting the internet host address in, given in network byte order, to a string in IPv4 dotted-decimal notation that is the human-readable format.
  • Syntax: char * inet_ntoa (struct in_addr in);
  • inet_ntoa() returns the dots-and-numbers string in a static buffer that is overwritten with each call to the function.
unsigned long int ip_net = 2110443574;
struct in_addr addr1;
addr1.s_addr = ip_net;
char *dot_ip = inet_ntoa(addr1);
printf("\n IP address is: %s", dot_ip);
          /* OR */
struct sockaddr_in addr2;
addr2.sin_addr.s_addr = ip_net;
dot_ip = inet_ntoa(addr2.sin_addr);
printf("\n IP address is: %s", dot_ip);

inet_aton():

  • This API is used for converting the dotted-decimal IP address into network order.
  • Syntax: int aton (const char *ip, struct in_addr *add)
  • It returns 0 if the address is invalid and non-zero for a valid IP address.
  • On successful, it stores the IP into add.
  • This can be used for validating the IP address.
char *ip = "10.1.1.1";
struct in_addr addr1;

if (inet_aton(ip,  &addr1))
   printf("\n It is a valid IP address %d\n", addr1.s_addr);
else
   printf("\n It is a invalid IP address\n");
            /* OR */
struct sockaddr_in addr2;
if (inet_aton(ip,  &addr2.sin_addr)) 
   printf("\n It is a valid IP address %d\n", addr2.sin_addr.s_addr);
else
   printf("\n It is a invalid IP address\n");

inet_addr():

  • This API is also used to convert the dotted-decimal notation address into network order.
  • Syntax: in_addr_t inet_addr (const char *cp);
  • It is depreciated now.
  • It considers address 255.255.255.255 as invalid address unlike inet_aton(), hence inet_aton() should always be used.
  • net_addr() returns the address as an in_addr_t, or -1 if there’s an error.
  char *ip = "255.255.255.255";
  struct sockaddr_in addr1;
  /* Both can be used to store the network order address */
  addr1.sin_addr.s_addr = inet_addr(ip)
  
  if ( addr1.sin_addr.s_addr == -1)
     printf("\n It is invalid IP address\n");
  else
      printf("\n It is valid IP address: %d,\n",addr1.sin_addr.s_addr);
                            /* OR */
  struct in_addr addr2;
  addr2.s_addr = inet_addr(ip);
  
  if ( addr2.s_addr == -1)
     printf("\n It is invalid IP address\n");
  else
    printf("\n It is valid IP address: %d,\n",addr1.sin_addr.s_addr);

Relevant Posts:



Categories: Operating system (OS)

1 reply

Trackbacks

  1. Index of Operating System - Tech Access

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: