aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSree Harsha Totakura <totakura@in.tum.de>2012-05-25 14:40:31 +0000
committerSree Harsha Totakura <totakura@in.tum.de>2012-05-25 14:40:31 +0000
commit4555944c8cb5dd2e9f49251d764f20a46068edb9 (patch)
tree9a21a12299eaf0b96d4b4a1888a0de9717e1c78e /src
parent2defa2cf4f689a69c503b2cc4ad0761d5ebc3c69 (diff)
downloadgnunet-4555944c8cb5dd2e9f49251d764f20a46068edb9.tar.gz
gnunet-4555944c8cb5dd2e9f49251d764f20a46068edb9.zip
testing port reservation
Diffstat (limited to 'src')
-rw-r--r--src/testing/testing_new.c90
1 files changed, 57 insertions, 33 deletions
diff --git a/src/testing/testing_new.c b/src/testing/testing_new.c
index 329f58c9d..e6c4cb56f 100644
--- a/src/testing/testing_new.c
+++ b/src/testing/testing_new.c
@@ -194,39 +194,63 @@ uint16_t
194reserve_port (struct GNUNET_TESTING_System *system, 194reserve_port (struct GNUNET_TESTING_System *system,
195 int is_tcp) 195 int is_tcp)
196{ 196{
197 /* struct GNUNET_NETWORK_Handle *socket; */ 197 struct GNUNET_NETWORK_Handle *socket;
198 /* struct sockaddr_in addr; */ 198 struct addrinfo hint;
199 /* uint32_t *port_buckets; */ 199 struct addrinfo *ret;
200 /* uint32_t xor_image; */ 200 uint32_t *port_buckets;
201 /* uint16_t index; */ 201 char *open_port_str;
202 /* uint16_t buckets; */ 202 uint32_t xor_image;
203 203 uint16_t index;
204 /* if (GNUNET_YES == is_tcp) */ 204 uint16_t open_port;
205 /* { */ 205 uint8_t pos;
206 /* socket = GNUNET_NETWORK_socket_create (AF_INET, */ 206
207 /* SOCKET_STREAM, */ 207 if (GNUNET_YES == is_tcp)
208 /* 0); */ 208 {
209 /* port_buckets = system->reserved_tcp_ports; */ 209 socket = GNUNET_NETWORK_socket_create (AF_UNSPEC,
210 /* } */ 210 SOCK_STREAM,
211 /* else */ 211 0);
212 /* { */ 212 port_buckets = system->reserved_tcp_ports;
213 /* socket = GNUNET_NETWORK_socket_create (AF_INET, */ 213 }
214 /* SOCKET_DGRAM, */ 214 else
215 /* 0); */ 215 {
216 /* port_buckets = system->reserved_udp_ports; */ 216 socket = GNUNET_NETWORK_socket_create (AF_UNSPEC,
217 /* } */ 217 SOCK_DGRAM,
218 /* buckets = 65536 / 32; */ 218 0);
219 /* for (index = (LOW_PORT / 32) + 1; index < (HIGH_PORT / 32); index++) */ 219 port_buckets = system->reserved_udp_ports;
220 /* { */ 220 }
221 /* xor_image = ((uint32_t) 0xffffffff) ^ port_buckets[index]; */ 221 for (index = (LOW_PORT / 32) + 1; index < (HIGH_PORT / 32); index++)
222 /* if (0 == xor_image) /\* Ports in the bucket are full *\/ */ 222 {
223 /* continue; */ 223 xor_image = (UINT32_MAX ^ port_buckets[index]);
224 224 if (0 == xor_image) /* Ports in the bucket are full */
225 /* } */ 225 continue;
226 /* addr.sin_family = AF_INET; */ 226
227 /* addr.sin_port = ??; */ 227 pos = 0;
228 228 while (pos < 32)
229 GNUNET_break (0); 229 {
230 if (0 == ((xor_image >> pos) & 1U))
231 break;
232 open_port = (index * 32) + pos;
233 GNUNET_asprintf (&open_port_str, "%u", open_port);
234 hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
235 hint.ai_socktype = (GNUNET_YES == is_tcp)? SOCK_STREAM : SOCK_DGRAM;
236 hint.ai_protocol = 0;
237 hint.ai_addrlen = 0;
238 hint.ai_addr = NULL;
239 hint.ai_canonname = NULL;
240 hint.ai_next = NULL;
241 hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
242 ret = NULL;
243 GNUNET_assert (0 != getaddrinfo (NULL, open_port_str, &hint, &ret));
244 GNUNET_free (open_port_str);
245 if (GNUNET_OK == GNUNET_NETWORK_socket_bind (socket, ret->ai_addr, ret->ai_addrlen))
246 {
247 return open_port;
248 }
249 /* This port is in use by some other application */
250 port_buckets[index] |= (1U << pos);
251 pos++;
252 }
253 }
230 return 0; 254 return 0;
231} 255}
232 256