summaryrefslogtreecommitdiff
path: root/tests/check/transmitter/rawudp-upnp.c
blob: 9ccf85d8ac8b7bf814a7714058ebf94645d0968d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* Farstream unit tests for FsRawUdpTransmitter UPnP code
 *
 * Copyright (C) 2009 Collabora, Nokia
 * @author: Olivier Crete <olivier.crete@collabora.co.uk>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
*/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "check-threadsafe.h"


gboolean got_address = FALSE;
gboolean added_mapping = FALSE;

void
get_vars (gboolean *out_got_address,
    gboolean *out_added_mapping)
{
  *out_got_address = got_address;
  *out_added_mapping = added_mapping;
}


#if defined(HAVE_GUPNP_CORE_10) || defined(HAVE_GUPNP_CORE_12)

#include <libgupnp/gupnp.h>

static void
get_external_ip_address_cb (GUPnPService *service,
    GUPnPServiceAction *action,
    gpointer user_data)
{
  gupnp_service_action_set (action,
      "NewExternalIPAddress", G_TYPE_STRING, "127.0.0.1",
      NULL);
  gupnp_service_action_return (action);
  got_address = TRUE;
}

static void
add_port_mapping_cb (GUPnPService *service,
    GUPnPServiceAction *action,
    gpointer user_data)
{
  gchar *remote_host = NULL;
  guint external_port = 0;
  gchar *proto = NULL;
  guint internal_port = 0;
  gchar *internal_client = NULL;
  gboolean enabled = -1;
  gchar *desc = NULL;
  guint lease = 0;

  gupnp_service_action_get (action,
      "NewRemoteHost", G_TYPE_STRING, &remote_host,
      "NewExternalPort", G_TYPE_UINT, &external_port,
      "NewProtocol", G_TYPE_STRING, &proto,
      "NewInternalPort", G_TYPE_UINT, &internal_port,
      "NewInternalClient", G_TYPE_STRING, &internal_client,
      "NewEnabled", G_TYPE_BOOLEAN, &enabled,
      "NewPortMappingDescription", G_TYPE_STRING, &desc,
      "NewLeaseDuration", G_TYPE_UINT, &lease,
      NULL);

  ts_fail_unless (remote_host && !strcmp (remote_host, ""), "Remote host invalid");
  ts_fail_unless (external_port == internal_port, "External and internal ports different");
  ts_fail_unless (proto && (!strcmp (proto, "UDP") || !strcmp (proto, "TCP")));
  ts_fail_unless (enabled == TRUE, "enable is not true");
  ts_fail_unless (desc != NULL, "no desc");

  g_free (remote_host);
  g_free (proto);
  g_free (internal_client);
  g_free (desc);

  gupnp_service_action_return (action);
  added_mapping = TRUE;
}


static void
delete_port_mapping_cb (GUPnPService *service,
    GUPnPServiceAction *action,
    gpointer user_data)
{
  gchar *remote_host = NULL;
  guint external_port = 0;
  gchar *proto = NULL;

  gupnp_service_action_get (action,
      "NewRemoteHost", G_TYPE_STRING, &remote_host,
      "NewExternalPort", G_TYPE_UINT, &external_port,
      "NewProtocol", G_TYPE_STRING, &proto,
      NULL);

  ts_fail_if (remote_host == NULL, "remote host NULL on remove");
  ts_fail_unless (external_port, "external port wrong on remove");
  ts_fail_unless (proto && !strcmp (proto, "UDP"), "proto wrong on remove");

  gupnp_service_action_return (action);
}


GObject *
start_upnp_server (void)
{
  GUPnPContext *context;
  GUPnPRootDevice *dev;
  GUPnPServiceInfo *service;
  GUPnPDeviceInfo *subdev1;
  GUPnPDeviceInfo *subdev2;
  const gchar *upnp_xml_path;
  GError *gerr = NULL;

#ifdef HAVE_GUPNP_CORE_12
  context = gupnp_context_new (NULL, 0, &gerr);
#else
  context = gupnp_context_new (NULL, NULL, 0, &gerr);
#endif
  g_assert_no_error (gerr);
  ts_fail_if (context == NULL, "Can't get gupnp context");

  if (g_getenv ("UPNP_XML_PATH"))
    upnp_xml_path = g_getenv ("UPNP_XML_PATH");
  else
    upnp_xml_path  = ".";

  gupnp_context_host_path (context, upnp_xml_path, "");

#ifdef HAVE_GUPNP_CORE_12
  dev = gupnp_root_device_new (context, "InternetGatewayDevice.xml",
      upnp_xml_path, &gerr);
  g_assert_no_error (gerr);
#else
  dev = gupnp_root_device_new (context, "InternetGatewayDevice.xml",
      upnp_xml_path);
#endif
  ts_fail_if (dev == NULL, "could not get root dev");

  subdev1 = gupnp_device_info_get_device (GUPNP_DEVICE_INFO (dev),
      "urn:schemas-upnp-org:device:WANDevice:1");
  ts_fail_if (subdev1 == NULL, "Could not get WANDevice");

  subdev2 = gupnp_device_info_get_device (subdev1,
      "urn:schemas-upnp-org:device:WANConnectionDevice:1");
  ts_fail_if (subdev2 == NULL, "Could not get WANConnectionDevice");
  g_object_unref (subdev1);

  service = gupnp_device_info_get_service (subdev2,
      "urn:schemas-upnp-org:service:WANIPConnection:1");
  ts_fail_if (service == NULL, "Could not get WANIPConnection");
  g_object_unref (subdev2);

  g_signal_connect (service, "action-invoked::GetExternalIPAddress",
      G_CALLBACK (get_external_ip_address_cb), NULL);
  g_signal_connect (service, "action-invoked::AddPortMapping",
      G_CALLBACK (add_port_mapping_cb), NULL);
  g_signal_connect (service, "action-invoked::DeletePortMapping",
      G_CALLBACK (delete_port_mapping_cb), NULL);

  gupnp_root_device_set_available (dev, TRUE);

  return G_OBJECT (context);
}

#else

GObject *
start_upnp_server (void)
{
  return NULL;
}

#endif