| 1 |
/* |
| 2 |
* Copyright (C) 2007 Zeeshan Ali (Khattak) <[email protected]> |
| 3 |
* Copyright (C) 2013 Jens Georg <[email protected]> |
| 4 |
* |
| 5 |
* Authors: Zeeshan Ali (Khattak) <[email protected]> |
| 6 |
* Jens Georg <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation; either version 2 of the License, or |
| 11 |
* (at your option) any later version. |
| 12 |
* |
| 13 |
* This program 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 |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License along |
| 19 |
* with this program; if not, write to the Free Software Foundation, |
| 20 |
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
|
| 25 |
#include <libgupnp/gupnp.h> |
| 26 |
#include "gui.h" |
| 27 |
#include <string.h> |
| 28 |
#include <stdlib.h> |
| 29 |
#include <locale.h> |
| 30 |
|
| 31 |
#include <gmodule.h> |
| 32 |
#include <glib/gi18n.h> |
| 33 |
|
| 34 |
static int upnp_port = 0; |
| 35 |
static char **interfaces = NULL; |
| 36 |
static gboolean ipv4 = TRUE; |
| 37 |
static gboolean ipv6 = TRUE; |
| 38 |
static GSSDPUDAVersion uda_version = GSSDP_UDA_VERSION_1_0; |
| 39 |
|
| 40 |
static gboolean |
| 41 |
parse_uda_version (const gchar *option_name, |
| 42 |
const gchar *value, |
| 43 |
gpointer data, |
| 44 |
GError **error); |
| 45 |
|
| 46 |
// clang-format off |
| 47 |
static GOptionEntry entries[] = |
| 48 |
{ |
| 49 |
{ "port", 'p', 0, G_OPTION_ARG_INT, &upnp_port, N_("Network PORT to use for UPnP"), "PORT" }, |
| 50 |
{ "interface", 'i', 0, G_OPTION_ARG_STRING_ARRAY, &interfaces, N_("Network interfaces to use for UPnP communication"), "INTERFACE" }, |
| 51 |
{ "v4", '4', 0, G_OPTION_ARG_NONE, &ipv4, N_("Use the IPv4 protocol family"), NULL }, |
| 52 |
{ "v6", '6', 0, G_OPTION_ARG_NONE, &ipv6, N_("Use the IPv6 protocol family"), NULL }, |
| 53 |
{ "no-v4", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &ipv4, N_("Do not use the IPv4 protocol family"), NULL }, |
| 54 |
{ "no-v6", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &ipv6, N_("Do not use the IPv6 protocol family"), NULL }, |
| 55 |
{ "uda-version", 'v', 0, G_OPTION_ARG_CALLBACK, parse_uda_version, N_("The UDA version to use"), "VERSION"}, |
| 56 |
{ NULL, 0, 0, 0, NULL, NULL, NULL } |
| 57 |
}; |
| 58 |
// clang-format on |
| 59 |
|
| 60 |
static gboolean |
| 61 |
parse_uda_version (const gchar *option_name, |
| 62 |
const gchar *value, |
| 63 |
gpointer data, |
| 64 |
GError **error) |
| 65 |
{ |
| 66 |
if (g_str_equal (value, "1.0")) { |
| 67 |
uda_version = GSSDP_UDA_VERSION_1_0; |
| 68 |
} else if (g_str_equal (value, "1.1")) { |
| 69 |
uda_version = GSSDP_UDA_VERSION_1_1; |
| 70 |
} else { |
| 71 |
g_set_error (error, |
| 72 |
G_IO_ERROR, |
| 73 |
G_IO_ERROR_INVALID_ARGUMENT, |
| 74 |
"Invalid UDA version: %s", value); |
| 75 |
|
| 76 |
return FALSE; |
| 77 |
} |
| 78 |
|
| 79 |
return TRUE; |
| 80 |
} |
| 81 |
|
| 82 |
void |
| 83 |
application_exit (void); |
| 84 |
|
| 85 |
static GUPnPContextManager *context_manager; |
| 86 |
|
| 87 |
static void |
| 88 |
device_proxy_available_cb (GUPnPControlPoint *cp, |
| 89 |
GUPnPDeviceProxy *proxy) |
| 90 |
{ |
| 91 |
append_device (GUPNP_DEVICE_INFO (proxy)); |
| 92 |
} |
| 93 |
|
| 94 |
static void |
| 95 |
device_proxy_unavailable_cb (GUPnPControlPoint *cp, |
| 96 |
GUPnPDeviceProxy *proxy) |
| 97 |
{ |
| 98 |
remove_device (GUPNP_DEVICE_INFO (proxy)); |
| 99 |
} |
| 100 |
|
| 101 |
static void |
| 102 |
on_context_available (GUPnPContextManager *manager, |
| 103 |
GUPnPContext *context, |
| 104 |
gpointer user_data) |
| 105 |
{ |
| 106 |
GUPnPControlPoint *cp; |
| 107 |
|
| 108 |
/* We're interested in everything */ |
| 109 |
cp = gupnp_control_point_new (context, "upnp:rootdevice"); |
| 110 |
|
| 111 |
g_signal_connect (cp, |
| 112 |
"device-proxy-available", |
| 113 |
G_CALLBACK (device_proxy_available_cb), |
| 114 |
NULL); |
| 115 |
g_signal_connect (cp, |
| 116 |
"device-proxy-unavailable", |
| 117 |
G_CALLBACK (device_proxy_unavailable_cb), |
| 118 |
NULL); |
| 119 |
|
| 120 |
gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (cp), TRUE); |
| 121 |
|
| 122 |
/* Let context manager take care of the control point life cycle */ |
| 123 |
gupnp_context_manager_manage_control_point (context_manager, cp); |
| 124 |
|
| 125 |
/* We don't need to keep our own reference to the control point */ |
| 126 |
g_object_unref (cp); |
| 127 |
} |
| 128 |
|
| 129 |
static gboolean |
| 130 |
init_upnp (void) |
| 131 |
{ |
| 132 |
GUPnPContextFilter *context_filter; |
| 133 |
|
| 134 |
GSocketFamily family = G_SOCKET_FAMILY_INVALID; |
| 135 |
|
| 136 |
if (!(ipv4 && ipv6)) { |
| 137 |
if (ipv4) { |
| 138 |
family = G_SOCKET_FAMILY_IPV4; |
| 139 |
} else if (ipv6) { |
| 140 |
family = G_SOCKET_FAMILY_IPV6; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
context_manager = gupnp_context_manager_create_full (uda_version, |
| 145 |
family, |
| 146 |
upnp_port); |
| 147 |
g_assert (context_manager != NULL); |
| 148 |
|
| 149 |
if (interfaces != NULL) { |
| 150 |
context_filter = gupnp_context_manager_get_context_filter ( |
| 151 |
context_manager); |
| 152 |
gupnp_context_filter_add_entryv (context_filter, interfaces); |
| 153 |
gupnp_context_filter_set_enabled (context_filter, TRUE); |
| 154 |
} |
| 155 |
|
| 156 |
g_signal_connect (context_manager, |
| 157 |
"context-available", |
| 158 |
G_CALLBACK (on_context_available), |
| 159 |
NULL); |
| 160 |
|
| 161 |
return TRUE; |
| 162 |
} |
| 163 |
|
| 164 |
static void |
| 165 |
deinit_upnp (void) |
| 166 |
{ |
| 167 |
g_object_unref (context_manager); |
| 168 |
} |
| 169 |
|
| 170 |
G_MODULE_EXPORT |
| 171 |
void |
| 172 |
application_exit (void) |
| 173 |
{ |
| 174 |
deinit_upnp (); |
| 175 |
deinit_ui (); |
| 176 |
|
| 177 |
gtk_main_quit (); |
| 178 |
} |
| 179 |
|
| 180 |
gint |
| 181 |
main (gint argc, |
| 182 |
gchar *argv[]) |
| 183 |
{ |
| 184 |
GError *error = NULL; |
| 185 |
GOptionContext *context = NULL; |
| 186 |
|
| 187 |
setlocale (LC_ALL, ""); |
| 188 |
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); |
| 189 |
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); |
| 190 |
textdomain (GETTEXT_PACKAGE); |
| 191 |
|
| 192 |
context = g_option_context_new (_("- UPnP AV control point")); |
| 193 |
g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); |
| 194 |
g_option_context_add_group (context, gtk_get_option_group (TRUE)); |
| 195 |
|
| 196 |
if (!g_option_context_parse (context, &argc, &argv, &error)) { |
| 197 |
g_print (_("Could not parse options: %s\n"), error->message); |
| 198 |
|
| 199 |
return -4; |
| 200 |
} |
| 201 |
|
| 202 |
if (!init_ui (&argc, &argv)) { |
| 203 |
return -2; |
| 204 |
} |
| 205 |
|
| 206 |
if (!init_upnp ()) { |
| 207 |
return -3; |
| 208 |
} |
| 209 |
|
| 210 |
gtk_main (); |
| 211 |
|
| 212 |
return 0; |
| 213 |
} |
| 214 |
|