<commit_before>--[[

Copyright 2014-2015 The Luvit Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

--]]

-- Derived from <NAME> (agentzh)
-- https://github.com/openresty/lua-resty-dns/blob/master/lib/resty/dns/resolver.lua
--[[lit-meta
  name = "luvit/dns"
  version = "2.0.3"
  dependencies = {
    "luvit/dgram@2.0.0",
    "luvit/fs@2.0.0",
    "luvit/net@2.0.0",
    "luvit/timer@2.0.0",
    "luvit/core@2.0.0",
    "luvit/tls@2.0.0",
    "luvit/utils@2.0.0",
  }
  license = "Apache 2"
  homepage = "https://github.com/luvit/luvit/blob/master/deps/dns.lua"
  description = "Node-style dns module for luvit"
  tags = {"luvit", "dns"}
]]

local dgram = require('dgram')
local fs = require('fs')
local net = require('net')
local timer = require('timer')
local Error = require('core').Error
local adapt = require('utils').adapt
local ffi = require('ffi')

local bit = require('bit')
local crypto = require('tls/lcrypto')
local char = string.char
local byte = string.byte
local gsub = string.gsub
local sub = string.sub
local format = string.format
local match = string.match
local band = bit.band
local bor = bit.bor
local rshift = bit.rshift
local lshift = bit.lshift
local insert = table.insert
local concat = table.concat
local ipapi

if ffi.os=='Windows' then
  ffi.cdef[[
    typedef uint32_t DWORD; //Integer
    typedef uint32_t ULONG; //Alias
    typedef uint32_t UINT; //Alias
    typedef ULONG *PULONG; //Pointer

    enum {
      ERROR_SUCCESS         = 0L,
      ERROR_BUFFER_OVERFLOW = 111L,
      MAX_HOSTNAME_LEN      = 128,
      MAX_DOMAIN_NAME_LEN   = 128,
      MAX_SCOPE_ID_LEN      = 256
    };

    typedef struct {
      char String[16];
    } IP_ADDRESS_STRING, *PIP_ADDRESS_STRING, IP_MASK_STRING, *PIP_MASK_STRING;

    typedef struct _IP_ADDR_STRING {
      struct _IP_ADDR_STRING* Next;
      IP_ADDRESS_STRING IpAddress;
      IP_MASK_STRING IpMask;
      DWORD Context;
    } IP_ADDR_STRING, *PIP_ADDR_STRING;

    typedef struct {
      char HostName[ MAX_HOSTNAME_LEN + 4 ];
      char DomainName[ MAX_DOMAIN_NAME_LEN + 4 ];
      PIP_ADDR_STRING CurrentDnsServer;
      IP_ADDR_STRING DnsServerList;
      UINT NodeType;
      char ScopeId[ MAX_SCOPE_ID_LEN + 4 ];
      UINT EnableRouting;
      UINT EnableProxy;
      UINT EnableDns;
    } FIXED_INFO, *PFIXED_INFO;

    DWORD GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen);
  ]]

  ipapi = ffi.load('Iphlpapi.dll')
end

local DEFAULT_SERVERS = {
  {
    ['host'] = '8.8.8.8',
    ['port'] = 53,
    ['tcp'] = false
  },
  {
    ['host'] = '8.8.4.4',
    ['port'] = 53,
    ['tcp'] = false
  },
}
local SERVERS = DEFAULT_SERVERS
local DEFAULT_TIMEOUT = 2000   -- 2 seconds
local TIMEOUT = DEFAULT_TIMEOUT

local TYPE_A      = 1
local TYPE_NS     = 2
local TYPE_CNAME  = 5
local TYPE_PTR    = 12
local TYPE_MX     = 15
local TYPE_TXT    = 16
local TYPE_AAAA   = 28
local TYPE_SRV    = 33
local CLASS_IN    = 1

local resolver_errstrs = {<commit_msg>update dns to avoid https://github.com/LuaJIT/LuaJIT/issues/28<commit_after>--[[

Copyright 2014-2015 The Luvit Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

--]]

-- Derived from <NAME> (agentzh)
-- https://github.com/openresty/lua-resty-dns/blob/master/lib/resty/dns/resolver.lua
--[[lit-meta
  name = "luvit/dns"
  version = "2.0.3"
  dependencies = {
    "luvit/dgram@2.0.0",
    "luvit/fs@2.0.0",
    "luvit/net@2.0.0",
    "luvit/timer@2.0.0",
    "luvit/core@2.0.0",
    "luvit/tls@2.0.0",
    "luvit/utils@2.0.0",
  }
  license = "Apache 2"
  homepage = "https://github.com/luvit/luvit/blob/master/deps/dns.lua"
  description = "Node-style dns module for luvit"
  tags = {"luvit", "dns"}
]]

local dgram = require('dgram')
local fs = require('fs')
local net = require('net')
local timer = require('timer')
local Error = require('core').Error
local adapt = require('utils').adapt
local ffi = require('ffi')

local bit = require('bit')
local crypto = require('tls/lcrypto')
local char = string.char
local byte = string.byte
local gsub = string.gsub
local sub = string.sub
local format = string.format
local match = string.match
local band = bit.band
local bor = bit.bor
local rshift = bit.rshift
local lshift = bit.lshift
local insert = table.insert
local concat = table.concat
local ipapi

if _G._luvit_dns_load then
  ipapi = _G._luvit_dns_load
elseif ffi.os=='Windows' then
  ffi.cdef[[
    typedef uint32_t DWORD; //Integer
    typedef uint32_t ULONG; //Alias
    typedef uint32_t UINT; //Alias
    typedef ULONG *PULONG; //Pointer

    enum {
      ERROR_SUCCESS         = 0L,
      ERROR_BUFFER_OVERFLOW = 111L,
      MAX_HOSTNAME_LEN      = 128,
      MAX_DOMAIN_NAME_LEN   = 128,
      MAX_SCOPE_ID_LEN      = 256
    };

    typedef struct {
      char String[16];
    } IP_ADDRESS_STRING, *PIP_ADDRESS_STRING, IP_MASK_STRING, *PIP_MASK_STRING;

    typedef struct _IP_ADDR_STRING {
      struct _IP_ADDR_STRING* Next;
      IP_ADDRESS_STRING IpAddress;
      IP_MASK_STRING IpMask;
      DWORD Context;
    } IP_ADDR_STRING, *PIP_ADDR_STRING;

    typedef struct {
      char HostName[ MAX_HOSTNAME_LEN + 4 ];
      char DomainName[ MAX_DOMAIN_NAME_LEN + 4 ];
      PIP_ADDR_STRING CurrentDnsServer;
      IP_ADDR_STRING DnsServerList;
      UINT NodeType;
      char ScopeId[ MAX_SCOPE_ID_LEN + 4 ];
      UINT EnableRouting;
      UINT EnableProxy;
      UINT EnableDns;
    } FIXED_INFO, *PFIXED_INFO;

    DWORD GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen);
  ]]

  ipapi = ffi.load('Iphlpapi.dll')
  _G._luvit_dns_load = ipapi
end

local DEFAULT_SERVERS = {
  {
    ['host'] = '8.8.8.8',
    ['port'] = 53,
    ['tcp'] = false
  },
  {
    ['host'] = '8.8.4.4',
    ['port'] = 53,
    ['tcp'] = false
  },
}
local SERVERS = DEFAULT_SERVERS
local DEFAULT_TIMEOUT = 2000   -- 2 seconds
local TIMEOUT = DEFAULT_TIMEOUT

local TYPE_A      = 1
local TYPE_NS     = 2
local TYPE_CNAME  = 5
local TYPE_PTR    = 12
local TYPE_MX     = 15
local TYPE_TXT    = 16
local TYPE_AAAA   = 28
local TYPE_SRV    = 33
local CLASS_IN    = 1

local resolver_errstrs = {
{'fraction_non_alphanumeric': 0.11264204545454545, 'fraction_numerical': 0.026420454545454546, 'mean_word_length': 3.146643109540636, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 5, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, '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': '1008397', 'n_tokens_mistral': 2625, 'n_tokens_neox': 2457, 'n_words': 663}