Removed submodules
This commit is contained in:
@@ -0,0 +1,255 @@
|
||||
#include <arpa/inet.h>
|
||||
#include <assert.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "dns.h"
|
||||
#include "port_utils.h"
|
||||
#include "utils.h"
|
||||
|
||||
#define BUFFER_MAX_SIZE 4096
|
||||
|
||||
const char *HOSTNAME = "127.0.0.1";
|
||||
|
||||
ssize_t
|
||||
query_dns (unsigned char *query, size_t size, unsigned char **response)
|
||||
{
|
||||
|
||||
// hardcoded for dukens server
|
||||
const char *port_str = get_port (); // same port dukens listens on
|
||||
|
||||
int port = strtol (port_str, NULL, 10);
|
||||
if (port <= 0 || port > 65535)
|
||||
{
|
||||
fprintf (stderr, "Invalid port: %s\n", port_str);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
struct sockaddr_in addr = { 0 };
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons (port);
|
||||
if (inet_aton (HOSTNAME, &addr.sin_addr) != 1)
|
||||
{
|
||||
fprintf (stderr, "Invalid IPv4 address: %s\n", HOSTNAME);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// create socket
|
||||
int sockfd = socket (AF_INET, SOCK_DGRAM, 0);
|
||||
if (sockfd < 0)
|
||||
{
|
||||
perror ("socket");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
ssize_t sent = sendto (sockfd, query, size, 0, (struct sockaddr *)&addr,
|
||||
sizeof (addr));
|
||||
if (sent < 0)
|
||||
{
|
||||
perror ("sendto");
|
||||
close (sockfd);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// receive and print response
|
||||
*response = calloc (BUFFER_MAX_SIZE, sizeof (unsigned char));
|
||||
ssize_t received
|
||||
= recvfrom (sockfd, *response, BUFFER_MAX_SIZE, 0, NULL, NULL);
|
||||
if (received < 0)
|
||||
{
|
||||
perror ("recvfrom");
|
||||
close (sockfd);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// cleanup
|
||||
close (sockfd);
|
||||
|
||||
return received;
|
||||
}
|
||||
|
||||
void
|
||||
phase_one ()
|
||||
{
|
||||
// hard coded DNS request
|
||||
unsigned char query[17]
|
||||
= { 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01 };
|
||||
|
||||
unsigned char *res_bytes;
|
||||
ssize_t received = query_dns (query, sizeof (query), &res_bytes);
|
||||
|
||||
printf ("Sending %zu bytes to %s:\n", sizeof (query), HOSTNAME);
|
||||
print_byte_block (query, sizeof (query));
|
||||
|
||||
printf ("\nReceived %zd bytes from %s:\n", received, HOSTNAME);
|
||||
print_byte_block (res_bytes, received);
|
||||
printf ("\n");
|
||||
|
||||
dns_response_t res = { 0 };
|
||||
parse_dns_response (res_bytes, sizeof (query), &res);
|
||||
dns_answer_t answer = res.answers[0];
|
||||
|
||||
printf ("Domain name: %s\n", answer.domain);
|
||||
printf ("Record type: %s %s\n", qclass_to_str (answer.qclass),
|
||||
record_to_str (answer.qtype));
|
||||
printf ("TTL: %d\n", answer.ttl);
|
||||
printf ("IPv4 address: %s\n", answer.rdata);
|
||||
|
||||
free (res_bytes);
|
||||
}
|
||||
|
||||
void
|
||||
not_phase_one (int argc, char *argv[])
|
||||
{
|
||||
// building query
|
||||
uint16_t xid = strtol (argv[2], NULL, 10);
|
||||
|
||||
uint16_t record = get_record_from_str (argv[4]);
|
||||
|
||||
char domain_name[BUFFER_LENGTH];
|
||||
|
||||
if (record == PTR)
|
||||
{
|
||||
ptr_ip (domain_name, BUFFER_LENGTH, argv[3]);
|
||||
}
|
||||
else
|
||||
{
|
||||
strncpy (domain_name, argv[3], sizeof (domain_name) - 1);
|
||||
domain_name[sizeof (domain_name) - 1] = '\0';
|
||||
}
|
||||
|
||||
unsigned char query_bytes[512];
|
||||
size_t offset = 0;
|
||||
|
||||
// DNS Header
|
||||
query_bytes[offset++] = (xid >> 8) & 0xFF;
|
||||
query_bytes[offset++] = xid & 0xFF;
|
||||
query_bytes[offset++] = 0x01; // flags
|
||||
query_bytes[offset++] = 0x00;
|
||||
query_bytes[offset++] = 0x00; // QDCOUNT high
|
||||
query_bytes[offset++] = 0x01; // QDCOUNT low
|
||||
// rest zeros (ANCOUNT, NSCOUNT, ARCOUNT)
|
||||
for (int i = 0; i < 6; i++)
|
||||
query_bytes[offset++] = 0x00;
|
||||
|
||||
// Question section: domain name
|
||||
char *token = strtok (domain_name, ".");
|
||||
while (token)
|
||||
{
|
||||
size_t len = strlen (token);
|
||||
query_bytes[offset++] = (unsigned char)len;
|
||||
memcpy (&query_bytes[offset], token, len);
|
||||
offset += len;
|
||||
token = strtok (NULL, ".");
|
||||
}
|
||||
query_bytes[offset++] = 0x00; // end of domain
|
||||
|
||||
query_bytes[offset++] = record >> 8;
|
||||
query_bytes[offset++] = record & 0xff;
|
||||
query_bytes[offset++] = 0x00; // QCLASS = IN
|
||||
query_bytes[offset++] = 0x01;
|
||||
|
||||
unsigned char *res_bytes;
|
||||
query_dns (query_bytes, offset, &res_bytes);
|
||||
|
||||
dns_query_t query = { 0 };
|
||||
parse_dns_query (&query_bytes[0], &query);
|
||||
dns_response_t response = { 0 };
|
||||
parse_dns_response (&res_bytes[0], offset, &response);
|
||||
|
||||
// print header
|
||||
print_dns_header (&response.query.header);
|
||||
|
||||
// print question section
|
||||
print_question_section (&query);
|
||||
|
||||
uint8_t status = (response.query.header.flags >> 0) & 0xF;
|
||||
|
||||
if (status == 0 && response.query.header.ancount > 0)
|
||||
{
|
||||
printf ("\n;; ANSWER SECTION:\n");
|
||||
|
||||
for (size_t i = 0; i < response.query.header.ancount; i++)
|
||||
{
|
||||
dns_answer_t answer = response.answers[i];
|
||||
print_answer_section (&answer);
|
||||
}
|
||||
}
|
||||
|
||||
if (response.query.header.nscount > 0)
|
||||
{
|
||||
printf ("\n;; AUTHORITY SECTION:\n");
|
||||
|
||||
for (size_t i = 0; i < response.query.header.nscount; i++)
|
||||
{
|
||||
dns_answer_t authority = response.authorities[i];
|
||||
print_answer_section (&authority);
|
||||
}
|
||||
}
|
||||
|
||||
if (response.query.header.arcount > 0)
|
||||
{
|
||||
printf ("\n;; ADDITIONAL SECTION:\n");
|
||||
|
||||
for (size_t i = 0; i < response.query.header.arcount; i++)
|
||||
{
|
||||
dns_answer_t additional = response.additionals[i];
|
||||
print_answer_section (&additional);
|
||||
}
|
||||
}
|
||||
|
||||
free (res_bytes);
|
||||
}
|
||||
|
||||
static bool
|
||||
get_args (int argc, char **argv, bool *opendns, bool *xflag)
|
||||
{
|
||||
int ch = 0;
|
||||
while ((ch = getopt (argc, argv, "ox")) != -1)
|
||||
{
|
||||
switch (ch)
|
||||
{
|
||||
case 'o':
|
||||
*opendns = true;
|
||||
break;
|
||||
case 'x':
|
||||
*xflag = true;
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
bool opendns = false;
|
||||
bool xflag = false;
|
||||
|
||||
if (!get_args (argc, argv, &opendns, &xflag))
|
||||
{
|
||||
fprintf (stderr, "ERROR: Show usage\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (!xflag)
|
||||
{
|
||||
phase_one ();
|
||||
}
|
||||
else
|
||||
{
|
||||
not_phase_one (argc, argv);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
@@ -0,0 +1,393 @@
|
||||
#include <arpa/inet.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
|
||||
#include "dns.h"
|
||||
|
||||
#define BUFFER_MAX_SIZE 4096
|
||||
|
||||
// SUGGESTION: Use this file for DNS-related functionality
|
||||
const char *
|
||||
opcode_to_str (uint8_t opcode)
|
||||
{
|
||||
switch (opcode)
|
||||
{
|
||||
case 0:
|
||||
return "QUERY";
|
||||
case 1:
|
||||
return "IQUERY";
|
||||
case 2:
|
||||
return "STATUS";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
const char *
|
||||
status_to_str (uint8_t status)
|
||||
{
|
||||
switch (status)
|
||||
{
|
||||
case 0:
|
||||
return "NOERROR";
|
||||
case 1:
|
||||
return "FORMERR";
|
||||
case 2:
|
||||
return "SERVFAIL";
|
||||
case 3:
|
||||
return "NXDOMAIN";
|
||||
case 4:
|
||||
return "NOTIMP";
|
||||
case 5:
|
||||
return "REFUSED";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
const char *
|
||||
record_to_str (uint16_t record)
|
||||
{
|
||||
switch (record)
|
||||
{
|
||||
case 1:
|
||||
return "A";
|
||||
case 2:
|
||||
return "NS";
|
||||
case 5:
|
||||
return "CNAME";
|
||||
case 6:
|
||||
return "SOA";
|
||||
case 12:
|
||||
return "PTR";
|
||||
case 15:
|
||||
return "MX";
|
||||
case 28:
|
||||
return "AAAA";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
const char *
|
||||
qclass_to_str (uint16_t qclass_bytes)
|
||||
{
|
||||
switch (qclass_bytes)
|
||||
{
|
||||
case 1:
|
||||
return "IN";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t
|
||||
get_record_from_str (const char *record_str)
|
||||
{
|
||||
if (strcasecmp (record_str, "A") == 0)
|
||||
return A;
|
||||
else if (strcasecmp (record_str, "AAAA") == 0)
|
||||
return AAAA;
|
||||
else if (strcasecmp (record_str, "CNAME") == 0)
|
||||
return CNAME;
|
||||
else if (strcasecmp (record_str, "MX") == 0)
|
||||
return MX;
|
||||
else if (strcasecmp (record_str, "NS") == 0)
|
||||
return NS;
|
||||
else if (strcasecmp (record_str, "SOA") == 0)
|
||||
return SOA;
|
||||
else if (strcasecmp (record_str, "PTR") == 0)
|
||||
return PTR;
|
||||
else
|
||||
{
|
||||
fprintf (stderr, "Unsupported record type: %s\n", record_str);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t
|
||||
merge_32 (unsigned char *bytes)
|
||||
{
|
||||
return ((uint32_t)bytes[0] << 24) | ((uint32_t)bytes[1] << 16)
|
||||
| ((uint32_t)bytes[2] << 8) | ((uint32_t)bytes[3]);
|
||||
}
|
||||
|
||||
void
|
||||
recursive_domain_parse (unsigned char *bytes, ssize_t *pointer, char **domain)
|
||||
{
|
||||
if (bytes[*pointer] == 0x00)
|
||||
{
|
||||
(*pointer)++;
|
||||
return;
|
||||
}
|
||||
|
||||
bool is_compressed = (bytes[*pointer] & 0xc0) == 0xc0;
|
||||
|
||||
if (!is_compressed)
|
||||
{
|
||||
size_t len = (size_t)bytes[(*pointer)++];
|
||||
strncat (*domain, (char *)&bytes[*pointer], len);
|
||||
strncat (*domain, ".", 2);
|
||||
*pointer += len;
|
||||
|
||||
recursive_domain_parse (bytes, pointer, domain);
|
||||
}
|
||||
else
|
||||
{
|
||||
ssize_t offset = (bytes[*pointer] << 8 | bytes[*pointer + 1]) & 0x3fff;
|
||||
recursive_domain_parse (bytes, &offset, domain);
|
||||
*pointer += 2;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
parse_domain (unsigned char *bytes, ssize_t *pointer, char **domain)
|
||||
{
|
||||
recursive_domain_parse (bytes, pointer, domain);
|
||||
|
||||
if (strlen (*domain) == 0)
|
||||
{
|
||||
strncat (*domain, ".", 2);
|
||||
}
|
||||
}
|
||||
|
||||
char *
|
||||
parse_ip_addr (unsigned char *bytes, uint16_t rdlen)
|
||||
{
|
||||
char ip_addr[INET6_ADDRSTRLEN];
|
||||
uint8_t ip_ver = rdlen == 4 ? AF_INET : AF_INET6;
|
||||
|
||||
if (inet_ntop (ip_ver, bytes, ip_addr, sizeof (ip_addr)) == NULL)
|
||||
{
|
||||
perror ("inet_ntop");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return strdup (ip_addr);
|
||||
}
|
||||
|
||||
void
|
||||
print_dns_header (dns_header_t *header)
|
||||
{
|
||||
|
||||
// switch endianness
|
||||
uint16_t xid = header->xid;
|
||||
uint16_t flags = header->flags;
|
||||
uint16_t qdcount = header->qdcount;
|
||||
uint16_t ancount = header->ancount;
|
||||
uint16_t nscount = header->nscount;
|
||||
uint16_t arcount = header->arcount;
|
||||
|
||||
// find flags
|
||||
uint8_t qr = (flags >> 15) & 0x1;
|
||||
uint8_t opcode = (flags >> 11) & 0xF;
|
||||
uint8_t aa = (flags >> 10) & 0x1;
|
||||
uint8_t tc = (flags >> 9) & 0x1;
|
||||
uint8_t rd = (flags >> 8) & 0x1;
|
||||
uint8_t ra = (flags >> 7) & 0x1;
|
||||
uint8_t status = (flags >> 0) & 0xF;
|
||||
|
||||
// get string equivalent for opcode and status
|
||||
const char *opcode_str = opcode_to_str (opcode);
|
||||
const char *status_str = status_to_str (status);
|
||||
|
||||
// print first line of header
|
||||
printf (";; ->>HEADER<<- opcode: %s, status: %s, id: %u\n", opcode_str,
|
||||
status_str, xid);
|
||||
|
||||
// print set flags
|
||||
printf (";; flags:");
|
||||
if (qr)
|
||||
printf (" qr");
|
||||
if (aa)
|
||||
printf (" aa");
|
||||
if (tc)
|
||||
printf (" tc");
|
||||
if (rd)
|
||||
printf (" rd");
|
||||
if (ra)
|
||||
printf (" ra");
|
||||
|
||||
// print the last line
|
||||
printf ("; QUERY: %u, ANSWER: %u, AUTHORITY: %u, ADDITIONAL: %u\n\n",
|
||||
qdcount, ancount, nscount, arcount);
|
||||
}
|
||||
|
||||
// print question section
|
||||
void
|
||||
print_question_section (dns_query_t *query)
|
||||
{
|
||||
// print first line
|
||||
printf (";; QUESTION SECTION:\n");
|
||||
|
||||
char *domain = calloc (BUFFER_MAX_SIZE, sizeof (char));
|
||||
ssize_t pointer = 0;
|
||||
parse_domain (query->domain, &pointer, &domain);
|
||||
|
||||
printf (";%s %s %s\n", domain, qclass_to_str (query->qclass),
|
||||
record_to_str (query->qtype));
|
||||
|
||||
free (domain);
|
||||
}
|
||||
|
||||
// print the answer section
|
||||
void
|
||||
print_answer_section (dns_answer_t *answer)
|
||||
{
|
||||
char buf[BUFFER_LENGTH] = { 0 };
|
||||
snprintf (buf, sizeof (buf), "%s %s %u.", qclass_to_str (answer->qclass),
|
||||
record_to_str (answer->qtype), answer->ttl);
|
||||
|
||||
printf ("%-30s %-16s %s\n", answer->domain, buf, answer->rdata);
|
||||
}
|
||||
|
||||
ssize_t
|
||||
parse_dns_header (unsigned char *bytes, dns_header_t *header)
|
||||
{
|
||||
memcpy (header, bytes, sizeof (dns_header_t));
|
||||
|
||||
header->xid = ntohs (header->xid);
|
||||
header->flags = ntohs (header->flags);
|
||||
header->qdcount = ntohs (header->qdcount);
|
||||
header->ancount = ntohs (header->ancount);
|
||||
header->nscount = ntohs (header->nscount);
|
||||
header->arcount = ntohs (header->arcount);
|
||||
|
||||
return sizeof (dns_header_t);
|
||||
}
|
||||
|
||||
ssize_t
|
||||
parse_dns_query (unsigned char *bytes, dns_query_t *query)
|
||||
{
|
||||
ssize_t pointer = parse_dns_header (bytes, &(query->header));
|
||||
|
||||
query->domain = &bytes[pointer];
|
||||
while (bytes[pointer++] != 0x00)
|
||||
;
|
||||
|
||||
query->qtype = (bytes[pointer] << 8) | bytes[pointer + 1];
|
||||
pointer += 2;
|
||||
|
||||
query->qclass = (bytes[pointer] << 8) | bytes[pointer + 1];
|
||||
pointer += 2;
|
||||
|
||||
return pointer;
|
||||
}
|
||||
|
||||
void
|
||||
parse_dns_answer (unsigned char *bytes, ssize_t *pointer, dns_answer_t *answer)
|
||||
{
|
||||
answer->domain = calloc (BUFFER_MAX_SIZE, sizeof (char));
|
||||
parse_domain (bytes, pointer, &(answer->domain));
|
||||
|
||||
answer->qtype = (bytes[*pointer] << 8) | bytes[*pointer + 1];
|
||||
*pointer += 2;
|
||||
|
||||
answer->qclass = (bytes[*pointer] << 8) | bytes[*pointer + 1];
|
||||
*pointer += 2;
|
||||
|
||||
for (int i = 3; i >= 0; i--)
|
||||
{
|
||||
answer->ttl |= bytes[(*pointer)++] << (8 * i);
|
||||
}
|
||||
|
||||
answer->rdlen = (bytes[*pointer] << 8) | bytes[*pointer + 1];
|
||||
*pointer += 2;
|
||||
|
||||
answer->rdata = calloc (BUFFER_MAX_SIZE, sizeof (char));
|
||||
|
||||
if (answer->qtype == A || answer->qtype == AAAA)
|
||||
{
|
||||
answer->rdata = parse_ip_addr (&bytes[*pointer], answer->rdlen);
|
||||
*pointer += answer->rdlen;
|
||||
return;
|
||||
}
|
||||
|
||||
if (answer->qtype == NS || answer->qtype == CNAME || answer->qtype == PTR)
|
||||
{
|
||||
parse_domain (bytes, pointer, &(answer->rdata));
|
||||
return;
|
||||
}
|
||||
|
||||
if (answer->qtype == MX)
|
||||
{
|
||||
uint16_t preference = (bytes[*pointer] << 8) | bytes[*pointer + 1];
|
||||
*pointer += 2;
|
||||
|
||||
char *exchange = calloc (BUFFER_LENGTH, sizeof (char));
|
||||
parse_domain (bytes, pointer, &exchange);
|
||||
|
||||
snprintf (answer->rdata, BUFFER_MAX_SIZE, "%u %s", preference, exchange);
|
||||
|
||||
free (exchange);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (answer->qtype == SOA)
|
||||
{
|
||||
char *mname = calloc (BUFFER_LENGTH, sizeof (char));
|
||||
parse_domain (bytes, pointer, &mname);
|
||||
|
||||
char *rname = calloc (BUFFER_LENGTH, sizeof (char));
|
||||
parse_domain (bytes, pointer, &rname);
|
||||
|
||||
uint32_t serial = merge_32 (&bytes[*pointer]);
|
||||
uint32_t refresh = merge_32 (&bytes[*pointer + 4]);
|
||||
uint32_t retry = merge_32 (&bytes[*pointer + 8]);
|
||||
uint32_t expire = merge_32 (&bytes[*pointer + 12]);
|
||||
uint32_t minimum = merge_32 (&bytes[*pointer + 16]);
|
||||
|
||||
*pointer += 20;
|
||||
|
||||
snprintf (answer->rdata, BUFFER_MAX_SIZE, "%s %s %u %u %u %u %u", mname,
|
||||
rname, serial, refresh, retry, expire, minimum);
|
||||
|
||||
free (mname);
|
||||
free (rname);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
ssize_t
|
||||
parse_dns_response (unsigned char *bytes, ssize_t query_len,
|
||||
dns_response_t *response)
|
||||
{
|
||||
ssize_t pointer = parse_dns_query (bytes, &(response->query));
|
||||
|
||||
response->answers
|
||||
= calloc (response->query.header.ancount, sizeof (dns_answer_t));
|
||||
|
||||
for (size_t i = 0; i < response->query.header.ancount; i++)
|
||||
{
|
||||
dns_answer_t answer = { 0 };
|
||||
parse_dns_answer (bytes, &pointer, &answer);
|
||||
memcpy (&(response->answers[i]), &answer, sizeof (dns_answer_t));
|
||||
}
|
||||
|
||||
response->authorities
|
||||
= calloc (response->query.header.nscount, sizeof (dns_answer_t));
|
||||
|
||||
for (size_t i = 0; i < response->query.header.nscount; i++)
|
||||
{
|
||||
dns_answer_t authoritative = { 0 };
|
||||
parse_dns_answer (bytes, &pointer, &authoritative);
|
||||
memcpy (&(response->authorities[i]), &authoritative,
|
||||
sizeof (dns_answer_t));
|
||||
}
|
||||
|
||||
response->additionals
|
||||
= calloc (response->query.header.arcount, sizeof (dns_answer_t));
|
||||
|
||||
for (size_t i = 0; i < response->query.header.arcount; i++)
|
||||
{
|
||||
dns_answer_t additional = { 0 };
|
||||
parse_dns_answer (bytes, &pointer, &additional);
|
||||
memcpy (&(response->additionals[i]), &additional, sizeof (dns_answer_t));
|
||||
}
|
||||
|
||||
return pointer;
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
#ifndef __cs361_dns_h__
|
||||
#define __cs361_dns_h__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// Maximum buffer lengths allows
|
||||
#define BUFFER_LENGTH 512
|
||||
|
||||
// Structure of the bytes for a DNS header
|
||||
typedef struct
|
||||
{
|
||||
uint16_t xid;
|
||||
uint16_t flags;
|
||||
uint16_t qdcount;
|
||||
uint16_t ancount;
|
||||
uint16_t nscount;
|
||||
uint16_t arcount;
|
||||
} dns_header_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
dns_header_t header;
|
||||
unsigned char *domain;
|
||||
uint16_t qtype;
|
||||
uint16_t qclass;
|
||||
} dns_query_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char *domain;
|
||||
uint16_t qtype;
|
||||
uint16_t qclass;
|
||||
uint32_t ttl;
|
||||
uint16_t rdlen;
|
||||
char *rdata;
|
||||
} dns_answer_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
dns_query_t query;
|
||||
dns_answer_t *answers;
|
||||
dns_answer_t *authorities;
|
||||
dns_answer_t *additionals;
|
||||
} dns_response_t;
|
||||
|
||||
// Supported record types
|
||||
#define A 1
|
||||
#define NS 2
|
||||
#define CNAME 5
|
||||
#define SOA 6
|
||||
#define PTR 12
|
||||
#define MX 15
|
||||
#define AAAA 28
|
||||
|
||||
void print_dns_header (dns_header_t *);
|
||||
void print_question_section (dns_query_t *query);
|
||||
void print_answer_section (dns_answer_t *answer);
|
||||
ssize_t parse_dns_query (unsigned char *bytes, dns_query_t *query);
|
||||
ssize_t parse_dns_response (unsigned char *bytes, ssize_t query_len,
|
||||
dns_response_t *response);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,6 @@
|
||||
#ifndef __cs361_port_utils_h__
|
||||
#define __cs361_port_utils_h__
|
||||
|
||||
char *get_port (void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,53 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "dns.h"
|
||||
#include "port_utils.h"
|
||||
#include "utils.h"
|
||||
|
||||
// SUGGESTION: Use this file for any network-related functionality, such as
|
||||
// sending or receiving messages.
|
||||
|
||||
void
|
||||
ptr_ip (char *dst, size_t dst_size, const char *ip)
|
||||
{
|
||||
char tmp[64];
|
||||
strncpy (tmp, ip, sizeof (tmp));
|
||||
tmp[sizeof (tmp) - 1] = '\0';
|
||||
|
||||
char *octets[4];
|
||||
char *token = strtok (tmp, ".");
|
||||
int count = 0;
|
||||
while (token && count < 4)
|
||||
{
|
||||
octets[count++] = token;
|
||||
token = strtok (NULL, ".");
|
||||
}
|
||||
|
||||
if (count != 4)
|
||||
{
|
||||
fprintf (stderr, "Invalid PTR IP");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
snprintf (dst, dst_size, "%s.%s.%s.%s.in-addr.arpa.", octets[3], octets[2],
|
||||
octets[1], octets[0]);
|
||||
}
|
||||
|
||||
void
|
||||
print_byte_block (uint8_t *bytes, size_t length)
|
||||
{
|
||||
printf (" ");
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
printf ("%02x", bytes[i]);
|
||||
if (i == length - 1)
|
||||
printf ("\n");
|
||||
else if ((i + 1) % 16 == 0)
|
||||
printf ("\n ");
|
||||
else if ((i % 2) != 0)
|
||||
printf (" ");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifndef __cs361_utils_h__
|
||||
#define __cs361_utils_h__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void ptr_ip (char *dst, size_t dst_size, const char *ip);
|
||||
void print_byte_block (uint8_t *, size_t);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user