<commit_before>module DhcpsApi
  module Reservation
    include CommonMethods

    # Lists subnet reservations.
    #
    # @example List subnet reservations
    #
    # api.list_reservations('192.168.42.0')
    #
    # @param reservation_ip [String] Reservation ip address
    #
    # @return [Array<Hash>]
    #
    # @see DHCP_IP_RESERVATION_INFO DHCP_IP_RESERVATION_INFO documentation for the list of available fields.
    #
    def list_reservations(subnet_address)
      items, _ = retrieve_items(:dhcp_v4_enum_subnet_reservations, subnet_address, 1024, 0)
      items
    end

    # Creates a new reservation.
    #
    # @example Create a new reservation
    #
    # api.create_reservation('192.168.42.100', '255.255.255.0', '00:01:02:03:04:05', 'test_reservation', 'test reservation comment')
    #
    # @param reservation_ip [String] Reservation ip address
    # @param reservation_subnet_mask [String] Reservation subnet mask
    # @param reservation_mac [String] Reservation mac address
    # @param reservation_name [String] Reservation name
    # @param reservation_comment [String] Reservation comment
    #
    # @return [Hash]
    #
    # @see DHCP_IP_RESERVATION_INFO DHCP_IP_RESERVATION_INFO documentation for the list of available fields.
    #
    def create_reservation(reservation_ip, reservation_subnet_mask, reservation_mac, reservation_name, reservation_comment = '')
      subnet_element = DhcpsApi::DHCP_SUBNET_ELEMENT_DATA_V4.new
      subnet_element[:element_type] = DhcpsApi::DHCP_SUBNET_ELEMENT_TYPE::DhcpReservedIps
      subnet_element[:element][:reserved_ip] = (reserved_ip = DhcpsApi::DHCP_IP_RESERVATION_V4.new).pointer

      reserved_ip[:reserved_ip_address] = ip_to_uint32(reservation_ip)
      reserved_ip[:reserved_for_client] = DhcpsApi::DHCP_CLIENT_UID.from_mac_address(reservation_mac).pointer
      reserved_ip[:b_allowed_client_types] = DhcpsApi::ClientType::CLIENT_TYPE_NONE

      ip_as_octets = reservation_ip.split('.').map {|octet| octet.to_i}
      mask_as_octets = reservation_subnet_mask.split('.').map {|octet| octet.to_i}
      subnet_address = (0..3).inject([]) {|all, i| all << (ip_as_octets[i] & mask_as_octets[i])}.join('.')

      error = DhcpsApi::Win2008::SubnetElement.DhcpAddSubnetElementV4(to_wchar_string(server_ip_address), ip_to_uint32(subnet_address), subnet_element.pointer)
      raise DhcpsApi::Error.new("Error creating reservation.", error) if error != 0

      modify_client(reservation_ip, reservation_subnet_mask, reservation_mac, reservation_name, reservation_comment, 0, DhcpsApi::ClientType::CLIENT_TYPE_NONE)

      subnet_element.as_ruby_struct
    end

    # Deletes subnet reservations.
    #
    # @example Delete subnet reservations
    #
    # api.delete_reservations('192.168.42.42', '192.168.42.0', '00:01:02:03:04:05')
    #
    # @param reservation_ip [String] Reservation ip address
    # @param subnet_address [String] Subnet ip address
    # @param reservation_mac [String] Reservation mac address
    #
    # @return [void]
    #
    def delete_reservation(reservation_ip, subnet_address, reservation_mac)
      to_delete = DhcpsApi::DHCP_SUBNET_ELEMENT_DATA_V4.new
      to_delete[:element_type] = DhcpsApi::DHCP_SUBNET_ELEMENT_TYPE::DhcpReservedIps
      to_delete[:element][:reserved_ip] = (reserved_ip = DhcpsApi::DHCP_IP_RESERVATION_V4.new).pointer

      reserved_ip[:reserved_ip_address] = ip_to_uint32(reservation_ip)
      reserved_ip[:reserved_for_client] = DhcpsApi::DHCP_CLIENT_UID.from_mac_address(reservation_mac).pointer
      reserved_ip[:b_allowed_client_types] = DhcpsApi::ClientType::CLIENT_TYPE_NONE

      error = DhcpsApi::Win2008::SubnetElement.DhcpRemoveSubnetElementV4(
          to_wchar_string(server_ip_address),<commit_msg>Reservations are now being created with client type 'CLIENT_TYPE_DHCP' by default<commit_after>module DhcpsApi
  module Reservation
    include CommonMethods

    # Lists subnet reservations.
    #
    # @example List subnet reservations
    #
    # api.list_reservations('192.168.42.0')
    #
    # @param reservation_ip [String] Reservation ip address
    #
    # @return [Array<Hash>]
    #
    # @see DHCP_IP_RESERVATION_INFO DHCP_IP_RESERVATION_INFO documentation for the list of available fields.
    #
    def list_reservations(subnet_address)
      items, _ = retrieve_items(:dhcp_v4_enum_subnet_reservations, subnet_address, 1024, 0)
      items
    end

    # Creates a new reservation.
    #
    # @example Create a new reservation
    #
    # api.create_reservation('192.168.42.100', '255.255.255.0', '00:01:02:03:04:05', 'test_reservation', 'test reservation comment')
    #
    # @param reservation_ip [String] Reservation ip address
    # @param reservation_subnet_mask [String] Reservation subnet mask
    # @param reservation_mac [String] Reservation mac address
    # @param reservation_name [String] Reservation name
    # @param reservation_comment [String] Reservation comment
    # @param client_type [String] Client type
    #
    # @return [Hash]
    #
    # @see DHCP_IP_RESERVATION_INFO DHCP_IP_RESERVATION_INFO documentation for the list of available fields.
    # @see ClientType ClientType documentation for the list of available client types.
    #
    def create_reservation(reservation_ip, reservation_subnet_mask, reservation_mac, reservation_name, reservation_comment = '', client_type = DhcpsApi::ClientType::CLIENT_TYPE_DHCP)
      subnet_element = DhcpsApi::DHCP_SUBNET_ELEMENT_DATA_V4.new
      subnet_element[:element_type] = DhcpsApi::DHCP_SUBNET_ELEMENT_TYPE::DhcpReservedIps
      subnet_element[:element][:reserved_ip] = (reserved_ip = DhcpsApi::DHCP_IP_RESERVATION_V4.new).pointer

      reserved_ip[:reserved_ip_address] = ip_to_uint32(reservation_ip)
      reserved_ip[:reserved_for_client] = DhcpsApi::DHCP_CLIENT_UID.from_mac_address(reservation_mac).pointer
      reserved_ip[:b_allowed_client_types] = client_type

      ip_as_octets = reservation_ip.split('.').map {|octet| octet.to_i}
      mask_as_octets = reservation_subnet_mask.split('.').map {|octet| octet.to_i}
      subnet_address = (0..3).inject([]) {|all, i| all << (ip_as_octets[i] & mask_as_octets[i])}.join('.')

      error = DhcpsApi::Win2008::SubnetElement.DhcpAddSubnetElementV4(to_wchar_string(server_ip_address), ip_to_uint32(subnet_address), subnet_element.pointer)
      raise DhcpsApi::Error.new("Error creating reservation.", error) if error != 0

      modify_client(reservation_ip, reservation_subnet_mask, reservation_mac, reservation_name, reservation_comment, 0, client_type)

      subnet_element.as_ruby_struct
    end

    # Deletes subnet reservations.
    #
    # @example Delete subnet reservations
    #
    # api.delete_reservations('192.168.42.42', '192.168.42.0', '00:01:02:03:04:05')
    #
    # @param reservation_ip [String] Reservation ip address
    # @param subnet_address [String] Subnet ip address
    # @param reservation_mac [String] Reservation mac address
    #
    # @return [void]
    #
    def delete_reservation(reservation_ip, subnet_address, reservation_mac)
      to_delete = DhcpsApi::DHCP_SUBNET_ELEMENT_DATA_V4.new
      to_delete[:element_type] = DhcpsApi::DHCP_SUBNET_ELEMENT_TYPE::DhcpReservedIps
      to_delete[:element][:reserved_ip] = (reserved_ip = DhcpsApi::DHCP_IP_RESERVATION_V4.new).pointer

      reserved_ip[:reserved_ip_address] = ip_to_uint32(reservation_ip)
      reserved_ip[:reserved_for_client] = DhcpsApi::DHCP_CLIENT_UID.from_mac_address(reservation_mac).pointer
      reserved_ip[:b_allowed_client_types] = DhcpsApi::ClientType::CLIENT_TYPE_NONE

      error = DhcpsApi::Win2008::SubnetElement.DhcpRemoveSubnetElementV4(
          to_wchar_string(server_ip_address),
{'fraction_non_alphanumeric': 0.09881833528113232, 'fraction_numerical': 0.026749772756784834, 'mean_word_length': 4.684132841328413, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 10, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7029360', 'n_tokens_mistral': 2806, 'n_tokens_neox': 2565, 'n_words': 431}