AzureMXChip/MXCHIPAZ3166/core/lib/netxduo/addons/dhcp/nxd_dhcpv6_server.c
UINT  _nx_dhcpv6_reserve_ip_address_range(NX_DHCPV6_SERVER *dhcpv6_server_ptr,  
                                         NXD_ADDRESS *start_ipv6_address, NXD_ADDRESS *end_ipv6_address,
                                         UINT *addresses_reserved)
{

UINT                         i;
NX_IP                        *ip_ptr;
UINT                         prefix_length;
NXD_ADDRESS                  next_ipv6_address;
NX_DHCPV6_ADDRESS_LEASE      *ip_lease_ptr = NX_NULL; 
UINT                         ga_address_index;


    /* Initialize local and input variables. */
    *addresses_reserved = 0;

    COPY_NXD_ADDRESS(start_ipv6_address, &next_ipv6_address);

    /* Set up a local variable for convenience. */
    ip_ptr = dhcpv6_server_ptr -> nx_dhcpv6_ip_ptr;    
    ga_address_index = dhcpv6_server_ptr -> nx_dhcpv6_server_ga_address_index;         
    prefix_length = ip_ptr -> nx_ipv6_address[ga_address_index].nxd_ipv6_address_prefix_length;

    /* Verify the input addresses match the server interface address prefix (in IPv4 terms, the
       network masked addresses should be equal). */
    if (!CHECK_IP_ADDRESSES_BY_PREFIX(&(ip_ptr -> nx_ipv6_address[ga_address_index].nxd_ipv6_address[0]),
                                       &start_ipv6_address -> nxd_ip_address.v6[0], prefix_length))
    {

        return NX_DHCPV6_INVALID_INTERFACE_IP_ADDRESS;
    }

    if (!CHECK_IP_ADDRESSES_BY_PREFIX(&(ip_ptr -> nx_ipv6_address[ga_address_index].nxd_ipv6_address[0]),
                                       &end_ipv6_address -> nxd_ip_address.v6[0], prefix_length))
    {

        return NX_DHCPV6_INVALID_INTERFACE_IP_ADDRESS;
    }

    /* Clear out existing entries and start adding IP addresses at the beginning of the list. */
    i = 0;

    /* Match on start-end addresses for which leases to reserve. */
    while (i < NX_DHCPV6_MAX_LEASES)
    {

        /* Set local pointer to the entry for convenience. */
        ip_lease_ptr = &dhcpv6_server_ptr -> nx_dhcpv6_lease_list[i];

        /* Add the IP address into this slot. */
        COPY_NXD_ADDRESS(&next_ipv6_address, &ip_lease_ptr -> nx_dhcpv6_lease_IP_address);

        /* Check if we found the starting address.  */
        if (CHECK_IPV6_ADDRESSES_SAME(&ip_lease_ptr -> nx_dhcpv6_lease_IP_address.nxd_ip_address.v6[0], &start_ipv6_address -> nxd_ip_address.v6[0]))
        {
            break;
        }
    }

    if (i == NX_DHCPV6_MAX_LEASES)
    {

        /* No starting address found in the server IP address list. */
        return NX_DHCPV6_INVALID_IP_ADDRESS;
    }

    /* Set the lease times of this entry to infinity. */
    ip_lease_ptr -> nx_dhcpv6_lease_T1_lifetime = NX_DHCPV6_INFINTY_LEASE;
    ip_lease_ptr -> nx_dhcpv6_lease_T2_lifetime = NX_DHCPV6_INFINTY_LEASE;
    ip_lease_ptr -> nx_dhcpv6_lease_valid_lifetime = NX_DHCPV6_INFINTY_LEASE;
    ip_lease_ptr -> nx_dhcpv6_lease_preferred_lifetime = NX_DHCPV6_INFINTY_LEASE;
    ip_lease_ptr -> nx_dhcpv6_lease_assigned_to = (NX_DHCPV6_CLIENT *)(ALIGN_TYPE)0xFFFFFFFF;

    /* Update the number of addresses reserved. */
    (*addresses_reserved)++;

    /* Process the next entry. */

    /* We found the starting address, so start reserving the rest of the address(es). */
    while ((!CHECK_IPV6_ADDRESSES_SAME(&next_ipv6_address.nxd_ip_address.v6[0], &end_ipv6_address -> nxd_ip_address.v6[0])) &&
           (i < NX_DHCPV6_MAX_LEASES))
    {

        i++;

        /* Set local pointer to the next entry for convenience. */
        ip_lease_ptr = &dhcpv6_server_ptr -> nx_dhcpv6_lease_list[i];

        /* Set the lease times of this entry to infinity. */
        ip_lease_ptr -> nx_dhcpv6_lease_T1_lifetime = NX_DHCPV6_INFINTY_LEASE;
        ip_lease_ptr -> nx_dhcpv6_lease_T2_lifetime = NX_DHCPV6_INFINTY_LEASE;
        ip_lease_ptr -> nx_dhcpv6_lease_valid_lifetime = NX_DHCPV6_INFINTY_LEASE;
        ip_lease_ptr -> nx_dhcpv6_lease_preferred_lifetime = NX_DHCPV6_INFINTY_LEASE;

        /* Set the number of addresses actually reserved. */
        (*addresses_reserved)++;

        /* Return a pointer to the beginning of non reserved addresses.*/
        COPY_NXD_ADDRESS(&ip_lease_ptr -> nx_dhcpv6_lease_IP_address, &next_ipv6_address);
    } 

    return(NX_SUCCESS);
}
/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_dhcpv6_reserve_ip_address_range                 PORTABLE C      */
/*                                                           6.1          */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*   This function reserves a range of IPv6 addresses not to assign to    */
/*   Clients, using a start and end address.  It returns the number of    */
/*   addresses actually reserved, depending on the size of the server     */
/*   address table.  To reserve a single address, start and end address   */
/*   are the same.                                                        */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    dhcpv6_server_ptr                  Pointer to DHCPv6 server         */
/*    start_ipv6_address                 Start of IP address in list.     */
/*    end_ipv6_address                   End of IP address in list.       */
/*    addresses_reserved                 Number of addresses reserved     */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                         Successful IP address reservation*/
/*    NX_DHCPV6_INVALID_IP_ADDRESS       Reserved addresses out of range  */
/*    NX_DHCPV6_INVALID_INTERFACE_IP_ADDRESS                              */
/*                                       Supplied address is unreachable  */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    CHECK_IP_ADDRESSES_BY_PREFIX       Confirm addresses match the      */
/*                                        server domain by matching prefix*/
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application code                                                    */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
/*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
/*                                            resulting in version 6.1    */
/*                                                                        */
/**************************************************************************/
FUNCTION                                               RELEASE

_nx_dhcpv6_reserve_ip_address_range                 PORTABLE C
6.1
AUTHOR

Yuxin Zhou, Microsoft Corporation

DESCRIPTION

This function reserves a range of IPv6 addresses not to assign to
Clients, using a start and end address.  It returns the number of
addresses actually reserved, depending on the size of the server
address table.  To reserve a single address, start and end address
are the same.

INPUT

dhcpv6_server_ptr                  Pointer to DHCPv6 server
start_ipv6_address                 Start of IP address in list.
end_ipv6_address                   End of IP address in list.
addresses_reserved                 Number of addresses reserved

OUTPUT

NX_SUCCESS                         Successful IP address reservation
NX_DHCPV6_INVALID_IP_ADDRESS       Reserved addresses out of range
NX_DHCPV6_INVALID_INTERFACE_IP_ADDRESS
Supplied address is unreachable

CALLS

CHECK_IP_ADDRESSES_BY_PREFIX       Confirm addresses match the
server domain by matching prefix

CALLED BY

Application code

RELEASE HISTORY

DATE              NAME                      DESCRIPTION

05-19-2020     Yuxin Zhou               Initial Version 6.0
09-30-2020     Yuxin Zhou               Modified comment(s),
resulting in version 6.1
FUNCTION RELEASE _nx_dhcpv6_reserve_ip_address_range PORTABLE C 6 . 1 AUTHOR Yuxin Zhou Microsoft Corporation DESCRIPTION This function reserves a range of IPv6 addresses not to assign to Clients using a start and end address . It returns the number of addresses actually reserved depending on the size of the server address table . To reserve a single address start and end address are the same . INPUT dhcpv6_server_ptr Pointer to DHCPv6 server start_ipv6_address Start of IP address in list . end_ipv6_address End of IP address in list . addresses_reserved Number of addresses reserved OUTPUT NX_SUCCESS Successful IP address reservation NX_DHCPV6_INVALID_IP_ADDRESS Reserved addresses out of range NX_DHCPV6_INVALID_INTERFACE_IP_ADDRESS Supplied address is unreachable CALLS CHECK_IP_ADDRESSES_BY_PREFIX Confirm addresses match the server domain by matching prefix CALLED BY Application code RELEASE HISTORY DATE NAME DESCRIPTION 05 - 19 - 2020 Yuxin Zhou Initial Version 6 . 0 09 - 30 - 2020 Yuxin Zhou Modified comment ( s ) resulting in version 6 . 1
UINT  _nx_dhcpv6_reserve_ip_address_range(NX_DHCPV6_SERVER *dhcpv6_server_ptr,  
                                         NXD_ADDRESS *start_ipv6_address, NXD_ADDRESS *end_ipv6_address,
                                         UINT *addresses_reserved)
{
UINT                         i;
NX_IP                        *ip_ptr;
UINT                         prefix_length;
NXD_ADDRESS                  next_ipv6_address;
NX_DHCPV6_ADDRESS_LEASE      *ip_lease_ptr = NX_NULL; 
UINT                         ga_address_index;
    *addresses_reserved = 0;
    COPY_NXD_ADDRESS(start_ipv6_address, &next_ipv6_address);
    ip_ptr = dhcpv6_server_ptr -> nx_dhcpv6_ip_ptr;    
    ga_address_index = dhcpv6_server_ptr -> nx_dhcpv6_server_ga_address_index;         
    prefix_length = ip_ptr -> nx_ipv6_address[ga_address_index].nxd_ipv6_address_prefix_length;
    if (!CHECK_IP_ADDRESSES_BY_PREFIX(&(ip_ptr -> nx_ipv6_address[ga_address_index].nxd_ipv6_address[0]),
                                       &start_ipv6_address -> nxd_ip_address.v6[0], prefix_length))
    {
        return NX_DHCPV6_INVALID_INTERFACE_IP_ADDRESS;
    }
    if (!CHECK_IP_ADDRESSES_BY_PREFIX(&(ip_ptr -> nx_ipv6_address[ga_address_index].nxd_ipv6_address[0]),
                                       &end_ipv6_address -> nxd_ip_address.v6[0], prefix_length))
    {
        return NX_DHCPV6_INVALID_INTERFACE_IP_ADDRESS;
    }
    i = 0;
    while (i < NX_DHCPV6_MAX_LEASES)
    {
        ip_lease_ptr = &dhcpv6_server_ptr -> nx_dhcpv6_lease_list[i];
        COPY_NXD_ADDRESS(&next_ipv6_address, &ip_lease_ptr -> nx_dhcpv6_lease_IP_address);
        if (CHECK_IPV6_ADDRESSES_SAME(&ip_lease_ptr -> nx_dhcpv6_lease_IP_address.nxd_ip_address.v6[0], &start_ipv6_address -> nxd_ip_address.v6[0]))
        {
            break;
        }
    }
    if (i == NX_DHCPV6_MAX_LEASES)
    {
        return NX_DHCPV6_INVALID_IP_ADDRESS;
    }
    ip_lease_ptr -> nx_dhcpv6_lease_T1_lifetime = NX_DHCPV6_INFINTY_LEASE;
    ip_lease_ptr -> nx_dhcpv6_lease_T2_lifetime = NX_DHCPV6_INFINTY_LEASE;
    ip_lease_ptr -> nx_dhcpv6_lease_valid_lifetime = NX_DHCPV6_INFINTY_LEASE;
    ip_lease_ptr -> nx_dhcpv6_lease_preferred_lifetime = NX_DHCPV6_INFINTY_LEASE;
    ip_lease_ptr -> nx_dhcpv6_lease_assigned_to = (NX_DHCPV6_CLIENT *)(ALIGN_TYPE)0xFFFFFFFF;
    (*addresses_reserved)++;
    while ((!CHECK_IPV6_ADDRESSES_SAME(&next_ipv6_address.nxd_ip_address.v6[0], &end_ipv6_address -> nxd_ip_address.v6[0])) &&
           (i < NX_DHCPV6_MAX_LEASES))
    {
        i++;
        ip_lease_ptr = &dhcpv6_server_ptr -> nx_dhcpv6_lease_list[i];
        ip_lease_ptr -> nx_dhcpv6_lease_T1_lifetime = NX_DHCPV6_INFINTY_LEASE;
        ip_lease_ptr -> nx_dhcpv6_lease_T2_lifetime = NX_DHCPV6_INFINTY_LEASE;
        ip_lease_ptr -> nx_dhcpv6_lease_valid_lifetime = NX_DHCPV6_INFINTY_LEASE;
        ip_lease_ptr -> nx_dhcpv6_lease_preferred_lifetime = NX_DHCPV6_INFINTY_LEASE;
        (*addresses_reserved)++;
        COPY_NXD_ADDRESS(&ip_lease_ptr -> nx_dhcpv6_lease_IP_address, &next_ipv6_address);
    } 
    return(NX_SUCCESS);
}
UINT _nx_dhcpv6_reserve_ip_address_range ( NX_DHCPV6_SERVER * dhcpv6_server_ptr , NXD_ADDRESS * start_ipv6_address , NXD_ADDRESS * end_ipv6_address , UINT * addresses_reserved ) { UINT i ; NX_IP * ip_ptr ; UINT prefix_length ; NXD_ADDRESS next_ipv6_address ; NX_DHCPV6_ADDRESS_LEASE * ip_lease_ptr = NX_NULL ; UINT ga_address_index ; * addresses_reserved = 0 ; COPY_NXD_ADDRESS ( start_ipv6_address , & next_ipv6_address ) ; ip_ptr = dhcpv6_server_ptr -> nx_dhcpv6_ip_ptr ; ga_address_index = dhcpv6_server_ptr -> nx_dhcpv6_server_ga_address_index ; prefix_length = ip_ptr -> nx_ipv6_address [ ga_address_index ] . nxd_ipv6_address_prefix_length ; if ( ! CHECK_IP_ADDRESSES_BY_PREFIX ( & ( ip_ptr -> nx_ipv6_address [ ga_address_index ] . nxd_ipv6_address [ 0 ] ) , & start_ipv6_address -> nxd_ip_address . v6 [ 0 ] , prefix_length ) ) { return NX_DHCPV6_INVALID_INTERFACE_IP_ADDRESS ; } if ( ! CHECK_IP_ADDRESSES_BY_PREFIX ( & ( ip_ptr -> nx_ipv6_address [ ga_address_index ] . nxd_ipv6_address [ 0 ] ) , & end_ipv6_address -> nxd_ip_address . v6 [ 0 ] , prefix_length ) ) { return NX_DHCPV6_INVALID_INTERFACE_IP_ADDRESS ; } i = 0 ; while ( i < NX_DHCPV6_MAX_LEASES ) { ip_lease_ptr = & dhcpv6_server_ptr -> nx_dhcpv6_lease_list [ i ] ; COPY_NXD_ADDRESS ( & next_ipv6_address , & ip_lease_ptr -> nx_dhcpv6_lease_IP_address ) ; if ( CHECK_IPV6_ADDRESSES_SAME ( & ip_lease_ptr -> nx_dhcpv6_lease_IP_address . nxd_ip_address . v6 [ 0 ] , & start_ipv6_address -> nxd_ip_address . v6 [ 0 ] ) ) { break ; } } if ( i == NX_DHCPV6_MAX_LEASES ) { return NX_DHCPV6_INVALID_IP_ADDRESS ; } ip_lease_ptr -> nx_dhcpv6_lease_T1_lifetime = NX_DHCPV6_INFINTY_LEASE ; ip_lease_ptr -> nx_dhcpv6_lease_T2_lifetime = NX_DHCPV6_INFINTY_LEASE ; ip_lease_ptr -> nx_dhcpv6_lease_valid_lifetime = NX_DHCPV6_INFINTY_LEASE ; ip_lease_ptr -> nx_dhcpv6_lease_preferred_lifetime = NX_DHCPV6_INFINTY_LEASE ; ip_lease_ptr -> nx_dhcpv6_lease_assigned_to = ( NX_DHCPV6_CLIENT * ) ( ALIGN_TYPE ) 0xFFFFFFFF ; ( * addresses_reserved ) ++ ; while ( ( ! CHECK_IPV6_ADDRESSES_SAME ( & next_ipv6_address . nxd_ip_address . v6 [ 0 ] , & end_ipv6_address -> nxd_ip_address . v6 [ 0 ] ) ) && ( i < NX_DHCPV6_MAX_LEASES ) ) { i ++ ; ip_lease_ptr = & dhcpv6_server_ptr -> nx_dhcpv6_lease_list [ i ] ; ip_lease_ptr -> nx_dhcpv6_lease_T1_lifetime = NX_DHCPV6_INFINTY_LEASE ; ip_lease_ptr -> nx_dhcpv6_lease_T2_lifetime = NX_DHCPV6_INFINTY_LEASE ; ip_lease_ptr -> nx_dhcpv6_lease_valid_lifetime = NX_DHCPV6_INFINTY_LEASE ; ip_lease_ptr -> nx_dhcpv6_lease_preferred_lifetime = NX_DHCPV6_INFINTY_LEASE ; ( * addresses_reserved ) ++ ; COPY_NXD_ADDRESS ( & ip_lease_ptr -> nx_dhcpv6_lease_IP_address , & next_ipv6_address ) ; } return ( NX_SUCCESS ) ; }
FUNCTION                                               RELEASE
FUNCTION RELEASE
/* Initialize local and input variables. */ /* Set up a local variable for convenience. */ /* Verify the input addresses match the server interface address prefix (in IPv4 terms, the
       network masked addresses should be equal). */ /* Clear out existing entries and start adding IP addresses at the beginning of the list. */ /* Match on start-end addresses for which leases to reserve. */ /* Set local pointer to the entry for convenience. */ /* Add the IP address into this slot. */ /* Check if we found the starting address.  */ /* No starting address found in the server IP address list. */ /* Set the lease times of this entry to infinity. */ /* Update the number of addresses reserved. */ /* Process the next entry. */ /* We found the starting address, so start reserving the rest of the address(es). */ /* Set local pointer to the next entry for convenience. */ /* Set the lease times of this entry to infinity. */ /* Set the number of addresses actually reserved. */ /* Return a pointer to the beginning of non reserved addresses.*/