probonopd
4/5/2014 - 1:08 AM

Announce services using tinysvcmdns

Announce services using tinysvcmdns

#!/bin/sh /etc/rc.common

# This script should not have the same name as one of the binaries
# that gets launched by it, otherwise it terminates itself rather
# than shutting down cleanly.

START=94
STOP=1

start() {
	run-announce & # & is important, otherwise boot hangs
}

stop() {
	killall run-announce >/dev/null 2>&1
	killall announce >/dev/null 2>&1
}
/*
 * Announce services using tinysvcmdns
 */

#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>

#include <signal.h>
#include <stdio.h>
#include "mdns.h"
#include "mdnsd.h"
#include <netdb.h>

int done = 0;

void term(int signum)
{
    printf("\nReceived SIGTERM, exiting...\n");
    done = 1;
}

int main(int argc, char *argv[]) {

	struct sigaction action;
	memset(&action, 0, sizeof(struct sigaction));
	action.sa_handler = term;
	sigaction(SIGTERM, &action, NULL); // Catch kill, killall
	sigaction(SIGINT, &action, NULL);  // Catch Ctrl-C

	if (argc < 7) {
		printf("Usage: %s 'hello.local' 192.168.0.11 'My Website' '_http._tcp.local' 80 'path=/mywebsite'\n", argv[0]);
		exit(1);
	}

	struct mdnsd *svr = mdnsd_start();
	if (svr == NULL) {
		printf("mdnsd_start() error\n");
		return 1;
	}
	
	char *hostname = argv[1];
	mdnsd_set_hostname(svr, hostname, inet_addr(argv[2]));

	struct rr_entry *a2_e = NULL;
	a2_e = rr_create_a(create_nlabel(hostname), inet_addr(argv[2]));
	mdnsd_add_rr(svr, a2_e);

	const char *txt[] = { argv[6], NULL };
	struct mdns_service *svc = mdnsd_register_svc(svr, argv[3], argv[4], atoi(argv[5]), NULL, txt);
	mdns_service_destroy(svc);

	printf("Announcing service. Press Ctrl-C to exit\n");

	while (!done) sleep(1);

	mdnsd_stop(svr);

	return 0;
}
#!/bin/sh

# Check whether certain services are running locally and announce them accordingly

HOSTNAME=$(uci get system.@system[0].hostname)
L=".local"
 
launch() {
  IPS=$(ifconfig | grep "inet addr" | cut -d ":" -f 2 | cut -d " " -f 1)
  for IP in $IPS ; do
    announce $HOSTNAME$L $IP $HOSTNAME $1$L $2 ''$3 >/dev/null 2>&1 &
  done
}
 
killall announce >/dev/null 2>&1
 
while true ; do
  killall -9 announce >/dev/null 2>&1
  pidof dropbear >/dev/null && launch "_ssh._tcp" 22
  pidof uhttpd >/dev/null && launch "_http._tcp" 80 'path=/'
  ls /usr/libexec/sftp-server >/dev/null && launch "_sftp-ssh._tcp" 22
  sleep 15
done