# nishayo/dns_resolver architecture

> Architecture diagram of the GitHub repository nishayo/dns_resolver, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nishayo/dns_resolver
- Repository: https://github.com/nishayo/dns_resolver

## Overview

##### Main Components
- DNS Query Module: Represented by the source file (src/dns_query.c) – this component is responsible for constructing DNS query packets, handling UDP socket creation and communication on port 53, and sending the query.
- DNS Response Module: Represented by (src/dns_response.c) – this module handles the reception and parsing of DNS responses, including decoding A, AAAA, and CNAME records.
- Main Driver: The (src/main.c) file acts as the orchestrator that initializes the process, invokes the query and response modules, and handles command-line arguments (e.g., the domain name).
- Header Definitions: The (include/dns.h) file contains the necessary definitions, constants, and function declarations utilized by the other modules.
- Build & Configuration: The Makefile handles compiling and linking the source code, while setup instructions are provided in the README.

##### Relationships and Interactions
- The Main Driver calls functions in the DNS Query Module to construct and dispatch the queries.
- It then waits for a response from the external DNS server (Google DNS, port 53) and hands the received data to the DNS Response Module for processing.
- The DNS Response Module parses the packet, extracts the necessary records (A, AAAA, and possibly CNAME), and returns structured results back to the Main Driver.
- These interactions follow a sequential flow: User Input → Main Driver → DNS Query → External DNS Server → DNS Response → Output Display.

##### Architectural Patterns and Design Principles
- Modular Design: Each primary function (querying, response parsing) is clearly separated into modules (dns_query.c and dns_response.c) with their declarations in a common header (include/dns.h).
- Layered Abstraction: The main application logic (in src/main.c) abstracts away the lower-level details of packet handling from the user.
- External Communication: The design clearly delineates internal processing from external UDP communication with a designated DNS server.

##### Relevant Technologies, Frameworks, and Libraries
- Language: C with socket programming facilities provided by the Linux/Unix environment.
- Build System: The Make build system controlled via the Makefile.
- External Dependency: Google DNS server at 8.8.8.8 is an integral part of the operational flow.

- Main Driver (src/main.c):
- Input handling (command-line arguments)
- Orchestration of DNS query and response processing
- DNS Query Module (src/dns_query.c):
- Builds DNS query packet
- Handles UDP socket communication (send function)
- DNS Response Module (src/dns_response.c):
- Receives DNS response data
- Parses response, handling A, AAAA and CNAME records
- Header File (include/dns.h):
- Shared definitions and prototypes
- External component:
- Google DNS Server (8.8.8.8) with an annotation “UDP port 53”
- Build/Configuration:
- Makefile indicating the build process, and additional notes from README pertaining to environment and dependencies.

## Diagram (Mermaid)

```mermaid
flowchart TD
    UserInput("User Input (domain name)"):::internal
    MainDriver("Main Driver"):::internal
    DnsQuery("DNS Query Module"):::internal
    DnsResponse("DNS Response Module"):::internal
    Header("Header Definitions"):::internal
    ExtDNS("External DNS Server (8.8.8.8, UDP port 53)"):::external
    Build("Build & Configuration"):::build

    UserInput -->|"providesDomain"| MainDriver
    MainDriver -->|"initQuery"| DnsQuery
    DnsQuery -->|"sendUDPQuery"| ExtDNS
    ExtDNS -->|"returnUDPResponse"| DnsResponse
    DnsResponse -->|"returnParsedData"| MainDriver

    DnsQuery -.->|"uses"| Header
    DnsResponse -.->|"uses"| Header

    Build -->|"compiles"| MainDriver
    Build -->|"compiles"| DnsQuery
    Build -->|"compiles"| DnsResponse

    click MainDriver "https://github.com/nishayo/dns_resolver/blob/main/src/main.c"
    click DnsQuery "https://github.com/nishayo/dns_resolver/blob/main/src/dns_query.c"
    click DnsResponse "https://github.com/nishayo/dns_resolver/blob/main/src/dns_response.c"
    click Header "https://github.com/nishayo/dns_resolver/blob/main/include/dns.h"
    click Build "https://github.com/nishayo/dns_resolver/tree/main/Makefile"

    classDef internal fill:#BBDEFB,stroke:#1A237E,stroke-width:2px;
    classDef external fill:#FFCDD2,stroke:#B71C1C,stroke-width:2px;
    classDef build fill:#C8E6C9,stroke:#2E7D32,stroke-width:2px;
```
