-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.c
192 lines (159 loc) · 5.71 KB
/
client.c
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include "dns.h"
#include "request.h"
#include "response.h"
#include "query.h"
#define DEFAULT_PORT 53
#define DEFAULT_TIMEOUT 5
#define DEFAULT_MAX_RETRIES 3
#define MAX_IP_STRING_SIZE 16
static void format_ip_address(uint32_t ip_address, char *buffer);
static char *get_authority_string(int is_authoritative);
int main(int argc, char **argv) {
char server[MAX_DOMAIN_LENGTH + 1], domain[MAX_DOMAIN_LENGTH + 1];
char error_message[ERROR_BUFFER + 1], ip_buffer[MAX_IP_STRING_SIZE];
int i, request_id, packet_size, answer_count;
int port, timeout, retries, arg_counter, optional_argc, request_q_type;
void *request_buffer, *response_buffer;
struct dns_response *responses;
*error_message = 0;
/* Use the current time as a seed for the random number generator. */
srand(time(0));
/* Verify that enough arguments were passed in */
if (argc < 3) {
printf("USAGE: %s [-p <port>] [-t <timeout>] [-i <max-retries>] "
"[-ns|-mx] @<server> <name>\n", argv[0]);
exit(1);
}
/* set defaults for optional arguments in case none are specified */
port = DEFAULT_PORT;
timeout = DEFAULT_TIMEOUT;
retries = DEFAULT_MAX_RETRIES;
request_q_type = DNS_A_RECORD;
// counter that will be used to differntiate
// vals from option flags
optional_argc = argc - 2;
arg_counter = 1;
// while we have enough args and the count is less
// than the number of REQUIRED args
while(arg_counter < optional_argc) {
// handle timeout arg
if (strcmp("-t", argv[arg_counter]) == 0) {
if (arg_counter + 1 < optional_argc) {
timeout = atoi(argv[++arg_counter]);
} else {
fprintf(stderr, "ERROR must specify a timeout value with -t\n");
exit(1);
}
// handle max retries arg
} else if (strcmp("-i", argv[arg_counter]) == 0) {
if (arg_counter + 1 < optional_argc) {
retries = atoi(argv[++arg_counter]);
} else {
fprintf(stderr, "ERROR must specify a retry value with -i\n");
exit(1);
}
// handle port arg
} else if (strcmp("-p", argv[arg_counter]) == 0) {
if (arg_counter + 1 < optional_argc) {
port = atoi(argv[++arg_counter]);
} else {
fprintf(stderr, "ERROR must specify a port with -p\n");
exit(1);
}
} else if (strcmp("-ns",argv[arg_counter]) == 0) {
request_q_type = DNS_NS_RECORD;
} else if (strcmp("-mx",argv[arg_counter]) == 0) {
request_q_type = DNS_MX_RECORD;
}
++arg_counter;
}
if (strlen(argv[argc - 2]) > MAX_DOMAIN_LENGTH ||
strlen(argv[argc - 1]) > MAX_DOMAIN_LENGTH) {
fprintf(stderr, "ERROR max length of server and domain is %d\n",
MAX_DOMAIN_LENGTH);
exit(1);
}
/* Use arg list to set REQUIRED variables. If the server name starts with an
@, don't include it when copying into the buffer. */
if (*argv[argc - 2] == '@') {
strncpy(server, argv[argc - 2] + 1, MAX_DOMAIN_LENGTH);
} else {
strncpy(server, argv[argc - 2], MAX_DOMAIN_LENGTH);
}
strncpy(domain, argv[argc - 1], MAX_DOMAIN_LENGTH);
/* Build the DNS request packet for the supplied domain name. */
request_buffer = build_dns_request_packet(domain, &packet_size, &request_id,
request_q_type, error_message);
if (request_buffer == 0) {
fprintf(stderr, "ERROR %s\n", error_message);
exit(1);
}
/* Send the request packet and wait for a response from the server. */
response_buffer = query_dns_server(request_buffer, &packet_size, server,
port, timeout, retries, error_message);
free_dns_request_packet(request_buffer);
if (response_buffer == 0) {
fprintf(stderr, "ERROR %s\n", error_message);
exit(1);
}
/* Parse the response from the server. */
responses = parse_dns_response(response_buffer, packet_size, request_id,
domain, &answer_count, error_message);
free_response_buffer(response_buffer);
/* If a null value was returned, it could either mean there was an error or
the domain name was not found. Check the error_message buffer to see
if it contains any data. */
if (responses == 0) {
if (*error_message != 0) {
fprintf(stderr, "ERROR %s\n", error_message);
exit(1);
} else {
fprintf(stdout, "NOTFOUND\n");
exit(0);
}
}
for (i = 0; i < answer_count; ++i) {
switch(responses[i].response_type) {
case DNS_A_RECORD:
format_ip_address(responses[i].ip_address, ip_buffer);
fprintf(stdout, "IP\t%s\t%d\t%s\n", ip_buffer,
responses[i].cache_time, get_authority_string(responses[i].authoritative));
break;
case DNS_NS_RECORD:
fprintf(stdout, "NS\t%s\t%d\t%s\n", responses[i].name,
responses[i].cache_time, get_authority_string(responses[i].authoritative));
break;
case DNS_CNAME_RECORD:
fprintf(stdout, "CNAME\t%s\t%d\t%s\n", responses[i].name,
responses[i].cache_time, get_authority_string(responses[i].authoritative));
break;
case DNS_MX_RECORD:
fprintf(stdout, "MX\t%s\t%d\t%d\t%s\n", responses[i].name,
responses[i].preference, responses[i].cache_time,
get_authority_string(responses[i].authoritative));
break;
default:
fprintf(stderr, "ERROR unknown response type\n");
break;
}
}
free(responses);
return 0;
}
/* Formats a 32-bit IP address into a dotted quad string and
copies it into the given buffer. */
static void format_ip_address(uint32_t ip_address, char *buffer) {
uint8_t *segments = (uint8_t *)&ip_address;
sprintf(buffer, "%d.%d.%d.%d", segments[3], segments[2],
segments[1], segments[0]);
}
static char *get_authority_string(int is_authoritative) {
return is_authoritative ? "auth" : "nonauth";
}