Tcl Library Source Code

View Ticket
Login
Ticket UUID: 294ebffb63ea83f340dd91303303c071f2de497c
Title: add multicast DNS capabilities
Type: Patch Version: 1.21
Submitter: umlaeute Created on: 2022-05-25 14:06:58
Subsystem: dns Assigned To:
Priority: 5 Medium Severity: Minor
Status: Open Last Modified: 2022-06-10 07:21:07
Resolution: None Closed By: nobody
    Closed on:
Description:
I'm currently trying to build a ZeroConf client in Tcl/Tk (for service discovery)
Unfortunately tcllib's "dns" module lacks Multicast DNS ("mdns") support, which is required by ZeroConf/Bonjour/Avahi in order to work.

After discussing the issue on [StackOverflow](https://stackoverflow.com/a/72369839/1169096), it seems that the changes required are not that much.

The most simplistic approach would be:

```git-diff
From 33d20fbb0f03532222279f97882bd5a657c7f313 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?IOhannes=20m=20zm=C3=B6lnig?= <[email protected]>
Date: Wed, 25 May 2022 15:42:45 +0200
Subject: [PATCH] enable mdns

---
 modules/dns/dns.tcl | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/modules/dns/dns.tcl b/modules/dns/dns.tcl
index 316458bb2..29ab204ac 100644
--- a/modules/dns/dns.tcl
+++ b/modules/dns/dns.tcl
@@ -802,6 +802,11 @@ proc ::dns::UdpTransmit {token} {
     } else {
         # using tcludp
         set state(sock) [udp_open]
+        if { $state(-protocol) eq "mdns" } {
+            set state(-nameserver) "224.0.0.251"
+            set state(-port)       5353
+            fconfigure $state(sock) -mcastadd $state(-nameserver);
+        }
         udp_conf $state(sock) $state(-nameserver) $state(-port)
     }
     fconfigure $state(sock) -translation binary -buffering none
-- 
2.36.1

```

which adds a new protocol "-mdns" to the `::dns::resolve` proc.

On StackOverflow, it was also suggested to replace the `udp_conf` with `chan configure ...`, which i have left out here for simplicities sake (and it seems to work with TclUDP-1.0.11 regardless).
The patch requires TclUDP to be installed (but that is an optional dependency of the 'dns' package anyhow).
I have done a casual check whether TclUDP>1.0.4 would be required for the mdns-patch, but it seems that "-mcastadd" is already present in 1.0.4 (which is the minimum version of TclUDP required right now)


Sidenote: the actual credit should go to [mrcalvin](https://stackoverflow.com/users/3207222/)
User Comments: apnadkarni added on 2022-06-10 07:21:07:
[email protected], a request to make your ZeroConf code open source if possible. 

You may also want to see https://github.com/dongola7/tcl_bonjour/

/Ashok

umlaeute added on 2022-05-31 06:59:23:
An alternative implementation, that does not hardcode the mDNS server (so the user has to provide both nameserver IP and port), but automatically joins the multicast group (if the nameserver is a multicast IP).

i don't specifically like hardcoding values (hence this alternative). otoh, the mDNS nameserver address and port are standardized in RFC6762...


```git-diff
From 2357dd0ad1bba5352c7c659320829137778056a6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?IOhannes=20m=20zm=C3=B6lnig?= <[email protected]>
Date: Tue, 31 May 2022 08:43:15 +0200
Subject: [PATCH] Automatically join the multicast group, if the given
 nameserver is a multicast IP address

this allows the caller to do mDNS queries like so:

   dns::resolve _http._tcp.local -nameserver "224.0.0.251" -port 5353
---
 modules/dns/dns.tcl | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/modules/dns/dns.tcl b/modules/dns/dns.tcl
index 316458bb2..a1ffeaa40 100644
--- a/modules/dns/dns.tcl
+++ b/modules/dns/dns.tcl
@@ -802,6 +802,12 @@ proc ::dns::UdpTransmit {token} {
     } else {
         # using tcludp
         set state(sock) [udp_open]
+        catch {
+            # automatically join the multicast group, if the nameserver is a multicast IP address
+            if { [ip::type $state(-nameserver)] eq "multicast" || [ip::isOverlap 224/8 $state(-nameserver)] } {
+                fconfigure $state(sock) -mcastadd $state(-nameserver);
+            }
+        }
         udp_conf $state(sock) $state(-nameserver) $state(-port)
     }
     fconfigure $state(sock) -translation binary -buffering none
-- 
2.36.1
```

Attachments: