2 * Copyright (C) 2013 Intel Corporation.
4 * Author: Ludovic Ferrandis <ludovic.ferrandis@intel.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:gupnp-white-list
24 * @short_description: Class for network filtering.
26 * #GUPnPWhiteList handles network filtering. It provides API to manage a list
27 * of entries that will be used to filter networks.
28 * The #GUPnPWhiteList could be enabled or not. If it's enabled but the entries
29 * list is empty, it behaves as disabled.
36 #include "gupnp-white-list.h"
38 G_DEFINE_TYPE (GUPnPWhiteList,
42 struct _GUPnPWhiteListPrivate {
60 gupnp_white_list_init (GUPnPWhiteList *list)
62 list->priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
63 GUPNP_TYPE_WHITE_LIST,
64 GUPnPWhiteListPrivate);
66 list->priv->entries = NULL;
70 gupnp_white_list_set_property (GObject *object,
77 list = GUPNP_WHITE_LIST (object);
79 switch (property_id) {
81 list->priv->enabled = g_value_get_boolean (value);
84 list->priv->entries = g_value_get_pointer (value);
87 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
93 gupnp_white_list_get_property (GObject *object,
100 list = GUPNP_WHITE_LIST (object);
102 switch (property_id) {
104 g_value_set_boolean (value, list->priv->enabled);
107 g_value_set_pointer (value, list->priv->entries);
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
116 gupnp_white_list_class_finalize (GObject *object)
118 GUPnPWhiteList *list;
119 GObjectClass *object_class;
121 list = GUPNP_WHITE_LIST (object);
123 g_list_free_full (list->priv->entries, g_free);
124 list->priv->entries = NULL;
127 object_class = G_OBJECT_CLASS (gupnp_white_list_parent_class);
128 object_class->finalize (object);
132 gupnp_white_list_class_init (GUPnPWhiteListClass *klass)
134 GObjectClass *object_class;
136 object_class = G_OBJECT_CLASS (klass);
138 object_class->set_property = gupnp_white_list_set_property;
139 object_class->get_property = gupnp_white_list_get_property;
140 object_class->finalize = gupnp_white_list_class_finalize;
142 g_type_class_add_private (klass, sizeof (GUPnPWhiteListPrivate));
145 * GUPnPWhiteList:enabled:
147 * Whether this white list is active or not.
151 g_object_class_install_property
157 "TRUE if the white list is active.",
161 G_PARAM_STATIC_STRINGS));
164 * GUPnPWhiteList:entries: (type GList(utf8))
166 * Whether this white list is active or not.
170 g_object_class_install_property
176 "GList of strings that compose the white list.",
177 G_PARAM_CONSTRUCT_ONLY |
179 G_PARAM_STATIC_STRINGS));
183 * gupnp_white_list_new:
185 * Create a new #GUPnPWhiteList.
186 * The white list is disabled by default.
188 * Returns: (transfer full): A new #GUPnPWhiteList object.
193 gupnp_white_list_new (void)
195 return g_object_new (GUPNP_TYPE_WHITE_LIST, NULL);
199 * gupnp_white_list_set_enabled:
200 * @white_list: A #GUPnPWhiteList
201 * @enable: %TRUE to enable @white_list, %FALSE otherwise
203 * Enable or disable the #GUPnPWhiteList to perform the network filtering.
208 gupnp_white_list_set_enabled (GUPnPWhiteList *white_list, gboolean enable)
210 g_return_if_fail (GUPNP_IS_WHITE_LIST (white_list));
212 white_list->priv->enabled = enable;
213 g_object_notify (G_OBJECT (white_list), "enabled");
217 * gupnp_white_list_get_enabled:
218 * @white_list: A #GUPnPWhiteList
220 * Return the status of the #GUPnPWhiteList
222 * Return value: %TRUE if @white_list is enabled, %FALSE otherwise.
227 gupnp_white_list_get_enabled (GUPnPWhiteList *white_list)
229 g_return_val_if_fail (GUPNP_IS_WHITE_LIST (white_list), FALSE);
231 return white_list->priv->enabled;
235 * gupnp_white_list_is_empty:
236 * @white_list: A #GUPnPWhiteList
238 * Return the state of the entries list of #GUPnPWhiteList
240 * Return value: %TRUE if @white_list is empty, %FALSE otherwise.
245 gupnp_white_list_is_empty (GUPnPWhiteList *white_list)
247 g_return_val_if_fail (GUPNP_IS_WHITE_LIST (white_list), TRUE);
249 return (white_list->priv->entries == NULL);
253 * gupnp_white_list_add_entry:
254 * @white_list: A #GUPnPWhiteList
255 * @entry: A value used to filter network
257 * Add @entry in the list of valid criteria used by @white_list to
259 * if @entry already exists, it won't be added a second time.
261 * Return value: %TRUE if @entry is added, %FALSE otherwise.
266 gupnp_white_list_add_entry (GUPnPWhiteList *white_list, gchar* entry)
269 GUPnPWhiteListPrivate *priv;
271 g_return_val_if_fail (GUPNP_IS_WHITE_LIST (white_list), FALSE);
272 g_return_val_if_fail ((entry != NULL), FALSE);
274 priv = white_list->priv;
276 s_entry = g_list_find_custom (priv->entries, entry,
277 (GCompareFunc) g_ascii_strcasecmp);
279 if (s_entry == NULL) {
280 priv->entries = g_list_prepend (priv->entries,
282 g_object_notify (G_OBJECT (white_list), "entries");
285 return (s_entry == NULL);
289 * gupnp_white_list_add_entryv:
290 * @white_list: A #GUPnPWhiteList
291 * @entries: (array zero-terminated=1): A %NULL-terminated list of strings
293 * Add a list of entries to a #GUPnPWhiteList. This is a helper function to
294 * directly add a %NULL-terminated array of string usually aquired from
300 gupnp_white_list_add_entryv (GUPnPWhiteList *white_list, gchar **entries)
302 gchar * const * iter = entries;
304 g_return_if_fail (GUPNP_IS_WHITE_LIST (white_list));
305 g_return_if_fail ((entries != NULL));
307 for (; *iter != NULL; iter++)
308 gupnp_white_list_add_entry (white_list, *iter);
312 * gupnp_white_list_remove_entry:
313 * @white_list: A #GUPnPWhiteList
314 * @entry: A value to remove from the filter list.
316 * Remove @entry in the list of valid criteria used by @white_list to
319 * Return value: %TRUE if @entry is removed, %FALSE otherwise.
324 gupnp_white_list_remove_entry (GUPnPWhiteList *white_list, gchar* entry)
327 GUPnPWhiteListPrivate *priv;
329 g_return_val_if_fail (GUPNP_IS_WHITE_LIST (white_list), FALSE);
330 g_return_val_if_fail ((entry != NULL), FALSE);
332 priv = white_list->priv;
334 s_entry = g_list_find_custom (priv->entries, entry,
335 (GCompareFunc) g_ascii_strcasecmp);
337 if (s_entry != NULL) {
338 priv->entries = g_list_remove_link (priv->entries, s_entry);
339 g_list_free_full (s_entry, g_free);
340 g_object_notify (G_OBJECT (white_list), "entries");
343 return (s_entry != NULL);
347 * gupnp_white_list_get_entries:
348 * @white_list: A #GUPnPWhiteList
350 * Get the #GList of entries that compose the white list. Do not free
352 * Return value: (element-type utf8) (transfer none): a #GList of entries
353 * used to filter networks, interfaces,... or %NULL.
354 * Do not modify or free the list nor its elements.
359 gupnp_white_list_get_entries (GUPnPWhiteList *white_list)
361 g_return_val_if_fail (GUPNP_IS_WHITE_LIST (white_list), NULL);
363 return white_list->priv->entries;
367 * gupnp_white_list_clear:
368 * @white_list: A #GUPnPWhiteList
370 * Remove all entries from #GList that compose the white list.
371 * The list is now empty. Even if #GUPnPWhiteList is enabled, it will have the
372 * same behavior as if it was disabled.
377 gupnp_white_list_clear (GUPnPWhiteList *white_list)
379 GUPnPWhiteListPrivate *priv;
381 g_return_if_fail (GUPNP_IS_WHITE_LIST(white_list));
383 priv = white_list->priv;
384 g_list_free_full (priv->entries, g_free);
385 priv->entries = NULL;
386 g_object_notify (G_OBJECT (white_list), "entries");
390 * gupnp_white_list_check_context:
391 * @white_list: A #GUPnPWhiteList
392 * @context: A #GUPnPContext to test.
394 * It will check if the @context is allowed or not. The @white_list will check
395 * all its entries againt #GUPnPContext interface, host ip and network fields
396 * information. This function doesn't take into account the @white_list status
399 * Return value: %TRUE if @context is matching the @white_list criterias,
405 gupnp_white_list_check_context (GUPnPWhiteList *white_list,
406 GUPnPContext *context)
410 const char *interface;
413 gboolean match = FALSE;
415 g_return_val_if_fail (GUPNP_IS_WHITE_LIST (white_list), FALSE);
416 g_return_val_if_fail (GUPNP_IS_CONTEXT (context), FALSE);
418 client = GSSDP_CLIENT (context);
420 interface = gssdp_client_get_interface (client);
421 host_ip = gssdp_client_get_host_ip (client);
422 network = gssdp_client_get_network (client);
424 l = white_list->priv->entries;
426 while (l && !match) {
427 match = (interface && !strcmp (l->data, interface)) ||
428 (host_ip && !strcmp (l->data, host_ip)) ||
429 (network && !strcmp (l->data, network));