|
-
Sep 17th, 2009, 04:09 PM
#1
[RESOLVED] Bits, Bytes and Boomerangs (without the boomerangs...)
Sorry, I like my thread titles with alliteration 
Anyway... now that you think I'm weird, lets get down to business.
As per this thread: http://www.vbforums.com/showthread.php?t=584412 I am trying to perform a DNS query in code. Having spent the last few hours trying to get my head around this and failing miserably I figured I would see if anyone on here could point me in the right direction.
The DNS RFC (http://www.faqs.org/rfcs/rfc1035.html) tells me that the header of each UDP packet that contains a DNS query should look like this:
Code:
The header contains the following fields:
1 1 1 1 1 1
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ID |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|QR| Opcode |AA|TC|RD|RA| Z | RCODE |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| QDCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ANCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| NSCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ARCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
So we have got 16 bits (which I believe is 2 bytes) available for each section of the header, but as you can see the second section of the header is not just a nice full 2 byte value - its several individual bit values (with a couple of 4 bit values as well) and its this that is tripping me up at the moment.
I'm not great with Bytes, let alone Bits, so I'm pretty stuck with trying to work out how I can 'set' the relevant Bits and then combine the whole thing to make a Byte array that is 12 Bytes long.
I'm not even sure if I am setting the values correctly for the sections of the header that are 2 full bytes. Here is what I am doing at the moment, I'm using a List(Of Byte) with the intention of just calling ToArray when I have finished building it... because although the header is 12 bytes long, the rest of the packet has a variable length so I thought this would be easier than lots of ReDiming of a Byte array but feel free to tell me if thats a stupid idea 
vb.net Code:
Dim UDPClt As New UdpClient(53, AddressFamily.InterNetwork)
Dim QuestionBytes As New List(Of Byte)
'<--Construct Header-->
'ID
QuestionBytes.Add(10)
QuestionBytes.Add(10)
'Parameters
'-- This is the part that I need help with the most --
QuestionBytes.Add(some bits!)
'QDCOUNT
QuestionBytes.Add(0)
QuestionBytes.Add(1)
'ANCOUNT
QuestionBytes.Add(0)
QuestionBytes.Add(0)
'NSCOUNT
QuestionBytes.Add(0)
QuestionBytes.Add(0)
'ARCOUNT
QuestionBytes.Add(0)
QuestionBytes.Add(0)
'</--Construct Header-->
Is that even the right way to add bytes to a byte array - I mean can you add an item to a Byte array just using an Integer as the input or am I doing it wrong and I need to use some conversion? I'm surprised I didnt need to use CByte to keep Option Strict happy but maybe thats just because Bytes are still a bit of a mystery to me... (hopefully this will be a good learning experience).
I'm currently looking into the BitArray class but not getting anywhere fast and not even sure if thats what I should be doing so any pointers would be more than welcome!
Also, the other 2 byte sections in the header are defined as "An unsigned 16 bit Integer" but in the examples of DNS queries I have seen these fields are just set to 01 or 00 so can I get away with just writing two bytes as I am doing in my example code above? Does that equate to the same as a 16 bit unsigned integer?
Thanks 
Chris
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|