Jump to content
nend

Get IPadres from mDNS/Bonjour

Recommended Posts

Hi I made a small UDF to get the IPadres from mDNS/Bonjour service/device
I used this script as a example how this work an learn from it.

 

This is the small UDF I made

ConsoleWrite(_mDNS("http://Diskstation.local") & @CRLF)

Func _mDNS($domain)
    Local $return_ip, $domain_convert, $mDNS_msg

    $domain = StringReplace($domain , "http://", "")
    Local $domain_array = StringSplit($domain, ".")
    For $i = 1 To $domain_array[0]
        $domain_convert = $domain_convert & Hex(BinaryLen($domain_array[$i]), 2) & StringTrimLeft(StringToBinary($domain_array[$i]), 2)
    Next

    UDPStartup()

    Local $mDNS_sock = UDPOpen("224.0.0.251", 5353)
    If @error Then
        UDPShutdown()
        SetError(1)
        Return ""
    EndIf

    Local $query_time = TimerInit()

    UDPSend($mDNS_sock, "0x5A8901000001000000000000" & $domain_convert & "0000010001")

    Do
        $mDNS_msg = UDPRecv($mDNS_sock, 512, 1)
    Until $mDNS_msg <> "" Or TimerDiff($query_time) > 1000

    UDPShutdown()

    For $i = 3 To 0 Step -1
        $return_ip = $return_ip & Dec(StringMid(BinaryMid($mDNS_msg, BinaryLen($mDNS_msg) - $i, 1), 3))
        If $i <> 0 Then
            $return_ip = $return_ip & "."
        EndIf
    Next

    Return $return_ip
EndFunc

 

Share this post


Link to post
Share on other sites

something mybe better ^^

_GetIP_of_Domain("https://www.autoitscript.com/forum/forum/9-autoit-example-scripts/")
_GetIP_of_Domain("trong.live")

Func _GetIP_of_Domain($iDomain, $sDNS_Server = Default, $sDNS_ServerPort = Default)
    If Not StringInStr($iDomain, '.') Then Return SetError(-1, 0, "")
    $iDomain = StringStripWS($iDomain, 7)
    If ($sDNS_Server = Default) Or (Not StringInStr($sDNS_Server, ".")) Then $sDNS_Server = "8.8.8.8"
    If ($sDNS_ServerPort = Default) Or ($sDNS_ServerPort < 0) Or ($sDNS_ServerPort > 65353) Then $sDNS_ServerPort = 53
    Local $sPattern = "^(?i)(?:(?:[a-z]+):\/\/)?" & _ ; Protocol
            "(?:(?:(?:[^@:]+))" & _               ; Username
            "(?::(?:[^@]+))?@)?" & _              ; Password
            "([^\/:]+)" & _                       ; Host
            "(?::(?:\d+))?" & _                   ; Port
            "(?:\/(?:[^?]+)?)?" & _               ; Path
            "(?:\?\N+)?"                          ; Query - regular expression by jguinch
    Local $aHost = StringRegExp($iDomain, $sPattern, 1)
    If IsArray($aHost) Then
        If StringInStr($aHost[0], '.') Then $iDomain = $aHost[0]
    EndIf
    ConsoleWrite("- Get ip domain: " & $iDomain & @CRLF)
    Local $iDomain_Convert, $iDomain_array = StringSplit($iDomain, ".")
    For $i = 1 To $iDomain_array[0]
        $iDomain_Convert = $iDomain_Convert & Hex(BinaryLen($iDomain_array[$i]), 2) & StringTrimLeft(StringToBinary($iDomain_array[$i]), 2)
    Next
    ConsoleWrite("- Connect to DNS Server: " & $sDNS_Server & ":" & $sDNS_ServerPort & @CRLF)
    UDPStartup()
    Local $mDNS_sock = UDPOpen($sDNS_Server, $sDNS_ServerPort)
    If @error Then
        UDPShutdown()
        $sDNS_Server = "1.1.1.1"
        UDPStartup()
        ConsoleWrite("! Error !!!  Try again " & @CRLF & "- Connect to DNS Server: " & $sDNS_Server & ":" & $sDNS_ServerPort & @CRLF)
        $mDNS_sock = UDPOpen($sDNS_Server, $sDNS_ServerPort)
        If @error Then
            UDPShutdown()
            ConsoleWrite("! Error !!! Exit" & @CRLF)
            Return SetError(1, 0, "")
        EndIf
    EndIf
    Local $return_ip, $mDNS_msg, $query_time = TimerInit()
    ConsoleWrite("- Send data: " & "0x5A8901000001000000000000 " & $iDomain_Convert & " 0000010001" & @CRLF)
    UDPSend($mDNS_sock, "0x5A8901000001000000000000" & $iDomain_Convert & "0000010001")
    Do
        $mDNS_msg = UDPRecv($mDNS_sock, 512, 1)
    Until $mDNS_msg <> "" Or TimerDiff($query_time) > 1000
    UDPShutdown()
    ConsoleWrite("- Receives data: " & $mDNS_msg & @CRLF)
    For $i = 3 To 0 Step -1
        $return_ip = $return_ip & Dec(StringMid(BinaryMid($mDNS_msg, BinaryLen($mDNS_msg) - $i, 1), 3))
        If $i <> 0 Then
            $return_ip = $return_ip & "."
        EndIf
    Next
    If $return_ip <> "0.0.0.0" Then
        ConsoleWrite("+ IP of domain: " & $iDomain & " is " & $return_ip & @CRLF)
    Else
        ConsoleWrite("- Maybe something wrong ip received is 0.0.0.0" & @CRLF)
    EndIf
    Return $return_ip
EndFunc   ;==>_GetIP_of_Domain

 


Regards,
 

Share this post


Link to post
Share on other sites

@vip

I tryd it and it wont return the correct ipadres of the mDNS/Bonjour device

The only thing I can see is this.
 

- Get ip domain: envoy.local
- Connect to DNS Server: 8.8.8.8:53
- Send data: 0x5A8901000001000000000000 05656E766F79056C6F63616C 0000010001
- Receives data: 0x5A898183000100000001000005656E766F79056C6F63616C0000010001000006000100015179004001610C726F6F742D73657276657273036E657400056E73746C640C766572697369676E2D67727303636F6D0078689AD4000007080000038400093A8000015180
+ IP of domain: envoy.local is 0.1.81.128

The Ip which it is returning isn't correct.

Share this post


Link to post
Share on other sites

You need to declare your mDNS server!
Or edit UDF with your mDNS server address!
Local domain names will not work with public servers.

_GetIP_of_Domain("envoy.local", "224.0.0.251", "5353")

 


Regards,
 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...