4971c4e3e70eedbca333612a926d83c582caf7cf
[project/mdnsd.git] / announce.c
1 /*
2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14 #include <sys/types.h>
15
16 #include <stdio.h>
17
18 #include <libubox/uloop.h>
19
20 #include "cache.h"
21 #include "dns.h"
22 #include "util.h"
23 #include "service.h"
24 #include "announce.h"
25 #include "interface.h"
26
27 #define TTL_TIMEOUT 75
28
29 enum {
30 STATE_PROBE1 = 0,
31 STATE_PROBE2,
32 STATE_PROBE3,
33 STATE_PROBE_WAIT,
34 STATE_PROBE_END,
35 STATE_ANNOUNCE,
36 };
37
38 static struct uloop_timeout announce;
39 static int announce_state;
40 int announce_ttl = 75 * 60;
41
42 static void
43 announce_timer(struct uloop_timeout *timeout)
44 {
45 char host[256];
46
47 snprintf(host, sizeof(host), "%s.local", hostname);
48
49 switch (announce_state) {
50 case STATE_PROBE1:
51 case STATE_PROBE2:
52 case STATE_PROBE3:
53 dns_send_question(cur_iface, host, TYPE_ANY);
54 uloop_timeout_set(timeout, 250);
55 announce_state++;
56 break;
57
58 case STATE_PROBE_WAIT:
59 uloop_timeout_set(timeout, 500);
60 announce_state++;
61 break;
62
63 case STATE_PROBE_END:
64 if (cache_host_is_known(host)) {
65 fprintf(stderr, "the host %s already exists. stopping announce service\n", host);
66 return;
67 }
68 announce_state++;
69
70 case STATE_ANNOUNCE:
71 service_announce(cur_iface);
72 uloop_timeout_set(timeout, announce_ttl * 800);
73 break;
74 }
75 }
76
77 void
78 announce_init(void)
79 {
80 announce_state = STATE_PROBE1;
81 announce.cb = announce_timer;
82 uloop_timeout_set(&announce, 100);
83 }