gupnp-white-list.c 11.2 KB
Newer Older
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
/*
 * Copyright (C) 2013 Intel Corporation.
 *
 * Author: Ludovic Ferrandis <ludovic.ferrandis@intel.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/**
 * SECTION:gupnp-white-list
 * @short_description: Class for network filtering.
 *
 * #GUPnPWhiteList handles network filtering. It provides API to manage a list
 * of entries that will be used to filter networks.
 * The #GUPnPWhiteList could be enabled or not. If it's enabled but the entries
 * list is empty, it behaves as disabled.
 */

Ludovic Ferrandis's avatar
Ludovic Ferrandis committed
32
33
#include <string.h>

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
#include "gupnp-white-list.h"

G_DEFINE_TYPE (GUPnPWhiteList,
               gupnp_white_list,
               G_TYPE_OBJECT);

struct _GUPnPWhiteListPrivate {
        gboolean enabled;
        GList *entries;
};

enum {
        PROP_0,
        PROP_ENABLED,
        PROP_ENTRIES
};

enum {
        ENTRY_CHANGE,
        ENABLED,
        SIGNAL_LAST
};

static void
gupnp_white_list_init (GUPnPWhiteList *list)
{
        list->priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
                                                  GUPNP_TYPE_WHITE_LIST,
                                                  GUPnPWhiteListPrivate);

        list->priv->entries = NULL;
}

static void
gupnp_white_list_set_property (GObject      *object,
                               guint         property_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
        GUPnPWhiteList *list;

        list = GUPNP_WHITE_LIST (object);

        switch (property_id) {
        case PROP_ENABLED:
                list->priv->enabled = g_value_get_boolean (value);
                break;
        case PROP_ENTRIES:
                list->priv->entries = g_value_get_pointer (value);
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
                break;
        }
}

static void
gupnp_white_list_get_property (GObject    *object,
                               guint       property_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
        GUPnPWhiteList *list;

        list = GUPNP_WHITE_LIST (object);

        switch (property_id) {
        case PROP_ENABLED:
                g_value_set_boolean (value, list->priv->enabled);
                break;
        case PROP_ENTRIES:
                g_value_set_pointer (value, list->priv->entries);
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
                break;
        }
}

static void
gupnp_white_list_class_finalize (GObject *object)
{
        GUPnPWhiteList *list;
        GObjectClass *object_class;

        list = GUPNP_WHITE_LIST (object);

        g_list_free_full (list->priv->entries, g_free);
        list->priv->entries = NULL;

        /* Call super */
        object_class = G_OBJECT_CLASS (gupnp_white_list_parent_class);
        object_class->finalize (object);
}

static void
gupnp_white_list_class_init (GUPnPWhiteListClass *klass)
{
        GObjectClass *object_class;

        object_class = G_OBJECT_CLASS (klass);

        object_class->set_property = gupnp_white_list_set_property;
        object_class->get_property = gupnp_white_list_get_property;
        object_class->finalize     = gupnp_white_list_class_finalize;

        g_type_class_add_private (klass, sizeof (GUPnPWhiteListPrivate));

        /**
         * GUPnPWhiteList:enabled:
         *
         * Whether this white list is active or not.
         **/
        g_object_class_install_property
                (object_class,
                 PROP_ENABLED,
                 g_param_spec_boolean
                         ("enabled",
                          "Enabled",
                          "TRUE if the white list is active.",
                          FALSE,
                          G_PARAM_CONSTRUCT |
                          G_PARAM_READWRITE |
                          G_PARAM_STATIC_STRINGS));

        /**
         * GUPnPWhiteList:entries:
         *
         * Whether this white list is active or not.
Jens Georg's avatar
Jens Georg committed
163
164
         * Type: GList
         * Transfer: none
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
         **/
        g_object_class_install_property
                (object_class,
                 PROP_ENTRIES,
                 g_param_spec_pointer
                         ("entries",
                          "Entries",
                          "GList of strings that compose the white list.",
                          G_PARAM_CONSTRUCT_ONLY |
                          G_PARAM_READWRITE |
                          G_PARAM_STATIC_STRINGS));
}

/**
 * gupnp_white_list_new:
 *
 * Create a new #GUPnPWhiteList.
 * The white list is disabled by default.
 *
 * Returns: (transfer full): A new #GUPnPWhiteList object.
 **/
GUPnPWhiteList *
gupnp_white_list_new (void)
{
        return g_object_new (GUPNP_TYPE_WHITE_LIST, NULL);
}

/**
Jens Georg's avatar
Jens Georg committed
193
 * gupnp_white_list_set_enabled:
194
195
196
197
198
199
 * @white_list: A #GUPnPWhiteList
 * @enable:  %TRUE to enable @white_list, %FALSE otherwise
 *
 * Enable or disable the #GUPnPWhiteList to perform the network filtering.
**/
void
Jens Georg's avatar
Jens Georg committed
200
gupnp_white_list_set_enabled (GUPnPWhiteList *white_list, gboolean enable)
201
202
203
204
205
206
207
208
{
        g_return_if_fail (GUPNP_IS_WHITE_LIST (white_list));

        white_list->priv->enabled = enable;
        g_object_notify (G_OBJECT (white_list), "enabled");
}

/**
Jens Georg's avatar
Jens Georg committed
209
 * gupnp_white_list_get_enabled:
210
211
212
213
214
215
216
 * @white_list: A #GUPnPWhiteList
 *
 * Return the status of the #GUPnPWhiteList
 *
 * Return value: %TRUE if @white_list is enabled, %FALSE otherwise.
 **/
gboolean
Jens Georg's avatar
Jens Georg committed
217
gupnp_white_list_get_enabled (GUPnPWhiteList *white_list)
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
{
        g_return_val_if_fail (GUPNP_IS_WHITE_LIST (white_list), FALSE);

        return white_list->priv->enabled;
}

/**
 * gupnp_white_list_is_empty:
 * @white_list: A #GUPnPWhiteList
 *
 * Return the state of the entries list of #GUPnPWhiteList
 *
 * Return value: %TRUE if @white_list is empty, %FALSE otherwise.
 **/
gboolean
gupnp_white_list_is_empty (GUPnPWhiteList *white_list)
{
        g_return_val_if_fail (GUPNP_IS_WHITE_LIST (white_list), TRUE);

        return (white_list->priv->entries == NULL);
}

/**
 * gupnp_white_list_add_entry:
 * @white_list: A #GUPnPWhiteList
 * @entry: A value used to filter network
 *
 * Add @entry in the list of valid criteria used by @white_list to
 * filter networks.
 * if @entry already exists, it won't be added a second time.
 *
 * Return value: %TRUE if @entry is added, %FALSE otherwise.
 **/
gboolean
gupnp_white_list_add_entry (GUPnPWhiteList *white_list, gchar* entry)
{
        GList *s_entry;
        GUPnPWhiteListPrivate *priv;

        g_return_val_if_fail (GUPNP_IS_WHITE_LIST (white_list), FALSE);
        g_return_val_if_fail ((entry != NULL), FALSE);

        priv = white_list->priv;

        s_entry = g_list_find_custom (priv->entries, entry,
                                      (GCompareFunc) g_ascii_strcasecmp);

        if (s_entry == NULL) {
                priv->entries = g_list_prepend (priv->entries,
                                                g_strdup (entry));
                g_object_notify (G_OBJECT (white_list), "entries");
        }

        return (s_entry == NULL);
}

/**
 * gupnp_white_list_remove_entry:
 * @white_list: A #GUPnPWhiteList
 * @entry: A value to remove from the filter list.
 *
 * Remove @entry in the list of valid criteria used by @white_list to
 * filter networks.
 *
 * Return value: %TRUE if @entry is removed, %FALSE otherwise.
 **/
gboolean
gupnp_white_list_remove_entry (GUPnPWhiteList *white_list, gchar* entry)
{
        GList *s_entry;
        GUPnPWhiteListPrivate *priv;

        g_return_val_if_fail (GUPNP_IS_WHITE_LIST (white_list), FALSE);
        g_return_val_if_fail ((entry != NULL), FALSE);

        priv = white_list->priv;

        s_entry = g_list_find_custom (priv->entries, entry,
                                      (GCompareFunc) g_ascii_strcasecmp);

        if (s_entry != NULL) {
299
300
                priv->entries = g_list_remove_link (priv->entries, s_entry);
		g_list_free_full (s_entry, g_free);
301
302
303
304
305
306
307
                g_object_notify (G_OBJECT (white_list), "entries");
        }

        return (s_entry != NULL);
}

/**
Jens Georg's avatar
Jens Georg committed
308
 * gupnp_white_list_get_entries:
309
310
311
312
313
314
315
316
317
 * @white_list: A #GUPnPWhiteList
 *
 * Get the #GList of entries that compose the white list. Do not free
 *
 * Return value: (element-type utf8) (transfer none):  a #GList of entries
 * used to filter networks, interfaces,... or %NULL.
 * Do not modify or free the list nor its elements.
 **/
GList *
Jens Georg's avatar
Jens Georg committed
318
gupnp_white_list_get_entries (GUPnPWhiteList *white_list)
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
383
384
385
386
387
388
389
390
{
        g_return_val_if_fail (GUPNP_IS_WHITE_LIST (white_list), NULL);

        return white_list->priv->entries;
}

/**
 * gupnp_white_list_clear:
 * @white_list: A #GUPnPWhiteList
 *
 * Remove all entries from #GList that compose the white list.
 * The list is now empty. Even if #GUPnPWhiteList is enabled, it will have the
 * same behavior as if it was disabled.
**/
void
gupnp_white_list_clear (GUPnPWhiteList *white_list)
{
        GUPnPWhiteListPrivate *priv;

        g_return_if_fail (GUPNP_IS_WHITE_LIST(white_list));

        priv = white_list->priv;
        g_list_free_full (priv->entries, g_free);
        priv->entries = NULL;
        g_object_notify (G_OBJECT (white_list), "entries");
}

/**
 * gupnp_white_list_check_context:
 * @white_list: A #GUPnPWhiteList
 * @context: A #GUPnPContext to test.
 *
 * It will check if the @context is allowed or not. The @white_list will check
 * all its entries againt #GUPnPContext interface, host ip and network fields
 * information. This function doesn't take into account the @white_list status
 * (enabled or not).
 *
 * Return value: %TRUE if @context is matching the @white_list criterias,
 * %FALSE otherwise.
 **/
gboolean
gupnp_white_list_check_context (GUPnPWhiteList *white_list,
                                GUPnPContext *context)
{
        GSSDPClient  *client;
        GList *l;
        const char *interface;
        const char *host_ip;
        const char *network;
        gboolean match = FALSE;

        g_return_val_if_fail (GUPNP_IS_WHITE_LIST (white_list), FALSE);
        g_return_val_if_fail (GUPNP_IS_CONTEXT (context), FALSE);

        client = GSSDP_CLIENT (context);

        interface = gssdp_client_get_interface (client);
        host_ip = gssdp_client_get_host_ip (client);
        network = gssdp_client_get_network (client);

        l = white_list->priv->entries;

        while (l && !match) {
                match = (interface && !strcmp (l->data, interface)) ||
                        (host_ip && !strcmp (l->data, host_ip)) ||
                        (network && !strcmp (l->data, network));

                l = l->next;
        }

        return match;
}