Skip to content

Commit 6b48c02

Browse files
committed
try add minimdnsd
1 parent c6c31fb commit 6b48c02

File tree

9 files changed

+1224
-0
lines changed

9 files changed

+1224
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

.github/workflows/minimdnsd.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
on:
3+
push:
4+
branches:
5+
- main
6+
paths:
7+
- .github/workflows/minimdnsd.yml
8+
- minimdnsd/**
9+
jobs:
10+
minimdnsd:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v2
15+
with:
16+
submodules: true
17+
- name: Login
18+
uses: docker/login-action@v1
19+
with:
20+
username: ${{ secrets.DOCKER_HUB_USERNAME }}
21+
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
22+
- name: Setup QEMU
23+
uses: docker/setup-qemu-action@v1
24+
- name: Setup Docker
25+
uses: docker/setup-buildx-action@v1
26+
- name: Build Docker
27+
uses: docker/build-push-action@v2
28+
with:
29+
push: true
30+
context: ${{ github.job }}
31+
file: ${{ github.job }}/Dockerfile
32+
platforms: linux/amd64
33+
tags: ${{ secrets.DOCKER_HUB_USERNAME }}/${{ github.job }}:latest
34+
cache-from: type=registry,ref=${{ secrets.DOCKER_HUB_USERNAME }}/${{ github.job }}:buildcache
35+
cache-to: type=registry,ref=${{ secrets.DOCKER_HUB_USERNAME }}/${{ github.job }}:buildcache,mode=max
36+
- name: Push Docker Description
37+
uses: peter-evans/dockerhub-description@v2
38+
with:
39+
username: ${{ secrets.DOCKER_HUB_USERNAME }}
40+
password: ${{ secrets.DOCKER_HUB_PASSWORD }}
41+
repository: ${{ secrets.DOCKER_HUB_USERNAME }}/${{ github.job }}
42+
short-description: '使用参考 README,信息参考:https://www.dosk.win/'

minimdnsd/Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM ubuntu AS builder
2+
ADD minimdnsd.c /minimdnsd.c
3+
RUN apt update -y && apt install -y build-essential && gcc /minimdnsd.c -o /minimdnsd -static -Wall -pedantic -Os -g -flto -ffunction-sections -Wl,--gc-sections -fdata-sections
4+
5+
FROM alpine
6+
COPY --from=builder /minimdnsd /minimdnsd
7+
ENTRYPOINT ["/minimdnsd"]

minimdnsd/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 <>< Charles Lohr
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

minimdnsd/Makefile

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
all : minimdnsd
2+
3+
PACKAGE_VERSION:=0.1-$(shell cat .github/build_number)
4+
PACKAGE:=minimdnsd_$(PACKAGE_VERSION)
5+
6+
SHELL=/bin/bash
7+
8+
CFLAGS:=-Wall -pedantic -Os -g -flto -ffunction-sections -Wl,--gc-sections -fdata-sections
9+
10+
minimdnsd : minimdnsd.c
11+
echo $(shell expr 1 + $(shell cat .github/build_number)) > .github/build_number
12+
gcc -o $@ $^ $(CFLAGS)
13+
size $@
14+
15+
install : minimdnsd
16+
sudo install minimdnsd /usr/local/bin/
17+
sudo cp minimdnsd.service /etc/systemd/system
18+
sudo systemctl daemon-reload
19+
sudo systemctl enable minimdnsd.service
20+
sudo service minimdnsd restart
21+
cat minimdnsd.1 | gzip > /usr/share/man/man1/minimdnsd.1.gz
22+
23+
test : minimdnsd
24+
./minimdnsd &
25+
./minimdnsd -h testminimdnsd &
26+
ping -c 1 $(shell cat /etc/hostname).local # Ok, doesn't actually test anything
27+
killall minimdnsd
28+
29+
deb : minimdnsd
30+
rm -rf $(PACKAGE)
31+
mkdir -p $(PACKAGE)/DEBIAN
32+
33+
mkdir -p $(PACKAGE)/usr/bin
34+
cp minimdnsd $(PACKAGE)/usr/bin/
35+
mkdir -p $(PACKAGE)/etc/systemd/system
36+
cp minimdnsd $(PACKAGE)/etc/systemd/system/
37+
mkdir -p $(PACKAGE)/usr/share/man/man1
38+
cat minimdnsd.1 | gzip > $(PACKAGE)/usr/share/man/man1/minimdnsd.1.gz
39+
40+
cd $(PACKAGE); find . -type f -exec md5sum {} + | cut -c 1-33,38- > DEBIAN/md5sums
41+
42+
echo "Package: minimdnsd" > $(PACKAGE)/DEBIAN/control
43+
echo "Version: $(PACKAGE_VERSION)" >> $(PACKAGE)/DEBIAN/control
44+
echo "Section: base" >> $(PACKAGE)/DEBIAN/control
45+
echo "Priority: optional" >> $(PACKAGE)/DEBIAN/control
46+
echo "Architecture: $(shell dpkg --print-architecture)" >> $(PACKAGE)/DEBIAN/control
47+
echo "Maintainer: cnlohr <lohr85@gmail.com>" >> $(PACKAGE)/DEBIAN/control
48+
echo "Description: Bare bones MDNS server" >> $(PACKAGE)/DEBIAN/control
49+
echo "#!/bin/sh" > $(PACKAGE)/DEBIAN/postinst
50+
echo "set -e" >> $(PACKAGE)/DEBIAN/postinst
51+
echo 'case "$$1" in' >> $(PACKAGE)/DEBIAN/postinst
52+
echo " abort-upgrade|abort-remove|abort-deconfigure|configure)" >> $(PACKAGE)/DEBIAN/postinst
53+
echo " systemctl daemon-reload;systemctl enable minimdnsd.service; service minimdnsd restart" >> $(PACKAGE)/DEBIAN/postinst
54+
echo " ;;" >> $(PACKAGE)/DEBIAN/postinst
55+
echo " triggered)" >> $(PACKAGE)/DEBIAN/postinst
56+
echo " systemctl daemon-reload; service minimdnsd restart" >> $(PACKAGE)/DEBIAN/postinst
57+
echo " ;;" >> $(PACKAGE)/DEBIAN/postinst
58+
echo " *)" >> $(PACKAGE)/DEBIAN/postinst
59+
echo " ;;" >> $(PACKAGE)/DEBIAN/postinst
60+
echo "esac" >> $(PACKAGE)/DEBIAN/postinst
61+
echo "exit 0" >> $(PACKAGE)/DEBIAN/postinst
62+
chmod 775 $(PACKAGE)/DEBIAN/postinst
63+
64+
echo "#!/bin/sh" > $(PACKAGE)/DEBIAN/prerm
65+
echo "set -e" >> $(PACKAGE)/DEBIAN/prerm
66+
echo 'case "$$1" in' >> $(PACKAGE)/DEBIAN/prerm
67+
echo " remove|remove-in-favour|deconfigure|deconfigure-in-favour)" >> $(PACKAGE)/DEBIAN/prerm
68+
echo " systemctl daemon-reload;systemctl disable minimdnsd.service; service minimdnsd stop" >> $(PACKAGE)/DEBIAN/prerm
69+
echo " ;;" >> $(PACKAGE)/DEBIAN/prerm
70+
echo " upgrade|failed-upgrade)" >> $(PACKAGE)/DEBIAN/prerm
71+
echo " service minimdnsd stop" >> $(PACKAGE)/DEBIAN/prerm
72+
echo " ;;" >> $(PACKAGE)/DEBIAN/prerm
73+
echo " *)" >> $(PACKAGE)/DEBIAN/prerm
74+
echo " ;;" >> $(PACKAGE)/DEBIAN/prerm
75+
echo "esac" >> $(PACKAGE)/DEBIAN/prerm
76+
echo "exit 0" >> $(PACKAGE)/DEBIAN/postinst
77+
chmod 775 $(PACKAGE)/DEBIAN/prerm
78+
79+
dpkg-deb --build $(PACKAGE)
80+
81+
82+
# For manual installation override
83+
#mkdir -p $(PACKAGE)/etc/systemd/system/multi-user.target.wants/
84+
#cd $(PACKAGE)/etc/systemd/system/multi-user.target.wants && ln -s ../minimdnsd.service . || true
85+
86+
clean :
87+
rm -rf minimdnsd minimdnsd_* -rf

minimdnsd/README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# minimdnsd (MDNS server)
2+
3+
Extremely simple MDNS server for Linux. Much smaller and lighter weight than Avahi.
4+
5+
⚠️ This has not been battle hardened, or even thoroughly checked ⚠️
6+
7+
Primarily MDNS Hostname responder - i.e. run this, and any computer on your network can say `ping your_hostname.local` and it will resolve to your PC. Specifically, it uses whatever name is in your `/etc/hostname`
8+
9+
* Uses no CPU unless event requested.
10+
* Only needs around 32kB RAM.
11+
* Compiles to between 15-45kB
12+
* Can run as a user or root.
13+
* Zero config + Watches for `/etc/hostname` changes. (Optionally: Can use -h to also watch for a host alias)
14+
* Works on IPv6
15+
16+
⚠️ Caveats ⚠️
17+
* This tool only replies to hostnames, so you can use `hostname.local` but not services, so you can't use it to find your printer.
18+
* This tool automatically perform both a `UNICAST-RESPONSE` and `MULTICAST-RESPONSE` - whether true or not.
19+
20+
## General Motivation
21+
22+
1. To obviate need for avahi-daemon
23+
2. To provide an MDNS server on very simple systems
24+
3. To act as an educational tool for the following items
25+
26+
### To demonstrate the following:
27+
28+
1. Use of inotify to detect changes of `/etc/hostname`
29+
2. Use of `getifaddrs` to iterate through all available interfaces
30+
3. Use of `NETLINK_ROUTE` and `RTMGRP_IPV4_IFADDR` and `RTMGRP_IPV6_IFADDR` to monitor for any new network interfaces or addresses.
31+
4. Use of multicast in IPv4 and IPv6 to join a multicast group
32+
5. Use of `recvmsg` to get the interface and address that a UDP packet is received on
33+
6. Use of `optarg` to handle command-line parameters.
34+
7. Use of `fork()` and `wait3()` to handle workers. (Only used in DNS forwarding mode)
35+
36+
### And for general housekeeping:
37+
38+
7. Use of github workflows to build and test tool on Linux
39+
8. Makefile example for building .deb files
40+
9. Github workflow example for generating releases on new tags.
41+
10. How to use/install a basic man page
42+
43+
## Building
44+
45+
### Prerequisits
46+
* build-essential (make + GCC + system headers)
47+
48+
### Build process
49+
* `make`
50+
* or, optionally `make install` to install it to /usr/local/bin/minimdnsd, and install the initd service
51+
52+
## Long-Term
53+
54+
* Allow response to services (see original MDNS server here: https://github.com/cnlohr/esp82xx/blob/master/fwsrc/mdns.c)
55+
* Keep it under or around 1k LoC
56+
* Keep it < 32kB `.text`
57+
58+
## Things I learned
59+
60+
IPv6 is pain. this would have been a tiny fraction of its size if it weren't for IPv6.
61+
62+
Also IPv6 with MDNS is in a really sorry state, so I was barely able to test this with IPv6.
63+
64+
https://superuser.com/questions/1086954/how-do-i-use-mdns-for-ssh-6
65+
66+
And ping6 has a similar issue.
67+
68+
### Thanks
69+
70+
71+
For code reviews: @GPSBabelDeveloper, @probonopd
72+
73+
74+
75+

minimdnsd/minimdnsd.1

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.TH MINIMDNSD 1 "June 2024" "version 0.1" "User Manuals"
2+
.SH "NAME"
3+
minimdns \- Minimal MDNS server
4+
.SH "SYNOPSIS"
5+
.B minimdnsd [-h host_alias_override] [-4] [-r]
6+
.SH "DESCRIPTION"
7+
.B minimdnsd is a minimal MDNS server, able to reply to other computers on the network at (your hostname).local
8+
.SH "OPTIONS"
9+
Usually this is intended to be used without any -h flag.
10+
.IP -h
11+
Specify a hostname override instead of using /etc/hostname - you can launch multiple instances, to get multiple overrides.
12+
.IP -r
13+
Create a dummy responder, it listens on 127.0.0.67:53 and forwards requests to 224.0.0.251:5353, but note: may or may not forward AAAA requests.
14+
.IP -4
15+
Disable IPv6 operation.
16+
.SH "AUTHOR"
17+
cnlohr <lohr85@gmail.com>
18+
19+
.SH COPYRIGHT
20+
Copyright \(co 2024 cnlohr
21+
.br
22+
.SH LICENSE
23+
minimdns is licensed under the MIT-x11 license.
24+

0 commit comments

Comments
 (0)