INET_PTON



INET_PTON

NAME
SYNOPSIS
DESCRIPTION
RETURN VALUE
CONFORMING TO
NOTES
BUGS
EXAMPLE
SEE ALSO
COLOPHON

NAME

inet_pton − convert IPv4 and IPv6 addresses from text to binary form

SYNOPSIS

#include <arpa/inet.h>

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

DESCRIPTION

This function converts the character string src into a network address structure in the af address family, then copies the network address structure to dst. The af argument must be either AF_INET or AF_INET6.

The following address families are currently supported:
AF_INET

src points to a character string containing an IPv4 network address in dotted-decimal format, "ddd.ddd.ddd.ddd", where ddd is a decimal number of up to three digits in the range 0 to 255. The address is converted to a struct in_addr and copied to dst, which must be sizeof(struct in_addr) 411toppm(1) bytes (32 bits) long.

AF_INET6

src points to a character string containing an IPv6 network address. The address is converted to a struct in6_addr and copied to dst, which must be sizeof(struct in6_addr) (16) bytes (128 bits) long. The allowed formats for IPv6 addresses follow these rules:

1.

The preferred format is x:x:x:x:x:x:x:x. This form consists of eight hexadecimal numbers, each of which expresses a 16-bit value (i.e., each x can be up to 4 hex digits).

2.

A series of contiguous zero values in the preferred format can be abbreviated to ::. Only one instance of :: can occur in an address. For example, the loopback address 0:0:0:0:0:0:0:1 can be abbreviated as ::1. The wildcard address, consisting of all zeros, can be written as ::.

3.

An alternate format is useful for expressing IPv4-mapped IPv6 addresses. This form is written as x:x:x:x:x:x:d.d.d.d, where the six leading xs are hexadecimal values that define the six most-significant 16-bit pieces of the address (i.e., 96 bits), and the ds express a value in dotted-decimal notation that defines the least significant 32 bits of the address. An example of such an address is ::FFFF:204.152.189.116.

See RFC 2373 for further details on the representation of IPv6 addresses.

RETURN VALUE

inet_pton() returns 1 on success (network address was successfully converted). 0 is returned if src does not contain a character string representing a valid network address in the specified address family. If af does not contain a valid address family, −1 is returned and errno is set to EAFNOSUPPORT.

CONFORMING TO

POSIX.1-2001.

NOTES

Unlike inet_aton(3) and inet_addr(3), inet_pton() supports IPv6 addresses. On the other hand, inet_pton() accepts only IPv4 addresses in dotted-decimal notation, whereas inet_aton(3) and inet_addr(3) allow the more general numbers-and-dots notation (hexadecimal and octal number formats, and formats that don’t require all four bytes to be explicitly written). For an interface that handles both IPv6 addresses, and IPv4 addresses in numbers-and-dots notation, see getaddrinfo(3).

BUGS

AF_INET6 does not recognize IPv4 addresses. An explicit IPv4-mapped IPv6 address must be supplied in src instead.

EXAMPLE

The program below demonstrates the use of inet_pton() and inet_ntop(3). Here are some example runs:

$ ./a.out i6 0:0:0:0:0:0:0:0
::
$ ./a.out i6 1:0:0:0:0:0:0:8
1::8
$ ./a.out i6 0:0:0:0:0:FFFF:204.152.189.116
::ffff:204.152.189.116

Program source
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main(int argc, char *argv[])
{
unsigned char buf[sizeof(struct in6_addr)];
int domain, s;
char str[INET6_ADDRSTRLEN];

if (argc != 3) {
fprintf(stderr, "Usage: %s {i4|i6|<num>} string\n", argv[0]);
exit(EXIT_FAILURE);
}

domain = (strcmp(argv[1], "i4") == 0) ? AF_INET :
(strcmp(argv[1], "i6") == 0) ? AF_INET6 : atoi(argv[1]);

s = inet_pton(domain, argv[2], buf);
if (s <= 0) {
if (s == 0)
fprintf(stderr, "Not in presentation format");
else
perror("inet_pton");
exit(EXIT_FAILURE);
}

if (inet_ntop(domain, buf, str, INET6_ADDRSTRLEN) == NULL) {
perror("inet_ntop");
exit(EXIT_FAILURE);
}

printf("%s\n", str);

exit(EXIT_SUCCESS);
}

SEE ALSO

getaddrinfo(3), inet(3), inet_ntop(3)

COLOPHON

This page is part of release 3.69 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at http://www.kernel.org/doc/man−pages/.




More Linux Commands

manpages/vconfig.8.html
vconfig(8) - VLAN (802.1q) configuration program. (ManPage)
The vconfig program allows you to create and remove vlan-devices on a vlan enabled kernel. Vlan-devices are virtual ethernet devices which represents the virtua

manpages/acl_get_entry.3.html
acl_get_entry(3) - get an ACL entry - Linux manual page.....
The acl_get_entry() function obtains a descriptor for an ACL entry as specified by entry_id within the ACL indicated by the argument acl. If the value of entry_

manpages/pidof.8.html
pidof(8) - find the process ID of a running program.........
Pidof finds the process ids (pids) of the named programs. It prints those ids on the standard output. This program is on some systems used in run-level change s

manpages/glRasterPos2iv.3gl.html
glRasterPos2iv(3gl) - specify the raster position for pixel
The GL maintains a 3D position in window coordinates. This position, called the raster position, is used to position pixel and bitmap write operations. It is ma

manpages/FcPatternRemove.3.html
FcPatternRemove(3) - Remove one object of the specified type
Removes the value associated with the property object at position id, returning whether the property existed and had a value at that position or not. VERSION Fo

manpages/insl.2.html
insl(2) - port I/O (System calls - Linux man page)..........
This family of functions is used to do low-level port input and output. The out* functions do port output, the in* functions do port input; the b-suffix functio

manpages/grub2-mkpasswd-pbkdf2.1.html
grub2-mkpasswd-pbkdf2(1) generate hashed password for GRUB
Generate PBKDF2 password hash. -c, --iteration-count=NUM Number of PBKDF2 iterations -l, --buflen=NUM Length of generated hash -s, --salt=NUM Length of salt -?,

manpages/cacafire.1.html
cacafire(1) - libcaca's demonstration applications (ManPage)
This manual page documents briefly the cacademo and cacafire programs. cacademo displays ASCII art effects with animated transitions: metaballs, moire pattern o

manpages/oo_define.n.html
oo_define(n) define and configure classes and objects.......
The oo::define command is used to control the configuration of classes, and the oo::objdefine command is used to control the configuration of objects (including

manpages/MenuPopdown.3.html
MenuPopdown(3) - unmap a pop-up (Library - Linux man page)
The XtPopdown function performs the following: * Calls XtCheckSubclass to ensure popup_shell is a subclass of Shell. * Checks that popup_shell is currently popp

manpages/vfs_dirsort.8.html
vfs_dirsort(8) - Sort directory contents - Linux man page...
This VFS module is part of the samba(7) suite. The vfs_dirsort module sorts directory entries alphabetically before sending them to the client. Please be aware

manpages/gnutls_dh_params_import_pkcs3.3.html
gnutls_dh_params_import_pkcs3(3) - API function (Man Page)
This function will extract the DHParams found in a PKCS3 formatted structure. This is the format generated by openssl dhparam tool. If the structure is PEM enco





We can't live, work or learn in freedom unless the software we use is free.