inet_ntop函数和inet_pton函数

#include
int inet_pton(int family, const char *strptr, void *addrptr);
  返回:若成功则为1,若输入不是有效的表达格式则为0,若出错则为-1
const char *inet_ntop(int family, const void *addrptr, char *strptr, size_t len);
 返回:若成功则为指向结果的指针, 若出错则为NULL

这两个函数的family参数既可以是AF_INET,也可以是AF_INET6。如果以不被支持的地址族作为family的参数,这两个函数就都返回一个错误,并将errno置为EAFNOSUPPORT。

第一个函数尝试转换由strptr指针所指的字符串,并通过addrptr指针存放二进制结果。若成功则返回1,否则如果对所指定的family而言输入的字符串不是有效的表达式,那么值为0。

inet_ntop进行相反的转换,从数值格式(addrptr)转换到表达格式(strptr)。len参数是目标存储单元的大小,以免该函数溢出其调用者的缓冲区。为有助于指定这个大小,在

INET_NTOP(3)               Linux Programmer's Manual              INET_NTOP(3)

NAME
       inet_ntop - convert IPv4 and IPv6 addresses from binary to text form

SYNOPSIS
       #include 

       const char *inet_ntop(int af, const void *src,
                             char *dst, socklen_t size);

DESCRIPTION
       This  function  converts  the  network  address structure src in the af
       address family into a character string.  The resulting string is copied
       to the buffer pointed to by dst, which must be a non-null pointer.  The
       caller specifies the number of bytes available in this  buffer  in  the
       argument size.

       inet_ntop()  extends  the  inet_ntoa(3)  function  to  support multiple
       address families, inet_ntoa(3) is now considered to  be  deprecated  in
       favor  of  inet_ntop().   The  following address families are currently
       supported:

       AF_INET
              src points to a struct in_addr (in network byte order) which  is
              converted  to an IPv4 network address in the dotted-decimal for‐
              mat,  "ddd.ddd.ddd.ddd".   The  buffer  dst  must  be  at  least
              INET_ADDRSTRLEN bytes long.

       AF_INET6
              src points to a struct in6_addr (in network byte order) which is
              converted to a representation of this address in the most appro‐
              priate IPv6 network address format for this address.  The buffer
              dst must be at least INET6_ADDRSTRLEN bytes long.

RETURN VALUE
       On success, inet_ntop() returns a non-null pointer  to  dst.   NULL  is
       returned if there was an error, with errno set to indicate the error.

ERRORS
       EAFNOSUPPORT
              af was not a valid address family.

       ENOSPC The  converted  address  string  would  exceed the size given by
              size.

ATTRIBUTES
       For  an  explanation  of  the  terms  used   in   this   section,   see
       attributes(7).

       ┌────────────┬───────────────┬────────────────┐
       │Interface   │ Attribute     │ Value          │
       ├────────────┼───────────────┼────────────────┤
       │inet_ntop() │ Thread safety │ MT-Safe locale │
       └────────────┴───────────────┴────────────────┘

CONFORMING TO
       POSIX.1-2001,  POSIX.1-2008.   Note  that  RFC 2553 defines a prototype
       where the last argument size is of type size_t.   Many  systems  follow
       RFC 2553.   Glibc  2.0  and  2.1  have  size_t,  but 2.2 and later have
       socklen_t.

BUGS
       AF_INET6 converts IPv4-mapped IPv6 addresses into an IPv6 format.

EXAMPLE
       See inet_pton(3).

SEE ALSO
       getnameinfo(3), inet(3), inet_pton(3)

COLOPHON
       This page is part of release 4.04 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/.

Linux                             2015-08-08                      INET_NTOP(3)

INET_PTON(3)               Linux Programmer's Manual              INET_PTON(3)

NAME
       inet_pton - convert IPv4 and IPv6 addresses from text to binary form

SYNOPSIS
       #include 

       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) (4) 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 nota‐
                 tion 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 con‐
       verted).  0 is returned if src does not contain a character string rep‐
       resenting  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.

ATTRIBUTES
       For   an   explanation   of   the  terms  used  in  this  section,  see
       attributes(7).

       ┌────────────┬───────────────┬────────────────┐
       │Interface   │ Attribute     │ Value          │
       ├────────────┼───────────────┼────────────────┤
       │inet_pton() │ Thread safety │ MT-Safe locale │
       └────────────┴───────────────┴────────────────┘
CONFORMING TO
       POSIX.1-2001, POSIX.1-2008.

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 num‐
       ber 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  getad‐
       drinfo(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 
       #include 
       #include 
       #include 

       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|} 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 4.04 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/.

Linux                             2015-08-08                      INET_PTON(3)




你可能感兴趣的:(嵌入式,linux,嵌入式-Linux)