• Stars
    star
    8
  • Rank 2,033,537 (Top 42 %)
  • Language
    Crystal
  • License
    MIT License
  • Created over 3 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Crystal Lang mDNS and DNS-SD Support

Crystal Lang mDNS Support

CI

  • Discover services using DNS-SD

DNS-SD is used where there might be multiple devices implementing a service.

  • Lookup devices using mDNS

mDNS is used to find the IP address of device, like a Raspberry Pi

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      mDNS:
        github: spider-gazelle/mdns
  2. Run shards install

Single Shot usage

As described in the rfc

mDNS query

If we are looking for the IP address of a device for instance.

# We're requesting the IPv4 address of the phone (A record)
# We could also do a multi-request and get the IPv6 address too (AAAA record)
results = MDNS.one_shot "Steves-iPhone.local", type: MDNS::Type::A

if address_response = results.first?
  # NOTE:: the `_address` here is the device that responded, it might not be an
  # authoritative answer (i.e. something replying on behalf of the device) so
  # extract the IP address from the payload (mDNS only, for DNS-SD use the `_address`)
  _address, response = address_response

  # This is assuming answers[0] is an A or AAAA record
  response.answers[0].payload.as(Socket::IPAddress).address # => "192.168.40.150"
end

Another common request, if you want the port of the service, is the SRV query. Use the SRV query in tandem with the DNS-SD request described below

Simple DNS-SD query

To perform a simple query for a service on the network

require "mdns"

# Look up homekit devices on the network
results = MDNS.one_shot "_hap._tcp.local"

results.each do |(address, response)|
  address # => Socket::IPAddress
  response # => MDNS::Message

  response.is_response # => true
  response.authoritative_answer # => true / false
  response.response_code # => ResponseCode::NoError
  response.answer_count # => 1
  response.answers # => Array(MDNS::Resource)

  # The answers will most likely be PTRs to the devices local domain names
  # Then you can perform a SRV query for the service details of the pointer
end

Multiple service queries

In a single request

# Look up homekit devices and hubs on the network
results = MDNS.one_shot do |message|
  message.query "_hap._tcp.local"
  message.query "_homekit._tcp.local"
end

# You will need to inspect the answers to differentiate between devices and hubs
results.each do |(address, response)|
  is_hap = false
  is_kit = false

  response.answers.each do |answer|
    domain_name = answer.domain_name

    if domain_name == "_hap._tcp.local"
      is_hap = true
    elsif domain_name == "_homekit._tcp.local"
      is_kit = true
    end
  end
end

Continuous Multicast DNS Querying

This is only the most basic of servers implementing the transport layer. It could definitely be used as the basis for a DNS cache as described by the RFC

require "mdns"

server = MDNS::Server.new(MDNS::IPv4)
loop do
  break if server.closed?
  address, message = server.receive

  if message.query?
    # process a query here
  else
    # process responses here
  end

  # This code prints the details of the message
  puts Time.utc.to_s("%X")
  puts "QUERY:"
  puts message.queries.map(&.domain_name).join("\n")
  puts "ANSWERS:"
  puts message.answers.map { |answer|
    String.build do |str|
      str << answer.domain_name
    	str << " => (#{answer.type}) "
      str << answer.payload.inspect
    end
  }.join("\n")
  puts "\n\n\n"
end

See the example for how to register a service on your local network.

More Repositories

1

spider-gazelle

A Rails esque web framework with a focus on speed and extensibility for crystal lang
Crystal
175
star
2

tasker

Scheduled tasks for crystal lang
Crystal
54
star
3

bindata

BinData - Parsing Binary Data in Crystal Lang
Crystal
48
star
4

ssh2.cr

libssh2 binding for Crystal language
Crystal
42
star
5

promise

Type aware promises for crystal lang
Crystal
40
star
6

action-controller

A rails-esque controller framework for crystal lang
Crystal
39
star
7

active-model

A rails-esque model framework for crystal lang
Crystal
28
star
8

rethinkdb-orm

RethinkDB ORM for Crystal lang
Crystal
24
star
9

crystal-mqtt

Crystal lang implementation of the MQTT protocol, a lightweight protocol for publish/subscribe messaging
Crystal
19
star
10

ffmpeg

ffmpeg crystal bindings
Crystal
18
star
11

qr-code

a QR Code implementation written in crystal lang
Crystal
17
star
12

crystal-ldap

a Crystal lang LDAP client
Crystal
16
star
13

crystal-snmp

SNMP implementation for crystal lang
Crystal
16
star
14

priority-queue

Priority Queue and Heap implementation for Crystal Lang
Crystal
13
star
15

bisect

Library for maintaining sorted Arrays
Crystal
12
star
16

json-schema

Describe crystal-lang JSON serializable types with JSON Schema
Crystal
12
star
17

pinger

Microlibrary to perform ping requests with Crystal Lang
Crystal
11
star
18

telnet.cr

Telnet protocol helper for crystal lang
Crystal
11
star
19

pars

Parser combinator library for crystal-lang
Crystal
11
star
20

inactive-support

Utilities for crystal-lang
Crystal
10
star
21

pg-orm

Postgres ORM for Crystal Lang
Crystal
9
star
22

tensorflow_lite

tensorflow lite bindings for crystal lang
Crystal
8
star
23

ed25519

Ed25519 high-performance public-key signature system for crystal lang
Crystal
7
star
24

crystal-gpt

ChatGPT plugin template that allows you to focus on writing actions, automatically generating the required metadata
Crystal
7
star
25

secure-remote-password

Crystal implementation of the Secure Remote Password protocol (SRP-6a)
Crystal
6
star
26

simple_retry

a tool for retrying code blocks
Crystal
6
star
27

connect-proxy

crystal lang connect / HTTP proxy implementation
Crystal
6
star
28

crystal-openai

OpenAI ChatGPT, GPT-3, GPT-4, DALLΒ·E, Whisper API Client for Crystal
Crystal
6
star
29

crunits

Physical quantity and units of measure conversion and math for crystal lang
Crystal
6
star
30

secrets-env

Extension to the crystal lang ENV module to support reading secrets
Crystal
6
star
31

v4l2.cr

crystal lang video for linux device helpers / bindings
Crystal
5
star
32

log_helper

Extension for Crystal Log to aid logging key-value data
Crystal
5
star
33

readers-writer

A simple readers writer lock for crystal lang
Crystal
5
star
34

cmac

Crystal implementation of the Cipher-based Message Authentication Code (CMAC)
Crystal
5
star
35

guide

Spider Gazelle Documentation
Python
4
star
36

ntlm

NTLM authentication for crystal lang
Crystal
4
star
37

worker_pool

a basic fiber pool implementation for crystal lang
Crystal
4
star
38

digest-auth

HTTP digest auth for crystal lang
Crystal
4
star
39

matter

A complete Crystal implementation of the Matter protocol specification (https://buildwithmatter.com). Includes full support for controller, device, commissioning, secure communications, device types, and cluster definitions.
Crystal
4
star
40

eventbus

Listen for Postgres database change events and publish them to event listeners
Crystal
3
star
41

knx

KNX protocol support for crystal lang
Crystal
3
star
42

tokenizer

Simplified binary stream tokenization for crystal lang
Crystal
2
star
43

crystal-dtls

DTLS support for crystal lang
Crystal
2
star
44

upload-signer

Provide API for generating pre-signed URLs for file uploads to cloud storage
Crystal
2
star
45

stomp

crystal lang implementation of the STOMP protocol
Crystal
1
star
46

tlv

Matter TLV encoder/decoder
Crystal
1
star
47

gpio.cr

crystal lang bindings for linux gpiod
Crystal
1
star
48

stumpy_resize

resizes stumpy canvas images in pure crystal
Crystal
1
star
49

HKDF

HMAC-based Extract-and-Expand Key Derivation Function (HKDF) for crystal lang
Crystal
1
star
50

SPAKE2_plus

a crystal lang implementation of SPAKE2+, a Password Authenticated Key Exchange (PAKE) protocol
Crystal
1
star
51

panopticon

Distributed tracing for services built in crystal-lang
Crystal
1
star
52

tflite_image

image classification and feature detection with tflite and crystal lang
Crystal
1
star
53

tflite_pipeline

video processing AI pipeline leveraging tflite_image
Crystal
1
star
54

link-header

Crystal Lang HTTP Link Header Parser
Crystal
1
star