mirror of https://github.com/Chizi123/dnscomp.git

Joel Grunbaum
2020-10-19 415e3b82ed70c48f61b51b7a93576335af2da7cb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include "dns.h"
#include "servers.h"
#include "slist.h"
 
#define NUM_TESTS 10
 
struct timespec test_server(char *dns_addr, struct hosts_list *hosts);
 
int main(int argc, char** argv)
{
    int option, type = T_A;
    char* server = "1.1.1.1", *hostname = "google.com";
    struct hosts_list *servers_hosts = NULL;
    struct dns_list *servers_dns = NULL;
    while((option = getopt(argc, argv, "s:h:t:")) != -1) {
        switch (option) {
        case 's': //server to use
            add_dns_server(&servers_dns, optarg);
            break;
        case 'h': //hostname to search
            add_hosts_server(&servers_hosts, optarg);
            break;
        case 't':
            if (!strcmp(optarg, "A")) {
                type = T_A;
            } else if (!strcmp(optarg, "AAAA")) {
                type = T_AAAA;
            } else if (!strcmp(optarg, "NS")) {
                type = T_NS;
            } else if (!strcmp(optarg, "CNAME")) {
                type = T_CNAME;
            } else if (!strcmp(optarg, "SOA")) {
                type = T_SOA;
            } else if (!strcmp(optarg, "PTR")) {
                type = T_PTR;
            } else if (!strcmp(optarg, "MX")) {
                type = T_MX;
            } else if (!strcmp(optarg, "TXT")) {
                type = T_TXT;
            } else {
                printf("Error: %s is not a valid DNS record type\n", optarg);
                free_hosts_list(&servers_hosts);
                free_dns_list(&servers_dns);
                exit(1);
            }
            break;
        case '?':
        default:
            printf("Error: invalid option -%c\n", optopt);
            exit(1);
        }
    }
    struct timespec total;
    total = test_server(server, servers_hosts);
    printf("Test took %ld.%09lds\n", total.tv_sec, total.tv_nsec);
    free_dns_list(&servers_dns);
    free_hosts_list(&servers_hosts);
    return 0;
}
 
struct timespec test_server(char* dns_addr, struct hosts_list *hosts)
{
    struct timespec total;
    total.tv_sec = 0; total.tv_nsec = 0;
    for (int i = 0; i < NUM_TESTS; i++) {
        struct hosts_list *curr = hosts;
        while (curr) {
            struct timespec run;
            unsigned char buf[65536];
            run = resolve(buf, curr->server, dns_addr, T_A);
            total.tv_sec += run.tv_sec;
            total.tv_nsec += run.tv_nsec;
            if (total.tv_nsec >= 1000000000) {
                total.tv_sec += 1;
                total.tv_nsec -= 1000000000;
            }
            curr = curr->next;
        }
    }
    return total;
}