Giter VIP home page Giter VIP logo

dns-proxy's Introduction

Сaching Dns proxy

Сaching Dns proxy in the form of GNU/Linux kernel module.

Module was written and tested on Ubuntu 12.04.5 Server 32-bit (3.13 kernel), GCC 4.6.3.

Restrictions:

  • support only protocol IPv4
  • handles only requests of type A (i.e. conversion of the domain to the protocol IPv4)
  • limited cache size (specified during configuration)
  • delete cache on shutdown or restart (cache is located in RAM)

How works caching Dns proxy

Scheme of work module:

                        +----------------------------------------------+
                       /|                                             /|
                      +----------------------------------------------+ |
                      | |                                            | |
                      | |               Gateway machine              | |
                      | |                                            | |
                      | |      ________________________________      | |
      +---------+     | |     |                                |     | |       +----------+
     /         /|     | |     |            Kernel              |     | |      /          /|
    +---------+ |     | |     |                                |     | |     +----------+ |
    |         | |     | |     |   +-------+     +----------+   |     | |     |   Dns    | |
    |  Client | |-----|-------|-->|  Dns  |---->| iptables |---|-----|------>|  Server  | |
    |(browser)| |<----|-------|---| proxy |<----|  (NAT)   |<--|-----|-------|    on    | |
    |         |/      | |     |   +-------+     +----------+   |     | |     | Internet |/
    +---------+       | |     |      | ^                       |     | |     +----------+
                      | |     |      V |                       |     | |
                      | |     |   +-------+                    |     | |
                      | |     |   | Cache |                    |     | |
                      | |     |   +-------+                    |     | |
                      | |     |________________________________|     | |
                      | |                                            | |
                      | |____________________________________________|_|
                      |/                                             |/
                      +----------------------------------------------+

Main moments

Listening of network traffic

The path of the package inspection in the system Netfilter:

         +------------------+
        /                  /|
       +------------------+ |
       |                  | |
       |  Network driver  | |
       |                  |/
       +------------------+
                 |
                 V
      +-----------------------+
     /                       /|
    +-----------------------+ |
    |                       | |
    |  NF_INET_PRE_ROUTING  | |
    |                       |/
    +-----------------------+
                 |
                 V
            +-----------+             +-----------------------+
           /           /|            /                       /|
          +-----------+ |           +-----------------------+ |
          |           | |           |                       | |
          |  Routing  | | --------> |    NF_INET_LOCAL_IN   | |
          |           |/            |                       |/
          +-----------+             +-----------------------+
                 |                               |
                 |                               V
      +-----------------------+         +--------------------+
     /                       /|        /                    /|
    +-----------------------+ |       +--------------------+ |
    |                       | |       |                    | |
    |    NF_INET_FORWARD    | |       |  Local IP service  | |
    |                       |/        |                    |/
    +-----------------------+         +--------------------+
                 |                               |
                 V                               V
            +-----------+             +-----------------------+
           /           /|            /                       /|
          +-----------+ |           +-----------------------+ |
          |           | |           |                       | |
          |  Routing  | | <-------- |   NF_INET_LOCAL_OUT   | |
          |           |/            |                       |/
          +-----------+             +-----------------------+
                 |                      
                 V
      +-----------------------+
     /                       /|
    +-----------------------+ |
    |                       | |
    |  NF_INET_POST_ROUTING | |
    |                       |/
    +-----------------------+
                 |
                 V
         +------------------+
        /                  /|
       +------------------+ |
       |                  | |
       |  Network driver  | |
       |                  |/
       +------------------+

Data processing

The format of the network packet:

      +----------+----------+----------+--------+
     /          /          /          /        /|
    +----------+----------+----------+--------+ |
    |          |          |          |        | |
    |   MAC    |    IP    |   UPD    |  DNS   | |
    |  header  |  header  |  header  |  data  | |
    |          |          |          |        |/
    +----------+----------+ ---------+--------+

Forming a reply message

The structure of the DNS packet:

      +----------+------------+----------+-------------+--------------+
     /          /            /          /             /              /|
    +----------+------------+----------+-------------+--------------+ |
    |          |            |          |             |              | |
    |  Header  |  Question  |  Answer  |  Authority  |  Additional  | |
    |          |            |          |             |              |/
    +----------+------------+ ---------+-------------+--------------+

Cache

The entire cache is stored in RAM PC. Cache is implemented on a data structure: the hash table.

The hash table is static and has a fixed size. The size is defined in the header file dnsproxy.h constant HASHTAB_SIZE.

Used the RS hash function (for strings).

Function listing:

static unsigned int rs_hash(unsigned char *str, unsigned int len)
{
    unsigned int b = 378551;
    unsigned int a = 63689;
    unsigned int hash = 0;
    unsigned int i = 0;

    for (i = 0; i < len; str++, i++){
        hash = hash * a + (unsigned char)(*str);
        a *= b;
    }
    
    return (hash % HASHTAB_SIZE);
}

Advantage hash function:

  • fast calculation of the hash code
  • deterministic
  • uniform distribution of hash values

Installing

Building:

$ make

Installing:

$ make install

Uninstalling:

$ make uninstall

License

This software is licensed under the terms of the GNU General Public License version 2.

dns-proxy's People

Contributors

oct2i avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.