summaryrefslogtreecommitdiffstats
path: root/announce.c
blob: 9229753d9051beb9a78153f0038ddab9740413c3 (plain)
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
#include "announce.h"
#include "util.h"

#define FLX_ANNOUNCEMENT_JITTER_MSEC 0

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);

    flx_interface_post_response(a->interface, NULL, a->entry->record, FALSE);

    if (a->n_announced++ <= 8)
        a->sec_delay *= 2;

    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);

    if (a->n_announced >= 4) {
        g_message("Enough announcements for record [%s]", t = flx_record_to_string(a->entry->record));
        g_free(t);
        remove_announcement(a->server, a);
    } else { 
        flx_elapse_time(&tv, a->sec_delay*1000, FLX_ANNOUNCEMENT_JITTER_MSEC);
        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);

    g_message("NEW ANNOUNCEMENT: %s.%i [%s]", i->hardware->name, i->protocol, t = flx_record_to_string(e->record));
    g_free(t);
    
    if (!flx_interface_match(i, e->interface, e->protocol) || !i->announcing)
        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, NULL, e->record, FALSE);
    
    a = g_new(flxAnnouncement, 1);
    a->server = s;
    a->interface = i;
    a->entry = e;
    a->n_announced = 1;
    a->sec_delay = 1;
    
    FLX_LLIST_PREPEND(flxAnnouncement, by_interface, i->announcements, a);
    FLX_LLIST_PREPEND(flxAnnouncement, by_entry, e->announcements, a);
    
    flx_elapse_time(&tv, a->sec_delay*1000, FLX_ANNOUNCEMENT_JITTER_MSEC);
    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 (!i->announcing)
        return;

    g_message("ANNOUNCE INTERFACE");
    
    for (e = s->entries; e; e = e->entry_next)
        new_announcement(s, i, e);
}

static void announce_walk_callback(flxInterfaceMonitor *m, flxInterface *i, gpointer userdata) {
    flxServerEntry *e = userdata;
    
    g_assert(m);
    g_assert(i);
    g_assert(e);

    new_announcement(m->server, i, e);
}

void flx_announce_entry(flxServer *s, flxServerEntry *e) {
    g_assert(s);
    g_assert(e);

    g_message("ANNOUNCE ENTRY");

    flx_interface_monitor_walk(s->monitor, e->interface, e->protocol, announce_walk_callback, 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, NULL, g, TRUE);
                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);
}