Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Making DNS & NTP requests

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM Project Blog
View previous topic :: View next topic  
Author Message
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1198
Location: SYDNEY

australia.gif
PostPosted: Mon May 23, 2016 11:54 am    Post subject: Making DNS & NTP requests Reply with quote

Instead of putting this in the web server blog
I thought I would put it separately so it can be fully explained
Both Ben and SIX1 have made code to do this
Ben for the ENC28J60
SIX1 for wiznet
But they did not break it down to a byte by byte explanation
and with some work so far I find I can use less memory to do it
I also will try to make it more portable

We need to make a DNS request to get an IP "A" record from the time server pool
then when we have that we can then ask for the time NTP request

First a break down of a DNS request for www.mcselec.com

Code:

'
'  DNS request for www.mcselec.com
'                               w  w  w      m  c  s  e  l  e  c     c  o   m
'  X Y 1 0 0 1 0 0 0 0 0 0 [3] 77 77 77 [7] 6D 63 73 65 6C 65 63 [3] 63 6F 6D [0] 01 01
'                          [3] for 3 characters   [7] for 7 characters   [3] for 3 characters  [0] for no characters
'                                   these will change depending on how many characters in the URL name
'    X & Y are random numbers to identify the message
'     last two 01 01   IP "A" record
'                   0F 01  MX record
'
'
' So the  bytes are
  '------------------------------
  '16 bit random number ,bytes 1 & 2
  'byte 3 &H01
  'byte 4 &H00
  'byte 5 &H00
  'byte 6 &H01
  'byte 7 &H00
  'byte 8 &H00
  'byte 9 &H00
  'byte 10 &H00
  'byte 11 &H00
  'byte 12 &H00
  'byte 13 &H03   3 characters
  'byte 14 &H77   w
  'byte 15 &H77   w
  'byte 16 &H77   w
  'byte 17 &H07    7 characters
  'byte 18 &H6D   m
  'byte 19 &H63   c
  'byte 20 &H73   s
  'byte 21 &H65   e
  'byte 22 &H6C   l
  'byte 23 &H65   e
  'byte 24 &H63   c
  'byte 25 &H03   3 characters
  'byte 26 &H63   c
  'byte 27 &H6F   o
  'byte 28 &H6D  m
  'byte 29 &H00   0 characters
  'byte 30 &H01   "A"  record
  'byte 31 &H01    for Internet
 


Regards Paul


Last edited by Paulvk on Tue May 24, 2016 10:41 am; edited 2 times in total
Back to top
View user's profile
bzijlstra

Bascom Ambassador



Joined: 30 Dec 2004
Posts: 1179
Location: Tilburg - Netherlands

netherlands.gif
PostPosted: Mon May 23, 2016 3:35 pm    Post subject: Nice work Reply with quote

Thanks for sharing Paul!
Back to top
View user's profile Visit poster's website
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1198
Location: SYDNEY

australia.gif
PostPosted: Tue May 24, 2016 10:45 am    Post subject: Reply with quote

Now a program that builds the request
then prints it to a terminal in HEX to compare it to the example
I used an Arduino M2560
Note 1194 bytes of flash without the print to terminal

Next step is to put it into a function just wish we had local arrays!

Regards Paul


Code:


$regfile = "m2560def.dat"
$crystal = 16000000
$baud = 19200
$hwstack = 100
$swstack = 100
$framesize = 100


Dim Buffer(200) As Byte
Dim Pos As Byte
Dim Url As String * 40
Dim Work(15) As String * 20
Dim Char As String * 1
Dim Lenurl As Byte
Dim W As Byte
Dim X As Byte
Dim Y As Byte
Dim Z As Byte


  Url = "www.mcselec.com"                                   'use example url

'
'  DNS request for www.mcselec.com
'                                                w  w   w        m   c    s   e   l     e    c        c  o   m
'  X Y 0 1 0 1 0 0 0 0 0 0 [3] 77 77 77 [7] 6D 63 73 65 6C 65 63 [3] 63 6F 6D [0] 01 01
'53360100000100000000000003777777076D6373656C656303636F6D000101

'                                   [3] for 3 characters   [7] for 7 characters     [3] for 3 characters  [0] for no characters
'                                   these will change depending on how many characters in the URL name
'    X & Y are random numbers to identify the message
'     last two 01 01   IP "A" record
'                   0F 01  MX record
'
'
' So the  bytes are
  '------------------------------
  '16 bit random number ,bytes 1 & 2
  'byte 3 &H01
  'byte 4 &H00
  'byte 5 &H00
  'byte 6 &H01
  'byte 7 &H00
  'byte 8 &H00
  'byte 9 &H00
  'byte 10 &H00
  'byte 11 &H00
  'byte 12 &H00
  'byte 13 &H03   3 characters
  'byte 14 &H77   w
  'byte 15 &H77   w
  'byte 16 &H77   w
  'byte 17 &H07    7 characters
  'byte 18 &H6D   m
  'byte 19 &H63   c
  'byte 20 &H73   s
  'byte 21 &H65   e
  'byte 22 &H6C   l
  'byte 23 &H65   e
  'byte 24 &H63   c
  'byte 25 &H03   3 characters
  'byte 26 &H63   c
  'byte 27 &H6F   o
  'byte 28 &H6D  m
  'byte 29 &H00   0 characters
  'byte 30 &H01   "A"  record
  'byte 31 &H01    for Internet




'Clear buffer
For Pos = 1 To 200
         Buffer(pos) = &H00
Next
 'First two bytes are a random 16 bit number
 'this is the transaction ID
     Buffer(1) = Rnd(&Hff)
     Buffer(2) = Rnd(&Hff)
'Now to set the  flags bytes 3 & 4 Standard query = &H1000
     Buffer(3) = &H01
'Now the Questions bytes 5 & 6 = &H0001
     Buffer(6) = &H01
     Pos = 13                                               'point to byte 13 in the buffer


  'Split up the parts of the URL  www    mcselec    com
Lenurl = Split(url , Work(1) , ".")

  'Lenurl will = 3 so we have 3 parts to this URL
  'work(1)  will be   www
  'work(2) will be    mcselec
  'work(3) will be    com

  'now we need to go through each part
  'and build up the ASCII packet

  For X = 1 To Lenurl

     Z = Len(work(x))
      Buffer(pos) = Z                                       'find the number of characters in the part of the URL

      Incr Pos                                              'move to the next byte in the buffer

     For W = 1 To Z

       Char = Mid(work(x) , W , 1)
       Buffer(pos) = Asc(char)
       Incr Pos


     Next



  Next

    Buffer(pos) = &H00                                      'no more url's

    Incr Pos

    Buffer(pos) = &H01

    Incr Pos

    Buffer(pos) = &H01

   For X = 1 To Pos
   Print Hex(buffer(x))
   Next
  End

 
Back to top
View user's profile
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1198
Location: SYDNEY

australia.gif
PostPosted: Thu May 26, 2016 9:50 am    Post subject: Reply with quote

Now moving it into a function
still some more work to be done but it works

Code:


$regfile = "m2560def.dat"
$crystal = 16000000
$baud = 19200
$hwstack = 100
$swstack = 100
$framesize = 100


Dim Buffer(200) As Byte
Dim Work(5) As String * 20
Dim Result As String * 40
Dim Urls As String * 40

Declare Function Dns_req(byref Url As String * 40 ) As String * 40
 ' Url = "www.mcselec.com"                                   'use example url

'
'  DNS request for www.mcselec.com
'                                                w  w   w        m   c    s   e   l     e    c        c  o   m
'  X Y 0 1 0 1 0 0 0 0 0 0 [3] 77 77 77 [7] 6D 63 73 65 6C 65 63 [3] 63 6F 6D [0] 01 01
'53360100000100000000000003777777076D6373656C656303636F6D000101

'                                   [3] for 3 characters   [7] for 7 characters     [3] for 3 characters  [0] for no characters
'                                   these will change depending on how many characters in the URL name
'    X & Y are random numbers to identify the message
'     last two 01 01   IP "A" record
'                   0F 01  MX record
'
'
' So the  bytes are
  '------------------------------
  '16 bit random number ,bytes 1 & 2
  'byte 3 &H01
  'byte 4 &H00
  'byte 5 &H00
  'byte 6 &H01
  'byte 7 &H00
  'byte 8 &H00
  'byte 9 &H00
  'byte 10 &H00
  'byte 11 &H00
  'byte 12 &H00
  'byte 13 &H03   3 characters
  'byte 14 &H77   w
  'byte 15 &H77   w
  'byte 16 &H77   w
  'byte 17 &H07    7 characters
  'byte 18 &H6D   m
  'byte 19 &H63   c
  'byte 20 &H73   s
  'byte 21 &H65   e
  'byte 22 &H6C   l
  'byte 23 &H65   e
  'byte 24 &H63   c
  'byte 25 &H03   3 characters
  'byte 26 &H63   c
  'byte 27 &H6F   o
  'byte 28 &H6D  m
  'byte 29 &H00   0 characters
  'byte 30 &H01   "A"  record
  'byte 31 &H01    for Internet



Urls = "www.mcselec.com"

Result = Dns_req(urls)
'print buffer to terminal in HEX format
'to confirm result
   For X = 1 To Pos
   Print Hex(buffer(x))
   Next

End


Function Dns_req(byref Url As String * 40) As String * 40

Local Pos As Byte
Local Char As String * 1
Local Lenurl As Byte
Local W As Byte
Local X As Byte
Local Y As Byte
Local Z As Byte


' Print Urls ; "   url"

'Clear buffer
For Pos = 1 To 200
         Buffer(pos) = &H00
Next
 'First two bytes are a random 16 bit number
 'this is the transaction ID
     Buffer(1) = Rnd(&Hff)
     Buffer(2) = Rnd(&Hff)
'Now to set the  flags bytes 3 & 4 Standard query = &H1000
     Buffer(3) = &H01
'Now the Questions bytes 5 & 6 = &H0001
     Buffer(6) = &H01
     Pos = 13                                               'point to byte 13 in the buffer


  'Split up the parts of the URL  www    mcselec    com
Lenurl = Split(url , Work(1) , ".")

  'Lenurl will = 3 so we have 3 parts to this URL
  'work(1)  will be   www
  'work(2) will be    mcselec
  'work(3) will be    com

  'now we need to go through each part
  'and build up the ASCII packet

  For X = 1 To Lenurl

     Z = Len(work(x))
      Buffer(pos) = Z                                       'find the number of characters in the part of the URL

      Incr Pos                                              'move to the next byte in the buffer

     For W = 1 To Z

       Char = Mid(work(x) , W , 1)
       Buffer(pos) = Asc(char)
       Incr Pos


     Next



  Next

    Buffer(pos) = &H00                                      'no more url's

    Incr Pos

    Buffer(pos) = &H01

    Incr Pos

    Buffer(pos) = &H01
  End Function

 
Back to top
View user's profile
bzijlstra

Bascom Ambassador



Joined: 30 Dec 2004
Posts: 1179
Location: Tilburg - Netherlands

netherlands.gif
PostPosted: Mon Aug 08, 2016 5:24 pm    Post subject: NTP? Reply with quote

Paul

Any luck on the NTP?

Have fun
Ben Zijlstra
Back to top
View user's profile Visit poster's website
six1

Bascom Expert



Joined: 27 Feb 2009
Posts: 552

germany.gif
PostPosted: Tue Aug 09, 2016 6:29 am    Post subject: Reply with quote

Hi Paul,
i've done this "byte by byte" Very Happy
http://www.mcselec.com/index2.php?option=com_forum&Itemid=59&page=viewtopic&t=11141&highlight=dns

_________________
For technical reasons, the signature is on the back of this message.
Back to top
View user's profile
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1198
Location: SYDNEY

australia.gif
PostPosted: Tue Aug 09, 2016 7:55 am    Post subject: Reply with quote

Hello Ben , six1

Been busy with other things have not tested my DNS yet against a server
So I am yet to get on to the NTP

Regards Paul
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM Project Blog All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum