Winsock IPv6 API

This section details the Windows Sockets API (Winsock) functions and structures that are specifically designed for or compatible with IPv6 network communication.

Introduction to IPv6 in Winsock

IPv6 (Internet Protocol version 6) is the successor to IPv4, offering a vastly larger address space and other improvements. Winsock provides a robust set of APIs to enable applications to leverage these benefits. Key aspects include support for IPv6 addresses, socket creation for dual-stack or IPv6-only environments, and mechanisms for handling IPv6-specific options.

Core IPv6 Structures and Types

Understanding these structures is crucial for working with IPv6 sockets:

SOCKADDR_IN6 Structure

This structure represents an IPv6 socket address. It includes the IPv6 address, port number, flow information, and scope identifier.

typedef struct {
    ADDRESS_FAMILY        sin6_family;   // AF_INET6
    USHORT                sin6_port;     // Port number
    ULONG                 sin6_flowinfo; // Flow information
    struct {
        USHORT            s6_addr[8];    // 128-bit IPv6 address
    } sin6_addr;
    union {
        ULONG             sin6_scope_id; // Scope identifier for link-local addresses
        SCOPE_ID          sin6_scope_struct; // For compatibility with struct
    };
} SOCKADDR_IN6, *PSOCKADDR_IN6, *LPSOCKADDR_IN6;
                    

IN6_ADDR Structure

Represents an IPv6 address itself. It's a union that can hold the 128-bit IPv6 address.

typedef struct {
    UCHAR             s6_addr[16];    // 16 bytes for IPv6 address
} IN6_ADDR, *PIN6_ADDR, *LPIN6_ADDR;
                    

Key Winsock IPv6 Functions

These functions are essential for creating and managing IPv6 sockets:

Function Description
socket() Creates a socket. Use AF_INET6 for IPv6.
bind() Associates a local address (SOCKADDR_IN6) with a socket.
connect() Establishes a connection to a remote host (IPv6 address).
sendto() Sends data on a socket, specifying the destination IPv6 address.
recvfrom() Receives data from a socket, returning the sender's IPv6 address.
getaddrinfo() Resolves a hostname or service name to a list of socket address structures, supporting both IPv4 and IPv6. Crucial for modern network programming.
inet_pton() Converts an IPv6 address string to its binary representation.
inet_ntop() Converts an IPv6 address in binary format to its string representation.
setsockopt() Used to set IPv6-specific socket options like IPV6_V6ONLY.
getsockopt() Used to retrieve IPv6-specific socket options.

Dual-Stack Support

Modern Windows systems typically run in a dual-stack mode, supporting both IPv4 and IPv6. Winsock applications can be written to connect to either protocol. The getaddrinfo function plays a key role in abstracting this, allowing you to specify the desired address family or let the system choose.

IPV6_V6ONLY Socket Option

By default, a socket created with AF_INET6 on a dual-stack system may be able to communicate using both IPv4 and IPv6 (IPv4-mapped IPv6 addresses). The IPV6_V6ONLY socket option (set using setsockopt) allows you to explicitly bind a socket to *only* IPv6 traffic, preventing it from accepting or sending IPv4-mapped addresses.

Example: Setting IPV6_V6ONLY

int value = 1; // Set to 1 to enable IPv6-only
if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&value, sizeof(value)) == SOCKET_ERROR) {
    // Handle error
}
                    

Further Reading