ng
[oweals/gnunet.git] / src / upnp / test_upnp.c
1 /*
2      This file is part of GNUnet.
3      (C) 2006 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file src/transports/upnp/upnptest.c
22  * @brief Testcase for UPnP
23  * @author Christian Grothoff
24  */
25
26 #include "gnunet_util.h"
27 #include "gnunet_upnp_service.h"
28 #include "gnunet_core.h"
29 #include "platform.h"
30
31
32
33 int
34 main (int argc, const char *argv[])
35 {
36   static GNUNET_CoreAPIForPlugins capi;
37   struct GNUNET_GE_Context *ectx;
38   struct GNUNET_GC_Configuration *cfg;
39   struct in_addr addr;
40   int i;
41   GNUNET_UPnP_ServiceAPI *upnp;
42   struct GNUNET_PluginHandle *plug;
43   GNUNET_ServicePluginInitializationMethod init;
44   GNUNET_ServicePluginShutdownMethod done;
45   char ntop_buf[INET_ADDRSTRLEN];
46
47   ectx = GNUNET_GE_create_context_stderr (GNUNET_NO,
48                                           GNUNET_GE_WARNING | GNUNET_GE_ERROR
49                                           | GNUNET_GE_FATAL | GNUNET_GE_USER |
50                                           GNUNET_GE_ADMIN |
51                                           GNUNET_GE_DEVELOPER |
52                                           GNUNET_GE_IMMEDIATE |
53                                           GNUNET_GE_BULK);
54   GNUNET_GE_setDefaultContext (ectx);
55   cfg = GNUNET_GC_create ();
56   GNUNET_GE_ASSERT (ectx, cfg != NULL);
57   GNUNET_os_init (ectx);
58   capi.ectx = ectx;
59   capi.cfg = cfg;
60   plug = GNUNET_plugin_load (ectx, "libgnunet", "module_upnp");
61   if (plug == NULL)
62     {
63       GNUNET_GC_free (cfg);
64       GNUNET_GE_free_context (ectx);
65       return 1;
66     }
67   init = GNUNET_plugin_resolve_function (plug, "provide_", GNUNET_YES);
68   if (init == NULL)
69     {
70       GNUNET_plugin_unload (plug);
71       GNUNET_GC_free (cfg);
72       GNUNET_GE_free_context (ectx);
73       return 1;
74     }
75   upnp = init (&capi);
76   if (upnp == NULL)
77     {
78       GNUNET_plugin_unload (plug);
79       GNUNET_GC_free (cfg);
80       GNUNET_GE_free_context (ectx);
81       return 1;
82     }
83   for (i = 0; i < 10; i++)
84     {
85       if (GNUNET_shutdown_test () != GNUNET_NO)
86         break;
87       if (GNUNET_OK == upnp->get_ip (2086, "TCP", &addr))
88         {
89           printf ("UPnP returned external IP %s\n",
90                   inet_ntop (AF_INET, &addr, ntop_buf, INET_ADDRSTRLEN));
91         }
92       else
93         {
94           /* we cannot be sure that there is a UPnP-capable
95              NAT-box out there, so test should not fail
96              just because of this! */
97           printf ("No UPnP response (yet).\n");
98         }
99       GNUNET_thread_sleep (2 * GNUNET_CRON_SECONDS);
100     }
101   done = GNUNET_plugin_resolve_function (plug, "release_", GNUNET_YES);
102   if (done != NULL)
103     done ();
104   GNUNET_plugin_unload (plug);
105   GNUNET_GC_free (cfg);
106   GNUNET_GE_free_context (ectx);
107   return 0;
108 }
109
110 /* end of upnptest.c */