Skip to content

Commit

Permalink
rewrite: add mDNS registration
Browse files Browse the repository at this point in the history
  • Loading branch information
abrasive committed Apr 1, 2013
1 parent a2fb5d2 commit 04b142d
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SRCS := shairport.c rtsp.c common.c rtp.c player.c alac.c $(wildcard audio_*.c)
SRCS := shairport.c rtsp.c mdns.c common.c rtp.c player.c alac.c $(wildcard audio_*.c)

LIBS := -lcrypto -lm -lao -lpthread

Expand Down
1 change: 1 addition & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

typedef struct {
char *password;
char *apname;
char hw_addr[6];
int port;
audio_ops *output;
Expand Down
56 changes: 56 additions & 0 deletions mdns.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <signal.h>
#include <memory.h>
#include <string.h>
#include <unistd.h>
#include "common.h"

static int mdns_pid = 0;

void mdns_unregister(void) {
if (mdns_pid)
kill(mdns_pid, SIGTERM);
mdns_pid = 0;
}

void mdns_register(void) {
if (mdns_pid)
die("attempted to register with mDNS twice");

// XXX todo: native avahi support, if available at compile time

if ((mdns_pid = fork()))
return;

char *mdns_apname = malloc(strlen(config.apname) + 14);
char *p = mdns_apname;
int i;
for (i=0; i<6; i++) {
sprintf(p, "%02X", config.hw_addr[i]);
p += 2;
}
*p++ = '@';
strcpy(p, config.apname);

char mdns_port[6];
sprintf(mdns_port, "%d", config.port);

#define RECORD "tp=UDP", "sm=false", "ek=1", "et=0,1", "cn=0,1", "ch=2", \
"ss=16", "sr=44100", "vn=3", "txtvers=1", \
config.password ? "pw=true" : "pw=false"

char *argv[] = {
NULL, mdns_apname, "_raop._tcp", mdns_port, RECORD
};

argv[0] = "avahi-publish-service";
execvp(argv[0], argv);

argv[0] = "mDNSPublish";
execvp(argv[0], argv);

char *mac_argv[] = {"dns-sd", "-R", mdns_apname, "_raop._tcp", ".",
mdns_port, RECORD};
execvp(mac_argv[0], mac_argv);

die("Could not establish mDNS advertisement!\n");
}
7 changes: 7 additions & 0 deletions mdns.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef _MDNS_H
#define _MDNS_H

void mdns_unregister(void);
void mdns_register(void);

#endif // _MDNS_H
3 changes: 3 additions & 0 deletions rtsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "common.h"
#include "player.h"
#include "rtp.h"
#include "mdns.h"

// only one thread is allowed to use the player at once.
// it monitors the request variable (at least when interrupted)
Expand Down Expand Up @@ -699,6 +700,8 @@ void rtsp_listen_loop(void) {
if (sockfd[1])
FD_SET(sockfd[1], &fds);

mdns_register();

printf("Listening for connections.\n");

int acceptfd;
Expand Down
7 changes: 7 additions & 0 deletions shairport.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
#include <memory.h>
#include "common.h"
#include "rtsp.h"
#include "mdns.h"


static void shutdown(void) {
printf("Shutting down...\n");
mdns_unregister();
rtsp_shutdown_stream();
config.output->deinit();
exit(0);
Expand All @@ -23,17 +25,21 @@ int main(int argc, char **argv) {
// config.password = "hello";
memcpy(config.hw_addr, "\0\x11\x22\x33\x44\x55", 6);
config.buffer_start_fill = 220;
config.port = 9000;
config.apname = "hellothere";

config.output = &audio_dummy;
//config.output = &audio_ao;

// mask off all signals before creating threads.
// this way we control which thread gets which signals.
// for now, we don't care which thread gets the following.
sigset_t set;
sigfillset(&set);
sigdelset(&set, SIGINT);
sigdelset(&set, SIGTERM);
sigdelset(&set, SIGSTOP);
sigdelset(&set, SIGCHLD);
pthread_sigmask(SIG_BLOCK, &set, NULL);

// setting this to SIG_IGN would prevent signalling any threads.
Expand All @@ -46,6 +52,7 @@ int main(int argc, char **argv) {
sa.sa_sigaction = &sig_shutdown;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGCHLD, &sa, NULL);

config.output->init(0, 0);

Expand Down

0 comments on commit 04b142d

Please sign in to comment.