GNUnet 0.22.0
gnunet_testbed_lib.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define GNUNET_TESTBED_PREFIX   "GNUNET_TESTBED_PREFIX"
 

Functions

struct GNUNET_TESTBED_SystemGNUNET_TESTBED_system_create_with_portrange (const char *testdir, const char *trusted_ip, const char *hostname, uint16_t lowport, uint16_t highport)
 Create a system handle. More...
 
struct GNUNET_TESTBED_SystemGNUNET_TESTBED_system_create (const char *testdir, const char *trusted_ip, const char *hostname)
 Create a system handle. More...
 
void GNUNET_TESTBED_system_destroy (struct GNUNET_TESTBED_System *system, bool remove_paths)
 Free system resources. More...
 
uint16_t GNUNET_TESTBED_reserve_port (struct GNUNET_TESTBED_System *system)
 Reserve a TCP or UDP port for a peer. More...
 
void GNUNET_TESTBED_release_port (struct GNUNET_TESTBED_System *system, uint16_t port)
 Release reservation of a TCP or UDP port for a peer (used during #GNUNET_TESTBED_peer_destroy()). More...
 
enum GNUNET_GenericReturnValue GNUNET_TESTBED_configuration_create (struct GNUNET_TESTBED_System *system, struct GNUNET_CONFIGURATION_Handle *cfg, uint16_t **ports, unsigned int *nports)
 Create a new configuration using the given configuration as a template; ports and paths will be modified to select available ports on the local system. More...
 

Macro Definition Documentation

◆ GNUNET_TESTBED_PREFIX

#define GNUNET_TESTBED_PREFIX   "GNUNET_TESTBED_PREFIX"

Definition at line 9 of file gnunet_testbed_lib.h.

Function Documentation

◆ GNUNET_TESTBED_system_create_with_portrange()

struct GNUNET_TESTBED_System * GNUNET_TESTBED_system_create_with_portrange ( const char *  testdir,
const char *  trusted_ip,
const char *  hostname,
uint16_t  lowport,
uint16_t  highport 
)

Create a system handle.

There must only be one system handle per operating system.

Parameters
testdironly the directory name without any path. This is used for all service homes; the directory will be created in a temporary location depending on the underlying OS. This variable will be overridden with the value of the environmental variable GNUNET_TESTBED_PREFIX, if it exists.
trusted_ipthe ip address which will be set as TRUSTED HOST in all service configurations generated to allow control connections from this ip. This can either be a single ip address or a network address in CIDR notation.
hostnamethe hostname of the system we are using for testing; NULL for localhost
lowportlowest port number this system is allowed to allocate (inclusive)
highporthighest port number this system is allowed to allocate (exclusive)
Returns
handle to this system, NULL on error

Definition at line 105 of file testbed.c.

111{
112 struct GNUNET_TESTBED_System *system;
113
114 GNUNET_assert (NULL != testdir);
115 system = GNUNET_new (struct GNUNET_TESTBED_System);
116 if (NULL == (system->tmppath = getenv (GNUNET_TESTBED_PREFIX)))
117 system->tmppath = GNUNET_DISK_mkdtemp (testdir);
118 else
119 system->tmppath = GNUNET_strdup (system->tmppath);
120 system->lowport = lowport;
121 system->highport = highport;
122 if (NULL == system->tmppath)
123 {
124 GNUNET_free (system);
125 return NULL;
126 }
127 if (NULL != trusted_ip)
129 if (NULL != hostname)
130 system->hostname = GNUNET_strdup (hostname);
131 return system;
132}
char * getenv()
#define GNUNET_TESTBED_PREFIX
char * GNUNET_DISK_mkdtemp(const char *t)
Create an (empty) temporary directory on disk.
Definition: disk.c:340
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
Handle for a system on which GNUnet peers are executed; a system is used for reserving unique paths a...
Definition: testbed.c:54
uint16_t highport
Highest port we are allowed to use.
Definition: testbed.c:100
uint16_t lowport
Lowest port we are allowed to use.
Definition: testbed.c:95
char * hostname
our hostname
Definition: testbed.c:70
char * tmppath
Prefix (e.g.
Definition: testbed.c:59
char * trusted_ip
The trusted ip.
Definition: testbed.c:65

References getenv(), GNUNET_assert, GNUNET_DISK_mkdtemp(), GNUNET_free, GNUNET_new, GNUNET_strdup, GNUNET_TESTBED_PREFIX, GNUNET_TESTBED_System::highport, GNUNET_TESTBED_System::hostname, GNUNET_TESTBED_System::lowport, GNUNET_TESTBED_System::tmppath, and GNUNET_TESTBED_System::trusted_ip.

Referenced by GNUNET_TESTBED_system_create().

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

◆ GNUNET_TESTBED_system_create()

struct GNUNET_TESTBED_System * GNUNET_TESTBED_system_create ( const char *  testdir,
const char *  trusted_ip,
const char *  hostname 
)

Create a system handle.

There must only be one system handle per operating system. Uses a default range for allowed ports. Ports are still tested for availability.

Parameters
testdironly the directory name without any path. This is used for all service homes; the directory will be created in a temporary location depending on the underlying OS. This variable will be overridden with the value of the environmental variable GNUNET_TESTBED_PREFIX, if it exists.
trusted_ipthe ip address which will be set as TRUSTED HOST in all service configurations generated to allow control connections from this ip. This can either be a single ip address or a network address in CIDR notation.
hostnamethe hostname of the system we are using for testing; NULL for localhost
shared_servicesNULL terminated array describing services that are to be shared among peers
Returns
handle to this system, NULL on error

Definition at line 136 of file testbed.c.

140{
142 trusted_ip,
143 hostname,
144 LOW_PORT,
145 HIGH_PORT);
146}
struct GNUNET_TESTBED_System * GNUNET_TESTBED_system_create_with_portrange(const char *testdir, const char *trusted_ip, const char *hostname, uint16_t lowport, uint16_t highport)
Create a system handle.
Definition: testbed.c:105
#define HIGH_PORT
Highest port used for GNUnet testing.
Definition: testbed.c:46
#define LOW_PORT
Lowest port used for GNUnet testing.
Definition: testbed.c:39

References GNUNET_TESTBED_system_create_with_portrange(), HIGH_PORT, GNUNET_TESTBED_System::hostname, LOW_PORT, and GNUNET_TESTBED_System::trusted_ip.

Referenced by create_unique_cfgs(), and system_create_run().

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

◆ GNUNET_TESTBED_system_destroy()

void GNUNET_TESTBED_system_destroy ( struct GNUNET_TESTBED_System system,
bool  remove_paths 
)

Free system resources.

Parameters
systemsystem to be freed
remove_pathsshould the 'testdir' and all subdirectories be removed (clean up on shutdown)?

Definition at line 150 of file testbed.c.

152{
153 if (remove_paths)
155 GNUNET_free (system->tmppath);
156 GNUNET_free (system->trusted_ip);
157 GNUNET_free (system->hostname);
158 GNUNET_free (system);
159}
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_remove(const char *filename)
Remove all files in a directory (rm -rf).
Definition: disk.c:1088

References GNUNET_DISK_directory_remove(), GNUNET_free, GNUNET_TESTBED_System::hostname, GNUNET_TESTBED_System::tmppath, and GNUNET_TESTBED_System::trusted_ip.

Referenced by create_unique_cfgs(), and system_create_cleanup().

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

◆ GNUNET_TESTBED_reserve_port()

uint16_t GNUNET_TESTBED_reserve_port ( struct GNUNET_TESTBED_System system)

Reserve a TCP or UDP port for a peer.

Parameters
systemsystem to use for reservation tracking
Returns
0 if no free port was available

Definition at line 163 of file testbed.c.

164{
165 struct GNUNET_NETWORK_Handle *socket;
166 struct addrinfo hint;
167 struct addrinfo *ret;
168 struct addrinfo *ai;
169 uint32_t *port_buckets;
170 char *open_port_str;
171 int bind_status;
172 uint32_t xor_image;
173 uint16_t index;
174 uint16_t open_port;
175 uint16_t pos;
176
177 /*
178 FIXME: Instead of using getaddrinfo we should try to determine the port
179 status by the following heurestics.
180
181 On systems which support both IPv4 and IPv6, only ports open on both
182 address families are considered open.
183 On system with either IPv4 or IPv6. A port is considered open if it's
184 open in the respective address family
185 */hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
186 hint.ai_socktype = 0;
187 hint.ai_protocol = 0;
188 hint.ai_addrlen = 0;
189 hint.ai_addr = NULL;
190 hint.ai_canonname = NULL;
191 hint.ai_next = NULL;
192 hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
193 port_buckets = system->reserved_ports;
194 for (index = (system->lowport / 32) + 1; index < (system->highport / 32);
195 index++)
196 {
197 xor_image = (UINT32_MAX ^ port_buckets[index]);
198 if (0 == xor_image) /* Ports in the bucket are full */
199 continue;
200 pos = system->lowport % 32;
201 while (pos < 32)
202 {
203 if (0 == ((xor_image >> pos) & 1U))
204 {
205 pos++;
206 continue;
207 }
208 open_port = (index * 32) + pos;
209 if (open_port >= system->highport)
210 return 0;
211 GNUNET_asprintf (&open_port_str, "%u", (unsigned int) open_port);
212 ret = NULL;
213 GNUNET_assert (0 == getaddrinfo (NULL, open_port_str, &hint, &ret));
214 GNUNET_free (open_port_str);
215 bind_status = GNUNET_NO;
216 for (ai = ret; NULL != ai; ai = ai->ai_next)
217 {
218 socket = GNUNET_NETWORK_socket_create (ai->ai_family, SOCK_STREAM, 0);
219 if (NULL == socket)
220 continue;
221 bind_status =
222 GNUNET_NETWORK_socket_bind (socket, ai->ai_addr, ai->ai_addrlen);
224 if (GNUNET_OK != bind_status)
225 break;
226 socket = GNUNET_NETWORK_socket_create (ai->ai_family, SOCK_DGRAM, 0);
227 if (NULL == socket)
228 continue;
229 bind_status =
230 GNUNET_NETWORK_socket_bind (socket, ai->ai_addr, ai->ai_addrlen);
232 if (GNUNET_OK != bind_status)
233 break;
234 }
235 port_buckets[index] |= (1U << pos); /* Set the port bit */
236 freeaddrinfo (ret);
237 if (GNUNET_OK == bind_status)
238 {
240 "Found a free port %u\n",
241 (unsigned int) open_port);
242 return open_port;
243 }
244 pos++;
245 }
246 }
247 return 0;
248}
static int ret
Final status code.
Definition: gnunet-arm.c:93
static struct GNUNET_TRANSPORT_AddressIdentifier * ai
Handle to the operation that publishes our address.
@ GNUNET_OK
@ GNUNET_NO
@ GNUNET_ERROR_TYPE_DEBUG
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:508
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
Definition: network.c:833
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_bind(struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len)
Bind a socket to a particular address.
Definition: network.c:439
#define AI_NUMERICSERV
AI_NUMERICSERV not defined in windows.
Definition: platform.h:219
handle to a socket
Definition: network.c:53
uint32_t reserved_ports[65536/32]
Bitmap where each port that has already been reserved for some GNUnet peer is recorded.
Definition: testbed.c:80
#define LOG(kind,...)
Definition: testbed.c:31

References ai, AI_NUMERICSERV, GNUNET_asprintf(), GNUNET_assert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_NETWORK_socket_bind(), GNUNET_NETWORK_socket_close(), GNUNET_NETWORK_socket_create(), GNUNET_NO, GNUNET_OK, GNUNET_TESTBED_System::highport, LOG, GNUNET_TESTBED_System::lowport, GNUNET_TESTBED_System::reserved_ports, and ret.

Referenced by update_config().

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

◆ GNUNET_TESTBED_release_port()

void GNUNET_TESTBED_release_port ( struct GNUNET_TESTBED_System system,
uint16_t  port 
)

Release reservation of a TCP or UDP port for a peer (used during #GNUNET_TESTBED_peer_destroy()).

Parameters
systemsystem to use for reservation tracking
portreserved port to release

Definition at line 252 of file testbed.c.

254{
255 uint32_t *port_buckets;
256 uint16_t bucket;
257 uint16_t pos;
258
259 port_buckets = system->reserved_ports;
260 bucket = port / 32;
261 pos = port % 32;
262 LOG (GNUNET_ERROR_TYPE_DEBUG, "Releasing port %u\n", port);
263 if (0 == (port_buckets[bucket] & (1U << pos)))
264 {
265 GNUNET_break (0); /* Port was not reserved by us using reserve_port() */
266 return;
267 }
268 port_buckets[bucket] &= ~(1U << pos);
269}
static uint16_t port
Port number.
Definition: gnunet-bcd.c:147
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.

References GNUNET_break, GNUNET_ERROR_TYPE_DEBUG, LOG, port, and GNUNET_TESTBED_System::reserved_ports.

◆ GNUNET_TESTBED_configuration_create()

enum GNUNET_GenericReturnValue GNUNET_TESTBED_configuration_create ( struct GNUNET_TESTBED_System system,
struct GNUNET_CONFIGURATION_Handle cfg,
uint16_t **  ports,
unsigned int *  nports 
)

Create a new configuration using the given configuration as a template; ports and paths will be modified to select available ports on the local system.

The default configuration will be available in PATHS section under the option DEFAULTCONFIG after the call. GNUNET_HOME is also set in PATHS section to the temporary directory specific to this configuration. If we run out of "*port" numbers, return GNUNET_SYSERR.

This is primarily a helper function used internally by 'GNUNET_TESTBED_peer_configure'.

Parameters
systemsystem to use to coordinate resource usage
cfgtemplate configuration to update
portsarray with port numbers used in the created configuration. Will be updated upon successful return. Can be NULL
nportsthe size of the ‘ports’ array. Will be updated.
Returns
GNUNET_OK on success, GNUNET_SYSERR on error - the configuration will be incomplete and should not be used there upon

Definition at line 532 of file testbed.c.

537{
538 struct UpdateContext uc;
539 char *default_config;
540
541 uc.system = system;
542 uc.cfg = cfg;
543 uc.status = GNUNET_OK;
544 uc.ports = NULL;
545 uc.nports = 0;
546 GNUNET_asprintf (&uc.gnunet_home,
547 "%s/%u",
550 GNUNET_asprintf (&default_config, "%s/config", uc.gnunet_home);
552 "PATHS",
553 "DEFAULTCONFIG",
554 default_config);
555 GNUNET_CONFIGURATION_set_value_string (cfg, "arm", "CONFIG", default_config);
556 GNUNET_free (default_config);
558 "PATHS",
559 "GNUNET_HOME",
560 uc.gnunet_home);
561 /* make PORTs and UNIXPATHs unique */
563 /* allow connections to services from system trusted_ip host */
565 /* enable loopback-based connections between peers */
566 GNUNET_CONFIGURATION_set_value_string (cfg, "nat", "USE_LOCALADDR", "YES");
567 GNUNET_free (uc.gnunet_home);
568 if ((NULL != ports) && (NULL != nports))
569 {
570 *ports = uc.ports;
571 *nports = uc.nports;
572 }
573 else
574 GNUNET_free (uc.ports);
575 return uc.status;
576}
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:108
static struct GNUNET_FS_UnindexContext * uc
void GNUNET_CONFIGURATION_set_value_string(struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, const char *value)
Set a configuration value that should be a string.
void GNUNET_CONFIGURATION_iterate_sections(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_CONFIGURATION_SectionIterator iter, void *iter_cls)
Iterate over all sections in the configuration.
void GNUNET_CONFIGURATION_iterate(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_CONFIGURATION_Iterator iter, void *iter_cls)
Iterate over all options in the configuration.
uint32_t path_counter
Counter we use to make service home paths unique on this system; the full path consists of the tmppat...
Definition: testbed.c:90
Closure for iterator for updating.
struct GNUNET_TESTBED_System * system
The system for which we are building configurations.
Definition: testbed.c:281
uint16_t * ports
Array of ports currently allocated to this peer.
Definition: testbed.c:298
unsigned int nports
The number of ports in the above array.
Definition: testbed.c:303
static void update_config(void *cls, const char *section, const char *option, const char *value)
Function to iterate over options.
Definition: testbed.c:323
static void update_config_sections(void *cls, const char *section)
Section iterator to set ACCEPT_FROM/ACCEPT_FROM6 to include the address of 'trusted_hosts' in all sec...
Definition: testbed.c:420

References cfg, GNUNET_asprintf(), GNUNET_CONFIGURATION_iterate(), GNUNET_CONFIGURATION_iterate_sections(), GNUNET_CONFIGURATION_set_value_string(), GNUNET_free, GNUNET_OK, UpdateContext::nports, GNUNET_TESTBED_System::path_counter, UpdateContext::ports, UpdateContext::system, GNUNET_TESTBED_System::tmppath, uc, update_config(), and update_config_sections().

Referenced by create_unique_cfgs(), and start_peer_run().

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