## IPv6

IPv6 — IPv6 policy functions

## Synopsis

    #include <gnet.h>

    enum                GIPv6Policy;
    void                gnet_ipv6_set_policy                (GIPv6Policy policy);
    GIPv6Policy         gnet_ipv6_get_policy                (void);

## Description

The IPv6 module provides functions for setting and getting the "IPv6 policy". The IPv6 policy affects domain name resolution and server binding. The possible policies are: IPv4 only, IPv6 only, IPv4 then IPv6, and IPv6 then IPv4. GNet attempts to set the policy when `gnet_init()` is called based on two environment variables (if set) or on the host's interfaces. This can be overridden by calling `gnet_ipv6_set_policy()`.

IPv6 policy affects domain name resolution. A domain name can be resolved to several addresses. Most programs will only use the first address in the list. The problem then is what order the addresses should be in. For example, if there are both IPv4 and IPv6 addresses in the list and the system cannot connect to IPv6 hosts then an IPv6 address should not be first in the list. Otherwise, the host will attempt to connect to it and fail. IPv6 policy determines the order of the list. If the policy is "IPv4 only", only IPv4 addresses will be returned. If the policy is "IPv6 then IPv4", IPv6 addresses will come before IPv4 addresses in the list.

IPv6 policy also affects server binding. When a server socket is created, GNet binds to the "any" address by default. There are IPv4 and IPv6 "any" addresses. GNet needs to know which one to use. If the IPv6 policy allows IPv6, GNet will use the IPv6 "any" address. If the IPv6 policy allows only IPv4, GNet will use the IPv4 "any" address.

GNet sets IPv6 policy in `gnet_init()`. First it checks two environment variables: GNET_IPV6_POLICY and IPV6_POLICY. If either is set, it uses the value to set IPv6 policy. Set the value to "4" for `GIPV6_POLICY_IPV4_ONLY`, "6" for `GIPV6_POLICY_IPV6_ONLY`, "46" for `GIPV6_POLICY_IPV4_THEN_IPV6`, or "64" for `GIPV6_POLICY_IPV6_THEN_IPV4`.

If neither environment variable is set, GNet sets the policy based on the host's interfaces. If there are only IPv4 interfaces, the policy is set to `GIPV6_POLICY_IPV4_ONLY`. If there are only IPv6 interfaces, the policy is set to `GIPV6_POLICY_IPV6_ONLY`. If there are both, the policy is set to `GIPV6_POLICY_IPV4_THEN_IPV6`.

At runtime on Windows, GNet will check to see the computer can support IPv6. If so it will set the policy to `GIPV6_POLICY_IPV4_THEN_IPV6`. If not it will set the policy to `GIPV6_POLICY_IPV4_ONLY`. Environment variables are not checked on Windows.

## Details

### enum GIPv6Policy

    typedef enum {
      GIPV6_POLICY_IPV4_THEN_IPV6,
      GIPV6_POLICY_IPV6_THEN_IPV4,
      GIPV6_POLICY_IPV4_ONLY,
      GIPV6_POLICY_IPV6_ONLY
    } GIPv6Policy;

Policy for IPv6 use in GNet. This affects domain name resolution and server binding. `gnet_init()` attempts to set a reasonable default based on environment variables or the interfaces available on the host.

`GIPV6_POLICY_IPV4_THEN_IPV6` | Use IPv4, then IPv6
`GIPV6_POLICY_IPV6_THEN_IPV4` | Use IPv6, then IPv4
`GIPV6_POLICY_IPV4_ONLY` | Use IPv4 only
`GIPV6_POLICY_IPV6_ONLY` | Use IPv6 only

### gnet_ipv6_set_policy ()

    void                gnet_ipv6_set_policy                (GIPv6Policy policy);

Sets the IPv6 policy.

`policy` : | IPv6 policy

### gnet_ipv6_get_policy ()

    GIPv6Policy         gnet_ipv6_get_policy                (void);

Gets the IPv6 policy.

Returns : | IPv6 policy.