-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1
279 lines (238 loc) · 7.64 KB
/
1
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <netdb.h>
uint8_t *processName(uint8_t *bstart, uint8_t *bcur, char *name);
void name_encode(char* name, char* name_encoded);
struct UDP_header
{
uint16_t id; // identification number
uint8_t rd :1; // recursion desired
uint8_t tc:1; // truncated message
uint8_t aa :1; // authoritive answer
uint8_t opcode :4; // purpose of message
uint8_t qr :1; // query/response flag
uint8_t rcode :4; // response code
uint8_t cd :1; // checking disabled
uint8_t ad :1; // authenticated data
uint8_t z :1; // its z! reserved
uint8_t ra :1; // recursion available
uint16_t q_count; // number of question entries
uint16_t ans_count; // number of answer entries
uint16_t auth_count; // number of authority entries
uint16_t add_count; // number of resource entries
};
/*struct UDP_header{
unsigned int id : 16;
unsigned int qr : 1; *//*query or response*/
/* unsigned int Opcode : 4;
unsigned int aa : 1;
unsigned int tc : 1;
unsigned int rd : 1;
unsigned int ra : 1;
unsigned int z : 1;
unsigned int ad : 1;
unsigned int cd : 1;
unsigned int rcode : 4;
unsigned int qcount;
unsigned int acount;
unsigned int nscount;
unsigned int arcount;
};*/
struct DNS_query{
unsigned short type;
unsigned short class;
};
typedef struct{
char *name;
struct DNS_query *q;
}QUERY;
struct response_fields{
uint16_t type;
uint16_t class;
uint32_t ttl;
uint16_t dl;
};
struct response{
char *name;
struct response_fields *rf;
char *res_data;
};
int main(int argc, char **argv){
char name_dotted[500];
char buff[65536], *name, buff_rec[65536], *res_data;
uint8_t *reader;
struct response_fields *res_fields;
struct DNS_query *question;
int i, bytes, bytes_rec, resource_data_length;
int sockfd;
struct UDP_header *uheader;
struct sockaddr_in dest_address;
char ip_text[INET_ADDRSTRLEN];
/*printf("entered hostname: %s\n", argv[1]);*/
/* bzero(buff, 65536);*/
uheader = (struct UDP_header *) buff;
uheader->id = getpid();
uheader->qr = 0;
uheader->opcode = 0;
uheader->aa = 0;
uheader->tc = 0;
uheader->rd = 1; /*query is recursive*/
uheader->ra = 0;
uheader->z = 0;
uheader->ad = 0;
uheader->cd = 0;
uheader->rcode = 0;
uheader->q_count = htons(1);
uheader->ans_count = 0;
uheader->auth_count = 0;
uheader->add_count = 0;
/*create a UDP socket*/
sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(sockfd < 0){
perror("error creating socket\n");
}
/* dest_address = (struct sockaddr_in *) &address;*/
bzero(&dest_address, sizeof(dest_address));
dest_address.sin_family = AF_INET;
dest_address.sin_port = htons(53);
dest_address.sin_addr.s_addr = inet_addr("208.67.222.222");
if (argc != 2){
printf("the name wasn't entered\n");
} else {
/* printf("%s\n", argv[1]);*/
}
/* printf("sin_port: %u\n", dest_address->sin_port); */
inet_ntop(AF_INET, &(dest_address.sin_addr), ip_text, INET_ADDRSTRLEN);
/*!!!*/
name = &buff[sizeof(struct UDP_header)];
name_encode(argv[1], name);
question = (struct DNS_query *) &buff[sizeof(struct UDP_header) + strlen((const char *) name) + 1];
/*query type*/
question->type = htons(1);
question->class = htons(1);
/* printf("%s\n", name);*/
/* printf("%d\n", question->type);*/
/*printf("sockfd: %i\n", sockfd);*/
/* printf("name: %s\n", name);*/
bytes = sendto(sockfd, buff, sizeof(struct UDP_header) + sizeof(struct DNS_query) + strlen((const char *) name) + 1, 0, (struct sockaddr *) &dest_address, sizeof(dest_address));
printf("bytes sent: %d\n", bytes);
/*processName(&uheader, hostname);*/
if (bytes < -1){
perror("sendto error\n");
return -1;
}
bzero(buff_rec, 65536);
bytes_rec = recvfrom(sockfd, &buff_rec, 65536, 0, NULL, 0);
if (bytes_rec < 0){
perror("receive error\n");
}
printf("bytes in response: %d\n", bytes_rec);
uheader = (struct UDP_header *) buff_rec;
reader = (unsigned char *) &buff_rec[(sizeof(struct UDP_header) + strlen((const char *) name) + 1 + sizeof(struct DNS_query))];
printf("The number of questions: %d\n", ntohs(uheader->q_count));
printf("The number of answers: %d\n", ntohs(uheader->ans_count));
/* printf("sizeof(unsigned int) = %lu sizeof(unsigned short) = %lu\n", sizeof(unsigned int), sizeof(unsigned short));
printf("sizeof(unsigned char) = %lu sizeof(char) = %lu\n", sizeof(unsigned char), sizeof(char));
*/
/* printf("sizeof(uint8_t)=%lu, sizeof(char)=%lu\n", sizeof(uint8_t), sizeof(char));*/
bzero(name_dotted, 500);
reader = processName ((unsigned char*) buff_rec, reader, name_dotted);
res_fields = (struct response_fields *) reader;
resource_data_length = ntohs(res_fields->dl);
/* printf("name dotted = %s, length of dotted name = %lu\n", name_dotted, strlen(name_dotted));
printf("type of response = %d\n", ntohs(res_fields->type));*/
printf("resource data length: %d\n", resource_data_length);
reader = reader + sizeof(struct response_fields);
/* res_data = malloc(resource_data_length);*/
/*printf("response data: %s\n", reader); */
struct sockaddr_in address;
char *hraddress;
char addr[resource_data_length];
for (i=0; i<resource_data_length; i++){
addr[i] = *(reader + i);
}
address.sin_addr.s_addr = (unsigned long *) addr;
hraddress = inet_ntoa(address.sin_addr);
close(sockfd);
/* free(res_data);*/
return 0;
}
/* Processes one string in a DNS resource record.
*
* bstart (in): pointer to the start of the DNS message (UDP payload)
* bcur (in): pointer to the currently processed position in a message.
This should point to the start of compressed or uncompressed
name string.
* name (out): buffer for storing the name string in dot-separated format
*
* returns: updated position of bcur, pointing to the next position
* following the name
*/
uint8_t *processName(uint8_t *bstart, uint8_t *bcur, char *name)
{
uint8_t *p = bcur;
char strbuf[80];
char *strp;
int compressed = 0;
name[0] = 0;
do {
strp = strbuf;
if ((*p & 0xc0) == 0xc0) { /* first two bits are set =>
compressed format */
uint16_t offset = (*p & 0x3f);
offset = (offset << 8) + *(p+1);
p = bstart + offset; /* move the read pointer to the
offset given in message */
if (!compressed) bcur += 2; /* adjustment of bcur must
only be done once, in
case there are multiple
nested pointers in msg */
compressed = 1;
} else if (*p > 0) {
// strbuf contains one element of name, not full name
memcpy(strbuf, p+1, *p);
strp += *p;
p += *p + 1;
if (!compressed)
bcur = p; /* adjustment of bcur based on string
string length is only done if it
was not compressed, otherwise it
is assumed to be 16 bits always */
*strp = '.';
*(strp+1) = 0;
strcat(name, strbuf);
}
} while (*p > 0);
if (!compressed) bcur++; /* compensate for trailing 0 (unless name was
compressed) */
return bcur;
}
/* function that does conversion www.gmail.com -> */
void name_encode(char* name, char* name_encoded){
char *ptr;
int diff, i;
ptr = strchr(name, '.');
diff = ptr - name;
name_encoded[0] = diff;
for (i=0; i<strlen(name); i++){
name_encoded[i+1] = name[i];
if (name[i] == '.'){
ptr = strchr(&name[i+1], '.');
if (ptr == NULL){
printf("ptr null\n");
ptr = strchr(&name[i], '\0');
}
diff = ptr - &name[i] - 1;
name_encoded[i+1] = diff;
}
}
name_encoded[i+1] = 0;
/*printf ("%s, strlen=%lu\n", name_encoded, strlen(name_encoded));*/
}