#include <sys/types.h>
|
#include <sys/socket.h>
|
#include <netinet/in.h>
|
#include <arpa/inet.h>
|
in_addr_t
inet_addr (const char *cp); |
int
inet_aton (const char *cp, struct in_addr *pin); |
char *
inet_ntoa (struct in_addr in); |
const char *
.Fo inet_ntop int af const void * restrict src char * restrict dst socklen_t size .Fc |
int
inet_pton (int af, const char * restrict src, void * restrict dst); |
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. |
a.b.c.d a.b.c a.b a
When four parts are specified, each is interpreted as a byte of data and assigned, from left to right, to the four bytes of an Internet address. Note that when an Internet address is viewed as a 32-bit integer quantity on the VAX the bytes referred to above appear as "d.c.b.a." That is, VAX bytes are ordered from right to left.
When a three part address is specified, the last part is interpreted as a 16-bit quantity and placed in the right-most two bytes of the network address. This makes the three part address format convenient for specifying Class B network addresses as "128.net.host."
When a two part address is supplied, the last part is interpreted as a 24-bit quantity and placed in the right most three bytes of the network address. This makes the two part address format convenient for specifying Class A network addresses as "net.host."
When only one part is given, the value is stored directly in the network address without any byte rearrangement.
All numbers supplied as "parts" in a ‘.’ notation may be decimal, octal, or hexadecimal, as specified in the C language (i.e., a leading 0x or 0X implies hexadecimal; otherwise, a leading 0 implies octal; otherwise, the number is interpreted as decimal).
inet_aton and inet_ntoa function is semi-deprecated in favor of the addr2ascii family. However, since those functions are not yet widely implemented, portable programs cannot rely on their presence and will continue to use the inet functions for some time.
[ENOSPC] | |
size was not large enough to store the presentation form of the address. | |
[EAFNOSUPPORT] | |
*src was not an AF_INET or AF_INET6 family address. | |
IP Version 6 Addressing Architecture
#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 failed0); } else { printf("inet_ntop passed0); } err=inet_aton(ipaddrstring,&ipstruct); if(err==0) { printf("invalid address 0); } else { printf("inet_aton passed0); } 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.
Inet_addr should return a struct in_addr.
© 2005-2007 Nokia |