aboutsummaryrefslogtreecommitdiff
path: root/src/setup/gnunet-setup-transport.c
blob: ae41aa323c8bddd4767516daf063b4f63fde243e (plain) (blame)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
     This file is part of GNUnet.
     (C) 2010 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     GNUnet 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
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file src/setup/gnunet-setup-transport.c
 * @brief support for transport (NAT) configuration
 * @author Christian Grothoff
 */
#include "gnunet-setup.h"
#include <gnunet/gnunet_util_lib.h>
#include <gnunet/gnunet_resolver_service.h>
#include <gnunet/gnunet_nat_lib.h>

/**
 * How long do we wait for the NAT test to report success?
 */
#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)

/**
 * Handle to the active NAT test.
 */
static struct GNUNET_NAT_Test *tst;

/**
 * Task identifier for the timeout.
 */
static GNUNET_SCHEDULER_TaskIdentifier tsk;

static struct GNUNET_OS_Process *resolver;


/**
 * Update the ICMP server button based on the result.
 *
 * @param on GNUNET_YES to enable, GNUNET_NO to disable
 */
static void
update_icmp_server_enable_button (int on)
{
  GtkToggleButton *button;

  button =
      GTK_TOGGLE_BUTTON (GNUNET_SETUP_get_object
                         ("GNUNET_setup_transport_icmp_server_enable_checkbutton"));
  if (button == NULL)
  {
    GNUNET_break (0);
    return;
  }
  gtk_toggle_button_set_active (button, on ? TRUE : FALSE);
}


/**
 * Function called by NAT on success.
 * Clean up and update GUI (with success).
 *
 * @param cls closure (unused)
 * @param success currently always GNUNET_OK
 */
static void
result_callback (void *cls, int success)
{
  GNUNET_SCHEDULER_cancel (tsk);
  tsk = GNUNET_SCHEDULER_NO_TASK;
  GNUNET_NAT_test_stop (tst);
  tst = NULL;
  if (NULL != resolver)
  {
    GNUNET_break (0 == GNUNET_OS_process_kill (resolver, SIGTERM));
    GNUNET_OS_process_close (resolver);
  }
  update_icmp_server_enable_button (success);
}


/**
 * Function called if NAT failed to confirm success.
 * Clean up and update GUI (with failure).
 *
 * @param cls closure (unused)
 * @param tc scheduler callback
 */
static void
fail_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  GNUNET_assert (NULL != tst);
  tsk = GNUNET_SCHEDULER_NO_TASK;
  GNUNET_NAT_test_stop (tst);
  tst = NULL;
  update_icmp_server_enable_button (GNUNET_NO);
}


/**
 * Main function for the connection reversal test.
 *
 * @param cls the 'int*' for the result
 * @param tc scheduler context
 */
static void
reversal_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  int *ok = cls;

  GNUNET_assert (NULL != cfg);
  GNUNET_RESOLVER_connect (cfg);
  tst = GNUNET_NAT_test_start (cfg, GNUNET_YES, 0, 0, &result_callback, ok);
  if (NULL == tst)
  {
    *ok = GNUNET_SYSERR;
    return;
  }
  tsk = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &fail_timeout, ok);
}

/**
 * Test if connection reversal (ICMP method) works.
 */
static void
test_connection_reversal ()
{
  if (NULL != resolver)
    return;                     /* test already active!? */
  resolver =
    GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-resolver",
                               "gnunet-service-resolver", NULL);
  GNUNET_SCHEDULER_add_now (&reversal_test, NULL);
}


/**
 * Process list of local IP addresses.  Find and set the
 * one of the default interface.
 *
 * @param cls closure (not used)
 * @param name name of the interface (can be NULL for unknown)
 * @param isDefault is this presumably the default interface
 * @param addr address of this interface (can be NULL for unknown or unassigned)
 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
 * @param netmask the network mask (can be NULL for unknown or unassigned))
 * @param addrlen length of the address
 * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort
 */
static int
nipo (void *cls, const char *name, int isDefault, const struct sockaddr *addr,
      const struct sockaddr *broadcast_addr, const struct sockaddr *netmask,
      socklen_t addrlen)
{
  const struct sockaddr_in *in;
  char buf[INET_ADDRSTRLEN];
  GtkEntry *entry;

  if (!isDefault)
    return GNUNET_OK;
  if (addrlen != sizeof (struct sockaddr_in))
    return GNUNET_OK;
  in = (const struct sockaddr_in *) addr;

  /* set internal IP address */
  if (NULL == inet_ntop (AF_INET, &in->sin_addr, buf, sizeof (buf)))
  {
    GNUNET_break (0);
    return GNUNET_OK;
  }
  GNUNET_CONFIGURATION_set_value_string (cfg, "nat", "INTERNAL_ADDRESS", buf);
  entry =
      GTK_ENTRY (GNUNET_SETUP_get_object
                 ("GNUNET_setup_transport_internal_ip_entry"));
  if (entry == NULL)
  {
    GNUNET_break (0);
    return GNUNET_SYSERR;
  }
  gtk_entry_set_text (entry, buf);
  /* no need to continue iteration */
  return GNUNET_SYSERR;
}


/**
 * Set our external IPv4 address.
 *
 * @param cls closure
 * @param addr the address, NULL on errors
 */
static void
set_external_ipv4 (void *cls, const struct in_addr *addr)
{
  char buf[INET_ADDRSTRLEN];
  GObject *o;
  GtkEntry *entry;
  GtkToggleButton *button;

  if (NULL != addr)
  {
    /* enable 'behind nat' */
    if (NULL != cfg)
      GNUNET_CONFIGURATION_set_value_string (cfg, "nat", "BEHIND_NAT", "YES");
    o = GNUNET_SETUP_get_object ("GNUNET_setup_transport_nat_checkbutton");
    if (NULL != o)
    {
      button = GTK_TOGGLE_BUTTON (o);
      if (button == NULL)
      {
        GNUNET_break (0);
        return;
      }
      gtk_toggle_button_set_active (button, TRUE);
    }

    /* set external IP address */
    if (NULL == inet_ntop (AF_INET, addr, buf, sizeof (buf)))
    {
      GNUNET_break (0);
      return;
    }
    if (NULL != cfg)
      GNUNET_CONFIGURATION_set_value_string (cfg, "nat", "EXTERNAL_ADDRESS",
                                             buf);
    o = GNUNET_SETUP_get_object
        ("GNUNET_setup_transport_external_ip_address_entry");
    if (NULL != o)
    {
      entry = GTK_ENTRY (o);
      if (entry == NULL)
      {
        GNUNET_break (0);
        return;
      }
      gtk_entry_set_text (entry, buf);
    }
  }
}


/**
 * User asked for autoconfiguration.  Try the full program.
 */
void
GNUNET_setup_transport_autoconfig_button_clicked_cb ()
{
  GtkToggleButton *button;
  int hns;
  int hnc;
  char *tmp;

  /* try to detect external IP */
  (void) GNUNET_NAT_mini_get_external_ipv4 (TIMEOUT, &set_external_ipv4, NULL);
  /* Try to detect internal IP */
  GNUNET_OS_network_interfaces_list (&nipo, NULL);

  /* FIXME: do more: test if UPnP works */

  /* test gnunet-helper-nat-server */
  tmp = NULL;
  hns =
      ((GNUNET_OK ==
        GNUNET_CONFIGURATION_get_value_string (cfg, "nat", "EXTERNAL_ADDRESS",
                                               &tmp)) && (0 < strlen (tmp)) &&
       (GNUNET_YES ==
        GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "BEHIND_NAT")) &&
       (GNUNET_YES ==
        GNUNET_OS_check_helper_binary ("gnunet-helper-nat-server")));
  GNUNET_free_non_null (tmp);
  if (hns)
    test_connection_reversal ();

  /* test gnunet-helper-nat-client */
  tmp = NULL;
  hnc =
      ((GNUNET_OK ==
        GNUNET_CONFIGURATION_get_value_string (cfg, "nat", "INTERNAL_ADDRESS",
                                               &tmp)) && (0 < strlen (tmp)) &&
       (GNUNET_YES !=
        GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "BEHIND_NAT")) &&
       (GNUNET_YES ==
        GNUNET_OS_check_helper_binary ("gnunet-helper-nat-client")));
  GNUNET_free_non_null (tmp);
  button =
      GTK_TOGGLE_BUTTON (GNUNET_SETUP_get_object
                         ("GNUNET_setup_transport_icmp_client_enable_checkbutton"));
  if (button == NULL)
  {
    GNUNET_break (0);
    return;
  }
  gtk_toggle_button_set_active (button, hnc ? TRUE : FALSE);
}

/* end of gnunet-setup-transport.c */