| | |
| | | #include <stdio.h> |
| | | #include <stdlib.h> |
| | | #include <unistd.h> |
| | | #include <string.h> |
| | | #include <sys/socket.h> |
| | | #include <arpa/inet.h> |
| | | #include <netint/in.h> |
| | | //#include <netdb.h> |
| | | #include <sys/time.h> |
| | | |
| | | // DNS resource records |
| | | #define T_A 1 // IPv4 address |
| | | #define T_NS // Name Server |
| | | #define T_CNAME 5 // Cannonical name |
| | | #define T_SOA 6 // State of Authority |
| | | #define T_PTR 12 // Domain name pointer |
| | | #define T_MX 15 // Mail exchange |
| | | |
| | | struct DNS_QUERY |
| | | { |
| | | unsigned short id; //ID number |
| | | unsigned char rd :1; //recursion |
| | | unsigned char aa :1; //truncated message |
| | | unsigned char opcode :4; //message purpose |
| | | unsigned char qr :1; //query/response |
| | | unsigned char rcode :4; //response code |
| | | |
| | | } |
| | | #include <time.h> |
| | | #include "dns.h" |
| | | |
| | | int main(int argc, char** argv) |
| | | { |
| | | |
| | | } |
| | | |
| | | void resolve(char* hostname, char* dns_ip) |
| | | { |
| | | int s; |
| | | struct sockaddr_in dest; |
| | | |
| | | s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| | | dest.sin_family = AF_INET; |
| | | dest.sin_port = htons(53); |
| | | dest.sin_addr.s_addr = inet_addr(dns_ip); |
| | | |
| | | 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]; |
| | | struct timespec time; |
| | | time = resolve(buf, hostname, server, type); |
| | | printf("Request took %ld.%09lds\n", time.tv_sec, time.tv_nsec); |
| | | print_packet(buf); |
| | | return 0; |
| | | } |