My question pertains to constructing a DNS header for a DNS query as defined by RFC 1035. The RFC states every DNS messages is in the following format:
+----------------------------------+
| HEADER |
+----------------------------------+
| QUESTION |
+----------------------------------+
| ANSWER |
+----------------------------------+
| AUTHORITY |
+----------------------------------+
| ADDITIONAL |
+----------------------------------+
And That the header field/section follows this format:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ID |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|QR| OPCODE |AA|TC|RD|RA| Z | RCODE |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| QDCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ANCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| NSCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ARCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
How would I define a header struct/object to hold the values for the second row? The trouble i'm seeing is setting specifics bits in this field for the different situations. The only designs i've come up with that still compile and don't violate the RFC's definition require a lot of bit manipulation and bit masking.
These are the two implementations I have considered however I wouldn't consider either an ideal solution.
struct messageHeader{
short ID;
//Start of second row which is broken into 2 bytes
unsigned int QR : 1;
unsigned int OPCODE : 4;
unsigned int AA : 1;
unsigned int TC : 1;
unsigned int RD : 1;
//Start of Second byte needed for row 2
unsigned int RA : 1;
unsigned int Z : 3;
unsigned int RCODE : 4;
//End Second row
short QDCNT;
short ANCNT;
short NSCNT;
short ARCNT;
};//End Message Header Struct
I've never worked with bit fields like this before so I don't fully understand how I would pack the bits into this...all I know is it compiled without errors even using the -pedantic flag and that kinda scares me.
The second is:
struct messageHeader{
short ID;
short row2;
short QDCNT;
short ANCNT;
short NSCNT;
short ARCNT;
};/End Message Header Struct
For the second option I would use the << operator to shift bits into the correct location for the short being used for row 2. This seems like an awful lot of bit manipulation and bit masking to me especially for all of the different combinations these codes could be.