Internet Address Formats (The GNU C Library)

#### 16.6.1 Internet Socket Address Formats ¶

In the Internet namespace, for both IPv4 (`AF_INET`) and IPv6 (`AF_INET6`), a socket address consists of a host address and a port on that host. In addition, the protocol you choose serves effectively as a part of the address because local port numbers are meaningful only within a particular protocol.

The data types for representing socket addresses in the Internet namespace are defined in the header file `netinet/in.h`.

Data Type: struct sockaddr_in ¶

  This is the data type used to represent socket addresses in the Internet namespace. It has the following members:

  `sa_family_t sin_family`

    This identifies the address family or format of the socket address. You should store the value `AF_INET` in this member. The address family is stored in host byte order. See Socket Addresses.

  `struct in_addr sin_addr`

    This is the IPv4 address. See Host Addresses, and Host Names, for how to get a value to store here. The IPv4 address is stored in network byte order.

  `unsigned short int sin_port`

    This is the port number. See Internet Ports. The port number is stored in network byte order.

When you call `bind` or `getsockname`, you should specify `sizeof (struct sockaddr_in)` as the length parameter if you are using an IPv4 Internet namespace socket address.

Data Type: struct sockaddr_in6 ¶

  This is the data type used to represent socket addresses in the IPv6 namespace. It has the following members:

  `sa_family_t sin6_family`

    This identifies the address family or format of the socket address. You should store the value of `AF_INET6` in this member. See Socket Addresses. The address family is stored in host byte order.

  `struct in6_addr sin6_addr`

    This is the IPv6 address of the host machine. See Host Addresses, and Host Names, for how to get a value to store here. The address is stored in network byte order.

  `uint32_t sin6_flowinfo` ¶

    This combines the IPv6 traffic class and flow label values, as found in the IPv6 header. This field is stored in network byte order. Only the 28 lower bits (of the number in network byte order) are used; the remaining bits must be zero. The lower 20 bits are the flow label, and bits 20 to 27 are the the traffic class. Typically, this field is zero.

  `uint32_t sin6_scope_id` ¶

    For link-local addresses, this identifies the interface on which this address is valid. The scope ID is stored in host byte order. Typically, this field is zero.

  `uint16_t sin6_port`

    This is the port number. See Internet Ports. The port number is stored in network byte order.