2 #include <sys/socket.h>
3 #include <netinet/in.h>
17 #include "dns_sd_txt.h"
20 #elif defined(__GNUC__)
21 # define UNUSED(x) UNUSED_ ## x __attribute__((unused))
22 #elif defined(__LCLINT__)
23 # define UNUSED(x) /*@unused@*/ x
28 #define HOSTNAMESIZE 64
37 typedef struct _service_info
40 char hostname[HOSTNAMESIZE];
46 /* service-discovery records */
47 TMdnsdRecord *host_to_ip;
48 TMdnsdRecord *ip_to_host;
49 struct in_addr announce_ip;
53 /* service-discovery records */
54 TMdnsdRecord *srv_to_host;
55 TMdnsdRecord *txt_for_srv;
57 TMdnsdRecord *ptr_to_srv;
62 static ServiceInfo service_info;
64 void request_service (ServiceInfo *info, int stage);
65 void request_ip_addresses (ServiceInfo *info);
67 char *increment_name (char *name)
74 pos = strrchr (name, '-');
77 id = strtol (pos + 1, &end, 10);
87 asprintf (&ret, "%s-%d", name, id);
93 /* conflict handling */
94 void handle_conflict (TMdnsdRecord *record, uint8_t *name, int UNUSED(type), void *arg)
96 ServiceInfo *info = (ServiceInfo *) arg;
99 if (record == info->ip_to_host) {
100 /* can't do anything about a reverse lookup conflict. Just stop
102 info->ip_to_host = NULL;
103 fprintf (stderr, "zeroconf reverse lookup conflict for %s!\n", info->ip);
107 if (record == info->host_to_ip) {
108 info->host_to_ip = NULL;
109 info->announce_ip.s_addr = 0;
112 if (info->servicename == NULL) {
113 newname = increment_name (info->hostname);
115 newname = increment_name (info->servicename);
116 free (info->servicename);
119 info->servicename = newname;
121 if (record == info->srv_to_host) {
122 info->srv_to_host = NULL;
125 if (record == info->txt_for_srv) {
126 info->txt_for_srv = NULL;
129 fprintf (stderr, "conflicting name \"%s\". trying %s\n",
130 name, info->servicename);
132 /* The hostname was changed, so go back to probe state */
133 info->state = MDNSD_PROBE;
137 /* quit and updates */
138 void sighandler (int sig)
141 service_info.state = MDNSD_SHUTDOWN;
146 /* create multicast 224.0.0.251:5353 socket */
153 struct sockaddr_in in;
156 bzero (&in, sizeof (in));
157 in.sin_family = AF_INET;
158 in.sin_port = htons (MDNS_PORT);
159 in.sin_addr.s_addr = 0;
161 if ((sock_fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) {
165 setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (char*) &flag, sizeof (flag));
166 if (bind (sock_fd, (struct sockaddr*) &in, sizeof (in))) {
171 mc.imr_multiaddr.s_addr = inet_addr ("224.0.0.251");
172 mc.imr_interface.s_addr = htonl (INADDR_ANY);
173 setsockopt (sock_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mc, sizeof (mc));
174 setsockopt (sock_fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof (ttl));
175 setsockopt (sock_fd, IPPROTO_IP, IP_MULTICAST_TTL, &ittl, sizeof (ittl));
177 flag = fcntl (sock_fd, F_GETFL, 0);
179 fcntl (sock_fd, F_SETFL, flag);
184 void request_ip_addresses (ServiceInfo *info)
191 sprintf (hostlocal, "%s.local.", info->servicename ? info->servicename : info->hostname);
194 ip.s_addr = inet_addr (info->ip);
196 if (ip.s_addr != info->announce_ip.s_addr) {
197 snprintf (revlookup, 256, "%d.%d.%d.%d.in-addr.arpa.",
198 (ip.s_addr >> 24) & 0xff, (ip.s_addr >> 16) & 0xff,
199 (ip.s_addr >> 8) & 0xff, (ip.s_addr >> 0) & 0xff);
201 if (!info->host_to_ip) {
202 info->host_to_ip = MdnsdAllocUnique(info->mdnsd, hostlocal,
203 QTYPE_A, 120, handle_conflict, info);
205 MdnsdSetRaw (info->mdnsd, info->host_to_ip, (uint8_t *) &ip, 4);
207 if (!info->ip_to_host) {
208 info->ip_to_host = MdnsdAllocUnique(info->mdnsd, revlookup,
209 QTYPE_PTR, 120, handle_conflict, info);
211 MdnsdSetHost (info->mdnsd, info->ip_to_host, hostlocal);
213 info->announce_ip = ip;
218 if (info->host_to_ip) {
219 MdnsdDone (service_info.mdnsd, info->host_to_ip);
221 if (info->ip_to_host) {
222 MdnsdDone (service_info.mdnsd, info->ip_to_host);
225 info->host_to_ip = NULL;
226 info->ip_to_host = NULL;
227 info->announce_ip.s_addr = 0;
231 void request_service (ServiceInfo *info, int stage)
238 sprintf (servlocal, "%s._http._tcp.local.",
239 info->servicename ? info->servicename : info->hostname);
242 * Timeouts according to
243 * http://files.multicastdns.org/draft-cheshire-dnsext-multicastdns.txt
245 * As a general rule, the recommended TTL value for Multicast DNS
246 * resource records with a host name as the resource record's name
247 * (e.g. A, AAAA, HINFO, etc.) or contained within the resource record's
248 * rdata (e.g. SRV, reverse mapping PTR record, etc.) is 120 seconds.
250 * The recommended TTL value for other Multicast DNS resource records
257 request_ip_addresses (info);
262 sprintf (hostlocal, "%s.local.",
263 info->servicename ? info->servicename : info->hostname);
265 if (!info->srv_to_host) {
266 info->srv_to_host = MdnsdAllocUnique (info->mdnsd, servlocal,
267 QTYPE_SRV, 120, handle_conflict, info);
270 MdnsdSetSrv (info->mdnsd, info->srv_to_host, 0, 0, info->port, hostlocal);
272 if (!info->txt_for_srv) {
273 info->txt_for_srv = MdnsdAllocUnique (info->mdnsd, servlocal,
274 QTYPE_TXT, 4500, handle_conflict, info);
277 packet = DnsSd2Txt (info->metadata, &len);
278 MdnsdSetRaw (info->mdnsd, info->txt_for_srv, packet, len);
283 if (!info->ptr_to_srv) {
284 info->ptr_to_srv = MdnsdAllocShared (info->mdnsd, "_http._tcp.local.",
287 MdnsdSetHost (info->mdnsd, info->ptr_to_srv, servlocal);
290 fprintf (stderr, "Announcing \"%s.local\" to %s:%d\n",
291 info->servicename ? info->servicename : info->hostname,
292 info->ip, info->port);
297 fprintf (stderr, "announce stage %d is invalid\n", stage);
302 int main(int argc, char *argv[])
308 int ssize = sizeof(struct sockaddr_in);
309 uint8_t buf[MAX_PACKET_LEN];
310 struct sockaddr_in from;
311 struct sockaddr_in to;
314 struct in_addr remote_ip;
317 int announce_stage = 0;
318 struct pollfd fds[1];
322 fprintf (stderr, "usage: mhttp <ip> <port> <key1>=<value1> <key2>=<value2> ...\n");
323 fprintf (stderr, " <ip> The IP address to promote\n");
324 fprintf (stderr, " <port> is the port number of the service to be advertized\n");
325 fprintf (stderr, " <key>=<value> are the keys that get embedded into the TXT record.\n");
329 service_info.mdnsd = MdnsdNew (1, 1000);
331 //gethostname (service_info.hostname, HOSTNAMESIZE);
332 sprintf(service_info.hostname, "reinhardt");
333 service_info.hostname[HOSTNAMESIZE-1] = '\0';
334 if (strchr (service_info.hostname, '.'))
335 strchr (service_info.hostname, '.')[0] = '\0';
337 service_info.servicename = NULL;
339 service_info.ip = strdup(argv[1]);
340 service_info.announce_ip.s_addr = inet_addr(service_info.ip);
341 service_info.host_to_ip = NULL;
342 service_info.ip_to_host = NULL;
344 service_info.port = atoi(argv[2]);
346 service_info.metadata = SHashInit (11);
347 for (idx = 2; idx < argc; idx++) {
348 value = index (argv[idx], '=');
352 SHashSet (service_info.metadata, argv[idx], value);
356 service_info.ptr_to_srv = NULL;
357 service_info.srv_to_host = NULL;
358 service_info.txt_for_srv = NULL;
360 signal(SIGHUP, sighandler);
361 signal(SIGINT, sighandler);
362 signal(SIGQUIT, sighandler);
363 signal(SIGTERM, sighandler);
365 if ((s = msock()) == 0)
367 fprintf (stderr, "can't create socket: %s\n", strerror(errno));
371 request_ip_addresses (&service_info);
373 service_info.state = MDNSD_PROBE;
377 fds[0].events = POLLIN;
380 poll (fds, 1, polltime);
382 switch (service_info.state)
386 if (service_info.ptr_to_srv) {
387 MdnsdDone (service_info.mdnsd, service_info.ptr_to_srv);
390 if (service_info.srv_to_host) {
391 MdnsdDone (service_info.mdnsd, service_info.srv_to_host);
394 if (service_info.txt_for_srv) {
395 MdnsdDone (service_info.mdnsd, service_info.txt_for_srv);
398 service_info.ptr_to_srv = NULL;
399 service_info.srv_to_host = NULL;
400 service_info.txt_for_srv = NULL;
402 if (service_info.host_to_ip) {
403 MdnsdDone (service_info.mdnsd, service_info.host_to_ip);
406 if (service_info.ip_to_host) {
407 MdnsdDone (service_info.mdnsd, service_info.ip_to_host);
410 service_info.host_to_ip = NULL;
411 service_info.ip_to_host = NULL;
412 service_info.announce_ip.s_addr = 0;
414 service_info.state = MDNSD_ANNOUNCE;
421 if (announce_stage < 3) {
422 struct timeval cur_tv;
425 gettimeofday (&cur_tv, NULL);
426 msecs = (cur_tv.tv_sec - tv.tv_sec) * 1000 + cur_tv.tv_usec / 1000 - tv.tv_usec / 1000;
428 if ((tv.tv_sec == 0) || (msecs > 755)) {
429 request_service (&service_info, announce_stage);
432 cur_tv = *MdnsdGetMaxSleepTime (service_info.mdnsd);
433 polltime = cur_tv.tv_sec * 1000 + cur_tv.tv_usec / 1000;
434 if (polltime >= 756) {
438 cur_tv = *MdnsdGetMaxSleepTime (service_info.mdnsd);
439 polltime = cur_tv.tv_sec * 1000 + cur_tv.tv_usec / 1000;
440 if (polltime >= 756 - msecs) {
441 polltime = 756 - msecs;
445 tv = *MdnsdGetMaxSleepTime (service_info.mdnsd);
446 polltime = tv.tv_sec * 1000 + tv.tv_usec / 1000;
448 service_info.state = MDNSD_RUN;
453 tv = *MdnsdGetMaxSleepTime (service_info.mdnsd);
454 polltime = tv.tv_sec * 1000 + tv.tv_usec / 1000;
458 MdnsdShutdown (service_info.mdnsd);
462 fprintf (stderr, "in default???\n");
466 if (fds[0].revents) {
467 while ((bsize = recvfrom (s, buf, MAX_PACKET_LEN, 0, (struct sockaddr*) &from, &ssize)) > 0)
469 bzero (&msg, sizeof (DNSMESSAGE));
470 DnsParseMsg (&msg, buf);
471 MdnsdInput(service_info.mdnsd, &msg,
476 if (bsize < 0 && errno != EAGAIN) {
477 fprintf (stderr, "can't read from socket: %s\n", strerror (errno));
481 while (MdnsdOutput (service_info.mdnsd, &msg, &remote_ip, &port)) {
482 bzero (&to, sizeof (to));
483 to.sin_family = AF_INET;
485 to.sin_addr.s_addr = remote_ip.s_addr;
487 if (sendto (s, DnsMsg2Pkt (&msg), DnsMsgLen(&msg), 0, (struct sockaddr *) &to, sizeof (struct sockaddr_in)) != DnsMsgLen(&msg)) {
488 fprintf (stderr, "can't write to socket: %s\n", strerror(errno));
492 if (service_info.state == MDNSD_SHUTDOWN) {
497 MdnsdShutdown (service_info.mdnsd);
498 MdnsdFree (service_info.mdnsd);