summaryrefslogtreecommitdiff
path: root/cleopatre/application/libspid/src/multicast_info.c
blob: 1e3dacabfdd99f4fb415f060f8afe9411e22fbaa (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
/* SPC300 bundle {{{
 *
 * Copyright (C) 2009 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    application/libspid/src/multicast_info.c
 * \brief   functions for reading/writing the multicast info file.
 * \ingroup libspid
 *
 */

#include "multicast_info.h"

#include <stdio.h>
#include <string.h>

#include "libspid.h"

/* The mcast.info file has the following format:
 * group1_mac_addr<delimiter>member1_mac_addr<delimiter>member2_mac_addr<delimiter>membern_mac_addr<delimiter>
 * group2_mac_addr<delimiter>member1_mac_addr<delimiter>member2_mac_addr<delimiter>membern_mac_addr<delimiter>
 * groupn_mac_addr<delimiter>member1_mac_addr<delimiter>member2_mac_addr<delimiter>membern_mac_addr<delimiter>
 *
 * So we have at most (1 + LIBSPID_MULTICAST_INFO_MEMBER_MAX_NB) mac_address and
 * delimiter.
 * We add then length of one mac_address and delimiter, to be able to detect
 * if there are more members than expected.
 * and each line ends with a \n
 * and then we have the \0
 */
#define MULTICAST_INFO_LINE_MAX_SIZE    \
    (((LIBSPID_MAC_STR_LEN + strlen(LIBSPID_MULTICAST_INFO_DELIMITER)) \
      * (1 + LIBSPID_MULTICAST_INFO_MEMBER_MAX_NB + 1)) \
     + 1 \
     + 1 )

/**
 * Tell whether the provided MAC address is a multicast address or not.
 * \param  mac_addr  the multicast address to test.
 * \return  LIBSPID_TRUE if it's a multicast address,
 *          LIBSPID_FALSE if it's not.
 */
static libspid_boolean_t
is_multicast_mac_addr (const unsigned char *mac_addr)
{
    return ((mac_addr[0] == 0x01)
         && (mac_addr[1] == 0x00)
         && (mac_addr[2] == 0x5e));
}

/**
 * Read a file to extract the multicast groups and their members.
 * \param  mcast_info  The multicast info to fill from the file.
 * \param  read_status  Details about the reading of the file, afterwards.
 * \return  LIBSPID_SUCCESS, on success.
 *          LIBSPID_ERROR_PROCESSING, if a processing related error occured.
 *              see read_status for more info.
 *          LIBSPID_ERROR_PARAM, input parameters are invalid.
 *          LIBSPID_ERROR_SYSTEM, if a system error occured
 *              (check errno for more info)
 */
libspid_error_t
libspid_multicast_info_read_file (libspid_multicast_info_t *mcast_info,
                                  libspid_multicast_info_read_status_t *read_status)
{
    if ((mcast_info == NULL) || (read_status == NULL))
    {
        return LIBSPID_ERROR_PARAM;
    }

    *read_status = 0;
    mcast_info->entry_count = 0;

    FILE *fp = fopen (LIBSPID_MULTICAST_INFO_PATH, "r");
    if (fp == NULL)
    {
        return LIBSPID_ERROR_SYSTEM;
    }

    char line[MULTICAST_INFO_LINE_MAX_SIZE];

    uint8_t group_idx = 0;
    char *ret;
    while (((ret = fgets (line, sizeof (line), fp)) != NULL)
          && (group_idx < LIBSPID_MULTICAST_INFO_GROUP_MAX_NB))
    {

        {
            /* Remove \n to help strtok */

            char *last_char = &line[strlen (line) - 1];
            if (*last_char == '\n')
            {
                *last_char = '\0';
            }
        }

        /* The Group */
        char *token;
        token = strtok (line, LIBSPID_MULTICAST_INFO_DELIMITER); /* TODO: strtok_r ? */
        if ((token == NULL)
            || ((strlen (token) + 1) != LIBSPID_MAC_STR_LEN))
        {
            *read_status |= LIBSPID_MULTICAST_INFO_READ_PARSING_ERROR;
            return LIBSPID_ERROR_PROCESSING;
        }

        libspid_multicast_info_entry_t *entry = &mcast_info->entries[group_idx];

        libspid_error_t status;

        status = libspid_mac_str_to_bin (token, entry->group_mac_addr);
        if (status != LIBSPID_SUCCESS)
        {
            *read_status |= LIBSPID_MULTICAST_INFO_READ_PARSING_ERROR;
            return LIBSPID_ERROR_PROCESSING;
        }

        if (!is_multicast_mac_addr (entry->group_mac_addr))
        {
            *read_status |= LIBSPID_MULTICAST_INFO_READ_NOT_MULTICAST_ADDR;
            return LIBSPID_ERROR_PROCESSING;
        }

        /* The members */
        uint8_t member_idx = 0;
        while (((token = strtok (NULL, LIBSPID_MULTICAST_INFO_DELIMITER)) != NULL) /* TODO: strtok_r ? */
                && (member_idx < LIBSPID_MULTICAST_INFO_MEMBER_MAX_NB))
        {
            if ((strlen (token) + 1) != LIBSPID_MAC_STR_LEN)
            {
                *read_status |= LIBSPID_MULTICAST_INFO_READ_PARSING_ERROR;
                return LIBSPID_ERROR_PROCESSING;
            }

            status = libspid_mac_str_to_bin (token,
                                             entry->members_mac_addr[member_idx]);
            if (status != LIBSPID_SUCCESS)
            {
                *read_status |= LIBSPID_MULTICAST_INFO_READ_PARSING_ERROR;
                return LIBSPID_ERROR_PROCESSING;
            }

            member_idx++;
        }

        if ((token != NULL) && (member_idx == LIBSPID_MULTICAST_INFO_MEMBER_MAX_NB))
        {
             /* Too many members, so we won't store this group.
              * If we store and send only the LIBSPID_MULTICAST_INFO_MEMBER_MAX_NB
              * first members to cesar, the other members won't receive packets */

            *read_status |= LIBSPID_MULTICAST_INFO_READ_TOO_MANY_MEMBERS;
            continue;
        }

        if ((token == NULL) & (member_idx == 0))
        {
            *read_status |= LIBSPID_MULTICAST_INFO_READ_EMPTY_GROUP;
            return LIBSPID_ERROR_PROCESSING;
        }

        entry->member_count = member_idx;
        group_idx++;
    }

    if ((ret != NULL) && (group_idx == LIBSPID_MULTICAST_INFO_GROUP_MAX_NB))
    {
        *read_status |= LIBSPID_MULTICAST_INFO_READ_TOO_MANY_GROUPS;
    }

    mcast_info->entry_count = group_idx;

    return LIBSPID_SUCCESS;
}

/**
 * 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
libspid_write_mac_to_file (FILE *fp, const unsigned char *mac_bin)
{
    assert (fp != NULL);
    assert (mac_bin != NULL);

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

/**
 * Write the multicast info to the file.
 * \param  mcast_info  The multicast info to use to write the file.
 *                     Passing NULL, writes an empty file.
 * \return  LIBSPID_SUCCESS, on success.
 *          LIBSPID_ERROR_SYSTEM, if a system error occured.
 */
libspid_error_t
libspid_multicast_info_write_file (libspid_multicast_info_t *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 */

    const char *path = LIBSPID_MULTICAST_INFO_PATH;

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

    int fd = mkstemp (tmp);
    if (fd == -1)
    {
        return LIBSPID_ERROR_SYSTEM;
    }

    FILE *fp;
    fp = fdopen (fd, "w");
    if (fp == NULL)
    {
        return LIBSPID_ERROR_SYSTEM;
    }

    unsigned int entry_count = (mcast_info != NULL) ? mcast_info->entry_count : 0;

    unsigned grp_idx;
    for (grp_idx = 0; grp_idx < entry_count; grp_idx++)
    {
        const libspid_multicast_info_entry_t *entry = &mcast_info->entries[grp_idx];

        libspid_write_mac_to_file (fp, entry->group_mac_addr);

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

        fprintf (fp, "\n");
    }

    if (fclose (fp) != 0)
    {
        return LIBSPID_ERROR_SYSTEM;
    }

    if (rename (tmp, path) == -1)
    {
        return LIBSPID_ERROR_SYSTEM;
    }

    return LIBSPID_SUCCESS;
}