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.
34 #include "gupnp-white-list.h"
36 G_DEFINE_TYPE (GUPnPWhiteList,
40 struct _GUPnPWhiteListPrivate {
58 gupnp_white_list_init (GUPnPWhiteList *list)
60 list->priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
61 GUPNP_TYPE_WHITE_LIST,
62 GUPnPWhiteListPrivate);
64 list->priv->entries = NULL;
68 gupnp_white_list_set_property (GObject *object,
75 list = GUPNP_WHITE_LIST (object);
77 switch (property_id) {
79 list->priv->enabled = g_value_get_boolean (value);
82 list->priv->entries = g_value_get_pointer (value);
85 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
91 gupnp_white_list_get_property (GObject *object,
98 list = GUPNP_WHITE_LIST (object);
100 switch (property_id) {
102 g_value_set_boolean (value, list->priv->enabled);
105 g_value_set_pointer (value, list->priv->entries);
108 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
114 gupnp_white_list_class_finalize (GObject *object)
116 GUPnPWhiteList *list;
117 GObjectClass *object_class;
119 list = GUPNP_WHITE_LIST (object);
121 g_list_free_full (list->priv->entries, g_free);
122 list->priv->entries = NULL;
125 object_class = G_OBJECT_CLASS (gupnp_white_list_parent_class);
126 object_class->finalize (object);
130 gupnp_white_list_class_init (GUPnPWhiteListClass *klass)
132 GObjectClass *object_class;
134 object_class = G_OBJECT_CLASS (klass);
136 object_class->set_property = gupnp_white_list_set_property;
137 object_class->get_property = gupnp_white_list_get_property;
138 object_class->finalize = gupnp_white_list_class_finalize;
140 g_type_class_add_private (klass, sizeof (GUPnPWhiteListPrivate));
143 * GUPnPWhiteList:enabled:
145 * Whether this white list is active or not.
147 g_object_class_install_property
153 "TRUE if the white list is active.",
157 G_PARAM_STATIC_STRINGS));
160 * GUPnPWhiteList:entries:
162 * Whether this white list is active or not.
166 g_object_class_install_property
172 "GList of strings that compose the white list.",
173 G_PARAM_CONSTRUCT_ONLY |
175 G_PARAM_STATIC_STRINGS));
179 * gupnp_white_list_new:
181 * Create a new #GUPnPWhiteList.
182 * The white list is disabled by default.
184 * Returns: (transfer full): A new #GUPnPWhiteList object.
187 gupnp_white_list_new (void)
189 return g_object_new (GUPNP_TYPE_WHITE_LIST, NULL);
193 * gupnp_white_list_set_enabled:
194 * @white_list: A #GUPnPWhiteList
195 * @enable: %TRUE to enable @white_list, %FALSE otherwise
197 * Enable or disable the #GUPnPWhiteList to perform the network filtering.
200 gupnp_white_list_set_enabled (GUPnPWhiteList *white_list, gboolean enable)
202 g_return_if_fail (GUPNP_IS_WHITE_LIST (white_list));
204 white_list->priv->enabled = enable;
205 g_object_notify (G_OBJECT (white_list), "enabled");
209 * gupnp_white_list_get_enabled:
210 * @white_list: A #GUPnPWhiteList
212 * Return the status of the #GUPnPWhiteList
214 * Return value: %TRUE if @white_list is enabled, %FALSE otherwise.
217 gupnp_white_list_get_enabled (GUPnPWhiteList *white_list)
219 g_return_val_if_fail (GUPNP_IS_WHITE_LIST (white_list), FALSE);
221 return white_list->priv->enabled;
225 * gupnp_white_list_is_empty:
226 * @white_list: A #GUPnPWhiteList
228 * Return the state of the entries list of #GUPnPWhiteList
230 * Return value: %TRUE if @white_list is empty, %FALSE otherwise.
233 gupnp_white_list_is_empty (GUPnPWhiteList *white_list)
235 g_return_val_if_fail (GUPNP_IS_WHITE_LIST (white_list), TRUE);
237 return (white_list->priv->entries == NULL);
241 * gupnp_white_list_add_entry:
242 * @white_list: A #GUPnPWhiteList
243 * @entry: A value used to filter network
245 * Add @entry in the list of valid criteria used by @white_list to
247 * if @entry already exists, it won't be added a second time.
249 * Return value: %TRUE if @entry is added, %FALSE otherwise.
252 gupnp_white_list_add_entry (GUPnPWhiteList *white_list, gchar* entry)
255 GUPnPWhiteListPrivate *priv;
257 g_return_val_if_fail (GUPNP_IS_WHITE_LIST (white_list), FALSE);
258 g_return_val_if_fail ((entry != NULL), FALSE);
260 priv = white_list->priv;
262 s_entry = g_list_find_custom (priv->entries, entry,
263 (GCompareFunc) g_ascii_strcasecmp);
265 if (s_entry == NULL) {
266 priv->entries = g_list_prepend (priv->entries,
268 g_object_notify (G_OBJECT (white_list), "entries");
271 return (s_entry == NULL);
275 * gupnp_white_list_remove_entry:
276 * @white_list: A #GUPnPWhiteList
277 * @entry: A value to remove from the filter list.
279 * Remove @entry in the list of valid criteria used by @white_list to
282 * Return value: %TRUE if @entry is removed, %FALSE otherwise.
285 gupnp_white_list_remove_entry (GUPnPWhiteList *white_list, gchar* entry)
288 GUPnPWhiteListPrivate *priv;
290 g_return_val_if_fail (GUPNP_IS_WHITE_LIST (white_list), FALSE);
291 g_return_val_if_fail ((entry != NULL), FALSE);
293 priv = white_list->priv;
295 s_entry = g_list_find_custom (priv->entries, entry,
296 (GCompareFunc) g_ascii_strcasecmp);
298 if (s_entry != NULL) {
299 priv->entries = g_list_remove_link (priv->entries, s_entry);
300 g_list_free_full (s_entry, g_free);
301 g_object_notify (G_OBJECT (white_list), "entries");
304 return (s_entry != NULL);
308 * gupnp_white_list_get_entries:
309 * @white_list: A #GUPnPWhiteList
311 * Get the #GList of entries that compose the white list. Do not free
313 * Return value: (element-type utf8) (transfer none): a #GList of entries
314 * used to filter networks, interfaces,... or %NULL.
315 * Do not modify or free the list nor its elements.
318 gupnp_white_list_get_entries (GUPnPWhiteList *white_list)
320 g_return_val_if_fail (GUPNP_IS_WHITE_LIST (white_list), NULL);
322 return white_list->priv->entries;
326 * gupnp_white_list_clear:
327 * @white_list: A #GUPnPWhiteList
329 * Remove all entries from #GList that compose the white list.
330 * The list is now empty. Even if #GUPnPWhiteList is enabled, it will have the
331 * same behavior as if it was disabled.
334 gupnp_white_list_clear (GUPnPWhiteList *white_list)
336 GUPnPWhiteListPrivate *priv;
338 g_return_if_fail (GUPNP_IS_WHITE_LIST(white_list));
340 priv = white_list->priv;
341 g_list_free_full (priv->entries, g_free);
342 priv->entries = NULL;
343 g_object_notify (G_OBJECT (white_list), "entries");
347 * gupnp_white_list_check_context:
348 * @white_list: A #GUPnPWhiteList
349 * @context: A #GUPnPContext to test.
351 * It will check if the @context is allowed or not. The @white_list will check
352 * all its entries againt #GUPnPContext interface, host ip and network fields
353 * information. This function doesn't take into account the @white_list status
356 * Return value: %TRUE if @context is matching the @white_list criterias,
360 gupnp_white_list_check_context (GUPnPWhiteList *white_list,
361 GUPnPContext *context)
365 const char *interface;
368 gboolean match = FALSE;
370 g_return_val_if_fail (GUPNP_IS_WHITE_LIST (white_list), FALSE);
371 g_return_val_if_fail (GUPNP_IS_CONTEXT (context), FALSE);
373 client = GSSDP_CLIENT (context);
375 interface = gssdp_client_get_interface (client);
376 host_ip = gssdp_client_get_host_ip (client);
377 network = gssdp_client_get_network (client);
379 l = white_list->priv->entries;
381 while (l && !match) {
382 match = (interface && !strcmp (l->data, interface)) ||
383 (host_ip && !strcmp (l->data, host_ip)) ||
384 (network && !strcmp (l->data, network));