diff options
Diffstat (limited to 'src/transport/gnunet-communicator-unix.c')
-rw-r--r-- | src/transport/gnunet-communicator-unix.c | 1147 |
1 files changed, 1147 insertions, 0 deletions
diff --git a/src/transport/gnunet-communicator-unix.c b/src/transport/gnunet-communicator-unix.c new file mode 100644 index 000000000..b2eebbe20 --- /dev/null +++ b/src/transport/gnunet-communicator-unix.c | |||
@@ -0,0 +1,1147 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2010-2014, 2018 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your 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 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | /** | ||
20 | * @file transport/gnunet-communicator-unix.c | ||
21 | * @brief Transport plugin using unix domain sockets (!) | ||
22 | * Clearly, can only be used locally on Unix/Linux hosts... | ||
23 | * ONLY INTENDED FOR TESTING!!! | ||
24 | * @author Christian Grothoff | ||
25 | * @author Nathan Evans | ||
26 | */ | ||
27 | #include "platform.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | #include "gnunet_protocols.h" | ||
30 | #include "gnunet_statistics_service.h" | ||
31 | #include "gnunet_transport_communication_service.h" | ||
32 | |||
33 | /** | ||
34 | * How many messages do we keep at most in the queue to the | ||
35 | * transport service before we start to drop (default, | ||
36 | * can be changed via the configuration file). | ||
37 | * Should be _below_ the level of the communicator API, as | ||
38 | * otherwise we may read messages just to have them dropped | ||
39 | * by the communicator API. | ||
40 | */ | ||
41 | #define DEFAULT_MAX_QUEUE_LENGTH 8 | ||
42 | |||
43 | /** | ||
44 | * Address prefix used by the communicator. | ||
45 | */ | ||
46 | #define COMMUNICATOR_ADDRESS_PREFIX "unix" | ||
47 | |||
48 | /** | ||
49 | * Configuration section used by the communicator. | ||
50 | */ | ||
51 | #define COMMUNICATOR_CONFIG_SECTION "communicator-unix" | ||
52 | |||
53 | |||
54 | GNUNET_NETWORK_STRUCT_BEGIN | ||
55 | |||
56 | /** | ||
57 | * UNIX Message-Packet header. | ||
58 | */ | ||
59 | struct UNIXMessage | ||
60 | { | ||
61 | /** | ||
62 | * Message header. | ||
63 | */ | ||
64 | struct GNUNET_MessageHeader header; | ||
65 | |||
66 | /** | ||
67 | * What is the identity of the sender (GNUNET_hash of public key) | ||
68 | */ | ||
69 | struct GNUNET_PeerIdentity sender; | ||
70 | |||
71 | }; | ||
72 | |||
73 | GNUNET_NETWORK_STRUCT_END | ||
74 | |||
75 | |||
76 | /** | ||
77 | * Handle for a queue. | ||
78 | */ | ||
79 | struct Queue | ||
80 | { | ||
81 | |||
82 | /** | ||
83 | * Queues with pending messages (!) are kept in a DLL. | ||
84 | */ | ||
85 | struct Queue *next; | ||
86 | |||
87 | /** | ||
88 | * Queues with pending messages (!) are kept in a DLL. | ||
89 | */ | ||
90 | struct Queue *prev; | ||
91 | |||
92 | /** | ||
93 | * To whom are we talking to. | ||
94 | */ | ||
95 | struct GNUNET_PeerIdentity target; | ||
96 | |||
97 | /** | ||
98 | * Address of the other peer. | ||
99 | */ | ||
100 | struct sockaddr_un *address; | ||
101 | |||
102 | /** | ||
103 | * Length of the address. | ||
104 | */ | ||
105 | socklen_t address_len; | ||
106 | |||
107 | /** | ||
108 | * Message currently scheduled for transmission, non-NULL if and only | ||
109 | * if this queue is in the #queue_head DLL. | ||
110 | */ | ||
111 | const struct GNUNET_MessageHeader *msg; | ||
112 | |||
113 | /** | ||
114 | * Message queue we are providing for the #ch. | ||
115 | */ | ||
116 | struct GNUNET_MQ_Handle *mq; | ||
117 | |||
118 | /** | ||
119 | * handle for this queue with the #ch. | ||
120 | */ | ||
121 | struct GNUNET_TRANSPORT_QueueHandle *qh; | ||
122 | |||
123 | /** | ||
124 | * Number of bytes we currently have in our write queue. | ||
125 | */ | ||
126 | unsigned long long bytes_in_queue; | ||
127 | |||
128 | /** | ||
129 | * Timeout for this queue. | ||
130 | */ | ||
131 | struct GNUNET_TIME_Absolute timeout; | ||
132 | |||
133 | /** | ||
134 | * Queue timeout task. | ||
135 | */ | ||
136 | struct GNUNET_SCHEDULER_Task *timeout_task; | ||
137 | |||
138 | }; | ||
139 | |||
140 | |||
141 | /** | ||
142 | * ID of read task | ||
143 | */ | ||
144 | static struct GNUNET_SCHEDULER_Task *read_task; | ||
145 | |||
146 | /** | ||
147 | * ID of write task | ||
148 | */ | ||
149 | static struct GNUNET_SCHEDULER_Task *write_task; | ||
150 | |||
151 | /** | ||
152 | * Number of messages we currently have in our queues towards the transport service. | ||
153 | */ | ||
154 | static unsigned long long delivering_messages; | ||
155 | |||
156 | /** | ||
157 | * Maximum queue length before we stop reading towards the transport service. | ||
158 | */ | ||
159 | static unsigned long long max_queue_length; | ||
160 | |||
161 | /** | ||
162 | * For logging statistics. | ||
163 | */ | ||
164 | static struct GNUNET_STATISTICS_Handle *stats; | ||
165 | |||
166 | /** | ||
167 | * Our environment. | ||
168 | */ | ||
169 | static struct GNUNET_TRANSPORT_CommunicatorHandle *ch; | ||
170 | |||
171 | /** | ||
172 | * Queues (map from peer identity to `struct Queue`) | ||
173 | */ | ||
174 | static struct GNUNET_CONTAINER_MultiPeerMap *queue_map; | ||
175 | |||
176 | /** | ||
177 | * Head of queue of messages to transmit. | ||
178 | */ | ||
179 | static struct Queue *queue_head; | ||
180 | |||
181 | /** | ||
182 | * Tail of queue of messages to transmit. | ||
183 | */ | ||
184 | static struct Queue *queue_tail; | ||
185 | |||
186 | /** | ||
187 | * socket that we transmit all data with | ||
188 | */ | ||
189 | static struct GNUNET_NETWORK_Handle *unix_sock; | ||
190 | |||
191 | /** | ||
192 | * Handle to the operation that publishes our address. | ||
193 | */ | ||
194 | static struct GNUNET_TRANSPORT_AddressIdentifier *ai; | ||
195 | |||
196 | |||
197 | /** | ||
198 | * Functions with this signature are called whenever we need | ||
199 | * to close a queue due to a disconnect or failure to | ||
200 | * establish a connection. | ||
201 | * | ||
202 | * @param queue queue to close down | ||
203 | */ | ||
204 | static void | ||
205 | queue_destroy (struct Queue *queue) | ||
206 | { | ||
207 | struct GNUNET_MQ_Handle *mq; | ||
208 | |||
209 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
210 | "Disconnecting queue for peer `%s'\n", | ||
211 | GNUNET_i2s (&queue->target)); | ||
212 | if (0 != queue->bytes_in_queue) | ||
213 | { | ||
214 | GNUNET_CONTAINER_DLL_remove (queue_head, | ||
215 | queue_tail, | ||
216 | queue); | ||
217 | queue->bytes_in_queue = 0; | ||
218 | } | ||
219 | if (NULL != (mq = queue->mq)) | ||
220 | { | ||
221 | queue->mq = NULL; | ||
222 | GNUNET_MQ_destroy (mq); | ||
223 | } | ||
224 | GNUNET_assert (GNUNET_YES == | ||
225 | GNUNET_CONTAINER_multipeermap_remove (queue_map, | ||
226 | &queue->target, | ||
227 | queue)); | ||
228 | GNUNET_STATISTICS_set (stats, | ||
229 | "# UNIX queues active", | ||
230 | GNUNET_CONTAINER_multipeermap_size (queue_map), | ||
231 | GNUNET_NO); | ||
232 | if (NULL != queue->timeout_task) | ||
233 | { | ||
234 | GNUNET_SCHEDULER_cancel (queue->timeout_task); | ||
235 | queue->timeout_task = NULL; | ||
236 | } | ||
237 | GNUNET_free (queue->address); | ||
238 | GNUNET_free (queue); | ||
239 | } | ||
240 | |||
241 | |||
242 | /** | ||
243 | * Queue was idle for too long, so disconnect it | ||
244 | * | ||
245 | * @param cls the `struct Queue *` to disconnect | ||
246 | */ | ||
247 | static void | ||
248 | queue_timeout (void *cls) | ||
249 | { | ||
250 | struct Queue *queue = cls; | ||
251 | struct GNUNET_TIME_Relative left; | ||
252 | |||
253 | queue->timeout_task = NULL; | ||
254 | left = GNUNET_TIME_absolute_get_remaining (queue->timeout); | ||
255 | if (0 != left.rel_value_us) | ||
256 | { | ||
257 | /* not actually our turn yet, but let's at least update | ||
258 | the monitor, it may think we're about to die ... */ | ||
259 | queue->timeout_task | ||
260 | = GNUNET_SCHEDULER_add_delayed (left, | ||
261 | &queue_timeout, | ||
262 | queue); | ||
263 | return; | ||
264 | } | ||
265 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
266 | "Queue %p was idle for %s, disconnecting\n", | ||
267 | queue, | ||
268 | GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, | ||
269 | GNUNET_YES)); | ||
270 | queue_destroy (queue); | ||
271 | } | ||
272 | |||
273 | |||
274 | /** | ||
275 | * Increment queue timeout due to activity. We do not immediately | ||
276 | * notify the monitor here as that might generate excessive | ||
277 | * signalling. | ||
278 | * | ||
279 | * @param queue queue for which the timeout should be rescheduled | ||
280 | */ | ||
281 | static void | ||
282 | reschedule_queue_timeout (struct Queue *queue) | ||
283 | { | ||
284 | GNUNET_assert (NULL != queue->timeout_task); | ||
285 | queue->timeout | ||
286 | = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT); | ||
287 | } | ||
288 | |||
289 | |||
290 | /** | ||
291 | * Convert unix path to a `struct sockaddr_un *` | ||
292 | * | ||
293 | * @param unixpath path to convert | ||
294 | * @param[out] sock_len set to the length of the address | ||
295 | * @param is_abstract is this an abstract @a unixpath | ||
296 | * @return converted unix path | ||
297 | */ | ||
298 | static struct sockaddr_un * | ||
299 | unix_address_to_sockaddr (const char *unixpath, | ||
300 | socklen_t *sock_len) | ||
301 | { | ||
302 | struct sockaddr_un *un; | ||
303 | size_t slen; | ||
304 | |||
305 | GNUNET_assert (0 < strlen (unixpath)); /* sanity check */ | ||
306 | un = GNUNET_new (struct sockaddr_un); | ||
307 | un->sun_family = AF_UNIX; | ||
308 | slen = strlen (unixpath); | ||
309 | if (slen >= sizeof (un->sun_path)) | ||
310 | slen = sizeof (un->sun_path) - 1; | ||
311 | GNUNET_memcpy (un->sun_path, | ||
312 | unixpath, | ||
313 | slen); | ||
314 | un->sun_path[slen] = '\0'; | ||
315 | slen = sizeof (struct sockaddr_un); | ||
316 | #if HAVE_SOCKADDR_UN_SUN_LEN | ||
317 | un->sun_len = (u_char) slen; | ||
318 | #endif | ||
319 | (*sock_len) = slen; | ||
320 | if ('@' == un->sun_path[0]) | ||
321 | un->sun_path[0] = '\0'; | ||
322 | return un; | ||
323 | } | ||
324 | |||
325 | |||
326 | /** | ||
327 | * Closure to #lookup_queue_it(). | ||
328 | */ | ||
329 | struct LookupCtx | ||
330 | { | ||
331 | /** | ||
332 | * Location to store the queue, if found. | ||
333 | */ | ||
334 | struct Queue *res; | ||
335 | |||
336 | /** | ||
337 | * Address we are looking for. | ||
338 | */ | ||
339 | const struct sockaddr_un *un; | ||
340 | |||
341 | /** | ||
342 | * Number of bytes in @a un | ||
343 | */ | ||
344 | socklen_t un_len; | ||
345 | }; | ||
346 | |||
347 | |||
348 | /** | ||
349 | * Function called to find a queue by address. | ||
350 | * | ||
351 | * @param cls the `struct LookupCtx *` | ||
352 | * @param key peer we are looking for (unused) | ||
353 | * @param value a queue | ||
354 | * @return #GNUNET_YES if not found (continue looking), #GNUNET_NO on success | ||
355 | */ | ||
356 | static int | ||
357 | lookup_queue_it (void *cls, | ||
358 | const struct GNUNET_PeerIdentity *key, | ||
359 | void *value) | ||
360 | { | ||
361 | struct LookupCtx *lctx = cls; | ||
362 | struct Queue *queue = value; | ||
363 | |||
364 | if ( (queue->address_len = lctx->un_len) && | ||
365 | (0 == memcmp (lctx->un, | ||
366 | queue->address, | ||
367 | queue->address_len)) ) | ||
368 | { | ||
369 | lctx->res = queue; | ||
370 | return GNUNET_NO; | ||
371 | } | ||
372 | return GNUNET_YES; | ||
373 | } | ||
374 | |||
375 | |||
376 | /** | ||
377 | * Find an existing queue by address. | ||
378 | * | ||
379 | * @param plugin the plugin | ||
380 | * @param address the address to find | ||
381 | * @return NULL if queue was not found | ||
382 | */ | ||
383 | static struct Queue * | ||
384 | lookup_queue (const struct GNUNET_PeerIdentity *peer, | ||
385 | const struct sockaddr_un *un, | ||
386 | socklen_t un_len) | ||
387 | { | ||
388 | struct LookupCtx lctx; | ||
389 | |||
390 | lctx.un = un; | ||
391 | lctx.un_len = un_len; | ||
392 | GNUNET_CONTAINER_multipeermap_get_multiple (queue_map, | ||
393 | peer, | ||
394 | &lookup_queue_it, | ||
395 | &lctx); | ||
396 | return lctx.res; | ||
397 | } | ||
398 | |||
399 | |||
400 | /** | ||
401 | * We have been notified that our socket is ready to write. | ||
402 | * Then reschedule this function to be called again once more is available. | ||
403 | * | ||
404 | * @param cls NULL | ||
405 | */ | ||
406 | static void | ||
407 | select_write_cb (void *cls) | ||
408 | { | ||
409 | struct Queue *queue = queue_tail; | ||
410 | const struct GNUNET_MessageHeader *msg = queue->msg; | ||
411 | size_t msg_size = ntohs (msg->size); | ||
412 | ssize_t sent; | ||
413 | |||
414 | /* take queue of the ready list */ | ||
415 | write_task = NULL; | ||
416 | GNUNET_CONTAINER_DLL_remove (queue_head, | ||
417 | queue_tail, | ||
418 | queue); | ||
419 | if (NULL != queue_head) | ||
420 | write_task = | ||
421 | GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, | ||
422 | unix_sock, | ||
423 | &select_write_cb, | ||
424 | NULL); | ||
425 | |||
426 | /* send 'msg' */ | ||
427 | queue->msg = NULL; | ||
428 | GNUNET_MQ_impl_send_continue (queue->mq); | ||
429 | resend: | ||
430 | /* Send the data */ | ||
431 | sent = GNUNET_NETWORK_socket_sendto (unix_sock, | ||
432 | queue->msg, | ||
433 | msg_size, | ||
434 | (const struct sockaddr *) queue->address, | ||
435 | queue->address_len); | ||
436 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
437 | "UNIX transmitted message to %s (%d/%u: %s)\n", | ||
438 | GNUNET_i2s (&queue->target), | ||
439 | (int) sent, | ||
440 | (unsigned int) msg_size, | ||
441 | (sent < 0) ? STRERROR (errno) : "ok"); | ||
442 | if (-1 != sent) | ||
443 | { | ||
444 | GNUNET_STATISTICS_update (stats, | ||
445 | "# bytes sent", | ||
446 | (long long) sent, | ||
447 | GNUNET_NO); | ||
448 | reschedule_queue_timeout (queue); | ||
449 | return; /* all good */ | ||
450 | } | ||
451 | GNUNET_STATISTICS_update (stats, | ||
452 | "# network transmission failures", | ||
453 | 1, | ||
454 | GNUNET_NO); | ||
455 | switch (errno) | ||
456 | { | ||
457 | case EAGAIN: | ||
458 | case ENOBUFS: | ||
459 | /* We should retry later... */ | ||
460 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, | ||
461 | "send"); | ||
462 | return; | ||
463 | case EMSGSIZE: | ||
464 | { | ||
465 | socklen_t size = 0; | ||
466 | socklen_t len = sizeof (size); | ||
467 | |||
468 | GNUNET_NETWORK_socket_getsockopt (unix_sock, | ||
469 | SOL_SOCKET, | ||
470 | SO_SNDBUF, | ||
471 | &size, | ||
472 | &len); | ||
473 | if (size > ntohs (msg->size)) | ||
474 | { | ||
475 | /* Buffer is bigger than message: error, no retry | ||
476 | * This should never happen!*/ | ||
477 | GNUNET_break (0); | ||
478 | return; | ||
479 | } | ||
480 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
481 | "Trying to increase socket buffer size from %u to %u for message size %u\n", | ||
482 | (unsigned int) size, | ||
483 | (unsigned int) ((msg_size / 1000) + 2) * 1000, | ||
484 | (unsigned int) msg_size); | ||
485 | size = ((msg_size / 1000) + 2) * 1000; | ||
486 | if (GNUNET_OK == | ||
487 | GNUNET_NETWORK_socket_setsockopt (unix_sock, | ||
488 | SOL_SOCKET, | ||
489 | SO_SNDBUF, | ||
490 | &size, | ||
491 | sizeof (size))) | ||
492 | goto resend; /* Increased buffer size, retry sending */ | ||
493 | /* Ok, then just try very modest increase */ | ||
494 | size = msg_size; | ||
495 | if (GNUNET_OK == | ||
496 | GNUNET_NETWORK_socket_setsockopt (unix_sock, | ||
497 | SOL_SOCKET, | ||
498 | SO_SNDBUF, | ||
499 | &size, | ||
500 | sizeof (size))) | ||
501 | goto resend; /* Increased buffer size, retry sending */ | ||
502 | /* Could not increase buffer size: error, no retry */ | ||
503 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, | ||
504 | "setsockopt"); | ||
505 | return; | ||
506 | } | ||
507 | default: | ||
508 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, | ||
509 | "send"); | ||
510 | return; | ||
511 | } | ||
512 | } | ||
513 | |||
514 | |||
515 | /** | ||
516 | * Signature of functions implementing the sending functionality of a | ||
517 | * message queue. | ||
518 | * | ||
519 | * @param mq the message queue | ||
520 | * @param msg the message to send | ||
521 | * @param impl_state our `struct Queue` | ||
522 | */ | ||
523 | static void | ||
524 | mq_send (struct GNUNET_MQ_Handle *mq, | ||
525 | const struct GNUNET_MessageHeader *msg, | ||
526 | void *impl_state) | ||
527 | { | ||
528 | struct Queue *queue = impl_state; | ||
529 | |||
530 | GNUNET_assert (mq == queue->mq); | ||
531 | GNUNET_assert (NULL == queue->msg); | ||
532 | queue->msg = msg; | ||
533 | GNUNET_CONTAINER_DLL_insert (queue_head, | ||
534 | queue_tail, | ||
535 | queue); | ||
536 | GNUNET_assert (NULL != unix_sock); | ||
537 | if (NULL == write_task) | ||
538 | write_task = | ||
539 | GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, | ||
540 | unix_sock, | ||
541 | &select_write_cb, | ||
542 | NULL); | ||
543 | } | ||
544 | |||
545 | |||
546 | /** | ||
547 | * Signature of functions implementing the destruction of a message | ||
548 | * queue. Implementations must not free @a mq, but should take care | ||
549 | * of @a impl_state. | ||
550 | * | ||
551 | * @param mq the message queue to destroy | ||
552 | * @param impl_state our `struct Queue` | ||
553 | */ | ||
554 | static void | ||
555 | mq_destroy (struct GNUNET_MQ_Handle *mq, | ||
556 | void *impl_state) | ||
557 | { | ||
558 | struct Queue *queue = impl_state; | ||
559 | |||
560 | if (mq == queue->mq) | ||
561 | { | ||
562 | queue->mq = NULL; | ||
563 | queue_destroy (queue); | ||
564 | } | ||
565 | } | ||
566 | |||
567 | |||
568 | /** | ||
569 | * Implementation function that cancels the currently sent message. | ||
570 | * | ||
571 | * @param mq message queue | ||
572 | * @param impl_state our `struct Queue` | ||
573 | */ | ||
574 | static void | ||
575 | mq_cancel (struct GNUNET_MQ_Handle *mq, | ||
576 | void *impl_state) | ||
577 | { | ||
578 | struct Queue *queue = impl_state; | ||
579 | |||
580 | GNUNET_assert (NULL != queue->msg); | ||
581 | queue->msg = NULL; | ||
582 | GNUNET_CONTAINER_DLL_remove (queue_head, | ||
583 | queue_tail, | ||
584 | queue); | ||
585 | GNUNET_assert (NULL != write_task); | ||
586 | if (NULL == queue_head) | ||
587 | { | ||
588 | GNUNET_SCHEDULER_cancel (write_task); | ||
589 | write_task = NULL; | ||
590 | } | ||
591 | } | ||
592 | |||
593 | |||
594 | /** | ||
595 | * Generic error handler, called with the appropriate | ||
596 | * error code and the same closure specified at the creation of | ||
597 | * the message queue. | ||
598 | * Not every message queue implementation supports an error handler. | ||
599 | * | ||
600 | * @param cls our `struct Queue` | ||
601 | * @param error error code | ||
602 | */ | ||
603 | static void | ||
604 | mq_error (void *cls, | ||
605 | enum GNUNET_MQ_Error error) | ||
606 | { | ||
607 | struct Queue *queue = cls; | ||
608 | |||
609 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
610 | "UNIX MQ error in queue to %s: %d\n", | ||
611 | GNUNET_i2s (&queue->target), | ||
612 | (int) error); | ||
613 | queue_destroy (queue); | ||
614 | } | ||
615 | |||
616 | |||
617 | /** | ||
618 | * Creates a new outbound queue the transport service will use to send | ||
619 | * data to another peer. | ||
620 | * | ||
621 | * @param peer the target peer | ||
622 | * @param un the address | ||
623 | * @param un_len number of bytes in @a un | ||
624 | * @return the queue or NULL of max connections exceeded | ||
625 | */ | ||
626 | static struct Queue * | ||
627 | setup_queue (const struct GNUNET_PeerIdentity *target, | ||
628 | const struct sockaddr_un *un, | ||
629 | socklen_t un_len) | ||
630 | { | ||
631 | struct Queue *queue; | ||
632 | |||
633 | queue = GNUNET_new (struct Queue); | ||
634 | queue->target = *target; | ||
635 | queue->address = GNUNET_memdup (un, | ||
636 | un_len); | ||
637 | queue->address_len = un_len; | ||
638 | (void) GNUNET_CONTAINER_multipeermap_put (queue_map, | ||
639 | &queue->target, | ||
640 | queue, | ||
641 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); | ||
642 | GNUNET_STATISTICS_set (stats, | ||
643 | "# queues active", | ||
644 | GNUNET_CONTAINER_multipeermap_size (queue_map), | ||
645 | GNUNET_NO); | ||
646 | queue->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT); | ||
647 | queue->timeout_task | ||
648 | = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, | ||
649 | &queue_timeout, | ||
650 | queue); | ||
651 | queue->mq | ||
652 | = GNUNET_MQ_queue_for_callbacks (&mq_send, | ||
653 | &mq_destroy, | ||
654 | &mq_cancel, | ||
655 | queue, | ||
656 | NULL, | ||
657 | &mq_error, | ||
658 | queue); | ||
659 | { | ||
660 | char *foreign_addr; | ||
661 | |||
662 | if ('\0' == un->sun_path[0]) | ||
663 | GNUNET_asprintf (&foreign_addr, | ||
664 | "%s-@%s", | ||
665 | COMMUNICATOR_ADDRESS_PREFIX, | ||
666 | &un->sun_path[1]); | ||
667 | else | ||
668 | GNUNET_asprintf (&foreign_addr, | ||
669 | "%s-%s", | ||
670 | COMMUNICATOR_ADDRESS_PREFIX, | ||
671 | un->sun_path); | ||
672 | queue->qh | ||
673 | = GNUNET_TRANSPORT_communicator_mq_add (ch, | ||
674 | &queue->target, | ||
675 | foreign_addr, | ||
676 | GNUNET_ATS_NET_LOOPBACK, | ||
677 | queue->mq); | ||
678 | GNUNET_free (foreign_addr); | ||
679 | } | ||
680 | return queue; | ||
681 | } | ||
682 | |||
683 | |||
684 | /** | ||
685 | * We have been notified that our socket has something to read. Do the | ||
686 | * read and reschedule this function to be called again once more is | ||
687 | * available. | ||
688 | * | ||
689 | * @param cls NULL | ||
690 | */ | ||
691 | static void | ||
692 | select_read_cb (void *cls); | ||
693 | |||
694 | |||
695 | /** | ||
696 | * Function called when message was successfully passed to | ||
697 | * transport service. Continue read activity. | ||
698 | * | ||
699 | * @param cls NULL | ||
700 | * @param success #GNUNET_OK on success | ||
701 | */ | ||
702 | static void | ||
703 | receive_complete_cb (void *cls, | ||
704 | int success) | ||
705 | { | ||
706 | delivering_messages--; | ||
707 | if (GNUNET_OK != success) | ||
708 | GNUNET_STATISTICS_update (stats, | ||
709 | "# transport transmission failures", | ||
710 | 1, | ||
711 | GNUNET_NO); | ||
712 | GNUNET_assert (NULL != unix_sock); | ||
713 | if ( (NULL == read_task) && | ||
714 | (delivering_messages < max_queue_length) ) | ||
715 | read_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | ||
716 | unix_sock, | ||
717 | &select_read_cb, | ||
718 | NULL); | ||
719 | } | ||
720 | |||
721 | |||
722 | /** | ||
723 | * We have been notified that our socket has something to read. Do the | ||
724 | * read and reschedule this function to be called again once more is | ||
725 | * available. | ||
726 | * | ||
727 | * @param cls NULL | ||
728 | */ | ||
729 | static void | ||
730 | select_read_cb (void *cls) | ||
731 | { | ||
732 | char buf[65536] GNUNET_ALIGN; | ||
733 | struct Queue *queue; | ||
734 | const struct UNIXMessage *msg; | ||
735 | struct sockaddr_un un; | ||
736 | socklen_t addrlen; | ||
737 | ssize_t ret; | ||
738 | uint16_t msize; | ||
739 | |||
740 | GNUNET_assert (NULL != unix_sock); | ||
741 | read_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | ||
742 | unix_sock, | ||
743 | &select_read_cb, | ||
744 | NULL); | ||
745 | addrlen = sizeof (un); | ||
746 | memset (&un, | ||
747 | 0, | ||
748 | sizeof (un)); | ||
749 | ret = GNUNET_NETWORK_socket_recvfrom (unix_sock, | ||
750 | buf, | ||
751 | sizeof (buf), | ||
752 | (struct sockaddr *) &un, | ||
753 | &addrlen); | ||
754 | if ( (-1 == ret) && | ||
755 | ( (EAGAIN == errno) || | ||
756 | (ENOBUFS == errno) ) ) | ||
757 | return; | ||
758 | if (-1 == ret) | ||
759 | { | ||
760 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, | ||
761 | "recvfrom"); | ||
762 | return; | ||
763 | } | ||
764 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
765 | "Read %d bytes from socket %s\n", | ||
766 | (int) ret, | ||
767 | un.sun_path); | ||
768 | GNUNET_assert (AF_UNIX == (un.sun_family)); | ||
769 | msg = (struct UNIXMessage *) buf; | ||
770 | msize = ntohs (msg->header.size); | ||
771 | if ( (msize < sizeof (struct UNIXMessage)) || | ||
772 | (msize > ret) ) | ||
773 | { | ||
774 | GNUNET_break_op (0); | ||
775 | return; | ||
776 | } | ||
777 | queue = lookup_queue (&msg->sender, | ||
778 | &un, | ||
779 | addrlen); | ||
780 | if (NULL == queue) | ||
781 | queue = setup_queue (&msg->sender, | ||
782 | &un, | ||
783 | addrlen); | ||
784 | else | ||
785 | reschedule_queue_timeout (queue); | ||
786 | if (NULL == queue) | ||
787 | { | ||
788 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
789 | _("Maximum number of UNIX connections exceeded, dropping incoming message\n")); | ||
790 | return; | ||
791 | } | ||
792 | |||
793 | { | ||
794 | uint16_t offset = 0; | ||
795 | uint16_t tsize = msize - sizeof (struct UNIXMessage); | ||
796 | const char *msgbuf = (const char *) &msg[1]; | ||
797 | |||
798 | while (offset + sizeof (struct GNUNET_MessageHeader) <= tsize) | ||
799 | { | ||
800 | const struct GNUNET_MessageHeader *currhdr; | ||
801 | struct GNUNET_MessageHeader al_hdr; | ||
802 | uint16_t csize; | ||
803 | |||
804 | currhdr = (const struct GNUNET_MessageHeader *) &msgbuf[offset]; | ||
805 | /* ensure aligned access */ | ||
806 | memcpy (&al_hdr, | ||
807 | currhdr, | ||
808 | sizeof (al_hdr)); | ||
809 | csize = ntohs (al_hdr.size); | ||
810 | if ( (csize < sizeof (struct GNUNET_MessageHeader)) || | ||
811 | (csize > tsize - offset)) | ||
812 | { | ||
813 | GNUNET_break_op (0); | ||
814 | break; | ||
815 | } | ||
816 | ret = GNUNET_TRANSPORT_communicator_receive (ch, | ||
817 | &msg->sender, | ||
818 | currhdr, | ||
819 | &receive_complete_cb, | ||
820 | NULL); | ||
821 | if (GNUNET_SYSERR == ret) | ||
822 | return; /* transport not up */ | ||
823 | if (GNUNET_NO == ret) | ||
824 | break; | ||
825 | delivering_messages++; | ||
826 | offset += csize; | ||
827 | } | ||
828 | } | ||
829 | if (delivering_messages >= max_queue_length) | ||
830 | { | ||
831 | /* we should try to apply 'back pressure' */ | ||
832 | GNUNET_SCHEDULER_cancel (read_task); | ||
833 | read_task = NULL; | ||
834 | } | ||
835 | } | ||
836 | |||
837 | |||
838 | /** | ||
839 | * Function called by the transport service to initialize a | ||
840 | * message queue given address information about another peer. | ||
841 | * If and when the communication channel is established, the | ||
842 | * communicator must call #GNUNET_TRANSPORT_communicator_mq_add() | ||
843 | * to notify the service that the channel is now up. It is | ||
844 | * the responsibility of the communicator to manage sane | ||
845 | * retries and timeouts for any @a peer/@a address combination | ||
846 | * provided by the transport service. Timeouts and retries | ||
847 | * do not need to be signalled to the transport service. | ||
848 | * | ||
849 | * @param cls closure | ||
850 | * @param peer identity of the other peer | ||
851 | * @param address where to send the message, human-readable | ||
852 | * communicator-specific format, 0-terminated, UTF-8 | ||
853 | * @return #GNUNET_OK on success, #GNUNET_SYSERR if the provided address is invalid | ||
854 | */ | ||
855 | static int | ||
856 | mq_init (void *cls, | ||
857 | const struct GNUNET_PeerIdentity *peer, | ||
858 | const char *address) | ||
859 | { | ||
860 | struct Queue *queue; | ||
861 | const char *path; | ||
862 | struct sockaddr_un *un; | ||
863 | socklen_t un_len; | ||
864 | |||
865 | if (0 != strncmp (address, | ||
866 | COMMUNICATOR_ADDRESS_PREFIX "-", | ||
867 | strlen (COMMUNICATOR_ADDRESS_PREFIX "-"))) | ||
868 | { | ||
869 | GNUNET_break_op (0); | ||
870 | return GNUNET_SYSERR; | ||
871 | } | ||
872 | path = &address[strlen (COMMUNICATOR_ADDRESS_PREFIX "-")]; | ||
873 | un = unix_address_to_sockaddr (path, | ||
874 | &un_len); | ||
875 | queue = lookup_queue (peer, | ||
876 | un, | ||
877 | un_len); | ||
878 | if (NULL != queue) | ||
879 | { | ||
880 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
881 | "Address `%s' for %s ignored, queue exists\n", | ||
882 | path, | ||
883 | GNUNET_i2s (peer)); | ||
884 | GNUNET_free (un); | ||
885 | return GNUNET_OK; | ||
886 | } | ||
887 | queue = setup_queue (peer, | ||
888 | un, | ||
889 | un_len); | ||
890 | GNUNET_free (un); | ||
891 | if (NULL == queue) | ||
892 | { | ||
893 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
894 | "Failed to setup queue to %s at `%s'\n", | ||
895 | GNUNET_i2s (peer), | ||
896 | path); | ||
897 | return GNUNET_NO; | ||
898 | } | ||
899 | return GNUNET_OK; | ||
900 | } | ||
901 | |||
902 | |||
903 | /** | ||
904 | * Iterator over all message queues to clean up. | ||
905 | * | ||
906 | * @param cls NULL | ||
907 | * @param target unused | ||
908 | * @param value the queue to destroy | ||
909 | * @return #GNUNET_OK to continue to iterate | ||
910 | */ | ||
911 | static int | ||
912 | get_queue_delete_it (void *cls, | ||
913 | const struct GNUNET_PeerIdentity *target, | ||
914 | void *value) | ||
915 | { | ||
916 | struct Queue *queue = value; | ||
917 | |||
918 | (void) cls; | ||
919 | (void) target; | ||
920 | queue_destroy (queue); | ||
921 | return GNUNET_OK; | ||
922 | } | ||
923 | |||
924 | |||
925 | /** | ||
926 | * Shutdown the UNIX communicator. | ||
927 | * | ||
928 | * @param cls NULL (always) | ||
929 | */ | ||
930 | static void | ||
931 | do_shutdown (void *cls) | ||
932 | { | ||
933 | if (NULL != read_task) | ||
934 | { | ||
935 | GNUNET_SCHEDULER_cancel (read_task); | ||
936 | read_task = NULL; | ||
937 | } | ||
938 | if (NULL != write_task) | ||
939 | { | ||
940 | GNUNET_SCHEDULER_cancel (write_task); | ||
941 | write_task = NULL; | ||
942 | } | ||
943 | if (NULL != unix_sock) | ||
944 | { | ||
945 | GNUNET_break (GNUNET_OK == | ||
946 | GNUNET_NETWORK_socket_close (unix_sock)); | ||
947 | unix_sock = NULL; | ||
948 | } | ||
949 | GNUNET_CONTAINER_multipeermap_iterate (queue_map, | ||
950 | &get_queue_delete_it, | ||
951 | NULL); | ||
952 | GNUNET_CONTAINER_multipeermap_destroy (queue_map); | ||
953 | if (NULL != ai) | ||
954 | { | ||
955 | GNUNET_TRANSPORT_communicator_address_remove (ai); | ||
956 | ai = NULL; | ||
957 | } | ||
958 | if (NULL != ch) | ||
959 | { | ||
960 | GNUNET_TRANSPORT_communicator_disconnect (ch); | ||
961 | ch = NULL; | ||
962 | } | ||
963 | if (NULL != stats) | ||
964 | { | ||
965 | GNUNET_STATISTICS_destroy (stats, | ||
966 | GNUNET_NO); | ||
967 | stats = NULL; | ||
968 | } | ||
969 | } | ||
970 | |||
971 | |||
972 | /** | ||
973 | * Setup communicator and launch network interactions. | ||
974 | * | ||
975 | * @param cls NULL (always) | ||
976 | * @param args remaining command-line arguments | ||
977 | * @param cfgfile name of the configuration file used (for saving, can be NULL!) | ||
978 | * @param cfg configuration | ||
979 | */ | ||
980 | static void | ||
981 | run (void *cls, | ||
982 | char *const *args, | ||
983 | const char *cfgfile, | ||
984 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
985 | { | ||
986 | char *unix_socket_path; | ||
987 | struct sockaddr_un *un; | ||
988 | socklen_t un_len; | ||
989 | char *my_addr; | ||
990 | (void) cls; | ||
991 | |||
992 | if (GNUNET_OK != | ||
993 | GNUNET_CONFIGURATION_get_value_filename (cfg, | ||
994 | COMMUNICATOR_CONFIG_SECTION, | ||
995 | "UNIXPATH", | ||
996 | &unix_socket_path)) | ||
997 | { | ||
998 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | ||
999 | COMMUNICATOR_CONFIG_SECTION, | ||
1000 | "UNIXPATH"); | ||
1001 | return; | ||
1002 | } | ||
1003 | if (GNUNET_OK != | ||
1004 | GNUNET_CONFIGURATION_get_value_number (cfg, | ||
1005 | COMMUNICATOR_CONFIG_SECTION, | ||
1006 | "MAX_QUEUE_LENGTH", | ||
1007 | &max_queue_length)) | ||
1008 | max_queue_length = DEFAULT_MAX_QUEUE_LENGTH; | ||
1009 | |||
1010 | un = unix_address_to_sockaddr (unix_socket_path, | ||
1011 | &un_len); | ||
1012 | if (NULL == un) | ||
1013 | { | ||
1014 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1015 | "Failed to setup UNIX domain socket address with path `%s'\n", | ||
1016 | unix_socket_path); | ||
1017 | GNUNET_free (unix_socket_path); | ||
1018 | return; | ||
1019 | } | ||
1020 | unix_sock = GNUNET_NETWORK_socket_create (AF_UNIX, | ||
1021 | SOCK_DGRAM, | ||
1022 | 0); | ||
1023 | if (NULL == unix_sock) | ||
1024 | { | ||
1025 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, | ||
1026 | "socket"); | ||
1027 | GNUNET_free (un); | ||
1028 | GNUNET_free (unix_socket_path); | ||
1029 | return; | ||
1030 | } | ||
1031 | if ( ('\0' != un->sun_path[0]) && | ||
1032 | (GNUNET_OK != | ||
1033 | GNUNET_DISK_directory_create_for_file (un->sun_path)) ) | ||
1034 | { | ||
1035 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1036 | _("Cannot create path to `%s'\n"), | ||
1037 | un->sun_path); | ||
1038 | GNUNET_NETWORK_socket_close (unix_sock); | ||
1039 | unix_sock = NULL; | ||
1040 | GNUNET_free (un); | ||
1041 | GNUNET_free (unix_socket_path); | ||
1042 | return; | ||
1043 | } | ||
1044 | if (GNUNET_OK != | ||
1045 | GNUNET_NETWORK_socket_bind (unix_sock, | ||
1046 | (const struct sockaddr *) un, | ||
1047 | un_len)) | ||
1048 | { | ||
1049 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, | ||
1050 | "bind", | ||
1051 | un->sun_path); | ||
1052 | GNUNET_NETWORK_socket_close (unix_sock); | ||
1053 | unix_sock = NULL; | ||
1054 | GNUNET_free (un); | ||
1055 | GNUNET_free (unix_socket_path); | ||
1056 | return; | ||
1057 | } | ||
1058 | GNUNET_free (un); | ||
1059 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1060 | "Bound to `%s'\n", | ||
1061 | unix_socket_path); | ||
1062 | stats = GNUNET_STATISTICS_create ("C-UNIX", | ||
1063 | cfg); | ||
1064 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, | ||
1065 | NULL); | ||
1066 | read_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | ||
1067 | unix_sock, | ||
1068 | &select_read_cb, | ||
1069 | NULL); | ||
1070 | queue_map = GNUNET_CONTAINER_multipeermap_create (10, | ||
1071 | GNUNET_NO); | ||
1072 | ch = GNUNET_TRANSPORT_communicator_connect (cfg, | ||
1073 | COMMUNICATOR_CONFIG_SECTION, | ||
1074 | COMMUNICATOR_ADDRESS_PREFIX, | ||
1075 | 65535, | ||
1076 | &mq_init, | ||
1077 | NULL); | ||
1078 | if (NULL == ch) | ||
1079 | { | ||
1080 | GNUNET_break (0); | ||
1081 | GNUNET_SCHEDULER_shutdown (); | ||
1082 | GNUNET_free (unix_socket_path); | ||
1083 | return; | ||
1084 | } | ||
1085 | GNUNET_asprintf (&my_addr, | ||
1086 | "%s-%s", | ||
1087 | COMMUNICATOR_ADDRESS_PREFIX, | ||
1088 | unix_socket_path); | ||
1089 | GNUNET_free (unix_socket_path); | ||
1090 | ai = GNUNET_TRANSPORT_communicator_address_add (ch, | ||
1091 | my_addr, | ||
1092 | GNUNET_ATS_NET_LOOPBACK, | ||
1093 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
1094 | GNUNET_free (my_addr); | ||
1095 | } | ||
1096 | |||
1097 | |||
1098 | /** | ||
1099 | * The main function for the UNIX communicator. | ||
1100 | * | ||
1101 | * @param argc number of arguments from the command line | ||
1102 | * @param argv command line arguments | ||
1103 | * @return 0 ok, 1 on error | ||
1104 | */ | ||
1105 | int | ||
1106 | main (int argc, | ||
1107 | char *const *argv) | ||
1108 | { | ||
1109 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
1110 | GNUNET_GETOPT_OPTION_END | ||
1111 | }; | ||
1112 | int ret; | ||
1113 | |||
1114 | if (GNUNET_OK != | ||
1115 | GNUNET_STRINGS_get_utf8_args (argc, argv, | ||
1116 | &argc, &argv)) | ||
1117 | return 2; | ||
1118 | |||
1119 | ret = | ||
1120 | (GNUNET_OK == | ||
1121 | GNUNET_PROGRAM_run (argc, argv, | ||
1122 | "gnunet-communicator-unix", | ||
1123 | _("GNUnet UNIX domain socket communicator"), | ||
1124 | options, | ||
1125 | &run, | ||
1126 | NULL)) ? 0 : 1; | ||
1127 | GNUNET_free ((void*) argv); | ||
1128 | return ret; | ||
1129 | } | ||
1130 | |||
1131 | |||
1132 | #if defined(LINUX) && defined(__GLIBC__) | ||
1133 | #include <malloc.h> | ||
1134 | |||
1135 | /** | ||
1136 | * MINIMIZE heap size (way below 128k) since this process doesn't need much. | ||
1137 | */ | ||
1138 | void __attribute__ ((constructor)) | ||
1139 | GNUNET_ARM_memory_init () | ||
1140 | { | ||
1141 | mallopt (M_TRIM_THRESHOLD, 4 * 1024); | ||
1142 | mallopt (M_TOP_PAD, 1 * 1024); | ||
1143 | malloc_trim (0); | ||
1144 | } | ||
1145 | #endif | ||
1146 | |||
1147 | /* end of gnunet-communicator-unix.c */ | ||