~ubuntu-branches/ubuntu/intrepid/gnunet/intrepid

« back to all changes in this revision

Viewing changes to src/transports/udp.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2008-01-31 17:40:18 UTC
  • mfrom: (1.2.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20080131174018-sfnbb8wv7p4nmut1
Tags: 0.7.3-2ubuntu1
* Merge from debian unstable, remaining changes:
  - debian/rules:
    = Make use of code from cdbs' clean-la.mk file to clear the
      dependency_libs field in all .la files in the gnunet-dev package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#include "ip.h"
33
33
#include "platform.h"
34
34
 
35
 
#define DEBUG_UDP NO
 
35
#define DEBUG_UDP GNUNET_NO
36
36
 
37
 
static UPnP_ServiceAPI *upnp;
 
37
static GNUNET_UPnP_ServiceAPI *upnp;
38
38
 
39
39
#include "udp_helper.c"
40
40
 
46
46
  /**
47
47
   * claimed IP of the sender, network byte order
48
48
   */
49
 
  IPaddr ip;
 
49
  GNUNET_IPv4Address ip;
50
50
 
51
51
  /**
52
52
   * claimed port of the sender, network byte order
60
60
 
61
61
} HostAddress;
62
62
 
63
 
static struct GC_Configuration *cfg;
64
 
 
65
 
static struct LoadMonitor *load_monitor;
66
 
 
67
 
static struct CIDRNetwork *filteredNetworks_;
68
 
 
69
 
static struct CIDRNetwork *allowedNetworks_;
70
 
 
71
 
static struct MUTEX *configLock;
 
63
static struct GNUNET_GC_Configuration *cfg;
 
64
 
 
65
static struct GNUNET_LoadMonitor *load_monitor;
 
66
 
 
67
static struct GNUNET_IPv4NetworkSet *filteredNetworks_;
 
68
 
 
69
static struct GNUNET_IPv4NetworkSet *allowedNetworks_;
 
70
 
 
71
static struct GNUNET_Mutex *configLock;
72
72
 
73
73
/**
74
74
 * Get the GNUnet UDP port from the configuration, or from
82
82
  struct servent *pse;          /* pointer to service information entry        */
83
83
  unsigned long long port;
84
84
 
85
 
  if (-1 == GC_get_configuration_value_number (cfg,
86
 
                                               "UDP",
87
 
                                               "PORT", 1, 65535, 2086, &port))
 
85
  if (-1 == GNUNET_GC_get_configuration_value_number (cfg,
 
86
                                                      "UDP",
 
87
                                                      "PORT", 1, 65535, 2086,
 
88
                                                      &port))
88
89
    {
89
90
      if ((pse = getservbyname ("gnunet", "udp")))
90
91
        port = htons (pse->s_port);
107
108
  sock = SOCKET (PF_INET, SOCK_DGRAM, 17);
108
109
  if (sock < 0)
109
110
    {
110
 
      GE_DIE_STRERROR (ectx, GE_FATAL | GE_ADMIN | GE_IMMEDIATE, "socket");
 
111
      GNUNET_GE_DIE_STRERROR (ectx,
 
112
                              GNUNET_GE_FATAL | GNUNET_GE_ADMIN |
 
113
                              GNUNET_GE_IMMEDIATE, "socket");
111
114
      return -1;
112
115
    }
113
116
  if (SETSOCKOPT (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) < 0)
114
117
    {
115
 
      GE_DIE_STRERROR (ectx,
116
 
                       GE_FATAL | GE_ADMIN | GE_IMMEDIATE, "setsockopt");
 
118
      GNUNET_GE_DIE_STRERROR (ectx,
 
119
                              GNUNET_GE_FATAL | GNUNET_GE_ADMIN |
 
120
                              GNUNET_GE_IMMEDIATE, "setsockopt");
117
121
      return -1;
118
122
    }
119
 
  GE_ASSERT (NULL, port != 0);
 
123
  GNUNET_GE_ASSERT (NULL, port != 0);
120
124
  memset (&sin, 0, sizeof (sin));
121
125
  sin.sin_family = AF_INET;
122
126
  sin.sin_addr.s_addr = INADDR_ANY;
123
127
  sin.sin_port = htons (port);
124
128
  if (BIND (sock, (struct sockaddr *) &sin, sizeof (sin)) < 0)
125
129
    {
126
 
      GE_LOG_STRERROR (ectx, GE_FATAL | GE_ADMIN | GE_IMMEDIATE, "bind");
127
 
      GE_LOG (ectx,
128
 
              GE_FATAL | GE_ADMIN | GE_IMMEDIATE,
129
 
              _("Failed to bind to UDP port %d.\n"), port);
130
 
      GE_DIE_STRERROR (ectx, GE_FATAL | GE_USER | GE_IMMEDIATE, "bind");
 
130
      GNUNET_GE_LOG_STRERROR (ectx,
 
131
                              GNUNET_GE_FATAL | GNUNET_GE_ADMIN |
 
132
                              GNUNET_GE_IMMEDIATE, "bind");
 
133
      GNUNET_GE_LOG (ectx,
 
134
                     GNUNET_GE_FATAL | GNUNET_GE_ADMIN | GNUNET_GE_IMMEDIATE,
 
135
                     _("Failed to bind to UDP port %d.\n"), port);
 
136
      GNUNET_GE_DIE_STRERROR (ectx,
 
137
                              GNUNET_GE_FATAL | GNUNET_GE_USER |
 
138
                              GNUNET_GE_IMMEDIATE, "bind");
131
139
      return -1;
132
140
    }
133
141
  /* do not bind if port == 0, then we use
141
149
static int
142
150
isBlacklisted (const void *addr, unsigned int addr_len)
143
151
{
144
 
  IPaddr ip;
 
152
  GNUNET_IPv4Address ip;
145
153
  int ret;
146
154
 
147
155
  if (addr_len == sizeof (struct sockaddr_in))
148
156
    {
149
 
      memcpy (&ip, &((struct sockaddr_in *) addr)->sin_addr, sizeof (IPaddr));
 
157
      memcpy (&ip, &((struct sockaddr_in *) addr)->sin_addr,
 
158
              sizeof (GNUNET_IPv4Address));
150
159
    }
151
 
  else if (addr_len == sizeof (IPaddr))
 
160
  else if (addr_len == sizeof (GNUNET_IPv4Address))
152
161
    {
153
162
      memcpy (&ip, addr, addr_len);
154
163
    }
155
164
  else
156
165
    {
157
 
      return SYSERR;
 
166
      return GNUNET_SYSERR;
158
167
    }
159
 
  MUTEX_LOCK (configLock);
160
 
  ret = check_ipv4_listed (filteredNetworks_, ip);
161
 
  MUTEX_UNLOCK (configLock);
 
168
  GNUNET_mutex_lock (configLock);
 
169
  ret = GNUNET_check_ipv4_listed (filteredNetworks_, ip);
 
170
  GNUNET_mutex_unlock (configLock);
162
171
  return ret;
163
172
}
164
173
 
168
177
static int
169
178
isWhitelisted (const void *addr, unsigned int addr_len)
170
179
{
171
 
  IPaddr ip;
 
180
  GNUNET_IPv4Address ip;
172
181
  int ret;
173
182
 
174
183
  if (addr_len == sizeof (struct sockaddr_in))
175
184
    {
176
 
      memcpy (&ip, &((struct sockaddr_in *) addr)->sin_addr, sizeof (IPaddr));
 
185
      memcpy (&ip, &((struct sockaddr_in *) addr)->sin_addr,
 
186
              sizeof (GNUNET_IPv4Address));
177
187
    }
178
 
  else if (addr_len == sizeof (IPaddr))
 
188
  else if (addr_len == sizeof (GNUNET_IPv4Address))
179
189
    {
180
190
      memcpy (&ip, addr, addr_len);
181
191
    }
182
192
  else
183
193
    {
184
 
      return SYSERR;
 
194
      return GNUNET_SYSERR;
185
195
    }
186
 
  ret = OK;
187
 
  MUTEX_LOCK (configLock);
 
196
  ret = GNUNET_OK;
 
197
  GNUNET_mutex_lock (configLock);
188
198
  if (allowedNetworks_ != NULL)
189
 
    ret = check_ipv4_listed (allowedNetworks_, ip);
190
 
  MUTEX_UNLOCK (configLock);
 
199
    ret = GNUNET_check_ipv4_listed (allowedNetworks_, ip);
 
200
  GNUNET_mutex_unlock (configLock);
191
201
  return ret;
192
202
}
193
203
 
194
204
static int
195
205
isRejected (const void *addr, unsigned int addr_len)
196
206
{
197
 
  if ((YES == isBlacklisted (addr,
198
 
                             addr_len)) ||
199
 
      (YES != isWhitelisted (addr, addr_len)))
 
207
  if ((GNUNET_YES == isBlacklisted (addr,
 
208
                                    addr_len)) ||
 
209
      (GNUNET_YES != isWhitelisted (addr, addr_len)))
200
210
    {
201
211
#if DEBUG_UDP
202
 
      GE_LOG (ectx,
203
 
              GE_DEBUG | GE_USER | GE_BULK,
204
 
              "Rejecting traffic from %u.%u.%u.%u.\n",
205
 
              PRIP (ntohl (*(int *) addr)));
 
212
      GNUNET_GE_LOG (ectx,
 
213
                     GNUNET_GE_DEBUG | GNUNET_GE_USER | GNUNET_GE_BULK,
 
214
                     "Rejecting traffic from %u.%u.%u.%u.\n",
 
215
                     GNUNET_PRIP (ntohl (*(int *) addr)));
206
216
#endif
207
 
      return YES;
 
217
      return GNUNET_YES;
208
218
    }
209
 
  return NO;
 
219
  return GNUNET_NO;
210
220
}
211
221
 
212
222
 
217
227
 *
218
228
 * @param helo the hello message to verify
219
229
 *        (the signature/crc have been verified before)
220
 
 * @return OK on success, SYSERR on failure
 
230
 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
221
231
 */
222
232
static int
223
 
verifyHello (const P2P_hello_MESSAGE * hello)
 
233
verifyHello (const GNUNET_MessageHello * hello)
224
234
{
225
235
  const HostAddress *haddr;
226
236
 
227
237
  haddr = (const HostAddress *) &hello[1];
228
238
  if ((ntohs (hello->senderAddressSize) != sizeof (HostAddress)) ||
229
 
      (ntohs (hello->header.size) != P2P_hello_MESSAGE_size (hello)) ||
230
 
      (ntohs (hello->header.type) != p2p_PROTO_hello))
231
 
    {
232
 
      GE_BREAK (NULL, 0);
233
 
      return SYSERR;
234
 
    }
235
 
  if ((YES == isBlacklisted (&haddr->ip,
236
 
                             sizeof (IPaddr))) ||
237
 
      (YES != isWhitelisted (&haddr->ip, sizeof (IPaddr))))
238
 
    {
239
 
#if DEBUG_UDP
240
 
      GE_LOG (ectx,
241
 
              GE_DEBUG | GE_USER | GE_BULK,
242
 
              "Rejecting UDP HELLO from %u.%u.%u.%u:%u due to configuration.\n",
243
 
              PRIP (ntohl (*(int *) &haddr->ip.addr)), ntohs (haddr->port));
244
 
#endif
245
 
      return SYSERR;            /* obviously invalid */
246
 
    }
247
 
#if DEBUG_UDP
248
 
  GE_LOG (ectx,
249
 
          GE_DEBUG | GE_USER | GE_BULK,
250
 
          "Verified UDP HELLO from %u.%u.%u.%u:%u.\n",
251
 
          PRIP (ntohl (*(int *) &haddr->ip.addr)), ntohs (haddr->port));
252
 
#endif
253
 
  return OK;
 
239
      (ntohs (hello->header.size) != GNUNET_sizeof_hello (hello)) ||
 
240
      (ntohs (hello->header.type) != GNUNET_P2P_PROTO_HELLO))
 
241
    {
 
242
      GNUNET_GE_BREAK (NULL, 0);
 
243
      return GNUNET_SYSERR;
 
244
    }
 
245
  if ((GNUNET_YES == isBlacklisted (&haddr->ip,
 
246
                                    sizeof (GNUNET_IPv4Address))) ||
 
247
      (GNUNET_YES != isWhitelisted (&haddr->ip, sizeof (GNUNET_IPv4Address))))
 
248
    {
 
249
#if DEBUG_UDP
 
250
      GNUNET_GE_LOG (ectx,
 
251
                     GNUNET_GE_DEBUG | GNUNET_GE_USER | GNUNET_GE_BULK,
 
252
                     "Rejecting UDP HELLO from %u.%u.%u.%u:%u due to configuration.\n",
 
253
                     GNUNET_PRIP (ntohl (*(int *) &haddr->ip.addr)),
 
254
                     ntohs (haddr->port));
 
255
#endif
 
256
      return GNUNET_SYSERR;     /* obviously invalid */
 
257
    }
 
258
#if DEBUG_UDP
 
259
  GNUNET_GE_LOG (ectx,
 
260
                 GNUNET_GE_DEBUG | GNUNET_GE_USER | GNUNET_GE_BULK,
 
261
                 "Verified UDP HELLO from %u.%u.%u.%u:%u.\n",
 
262
                 GNUNET_PRIP (ntohl (*(int *) &haddr->ip.addr)),
 
263
                 ntohs (haddr->port));
 
264
#endif
 
265
  return GNUNET_OK;
254
266
}
255
267
 
256
268
/**
257
269
 * Create a hello-Message for the current node. The hello is created
258
270
 * without signature and without a timestamp. The GNUnet core will
259
 
 * sign the message and add an expiration time.
 
271
 * GNUNET_RSA_sign the message and add an expiration time.
260
272
 *
261
273
 * @return hello on success, NULL on error
262
274
 */
263
 
static P2P_hello_MESSAGE *
 
275
static GNUNET_MessageHello *
264
276
createhello ()
265
277
{
266
278
  static HostAddress last_addr;
267
 
  P2P_hello_MESSAGE *msg;
 
279
  GNUNET_MessageHello *msg;
268
280
  HostAddress *haddr;
269
281
  unsigned short port;
270
282
 
272
284
  if (port == 0)
273
285
    return NULL;                /* UDP transport configured send-only */
274
286
 
275
 
  msg = MALLOC (sizeof (P2P_hello_MESSAGE) + sizeof (HostAddress));
 
287
  msg = GNUNET_malloc (sizeof (GNUNET_MessageHello) + sizeof (HostAddress));
276
288
  haddr = (HostAddress *) & msg[1];
277
289
 
278
290
 
279
291
  if (!(((upnp != NULL) &&
280
 
         (OK == upnp->get_ip (port,
281
 
                              "UDP",
282
 
                              &haddr->ip))) ||
283
 
        (SYSERR != getPublicIPAddress (cfg, ectx, &haddr->ip))))
 
292
         (GNUNET_OK == upnp->get_ip (port,
 
293
                                     "UDP",
 
294
                                     &haddr->ip))) ||
 
295
        (GNUNET_SYSERR !=
 
296
         GNUNET_IP_get_public_ipv4_address (cfg, ectx, &haddr->ip))))
284
297
    {
285
 
      FREE (msg);
286
 
      GE_LOG (ectx,
287
 
              GE_WARNING | GE_ADMIN | GE_USER | GE_BULK,
288
 
              _("UDP: Could not determine my public IP address.\n"));
 
298
      GNUNET_free (msg);
 
299
      GNUNET_GE_LOG (ectx,
 
300
                     GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_USER |
 
301
                     GNUNET_GE_BULK,
 
302
                     _("UDP: Could not determine my public IP address.\n"));
289
303
      return NULL;
290
304
    }
291
305
  haddr->port = htons (port);
292
306
  haddr->reserved = htons (0);
293
307
  if (0 != memcmp (haddr, &last_addr, sizeof (HostAddress)))
294
308
    {
295
 
      GE_LOG (ectx,
296
 
              GE_DEBUG | GE_USER | GE_BULK,
297
 
              "UDP uses IP address %u.%u.%u.%u.\n",
298
 
              PRIP (ntohl (*(int *) &haddr->ip)));
 
309
      GNUNET_GE_LOG (ectx,
 
310
                     GNUNET_GE_DEBUG | GNUNET_GE_USER | GNUNET_GE_BULK,
 
311
                     "UDP uses IP address %u.%u.%u.%u.\n",
 
312
                     GNUNET_PRIP (ntohl (*(int *) &haddr->ip)));
299
313
      last_addr = *haddr;
300
314
    }
301
315
  msg->senderAddressSize = htons (sizeof (HostAddress));
302
 
  msg->protocol = htons (UDP_PROTOCOL_NUMBER);
 
316
  msg->protocol = htons (GNUNET_TRANSPORT_PROTOCOL_NUMBER_UDP);
303
317
  msg->MTU = htonl (udpAPI.mtu);
304
318
  return msg;
305
319
}
307
321
/**
308
322
 * Send a message to the specified remote node.
309
323
 *
310
 
 * @param tsession the P2P_hello_MESSAGE identifying the remote node
 
324
 * @param tsession the GNUNET_MessageHello identifying the remote node
311
325
 * @param message what to send
312
326
 * @param size the size of the message
313
 
 * @return SYSERR on error, OK on success
 
327
 * @return GNUNET_SYSERR on error, GNUNET_OK on success
314
328
 */
315
329
static int
316
 
udpSend (TSession * tsession,
 
330
udpSend (GNUNET_TSession * tsession,
317
331
         const void *message, const unsigned int size, int important)
318
332
{
319
333
  UDPMessage *mp;
320
 
  P2P_hello_MESSAGE *hello;
 
334
  GNUNET_MessageHello *hello;
321
335
  HostAddress *haddr;
322
336
  struct sockaddr_in sin;       /* an Internet endpoint address */
323
337
  int ok;
324
338
  int ssize;
325
339
  size_t sent;
326
340
 
327
 
  GE_ASSERT (NULL, tsession != NULL);
 
341
  GNUNET_GE_ASSERT (NULL, tsession != NULL);
328
342
  if (udp_sock == NULL)
329
 
    return SYSERR;
 
343
    return GNUNET_SYSERR;
330
344
  if (size == 0)
331
345
    {
332
 
      GE_BREAK (ectx, 0);
333
 
      return SYSERR;
 
346
      GNUNET_GE_BREAK (ectx, 0);
 
347
      return GNUNET_SYSERR;
334
348
    }
335
349
  if (size > udpAPI.mtu)
336
350
    {
337
 
      GE_BREAK (ectx, 0);
338
 
      return SYSERR;
 
351
      GNUNET_GE_BREAK (ectx, 0);
 
352
      return GNUNET_SYSERR;
339
353
    }
340
 
  hello = (P2P_hello_MESSAGE *) tsession->internal;
 
354
  hello = (GNUNET_MessageHello *) tsession->internal;
341
355
  if (hello == NULL)
342
 
    return SYSERR;
 
356
    return GNUNET_SYSERR;
343
357
 
344
358
  haddr = (HostAddress *) & hello[1];
345
359
  ssize = size + sizeof (UDPMessage);
346
 
  mp = MALLOC (ssize);
 
360
  mp = GNUNET_malloc (ssize);
347
361
  mp->header.size = htons (ssize);
348
362
  mp->header.type = 0;
349
363
  mp->sender = *(coreAPI->myIdentity);
350
364
  memcpy (&mp[1], message, size);
351
 
  ok = SYSERR;
 
365
  ok = GNUNET_SYSERR;
352
366
  memset (&sin, 0, sizeof (sin));
353
367
  sin.sin_family = AF_INET;
354
368
  sin.sin_port = haddr->port;
355
369
 
356
 
  GE_ASSERT (ectx, sizeof (struct in_addr) == sizeof (IPaddr));
357
 
  memcpy (&sin.sin_addr, &haddr->ip, sizeof (IPaddr));
 
370
  GNUNET_GE_ASSERT (ectx,
 
371
                    sizeof (struct in_addr) == sizeof (GNUNET_IPv4Address));
 
372
  memcpy (&sin.sin_addr, &haddr->ip, sizeof (GNUNET_IPv4Address));
358
373
#if DEBUG_UDP
359
 
  GE_LOG (ectx,
360
 
          GE_DEBUG | GE_USER | GE_BULK,
361
 
          "Sending message of %d bytes via UDP to %u.%u.%u.%u:%u.\n",
362
 
          ssize, PRIP (ntohl (*(int *) &sin.sin_addr)), ntohs (sin.sin_port));
 
374
  GNUNET_GE_LOG (ectx,
 
375
                 GNUNET_GE_DEBUG | GNUNET_GE_USER | GNUNET_GE_BULK,
 
376
                 "Sending message of %d bytes via UDP to %u.%u.%u.%u:%u.\n",
 
377
                 ssize, GNUNET_PRIP (ntohl (*(int *) &sin.sin_addr)),
 
378
                 ntohs (sin.sin_port));
363
379
#endif
364
380
#ifndef MINGW
365
 
  if (YES == socket_send_to (udp_sock,
366
 
                             NC_Nonblocking,
367
 
                             mp,
368
 
                             ssize, &sent, (const char *) &sin, sizeof (sin)))
 
381
  if (GNUNET_YES == GNUNET_socket_send_to (udp_sock,
 
382
                                           GNUNET_NC_NONBLOCKING,
 
383
                                           mp,
 
384
                                           ssize, &sent, (const char *) &sin,
 
385
                                           sizeof (sin)))
369
386
#else
370
387
  sent =
371
388
    win_ols_sendto (udp_sock, mp, ssize, (const char *) &sin, sizeof (sin));
372
389
  if (sent != SOCKET_ERROR)
373
390
#endif
374
391
    {
375
 
      ok = OK;
 
392
      ok = GNUNET_OK;
376
393
      if (stats != NULL)
377
394
        stats->change (stat_bytesSent, sent);
378
395
    }
379
396
  else
380
397
    {
381
 
      GE_LOG (ectx,
382
 
              GE_WARNING | GE_ADMIN | GE_BULK,
383
 
              _
384
 
              ("Failed to send message of size %d via UDP to %u.%u.%u.%u:%u: %s\n"),
385
 
              ssize, PRIP (ntohl (*(int *) &sin.sin_addr)),
386
 
              ntohs (sin.sin_port), STRERROR (errno));
 
398
      GNUNET_GE_LOG (ectx,
 
399
                     GNUNET_GE_WARNING | GNUNET_GE_ADMIN | GNUNET_GE_BULK,
 
400
                     _
 
401
                     ("Failed to send message of size %d via UDP to %u.%u.%u.%u:%u: %s\n"),
 
402
                     ssize, GNUNET_PRIP (ntohl (*(int *) &sin.sin_addr)),
 
403
                     ntohs (sin.sin_port), STRERROR (errno));
387
404
      if (stats != NULL)
388
405
        stats->change (stat_bytesDropped, ssize);
389
406
    }
390
 
  FREE (mp);
 
407
  GNUNET_free (mp);
391
408
  return ok;
392
409
}
393
410
 
394
411
/**
395
412
 * Start the server process to receive inbound traffic.
396
413
 *
397
 
 * @return OK on success, SYSERR if the operation failed
 
414
 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
398
415
 */
399
416
static int
400
417
startTransportServer ()
402
419
  int sock;
403
420
  unsigned short port;
404
421
 
405
 
  GE_ASSERT (ectx, selector == NULL);
 
422
  GNUNET_GE_ASSERT (ectx, selector == NULL);
406
423
  /* initialize UDP network */
407
424
  port = getGNUnetUDPPort ();
408
425
  if (port != 0)
409
426
    {
410
427
      sock = listensock (port);
411
428
      if (sock == -1)
412
 
        return SYSERR;
413
 
      selector = select_create ("udp", YES, ectx, load_monitor, sock, sizeof (struct sockaddr_in), 0,   /* timeout */
414
 
                                &select_message_handler,
415
 
                                NULL,
416
 
                                &select_accept_handler,
417
 
                                &isRejected,
418
 
                                &select_close_handler,
419
 
                                NULL, 64 * 1024, 16 /* max sockets */ );
 
429
        return GNUNET_SYSERR;
 
430
      selector = GNUNET_select_create ("udp", GNUNET_YES, ectx, load_monitor, sock, sizeof (struct sockaddr_in), 0,     /* timeout */
 
431
                                       &select_message_handler,
 
432
                                       NULL,
 
433
                                       &select_accept_handler,
 
434
                                       &isRejected,
 
435
                                       &select_close_handler,
 
436
                                       NULL, 64 * 1024,
 
437
                                       16 /* max sockets */ );
420
438
      if (selector == NULL)
421
 
        return SYSERR;
 
439
        return GNUNET_SYSERR;
422
440
    }
423
441
#ifndef MINGW
424
442
  sock = SOCKET (PF_INET, SOCK_DGRAM, 17);
427
445
#endif
428
446
  if (sock == -1)
429
447
    {
430
 
      GE_LOG_STRERROR (ectx, GE_ERROR | GE_ADMIN | GE_BULK, "socket");
431
 
      select_destroy (selector);
 
448
      GNUNET_GE_LOG_STRERROR (ectx,
 
449
                              GNUNET_GE_ERROR | GNUNET_GE_ADMIN |
 
450
                              GNUNET_GE_BULK, "socket");
 
451
      GNUNET_select_destroy (selector);
432
452
      selector = NULL;
433
 
      return SYSERR;
 
453
      return GNUNET_SYSERR;
434
454
    }
435
 
  udp_sock = socket_create (ectx, load_monitor, sock);
436
 
  GE_ASSERT (ectx, udp_sock != NULL);
437
 
  return OK;
 
455
  udp_sock = GNUNET_socket_create (ectx, load_monitor, sock);
 
456
  GNUNET_GE_ASSERT (ectx, udp_sock != NULL);
 
457
  return GNUNET_OK;
438
458
}
439
459
 
440
460
/**
445
465
{
446
466
  char *ch;
447
467
 
448
 
  MUTEX_LOCK (configLock);
449
 
  FREENONNULL (filteredNetworks_);
450
 
  FREENONNULL (allowedNetworks_);
451
 
  ch = NULL;
452
 
  GC_get_configuration_value_string (cfg, "UDP", "BLACKLIST", "", &ch);
453
 
  filteredNetworks_ = parse_ipv4_network_specification (ectx, ch);
454
 
  FREE (ch);
455
 
  ch = NULL;
456
 
  GC_get_configuration_value_string (cfg, "UDP", "WHITELIST", "", &ch);
 
468
  GNUNET_mutex_lock (configLock);
 
469
  GNUNET_free_non_null (filteredNetworks_);
 
470
  GNUNET_free_non_null (allowedNetworks_);
 
471
  ch = NULL;
 
472
  GNUNET_GC_get_configuration_value_string (cfg, "UDP", "BLACKLIST", "", &ch);
 
473
  filteredNetworks_ = GNUNET_parse_ipv4_network_specification (ectx, ch);
 
474
  GNUNET_free (ch);
 
475
  ch = NULL;
 
476
  GNUNET_GC_get_configuration_value_string (cfg, "UDP", "WHITELIST", "", &ch);
457
477
  if (strlen (ch) > 0)
458
 
    allowedNetworks_ = parse_ipv4_network_specification (ectx, ch);
 
478
    allowedNetworks_ = GNUNET_parse_ipv4_network_specification (ectx, ch);
459
479
  else
460
480
    allowedNetworks_ = NULL;
461
 
  FREE (ch);
462
 
  MUTEX_UNLOCK (configLock);
 
481
  GNUNET_free (ch);
 
482
  GNUNET_mutex_unlock (configLock);
463
483
  return 0;
464
484
}
465
485
 
467
487
 * Convert UDP hello to IP address
468
488
 */
469
489
static int
470
 
helloToAddress (const P2P_hello_MESSAGE * hello,
 
490
helloToAddress (const GNUNET_MessageHello * hello,
471
491
                void **sa, unsigned int *sa_len)
472
492
{
473
493
  const HostAddress *haddr = (const HostAddress *) &hello[1];
474
494
  struct sockaddr_in *serverAddr;
475
495
 
476
496
  *sa_len = sizeof (struct sockaddr_in);
477
 
  serverAddr = MALLOC (sizeof (struct sockaddr_in));
 
497
  serverAddr = GNUNET_malloc (sizeof (struct sockaddr_in));
478
498
  *sa = serverAddr;
479
499
  memset (serverAddr, 0, sizeof (struct sockaddr_in));
480
500
  serverAddr->sin_family = AF_INET;
481
 
  memcpy (&serverAddr->sin_addr, haddr, sizeof (IPaddr));
 
501
  memcpy (&serverAddr->sin_addr, haddr, sizeof (GNUNET_IPv4Address));
482
502
  serverAddr->sin_port = haddr->port;
483
 
  return OK;
 
503
  return GNUNET_OK;
484
504
}
485
505
 
486
506
/**
493
513
 * The exported method. Makes the core api available via a global and
494
514
 * returns the udp transport API.
495
515
 */
496
 
TransportAPI *
497
 
inittransport_udp (CoreAPIForTransport * core)
 
516
GNUNET_TransportAPI *
 
517
inittransport_udp (GNUNET_CoreAPIForTransport * core)
498
518
{
499
519
  unsigned long long mtu;
500
520
 
501
521
  ectx = core->ectx;
502
522
  cfg = core->cfg;
503
523
  load_monitor = core->load_monitor;
504
 
  GE_ASSERT (ectx, sizeof (HostAddress) == 8);
505
 
  GE_ASSERT (ectx, sizeof (UDPMessage) == 68);
 
524
  GNUNET_GE_ASSERT (ectx, sizeof (HostAddress) == 8);
 
525
  GNUNET_GE_ASSERT (ectx, sizeof (UDPMessage) == 68);
506
526
  coreAPI = core;
507
 
  if (-1 == GC_get_configuration_value_number (cfg,
508
 
                                               "UDP",
509
 
                                               "MTU",
510
 
                                               sizeof (UDPMessage)
511
 
                                               + P2P_MESSAGE_OVERHEAD
512
 
                                               + sizeof (MESSAGE_HEADER) + 32,
513
 
                                               65500, MESSAGE_SIZE, &mtu))
 
527
  if (-1 == GNUNET_GC_get_configuration_value_number (cfg,
 
528
                                                      "UDP",
 
529
                                                      "MTU",
 
530
                                                      sizeof (UDPMessage)
 
531
                                                      +
 
532
                                                      GNUNET_P2P_MESSAGE_OVERHEAD
 
533
                                                      +
 
534
                                                      sizeof
 
535
                                                      (GNUNET_MessageHeader) +
 
536
                                                      32, 65500,
 
537
                                                      MESSAGE_SIZE, &mtu))
514
538
    {
515
539
      return NULL;
516
540
    }
517
541
  if (mtu < 1200)
518
 
    GE_LOG (ectx,
519
 
            GE_ERROR | GE_USER | GE_IMMEDIATE,
520
 
            _("MTU %llu for `%s' is probably too low!\n"), mtu, "UDP");
521
 
  if (GC_get_configuration_value_yesno (cfg, "UDP", "UPNP", YES) == YES)
 
542
    GNUNET_GE_LOG (ectx,
 
543
                   GNUNET_GE_ERROR | GNUNET_GE_USER | GNUNET_GE_IMMEDIATE,
 
544
                   _("MTU %llu for `%s' is probably too low!\n"), mtu, "UDP");
 
545
  if (GNUNET_GC_get_configuration_value_yesno (cfg, "UDP", "UPNP", GNUNET_YES)
 
546
      == GNUNET_YES)
522
547
    {
523
 
      upnp = coreAPI->requestService ("upnp");
 
548
      upnp = coreAPI->request_service ("upnp");
524
549
 
525
550
      if (upnp == NULL)
526
 
        GE_LOG (ectx,
527
 
                GE_ERROR | GE_USER | GE_IMMEDIATE,
528
 
                "The UPnP service could not be loaded. To disable UPnP, set the "
529
 
                "configuration option \"UPNP\" in section \"UDP\" to \"NO\"\n");
 
551
        GNUNET_GE_LOG (ectx,
 
552
                       GNUNET_GE_ERROR | GNUNET_GE_USER | GNUNET_GE_IMMEDIATE,
 
553
                       "The UPnP service could not be loaded. To disable UPnP, set the "
 
554
                       "configuration option \"UPNP\" in section \"UDP\" to \"NO\"\n");
530
555
    }
531
 
  stats = coreAPI->requestService ("stats");
 
556
  stats = coreAPI->request_service ("stats");
532
557
  if (stats != NULL)
533
558
    {
534
559
      stat_bytesReceived
539
564
      stat_udpConnected
540
565
        = stats->create (gettext_noop ("# UDP connections (right now)"));
541
566
    }
542
 
  configLock = MUTEX_CREATE (NO);
 
567
  configLock = GNUNET_mutex_create (GNUNET_NO);
543
568
  reloadConfiguration ();
544
 
  udpAPI.protocolNumber = UDP_PROTOCOL_NUMBER;
 
569
  udpAPI.protocolNumber = GNUNET_TRANSPORT_PROTOCOL_NUMBER_UDP;
545
570
  udpAPI.mtu = mtu - sizeof (UDPMessage);
546
571
  udpAPI.cost = 20000;
547
572
  udpAPI.verifyHello = &verifyHello;
563
588
{
564
589
  if (stats != NULL)
565
590
    {
566
 
      coreAPI->releaseService (stats);
 
591
      coreAPI->release_service (stats);
567
592
      stats = NULL;
568
593
    }
569
594
  if (upnp != NULL)
570
595
    {
571
 
      coreAPI->releaseService (upnp);
 
596
      coreAPI->release_service (upnp);
572
597
      upnp = NULL;
573
598
    }
574
 
  MUTEX_DESTROY (configLock);
 
599
  GNUNET_mutex_destroy (configLock);
575
600
  configLock = NULL;
576
 
  FREENONNULL (filteredNetworks_);
 
601
  GNUNET_free_non_null (filteredNetworks_);
577
602
  coreAPI = NULL;
578
603
}
579
604