aboutsummaryrefslogtreecommitdiff
path: root/pa5_dns_resolver/lookup.c
blob: 5b8e4cdb8c26e1522e24d599d97094a0a80460cc (plain)
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
// Jack Sangdahl
// CSCI3753 SP24
// PA5 DNS infoolver
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#define MAX_NAME_LENGTH 16 // longest string in data/
#define MAX_NAME_COUNT 24  // longest file by lines in data/
struct addrinfo hints;

char *domain_ip(char *domain) {
  char *res = (char *)malloc(sizeof(char) * INET_ADDRSTRLEN);
  struct addrinfo *info, *i;
  if (getaddrinfo(domain, NULL, &hints, &info))
    return (char *)"NOT_RESOLVED";
  for (i = info; i != NULL; i = i->ai_next) {
    struct sockaddr_in *ipv4 = (struct sockaddr_in *)i->ai_addr;
    char ipstr[INET_ADDRSTRLEN]; // INET_ADDRSTRLEN translates to 16
    inet_ntop(AF_INET, &(ipv4->sin_addr), ipstr, INET_ADDRSTRLEN);
    strcpy(res, ipstr);
    break;
  }
  freeaddrinfo(info);
  return res;
}

int main(int argc, char **argv) {
  if (argc < 3) {
    fprintf(stdout, "usage: %s <log_file> [ <datafile> ...]\n", argv[0]);
    return EXIT_FAILURE;
  }
  struct timeval begin, end;
  gettimeofday(&begin, NULL);
  if (!memset(&hints, 0, sizeof(hints))) {
    fprintf(stderr, "ERROR: memset failed\n");
    return EXIT_FAILURE;
  }
  hints.ai_family = AF_INET;       // limit to only ipv4
  hints.ai_socktype = SOCK_STREAM; // limit to tcp ports
  FILE *log_file = fopen(argv[1], "w");
  for (uint8_t i = 1; i < argc; i++) {
    FILE *data_file = fopen(argv[i], "r");
    if (!data_file) {
      fprintf(stdout, "ERROR: %s\n", strerror(errno));
      return EXIT_FAILURE;
    }
    char buffer[MAX_NAME_LENGTH];
    char **hostnames = (char **)malloc(sizeof(char *) * MAX_NAME_COUNT);
    uint8_t name_cnt = 0;
    while (fgets(buffer, sizeof(buffer), data_file)) {
      hostnames[name_cnt] = (char *)malloc(sizeof(char) * (strlen(buffer) + 1));
      buffer[strcspn(buffer, "\n")] = 0;
      strcpy(hostnames[name_cnt], buffer);
      name_cnt++;
    }
    for (uint8_t i = 0; i < name_cnt; i++)
      fprintf(log_file, "%s, %s\n", hostnames[i], domain_ip(hostnames[i]));
    free(hostnames);
    fclose(data_file);
  }
  fclose(log_file);
  gettimeofday(&end, NULL);
  double runtime = (double)(end.tv_usec - begin.tv_usec) / 1000000 +
                   (double)(end.tv_sec - begin.tv_sec);
  fprintf(stdout, "%s: total time is %f seconds\n", argv[0], runtime);
  return EXIT_SUCCESS;
}