-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns.h
79 lines (73 loc) · 2.01 KB
/
dns.h
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
#include <stdint.h>
/* DNS header structure. Most of the message is variable
size, which means we can't declare them in this C struct */
struct dns_hdr {
uint16_t id,flags,q_count,a_count,auth_count,other_count;
// variable number of questions
// variable number of answer resource records
// variable number of authoritative server records
// variable number of other resource records
} __attribute__((packed));
struct dns_rr {
// first a variable sized name, then
uint16_t type;
uint16_t class;
uint32_t ttl;
uint16_t datalen;
// and then a variable sized data field
} __attribute__((packed));
/* converts between the normal www.cs.uic.edu format to
the DNS-style 3www2cs3uic3edu format. Returns the
length of the string written to dns_name. */
int to_dns_style(char* str_name, uint8_t* dns_name)
{
int part_len=0;
int i;
for(i=0;i<strlen(str_name);i++) {
if(str_name[i]!='.') {
dns_name[i+1]=str_name[i];
part_len++;
}
else {
dns_name[i-part_len]=part_len;
part_len=0;
}
}
dns_name[strlen(str_name)-part_len]=part_len;
dns_name[strlen(str_name)+1]=0;
return strlen(str_name)+2;
}
/* converts between DNS-style format to normal host name format.
Also supports DNS-style name compression: message should point to
the first byte of the DNS message.
Returns the number of bytes of dns_name read.
*/
int from_dns_style(uint8_t *message, uint8_t* dns_name, char* str_name) {
uint8_t part_remainder=0;
int len=0;
int return_len=0;
uint8_t* orig_name = dns_name;
while(*dns_name) {
if(part_remainder==0) {
// this condition checks for message compression, see RFC 1035 4.1.4
if((*dns_name)>=0xc0) {
if(return_len==0)
return_len = (dns_name-orig_name)+2;
dns_name=message+(((*dns_name)&0x3f)<<8)+*(dns_name+1);
continue;
}
else {
part_remainder=*dns_name;
if(len>0)
str_name[len++]='.';
}
}
else {
str_name[len++]=*dns_name;
part_remainder--;
}
dns_name++;
}
str_name[len]=0;
return (return_len?return_len:dns_name-orig_name+1);
}