GNUnet  0.20.0
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:484

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

Definition at line 33 of file nt.c.

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 129 of file nt.c.

130 {
131  struct NT_Network *cur;
132 
133  while (NULL != (cur = is->net_head))
134  {
135  GNUNET_CONTAINER_DLL_remove (is->net_head,
136  is->net_tail,
137  cur);
138  GNUNET_free (cur);
139  }
140 }
struct GNUNET_TESTING_Interpreter * is
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_free(ptr)
Wrapper around free.
We keep a list of our local networks so we can answer LAN vs.
Definition: nt.c:73

References GNUNET_CONTAINER_DLL_remove, GNUNET_free, and is.

Referenced by get_addresses(), and GNUNET_NT_scanner_done().

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 158 of file nt.c.

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

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, name, NT_Network::netmask, and NT_Network::network.

Referenced by get_addresses(), and GNUNET_NT_scanner_init().

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 284 of file nt.c.

285 {
286  struct GNUNET_NT_InterfaceScanner *is = cls;
287 
288  is->interface_task = NULL;
291  is);
292  is->interface_task = GNUNET_SCHEDULER_add_delayed (
294  &get_addresses,
295  is);
296 }
void GNUNET_OS_network_interfaces_list(GNUNET_OS_NetworkInterfaceProcessor proc, void *proc_cls)
Enumerate all network interfaces.
Definition: os_network.c:397
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:1272
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:158
static void delete_networks(struct GNUNET_NT_InterfaceScanner *is)
Delete all entries from the current network list.
Definition: nt.c:129
#define INTERFACE_PROCESSING_INTERVAL
How frequently do we scan the interfaces for changes to the addresses?
Definition: nt.c:33
static void get_addresses(void *cls)
Periodically get list of network addresses from our interfaces.
Definition: nt.c:284

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

Referenced by GNUNET_NT_scanner_init().

Here is the call graph for this function:
Here is the caller graph for this function: