summaryrefslogtreecommitdiff
path: root/cleopatre/application/igmp_snoopd/src/write_file.c
blob: e03ffc2e5a9cfc1856218379d6fdd3889ba75ea6 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include "write_file.h"

#include <errno.h>
#include <stdlib.h>
#include <string.h>

#include "libspid.h"

#include "igmp_common.h"
#include "utils.h"

/** A member of the union list. */
struct union_member
{
    /* A member. */
    const struct group_member *member;

    /** List management. */
    LIST_ENTRY (union_member) union_members;
};

/** When multiple groups have the same mapped Ethernet MAC address,
 * the list below is used to create a union of their members. */
struct union_list
{
    /** List management. */
    LIST_HEAD (list_head_union_members, union_member) head;

    /** The number of members in the union. */
    unsigned int member_count;
};

/**
 * Initialize the union list.
 * \param  u_list  The union list.
 */
static void
union_init_list (struct union_list *u_list)
{
    LIST_INIT (&u_list->head);
    u_list->member_count = 0;
}

/**
 * Add a group_member to the union list.
 * The union list is sorted by eth_addr, just like the list of members
 * in a group.
 *
 * \param  u_list  The union list.
 * \param  member  The member to insert into the union list.
 */
static void
union_add_member (struct union_list *u_list, const struct group_member *member)
{
    struct union_member *prev_u_member = NULL;
    struct union_member *u_member = NULL;
    LIST_FOREACH (u_member, &u_list->head, union_members)
    {
        int memcmp_result
            = memcmp (member->eth_addr, u_member->member->eth_addr, ETH_ALEN);

        if (memcmp_result == 0)
        {
            /* The member is already present in the union list.
             * Nothing to do. */
            return;
        }
        else if (memcmp_result < 0)
        {
            break;
        }

        prev_u_member = u_member;
    }

    struct union_member *new_u_member = malloc (sizeof (struct union_member));
    new_u_member->member = member;

    if (prev_u_member)
    {
        LIST_INSERT_AFTER (prev_u_member, new_u_member, union_members);
    }
    else
    {
        LIST_INSERT_HEAD (&u_list->head, new_u_member, union_members);
    }

    u_list->member_count++;
}

/**
 * Add a list of group_members to the union list.
 * \param  u_list  The union list.
 * \param  grp  The group containing the members to add.
 */
static void
union_add_members (struct union_list *u_list,
                   const struct group *grp)
{
    struct group_member *member;
    LIST_FOREACH (member, &grp->members_head, members)
    {
        union_add_member (u_list, member);
    }
}

/**
 * Fill the members part in the multicast info entry.
 * \param  entry  The entry to update.
 * \param  grp  The group, to retrieve its members.
 */
static void
fill_entry_members (struct multicast_info_entry *entry,
                    const struct group *grp)
{
    unsigned int member_idx = 0;
    struct group_member *member;
    LIST_FOREACH (member, &grp->members_head, members)
    {
        memcpy (entry->members_mac_addr[member_idx], member->eth_addr,
                LIBSPID_MAC_BIN_LEN);

        member_idx++;
    }

    entry->member_count = member_idx;
}

/**
 * Fill the members part in the multicast info entry, using the union list.
 * \param  entry  The entry to update.
 * \param  u_list  The union list.
 */
static void
fill_entry_members_from_union (struct multicast_info_entry *entry,
                               struct union_list *u_list)
{
    /* Iterate through the union list to write the members.
     * The union list is deleted in the process */

    unsigned int member_idx = 0;
    struct union_member *u_member = LIST_FIRST (&u_list->head);
    while (u_member)
    {
        memcpy (entry->members_mac_addr[member_idx],
                u_member->member->eth_addr,
                LIBSPID_MAC_BIN_LEN);

        struct union_member *bak = u_member;
        u_member = LIST_NEXT (u_member, union_members);
        free(bak);

        member_idx++;
    }

    union_init_list (u_list);

    entry->member_count = member_idx;
}

/**
 * Fill a multicast info entry.
 * \param  entry  The entry to fill.
 * \param  grp  The group to use to fill the entry.
 * \param  u_list  The union list to use (eventually) to fill the entry.
 * \return   0, on success.
 *          -1, if there were too many members.
 */
static int
fill_entry (struct multicast_info_entry *entry,
            const struct group *grp,
            struct union_list *u_list)
{
    if (u_list->member_count == 0)
    {
        /* The multicast IP address of the current group doesn't map to
         * the Ethernet MAC address of another group.
         * We can write the members directly. */

        if (grp->member_count <= MCAST_MEMBER_MAX_NB)
        {
            fill_entry_members (entry, grp);
        }
        else
        {
            return -1;
        }
    }
    else
    {
        /* Add the members of the last group */
        union_add_members (u_list, grp);

        if (u_list->member_count <= MCAST_MEMBER_MAX_NB)
        {
            fill_entry_members_from_union (entry, u_list);
        }
        else
        {
            return -1;
        }
     }

    /* Now that members are copied and ok, copy the MAC address of the group */
     memcpy (entry->group_mac_addr, grp->mapped_eth_addr, LIBSPID_MAC_BIN_LEN);

     return 0;
}

/**
 * Fill the multicast info.
 * \param  multicast_info  The multicast info to fill.
 * \param  groups_head  The head of the group list to use.
 */
static void
fill_multicast_info (struct multicast_info *multicast_info,
                     const struct list_head_groups *groups_head)
{
    /* We cannot just copy directly the data from groups_head
     * because some groups map to the same Ethernet MAC address.
     * For those groups, we have to first create a union of their members. */

    /* Union list of members */
    struct union_list u_list;
    union_init_list (&u_list);

    unsigned int grp_idx = 0;
    struct group *grp = LIST_FIRST (groups_head);
    while (grp && (grp_idx < MCAST_GROUP_MAX_NB))
    {
        struct group *next_grp = LIST_NEXT (grp, groups);

        if (next_grp
            && CMP_MCAST_ETH_ADDR (grp->mapped_eth_addr,
                                   next_grp->mapped_eth_addr) == 0)
        {
            /* The next group's multicast IP address maps to the same Ethernet
             * MAC address as the one for the current group. We cannot write
             * the members yet, we have first to create a union of the members. */

            union_add_members (&u_list, grp);
        }
        else
        {
            /* This group is the last of a series of groups that share the same
             * mapped_eth_addr, or it is the only group with that MAC address.
             * So we can generate the entry. */

            int ret = fill_entry (&multicast_info->entries[grp_idx], grp, &u_list);

            if (ret == 0)
            {
                grp_idx++;
            }
            else if (ret == -1)
            {
                log_debug ("too many members");
            }
        }

        grp = next_grp;
    }

    multicast_info->entry_count = grp_idx;

    if ((grp_idx == MCAST_GROUP_MAX_NB) && grp != NULL)
    {
        log_debug ("too many groups");
    }
}

bool
update_multicast_info (struct multicast_info **multicast_info,
                       const struct list_head_groups *groups_head)
{
    struct multicast_info *new_multicast_info =
        malloc (sizeof (struct multicast_info));

    if (new_multicast_info == NULL)
    {
        log_error ("malloc error");

        if (*multicast_info != NULL)
        {
            free (*multicast_info);
            *multicast_info = NULL;
        }

        return true;
    }

    /* memset() to ensure correctness of memcmp() */
    memset (new_multicast_info, 0, sizeof (struct multicast_info));

    fill_multicast_info (new_multicast_info, groups_head);

    if ((*multicast_info == NULL)
        || (memcmp (*multicast_info, new_multicast_info,
                    sizeof (struct multicast_info)) != 0))
    {
        free (*multicast_info);
        *multicast_info = new_multicast_info;
        return true;
    }
    else
    {
        free (new_multicast_info);
        return false;
    }
}

/**
 * Write the MAC address to the file in string format.
 * \param  fp  The file stream.
 * \param  mac_bin  The MAC address in binary format.
 */
static void
write_mac_to_file (FILE *fp, const unsigned char *mac_bin)
{
    char mac_str[LIBSPID_MAC_STR_LEN];
    libspid_error_t status;

    status = libspid_mac_bin_to_str (mac_bin, mac_str);
    assert(status == LIBSPID_SUCCESS);
    fprintf (fp, "%s%s", mac_str, LIBSPID_MCAST_INFO_DELIMITER);
}

void
write_to_file (const char *path, struct multicast_info *mcast_info)
{
    /* The new group and members info must not be partially visible.
     * The file must be written atomically.
     * To do that, the info is first written to a temporary file,
     * then this temporary file is renamed (atomically) to the final file */

    char tmp[strlen (path) + 7 + 1];
    sprintf (tmp, "%s.XXXXXX", path);

    int fd = mkstemp (tmp);
    if (fd == -1)
    {
        log_error ("mkstemp: %s", strerror (errno));
        return;
    }

    FILE *fp;
    fp = fdopen (fd, "w");
    if (fp == NULL)
    {
        log_error ("fopen: %s", strerror (errno));
        return;
    }

    if (mcast_info != NULL)
    {
        unsigned grp_idx;
        for (grp_idx = 0; grp_idx < mcast_info->entry_count; grp_idx++)
        {
            const struct multicast_info_entry *entry = &mcast_info->entries[grp_idx];

            write_mac_to_file (fp, entry->group_mac_addr);

            unsigned int member_idx;
            for (member_idx = 0; member_idx < entry->member_count; member_idx++)
            {
                write_mac_to_file (fp, entry->members_mac_addr[member_idx]);
            }

            fprintf (fp, "\n");
        }
    }

    if (fclose (fp) != 0)
    {
        log_error ("fclose: %s", strerror (errno));
    }

    if (rename (tmp, path) == -1)
    {
        log_error ("rename: %s", strerror (errno));
    }
}