I have a question about DNS query Format ....
I know this structure as the structure of DNS query header ...
typedef struct _DNS_HEADER {
unsigned short id; // identification number
unsigned char qr :1; // query/response flag
unsigned char opcode :4; // purpose of message
unsigned char aa :1; // authoritative answer
unsigned char tc :1; // truncated message
unsigned char rd :1; // recursion desired
unsigned char ra :1; // recursion available
unsigned char z :1; // its z! reserved
unsigned char ad :1; // authenticated data
unsigned char cd :1; // checking disabled
unsigned char rcode :4; // response code
unsigned short q_count; // number of question entries
unsigned short ans_count; // number of answer entries
unsigned short auth_count; // number of authority entries
unsigned short add_count; // number of resource entries
} DNS_HEADER;
+ question query and ...
but there is a problem here:
I created a socket socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP)
and i created a query request:
id = htons(10000);
qr = 0;
opcode = 0
aa = 0
tc = 0
rd = 1
ra = 0
z = 0
ad = 0
cd = 0
q_count = 1
ans_count = 0
auth_count = 0
add_count = 0
and my question ->
3,'w','w','w',4,'s','i','t','e',3,'c','o','m',0
TYPE = htons(1)
CLASS = htons(1)
I sent this request to my special DNS server (bind) and I think there was a problem. Sometimes DNS server didn't responded me and even on response, it wasn't a true response (everything was 0 and nothing received except same header (12-BYTE))
I searched and I found out that there is a problem with my DNS header and it Must be like this ->
typedef struct _DNS_HEADER {
unsigned short id; // identification number
unsigned char rd :1; // recursion desired
unsigned char tc :1; // truncated message
unsigned char aa :1; // authoritative answer
unsigned char opcode :4; // purpose of message
unsigned char qr :1; // query/response flag
unsigned char rcode :4; // response code
unsigned char cd :1; // checking disabled
unsigned char ad :1; // authenticated data
unsigned char z :1; // its z! reserved
unsigned char ra :1; // recursion available
unsigned short q_count; // number of question entries
unsigned short ans_count; // number of answer entries
unsigned short auth_count; // number of authority entries
unsigned short add_count; // number of resource entries
} DNS_HEADER;
something about little-big endian. I changed my structure to this structure and I sent request and DONE - everything worked and there wasn't any problem, but this is my problem: I don't get it what this is this. Yes, I read about little-big endian a little but still i don't get it:
- Why haven't other websites that teaches about DNS format mentioned this? They teach with the first structure.
- What is this? Why we must change the higher 8 bits of the second 16 bits to be the lower 8 bits of the second 16 bits?
#ifdef
's around the bitfields to place them in the correct order. This particularDNS_HEADER
won't work everywhere as @AnttiHaapala noticed.unsigned char
can be used as a bitfield type.