diff options
author | Christian Grothoff <christian@grothoff.org> | 2012-03-23 16:55:19 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2012-03-23 16:55:19 +0000 |
commit | 62ced7d7c6daac0e0629edf54f417638db6c1dbd (patch) | |
tree | 72a1213ab4d7c00a04e2342d224acb221e492c7c | |
parent | 34584e4bca53f072fbf8ba471084957bdf842476 (diff) | |
download | gnunet-62ced7d7c6daac0e0629edf54f417638db6c1dbd.tar.gz gnunet-62ced7d7c6daac0e0629edf54f417638db6c1dbd.zip |
-eliminating duplicate code
-rw-r--r-- | src/util/network.c | 145 |
1 files changed, 61 insertions, 84 deletions
diff --git a/src/util/network.c b/src/util/network.c index e530ab743..9371ae6f6 100644 --- a/src/util/network.c +++ b/src/util/network.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | (C) 2009 Christian Grothoff (and other contributing authors) | 3 | (C) 2009, 2012 Christian Grothoff (and other contributing authors) |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 5 | GNUnet is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published | 6 | it under the terms of the GNU General Public License as published |
@@ -193,6 +193,61 @@ socket_set_nodelay (const struct GNUNET_NETWORK_Handle *h) | |||
193 | 193 | ||
194 | 194 | ||
195 | /** | 195 | /** |
196 | * Perform proper canonical initialization for a network handle. | ||
197 | * Set it to non-blocking, make it non-inheritable to child | ||
198 | * processes, disable SIGPIPE, enable "nodelay" (if non-UNIX | ||
199 | * stream socket) and check that it is smaller than FS_SETSIZE. | ||
200 | * | ||
201 | * @param h socket to initialize | ||
202 | * @param af address family of the socket | ||
203 | * @param type socket type | ||
204 | * @return GNUNET_OK on success, GNUNET_SYSERR if initialization | ||
205 | * failed and the handle was destroyed | ||
206 | */ | ||
207 | static int | ||
208 | initialize_network_handle (struct GNUNET_NETWORK_Handle *h, | ||
209 | int af, int type) | ||
210 | { | ||
211 | h->af = af; | ||
212 | if (h->fd == INVALID_SOCKET) | ||
213 | { | ||
214 | #ifdef MINGW | ||
215 | SetErrnoFromWinsockError (WSAGetLastError ()); | ||
216 | #endif | ||
217 | GNUNET_free (h); | ||
218 | return GNUNET_SYSERR; | ||
219 | } | ||
220 | #ifndef MINGW | ||
221 | if (h->fd >= FD_SETSIZE) | ||
222 | { | ||
223 | GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h)); | ||
224 | errno = EMFILE; | ||
225 | return GNUNET_SYSERR; | ||
226 | } | ||
227 | if (GNUNET_OK != socket_set_inheritable (h)) | ||
228 | LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
229 | "socket_set_inheritable"); | ||
230 | #endif | ||
231 | if (GNUNET_SYSERR == socket_set_blocking (h, GNUNET_NO)) | ||
232 | { | ||
233 | GNUNET_break (0); | ||
234 | GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (h)); | ||
235 | return GNUNET_SYSERR; | ||
236 | } | ||
237 | #ifdef DARWIN | ||
238 | socket_set_nosigpipe (h); | ||
239 | #endif | ||
240 | if ( (type == SOCK_STREAM) | ||
241 | #ifdef AF_UNIX | ||
242 | && (af != AF_UNIX) | ||
243 | #endif | ||
244 | ) | ||
245 | socket_set_nodelay (h); | ||
246 | return GNUNET_OK; | ||
247 | } | ||
248 | |||
249 | |||
250 | /** | ||
196 | * accept a new connection on a socket | 251 | * accept a new connection on a socket |
197 | * | 252 | * |
198 | * @param desc bound socket | 253 | * @param desc bound socket |
@@ -219,49 +274,10 @@ GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc, | |||
219 | } | 274 | } |
220 | #endif | 275 | #endif |
221 | ret->fd = accept (desc->fd, address, address_len); | 276 | ret->fd = accept (desc->fd, address, address_len); |
222 | if (address != NULL) | 277 | if (GNUNET_OK != initialize_network_handle (ret, |
223 | ret->af = address->sa_family; | 278 | (NULL != address) ? address->sa_family : desc->af, |
224 | else | 279 | SOCK_STREAM)) |
225 | ret->af = desc->af; | ||
226 | if (ret->fd == INVALID_SOCKET) | ||
227 | { | ||
228 | #ifdef MINGW | ||
229 | SetErrnoFromWinsockError (WSAGetLastError ()); | ||
230 | #endif | ||
231 | GNUNET_free (ret); | ||
232 | return NULL; | ||
233 | } | ||
234 | #ifndef MINGW | ||
235 | if (ret->fd >= FD_SETSIZE) | ||
236 | { | ||
237 | GNUNET_break (0 == close (ret->fd)); | ||
238 | GNUNET_free (ret); | ||
239 | errno = EMFILE; | ||
240 | return NULL; | ||
241 | } | ||
242 | #endif | ||
243 | if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO)) | ||
244 | |||
245 | { | ||
246 | |||
247 | /* we might want to treat this one as fatal... */ | ||
248 | GNUNET_break (0); | ||
249 | GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret)); | ||
250 | return NULL; | 280 | return NULL; |
251 | } | ||
252 | |||
253 | #ifndef MINGW | ||
254 | if (GNUNET_OK != socket_set_inheritable (ret)) | ||
255 | LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
256 | "socket_set_inheritable"); | ||
257 | #endif | ||
258 | #ifdef DARWIN | ||
259 | socket_set_nosigpipe (ret); | ||
260 | #endif | ||
261 | #ifdef AF_UNIX | ||
262 | if (ret->af != AF_UNIX) | ||
263 | #endif | ||
264 | socket_set_nodelay (ret); | ||
265 | return ret; | 281 | return ret; |
266 | } | 282 | } |
267 | 283 | ||
@@ -677,49 +693,10 @@ GNUNET_NETWORK_socket_create (int domain, int type, int protocol) | |||
677 | struct GNUNET_NETWORK_Handle *ret; | 693 | struct GNUNET_NETWORK_Handle *ret; |
678 | 694 | ||
679 | ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle)); | 695 | ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle)); |
680 | ret->af = domain; | ||
681 | ret->fd = socket (domain, type, protocol); | 696 | ret->fd = socket (domain, type, protocol); |
682 | if (INVALID_SOCKET == ret->fd) | 697 | if (GNUNET_OK != |
683 | { | 698 | initialize_network_handle (ret, domain, type)) |
684 | #ifdef MINGW | ||
685 | SetErrnoFromWinsockError (WSAGetLastError ()); | ||
686 | #endif | ||
687 | GNUNET_free (ret); | ||
688 | return NULL; | 699 | return NULL; |
689 | } | ||
690 | |||
691 | #ifndef MINGW | ||
692 | if (ret->fd >= FD_SETSIZE) | ||
693 | { | ||
694 | GNUNET_break (0 == close (ret->fd)); | ||
695 | GNUNET_free (ret); | ||
696 | errno = EMFILE; | ||
697 | return NULL; | ||
698 | } | ||
699 | |||
700 | #endif | ||
701 | if (GNUNET_SYSERR == socket_set_blocking (ret, GNUNET_NO)) | ||
702 | { | ||
703 | /* we might want to treat this one as fatal... */ | ||
704 | GNUNET_break (0); | ||
705 | GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret)); | ||
706 | return NULL; | ||
707 | } | ||
708 | |||
709 | #ifndef MINGW | ||
710 | if (GNUNET_OK != socket_set_inheritable (ret)) | ||
711 | LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
712 | "socket_set_inheritable"); | ||
713 | #endif | ||
714 | #ifdef DARWIN | ||
715 | socket_set_nosigpipe (ret); | ||
716 | #endif | ||
717 | if ((type == SOCK_STREAM) | ||
718 | #ifdef AF_UNIX | ||
719 | && (domain != AF_UNIX) | ||
720 | #endif | ||
721 | ) | ||
722 | socket_set_nodelay (ret); | ||
723 | return ret; | 700 | return ret; |
724 | } | 701 | } |
725 | 702 | ||