/*
This file is part of GNUnet.
(C) 2009, 2010, 2011 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 3, 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 nat/nat.c
* @brief Library handling UPnP and NAT-PMP port forwarding and
* external IP address retrieval
* @author Milan Bouchet-Valat
* @author Christian Grothoff
*/
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_resolver_service.h"
#include "gnunet_nat_lib.h"
#include "nat.h"
#define LOG(kind,...) GNUNET_log_from (kind, "nat", __VA_ARGS__)
/**
* How often do we scan for changes in our IP address from our local
* interfaces?
*/
#define IFC_SCAN_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
/**
* How often do we scan for changes in how our hostname resolves?
*/
#define HOSTNAME_DNS_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 20)
/**
* How often do we scan for changes in how our external (dyndns) hostname resolves?
*/
#define DYNDNS_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 7)
/**
* How long until we give up trying to resolve our own hostname?
*/
#define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
/**
* Where did the given local address originate from?
* To be used for debugging as well as in the future
* to remove all addresses from a certain source when
* we reevaluate the source.
*/
enum LocalAddressSource
{
/**
* Address was obtained by DNS resolution of the external hostname
* given in the configuration (i.e. hole-punched DynDNS setup).
*/
LAL_EXTERNAL_IP,
/**
* Address was obtained by looking up our own hostname in DNS.
*/
LAL_HOSTNAME_DNS,
/**
* Address was obtained by scanning our hosts's network interfaces
* and taking their address (no DNS involved).
*/
LAL_INTERFACE_ADDRESS,
/**
* Addresses we were explicitly bound to.
*/
LAL_BINDTO_ADDRESS,
/**
* Addresses from UPnP or PMP
*/
LAL_UPNP,
/**
* End of the list.
*/
LAL_END
};
/**
* List of local addresses that we currently deem valid. Actual
* struct is followed by the 'struct sockaddr'. Note that the code
* intentionally makes no attempt to ensure that a particular address
* is only listed once (especially since it may come from different
* sources, and the source is an "internal" construct).
*/
struct LocalAddressList
{
/**
* This is a linked list.
*/
struct LocalAddressList *next;
/**
* Previous entry.
*/
struct LocalAddressList *prev;
/**
* Number of bytes of address that follow.
*/
socklen_t addrlen;
/**
* Origin of the local address.
*/
enum LocalAddressSource source;
};
/**
* Handle for miniupnp-based NAT traversal actions.
*/
struct MiniList
{
/**
* Doubly-linked list.
*/
struct MiniList *next;
/**
* Doubly-linked list.
*/
struct MiniList *prev;
/**
* Handle to mini-action.
*/
struct GNUNET_NAT_MiniHandle *mini;
/**
* Local port number that was mapped.
*/
uint16_t port