C Programming

C: inet_pton function example

The function inet_pton() is used to convert the internet address, the IP address, in the text form containing numeric binary format. This function is used to convert the address that is a human-readable IP address to the binary format.

The inet_pton function works on the IPv4 and IPv6 internet network addresses. In this case, when UNICODE is not defined, Inet_pton is defined to inet_ptonA.

Socket programming and Inet_pton()

A socket is a process/ mechanism provided by many operating systems so that all the programs can access the network accordingly. The socket mechanism does not depend mainly on any specific type of network or IP. A simple socket is created through a system call. This call is like the function call with three arguments inside the parameter Domain, type, and protocol. All these values of parameters and those returned are in integer data type. The domain portion of the argument contains the address family-like AF_INET (IP). And AF_INET6 in the case of IP6, but by default, IPv4 is selected. This is the way socket programming and inet_pton are associated with each other.

Syntax

# int inet_pton(int af, const character *sourc, void *dst);

The syntax includes the input arguments, the ‘src’ refers to the source, and it is null-terminated. It refers to the string that is passed to it. The second argument, ‘dst,’ points towards the buffer that is the storage for the numeric address that inet_pton() stores after the conversion. The system caller ensures the storage capability of a buffer. It makes sure that the buffer allotted by “dst” is larger enough to hold the numeric address.

The third argument is fundamental in the case of Inet_pton usage. When Af_INET is the family parameter, then the parameter points towards the text representation of an IPv4 address in the dotted-decimal notation that is a standard. In the case of AF_INET6, the parameter points towards the text representation of IPv6 in standard notation. The buffer should be capable of holding the IN_ADDR structure in the case of AF_INET. And IN6_ADDR in the case of AF_INET6.

The dotted-decimal notation of address is like xxx.xxx.xxx.xxx., where ‘xxx’ is a 1 to 3 digit decimal number ranging between 0 and 255. In the case of AF_INET6, the “src” string must contain the below-mentioned standard IPv6 terms.

The preferred format contains the hexadecimal values of 8, 16-bit pieces of the address. Having zeros for leading values should be avoided. But in every field, there must be one numeric value.

The address having a string of zeros can be represented “as:”. The symbol “::” should be used once in the whole address. The not specified address should be written as “::”.

A third form that is easy to implement when dealing with the mixed environment of IPV4 and IPv6 is x:x:d.d and so on.

Returned type/value

If the program is successful, inet_pton() returns the value of 1 and then stores the address in the binary format of the internet address in the specific point in the buffer allocated by “dst.” If the function is unsuccessful, it returns 0, but only when the input buffer pointed to the “src” is an invalid string, so the inet_pton() function returns 0. The second case of an unsuccessful function, -1, is returned because the argument is unknown, so a negative value is returned, and “errno” is set. To extend error information, a specific error code can be obtained by calling WSAGetLastError.

Errors can be of two possibilities. For instance, the first one that belongs to the address family that is specified is unsupported. The error is returned if the family parameter specified is not AF_INET. The second one is either null or is not part of the user’s address space.

Implementation of Inet_pton()

The inet_pton() function description is also available on the Linux terminal through the manual page. You can access it by simply using the following command.

$ man inet_pton

Example 1

This example demonstrates the usage of init_pton in the C programming language. Before the implementation, let me mention the tools we have used here. We are implementing the source code in a Linux environment. We will use an Ubuntu text editor to write the code, a Ubuntu terminal to execute the file and display the resultant values.

Like all other source codes, the program starts with libraries. All libraries are well known and commonly used except arpa/inet.h.

#include <arpa/inet.h>

The purpose of using this header file is to contain all the definitions for internet operations.

An IP address is mentioned as a constant character in the main program. The inet_pton() function will take the family, IP address, and source name. Here we have used a switch statement to move in the program according to the options related to the output value. If the value is in a positive number, so kindly display the address after conversion. Before the conversion, the specific buffer is freed or created as described above. The converted binary format address is placed there. In the second case, if the returned value from the function is 0, it means that the match is not found. And for the third case, when the resultant value is -1, an error is created and notified.

After writing the code, save the source code in the file with the C language extension. Now execute the code in the terminal. Use a gcc compiler for that purpose, “pton.c” is the name of a file.

$ gcc –o pton pton.c
$ ./pton

The resultant value shows that the address used in the program is converted into the binary value having alpha and numeric values.

Example 2

This example also displays the address using the same concept with different implementations. But this time, we have taken two addresses, one for INET and INet6. If no number is mentioned with either INET or buffer, it is for Buf6 because it is selected by default.

Two variables will contain addresses as parameters. Similarly, two buffers are assigned to be freed to take the address after conversion. If-else statement is used here. The first two possibilities are for the errors occurring because of the 0 and negative values. Buf6 is used to store the converted address. Inet6 is used for IPv6 here. Now to see the result, go to the terminal.

The resultant value shows that inet6_pton displays the address in the binary form. According to the rules, ‘::’ indicates an unidentified address now replaced with a double colon.

Conclusion

The article ‘INET_pton function example is implemented in C language in Ubuntu Linux operating system. We have explained the working of this function by describing the syntax and the parameters used as an argument inside the function. Some errors are also highlighted that are occurred and are observed through the return values. Examples are explained in detail to remove any ambiguity regarding the purpose and usage of the Init_pton() function.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.