aboutsummaryrefslogtreecommitdiff
path: root/src/util/network.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/network.c')
-rw-r--r--src/util/network.c932
1 files changed, 473 insertions, 459 deletions
diff --git a/src/util/network.c b/src/util/network.c
index 6f8a8ff5a..ce4f2f896 100644
--- a/src/util/network.c
+++ b/src/util/network.c
@@ -28,9 +28,15 @@
28#include "gnunet_util_lib.h" 28#include "gnunet_util_lib.h"
29#include "disk.h" 29#include "disk.h"
30 30
31#define LOG(kind, ...) GNUNET_log_from(kind, "util-network", __VA_ARGS__) 31#define LOG(kind, ...) GNUNET_log_from (kind, "util-network", __VA_ARGS__)
32#define LOG_STRERROR_FILE(kind, syscall, filename) GNUNET_log_from_strerror_file(kind, "util-network", syscall, filename) 32#define LOG_STRERROR_FILE(kind, syscall, \
33#define LOG_STRERROR(kind, syscall) GNUNET_log_from_strerror(kind, "util-network", syscall) 33 filename) GNUNET_log_from_strerror_file (kind, \
34 "util-network", \
35 syscall, \
36 filename)
37#define LOG_STRERROR(kind, syscall) GNUNET_log_from_strerror (kind, \
38 "util-network", \
39 syscall)
34 40
35#define DEBUG_NETWORK GNUNET_EXTRA_LOGGING 41#define DEBUG_NETWORK GNUNET_EXTRA_LOGGING
36 42
@@ -43,7 +49,8 @@
43/** 49/**
44 * @brief handle to a socket 50 * @brief handle to a socket
45 */ 51 */
46struct GNUNET_NETWORK_Handle { 52struct GNUNET_NETWORK_Handle
53{
47 int fd; 54 int fd;
48 55
49 /** 56 /**
@@ -75,7 +82,7 @@ struct GNUNET_NETWORK_Handle {
75 * @return #GNUNET_OK if the PF is supported 82 * @return #GNUNET_OK if the PF is supported
76 */ 83 */
77int 84int
78GNUNET_NETWORK_test_pf(int pf) 85GNUNET_NETWORK_test_pf (int pf)
79{ 86{
80 static int cache_v4 = -1; 87 static int cache_v4 = -1;
81 static int cache_v6 = -1; 88 static int cache_v6 = -1;
@@ -84,56 +91,56 @@ GNUNET_NETWORK_test_pf(int pf)
84 int ret; 91 int ret;
85 92
86 switch (pf) 93 switch (pf)
87 { 94 {
88 case PF_INET: 95 case PF_INET:
89 if (-1 != cache_v4) 96 if (-1 != cache_v4)
90 return cache_v4; 97 return cache_v4;
91 break; 98 break;
92 99
93 case PF_INET6: 100 case PF_INET6:
94 if (-1 != cache_v6) 101 if (-1 != cache_v6)
95 return cache_v6; 102 return cache_v6;
96 break; 103 break;
97 104
98#ifdef PF_UNIX 105#ifdef PF_UNIX
99 case PF_UNIX: 106 case PF_UNIX:
100 if (-1 != cache_un) 107 if (-1 != cache_un)
101 return cache_un; 108 return cache_un;
102 break; 109 break;
103#endif 110#endif
104 } 111 }
105 s = socket(pf, SOCK_STREAM, 0); 112 s = socket (pf, SOCK_STREAM, 0);
106 if (-1 == s) 113 if (-1 == s)
114 {
115 if (EAFNOSUPPORT != errno)
107 { 116 {
108 if (EAFNOSUPPORT != errno) 117 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
109 { 118 "socket");
110 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, 119 return GNUNET_SYSERR;
111 "socket");
112 return GNUNET_SYSERR;
113 }
114 ret = GNUNET_NO;
115 } 120 }
121 ret = GNUNET_NO;
122 }
116 else 123 else
117 { 124 {
118 close(s); 125 close (s);
119 ret = GNUNET_OK; 126 ret = GNUNET_OK;
120 } 127 }
121 switch (pf) 128 switch (pf)
122 { 129 {
123 case PF_INET: 130 case PF_INET:
124 cache_v4 = ret; 131 cache_v4 = ret;
125 break; 132 break;
126 133
127 case PF_INET6: 134 case PF_INET6:
128 cache_v6 = ret; 135 cache_v6 = ret;
129 break; 136 break;
130 137
131#ifdef PF_UNIX 138#ifdef PF_UNIX
132 case PF_UNIX: 139 case PF_UNIX:
133 cache_un = ret; 140 cache_un = ret;
134 break; 141 break;
135#endif 142#endif
136 } 143 }
137 return ret; 144 return ret;
138} 145}
139 146
@@ -148,7 +155,7 @@ GNUNET_NETWORK_test_pf(int pf)
148 * @return shortened unixpath, NULL on error 155 * @return shortened unixpath, NULL on error
149 */ 156 */
150char * 157char *
151GNUNET_NETWORK_shorten_unixpath(char *unixpath) 158GNUNET_NETWORK_shorten_unixpath (char *unixpath)
152{ 159{
153 struct sockaddr_un dummy; 160 struct sockaddr_un dummy;
154 size_t slen; 161 size_t slen;
@@ -158,25 +165,26 @@ GNUNET_NETWORK_shorten_unixpath(char *unixpath)
158 size_t upm; 165 size_t upm;
159 166
160 upm = sizeof(dummy.sun_path); 167 upm = sizeof(dummy.sun_path);
161 slen = strlen(unixpath); 168 slen = strlen (unixpath);
162 if (slen < upm) 169 if (slen < upm)
163 return unixpath; /* no shortening required */ 170 return unixpath; /* no shortening required */
164 GNUNET_CRYPTO_hash(unixpath, slen, &sh); 171 GNUNET_CRYPTO_hash (unixpath, slen, &sh);
165 while (16 + strlen(unixpath) >= upm) 172 while (16 + strlen (unixpath) >= upm)
173 {
174 if (NULL == (end = strrchr (unixpath, '/')))
166 { 175 {
167 if (NULL == (end = strrchr(unixpath, '/'))) 176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
168 { 177 _ (
169 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 178 "Unable to shorten unix path `%s' while keeping name unique\n"),
170 _("Unable to shorten unix path `%s' while keeping name unique\n"), 179 unixpath);
171 unixpath); 180 GNUNET_free (unixpath);
172 GNUNET_free(unixpath); 181 return NULL;
173 return NULL;
174 }
175 *end = '\0';
176 } 182 }
177 GNUNET_CRYPTO_hash_to_enc(&sh, &ae); 183 *end = '\0';
184 }
185 GNUNET_CRYPTO_hash_to_enc (&sh, &ae);
178 ae.encoding[16] = '\0'; 186 ae.encoding[16] = '\0';
179 strcat(unixpath, (char *)ae.encoding); 187 strcat (unixpath, (char *) ae.encoding);
180 return unixpath; 188 return unixpath;
181} 189}
182 190
@@ -191,48 +199,48 @@ GNUNET_NETWORK_shorten_unixpath(char *unixpath)
191 * @param un unix domain socket address to check 199 * @param un unix domain socket address to check
192 */ 200 */
193void 201void
194GNUNET_NETWORK_unix_precheck(const struct sockaddr_un *un) 202GNUNET_NETWORK_unix_precheck (const struct sockaddr_un *un)
195{ 203{
196 int s; 204 int s;
197 int eno; 205 int eno;
198 struct stat sbuf; 206 struct stat sbuf;
199 int ret; 207 int ret;
200 208
201 s = socket(AF_UNIX, SOCK_STREAM, 0); 209 s = socket (AF_UNIX, SOCK_STREAM, 0);
202 if (-1 == s) 210 if (-1 == s)
203 { 211 {
204 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, 212 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
205 "Failed to open AF_UNIX socket"); 213 "Failed to open AF_UNIX socket");
206 return; 214 return;
207 } 215 }
208 ret = connect(s, 216 ret = connect (s,
209 (struct sockaddr *)un, 217 (struct sockaddr *) un,
210 sizeof(struct sockaddr_un)); 218 sizeof(struct sockaddr_un));
211 eno = errno; 219 eno = errno;
212 GNUNET_break(0 == close(s)); 220 GNUNET_break (0 == close (s));
213 if (0 == ret) 221 if (0 == ret)
214 return; /* another process is listening, do not remove! */ 222 return; /* another process is listening, do not remove! */
215 if (ECONNREFUSED != eno) 223 if (ECONNREFUSED != eno)
216 return; /* some other error, likely "no such file or directory" -- all well */ 224 return; /* some other error, likely "no such file or directory" -- all well */
217 /* should unlink, but sanity checks first */ 225 /* should unlink, but sanity checks first */
218 if (0 != stat(un->sun_path, 226 if (0 != stat (un->sun_path,
219 &sbuf)) 227 &sbuf))
220 return; /* failed to 'stat', likely does not exist after all */ 228 return; /* failed to 'stat', likely does not exist after all */
221 if (S_IFSOCK != (S_IFMT & sbuf.st_mode)) 229 if (S_IFSOCK != (S_IFMT & sbuf.st_mode))
222 return; /* refuse to unlink anything except sockets */ 230 return; /* refuse to unlink anything except sockets */
223 /* finally, really unlink */ 231 /* finally, really unlink */
224 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 232 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
225 "Removing left-over `%s' from previous exeuction\n", 233 "Removing left-over `%s' from previous exeuction\n",
226 un->sun_path); 234 un->sun_path);
227 if (0 != unlink(un->sun_path)) 235 if (0 != unlink (un->sun_path))
228 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, 236 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
229 "unlink", 237 "unlink",
230 un->sun_path); 238 un->sun_path);
231} 239}
232 240
233 241
234#ifndef FD_COPY 242#ifndef FD_COPY
235#define FD_COPY(s, d) do { GNUNET_memcpy((d), (s), sizeof(fd_set)); } while (0) 243#define FD_COPY(s, d) do { GNUNET_memcpy ((d), (s), sizeof(fd_set)); } while (0)
236#endif 244#endif
237 245
238 246
@@ -244,31 +252,31 @@ GNUNET_NETWORK_unix_precheck(const struct sockaddr_un *un)
244 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 252 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
245 */ 253 */
246int 254int
247GNUNET_NETWORK_socket_set_blocking(struct GNUNET_NETWORK_Handle *fd, 255GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
248 int doBlock) 256 int doBlock)
249{ 257{
250 int flags = fcntl(fd->fd, F_GETFL); 258 int flags = fcntl (fd->fd, F_GETFL);
251 259
252 if (flags == -1) 260 if (flags == -1)
253 { 261 {
254 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, 262 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
255 "fcntl"); 263 "fcntl");
256 return GNUNET_SYSERR; 264 return GNUNET_SYSERR;
257 } 265 }
258 if (doBlock) 266 if (doBlock)
259 flags &= ~O_NONBLOCK; 267 flags &= ~O_NONBLOCK;
260 268
261 else 269 else
262 flags |= O_NONBLOCK; 270 flags |= O_NONBLOCK;
263 if (0 != fcntl(fd->fd, 271 if (0 != fcntl (fd->fd,
264 F_SETFL, 272 F_SETFL,
265 flags)) 273 flags))
266 274
267 { 275 {
268 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, 276 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
269 "fcntl"); 277 "fcntl");
270 return GNUNET_SYSERR; 278 return GNUNET_SYSERR;
271 } 279 }
272 return GNUNET_OK; 280 return GNUNET_OK;
273} 281}
274 282
@@ -281,16 +289,16 @@ GNUNET_NETWORK_socket_set_blocking(struct GNUNET_NETWORK_Handle *fd,
281 * @warning Not implemented on Windows 289 * @warning Not implemented on Windows
282 */ 290 */
283static int 291static int
284socket_set_inheritable(const struct GNUNET_NETWORK_Handle *h) 292socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
285{ 293{
286 int i; 294 int i;
287 i = fcntl(h->fd, F_GETFD); 295 i = fcntl (h->fd, F_GETFD);
288 if (i < 0) 296 if (i < 0)
289 return GNUNET_SYSERR; 297 return GNUNET_SYSERR;
290 if (i == (i | FD_CLOEXEC)) 298 if (i == (i | FD_CLOEXEC))
291 return GNUNET_OK; 299 return GNUNET_OK;
292 i |= FD_CLOEXEC; 300 i |= FD_CLOEXEC;
293 if (fcntl(h->fd, F_SETFD, i) < 0) 301 if (fcntl (h->fd, F_SETFD, i) < 0)
294 return GNUNET_SYSERR; 302 return GNUNET_SYSERR;
295 303
296 return GNUNET_OK; 304 return GNUNET_OK;
@@ -304,14 +312,14 @@ socket_set_inheritable(const struct GNUNET_NETWORK_Handle *h)
304 * @param h the socket to make non-delaying 312 * @param h the socket to make non-delaying
305 */ 313 */
306static int 314static int
307socket_set_nosigpipe(const struct GNUNET_NETWORK_Handle *h) 315socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
308{ 316{
309 int abs_value = 1; 317 int abs_value = 1;
310 318
311 if (0 != 319 if (0 !=
312 setsockopt(h->fd, SOL_SOCKET, SO_NOSIGPIPE, 320 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE,
313 (const void *)&abs_value, 321 (const void *) &abs_value,
314 sizeof(abs_value))) 322 sizeof(abs_value)))
315 return GNUNET_SYSERR; 323 return GNUNET_SYSERR;
316 return GNUNET_OK; 324 return GNUNET_OK;
317} 325}
@@ -326,17 +334,17 @@ socket_set_nosigpipe(const struct GNUNET_NETWORK_Handle *h)
326 * @param h the socket to make non-delaying 334 * @param h the socket to make non-delaying
327 */ 335 */
328static void 336static void
329socket_set_nodelay(const struct GNUNET_NETWORK_Handle *h) 337socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
330{ 338{
331 int value = 1; 339 int value = 1;
332 340
333 if (0 != 341 if (0 !=
334 setsockopt(h->fd, 342 setsockopt (h->fd,
335 IPPROTO_TCP, 343 IPPROTO_TCP,
336 TCP_NODELAY, 344 TCP_NODELAY,
337 &value, sizeof(value))) 345 &value, sizeof(value)))
338 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, 346 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
339 "setsockopt"); 347 "setsockopt");
340} 348}
341 349
342 350
@@ -353,57 +361,57 @@ socket_set_nodelay(const struct GNUNET_NETWORK_Handle *h)
353 * failed and the handle was destroyed 361 * failed and the handle was destroyed
354 */ 362 */
355static int 363static int
356initialize_network_handle(struct GNUNET_NETWORK_Handle *h, 364initialize_network_handle (struct GNUNET_NETWORK_Handle *h,
357 int af, 365 int af,
358 int type) 366 int type)
359{ 367{
360 int eno; 368 int eno;
361 369
362 h->af = af; 370 h->af = af;
363 h->type = type; 371 h->type = type;
364 if (h->fd == INVALID_SOCKET) 372 if (h->fd == INVALID_SOCKET)
365 { 373 {
366 eno = errno; 374 eno = errno;
367 GNUNET_free(h); 375 GNUNET_free (h);
368 errno = eno; 376 errno = eno;
369 return GNUNET_SYSERR; 377 return GNUNET_SYSERR;
370 } 378 }
371 379
372 if (h->fd >= FD_SETSIZE) 380 if (h->fd >= FD_SETSIZE)
373 { 381 {
374 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close(h)); 382 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
375 errno = EMFILE; 383 errno = EMFILE;
376 return GNUNET_SYSERR; 384 return GNUNET_SYSERR;
377 } 385 }
378 386
379 if (GNUNET_OK != socket_set_inheritable(h)) 387 if (GNUNET_OK != socket_set_inheritable (h))
380 LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 388 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
381 "socket_set_inheritable"); 389 "socket_set_inheritable");
382 390
383 if (GNUNET_SYSERR == GNUNET_NETWORK_socket_set_blocking(h, GNUNET_NO)) 391 if (GNUNET_SYSERR == GNUNET_NETWORK_socket_set_blocking (h, GNUNET_NO))
384 { 392 {
385 eno = errno; 393 eno = errno;
386 GNUNET_break(0); 394 GNUNET_break (0);
387 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close(h)); 395 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
388 errno = eno; 396 errno = eno;
389 return GNUNET_SYSERR; 397 return GNUNET_SYSERR;
390 } 398 }
391#ifdef DARWIN 399#ifdef DARWIN
392 if (GNUNET_SYSERR == socket_set_nosigpipe(h)) 400 if (GNUNET_SYSERR == socket_set_nosigpipe (h))
393 { 401 {
394 eno = errno; 402 eno = errno;
395 GNUNET_break(0); 403 GNUNET_break (0);
396 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close(h)); 404 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h));
397 errno = eno; 405 errno = eno;
398 return GNUNET_SYSERR; 406 return GNUNET_SYSERR;
399 } 407 }
400#endif 408#endif
401 if ((type == SOCK_STREAM) 409 if ((type == SOCK_STREAM)
402#ifdef AF_UNIX 410#ifdef AF_UNIX
403 && (af != AF_UNIX) 411 && (af != AF_UNIX)
404#endif 412#endif
405 ) 413 )
406 socket_set_nodelay(h); 414 socket_set_nodelay (h);
407 return GNUNET_OK; 415 return GNUNET_OK;
408} 416}
409 417
@@ -417,47 +425,48 @@ initialize_network_handle(struct GNUNET_NETWORK_Handle *h,
417 * @return client socket 425 * @return client socket
418 */ 426 */
419struct GNUNET_NETWORK_Handle * 427struct GNUNET_NETWORK_Handle *
420GNUNET_NETWORK_socket_accept(const struct GNUNET_NETWORK_Handle *desc, 428GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
421 struct sockaddr *address, 429 struct sockaddr *address,
422 socklen_t *address_len) 430 socklen_t *address_len)
423{ 431{
424 struct GNUNET_NETWORK_Handle *ret; 432 struct GNUNET_NETWORK_Handle *ret;
425 int eno; 433 int eno;
426 434
427 ret = GNUNET_new(struct GNUNET_NETWORK_Handle); 435 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
428#if DEBUG_NETWORK 436#if DEBUG_NETWORK
429 { 437 {
430 struct sockaddr_storage name; 438 struct sockaddr_storage name;
431 socklen_t namelen = sizeof(name); 439 socklen_t namelen = sizeof(name);
432 440
433 int gsn = getsockname(desc->fd, 441 int gsn = getsockname (desc->fd,
434 (struct sockaddr *)&name, 442 (struct sockaddr *) &name,
435 &namelen); 443 &namelen);
436 444
437 if (0 == gsn) 445 if (0 == gsn)
438 LOG(GNUNET_ERROR_TYPE_DEBUG, 446 LOG (GNUNET_ERROR_TYPE_DEBUG,
439 "Accepting connection on `%s'\n", 447 "Accepting connection on `%s'\n",
440 GNUNET_a2s((const struct sockaddr *)&name, 448 GNUNET_a2s ((const struct sockaddr *) &name,
441 namelen)); 449 namelen));
442 } 450 }
443#endif 451#endif
444 ret->fd = accept(desc->fd, 452 ret->fd = accept (desc->fd,
445 address, 453 address,
446 address_len); 454 address_len);
447 if (-1 == ret->fd) 455 if (-1 == ret->fd)
448 { 456 {
449 eno = errno; 457 eno = errno;
450 GNUNET_free(ret); 458 GNUNET_free (ret);
451 errno = eno; 459 errno = eno;
452 return NULL; 460 return NULL;
453 } 461 }
454 if (GNUNET_OK != 462 if (GNUNET_OK !=
455 initialize_network_handle(ret, 463 initialize_network_handle (ret,
456 (NULL != address) ? address->sa_family : desc->af, 464 (NULL != address) ? address->sa_family :
457 SOCK_STREAM)) 465 desc->af,
458 { 466 SOCK_STREAM))
459 return NULL; 467 {
460 } 468 return NULL;
469 }
461 return ret; 470 return ret;
462} 471}
463 472
@@ -471,9 +480,9 @@ GNUNET_NETWORK_socket_accept(const struct GNUNET_NETWORK_Handle *desc,
471 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 480 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
472 */ 481 */
473int 482int
474GNUNET_NETWORK_socket_bind(struct GNUNET_NETWORK_Handle *desc, 483GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
475 const struct sockaddr *address, 484 const struct sockaddr *address,
476 socklen_t address_len) 485 socklen_t address_len)
477{ 486{
478 int ret; 487 int ret;
479 488
@@ -483,55 +492,56 @@ GNUNET_NETWORK_socket_bind(struct GNUNET_NETWORK_Handle *desc,
483 const int on = 1; 492 const int on = 1;
484 493
485 if (AF_INET6 == desc->af) 494 if (AF_INET6 == desc->af)
486 if (setsockopt(desc->fd, 495 if (setsockopt (desc->fd,
487 IPPROTO_IPV6, 496 IPPROTO_IPV6,
488 IPV6_V6ONLY, 497 IPV6_V6ONLY,
489 (const void *)&on, 498 (const void *) &on,
490 sizeof(on))) 499 sizeof(on)))
491 LOG_STRERROR(GNUNET_ERROR_TYPE_DEBUG, 500 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
492 "setsockopt"); 501 "setsockopt");
493 } 502 }
494#endif 503#endif
495#endif 504#endif
496 if (AF_UNIX == address->sa_family) 505 if (AF_UNIX == address->sa_family)
497 GNUNET_NETWORK_unix_precheck((const struct sockaddr_un *)address); 506 GNUNET_NETWORK_unix_precheck ((const struct sockaddr_un *) address);
498 { 507 {
499 const int on = 1; 508 const int on = 1;
500 509
501 /* This is required here for TCP sockets, but only on UNIX */ 510 /* This is required here for TCP sockets, but only on UNIX */
502 if ((SOCK_STREAM == desc->type) && 511 if ((SOCK_STREAM == desc->type) &&
503 (0 != setsockopt(desc->fd, 512 (0 != setsockopt (desc->fd,
504 SOL_SOCKET, 513 SOL_SOCKET,
505 SO_REUSEADDR, 514 SO_REUSEADDR,
506 &on, sizeof(on)))) 515 &on, sizeof(on))))
507 LOG_STRERROR(GNUNET_ERROR_TYPE_DEBUG, 516 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
508 "setsockopt"); 517 "setsockopt");
509 } 518 }
510 { 519 {
511 /* set permissions of newly created non-abstract UNIX domain socket to 520 /* set permissions of newly created non-abstract UNIX domain socket to
512 "user-only"; applications can choose to relax this later */ 521 "user-only"; applications can choose to relax this later */
513 mode_t old_mask = 0; /* assigned to make compiler happy */ 522 mode_t old_mask = 0; /* assigned to make compiler happy */
514 const struct sockaddr_un *un = (const struct sockaddr_un *)address; 523 const struct sockaddr_un *un = (const struct sockaddr_un *) address;
515 int not_abstract = 0; 524 int not_abstract = 0;
516 525
517 if ((AF_UNIX == address->sa_family) 526 if ((AF_UNIX == address->sa_family)
518 && ('\0' != un->sun_path[0])) /* Not an abstract socket */ 527 && ('\0' != un->sun_path[0])) /* Not an abstract socket */
519 not_abstract = 1; 528 not_abstract = 1;
520 if (not_abstract) 529 if (not_abstract)
521 old_mask = umask(S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH | S_IXOTH); 530 old_mask = umask (S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH
531 | S_IXOTH);
522 532
523 ret = bind(desc->fd, 533 ret = bind (desc->fd,
524 address, 534 address,
525 address_len); 535 address_len);
526 536
527 if (not_abstract) 537 if (not_abstract)
528 (void)umask(old_mask); 538 (void) umask (old_mask);
529} 539 }
530 if (0 != ret) 540 if (0 != ret)
531 return GNUNET_SYSERR; 541 return GNUNET_SYSERR;
532 542
533 desc->addr = GNUNET_malloc(address_len); 543 desc->addr = GNUNET_malloc (address_len);
534 GNUNET_memcpy(desc->addr, address, address_len); 544 GNUNET_memcpy (desc->addr, address, address_len);
535 desc->addrlen = address_len; 545 desc->addrlen = address_len;
536 546
537 return GNUNET_OK; 547 return GNUNET_OK;
@@ -545,58 +555,58 @@ GNUNET_NETWORK_socket_bind(struct GNUNET_NETWORK_Handle *desc,
545 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 555 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
546 */ 556 */
547int 557int
548GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc) 558GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
549{ 559{
550 int ret; 560 int ret;
551 561
552 ret = close(desc->fd); 562 ret = close (desc->fd);
553 563
554 const struct sockaddr_un *un = (const struct sockaddr_un *)desc->addr; 564 const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr;
555 565
556 /* Cleanup the UNIX domain socket and its parent directories in case of non 566 /* Cleanup the UNIX domain socket and its parent directories in case of non
557 abstract sockets */ 567 abstract sockets */
558 if ((AF_UNIX == desc->af) && 568 if ((AF_UNIX == desc->af) &&
559 (NULL != desc->addr) && 569 (NULL != desc->addr) &&
560 ('\0' != un->sun_path[0])) 570 ('\0' != un->sun_path[0]))
561 { 571 {
562 char *dirname = GNUNET_strndup(un->sun_path, 572 char *dirname = GNUNET_strndup (un->sun_path,
563 sizeof(un->sun_path)); 573 sizeof(un->sun_path));
564 574
565 if (0 != unlink(dirname)) 575 if (0 != unlink (dirname))
566 { 576 {
567 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, 577 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
568 "unlink", 578 "unlink",
569 dirname); 579 dirname);
570 } 580 }
571 else 581 else
582 {
583 size_t len;
584
585 len = strlen (dirname);
586 while ((len > 0) && (dirname[len] != DIR_SEPARATOR))
587 len--;
588 dirname[len] = '\0';
589 if ((0 != len) && (0 != rmdir (dirname)))
590 {
591 switch (errno)
572 { 592 {
573 size_t len; 593 case EACCES:
574 594 case ENOTEMPTY:
575 len = strlen(dirname); 595 case EPERM:
576 while ((len > 0) && (dirname[len] != DIR_SEPARATOR)) 596 /* these are normal and can just be ignored */
577 len--; 597 break;
578 dirname[len] = '\0'; 598
579 if ((0 != len) && (0 != rmdir(dirname))) 599 default:
580 { 600 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
581 switch (errno) 601 "rmdir",
582 { 602 dirname);
583 case EACCES: 603 break;
584 case ENOTEMPTY:
585 case EPERM:
586 /* these are normal and can just be ignored */
587 break;
588
589 default:
590 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING,
591 "rmdir",
592 dirname);
593 break;
594 }
595 }
596 } 604 }
597 GNUNET_free(dirname); 605 }
598 } 606 }
599 GNUNET_NETWORK_socket_free_memory_only_(desc); 607 GNUNET_free (dirname);
608 }
609 GNUNET_NETWORK_socket_free_memory_only_ (desc);
600 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR; 610 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
601} 611}
602 612
@@ -607,10 +617,10 @@ GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
607 * @param desc socket 617 * @param desc socket
608 */ 618 */
609void 619void
610GNUNET_NETWORK_socket_free_memory_only_(struct GNUNET_NETWORK_Handle *desc) 620GNUNET_NETWORK_socket_free_memory_only_ (struct GNUNET_NETWORK_Handle *desc)
611{ 621{
612 GNUNET_free_non_null(desc->addr); 622 GNUNET_free_non_null (desc->addr);
613 GNUNET_free(desc); 623 GNUNET_free (desc);
614} 624}
615 625
616 626
@@ -621,13 +631,13 @@ GNUNET_NETWORK_socket_free_memory_only_(struct GNUNET_NETWORK_Handle *desc)
621 * @return NULL on error (including not supported on target platform) 631 * @return NULL on error (including not supported on target platform)
622 */ 632 */
623struct GNUNET_NETWORK_Handle * 633struct GNUNET_NETWORK_Handle *
624GNUNET_NETWORK_socket_box_native(SOCKTYPE fd) 634GNUNET_NETWORK_socket_box_native (SOCKTYPE fd)
625{ 635{
626 struct GNUNET_NETWORK_Handle *ret; 636 struct GNUNET_NETWORK_Handle *ret;
627 637
628 if (fcntl(fd, F_GETFD) < 0) 638 if (fcntl (fd, F_GETFD) < 0)
629 return NULL; /* invalid FD */ 639 return NULL; /* invalid FD */
630 ret = GNUNET_new(struct GNUNET_NETWORK_Handle); 640 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
631 ret->fd = fd; 641 ret->fd = fd;
632 ret->af = AF_UNSPEC; 642 ret->af = AF_UNSPEC;
633 return ret; 643 return ret;
@@ -643,15 +653,15 @@ GNUNET_NETWORK_socket_box_native(SOCKTYPE fd)
643 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 653 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
644 */ 654 */
645int 655int
646GNUNET_NETWORK_socket_connect(const struct GNUNET_NETWORK_Handle *desc, 656GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
647 const struct sockaddr *address, 657 const struct sockaddr *address,
648 socklen_t address_len) 658 socklen_t address_len)
649{ 659{
650 int ret; 660 int ret;
651 661
652 ret = connect(desc->fd, 662 ret = connect (desc->fd,
653 address, 663 address,
654 address_len); 664 address_len);
655 665
656 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 666 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
657} 667}
@@ -668,18 +678,18 @@ GNUNET_NETWORK_socket_connect(const struct GNUNET_NETWORK_Handle *desc,
668 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 678 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
669 */ 679 */
670int 680int
671GNUNET_NETWORK_socket_getsockopt(const struct GNUNET_NETWORK_Handle *desc, 681GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
672 int level, 682 int level,
673 int optname, 683 int optname,
674 void *optval, 684 void *optval,
675 socklen_t *optlen) 685 socklen_t *optlen)
676{ 686{
677 int ret; 687 int ret;
678 688
679 ret = getsockopt(desc->fd, 689 ret = getsockopt (desc->fd,
680 level, 690 level,
681 optname, 691 optname,
682 optval, optlen); 692 optval, optlen);
683 693
684 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 694 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
685} 695}
@@ -693,13 +703,13 @@ GNUNET_NETWORK_socket_getsockopt(const struct GNUNET_NETWORK_Handle *desc,
693 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 703 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
694 */ 704 */
695int 705int
696GNUNET_NETWORK_socket_listen(const struct GNUNET_NETWORK_Handle *desc, 706GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
697 int backlog) 707 int backlog)
698{ 708{
699 int ret; 709 int ret;
700 710
701 ret = listen(desc->fd, 711 ret = listen (desc->fd,
702 backlog); 712 backlog);
703 713
704 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 714 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
705} 715}
@@ -712,18 +722,18 @@ GNUNET_NETWORK_socket_listen(const struct GNUNET_NETWORK_Handle *desc,
712 * @returns #GNUNET_SYSERR if no data is available, or on error! 722 * @returns #GNUNET_SYSERR if no data is available, or on error!
713 */ 723 */
714ssize_t 724ssize_t
715GNUNET_NETWORK_socket_recvfrom_amount(const struct GNUNET_NETWORK_Handle *desc) 725GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *desc)
716{ 726{
717 int error; 727 int error;
718 728
719 /* How much is there to be read? */ 729 /* How much is there to be read? */
720 int pending; 730 int pending;
721 731
722 error = ioctl(desc->fd, 732 error = ioctl (desc->fd,
723 FIONREAD, 733 FIONREAD,
724 &pending); 734 &pending);
725 if (0 == error) 735 if (0 == error)
726 return (ssize_t)pending; 736 return (ssize_t) pending;
727 return GNUNET_SYSERR; 737 return GNUNET_SYSERR;
728} 738}
729 739
@@ -739,11 +749,11 @@ GNUNET_NETWORK_socket_recvfrom_amount(const struct GNUNET_NETWORK_Handle *desc)
739 * @param addrlen length of the @a src_addr 749 * @param addrlen length of the @a src_addr
740 */ 750 */
741ssize_t 751ssize_t
742GNUNET_NETWORK_socket_recvfrom(const struct GNUNET_NETWORK_Handle *desc, 752GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
743 void *buffer, 753 void *buffer,
744 size_t length, 754 size_t length,
745 struct sockaddr *src_addr, 755 struct sockaddr *src_addr,
746 socklen_t *addrlen) 756 socklen_t *addrlen)
747{ 757{
748 int ret; 758 int ret;
749 int flags; 759 int flags;
@@ -753,12 +763,12 @@ GNUNET_NETWORK_socket_recvfrom(const struct GNUNET_NETWORK_Handle *desc,
753#ifdef MSG_DONTWAIT 763#ifdef MSG_DONTWAIT
754 flags |= MSG_DONTWAIT; 764 flags |= MSG_DONTWAIT;
755#endif 765#endif
756 ret = recvfrom(desc->fd, 766 ret = recvfrom (desc->fd,
757 buffer, 767 buffer,
758 length, 768 length,
759 flags, 769 flags,
760 src_addr, 770 src_addr,
761 addrlen); 771 addrlen);
762 return ret; 772 return ret;
763} 773}
764 774
@@ -772,9 +782,9 @@ GNUNET_NETWORK_socket_recvfrom(const struct GNUNET_NETWORK_Handle *desc,
772 * @return number of bytes received, -1 on error 782 * @return number of bytes received, -1 on error
773 */ 783 */
774ssize_t 784ssize_t
775GNUNET_NETWORK_socket_recv(const struct GNUNET_NETWORK_Handle *desc, 785GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
776 void *buffer, 786 void *buffer,
777 size_t length) 787 size_t length)
778{ 788{
779 int ret; 789 int ret;
780 int flags; 790 int flags;
@@ -784,10 +794,10 @@ GNUNET_NETWORK_socket_recv(const struct GNUNET_NETWORK_Handle *desc,
784#ifdef MSG_DONTWAIT 794#ifdef MSG_DONTWAIT
785 flags |= MSG_DONTWAIT; 795 flags |= MSG_DONTWAIT;
786#endif 796#endif
787 ret = recv(desc->fd, 797 ret = recv (desc->fd,
788 buffer, 798 buffer,
789 length, 799 length,
790 flags); 800 flags);
791 return ret; 801 return ret;
792} 802}
793 803
@@ -801,9 +811,9 @@ GNUNET_NETWORK_socket_recv(const struct GNUNET_NETWORK_Handle *desc,
801 * @return number of bytes sent, #GNUNET_SYSERR on error 811 * @return number of bytes sent, #GNUNET_SYSERR on error
802 */ 812 */
803ssize_t 813ssize_t
804GNUNET_NETWORK_socket_send(const struct GNUNET_NETWORK_Handle *desc, 814GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
805 const void *buffer, 815 const void *buffer,
806 size_t length) 816 size_t length)
807{ 817{
808 int ret; 818 int ret;
809 int flags; 819 int flags;
@@ -815,10 +825,10 @@ GNUNET_NETWORK_socket_send(const struct GNUNET_NETWORK_Handle *desc,
815#ifdef MSG_NOSIGNAL 825#ifdef MSG_NOSIGNAL
816 flags |= MSG_NOSIGNAL; 826 flags |= MSG_NOSIGNAL;
817#endif 827#endif
818 ret = send(desc->fd, 828 ret = send (desc->fd,
819 buffer, 829 buffer,
820 length, 830 length,
821 flags); 831 flags);
822 return ret; 832 return ret;
823} 833}
824 834
@@ -835,11 +845,11 @@ GNUNET_NETWORK_socket_send(const struct GNUNET_NETWORK_Handle *desc,
835 * @return number of bytes sent, #GNUNET_SYSERR on error 845 * @return number of bytes sent, #GNUNET_SYSERR on error
836 */ 846 */
837ssize_t 847ssize_t
838GNUNET_NETWORK_socket_sendto(const struct GNUNET_NETWORK_Handle *desc, 848GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
839 const void *message, 849 const void *message,
840 size_t length, 850 size_t length,
841 const struct sockaddr *dest_addr, 851 const struct sockaddr *dest_addr,
842 socklen_t dest_len) 852 socklen_t dest_len)
843{ 853{
844 int ret; 854 int ret;
845 int flags; 855 int flags;
@@ -852,7 +862,7 @@ GNUNET_NETWORK_socket_sendto(const struct GNUNET_NETWORK_Handle *desc,
852#ifdef MSG_NOSIGNAL 862#ifdef MSG_NOSIGNAL
853 flags |= MSG_NOSIGNAL; 863 flags |= MSG_NOSIGNAL;
854#endif 864#endif
855 ret = sendto(desc->fd, message, length, flags, dest_addr, dest_len); 865 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len);
856 return ret; 866 return ret;
857} 867}
858 868
@@ -868,19 +878,19 @@ GNUNET_NETWORK_socket_sendto(const struct GNUNET_NETWORK_Handle *desc,
868 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 878 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
869 */ 879 */
870int 880int
871GNUNET_NETWORK_socket_setsockopt(struct GNUNET_NETWORK_Handle *fd, 881GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
872 int level, 882 int level,
873 int option_name, 883 int option_name,
874 const void *option_value, 884 const void *option_value,
875 socklen_t option_len) 885 socklen_t option_len)
876{ 886{
877 int ret; 887 int ret;
878 888
879 ret = setsockopt(fd->fd, 889 ret = setsockopt (fd->fd,
880 level, 890 level,
881 option_name, 891 option_name,
882 option_value, 892 option_value,
883 option_len); 893 option_len);
884 894
885 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 895 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
886} 896}
@@ -897,22 +907,22 @@ GNUNET_NETWORK_socket_setsockopt(struct GNUNET_NETWORK_Handle *fd,
897 * @return new socket, NULL on error 907 * @return new socket, NULL on error
898 */ 908 */
899struct GNUNET_NETWORK_Handle * 909struct GNUNET_NETWORK_Handle *
900GNUNET_NETWORK_socket_create(int domain, 910GNUNET_NETWORK_socket_create (int domain,
901 int type, 911 int type,
902 int protocol) 912 int protocol)
903{ 913{
904 struct GNUNET_NETWORK_Handle *ret; 914 struct GNUNET_NETWORK_Handle *ret;
905 int fd; 915 int fd;
906 916
907 fd = socket(domain, type, protocol); 917 fd = socket (domain, type, protocol);
908 if (-1 == fd) 918 if (-1 == fd)
909 return NULL; 919 return NULL;
910 ret = GNUNET_new(struct GNUNET_NETWORK_Handle); 920 ret = GNUNET_new (struct GNUNET_NETWORK_Handle);
911 ret->fd = fd; 921 ret->fd = fd;
912 if (GNUNET_OK != 922 if (GNUNET_OK !=
913 initialize_network_handle(ret, 923 initialize_network_handle (ret,
914 domain, 924 domain,
915 type)) 925 type))
916 return NULL; 926 return NULL;
917 return ret; 927 return ret;
918} 928}
@@ -925,12 +935,12 @@ GNUNET_NETWORK_socket_create(int domain,
925 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 935 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
926 */ 936 */
927int 937int
928GNUNET_NETWORK_socket_shutdown(struct GNUNET_NETWORK_Handle *desc, 938GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
929 int how) 939 int how)
930{ 940{
931 int ret; 941 int ret;
932 942
933 ret = shutdown(desc->fd, how); 943 ret = shutdown (desc->fd, how);
934 944
935 return (0 == ret) ? GNUNET_OK : GNUNET_SYSERR; 945 return (0 == ret) ? GNUNET_OK : GNUNET_SYSERR;
936} 946}
@@ -946,7 +956,7 @@ GNUNET_NETWORK_socket_shutdown(struct GNUNET_NETWORK_Handle *desc,
946 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 956 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
947 */ 957 */
948int 958int
949GNUNET_NETWORK_socket_disable_corking(struct GNUNET_NETWORK_Handle *desc) 959GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
950{ 960{
951 int ret = 0; 961 int ret = 0;
952 962
@@ -955,22 +965,22 @@ GNUNET_NETWORK_socket_disable_corking(struct GNUNET_NETWORK_Handle *desc)
955 965
956 if (0 != 966 if (0 !=
957 (ret = 967 (ret =
958 setsockopt(desc->fd, 968 setsockopt (desc->fd,
959 SOL_SOCKET, 969 SOL_SOCKET,
960 SO_SNDBUF, 970 SO_SNDBUF,
961 &value, 971 &value,
962 sizeof(value)))) 972 sizeof(value))))
963 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, 973 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
964 "setsockopt"); 974 "setsockopt");
965 if (0 != 975 if (0 !=
966 (ret = 976 (ret =
967 setsockopt(desc->fd, 977 setsockopt (desc->fd,
968 SOL_SOCKET, 978 SOL_SOCKET,
969 SO_RCVBUF, 979 SO_RCVBUF,
970 &value, 980 &value,
971 sizeof(value)))) 981 sizeof(value))))
972 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, 982 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
973 "setsockopt"); 983 "setsockopt");
974#endif 984#endif
975 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 985 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
976} 986}
@@ -982,9 +992,9 @@ GNUNET_NETWORK_socket_disable_corking(struct GNUNET_NETWORK_Handle *desc)
982 * @param fds fd set 992 * @param fds fd set
983 */ 993 */
984void 994void
985GNUNET_NETWORK_fdset_zero(struct GNUNET_NETWORK_FDSet *fds) 995GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
986{ 996{
987 FD_ZERO(&fds->sds); 997 FD_ZERO (&fds->sds);
988 fds->nsds = 0; 998 fds->nsds = 0;
989} 999}
990 1000
@@ -996,13 +1006,13 @@ GNUNET_NETWORK_fdset_zero(struct GNUNET_NETWORK_FDSet *fds)
996 * @param desc socket to add 1006 * @param desc socket to add
997 */ 1007 */
998void 1008void
999GNUNET_NETWORK_fdset_set(struct GNUNET_NETWORK_FDSet *fds, 1009GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
1000 const struct GNUNET_NETWORK_Handle *desc) 1010 const struct GNUNET_NETWORK_Handle *desc)
1001{ 1011{
1002 FD_SET(desc->fd, 1012 FD_SET (desc->fd,
1003 &fds->sds); 1013 &fds->sds);
1004 fds->nsds = GNUNET_MAX(fds->nsds, 1014 fds->nsds = GNUNET_MAX (fds->nsds,
1005 desc->fd + 1); 1015 desc->fd + 1);
1006} 1016}
1007 1017
1008 1018
@@ -1014,11 +1024,11 @@ GNUNET_NETWORK_fdset_set(struct GNUNET_NETWORK_FDSet *fds,
1014 * @return 0 if the FD is not set 1024 * @return 0 if the FD is not set
1015 */ 1025 */
1016int 1026int
1017GNUNET_NETWORK_fdset_isset(const struct GNUNET_NETWORK_FDSet *fds, 1027GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
1018 const struct GNUNET_NETWORK_Handle *desc) 1028 const struct GNUNET_NETWORK_Handle *desc)
1019{ 1029{
1020 return FD_ISSET(desc->fd, 1030 return FD_ISSET (desc->fd,
1021 &fds->sds); 1031 &fds->sds);
1022} 1032}
1023 1033
1024 1034
@@ -1029,16 +1039,16 @@ GNUNET_NETWORK_fdset_isset(const struct GNUNET_NETWORK_FDSet *fds,
1029 * @param src the fd set to add from 1039 * @param src the fd set to add from
1030 */ 1040 */
1031void 1041void
1032GNUNET_NETWORK_fdset_add(struct GNUNET_NETWORK_FDSet *dst, 1042GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
1033 const struct GNUNET_NETWORK_FDSet *src) 1043 const struct GNUNET_NETWORK_FDSet *src)
1034{ 1044{
1035 int nfds; 1045 int nfds;
1036 1046
1037 for (nfds = src->nsds; nfds >= 0; nfds--) 1047 for (nfds = src->nsds; nfds >= 0; nfds--)
1038 if (FD_ISSET(nfds, &src->sds)) 1048 if (FD_ISSET (nfds, &src->sds))
1039 FD_SET(nfds, &dst->sds); 1049 FD_SET (nfds, &dst->sds);
1040 dst->nsds = GNUNET_MAX(dst->nsds, 1050 dst->nsds = GNUNET_MAX (dst->nsds,
1041 src->nsds); 1051 src->nsds);
1042} 1052}
1043 1053
1044 1054
@@ -1049,11 +1059,11 @@ GNUNET_NETWORK_fdset_add(struct GNUNET_NETWORK_FDSet *dst,
1049 * @param from source 1059 * @param from source
1050 */ 1060 */
1051void 1061void
1052GNUNET_NETWORK_fdset_copy(struct GNUNET_NETWORK_FDSet *to, 1062GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
1053 const struct GNUNET_NETWORK_FDSet *from) 1063 const struct GNUNET_NETWORK_FDSet *from)
1054{ 1064{
1055 FD_COPY(&from->sds, 1065 FD_COPY (&from->sds,
1056 &to->sds); 1066 &to->sds);
1057 to->nsds = from->nsds; 1067 to->nsds = from->nsds;
1058} 1068}
1059 1069
@@ -1065,7 +1075,7 @@ GNUNET_NETWORK_fdset_copy(struct GNUNET_NETWORK_FDSet *to,
1065 * @return POSIX file descriptor 1075 * @return POSIX file descriptor
1066 */ 1076 */
1067int 1077int
1068GNUNET_NETWORK_get_fd(const struct GNUNET_NETWORK_Handle *desc) 1078GNUNET_NETWORK_get_fd (const struct GNUNET_NETWORK_Handle *desc)
1069{ 1079{
1070 return desc->fd; 1080 return desc->fd;
1071} 1081}
@@ -1078,7 +1088,7 @@ GNUNET_NETWORK_get_fd(const struct GNUNET_NETWORK_Handle *desc)
1078 * @return sockaddr 1088 * @return sockaddr
1079 */ 1089 */
1080struct sockaddr* 1090struct sockaddr*
1081GNUNET_NETWORK_get_addr(const struct GNUNET_NETWORK_Handle *desc) 1091GNUNET_NETWORK_get_addr (const struct GNUNET_NETWORK_Handle *desc)
1082{ 1092{
1083 return desc->addr; 1093 return desc->addr;
1084} 1094}
@@ -1091,7 +1101,7 @@ GNUNET_NETWORK_get_addr(const struct GNUNET_NETWORK_Handle *desc)
1091 * @return socklen_t for sockaddr 1101 * @return socklen_t for sockaddr
1092 */ 1102 */
1093socklen_t 1103socklen_t
1094GNUNET_NETWORK_get_addrlen(const struct GNUNET_NETWORK_Handle *desc) 1104GNUNET_NETWORK_get_addrlen (const struct GNUNET_NETWORK_Handle *desc)
1095{ 1105{
1096 return desc->addrlen; 1106 return desc->addrlen;
1097} 1107}
@@ -1105,12 +1115,12 @@ GNUNET_NETWORK_get_addrlen(const struct GNUNET_NETWORK_Handle *desc)
1105 * @param nfds the biggest socket number in from + 1 1115 * @param nfds the biggest socket number in from + 1
1106 */ 1116 */
1107void 1117void
1108GNUNET_NETWORK_fdset_copy_native(struct GNUNET_NETWORK_FDSet *to, 1118GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
1109 const fd_set *from, 1119 const fd_set *from,
1110 int nfds) 1120 int nfds)
1111{ 1121{
1112 FD_COPY(from, 1122 FD_COPY (from,
1113 &to->sds); 1123 &to->sds);
1114 to->nsds = nfds; 1124 to->nsds = nfds;
1115} 1125}
1116 1126
@@ -1122,13 +1132,13 @@ GNUNET_NETWORK_fdset_copy_native(struct GNUNET_NETWORK_FDSet *to,
1122 * @param nfd native FD to set 1132 * @param nfd native FD to set
1123 */ 1133 */
1124void 1134void
1125GNUNET_NETWORK_fdset_set_native(struct GNUNET_NETWORK_FDSet *to, 1135GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
1126 int nfd) 1136 int nfd)
1127{ 1137{
1128 GNUNET_assert((nfd >= 0) && (nfd < FD_SETSIZE)); 1138 GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE));
1129 FD_SET(nfd, &to->sds); 1139 FD_SET (nfd, &to->sds);
1130 to->nsds = GNUNET_MAX(nfd + 1, 1140 to->nsds = GNUNET_MAX (nfd + 1,
1131 to->nsds); 1141 to->nsds);
1132} 1142}
1133 1143
1134 1144
@@ -1140,13 +1150,13 @@ GNUNET_NETWORK_fdset_set_native(struct GNUNET_NETWORK_FDSet *to,
1140 * @return #GNUNET_YES if FD is set in the set 1150 * @return #GNUNET_YES if FD is set in the set
1141 */ 1151 */
1142int 1152int
1143GNUNET_NETWORK_fdset_test_native(const struct GNUNET_NETWORK_FDSet *to, 1153GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
1144 int nfd) 1154 int nfd)
1145{ 1155{
1146 if ((-1 == nfd) || 1156 if ((-1 == nfd) ||
1147 (NULL == to)) 1157 (NULL == to))
1148 return GNUNET_NO; 1158 return GNUNET_NO;
1149 return FD_ISSET(nfd, &to->sds) ? GNUNET_YES : GNUNET_NO; 1159 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
1150} 1160}
1151 1161
1152 1162
@@ -1156,19 +1166,19 @@ GNUNET_NETWORK_fdset_test_native(const struct GNUNET_NETWORK_FDSet *to,
1156 * @param h the file handle to add 1166 * @param h the file handle to add
1157 */ 1167 */
1158void 1168void
1159GNUNET_NETWORK_fdset_handle_set(struct GNUNET_NETWORK_FDSet *fds, 1169GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
1160 const struct GNUNET_DISK_FileHandle *h) 1170 const struct GNUNET_DISK_FileHandle *h)
1161{ 1171{
1162 int fd; 1172 int fd;
1163 1173
1164 GNUNET_assert(GNUNET_OK == 1174 GNUNET_assert (GNUNET_OK ==
1165 GNUNET_DISK_internal_file_handle_(h, 1175 GNUNET_DISK_internal_file_handle_ (h,
1166 &fd, 1176 &fd,
1167 sizeof(int))); 1177 sizeof(int)));
1168 FD_SET(fd, 1178 FD_SET (fd,
1169 &fds->sds); 1179 &fds->sds);
1170 fds->nsds = GNUNET_MAX(fd + 1, 1180 fds->nsds = GNUNET_MAX (fd + 1,
1171 fds->nsds); 1181 fds->nsds);
1172} 1182}
1173 1183
1174 1184
@@ -1178,10 +1188,10 @@ GNUNET_NETWORK_fdset_handle_set(struct GNUNET_NETWORK_FDSet *fds,
1178 * @param h the file handle to add 1188 * @param h the file handle to add
1179 */ 1189 */
1180void 1190void
1181GNUNET_NETWORK_fdset_handle_set_first(struct GNUNET_NETWORK_FDSet *fds, 1191GNUNET_NETWORK_fdset_handle_set_first (struct GNUNET_NETWORK_FDSet *fds,
1182 const struct GNUNET_DISK_FileHandle *h) 1192 const struct GNUNET_DISK_FileHandle *h)
1183{ 1193{
1184 GNUNET_NETWORK_fdset_handle_set(fds, h); 1194 GNUNET_NETWORK_fdset_handle_set (fds, h);
1185} 1195}
1186 1196
1187 1197
@@ -1193,11 +1203,11 @@ GNUNET_NETWORK_fdset_handle_set_first(struct GNUNET_NETWORK_FDSet *fds,
1193 * @return #GNUNET_YES if the file handle is part of the set 1203 * @return #GNUNET_YES if the file handle is part of the set
1194 */ 1204 */
1195int 1205int
1196GNUNET_NETWORK_fdset_handle_isset(const struct GNUNET_NETWORK_FDSet *fds, 1206GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
1197 const struct GNUNET_DISK_FileHandle *h) 1207 const struct GNUNET_DISK_FileHandle *h)
1198{ 1208{
1199 return FD_ISSET(h->fd, 1209 return FD_ISSET (h->fd,
1200 &fds->sds); 1210 &fds->sds);
1201} 1211}
1202 1212
1203 1213
@@ -1209,22 +1219,22 @@ GNUNET_NETWORK_fdset_handle_isset(const struct GNUNET_NETWORK_FDSet *fds,
1209 * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise 1219 * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise
1210 */ 1220 */
1211int 1221int
1212GNUNET_NETWORK_fdset_overlap(const struct GNUNET_NETWORK_FDSet *fds1, 1222GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
1213 const struct GNUNET_NETWORK_FDSet *fds2) 1223 const struct GNUNET_NETWORK_FDSet *fds2)
1214{ 1224{
1215 int nfds; 1225 int nfds;
1216 1226
1217 nfds = GNUNET_MIN(fds1->nsds, 1227 nfds = GNUNET_MIN (fds1->nsds,
1218 fds2->nsds); 1228 fds2->nsds);
1219 while (nfds > 0) 1229 while (nfds > 0)
1220 { 1230 {
1221 nfds--; 1231 nfds--;
1222 if ((FD_ISSET(nfds, 1232 if ((FD_ISSET (nfds,
1223 &fds1->sds)) && 1233 &fds1->sds)) &&
1224 (FD_ISSET(nfds, 1234 (FD_ISSET (nfds,
1225 &fds2->sds))) 1235 &fds2->sds)))
1226 return GNUNET_YES; 1236 return GNUNET_YES;
1227 } 1237 }
1228 return GNUNET_NO; 1238 return GNUNET_NO;
1229} 1239}
1230 1240
@@ -1235,12 +1245,12 @@ GNUNET_NETWORK_fdset_overlap(const struct GNUNET_NETWORK_FDSet *fds1,
1235 * @return a new fd set 1245 * @return a new fd set
1236 */ 1246 */
1237struct GNUNET_NETWORK_FDSet * 1247struct GNUNET_NETWORK_FDSet *
1238GNUNET_NETWORK_fdset_create() 1248GNUNET_NETWORK_fdset_create ()
1239{ 1249{
1240 struct GNUNET_NETWORK_FDSet *fds; 1250 struct GNUNET_NETWORK_FDSet *fds;
1241 1251
1242 fds = GNUNET_new(struct GNUNET_NETWORK_FDSet); 1252 fds = GNUNET_new (struct GNUNET_NETWORK_FDSet);
1243 GNUNET_NETWORK_fdset_zero(fds); 1253 GNUNET_NETWORK_fdset_zero (fds);
1244 return fds; 1254 return fds;
1245} 1255}
1246 1256
@@ -1251,9 +1261,9 @@ GNUNET_NETWORK_fdset_create()
1251 * @param fds fd set 1261 * @param fds fd set
1252 */ 1262 */
1253void 1263void
1254GNUNET_NETWORK_fdset_destroy(struct GNUNET_NETWORK_FDSet *fds) 1264GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1255{ 1265{
1256 GNUNET_free(fds); 1266 GNUNET_free (fds);
1257} 1267}
1258 1268
1259 1269
@@ -1265,8 +1275,8 @@ GNUNET_NETWORK_fdset_destroy(struct GNUNET_NETWORK_FDSet *fds)
1265 * @return #GNUNET_OK if the port is available, #GNUNET_NO if not 1275 * @return #GNUNET_OK if the port is available, #GNUNET_NO if not
1266 */ 1276 */
1267int 1277int
1268GNUNET_NETWORK_test_port_free(int ipproto, 1278GNUNET_NETWORK_test_port_free (int ipproto,
1269 uint16_t port) 1279 uint16_t port)
1270{ 1280{
1271 struct GNUNET_NETWORK_Handle *socket; 1281 struct GNUNET_NETWORK_Handle *socket;
1272 int bind_status; 1282 int bind_status;
@@ -1276,13 +1286,13 @@ GNUNET_NETWORK_test_port_free(int ipproto,
1276 struct addrinfo *ret; 1286 struct addrinfo *ret;
1277 struct addrinfo *ai; 1287 struct addrinfo *ai;
1278 1288
1279 GNUNET_snprintf(open_port_str, 1289 GNUNET_snprintf (open_port_str,
1280 sizeof(open_port_str), 1290 sizeof(open_port_str),
1281 "%u", 1291 "%u",
1282 (unsigned int)port); 1292 (unsigned int) port);
1283 socktype = (IPPROTO_TCP == ipproto) ? SOCK_STREAM : SOCK_DGRAM; 1293 socktype = (IPPROTO_TCP == ipproto) ? SOCK_STREAM : SOCK_DGRAM;
1284 ret = NULL; 1294 ret = NULL;
1285 memset(&hint, 0, sizeof(hint)); 1295 memset (&hint, 0, sizeof(hint));
1286 hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */ 1296 hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
1287 hint.ai_socktype = socktype; 1297 hint.ai_socktype = socktype;
1288 hint.ai_protocol = ipproto; 1298 hint.ai_protocol = ipproto;
@@ -1291,26 +1301,26 @@ GNUNET_NETWORK_test_port_free(int ipproto,
1291 hint.ai_canonname = NULL; 1301 hint.ai_canonname = NULL;
1292 hint.ai_next = NULL; 1302 hint.ai_next = NULL;
1293 hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */ 1303 hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
1294 GNUNET_assert(0 == getaddrinfo(NULL, 1304 GNUNET_assert (0 == getaddrinfo (NULL,
1295 open_port_str, 1305 open_port_str,
1296 &hint, 1306 &hint,
1297 &ret)); 1307 &ret));
1298 bind_status = GNUNET_NO; 1308 bind_status = GNUNET_NO;
1299 for (ai = ret; NULL != ai; ai = ai->ai_next) 1309 for (ai = ret; NULL != ai; ai = ai->ai_next)
1300 { 1310 {
1301 socket = GNUNET_NETWORK_socket_create(ai->ai_family, 1311 socket = GNUNET_NETWORK_socket_create (ai->ai_family,
1302 ai->ai_socktype, 1312 ai->ai_socktype,
1303 ai->ai_protocol); 1313 ai->ai_protocol);
1304 if (NULL == socket) 1314 if (NULL == socket)
1305 continue; 1315 continue;
1306 bind_status = GNUNET_NETWORK_socket_bind(socket, 1316 bind_status = GNUNET_NETWORK_socket_bind (socket,
1307 ai->ai_addr, 1317 ai->ai_addr,
1308 ai->ai_addrlen); 1318 ai->ai_addrlen);
1309 GNUNET_NETWORK_socket_close(socket); 1319 GNUNET_NETWORK_socket_close (socket);
1310 if (GNUNET_OK != bind_status) 1320 if (GNUNET_OK != bind_status)
1311 break; 1321 break;
1312 } 1322 }
1313 freeaddrinfo(ret); 1323 freeaddrinfo (ret);
1314 return bind_status; 1324 return bind_status;
1315} 1325}
1316 1326
@@ -1325,10 +1335,10 @@ GNUNET_NETWORK_test_port_free(int ipproto,
1325 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error 1335 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
1326 */ 1336 */
1327int 1337int
1328GNUNET_NETWORK_socket_select(struct GNUNET_NETWORK_FDSet *rfds, 1338GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1329 struct GNUNET_NETWORK_FDSet *wfds, 1339 struct GNUNET_NETWORK_FDSet *wfds,
1330 struct GNUNET_NETWORK_FDSet *efds, 1340 struct GNUNET_NETWORK_FDSet *efds,
1331 const struct GNUNET_TIME_Relative timeout) 1341 const struct GNUNET_TIME_Relative timeout)
1332{ 1342{
1333 int nfds; 1343 int nfds;
1334 struct timeval tv; 1344 struct timeval tv;
@@ -1338,37 +1348,41 @@ GNUNET_NETWORK_socket_select(struct GNUNET_NETWORK_FDSet *rfds,
1338 else 1348 else
1339 nfds = 0; 1349 nfds = 0;
1340 if (NULL != wfds) 1350 if (NULL != wfds)
1341 nfds = GNUNET_MAX(nfds, 1351 nfds = GNUNET_MAX (nfds,
1342 wfds->nsds); 1352 wfds->nsds);
1343 if (NULL != efds) 1353 if (NULL != efds)
1344 nfds = GNUNET_MAX(nfds, 1354 nfds = GNUNET_MAX (nfds,
1345 efds->nsds); 1355 efds->nsds);
1346 if ((0 == nfds) && 1356 if ((0 == nfds) &&
1347 (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)) 1357 (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
1348 { 1358 {
1349 GNUNET_break(0); 1359 GNUNET_break (0);
1350 LOG(GNUNET_ERROR_TYPE_ERROR, 1360 LOG (GNUNET_ERROR_TYPE_ERROR,
1351 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"), 1361 _ (
1352 "select"); 1362 "Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1353 } 1363 "select");
1354 if (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us > (unsigned long long)LONG_MAX) 1364 }
1355 { 1365 if (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us > (unsigned
1356 tv.tv_sec = LONG_MAX; 1366 long long)
1357 tv.tv_usec = 999999L; 1367 LONG_MAX)
1358 } 1368 {
1369 tv.tv_sec = LONG_MAX;
1370 tv.tv_usec = 999999L;
1371 }
1359 else 1372 else
1360 { 1373 {
1361 tv.tv_sec = (long)(timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us); 1374 tv.tv_sec = (long) (timeout.rel_value_us
1362 tv.tv_usec = 1375 / GNUNET_TIME_UNIT_SECONDS.rel_value_us);
1363 (timeout.rel_value_us - 1376 tv.tv_usec =
1364 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us)); 1377 (timeout.rel_value_us
1365 } 1378 - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1366 return select(nfds, 1379 }
1367 (NULL != rfds) ? &rfds->sds : NULL, 1380 return select (nfds,
1368 (NULL != wfds) ? &wfds->sds : NULL, 1381 (NULL != rfds) ? &rfds->sds : NULL,
1369 (NULL != efds) ? &efds->sds : NULL, 1382 (NULL != wfds) ? &wfds->sds : NULL,
1370 (timeout.rel_value_us == 1383 (NULL != efds) ? &efds->sds : NULL,
1371 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv); 1384 (timeout.rel_value_us ==
1385 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv);
1372} 1386}
1373 1387
1374/* end of network.c */ 1388/* end of network.c */