Felipe Rodriguez
Felipe Rodriguez

Reputation: 23

Parse a SRV DNS record that was created in djbdns format using python

I'm parsing an DNS zone file written in TinyDNS format (djbdns) and I'm having problems decoding SRV records.

I'm so far able to parse everything correctly from the SRV record but the answer which contain a set of octal numbers for the priority, weight and port (normal values for a SRV DNS Record) below is an example that can be generated from here

:_sip._tcp.example.com:33:\000\012\000\144\023\304\003pbx\007example\003com\000:86400

The part that I'm having issues is the DNS answer:

\000\012\000\144\023\304\003pbx\007example\003com\000

First 24 chars

\000\012\000\144\023\304

Rest of chars

\003pbx\007example\003com\000

The first 24 chars contains the priority, weight and port of the service that the DNS will be using, from the 24th chart to the end of the string is the target host that is providing the service. So parsing the target is easy, each Octal set before the string is the number of chars to expect between and is removing the periods.

Priority, Weight and Port accepts values from 0 - 65535 which transforms the the values into 2 sets of 255 i.e \000\144 = 10 decimal but if the number is more than 256 bits then it uses the second set i.e \023\304 = 5060 at this point i'm blocked i have no idea what they are doing, converting each set individually or together is not giving me the right decimal number. I know i'm close but is super blocked. I'm using the default int method to transform from octal to dec which is fairly easy.

octal = '144'
dec   = int(octal, 8)

After I parse it it should be something like this (obviously the spaces is something that I'm doing in my code to concat values.

10 100 5060 pbx.example.com

Any guidance or hint guys would be appreciated!

Upvotes: 1

Views: 502

Answers (1)

dado
dado

Reputation: 74

Think of \023\304 as high and low parts. You need to bitshift the high part.

>>> high = '023'
>>> low = '304'
>>> high_dec = int(high, 8)
>>> low_dec = int(low, 8)
>>> (high_dec << 8) + low_dec
5060

Upvotes: 2

Related Questions