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.c1975
1 files changed, 988 insertions, 987 deletions
diff --git a/src/util/network.c b/src/util/network.c
index 2c16fe8c9..9eff6b96e 100644
--- a/src/util/network.c
+++ b/src/util/network.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file util/network.c 22 * @file util/network.c
@@ -28,9 +28,9 @@
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, filename) GNUNET_log_from_strerror_file(kind, "util-network", syscall, filename)
33#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-network", syscall) 33#define LOG_STRERROR(kind, syscall) GNUNET_log_from_strerror(kind, "util-network", syscall)
34 34
35#define DEBUG_NETWORK GNUNET_EXTRA_LOGGING 35#define DEBUG_NETWORK GNUNET_EXTRA_LOGGING
36 36
@@ -43,8 +43,7 @@
43/** 43/**
44 * @brief handle to a socket 44 * @brief handle to a socket
45 */ 45 */
46struct GNUNET_NETWORK_Handle 46struct GNUNET_NETWORK_Handle {
47{
48#ifndef MINGW 47#ifndef MINGW
49 int fd; 48 int fd;
50#else 49#else
@@ -70,7 +69,6 @@ struct GNUNET_NETWORK_Handle
70 * Address we were bound to, or NULL. 69 * Address we were bound to, or NULL.
71 */ 70 */
72 struct sockaddr *addr; 71 struct sockaddr *addr;
73
74}; 72};
75 73
76 74
@@ -81,7 +79,7 @@ struct GNUNET_NETWORK_Handle
81 * @return #GNUNET_OK if the PF is supported 79 * @return #GNUNET_OK if the PF is supported
82 */ 80 */
83int 81int
84GNUNET_NETWORK_test_pf (int pf) 82GNUNET_NETWORK_test_pf(int pf)
85{ 83{
86 static int cache_v4 = -1; 84 static int cache_v4 = -1;
87 static int cache_v6 = -1; 85 static int cache_v6 = -1;
@@ -90,56 +88,60 @@ GNUNET_NETWORK_test_pf (int pf)
90 int ret; 88 int ret;
91 89
92 switch (pf) 90 switch (pf)
93 { 91 {
94 case PF_INET: 92 case PF_INET:
95 if (-1 != cache_v4) 93 if (-1 != cache_v4)
96 return cache_v4; 94 return cache_v4;
97 break; 95 break;
98 case PF_INET6: 96
99 if (-1 != cache_v6) 97 case PF_INET6:
100 return cache_v6; 98 if (-1 != cache_v6)
101 break; 99 return cache_v6;
100 break;
101
102#ifdef PF_UNIX 102#ifdef PF_UNIX
103 case PF_UNIX: 103 case PF_UNIX:
104 if (-1 != cache_un) 104 if (-1 != cache_un)
105 return cache_un; 105 return cache_un;
106 break; 106 break;
107#endif 107#endif
108 } 108 }
109 s = socket (pf, SOCK_STREAM, 0); 109 s = socket(pf, SOCK_STREAM, 0);
110 if (-1 == s) 110 if (-1 == s)
111 {
112 if (EAFNOSUPPORT != errno)
113 { 111 {
114 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, 112 if (EAFNOSUPPORT != errno)
115 "socket"); 113 {
116 return GNUNET_SYSERR; 114 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING,
115 "socket");
116 return GNUNET_SYSERR;
117 }
118 ret = GNUNET_NO;
117 } 119 }
118 ret = GNUNET_NO;
119 }
120 else 120 else
121 { 121 {
122#if WINDOWS 122#if WINDOWS
123 closesocket (s); 123 closesocket(s);
124#else 124#else
125 close (s); 125 close(s);
126#endif 126#endif
127 ret = GNUNET_OK; 127 ret = GNUNET_OK;
128 } 128 }
129 switch (pf) 129 switch (pf)
130 { 130 {
131 case PF_INET: 131 case PF_INET:
132 cache_v4 = ret; 132 cache_v4 = ret;
133 break; 133 break;
134 case PF_INET6: 134
135 cache_v6 = ret; 135 case PF_INET6:
136 break; 136 cache_v6 = ret;
137 break;
138
137#ifdef PF_UNIX 139#ifdef PF_UNIX
138 case PF_UNIX: 140 case PF_UNIX:
139 cache_un = ret; 141 cache_un = ret;
140 break; 142 break;
141#endif 143#endif
142 } 144 }
143 return ret; 145 return ret;
144} 146}
145 147
@@ -154,7 +156,7 @@ GNUNET_NETWORK_test_pf (int pf)
154 * @return shortened unixpath, NULL on error 156 * @return shortened unixpath, NULL on error
155 */ 157 */
156char * 158char *
157GNUNET_NETWORK_shorten_unixpath (char *unixpath) 159GNUNET_NETWORK_shorten_unixpath(char *unixpath)
158{ 160{
159 struct sockaddr_un dummy; 161 struct sockaddr_un dummy;
160 size_t slen; 162 size_t slen;
@@ -163,26 +165,26 @@ GNUNET_NETWORK_shorten_unixpath (char *unixpath)
163 struct GNUNET_CRYPTO_HashAsciiEncoded ae; 165 struct GNUNET_CRYPTO_HashAsciiEncoded ae;
164 size_t upm; 166 size_t upm;
165 167
166 upm = sizeof (dummy.sun_path); 168 upm = sizeof(dummy.sun_path);
167 slen = strlen (unixpath); 169 slen = strlen(unixpath);
168 if (slen < upm) 170 if (slen < upm)
169 return unixpath; /* no shortening required */ 171 return unixpath; /* no shortening required */
170 GNUNET_CRYPTO_hash (unixpath, slen, &sh); 172 GNUNET_CRYPTO_hash(unixpath, slen, &sh);
171 while (16 + strlen (unixpath) >= upm) 173 while (16 + strlen(unixpath) >= upm)
172 {
173 if (NULL == (end = strrchr (unixpath, '/')))
174 { 174 {
175 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 175 if (NULL == (end = strrchr(unixpath, '/')))
176 _("Unable to shorten unix path `%s' while keeping name unique\n"), 176 {
177 unixpath); 177 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
178 GNUNET_free (unixpath); 178 _("Unable to shorten unix path `%s' while keeping name unique\n"),
179 return NULL; 179 unixpath);
180 GNUNET_free(unixpath);
181 return NULL;
182 }
183 *end = '\0';
180 } 184 }
181 *end = '\0'; 185 GNUNET_CRYPTO_hash_to_enc(&sh, &ae);
182 }
183 GNUNET_CRYPTO_hash_to_enc (&sh, &ae);
184 ae.encoding[16] = '\0'; 186 ae.encoding[16] = '\0';
185 strcat (unixpath, (char *) ae.encoding); 187 strcat(unixpath, (char *)ae.encoding);
186 return unixpath; 188 return unixpath;
187} 189}
188 190
@@ -198,50 +200,50 @@ GNUNET_NETWORK_shorten_unixpath (char *unixpath)
198 * @param un unix domain socket address to check 200 * @param un unix domain socket address to check
199 */ 201 */
200void 202void
201GNUNET_NETWORK_unix_precheck (const struct sockaddr_un *un) 203GNUNET_NETWORK_unix_precheck(const struct sockaddr_un *un)
202{ 204{
203 int s; 205 int s;
204 int eno; 206 int eno;
205 struct stat sbuf; 207 struct stat sbuf;
206 int ret; 208 int ret;
207 209
208 s = socket (AF_UNIX, SOCK_STREAM, 0); 210 s = socket(AF_UNIX, SOCK_STREAM, 0);
209 if (-1 == s) 211 if (-1 == s)
210 { 212 {
211 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, 213 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING,
212 "Failed to open AF_UNIX socket"); 214 "Failed to open AF_UNIX socket");
213 return; 215 return;
214 } 216 }
215 ret = connect (s, 217 ret = connect(s,
216 (struct sockaddr *) un, 218 (struct sockaddr *)un,
217 sizeof (struct sockaddr_un)); 219 sizeof(struct sockaddr_un));
218 eno = errno; 220 eno = errno;
219 GNUNET_break (0 == close (s)); 221 GNUNET_break(0 == close(s));
220 if (0 == ret) 222 if (0 == ret)
221 return; /* another process is listening, do not remove! */ 223 return; /* another process is listening, do not remove! */
222 if (ECONNREFUSED != eno) 224 if (ECONNREFUSED != eno)
223 return; /* some other error, likely "no such file or directory" -- all well */ 225 return; /* some other error, likely "no such file or directory" -- all well */
224 /* should unlink, but sanity checks first */ 226 /* should unlink, but sanity checks first */
225 if (0 != stat (un->sun_path, 227 if (0 != stat(un->sun_path,
226 &sbuf)) 228 &sbuf))
227 return; /* failed to 'stat', likely does not exist after all */ 229 return; /* failed to 'stat', likely does not exist after all */
228 if (S_IFSOCK != (S_IFMT & sbuf.st_mode)) 230 if (S_IFSOCK != (S_IFMT & sbuf.st_mode))
229 return; /* refuse to unlink anything except sockets */ 231 return; /* refuse to unlink anything except sockets */
230 /* finally, really unlink */ 232 /* finally, really unlink */
231 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 233 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
232 "Removing left-over `%s' from previous exeuction\n", 234 "Removing left-over `%s' from previous exeuction\n",
233 un->sun_path); 235 un->sun_path);
234 if (0 != unlink (un->sun_path)) 236 if (0 != unlink(un->sun_path))
235 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, 237 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING,
236 "unlink", 238 "unlink",
237 un->sun_path); 239 un->sun_path);
238} 240}
239#endif 241#endif
240 242
241 243
242 244
243#ifndef FD_COPY 245#ifndef FD_COPY
244#define FD_COPY(s, d) do { GNUNET_memcpy ((d), (s), sizeof (fd_set)); } while (0) 246#define FD_COPY(s, d) do { GNUNET_memcpy((d), (s), sizeof(fd_set)); } while (0)
245#endif 247#endif
246 248
247 249
@@ -253,51 +255,49 @@ GNUNET_NETWORK_unix_precheck (const struct sockaddr_un *un)
253 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 255 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
254 */ 256 */
255int 257int
256GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, 258GNUNET_NETWORK_socket_set_blocking(struct GNUNET_NETWORK_Handle *fd,
257 int doBlock) 259 int doBlock)
258{ 260{
259
260#if MINGW 261#if MINGW
261 u_long mode; 262 u_long mode;
262 263
263 mode = !doBlock; 264 mode = !doBlock;
264 if (SOCKET_ERROR == 265 if (SOCKET_ERROR ==
265 ioctlsocket (fd->fd, 266 ioctlsocket(fd->fd,
266 FIONBIO, 267 FIONBIO,
267 &mode)) 268 &mode))
268 269
269 { 270 {
270 SetErrnoFromWinsockError (WSAGetLastError ()); 271 SetErrnoFromWinsockError(WSAGetLastError());
271 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, 272 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING,
272 "ioctlsocket"); 273 "ioctlsocket");
273 return GNUNET_SYSERR; 274 return GNUNET_SYSERR;
274 } 275 }
275 return GNUNET_OK; 276 return GNUNET_OK;
276
277#else 277#else
278 /* not MINGW */ 278 /* not MINGW */
279 int flags = fcntl (fd->fd, F_GETFL); 279 int flags = fcntl(fd->fd, F_GETFL);
280 280
281 if (flags == -1) 281 if (flags == -1)
282 { 282 {
283 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, 283 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING,
284 "fcntl"); 284 "fcntl");
285 return GNUNET_SYSERR; 285 return GNUNET_SYSERR;
286 } 286 }
287 if (doBlock) 287 if (doBlock)
288 flags &= ~O_NONBLOCK; 288 flags &= ~O_NONBLOCK;
289 289
290 else 290 else
291 flags |= O_NONBLOCK; 291 flags |= O_NONBLOCK;
292 if (0 != fcntl (fd->fd, 292 if (0 != fcntl(fd->fd,
293 F_SETFL, 293 F_SETFL,
294 flags)) 294 flags))
295 295
296 { 296 {
297 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, 297 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING,
298 "fcntl"); 298 "fcntl");
299 return GNUNET_SYSERR; 299 return GNUNET_SYSERR;
300 } 300 }
301 return GNUNET_OK; 301 return GNUNET_OK;
302#endif 302#endif
303} 303}
@@ -311,27 +311,27 @@ GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd,
311 * @warning Not implemented on Windows 311 * @warning Not implemented on Windows
312 */ 312 */
313static int 313static int
314socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h) 314socket_set_inheritable(const struct GNUNET_NETWORK_Handle *h)
315{ 315{
316#ifndef MINGW 316#ifndef MINGW
317 int i; 317 int i;
318 i = fcntl (h->fd, F_GETFD); 318 i = fcntl(h->fd, F_GETFD);
319 if (i < 0) 319 if (i < 0)
320 return GNUNET_SYSERR; 320 return GNUNET_SYSERR;
321 if (i == (i | FD_CLOEXEC)) 321 if (i == (i | FD_CLOEXEC))
322 return GNUNET_OK; 322 return GNUNET_OK;
323 i |= FD_CLOEXEC; 323 i |= FD_CLOEXEC;
324 if (fcntl (h->fd, F_SETFD, i) < 0) 324 if (fcntl(h->fd, F_SETFD, i) < 0)
325 return GNUNET_SYSERR; 325 return GNUNET_SYSERR;
326#else 326#else
327 BOOL b; 327 BOOL b;
328 SetLastError (0); 328 SetLastError(0);
329 b = SetHandleInformation ((HANDLE) h->fd, HANDLE_FLAG_INHERIT, 0); 329 b = SetHandleInformation((HANDLE)h->fd, HANDLE_FLAG_INHERIT, 0);
330 if (!b) 330 if (!b)
331 { 331 {
332 SetErrnoFromWinsockError (WSAGetLastError ()); 332 SetErrnoFromWinsockError(WSAGetLastError());
333 return GNUNET_SYSERR; 333 return GNUNET_SYSERR;
334 } 334 }
335#endif 335#endif
336 return GNUNET_OK; 336 return GNUNET_OK;
337} 337}
@@ -344,14 +344,14 @@ socket_set_inheritable (const struct GNUNET_NETWORK_Handle *h)
344 * @param h the socket to make non-delaying 344 * @param h the socket to make non-delaying
345 */ 345 */
346static int 346static int
347socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h) 347socket_set_nosigpipe(const struct GNUNET_NETWORK_Handle *h)
348{ 348{
349 int abs_value = 1; 349 int abs_value = 1;
350 350
351 if (0 != 351 if (0 !=
352 setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, 352 setsockopt(h->fd, SOL_SOCKET, SO_NOSIGPIPE,
353 (const void *) &abs_value, 353 (const void *)&abs_value,
354 sizeof (abs_value))) 354 sizeof(abs_value)))
355 return GNUNET_SYSERR; 355 return GNUNET_SYSERR;
356 return GNUNET_OK; 356 return GNUNET_OK;
357} 357}
@@ -366,29 +366,29 @@ socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
366 * @param h the socket to make non-delaying 366 * @param h the socket to make non-delaying
367 */ 367 */
368static void 368static void
369socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h) 369socket_set_nodelay(const struct GNUNET_NETWORK_Handle *h)
370{ 370{
371#ifndef WINDOWS 371#ifndef WINDOWS
372 int value = 1; 372 int value = 1;
373 373
374 if (0 != 374 if (0 !=
375 setsockopt (h->fd, 375 setsockopt(h->fd,
376 IPPROTO_TCP, 376 IPPROTO_TCP,
377 TCP_NODELAY, 377 TCP_NODELAY,
378 &value, sizeof (value))) 378 &value, sizeof(value)))
379 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, 379 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING,
380 "setsockopt"); 380 "setsockopt");
381#else 381#else
382 const char *abs_value = "1"; 382 const char *abs_value = "1";
383 383
384 if (0 != 384 if (0 !=
385 setsockopt (h->fd, 385 setsockopt(h->fd,
386 IPPROTO_TCP, 386 IPPROTO_TCP,
387 TCP_NODELAY, 387 TCP_NODELAY,
388 (const void *) abs_value, 388 (const void *)abs_value,
389 sizeof (abs_value))) 389 sizeof(abs_value)))
390 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, 390 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING,
391 "setsockopt"); 391 "setsockopt");
392#endif 392#endif
393} 393}
394 394
@@ -406,60 +406,60 @@ socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h)
406 * failed and the handle was destroyed 406 * failed and the handle was destroyed
407 */ 407 */
408static int 408static int
409initialize_network_handle (struct GNUNET_NETWORK_Handle *h, 409initialize_network_handle(struct GNUNET_NETWORK_Handle *h,
410 int af, 410 int af,
411 int type) 411 int type)
412{ 412{
413 int eno; 413 int eno;
414 414
415 h->af = af; 415 h->af = af;
416 h->type = type; 416 h->type = type;
417 if (h->fd == INVALID_SOCKET) 417 if (h->fd == INVALID_SOCKET)
418 { 418 {
419#ifdef MINGW 419#ifdef MINGW
420 SetErrnoFromWinsockError (WSAGetLastError ()); 420 SetErrnoFromWinsockError(WSAGetLastError());
421#endif 421#endif
422 eno = errno; 422 eno = errno;
423 GNUNET_free (h); 423 GNUNET_free(h);
424 errno = eno; 424 errno = eno;
425 return GNUNET_SYSERR; 425 return GNUNET_SYSERR;
426 } 426 }
427#ifndef MINGW 427#ifndef MINGW
428 if (h->fd >= FD_SETSIZE) 428 if (h->fd >= FD_SETSIZE)
429 { 429 {
430 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h)); 430 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close(h));
431 errno = EMFILE; 431 errno = EMFILE;
432 return GNUNET_SYSERR; 432 return GNUNET_SYSERR;
433 } 433 }
434#endif 434#endif
435 if (GNUNET_OK != socket_set_inheritable (h)) 435 if (GNUNET_OK != socket_set_inheritable(h))
436 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 436 LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
437 "socket_set_inheritable"); 437 "socket_set_inheritable");
438 438
439 if (GNUNET_SYSERR == GNUNET_NETWORK_socket_set_blocking (h, GNUNET_NO)) 439 if (GNUNET_SYSERR == GNUNET_NETWORK_socket_set_blocking(h, GNUNET_NO))
440 { 440 {
441 eno = errno; 441 eno = errno;
442 GNUNET_break (0); 442 GNUNET_break(0);
443 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h)); 443 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close(h));
444 errno = eno; 444 errno = eno;
445 return GNUNET_SYSERR; 445 return GNUNET_SYSERR;
446 } 446 }
447#ifdef DARWIN 447#ifdef DARWIN
448 if (GNUNET_SYSERR == socket_set_nosigpipe (h)) 448 if (GNUNET_SYSERR == socket_set_nosigpipe(h))
449 { 449 {
450 eno = errno; 450 eno = errno;
451 GNUNET_break (0); 451 GNUNET_break(0);
452 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h)); 452 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close(h));
453 errno = eno; 453 errno = eno;
454 return GNUNET_SYSERR; 454 return GNUNET_SYSERR;
455 } 455 }
456#endif 456#endif
457 if ( (type == SOCK_STREAM) 457 if ((type == SOCK_STREAM)
458#ifdef AF_UNIX 458#ifdef AF_UNIX
459 && (af != AF_UNIX) 459 && (af != AF_UNIX)
460#endif 460#endif
461 ) 461 )
462 socket_set_nodelay (h); 462 socket_set_nodelay(h);
463 return GNUNET_OK; 463 return GNUNET_OK;
464} 464}
465 465
@@ -473,47 +473,47 @@ initialize_network_handle (struct GNUNET_NETWORK_Handle *h,
473 * @return client socket 473 * @return client socket
474 */ 474 */
475struct GNUNET_NETWORK_Handle * 475struct GNUNET_NETWORK_Handle *
476GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc, 476GNUNET_NETWORK_socket_accept(const struct GNUNET_NETWORK_Handle *desc,
477 struct sockaddr *address, 477 struct sockaddr *address,
478 socklen_t *address_len) 478 socklen_t *address_len)
479{ 479{
480 struct GNUNET_NETWORK_Handle *ret; 480 struct GNUNET_NETWORK_Handle *ret;
481 int eno; 481 int eno;
482 482
483 ret = GNUNET_new (struct GNUNET_NETWORK_Handle); 483 ret = GNUNET_new(struct GNUNET_NETWORK_Handle);
484#if DEBUG_NETWORK 484#if DEBUG_NETWORK
485 { 485 {
486 struct sockaddr_storage name; 486 struct sockaddr_storage name;
487 socklen_t namelen = sizeof (name); 487 socklen_t namelen = sizeof(name);
488 488
489 int gsn = getsockname (desc->fd, 489 int gsn = getsockname(desc->fd,
490 (struct sockaddr *) &name, 490 (struct sockaddr *)&name,
491 &namelen); 491 &namelen);
492 492
493 if (0 == gsn) 493 if (0 == gsn)
494 LOG (GNUNET_ERROR_TYPE_DEBUG, 494 LOG(GNUNET_ERROR_TYPE_DEBUG,
495 "Accepting connection on `%s'\n", 495 "Accepting connection on `%s'\n",
496 GNUNET_a2s ((const struct sockaddr *) &name, 496 GNUNET_a2s((const struct sockaddr *)&name,
497 namelen)); 497 namelen));
498 } 498 }
499#endif 499#endif
500 ret->fd = accept (desc->fd, 500 ret->fd = accept(desc->fd,
501 address, 501 address,
502 address_len); 502 address_len);
503 if (-1 == ret->fd) 503 if (-1 == ret->fd)
504 { 504 {
505 eno = errno; 505 eno = errno;
506 GNUNET_free (ret); 506 GNUNET_free(ret);
507 errno = eno; 507 errno = eno;
508 return NULL; 508 return NULL;
509 } 509 }
510 if (GNUNET_OK != 510 if (GNUNET_OK !=
511 initialize_network_handle (ret, 511 initialize_network_handle(ret,
512 (NULL != address) ? address->sa_family : desc->af, 512 (NULL != address) ? address->sa_family : desc->af,
513 SOCK_STREAM)) 513 SOCK_STREAM))
514 { 514 {
515 return NULL; 515 return NULL;
516 } 516 }
517 return ret; 517 return ret;
518} 518}
519 519
@@ -527,9 +527,9 @@ GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
527 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 527 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
528 */ 528 */
529int 529int
530GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc, 530GNUNET_NETWORK_socket_bind(struct GNUNET_NETWORK_Handle *desc,
531 const struct sockaddr *address, 531 const struct sockaddr *address,
532 socklen_t address_len) 532 socklen_t address_len)
533{ 533{
534 int ret; 534 int ret;
535 535
@@ -539,63 +539,63 @@ GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
539 const int on = 1; 539 const int on = 1;
540 540
541 if (AF_INET6 == desc->af) 541 if (AF_INET6 == desc->af)
542 if (setsockopt (desc->fd, 542 if (setsockopt(desc->fd,
543 IPPROTO_IPV6, 543 IPPROTO_IPV6,
544 IPV6_V6ONLY, 544 IPV6_V6ONLY,
545 (const void *) &on, 545 (const void *)&on,
546 sizeof (on))) 546 sizeof(on)))
547 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, 547 LOG_STRERROR(GNUNET_ERROR_TYPE_DEBUG,
548 "setsockopt"); 548 "setsockopt");
549 } 549 }
550#endif 550#endif
551#endif 551#endif
552#ifndef WINDOWS 552#ifndef WINDOWS
553 if (AF_UNIX == address->sa_family) 553 if (AF_UNIX == address->sa_family)
554 GNUNET_NETWORK_unix_precheck ((const struct sockaddr_un *) address); 554 GNUNET_NETWORK_unix_precheck((const struct sockaddr_un *)address);
555 { 555 {
556 const int on = 1; 556 const int on = 1;
557 557
558 /* This is required here for TCP sockets, but only on UNIX */ 558 /* This is required here for TCP sockets, but only on UNIX */
559 if ( (SOCK_STREAM == desc->type) && 559 if ((SOCK_STREAM == desc->type) &&
560 (0 != setsockopt (desc->fd, 560 (0 != setsockopt(desc->fd,
561 SOL_SOCKET, 561 SOL_SOCKET,
562 SO_REUSEADDR, 562 SO_REUSEADDR,
563 &on, sizeof (on)))) 563 &on, sizeof(on))))
564 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, 564 LOG_STRERROR(GNUNET_ERROR_TYPE_DEBUG,
565 "setsockopt"); 565 "setsockopt");
566 } 566 }
567 { 567 {
568 /* set permissions of newly created non-abstract UNIX domain socket to 568 /* set permissions of newly created non-abstract UNIX domain socket to
569 "user-only"; applications can choose to relax this later */ 569 "user-only"; applications can choose to relax this later */
570 mode_t old_mask = 0; /* assigned to make compiler happy */ 570 mode_t old_mask = 0; /* assigned to make compiler happy */
571 const struct sockaddr_un *un = (const struct sockaddr_un *) address; 571 const struct sockaddr_un *un = (const struct sockaddr_un *)address;
572 int not_abstract = 0; 572 int not_abstract = 0;
573 573
574 if ((AF_UNIX == address->sa_family) 574 if ((AF_UNIX == address->sa_family)
575 && ('\0' != un->sun_path[0]) ) /* Not an abstract socket */ 575 && ('\0' != un->sun_path[0])) /* Not an abstract socket */
576 not_abstract = 1; 576 not_abstract = 1;
577 if (not_abstract) 577 if (not_abstract)
578 old_mask = umask (S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH | S_IXOTH); 578 old_mask = umask(S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH | S_IXOTH);
579#endif 579#endif
580 580
581 ret = bind (desc->fd, 581 ret = bind(desc->fd,
582 address, 582 address,
583 address_len); 583 address_len);
584 584
585#ifndef WINDOWS 585#ifndef WINDOWS
586 if (not_abstract) 586 if (not_abstract)
587 (void) umask (old_mask); 587 (void)umask(old_mask);
588 } 588}
589#endif 589#endif
590#ifdef MINGW 590#ifdef MINGW
591 if (SOCKET_ERROR == ret) 591 if (SOCKET_ERROR == ret)
592 SetErrnoFromWinsockError (WSAGetLastError ()); 592 SetErrnoFromWinsockError(WSAGetLastError());
593#endif 593#endif
594 if (0 != ret) 594 if (0 != ret)
595 return GNUNET_SYSERR; 595 return GNUNET_SYSERR;
596#ifndef MINGW 596#ifndef MINGW
597 desc->addr = GNUNET_malloc (address_len); 597 desc->addr = GNUNET_malloc(address_len);
598 GNUNET_memcpy (desc->addr, address, address_len); 598 GNUNET_memcpy(desc->addr, address, address_len);
599 desc->addrlen = address_len; 599 desc->addrlen = address_len;
600#endif 600#endif
601 return GNUNET_OK; 601 return GNUNET_OK;
@@ -609,72 +609,73 @@ GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,
609 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 609 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
610 */ 610 */
611int 611int
612GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc) 612GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
613{ 613{
614 int ret; 614 int ret;
615 615
616#ifdef WINDOWS 616#ifdef WINDOWS
617 DWORD error = 0; 617 DWORD error = 0;
618 618
619 SetLastError (0); 619 SetLastError(0);
620 ret = closesocket (desc->fd); 620 ret = closesocket(desc->fd);
621 error = WSAGetLastError (); 621 error = WSAGetLastError();
622 SetErrnoFromWinsockError (error); 622 SetErrnoFromWinsockError(error);
623 LOG (GNUNET_ERROR_TYPE_DEBUG, 623 LOG(GNUNET_ERROR_TYPE_DEBUG,
624 "Closed 0x%x, closesocket() returned %d, GLE is %u\n", 624 "Closed 0x%x, closesocket() returned %d, GLE is %u\n",
625 desc->fd, 625 desc->fd,
626 ret, 626 ret,
627 error); 627 error);
628#else 628#else
629 ret = close (desc->fd); 629 ret = close(desc->fd);
630#endif 630#endif
631#ifndef WINDOWS 631#ifndef WINDOWS
632 const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr; 632 const struct sockaddr_un *un = (const struct sockaddr_un *)desc->addr;
633 633
634 /* Cleanup the UNIX domain socket and its parent directories in case of non 634 /* Cleanup the UNIX domain socket and its parent directories in case of non
635 abstract sockets */ 635 abstract sockets */
636 if ( (AF_UNIX == desc->af) && 636 if ((AF_UNIX == desc->af) &&
637 (NULL != desc->addr) && 637 (NULL != desc->addr) &&
638 ('\0' != un->sun_path[0]) ) 638 ('\0' != un->sun_path[0]))
639 {
640 char *dirname = GNUNET_strndup (un->sun_path,
641 sizeof (un->sun_path));
642
643 if (0 != unlink (dirname))
644 {
645 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
646 "unlink",
647 dirname);
648 }
649 else
650 { 639 {
651 size_t len; 640 char *dirname = GNUNET_strndup(un->sun_path,
652 641 sizeof(un->sun_path));
653 len = strlen (dirname); 642
654 while ((len > 0) && (dirname[len] != DIR_SEPARATOR)) 643 if (0 != unlink(dirname))
655 len--;
656 dirname[len] = '\0';
657 if ((0 != len) && (0 != rmdir (dirname)))
658 {
659 switch (errno)
660 { 644 {
661 case EACCES: 645 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING,
662 case ENOTEMPTY: 646 "unlink",
663 case EPERM: 647 dirname);
664 /* these are normal and can just be ignored */
665 break;
666 default:
667 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
668 "rmdir",
669 dirname);
670 break;
671 } 648 }
672 } 649 else
650 {
651 size_t len;
652
653 len = strlen(dirname);
654 while ((len > 0) && (dirname[len] != DIR_SEPARATOR))
655 len--;
656 dirname[len] = '\0';
657 if ((0 != len) && (0 != rmdir(dirname)))
658 {
659 switch (errno)
660 {
661 case EACCES:
662 case ENOTEMPTY:
663 case EPERM:
664 /* these are normal and can just be ignored */
665 break;
666
667 default:
668 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING,
669 "rmdir",
670 dirname);
671 break;
672 }
673 }
674 }
675 GNUNET_free(dirname);
673 } 676 }
674 GNUNET_free (dirname);
675 }
676#endif 677#endif
677 GNUNET_NETWORK_socket_free_memory_only_ (desc); 678 GNUNET_NETWORK_socket_free_memory_only_(desc);
678 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR; 679 return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR;
679} 680}
680 681
@@ -685,10 +686,10 @@ GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)
685 * @param desc socket 686 * @param desc socket
686 */ 687 */
687void 688void
688GNUNET_NETWORK_socket_free_memory_only_ (struct GNUNET_NETWORK_Handle *desc) 689GNUNET_NETWORK_socket_free_memory_only_(struct GNUNET_NETWORK_Handle *desc)
689{ 690{
690 GNUNET_free_non_null (desc->addr); 691 GNUNET_free_non_null(desc->addr);
691 GNUNET_free (desc); 692 GNUNET_free(desc);
692} 693}
693 694
694 695
@@ -699,27 +700,28 @@ GNUNET_NETWORK_socket_free_memory_only_ (struct GNUNET_NETWORK_Handle *desc)
699 * @return NULL on error (including not supported on target platform) 700 * @return NULL on error (including not supported on target platform)
700 */ 701 */
701struct GNUNET_NETWORK_Handle * 702struct GNUNET_NETWORK_Handle *
702GNUNET_NETWORK_socket_box_native (SOCKTYPE fd) 703GNUNET_NETWORK_socket_box_native(SOCKTYPE fd)
703{ 704{
704 struct GNUNET_NETWORK_Handle *ret; 705 struct GNUNET_NETWORK_Handle *ret;
706
705#if MINGW 707#if MINGW
706 unsigned long i; 708 unsigned long i;
707 DWORD d; 709 DWORD d;
708 /* FIXME: Find a better call to check that FD is valid */ 710 /* FIXME: Find a better call to check that FD is valid */
709 if (0 != 711 if (0 !=
710 WSAIoctl (fd, FIONBIO, 712 WSAIoctl(fd, FIONBIO,
711 (void *) &i, sizeof (i), 713 (void *)&i, sizeof(i),
712 NULL, 0, &d, 714 NULL, 0, &d,
713 NULL, NULL)) 715 NULL, NULL))
714 return NULL; /* invalid FD */ 716 return NULL; /* invalid FD */
715 ret = GNUNET_new (struct GNUNET_NETWORK_Handle); 717 ret = GNUNET_new(struct GNUNET_NETWORK_Handle);
716 ret->fd = fd; 718 ret->fd = fd;
717 ret->af = AF_UNSPEC; 719 ret->af = AF_UNSPEC;
718 return ret; 720 return ret;
719#else 721#else
720 if (fcntl (fd, F_GETFD) < 0) 722 if (fcntl(fd, F_GETFD) < 0)
721 return NULL; /* invalid FD */ 723 return NULL; /* invalid FD */
722 ret = GNUNET_new (struct GNUNET_NETWORK_Handle); 724 ret = GNUNET_new(struct GNUNET_NETWORK_Handle);
723 ret->fd = fd; 725 ret->fd = fd;
724 ret->af = AF_UNSPEC; 726 ret->af = AF_UNSPEC;
725 return ret; 727 return ret;
@@ -736,22 +738,22 @@ GNUNET_NETWORK_socket_box_native (SOCKTYPE fd)
736 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 738 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
737 */ 739 */
738int 740int
739GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc, 741GNUNET_NETWORK_socket_connect(const struct GNUNET_NETWORK_Handle *desc,
740 const struct sockaddr *address, 742 const struct sockaddr *address,
741 socklen_t address_len) 743 socklen_t address_len)
742{ 744{
743 int ret; 745 int ret;
744 746
745 ret = connect (desc->fd, 747 ret = connect(desc->fd,
746 address, 748 address,
747 address_len); 749 address_len);
748#ifdef MINGW 750#ifdef MINGW
749 if (SOCKET_ERROR == ret) 751 if (SOCKET_ERROR == ret)
750 { 752 {
751 SetErrnoFromWinsockError (WSAGetLastError ()); 753 SetErrnoFromWinsockError(WSAGetLastError());
752 if (errno == EWOULDBLOCK) 754 if (errno == EWOULDBLOCK)
753 errno = EINPROGRESS; 755 errno = EINPROGRESS;
754 } 756 }
755#endif 757#endif
756 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 758 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
757} 759}
@@ -768,26 +770,26 @@ GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,
768 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 770 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
769 */ 771 */
770int 772int
771GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc, 773GNUNET_NETWORK_socket_getsockopt(const struct GNUNET_NETWORK_Handle *desc,
772 int level, 774 int level,
773 int optname, 775 int optname,
774 void *optval, 776 void *optval,
775 socklen_t *optlen) 777 socklen_t *optlen)
776{ 778{
777 int ret; 779 int ret;
778 780
779 ret = getsockopt (desc->fd, 781 ret = getsockopt(desc->fd,
780 level, 782 level,
781 optname, 783 optname,
782 optval, optlen); 784 optval, optlen);
783 785
784#ifdef MINGW 786#ifdef MINGW
785 if ( (0 == ret) && 787 if ((0 == ret) &&
786 (SOL_SOCKET == level) && 788 (SOL_SOCKET == level) &&
787 (SO_ERROR == optname) ) 789 (SO_ERROR == optname))
788 *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval)); 790 *((int *)optval) = GetErrnoFromWinsockError(*((int *)optval));
789 else if (SOCKET_ERROR == ret) 791 else if (SOCKET_ERROR == ret)
790 SetErrnoFromWinsockError (WSAGetLastError ()); 792 SetErrnoFromWinsockError(WSAGetLastError());
791#endif 793#endif
792 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 794 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
793} 795}
@@ -801,16 +803,16 @@ GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,
801 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 803 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
802 */ 804 */
803int 805int
804GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc, 806GNUNET_NETWORK_socket_listen(const struct GNUNET_NETWORK_Handle *desc,
805 int backlog) 807 int backlog)
806{ 808{
807 int ret; 809 int ret;
808 810
809 ret = listen (desc->fd, 811 ret = listen(desc->fd,
810 backlog); 812 backlog);
811#ifdef MINGW 813#ifdef MINGW
812 if (SOCKET_ERROR == ret) 814 if (SOCKET_ERROR == ret)
813 SetErrnoFromWinsockError (WSAGetLastError ()); 815 SetErrnoFromWinsockError(WSAGetLastError());
814#endif 816#endif
815 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 817 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
816} 818}
@@ -823,7 +825,7 @@ GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,
823 * @returns #GNUNET_SYSERR if no data is available, or on error! 825 * @returns #GNUNET_SYSERR if no data is available, or on error!
824 */ 826 */
825ssize_t 827ssize_t
826GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *desc) 828GNUNET_NETWORK_socket_recvfrom_amount(const struct GNUNET_NETWORK_Handle *desc)
827{ 829{
828 int error; 830 int error;
829 831
@@ -831,20 +833,20 @@ GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *desc)
831#ifndef WINDOWS 833#ifndef WINDOWS
832 int pending; 834 int pending;
833 835
834 error = ioctl (desc->fd, 836 error = ioctl(desc->fd,
835 FIONREAD, 837 FIONREAD,
836 &pending); 838 &pending);
837 if (0 == error) 839 if (0 == error)
838 return (ssize_t) pending; 840 return (ssize_t)pending;
839 return GNUNET_SYSERR; 841 return GNUNET_SYSERR;
840#else 842#else
841 u_long pending; 843 u_long pending;
842 844
843 error = ioctlsocket (desc->fd, 845 error = ioctlsocket(desc->fd,
844 FIONREAD, 846 FIONREAD,
845 &pending); 847 &pending);
846 if (error != SOCKET_ERROR) 848 if (error != SOCKET_ERROR)
847 return (ssize_t) pending; 849 return (ssize_t)pending;
848 return GNUNET_SYSERR; 850 return GNUNET_SYSERR;
849#endif 851#endif
850} 852}
@@ -861,11 +863,11 @@ GNUNET_NETWORK_socket_recvfrom_amount (const struct GNUNET_NETWORK_Handle *desc)
861 * @param addrlen length of the @a src_addr 863 * @param addrlen length of the @a src_addr
862 */ 864 */
863ssize_t 865ssize_t
864GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc, 866GNUNET_NETWORK_socket_recvfrom(const struct GNUNET_NETWORK_Handle *desc,
865 void *buffer, 867 void *buffer,
866 size_t length, 868 size_t length,
867 struct sockaddr *src_addr, 869 struct sockaddr *src_addr,
868 socklen_t *addrlen) 870 socklen_t *addrlen)
869{ 871{
870 int ret; 872 int ret;
871 int flags; 873 int flags;
@@ -874,17 +876,16 @@ GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
874 876
875#ifdef MSG_DONTWAIT 877#ifdef MSG_DONTWAIT
876 flags |= MSG_DONTWAIT; 878 flags |= MSG_DONTWAIT;
877
878#endif 879#endif
879 ret = recvfrom (desc->fd, 880 ret = recvfrom(desc->fd,
880 buffer, 881 buffer,
881 length, 882 length,
882 flags, 883 flags,
883 src_addr, 884 src_addr,
884 addrlen); 885 addrlen);
885#ifdef MINGW 886#ifdef MINGW
886 if (SOCKET_ERROR == ret) 887 if (SOCKET_ERROR == ret)
887 SetErrnoFromWinsockError (WSAGetLastError ()); 888 SetErrnoFromWinsockError(WSAGetLastError());
888#endif 889#endif
889 return ret; 890 return ret;
890} 891}
@@ -899,9 +900,9 @@ GNUNET_NETWORK_socket_recvfrom (const struct GNUNET_NETWORK_Handle *desc,
899 * @return number of bytes received, -1 on error 900 * @return number of bytes received, -1 on error
900 */ 901 */
901ssize_t 902ssize_t
902GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc, 903GNUNET_NETWORK_socket_recv(const struct GNUNET_NETWORK_Handle *desc,
903 void *buffer, 904 void *buffer,
904 size_t length) 905 size_t length)
905{ 906{
906 int ret; 907 int ret;
907 int flags; 908 int flags;
@@ -911,13 +912,13 @@ GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
911#ifdef MSG_DONTWAIT 912#ifdef MSG_DONTWAIT
912 flags |= MSG_DONTWAIT; 913 flags |= MSG_DONTWAIT;
913#endif 914#endif
914 ret = recv (desc->fd, 915 ret = recv(desc->fd,
915 buffer, 916 buffer,
916 length, 917 length,
917 flags); 918 flags);
918#ifdef MINGW 919#ifdef MINGW
919 if (SOCKET_ERROR == ret) 920 if (SOCKET_ERROR == ret)
920 SetErrnoFromWinsockError (WSAGetLastError ()); 921 SetErrnoFromWinsockError(WSAGetLastError());
921#endif 922#endif
922 return ret; 923 return ret;
923} 924}
@@ -932,9 +933,9 @@ GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle *desc,
932 * @return number of bytes sent, #GNUNET_SYSERR on error 933 * @return number of bytes sent, #GNUNET_SYSERR on error
933 */ 934 */
934ssize_t 935ssize_t
935GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc, 936GNUNET_NETWORK_socket_send(const struct GNUNET_NETWORK_Handle *desc,
936 const void *buffer, 937 const void *buffer,
937 size_t length) 938 size_t length)
938{ 939{
939 int ret; 940 int ret;
940 int flags; 941 int flags;
@@ -942,20 +943,17 @@ GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
942 flags = 0; 943 flags = 0;
943#ifdef MSG_DONTWAIT 944#ifdef MSG_DONTWAIT
944 flags |= MSG_DONTWAIT; 945 flags |= MSG_DONTWAIT;
945
946#endif 946#endif
947#ifdef MSG_NOSIGNAL 947#ifdef MSG_NOSIGNAL
948 flags |= MSG_NOSIGNAL; 948 flags |= MSG_NOSIGNAL;
949
950#endif 949#endif
951 ret = send (desc->fd, 950 ret = send(desc->fd,
952 buffer, 951 buffer,
953 length, 952 length,
954 flags); 953 flags);
955#ifdef MINGW 954#ifdef MINGW
956 if (SOCKET_ERROR == ret) 955 if (SOCKET_ERROR == ret)
957 SetErrnoFromWinsockError (WSAGetLastError ()); 956 SetErrnoFromWinsockError(WSAGetLastError());
958
959#endif 957#endif
960 return ret; 958 return ret;
961} 959}
@@ -973,11 +971,11 @@ GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle *desc,
973 * @return number of bytes sent, #GNUNET_SYSERR on error 971 * @return number of bytes sent, #GNUNET_SYSERR on error
974 */ 972 */
975ssize_t 973ssize_t
976GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc, 974GNUNET_NETWORK_socket_sendto(const struct GNUNET_NETWORK_Handle *desc,
977 const void *message, 975 const void *message,
978 size_t length, 976 size_t length,
979 const struct sockaddr *dest_addr, 977 const struct sockaddr *dest_addr,
980 socklen_t dest_len) 978 socklen_t dest_len)
981{ 979{
982 int ret; 980 int ret;
983 int flags; 981 int flags;
@@ -990,10 +988,10 @@ GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
990#ifdef MSG_NOSIGNAL 988#ifdef MSG_NOSIGNAL
991 flags |= MSG_NOSIGNAL; 989 flags |= MSG_NOSIGNAL;
992#endif 990#endif
993 ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len); 991 ret = sendto(desc->fd, message, length, flags, dest_addr, dest_len);
994#ifdef MINGW 992#ifdef MINGW
995 if (SOCKET_ERROR == ret) 993 if (SOCKET_ERROR == ret)
996 SetErrnoFromWinsockError (WSAGetLastError ()); 994 SetErrnoFromWinsockError(WSAGetLastError());
997#endif 995#endif
998 return ret; 996 return ret;
999} 997}
@@ -1010,22 +1008,22 @@ GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle *desc,
1010 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 1008 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1011 */ 1009 */
1012int 1010int
1013GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd, 1011GNUNET_NETWORK_socket_setsockopt(struct GNUNET_NETWORK_Handle *fd,
1014 int level, 1012 int level,
1015 int option_name, 1013 int option_name,
1016 const void *option_value, 1014 const void *option_value,
1017 socklen_t option_len) 1015 socklen_t option_len)
1018{ 1016{
1019 int ret; 1017 int ret;
1020 1018
1021 ret = setsockopt (fd->fd, 1019 ret = setsockopt(fd->fd,
1022 level, 1020 level,
1023 option_name, 1021 option_name,
1024 option_value, 1022 option_value,
1025 option_len); 1023 option_len);
1026#ifdef MINGW 1024#ifdef MINGW
1027 if (SOCKET_ERROR == ret) 1025 if (SOCKET_ERROR == ret)
1028 SetErrnoFromWinsockError (WSAGetLastError ()); 1026 SetErrnoFromWinsockError(WSAGetLastError());
1029#endif 1027#endif
1030 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 1028 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
1031} 1029}
@@ -1042,22 +1040,22 @@ GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,
1042 * @return new socket, NULL on error 1040 * @return new socket, NULL on error
1043 */ 1041 */
1044struct GNUNET_NETWORK_Handle * 1042struct GNUNET_NETWORK_Handle *
1045GNUNET_NETWORK_socket_create (int domain, 1043GNUNET_NETWORK_socket_create(int domain,
1046 int type, 1044 int type,
1047 int protocol) 1045 int protocol)
1048{ 1046{
1049 struct GNUNET_NETWORK_Handle *ret; 1047 struct GNUNET_NETWORK_Handle *ret;
1050 int fd; 1048 int fd;
1051 1049
1052 fd = socket (domain, type, protocol); 1050 fd = socket(domain, type, protocol);
1053 if (-1 == fd) 1051 if (-1 == fd)
1054 return NULL; 1052 return NULL;
1055 ret = GNUNET_new (struct GNUNET_NETWORK_Handle); 1053 ret = GNUNET_new(struct GNUNET_NETWORK_Handle);
1056 ret->fd = fd; 1054 ret->fd = fd;
1057 if (GNUNET_OK != 1055 if (GNUNET_OK !=
1058 initialize_network_handle (ret, 1056 initialize_network_handle(ret,
1059 domain, 1057 domain,
1060 type)) 1058 type))
1061 return NULL; 1059 return NULL;
1062 return ret; 1060 return ret;
1063} 1061}
@@ -1070,15 +1068,15 @@ GNUNET_NETWORK_socket_create (int domain,
1070 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 1068 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1071 */ 1069 */
1072int 1070int
1073GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, 1071GNUNET_NETWORK_socket_shutdown(struct GNUNET_NETWORK_Handle *desc,
1074 int how) 1072 int how)
1075{ 1073{
1076 int ret; 1074 int ret;
1077 1075
1078 ret = shutdown (desc->fd, how); 1076 ret = shutdown(desc->fd, how);
1079#ifdef MINGW 1077#ifdef MINGW
1080 if (0 != ret) 1078 if (0 != ret)
1081 SetErrnoFromWinsockError (WSAGetLastError ()); 1079 SetErrnoFromWinsockError(WSAGetLastError());
1082#endif 1080#endif
1083 return (0 == ret) ? GNUNET_OK : GNUNET_SYSERR; 1081 return (0 == ret) ? GNUNET_OK : GNUNET_SYSERR;
1084} 1082}
@@ -1094,7 +1092,7 @@ GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,
1094 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise 1092 * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
1095 */ 1093 */
1096int 1094int
1097GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc) 1095GNUNET_NETWORK_socket_disable_corking(struct GNUNET_NETWORK_Handle *desc)
1098{ 1096{
1099 int ret = 0; 1097 int ret = 0;
1100 1098
@@ -1103,43 +1101,43 @@ GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
1103 1101
1104 if (0 != 1102 if (0 !=
1105 (ret = 1103 (ret =
1106 setsockopt (desc->fd, 1104 setsockopt(desc->fd,
1107 SOL_SOCKET, 1105 SOL_SOCKET,
1108 SO_SNDBUF, 1106 SO_SNDBUF,
1109 (char *) &value, 1107 (char *)&value,
1110 sizeof (value)))) 1108 sizeof(value))))
1111 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, 1109 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING,
1112 "setsockopt"); 1110 "setsockopt");
1113 if (0 != 1111 if (0 !=
1114 (ret = 1112 (ret =
1115 setsockopt (desc->fd, 1113 setsockopt(desc->fd,
1116 SOL_SOCKET, 1114 SOL_SOCKET,
1117 SO_RCVBUF, 1115 SO_RCVBUF,
1118 (char *) &value, 1116 (char *)&value,
1119 sizeof (value)))) 1117 sizeof(value))))
1120 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, 1118 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING,
1121 "setsockopt"); 1119 "setsockopt");
1122#elif LINUX 1120#elif LINUX
1123 int value = 0; 1121 int value = 0;
1124 1122
1125 if (0 != 1123 if (0 !=
1126 (ret = 1124 (ret =
1127 setsockopt (desc->fd, 1125 setsockopt(desc->fd,
1128 SOL_SOCKET, 1126 SOL_SOCKET,
1129 SO_SNDBUF, 1127 SO_SNDBUF,
1130 &value, 1128 &value,
1131 sizeof (value)))) 1129 sizeof(value))))
1132 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, 1130 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING,
1133 "setsockopt"); 1131 "setsockopt");
1134 if (0 != 1132 if (0 !=
1135 (ret = 1133 (ret =
1136 setsockopt (desc->fd, 1134 setsockopt(desc->fd,
1137 SOL_SOCKET, 1135 SOL_SOCKET,
1138 SO_RCVBUF, 1136 SO_RCVBUF,
1139 &value, 1137 &value,
1140 sizeof (value)))) 1138 sizeof(value))))
1141 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, 1139 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING,
1142 "setsockopt"); 1140 "setsockopt");
1143#endif 1141#endif
1144 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; 1142 return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;
1145} 1143}
@@ -1151,9 +1149,9 @@ GNUNET_NETWORK_socket_disable_corking (struct GNUNET_NETWORK_Handle *desc)
1151 * @param fds fd set 1149 * @param fds fd set
1152 */ 1150 */
1153void 1151void
1154GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds) 1152GNUNET_NETWORK_fdset_zero(struct GNUNET_NETWORK_FDSet *fds)
1155{ 1153{
1156 FD_ZERO (&fds->sds); 1154 FD_ZERO(&fds->sds);
1157 fds->nsds = 0; 1155 fds->nsds = 0;
1158#ifdef MINGW 1156#ifdef MINGW
1159 fds->handles_pos = 0; 1157 fds->handles_pos = 0;
@@ -1168,13 +1166,13 @@ GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)
1168 * @param desc socket to add 1166 * @param desc socket to add
1169 */ 1167 */
1170void 1168void
1171GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds, 1169GNUNET_NETWORK_fdset_set(struct GNUNET_NETWORK_FDSet *fds,
1172 const struct GNUNET_NETWORK_Handle *desc) 1170 const struct GNUNET_NETWORK_Handle *desc)
1173{ 1171{
1174 FD_SET (desc->fd, 1172 FD_SET(desc->fd,
1175 &fds->sds); 1173 &fds->sds);
1176 fds->nsds = GNUNET_MAX (fds->nsds, 1174 fds->nsds = GNUNET_MAX(fds->nsds,
1177 desc->fd + 1); 1175 desc->fd + 1);
1178} 1176}
1179 1177
1180 1178
@@ -1186,11 +1184,11 @@ GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,
1186 * @return 0 if the FD is not set 1184 * @return 0 if the FD is not set
1187 */ 1185 */
1188int 1186int
1189GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds, 1187GNUNET_NETWORK_fdset_isset(const struct GNUNET_NETWORK_FDSet *fds,
1190 const struct GNUNET_NETWORK_Handle *desc) 1188 const struct GNUNET_NETWORK_Handle *desc)
1191{ 1189{
1192 return FD_ISSET (desc->fd, 1190 return FD_ISSET(desc->fd,
1193 &fds->sds); 1191 &fds->sds);
1194} 1192}
1195 1193
1196 1194
@@ -1201,17 +1199,17 @@ GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,
1201 * @param src the fd set to add from 1199 * @param src the fd set to add from
1202 */ 1200 */
1203void 1201void
1204GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst, 1202GNUNET_NETWORK_fdset_add(struct GNUNET_NETWORK_FDSet *dst,
1205 const struct GNUNET_NETWORK_FDSet *src) 1203 const struct GNUNET_NETWORK_FDSet *src)
1206{ 1204{
1207#ifndef MINGW 1205#ifndef MINGW
1208 int nfds; 1206 int nfds;
1209 1207
1210 for (nfds = src->nsds; nfds >= 0; nfds--) 1208 for (nfds = src->nsds; nfds >= 0; nfds--)
1211 if (FD_ISSET (nfds, &src->sds)) 1209 if (FD_ISSET(nfds, &src->sds))
1212 FD_SET (nfds, &dst->sds); 1210 FD_SET(nfds, &dst->sds);
1213 dst->nsds = GNUNET_MAX (dst->nsds, 1211 dst->nsds = GNUNET_MAX(dst->nsds,
1214 src->nsds); 1212 src->nsds);
1215#else 1213#else
1216 /* This is MinGW32-specific implementation that relies on the code that 1214 /* This is MinGW32-specific implementation that relies on the code that
1217 * winsock2.h defines for FD_SET. Namely, it relies on FD_SET checking 1215 * winsock2.h defines for FD_SET. Namely, it relies on FD_SET checking
@@ -1224,16 +1222,16 @@ GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
1224 unsigned int i; 1222 unsigned int i;
1225 1223
1226 for (i = 0; i < src->sds.fd_count; i++) 1224 for (i = 0; i < src->sds.fd_count; i++)
1227 FD_SET (src->sds.fd_array[i], 1225 FD_SET(src->sds.fd_array[i],
1228 &dst->sds); 1226 &dst->sds);
1229 dst->nsds = GNUNET_MAX (src->nsds, 1227 dst->nsds = GNUNET_MAX(src->nsds,
1230 dst->nsds); 1228 dst->nsds);
1231 1229
1232 /* also copy over `struct GNUNET_DISK_FileHandle` array */ 1230 /* also copy over `struct GNUNET_DISK_FileHandle` array */
1233 if (dst->handles_pos + src->handles_pos > dst->handles_size) 1231 if (dst->handles_pos + src->handles_pos > dst->handles_size)
1234 GNUNET_array_grow (dst->handles, 1232 GNUNET_array_grow(dst->handles,
1235 dst->handles_size, 1233 dst->handles_size,
1236 ((dst->handles_pos + src->handles_pos) << 1)); 1234 ((dst->handles_pos + src->handles_pos) << 1));
1237 for (i = 0; i < src->handles_pos; i++) 1235 for (i = 0; i < src->handles_pos; i++)
1238 dst->handles[dst->handles_pos++] = src->handles[i]; 1236 dst->handles[dst->handles_pos++] = src->handles[i];
1239#endif 1237#endif
@@ -1247,20 +1245,20 @@ GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,
1247 * @param from source 1245 * @param from source
1248 */ 1246 */
1249void 1247void
1250GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to, 1248GNUNET_NETWORK_fdset_copy(struct GNUNET_NETWORK_FDSet *to,
1251 const struct GNUNET_NETWORK_FDSet *from) 1249 const struct GNUNET_NETWORK_FDSet *from)
1252{ 1250{
1253 FD_COPY (&from->sds, 1251 FD_COPY(&from->sds,
1254 &to->sds); 1252 &to->sds);
1255 to->nsds = from->nsds; 1253 to->nsds = from->nsds;
1256#ifdef MINGW 1254#ifdef MINGW
1257 if (from->handles_pos > to->handles_size) 1255 if (from->handles_pos > to->handles_size)
1258 GNUNET_array_grow (to->handles, 1256 GNUNET_array_grow(to->handles,
1259 to->handles_size, 1257 to->handles_size,
1260 from->handles_pos * 2); 1258 from->handles_pos * 2);
1261 GNUNET_memcpy (to->handles, 1259 GNUNET_memcpy(to->handles,
1262 from->handles, 1260 from->handles,
1263 from->handles_pos * sizeof (struct GNUNET_NETWORK_Handle *)); 1261 from->handles_pos * sizeof(struct GNUNET_NETWORK_Handle *));
1264 to->handles_pos = from->handles_pos; 1262 to->handles_pos = from->handles_pos;
1265#endif 1263#endif
1266} 1264}
@@ -1273,7 +1271,7 @@ GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,
1273 * @return POSIX file descriptor 1271 * @return POSIX file descriptor
1274 */ 1272 */
1275int 1273int
1276GNUNET_NETWORK_get_fd (const struct GNUNET_NETWORK_Handle *desc) 1274GNUNET_NETWORK_get_fd(const struct GNUNET_NETWORK_Handle *desc)
1277{ 1275{
1278 return desc->fd; 1276 return desc->fd;
1279} 1277}
@@ -1286,7 +1284,7 @@ GNUNET_NETWORK_get_fd (const struct GNUNET_NETWORK_Handle *desc)
1286 * @return sockaddr 1284 * @return sockaddr
1287 */ 1285 */
1288struct sockaddr* 1286struct sockaddr*
1289GNUNET_NETWORK_get_addr (const struct GNUNET_NETWORK_Handle *desc) 1287GNUNET_NETWORK_get_addr(const struct GNUNET_NETWORK_Handle *desc)
1290{ 1288{
1291 return desc->addr; 1289 return desc->addr;
1292} 1290}
@@ -1299,7 +1297,7 @@ GNUNET_NETWORK_get_addr (const struct GNUNET_NETWORK_Handle *desc)
1299 * @return socklen_t for sockaddr 1297 * @return socklen_t for sockaddr
1300 */ 1298 */
1301socklen_t 1299socklen_t
1302GNUNET_NETWORK_get_addrlen (const struct GNUNET_NETWORK_Handle *desc) 1300GNUNET_NETWORK_get_addrlen(const struct GNUNET_NETWORK_Handle *desc)
1303{ 1301{
1304 return desc->addrlen; 1302 return desc->addrlen;
1305} 1303}
@@ -1313,12 +1311,12 @@ GNUNET_NETWORK_get_addrlen (const struct GNUNET_NETWORK_Handle *desc)
1313 * @param nfds the biggest socket number in from + 1 1311 * @param nfds the biggest socket number in from + 1
1314 */ 1312 */
1315void 1313void
1316GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to, 1314GNUNET_NETWORK_fdset_copy_native(struct GNUNET_NETWORK_FDSet *to,
1317 const fd_set *from, 1315 const fd_set *from,
1318 int nfds) 1316 int nfds)
1319{ 1317{
1320 FD_COPY (from, 1318 FD_COPY(from,
1321 &to->sds); 1319 &to->sds);
1322 to->nsds = nfds; 1320 to->nsds = nfds;
1323} 1321}
1324 1322
@@ -1330,13 +1328,13 @@ GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,
1330 * @param nfd native FD to set 1328 * @param nfd native FD to set
1331 */ 1329 */
1332void 1330void
1333GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to, 1331GNUNET_NETWORK_fdset_set_native(struct GNUNET_NETWORK_FDSet *to,
1334 int nfd) 1332 int nfd)
1335{ 1333{
1336 GNUNET_assert ((nfd >= 0) && (nfd < FD_SETSIZE)); 1334 GNUNET_assert((nfd >= 0) && (nfd < FD_SETSIZE));
1337 FD_SET (nfd, &to->sds); 1335 FD_SET(nfd, &to->sds);
1338 to->nsds = GNUNET_MAX (nfd + 1, 1336 to->nsds = GNUNET_MAX(nfd + 1,
1339 to->nsds); 1337 to->nsds);
1340} 1338}
1341 1339
1342 1340
@@ -1348,13 +1346,13 @@ GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
1348 * @return #GNUNET_YES if FD is set in the set 1346 * @return #GNUNET_YES if FD is set in the set
1349 */ 1347 */
1350int 1348int
1351GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to, 1349GNUNET_NETWORK_fdset_test_native(const struct GNUNET_NETWORK_FDSet *to,
1352 int nfd) 1350 int nfd)
1353{ 1351{
1354 if ( (-1 == nfd) || 1352 if ((-1 == nfd) ||
1355 (NULL == to) ) 1353 (NULL == to))
1356 return GNUNET_NO; 1354 return GNUNET_NO;
1357 return FD_ISSET (nfd, &to->sds) ? GNUNET_YES : GNUNET_NO; 1355 return FD_ISSET(nfd, &to->sds) ? GNUNET_YES : GNUNET_NO;
1358} 1356}
1359 1357
1360 1358
@@ -1364,26 +1362,26 @@ GNUNET_NETWORK_fdset_test_native (const struct GNUNET_NETWORK_FDSet *to,
1364 * @param h the file handle to add 1362 * @param h the file handle to add
1365 */ 1363 */
1366void 1364void
1367GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds, 1365GNUNET_NETWORK_fdset_handle_set(struct GNUNET_NETWORK_FDSet *fds,
1368 const struct GNUNET_DISK_FileHandle *h) 1366 const struct GNUNET_DISK_FileHandle *h)
1369{ 1367{
1370#ifdef MINGW 1368#ifdef MINGW
1371 if (fds->handles_pos == fds->handles_size) 1369 if (fds->handles_pos == fds->handles_size)
1372 GNUNET_array_grow (fds->handles, 1370 GNUNET_array_grow(fds->handles,
1373 fds->handles_size, 1371 fds->handles_size,
1374 fds->handles_size * 2 + 2); 1372 fds->handles_size * 2 + 2);
1375 fds->handles[fds->handles_pos++] = h; 1373 fds->handles[fds->handles_pos++] = h;
1376#else 1374#else
1377 int fd; 1375 int fd;
1378 1376
1379 GNUNET_assert (GNUNET_OK == 1377 GNUNET_assert(GNUNET_OK ==
1380 GNUNET_DISK_internal_file_handle_ (h, 1378 GNUNET_DISK_internal_file_handle_(h,
1381 &fd, 1379 &fd,
1382 sizeof (int))); 1380 sizeof(int)));
1383 FD_SET (fd, 1381 FD_SET(fd,
1384 &fds->sds); 1382 &fds->sds);
1385 fds->nsds = GNUNET_MAX (fd + 1, 1383 fds->nsds = GNUNET_MAX(fd + 1,
1386 fds->nsds); 1384 fds->nsds);
1387#endif 1385#endif
1388} 1386}
1389 1387
@@ -1394,24 +1392,24 @@ GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,
1394 * @param h the file handle to add 1392 * @param h the file handle to add
1395 */ 1393 */
1396void 1394void
1397GNUNET_NETWORK_fdset_handle_set_first (struct GNUNET_NETWORK_FDSet *fds, 1395GNUNET_NETWORK_fdset_handle_set_first(struct GNUNET_NETWORK_FDSet *fds,
1398 const struct GNUNET_DISK_FileHandle *h) 1396 const struct GNUNET_DISK_FileHandle *h)
1399{ 1397{
1400#ifdef MINGW 1398#ifdef MINGW
1401 if (fds->handles_pos == fds->handles_size) 1399 if (fds->handles_pos == fds->handles_size)
1402 GNUNET_array_grow (fds->handles, 1400 GNUNET_array_grow(fds->handles,
1403 fds->handles_size, 1401 fds->handles_size,
1404 fds->handles_size * 2 + 2); 1402 fds->handles_size * 2 + 2);
1405 fds->handles[fds->handles_pos] = h; 1403 fds->handles[fds->handles_pos] = h;
1406 if (fds->handles[0] != h) 1404 if (fds->handles[0] != h)
1407 { 1405 {
1408 const struct GNUNET_DISK_FileHandle *bak = fds->handles[0]; 1406 const struct GNUNET_DISK_FileHandle *bak = fds->handles[0];
1409 fds->handles[0] = h; 1407 fds->handles[0] = h;
1410 fds->handles[fds->handles_pos] = bak; 1408 fds->handles[fds->handles_pos] = bak;
1411 } 1409 }
1412 fds->handles_pos++; 1410 fds->handles_pos++;
1413#else 1411#else
1414 GNUNET_NETWORK_fdset_handle_set (fds, h); 1412 GNUNET_NETWORK_fdset_handle_set(fds, h);
1415#endif 1413#endif
1416} 1414}
1417 1415
@@ -1424,19 +1422,19 @@ GNUNET_NETWORK_fdset_handle_set_first (struct GNUNET_NETWORK_FDSet *fds,
1424 * @return #GNUNET_YES if the file handle is part of the set 1422 * @return #GNUNET_YES if the file handle is part of the set
1425 */ 1423 */
1426int 1424int
1427GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds, 1425GNUNET_NETWORK_fdset_handle_isset(const struct GNUNET_NETWORK_FDSet *fds,
1428 const struct GNUNET_DISK_FileHandle *h) 1426 const struct GNUNET_DISK_FileHandle *h)
1429{ 1427{
1430#ifdef MINGW 1428#ifdef MINGW
1431 unsigned int i; 1429 unsigned int i;
1432 1430
1433 for (i=0;i<fds->handles_pos;i++) 1431 for (i = 0; i < fds->handles_pos; i++)
1434 if (fds->handles[i] == h) 1432 if (fds->handles[i] == h)
1435 return GNUNET_YES; 1433 return GNUNET_YES;
1436 return GNUNET_NO; 1434 return GNUNET_NO;
1437#else 1435#else
1438 return FD_ISSET (h->fd, 1436 return FD_ISSET(h->fd,
1439 &fds->sds); 1437 &fds->sds);
1440#endif 1438#endif
1441} 1439}
1442 1440
@@ -1451,12 +1449,12 @@ GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,
1451 * @return -1, 0 or 1, if the p1 < p2, p1==p2 or p1 > p2. 1449 * @return -1, 0 or 1, if the p1 < p2, p1==p2 or p1 > p2.
1452 */ 1450 */
1453static int 1451static int
1454ptr_cmp (const void *p1, 1452ptr_cmp(const void *p1,
1455 const void *p2) 1453 const void *p2)
1456{ 1454{
1457 if (p1 == p2) 1455 if (p1 == p2)
1458 return 0; 1456 return 0;
1459 if ((intptr_t) p1 < (intptr_t) p2) 1457 if ((intptr_t)p1 < (intptr_t)p2)
1460 return -1; 1458 return -1;
1461 return 1; 1459 return 1;
1462} 1460}
@@ -1471,23 +1469,23 @@ ptr_cmp (const void *p1,
1471 * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise 1469 * @return #GNUNET_YES if they do overlap, #GNUNET_NO otherwise
1472 */ 1470 */
1473int 1471int
1474GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1, 1472GNUNET_NETWORK_fdset_overlap(const struct GNUNET_NETWORK_FDSet *fds1,
1475 const struct GNUNET_NETWORK_FDSet *fds2) 1473 const struct GNUNET_NETWORK_FDSet *fds2)
1476{ 1474{
1477#ifndef MINGW 1475#ifndef MINGW
1478 int nfds; 1476 int nfds;
1479 1477
1480 nfds = GNUNET_MIN (fds1->nsds, 1478 nfds = GNUNET_MIN(fds1->nsds,
1481 fds2->nsds); 1479 fds2->nsds);
1482 while (nfds > 0) 1480 while (nfds > 0)
1483 { 1481 {
1484 nfds--; 1482 nfds--;
1485 if ( (FD_ISSET (nfds, 1483 if ((FD_ISSET(nfds,
1486 &fds1->sds)) && 1484 &fds1->sds)) &&
1487 (FD_ISSET (nfds, 1485 (FD_ISSET(nfds,
1488 &fds2->sds)) ) 1486 &fds2->sds)))
1489 return GNUNET_YES; 1487 return GNUNET_YES;
1490 } 1488 }
1491 return GNUNET_NO; 1489 return GNUNET_NO;
1492#else 1490#else
1493 unsigned int i; 1491 unsigned int i;
@@ -1501,37 +1499,39 @@ GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
1501 return GNUNET_YES; 1499 return GNUNET_YES;
1502 1500
1503 /* take a short cut if possible */ 1501 /* take a short cut if possible */
1504 if ( (0 == fds1->handles_pos) || 1502 if ((0 == fds1->handles_pos) ||
1505 (0 == fds2->handles_pos) ) 1503 (0 == fds2->handles_pos))
1506 return GNUNET_NO; 1504 return GNUNET_NO;
1507 1505
1508 /* Sort file handles array to avoid quadratic complexity when 1506 /* Sort file handles array to avoid quadratic complexity when
1509 checking for overlap */ 1507 checking for overlap */
1510 qsort (fds1->handles, 1508 qsort(fds1->handles,
1511 fds1->handles_pos, 1509 fds1->handles_pos,
1512 sizeof (void *), 1510 sizeof(void *),
1513 &ptr_cmp); 1511 &ptr_cmp);
1514 qsort (fds2->handles, 1512 qsort(fds2->handles,
1515 fds2->handles_pos, 1513 fds2->handles_pos,
1516 sizeof (void *), 1514 sizeof(void *),
1517 &ptr_cmp); 1515 &ptr_cmp);
1518 i = 0; 1516 i = 0;
1519 j = 0; 1517 j = 0;
1520 while ( (i < fds1->handles_pos) && 1518 while ((i < fds1->handles_pos) &&
1521 (j < fds2->handles_pos) ) 1519 (j < fds2->handles_pos))
1522 {
1523 switch (ptr_cmp (fds1->handles[i],
1524 fds2->handles[j]))
1525 { 1520 {
1526 case -1: 1521 switch (ptr_cmp(fds1->handles[i],
1527 i++; 1522 fds2->handles[j]))
1528 break; 1523 {
1529 case 0: 1524 case -1:
1530 return GNUNET_YES; 1525 i++;
1531 case 1: 1526 break;
1532 j++; 1527
1528 case 0:
1529 return GNUNET_YES;
1530
1531 case 1:
1532 j++;
1533 }
1533 } 1534 }
1534 }
1535 return GNUNET_NO; 1535 return GNUNET_NO;
1536#endif 1536#endif
1537} 1537}
@@ -1543,12 +1543,12 @@ GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
1543 * @return a new fd set 1543 * @return a new fd set
1544 */ 1544 */
1545struct GNUNET_NETWORK_FDSet * 1545struct GNUNET_NETWORK_FDSet *
1546GNUNET_NETWORK_fdset_create () 1546GNUNET_NETWORK_fdset_create()
1547{ 1547{
1548 struct GNUNET_NETWORK_FDSet *fds; 1548 struct GNUNET_NETWORK_FDSet *fds;
1549 1549
1550 fds = GNUNET_new (struct GNUNET_NETWORK_FDSet); 1550 fds = GNUNET_new(struct GNUNET_NETWORK_FDSet);
1551 GNUNET_NETWORK_fdset_zero (fds); 1551 GNUNET_NETWORK_fdset_zero(fds);
1552 return fds; 1552 return fds;
1553} 1553}
1554 1554
@@ -1559,14 +1559,14 @@ GNUNET_NETWORK_fdset_create ()
1559 * @param fds fd set 1559 * @param fds fd set
1560 */ 1560 */
1561void 1561void
1562GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds) 1562GNUNET_NETWORK_fdset_destroy(struct GNUNET_NETWORK_FDSet *fds)
1563{ 1563{
1564#ifdef MINGW 1564#ifdef MINGW
1565 GNUNET_array_grow (fds->handles, 1565 GNUNET_array_grow(fds->handles,
1566 fds->handles_size, 1566 fds->handles_size,
1567 0); 1567 0);
1568#endif 1568#endif
1569 GNUNET_free (fds); 1569 GNUNET_free(fds);
1570} 1570}
1571 1571
1572 1572
@@ -1574,8 +1574,7 @@ GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)
1574/** 1574/**
1575 * FIXME. 1575 * FIXME.
1576 */ 1576 */
1577struct _select_params 1577struct _select_params {
1578{
1579 /** 1578 /**
1580 * Read set. 1579 * Read set.
1581 */ 1580 */
@@ -1622,29 +1621,29 @@ struct _select_params
1622 * FIXME. 1621 * FIXME.
1623 */ 1622 */
1624static DWORD WINAPI 1623static DWORD WINAPI
1625_selector (LPVOID p) 1624_selector(LPVOID p)
1626{ 1625{
1627 struct _select_params *sp = p; 1626 struct _select_params *sp = p;
1628 1627
1629 while (1) 1628 while (1)
1630 {
1631 WaitForSingleObject (sp->standby,
1632 INFINITE);
1633 ResetEvent (sp->standby);
1634 sp->status = select (1,
1635 sp->r,
1636 sp->w,
1637 sp->e,
1638 sp->tv);
1639 if (FD_ISSET (sp->wakeup_socket,
1640 sp->r))
1641 { 1629 {
1642 FD_CLR (sp->wakeup_socket, 1630 WaitForSingleObject(sp->standby,
1643 sp->r); 1631 INFINITE);
1644 sp->status -= 1; 1632 ResetEvent(sp->standby);
1633 sp->status = select(1,
1634 sp->r,
1635 sp->w,
1636 sp->e,
1637 sp->tv);
1638 if (FD_ISSET(sp->wakeup_socket,
1639 sp->r))
1640 {
1641 FD_CLR(sp->wakeup_socket,
1642 sp->r);
1643 sp->status -= 1;
1644 }
1645 SetEvent(sp->wakeup);
1645 } 1646 }
1646 SetEvent (sp->wakeup);
1647 }
1648 return 0; 1647 return 0;
1649} 1648}
1650 1649
@@ -1674,7 +1673,7 @@ static struct timeval select_timeout;
1674 * initializes that thread. 1673 * initializes that thread.
1675 */ 1674 */
1676static void 1675static void
1677initialize_select_thread () 1676initialize_select_thread()
1678{ 1677{
1679 SOCKET select_listening_socket = -1; 1678 SOCKET select_listening_socket = -1;
1680 struct sockaddr_in s_in; 1679 struct sockaddr_in s_in;
@@ -1682,67 +1681,67 @@ initialize_select_thread ()
1682 int res; 1681 int res;
1683 unsigned long p; 1682 unsigned long p;
1684 1683
1685 select_standby_event = CreateEvent (NULL, TRUE, FALSE, NULL); 1684 select_standby_event = CreateEvent(NULL, TRUE, FALSE, NULL);
1686 select_finished_event = CreateEvent (NULL, TRUE, FALSE, NULL); 1685 select_finished_event = CreateEvent(NULL, TRUE, FALSE, NULL);
1687 1686
1688 select_wakeup_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); 1687 select_wakeup_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1689 1688
1690 select_listening_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); 1689 select_listening_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1691 1690
1692 p = 1; 1691 p = 1;
1693 res = ioctlsocket (select_wakeup_socket, FIONBIO, &p); 1692 res = ioctlsocket(select_wakeup_socket, FIONBIO, &p);
1694 LOG (GNUNET_ERROR_TYPE_DEBUG, 1693 LOG(GNUNET_ERROR_TYPE_DEBUG,
1695 "Select thread initialization: ioctlsocket() returns %d\n", 1694 "Select thread initialization: ioctlsocket() returns %d\n",
1696 res); 1695 res);
1697 1696
1698 alen = sizeof (s_in); 1697 alen = sizeof(s_in);
1699 s_in.sin_family = AF_INET; 1698 s_in.sin_family = AF_INET;
1700 s_in.sin_port = 0; 1699 s_in.sin_port = 0;
1701 s_in.sin_addr.S_un.S_un_b.s_b1 = 127; 1700 s_in.sin_addr.S_un.S_un_b.s_b1 = 127;
1702 s_in.sin_addr.S_un.S_un_b.s_b2 = 0; 1701 s_in.sin_addr.S_un.S_un_b.s_b2 = 0;
1703 s_in.sin_addr.S_un.S_un_b.s_b3 = 0; 1702 s_in.sin_addr.S_un.S_un_b.s_b3 = 0;
1704 s_in.sin_addr.S_un.S_un_b.s_b4 = 1; 1703 s_in.sin_addr.S_un.S_un_b.s_b4 = 1;
1705 res = bind (select_listening_socket, 1704 res = bind(select_listening_socket,
1706 (const struct sockaddr *) &s_in, 1705 (const struct sockaddr *)&s_in,
1707 sizeof (s_in)); 1706 sizeof(s_in));
1708 LOG (GNUNET_ERROR_TYPE_DEBUG, 1707 LOG(GNUNET_ERROR_TYPE_DEBUG,
1709 "Select thread initialization: bind() returns %d\n", 1708 "Select thread initialization: bind() returns %d\n",
1710 res); 1709 res);
1711 1710
1712 res = getsockname (select_listening_socket, 1711 res = getsockname(select_listening_socket,
1713 (struct sockaddr *) &s_in, 1712 (struct sockaddr *)&s_in,
1714 &alen); 1713 &alen);
1715 LOG (GNUNET_ERROR_TYPE_DEBUG, 1714 LOG(GNUNET_ERROR_TYPE_DEBUG,
1716 "Select thread initialization: getsockname() returns %d\n", 1715 "Select thread initialization: getsockname() returns %d\n",
1717 res); 1716 res);
1718 1717
1719 res = listen (select_listening_socket, 1718 res = listen(select_listening_socket,
1720 SOMAXCONN); 1719 SOMAXCONN);
1721 LOG (GNUNET_ERROR_TYPE_DEBUG, 1720 LOG(GNUNET_ERROR_TYPE_DEBUG,
1722 "Select thread initialization: listen() returns %d\n", 1721 "Select thread initialization: listen() returns %d\n",
1723 res); 1722 res);
1724 res = connect (select_wakeup_socket, 1723 res = connect(select_wakeup_socket,
1725 (const struct sockaddr *) &s_in, 1724 (const struct sockaddr *)&s_in,
1726 sizeof (s_in)); 1725 sizeof(s_in));
1727 LOG (GNUNET_ERROR_TYPE_DEBUG, 1726 LOG(GNUNET_ERROR_TYPE_DEBUG,
1728 "Select thread initialization: connect() returns %d\n", 1727 "Select thread initialization: connect() returns %d\n",
1729 res); 1728 res);
1730 1729
1731 select_send_socket = accept (select_listening_socket, 1730 select_send_socket = accept(select_listening_socket,
1732 (struct sockaddr *) &s_in, 1731 (struct sockaddr *)&s_in,
1733 &alen); 1732 &alen);
1734 1733
1735 closesocket (select_listening_socket); 1734 closesocket(select_listening_socket);
1736 1735
1737 sp.wakeup = select_finished_event; 1736 sp.wakeup = select_finished_event;
1738 sp.standby = select_standby_event; 1737 sp.standby = select_standby_event;
1739 sp.wakeup_socket = select_wakeup_socket; 1738 sp.wakeup_socket = select_wakeup_socket;
1740 1739
1741 select_thread = CreateThread (NULL, 1740 select_thread = CreateThread(NULL,
1742 0, 1741 0,
1743 _selector, 1742 _selector,
1744 &sp, 1743 &sp,
1745 0, NULL); 1744 0, NULL);
1746} 1745}
1747 1746
1748 1747
@@ -1756,8 +1755,8 @@ initialize_select_thread ()
1756 * @return #GNUNET_OK if the port is available, #GNUNET_NO if not 1755 * @return #GNUNET_OK if the port is available, #GNUNET_NO if not
1757 */ 1756 */
1758int 1757int
1759GNUNET_NETWORK_test_port_free (int ipproto, 1758GNUNET_NETWORK_test_port_free(int ipproto,
1760 uint16_t port) 1759 uint16_t port)
1761{ 1760{
1762 struct GNUNET_NETWORK_Handle *socket; 1761 struct GNUNET_NETWORK_Handle *socket;
1763 int bind_status; 1762 int bind_status;
@@ -1767,13 +1766,13 @@ GNUNET_NETWORK_test_port_free (int ipproto,
1767 struct addrinfo *ret; 1766 struct addrinfo *ret;
1768 struct addrinfo *ai; 1767 struct addrinfo *ai;
1769 1768
1770 GNUNET_snprintf (open_port_str, 1769 GNUNET_snprintf(open_port_str,
1771 sizeof (open_port_str), 1770 sizeof(open_port_str),
1772 "%u", 1771 "%u",
1773 (unsigned int) port); 1772 (unsigned int)port);
1774 socktype = (IPPROTO_TCP == ipproto) ? SOCK_STREAM : SOCK_DGRAM; 1773 socktype = (IPPROTO_TCP == ipproto) ? SOCK_STREAM : SOCK_DGRAM;
1775 ret = NULL; 1774 ret = NULL;
1776 memset (&hint, 0, sizeof (hint)); 1775 memset(&hint, 0, sizeof(hint));
1777 hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */ 1776 hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
1778 hint.ai_socktype = socktype; 1777 hint.ai_socktype = socktype;
1779 hint.ai_protocol = ipproto; 1778 hint.ai_protocol = ipproto;
@@ -1782,26 +1781,26 @@ GNUNET_NETWORK_test_port_free (int ipproto,
1782 hint.ai_canonname = NULL; 1781 hint.ai_canonname = NULL;
1783 hint.ai_next = NULL; 1782 hint.ai_next = NULL;
1784 hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */ 1783 hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
1785 GNUNET_assert (0 == getaddrinfo (NULL, 1784 GNUNET_assert(0 == getaddrinfo(NULL,
1786 open_port_str, 1785 open_port_str,
1787 &hint, 1786 &hint,
1788 &ret)); 1787 &ret));
1789 bind_status = GNUNET_NO; 1788 bind_status = GNUNET_NO;
1790 for (ai = ret; NULL != ai; ai = ai->ai_next) 1789 for (ai = ret; NULL != ai; ai = ai->ai_next)
1791 { 1790 {
1792 socket = GNUNET_NETWORK_socket_create (ai->ai_family, 1791 socket = GNUNET_NETWORK_socket_create(ai->ai_family,
1793 ai->ai_socktype, 1792 ai->ai_socktype,
1794 ai->ai_protocol); 1793 ai->ai_protocol);
1795 if (NULL == socket) 1794 if (NULL == socket)
1796 continue; 1795 continue;
1797 bind_status = GNUNET_NETWORK_socket_bind (socket, 1796 bind_status = GNUNET_NETWORK_socket_bind(socket,
1798 ai->ai_addr, 1797 ai->ai_addr,
1799 ai->ai_addrlen); 1798 ai->ai_addrlen);
1800 GNUNET_NETWORK_socket_close (socket); 1799 GNUNET_NETWORK_socket_close(socket);
1801 if (GNUNET_OK != bind_status) 1800 if (GNUNET_OK != bind_status)
1802 break; 1801 break;
1803 } 1802 }
1804 freeaddrinfo (ret); 1803 freeaddrinfo(ret);
1805 return bind_status; 1804 return bind_status;
1806} 1805}
1807 1806
@@ -1817,10 +1816,10 @@ GNUNET_NETWORK_test_port_free (int ipproto,
1817 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error 1816 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
1818 */ 1817 */
1819int 1818int
1820GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds, 1819GNUNET_NETWORK_socket_select(struct GNUNET_NETWORK_FDSet *rfds,
1821 struct GNUNET_NETWORK_FDSet *wfds, 1820 struct GNUNET_NETWORK_FDSet *wfds,
1822 struct GNUNET_NETWORK_FDSet *efds, 1821 struct GNUNET_NETWORK_FDSet *efds,
1823 const struct GNUNET_TIME_Relative timeout) 1822 const struct GNUNET_TIME_Relative timeout)
1824{ 1823{
1825 int nfds; 1824 int nfds;
1826 struct timeval tv; 1825 struct timeval tv;
@@ -1830,37 +1829,37 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1830 else 1829 else
1831 nfds = 0; 1830 nfds = 0;
1832 if (NULL != wfds) 1831 if (NULL != wfds)
1833 nfds = GNUNET_MAX (nfds, 1832 nfds = GNUNET_MAX(nfds,
1834 wfds->nsds); 1833 wfds->nsds);
1835 if (NULL != efds) 1834 if (NULL != efds)
1836 nfds = GNUNET_MAX (nfds, 1835 nfds = GNUNET_MAX(nfds,
1837 efds->nsds); 1836 efds->nsds);
1838 if ((0 == nfds) && 1837 if ((0 == nfds) &&
1839 (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)) 1838 (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
1840 { 1839 {
1841 GNUNET_break (0); 1840 GNUNET_break(0);
1842 LOG (GNUNET_ERROR_TYPE_ERROR, 1841 LOG(GNUNET_ERROR_TYPE_ERROR,
1843 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"), 1842 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1844 "select"); 1843 "select");
1845 } 1844 }
1846 if (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us > (unsigned long long) LONG_MAX) 1845 if (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us > (unsigned long long)LONG_MAX)
1847 { 1846 {
1848 tv.tv_sec = LONG_MAX; 1847 tv.tv_sec = LONG_MAX;
1849 tv.tv_usec = 999999L; 1848 tv.tv_usec = 999999L;
1850 } 1849 }
1851 else 1850 else
1852 { 1851 {
1853 tv.tv_sec = (long) (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us); 1852 tv.tv_sec = (long)(timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us);
1854 tv.tv_usec = 1853 tv.tv_usec =
1855 (timeout.rel_value_us - 1854 (timeout.rel_value_us -
1856 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us)); 1855 (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1857 } 1856 }
1858 return select (nfds, 1857 return select(nfds,
1859 (NULL != rfds) ? &rfds->sds : NULL, 1858 (NULL != rfds) ? &rfds->sds : NULL,
1860 (NULL != wfds) ? &wfds->sds : NULL, 1859 (NULL != wfds) ? &wfds->sds : NULL,
1861 (NULL != efds) ? &efds->sds : NULL, 1860 (NULL != efds) ? &efds->sds : NULL,
1862 (timeout.rel_value_us == 1861 (timeout.rel_value_us ==
1863 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv); 1862 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) ? NULL : &tv);
1864} 1863}
1865 1864
1866 1865
@@ -1875,25 +1874,25 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
1875 * @return #GNUNET_YES if the pipe is ready for reading 1874 * @return #GNUNET_YES if the pipe is ready for reading
1876 */ 1875 */
1877static int 1876static int
1878pipe_read_ready (const struct GNUNET_DISK_FileHandle *fh) 1877pipe_read_ready(const struct GNUNET_DISK_FileHandle *fh)
1879{ 1878{
1880 DWORD error; 1879 DWORD error;
1881 BOOL bret; 1880 BOOL bret;
1882 DWORD waitstatus = 0; 1881 DWORD waitstatus = 0;
1883 1882
1884 SetLastError (0); 1883 SetLastError(0);
1885 bret = PeekNamedPipe (fh->h, NULL, 0, NULL, &waitstatus, NULL); 1884 bret = PeekNamedPipe(fh->h, NULL, 0, NULL, &waitstatus, NULL);
1886 error = GetLastError (); 1885 error = GetLastError();
1887 if (0 == bret) 1886 if (0 == bret)
1888 { 1887 {
1889 /* TODO: either add more errors to this condition, or eliminate it 1888 /* TODO: either add more errors to this condition, or eliminate it
1890 * entirely (failed to peek -> pipe is in serious trouble, should 1889 * entirely (failed to peek -> pipe is in serious trouble, should
1891 * be selected as readable). 1890 * be selected as readable).
1892 */ 1891 */
1893 if ( (error != ERROR_BROKEN_PIPE) && 1892 if ((error != ERROR_BROKEN_PIPE) &&
1894 (error != ERROR_INVALID_HANDLE) ) 1893 (error != ERROR_INVALID_HANDLE))
1895 return GNUNET_NO; 1894 return GNUNET_NO;
1896 } 1895 }
1897 else if (waitstatus <= 0) 1896 else if (waitstatus <= 0)
1898 return GNUNET_NO; 1897 return GNUNET_NO;
1899 return GNUNET_YES; 1898 return GNUNET_YES;
@@ -1907,11 +1906,11 @@ pipe_read_ready (const struct GNUNET_DISK_FileHandle *fh)
1907 * @return #GNUNET_YES if the pipe is having an IO exception. 1906 * @return #GNUNET_YES if the pipe is having an IO exception.
1908 */ 1907 */
1909static int 1908static int
1910pipe_except_ready (const struct GNUNET_DISK_FileHandle *fh) 1909pipe_except_ready(const struct GNUNET_DISK_FileHandle *fh)
1911{ 1910{
1912 DWORD dwBytes; 1911 DWORD dwBytes;
1913 1912
1914 if (PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL)) 1913 if (PeekNamedPipe(fh->h, NULL, 0, NULL, &dwBytes, NULL))
1915 return GNUNET_NO; 1914 return GNUNET_NO;
1916 return GNUNET_YES; 1915 return GNUNET_YES;
1917} 1916}
@@ -1931,38 +1930,38 @@ pipe_except_ready (const struct GNUNET_DISK_FileHandle *fh)
1931 * @return number of ready handles 1930 * @return number of ready handles
1932 */ 1931 */
1933static int 1932static int
1934check_handles_status (struct GNUNET_NETWORK_FDSet *fds, 1933check_handles_status(struct GNUNET_NETWORK_FDSet *fds,
1935 int except, 1934 int except,
1936 HANDLE set_for_sure) 1935 HANDLE set_for_sure)
1937{ 1936{
1938 const struct GNUNET_DISK_FileHandle *fh; 1937 const struct GNUNET_DISK_FileHandle *fh;
1939 unsigned int roff; 1938 unsigned int roff;
1940 unsigned int woff; 1939 unsigned int woff;
1941 1940
1942 for (woff = 0, roff = 0; roff < fds->handles_pos; roff++) 1941 for (woff = 0, roff = 0; roff < fds->handles_pos; roff++)
1943 {
1944 fh = fds->handles[roff];
1945 if (fh == set_for_sure)
1946 { 1942 {
1947 fds->handles[woff++] = fh; 1943 fh = fds->handles[roff];
1948 } 1944 if (fh == set_for_sure)
1949 else if (fh->type == GNUNET_DISK_HANLDE_TYPE_PIPE) 1945 {
1950 { 1946 fds->handles[woff++] = fh;
1951 if ((except && pipe_except_ready (fh)) || 1947 }
1952 (!except && pipe_read_ready (fh))) 1948 else if (fh->type == GNUNET_DISK_HANLDE_TYPE_PIPE)
1953 fds->handles[woff++] = fh; 1949 {
1954 } 1950 if ((except && pipe_except_ready(fh)) ||
1955 else if (fh->type == GNUNET_DISK_HANLDE_TYPE_FILE) 1951 (!except && pipe_read_ready(fh)))
1956 { 1952 fds->handles[woff++] = fh;
1957 if (!except) 1953 }
1958 fds->handles[woff++] = fh; 1954 else if (fh->type == GNUNET_DISK_HANLDE_TYPE_FILE)
1959 } 1955 {
1960 else 1956 if (!except)
1961 { 1957 fds->handles[woff++] = fh;
1962 if (WAIT_OBJECT_0 == WaitForSingleObject (fh->h, 0)) 1958 }
1963 fds->handles[woff++] = fh; 1959 else
1960 {
1961 if (WAIT_OBJECT_0 == WaitForSingleObject(fh->h, 0))
1962 fds->handles[woff++] = fh;
1963 }
1964 } 1964 }
1965 }
1966 fds->handles_pos = woff; 1965 fds->handles_pos = woff;
1967 return woff; 1966 return woff;
1968} 1967}
@@ -1978,10 +1977,10 @@ check_handles_status (struct GNUNET_NETWORK_FDSet *fds,
1978 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error 1977 * @return number of selected sockets or pipes, #GNUNET_SYSERR on error
1979 */ 1978 */
1980int 1979int
1981GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds, 1980GNUNET_NETWORK_socket_select(struct GNUNET_NETWORK_FDSet *rfds,
1982 struct GNUNET_NETWORK_FDSet *wfds, 1981 struct GNUNET_NETWORK_FDSet *wfds,
1983 struct GNUNET_NETWORK_FDSet *efds, 1982 struct GNUNET_NETWORK_FDSet *efds,
1984 const struct GNUNET_TIME_Relative timeout) 1983 const struct GNUNET_TIME_Relative timeout)
1985{ 1984{
1986 const struct GNUNET_DISK_FileHandle *fh; 1985 const struct GNUNET_DISK_FileHandle *fh;
1987 int nfds; 1986 int nfds;
@@ -2003,68 +2002,68 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
2003 nfds = 0; 2002 nfds = 0;
2004 handles = 0; 2003 handles = 0;
2005 if (NULL != rfds) 2004 if (NULL != rfds)
2006 { 2005 {
2007 nfds = GNUNET_MAX (nfds, rfds->nsds); 2006 nfds = GNUNET_MAX(nfds, rfds->nsds);
2008 handles += rfds->handles_pos; 2007 handles += rfds->handles_pos;
2009 } 2008 }
2010 if (NULL != wfds) 2009 if (NULL != wfds)
2011 { 2010 {
2012 nfds = GNUNET_MAX (nfds, wfds->nsds); 2011 nfds = GNUNET_MAX(nfds, wfds->nsds);
2013 handles += wfds->handles_pos; 2012 handles += wfds->handles_pos;
2014 } 2013 }
2015 if (NULL != efds) 2014 if (NULL != efds)
2016 { 2015 {
2017 nfds = GNUNET_MAX (nfds, efds->nsds); 2016 nfds = GNUNET_MAX(nfds, efds->nsds);
2018 handles += efds->handles_pos; 2017 handles += efds->handles_pos;
2019 } 2018 }
2020 2019
2021 if ((0 == nfds) && 2020 if ((0 == nfds) &&
2022 (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == timeout.rel_value_us) && 2021 (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == timeout.rel_value_us) &&
2023 (0 == handles) ) 2022 (0 == handles))
2024 { 2023 {
2025 GNUNET_break (0); 2024 GNUNET_break(0);
2026 LOG (GNUNET_ERROR_TYPE_ERROR, 2025 LOG(GNUNET_ERROR_TYPE_ERROR,
2027 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"), 2026 _("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
2028 "select"); 2027 "select");
2029 } 2028 }
2030#define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set)) 2029#define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
2031 /* calculate how long we need to wait in microseconds */ 2030 /* calculate how long we need to wait in microseconds */
2032 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) 2031 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
2033 { 2032 {
2034 mcs_total = INFINITE; 2033 mcs_total = INFINITE;
2035 ms_rounded = INFINITE; 2034 ms_rounded = INFINITE;
2036 } 2035 }
2037 else 2036 else
2038 { 2037 {
2039 mcs_total = timeout.rel_value_us / GNUNET_TIME_UNIT_MICROSECONDS.rel_value_us; 2038 mcs_total = timeout.rel_value_us / GNUNET_TIME_UNIT_MICROSECONDS.rel_value_us;
2040 ms_rounded = (DWORD) (mcs_total / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us); 2039 ms_rounded = (DWORD)(mcs_total / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us);
2041 if (mcs_total > 0 && ms_rounded == 0) 2040 if (mcs_total > 0 && ms_rounded == 0)
2042 ms_rounded = 1; 2041 ms_rounded = 1;
2043 } 2042 }
2044 /* select() may be used as a portable way to sleep */ 2043 /* select() may be used as a portable way to sleep */
2045 if (! (rfds || wfds || efds)) 2044 if (!(rfds || wfds || efds))
2046 { 2045 {
2047 Sleep (ms_rounded); 2046 Sleep(ms_rounded);
2048 return 0; 2047 return 0;
2049 } 2048 }
2050 2049
2051 if (NULL == select_thread) 2050 if (NULL == select_thread)
2052 initialize_select_thread (); 2051 initialize_select_thread();
2053 2052
2054 FD_ZERO (&aread); 2053 FD_ZERO(&aread);
2055 FD_ZERO (&awrite); 2054 FD_ZERO(&awrite);
2056 FD_ZERO (&aexcept); 2055 FD_ZERO(&aexcept);
2057 if (rfds) 2056 if (rfds)
2058 FD_COPY (&rfds->sds, &aread); 2057 FD_COPY(&rfds->sds, &aread);
2059 if (wfds) 2058 if (wfds)
2060 FD_COPY (&wfds->sds, &awrite); 2059 FD_COPY(&wfds->sds, &awrite);
2061 if (efds) 2060 if (efds)
2062 FD_COPY (&efds->sds, &aexcept); 2061 FD_COPY(&efds->sds, &aexcept);
2063 2062
2064 /* Start by doing a fast check on sockets and pipes (without 2063 /* Start by doing a fast check on sockets and pipes (without
2065 waiting). It is cheap, and is sufficient most of the time. By 2064 waiting). It is cheap, and is sufficient most of the time. By
2066 profiling we detected that to be true in 90% of the cases. 2065 profiling we detected that to be true in 90% of the cases.
2067 */ 2066 */
2068 2067
2069 /* Do the select now */ 2068 /* Do the select now */
2070 select_timeout.tv_sec = 0; 2069 select_timeout.tv_sec = 0;
@@ -2072,304 +2071,306 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,
2072 2071
2073 /* Copy all the writes to the except, so we can detect connect() errors */ 2072 /* Copy all the writes to the except, so we can detect connect() errors */
2074 for (i = 0; i < awrite.fd_count; i++) 2073 for (i = 0; i < awrite.fd_count; i++)
2075 FD_SET (awrite.fd_array[i], 2074 FD_SET(awrite.fd_array[i],
2076 &aexcept); 2075 &aexcept);
2077 if ( (aread.fd_count > 0) || 2076 if ((aread.fd_count > 0) ||
2078 (awrite.fd_count > 0) || 2077 (awrite.fd_count > 0) ||
2079 (aexcept.fd_count > 0) ) 2078 (aexcept.fd_count > 0))
2080 selectret = select (1, 2079 selectret = select(1,
2081 (NULL != rfds) ? &aread : NULL, 2080 (NULL != rfds) ? &aread : NULL,
2082 (NULL != wfds) ? &awrite : NULL, 2081 (NULL != wfds) ? &awrite : NULL,
2083 &aexcept, 2082 &aexcept,
2084 &select_timeout); 2083 &select_timeout);
2085 else 2084 else
2086 selectret = 0; 2085 selectret = 0;
2087 if (-1 == selectret) 2086 if (-1 == selectret)
2088 { 2087 {
2089 /* Throw an error early on, while we still have the context. */ 2088 /* Throw an error early on, while we still have the context. */
2090 LOG (GNUNET_ERROR_TYPE_ERROR, 2089 LOG(GNUNET_ERROR_TYPE_ERROR,
2091 "W32 select(%d, %d, %d) failed: %lu\n", 2090 "W32 select(%d, %d, %d) failed: %lu\n",
2092 rfds ? aread.fd_count : 0, 2091 rfds ? aread.fd_count : 0,
2093 wfds ? awrite.fd_count : 0, 2092 wfds ? awrite.fd_count : 0,
2094 aexcept.fd_count, 2093 aexcept.fd_count,
2095 GetLastError ()); 2094 GetLastError());
2096 GNUNET_assert (0); 2095 GNUNET_assert(0);
2097 } 2096 }
2098 2097
2099 /* Check aexcept, if something is in there and we copied that 2098 /* Check aexcept, if something is in there and we copied that
2100 FD before to detect connect() errors, add it back to the 2099 FD before to detect connect() errors, add it back to the
2101 write set to report errors. */ 2100 write set to report errors. */
2102 if (NULL != wfds) 2101 if (NULL != wfds)
2103 for (i = 0; i < aexcept.fd_count; i++) 2102 for (i = 0; i < aexcept.fd_count; i++)
2104 if (FD_ISSET (aexcept.fd_array[i], 2103 if (FD_ISSET(aexcept.fd_array[i],
2105 &wfds->sds)) 2104 &wfds->sds))
2106 FD_SET (aexcept.fd_array[i], 2105 FD_SET(aexcept.fd_array[i],
2107 &awrite); 2106 &awrite);
2108 2107
2109 2108
2110 /* If our select returned something or is a 0-timed request, then 2109 /* If our select returned something or is a 0-timed request, then
2111 also check the pipes and get out of here! */ 2110 also check the pipes and get out of here! */
2112 /* Sadly, it means code duplication :( */ 2111 /* Sadly, it means code duplication :( */
2113 if ( (selectret > 0) || (0 == mcs_total) ) 2112 if ((selectret > 0) || (0 == mcs_total))
2114 { 2113 {
2115 retcode = 0; 2114 retcode = 0;
2116 2115
2117 /* Read Pipes */ 2116 /* Read Pipes */
2118 if (rfds && (rfds->handles_pos > 0)) 2117 if (rfds && (rfds->handles_pos > 0))
2119 retcode += check_handles_status (rfds, GNUNET_NO, NULL); 2118 retcode += check_handles_status(rfds, GNUNET_NO, NULL);
2120 2119
2121 /* wfds handles remain untouched, on W32 2120 /* wfds handles remain untouched, on W32
2122 we pretend our pipes are "always" write-ready */ 2121 we pretend our pipes are "always" write-ready */
2123 2122
2124 /* except pipes */ 2123 /* except pipes */
2125 if (efds && (efds->handles_pos > 0)) 2124 if (efds && (efds->handles_pos > 0))
2126 retcode += check_handles_status (efds, GNUNET_YES, NULL); 2125 retcode += check_handles_status(efds, GNUNET_YES, NULL);
2127 2126
2128 if (rfds) 2127 if (rfds)
2129 { 2128 {
2130 GNUNET_NETWORK_fdset_zero (rfds); 2129 GNUNET_NETWORK_fdset_zero(rfds);
2131 if (selectret != -1) 2130 if (selectret != -1)
2132 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, selectret); 2131 GNUNET_NETWORK_fdset_copy_native(rfds, &aread, selectret);
2133 } 2132 }
2134 if (wfds) 2133 if (wfds)
2135 { 2134 {
2136 GNUNET_NETWORK_fdset_zero (wfds); 2135 GNUNET_NETWORK_fdset_zero(wfds);
2137 if (selectret != -1) 2136 if (selectret != -1)
2138 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, selectret); 2137 GNUNET_NETWORK_fdset_copy_native(wfds, &awrite, selectret);
2139 } 2138 }
2140 if (efds) 2139 if (efds)
2141 { 2140 {
2142 GNUNET_NETWORK_fdset_zero (efds); 2141 GNUNET_NETWORK_fdset_zero(efds);
2143 if (selectret != -1) 2142 if (selectret != -1)
2144 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, selectret); 2143 GNUNET_NETWORK_fdset_copy_native(efds, &aexcept, selectret);
2144 }
2145 if (-1 == selectret)
2146 return -1;
2147 /* Add our select() FDs to the total return value */
2148 retcode += selectret;
2149 return retcode;
2145 } 2150 }
2146 if (-1 == selectret)
2147 return -1;
2148 /* Add our select() FDs to the total return value */
2149 retcode += selectret;
2150 return retcode;
2151 }
2152 2151
2153 /* If we got this far, use slower implementation that is able to do a waiting select 2152 /* If we got this far, use slower implementation that is able to do a waiting select
2154 on both sockets and pipes simultaneously */ 2153 on both sockets and pipes simultaneously */
2155 2154
2156 /* Events for pipes */ 2155 /* Events for pipes */
2157 if (! hEventReadReady) 2156 if (!hEventReadReady)
2158 hEventReadReady = CreateEvent (NULL, TRUE, TRUE, NULL); 2157 hEventReadReady = CreateEvent(NULL, TRUE, TRUE, NULL);
2159 if (! hEventPipeWrite) 2158 if (!hEventPipeWrite)
2160 hEventPipeWrite = CreateEvent (NULL, TRUE, TRUE, NULL); 2159 hEventPipeWrite = CreateEvent(NULL, TRUE, TRUE, NULL);
2161 retcode = 0; 2160 retcode = 0;
2162 2161
2163 FD_ZERO (&aread); 2162 FD_ZERO(&aread);
2164 FD_ZERO (&awrite); 2163 FD_ZERO(&awrite);
2165 FD_ZERO (&aexcept); 2164 FD_ZERO(&aexcept);
2166 if (rfds) 2165 if (rfds)
2167 FD_COPY (&rfds->sds, &aread); 2166 FD_COPY(&rfds->sds, &aread);
2168 if (wfds) 2167 if (wfds)
2169 FD_COPY (&wfds->sds, &awrite); 2168 FD_COPY(&wfds->sds, &awrite);
2170 if (efds) 2169 if (efds)
2171 FD_COPY (&efds->sds, &aexcept); 2170 FD_COPY(&efds->sds, &aexcept);
2172 /* We will first Add the PIPES to the events */ 2171 /* We will first Add the PIPES to the events */
2173 /* Track how far in `handle_array` the read pipes go, 2172 /* Track how far in `handle_array` the read pipes go,
2174 so we may by-pass them quickly if none of them 2173 so we may by-pass them quickly if none of them
2175 are selected. */ 2174 are selected. */
2176 read_pipes_off = 0; 2175 read_pipes_off = 0;
2177 if (rfds && (rfds->handles_pos > 0)) 2176 if (rfds && (rfds->handles_pos > 0))
2178 {
2179 for (i = 0; i <rfds->handles_pos; i++)
2180 { 2177 {
2181 fh = rfds->handles[i]; 2178 for (i = 0; i < rfds->handles_pos; i++)
2182 if (fh->type == GNUNET_DISK_HANLDE_TYPE_EVENT)
2183 {
2184 handle_array[nhandles++] = fh->h;
2185 continue;
2186 }
2187 if (fh->type != GNUNET_DISK_HANLDE_TYPE_PIPE)
2188 continue;
2189 /* Read zero bytes to check the status of the pipe */
2190 if (! ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead))
2191 {
2192 DWORD error_code = GetLastError ();
2193
2194 if (error_code == ERROR_IO_PENDING)
2195 { 2179 {
2196 /* add as unready */ 2180 fh = rfds->handles[i];
2197 handle_array[nhandles++] = fh->oOverlapRead->hEvent; 2181 if (fh->type == GNUNET_DISK_HANLDE_TYPE_EVENT)
2198 read_pipes_off++; 2182 {
2183 handle_array[nhandles++] = fh->h;
2184 continue;
2185 }
2186 if (fh->type != GNUNET_DISK_HANLDE_TYPE_PIPE)
2187 continue;
2188 /* Read zero bytes to check the status of the pipe */
2189 if (!ReadFile(fh->h, NULL, 0, NULL, fh->oOverlapRead))
2190 {
2191 DWORD error_code = GetLastError();
2192
2193 if (error_code == ERROR_IO_PENDING)
2194 {
2195 /* add as unready */
2196 handle_array[nhandles++] = fh->oOverlapRead->hEvent;
2197 read_pipes_off++;
2198 }
2199 else
2200 {
2201 /* add as ready */
2202 handle_array[nhandles++] = hEventReadReady;
2203 read_pipes_off++;
2204 }
2205 }
2206 else
2207 {
2208 /* error also counts as ready */
2209 handle_array[nhandles++] = hEventReadReady;
2210 read_pipes_off++;
2211 }
2199 } 2212 }
2200 else
2201 {
2202 /* add as ready */
2203 handle_array[nhandles++] = hEventReadReady;
2204 read_pipes_off++;
2205 }
2206 }
2207 else
2208 {
2209 /* error also counts as ready */
2210 handle_array[nhandles++] = hEventReadReady;
2211 read_pipes_off++;
2212 }
2213 } 2213 }
2214 }
2215 2214
2216 if (wfds && (wfds->handles_pos > 0)) 2215 if (wfds && (wfds->handles_pos > 0))
2217 { 2216 {
2218 LOG (GNUNET_ERROR_TYPE_DEBUG, 2217 LOG(GNUNET_ERROR_TYPE_DEBUG,
2219 "Adding the write ready event to the array as %d\n", 2218 "Adding the write ready event to the array as %d\n",
2220 nhandles); 2219 nhandles);
2221 handle_array[nhandles++] = hEventPipeWrite; 2220 handle_array[nhandles++] = hEventPipeWrite;
2222 } 2221 }
2223 2222
2224 sp.status = 0; 2223 sp.status = 0;
2225 if (nfds > 0) 2224 if (nfds > 0)
2226 {
2227 LOG (GNUNET_ERROR_TYPE_DEBUG,
2228 "Adding the socket event to the array as %d\n",
2229 nhandles);
2230 handle_array[nhandles++] = select_finished_event;
2231 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
2232 { 2225 {
2233 sp.tv = NULL; 2226 LOG(GNUNET_ERROR_TYPE_DEBUG,
2234 } 2227 "Adding the socket event to the array as %d\n",
2235 else 2228 nhandles);
2236 { 2229 handle_array[nhandles++] = select_finished_event;
2237 select_timeout.tv_sec = timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us; 2230 if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
2238 select_timeout.tv_usec = (timeout.rel_value_us - 2231 {
2239 (select_timeout.tv_sec * 2232 sp.tv = NULL;
2240 GNUNET_TIME_UNIT_SECONDS.rel_value_us)); 2233 }
2241 sp.tv = &select_timeout; 2234 else
2235 {
2236 select_timeout.tv_sec = timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us;
2237 select_timeout.tv_usec = (timeout.rel_value_us -
2238 (select_timeout.tv_sec *
2239 GNUNET_TIME_UNIT_SECONDS.rel_value_us));
2240 sp.tv = &select_timeout;
2241 }
2242 FD_SET(select_wakeup_socket, &aread);
2243 do
2244 {
2245 i = recv(select_wakeup_socket,
2246 (char *)&returnedpos,
2247 1,
2248 0);
2249 }
2250 while (i == 1);
2251 sp.r = &aread;
2252 sp.w = &awrite;
2253 sp.e = &aexcept;
2254 /* Failed connections cause sockets to be set in errorfds on W32,
2255 * but on POSIX it should set them in writefds.
2256 * First copy all awrite sockets to aexcept, later we'll
2257 * check aexcept and set its contents in awrite as well
2258 * Sockets are also set in errorfds when OOB data is available,
2259 * but we don't use OOB data.
2260 */
2261 for (i = 0; i < awrite.fd_count; i++)
2262 FD_SET(awrite.fd_array[i],
2263 &aexcept);
2264 ResetEvent(select_finished_event);
2265 SetEvent(select_standby_event);
2242 } 2266 }
2243 FD_SET (select_wakeup_socket, &aread);
2244 do
2245 {
2246 i = recv (select_wakeup_socket,
2247 (char *) &returnedpos,
2248 1,
2249 0);
2250 } while (i == 1);
2251 sp.r = &aread;
2252 sp.w = &awrite;
2253 sp.e = &aexcept;
2254 /* Failed connections cause sockets to be set in errorfds on W32,
2255 * but on POSIX it should set them in writefds.
2256 * First copy all awrite sockets to aexcept, later we'll
2257 * check aexcept and set its contents in awrite as well
2258 * Sockets are also set in errorfds when OOB data is available,
2259 * but we don't use OOB data.
2260 */
2261 for (i = 0; i < awrite.fd_count; i++)
2262 FD_SET (awrite.fd_array[i],
2263 &aexcept);
2264 ResetEvent (select_finished_event);
2265 SetEvent (select_standby_event);
2266 }
2267 2267
2268 /* NULL-terminate array */ 2268 /* NULL-terminate array */
2269 handle_array[nhandles] = NULL; 2269 handle_array[nhandles] = NULL;
2270 LOG (GNUNET_ERROR_TYPE_DEBUG, 2270 LOG(GNUNET_ERROR_TYPE_DEBUG,
2271 "nfds: %d, handles: %d, will wait: %llu mcs\n", 2271 "nfds: %d, handles: %d, will wait: %llu mcs\n",
2272 nfds, 2272 nfds,
2273 nhandles, 2273 nhandles,
2274 mcs_total); 2274 mcs_total);
2275 if (nhandles) 2275 if (nhandles)
2276 { 2276 {
2277 returncode 2277 returncode
2278 = WaitForMultipleObjects (nhandles, 2278 = WaitForMultipleObjects(nhandles,
2279 handle_array, 2279 handle_array,
2280 FALSE, 2280 FALSE,
2281 ms_rounded); 2281 ms_rounded);
2282 LOG (GNUNET_ERROR_TYPE_DEBUG, 2282 LOG(GNUNET_ERROR_TYPE_DEBUG,
2283 "WaitForMultipleObjects Returned: %d\n", 2283 "WaitForMultipleObjects Returned: %d\n",
2284 returncode); 2284 returncode);
2285 } 2285 }
2286 else if (nfds > 0) 2286 else if (nfds > 0)
2287 { 2287 {
2288 GNUNET_break (0); /* This branch shouldn't actually be executed...*/ 2288 GNUNET_break(0); /* This branch shouldn't actually be executed...*/
2289 i = (int) WaitForSingleObject (select_finished_event, 2289 i = (int)WaitForSingleObject(select_finished_event,
2290 INFINITE); 2290 INFINITE);
2291 returncode = WAIT_TIMEOUT; 2291 returncode = WAIT_TIMEOUT;
2292 } 2292 }
2293 else 2293 else
2294 { 2294 {
2295 /* Shouldn't come this far. If it does - investigate. */ 2295 /* Shouldn't come this far. If it does - investigate. */
2296 GNUNET_assert (0); 2296 GNUNET_assert(0);
2297 } 2297 }
2298 2298
2299 if (nfds > 0) 2299 if (nfds > 0)
2300 {
2301 /* Don't wake up select-thread when delay is 0, it should return immediately
2302 * and wake up by itself.
2303 */
2304 if (0 != mcs_total)
2305 i = send (select_send_socket,
2306 (const char *) &returnedpos,
2307 1,
2308 0);
2309 i = (int) WaitForSingleObject (select_finished_event,
2310 INFINITE);
2311 LOG (GNUNET_ERROR_TYPE_DEBUG,
2312 "Finished waiting for the select thread: %d %d\n",
2313 i,
2314 sp.status);
2315 if (0 != mcs_total)
2316 { 2300 {
2317 do 2301 /* Don't wake up select-thread when delay is 0, it should return immediately
2318 { 2302 * and wake up by itself.
2319 i = recv (select_wakeup_socket, 2303 */
2320 (char *) &returnedpos, 2304 if (0 != mcs_total)
2321 1, 0); 2305 i = send(select_send_socket,
2322 } while (1 == i); 2306 (const char *)&returnedpos,
2307 1,
2308 0);
2309 i = (int)WaitForSingleObject(select_finished_event,
2310 INFINITE);
2311 LOG(GNUNET_ERROR_TYPE_DEBUG,
2312 "Finished waiting for the select thread: %d %d\n",
2313 i,
2314 sp.status);
2315 if (0 != mcs_total)
2316 {
2317 do
2318 {
2319 i = recv(select_wakeup_socket,
2320 (char *)&returnedpos,
2321 1, 0);
2322 }
2323 while (1 == i);
2324 }
2325 /* Check aexcept, add its contents to awrite */
2326 for (i = 0; i < aexcept.fd_count; i++)
2327 FD_SET(aexcept.fd_array[i], &awrite);
2323 } 2328 }
2324 /* Check aexcept, add its contents to awrite */
2325 for (i = 0; i < aexcept.fd_count; i++)
2326 FD_SET (aexcept.fd_array[i], &awrite);
2327 }
2328 2329
2329 returnedpos = returncode - WAIT_OBJECT_0; 2330 returnedpos = returncode - WAIT_OBJECT_0;
2330 LOG (GNUNET_ERROR_TYPE_DEBUG, 2331 LOG(GNUNET_ERROR_TYPE_DEBUG,
2331 "return pos is: %d\n", 2332 "return pos is: %d\n",
2332 returnedpos); 2333 returnedpos);
2333 2334
2334 if (rfds) 2335 if (rfds)
2335 {
2336 /* We queued a zero-long read on each pipe to check
2337 * its state, now we must cancel these read operations.
2338 * This must be done while rfds->handles_pos is still
2339 * intact and matches the number of read handles that we
2340 * got from the caller.
2341 */
2342 for (i = 0; i < rfds->handles_pos; i++)
2343 { 2336 {
2344 fh = rfds->handles[i]; 2337 /* We queued a zero-long read on each pipe to check
2345 if (GNUNET_DISK_HANLDE_TYPE_PIPE == fh->type) 2338 * its state, now we must cancel these read operations.
2346 CancelIo (fh->h); 2339 * This must be done while rfds->handles_pos is still
2347 } 2340 * intact and matches the number of read handles that we
2341 * got from the caller.
2342 */
2343 for (i = 0; i < rfds->handles_pos; i++)
2344 {
2345 fh = rfds->handles[i];
2346 if (GNUNET_DISK_HANLDE_TYPE_PIPE == fh->type)
2347 CancelIo(fh->h);
2348 }
2348 2349
2349 /* We may have some pipes ready for reading. */ 2350 /* We may have some pipes ready for reading. */
2350 if (returnedpos < read_pipes_off) 2351 if (returnedpos < read_pipes_off)
2351 retcode += check_handles_status (rfds, GNUNET_NO, handle_array[returnedpos]); 2352 retcode += check_handles_status(rfds, GNUNET_NO, handle_array[returnedpos]);
2352 else 2353 else
2353 rfds->handles_pos = 0; 2354 rfds->handles_pos = 0;
2354 2355
2355 if (-1 != sp.status) 2356 if (-1 != sp.status)
2356 GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode); 2357 GNUNET_NETWORK_fdset_copy_native(rfds, &aread, retcode);
2357 } 2358 }
2358 if (wfds) 2359 if (wfds)
2359 { 2360 {
2360 retcode += wfds->handles_pos; 2361 retcode += wfds->handles_pos;
2361 /* wfds handles remain untouched */ 2362 /* wfds handles remain untouched */
2362 if (-1 != sp.status) 2363 if (-1 != sp.status)
2363 GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode); 2364 GNUNET_NETWORK_fdset_copy_native(wfds, &awrite, retcode);
2364 } 2365 }
2365 if (efds) 2366 if (efds)
2366 { 2367 {
2367 retcode += check_handles_status (rfds, 2368 retcode += check_handles_status(rfds,
2368 GNUNET_YES, 2369 GNUNET_YES,
2369 returnedpos < nhandles ? handle_array[returnedpos] : NULL); 2370 returnedpos < nhandles ? handle_array[returnedpos] : NULL);
2370 if (-1 != sp.status) 2371 if (-1 != sp.status)
2371 GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode); 2372 GNUNET_NETWORK_fdset_copy_native(efds, &aexcept, retcode);
2372 } 2373 }
2373 2374
2374 if (sp.status > 0) 2375 if (sp.status > 0)
2375 retcode += sp.status; 2376 retcode += sp.status;