Submit version 0.20.1 of GUPnP (4186015)
[profile/ivi/GUPnP.git] / libgupnp / gupnp-windows-context-manager.c
1 /*
2  * Copyright (C) 2009, 2010 Jens Georg
3  * Copyright (C) 2009 Nokia Corporation.
4  * Copyright (C) 2006, 2007, 2008 OpenedHand Ltd.
5  *
6  * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
7  *         Jorn Baayen <jorn@openedhand.com>
8  *         Jens Georg <mail@jensge.org>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */
25
26 /**
27  * SECTION:gupnp-windows-context-manager
28  * @short_description: Windows-specific implementation of #GUPnPContextManager.
29  */
30
31 #include <config.h>
32 #include <string.h>
33 #ifndef _WIN32_WINNT
34 #define _WIN32_WINNT 0x0502
35 #endif
36 #include <winsock2.h>
37 #include <ws2tcpip.h>
38 #include <iphlpapi.h>
39
40 #include "gupnp-windows-context-manager.h"
41 #include "gupnp-context.h"
42
43 G_DEFINE_TYPE (GUPnPWindowsContextManager,
44                gupnp_windows_context_manager,
45                GUPNP_TYPE_SIMPLE_CONTEXT_MANAGER);
46
47 /*
48  * Create a context for all network interfaces that are up.
49  */
50 static GList *
51 gupnp_windows_context_manager_get_interfaces
52                                         (GUPnPSimpleContextManager *manager)
53 {
54         GList *interfaces = NULL;
55         ULONG flags = GAA_FLAG_INCLUDE_PREFIX |
56                       GAA_FLAG_SKIP_DNS_SERVER |
57                       GAA_FLAG_SKIP_MULTICAST;
58         /* use 15k buffer initially as documented in MSDN */
59         DWORD size = 0x3C00;
60         DWORD ret;
61         PIP_ADAPTER_ADDRESSES adapters_addresses;
62         PIP_ADAPTER_ADDRESSES adapter;
63
64         do {
65                 adapters_addresses = (PIP_ADAPTER_ADDRESSES) g_malloc0 (size);
66                 ret = GetAdaptersAddresses (AF_UNSPEC,
67                                             flags,
68                                             NULL,
69                                             adapters_addresses,
70                                             &size);
71                 if (ret == ERROR_BUFFER_OVERFLOW) {
72                         g_free (adapters_addresses);
73                 }
74         } while (ret == ERROR_BUFFER_OVERFLOW);
75
76         if (ret != ERROR_SUCCESS)
77                 return NULL;
78
79         for (adapter = adapters_addresses;
80              adapter != NULL;
81              adapter = adapter->Next) {
82                 if (adapter->FirstUnicastAddress == NULL)
83                         continue;
84                 if (adapter->OperStatus != IfOperStatusUp)
85                         continue;
86                 interfaces = g_list_append (interfaces,
87                                             g_strdup (adapter->AdapterName));
88         }
89
90         return interfaces;
91 }
92
93 static void
94 gupnp_windows_context_manager_init (GUPnPWindowsContextManager *manager)
95 {
96 }
97
98 static void
99 gupnp_windows_context_manager_class_init (GUPnPWindowsContextManagerClass *klass)
100 {
101         GUPnPSimpleContextManagerClass *parent_class;
102
103         parent_class = GUPNP_SIMPLE_CONTEXT_MANAGER_CLASS (klass);
104         parent_class->get_interfaces =
105                                 gupnp_windows_context_manager_get_interfaces;
106 }