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

Joel Grunbaum
2020-10-18 2edf2937403e6448f155b31403a4c43d5fe277d4
added command line arguments
2 files modified
49 ■■■■■ changed files
dns.c 5 ●●●●● patch | view | raw | blame | history
main.c 44 ●●●●● patch | view | raw | blame | history
dns.c
@@ -73,8 +73,8 @@
     int s, i, j;
     struct sockaddr_in dest, a;
     unsigned char *qname, *reader;
     struct DNS_HEADER* dns = NULL;
     struct QUESTION* qinfo = NULL;
     struct DNS_HEADER* dns = (struct DNS_HEADER*)buf;
     struct QUESTION* qinfo;
     s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
     dest.sin_family = AF_INET;
@@ -82,7 +82,6 @@
     dest.sin_addr.s_addr = inet_addr(dns_ip);
     //dns packet header
     dns = (struct DNS_HEADER*)buf;
     dns->id = (unsigned short) htons(getpid());
     dns->qr = 0;               //make query
     dns->opcode = 0;           //standard query
main.c
@@ -1,9 +1,51 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "dns.h"
int main(int argc, char** argv)
{
     int option, type = T_A;
     char* server = "1.1.1.1", *hostname = "google.com";
     while((option = getopt(argc, argv, "s:h:t:")) != -1) {
          switch (option) {
          case 's': //server to use
               server = optarg;
               break;
          case 'h': //hostname to search
               hostname = 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_MX;
               } else {
                    printf("Error: %s is not a valid DNS record type\n", optarg);
                    exit(1);
               }
               break;
          case '?':
          default:
               printf("Error: invalid option -%c\n", optopt);
               exit(1);
          }
     }
     unsigned char buf[65536];
     resolve(buf, "google.com", "1.1.1.1", T_A);
     resolve(buf, hostname, server, type);
     print_packet(buf);
     return 0;
}