<commit_before>require "resolv"
require "socket"

module Tokaido
  module DNS
    class Server
      IPv4 = "127.0.0.1".freeze
      IPv6 = Addrinfo.getaddrinfo(nil, 80, "PF_INET6", :STREAM)[0].ip_address.freeze

      def initialize(port)
        @port = port
      end

      def start
        @server = UDPSocket.open
        @server.bind IPv4, @port
        Thread.new { process_requests }
      end

      def stop

      end

    private
      def process_requests
        loop do
          query, from = next_query

          message = query.respond do |question, response|
            if question.type == "A"
              response.data = IPv4
            elsif question.type == "AAAA"
              response.data = IPv6
            else
              response.not_found!
              next
            end

            response.ttl = 60
          end

          send_to from, message.encode
        end
      ensure
        @server.close
      end

      def next_query
        # from is [ family, port, host, ip ]<commit_msg>Add temporary debugging<commit_after>require "resolv"
require "socket"

module Tokaido
  module DNS
    class Server
      IPv4 = "127.0.0.1".freeze
      IPv6 = Addrinfo.getaddrinfo(nil, 80, "PF_INET6", :STREAM)[0].ip_address.freeze

      def initialize(port)
        @port = port
      end

      def start
        puts "Starting DNS Server"
        @server = UDPSocket.open
        @server.bind IPv4, @port
        Thread.new { process_requests }
      end

      def stop
        puts "Stopping DNS Server"
        @server.close
      end

    private
      def process_requests
        loop do
          query, from = next_query

          message = query.respond do |question, response|
            if question.type == "A"
              response.data = IPv4
            elsif question.type == "AAAA"
              response.data = IPv6
            else
              response.not_found!
              next
            end

            response.ttl = 60
          end

          send_to from, message.encode
        end
      ensure
        @server.close
      end

      def next_query
        # from is [ family, port, host, ip ]
{'fraction_non_alphanumeric': 0.07369875633348687, 'fraction_numerical': 0.01566098572086596, 'mean_word_length': 1.445945945945946, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '1971946', 'n_tokens_mistral': 651, 'n_tokens_neox': 613, 'n_words': 171}