initial code refactoring to prepare for adding dynamic interface support
[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 struct uloop_fd *announce_fd;
40 static int announce_state;
41 int announce_ttl = 75 * 60;
42
43 static void
44 announce_timer(struct uloop_timeout *timeout)
45 {
46         char host[256];
47
48         snprintf(host, sizeof(host), "%s.local", hostname);
49
50         switch (announce_state) {
51                 case STATE_PROBE1:
52                 case STATE_PROBE2:
53                 case STATE_PROBE3:
54                         dns_send_question(cur_iface, host, TYPE_ANY);
55                         uloop_timeout_set(timeout, 250);
56                         announce_state++;
57                         break;
58
59                 case STATE_PROBE_WAIT:
60                         uloop_timeout_set(timeout, 500);
61                         announce_state++;
62                         break;
63
64                 case STATE_PROBE_END:
65                         if (cache_host_is_known(host)) {
66                                 fprintf(stderr, "the host %s already exists. stopping announce service\n", host);
67                                 return;
68                         }
69                         announce_state++;
70
71                 case STATE_ANNOUNCE:
72                         service_announce(announce_fd);
73                         uloop_timeout_set(timeout, announce_ttl * 800);
74                         break;
75         }
76 }
77
78 void
79 announce_init(struct uloop_fd *u)
80 {
81         announce_state = STATE_PROBE1;
82         announce.cb = announce_timer;
83         announce_fd = u;
84         uloop_timeout_set(&announce, 100);
85 }