1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
#include "announce.h"
#include "util.h"
static void remove_announcement(flxServer *s, flxAnnouncement *a) {
g_assert(s);
g_assert(a);
flx_time_event_queue_remove(s->time_event_queue, a->time_event);
FLX_LLIST_REMOVE(flxAnnouncement, by_interface, a->interface->announcements, a);
FLX_LLIST_REMOVE(flxAnnouncement, by_entry, a->entry->announcements, a);
g_free(a);
}
static void elapse_announce(flxTimeEvent *e, void *userdata) {
flxAnnouncement *a = userdata;
GTimeVal tv;
gchar *t;
g_assert(e);
g_assert(a);
if (a->n_announced >= 3) {
g_message("Enough announcements for record [%s]", t = flx_record_to_string(a->entry->record));
g_free(t);
remove_announcement(a->server, a);
return;
}
flx_interface_post_response(a->interface, a->entry->record);
a->n_announced++;
g_message("Announcement #%i on interface %s.%i for entry [%s]", a->n_announced, a->interface->hardware->name, a->interface->protocol, t = flx_record_to_string(a->entry->record));
g_free(t);
flx_elapse_time(&tv, 1000, 100);
flx_time_event_queue_update(a->server->time_event_queue, a->time_event, &tv);
}
static void new_announcement(flxServer *s, flxInterface *i, flxServerEntry *e) {
flxAnnouncement *a;
GTimeVal tv;
gchar *t;
g_assert(s);
g_assert(i);
g_assert(e);
if (!flx_interface_match(i, e->interface, e->protocol) || !flx_interface_relevant(i))
return;
/* We don't want duplicates */
for (a = e->announcements; a; a = a->by_entry_next)
if (a->interface == i)
return;
g_message("New announcement on interface %s.%i for entry [%s]", i->hardware->name, i->protocol, t = flx_record_to_string(e->record));
g_free(t);
flx_interface_post_response(i, e->record);
a = g_new(flxAnnouncement, 1);
a->server = s;
a->interface = i;
a->entry = e;
a->n_announced = 1;
FLX_LLIST_PREPEND(flxAnnouncement, by_interface, i->announcements, a);
FLX_LLIST_PREPEND(flxAnnouncement, by_entry, e->announcements, a);
flx_elapse_time(&tv, 1000, 100);
a->time_event = flx_time_event_queue_add(s->time_event_queue, &tv, elapse_announce, a);
}
void flx_announce_interface(flxServer *s, flxInterface *i) {
flxServerEntry *e;
g_assert(s);
g_assert(i);
if (!flx_interface_relevant(i))
return;
for (e = s->entries; e; e = e->entry_next)
new_announcement(s, i, e);
}
void flx_announce_entry(flxServer *s, flxServerEntry *e) {
g_assert(s);
g_assert(e);
if (e->interface > 0) {
if (e->protocol != AF_UNSPEC) {
flxInterface *i;
if ((i = flx_interface_monitor_get_interface(s->monitor, e->interface, e->protocol)))
new_announcement(s, i, e);
} else {
flxHwInterface *hw;
if ((hw = flx_interface_monitor_get_hw_interface(s->monitor, e->interface))) {
flxInterface *i;
for (i = hw->interfaces; i; i = i->by_hardware_next)
new_announcement(s, i, e);
}
}
} else {
flxInterface *i;
for (i = s->monitor->interfaces; i; i = i->interface_next)
new_announcement(s, i, e);
}
}
static flxRecord *make_goodbye_record(flxRecord *r) {
gchar *t;
g_assert(r);
g_message("Preparing goodbye for record [%s]", t = flx_record_to_string(r));
g_free(t);
return flx_record_new(r->key, r->data, r->size, 0);
}
void flx_goodbye_interface(flxServer *s, flxInterface *i, gboolean goodbye) {
g_assert(s);
g_assert(i);
while (i->announcements)
remove_announcement(s, i->announcements);
if (goodbye && flx_interface_relevant(i)) {
flxServerEntry *e;
for (e = s->entries; e; e = e->entry_next)
if (flx_interface_match(i, e->interface, e->protocol)) {
flxRecord *g = make_goodbye_record(e->record);
flx_interface_post_response(i, g);
flx_record_unref(g);
}
}
}
void flx_goodbye_entry(flxServer *s, flxServerEntry *e, gboolean goodbye) {
g_assert(s);
g_assert(e);
while (e->announcements)
remove_announcement(s, e->announcements);
if (goodbye) {
flxRecord *g = make_goodbye_record(e->record);
flx_server_post_response(s, e->interface, e->protocol, g);
flx_record_unref(g);
}
}
void flx_goodbye_all(flxServer *s, gboolean goodbye) {
flxServerEntry *e;
g_assert(s);
for (e = s->entries; e; e = e->entry_next)
flx_goodbye_entry(s, e, goodbye);
}
|