C Programming

Inet Pton() Function in C Language

The socket functions use structures in their input arguments that contain client and server specific data. The most important information in these structures is undoubtedly the IP addresses.

There are several types of structures which are used by socket functions to store these addresses. For example: sockaddr, addrinfo or in_addr, etc.

Regardless of the type of structure in which they are stored, IP addresses are not encoded, which means that each of the numbers in their fields is represented by its binary equivalent. This means that in certain cases, it is necessary to convert the IP addresses to strings or strings to IP addresses in order to work with them.

In this Linux Hint article, you will learn how to convert the strings to IP addresses using the inet_pton() function.

We will look at the syntax of this function and a theoretical description of how it works, the input and output arguments, and the data type in each of them.

We then apply what we learned in practical examples that include code snippets and images that show how the inet_pton() function in the C language converts the strings to IP addresses.

Syntax of the Inet_Pton() Function in C Language

int inet_pton ( int af, const char *src, void *dst );

Description of the Inet_Pton() Function in C Language

The inet_pton() function converts the string which is referenced in src to a network address of the family that is specified in af. The result of this process is an address in direct binary format, returned in the structure which is pointed to by dest.

This function accepts IPv4 and IPv6 addresses and is very useful when we want to create the connection applications in local networks where we know the IP address to which we want to connect.

Inet_pton() is complementary to the inet_ntop() function and performs a reverse conversion.

Next, we look at the input arguments of this function in detail:

af: This entry specifies the address family to be converted.

AF_INET   Converts the string to an IPv4 family address.

AF_INET6 Converts the string to an address of the IPv6 family.

src: The string or pointer to it that specifies the address in text format.

 dest: The pointer to the structure where to store the address in direct binary format. These structures can be of type sockaddr, sockaddr_in, sockaddr_in6, etc.

If the conversion is successful, this function returns 1. If an error occurs, it returns 0. The error code can be queried in the errno global variable.

The inet_pton() function is defined in the “inet.h” header and is one of a series of “.h” files that define the connection functions and are stored in the “arpa” folder. To use this function, we must include it in our C file as follows:

#include <arpa/inet.h>

How to Convert a String to an IP Address Using the Inet_Pton() Function in the C Language

In the following example, we convert the buffer string into an address and store it in a structure of sockaddr_in type whose pointer is addr_in. To do this, we assign the “192.168.1.1” address to the buffer string and declare the addr_in structure and its pointer.

Then, from the if conditional, we call the inet_pton() and pass the family of addresses as input arguments af, which in this case is AF _INET. We pass the address which is stored in the buffer as the second argument in string format. Finally, we pass the address of the addr_in pointer of the structure where we store the address in binary format.

If an error occurs during the conversion, the program enters the if conditional and displays the following message: “Conversion error”.

If inet_pton() does not return an error, we check the address that is stored in the addr_in structure and use the complementary inet_ntop() function to convert it back to a string and output it to the shell with printf().

The following is the code that performs the conversion with inet_pton(). Then, it converts the address back to a string and prints the result to command console:

#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>

int main ()
  {
    char buffer  [INET_ADDRSTRLEN] = "192.168.1.1";
    struct sockaddr_in *addr_in;

//conversion
    if ( inet_pton(AF_INET, buffer, &addr_in) ==0){
           printf ("Conversion error");
             return 1;}

//check the address stored
    memset(&buffer, '\0', sizeof(buffer));
    inet_ntop(AF_INET, &addr_in,buffer, INET_ADDRSTRLEN);
    printf ("IP address: %s\n\n", buffer);
   }

The following image shows the compilation and execution of this code:

The following figure shows the result of sending a string with an invalid address format. In this case, inet_pton() returns 0 and the program flow enters the if condition where an error message is displayed.

How to Convert a String to an IPv6 Address Using the Inet_Pton() Function in the C Language

To convert a string to an IPv6 family address, the code from the previous example can be used. We just need to use the structures and buffers with the sizes that are compatible with this address type and set the AF _INET6 family to af input.

In this case, the buffer size must be INET6_ADDRSTRLEN and the destination structure must be of type sockaddr_in6. Here is the modified code to convert the strings to IPv6 addresses:

#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>

int main ()
    {
    char buffer [INET6_ADDRSTRLEN]="fd10:1aff:48ca:b5f4:0:0:0:0";
    struct sockaddr_in6  *addr_in;

//conversion
    if  (inet_pton (AF_INET6, buffer, &addr_in) ==0){
        printf ("Conversion error");
          return 1;}

//check the address stored
    memset(&buffer, '\0', sizeof(buffer));
    inet_ntop(AF_INET6, &addr_in,buffer, INET6_ADDRSTRLEN);
    printf ("\n\nIP address: %s\n\n", buffer);

    }

The following image shows the compilation and execution of this code that converts a string to an IPv6 address:

Conclusion

In this Linux Hints article, we explained how to convert the string format to IP addresses in direct binary using the inet_pton() function.

We have seen a theoretical description of this function, its input arguments, and the type of data that each processes. Then, we implemented this function in practical examples that show how to convert character strings into IP addresses stored in sockaddr_in type structures that are most often used to establish the connections.

We hope that you found this article useful. Subscribe to our website and use our search engine for more C articles and Linux Hints.

About the author

Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).