summaryrefslogtreecommitdiff
path: root/libgupnp/gupnp-context-filter.c
blob: 660204cf47e7a349997125ed3104e40dfb79c361 (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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
 * Copyright (C) 2013 Intel Corporation.
 *
 * Author: Ludovic Ferrandis <ludovic.ferrandis@intel.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#define G_LOG_DOMAIN "gupnp-context-filter"

#include "gupnp-context-filter.h"

#include <string.h>

struct _GUPnPContextFilterPrivate {
        gboolean enabled;
        GHashTable *entries;
};
typedef struct _GUPnPContextFilterPrivate GUPnPContextFilterPrivate;

/**
 * GUPnPContextFilter:
 *
 * Network context filter, used by [class@GUPnP.ContextManager]
 *
 * #GUPnPContextFilter handles network filtering. It provides API to manage a
 * list of entries that will be used to positive filter networks. The #GUPnPContextFilter
 * could be enabled or not. If it's enabled but the entries list is empty, it
 * behaves as if being disabled.
 *
 * The GUPnPContextFilter is used with the [class@GUPnP.ContextManager]
 * to narrow down the contexts that are notified by it.
 *
 * Contexts can be filtered by the following criteria:
 *
 *  - Their IP addresses
 *  - The network device they will live on
 *  - The name of the network the context would join
 *
 * To add or modify a context filter, you need to retrieve the current context filter
 * from the context manger using [method@GUPnP.ContextManager.get_context_filter].
 *
 * By default, a context filter is empty and disabled.
 *
 * For example, to only react to contexts that are appearing on eth0 or when being in the WiFi network with
 * the SSID "HomeNetwork", and on IPv6 localhost, you should do:
 *
 *
 * ```c
 * GUPnPContextFilter* filter;
 *
 * filter = gupnp_context_manager_get_context_filter (manager);
 * const char *filter_entries[] = {
 *     "eth0",
 *     "HomeNetwork",
 *     "::1",
 *     NULL
 * };
 * gupnp_context_filter_add_entryv (filter, filter_entries);
 * gupnp_context_filter_set_enabled (filter, TRUE);
 * ```
 *
 * Since: 1.4.0
 */

G_DEFINE_TYPE_WITH_PRIVATE (GUPnPContextFilter,
                            gupnp_context_filter,
                            G_TYPE_OBJECT)

enum
{
        PROP_0,
        PROP_ENABLED,
        PROP_ENTRIES
};

static void
gupnp_context_filter_init (GUPnPContextFilter *list)
{
        GUPnPContextFilterPrivate *priv;

        priv = gupnp_context_filter_get_instance_private (list);

        priv->entries =
                g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
}

static void
gupnp_context_filter_set_property (GObject *object,
                                   guint property_id,
                                   const GValue *value,
                                   GParamSpec *pspec)
{
        GUPnPContextFilter *list;
        GUPnPContextFilterPrivate *priv;

        list = GUPNP_CONTEXT_FILTER (object);
        priv = gupnp_context_filter_get_instance_private (list);

        switch (property_id) {
        case PROP_ENABLED:
                gupnp_context_filter_set_enabled (list,
                                                  g_value_get_boolean (value));
                break;
        case PROP_ENTRIES: {
                g_hash_table_remove_all (priv->entries);
                GPtrArray *array = g_ptr_array_new ();
                GList *entries = g_value_get_pointer (value);
                for (GList *it = entries; it != NULL; it = g_list_next (it)) {
                        g_ptr_array_add (array, it->data);
                }
                g_ptr_array_add (array, NULL);
                gupnp_context_filter_add_entryv (list, (char **) array->pdata);
                g_ptr_array_unref (array);
        } break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
                break;
        }
}

static void
gupnp_context_filter_get_property (GObject *object,
                                   guint property_id,
                                   GValue *value,
                                   GParamSpec *pspec)
{
        GUPnPContextFilter *list;
        GUPnPContextFilterPrivate *priv;

        list = GUPNP_CONTEXT_FILTER (object);
        priv = gupnp_context_filter_get_instance_private (list);

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

static void
gupnp_context_filter_class_finalize (GObject *object)
{
        GUPnPContextFilter *list;
        GObjectClass *object_class;
        GUPnPContextFilterPrivate *priv;

        list = GUPNP_CONTEXT_FILTER (object);
        priv = gupnp_context_filter_get_instance_private (list);

        g_clear_pointer (&priv->entries, g_hash_table_destroy);

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

static void
gupnp_context_filter_class_init (GUPnPContextFilterClass *klass)
{
        GObjectClass *object_class;

        object_class = G_OBJECT_CLASS (klass);

        object_class->set_property = gupnp_context_filter_set_property;
        object_class->get_property = gupnp_context_filter_get_property;
        object_class->finalize = gupnp_context_filter_class_finalize;

        /**
         * GUPnPContextFilter:enabled:(attributes org.gtk.Property.get=gupnp_context_filter_get_enabled org.gtk.Property.set=gupnp_context_filter_set_enabled)
         *
         * Whether this context filter is active or not.
         *
         * Since: 1.4.0
         **/
        g_object_class_install_property (
                object_class,
                PROP_ENABLED,
                g_param_spec_boolean ("enabled",
                                      "Enabled",
                                      "TRUE if the context filter is active.",
                                      FALSE,
                                      G_PARAM_CONSTRUCT | G_PARAM_READWRITE |
                                              G_PARAM_STATIC_STRINGS |
                                              G_PARAM_EXPLICIT_NOTIFY));

        /**
         * GUPnPContextFilter:entries: (type GList(utf8))(attributes org.gtk.Property.get=gupnp_context_filter_get_entries)
         *
         * A list of items to filter for.
         *
         * Since: 1.4.0
         **/
        g_object_class_install_property (
                object_class,
                PROP_ENTRIES,
                g_param_spec_pointer (
                        "entries",
                        "Filter entries",
                        "GList of strings that compose the context filter.",
                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
                                G_PARAM_STATIC_STRINGS |
                                G_PARAM_EXPLICIT_NOTIFY));
}

/**
 * gupnp_context_filter_new:
 *
 * Create a new #GUPnPContextFilter.
 * The context filter is disabled by default.
 *
 * Returns: (transfer full): A new #GUPnPContextFilter object.
 *
 * Since: 1.4.0
 **/
GUPnPContextFilter *
gupnp_context_filter_new (void)
{
        return g_object_new (GUPNP_TYPE_CONTEXT_FILTER, NULL);
}

/**
 * gupnp_context_filter_set_enabled:(attributes org.gtk.Method.set_property=enabled)
 * @context_filter: A #GUPnPContextFilter
 * @enable:  %TRUE to enable @context_filter, %FALSE otherwise
 *
 * Enable or disable the #GUPnPContextFilter to perform the network filtering.
 *
 * Since: 1.4.0
 **/
void
gupnp_context_filter_set_enabled (GUPnPContextFilter *context_filter,
                                  gboolean enable)
{
        GUPnPContextFilterPrivate *priv;

        g_return_if_fail (GUPNP_IS_CONTEXT_FILTER (context_filter));

        priv = gupnp_context_filter_get_instance_private (context_filter);
        if (priv->enabled != enable) {
                priv->enabled = enable;
                g_object_notify (G_OBJECT (context_filter), "enabled");
        }
}

/**
 * gupnp_context_filter_get_enabled:(attributes org.gtk.Method.get_property=enabled)
 * @context_filter: A #GUPnPContextFilter
 *
 * Return the status of the #GUPnPContextFilter
 *
 * Return value: %TRUE if @context_filter is enabled, %FALSE otherwise.
 *
 * Since: 1.4.0
 **/
gboolean
gupnp_context_filter_get_enabled (GUPnPContextFilter *context_filter)
{
        GUPnPContextFilterPrivate *priv;

        g_return_val_if_fail (GUPNP_IS_CONTEXT_FILTER (context_filter), FALSE);

        priv = gupnp_context_filter_get_instance_private (context_filter);

        return priv->enabled;
}

/**
 * gupnp_context_filter_is_empty:
 * @context_filter: A #GUPnPContextFilter
 *
 * Return the state of the entries list of #GUPnPContextFilter
 *
 * Return value: %TRUE if @context_filter is empty, %FALSE otherwise.
 *
 * Since: 1.4.0
 **/
gboolean
gupnp_context_filter_is_empty (GUPnPContextFilter *context_filter)
{
        GUPnPContextFilterPrivate *priv;

        g_return_val_if_fail (GUPNP_IS_CONTEXT_FILTER (context_filter), TRUE);

        priv = gupnp_context_filter_get_instance_private (context_filter);

        return (g_hash_table_size (priv->entries) == 0);
}

/**
 * gupnp_context_filter_add_entry:
 * @context_filter: A #GUPnPContextFilter
 * @entry: A value used to filter network
 *
 * Add @entry in the list of valid criteria used by @context_filter to
 * filter networks.
 * if @entry already exists, it won't be added a second time.
 *
 * Return value: %TRUE if @entry is added, %FALSE otherwise.
 *
 * Since: 1.4.0
 **/
gboolean
gupnp_context_filter_add_entry (GUPnPContextFilter *context_filter,
                                const gchar *entry)
{
        GUPnPContextFilterPrivate *priv;

        g_return_val_if_fail (GUPNP_IS_CONTEXT_FILTER (context_filter), FALSE);
        g_return_val_if_fail ((entry != NULL), FALSE);

        priv = gupnp_context_filter_get_instance_private (context_filter);

        if (g_hash_table_add (priv->entries, g_strdup (entry))) {
                g_object_notify (G_OBJECT (context_filter), "entries");

                return TRUE;
        }

        return FALSE;
}

/**
 * gupnp_context_filter_add_entryv:
 * @context_filter: A #GUPnPContextFilter
 * @entries: (array zero-terminated=1): A %NULL-terminated list of strings
 *
 * Add a list of entries to a #GUPnPContextFilter. This is a helper function to
 * directly add a %NULL-terminated array of string usually acquired from
 * command line arguments.
 *
 * Since: 1.4.0
 */
void
gupnp_context_filter_add_entryv (GUPnPContextFilter *context_filter,
                                 gchar **entries)
{
        gchar *const *iter = entries;
        GUPnPContextFilterPrivate *priv;

        g_return_if_fail (GUPNP_IS_CONTEXT_FILTER (context_filter));
        g_return_if_fail ((entries != NULL));

        priv = gupnp_context_filter_get_instance_private (context_filter);
        gboolean changed = FALSE;
        for (; *iter != NULL; iter++) {
                if (g_hash_table_add (priv->entries, g_strdup (*iter)))
                        changed = TRUE;
        }

        if (changed)
                g_object_notify (G_OBJECT (context_filter), "entries");
}

/**
 * gupnp_context_filter_remove_entry:
 * @context_filter: A #GUPnPContextFilter
 * @entry: A value to remove from the filter list.
 *
 * Remove @entry in the list of valid criteria used by @context_filter to
 * filter networks.
 *
 * Return value: %TRUE if @entry is removed, %FALSE otherwise.
 *
 * Since: 1.4.0
 **/
gboolean
gupnp_context_filter_remove_entry (GUPnPContextFilter *context_filter,
                                   const gchar *entry)
{
        GUPnPContextFilterPrivate *priv;

        g_return_val_if_fail (GUPNP_IS_CONTEXT_FILTER (context_filter), FALSE);
        g_return_val_if_fail ((entry != NULL), FALSE);

        priv = gupnp_context_filter_get_instance_private (context_filter);

        if (g_hash_table_remove (priv->entries, entry)) {
                g_object_notify (G_OBJECT (context_filter), "entries");

                return TRUE;
        }

        return FALSE;
}

/**
 * gupnp_context_filter_get_entries:
 * @context_filter: A #GUPnPContextFilter
 *
 * Get the #GList of entries that compose the context filter. Do not free
 *
 * Return value: (element-type utf8) (transfer container)(nullable):  a #GList of entries
 * used to filter networks, interfaces,... or %NULL.
 *
 * Since: 1.4.0
 **/
GList *
gupnp_context_filter_get_entries (GUPnPContextFilter *context_filter)
{
        GUPnPContextFilterPrivate *priv;

        g_return_val_if_fail (GUPNP_IS_CONTEXT_FILTER (context_filter), NULL);

        priv = gupnp_context_filter_get_instance_private (context_filter);

        return g_hash_table_get_keys (priv->entries);
}

/**
 * gupnp_context_filter_clear:
 * @context_filter: A #GUPnPContextFilter
 *
 * Remove all entries from #GList that compose the context filter.
 * The list is now empty. Even if #GUPnPContextFilter is enabled, it will have
 * the same behavior as if it was disabled.
 *
 * Since: 1.4.0
 **/
void
gupnp_context_filter_clear (GUPnPContextFilter *context_filter)
{
        GUPnPContextFilterPrivate *priv;

        g_return_if_fail (GUPNP_IS_CONTEXT_FILTER (context_filter));

        priv = gupnp_context_filter_get_instance_private (context_filter);
        g_hash_table_remove_all (priv->entries);

        g_object_notify (G_OBJECT (context_filter), "entries");
}

/**
 * gupnp_context_filter_check_context:
 * @context_filter: A #GUPnPContextFilter
 * @context: A #GUPnPContext to test.
 *
 * It will check if the @context is allowed or not. The @context_filter will
 * check all its entries against #GUPnPContext interface, host IP and network
 * fields information. This function doesn't take into account the
 * @context_filter status (enabled or not).
 *
 * Return value: %TRUE if @context is matching the @context_filter criteria,
 * %FALSE otherwise.
 *
 * Since: 1.4.0
 **/
gboolean
gupnp_context_filter_check_context (GUPnPContextFilter *context_filter,
                                    GUPnPContext *context)
{
        GSSDPClient *client;
        const char *interface;
        const char *host_ip;
        const char *network;
        GUPnPContextFilterPrivate *priv;

        g_return_val_if_fail (GUPNP_IS_CONTEXT_FILTER (context_filter), FALSE);
        g_return_val_if_fail (GUPNP_IS_CONTEXT (context), FALSE);

        client = GSSDP_CLIENT (context);
        priv = gupnp_context_filter_get_instance_private (context_filter);

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

        return g_hash_table_contains (priv->entries, interface) ||
               g_hash_table_contains (priv->entries, host_ip) ||
               g_hash_table_contains (priv->entries, network);
}