diff options
author | Nils Durner <durner@gnunet.org> | 2009-08-23 22:11:49 +0000 |
---|---|---|
committer | Nils Durner <durner@gnunet.org> | 2009-08-23 22:11:49 +0000 |
commit | 2518cfc0a86865ebe4d0550e0013ed52a494231b (patch) | |
tree | e9a130b782597e18bcff24a9fdab6e5c6aae1f9c | |
parent | 2ae973618f3b51fa9bbf5532eaa1352cafc24ecc (diff) | |
download | gnunet-2518cfc0a86865ebe4d0550e0013ed52a494231b.tar.gz gnunet-2518cfc0a86865ebe4d0550e0013ed52a494231b.zip |
low level network API
35 files changed, 1845 insertions, 646 deletions
diff --git a/src/hostlist/hostlist-client.c b/src/hostlist/hostlist-client.c index 468c6b64c..cc24dc135 100644 --- a/src/hostlist/hostlist-client.c +++ b/src/hostlist/hostlist-client.c | |||
@@ -380,11 +380,17 @@ run_multi () | |||
380 | fd_set ws; | 380 | fd_set ws; |
381 | fd_set es; | 381 | fd_set es; |
382 | int max; | 382 | int max; |
383 | struct GNUNET_NETWORK_FDSet *grs; | ||
384 | struct GNUNET_NETWORK_FDSet *gws; | ||
385 | struct GNUNET_NETWORK_FDSet *ges; | ||
383 | 386 | ||
384 | max = 0; | 387 | max = 0; |
385 | FD_ZERO (&rs); | 388 | FD_ZERO (&rs); |
386 | FD_ZERO (&ws); | 389 | FD_ZERO (&ws); |
387 | FD_ZERO (&es); | 390 | FD_ZERO (&es); |
391 | grs = GNUNET_NETWORK_fdset_create (); | ||
392 | gws = GNUNET_NETWORK_fdset_create (); | ||
393 | ges = GNUNET_NETWORK_fdset_create (); | ||
388 | mret = curl_multi_fdset (multi, &rs, &ws, &es, &max); | 394 | mret = curl_multi_fdset (multi, &rs, &ws, &es, &max); |
389 | if (mret != CURLM_OK) | 395 | if (mret != CURLM_OK) |
390 | { | 396 | { |
@@ -395,17 +401,22 @@ run_multi () | |||
395 | clean_up (); | 401 | clean_up (); |
396 | return; | 402 | return; |
397 | } | 403 | } |
404 | GNUNET_NETWORK_fdset_copy_native (grs, &rs, max); | ||
405 | GNUNET_NETWORK_fdset_copy_native (gws, &ws, max); | ||
406 | GNUNET_NETWORK_fdset_copy_native (ges, &es, max); | ||
398 | current_task | 407 | current_task |
399 | = GNUNET_SCHEDULER_add_select (sched, | 408 | = GNUNET_SCHEDULER_add_select (sched, |
400 | GNUNET_NO, | 409 | GNUNET_NO, |
401 | GNUNET_SCHEDULER_PRIORITY_DEFAULT, | 410 | GNUNET_SCHEDULER_PRIORITY_DEFAULT, |
402 | GNUNET_SCHEDULER_NO_TASK, | 411 | GNUNET_SCHEDULER_NO_TASK, |
403 | GNUNET_TIME_UNIT_MINUTES, | 412 | GNUNET_TIME_UNIT_MINUTES, |
404 | max, | 413 | grs, |
405 | &rs, | 414 | gws, |
406 | &ws, | ||
407 | &multi_ready, | 415 | &multi_ready, |
408 | multi); | 416 | multi); |
417 | GNUNET_NETWORK_fdset_destroy (ges); | ||
418 | GNUNET_NETWORK_fdset_destroy (gws); | ||
419 | GNUNET_NETWORK_fdset_destroy (grs); | ||
409 | } | 420 | } |
410 | 421 | ||
411 | 422 | ||
diff --git a/src/hostlist/hostlist-server.c b/src/hostlist/hostlist-server.c index 5cc26c409..03e3c5d69 100644 --- a/src/hostlist/hostlist-server.c +++ b/src/hostlist/hostlist-server.c | |||
@@ -221,6 +221,9 @@ prepare_daemon () | |||
221 | fd_set rs; | 221 | fd_set rs; |
222 | fd_set ws; | 222 | fd_set ws; |
223 | fd_set es; | 223 | fd_set es; |
224 | struct GNUNET_NETWORK_FDSet *wrs; | ||
225 | struct GNUNET_NETWORK_FDSet *wws; | ||
226 | struct GNUNET_NETWORK_FDSet *wes; | ||
224 | int max; | 227 | int max; |
225 | unsigned long long timeout; | 228 | unsigned long long timeout; |
226 | int haveto; | 229 | int haveto; |
@@ -229,6 +232,9 @@ prepare_daemon () | |||
229 | FD_ZERO(&rs); | 232 | FD_ZERO(&rs); |
230 | FD_ZERO(&ws); | 233 | FD_ZERO(&ws); |
231 | FD_ZERO(&es); | 234 | FD_ZERO(&es); |
235 | wrs = GNUNET_NETWORK_fdset_create (); | ||
236 | wes = GNUNET_NETWORK_fdset_create (); | ||
237 | wws = GNUNET_NETWORK_fdset_create (); | ||
232 | max = -1; | 238 | max = -1; |
233 | GNUNET_assert (MHD_YES == | 239 | GNUNET_assert (MHD_YES == |
234 | MHD_get_fdset (daemon_handle, | 240 | MHD_get_fdset (daemon_handle, |
@@ -241,17 +247,22 @@ prepare_daemon () | |||
241 | tv.value = (uint64_t) timeout; | 247 | tv.value = (uint64_t) timeout; |
242 | else | 248 | else |
243 | tv = GNUNET_TIME_UNIT_FOREVER_REL; | 249 | tv = GNUNET_TIME_UNIT_FOREVER_REL; |
250 | GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max); | ||
251 | GNUNET_NETWORK_fdset_copy_native (wws, &ws, max); | ||
252 | GNUNET_NETWORK_fdset_copy_native (wes, &es, max); | ||
244 | hostlist_task | 253 | hostlist_task |
245 | = GNUNET_SCHEDULER_add_select (sched, | 254 | = GNUNET_SCHEDULER_add_select (sched, |
246 | GNUNET_NO, | 255 | GNUNET_NO, |
247 | GNUNET_SCHEDULER_PRIORITY_HIGH, | 256 | GNUNET_SCHEDULER_PRIORITY_HIGH, |
248 | GNUNET_SCHEDULER_NO_TASK, | 257 | GNUNET_SCHEDULER_NO_TASK, |
249 | tv, | 258 | tv, |
250 | max, | 259 | wrs, |
251 | &rs, | 260 | wws, |
252 | &ws, | ||
253 | &run_daemon, | 261 | &run_daemon, |
254 | NULL); | 262 | NULL); |
263 | GNUNET_NETWORK_fdset_destroy (wrs); | ||
264 | GNUNET_NETWORK_fdset_destroy (wws); | ||
265 | GNUNET_NETWORK_fdset_destroy (wes); | ||
255 | } | 266 | } |
256 | 267 | ||
257 | 268 | ||
diff --git a/src/include/Makefile.am b/src/include/Makefile.am index 201deeb4f..69954fd30 100644 --- a/src/include/Makefile.am +++ b/src/include/Makefile.am | |||
@@ -18,6 +18,7 @@ gnunetinclude_HEADERS = \ | |||
18 | gnunet_constants.h \ | 18 | gnunet_constants.h \ |
19 | gnunet_configuration_lib.h \ | 19 | gnunet_configuration_lib.h \ |
20 | gnunet_container_lib.h \ | 20 | gnunet_container_lib.h \ |
21 | gnunet_connection_lib.h \ | ||
21 | gnunet_core_service.h \ | 22 | gnunet_core_service.h \ |
22 | gnunet_crypto_lib.h \ | 23 | gnunet_crypto_lib.h \ |
23 | gnunet_datacache_lib.h \ | 24 | gnunet_datacache_lib.h \ |
diff --git a/src/include/gnunet_client_lib.h b/src/include/gnunet_client_lib.h index b73691b79..eb75e8d1e 100644 --- a/src/include/gnunet_client_lib.h +++ b/src/include/gnunet_client_lib.h | |||
@@ -37,7 +37,7 @@ extern "C" | |||
37 | 37 | ||
38 | #include "gnunet_common.h" | 38 | #include "gnunet_common.h" |
39 | #include "gnunet_configuration_lib.h" | 39 | #include "gnunet_configuration_lib.h" |
40 | #include "gnunet_network_lib.h" | 40 | #include "gnunet_connection_lib.h" |
41 | #include "gnunet_scheduler_lib.h" | 41 | #include "gnunet_scheduler_lib.h" |
42 | #include "gnunet_time_lib.h" | 42 | #include "gnunet_time_lib.h" |
43 | 43 | ||
diff --git a/src/include/gnunet_connection_lib.h b/src/include/gnunet_connection_lib.h new file mode 100644 index 000000000..bc4aeea9b --- /dev/null +++ b/src/include/gnunet_connection_lib.h | |||
@@ -0,0 +1,330 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009 Christian Grothoff (and other contributing authors) | ||
4 | |||
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 | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file include/gnunet_connection_lib.h | ||
23 | * @brief basic, low-level TCP networking interface | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #ifndef GNUNET_NETWORK_LIB_H | ||
27 | #define GNUNET_NETWORK_LIB_H | ||
28 | |||
29 | #ifdef __cplusplus | ||
30 | extern "C" | ||
31 | { | ||
32 | #if 0 /* keep Emacsens' auto-indent happy */ | ||
33 | } | ||
34 | #endif | ||
35 | #endif | ||
36 | |||
37 | #include "gnunet_network_lib.h" | ||
38 | #include "gnunet_scheduler_lib.h" | ||
39 | #include "gnunet_time_lib.h" | ||
40 | |||
41 | /** | ||
42 | * Timeout we use on TCP connect before trying another | ||
43 | * result from the DNS resolver. 5s. | ||
44 | */ | ||
45 | #define GNUNET_NETWORK_CONNECT_RETRY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) | ||
46 | |||
47 | /** | ||
48 | * @brief handle for a network connection | ||
49 | */ | ||
50 | struct GNUNET_NETWORK_ConnectionHandle; | ||
51 | |||
52 | |||
53 | /** | ||
54 | * Function to call for access control checks. | ||
55 | * | ||
56 | * @param cls closure | ||
57 | * @param addr address | ||
58 | * @param addrlen length of address | ||
59 | * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR | ||
60 | * for unknown address family (will be denied). | ||
61 | */ | ||
62 | typedef int (*GNUNET_NETWORK_AccessCheck) (void *cls, | ||
63 | const struct sockaddr * addr, | ||
64 | socklen_t addrlen); | ||
65 | |||
66 | |||
67 | /** | ||
68 | * Callback function for data received from the network. Note that | ||
69 | * both "available" and "err" would be 0 if the read simply timed out. | ||
70 | * | ||
71 | * @param cls closure | ||
72 | * @param buf pointer to received data | ||
73 | * @param available number of bytes availabe in "buf", | ||
74 | * possibly 0 (on errors) | ||
75 | * @param addr address of the sender | ||
76 | * @param addrlen size of addr | ||
77 | * @param errCode value of errno (on errors receiving) | ||
78 | */ | ||
79 | typedef void (*GNUNET_NETWORK_Receiver) (void *cls, | ||
80 | const void *buf, | ||
81 | size_t available, | ||
82 | const struct sockaddr * addr, | ||
83 | socklen_t addrlen, int errCode); | ||
84 | |||
85 | |||
86 | /** | ||
87 | * Create a socket handle by boxing an existing OS socket. The OS | ||
88 | * socket should henceforth be no longer used directly. | ||
89 | * GNUNET_socket_destroy will close it. | ||
90 | * | ||
91 | * @param sched scheduler to use | ||
92 | * @param osSocket existing socket to box | ||
93 | * @param maxbuf maximum write buffer size for the socket (use | ||
94 | * 0 for sockets that need no write buffers, such as listen sockets) | ||
95 | * @return the boxed socket handle | ||
96 | */ | ||
97 | struct GNUNET_NETWORK_ConnectionHandle | ||
98 | *GNUNET_NETWORK_connection_create_from_existing (struct | ||
99 | GNUNET_SCHEDULER_Handle | ||
100 | *sched, | ||
101 | struct | ||
102 | GNUNET_NETWORK_Descriptor | ||
103 | *osSocket, size_t maxbuf); | ||
104 | |||
105 | |||
106 | /** | ||
107 | * Create a socket handle by accepting on a listen socket. This | ||
108 | * function may block if the listen socket has no connection ready. | ||
109 | * | ||
110 | * @param sched scheduler to use | ||
111 | * @param access function to use to check if access is allowed | ||
112 | * @param access_cls closure for access | ||
113 | * @param lsock listen socket | ||
114 | * @param maxbuf maximum write buffer size for the socket (use | ||
115 | * 0 for sockets that need no write buffers, such as listen sockets) | ||
116 | * @return the socket handle, NULL on error (for example, access refused) | ||
117 | */ | ||
118 | struct GNUNET_NETWORK_ConnectionHandle | ||
119 | *GNUNET_NETWORK_connection_create_from_accept (struct | ||
120 | GNUNET_SCHEDULER_Handle | ||
121 | *sched, | ||
122 | GNUNET_NETWORK_AccessCheck | ||
123 | access, void *access_cls, | ||
124 | struct | ||
125 | GNUNET_NETWORK_Descriptor | ||
126 | *lsock, size_t maxbuf); | ||
127 | |||
128 | |||
129 | /** | ||
130 | * Create a socket handle by (asynchronously) connecting to a host. | ||
131 | * This function returns immediately, even if the connection has not | ||
132 | * yet been established. This function only creates TCP connections. | ||
133 | * | ||
134 | * @param sched scheduler to use | ||
135 | * @param hostname name of the host to connect to | ||
136 | * @param port port to connect to | ||
137 | * @param maxbuf maximum write buffer size for the socket (use | ||
138 | * 0 for sockets that need no write buffers, such as listen sockets) | ||
139 | * @return the socket handle | ||
140 | */ | ||
141 | struct GNUNET_NETWORK_ConnectionHandle | ||
142 | *GNUNET_NETWORK_connection_create_from_connect (struct | ||
143 | GNUNET_SCHEDULER_Handle | ||
144 | *sched, | ||
145 | const char *hostname, | ||
146 | uint16_t port, | ||
147 | size_t maxbuf); | ||
148 | |||
149 | |||
150 | |||
151 | /** | ||
152 | * Create a socket handle by (asynchronously) connecting to a host. | ||
153 | * This function returns immediately, even if the connection has not | ||
154 | * yet been established. This function only creates TCP connections. | ||
155 | * | ||
156 | * @param sched scheduler to use | ||
157 | * @param af_family address family to use | ||
158 | * @param serv_addr server address | ||
159 | * @param addrlen length of server address | ||
160 | * @param maxbuf maximum write buffer size for the socket (use | ||
161 | * 0 for sockets that need no write buffers, such as listen sockets) | ||
162 | * @return the socket handle | ||
163 | */ | ||
164 | struct GNUNET_NETWORK_ConnectionHandle | ||
165 | *GNUNET_NETWORK_connection_create_from_sockaddr (struct | ||
166 | GNUNET_SCHEDULER_Handle | ||
167 | *sched, int af_family, | ||
168 | const struct sockaddr | ||
169 | *serv_addr, | ||
170 | socklen_t addrlen, | ||
171 | size_t maxbuf); | ||
172 | |||
173 | /** | ||
174 | * Check if socket is valid (no fatal errors have happened so far). | ||
175 | * Note that a socket that is still trying to connect is considered | ||
176 | * valid. | ||
177 | * | ||
178 | * @param sock socket to check | ||
179 | * @return GNUNET_YES if valid, GNUNET_NO otherwise | ||
180 | */ | ||
181 | int GNUNET_NETWORK_connection_check (struct GNUNET_NETWORK_ConnectionHandle | ||
182 | *sock); | ||
183 | |||
184 | |||
185 | /** | ||
186 | * Obtain the network address of the other party. | ||
187 | * | ||
188 | * @param sock the client to get the address for | ||
189 | * @param addr where to store the address | ||
190 | * @param addrlen where to store the length of the address | ||
191 | * @return GNUNET_OK on success | ||
192 | */ | ||
193 | int GNUNET_NETWORK_connection_get_address (struct | ||
194 | GNUNET_NETWORK_ConnectionHandle | ||
195 | *sock, void **addr, | ||
196 | size_t * addrlen); | ||
197 | |||
198 | /** | ||
199 | * Close the socket and free associated resources. Pending | ||
200 | * transmissions are simply dropped. A pending receive call will be | ||
201 | * called with an error code of "EPIPE". | ||
202 | * | ||
203 | * @param sock socket to destroy | ||
204 | */ | ||
205 | void GNUNET_NETWORK_connection_destroy (struct GNUNET_NETWORK_ConnectionHandle | ||
206 | *sock); | ||
207 | |||
208 | |||
209 | /** | ||
210 | * Receive data from the given socket. Note that this function will | ||
211 | * call "receiver" asynchronously using the scheduler. It will | ||
212 | * "immediately" return. Note that there MUST only be one active | ||
213 | * receive call per socket at any given point in time (so do not | ||
214 | * call receive again until the receiver callback has been invoked). | ||
215 | * | ||
216 | * @param sock socket handle | ||
217 | * @param max maximum number of bytes to read | ||
218 | * @param timeout maximum amount of time to wait | ||
219 | * @param receiver function to call with received data | ||
220 | * @param receiver_cls closure for receiver | ||
221 | * @return scheduler task ID used for receiving, GNUNET_SCHEDULER_NO_TASK on error | ||
222 | */ | ||
223 | GNUNET_SCHEDULER_TaskIdentifier | ||
224 | GNUNET_NETWORK_connection_receive (struct GNUNET_NETWORK_ConnectionHandle | ||
225 | *sock, size_t max, | ||
226 | struct GNUNET_TIME_Relative timeout, | ||
227 | GNUNET_NETWORK_Receiver receiver, | ||
228 | void *receiver_cls); | ||
229 | |||
230 | |||
231 | /** | ||
232 | * Cancel receive job on the given socket. Note that the | ||
233 | * receiver callback must not have been called yet in order | ||
234 | * for the cancellation to be valid. | ||
235 | * | ||
236 | * @param sock socket handle | ||
237 | * @param task task identifier returned from the receive call | ||
238 | * @return closure of the original receiver callback | ||
239 | */ | ||
240 | void *GNUNET_NETWORK_connection_receive_cancel (struct | ||
241 | GNUNET_NETWORK_ConnectionHandle | ||
242 | *sock, | ||
243 | GNUNET_SCHEDULER_TaskIdentifier | ||
244 | task); | ||
245 | |||
246 | |||
247 | /** | ||
248 | * Function called to notify a client about the socket | ||
249 | * begin ready to queue more data. "buf" will be | ||
250 | * NULL and "size" zero if the socket was closed for | ||
251 | * writing in the meantime. | ||
252 | * | ||
253 | * @param cls closure | ||
254 | * @param size number of bytes available in buf | ||
255 | * @param buf where the callee should write the message | ||
256 | * @return number of bytes written to buf | ||
257 | */ | ||
258 | typedef size_t (*GNUNET_NETWORK_TransmitReadyNotify) (void *cls, | ||
259 | size_t size, void *buf); | ||
260 | |||
261 | |||
262 | /** | ||
263 | * Opaque handle that can be used to cancel | ||
264 | * a transmit-ready notification. | ||
265 | */ | ||
266 | struct GNUNET_NETWORK_TransmitHandle; | ||
267 | |||
268 | /** | ||
269 | * Ask the socket to call us once the specified number of bytes | ||
270 | * are free in the transmission buffer. May call the notify | ||
271 | * method immediately if enough space is available. Note that | ||
272 | * this function will abort if "size" is greater than | ||
273 | * "maxbuf" (as specified when the socket handle was created). | ||
274 | * | ||
275 | * Note that "notify" will be called either when enough | ||
276 | * buffer space is available OR when the socket is destroyed. | ||
277 | * The size parameter given to notify is guaranteed to be | ||
278 | * larger or equal to size if the buffer is ready, or zero | ||
279 | * if the socket was destroyed (or at least closed for | ||
280 | * writing). Finally, any time before 'notify' is called, a | ||
281 | * client may call "notify_transmit_ready_cancel" to cancel | ||
282 | * the transmission request. | ||
283 | * | ||
284 | * Only one transmission request can be scheduled at the same | ||
285 | * time. Notify will be run with the same scheduler priority | ||
286 | * as that of the caller. | ||
287 | * | ||
288 | * @param sock socket | ||
289 | * @param size number of bytes to send | ||
290 | * @param timeout after how long should we give up (and call | ||
291 | * notify with buf NULL and size 0)? | ||
292 | * @param notify function to call when buffer space is available | ||
293 | * @param notify_cls closure for notify | ||
294 | * @return non-NULL if the notify callback was queued, | ||
295 | * NULL if we are already going to notify someone else (busy) | ||
296 | */ | ||
297 | struct GNUNET_NETWORK_TransmitHandle | ||
298 | *GNUNET_NETWORK_connection_notify_transmit_ready (struct | ||
299 | GNUNET_NETWORK_ConnectionHandle | ||
300 | *sock, size_t size, | ||
301 | struct | ||
302 | GNUNET_TIME_Relative | ||
303 | timeout, | ||
304 | GNUNET_NETWORK_TransmitReadyNotify | ||
305 | notify, void *notify_cls); | ||
306 | |||
307 | |||
308 | /** | ||
309 | * Cancel the specified transmission-ready | ||
310 | * notification. | ||
311 | * | ||
312 | * @param h handle for notification to cancel | ||
313 | */ | ||
314 | void | ||
315 | GNUNET_NETWORK_connection_notify_transmit_ready_cancel (struct | ||
316 | GNUNET_NETWORK_TransmitHandle | ||
317 | *h); | ||
318 | |||
319 | |||
320 | #if 0 /* keep Emacsens' auto-indent happy */ | ||
321 | { | ||
322 | #endif | ||
323 | #ifdef __cplusplus | ||
324 | } | ||
325 | #endif | ||
326 | |||
327 | |||
328 | /* ifndef GNUNET_NETWORK_LIB_H */ | ||
329 | #endif | ||
330 | /* end of gnunet_connection_lib.h */ | ||
diff --git a/src/include/gnunet_disk_lib.h b/src/include/gnunet_disk_lib.h index 49f2ece48..a5f279f05 100644 --- a/src/include/gnunet_disk_lib.h +++ b/src/include/gnunet_disk_lib.h | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | (C) 2001, 2002, 2003, 2004, 2005, 2006 Christian Grothoff (and other contributing authors) | 3 | (C) 2001, 2002, 2003, 2004, 2005, 2006, 2009 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 |
@@ -76,6 +76,8 @@ enum GNUNET_DISK_Seek {GNUNET_SEEK_SET, GNUNET_SEEK_CUR, GNUNET_SEEK_END}; | |||
76 | 76 | ||
77 | struct GNUNET_DISK_FileHandle; | 77 | struct GNUNET_DISK_FileHandle; |
78 | 78 | ||
79 | struct GNUNET_DISK_PipeHandle; | ||
80 | |||
79 | /** | 81 | /** |
80 | * Get the number of blocks that are left on the partition that | 82 | * Get the number of blocks that are left on the partition that |
81 | * contains the given file (for normal users). | 83 | * contains the given file (for normal users). |
@@ -150,6 +152,19 @@ GNUNET_DISK_mktemp (const char *template); | |||
150 | */ | 152 | */ |
151 | struct GNUNET_DISK_FileHandle *GNUNET_DISK_file_open (const char *fn, int flags, ...); | 153 | struct GNUNET_DISK_FileHandle *GNUNET_DISK_file_open (const char *fn, int flags, ...); |
152 | 154 | ||
155 | /** | ||
156 | * Creates an interprocess channel | ||
157 | * @param blocking creates an asynchronous pipe if set to GNUNET_NO | ||
158 | * @return handle to the new pipe, NULL on error | ||
159 | */ | ||
160 | struct GNUNET_DISK_PipeHandle *GNUNET_DISK_pipe (int blocking); | ||
161 | |||
162 | /** | ||
163 | * Closes an interprocess channel | ||
164 | * @param p pipe | ||
165 | * @return GNUNET_OK on success, GNUNET_SYSERR otherwise | ||
166 | */ | ||
167 | int GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p); | ||
153 | 168 | ||
154 | /** | 169 | /** |
155 | * Close an open file. | 170 | * Close an open file. |
@@ -159,6 +174,14 @@ struct GNUNET_DISK_FileHandle *GNUNET_DISK_file_open (const char *fn, int flags, | |||
159 | */ | 174 | */ |
160 | int GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h); | 175 | int GNUNET_DISK_file_close (struct GNUNET_DISK_FileHandle *h); |
161 | 176 | ||
177 | /** | ||
178 | * Get the handle to a particular pipe end | ||
179 | * @param p pipe | ||
180 | * @param n number of the end | ||
181 | */ | ||
182 | const struct GNUNET_DISK_FileHandle *GNUNET_DISK_pipe_handle (const struct | ||
183 | GNUNET_DISK_PipeHandle | ||
184 | *p, int n); | ||
162 | 185 | ||
163 | /** | 186 | /** |
164 | * Read the contents of a binary file into a buffer. | 187 | * Read the contents of a binary file into a buffer. |
diff --git a/src/include/gnunet_network_lib.h b/src/include/gnunet_network_lib.h index 68ca346e5..baba3aba8 100644 --- a/src/include/gnunet_network_lib.h +++ b/src/include/gnunet_network_lib.h | |||
@@ -20,11 +20,12 @@ | |||
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file include/gnunet_network_lib.h | 22 | * @file include/gnunet_network_lib.h |
23 | * @brief basic, low-level TCP networking interface | 23 | * @brief basic low-level networking interface |
24 | * @author Christian Grothoff | 24 | * @author Nils Durner |
25 | */ | 25 | */ |
26 | #ifndef GNUNET_NETWORK_LIB_H | 26 | |
27 | #define GNUNET_NETWORK_LIB_H | 27 | #ifndef GNUNET_NETWORK_LIB_H_ |
28 | #define GNUNET_NETWORK_LIB_H_ | ||
28 | 29 | ||
29 | #ifdef __cplusplus | 30 | #ifdef __cplusplus |
30 | extern "C" | 31 | extern "C" |
@@ -34,265 +35,102 @@ extern "C" | |||
34 | #endif | 35 | #endif |
35 | #endif | 36 | #endif |
36 | 37 | ||
37 | #include "gnunet_scheduler_lib.h" | ||
38 | #include "gnunet_time_lib.h" | 38 | #include "gnunet_time_lib.h" |
39 | 39 | ||
40 | /** | ||
41 | * Timeout we use on TCP connect before trying another | ||
42 | * result from the DNS resolver. 5s. | ||
43 | */ | ||
44 | #define GNUNET_NETWORK_CONNECT_RETRY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) | ||
45 | 40 | ||
46 | /** | 41 | /** |
47 | * @brief handle for a network connection | 42 | * @brief handle to a socket |
48 | */ | 43 | */ |
49 | struct GNUNET_NETWORK_ConnectionHandle; | 44 | struct GNUNET_NETWORK_Descriptor; |
50 | |||
51 | 45 | ||
52 | /** | 46 | /** |
53 | * Function to call for access control checks. | 47 | * @brief collection of IO descriptors |
54 | * | ||
55 | * @param cls closure | ||
56 | * @param addr address | ||
57 | * @param addrlen length of address | ||
58 | * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR | ||
59 | * for unknown address family (will be denied). | ||
60 | */ | 48 | */ |
61 | typedef int (*GNUNET_NETWORK_AccessCheck) (void *cls, | 49 | struct GNUNET_NETWORK_FDSet; |
62 | const struct sockaddr * addr, | ||
63 | socklen_t addrlen); | ||
64 | 50 | ||
51 | struct GNUNET_DISK_FileHandle; | ||
65 | 52 | ||
66 | /** | 53 | struct GNUNET_NETWORK_Descriptor *GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Descriptor *desc, |
67 | * Callback function for data received from the network. Note that | 54 | struct sockaddr *address, |
68 | * both "available" and "err" would be 0 if the read simply timed out. | 55 | socklen_t *address_len); |
69 | * | ||
70 | * @param cls closure | ||
71 | * @param buf pointer to received data | ||
72 | * @param available number of bytes availabe in "buf", | ||
73 | * possibly 0 (on errors) | ||
74 | * @param addr address of the sender | ||
75 | * @param addrlen size of addr | ||
76 | * @param errCode value of errno (on errors receiving) | ||
77 | */ | ||
78 | typedef void (*GNUNET_NETWORK_Receiver) (void *cls, | ||
79 | const void *buf, | ||
80 | size_t available, | ||
81 | const struct sockaddr * addr, | ||
82 | socklen_t addrlen, int errCode); | ||
83 | 56 | ||
57 | int GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Descriptor *desc, | ||
58 | const struct sockaddr *address, socklen_t address_len); | ||
84 | 59 | ||
85 | /** | 60 | int GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Descriptor *desc); |
86 | * Create a socket handle by boxing an existing OS socket. The OS | ||
87 | * socket should henceforth be no longer used directly. | ||
88 | * GNUNET_socket_destroy will close it. | ||
89 | * | ||
90 | * @param sched scheduler to use | ||
91 | * @param osSocket existing socket to box | ||
92 | * @param maxbuf maximum write buffer size for the socket (use | ||
93 | * 0 for sockets that need no write buffers, such as listen sockets) | ||
94 | * @return the boxed socket handle | ||
95 | */ | ||
96 | struct GNUNET_NETWORK_ConnectionHandle | ||
97 | *GNUNET_NETWORK_connection_create_from_existing (struct GNUNET_SCHEDULER_Handle | ||
98 | *sched, int osSocket, | ||
99 | size_t maxbuf); | ||
100 | 61 | ||
62 | int GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Descriptor *desc, | ||
63 | const struct sockaddr *address, socklen_t address_len); | ||
101 | 64 | ||
102 | /** | 65 | int GNUNET_NETWORK_socket_getsockopt(const struct GNUNET_NETWORK_Descriptor *desc, int level, int optname, |
103 | * Create a socket handle by accepting on a listen socket. This | 66 | void *optval, socklen_t *optlen); |
104 | * function may block if the listen socket has no connection ready. | ||
105 | * | ||
106 | * @param sched scheduler to use | ||
107 | * @param access function to use to check if access is allowed | ||
108 | * @param access_cls closure for access | ||
109 | * @param lsock listen socket | ||
110 | * @param maxbuf maximum write buffer size for the socket (use | ||
111 | * 0 for sockets that need no write buffers, such as listen sockets) | ||
112 | * @return the socket handle, NULL on error (for example, access refused) | ||
113 | */ | ||
114 | struct GNUNET_NETWORK_ConnectionHandle | ||
115 | *GNUNET_NETWORK_connection_create_from_accept (struct GNUNET_SCHEDULER_Handle | ||
116 | *sched, | ||
117 | GNUNET_NETWORK_AccessCheck | ||
118 | access, void *access_cls, | ||
119 | int lsock, size_t maxbuf); | ||
120 | 67 | ||
68 | int GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Descriptor *desc, int backlog); | ||
121 | 69 | ||
122 | /** | 70 | ssize_t GNUNET_NETWORK_socket_read (const struct GNUNET_NETWORK_Descriptor *desc, void *buf, |
123 | * Create a socket handle by (asynchronously) connecting to a host. | 71 | size_t nbyte); |
124 | * This function returns immediately, even if the connection has not | ||
125 | * yet been established. This function only creates TCP connections. | ||
126 | * | ||
127 | * @param sched scheduler to use | ||
128 | * @param hostname name of the host to connect to | ||
129 | * @param port port to connect to | ||
130 | * @param maxbuf maximum write buffer size for the socket (use | ||
131 | * 0 for sockets that need no write buffers, such as listen sockets) | ||
132 | * @return the socket handle | ||
133 | */ | ||
134 | struct GNUNET_NETWORK_ConnectionHandle | ||
135 | *GNUNET_NETWORK_connection_create_from_connect (struct GNUNET_SCHEDULER_Handle | ||
136 | *sched, const char *hostname, | ||
137 | uint16_t port, size_t maxbuf); | ||
138 | 72 | ||
73 | ssize_t GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Descriptor *desc, void *buffer, | ||
74 | size_t length, int flags); | ||
139 | 75 | ||
76 | int GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds, | ||
77 | struct GNUNET_NETWORK_FDSet *wfds, struct GNUNET_NETWORK_FDSet *efds, | ||
78 | struct GNUNET_TIME_Relative timeout); | ||
140 | 79 | ||
141 | /** | 80 | /** |
142 | * Create a socket handle by (asynchronously) connecting to a host. | 81 | * Set if a socket should use blocking or non-blocking IO. |
143 | * This function returns immediately, even if the connection has not | ||
144 | * yet been established. This function only creates TCP connections. | ||
145 | * | 82 | * |
146 | * @param sched scheduler to use | 83 | * @return GNUNET_OK on success, GNUNET_SYSERR on error |
147 | * @param af_family address family to use | ||
148 | * @param serv_addr server address | ||
149 | * @param addrlen length of server address | ||
150 | * @param maxbuf maximum write buffer size for the socket (use | ||
151 | * 0 for sockets that need no write buffers, such as listen sockets) | ||
152 | * @return the socket handle | ||
153 | */ | 84 | */ |
154 | struct GNUNET_NETWORK_ConnectionHandle | 85 | int GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Descriptor *fd, int doBlock); |
155 | *GNUNET_NETWORK_connection_create_from_sockaddr (struct GNUNET_SCHEDULER_Handle | ||
156 | *sched, int af_family, | ||
157 | const struct sockaddr | ||
158 | *serv_addr, socklen_t addrlen, | ||
159 | size_t maxbuf); | ||
160 | 86 | ||
161 | /** | 87 | ssize_t GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Descriptor *desc, |
162 | * Check if socket is valid (no fatal errors have happened so far). | 88 | const void *buffer, size_t length, int flags); |
163 | * Note that a socket that is still trying to connect is considered | ||
164 | * valid. | ||
165 | * | ||
166 | * @param sock socket to check | ||
167 | * @return GNUNET_YES if valid, GNUNET_NO otherwise | ||
168 | */ | ||
169 | int GNUNET_NETWORK_connection_check (struct GNUNET_NETWORK_ConnectionHandle *sock); | ||
170 | 89 | ||
90 | ssize_t GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Descriptor *desc, | ||
91 | const void *message, size_t length, int flags, | ||
92 | const struct sockaddr *dest_addr, | ||
93 | socklen_t dest_len); | ||
171 | 94 | ||
172 | /** | 95 | int GNUNET_NETWORK_socket_setsockopt(struct GNUNET_NETWORK_Descriptor *fd, int level, int option_name, |
173 | * Obtain the network address of the other party. | 96 | const void *option_value, socklen_t option_len); |
174 | * | ||
175 | * @param sock the client to get the address for | ||
176 | * @param addr where to store the address | ||
177 | * @param addrlen where to store the length of the address | ||
178 | * @return GNUNET_OK on success | ||
179 | */ | ||
180 | int GNUNET_NETWORK_connection_get_address (struct GNUNET_NETWORK_ConnectionHandle | ||
181 | *sock, void **addr, size_t * addrlen); | ||
182 | 97 | ||
183 | /** | 98 | int GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Descriptor *desc, int how); |
184 | * Close the socket and free associated resources. Pending | ||
185 | * transmissions are simply dropped. A pending receive call will be | ||
186 | * called with an error code of "EPIPE". | ||
187 | * | ||
188 | * @param sock socket to destroy | ||
189 | */ | ||
190 | void GNUNET_NETWORK_connection_destroy (struct GNUNET_NETWORK_ConnectionHandle *sock); | ||
191 | 99 | ||
100 | struct GNUNET_NETWORK_Descriptor *GNUNET_NETWORK_socket_socket (int domain, int type, int protocol); | ||
192 | 101 | ||
193 | /** | 102 | ssize_t GNUNET_NETWORK_socket_write (const struct GNUNET_NETWORK_Descriptor *desc, |
194 | * Receive data from the given socket. Note that this function will | 103 | const void *buf, size_t nbyte); |
195 | * call "receiver" asynchronously using the scheduler. It will | ||
196 | * "immediately" return. Note that there MUST only be one active | ||
197 | * receive call per socket at any given point in time (so do not | ||
198 | * call receive again until the receiver callback has been invoked). | ||
199 | * | ||
200 | * @param sock socket handle | ||
201 | * @param max maximum number of bytes to read | ||
202 | * @param timeout maximum amount of time to wait | ||
203 | * @param receiver function to call with received data | ||
204 | * @param receiver_cls closure for receiver | ||
205 | * @return scheduler task ID used for receiving, GNUNET_SCHEDULER_NO_TASK on error | ||
206 | */ | ||
207 | GNUNET_SCHEDULER_TaskIdentifier | ||
208 | GNUNET_NETWORK_connection_receive (struct GNUNET_NETWORK_ConnectionHandle *sock, | ||
209 | size_t max, | ||
210 | struct GNUNET_TIME_Relative timeout, | ||
211 | GNUNET_NETWORK_Receiver receiver, void *receiver_cls); | ||
212 | 104 | ||
213 | 105 | ||
214 | /** | 106 | void GNUNET_NETWORK_fdset_zero(struct GNUNET_NETWORK_FDSet *fds); |
215 | * Cancel receive job on the given socket. Note that the | ||
216 | * receiver callback must not have been called yet in order | ||
217 | * for the cancellation to be valid. | ||
218 | * | ||
219 | * @param sock socket handle | ||
220 | * @param task task identifier returned from the receive call | ||
221 | * @return closure of the original receiver callback | ||
222 | */ | ||
223 | void *GNUNET_NETWORK_connection_receive_cancel (struct GNUNET_NETWORK_ConnectionHandle *sock, | ||
224 | GNUNET_SCHEDULER_TaskIdentifier task); | ||
225 | 107 | ||
108 | void GNUNET_NETWORK_fdset_set(struct GNUNET_NETWORK_FDSet *fds, | ||
109 | const struct GNUNET_NETWORK_Descriptor *desc); | ||
226 | 110 | ||
227 | /** | 111 | int GNUNET_NETWORK_fdset_isset(const struct GNUNET_NETWORK_FDSet *fds, |
228 | * Function called to notify a client about the socket | 112 | const struct GNUNET_NETWORK_Descriptor *desc); |
229 | * begin ready to queue more data. "buf" will be | ||
230 | * NULL and "size" zero if the socket was closed for | ||
231 | * writing in the meantime. | ||
232 | * | ||
233 | * @param cls closure | ||
234 | * @param size number of bytes available in buf | ||
235 | * @param buf where the callee should write the message | ||
236 | * @return number of bytes written to buf | ||
237 | */ | ||
238 | typedef size_t (*GNUNET_NETWORK_TransmitReadyNotify) (void *cls, | ||
239 | size_t size, void *buf); | ||
240 | 113 | ||
114 | void GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst, | ||
115 | const struct GNUNET_NETWORK_FDSet *src); | ||
241 | 116 | ||
242 | /** | 117 | void GNUNET_NETWORK_fdset_copy(struct GNUNET_NETWORK_FDSet *to, |
243 | * Opaque handle that can be used to cancel | 118 | const struct GNUNET_NETWORK_FDSet *from); |
244 | * a transmit-ready notification. | ||
245 | */ | ||
246 | struct GNUNET_NETWORK_TransmitHandle; | ||
247 | 119 | ||
248 | /** | 120 | void GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to, const fd_set *from, |
249 | * Ask the socket to call us once the specified number of bytes | 121 | int nfds); |
250 | * are free in the transmission buffer. May call the notify | ||
251 | * method immediately if enough space is available. Note that | ||
252 | * this function will abort if "size" is greater than | ||
253 | * "maxbuf" (as specified when the socket handle was created). | ||
254 | * | ||
255 | * Note that "notify" will be called either when enough | ||
256 | * buffer space is available OR when the socket is destroyed. | ||
257 | * The size parameter given to notify is guaranteed to be | ||
258 | * larger or equal to size if the buffer is ready, or zero | ||
259 | * if the socket was destroyed (or at least closed for | ||
260 | * writing). Finally, any time before 'notify' is called, a | ||
261 | * client may call "notify_transmit_ready_cancel" to cancel | ||
262 | * the transmission request. | ||
263 | * | ||
264 | * Only one transmission request can be scheduled at the same | ||
265 | * time. Notify will be run with the same scheduler priority | ||
266 | * as that of the caller. | ||
267 | * | ||
268 | * @param sock socket | ||
269 | * @param size number of bytes to send | ||
270 | * @param timeout after how long should we give up (and call | ||
271 | * notify with buf NULL and size 0)? | ||
272 | * @param notify function to call when buffer space is available | ||
273 | * @param notify_cls closure for notify | ||
274 | * @return non-NULL if the notify callback was queued, | ||
275 | * NULL if we are already going to notify someone else (busy) | ||
276 | */ | ||
277 | struct GNUNET_NETWORK_TransmitHandle | ||
278 | *GNUNET_NETWORK_connection_notify_transmit_ready (struct GNUNET_NETWORK_ConnectionHandle | ||
279 | *sock, size_t size, | ||
280 | struct GNUNET_TIME_Relative timeout, | ||
281 | GNUNET_NETWORK_TransmitReadyNotify | ||
282 | notify, void *notify_cls); | ||
283 | 122 | ||
123 | void GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds, | ||
124 | const struct GNUNET_DISK_FileHandle *h); | ||
284 | 125 | ||
285 | /** | 126 | int GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds, |
286 | * Cancel the specified transmission-ready | 127 | const struct GNUNET_DISK_FileHandle *h); |
287 | * notification. | 128 | |
288 | * | 129 | int GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1, const struct GNUNET_NETWORK_FDSet *fds2); |
289 | * @param h handle for notification to cancel | ||
290 | */ | ||
291 | void | ||
292 | GNUNET_NETWORK_connection_notify_transmit_ready_cancel (struct | ||
293 | GNUNET_NETWORK_TransmitHandle | ||
294 | *h); | ||
295 | 130 | ||
131 | struct GNUNET_NETWORK_FDSet *GNUNET_NETWORK_fdset_create (); | ||
132 | |||
133 | void GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds); | ||
296 | 134 | ||
297 | 135 | ||
298 | #if 0 /* keep Emacsens' auto-indent happy */ | 136 | #if 0 /* keep Emacsens' auto-indent happy */ |
@@ -302,7 +140,4 @@ GNUNET_NETWORK_connection_notify_transmit_ready_cancel (struct | |||
302 | } | 140 | } |
303 | #endif | 141 | #endif |
304 | 142 | ||
305 | 143 | #endif /* GNUNET_NETWORK_LIB_H_ */ | |
306 | /* ifndef GNUNET_NETWORK_LIB_H */ | ||
307 | #endif | ||
308 | /* end of gnunet_network_lib.h */ | ||
diff --git a/src/include/gnunet_scheduler_lib.h b/src/include/gnunet_scheduler_lib.h index d4013e630..86f709ed6 100644 --- a/src/include/gnunet_scheduler_lib.h +++ b/src/include/gnunet_scheduler_lib.h | |||
@@ -35,15 +35,15 @@ extern "C" | |||
35 | #endif | 35 | #endif |
36 | #endif | 36 | #endif |
37 | 37 | ||
38 | #include "gnunet_time_lib.h" | ||
39 | 38 | ||
39 | #include "gnunet_time_lib.h" | ||
40 | #include "gnunet_network_lib.h" | ||
40 | 41 | ||
41 | /** | 42 | /** |
42 | * Opaque handle for the scheduling service. | 43 | * Opaque handle for the scheduling service. |
43 | */ | 44 | */ |
44 | struct GNUNET_SCHEDULER_Handle; | 45 | struct GNUNET_SCHEDULER_Handle; |
45 | 46 | ||
46 | |||
47 | /** | 47 | /** |
48 | * Opaque reference to a task. | 48 | * Opaque reference to a task. |
49 | */ | 49 | */ |
@@ -173,14 +173,14 @@ struct GNUNET_SCHEDULER_TaskContext | |||
173 | * note that additional bits may be set | 173 | * note that additional bits may be set |
174 | * that were not in the original request | 174 | * that were not in the original request |
175 | */ | 175 | */ |
176 | const fd_set *read_ready; | 176 | const struct GNUNET_NETWORK_FDSet *read_ready; |
177 | 177 | ||
178 | /** | 178 | /** |
179 | * Set of file descriptors ready for writing; | 179 | * Set of file descriptors ready for writing; |
180 | * note that additional bits may be set | 180 | * note that additional bits may be set |
181 | * that were not in the original request. | 181 | * that were not in the original request. |
182 | */ | 182 | */ |
183 | const fd_set *write_ready; | 183 | const struct GNUNET_NETWORK_FDSet *write_ready; |
184 | 184 | ||
185 | }; | 185 | }; |
186 | 186 | ||
@@ -345,12 +345,78 @@ GNUNET_SCHEDULER_add_delayed (struct GNUNET_SCHEDULER_Handle *sched, | |||
345 | * only valid until "main" is started! | 345 | * only valid until "main" is started! |
346 | */ | 346 | */ |
347 | GNUNET_SCHEDULER_TaskIdentifier | 347 | GNUNET_SCHEDULER_TaskIdentifier |
348 | GNUNET_SCHEDULER_add_read (struct GNUNET_SCHEDULER_Handle *sched, | 348 | GNUNET_SCHEDULER_add_read_net (struct GNUNET_SCHEDULER_Handle *sched, |
349 | int run_on_shutdown, | ||
350 | enum GNUNET_SCHEDULER_Priority prio, | ||
351 | GNUNET_SCHEDULER_TaskIdentifier prerequisite_task, | ||
352 | struct GNUNET_TIME_Relative delay, | ||
353 | struct GNUNET_NETWORK_Descriptor *rfd, GNUNET_SCHEDULER_Task main, void *cls); | ||
354 | |||
355 | |||
356 | /** | ||
357 | * Schedule a new task to be run with a specified delay or when the | ||
358 | * specified file descriptor is ready for writing. The delay can be | ||
359 | * used as a timeout on the socket being ready. The task will be | ||
360 | * scheduled for execution once either the delay has expired or the | ||
361 | * socket operation is ready. | ||
362 | * | ||
363 | * @param sched scheduler to use | ||
364 | * @param run_on_shutdown run on shutdown? Set this | ||
365 | * argument to GNUNET_NO to skip this task if | ||
366 | * the user requested process termination. | ||
367 | * @param prio how important is this task? | ||
368 | * @param prerequisite_task run this task after the task with the given | ||
369 | * task identifier completes (and any of our other | ||
370 | * conditions, such as delay, read or write-readyness | ||
371 | * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency | ||
372 | * on completion of other tasks. | ||
373 | * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever" | ||
374 | * @param wfd write file-descriptor | ||
375 | * @param main main function of the task | ||
376 | * @param cls closure of task | ||
377 | * @return unique task identifier for the job | ||
378 | * only valid until "main" is started! | ||
379 | */ | ||
380 | GNUNET_SCHEDULER_TaskIdentifier | ||
381 | GNUNET_SCHEDULER_add_write_net (struct GNUNET_SCHEDULER_Handle *sched, | ||
382 | int run_on_shutdown, | ||
383 | enum GNUNET_SCHEDULER_Priority prio, | ||
384 | GNUNET_SCHEDULER_TaskIdentifier prerequisite_task, | ||
385 | struct GNUNET_TIME_Relative delay, | ||
386 | struct GNUNET_NETWORK_Descriptor *wfd, GNUNET_SCHEDULER_Task main, void *cls); | ||
387 | |||
388 | |||
389 | /** | ||
390 | * Schedule a new task to be run with a specified delay or when the | ||
391 | * specified file descriptor is ready for reading. The delay can be | ||
392 | * used as a timeout on the socket being ready. The task will be | ||
393 | * scheduled for execution once either the delay has expired or the | ||
394 | * socket operation is ready. | ||
395 | * | ||
396 | * @param sched scheduler to use | ||
397 | * @param run_on_shutdown run on shutdown? Set this | ||
398 | * argument to GNUNET_NO to skip this task if | ||
399 | * the user requested process termination. | ||
400 | * @param prio how important is this task? | ||
401 | * @param prerequisite_task run this task after the task with the given | ||
402 | * task identifier completes (and any of our other | ||
403 | * conditions, such as delay, read or write-readyness | ||
404 | * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency | ||
405 | * on completion of other tasks. | ||
406 | * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever" | ||
407 | * @param rfd read file-descriptor | ||
408 | * @param main main function of the task | ||
409 | * @param cls closure of task | ||
410 | * @return unique task identifier for the job | ||
411 | * only valid until "main" is started! | ||
412 | */ | ||
413 | GNUNET_SCHEDULER_TaskIdentifier | ||
414 | GNUNET_SCHEDULER_add_read_file (struct GNUNET_SCHEDULER_Handle *sched, | ||
349 | int run_on_shutdown, | 415 | int run_on_shutdown, |
350 | enum GNUNET_SCHEDULER_Priority prio, | 416 | enum GNUNET_SCHEDULER_Priority prio, |
351 | GNUNET_SCHEDULER_TaskIdentifier prerequisite_task, | 417 | GNUNET_SCHEDULER_TaskIdentifier prerequisite_task, |
352 | struct GNUNET_TIME_Relative delay, | 418 | struct GNUNET_TIME_Relative delay, |
353 | int rfd, GNUNET_SCHEDULER_Task main, void *cls); | 419 | struct GNUNET_DISK_FileHandle *rfd, GNUNET_SCHEDULER_Task main, void *cls); |
354 | 420 | ||
355 | 421 | ||
356 | /** | 422 | /** |
@@ -378,12 +444,12 @@ GNUNET_SCHEDULER_add_read (struct GNUNET_SCHEDULER_Handle *sched, | |||
378 | * only valid until "main" is started! | 444 | * only valid until "main" is started! |
379 | */ | 445 | */ |
380 | GNUNET_SCHEDULER_TaskIdentifier | 446 | GNUNET_SCHEDULER_TaskIdentifier |
381 | GNUNET_SCHEDULER_add_write (struct GNUNET_SCHEDULER_Handle *sched, | 447 | GNUNET_SCHEDULER_add_write_file (struct GNUNET_SCHEDULER_Handle *sched, |
382 | int run_on_shutdown, | 448 | int run_on_shutdown, |
383 | enum GNUNET_SCHEDULER_Priority prio, | 449 | enum GNUNET_SCHEDULER_Priority prio, |
384 | GNUNET_SCHEDULER_TaskIdentifier prerequisite_task, | 450 | GNUNET_SCHEDULER_TaskIdentifier prerequisite_task, |
385 | struct GNUNET_TIME_Relative delay, | 451 | struct GNUNET_TIME_Relative delay, |
386 | int wfd, GNUNET_SCHEDULER_Task main, void *cls); | 452 | struct GNUNET_DISK_FileHandle *wfd, GNUNET_SCHEDULER_Task main, void *cls); |
387 | 453 | ||
388 | 454 | ||
389 | /** | 455 | /** |
@@ -429,7 +495,7 @@ GNUNET_SCHEDULER_add_select (struct GNUNET_SCHEDULER_Handle *sched, | |||
429 | GNUNET_SCHEDULER_TaskIdentifier | 495 | GNUNET_SCHEDULER_TaskIdentifier |
430 | prerequisite_task, | 496 | prerequisite_task, |
431 | struct GNUNET_TIME_Relative delay, | 497 | struct GNUNET_TIME_Relative delay, |
432 | int nfds, const fd_set * rs, const fd_set * ws, | 498 | const struct GNUNET_NETWORK_FDSet * rs, const struct GNUNET_NETWORK_FDSet * ws, |
433 | GNUNET_SCHEDULER_Task main, void *cls); | 499 | GNUNET_SCHEDULER_Task main, void *cls); |
434 | 500 | ||
435 | #if 0 /* keep Emacsens' auto-indent happy */ | 501 | #if 0 /* keep Emacsens' auto-indent happy */ |
diff --git a/src/include/gnunet_server_lib.h b/src/include/gnunet_server_lib.h index 82085ad0d..092bab40b 100644 --- a/src/include/gnunet_server_lib.h +++ b/src/include/gnunet_server_lib.h | |||
@@ -37,7 +37,7 @@ extern "C" | |||
37 | #endif | 37 | #endif |
38 | 38 | ||
39 | #include "gnunet_common.h" | 39 | #include "gnunet_common.h" |
40 | #include "gnunet_network_lib.h" | 40 | #include "gnunet_connection_lib.h" |
41 | #include "gnunet_scheduler_lib.h" | 41 | #include "gnunet_scheduler_lib.h" |
42 | 42 | ||
43 | 43 | ||
diff --git a/src/include/gnunet_transport_service.h b/src/include/gnunet_transport_service.h index 037a247a2..385bf7cfb 100644 --- a/src/include/gnunet_transport_service.h +++ b/src/include/gnunet_transport_service.h | |||
@@ -37,7 +37,7 @@ extern "C" | |||
37 | 37 | ||
38 | #include "gnunet_configuration_lib.h" | 38 | #include "gnunet_configuration_lib.h" |
39 | #include "gnunet_crypto_lib.h" | 39 | #include "gnunet_crypto_lib.h" |
40 | #include "gnunet_network_lib.h" | 40 | #include "gnunet_connection_lib.h" |
41 | #include "gnunet_scheduler_lib.h" | 41 | #include "gnunet_scheduler_lib.h" |
42 | #include "gnunet_time_lib.h" | 42 | #include "gnunet_time_lib.h" |
43 | 43 | ||
diff --git a/src/include/gnunet_util_lib.h b/src/include/gnunet_util_lib.h index 40686b189..fe382be3f 100644 --- a/src/include/gnunet_util_lib.h +++ b/src/include/gnunet_util_lib.h | |||
@@ -39,6 +39,7 @@ extern "C" | |||
39 | #include "gnunet_common.h" | 39 | #include "gnunet_common.h" |
40 | #include "gnunet_client_lib.h" | 40 | #include "gnunet_client_lib.h" |
41 | #include "gnunet_configuration_lib.h" | 41 | #include "gnunet_configuration_lib.h" |
42 | #include "gnunet_connection_lib.h" | ||
42 | #include "gnunet_container_lib.h" | 43 | #include "gnunet_container_lib.h" |
43 | #include "gnunet_crypto_lib.h" | 44 | #include "gnunet_crypto_lib.h" |
44 | #include "gnunet_disk_lib.h" | 45 | #include "gnunet_disk_lib.h" |
diff --git a/src/transport/plugin_transport_http.c b/src/transport/plugin_transport_http.c index f7e06a9cd..78691852d 100644 --- a/src/transport/plugin_transport_http.c +++ b/src/transport/plugin_transport_http.c | |||
@@ -399,7 +399,7 @@ static int stat_connect_calls; | |||
399 | */ | 399 | */ |
400 | static unsigned int http_requests_pending; | 400 | static unsigned int http_requests_pending; |
401 | 401 | ||
402 | static int signal_pipe[2]; | 402 | static struct GNUNET_DISK_FileHandle signal_pipe[2]; |
403 | 403 | ||
404 | static char *proxy; | 404 | static char *proxy; |
405 | 405 | ||
@@ -453,7 +453,7 @@ static void | |||
453 | signal_select () | 453 | signal_select () |
454 | { | 454 | { |
455 | static char c; | 455 | static char c; |
456 | WRITE (signal_pipe[1], &c, sizeof (c)); | 456 | GNUNET_DISK_file_write (signal_pipe[1], &c, sizeof (c)); |
457 | } | 457 | } |
458 | 458 | ||
459 | /** | 459 | /** |
@@ -1747,8 +1747,10 @@ curl_runner (void *unused) | |||
1747 | fd_set rs; | 1747 | fd_set rs; |
1748 | fd_set ws; | 1748 | fd_set ws; |
1749 | fd_set es; | 1749 | fd_set es; |
1750 | struct GNUNET_NETWORK_FDSet *hrs; | ||
1751 | struct GNUNET_NETWORK_FDSet *hws; | ||
1752 | struct GNUNET_NETWORK_FDSet *hes; | ||
1750 | int max; | 1753 | int max; |
1751 | struct timeval tv; | ||
1752 | int running; | 1754 | int running; |
1753 | unsigned long long timeout; | 1755 | unsigned long long timeout; |
1754 | long ms; | 1756 | long ms; |
@@ -1761,6 +1763,11 @@ curl_runner (void *unused) | |||
1761 | GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER, | 1763 | GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER, |
1762 | "HTTP transport select thread started\n"); | 1764 | "HTTP transport select thread started\n"); |
1763 | #endif | 1765 | #endif |
1766 | |||
1767 | hrs = GNUNET_net_fdset_create (); | ||
1768 | hws = GNUNET_net_fdset_create (); | ||
1769 | hes = GNUNET_net_fdset_create (); | ||
1770 | |||
1764 | while (GNUNET_YES == http_running) | 1771 | while (GNUNET_YES == http_running) |
1765 | { | 1772 | { |
1766 | max = 0; | 1773 | max = 0; |
@@ -1793,16 +1800,20 @@ curl_runner (void *unused) | |||
1793 | have_tv = MHD_YES; | 1800 | have_tv = MHD_YES; |
1794 | } | 1801 | } |
1795 | GNUNET_mutex_unlock (lock); | 1802 | GNUNET_mutex_unlock (lock); |
1796 | FD_SET (signal_pipe[0], &rs); | 1803 | |
1797 | if (max < signal_pipe[0]) | 1804 | GNUNET_net_fdset_zero (hws); |
1798 | max = signal_pipe[0]; | 1805 | GNUNET_net_fdset_zero (hrs); |
1799 | tv.tv_sec = timeout / 1000; | 1806 | GNUNET_net_fdset_zero (hes); |
1800 | tv.tv_usec = (timeout % 1000) * 1000; | 1807 | GNUNET_net_fdset_copy_native (hws, ws); |
1808 | GNUNET_net_fdset_copy_native (hrs, rs); | ||
1809 | GNUNET_net_fdset_copy_native (hes, es); | ||
1810 | |||
1811 | GNUNET_net_fdset_handle_set (signal_pipe[0], hrs); | ||
1801 | if (stats != NULL) | 1812 | if (stats != NULL) |
1802 | stats->change (stat_select_calls, 1); | 1813 | stats->change (stat_select_calls, 1); |
1803 | ret = | 1814 | ret = |
1804 | SELECT (max + 1, &rs, &ws, &es, (have_tv == MHD_YES) ? &tv : NULL); | 1815 | GNUNET_net_select (hrs, hws, hes, (have_tv == MHD_YES) ? timeout : GNUNET_TIME_UNIT_FOREVER_REL); |
1805 | if (ret == -1) | 1816 | if (ret == GNUNET_SYSERR) |
1806 | { | 1817 | { |
1807 | GNUNET_GE_LOG_STRERROR (coreAPI->ectx, | 1818 | GNUNET_GE_LOG_STRERROR (coreAPI->ectx, |
1808 | GNUNET_GE_ERROR | GNUNET_GE_ADMIN | | 1819 | GNUNET_GE_ERROR | GNUNET_GE_ADMIN | |
@@ -1819,8 +1830,8 @@ curl_runner (void *unused) | |||
1819 | } | 1830 | } |
1820 | while ((mret == CURLM_CALL_MULTI_PERFORM) | 1831 | while ((mret == CURLM_CALL_MULTI_PERFORM) |
1821 | && (http_running == GNUNET_YES)); | 1832 | && (http_running == GNUNET_YES)); |
1822 | if (FD_ISSET (signal_pipe[0], &rs)) | 1833 | if (GNUNET_net_fdset_handle_isset (signal_pipe[0], hrs)) |
1823 | read (signal_pipe[0], buf, sizeof (buf)); | 1834 | GNUNET_DISK_file_read (signal_pipe[0], buf, sizeof (buf)); |
1824 | if ((mret != CURLM_OK) && (mret != CURLM_CALL_MULTI_PERFORM)) | 1835 | if ((mret != CURLM_OK) && (mret != CURLM_CALL_MULTI_PERFORM)) |
1825 | GNUNET_GE_LOG (coreAPI->ectx, | 1836 | GNUNET_GE_LOG (coreAPI->ectx, |
1826 | GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER | | 1837 | GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER | |
@@ -1914,7 +1925,7 @@ startTransportServer () | |||
1914 | GNUNET_YES)) | 1925 | GNUNET_YES)) |
1915 | available_protocols |= VERSION_AVAILABLE_IPV6; | 1926 | available_protocols |= VERSION_AVAILABLE_IPV6; |
1916 | } | 1927 | } |
1917 | if (0 != PIPE (signal_pipe)) | 1928 | if (GNUNET_OK != GNUNET_DISK_pipe (signal_pipe, GNUNET_NO)) |
1918 | { | 1929 | { |
1919 | MHD_stop_daemon (mhd_daemon); | 1930 | MHD_stop_daemon (mhd_daemon); |
1920 | curl_multi_cleanup (curl_multi); | 1931 | curl_multi_cleanup (curl_multi); |
@@ -1922,8 +1933,6 @@ startTransportServer () | |||
1922 | mhd_daemon = NULL; | 1933 | mhd_daemon = NULL; |
1923 | return GNUNET_SYSERR; | 1934 | return GNUNET_SYSERR; |
1924 | } | 1935 | } |
1925 | GNUNET_pipe_make_nonblocking (coreAPI->ectx, signal_pipe[0]); | ||
1926 | GNUNET_pipe_make_nonblocking (coreAPI->ectx, signal_pipe[1]); | ||
1927 | http_running = GNUNET_YES; | 1936 | http_running = GNUNET_YES; |
1928 | curl_thread = GNUNET_thread_create (&curl_runner, NULL, 32 * 1024); | 1937 | curl_thread = GNUNET_thread_create (&curl_runner, NULL, 32 * 1024); |
1929 | if (curl_thread == NULL) | 1938 | if (curl_thread == NULL) |
@@ -1950,8 +1959,8 @@ stopTransportServer () | |||
1950 | signal_select (); | 1959 | signal_select (); |
1951 | GNUNET_thread_stop_sleep (curl_thread); | 1960 | GNUNET_thread_stop_sleep (curl_thread); |
1952 | GNUNET_thread_join (curl_thread, &unused); | 1961 | GNUNET_thread_join (curl_thread, &unused); |
1953 | CLOSE (signal_pipe[0]); | 1962 | GNUNET_DISK_close (signal_pipe[0]); |
1954 | CLOSE (signal_pipe[1]); | 1963 | GNUNET_DISK_close (signal_pipe[1]); |
1955 | if (mhd_daemon != NULL) | 1964 | if (mhd_daemon != NULL) |
1956 | { | 1965 | { |
1957 | MHD_stop_daemon (mhd_daemon); | 1966 | MHD_stop_daemon (mhd_daemon); |
diff --git a/src/transport/plugin_transport_tcp.c b/src/transport/plugin_transport_tcp.c index 764ef8b2f..2e174f80a 100644 --- a/src/transport/plugin_transport_tcp.c +++ b/src/transport/plugin_transport_tcp.c | |||
@@ -26,7 +26,7 @@ | |||
26 | 26 | ||
27 | #include "platform.h" | 27 | #include "platform.h" |
28 | #include "gnunet_hello_lib.h" | 28 | #include "gnunet_hello_lib.h" |
29 | #include "gnunet_network_lib.h" | 29 | #include "gnunet_connection_lib.h" |
30 | #include "gnunet_os_lib.h" | 30 | #include "gnunet_os_lib.h" |
31 | #include "gnunet_peerinfo_service.h" | 31 | #include "gnunet_peerinfo_service.h" |
32 | #include "gnunet_protocols.h" | 32 | #include "gnunet_protocols.h" |
diff --git a/src/transport/plugin_transport_template.c b/src/transport/plugin_transport_template.c index bce4bbc52..6307092fc 100644 --- a/src/transport/plugin_transport_template.c +++ b/src/transport/plugin_transport_template.c | |||
@@ -26,7 +26,7 @@ | |||
26 | 26 | ||
27 | #include "platform.h" | 27 | #include "platform.h" |
28 | #include "gnunet_protocols.h" | 28 | #include "gnunet_protocols.h" |
29 | #include "gnunet_network_lib.h" | 29 | #include "gnunet_connection_lib.h" |
30 | #include "gnunet_server_lib.h" | 30 | #include "gnunet_server_lib.h" |
31 | #include "gnunet_service_lib.h" | 31 | #include "gnunet_service_lib.h" |
32 | #include "gnunet_statistics_service.h" | 32 | #include "gnunet_statistics_service.h" |
diff --git a/src/transport/plugin_transport_udp.c b/src/transport/plugin_transport_udp.c index ccaf9fbd1..c84b2bae7 100644 --- a/src/transport/plugin_transport_udp.c +++ b/src/transport/plugin_transport_udp.c | |||
@@ -267,31 +267,23 @@ udp_test_would_try (GNUNET_TSession * tsession, unsigned int size, | |||
267 | * Create a UDP socket. If possible, use IPv6, otherwise | 267 | * Create a UDP socket. If possible, use IPv6, otherwise |
268 | * try IPv4. Update available_protocols accordingly. | 268 | * try IPv4. Update available_protocols accordingly. |
269 | */ | 269 | */ |
270 | static int | 270 | static struct GNUNET_NETWORK_Descriptor * |
271 | udp_create_socket () | 271 | udp_create_socket () |
272 | { | 272 | { |
273 | int s; | 273 | struct GNUNET_NETWORK_Descriptor *desc; |
274 | 274 | ||
275 | available_protocols = VERSION_AVAILABLE_NONE; | 275 | available_protocols = VERSION_AVAILABLE_NONE; |
276 | s = -1; | 276 | desc = NULL; |
277 | if (GNUNET_YES != | 277 | if (GNUNET_YES != |
278 | GNUNET_GC_get_configuration_value_yesno (cfg, "GNUNETD", "DISABLE-IPV6", | 278 | GNUNET_GC_get_configuration_value_yesno (cfg, "GNUNETD", "DISABLE-IPV6", |
279 | GNUNET_YES)) | 279 | GNUNET_YES)) |
280 | { | 280 | { |
281 | #ifndef MINGW | 281 | desc = GNUNET_net_socket (PF_INET6, SOCK_DGRAM, 17); |
282 | s = SOCKET (PF_INET6, SOCK_DGRAM, 17); | ||
283 | #else | ||
284 | s = win_ols_socket (PF_INET6, SOCK_DGRAM, 17); | ||
285 | #endif | ||
286 | } | 282 | } |
287 | if (s < 0) | 283 | if (NULL == desc) |
288 | { | 284 | { |
289 | #ifndef MINGW | 285 | desc = GNUNET_net_socket (PF_INET, SOCK_DGRAM, 17); |
290 | s = SOCKET (PF_INET, SOCK_DGRAM, 17); | 286 | if (NULL == desc) |
291 | #else | ||
292 | s = win_ols_socket (PF_INET, SOCK_DGRAM, 17); | ||
293 | #endif | ||
294 | if (s < 0) | ||
295 | { | 287 | { |
296 | GNUNET_GE_LOG_STRERROR (coreAPI->ectx, | 288 | GNUNET_GE_LOG_STRERROR (coreAPI->ectx, |
297 | GNUNET_GE_ERROR | GNUNET_GE_ADMIN | | 289 | GNUNET_GE_ERROR | GNUNET_GE_ADMIN | |
@@ -304,7 +296,7 @@ udp_create_socket () | |||
304 | { | 296 | { |
305 | available_protocols = VERSION_AVAILABLE_IPV6 | VERSION_AVAILABLE_IPV4; | 297 | available_protocols = VERSION_AVAILABLE_IPV6 | VERSION_AVAILABLE_IPV4; |
306 | } | 298 | } |
307 | return s; | 299 | return desc; |
308 | } | 300 | } |
309 | 301 | ||
310 | /** | 302 | /** |
@@ -424,7 +416,7 @@ udp_transport_server_start () | |||
424 | struct sockaddr_in6 serverAddrv6; | 416 | struct sockaddr_in6 serverAddrv6; |
425 | struct sockaddr *serverAddr; | 417 | struct sockaddr *serverAddr; |
426 | socklen_t addrlen; | 418 | socklen_t addrlen; |
427 | int sock; | 419 | GNUNET_NETWORK_Descriptor *desc; |
428 | const int on = 1; | 420 | const int on = 1; |
429 | unsigned short port; | 421 | unsigned short port; |
430 | 422 | ||
@@ -433,10 +425,10 @@ udp_transport_server_start () | |||
433 | port = get_port (); | 425 | port = get_port (); |
434 | if (port != 0) | 426 | if (port != 0) |
435 | { | 427 | { |
436 | sock = udp_create_socket (); | 428 | desc = udp_create_socket (); |
437 | if (sock < 0) | 429 | if (NULL == desc) |
438 | return GNUNET_SYSERR; | 430 | return GNUNET_SYSERR; |
439 | if (SETSOCKOPT (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) < 0) | 431 | if (GNUNET_net_setsockopt (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) < 0) |
440 | { | 432 | { |
441 | GNUNET_GE_DIE_STRERROR (coreAPI->ectx, | 433 | GNUNET_GE_DIE_STRERROR (coreAPI->ectx, |
442 | GNUNET_GE_FATAL | GNUNET_GE_ADMIN | | 434 | GNUNET_GE_FATAL | GNUNET_GE_ADMIN | |
@@ -461,7 +453,7 @@ udp_transport_server_start () | |||
461 | addrlen = sizeof (serverAddrv6); | 453 | addrlen = sizeof (serverAddrv6); |
462 | serverAddr = (struct sockaddr *) &serverAddrv6; | 454 | serverAddr = (struct sockaddr *) &serverAddrv6; |
463 | } | 455 | } |
464 | if (BIND (sock, serverAddr, addrlen) < 0) | 456 | if (GNUNET_net_bind (desc, serverAddr, addrlen) < 0) |
465 | { | 457 | { |
466 | GNUNET_GE_LOG_STRERROR (coreAPI->ectx, | 458 | GNUNET_GE_LOG_STRERROR (coreAPI->ectx, |
467 | GNUNET_GE_FATAL | GNUNET_GE_ADMIN | | 459 | GNUNET_GE_FATAL | GNUNET_GE_ADMIN | |
@@ -471,14 +463,14 @@ udp_transport_server_start () | |||
471 | GNUNET_GE_IMMEDIATE, | 463 | GNUNET_GE_IMMEDIATE, |
472 | _("Failed to bind to %s port %d.\n"), | 464 | _("Failed to bind to %s port %d.\n"), |
473 | MY_TRANSPORT_NAME, port); | 465 | MY_TRANSPORT_NAME, port); |
474 | if (0 != CLOSE (sock)) | 466 | if (0 != GNUNET_net_close (&desc)) |
475 | GNUNET_GE_LOG_STRERROR (coreAPI->ectx, | 467 | GNUNET_GE_LOG_STRERROR (coreAPI->ectx, |
476 | GNUNET_GE_ERROR | GNUNET_GE_USER | | 468 | GNUNET_GE_ERROR | GNUNET_GE_USER | |
477 | GNUNET_GE_ADMIN | GNUNET_GE_BULK, | 469 | GNUNET_GE_ADMIN | GNUNET_GE_BULK, |
478 | "close"); | 470 | "close"); |
479 | return GNUNET_SYSERR; | 471 | return GNUNET_SYSERR; |
480 | } | 472 | } |
481 | selector = GNUNET_select_create ("udp", GNUNET_YES, coreAPI->ectx, load_monitor, sock, addrlen, 0, /* timeout */ | 473 | selector = GNUNET_select_create ("udp", GNUNET_YES, coreAPI->ectx, load_monitor, desc, addrlen, 0, /* timeout */ |
482 | &select_message_handler, | 474 | &select_message_handler, |
483 | NULL, | 475 | NULL, |
484 | &select_accept_handler, | 476 | &select_accept_handler, |
@@ -489,8 +481,8 @@ udp_transport_server_start () | |||
489 | if (selector == NULL) | 481 | if (selector == NULL) |
490 | return GNUNET_SYSERR; | 482 | return GNUNET_SYSERR; |
491 | } | 483 | } |
492 | sock = udp_create_socket (); | 484 | desc = udp_create_socket (); |
493 | if (sock == -1) | 485 | if (NULL == desc) |
494 | { | 486 | { |
495 | GNUNET_GE_LOG_STRERROR (coreAPI->ectx, | 487 | GNUNET_GE_LOG_STRERROR (coreAPI->ectx, |
496 | GNUNET_GE_ERROR | GNUNET_GE_ADMIN | | 488 | GNUNET_GE_ERROR | GNUNET_GE_ADMIN | |
@@ -499,7 +491,7 @@ udp_transport_server_start () | |||
499 | selector = NULL; | 491 | selector = NULL; |
500 | return GNUNET_SYSERR; | 492 | return GNUNET_SYSERR; |
501 | } | 493 | } |
502 | udp_sock = GNUNET_socket_create (coreAPI->ectx, load_monitor, sock); | 494 | udp_sock = GNUNET_socket_create (coreAPI->ectx, load_monitor, desc); |
503 | GNUNET_GE_ASSERT (coreAPI->ectx, udp_sock != NULL); | 495 | GNUNET_GE_ASSERT (coreAPI->ectx, udp_sock != NULL); |
504 | return GNUNET_OK; | 496 | return GNUNET_OK; |
505 | } | 497 | } |
diff --git a/src/upnp/upnp.c b/src/upnp/upnp.c index 6dc4338a0..afc815b1f 100644 --- a/src/upnp/upnp.c +++ b/src/upnp/upnp.c | |||
@@ -104,7 +104,7 @@ typedef struct | |||
104 | char *full_url; | 104 | char *full_url; |
105 | char *buf; | 105 | char *buf; |
106 | unsigned int buf_len; | 106 | unsigned int buf_len; |
107 | int sock; | 107 | struct GNUNET_NETWORK_Descriptor *sock; |
108 | } UPnPDiscoveryData; | 108 | } UPnPDiscoveryData; |
109 | 109 | ||
110 | static GaimUPnPControlInfo control_info = { | 110 | static GaimUPnPControlInfo control_info = { |
@@ -543,7 +543,7 @@ gaim_upnp_parse_description (char *proxy, UPnPDiscoveryData * dd) | |||
543 | } | 543 | } |
544 | 544 | ||
545 | int | 545 | int |
546 | gaim_upnp_discover (struct GNUNET_CONFIGURATION_Handle *cfg, int sock) | 546 | gaim_upnp_discover (struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_NETWORK_Descriptor *sock) |
547 | { | 547 | { |
548 | char *proxy; | 548 | char *proxy; |
549 | socklen_t avail; | 549 | socklen_t avail; |
@@ -613,7 +613,7 @@ gaim_upnp_discover (struct GNUNET_CONFIGURATION_Handle *cfg, int sock) | |||
613 | /* try to read response */ | 613 | /* try to read response */ |
614 | do | 614 | do |
615 | { | 615 | { |
616 | buf_len = recv (dd.sock, buf, sizeof (buf) - 1, 0); | 616 | buf_len = GNUNET_IO_recv (dd.sock, buf, sizeof (buf) - 1, 0); |
617 | if (buf_len > 0) | 617 | if (buf_len > 0) |
618 | { | 618 | { |
619 | buf[buf_len] = '\0'; | 619 | buf[buf_len] = '\0'; |
diff --git a/src/upnp/upnp_init.c b/src/upnp/upnp_init.c index 3dc0d2b7c..8390494e5 100644 --- a/src/upnp/upnp_init.c +++ b/src/upnp/upnp_init.c | |||
@@ -50,7 +50,7 @@ static unsigned int maps_size; | |||
50 | 50 | ||
51 | static struct GNUNET_ThreadHandle *discovery; | 51 | static struct GNUNET_ThreadHandle *discovery; |
52 | 52 | ||
53 | static int discovery_socket; | 53 | static struct GNUNET_NETWORK_Descriptor *discovery_socket; |
54 | 54 | ||
55 | /** | 55 | /** |
56 | * Obtain the public/external IP address. | 56 | * Obtain the public/external IP address. |
@@ -84,8 +84,8 @@ kill_discovery () | |||
84 | 84 | ||
85 | if (discovery != NULL) | 85 | if (discovery != NULL) |
86 | { | 86 | { |
87 | SHUTDOWN (discovery_socket, SHUT_RDWR); | 87 | GNUNET_IO_shutdown (discovery_socket, SHUT_RDWR); |
88 | CLOSE (discovery_socket); | 88 | GNUNET_IO_close (&discovery_socket); |
89 | GNUNET_thread_join (discovery, &unused); | 89 | GNUNET_thread_join (discovery, &unused); |
90 | discovery = NULL; | 90 | discovery = NULL; |
91 | } | 91 | } |
@@ -105,8 +105,8 @@ static void | |||
105 | discover (void *unused) | 105 | discover (void *unused) |
106 | { | 106 | { |
107 | kill_discovery (); | 107 | kill_discovery (); |
108 | discovery_socket = SOCKET (PF_INET, SOCK_DGRAM, 0); | 108 | discovery_socket = GNUNET_IO_socket (PF_INET, SOCK_DGRAM, 0); |
109 | if (discovery_socket == -1) | 109 | if (NULL == discovery_socket) |
110 | return; | 110 | return; |
111 | discovery = GNUNET_thread_create (&discover_thread, NULL, 1024 * 128); | 111 | discovery = GNUNET_thread_create (&discover_thread, NULL, 1024 * 128); |
112 | } | 112 | } |
diff --git a/src/util/Makefile.am b/src/util/Makefile.am index 089e92018..46d1c3a7a 100644 --- a/src/util/Makefile.am +++ b/src/util/Makefile.am | |||
@@ -32,6 +32,7 @@ libgnunetutil_la_SOURCES = \ | |||
32 | crypto_random.c \ | 32 | crypto_random.c \ |
33 | crypto_rsa.c \ | 33 | crypto_rsa.c \ |
34 | disk.c \ | 34 | disk.c \ |
35 | disk.h \ | ||
35 | getopt.c \ | 36 | getopt.c \ |
36 | getopt_helpers.c \ | 37 | getopt_helpers.c \ |
37 | network.c \ | 38 | network.c \ |
@@ -47,6 +48,7 @@ libgnunetutil_la_SOURCES = \ | |||
47 | server_tc.c \ | 48 | server_tc.c \ |
48 | service.c \ | 49 | service.c \ |
49 | signal.c \ | 50 | signal.c \ |
51 | sock.c \ | ||
50 | strings.c \ | 52 | strings.c \ |
51 | time.c \ | 53 | time.c \ |
52 | $(WINSRC) | 54 | $(WINSRC) |
diff --git a/src/util/disk.c b/src/util/disk.c index 08f14caf6..b9fad5477 100644 --- a/src/util/disk.c +++ b/src/util/disk.c | |||
@@ -31,6 +31,7 @@ | |||
31 | #include "gnunet_disk_lib.h" | 31 | #include "gnunet_disk_lib.h" |
32 | #include "gnunet_scheduler_lib.h" | 32 | #include "gnunet_scheduler_lib.h" |
33 | #include "gnunet_strings_lib.h" | 33 | #include "gnunet_strings_lib.h" |
34 | #include "disk.h" | ||
34 | 35 | ||
35 | 36 | ||
36 | #if LINUX || CYGWIN | 37 | #if LINUX || CYGWIN |
@@ -74,13 +75,9 @@ typedef struct | |||
74 | int include_sym_links; | 75 | int include_sym_links; |
75 | } GetFileSizeData; | 76 | } GetFileSizeData; |
76 | 77 | ||
77 | struct GNUNET_DISK_FileHandle | 78 | struct GNUNET_DISK_PipeHandle |
78 | { | 79 | { |
79 | #if MINGW | 80 | struct GNUNET_DISK_FileHandle fd[2]; |
80 | HANDLE h; | ||
81 | #else | ||
82 | int fd; | ||
83 | #endif | ||
84 | }; | 81 | }; |
85 | 82 | ||
86 | static int | 83 | static int |
@@ -1390,4 +1387,165 @@ GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h) | |||
1390 | #endif | 1387 | #endif |
1391 | } | 1388 | } |
1392 | 1389 | ||
1390 | /** | ||
1391 | * Creates an interprocess channel | ||
1392 | * @param blocking creates an asynchronous pipe if set to GNUNET_NO | ||
1393 | * @return handle to the new pipe, NULL on error | ||
1394 | */ | ||
1395 | struct GNUNET_DISK_PipeHandle * | ||
1396 | GNUNET_DISK_pipe (int blocking) | ||
1397 | { | ||
1398 | struct GNUNET_DISK_PipeHandle *p; | ||
1399 | int err; | ||
1400 | |||
1401 | err = GNUNET_NO; | ||
1402 | p = GNUNET_malloc (sizeof (struct GNUNET_DISK_PipeHandle)); | ||
1403 | |||
1404 | #ifndef MINGW | ||
1405 | int fd[2]; | ||
1406 | int ret; | ||
1407 | int flags; | ||
1408 | |||
1409 | ret = pipe (fd); | ||
1410 | if (ret != -1) | ||
1411 | { | ||
1412 | p->fd[0].fd = fd[0]; | ||
1413 | p->fd[1].fd = fd[1]; | ||
1414 | |||
1415 | if (!blocking) | ||
1416 | { | ||
1417 | flags = fcntl (fd[0], F_GETFL); | ||
1418 | flags |= O_NONBLOCK; | ||
1419 | ret = fcntl (fd[0], F_SETFL, flags); | ||
1420 | if (ret != -1) | ||
1421 | { | ||
1422 | flags = fcntl (fd[1], F_GETFL); | ||
1423 | flags |= O_NONBLOCK; | ||
1424 | ret = fcntl (fd[1], F_SETFL, flags); | ||
1425 | } | ||
1426 | if (ret == -1) | ||
1427 | { | ||
1428 | GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "fcntl"); | ||
1429 | close (fd[0]); | ||
1430 | close (fd[1]); | ||
1431 | err = GNUNET_YES; | ||
1432 | } | ||
1433 | } | ||
1434 | } | ||
1435 | else | ||
1436 | err = GNUNET_YES; | ||
1437 | #else | ||
1438 | BOOL ret; | ||
1439 | |||
1440 | ret = CreatePipe (&p->fd[0].h, &p->fd[1].h, NULL, 0); | ||
1441 | if (ret) | ||
1442 | { | ||
1443 | if (!blocking) | ||
1444 | { | ||
1445 | DWORD mode; | ||
1446 | |||
1447 | mode = PIPE_NOWAIT; | ||
1448 | SetNamedPipeHandleState (p->fd[0].h, &mode, NULL, NULL); | ||
1449 | SetNamedPipeHandleState (p->fd[1].h, &mode, NULL, NULL); | ||
1450 | /* this always fails on Windows 95, so we don't care about error handling */ | ||
1451 | } | ||
1452 | } | ||
1453 | else | ||
1454 | err = GNUNET_YES; | ||
1455 | #endif | ||
1456 | |||
1457 | if (GNUNET_YES == err) | ||
1458 | { | ||
1459 | GNUNET_free (p); | ||
1460 | p = NULL; | ||
1461 | } | ||
1462 | |||
1463 | return p; | ||
1464 | } | ||
1465 | |||
1466 | /** | ||
1467 | * Closes an interprocess channel | ||
1468 | * @param p pipe | ||
1469 | * @return GNUNET_OK on success, GNUNET_SYSERR otherwise | ||
1470 | */ | ||
1471 | int | ||
1472 | GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p) | ||
1473 | { | ||
1474 | int ret; | ||
1475 | |||
1476 | ret = GNUNET_OK; | ||
1477 | |||
1478 | #ifdef MINGW | ||
1479 | if (!CloseHandle (p->fd[0].h)) | ||
1480 | { | ||
1481 | SetErrnoFromWinError (GetLastError ()); | ||
1482 | ret = GNUNET_SYSERR; | ||
1483 | } | ||
1484 | |||
1485 | if (!CloseHandle (p->fd[1].h)) | ||
1486 | { | ||
1487 | SetErrnoFromWinError (GetLastError ()); | ||
1488 | ret = GNUNET_SYSERR; | ||
1489 | } | ||
1490 | #else | ||
1491 | { | ||
1492 | int save; | ||
1493 | |||
1494 | if (close (p->fd[0].fd) != -1) | ||
1495 | { | ||
1496 | ret = GNUNET_SYSERR; | ||
1497 | save = errno; | ||
1498 | } | ||
1499 | else | ||
1500 | save = 0; | ||
1501 | |||
1502 | if (close (p->fd[1].fd) != -1) | ||
1503 | { | ||
1504 | ret = GNUNET_SYSERR; | ||
1505 | } | ||
1506 | else | ||
1507 | errno = save; | ||
1508 | } | ||
1509 | #endif | ||
1510 | GNUNET_free (p); | ||
1511 | |||
1512 | return ret; | ||
1513 | } | ||
1514 | |||
1515 | /** | ||
1516 | * Get the handle to a particular pipe end | ||
1517 | * @param p pipe | ||
1518 | * @param n number of the end | ||
1519 | */ | ||
1520 | const struct GNUNET_DISK_FileHandle * | ||
1521 | GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p, int n) | ||
1522 | { | ||
1523 | return &p->fd[n]; | ||
1524 | } | ||
1525 | |||
1526 | /** | ||
1527 | * Retrieve OS file handle | ||
1528 | * @internal | ||
1529 | * @param fh GNUnet file descriptor | ||
1530 | * @param dst destination buffer | ||
1531 | * @param dst_len length of dst | ||
1532 | * @return GNUNET_OK on success, GNUNET_SYSERR otherwise | ||
1533 | */ | ||
1534 | int | ||
1535 | GNUNET_internal_disk_file_handle (const struct GNUNET_DISK_FileHandle *fh, | ||
1536 | void *dst, unsigned int dst_len) | ||
1537 | { | ||
1538 | #ifdef MINGW | ||
1539 | if (dst_len < sizeof (HANDLE)) | ||
1540 | return GNUNET_SYSERR; | ||
1541 | *((HANDLE *) dst) = fh->h; | ||
1542 | #else | ||
1543 | if (dst_len < sizeof(int)) | ||
1544 | return GNUNET_SYSERR; | ||
1545 | *((int *) dst) = fh->fd; | ||
1546 | #endif | ||
1547 | |||
1548 | return GNUNET_OK; | ||
1549 | } | ||
1550 | |||
1393 | /* end of disk.c */ | 1551 | /* end of disk.c */ |
diff --git a/src/util/disk.h b/src/util/disk.h new file mode 100644 index 000000000..9857a0fed --- /dev/null +++ b/src/util/disk.h | |||
@@ -0,0 +1,53 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009 Christian Grothoff (and other contributing authors) | ||
4 | |||
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 | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file util/disk.h | ||
23 | * @brief Internal DISK related helper functions | ||
24 | * @author Nils Durner | ||
25 | */ | ||
26 | |||
27 | |||
28 | #ifndef GNUNET_DISK_H_ | ||
29 | #define GNUNET_DISK_H_ | ||
30 | |||
31 | #include "gnunet_disk_lib.h" | ||
32 | |||
33 | struct GNUNET_DISK_FileHandle | ||
34 | { | ||
35 | #ifdef MINGW | ||
36 | HANDLE h; | ||
37 | #else | ||
38 | int fd; | ||
39 | #endif | ||
40 | }; | ||
41 | |||
42 | /** | ||
43 | * Retrieve OS file handle | ||
44 | * @internal | ||
45 | * @param fh GNUnet file descriptor | ||
46 | * @param dst destination buffer | ||
47 | * @param dst_len length of dst | ||
48 | * @return GNUNET_OK on success, GNUNET_SYSERR otherwise | ||
49 | */ | ||
50 | int GNUNET_internal_disk_file_handle (const struct GNUNET_DISK_FileHandle *fh, | ||
51 | void *dst, unsigned int dst_len); | ||
52 | |||
53 | #endif /* GNUNET_DISK_H_ */ | ||
diff --git a/src/util/network.c b/src/util/network.c index 3ee19cfb2..6d8b9f938 100644 --- a/src/util/network.c +++ b/src/util/network.c | |||
@@ -37,7 +37,7 @@ | |||
37 | 37 | ||
38 | #include "platform.h" | 38 | #include "platform.h" |
39 | #include "gnunet_common.h" | 39 | #include "gnunet_common.h" |
40 | #include "gnunet_network_lib.h" | 40 | #include "gnunet_connection_lib.h" |
41 | #include "gnunet_scheduler_lib.h" | 41 | #include "gnunet_scheduler_lib.h" |
42 | 42 | ||
43 | #define DEBUG_NETWORK GNUNET_NO | 43 | #define DEBUG_NETWORK GNUNET_NO |
@@ -172,9 +172,9 @@ struct GNUNET_NETWORK_ConnectionHandle | |||
172 | struct GNUNET_NETWORK_TransmitHandle nth; | 172 | struct GNUNET_NETWORK_TransmitHandle nth; |
173 | 173 | ||
174 | /** | 174 | /** |
175 | * Underlying OS's socket, set to -1 after fatal errors. | 175 | * Underlying OS's socket, set to NULL after fatal errors. |
176 | */ | 176 | */ |
177 | int sock; | 177 | struct GNUNET_NETWORK_Descriptor *sock; |
178 | 178 | ||
179 | /** | 179 | /** |
180 | * Port to connect to. | 180 | * Port to connect to. |
@@ -219,7 +219,7 @@ struct GNUNET_NETWORK_ConnectionHandle | |||
219 | */ | 219 | */ |
220 | struct GNUNET_NETWORK_ConnectionHandle * | 220 | struct GNUNET_NETWORK_ConnectionHandle * |
221 | GNUNET_NETWORK_connection_create_from_existing (struct GNUNET_SCHEDULER_Handle | 221 | GNUNET_NETWORK_connection_create_from_existing (struct GNUNET_SCHEDULER_Handle |
222 | *sched, int osSocket, | 222 | *sched, struct GNUNET_NETWORK_Descriptor *osSocket, |
223 | size_t maxbuf) | 223 | size_t maxbuf) |
224 | { | 224 | { |
225 | struct GNUNET_NETWORK_ConnectionHandle *ret; | 225 | struct GNUNET_NETWORK_ConnectionHandle *ret; |
@@ -248,13 +248,13 @@ struct GNUNET_NETWORK_ConnectionHandle * | |||
248 | GNUNET_NETWORK_connection_create_from_accept (struct GNUNET_SCHEDULER_Handle | 248 | GNUNET_NETWORK_connection_create_from_accept (struct GNUNET_SCHEDULER_Handle |
249 | *sched, | 249 | *sched, |
250 | GNUNET_NETWORK_AccessCheck access, | 250 | GNUNET_NETWORK_AccessCheck access, |
251 | void *access_cls, int lsock, | 251 | void *access_cls, struct GNUNET_NETWORK_Descriptor *lsock, |
252 | size_t maxbuf) | 252 | size_t maxbuf) |
253 | { | 253 | { |
254 | struct GNUNET_NETWORK_ConnectionHandle *ret; | 254 | struct GNUNET_NETWORK_ConnectionHandle *ret; |
255 | char addr[32]; | 255 | char addr[32]; |
256 | socklen_t addrlen; | 256 | socklen_t addrlen; |
257 | int fd; | 257 | struct GNUNET_NETWORK_Descriptor *sock; |
258 | int aret; | 258 | int aret; |
259 | struct sockaddr_in *v4; | 259 | struct sockaddr_in *v4; |
260 | struct sockaddr_in6 *v6; | 260 | struct sockaddr_in6 *v6; |
@@ -262,22 +262,21 @@ GNUNET_NETWORK_connection_create_from_accept (struct GNUNET_SCHEDULER_Handle | |||
262 | void *uaddr; | 262 | void *uaddr; |
263 | 263 | ||
264 | addrlen = sizeof (addr); | 264 | addrlen = sizeof (addr); |
265 | fd = accept (lsock, (struct sockaddr *) &addr, &addrlen); | 265 | sock = GNUNET_NETWORK_socket_accept (lsock, (struct sockaddr *) &addr, &addrlen); |
266 | if (fd == -1) | 266 | if (NULL == sock) |
267 | { | 267 | { |
268 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept"); | 268 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept"); |
269 | return NULL; | 269 | return NULL; |
270 | } | 270 | } |
271 | #ifndef MINGW | 271 | #ifndef MINGW |
272 | // FIXME NILS | 272 | if (GNUNET_OK != GNUNET_NETWORK_socket_set_inheritable (sock)) |
273 | if (0 != fcntl (fd, F_SETFD, fcntl (fd, F_GETFD) | FD_CLOEXEC)) | ||
274 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 273 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
275 | "fcntl"); | 274 | "fcntl"); |
276 | #endif | 275 | #endif |
277 | if (addrlen > sizeof (addr)) | 276 | if (addrlen > sizeof (addr)) |
278 | { | 277 | { |
279 | GNUNET_break (0); | 278 | GNUNET_break (0); |
280 | GNUNET_break (0 == CLOSE (fd)); | 279 | GNUNET_break (0 == GNUNET_NETWORK_socket_close (sock)); |
281 | return NULL; | 280 | return NULL; |
282 | } | 281 | } |
283 | 282 | ||
@@ -310,8 +309,8 @@ GNUNET_NETWORK_connection_create_from_accept (struct GNUNET_SCHEDULER_Handle | |||
310 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 309 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, |
311 | _("Access denied to `%s'\n"), | 310 | _("Access denied to `%s'\n"), |
312 | GNUNET_a2s(uaddr, addrlen)); | 311 | GNUNET_a2s(uaddr, addrlen)); |
313 | GNUNET_break (0 == SHUTDOWN (fd, SHUT_RDWR)); | 312 | GNUNET_break (0 == GNUNET_NETWORK_socket_shutdown (sock, SHUT_RDWR)); |
314 | GNUNET_break (0 == CLOSE (fd)); | 313 | GNUNET_break (0 == GNUNET_NETWORK_socket_close (sock)); |
315 | GNUNET_free (uaddr); | 314 | GNUNET_free (uaddr); |
316 | return NULL; | 315 | return NULL; |
317 | } | 316 | } |
@@ -325,7 +324,7 @@ GNUNET_NETWORK_connection_create_from_accept (struct GNUNET_SCHEDULER_Handle | |||
325 | ret->write_buffer_size = maxbuf; | 324 | ret->write_buffer_size = maxbuf; |
326 | ret->addr = uaddr; | 325 | ret->addr = uaddr; |
327 | ret->addrlen = addrlen; | 326 | ret->addrlen = addrlen; |
328 | ret->sock = fd; | 327 | ret->sock = sock; |
329 | ret->sched = sched; | 328 | ret->sched = sched; |
330 | return ret; | 329 | return ret; |
331 | } | 330 | } |
@@ -350,64 +349,6 @@ GNUNET_NETWORK_connection_get_address (struct GNUNET_NETWORK_ConnectionHandle *s | |||
350 | return GNUNET_OK; | 349 | return GNUNET_OK; |
351 | } | 350 | } |
352 | 351 | ||
353 | |||
354 | /** | ||
355 | * Set if a socket should use blocking or non-blocking IO. | ||
356 | * | ||
357 | * @return GNUNET_OK on success, GNUNET_SYSERR on error | ||
358 | */ | ||
359 | static int | ||
360 | socket_set_blocking (int handle, int doBlock) | ||
361 | { | ||
362 | #if MINGW | ||
363 | u_long mode; | ||
364 | mode = !doBlock; | ||
365 | #if HAVE_PLIBC_FD | ||
366 | if (ioctlsocket (plibc_fd_get_handle (handle), FIONBIO, &mode) == | ||
367 | SOCKET_ERROR) | ||
368 | { | ||
369 | SetErrnoFromWinsockError (WSAGetLastError ()); | ||
370 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket"); | ||
371 | return GNUNET_SYSERR; | ||
372 | } | ||
373 | #else | ||
374 | if (ioctlsocket (handle, FIONBIO, &mode) == SOCKET_ERROR) | ||
375 | { | ||
376 | SetErrnoFromWinsockError (WSAGetLastError ()); | ||
377 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket"); | ||
378 | return GNUNET_SYSERR; | ||
379 | } | ||
380 | #endif | ||
381 | /* store the blocking mode */ | ||
382 | #if HAVE_PLIBC_FD | ||
383 | plibc_fd_set_blocking (handle, doBlock); | ||
384 | #else | ||
385 | __win_SetHandleBlockingMode (handle, doBlock); | ||
386 | #endif | ||
387 | return GNUNET_OK; | ||
388 | |||
389 | #else | ||
390 | /* not MINGW */ | ||
391 | int flags = fcntl (handle, F_GETFL); | ||
392 | if (flags == -1) | ||
393 | { | ||
394 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl"); | ||
395 | return GNUNET_SYSERR; | ||
396 | } | ||
397 | if (doBlock) | ||
398 | flags &= ~O_NONBLOCK; | ||
399 | else | ||
400 | flags |= O_NONBLOCK; | ||
401 | if (0 != fcntl (handle, F_SETFL, flags)) | ||
402 | { | ||
403 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl"); | ||
404 | return GNUNET_SYSERR; | ||
405 | } | ||
406 | return GNUNET_OK; | ||
407 | #endif | ||
408 | } | ||
409 | |||
410 | |||
411 | /** | 352 | /** |
412 | * Perform a DNS lookup for the hostname associated | 353 | * Perform a DNS lookup for the hostname associated |
413 | * with the current socket, iterating over the address | 354 | * with the current socket, iterating over the address |
@@ -448,7 +389,7 @@ try_lookup (struct GNUNET_NETWORK_ConnectionHandle *sock) | |||
448 | static int | 389 | static int |
449 | try_connect (struct GNUNET_NETWORK_ConnectionHandle *sock) | 390 | try_connect (struct GNUNET_NETWORK_ConnectionHandle *sock) |
450 | { | 391 | { |
451 | int s; | 392 | struct GNUNET_NETWORK_Descriptor *s; |
452 | 393 | ||
453 | if (sock->addr != NULL) | 394 | if (sock->addr != NULL) |
454 | { | 395 | { |
@@ -479,8 +420,8 @@ try_connect (struct GNUNET_NETWORK_ConnectionHandle *sock) | |||
479 | sock->ai_pos = sock->ai_pos->ai_next; | 420 | sock->ai_pos = sock->ai_pos->ai_next; |
480 | continue; | 421 | continue; |
481 | } | 422 | } |
482 | s = SOCKET (sock->ai_pos->ai_family, SOCK_STREAM, 0); | 423 | s = GNUNET_NETWORK_socket_socket (sock->ai_pos->ai_family, SOCK_STREAM, 0); |
483 | if (s == -1) | 424 | if (s == NULL) |
484 | { | 425 | { |
485 | /* maybe unsupported address family, try next */ | 426 | /* maybe unsupported address family, try next */ |
486 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "socket"); | 427 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "socket"); |
@@ -488,15 +429,14 @@ try_connect (struct GNUNET_NETWORK_ConnectionHandle *sock) | |||
488 | continue; | 429 | continue; |
489 | } | 430 | } |
490 | #ifndef MINGW | 431 | #ifndef MINGW |
491 | // FIXME NILS | 432 | if (GNUNET_OK != GNUNET_NETWORK_socket_set_inheritable (s)) |
492 | if (0 != fcntl (s, F_SETFD, fcntl (s, F_GETFD) | FD_CLOEXEC)) | ||
493 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 433 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
494 | "fcntl"); | 434 | "GNUNET_NETWORK_socket_set_inheritable"); |
495 | #endif | 435 | #endif |
496 | if (GNUNET_SYSERR == socket_set_blocking (s, GNUNET_NO)) | 436 | if (GNUNET_SYSERR == GNUNET_NETWORK_socket_set_blocking (s, GNUNET_NO)) |
497 | { | 437 | { |
498 | /* we'll treat this one as fatal */ | 438 | /* we'll treat this one as fatal */ |
499 | GNUNET_break (0 == CLOSE (s)); | 439 | GNUNET_break (0 == GNUNET_NETWORK_socket_close (s)); |
500 | return GNUNET_SYSERR; | 440 | return GNUNET_SYSERR; |
501 | } | 441 | } |
502 | #if DEBUG_NETWORK | 442 | #if DEBUG_NETWORK |
@@ -505,13 +445,13 @@ try_connect (struct GNUNET_NETWORK_ConnectionHandle *sock) | |||
505 | GNUNET_a2s(sock->ai_pos->ai_addr, | 445 | GNUNET_a2s(sock->ai_pos->ai_addr, |
506 | sock->ai_pos->ai_addrlen)); | 446 | sock->ai_pos->ai_addrlen)); |
507 | #endif | 447 | #endif |
508 | if ((0 != CONNECT (s, | 448 | if ((0 != GNUNET_NETWORK_socket_connect (s, |
509 | sock->ai_pos->ai_addr, | 449 | sock->ai_pos->ai_addr, |
510 | sock->ai_pos->ai_addrlen)) && (errno != EINPROGRESS)) | 450 | sock->ai_pos->ai_addrlen)) && (errno != EINPROGRESS)) |
511 | { | 451 | { |
512 | /* maybe refused / unsupported address, try next */ | 452 | /* maybe refused / unsupported address, try next */ |
513 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect"); | 453 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect"); |
514 | GNUNET_break (0 == CLOSE (s)); | 454 | GNUNET_break (0 == GNUNET_NETWORK_socket_close (s)); |
515 | sock->ai_pos = sock->ai_pos->ai_next; | 455 | sock->ai_pos = sock->ai_pos->ai_next; |
516 | continue; | 456 | continue; |
517 | } | 457 | } |
@@ -548,7 +488,7 @@ connect_continuation (void *cls, | |||
548 | errno = 0; | 488 | errno = 0; |
549 | error = 0; | 489 | error = 0; |
550 | if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) || | 490 | if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) || |
551 | (0 != getsockopt (sock->sock, SOL_SOCKET, SO_ERROR, &error, &len)) || | 491 | (0 != GNUNET_NETWORK_socket_getsockopt (sock->sock, SOL_SOCKET, SO_ERROR, &error, &len)) || |
552 | (error != 0) || (errno != 0)) | 492 | (error != 0) || (errno != 0)) |
553 | { | 493 | { |
554 | #if DEBUG_NETWORK | 494 | #if DEBUG_NETWORK |
@@ -557,8 +497,8 @@ connect_continuation (void *cls, | |||
557 | GNUNET_a2s(sock->addr, sock->addrlen)); | 497 | GNUNET_a2s(sock->addr, sock->addrlen)); |
558 | #endif | 498 | #endif |
559 | /* connect failed / timed out */ | 499 | /* connect failed / timed out */ |
560 | GNUNET_break (0 == CLOSE (sock->sock)); | 500 | GNUNET_break (0 == GNUNET_NETWORK_socket_close (sock->sock)); |
561 | sock->sock = -1; | 501 | sock->sock = NULL; |
562 | if (GNUNET_SYSERR == try_connect (sock)) | 502 | if (GNUNET_SYSERR == try_connect (sock)) |
563 | { | 503 | { |
564 | /* failed for good */ | 504 | /* failed for good */ |
@@ -572,7 +512,7 @@ connect_continuation (void *cls, | |||
572 | sock->ai = NULL; | 512 | sock->ai = NULL; |
573 | return; | 513 | return; |
574 | } | 514 | } |
575 | sock->connect_task = GNUNET_SCHEDULER_add_write (tc->sched, GNUNET_NO, /* abort on shutdown */ | 515 | sock->connect_task = GNUNET_SCHEDULER_add_write_net (tc->sched, GNUNET_NO, /* abort on shutdown */ |
576 | GNUNET_SCHEDULER_PRIORITY_KEEP, | 516 | GNUNET_SCHEDULER_PRIORITY_KEEP, |
577 | GNUNET_SCHEDULER_NO_TASK, | 517 | GNUNET_SCHEDULER_NO_TASK, |
578 | GNUNET_NETWORK_CONNECT_RETRY_TIMEOUT, | 518 | GNUNET_NETWORK_CONNECT_RETRY_TIMEOUT, |
@@ -613,7 +553,7 @@ GNUNET_NETWORK_connection_create_from_connect (struct GNUNET_SCHEDULER_Handle | |||
613 | struct GNUNET_NETWORK_ConnectionHandle *ret; | 553 | struct GNUNET_NETWORK_ConnectionHandle *ret; |
614 | 554 | ||
615 | ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_ConnectionHandle) + maxbuf); | 555 | ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_ConnectionHandle) + maxbuf); |
616 | ret->sock = -1; | 556 | ret->sock = NULL; |
617 | ret->sched = sched; | 557 | ret->sched = sched; |
618 | ret->write_buffer = (char *) &ret[1]; | 558 | ret->write_buffer = (char *) &ret[1]; |
619 | ret->write_buffer_size = maxbuf; | 559 | ret->write_buffer_size = maxbuf; |
@@ -628,7 +568,7 @@ GNUNET_NETWORK_connection_create_from_connect (struct GNUNET_SCHEDULER_Handle | |||
628 | GNUNET_free (ret); | 568 | GNUNET_free (ret); |
629 | return NULL; | 569 | return NULL; |
630 | } | 570 | } |
631 | ret->connect_task = GNUNET_SCHEDULER_add_write (sched, GNUNET_NO, /* abort on shutdown */ | 571 | ret->connect_task = GNUNET_SCHEDULER_add_write_net (sched, GNUNET_NO, /* abort on shutdown */ |
632 | GNUNET_SCHEDULER_PRIORITY_KEEP, | 572 | GNUNET_SCHEDULER_PRIORITY_KEEP, |
633 | GNUNET_SCHEDULER_NO_TASK, | 573 | GNUNET_SCHEDULER_NO_TASK, |
634 | GNUNET_NETWORK_CONNECT_RETRY_TIMEOUT, | 574 | GNUNET_NETWORK_CONNECT_RETRY_TIMEOUT, |
@@ -658,25 +598,28 @@ GNUNET_NETWORK_connection_create_from_sockaddr (struct GNUNET_SCHEDULER_Handle | |||
658 | const struct sockaddr *serv_addr, | 598 | const struct sockaddr *serv_addr, |
659 | socklen_t addrlen, size_t maxbuf) | 599 | socklen_t addrlen, size_t maxbuf) |
660 | { | 600 | { |
661 | int s; | 601 | struct GNUNET_NETWORK_Descriptor *s; |
662 | struct GNUNET_NETWORK_ConnectionHandle *ret; | 602 | struct GNUNET_NETWORK_ConnectionHandle *ret; |
663 | 603 | ||
664 | s = SOCKET (af_family, SOCK_STREAM, 0); | 604 | s = GNUNET_NETWORK_socket_socket (af_family, SOCK_STREAM, 0); |
665 | if (s == -1) | 605 | if (s == NULL) |
666 | { | 606 | { |
667 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | | 607 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | |
668 | GNUNET_ERROR_TYPE_BULK, "socket"); | 608 | GNUNET_ERROR_TYPE_BULK, "socket"); |
669 | return NULL; | 609 | return NULL; |
670 | } | 610 | } |
671 | #ifndef MINGW | 611 | #ifndef MINGW |
612 | #if 0 | ||
613 | // FIXME NILS | ||
672 | if (0 != fcntl (s, F_SETFD, fcntl (s, F_GETFD) | FD_CLOEXEC)) | 614 | if (0 != fcntl (s, F_SETFD, fcntl (s, F_GETFD) | FD_CLOEXEC)) |
673 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 615 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
674 | "fcntl"); | 616 | "fcntl"); |
675 | #endif | 617 | #endif |
676 | if (GNUNET_SYSERR == socket_set_blocking (s, GNUNET_NO)) | 618 | #endif |
619 | if (GNUNET_SYSERR == GNUNET_NETWORK_socket_set_blocking (s, GNUNET_NO)) | ||
677 | { | 620 | { |
678 | /* we'll treat this one as fatal */ | 621 | /* we'll treat this one as fatal */ |
679 | GNUNET_break (0 == CLOSE (s)); | 622 | GNUNET_break (0 == GNUNET_NETWORK_socket_close (s)); |
680 | return NULL; | 623 | return NULL; |
681 | } | 624 | } |
682 | #if DEBUG_NETWORK | 625 | #if DEBUG_NETWORK |
@@ -684,11 +627,11 @@ GNUNET_NETWORK_connection_create_from_sockaddr (struct GNUNET_SCHEDULER_Handle | |||
684 | _("Trying to connect to `%s'\n"), | 627 | _("Trying to connect to `%s'\n"), |
685 | GNUNET_a2s(serv_addr, addrlen)); | 628 | GNUNET_a2s(serv_addr, addrlen)); |
686 | #endif | 629 | #endif |
687 | if ((0 != CONNECT (s, serv_addr, addrlen)) && (errno != EINPROGRESS)) | 630 | if ((0 != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen)) && (errno != EINPROGRESS)) |
688 | { | 631 | { |
689 | /* maybe refused / unsupported address, try next */ | 632 | /* maybe refused / unsupported address, try next */ |
690 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect"); | 633 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect"); |
691 | GNUNET_break (0 == CLOSE (s)); | 634 | GNUNET_break (0 == GNUNET_NETWORK_socket_close (s)); |
692 | return NULL; | 635 | return NULL; |
693 | } | 636 | } |
694 | ret = GNUNET_NETWORK_connection_create_from_existing (sched, s, maxbuf); | 637 | ret = GNUNET_NETWORK_connection_create_from_existing (sched, s, maxbuf); |
@@ -712,7 +655,7 @@ GNUNET_NETWORK_connection_check (struct GNUNET_NETWORK_ConnectionHandle *sock) | |||
712 | { | 655 | { |
713 | if (sock->ai != NULL) | 656 | if (sock->ai != NULL) |
714 | return GNUNET_YES; /* still trying to connect */ | 657 | return GNUNET_YES; /* still trying to connect */ |
715 | return (sock->sock == -1) ? GNUNET_NO : GNUNET_YES; | 658 | return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES; |
716 | } | 659 | } |
717 | 660 | ||
718 | 661 | ||
@@ -736,12 +679,12 @@ destroy_continuation (void *cls, | |||
736 | &destroy_continuation, sock); | 679 | &destroy_continuation, sock); |
737 | return; | 680 | return; |
738 | } | 681 | } |
739 | if (sock->sock != -1) | 682 | if (sock->sock != NULL) |
740 | { | 683 | { |
741 | #if DEBUG_NETWORK | 684 | #if DEBUG_NETWORK |
742 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down socket.\n"); | 685 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down socket.\n"); |
743 | #endif | 686 | #endif |
744 | SHUTDOWN (sock->sock, SHUT_RDWR); | 687 | GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR); |
745 | } | 688 | } |
746 | if (sock->read_task != GNUNET_SCHEDULER_NO_TASK) | 689 | if (sock->read_task != GNUNET_SCHEDULER_NO_TASK) |
747 | { | 690 | { |
@@ -762,8 +705,8 @@ destroy_continuation (void *cls, | |||
762 | sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; | 705 | sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; |
763 | } | 706 | } |
764 | } | 707 | } |
765 | if (sock->sock != -1) | 708 | if (sock->sock != NULL) |
766 | GNUNET_break (0 == CLOSE (sock->sock)); | 709 | GNUNET_break (0 == GNUNET_NETWORK_socket_close (sock->sock)); |
767 | GNUNET_free_non_null (sock->addr); | 710 | GNUNET_free_non_null (sock->addr); |
768 | if (sock->ai != NULL) | 711 | if (sock->ai != NULL) |
769 | freeaddrinfo (sock->ai); | 712 | freeaddrinfo (sock->ai); |
@@ -850,7 +793,7 @@ receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
850 | signal_timeout (sh); | 793 | signal_timeout (sh); |
851 | return; | 794 | return; |
852 | } | 795 | } |
853 | if (sh->sock == -1) | 796 | if (sh->sock == NULL) |
854 | { | 797 | { |
855 | /* connect failed for good */ | 798 | /* connect failed for good */ |
856 | #if DEBUG_NETWORK | 799 | #if DEBUG_NETWORK |
@@ -860,9 +803,9 @@ receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
860 | signal_error (sh, ECONNREFUSED); | 803 | signal_error (sh, ECONNREFUSED); |
861 | return; | 804 | return; |
862 | } | 805 | } |
863 | GNUNET_assert (FD_ISSET (sh->sock, tc->read_ready)); | 806 | GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, sh->sock)); |
864 | RETRY: | 807 | RETRY: |
865 | ret = RECV (sh->sock, buffer, sh->max, | 808 | ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max, |
866 | #ifndef MINGW | 809 | #ifndef MINGW |
867 | // FIXME MINGW | 810 | // FIXME MINGW |
868 | MSG_DONTWAIT | 811 | MSG_DONTWAIT |
@@ -908,7 +851,7 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
908 | struct GNUNET_TIME_Absolute now; | 851 | struct GNUNET_TIME_Absolute now; |
909 | 852 | ||
910 | sh->read_task = GNUNET_SCHEDULER_NO_TASK; | 853 | sh->read_task = GNUNET_SCHEDULER_NO_TASK; |
911 | if ((sh->sock == -1) && | 854 | if ((sh->sock == NULL) && |
912 | (sh->connect_task == GNUNET_SCHEDULER_NO_TASK)) | 855 | (sh->connect_task == GNUNET_SCHEDULER_NO_TASK)) |
913 | { | 856 | { |
914 | /* not connected and no longer trying */ | 857 | /* not connected and no longer trying */ |
@@ -941,7 +884,7 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
941 | return; | 884 | return; |
942 | } | 885 | } |
943 | /* connect succeeded, wait for data! */ | 886 | /* connect succeeded, wait for data! */ |
944 | sh->read_task = GNUNET_SCHEDULER_add_read (tc->sched, | 887 | sh->read_task = GNUNET_SCHEDULER_add_read_net (tc->sched, |
945 | GNUNET_YES, | 888 | GNUNET_YES, |
946 | GNUNET_SCHEDULER_PRIORITY_KEEP, | 889 | GNUNET_SCHEDULER_PRIORITY_KEEP, |
947 | sh->connect_task, | 890 | sh->connect_task, |
@@ -1120,25 +1063,25 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
1120 | sock); | 1063 | sock); |
1121 | return; | 1064 | return; |
1122 | } | 1065 | } |
1123 | if ( (sock->sock == -1) || | 1066 | if ( (sock->sock == NULL) || |
1124 | ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) && | 1067 | ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) && |
1125 | (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE)) && | 1068 | (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE)) && |
1126 | (!FD_ISSET (sock->sock, tc->write_ready))) ) | 1069 | (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock))) ) |
1127 | { | 1070 | { |
1128 | #if DEBUG_NETWORK | 1071 | #if DEBUG_NETWORK |
1129 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 1072 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, |
1130 | _("Could not satisfy pending transmission request, socket closed or connect failed.\n")); | 1073 | _("Could not satisfy pending transmission request, socket closed or connect failed.\n")); |
1131 | #endif | 1074 | #endif |
1132 | if (-1 != sock->sock) | 1075 | if (NULL != sock->sock) |
1133 | { | 1076 | { |
1134 | SHUTDOWN (sock->sock, SHUT_RDWR); | 1077 | GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR); |
1135 | GNUNET_break (0 == CLOSE (sock->sock)); | 1078 | GNUNET_break (0 == GNUNET_NETWORK_socket_close (sock->sock)); |
1136 | sock->sock = -1; | 1079 | sock->sock = NULL; |
1137 | } | 1080 | } |
1138 | transmit_error (sock); | 1081 | transmit_error (sock); |
1139 | return; /* connect failed for good, we're finished */ | 1082 | return; /* connect failed for good, we're finished */ |
1140 | } | 1083 | } |
1141 | if ((tc->write_ready == NULL) || (!FD_ISSET (sock->sock, tc->write_ready))) | 1084 | if ((tc->write_ready == NULL) || (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock))) |
1142 | { | 1085 | { |
1143 | /* special circumstances (in particular, | 1086 | /* special circumstances (in particular, |
1144 | PREREQ_DONE after connect): not yet ready to write, | 1087 | PREREQ_DONE after connect): not yet ready to write, |
@@ -1154,7 +1097,7 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
1154 | return; | 1097 | return; |
1155 | } | 1098 | } |
1156 | RETRY: | 1099 | RETRY: |
1157 | ret = SEND (sock->sock, | 1100 | ret = GNUNET_NETWORK_socket_send (sock->sock, |
1158 | &sock->write_buffer[sock->write_buffer_pos], | 1101 | &sock->write_buffer[sock->write_buffer_pos], |
1159 | have, | 1102 | have, |
1160 | #ifndef MINGW | 1103 | #ifndef MINGW |
@@ -1171,9 +1114,9 @@ RETRY: | |||
1171 | #if DEBUG_NETWORK | 1114 | #if DEBUG_NETWORK |
1172 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send"); | 1115 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send"); |
1173 | #endif | 1116 | #endif |
1174 | SHUTDOWN (sock->sock, SHUT_RDWR); | 1117 | GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR); |
1175 | GNUNET_break (0 == CLOSE (sock->sock)); | 1118 | GNUNET_break (0 == GNUNET_NETWORK_socket_close (sock->sock)); |
1176 | sock->sock = -1; | 1119 | sock->sock = NULL; |
1177 | transmit_error (sock); | 1120 | transmit_error (sock); |
1178 | return; | 1121 | return; |
1179 | } | 1122 | } |
@@ -1197,7 +1140,7 @@ RETRY: | |||
1197 | SCHEDULE_WRITE: | 1140 | SCHEDULE_WRITE: |
1198 | if (sock->write_task == GNUNET_SCHEDULER_NO_TASK) | 1141 | if (sock->write_task == GNUNET_SCHEDULER_NO_TASK) |
1199 | sock->write_task = | 1142 | sock->write_task = |
1200 | GNUNET_SCHEDULER_add_write (tc->sched, | 1143 | GNUNET_SCHEDULER_add_write_net (tc->sched, |
1201 | GNUNET_NO, | 1144 | GNUNET_NO, |
1202 | GNUNET_SCHEDULER_PRIORITY_KEEP, | 1145 | GNUNET_SCHEDULER_PRIORITY_KEEP, |
1203 | GNUNET_SCHEDULER_NO_TASK, | 1146 | GNUNET_SCHEDULER_NO_TASK, |
@@ -1232,7 +1175,7 @@ GNUNET_NETWORK_connection_notify_transmit_ready (struct GNUNET_NETWORK_Connectio | |||
1232 | GNUNET_assert (notify != NULL); | 1175 | GNUNET_assert (notify != NULL); |
1233 | GNUNET_assert (sock->write_buffer_size >= size); | 1176 | GNUNET_assert (sock->write_buffer_size >= size); |
1234 | 1177 | ||
1235 | if ((sock->sock == -1) && | 1178 | if ((sock->sock == NULL) && |
1236 | (sock->connect_task == GNUNET_SCHEDULER_NO_TASK)) | 1179 | (sock->connect_task == GNUNET_SCHEDULER_NO_TASK)) |
1237 | { | 1180 | { |
1238 | #if DEBUG_NETWORK | 1181 | #if DEBUG_NETWORK |
@@ -1261,7 +1204,7 @@ GNUNET_NETWORK_connection_notify_transmit_ready (struct GNUNET_NETWORK_Connectio | |||
1261 | if (sock->write_task == GNUNET_SCHEDULER_NO_TASK) | 1204 | if (sock->write_task == GNUNET_SCHEDULER_NO_TASK) |
1262 | { | 1205 | { |
1263 | if (sock->connect_task == GNUNET_SCHEDULER_NO_TASK) | 1206 | if (sock->connect_task == GNUNET_SCHEDULER_NO_TASK) |
1264 | sock->write_task = GNUNET_SCHEDULER_add_write (sock->sched, | 1207 | sock->write_task = GNUNET_SCHEDULER_add_write_net (sock->sched, |
1265 | GNUNET_NO, | 1208 | GNUNET_NO, |
1266 | GNUNET_SCHEDULER_PRIORITY_KEEP, | 1209 | GNUNET_SCHEDULER_PRIORITY_KEEP, |
1267 | GNUNET_SCHEDULER_NO_TASK, | 1210 | GNUNET_SCHEDULER_NO_TASK, |
diff --git a/src/util/scheduler.c b/src/util/scheduler.c index 0b5307290..69a5a83e1 100644 --- a/src/util/scheduler.c +++ b/src/util/scheduler.c | |||
@@ -55,7 +55,7 @@ struct Task | |||
55 | * to reflect the set of file descriptors ready | 55 | * to reflect the set of file descriptors ready |
56 | * for operation. | 56 | * for operation. |
57 | */ | 57 | */ |
58 | fd_set read_set; | 58 | struct GNUNET_NETWORK_FDSet *read_set; |
59 | 59 | ||
60 | /** | 60 | /** |
61 | * Set of file descriptors this task is waiting | 61 | * Set of file descriptors this task is waiting |
@@ -63,7 +63,7 @@ struct Task | |||
63 | * to reflect the set of file descriptors ready | 63 | * to reflect the set of file descriptors ready |
64 | * for operation. | 64 | * for operation. |
65 | */ | 65 | */ |
66 | fd_set write_set; | 66 | struct GNUNET_NETWORK_FDSet *write_set; |
67 | 67 | ||
68 | /** | 68 | /** |
69 | * Unique task identifier. | 69 | * Unique task identifier. |
@@ -94,11 +94,6 @@ struct Task | |||
94 | enum GNUNET_SCHEDULER_Priority priority; | 94 | enum GNUNET_SCHEDULER_Priority priority; |
95 | 95 | ||
96 | /** | 96 | /** |
97 | * highest-numbered file descriptor in read_set or write_set plus one | ||
98 | */ | ||
99 | int nfds; | ||
100 | |||
101 | /** | ||
102 | * Should this task be run on shutdown? | 97 | * Should this task be run on shutdown? |
103 | */ | 98 | */ |
104 | int run_on_shutdown; | 99 | int run_on_shutdown; |
@@ -171,33 +166,6 @@ check_priority (enum GNUNET_SCHEDULER_Priority p) | |||
171 | 166 | ||
172 | 167 | ||
173 | /** | 168 | /** |
174 | * Update the timeout value so that it is smaller than min. | ||
175 | */ | ||
176 | static void | ||
177 | update_timeout (struct timeval *tv, struct GNUNET_TIME_Relative min) | ||
178 | { | ||
179 | if (((tv->tv_sec * 1000) + (tv->tv_usec / 1000)) > min.value) | ||
180 | { | ||
181 | tv->tv_sec = min.value / 1000; | ||
182 | tv->tv_usec = (min.value - tv->tv_sec * 1000) * 1000; | ||
183 | } | ||
184 | } | ||
185 | |||
186 | |||
187 | /** | ||
188 | * Set the given file descriptor bit in the given set and update max | ||
189 | * to the maximum of the existing max and fd+1. | ||
190 | */ | ||
191 | static void | ||
192 | set_fd (int fd, int *max, fd_set * set) | ||
193 | { | ||
194 | if (*max <= fd) | ||
195 | *max = fd + 1; | ||
196 | FD_SET (fd, set); | ||
197 | } | ||
198 | |||
199 | |||
200 | /** | ||
201 | * Is a task with this identifier still pending? Also updates | 169 | * Is a task with this identifier still pending? Also updates |
202 | * "lowest_pending_id" as a side-effect (for faster checks in the | 170 | * "lowest_pending_id" as a side-effect (for faster checks in the |
203 | * future), but only if the return value is "GNUNET_NO" (and | 171 | * future), but only if the return value is "GNUNET_NO" (and |
@@ -247,14 +215,14 @@ is_pending (struct GNUNET_SCHEDULER_Handle *sched, | |||
247 | */ | 215 | */ |
248 | static void | 216 | static void |
249 | update_sets (struct GNUNET_SCHEDULER_Handle *sched, | 217 | update_sets (struct GNUNET_SCHEDULER_Handle *sched, |
250 | int *max, fd_set * rs, fd_set * ws, struct timeval *tv) | 218 | struct GNUNET_NETWORK_FDSet * rs, struct GNUNET_NETWORK_FDSet * ws, struct GNUNET_TIME_Relative *timeout) |
251 | { | 219 | { |
252 | int i; | ||
253 | struct Task *pos; | 220 | struct Task *pos; |
254 | 221 | ||
255 | pos = sched->pending; | 222 | pos = sched->pending; |
256 | while (pos != NULL) | 223 | while (pos != NULL) |
257 | { | 224 | { |
225 | |||
258 | if ((pos->prereq_id != GNUNET_SCHEDULER_NO_TASK) && | 226 | if ((pos->prereq_id != GNUNET_SCHEDULER_NO_TASK) && |
259 | (GNUNET_YES == is_pending (sched, pos->prereq_id))) | 227 | (GNUNET_YES == is_pending (sched, pos->prereq_id))) |
260 | { | 228 | { |
@@ -263,15 +231,16 @@ update_sets (struct GNUNET_SCHEDULER_Handle *sched, | |||
263 | } | 231 | } |
264 | 232 | ||
265 | if (pos->timeout.value != GNUNET_TIME_UNIT_FOREVER_ABS.value) | 233 | if (pos->timeout.value != GNUNET_TIME_UNIT_FOREVER_ABS.value) |
266 | update_timeout (tv, | ||
267 | GNUNET_TIME_absolute_get_remaining (pos->timeout)); | ||
268 | for (i = 0; i < pos->nfds; i++) | ||
269 | { | 234 | { |
270 | if (FD_ISSET (i, &pos->read_set)) | 235 | struct GNUNET_TIME_Relative to; |
271 | set_fd (i, max, rs); | 236 | |
272 | if (FD_ISSET (i, &pos->write_set)) | 237 | to = GNUNET_TIME_absolute_get_remaining (pos->timeout); |
273 | set_fd (i, max, ws); | 238 | if (timeout->value > to.value) |
239 | *timeout = to; | ||
274 | } | 240 | } |
241 | |||
242 | GNUNET_NETWORK_fdset_add (rs, pos->read_set); | ||
243 | GNUNET_NETWORK_fdset_add (ws, pos->write_set); | ||
275 | pos = pos->next; | 244 | pos = pos->next; |
276 | } | 245 | } |
277 | } | 246 | } |
@@ -286,18 +255,15 @@ update_sets (struct GNUNET_SCHEDULER_Handle *sched, | |||
286 | * @return GNUNET_YES if there was some overlap | 255 | * @return GNUNET_YES if there was some overlap |
287 | */ | 256 | */ |
288 | static int | 257 | static int |
289 | set_overlaps (const fd_set * ready, fd_set * want, int maxfd) | 258 | set_overlaps (const struct GNUNET_NETWORK_FDSet * ready, struct GNUNET_NETWORK_FDSet * want) |
290 | { | 259 | { |
291 | int i; | 260 | if (GNUNET_NETWORK_fdset_overlap (ready, want)) |
292 | 261 | { | |
293 | for (i = 0; i < maxfd; i++) | 262 | /* copy all over (yes, there maybe unrelated bits, |
294 | if (FD_ISSET (i, want) && FD_ISSET (i, ready)) | 263 | but this should not hurt well-written clients) */ |
295 | { | 264 | GNUNET_NETWORK_fdset_copy (want, ready); |
296 | /* copy all over (yes, there maybe unrelated bits, | 265 | return GNUNET_YES; |
297 | but this should not hurt well-written clients) */ | 266 | } |
298 | memcpy (want, ready, sizeof (fd_set)); | ||
299 | return GNUNET_YES; | ||
300 | } | ||
301 | return GNUNET_NO; | 267 | return GNUNET_NO; |
302 | } | 268 | } |
303 | 269 | ||
@@ -312,7 +278,7 @@ static int | |||
312 | is_ready (struct GNUNET_SCHEDULER_Handle *sched, | 278 | is_ready (struct GNUNET_SCHEDULER_Handle *sched, |
313 | struct Task *task, | 279 | struct Task *task, |
314 | struct GNUNET_TIME_Absolute now, | 280 | struct GNUNET_TIME_Absolute now, |
315 | const fd_set * rs, const fd_set * ws) | 281 | const struct GNUNET_NETWORK_FDSet * rs, const struct GNUNET_NETWORK_FDSet * ws) |
316 | { | 282 | { |
317 | if ((GNUNET_NO == task->run_on_shutdown) && (GNUNET_YES == sched->shutdown)) | 283 | if ((GNUNET_NO == task->run_on_shutdown) && (GNUNET_YES == sched->shutdown)) |
318 | return GNUNET_NO; | 284 | return GNUNET_NO; |
@@ -322,10 +288,10 @@ is_ready (struct GNUNET_SCHEDULER_Handle *sched, | |||
322 | if (now.value >= task->timeout.value) | 288 | if (now.value >= task->timeout.value) |
323 | task->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT; | 289 | task->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT; |
324 | if ((0 == (task->reason & GNUNET_SCHEDULER_REASON_READ_READY)) && | 290 | if ((0 == (task->reason & GNUNET_SCHEDULER_REASON_READ_READY)) && |
325 | (rs != NULL) && (set_overlaps (rs, &task->read_set, task->nfds))) | 291 | (rs != NULL) && (set_overlaps (rs, task->read_set))) |
326 | task->reason |= GNUNET_SCHEDULER_REASON_READ_READY; | 292 | task->reason |= GNUNET_SCHEDULER_REASON_READ_READY; |
327 | if ((0 == (task->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) && | 293 | if ((0 == (task->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) && |
328 | (ws != NULL) && (set_overlaps (ws, &task->write_set, task->nfds))) | 294 | (ws != NULL) && (set_overlaps (ws, task->write_set))) |
329 | task->reason |= GNUNET_SCHEDULER_REASON_WRITE_READY; | 295 | task->reason |= GNUNET_SCHEDULER_REASON_WRITE_READY; |
330 | if (task->reason == 0) | 296 | if (task->reason == 0) |
331 | return GNUNET_NO; /* not ready */ | 297 | return GNUNET_NO; /* not ready */ |
@@ -357,7 +323,7 @@ queue_ready_task (struct GNUNET_SCHEDULER_Handle *handle, struct Task *task) | |||
357 | */ | 323 | */ |
358 | static void | 324 | static void |
359 | check_ready (struct GNUNET_SCHEDULER_Handle *handle, | 325 | check_ready (struct GNUNET_SCHEDULER_Handle *handle, |
360 | const fd_set * rs, const fd_set * ws) | 326 | const struct GNUNET_NETWORK_FDSet * rs, const struct GNUNET_NETWORK_FDSet * ws) |
361 | { | 327 | { |
362 | struct Task *pos; | 328 | struct Task *pos; |
363 | struct Task *prev; | 329 | struct Task *prev; |
@@ -387,6 +353,19 @@ check_ready (struct GNUNET_SCHEDULER_Handle *handle, | |||
387 | 353 | ||
388 | 354 | ||
389 | /** | 355 | /** |
356 | * Destroy a task | ||
357 | */ | ||
358 | static void destroy_task (struct Task *t) | ||
359 | { | ||
360 | if (t->read_set) | ||
361 | GNUNET_NETWORK_fdset_destroy (t->read_set); | ||
362 | if (t->write_set) | ||
363 | GNUNET_NETWORK_fdset_destroy (t->write_set); | ||
364 | GNUNET_free (t); | ||
365 | } | ||
366 | |||
367 | |||
368 | /** | ||
390 | * Run at least one task in the highest-priority queue that is not | 369 | * Run at least one task in the highest-priority queue that is not |
391 | * empty. Keep running tasks until we are either no longer running | 370 | * empty. Keep running tasks until we are either no longer running |
392 | * "URGENT" tasks or until we have at least one "pending" task (which | 371 | * "URGENT" tasks or until we have at least one "pending" task (which |
@@ -420,10 +399,10 @@ run_ready (struct GNUNET_SCHEDULER_Handle *sched) | |||
420 | GNUNET_assert (pos->priority == p); | 399 | GNUNET_assert (pos->priority == p); |
421 | tc.sched = sched; | 400 | tc.sched = sched; |
422 | tc.reason = pos->reason; | 401 | tc.reason = pos->reason; |
423 | tc.read_ready = &pos->read_set; | 402 | tc.read_ready = pos->read_set; |
424 | tc.write_ready = &pos->write_set; | 403 | tc.write_ready = pos->write_set; |
425 | pos->callback (pos->callback_cls, &tc); | 404 | pos->callback (pos->callback_cls, &tc); |
426 | GNUNET_free (pos); | 405 | destroy_task (pos); |
427 | } | 406 | } |
428 | while ((sched->pending == NULL) || (p == GNUNET_SCHEDULER_PRIORITY_URGENT)); | 407 | while ((sched->pending == NULL) || (p == GNUNET_SCHEDULER_PRIORITY_URGENT)); |
429 | } | 408 | } |
@@ -458,10 +437,9 @@ void | |||
458 | GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *cls) | 437 | GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *cls) |
459 | { | 438 | { |
460 | struct GNUNET_SCHEDULER_Handle sched; | 439 | struct GNUNET_SCHEDULER_Handle sched; |
461 | fd_set rs; | 440 | struct GNUNET_NETWORK_FDSet *rs; |
462 | fd_set ws; | 441 | struct GNUNET_NETWORK_FDSet *ws; |
463 | int max; | 442 | struct GNUNET_TIME_Relative timeout; |
464 | struct timeval tv; | ||
465 | int ret; | 443 | int ret; |
466 | struct GNUNET_SIGNAL_Context *shc_int; | 444 | struct GNUNET_SIGNAL_Context *shc_int; |
467 | struct GNUNET_SIGNAL_Context *shc_term; | 445 | struct GNUNET_SIGNAL_Context *shc_term; |
@@ -470,6 +448,8 @@ GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *cls) | |||
470 | struct Task *tpos; | 448 | struct Task *tpos; |
471 | 449 | ||
472 | sig_shutdown = 0; | 450 | sig_shutdown = 0; |
451 | rs = GNUNET_NETWORK_fdset_create (); | ||
452 | ws = GNUNET_NETWORK_fdset_create (); | ||
473 | #ifndef MINGW | 453 | #ifndef MINGW |
474 | shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown); | 454 | shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown); |
475 | shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown); | 455 | shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown); |
@@ -486,27 +466,24 @@ GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *cls) | |||
486 | (!sig_shutdown) && | 466 | (!sig_shutdown) && |
487 | ((sched.pending != NULL) || (sched.ready_count > 0))) | 467 | ((sched.pending != NULL) || (sched.ready_count > 0))) |
488 | { | 468 | { |
489 | FD_ZERO (&rs); | 469 | GNUNET_NETWORK_fdset_zero (rs); |
490 | FD_ZERO (&ws); | 470 | GNUNET_NETWORK_fdset_zero (ws); |
491 | max = 0; | 471 | timeout = GNUNET_TIME_relative_get_forever(); |
492 | tv.tv_sec = 0x7FFFFFFF; | ||
493 | tv.tv_usec = 0; | ||
494 | if (sched.ready_count > 0) | 472 | if (sched.ready_count > 0) |
495 | { | 473 | { |
496 | /* no blocking, more work already ready! */ | 474 | /* no blocking, more work already ready! */ |
497 | tv.tv_sec = 0; | 475 | timeout = GNUNET_TIME_relative_get_zero(); |
498 | tv.tv_usec = 0; | ||
499 | } | 476 | } |
500 | update_sets (&sched, &max, &rs, &ws, &tv); | 477 | update_sets (&sched, rs, ws, &timeout); |
501 | ret = SELECT (max, &rs, &ws, NULL, &tv); | 478 | ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, GNUNET_TIME_relative_get_zero()); |
502 | if (ret == -1) | 479 | if (ret == GNUNET_SYSERR) |
503 | { | 480 | { |
504 | if (errno == EINTR) | 481 | if (errno == EINTR) |
505 | continue; | 482 | continue; |
506 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select"); | 483 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select"); |
507 | break; | 484 | break; |
508 | } | 485 | } |
509 | check_ready (&sched, &rs, &ws); | 486 | check_ready (&sched, rs, ws); |
510 | run_ready (&sched); | 487 | run_ready (&sched); |
511 | } | 488 | } |
512 | if (sig_shutdown) | 489 | if (sig_shutdown) |
@@ -526,6 +503,8 @@ GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *cls) | |||
526 | sched.pending = tpos->next; | 503 | sched.pending = tpos->next; |
527 | GNUNET_free (tpos); | 504 | GNUNET_free (tpos); |
528 | } | 505 | } |
506 | GNUNET_NETWORK_fdset_destroy (rs); | ||
507 | GNUNET_NETWORK_fdset_destroy (ws); | ||
529 | } | 508 | } |
530 | 509 | ||
531 | 510 | ||
@@ -626,7 +605,7 @@ GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Handle *sched, | |||
626 | else | 605 | else |
627 | prev->next = t->next; | 606 | prev->next = t->next; |
628 | ret = t->callback_cls; | 607 | ret = t->callback_cls; |
629 | GNUNET_free (t); | 608 | destroy_task (t); |
630 | return ret; | 609 | return ret; |
631 | } | 610 | } |
632 | 611 | ||
@@ -688,7 +667,7 @@ GNUNET_SCHEDULER_add_after (struct GNUNET_SCHEDULER_Handle *sched, | |||
688 | return GNUNET_SCHEDULER_add_select (sched, run_on_shutdown, prio, | 667 | return GNUNET_SCHEDULER_add_select (sched, run_on_shutdown, prio, |
689 | prerequisite_task, | 668 | prerequisite_task, |
690 | GNUNET_TIME_UNIT_ZERO, | 669 | GNUNET_TIME_UNIT_ZERO, |
691 | 0, NULL, NULL, main, cls); | 670 | NULL, NULL, main, cls); |
692 | } | 671 | } |
693 | 672 | ||
694 | 673 | ||
@@ -726,7 +705,7 @@ GNUNET_SCHEDULER_add_delayed (struct GNUNET_SCHEDULER_Handle * sched, | |||
726 | { | 705 | { |
727 | return GNUNET_SCHEDULER_add_select (sched, run_on_shutdown, prio, | 706 | return GNUNET_SCHEDULER_add_select (sched, run_on_shutdown, prio, |
728 | prerequisite_task, delay, | 707 | prerequisite_task, delay, |
729 | 0, NULL, NULL, main, cls); | 708 | NULL, NULL, main, cls); |
730 | } | 709 | } |
731 | 710 | ||
732 | 711 | ||
@@ -755,21 +734,24 @@ GNUNET_SCHEDULER_add_delayed (struct GNUNET_SCHEDULER_Handle * sched, | |||
755 | * only valid until "main" is started! | 734 | * only valid until "main" is started! |
756 | */ | 735 | */ |
757 | GNUNET_SCHEDULER_TaskIdentifier | 736 | GNUNET_SCHEDULER_TaskIdentifier |
758 | GNUNET_SCHEDULER_add_read (struct GNUNET_SCHEDULER_Handle * sched, | 737 | GNUNET_SCHEDULER_add_read_net (struct GNUNET_SCHEDULER_Handle * sched, |
759 | int run_on_shutdown, | 738 | int run_on_shutdown, |
760 | enum GNUNET_SCHEDULER_Priority prio, | 739 | enum GNUNET_SCHEDULER_Priority prio, |
761 | GNUNET_SCHEDULER_TaskIdentifier prerequisite_task, | 740 | GNUNET_SCHEDULER_TaskIdentifier prerequisite_task, |
762 | struct GNUNET_TIME_Relative delay, | 741 | struct GNUNET_TIME_Relative delay, |
763 | int rfd, GNUNET_SCHEDULER_Task main, void *cls) | 742 | struct GNUNET_NETWORK_Descriptor *rfd, GNUNET_SCHEDULER_Task main, void *cls) |
764 | { | 743 | { |
765 | fd_set rs; | 744 | struct GNUNET_NETWORK_FDSet *rs; |
745 | GNUNET_SCHEDULER_TaskIdentifier ret; | ||
766 | 746 | ||
767 | GNUNET_assert (rfd >= 0); | 747 | GNUNET_assert (rfd != NULL); |
768 | FD_ZERO (&rs); | 748 | rs = GNUNET_NETWORK_fdset_create (); |
769 | FD_SET (rfd, &rs); | 749 | GNUNET_NETWORK_fdset_set (rs, rfd); |
770 | return GNUNET_SCHEDULER_add_select (sched, run_on_shutdown, prio, | 750 | ret = GNUNET_SCHEDULER_add_select (sched, run_on_shutdown, prio, |
771 | prerequisite_task, delay, | 751 | prerequisite_task, delay, |
772 | rfd + 1, &rs, NULL, main, cls); | 752 | rs, NULL, main, cls); |
753 | GNUNET_NETWORK_fdset_destroy (rs); | ||
754 | return ret; | ||
773 | } | 755 | } |
774 | 756 | ||
775 | 757 | ||
@@ -798,21 +780,24 @@ GNUNET_SCHEDULER_add_read (struct GNUNET_SCHEDULER_Handle * sched, | |||
798 | * only valid until "main" is started! | 780 | * only valid until "main" is started! |
799 | */ | 781 | */ |
800 | GNUNET_SCHEDULER_TaskIdentifier | 782 | GNUNET_SCHEDULER_TaskIdentifier |
801 | GNUNET_SCHEDULER_add_write (struct GNUNET_SCHEDULER_Handle * sched, | 783 | GNUNET_SCHEDULER_add_write_net (struct GNUNET_SCHEDULER_Handle * sched, |
802 | int run_on_shutdown, | 784 | int run_on_shutdown, |
803 | enum GNUNET_SCHEDULER_Priority prio, | 785 | enum GNUNET_SCHEDULER_Priority prio, |
804 | GNUNET_SCHEDULER_TaskIdentifier prerequisite_task, | 786 | GNUNET_SCHEDULER_TaskIdentifier prerequisite_task, |
805 | struct GNUNET_TIME_Relative delay, | 787 | struct GNUNET_TIME_Relative delay, |
806 | int wfd, GNUNET_SCHEDULER_Task main, void *cls) | 788 | struct GNUNET_NETWORK_Descriptor *wfd, GNUNET_SCHEDULER_Task main, void *cls) |
807 | { | 789 | { |
808 | fd_set ws; | 790 | struct GNUNET_NETWORK_FDSet *ws; |
791 | GNUNET_SCHEDULER_TaskIdentifier ret; | ||
809 | 792 | ||
810 | GNUNET_assert (wfd >= 0); | 793 | GNUNET_assert (wfd != NULL); |
811 | FD_ZERO (&ws); | 794 | ws = GNUNET_NETWORK_fdset_create (); |
812 | FD_SET (wfd, &ws); | 795 | GNUNET_NETWORK_fdset_set (ws, wfd); |
813 | return GNUNET_SCHEDULER_add_select (sched, run_on_shutdown, prio, | 796 | ret = GNUNET_SCHEDULER_add_select (sched, run_on_shutdown, prio, |
814 | prerequisite_task, delay, | 797 | prerequisite_task, delay, |
815 | wfd + 1, NULL, &ws, main, cls); | 798 | NULL, ws, main, cls); |
799 | GNUNET_NETWORK_fdset_destroy (ws); | ||
800 | return ret; | ||
816 | } | 801 | } |
817 | 802 | ||
818 | 803 | ||
@@ -844,7 +829,6 @@ GNUNET_SCHEDULER_add_write (struct GNUNET_SCHEDULER_Handle * sched, | |||
844 | * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency | 829 | * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency |
845 | * on completion of other tasks. | 830 | * on completion of other tasks. |
846 | * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever" | 831 | * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever" |
847 | * @param nfds highest-numbered file descriptor in any of the two sets plus one | ||
848 | * @param rs set of file descriptors we want to read (can be NULL) | 832 | * @param rs set of file descriptors we want to read (can be NULL) |
849 | * @param ws set of file descriptors we want to write (can be NULL) | 833 | * @param ws set of file descriptors we want to write (can be NULL) |
850 | * @param main main function of the task | 834 | * @param main main function of the task |
@@ -859,7 +843,7 @@ GNUNET_SCHEDULER_add_select (struct GNUNET_SCHEDULER_Handle * sched, | |||
859 | GNUNET_SCHEDULER_TaskIdentifier | 843 | GNUNET_SCHEDULER_TaskIdentifier |
860 | prerequisite_task, | 844 | prerequisite_task, |
861 | struct GNUNET_TIME_Relative delay, | 845 | struct GNUNET_TIME_Relative delay, |
862 | int nfds, const fd_set * rs, const fd_set * ws, | 846 | const struct GNUNET_NETWORK_FDSet * rs, const struct GNUNET_NETWORK_FDSet * ws, |
863 | GNUNET_SCHEDULER_Task main, void *cls) | 847 | GNUNET_SCHEDULER_Task main, void *cls) |
864 | { | 848 | { |
865 | struct Task *task; | 849 | struct Task *task; |
@@ -867,10 +851,12 @@ GNUNET_SCHEDULER_add_select (struct GNUNET_SCHEDULER_Handle * sched, | |||
867 | task = GNUNET_malloc (sizeof (struct Task)); | 851 | task = GNUNET_malloc (sizeof (struct Task)); |
868 | task->callback = main; | 852 | task->callback = main; |
869 | task->callback_cls = cls; | 853 | task->callback_cls = cls; |
870 | if ((rs != NULL) && (nfds > 0)) | 854 | task->read_set = GNUNET_NETWORK_fdset_create (); |
871 | memcpy (&task->read_set, rs, sizeof (fd_set)); | 855 | if (rs != NULL) |
872 | if ((ws != NULL) && (nfds > 0)) | 856 | GNUNET_NETWORK_fdset_copy (task->read_set, rs); |
873 | memcpy (&task->write_set, ws, sizeof (fd_set)); | 857 | task->write_set = GNUNET_NETWORK_fdset_create (); |
858 | if (ws != NULL) | ||
859 | GNUNET_NETWORK_fdset_copy (task->write_set, ws); | ||
874 | task->id = ++sched->last_id; | 860 | task->id = ++sched->last_id; |
875 | task->prereq_id = prerequisite_task; | 861 | task->prereq_id = prerequisite_task; |
876 | task->timeout = GNUNET_TIME_relative_to_absolute (delay); | 862 | task->timeout = GNUNET_TIME_relative_to_absolute (delay); |
@@ -878,11 +864,102 @@ GNUNET_SCHEDULER_add_select (struct GNUNET_SCHEDULER_Handle * sched, | |||
878 | check_priority ((prio == | 864 | check_priority ((prio == |
879 | GNUNET_SCHEDULER_PRIORITY_KEEP) ? sched->current_priority | 865 | GNUNET_SCHEDULER_PRIORITY_KEEP) ? sched->current_priority |
880 | : prio); | 866 | : prio); |
881 | task->nfds = nfds; | ||
882 | task->run_on_shutdown = run_on_shutdown; | 867 | task->run_on_shutdown = run_on_shutdown; |
883 | task->next = sched->pending; | 868 | task->next = sched->pending; |
884 | sched->pending = task; | 869 | sched->pending = task; |
885 | return task->id; | 870 | return task->id; |
886 | } | 871 | } |
887 | 872 | ||
873 | /** | ||
874 | * Schedule a new task to be run with a specified delay or when the | ||
875 | * specified file descriptor is ready for reading. The delay can be | ||
876 | * used as a timeout on the socket being ready. The task will be | ||
877 | * scheduled for execution once either the delay has expired or the | ||
878 | * socket operation is ready. | ||
879 | * | ||
880 | * @param sched scheduler to use | ||
881 | * @param run_on_shutdown run on shutdown? Set this | ||
882 | * argument to GNUNET_NO to skip this task if | ||
883 | * the user requested process termination. | ||
884 | * @param prio how important is this task? | ||
885 | * @param prerequisite_task run this task after the task with the given | ||
886 | * task identifier completes (and any of our other | ||
887 | * conditions, such as delay, read or write-readyness | ||
888 | * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency | ||
889 | * on completion of other tasks. | ||
890 | * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever" | ||
891 | * @param rfd read file-descriptor | ||
892 | * @param main main function of the task | ||
893 | * @param cls closure of task | ||
894 | * @return unique task identifier for the job | ||
895 | * only valid until "main" is started! | ||
896 | */ | ||
897 | GNUNET_SCHEDULER_TaskIdentifier | ||
898 | GNUNET_SCHEDULER_add_read_file (struct GNUNET_SCHEDULER_Handle * sched, | ||
899 | int run_on_shutdown, | ||
900 | enum GNUNET_SCHEDULER_Priority prio, | ||
901 | GNUNET_SCHEDULER_TaskIdentifier prerequisite_task, | ||
902 | struct GNUNET_TIME_Relative delay, | ||
903 | struct GNUNET_DISK_FileHandle *rfd, GNUNET_SCHEDULER_Task main, void *cls) | ||
904 | { | ||
905 | struct GNUNET_NETWORK_FDSet *rs; | ||
906 | GNUNET_SCHEDULER_TaskIdentifier ret; | ||
907 | |||
908 | GNUNET_assert (rfd != NULL); | ||
909 | rs = GNUNET_NETWORK_fdset_create (); | ||
910 | GNUNET_NETWORK_fdset_handle_set (rs, rfd); | ||
911 | ret = GNUNET_SCHEDULER_add_select (sched, run_on_shutdown, prio, | ||
912 | prerequisite_task, delay, | ||
913 | rs, NULL, main, cls); | ||
914 | GNUNET_NETWORK_fdset_destroy (rs); | ||
915 | return ret; | ||
916 | } | ||
917 | |||
918 | |||
919 | /** | ||
920 | * Schedule a new task to be run with a specified delay or when the | ||
921 | * specified file descriptor is ready for writing. The delay can be | ||
922 | * used as a timeout on the socket being ready. The task will be | ||
923 | * scheduled for execution once either the delay has expired or the | ||
924 | * socket operation is ready. | ||
925 | * | ||
926 | * @param sched scheduler to use | ||
927 | * @param run_on_shutdown run on shutdown? Set this | ||
928 | * argument to GNUNET_NO to skip this task if | ||
929 | * the user requested process termination. | ||
930 | * @param prio how important is this task? | ||
931 | * @param prerequisite_task run this task after the task with the given | ||
932 | * task identifier completes (and any of our other | ||
933 | * conditions, such as delay, read or write-readyness | ||
934 | * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency | ||
935 | * on completion of other tasks. | ||
936 | * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever" | ||
937 | * @param wfd write file-descriptor | ||
938 | * @param main main function of the task | ||
939 | * @param cls closure of task | ||
940 | * @return unique task identifier for the job | ||
941 | * only valid until "main" is started! | ||
942 | */ | ||
943 | GNUNET_SCHEDULER_TaskIdentifier | ||
944 | GNUNET_SCHEDULER_add_write_file (struct GNUNET_SCHEDULER_Handle * sched, | ||
945 | int run_on_shutdown, | ||
946 | enum GNUNET_SCHEDULER_Priority prio, | ||
947 | GNUNET_SCHEDULER_TaskIdentifier prerequisite_task, | ||
948 | struct GNUNET_TIME_Relative delay, | ||
949 | struct GNUNET_DISK_FileHandle *wfd, GNUNET_SCHEDULER_Task main, void *cls) | ||
950 | { | ||
951 | struct GNUNET_NETWORK_FDSet *ws; | ||
952 | GNUNET_SCHEDULER_TaskIdentifier ret; | ||
953 | |||
954 | GNUNET_assert (wfd != NULL); | ||
955 | ws = GNUNET_NETWORK_fdset_create (); | ||
956 | GNUNET_NETWORK_fdset_handle_set (ws, wfd); | ||
957 | ret = GNUNET_SCHEDULER_add_select (sched, run_on_shutdown, prio, | ||
958 | prerequisite_task, delay, | ||
959 | NULL, ws, main, cls); | ||
960 | GNUNET_NETWORK_fdset_destroy (ws); | ||
961 | return ret; | ||
962 | } | ||
963 | |||
964 | |||
888 | /* end of scheduler.c */ | 965 | /* end of scheduler.c */ |
diff --git a/src/util/server.c b/src/util/server.c index d518cbeec..ff9ea993d 100644 --- a/src/util/server.c +++ b/src/util/server.c | |||
@@ -29,10 +29,11 @@ | |||
29 | 29 | ||
30 | #include "platform.h" | 30 | #include "platform.h" |
31 | #include "gnunet_common.h" | 31 | #include "gnunet_common.h" |
32 | #include "gnunet_network_lib.h" | 32 | #include "gnunet_connection_lib.h" |
33 | #include "gnunet_scheduler_lib.h" | 33 | #include "gnunet_scheduler_lib.h" |
34 | #include "gnunet_server_lib.h" | 34 | #include "gnunet_server_lib.h" |
35 | #include "gnunet_time_lib.h" | 35 | #include "gnunet_time_lib.h" |
36 | #include "gnunet_disk_lib.h" | ||
36 | 37 | ||
37 | #define DEBUG_SERVER GNUNET_NO | 38 | #define DEBUG_SERVER GNUNET_NO |
38 | 39 | ||
@@ -124,13 +125,13 @@ struct GNUNET_SERVER_Handle | |||
124 | /** | 125 | /** |
125 | * Pipe used to signal shutdown of the server. | 126 | * Pipe used to signal shutdown of the server. |
126 | */ | 127 | */ |
127 | int shutpipe[2]; | 128 | struct GNUNET_DISK_PipeHandle *shutpipe; |
128 | 129 | ||
129 | /** | 130 | /** |
130 | * Socket used to listen for new connections. Set to | 131 | * Socket used to listen for new connections. Set to |
131 | * "-1" by GNUNET_SERVER_destroy to initiate shutdown. | 132 | * "-1" by GNUNET_SERVER_destroy to initiate shutdown. |
132 | */ | 133 | */ |
133 | int listen_socket; | 134 | struct GNUNET_NETWORK_Descriptor *listen_socket; |
134 | 135 | ||
135 | /** | 136 | /** |
136 | * Set to GNUNET_YES if we are shutting down. | 137 | * Set to GNUNET_YES if we are shutting down. |
@@ -280,9 +281,8 @@ destroy_server (struct GNUNET_SERVER_Handle *server) | |||
280 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 281 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
281 | "Server shutting down.\n"); | 282 | "Server shutting down.\n"); |
282 | #endif | 283 | #endif |
283 | GNUNET_assert (server->listen_socket == -1); | 284 | GNUNET_assert (server->listen_socket == NULL); |
284 | GNUNET_break (0 == CLOSE (server->shutpipe[0])); | 285 | GNUNET_break (GNUNET_YES == GNUNET_DISK_pipe_close (server->shutpipe)); |
285 | GNUNET_break (0 == CLOSE (server->shutpipe[1])); | ||
286 | while (server->clients != NULL) | 286 | while (server->clients != NULL) |
287 | { | 287 | { |
288 | pos = server->clients; | 288 | pos = server->clients; |
@@ -314,21 +314,23 @@ process_listen_socket (void *cls, | |||
314 | struct GNUNET_SERVER_Handle *server = cls; | 314 | struct GNUNET_SERVER_Handle *server = cls; |
315 | struct GNUNET_NETWORK_ConnectionHandle *sock; | 315 | struct GNUNET_NETWORK_ConnectionHandle *sock; |
316 | struct GNUNET_SERVER_Client *client; | 316 | struct GNUNET_SERVER_Client *client; |
317 | fd_set r; | 317 | struct GNUNET_NETWORK_FDSet *r; |
318 | const struct GNUNET_DISK_FileHandle *shutpipe; | ||
318 | 319 | ||
319 | if ((server->do_shutdown) || | 320 | if ((server->do_shutdown) || |
320 | ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)) | 321 | ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)) |
321 | { | 322 | { |
322 | /* shutdown was initiated */ | 323 | /* shutdown was initiated */ |
323 | GNUNET_assert (server->listen_socket != -1); | 324 | GNUNET_assert (server->listen_socket != NULL); |
324 | GNUNET_break (0 == CLOSE (server->listen_socket)); | 325 | GNUNET_break (0 == GNUNET_NETWORK_socket_close (server->listen_socket)); |
325 | server->listen_socket = -1; | 326 | server->listen_socket = NULL; |
326 | if (server->do_shutdown) | 327 | if (server->do_shutdown) |
327 | destroy_server (server); | 328 | destroy_server (server); |
328 | return; | 329 | return; |
329 | } | 330 | } |
330 | GNUNET_assert (FD_ISSET (server->listen_socket, tc->read_ready)); | 331 | shutpipe = GNUNET_DISK_pipe_handle (server->shutpipe, 0); |
331 | GNUNET_assert (!FD_ISSET (server->shutpipe[0], tc->read_ready)); | 332 | GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, server->listen_socket)); |
333 | GNUNET_assert (!GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, shutpipe)); | ||
332 | sock = GNUNET_NETWORK_connection_create_from_accept (tc->sched, | 334 | sock = GNUNET_NETWORK_connection_create_from_accept (tc->sched, |
333 | server->access, | 335 | server->access, |
334 | server->access_cls, | 336 | server->access_cls, |
@@ -345,30 +347,30 @@ process_listen_socket (void *cls, | |||
345 | GNUNET_SERVER_client_drop (client); | 347 | GNUNET_SERVER_client_drop (client); |
346 | } | 348 | } |
347 | /* listen for more! */ | 349 | /* listen for more! */ |
348 | FD_ZERO (&r); | 350 | r = GNUNET_NETWORK_fdset_create (); |
349 | FD_SET (server->listen_socket, &r); | 351 | GNUNET_NETWORK_fdset_set (r, server->listen_socket); |
350 | FD_SET (server->shutpipe[0], &r); | 352 | GNUNET_NETWORK_fdset_handle_set (r, shutpipe); |
351 | GNUNET_SCHEDULER_add_select (server->sched, | 353 | GNUNET_SCHEDULER_add_select (server->sched, |
352 | GNUNET_YES, | 354 | GNUNET_YES, |
353 | GNUNET_SCHEDULER_PRIORITY_HIGH, | 355 | GNUNET_SCHEDULER_PRIORITY_HIGH, |
354 | GNUNET_SCHEDULER_NO_TASK, | 356 | GNUNET_SCHEDULER_NO_TASK, |
355 | GNUNET_TIME_UNIT_FOREVER_REL, | 357 | GNUNET_TIME_UNIT_FOREVER_REL, |
356 | GNUNET_MAX (server->listen_socket, | 358 | r, NULL, |
357 | server->shutpipe[0]) + 1, &r, NULL, | ||
358 | &process_listen_socket, server); | 359 | &process_listen_socket, server); |
360 | GNUNET_NETWORK_fdset_destroy (r); | ||
359 | } | 361 | } |
360 | 362 | ||
361 | 363 | ||
362 | /** | 364 | /** |
363 | * Create and initialize a listen socket for the server. | 365 | * Create and initialize a listen socket for the server. |
364 | * | 366 | * |
365 | * @return -1 on error, otherwise the listen socket | 367 | * @return NULL on error, otherwise the listen socket |
366 | */ | 368 | */ |
367 | static int | 369 | static struct GNUNET_NETWORK_Descriptor * |
368 | open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen) | 370 | open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen) |
369 | { | 371 | { |
370 | const static int on = 1; | 372 | const static int on = 1; |
371 | int fd; | 373 | struct GNUNET_NETWORK_Descriptor *sock; |
372 | uint16_t port; | 374 | uint16_t port; |
373 | 375 | ||
374 | switch (serverAddr->sa_family) | 376 | switch (serverAddr->sa_family) |
@@ -381,46 +383,45 @@ open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen) | |||
381 | break; | 383 | break; |
382 | default: | 384 | default: |
383 | GNUNET_break (0); | 385 | GNUNET_break (0); |
384 | return -1; | 386 | return NULL; |
385 | } | 387 | } |
386 | fd = SOCKET (serverAddr->sa_family, SOCK_STREAM, 0); | 388 | sock = GNUNET_NETWORK_socket_socket (serverAddr->sa_family, SOCK_STREAM, 0); |
387 | if (fd < 0) | 389 | if (NULL == sock) |
388 | { | 390 | { |
389 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket"); | 391 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket"); |
390 | return -1; | 392 | return NULL; |
391 | } | 393 | } |
392 | #ifndef MINGW | 394 | #ifndef MINGW |
393 | // FIXME NILS | 395 | if (GNUNET_OK != GNUNET_NETWORK_socket_set_inheritable (sock)) |
394 | if (0 != fcntl (fd, F_SETFD, fcntl (fd, F_GETFD) | FD_CLOEXEC)) | ||
395 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 396 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
396 | "fcntl"); | 397 | "fcntl"); |
397 | #endif | 398 | #endif |
398 | if (SETSOCKOPT (fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) < 0) | 399 | if (GNUNET_NETWORK_socket_setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) < 0) |
399 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 400 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
400 | "setsockopt"); | 401 | "setsockopt"); |
401 | /* bind the socket */ | 402 | /* bind the socket */ |
402 | if (BIND (fd, serverAddr, socklen) < 0) | 403 | if (GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen) < 0) |
403 | { | 404 | { |
404 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind"); | 405 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind"); |
405 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 406 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
406 | _ | 407 | _ |
407 | ("`%s' failed for port %d. Is the service already running?\n"), | 408 | ("`%s' failed for port %d. Is the service already running?\n"), |
408 | "bind", port); | 409 | "bind", port); |
409 | GNUNET_break (0 == CLOSE (fd)); | 410 | GNUNET_break (0 == GNUNET_NETWORK_socket_close (sock)); |
410 | return -1; | 411 | return NULL; |
411 | } | 412 | } |
412 | if (0 != LISTEN (fd, 5)) | 413 | if (0 != GNUNET_NETWORK_socket_listen (sock, 5)) |
413 | { | 414 | { |
414 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen"); | 415 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen"); |
415 | GNUNET_break (0 == CLOSE (fd)); | 416 | GNUNET_break (0 == GNUNET_NETWORK_socket_close (sock)); |
416 | return -1; | 417 | return NULL; |
417 | } | 418 | } |
418 | #if DEBUG_SERVER | 419 | #if DEBUG_SERVER |
419 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 420 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
420 | "Server starts to listen on port %u.\n", | 421 | "Server starts to listen on port %u.\n", |
421 | port); | 422 | port); |
422 | #endif | 423 | #endif |
423 | return fd; | 424 | return sock; |
424 | } | 425 | } |
425 | 426 | ||
426 | 427 | ||
@@ -451,20 +452,22 @@ GNUNET_SERVER_create (struct GNUNET_SCHEDULER_Handle *sched, | |||
451 | idle_timeout, int require_found) | 452 | idle_timeout, int require_found) |
452 | { | 453 | { |
453 | struct GNUNET_SERVER_Handle *ret; | 454 | struct GNUNET_SERVER_Handle *ret; |
454 | int lsock; | 455 | struct GNUNET_NETWORK_Descriptor *lsock; |
455 | fd_set r; | 456 | struct GNUNET_NETWORK_FDSet *r; |
456 | 457 | ||
457 | lsock = -2; | 458 | lsock = NULL; // FIXME NILS: this was -2, does that have a special meaning? |
458 | if (serverAddr != NULL) | 459 | if (serverAddr != NULL) |
459 | { | 460 | { |
460 | lsock = open_listen_socket (serverAddr, socklen); | 461 | lsock = open_listen_socket (serverAddr, socklen); |
461 | if (lsock == -1) | 462 | if (lsock == NULL) |
462 | return NULL; | 463 | return NULL; |
463 | } | 464 | } |
464 | ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Handle)); | 465 | ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Handle)); |
465 | if (0 != PIPE (ret->shutpipe)) | 466 | ret->shutpipe = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileDescriptor *[2])); |
467 | if ((ret->shutpipe = GNUNET_DISK_pipe (GNUNET_NO)) == NULL) | ||
466 | { | 468 | { |
467 | GNUNET_break (0 == CLOSE (lsock)); | 469 | GNUNET_break (0 == GNUNET_NETWORK_socket_close (lsock)); |
470 | GNUNET_free (ret->shutpipe); | ||
468 | GNUNET_free (ret); | 471 | GNUNET_free (ret); |
469 | return NULL; | 472 | return NULL; |
470 | } | 473 | } |
@@ -475,19 +478,19 @@ GNUNET_SERVER_create (struct GNUNET_SCHEDULER_Handle *sched, | |||
475 | ret->access = access; | 478 | ret->access = access; |
476 | ret->access_cls = access_cls; | 479 | ret->access_cls = access_cls; |
477 | ret->require_found = require_found; | 480 | ret->require_found = require_found; |
478 | if (lsock >= 0) | 481 | if (lsock != NULL) |
479 | { | 482 | { |
480 | FD_ZERO (&r); | 483 | r = GNUNET_NETWORK_fdset_create (); |
481 | FD_SET (ret->listen_socket, &r); | 484 | GNUNET_NETWORK_fdset_set (r, ret->listen_socket); |
482 | FD_SET (ret->shutpipe[0], &r); | 485 | GNUNET_NETWORK_fdset_handle_set (r, GNUNET_DISK_pipe_handle (ret->shutpipe, 0)); |
483 | GNUNET_SCHEDULER_add_select (sched, | 486 | GNUNET_SCHEDULER_add_select (sched, |
484 | GNUNET_YES, | 487 | GNUNET_YES, |
485 | GNUNET_SCHEDULER_PRIORITY_HIGH, | 488 | GNUNET_SCHEDULER_PRIORITY_HIGH, |
486 | GNUNET_SCHEDULER_NO_TASK, | 489 | GNUNET_SCHEDULER_NO_TASK, |
487 | GNUNET_TIME_UNIT_FOREVER_REL, | 490 | GNUNET_TIME_UNIT_FOREVER_REL, |
488 | GNUNET_MAX (ret->listen_socket, | 491 | r, |
489 | ret->shutpipe[0]) + 1, &r, | ||
490 | NULL, &process_listen_socket, ret); | 492 | NULL, &process_listen_socket, ret); |
493 | GNUNET_NETWORK_fdset_destroy (r); | ||
491 | } | 494 | } |
492 | return ret; | 495 | return ret; |
493 | } | 496 | } |
@@ -503,10 +506,10 @@ GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s) | |||
503 | 506 | ||
504 | GNUNET_assert (s->do_shutdown == GNUNET_NO); | 507 | GNUNET_assert (s->do_shutdown == GNUNET_NO); |
505 | s->do_shutdown = GNUNET_YES; | 508 | s->do_shutdown = GNUNET_YES; |
506 | if (s->listen_socket == -1) | 509 | if (s->listen_socket == NULL) |
507 | destroy_server (s); | 510 | destroy_server (s); |
508 | else | 511 | else |
509 | GNUNET_break (1 == WRITE (s->shutpipe[1], &c, 1)); | 512 | GNUNET_break (1 == GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (s->shutpipe, 1), &c, 1)); |
510 | } | 513 | } |
511 | 514 | ||
512 | 515 | ||
diff --git a/src/util/server_tc.c b/src/util/server_tc.c index dc51e1433..bffb892b1 100644 --- a/src/util/server_tc.c +++ b/src/util/server_tc.c | |||
@@ -27,7 +27,7 @@ | |||
27 | 27 | ||
28 | #include "platform.h" | 28 | #include "platform.h" |
29 | #include "gnunet_common.h" | 29 | #include "gnunet_common.h" |
30 | #include "gnunet_network_lib.h" | 30 | #include "gnunet_connection_lib.h" |
31 | #include "gnunet_scheduler_lib.h" | 31 | #include "gnunet_scheduler_lib.h" |
32 | #include "gnunet_server_lib.h" | 32 | #include "gnunet_server_lib.h" |
33 | #include "gnunet_time_lib.h" | 33 | #include "gnunet_time_lib.h" |
diff --git a/src/util/service.c b/src/util/service.c index a976fab78..782c70fe9 100644 --- a/src/util/service.c +++ b/src/util/service.c | |||
@@ -813,9 +813,11 @@ setup_service (struct GNUNET_SERVICE_Context *sctx) | |||
813 | 813 | ||
814 | if (!disablev6) | 814 | if (!disablev6) |
815 | { | 815 | { |
816 | struct GNUNET_NETWORK_Descriptor *desc; | ||
817 | |||
816 | /* probe IPv6 support */ | 818 | /* probe IPv6 support */ |
817 | ret = SOCKET (PF_INET6, SOCK_STREAM, 0); | 819 | desc = GNUNET_NETWORK_socket_socket (PF_INET6, SOCK_STREAM, 0); |
818 | if (ret == -1) | 820 | if (NULL == desc) |
819 | { | 821 | { |
820 | if ((errno == ENOBUFS) || | 822 | if ((errno == ENOBUFS) || |
821 | (errno == ENOMEM) || (errno == ENFILE) || (errno == EACCES)) | 823 | (errno == ENOMEM) || (errno == ENFILE) || (errno == EACCES)) |
@@ -823,18 +825,18 @@ setup_service (struct GNUNET_SERVICE_Context *sctx) | |||
823 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket"); | 825 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket"); |
824 | return GNUNET_SYSERR; | 826 | return GNUNET_SYSERR; |
825 | } | 827 | } |
826 | ret = SOCKET (PF_INET, SOCK_STREAM, 0); | 828 | desc = GNUNET_NETWORK_socket_socket (PF_INET, SOCK_STREAM, 0); |
827 | if (ret != -1) | 829 | if (NULL == desc) |
828 | { | 830 | { |
829 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 831 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, |
830 | _ | 832 | _ |
831 | ("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"), | 833 | ("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"), |
832 | sctx->serviceName, strerror (errno)); | 834 | sctx->serviceName, STRERROR (errno)); |
833 | disablev6 = GNUNET_YES; | 835 | disablev6 = GNUNET_YES; |
834 | } | 836 | } |
835 | } | 837 | } |
836 | if (ret != -1) | 838 | if (NULL != desc) |
837 | GNUNET_break (0 == CLOSE (ret)); | 839 | GNUNET_break (0 == GNUNET_NETWORK_socket_close (desc)); |
838 | } | 840 | } |
839 | 841 | ||
840 | 842 | ||
diff --git a/src/util/sock.c b/src/util/sock.c new file mode 100644 index 000000000..33b6df148 --- /dev/null +++ b/src/util/sock.c | |||
@@ -0,0 +1,678 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009 Christian Grothoff (and other contributing authors) | ||
4 | |||
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 | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file util/sock.c | ||
23 | * @brief basic, low-level networking interface | ||
24 | * @author Nils Durner | ||
25 | */ | ||
26 | |||
27 | #include "platform.h" | ||
28 | #include "gnunet_disk_lib.h" | ||
29 | #include "disk.h" | ||
30 | #include "gnunet_container_lib.h" | ||
31 | |||
32 | #define DEBUG_SOCK GNUNET_NO | ||
33 | |||
34 | struct GNUNET_NETWORK_Descriptor | ||
35 | { | ||
36 | int fd; | ||
37 | }; | ||
38 | |||
39 | struct GNUNET_NETWORK_FDSet | ||
40 | { | ||
41 | /* socket descriptors */ | ||
42 | int nsds; | ||
43 | fd_set sds; | ||
44 | #ifdef WINDOWS | ||
45 | /* handles */ | ||
46 | struct GNUNET_CONTAINER_Vector *handles; | ||
47 | #endif | ||
48 | }; | ||
49 | |||
50 | #ifndef FD_COPY | ||
51 | #define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set))) | ||
52 | #endif | ||
53 | |||
54 | struct GNUNET_NETWORK_Descriptor * | ||
55 | GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Descriptor *desc, | ||
56 | struct sockaddr *address, | ||
57 | socklen_t * address_len) | ||
58 | { | ||
59 | struct GNUNET_NETWORK_Descriptor *ret; | ||
60 | |||
61 | ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Descriptor)); | ||
62 | ret->fd = accept (desc->fd, address, address_len); | ||
63 | #ifdef MINGW | ||
64 | if (INVALID_SOCKET == ret->fd) | ||
65 | SetErrnoFromWinsockError (WSAGetLastError ()); | ||
66 | #endif | ||
67 | return ret; | ||
68 | } | ||
69 | |||
70 | int | ||
71 | GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Descriptor *desc, | ||
72 | const struct sockaddr *address, | ||
73 | socklen_t address_len) | ||
74 | { | ||
75 | int ret; | ||
76 | |||
77 | ret = bind (desc->fd, address, address_len); | ||
78 | #ifdef MINGW | ||
79 | if (SOCKET_ERROR == ret) | ||
80 | SetErrnoFromWinsockError (WSAGetLastError ()); | ||
81 | #endif | ||
82 | return ret; | ||
83 | } | ||
84 | |||
85 | /** | ||
86 | * Set if a socket should use blocking or non-blocking IO. | ||
87 | * | ||
88 | * @return GNUNET_OK on success, GNUNET_SYSERR on error | ||
89 | */ | ||
90 | int | ||
91 | GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Descriptor *fd, | ||
92 | int doBlock) | ||
93 | { | ||
94 | #if MINGW | ||
95 | u_long mode; | ||
96 | mode = !doBlock; | ||
97 | if (ioctlsocket (fd->fd, FIONBIO, &mode) == SOCKET_ERROR) | ||
98 | { | ||
99 | SetErrnoFromWinsockError (WSAGetLastError ()); | ||
100 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctlsocket"); | ||
101 | return GNUNET_SYSERR; | ||
102 | } | ||
103 | return GNUNET_OK; | ||
104 | |||
105 | #else | ||
106 | /* not MINGW */ | ||
107 | int flags = fcntl (fd->fd, F_GETFL); | ||
108 | if (flags == -1) | ||
109 | { | ||
110 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl"); | ||
111 | return GNUNET_SYSERR; | ||
112 | } | ||
113 | if (doBlock) | ||
114 | flags &= ~O_NONBLOCK; | ||
115 | else | ||
116 | flags |= O_NONBLOCK; | ||
117 | if (0 != fcntl (fd->fd, F_SETFL, flags)) | ||
118 | { | ||
119 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fcntl"); | ||
120 | return GNUNET_SYSERR; | ||
121 | } | ||
122 | return GNUNET_OK; | ||
123 | #endif | ||
124 | } | ||
125 | |||
126 | int | ||
127 | GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Descriptor *desc) | ||
128 | { | ||
129 | int ret; | ||
130 | #ifdef MINGW | ||
131 | ret = closesocket (desc->fd); | ||
132 | if (SOCKET_ERROR != ret) | ||
133 | GNUNET_free (desc); | ||
134 | else | ||
135 | SetErrnoFromWinsockError (WSAGetLastError ()); | ||
136 | #else | ||
137 | ret = close (desc->fd); | ||
138 | if (-1 == ret) | ||
139 | { | ||
140 | GNUNET_free (desc); | ||
141 | } | ||
142 | #endif | ||
143 | |||
144 | return ret; | ||
145 | } | ||
146 | |||
147 | int | ||
148 | GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Descriptor *desc, | ||
149 | const struct sockaddr *address, | ||
150 | socklen_t address_len) | ||
151 | { | ||
152 | int ret; | ||
153 | |||
154 | ret = connect (desc->fd, address, address_len); | ||
155 | #ifdef MINGW | ||
156 | if (SOCKET_ERROR == ret) | ||
157 | SetErrnoFromWinsockError (WSAGetLastError ()); | ||
158 | #endif | ||
159 | return ret; | ||
160 | } | ||
161 | |||
162 | int | ||
163 | GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Descriptor *desc, | ||
164 | int level, int optname, void *optval, | ||
165 | socklen_t * optlen) | ||
166 | { | ||
167 | int ret; | ||
168 | |||
169 | ret = getsockopt (desc->fd, level, optname, optval, optlen); | ||
170 | #ifdef MINGW | ||
171 | if (ret == 0 && level == SOL_SOCKET && optname == SO_ERROR) | ||
172 | *((int *) optval) = GetErrnoFromWinsockError (*((int *) optval)); | ||
173 | else if (SOCKET_ERROR == ret) | ||
174 | SetErrnoFromWinsockError (WSAGetLastError ()); | ||
175 | #endif | ||
176 | return ret; | ||
177 | } | ||
178 | |||
179 | int | ||
180 | GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Descriptor *desc, | ||
181 | int backlog) | ||
182 | { | ||
183 | int ret; | ||
184 | |||
185 | ret = listen (desc->fd, backlog); | ||
186 | #ifdef MINGW | ||
187 | if (SOCKET_ERROR == ret) | ||
188 | SetErrnoFromWinsockError (WSAGetLastError ()); | ||
189 | #endif | ||
190 | |||
191 | return ret; | ||
192 | } | ||
193 | |||
194 | ssize_t | ||
195 | GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Descriptor * desc, | ||
196 | void *buffer, size_t length, int flags) | ||
197 | { | ||
198 | int ret; | ||
199 | |||
200 | ret = recv (desc->fd, buffer, length, flags); | ||
201 | #ifdef MINGW | ||
202 | if (SOCKET_ERROR == ret) | ||
203 | SetErrnoFromWinsockError (WSAGetLastError ()); | ||
204 | #endif | ||
205 | |||
206 | return ret; | ||
207 | } | ||
208 | |||
209 | ssize_t | ||
210 | GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Descriptor * desc, | ||
211 | const void *buffer, size_t length, int flags) | ||
212 | { | ||
213 | int ret; | ||
214 | |||
215 | ret = send (desc->fd, buffer, length, flags); | ||
216 | #ifdef MINGW | ||
217 | if (SOCKET_ERROR == ret) | ||
218 | SetErrnoFromWinsockError (WSAGetLastError ()); | ||
219 | #endif | ||
220 | |||
221 | return ret; | ||
222 | } | ||
223 | |||
224 | ssize_t | ||
225 | GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Descriptor * desc, | ||
226 | const void *message, size_t length, int flags, | ||
227 | const struct sockaddr * dest_addr, | ||
228 | socklen_t dest_len) | ||
229 | { | ||
230 | int ret; | ||
231 | |||
232 | ret = sendto (desc->fd, message, length, flags, dest_addr, dest_len); | ||
233 | #ifdef MINGW | ||
234 | if (SOCKET_ERROR == ret) | ||
235 | SetErrnoFromWinsockError (WSAGetLastError ()); | ||
236 | #endif | ||
237 | |||
238 | return ret; | ||
239 | } | ||
240 | |||
241 | int | ||
242 | GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Descriptor *fd, | ||
243 | int level, int option_name, | ||
244 | const void *option_value, | ||
245 | socklen_t option_len) | ||
246 | { | ||
247 | int ret; | ||
248 | |||
249 | ret = setsockopt (fd->fd, level, option_name, option_value, option_len); | ||
250 | #ifdef MINGW | ||
251 | if (SOCKET_ERROR == ret) | ||
252 | SetErrnoFromWinsockError (WSAGetLastError ()); | ||
253 | #endif | ||
254 | |||
255 | return ret; | ||
256 | } | ||
257 | |||
258 | struct GNUNET_NETWORK_Descriptor * | ||
259 | GNUNET_NETWORK_socket_socket (int domain, int type, int protocol) | ||
260 | { | ||
261 | struct GNUNET_NETWORK_Descriptor *ret; | ||
262 | |||
263 | ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Descriptor)); | ||
264 | ret->fd = socket (domain, type, protocol); | ||
265 | #ifdef MINGW | ||
266 | if (INVALID_SOCKET == ret->fd) | ||
267 | SetErrnoFromWinsockError (WSAGetLastError ()); | ||
268 | #endif | ||
269 | |||
270 | if (ret->fd < 0) | ||
271 | { | ||
272 | GNUNET_free (ret); | ||
273 | ret = NULL; | ||
274 | } | ||
275 | |||
276 | return ret; | ||
277 | } | ||
278 | |||
279 | int | ||
280 | GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Descriptor *desc, | ||
281 | int how) | ||
282 | { | ||
283 | int ret; | ||
284 | |||
285 | ret = shutdown (desc->fd, how); | ||
286 | #ifdef MINGW | ||
287 | if (ret != 0) | ||
288 | SetErrnoFromWinsockError (WSAGetLastError ()); | ||
289 | #endif | ||
290 | |||
291 | return ret; | ||
292 | } | ||
293 | |||
294 | int | ||
295 | GNUNET_NETWORK_socket_set_inheritable (const struct GNUNET_NETWORK_Descriptor | ||
296 | *desc) | ||
297 | { | ||
298 | #ifdef MINGW | ||
299 | errno = ENOSYS; | ||
300 | return GNUNET_SYSERR; | ||
301 | #else | ||
302 | return fcntl (desc->fd, F_SETFD, | ||
303 | fcntl (desc->fd, | ||
304 | F_GETFD) | FD_CLOEXEC) == | ||
305 | 0 ? GNUNET_OK : GNUNET_SYSERR; | ||
306 | #endif | ||
307 | } | ||
308 | |||
309 | void | ||
310 | GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds) | ||
311 | { | ||
312 | FD_ZERO (&fds->sds); | ||
313 | fds->nsds = 0; | ||
314 | #ifdef MINGW | ||
315 | if (fds->handles) | ||
316 | GNUNET_CONTAINER_vector_destroy (fds->handles); | ||
317 | fds->handles = GNUNET_CONTAINER_vector_create (2); | ||
318 | #endif | ||
319 | } | ||
320 | |||
321 | void | ||
322 | GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds, | ||
323 | const struct GNUNET_NETWORK_Descriptor *desc) | ||
324 | { | ||
325 | FD_SET (desc->fd, &fds->sds); | ||
326 | |||
327 | if (desc->fd + 1 > fds->nsds) | ||
328 | fds->nsds = desc->fd + 1; | ||
329 | } | ||
330 | |||
331 | int | ||
332 | GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds, | ||
333 | const struct GNUNET_NETWORK_Descriptor *desc) | ||
334 | { | ||
335 | return FD_ISSET (desc->fd, &fds->sds); | ||
336 | } | ||
337 | |||
338 | void | ||
339 | GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst, | ||
340 | const struct GNUNET_NETWORK_FDSet *src) | ||
341 | { | ||
342 | int nfds; | ||
343 | |||
344 | for (nfds = src->nsds; nfds > 0; nfds--) | ||
345 | if (FD_ISSET (nfds, &src->sds)) | ||
346 | { | ||
347 | FD_SET (nfds, &dst->sds); | ||
348 | if (nfds + 1 > dst->nsds) | ||
349 | dst->nsds = nfds + 1; | ||
350 | } | ||
351 | } | ||
352 | |||
353 | void | ||
354 | GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to, | ||
355 | const struct GNUNET_NETWORK_FDSet *from) | ||
356 | { | ||
357 | FD_COPY (&from->sds, &to->sds); | ||
358 | to->nsds = from->nsds; | ||
359 | #ifdef MINGW | ||
360 | void *obj; | ||
361 | |||
362 | if (to->handles) | ||
363 | GNUNET_CONTAINER_vector_destroy (to->handles); | ||
364 | to->handles = GNUNET_CONTAINER_vector_create (2); | ||
365 | for (obj = GNUNET_CONTAINER_vector_get_first (from->handles); obj != NULL; | ||
366 | obj = GNUNET_CONTAINER_vector_get_next (from->handles)) | ||
367 | { | ||
368 | GNUNET_CONTAINER_vector_insert_last (to->handles, obj); | ||
369 | } | ||
370 | #endif | ||
371 | } | ||
372 | |||
373 | void | ||
374 | GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to, | ||
375 | const fd_set * from, int nfds) | ||
376 | { | ||
377 | FD_COPY (from, &to->sds); | ||
378 | to->nsds = nfds; | ||
379 | } | ||
380 | |||
381 | void | ||
382 | GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds, | ||
383 | const struct GNUNET_DISK_FileHandle *h) | ||
384 | { | ||
385 | #ifdef MINGW | ||
386 | HANDLE hw; | ||
387 | |||
388 | GNUNET_internal_disk_file_handle (h, &hw, sizeof (HANDLE)); | ||
389 | GNUNET_CONTAINER_vector_insert_last (fds->handles, h); | ||
390 | #else | ||
391 | int fd; | ||
392 | |||
393 | GNUNET_internal_disk_file_handle (h, &fd, sizeof (int)); | ||
394 | FD_SET (fd, &fds->sds); | ||
395 | if (fd + 1 > fds->nsds) | ||
396 | fds->nsds = fd + 1; | ||
397 | #endif | ||
398 | } | ||
399 | |||
400 | int | ||
401 | GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds, | ||
402 | const struct GNUNET_DISK_FileHandle *h) | ||
403 | { | ||
404 | #ifdef MINGW | ||
405 | return GNUNET_CONTAINER_vector_index_of (fds->handles, h->h) != | ||
406 | (unsigned int) -1; | ||
407 | #else | ||
408 | return FD_ISSET (h->fd, &fds->sds); | ||
409 | #endif | ||
410 | } | ||
411 | |||
412 | int | ||
413 | GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1, | ||
414 | const struct GNUNET_NETWORK_FDSet *fds2) | ||
415 | { | ||
416 | int nfds; | ||
417 | |||
418 | nfds = fds1->nsds; | ||
419 | if (nfds < fds2->nsds) | ||
420 | nfds = fds2->nsds; | ||
421 | |||
422 | for (; nfds >= 0; nfds--) | ||
423 | if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds)) | ||
424 | return GNUNET_YES; | ||
425 | |||
426 | return GNUNET_NO; | ||
427 | } | ||
428 | |||
429 | struct GNUNET_NETWORK_FDSet * | ||
430 | GNUNET_NETWORK_fdset_create () | ||
431 | { | ||
432 | struct GNUNET_NETWORK_FDSet *fds; | ||
433 | |||
434 | fds = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_FDSet)); | ||
435 | #ifdef MINGW | ||
436 | fds->handles = NULL; | ||
437 | #endif | ||
438 | GNUNET_NETWORK_fdset_zero (fds); | ||
439 | |||
440 | return fds; | ||
441 | } | ||
442 | |||
443 | void | ||
444 | GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds) | ||
445 | { | ||
446 | #ifdef MINGW | ||
447 | GNUNET_CONTAINER_vector_destroy (fds->handles); | ||
448 | #endif | ||
449 | GNUNET_free (fds); | ||
450 | } | ||
451 | |||
452 | int | ||
453 | GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds, | ||
454 | struct GNUNET_NETWORK_FDSet *wfds, | ||
455 | struct GNUNET_NETWORK_FDSet *efds, | ||
456 | const struct GNUNET_TIME_Relative timeout) | ||
457 | { | ||
458 | int nfds; | ||
459 | |||
460 | nfds = 0; | ||
461 | |||
462 | if (rfds) | ||
463 | nfds = rfds->nsds; | ||
464 | if (wfds && wfds->nsds > nfds) | ||
465 | nfds = wfds->nsds; | ||
466 | if (efds && efds->nsds > nfds) | ||
467 | nfds = efds->nsds; | ||
468 | |||
469 | #ifndef MINGW | ||
470 | struct timeval tv; | ||
471 | |||
472 | tv.tv_sec = timeout.value / GNUNET_TIME_UNIT_SECONDS.value; | ||
473 | tv.tv_usec = (timeout.value - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.value)) | ||
474 | / GNUNET_TIME_UNIT_MILLISECONDS.value; | ||
475 | |||
476 | return select (nfds + 1, rfds ? &rfds->sds : NULL, wfds ? &wfds->sds : NULL, | ||
477 | efds ? &efds->sds : NULL, timeout.value | ||
478 | == GNUNET_TIME_UNIT_FOREVER_REL.value ? NULL : &tv); | ||
479 | #else | ||
480 | DWORD limit; | ||
481 | fd_set sock_read, sock_write, sock_except; | ||
482 | fd_set aread, awrite, aexcept; | ||
483 | int i; | ||
484 | struct timeval tvslice; | ||
485 | int retcode; | ||
486 | DWORD ms_total; | ||
487 | |||
488 | #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set)) | ||
489 | |||
490 | /* calculate how long we need to wait in milliseconds */ | ||
491 | if (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value) | ||
492 | ms_total = INFINITE; | ||
493 | else | ||
494 | ms_total = timeout.value / GNUNET_TIME_UNIT_MILLISECONDS.value; | ||
495 | |||
496 | /* select() may be used as a portable way to sleep */ | ||
497 | if (!(rfds || wfds || efds)) | ||
498 | { | ||
499 | Sleep (ms_total); | ||
500 | |||
501 | return 0; | ||
502 | } | ||
503 | |||
504 | if (rfds) | ||
505 | sock_read = rfds->sds; | ||
506 | else | ||
507 | FD_ZERO(&sock_read); | ||
508 | |||
509 | if (wfds) | ||
510 | sock_write = wfds->sds; | ||
511 | else | ||
512 | FD_ZERO(&sock_write); | ||
513 | |||
514 | if (efds) | ||
515 | sock_except = efds->sds; | ||
516 | else | ||
517 | FD_ZERO(&sock_except); | ||
518 | |||
519 | /* | ||
520 | if (rfds) | ||
521 | FD_COPY (&rfds->sds, &sock_read); | ||
522 | else | ||
523 | FD_ZERO(&sock_read); | ||
524 | |||
525 | if (wfds) | ||
526 | FD_COPY (&wfds->sds, &sock_write); | ||
527 | else | ||
528 | FD_ZERO(&sock_write); | ||
529 | |||
530 | if (efds) | ||
531 | FD_COPY (&efds->sds, &sock_except); | ||
532 | else | ||
533 | FD_ZERO(&sock_except); | ||
534 | */ | ||
535 | |||
536 | /* multiplex between winsock select() and waiting on the handles */ | ||
537 | |||
538 | FD_ZERO (&aread); | ||
539 | FD_ZERO (&awrite); | ||
540 | FD_ZERO (&aexcept); | ||
541 | |||
542 | limit = GetTickCount () + ms_total; | ||
543 | do | ||
544 | { | ||
545 | retcode = 0; | ||
546 | |||
547 | if (nfds > 0) | ||
548 | { | ||
549 | /* overwrite the zero'd sets here; the select call | ||
550 | * will clear those that are not active */ | ||
551 | |||
552 | FD_COPY (&sock_read, &aread); | ||
553 | FD_COPY (&sock_write, &awrite); | ||
554 | FD_COPY (&sock_except, &aexcept); | ||
555 | |||
556 | tvslice.tv_sec = 0; | ||
557 | tvslice.tv_usec = 100000; | ||
558 | |||
559 | if ((retcode = | ||
560 | select (nfds + 1, &aread, &awrite, &aexcept, | ||
561 | &tvslice)) == SOCKET_ERROR) | ||
562 | { | ||
563 | SetErrnoFromWinsockError (WSAGetLastError ()); | ||
564 | if (errno == ENOTSOCK) | ||
565 | errno = EBADF; | ||
566 | |||
567 | #if DEBUG_SOCK | ||
568 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select"); | ||
569 | #endif | ||
570 | |||
571 | goto select_loop_end; | ||
572 | } | ||
573 | } | ||
574 | |||
575 | /* Poll read pipes */ | ||
576 | if (rfds) | ||
577 | for (i = GNUNET_CONTAINER_vector_size (rfds->handles) - 1; i >= 0; i--) | ||
578 | { | ||
579 | DWORD dwBytes; | ||
580 | |||
581 | if (!PeekNamedPipe | ||
582 | (GNUNET_CONTAINER_vector_get_at (rfds->handles, i), NULL, 0, | ||
583 | NULL, &dwBytes, NULL)) | ||
584 | { | ||
585 | GNUNET_CONTAINER_vector_remove_at (rfds->handles, i); | ||
586 | |||
587 | retcode = -1; | ||
588 | SetErrnoFromWinError (GetLastError ()); | ||
589 | #if DEBUG_SOCK | ||
590 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "PeekNamedPipe"); | ||
591 | #endif | ||
592 | goto select_loop_end; | ||
593 | } | ||
594 | else if (dwBytes) | ||
595 | { | ||
596 | retcode++; | ||
597 | } | ||
598 | else | ||
599 | GNUNET_CONTAINER_vector_remove_at (rfds->handles, i); | ||
600 | } | ||
601 | |||
602 | /* Poll for faulty pipes */ | ||
603 | if (efds) | ||
604 | for (i = GNUNET_CONTAINER_vector_size (efds->handles); i >= 0; i--) | ||
605 | { | ||
606 | DWORD dwBytes; | ||
607 | |||
608 | if (PeekNamedPipe | ||
609 | (GNUNET_CONTAINER_vector_get_at (rfds->handles, i), NULL, 0, | ||
610 | NULL, &dwBytes, NULL)) | ||
611 | { | ||
612 | GNUNET_CONTAINER_vector_remove_at (efds->handles, i); | ||
613 | |||
614 | retcode++; | ||
615 | } | ||
616 | } | ||
617 | |||
618 | /* FIXME */ | ||
619 | if (wfds) | ||
620 | GNUNET_assert (GNUNET_CONTAINER_vector_size (wfds->handles) == 0); | ||
621 | |||
622 | /* Check for closed sockets */ | ||
623 | for (i = 0; i < nfds; i++) | ||
624 | { | ||
625 | if (SAFE_FD_ISSET (i, &sock_read)) | ||
626 | { | ||
627 | struct sockaddr addr; | ||
628 | int len; | ||
629 | |||
630 | if (getpeername (i, &addr, &len) == SOCKET_ERROR) | ||
631 | { | ||
632 | int err, len; | ||
633 | |||
634 | len = sizeof (err); | ||
635 | if (getsockopt | ||
636 | (i, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == 0 | ||
637 | && err == WSAENOTCONN) | ||
638 | { | ||
639 | if (!SAFE_FD_ISSET (i, &aread)) | ||
640 | { | ||
641 | FD_SET (i, &aread); | ||
642 | retcode++; | ||
643 | } | ||
644 | } | ||
645 | } | ||
646 | } | ||
647 | } | ||
648 | |||
649 | select_loop_end:; | ||
650 | } | ||
651 | while (retcode == 0 && (ms_total == INFINITE || GetTickCount () < limit)); | ||
652 | |||
653 | if (retcode != -1) | ||
654 | { | ||
655 | if (rfds) | ||
656 | { | ||
657 | GNUNET_NETWORK_fdset_zero (rfds); | ||
658 | GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode); | ||
659 | } | ||
660 | |||
661 | if (wfds) | ||
662 | { | ||
663 | GNUNET_NETWORK_fdset_zero (wfds); | ||
664 | GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode); | ||
665 | } | ||
666 | |||
667 | if (efds) | ||
668 | { | ||
669 | GNUNET_NETWORK_fdset_zero (efds); | ||
670 | GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode); | ||
671 | } | ||
672 | } | ||
673 | |||
674 | return retcode; | ||
675 | #endif | ||
676 | } | ||
677 | |||
678 | /* end of io.c */ | ||
diff --git a/src/util/test_network.c b/src/util/test_network.c index cb0656921..6f09c4fce 100644 --- a/src/util/test_network.c +++ b/src/util/test_network.c | |||
@@ -23,7 +23,7 @@ | |||
23 | */ | 23 | */ |
24 | #include "platform.h" | 24 | #include "platform.h" |
25 | #include "gnunet_common.h" | 25 | #include "gnunet_common.h" |
26 | #include "gnunet_network_lib.h" | 26 | #include "gnunet_connection_lib.h" |
27 | #include "gnunet_scheduler_lib.h" | 27 | #include "gnunet_scheduler_lib.h" |
28 | #include "gnunet_time_lib.h" | 28 | #include "gnunet_time_lib.h" |
29 | 29 | ||
@@ -49,24 +49,24 @@ static int ls; | |||
49 | * | 49 | * |
50 | * @return -1 on error, otherwise the listen socket | 50 | * @return -1 on error, otherwise the listen socket |
51 | */ | 51 | */ |
52 | static int | 52 | static struct GNUNET_NETWORK_Descriptor * |
53 | open_listen_socket () | 53 | open_listen_socket () |
54 | { | 54 | { |
55 | const static int on = 1; | 55 | const static int on = 1; |
56 | struct sockaddr_in sa; | 56 | struct sockaddr_in sa; |
57 | int fd; | 57 | struct GNUNET_NETWORK_Descriptor *desc; |
58 | 58 | ||
59 | memset (&sa, 0, sizeof (sa)); | 59 | memset (&sa, 0, sizeof (sa)); |
60 | sa.sin_port = htons (PORT); | 60 | sa.sin_port = htons (PORT); |
61 | sa.sin_family = AF_INET; | 61 | sa.sin_family = AF_INET; |
62 | fd = SOCKET (AF_INET, SOCK_STREAM, 0); | 62 | desc = GNUNET_NETWORK_socket_socket (AF_INET, SOCK_STREAM, 0); |
63 | GNUNET_assert (fd >= 0); | 63 | GNUNET_assert (desc != NULL); |
64 | if (SETSOCKOPT (fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) < 0) | 64 | if (GNUNET_NETWORK_socket_setsockopt (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) < 0) |
65 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 65 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
66 | "setsockopt"); | 66 | "setsockopt"); |
67 | GNUNET_assert (BIND (fd, &sa, sizeof (sa)) >= 0); | 67 | GNUNET_assert (GNUNET_NETWORK_socket_bind (desc, &sa, sizeof (sa)) >= 0); |
68 | LISTEN (fd, 5); | 68 | GNUNET_NETWORK_socket_listen (desc, 5); |
69 | return fd; | 69 | return desc; |
70 | } | 70 | } |
71 | 71 | ||
72 | static void | 72 | static void |
@@ -166,7 +166,7 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
166 | #if VERBOSE | 166 | #if VERBOSE |
167 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test prepares to accept\n"); | 167 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test prepares to accept\n"); |
168 | #endif | 168 | #endif |
169 | GNUNET_SCHEDULER_add_read (tc->sched, | 169 | GNUNET_SCHEDULER_add_read_net (tc->sched, |
170 | GNUNET_NO, | 170 | GNUNET_NO, |
171 | GNUNET_SCHEDULER_PRIORITY_HIGH, | 171 | GNUNET_SCHEDULER_PRIORITY_HIGH, |
172 | GNUNET_SCHEDULER_NO_TASK, | 172 | GNUNET_SCHEDULER_NO_TASK, |
diff --git a/src/util/test_network_addressing.c b/src/util/test_network_addressing.c index aeb187634..a2682a733 100644 --- a/src/util/test_network_addressing.c +++ b/src/util/test_network_addressing.c | |||
@@ -23,7 +23,7 @@ | |||
23 | */ | 23 | */ |
24 | #include "platform.h" | 24 | #include "platform.h" |
25 | #include "gnunet_common.h" | 25 | #include "gnunet_common.h" |
26 | #include "gnunet_network_lib.h" | 26 | #include "gnunet_connection_lib.h" |
27 | #include "gnunet_scheduler_lib.h" | 27 | #include "gnunet_scheduler_lib.h" |
28 | #include "gnunet_time_lib.h" | 28 | #include "gnunet_time_lib.h" |
29 | 29 | ||
@@ -40,32 +40,32 @@ static struct GNUNET_NETWORK_ConnectionHandle *lsock; | |||
40 | 40 | ||
41 | static size_t sofar; | 41 | static size_t sofar; |
42 | 42 | ||
43 | static int ls; | 43 | static struct GNUNET_NETWORK_Descriptor *ls; |
44 | 44 | ||
45 | 45 | ||
46 | 46 | ||
47 | /** | 47 | /** |
48 | * Create and initialize a listen socket for the server. | 48 | * Create and initialize a listen socket for the server. |
49 | * | 49 | * |
50 | * @return -1 on error, otherwise the listen socket | 50 | * @return NULL on error, otherwise the listen socket |
51 | */ | 51 | */ |
52 | static int | 52 | static struct GNUNET_NETWORK_Descriptor * |
53 | open_listen_socket () | 53 | open_listen_socket () |
54 | { | 54 | { |
55 | const static int on = 1; | 55 | const static int on = 1; |
56 | struct sockaddr_in sa; | 56 | struct sockaddr_in sa; |
57 | int fd; | 57 | struct GNUNET_NETWORK_Descriptor *desc; |
58 | 58 | ||
59 | memset (&sa, 0, sizeof (sa)); | 59 | memset (&sa, 0, sizeof (sa)); |
60 | sa.sin_port = htons (PORT); | 60 | sa.sin_port = htons (PORT); |
61 | fd = SOCKET (AF_INET, SOCK_STREAM, 0); | 61 | desc = GNUNET_NETWORK_socket_socket (AF_INET, SOCK_STREAM, 0); |
62 | GNUNET_assert (fd >= 0); | 62 | GNUNET_assert (desc != 0); |
63 | if (SETSOCKOPT (fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) < 0) | 63 | if (GNUNET_NETWORK_socket_setsockopt (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) < 0) |
64 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 64 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
65 | "setsockopt"); | 65 | "setsockopt"); |
66 | GNUNET_assert (BIND (fd, &sa, sizeof (sa)) >= 0); | 66 | GNUNET_assert (GNUNET_NETWORK_socket_bind (desc, &sa, sizeof (sa)) >= 0); |
67 | LISTEN (fd, 5); | 67 | GNUNET_NETWORK_socket_listen (desc, 5); |
68 | return fd; | 68 | return desc; |
69 | } | 69 | } |
70 | 70 | ||
71 | 71 | ||
@@ -156,7 +156,7 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
156 | GNUNET_TIME_UNIT_SECONDS, | 156 | GNUNET_TIME_UNIT_SECONDS, |
157 | &make_hello, NULL)); | 157 | &make_hello, NULL)); |
158 | GNUNET_NETWORK_connection_destroy (csock); | 158 | GNUNET_NETWORK_connection_destroy (csock); |
159 | GNUNET_SCHEDULER_add_read (tc->sched, | 159 | GNUNET_SCHEDULER_add_read_net (tc->sched, |
160 | GNUNET_NO, | 160 | GNUNET_NO, |
161 | GNUNET_SCHEDULER_PRIORITY_HIGH, | 161 | GNUNET_SCHEDULER_PRIORITY_HIGH, |
162 | GNUNET_SCHEDULER_NO_TASK, | 162 | GNUNET_SCHEDULER_NO_TASK, |
diff --git a/src/util/test_network_receive_cancel.c b/src/util/test_network_receive_cancel.c index 51d8350af..1a3cb4866 100644 --- a/src/util/test_network_receive_cancel.c +++ b/src/util/test_network_receive_cancel.c | |||
@@ -23,7 +23,7 @@ | |||
23 | */ | 23 | */ |
24 | #include "platform.h" | 24 | #include "platform.h" |
25 | #include "gnunet_common.h" | 25 | #include "gnunet_common.h" |
26 | #include "gnunet_network_lib.h" | 26 | #include "gnunet_connection_lib.h" |
27 | #include "gnunet_scheduler_lib.h" | 27 | #include "gnunet_scheduler_lib.h" |
28 | #include "gnunet_time_lib.h" | 28 | #include "gnunet_time_lib.h" |
29 | 29 | ||
@@ -38,7 +38,7 @@ static struct GNUNET_NETWORK_ConnectionHandle *asock; | |||
38 | 38 | ||
39 | static struct GNUNET_NETWORK_ConnectionHandle *lsock; | 39 | static struct GNUNET_NETWORK_ConnectionHandle *lsock; |
40 | 40 | ||
41 | static int ls; | 41 | static struct GNUNET_NETWORK_Descriptor *ls; |
42 | 42 | ||
43 | static GNUNET_SCHEDULER_TaskIdentifier receive_task; | 43 | static GNUNET_SCHEDULER_TaskIdentifier receive_task; |
44 | 44 | ||
@@ -48,25 +48,25 @@ static GNUNET_SCHEDULER_TaskIdentifier receive_task; | |||
48 | /** | 48 | /** |
49 | * Create and initialize a listen socket for the server. | 49 | * Create and initialize a listen socket for the server. |
50 | * | 50 | * |
51 | * @return -1 on error, otherwise the listen socket | 51 | * @return NULL on error, otherwise the listen socket |
52 | */ | 52 | */ |
53 | static int | 53 | static struct GNUNET_NETWORK_Descriptor * |
54 | open_listen_socket () | 54 | open_listen_socket () |
55 | { | 55 | { |
56 | const static int on = 1; | 56 | const static int on = 1; |
57 | struct sockaddr_in sa; | 57 | struct sockaddr_in sa; |
58 | int fd; | 58 | struct GNUNET_NETWORK_Descriptor *desc; |
59 | 59 | ||
60 | memset (&sa, 0, sizeof (sa)); | 60 | memset (&sa, 0, sizeof (sa)); |
61 | sa.sin_port = htons (PORT); | 61 | sa.sin_port = htons (PORT); |
62 | fd = SOCKET (AF_INET, SOCK_STREAM, 0); | 62 | desc = GNUNET_NETWORK_socket_socket (AF_INET, SOCK_STREAM, 0); |
63 | GNUNET_assert (fd >= 0); | 63 | GNUNET_assert (desc != NULL); |
64 | if (SETSOCKOPT (fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) < 0) | 64 | if (GNUNET_NETWORK_socket_setsockopt (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) < 0) |
65 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 65 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
66 | "setsockopt"); | 66 | "setsockopt"); |
67 | GNUNET_assert (BIND (fd, &sa, sizeof (sa)) >= 0); | 67 | GNUNET_assert (GNUNET_NETWORK_socket_bind (desc, &sa, sizeof (sa)) >= 0); |
68 | LISTEN (fd, 5); | 68 | GNUNET_NETWORK_socket_listen (desc, 5); |
69 | return fd; | 69 | return desc; |
70 | } | 70 | } |
71 | 71 | ||
72 | 72 | ||
@@ -120,7 +120,7 @@ task_receive_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
120 | csock = GNUNET_NETWORK_connection_create_from_connect (tc->sched, | 120 | csock = GNUNET_NETWORK_connection_create_from_connect (tc->sched, |
121 | "localhost", PORT, 1024); | 121 | "localhost", PORT, 1024); |
122 | GNUNET_assert (csock != NULL); | 122 | GNUNET_assert (csock != NULL); |
123 | GNUNET_SCHEDULER_add_read (tc->sched, | 123 | GNUNET_SCHEDULER_add_read_net (tc->sched, |
124 | GNUNET_NO, | 124 | GNUNET_NO, |
125 | GNUNET_SCHEDULER_PRIORITY_HIGH, | 125 | GNUNET_SCHEDULER_PRIORITY_HIGH, |
126 | GNUNET_SCHEDULER_NO_TASK, | 126 | GNUNET_SCHEDULER_NO_TASK, |
diff --git a/src/util/test_network_timeout.c b/src/util/test_network_timeout.c index 1d8910842..442775463 100644 --- a/src/util/test_network_timeout.c +++ b/src/util/test_network_timeout.c | |||
@@ -23,7 +23,7 @@ | |||
23 | */ | 23 | */ |
24 | #include "platform.h" | 24 | #include "platform.h" |
25 | #include "gnunet_common.h" | 25 | #include "gnunet_common.h" |
26 | #include "gnunet_network_lib.h" | 26 | #include "gnunet_connection_lib.h" |
27 | #include "gnunet_scheduler_lib.h" | 27 | #include "gnunet_scheduler_lib.h" |
28 | #include "gnunet_time_lib.h" | 28 | #include "gnunet_time_lib.h" |
29 | 29 | ||
@@ -35,31 +35,31 @@ static struct GNUNET_NETWORK_ConnectionHandle *csock; | |||
35 | 35 | ||
36 | static struct GNUNET_NETWORK_ConnectionHandle *lsock; | 36 | static struct GNUNET_NETWORK_ConnectionHandle *lsock; |
37 | 37 | ||
38 | static int ls; | 38 | static struct GNUNET_NETWORK_Descriptor *ls; |
39 | 39 | ||
40 | 40 | ||
41 | /** | 41 | /** |
42 | * Create and initialize a listen socket for the server. | 42 | * Create and initialize a listen socket for the server. |
43 | * | 43 | * |
44 | * @return -1 on error, otherwise the listen socket | 44 | * @return NULL on error, otherwise the listen socket |
45 | */ | 45 | */ |
46 | static int | 46 | static struct GNUNET_NETWORK_Descriptor * |
47 | open_listen_socket () | 47 | open_listen_socket () |
48 | { | 48 | { |
49 | const static int on = 1; | 49 | const static int on = 1; |
50 | struct sockaddr_in sa; | 50 | struct sockaddr_in sa; |
51 | int fd; | 51 | struct GNUNET_NETWORK_Descriptor *desc; |
52 | 52 | ||
53 | memset (&sa, 0, sizeof (sa)); | 53 | memset (&sa, 0, sizeof (sa)); |
54 | sa.sin_port = htons (PORT); | 54 | sa.sin_port = htons (PORT); |
55 | fd = SOCKET (AF_INET, SOCK_STREAM, 0); | 55 | desc = GNUNET_NETWORK_socket_socket (AF_INET, SOCK_STREAM, 0); |
56 | GNUNET_assert (fd >= 0); | 56 | GNUNET_assert (desc != NULL); |
57 | if (SETSOCKOPT (fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) < 0) | 57 | if (GNUNET_NETWORK_socket_setsockopt (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) < 0) |
58 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 58 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
59 | "setsockopt"); | 59 | "setsockopt"); |
60 | GNUNET_assert (BIND (fd, &sa, sizeof (sa)) >= 0); | 60 | GNUNET_assert (GNUNET_NETWORK_socket_bind (desc, &sa, sizeof (sa)) >= 0); |
61 | LISTEN (fd, 5); | 61 | GNUNET_NETWORK_socket_listen (desc, 5); |
62 | return fd; | 62 | return desc; |
63 | } | 63 | } |
64 | 64 | ||
65 | 65 | ||
diff --git a/src/util/test_network_timeout_no_connect.c b/src/util/test_network_timeout_no_connect.c index 172a6991d..902edd2fa 100644 --- a/src/util/test_network_timeout_no_connect.c +++ b/src/util/test_network_timeout_no_connect.c | |||
@@ -23,7 +23,7 @@ | |||
23 | */ | 23 | */ |
24 | #include "platform.h" | 24 | #include "platform.h" |
25 | #include "gnunet_common.h" | 25 | #include "gnunet_common.h" |
26 | #include "gnunet_network_lib.h" | 26 | #include "gnunet_connection_lib.h" |
27 | #include "gnunet_scheduler_lib.h" | 27 | #include "gnunet_scheduler_lib.h" |
28 | #include "gnunet_time_lib.h" | 28 | #include "gnunet_time_lib.h" |
29 | 29 | ||
diff --git a/src/util/test_network_transmit_cancel.c b/src/util/test_network_transmit_cancel.c index 7a67000a8..c46fecd22 100644 --- a/src/util/test_network_transmit_cancel.c +++ b/src/util/test_network_transmit_cancel.c | |||
@@ -23,7 +23,7 @@ | |||
23 | */ | 23 | */ |
24 | #include "platform.h" | 24 | #include "platform.h" |
25 | #include "gnunet_common.h" | 25 | #include "gnunet_common.h" |
26 | #include "gnunet_network_lib.h" | 26 | #include "gnunet_connection_lib.h" |
27 | #include "gnunet_scheduler_lib.h" | 27 | #include "gnunet_scheduler_lib.h" |
28 | #include "gnunet_time_lib.h" | 28 | #include "gnunet_time_lib.h" |
29 | 29 | ||
diff --git a/src/util/test_scheduler.c b/src/util/test_scheduler.c index d5a93ed7d..34def7b1e 100644 --- a/src/util/test_scheduler.c +++ b/src/util/test_scheduler.c | |||
@@ -25,6 +25,7 @@ | |||
25 | #include "gnunet_common.h" | 25 | #include "gnunet_common.h" |
26 | #include "gnunet_scheduler_lib.h" | 26 | #include "gnunet_scheduler_lib.h" |
27 | #include "gnunet_time_lib.h" | 27 | #include "gnunet_time_lib.h" |
28 | #include "gnunet_disk_lib.h" | ||
28 | 29 | ||
29 | #define VERBOSE GNUNET_NO | 30 | #define VERBOSE GNUNET_NO |
30 | 31 | ||
@@ -55,7 +56,8 @@ task4 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
55 | (*ok) = 5; | 56 | (*ok) = 5; |
56 | } | 57 | } |
57 | 58 | ||
58 | static int fds[2]; | 59 | struct GNUNET_DISK_PipeHandle *p; |
60 | static struct GNUNET_DISK_FileHandle *fds[2]; | ||
59 | 61 | ||
60 | 62 | ||
61 | static void | 63 | static void |
@@ -64,10 +66,9 @@ taskWrt (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
64 | static char c; | 66 | static char c; |
65 | int *ok = cls; | 67 | int *ok = cls; |
66 | GNUNET_assert (6 == *ok); | 68 | GNUNET_assert (6 == *ok); |
67 | GNUNET_assert (FD_ISSET (fds[1], tc->write_ready)); | 69 | GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->write_ready, fds[1])); |
68 | (*ok) = 7; | 70 | (*ok) = 7; |
69 | GNUNET_assert (1 == WRITE (fds[1], &c, 1)); | 71 | GNUNET_assert (1 == GNUNET_DISK_file_write (fds[1], &c, 1)); |
70 | GNUNET_break (0 == CLOSE (fds[1])); | ||
71 | } | 72 | } |
72 | 73 | ||
73 | 74 | ||
@@ -92,9 +93,8 @@ taskRd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
92 | static char c; | 93 | static char c; |
93 | int *ok = cls; | 94 | int *ok = cls; |
94 | GNUNET_assert (7 == *ok); | 95 | GNUNET_assert (7 == *ok); |
95 | GNUNET_assert (FD_ISSET (fds[0], tc->read_ready)); | 96 | GNUNET_assert (GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, fds[0])); |
96 | GNUNET_assert (1 == READ (fds[0], &c, 1)); | 97 | GNUNET_assert (1 == GNUNET_DISK_file_read (fds[0], &c, 1)); |
97 | GNUNET_break (0 == CLOSE (fds[0])); | ||
98 | (*ok) = 8; | 98 | (*ok) = 8; |
99 | GNUNET_SCHEDULER_add_after (tc->sched, | 99 | GNUNET_SCHEDULER_add_after (tc->sched, |
100 | GNUNET_NO, | 100 | GNUNET_NO, |
@@ -114,14 +114,17 @@ task5 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
114 | int *ok = cls; | 114 | int *ok = cls; |
115 | GNUNET_assert (5 == *ok); | 115 | GNUNET_assert (5 == *ok); |
116 | (*ok) = 6; | 116 | (*ok) = 6; |
117 | GNUNET_assert (0 == PIPE (fds)); | 117 | p = GNUNET_DISK_pipe (GNUNET_NO); |
118 | GNUNET_SCHEDULER_add_read (tc->sched, | 118 | GNUNET_assert (NULL != p); |
119 | fds[0] = GNUNET_DISK_pipe_handle (p, 0); | ||
120 | fds[1] = GNUNET_DISK_pipe_handle (p, 1); | ||
121 | GNUNET_SCHEDULER_add_read_file (tc->sched, | ||
119 | GNUNET_NO, | 122 | GNUNET_NO, |
120 | GNUNET_SCHEDULER_PRIORITY_DEFAULT, | 123 | GNUNET_SCHEDULER_PRIORITY_DEFAULT, |
121 | GNUNET_SCHEDULER_NO_TASK, | 124 | GNUNET_SCHEDULER_NO_TASK, |
122 | GNUNET_TIME_UNIT_FOREVER_REL, | 125 | GNUNET_TIME_UNIT_FOREVER_REL, |
123 | fds[0], &taskRd, cls); | 126 | fds[0], &taskRd, cls); |
124 | GNUNET_SCHEDULER_add_write (tc->sched, | 127 | GNUNET_SCHEDULER_add_write_file (tc->sched, |
125 | GNUNET_NO, | 128 | GNUNET_NO, |
126 | GNUNET_SCHEDULER_PRIORITY_DEFAULT, | 129 | GNUNET_SCHEDULER_PRIORITY_DEFAULT, |
127 | GNUNET_SCHEDULER_NO_TASK, | 130 | GNUNET_SCHEDULER_NO_TASK, |
@@ -256,6 +259,7 @@ main (int argc, char *argv[]) | |||
256 | ret += check (); | 259 | ret += check (); |
257 | ret += checkSignal (); | 260 | ret += checkSignal (); |
258 | ret += checkCancel (); | 261 | ret += checkCancel (); |
262 | GNUNET_DISK_pipe_close (p); | ||
259 | 263 | ||
260 | return ret; | 264 | return ret; |
261 | } | 265 | } |
diff --git a/src/util/test_service.c b/src/util/test_service.c index e8e3f8741..8e351deb3 100644 --- a/src/util/test_service.c +++ b/src/util/test_service.c | |||
@@ -300,7 +300,7 @@ int | |||
300 | main (int argc, char *argv[]) | 300 | main (int argc, char *argv[]) |
301 | { | 301 | { |
302 | int ret = 0; | 302 | int ret = 0; |
303 | int s; | 303 | struct GNUNET_NETWORK_Descriptor *s; |
304 | 304 | ||
305 | GNUNET_log_setup ("test-service", | 305 | GNUNET_log_setup ("test-service", |
306 | #if VERBOSE | 306 | #if VERBOSE |
@@ -311,8 +311,8 @@ main (int argc, char *argv[]) | |||
311 | NULL); | 311 | NULL); |
312 | ret += check (); | 312 | ret += check (); |
313 | ret += check (); | 313 | ret += check (); |
314 | s = SOCKET (PF_INET6, SOCK_STREAM, 0); | 314 | s = GNUNET_NETWORK_socket_socket (PF_INET6, SOCK_STREAM, 0); |
315 | if (s == -1) | 315 | if (NULL == s) |
316 | { | 316 | { |
317 | if ((errno == ENOBUFS) || | 317 | if ((errno == ENOBUFS) || |
318 | (errno == ENOMEM) || (errno == ENFILE) || (errno == EACCES)) | 318 | (errno == ENOMEM) || (errno == ENFILE) || (errno == EACCES)) |
@@ -326,7 +326,7 @@ main (int argc, char *argv[]) | |||
326 | } | 326 | } |
327 | else | 327 | else |
328 | { | 328 | { |
329 | GNUNET_break (0 == CLOSE (s)); | 329 | GNUNET_break (0 == GNUNET_NETWORK_socket_close (s)); |
330 | ret += check6 (); | 330 | ret += check6 (); |
331 | ret += check6d (); /* with daemonization */ | 331 | ret += check6d (); /* with daemonization */ |
332 | } | 332 | } |
diff --git a/src/util/win.cc b/src/util/win.cc index f3e4bd98c..8e71744d0 100644 --- a/src/util/win.cc +++ b/src/util/win.cc | |||
@@ -30,7 +30,7 @@ | |||
30 | #include "winproc.h" | 30 | #include "winproc.h" |
31 | #include "platform.h" | 31 | #include "platform.h" |
32 | #include "gnunet_common.h" | 32 | #include "gnunet_common.h" |
33 | #include "gnunet_network_lib.h" | 33 | #include "gnunet_connection_lib.h" |
34 | 34 | ||
35 | #include <list> | 35 | #include <list> |
36 | using namespace std; | 36 | using namespace std; |