Add gobject introspection
[profile/ivi/GUPnP.git] / libgupnp / gupnp-unix-context-manager.c
1 /*
2  * Copyright (C) 2009 Nokia Corporation.
3  * Copyright (C) 2006, 2007, 2008 OpenedHand Ltd.
4  *
5  * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
6  *         Jorn Baayen <jorn@openedhand.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 /**
25  * SECTION:gupnp-unix-context-manager
26  * @short_description: Unix-specific implementation of #GUPnPContextManager.
27  *
28  */
29
30 #include <config.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <sys/utsname.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <arpa/inet.h>
43 #include <net/if.h>
44 #include <ifaddrs.h>
45 #include <libsoup/soup-address.h>
46 #include <glib/gstdio.h>
47 #include <libgssdp/gssdp-error.h>
48
49 #include "gupnp-unix-context-manager.h"
50 #include "gupnp-context.h"
51
52 G_DEFINE_TYPE (GUPnPUnixContextManager,
53                gupnp_unix_context_manager,
54                GUPNP_TYPE_SIMPLE_CONTEXT_MANAGER);
55
56 /*
57  * Create a context for all network interfaces that are up.
58  */
59 static GList *
60 gupnp_unix_context_manager_get_interfaces (GUPnPSimpleContextManager *manager)
61 {
62         struct ifaddrs *ifa_list, *ifa;
63         GList *processed;
64
65         g_return_val_if_fail (GUPNP_IS_UNIX_CONTEXT_MANAGER (manager), NULL);
66
67         if (getifaddrs (&ifa_list) != 0) {
68                 g_warning ("Failed to retrieve list of network interfaces:%s\n",
69                            strerror (errno));
70
71                 return FALSE;
72         }
73
74         processed = NULL;
75
76         /* Create contexts for each up interface */
77         for (ifa = ifa_list; ifa != NULL; ifa = ifa->ifa_next) {
78                 if (g_list_find_custom (processed,
79                                         ifa->ifa_name,
80                                         (GCompareFunc) strcmp) != NULL)
81                         continue;
82
83                 if (ifa->ifa_flags & IFF_POINTOPOINT)
84                         continue;
85
86                 if (ifa->ifa_flags & IFF_UP)
87                         processed = g_list_append (processed,
88                                                    g_strdup (ifa->ifa_name));
89         }
90
91         freeifaddrs (ifa_list);
92
93         return processed;
94 }
95
96 static void
97 gupnp_unix_context_manager_init (G_GNUC_UNUSED GUPnPUnixContextManager *manager)
98 {
99 }
100
101 static void
102 gupnp_unix_context_manager_class_init (GUPnPUnixContextManagerClass *klass)
103 {
104         GUPnPSimpleContextManagerClass *parent_class;
105
106         parent_class = GUPNP_SIMPLE_CONTEXT_MANAGER_CLASS (klass);
107         parent_class->get_interfaces =
108                                     gupnp_unix_context_manager_get_interfaces;
109 }
110