GNUnet  0.11.x
Data Structures | Macros | Functions
nt.c File Reference
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_nt_lib.h"
Include dependency graph for nt.c:

Go to the source code of this file.

Data Structures

struct  NT_Network
 We keep a list of our local networks so we can answer LAN vs. More...
 
struct  GNUNET_NT_InterfaceScanner
 Handle to the interface scanner. More...
 

Macros

#define INTERFACE_PROCESSING_INTERVAL
 How frequently do we scan the interfaces for changes to the addresses? More...
 

Functions

const char * GNUNET_NT_to_string (enum GNUNET_NetworkType net)
 Convert a enum GNUNET_NetworkType to a string. More...
 
static void delete_networks (struct GNUNET_NT_InterfaceScanner *is)
 Delete all entries from the current network list. More...
 
static int interface_proc (void *cls, const char *name, int isDefault, const struct sockaddr *addr, const struct sockaddr *broadcast_addr, const struct sockaddr *netmask, socklen_t addrlen)
 Function invoked for each interface found. More...
 
static void get_addresses (void *cls)
 Periodically get list of network addresses from our interfaces. More...
 
enum GNUNET_NetworkType GNUNET_NT_scanner_get_type (struct GNUNET_NT_InterfaceScanner *is, const struct sockaddr *addr, socklen_t addrlen)
 Returns where the address is located: LAN or WAN or ... More...
 
struct GNUNET_NT_InterfaceScannerGNUNET_NT_scanner_init ()
 Initialize the interface scanner. More...
 
void GNUNET_NT_scanner_done (struct GNUNET_NT_InterfaceScanner *is)
 Client is done with the interface scanner, release resources. More...
 

Macro Definition Documentation

◆ INTERFACE_PROCESSING_INTERVAL

#define INTERFACE_PROCESSING_INTERVAL
Value:
#define GNUNET_TIME_UNIT_MINUTES
One minute.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Multiply relative time by a given factor.
Definition: time.c:442

How frequently do we scan the interfaces for changes to the addresses?

Definition at line 33 of file nt.c.

Referenced by get_addresses(), and GNUNET_NT_scanner_init().

Function Documentation

◆ delete_networks()

static void delete_networks ( struct GNUNET_NT_InterfaceScanner is)
static

Delete all entries from the current network list.

Parameters
isscanner to clean up

Definition at line 135 of file nt.c.

References GNUNET_CONTAINER_DLL_remove, GNUNET_free, GNUNET_NT_InterfaceScanner::net_head, and GNUNET_NT_InterfaceScanner::net_tail.

Referenced by get_addresses(), and GNUNET_NT_scanner_done().

136 {
137  struct NT_Network *cur;
138 
139  while (NULL != (cur = is->net_head))
140  {
142  is->net_tail,
143  cur);
144  GNUNET_free (cur);
145  }
146 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
We keep a list of our local networks so we can answer LAN vs.
Definition: nt.c:78
struct NT_Network * net_head
Head of LAN networks list.
Definition: nt.c:115
struct NT_Network * net_tail
Tail of LAN networks list.
Definition: nt.c:120
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function:

◆ interface_proc()

static int interface_proc ( void *  cls,
const char *  name,
int  isDefault,
const struct sockaddr *  addr,
const struct sockaddr *  broadcast_addr,
const struct sockaddr *  netmask,
socklen_t  addrlen 
)
static

Function invoked for each interface found.

Adds the interface's network addresses to the respective DLL, so we can distinguish between LAN and WAN.

Parameters
clsclosure with the struct GNUNET_NT_InterfaceScanner
namename of the interface (can be NULL for unknown)
isDefaultis this presumably the default interface
addraddress of this interface (can be NULL for unknown or unassigned)
broadcast_addrthe broadcast address (can be NULL for unknown or unassigned)
netmaskthe network mask (can be NULL for unknown or unassigned)
addrlenlength of the address
Returns
GNUNET_OK to continue iteration

Definition at line 164 of file nt.c.

References GNUNET_a2s(), GNUNET_CONTAINER_DLL_insert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_log_from, GNUNET_malloc, GNUNET_memcpy, GNUNET_OK, GNUNET_strdup, is, GNUNET_NT_InterfaceScanner::net_head, GNUNET_NT_InterfaceScanner::net_tail, NT_Network::netmask, and NT_Network::network.

Referenced by get_addresses(), and GNUNET_NT_scanner_init().

171 {
172  struct GNUNET_NT_InterfaceScanner *is = cls;
173  /* Calculate network */
174  struct NT_Network *net = NULL;
175 
176  (void) name;
177  (void) isDefault;
178  (void) broadcast_addr;
179 
180  /* Skipping IPv4 loopback addresses since we have special check */
181  if (addr->sa_family == AF_INET)
182  {
183  const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
184 
185  if ((a4->sin_addr.s_addr & htonl (0xff000000)) == htonl (0x7f000000))
186  return GNUNET_OK;
187  }
188  /* Skipping IPv6 loopback addresses since we have special check */
189  if (addr->sa_family == AF_INET6)
190  {
191  const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
192  if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
193  return GNUNET_OK;
194  }
195 
196  if (addr->sa_family == AF_INET)
197  {
198  const struct sockaddr_in *addr4 = (const struct sockaddr_in *) addr;
199  const struct sockaddr_in *netmask4 = (const struct sockaddr_in *) netmask;
200  struct sockaddr_in *tmp;
201  struct sockaddr_in network4;
202 
203  net = GNUNET_malloc (sizeof(struct NT_Network) + 2 * sizeof(struct
204  sockaddr_in));
205  tmp = (struct sockaddr_in *) &net[1];
206  net->network = (struct sockaddr *) &tmp[0];
207  net->netmask = (struct sockaddr *) &tmp[1];
208  net->length = addrlen;
209 
210  memset (&network4,
211  0,
212  sizeof(network4));
213  network4.sin_family = AF_INET;
214 #if HAVE_SOCKADDR_IN_SIN_LEN
215  network4.sin_len = sizeof(network4);
216 #endif
217  network4.sin_addr.s_addr = (addr4->sin_addr.s_addr
218  & netmask4->sin_addr.s_addr);
219 
220  GNUNET_memcpy (net->netmask,
221  netmask4,
222  sizeof(struct sockaddr_in));
223  GNUNET_memcpy (net->network,
224  &network4,
225  sizeof(struct sockaddr_in));
226  }
227 
228  if (addr->sa_family == AF_INET6)
229  {
230  const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *) addr;
231  const struct sockaddr_in6 *netmask6 = (const struct sockaddr_in6 *) netmask;
232  struct sockaddr_in6 *tmp;
233  struct sockaddr_in6 network6;
234 
235  net = GNUNET_malloc (sizeof(struct NT_Network) + 2 * sizeof(struct
236  sockaddr_in6));
237  tmp = (struct sockaddr_in6 *) &net[1];
238  net->network = (struct sockaddr *) &tmp[0];
239  net->netmask = (struct sockaddr *) &tmp[1];
240  net->length = addrlen;
241 
242  memset (&network6, 0, sizeof(network6));
243  network6.sin6_family = AF_INET6;
244 #if HAVE_SOCKADDR_IN_SIN_LEN
245  network6.sin6_len = sizeof(network6);
246 #endif
247  unsigned int c = 0;
248  uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr;
249  uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr;
250  uint32_t *net_elem = (uint32_t *) &network6.sin6_addr;
251  for (c = 0; c < 4; c++)
252  net_elem[c] = addr_elem[c] & mask_elem[c];
253 
254  GNUNET_memcpy (net->netmask,
255  netmask6,
256  sizeof(struct sockaddr_in6));
257  GNUNET_memcpy (net->network,
258  &network6,
259  sizeof(struct sockaddr_in6));
260  }
261  if (NULL == net)
262  return GNUNET_OK; /* odd / unsupported address family */
263 
264  /* Store in list */
265 #if VERBOSE_NT
266  char *netmask = GNUNET_strdup (GNUNET_a2s ((struct sockaddr *) net->netmask,
267  addrlen));
269  "nt",
270  "Adding network `%s', netmask `%s'\n",
271  GNUNET_a2s ((struct sockaddr *) net->network,
272  addrlen),
273  netmask);
274  GNUNET_free (netmask);
275 #endif
277  is->net_tail,
278  net);
279 
280  return GNUNET_OK;
281 }
Handle to the interface scanner.
Definition: nt.c:110
We keep a list of our local networks so we can answer LAN vs.
Definition: nt.c:78
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
static struct GNUNET_NT_InterfaceScanner * is
Network scanner to determine network types.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
struct NT_Network * net_head
Head of LAN networks list.
Definition: nt.c:115
struct NT_Network * net_tail
Tail of LAN networks list.
Definition: nt.c:120
const char * GNUNET_a2s(const struct sockaddr *addr, socklen_t addrlen)
Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string (for printing debug messages)...
const char * name
#define GNUNET_log_from(kind, comp,...)
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_addresses()

static void get_addresses ( void *  cls)
static

Periodically get list of network addresses from our interfaces.

Parameters
clsclosure

Definition at line 290 of file nt.c.

References delete_networks(), GNUNET_OS_network_interfaces_list(), GNUNET_SCHEDULER_add_delayed(), interface_proc(), INTERFACE_PROCESSING_INTERVAL, GNUNET_NT_InterfaceScanner::interface_task, and is.

Referenced by GNUNET_NT_scanner_init().

291 {
292  struct GNUNET_NT_InterfaceScanner *is = cls;
293 
294  is->interface_task = NULL;
295  delete_networks (is);
297  is);
300  &get_addresses,
301  is);
302 }
Handle to the interface scanner.
Definition: nt.c:110
static void delete_networks(struct GNUNET_NT_InterfaceScanner *is)
Delete all entries from the current network list.
Definition: nt.c:135
static struct GNUNET_NT_InterfaceScanner * is
Network scanner to determine network types.
void GNUNET_OS_network_interfaces_list(GNUNET_OS_NetworkInterfaceProcessor proc, void *proc_cls)
Enumerate all network interfaces.
Definition: os_network.c:396
static int interface_proc(void *cls, const char *name, int isDefault, const struct sockaddr *addr, const struct sockaddr *broadcast_addr, const struct sockaddr *netmask, socklen_t addrlen)
Function invoked for each interface found.
Definition: nt.c:164
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:1253
#define INTERFACE_PROCESSING_INTERVAL
How frequently do we scan the interfaces for changes to the addresses?
Definition: nt.c:33
struct GNUNET_SCHEDULER_Task * interface_task
Task for periodically refreshing our LAN network list.
Definition: nt.c:125
static void get_addresses(void *cls)
Periodically get list of network addresses from our interfaces.
Definition: nt.c:290
Here is the call graph for this function:
Here is the caller graph for this function: