nickovs / slimDNS

Simple, Lightweight Implementation of Multicast DNS
Apache License 2.0
86 stars 9 forks source link

slimDNS: Simple, Lightweight Implementation of Multicast DNS

Why slimDNS?

slimDNS is a pure Python implementation of multicast DNS (mDNS) and DNS Service Discovery (DNS-SD) targeting memory-constrained devices such as those running MicroPython. The goal of this project is to allow MicroPython users to have their networked devices, and the services that they offer, more easily discoverable.

slimDNS directly supports both advertising and discovery of hostnames and it also can be used for the advertising and discovery of services. It aims to be compliant with the parts of the mDNS and DNS-SD specifications that it implements, even if it fails to implement all features.

Quick Start

In order to use slimDNS in the simplest possible way all that is necessary is to create a SlimDNSServer object with the local IP address and a hostname and tell the server to serve that address:

import network
import slimDNS
sta_if = network.WLAN(network.STA_IF)
local_addr = sta_if.ifconfig()[0]
server = SlimDNSServer(local_addr, "micropython")
server.run_forever()

Of course this isn't actually terribly useful, since without having multithreading support you now can't do anything else. In practice a much more likely model is to create the SlimDNSServer object, find out its server socket and add this to the list of sockets that you are going to be checking with the select call in your own runloop. When data is avilable on the that socket you can call the process_waiting_packets() method to let the server do its stuff.

server = SlimDNSServer(local_addr, "micropython")
...
while (running):
    read_sockets = [server.sock, other_socket, ...]
    (r, _, _) = select(read_sockets, [], [])
    ...
    if server.sock in r:
      server.process_waiting_packets()

The SlimDNSServer object also supports lookup of names over mDNS using the resolve_mdns_address() method:

host_address_bytes = server.resolve_mdns_address("something.local")

Design principles

Many devices running MicroPython have very small amounts of RAM. Such devices are likely to only be offering a small set of services on a single network interface. In order to support these devices the design philosophy of slimDNS includes the following principles.

License

slimDNS is released under the Apache 2.0 license.