Movatterモバイル変換


[0]ホーム

URL:


man7.org > Linux >man-pages

Linux/UNIX system programming training


inet(3) — Linux manual page

NAME |LIBRARY |SYNOPSIS |DESCRIPTION |ATTRIBUTES |STANDARDS |STANDARDS |NOTES |EXAMPLES |SEE ALSO |COLOPHON

inet(3)                  Library Functions Manualinet(3)

NAME        top

       inet_aton, inet_addr, inet_network, inet_ntoa, inet_makeaddr,       inet_lnaof, inet_netof - Internet address manipulation routines

LIBRARY        top

       Standard C library (libc,-lc)

SYNOPSIS        top

#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>int inet_aton(const char *cp, struct in_addr *inp);in_addr_t inet_addr(const char *cp);in_addr_t inet_network(const char *cp);[[deprecated]] char *inet_ntoa(struct in_addrin);[[deprecated]] struct in_addr inet_makeaddr(in_addr_tnet,in_addr_thost);[[deprecated]] in_addr_t inet_lnaof(struct in_addrin);[[deprecated]] in_addr_t inet_netof(struct in_addrin);   Feature Test Macro Requirements for glibc (seefeature_test_macros(7)):inet_aton(),inet_ntoa():           Since glibc 2.19:               _DEFAULT_SOURCE           In glibc up to and including 2.19:               _BSD_SOURCE || _BSD_SOURCE

DESCRIPTION        top

inet_aton() converts the Internet host addresscp from the IPv4       numbers-and-dots notation into binary form (in network byte order)       and stores it in the structure thatinp points to.inet_aton()       returns nonzero if the address is valid, zero if not.  The address       supplied incp can have one of the following forms:a.b.c.d              Each of the four numeric parts specifies a byte of the              address; the bytes are assigned in left-to-right order to              produce the binary address.a.b.c  Partsa andb specify the first two bytes of the binary              address.  Partc is interpreted as a 16-bit value that              defines the rightmost two bytes of the binary address.              This notation is suitable for specifying (outmoded) Class B              network addresses.a.b    Parta specifies the first byte of the binary address.              Partb is interpreted as a 24-bit value that defines the              rightmost three bytes of the binary address.  This notation              is suitable for specifying (outmoded) Class A network              addresses.a      The valuea is interpreted as a 32-bit value that is stored              directly into the binary address without any byte              rearrangement.       In all of the above forms, components of the dotted address can be       specified in decimal, octal (with a leading0), or hexadecimal,       with a leading0X).  Addresses in any of these forms are       collectively termedIPV4 numbers-and-dots notation.  The form that       uses exactly four decimal numbers is referred to asIPv4 dotted-decimal notation (or sometimes:IPv4 dotted-quad notation).inet_aton() returns 1 if the supplied string was successfully       interpreted, or 0 if the string is invalid (errnoisnot set on       error).       Theinet_addr() function converts the Internet host addresscp       from IPv4 numbers-and-dots notation into binary data in network       byte order.  If the input is invalid,INADDR_NONE(usually -1) is       returned.  Use of this function is problematic because -1 is a       valid address (255.255.255.255).  Avoid its use in favor ofinet_aton(),inet_pton(3), orgetaddrinfo(3), which provide a       cleaner way to indicate error return.       Theinet_network() function convertscp, a string in IPv4 numbers-       and-dots notation, into a number in host byte order suitable for       use as an Internet network address.  On success, the converted       address is returned.  If the input is invalid, -1 is returned.       Theinet_ntoa() function converts the Internet host addressin,       given in network byte order, to a string in IPv4 dotted-decimal       notation.  The string is returned in a statically allocated       buffer, which subsequent calls will overwrite.       Theinet_lnaof() function returns the local network address part       of the Internet addressin.  The returned value is in host byte       order.       Theinet_netof() function returns the network number part of the       Internet addressin.  The returned value is in host byte order.       Theinet_makeaddr() function is the converse ofinet_netof() andinet_lnaof().  It returns an Internet host address in network byte       order, created by combining the network number.I net with the       local addresshost, both in host byte order.       The structurein_addr as used ininet_ntoa(),inet_makeaddr(),inet_lnaof(), andinet_netof() is defined in<netinet/in.h> as:           typedef uint32_t in_addr_t;           struct in_addr {               in_addr_t s_addr;           };

ATTRIBUTES        top

       For an explanation of the terms used in this section, seeattributes(7).       ┌───────────────────────────────┬───────────────┬────────────────┐       │InterfaceAttributeValue│       ├───────────────────────────────┼───────────────┼────────────────┤       │inet_aton(),inet_addr(),     │ Thread safety │ MT-Safe locale │       │inet_network(),inet_ntoa()   │               │                │       ├───────────────────────────────┼───────────────┼────────────────┤       │inet_makeaddr(),              │ Thread safety │ MT-Safe        │       │inet_lnaof(),inet_netof()    │               │                │       └───────────────────────────────┴───────────────┴────────────────┘

STANDARDS        top

inet_addr()inet_ntoa()              POSIX.1-2008.inet_aton()              None.

STANDARDS        top

inet_addr()inet_ntoa()              POSIX.1-2001, 4.3BSD.inet_lnaof(),inet_netof(), andinet_makeaddr() are legacy       functions that assume they are dealing withclassful networkaddresses.  Classful networking divides IPv4 network addresses       into host and network components at byte boundaries, as follows:       Class A              This address type is indicated by the value 0 in the most              significant bit of the (network byte ordered) address.  The              network address is contained in the most significant byte,              and the host address occupies the remaining three bytes.       Class B              This address type is indicated by the binary value 10 in              the most significant two bits of the address.  The network              address is contained in the two most significant bytes, and              the host address occupies the remaining two bytes.       Class C              This address type is indicated by the binary value 110 in              the most significant three bits of the address.  The              network address is contained in the three most significant              bytes, and the host address occupies the remaining byte.       Classful network addresses are now obsolete, having been       superseded by Classless Inter-Domain Routing (CIDR), which divides       addresses into network and host components at arbitrary bit       (rather than byte) boundaries.

NOTES        top

       On x86 architectures, the host byte order is Least Significant       Byte first (little endian), whereas the network byte order, as       used on the Internet, is Most Significant Byte first (big endian).

EXAMPLES        top

       An example of the use ofinet_aton() andinet_ntoa() is shown       below.  Here are some example runs:           $./a.out 226.000.000.037# Last byte is in octal           226.0.0.31           $./a.out 0x7f.1# First byte is in hex           127.0.0.1Program source       #define _DEFAULT_SOURCE       #include <arpa/inet.h>       #include <stdio.h>       #include <stdlib.h>       int       main(int argc, char *argv[])       {           struct in_addr addr;           if (argc != 2) {               fprintf(stderr, "%s <dotted-address>\n", argv[0]);               exit(EXIT_FAILURE);           }           if (inet_aton(argv[1], &addr) == 0) {               fprintf(stderr, "Invalid address\n");               exit(EXIT_FAILURE);           }           printf("%s\n", inet_ntoa(addr));           exit(EXIT_SUCCESS);       }

SEE ALSO        top

byteorder(3),getaddrinfo(3),gethostbyname(3),getnameinfo(3),getnetent(3),inet_net_pton(3),inet_ntop(3),inet_pton(3),hosts(5),networks(5)

COLOPHON        top

       This page is part of theman-pages (Linux kernel and C library       user-space interface documentation) project.  Information about       the project can be found at        ⟨https://www.kernel.org/doc/man-pages/⟩.  If you have a bug report       for this manual page, see       ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩.       This page was obtained from the tarball man-pages-6.15.tar.gz       fetched from       ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on       2025-08-11.  If you discover any rendering problems in this HTML       version of the page, or you believe there is a better or more up-       to-date source for the page, or you have corrections or       improvements to the information in this COLOPHON (which isnot       part of the original manual page), send a mail to       man-pages@man7.orgLinux man-pages 6.15            2025-05-17inet(3)

Pages that refer to this page:getaddrinfo(3)getaddrinfo_a(3)gethostbyname(3)inet_net_pton(3)inet_ntop(3)inet_pton(3)networks(5)ip(7)



HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface.

For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere.

Hosting byjambit GmbH.

Cover of TLPI


[8]ページ先頭

©2009-2025 Movatter.jp