GNUnet  0.10.x
Data Structures | Macros | Functions | Variables
gnunet-service-nat_externalip.c File Reference

Code to figure out what our external IPv4 address(es) might be (external IPv4s are what is seen on the rest of the Internet). More...

#include "platform.h"
#include <math.h>
#include "gnunet_util_lib.h"
#include "gnunet_protocols.h"
#include "gnunet_signatures.h"
#include "gnunet_statistics_service.h"
#include "gnunet_resolver_service.h"
#include "gnunet_nat_service.h"
#include "gnunet-service-nat.h"
#include "gnunet-service-nat_externalip.h"
#include "gnunet-service-nat_stun.h"
#include "gnunet-service-nat_mini.h"
#include "gnunet-service-nat_helper.h"
#include "nat.h"
#include <gcrypt.h>
Include dependency graph for gnunet-service-nat_externalip.c:

Go to the source code of this file.

Data Structures

struct  GN_ExternalIPMonitor
 Handle to monitor for external IP changes. More...
 

Macros

#define EXTERN_IP_RETRY_TIMEOUT   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 15)
 How long do we wait until we re-try running external-ip if the command failed to terminate nicely? More...
 
#define EXTERN_IP_RETRY_FAILURE   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 30)
 How long do we wait until we re-try running external-ip if the command failed (but terminated)? More...
 
#define EXTERN_IP_RETRY_SUCCESS   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
 How long do we wait until we re-try running external-ip if the command succeeded? More...
 

Functions

static void notify_monitors_external_ipv4_change (int add, const struct in_addr *v4)
 Tell relevant clients about a change in our external IPv4 address. More...
 
static void run_external_ip (void *cls)
 Task used to run external-ip to get our external IPv4 address and pass it to NATed clients if possible. More...
 
static void handle_external_ip (void *cls, const struct in_addr *addr, enum GNUNET_NAT_StatusCode result)
 We learn our current external IP address. More...
 
void GN_nat_status_changed (int have_nat)
 We have changed our opinion about being NATed in the first place. More...
 
struct GN_ExternalIPMonitorGN_external_ipv4_monitor_start (GN_NotifyExternalIPv4Change cb, void *cb_cls)
 Start monitoring external IPv4 addresses. More...
 
void GN_external_ipv4_monitor_stop (struct GN_ExternalIPMonitor *mon)
 Stop calling monitor. More...
 

Variables

static struct GN_ExternalIPMonitormon_head
 List of monitors, kept in DLL. More...
 
static struct GN_ExternalIPMonitormon_tail
 List of monitors, kept in DLL. More...
 
static struct GNUNET_SCHEDULER_Taskprobe_external_ip_task
 Task run to obtain our external IP (if enable_upnp is set and if we find we have a NATed IP address). More...
 
static struct GNUNET_NAT_ExternalHandleprobe_external_ip_op
 Handle to our operation to run external-ip. More...
 
static struct in_addr mini_external_ipv4
 What is our external IP address as claimed by external-ip? 0 for unknown. More...
 

Detailed Description

Code to figure out what our external IPv4 address(es) might be (external IPv4s are what is seen on the rest of the Internet).

This can be implemented using different methods, and we allow the main service to be notified about changes to what we believe is our external IPv4 address.

Note that this is explicitly only about NATed systems; if one of our network interfaces has a global IP address this does not count as "external".

Functions for monitoring external IPv4 addresses

Author
Christian Grothoff

Definition in file gnunet-service-nat_externalip.c.

Macro Definition Documentation

◆ EXTERN_IP_RETRY_TIMEOUT

#define EXTERN_IP_RETRY_TIMEOUT   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 15)

How long do we wait until we re-try running external-ip if the command failed to terminate nicely?

Definition at line 57 of file gnunet-service-nat_externalip.c.

Referenced by run_external_ip().

◆ EXTERN_IP_RETRY_FAILURE

#define EXTERN_IP_RETRY_FAILURE   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 30)

How long do we wait until we re-try running external-ip if the command failed (but terminated)?

Definition at line 63 of file gnunet-service-nat_externalip.c.

Referenced by handle_external_ip().

◆ EXTERN_IP_RETRY_SUCCESS

#define EXTERN_IP_RETRY_SUCCESS   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)

How long do we wait until we re-try running external-ip if the command succeeded?

Definition at line 69 of file gnunet-service-nat_externalip.c.

Referenced by handle_external_ip().

Function Documentation

◆ notify_monitors_external_ipv4_change()

static void notify_monitors_external_ipv4_change ( int  add,
const struct in_addr *  v4 
)
static

Tell relevant clients about a change in our external IPv4 address.

Parameters
addGNUNET_YES to add, GNUNET_NO to remove
v4the external address that changed

Definition at line 134 of file gnunet-service-nat_externalip.c.

References GN_ExternalIPMonitor::next, and run_external_ip().

Referenced by handle_external_ip().

136 {
137  for (struct GN_ExternalIPMonitor *mon = mon_head;
138  NULL != mon;
139  mon = mon->next)
140  mon->cb(mon->cb_cls,
141  v4,
142  add);
143 }
Handle to monitor for external IP changes.
struct GN_ExternalIPMonitor * next
Kept in DLL.
static struct GN_ExternalIPMonitor * mon_head
List of monitors, kept in DLL.
static int add
Desired action is to add a record.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ run_external_ip()

static void run_external_ip ( void *  cls)
static

Task used to run external-ip to get our external IPv4 address and pass it to NATed clients if possible.

Parameters
clsNULL

Definition at line 217 of file gnunet-service-nat_externalip.c.

References EXTERN_IP_RETRY_TIMEOUT, GNUNET_NAT_mini_get_external_ipv4_(), GNUNET_NAT_mini_get_external_ipv4_cancel_(), GNUNET_SCHEDULER_add_delayed(), and handle_external_ip().

Referenced by GN_nat_status_changed(), handle_external_ip(), and notify_monitors_external_ipv4_change().

218 {
222  NULL);
223  if (NULL != probe_external_ip_op)
224  {
226  probe_external_ip_op = NULL;
227  }
230  NULL);
231 }
static void run_external_ip(void *cls)
Task used to run external-ip to get our external IPv4 address and pass it to NATed clients if possibl...
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1237
struct GNUNET_NAT_ExternalHandle * GNUNET_NAT_mini_get_external_ipv4_(GNUNET_NAT_IPCallback cb, void *cb_cls)
Try to get the external IPv4 address of this peer.
#define EXTERN_IP_RETRY_TIMEOUT
How long do we wait until we re-try running external-ip if the command failed to terminate nicely...
static struct GNUNET_SCHEDULER_Task * probe_external_ip_task
Task run to obtain our external IP (if enable_upnp is set and if we find we have a NATed IP address)...
static void handle_external_ip(void *cls, const struct in_addr *addr, enum GNUNET_NAT_StatusCode result)
We learn our current external IP address.
static struct GNUNET_NAT_ExternalHandle * probe_external_ip_op
Handle to our operation to run external-ip.
void GNUNET_NAT_mini_get_external_ipv4_cancel_(struct GNUNET_NAT_ExternalHandle *eh)
Cancel operation.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_external_ip()

static void handle_external_ip ( void *  cls,
const struct in_addr *  addr,
enum GNUNET_NAT_StatusCode  result 
)
static

We learn our current external IP address.

If it changed, notify all of our applicable clients. Also re-schedule run_external_ip with an appropriate timeout.

Parameters
clsNULL
addrthe address, NULL on errors
resultGNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code

Definition at line 166 of file gnunet-service-nat_externalip.c.

References buf, EXTERN_IP_RETRY_FAILURE, EXTERN_IP_RETRY_SUCCESS, GNUNET_assert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_NAT_ERROR_SUCCESS, GNUNET_NO, GNUNET_SCHEDULER_add_delayed(), GNUNET_SCHEDULER_cancel(), GNUNET_YES, mini_external_ipv4, notify_monitors_external_ipv4_change(), and run_external_ip().

Referenced by run_external_ip().

169 {
170  char buf[INET_ADDRSTRLEN];
171 
172  probe_external_ip_op = NULL;
179  NULL);
180  switch (result)
181  {
183  GNUNET_assert(NULL != addr);
184  if (addr->s_addr == mini_external_ipv4.s_addr)
185  return; /* not change */
187  "Our external IP is now %s\n",
188  inet_ntop(AF_INET,
189  addr,
190  buf,
191  sizeof(buf)));
192  if (0 != mini_external_ipv4.s_addr)
198  break;
199 
200  default:
201  if (0 != mini_external_ipv4.s_addr)
204  mini_external_ipv4.s_addr = 0;
205  break;
206  }
207 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static void run_external_ip(void *cls)
Task used to run external-ip to get our external IPv4 address and pass it to NATed clients if possibl...
#define GNUNET_NO
Definition: gnunet_common.h:78
#define EXTERN_IP_RETRY_SUCCESS
How long do we wait until we re-try running external-ip if the command succeeded? ...
struct sockaddr_storage addr
The address itself (i.e.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1237
static char buf[2048]
static int result
Global testing status.
static struct in_addr mini_external_ipv4
What is our external IP address as claimed by external-ip? 0 for unknown.
static struct GNUNET_SCHEDULER_Task * probe_external_ip_task
Task run to obtain our external IP (if enable_upnp is set and if we find we have a NATed IP address)...
#define GNUNET_log(kind,...)
static struct GNUNET_NAT_ExternalHandle * probe_external_ip_op
Handle to our operation to run external-ip.
#define GNUNET_YES
Definition: gnunet_common.h:77
#define EXTERN_IP_RETRY_FAILURE
How long do we wait until we re-try running external-ip if the command failed (but terminated)...
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956
static void notify_monitors_external_ipv4_change(int add, const struct in_addr *v4)
Tell relevant clients about a change in our external IPv4 address.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GN_nat_status_changed()

void GN_nat_status_changed ( int  have_nat)

We have changed our opinion about being NATed in the first place.

Adapt our probing.

Parameters
have_natGNUNET_YES if we believe we are behind NAT

Definition at line 241 of file gnunet-service-nat_externalip.c.

References enable_upnp, GNUNET_NAT_mini_get_external_ipv4_cancel_(), GNUNET_NO, GNUNET_SCHEDULER_add_now(), GNUNET_SCHEDULER_cancel(), GNUNET_YES, and run_external_ip().

Referenced by run_scan(), and shutdown_task().

242 {
243  if (GNUNET_YES != enable_upnp)
244  return;
245  if ((GNUNET_YES == have_nat) &&
246  (NULL == probe_external_ip_task) &&
247  (NULL == probe_external_ip_op))
248  {
251  NULL);
252  return;
253  }
254  if (GNUNET_NO == have_nat)
255  {
256  if (NULL != probe_external_ip_task)
257  {
259  probe_external_ip_task = NULL;
260  }
261  if (NULL != probe_external_ip_op)
262  {
264  probe_external_ip_op = NULL;
265  }
266  }
267 }
static void run_external_ip(void *cls)
Task used to run external-ip to get our external IPv4 address and pass it to NATed clients if possibl...
#define GNUNET_NO
Definition: gnunet_common.h:78
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1264
static struct GNUNET_SCHEDULER_Task * probe_external_ip_task
Task run to obtain our external IP (if enable_upnp is set and if we find we have a NATed IP address)...
int enable_upnp
Is UPnP enabled? GNUNET_YES if enabled, GNUNET_NO if disabled, GNUNET_SYSERR if configuration enabled...
static struct GNUNET_NAT_ExternalHandle * probe_external_ip_op
Handle to our operation to run external-ip.
#define GNUNET_YES
Definition: gnunet_common.h:77
void GNUNET_NAT_mini_get_external_ipv4_cancel_(struct GNUNET_NAT_ExternalHandle *eh)
Cancel operation.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GN_external_ipv4_monitor_start()

struct GN_ExternalIPMonitor* GN_external_ipv4_monitor_start ( GN_NotifyExternalIPv4Change  cb,
void *  cb_cls 
)

Start monitoring external IPv4 addresses.

Parameters
cbfunction to call on changes
cb_clsclosure for cb
Returns
handle to cancel

Definition at line 278 of file gnunet-service-nat_externalip.c.

References GN_ExternalIPMonitor::cb, GN_ExternalIPMonitor::cb_cls, GNUNET_CONTAINER_DLL_insert, GNUNET_new, GNUNET_YES, and mini_external_ipv4.

Referenced by handle_register().

280 {
281  struct GN_ExternalIPMonitor *mon;
282 
283  mon = GNUNET_new(struct GN_ExternalIPMonitor);
284  mon->cb = cb;
285  mon->cb_cls = cb_cls;
287  mon_tail,
288  mon);
289  if (0 != mini_external_ipv4.s_addr)
290  cb(cb_cls,
292  GNUNET_YES);
293  return mon;
294 }
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
Handle to monitor for external IP changes.
static struct in_addr mini_external_ipv4
What is our external IP address as claimed by external-ip? 0 for unknown.
static struct GN_ExternalIPMonitor * mon_head
List of monitors, kept in DLL.
GN_NotifyExternalIPv4Change cb
Function to call when we believe our external IPv4 address changed.
#define GNUNET_YES
Definition: gnunet_common.h:77
static struct GN_ExternalIPMonitor * mon_tail
List of monitors, kept in DLL.
Here is the caller graph for this function:

◆ GN_external_ipv4_monitor_stop()

void GN_external_ipv4_monitor_stop ( struct GN_ExternalIPMonitor mon)

Stop calling monitor.

Parameters
monmonitor to call

Definition at line 303 of file gnunet-service-nat_externalip.c.

References GNUNET_CONTAINER_DLL_remove, and GNUNET_free.

Referenced by client_disconnect_cb().

304 {
306  mon_tail,
307  mon);
308  GNUNET_free(mon);
309 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
static struct GN_ExternalIPMonitor * mon_head
List of monitors, kept in DLL.
static struct GN_ExternalIPMonitor * mon_tail
List of monitors, kept in DLL.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function:

Variable Documentation

◆ mon_head

struct GN_ExternalIPMonitor* mon_head
static

List of monitors, kept in DLL.

Definition at line 101 of file gnunet-service-nat_externalip.c.

◆ mon_tail

struct GN_ExternalIPMonitor* mon_tail
static

List of monitors, kept in DLL.

Definition at line 106 of file gnunet-service-nat_externalip.c.

◆ probe_external_ip_task

struct GNUNET_SCHEDULER_Task* probe_external_ip_task
static

Task run to obtain our external IP (if enable_upnp is set and if we find we have a NATed IP address).

Definition at line 112 of file gnunet-service-nat_externalip.c.

◆ probe_external_ip_op

struct GNUNET_NAT_ExternalHandle* probe_external_ip_op
static

Handle to our operation to run external-ip.

Definition at line 117 of file gnunet-service-nat_externalip.c.

◆ mini_external_ipv4

struct in_addr mini_external_ipv4
static

What is our external IP address as claimed by external-ip? 0 for unknown.

Definition at line 123 of file gnunet-service-nat_externalip.c.

Referenced by GN_external_ipv4_monitor_start(), and handle_external_ip().