diff options
Diffstat (limited to 'src/transport/plugin_transport_unix.c')
-rw-r--r-- | src/transport/plugin_transport_unix.c | 1890 |
1 files changed, 0 insertions, 1890 deletions
diff --git a/src/transport/plugin_transport_unix.c b/src/transport/plugin_transport_unix.c deleted file mode 100644 index 269949a99..000000000 --- a/src/transport/plugin_transport_unix.c +++ /dev/null | |||
@@ -1,1890 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2010-2014 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 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file transport/plugin_transport_unix.c | ||
23 | * @brief Transport plugin using unix domain sockets (!) | ||
24 | * Clearly, can only be used locally on Unix/Linux hosts... | ||
25 | * ONLY INTENDED FOR TESTING!!! | ||
26 | * @author Christian Grothoff | ||
27 | * @author Nathan Evans | ||
28 | */ | ||
29 | #include "platform.h" | ||
30 | #include "gnunet_util_lib.h" | ||
31 | #include "gnunet_hello_lib.h" | ||
32 | #include "gnunet_protocols.h" | ||
33 | #include "gnunet_statistics_service.h" | ||
34 | #include "gnunet_transport_service.h" | ||
35 | #include "gnunet_transport_plugin.h" | ||
36 | #include "transport.h" | ||
37 | |||
38 | |||
39 | /** | ||
40 | * Return code we give on 'send' if we failed to send right now | ||
41 | * but it makes sense to retry later. (Note: we might want to | ||
42 | * move this to the plugin API!?). | ||
43 | */ | ||
44 | #define RETRY 0 | ||
45 | |||
46 | /** | ||
47 | * Name of the plugin. | ||
48 | */ | ||
49 | #define PLUGIN_NAME "unix" | ||
50 | |||
51 | /** | ||
52 | * Options for UNIX Domain addresses. | ||
53 | */ | ||
54 | enum UNIX_ADDRESS_OPTIONS | ||
55 | { | ||
56 | /** | ||
57 | * No special options. | ||
58 | */ | ||
59 | UNIX_OPTIONS_NONE = 0, | ||
60 | |||
61 | /** | ||
62 | * Linux abstract domain sockets should be used. | ||
63 | */ | ||
64 | UNIX_OPTIONS_USE_ABSTRACT_SOCKETS = 1 | ||
65 | }; | ||
66 | |||
67 | |||
68 | /** | ||
69 | * How long until we give up on transmitting the welcome message? | ||
70 | */ | ||
71 | #define HOSTNAME_RESOLVE_TIMEOUT \ | ||
72 | GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) | ||
73 | |||
74 | #define LOG(kind, ...) GNUNET_log_from (kind, "transport-unix", __VA_ARGS__) | ||
75 | |||
76 | |||
77 | GNUNET_NETWORK_STRUCT_BEGIN | ||
78 | |||
79 | /** | ||
80 | * Binary format for an UNIX Domain Socket address in GNUnet. | ||
81 | */ | ||
82 | struct UnixAddress | ||
83 | { | ||
84 | /** | ||
85 | * Options to use for the address, in NBO | ||
86 | */ | ||
87 | uint32_t options GNUNET_PACKED; | ||
88 | |||
89 | /** | ||
90 | * Length of the address (path length), in NBO | ||
91 | */ | ||
92 | uint32_t addrlen GNUNET_PACKED; | ||
93 | |||
94 | /* followed by actual path */ | ||
95 | }; | ||
96 | |||
97 | |||
98 | /** | ||
99 | * UNIX Message-Packet header. | ||
100 | */ | ||
101 | struct UNIXMessage | ||
102 | { | ||
103 | /** | ||
104 | * Message header. | ||
105 | */ | ||
106 | struct GNUNET_MessageHeader header; | ||
107 | |||
108 | /** | ||
109 | * What is the identity of the sender (GNUNET_hash of public key) | ||
110 | */ | ||
111 | struct GNUNET_PeerIdentity sender; | ||
112 | }; | ||
113 | |||
114 | GNUNET_NETWORK_STRUCT_END | ||
115 | |||
116 | |||
117 | /** | ||
118 | * Information we track for a message awaiting transmission. | ||
119 | */ | ||
120 | struct UNIXMessageWrapper | ||
121 | { | ||
122 | /** | ||
123 | * We keep messages in a doubly linked list. | ||
124 | */ | ||
125 | struct UNIXMessageWrapper *next; | ||
126 | |||
127 | /** | ||
128 | * We keep messages in a doubly linked list. | ||
129 | */ | ||
130 | struct UNIXMessageWrapper *prev; | ||
131 | |||
132 | /** | ||
133 | * The actual payload (allocated separately right now). | ||
134 | */ | ||
135 | struct UNIXMessage *msg; | ||
136 | |||
137 | /** | ||
138 | * Session this message belongs to. | ||
139 | */ | ||
140 | struct GNUNET_ATS_Session *session; | ||
141 | |||
142 | /** | ||
143 | * Function to call upon transmission. | ||
144 | */ | ||
145 | GNUNET_TRANSPORT_TransmitContinuation cont; | ||
146 | |||
147 | /** | ||
148 | * Closure for @e cont. | ||
149 | */ | ||
150 | void *cont_cls; | ||
151 | |||
152 | /** | ||
153 | * Timeout for this message. | ||
154 | */ | ||
155 | struct GNUNET_TIME_Absolute timeout; | ||
156 | |||
157 | /** | ||
158 | * Number of bytes in @e msg. | ||
159 | */ | ||
160 | size_t msgsize; | ||
161 | |||
162 | /** | ||
163 | * Number of bytes of payload encapsulated in @e msg. | ||
164 | */ | ||
165 | size_t payload; | ||
166 | |||
167 | /** | ||
168 | * Priority of the message (ignored, just dragged along in UNIX). | ||
169 | */ | ||
170 | unsigned int priority; | ||
171 | }; | ||
172 | |||
173 | |||
174 | /** | ||
175 | * Handle for a session. | ||
176 | */ | ||
177 | struct GNUNET_ATS_Session | ||
178 | { | ||
179 | /** | ||
180 | * Sessions with pending messages (!) are kept in a DLL. | ||
181 | */ | ||
182 | struct GNUNET_ATS_Session *next; | ||
183 | |||
184 | /** | ||
185 | * Sessions with pending messages (!) are kept in a DLL. | ||
186 | */ | ||
187 | struct GNUNET_ATS_Session *prev; | ||
188 | |||
189 | /** | ||
190 | * To whom are we talking to (set to our identity | ||
191 | * if we are still waiting for the welcome message). | ||
192 | * | ||
193 | * FIXME: information duplicated with 'peer' in address! | ||
194 | */ | ||
195 | struct GNUNET_PeerIdentity target; | ||
196 | |||
197 | /** | ||
198 | * Pointer to the global plugin struct. | ||
199 | */ | ||
200 | struct Plugin *plugin; | ||
201 | |||
202 | /** | ||
203 | * Address of the other peer. | ||
204 | */ | ||
205 | struct GNUNET_HELLO_Address *address; | ||
206 | |||
207 | /** | ||
208 | * Number of bytes we currently have in our write queue. | ||
209 | */ | ||
210 | unsigned long long bytes_in_queue; | ||
211 | |||
212 | /** | ||
213 | * Timeout for this session. | ||
214 | */ | ||
215 | struct GNUNET_TIME_Absolute timeout; | ||
216 | |||
217 | /** | ||
218 | * Session timeout task. | ||
219 | */ | ||
220 | struct GNUNET_SCHEDULER_Task *timeout_task; | ||
221 | |||
222 | /** | ||
223 | * Number of messages we currently have in our write queue. | ||
224 | */ | ||
225 | unsigned int msgs_in_queue; | ||
226 | }; | ||
227 | |||
228 | |||
229 | /** | ||
230 | * Encapsulation of all of the state of the plugin. | ||
231 | */ | ||
232 | struct Plugin; | ||
233 | |||
234 | |||
235 | /** | ||
236 | * Information we keep for each of our listen sockets. | ||
237 | */ | ||
238 | struct UNIX_Sock_Info | ||
239 | { | ||
240 | /** | ||
241 | * The network handle | ||
242 | */ | ||
243 | struct GNUNET_NETWORK_Handle *desc; | ||
244 | }; | ||
245 | |||
246 | |||
247 | /** | ||
248 | * Encapsulation of all of the state of the plugin. | ||
249 | */ | ||
250 | struct Plugin | ||
251 | { | ||
252 | /** | ||
253 | * ID of task used to update our addresses when one expires. | ||
254 | */ | ||
255 | struct GNUNET_SCHEDULER_Task *address_update_task; | ||
256 | |||
257 | /** | ||
258 | * ID of read task | ||
259 | */ | ||
260 | struct GNUNET_SCHEDULER_Task *read_task; | ||
261 | |||
262 | /** | ||
263 | * ID of write task | ||
264 | */ | ||
265 | struct GNUNET_SCHEDULER_Task *write_task; | ||
266 | |||
267 | /** | ||
268 | * Number of bytes we currently have in our write queues. | ||
269 | */ | ||
270 | unsigned long long bytes_in_queue; | ||
271 | |||
272 | /** | ||
273 | * Our environment. | ||
274 | */ | ||
275 | struct GNUNET_TRANSPORT_PluginEnvironment *env; | ||
276 | |||
277 | /** | ||
278 | * Sessions (map from peer identity to `struct GNUNET_ATS_Session`) | ||
279 | */ | ||
280 | struct GNUNET_CONTAINER_MultiPeerMap *session_map; | ||
281 | |||
282 | /** | ||
283 | * Head of queue of messages to transmit. | ||
284 | */ | ||
285 | struct UNIXMessageWrapper *msg_head; | ||
286 | |||
287 | /** | ||
288 | * Tail of queue of messages to transmit. | ||
289 | */ | ||
290 | struct UNIXMessageWrapper *msg_tail; | ||
291 | |||
292 | /** | ||
293 | * Path of our unix domain socket (/tmp/unix-plugin) | ||
294 | */ | ||
295 | char *unix_socket_path; | ||
296 | |||
297 | /** | ||
298 | * Function to call about session status changes. | ||
299 | */ | ||
300 | GNUNET_TRANSPORT_SessionInfoCallback sic; | ||
301 | |||
302 | /** | ||
303 | * Closure for @e sic. | ||
304 | */ | ||
305 | void *sic_cls; | ||
306 | |||
307 | /** | ||
308 | * socket that we transmit all data with | ||
309 | */ | ||
310 | struct UNIX_Sock_Info unix_sock; | ||
311 | |||
312 | /** | ||
313 | * Address options in HBO | ||
314 | */ | ||
315 | uint32_t myoptions; | ||
316 | |||
317 | /** | ||
318 | * Are we using an abstract UNIX domain socket? | ||
319 | */ | ||
320 | int is_abstract; | ||
321 | }; | ||
322 | |||
323 | |||
324 | /** | ||
325 | * If a session monitor is attached, notify it about the new | ||
326 | * session state. | ||
327 | * | ||
328 | * @param plugin our plugin | ||
329 | * @param session session that changed state | ||
330 | * @param state new state of the session | ||
331 | */ | ||
332 | static void | ||
333 | notify_session_monitor (struct Plugin *plugin, | ||
334 | struct GNUNET_ATS_Session *session, | ||
335 | enum GNUNET_TRANSPORT_SessionState state) | ||
336 | { | ||
337 | struct GNUNET_TRANSPORT_SessionInfo info; | ||
338 | |||
339 | if (NULL == plugin->sic) | ||
340 | return; | ||
341 | memset (&info, 0, sizeof(info)); | ||
342 | info.state = state; | ||
343 | info.is_inbound = GNUNET_SYSERR; /* hard to say */ | ||
344 | info.num_msg_pending = session->msgs_in_queue; | ||
345 | info.num_bytes_pending = session->bytes_in_queue; | ||
346 | /* info.receive_delay remains zero as this is not supported by UNIX | ||
347 | (cannot selectively not receive from 'some' peer while continuing | ||
348 | to receive from others) */ | ||
349 | info.session_timeout = session->timeout; | ||
350 | info.address = session->address; | ||
351 | plugin->sic (plugin->sic_cls, session, &info); | ||
352 | } | ||
353 | |||
354 | |||
355 | /** | ||
356 | * Function called for a quick conversion of the binary address to | ||
357 | * a numeric address. Note that the caller must not free the | ||
358 | * address and that the next call to this function is allowed | ||
359 | * to override the address again. | ||
360 | * | ||
361 | * @param cls closure | ||
362 | * @param addr binary address | ||
363 | * @param addrlen length of the @a addr | ||
364 | * @return string representing the same address | ||
365 | */ | ||
366 | static const char * | ||
367 | unix_plugin_address_to_string (void *cls, const void *addr, size_t addrlen) | ||
368 | { | ||
369 | static char rbuf[1024]; | ||
370 | struct UnixAddress *ua = (struct UnixAddress *) addr; | ||
371 | char *addrstr; | ||
372 | size_t addr_str_len; | ||
373 | unsigned int off; | ||
374 | |||
375 | if ((NULL == addr) || (sizeof(struct UnixAddress) > addrlen)) | ||
376 | { | ||
377 | GNUNET_break (0); | ||
378 | return NULL; | ||
379 | } | ||
380 | addrstr = (char *) &ua[1]; | ||
381 | addr_str_len = ntohl (ua->addrlen); | ||
382 | |||
383 | if (addr_str_len != addrlen - sizeof(struct UnixAddress)) | ||
384 | { | ||
385 | GNUNET_break (0); | ||
386 | return NULL; | ||
387 | } | ||
388 | if ('\0' != addrstr[addr_str_len - 1]) | ||
389 | { | ||
390 | GNUNET_break (0); | ||
391 | return NULL; | ||
392 | } | ||
393 | if (strlen (addrstr) + 1 != addr_str_len) | ||
394 | { | ||
395 | GNUNET_break (0); | ||
396 | return NULL; | ||
397 | } | ||
398 | |||
399 | off = 0; | ||
400 | if ('\0' == addrstr[0]) | ||
401 | off++; | ||
402 | memset (rbuf, 0, sizeof(rbuf)); | ||
403 | GNUNET_snprintf (rbuf, | ||
404 | sizeof(rbuf) - 1, | ||
405 | "%s.%u.%s%.*s", | ||
406 | PLUGIN_NAME, | ||
407 | ntohl (ua->options), | ||
408 | (off == 1) ? "@" : "", | ||
409 | (int) (addr_str_len - off), | ||
410 | &addrstr[off]); | ||
411 | return rbuf; | ||
412 | } | ||
413 | |||
414 | |||
415 | /** | ||
416 | * Functions with this signature are called whenever we need | ||
417 | * to close a session due to a disconnect or failure to | ||
418 | * establish a connection. | ||
419 | * | ||
420 | * @param cls closure with the `struct Plugin *` | ||
421 | * @param session session to close down | ||
422 | * @return #GNUNET_OK on success | ||
423 | */ | ||
424 | static int | ||
425 | unix_plugin_session_disconnect (void *cls, struct GNUNET_ATS_Session *session) | ||
426 | { | ||
427 | struct Plugin *plugin = cls; | ||
428 | struct UNIXMessageWrapper *msgw; | ||
429 | struct UNIXMessageWrapper *next; | ||
430 | |||
431 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
432 | "Disconnecting session for peer `%s' `%s'\n", | ||
433 | GNUNET_i2s (&session->target), | ||
434 | unix_plugin_address_to_string (NULL, | ||
435 | session->address->address, | ||
436 | session->address->address_length)); | ||
437 | plugin->env->session_end (plugin->env->cls, session->address, session); | ||
438 | next = plugin->msg_head; | ||
439 | while (NULL != next) | ||
440 | { | ||
441 | msgw = next; | ||
442 | next = msgw->next; | ||
443 | if (msgw->session != session) | ||
444 | continue; | ||
445 | GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw); | ||
446 | session->msgs_in_queue--; | ||
447 | GNUNET_assert (session->bytes_in_queue >= msgw->msgsize); | ||
448 | session->bytes_in_queue -= msgw->msgsize; | ||
449 | GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize); | ||
450 | plugin->bytes_in_queue -= msgw->msgsize; | ||
451 | if (NULL != msgw->cont) | ||
452 | msgw->cont (msgw->cont_cls, | ||
453 | &msgw->session->target, | ||
454 | GNUNET_SYSERR, | ||
455 | msgw->payload, | ||
456 | 0); | ||
457 | GNUNET_free (msgw->msg); | ||
458 | GNUNET_free (msgw); | ||
459 | } | ||
460 | GNUNET_assert (GNUNET_YES == | ||
461 | GNUNET_CONTAINER_multipeermap_remove (plugin->session_map, | ||
462 | &session->target, | ||
463 | session)); | ||
464 | GNUNET_STATISTICS_set (plugin->env->stats, | ||
465 | "# UNIX sessions active", | ||
466 | GNUNET_CONTAINER_multipeermap_size ( | ||
467 | plugin->session_map), | ||
468 | GNUNET_NO); | ||
469 | if (NULL != session->timeout_task) | ||
470 | { | ||
471 | GNUNET_SCHEDULER_cancel (session->timeout_task); | ||
472 | session->timeout_task = NULL; | ||
473 | session->timeout = GNUNET_TIME_UNIT_ZERO_ABS; | ||
474 | } | ||
475 | notify_session_monitor (plugin, session, GNUNET_TRANSPORT_SS_DONE); | ||
476 | GNUNET_HELLO_address_free (session->address); | ||
477 | GNUNET_break (0 == session->bytes_in_queue); | ||
478 | GNUNET_break (0 == session->msgs_in_queue); | ||
479 | GNUNET_free (session); | ||
480 | return GNUNET_OK; | ||
481 | } | ||
482 | |||
483 | |||
484 | /** | ||
485 | * Session was idle for too long, so disconnect it | ||
486 | * | ||
487 | * @param cls the `struct GNUNET_ATS_Session *` to disconnect | ||
488 | */ | ||
489 | static void | ||
490 | session_timeout (void *cls) | ||
491 | { | ||
492 | struct GNUNET_ATS_Session *session = cls; | ||
493 | struct GNUNET_TIME_Relative left; | ||
494 | |||
495 | session->timeout_task = NULL; | ||
496 | left = GNUNET_TIME_absolute_get_remaining (session->timeout); | ||
497 | if (0 != left.rel_value_us) | ||
498 | { | ||
499 | /* not actually our turn yet, but let's at least update | ||
500 | the monitor, it may think we're about to die ... */ | ||
501 | notify_session_monitor (session->plugin, | ||
502 | session, | ||
503 | GNUNET_TRANSPORT_SS_UPDATE); | ||
504 | session->timeout_task = | ||
505 | GNUNET_SCHEDULER_add_delayed (left, &session_timeout, session); | ||
506 | return; | ||
507 | } | ||
508 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
509 | "Session %p was idle for %s, disconnecting\n", | ||
510 | session, | ||
511 | GNUNET_STRINGS_relative_time_to_string ( | ||
512 | GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, | ||
513 | GNUNET_YES)); | ||
514 | unix_plugin_session_disconnect (session->plugin, session); | ||
515 | } | ||
516 | |||
517 | |||
518 | /** | ||
519 | * Increment session timeout due to activity. We do not immediately | ||
520 | * notify the monitor here as that might generate excessive | ||
521 | * signalling. | ||
522 | * | ||
523 | * @param session session for which the timeout should be rescheduled | ||
524 | */ | ||
525 | static void | ||
526 | reschedule_session_timeout (struct GNUNET_ATS_Session *session) | ||
527 | { | ||
528 | GNUNET_assert (NULL != session->timeout_task); | ||
529 | session->timeout = | ||
530 | GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT); | ||
531 | } | ||
532 | |||
533 | |||
534 | /** | ||
535 | * Convert unix path to a `struct sockaddr_un *` | ||
536 | * | ||
537 | * @param unixpath path to convert | ||
538 | * @param[out] sock_len set to the length of the address | ||
539 | * @return converted unix path | ||
540 | */ | ||
541 | static struct sockaddr_un * | ||
542 | unix_address_to_sockaddr (const char *unixpath, socklen_t *sock_len) | ||
543 | { | ||
544 | struct sockaddr_un *un; | ||
545 | size_t slen; | ||
546 | |||
547 | GNUNET_assert (0 < strlen (unixpath)); /* sanity check */ | ||
548 | un = GNUNET_new (struct sockaddr_un); | ||
549 | un->sun_family = AF_UNIX; | ||
550 | slen = strlen (unixpath); | ||
551 | if (slen >= sizeof(un->sun_path)) | ||
552 | slen = sizeof(un->sun_path) - 1; | ||
553 | GNUNET_memcpy (un->sun_path, unixpath, slen); | ||
554 | un->sun_path[slen] = '\0'; | ||
555 | slen = sizeof(struct sockaddr_un); | ||
556 | #if HAVE_SOCKADDR_UN_SUN_LEN | ||
557 | un->sun_len = (u_char) slen; | ||
558 | #endif | ||
559 | (*sock_len) = slen; | ||
560 | return un; | ||
561 | } | ||
562 | |||
563 | |||
564 | /** | ||
565 | * Closure to #lookup_session_it(). | ||
566 | */ | ||
567 | struct LookupCtx | ||
568 | { | ||
569 | /** | ||
570 | * Location to store the session, if found. | ||
571 | */ | ||
572 | struct GNUNET_ATS_Session *res; | ||
573 | |||
574 | /** | ||
575 | * Address we are looking for. | ||
576 | */ | ||
577 | const struct GNUNET_HELLO_Address *address; | ||
578 | }; | ||
579 | |||
580 | |||
581 | /** | ||
582 | * Function called to find a session by address. | ||
583 | * | ||
584 | * @param cls the `struct LookupCtx *` | ||
585 | * @param key peer we are looking for (unused) | ||
586 | * @param value a session | ||
587 | * @return #GNUNET_YES if not found (continue looking), #GNUNET_NO on success | ||
588 | */ | ||
589 | static int | ||
590 | lookup_session_it (void *cls, | ||
591 | const struct GNUNET_PeerIdentity *key, | ||
592 | void *value) | ||
593 | { | ||
594 | struct LookupCtx *lctx = cls; | ||
595 | struct GNUNET_ATS_Session *session = value; | ||
596 | |||
597 | if (0 == GNUNET_HELLO_address_cmp (lctx->address, session->address)) | ||
598 | { | ||
599 | lctx->res = session; | ||
600 | return GNUNET_NO; | ||
601 | } | ||
602 | return GNUNET_YES; | ||
603 | } | ||
604 | |||
605 | |||
606 | /** | ||
607 | * Find an existing session by address. | ||
608 | * | ||
609 | * @param plugin the plugin | ||
610 | * @param address the address to find | ||
611 | * @return NULL if session was not found | ||
612 | */ | ||
613 | static struct GNUNET_ATS_Session * | ||
614 | lookup_session (struct Plugin *plugin, | ||
615 | const struct GNUNET_HELLO_Address *address) | ||
616 | { | ||
617 | struct LookupCtx lctx; | ||
618 | |||
619 | lctx.address = address; | ||
620 | lctx.res = NULL; | ||
621 | GNUNET_CONTAINER_multipeermap_get_multiple (plugin->session_map, | ||
622 | &address->peer, | ||
623 | &lookup_session_it, | ||
624 | &lctx); | ||
625 | return lctx.res; | ||
626 | } | ||
627 | |||
628 | |||
629 | /** | ||
630 | * Function that is called to get the keepalive factor. | ||
631 | * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to | ||
632 | * calculate the interval between keepalive packets. | ||
633 | * | ||
634 | * @param cls closure with the `struct Plugin` | ||
635 | * @return keepalive factor | ||
636 | */ | ||
637 | static unsigned int | ||
638 | unix_plugin_query_keepalive_factor (void *cls) | ||
639 | { | ||
640 | return 3; | ||
641 | } | ||
642 | |||
643 | |||
644 | /** | ||
645 | * Actually send out the message, assume we've got the address and | ||
646 | * send_handle squared away! | ||
647 | * | ||
648 | * @param cls closure | ||
649 | * @param send_handle which handle to send message on | ||
650 | * @param target who should receive this message (ignored by UNIX) | ||
651 | * @param msgbuf one or more GNUNET_MessageHeader(s) strung together | ||
652 | * @param msgbuf_size the size of the @a msgbuf to send | ||
653 | * @param priority how important is the message (ignored by UNIX) | ||
654 | * @param timeout when should we time out (give up) if we can not transmit? | ||
655 | * @param addr the addr to send the message to, needs to be a sockaddr for us | ||
656 | * @param addrlen the len of @a addr | ||
657 | * @param payload bytes payload to send | ||
658 | * @param cont continuation to call once the message has | ||
659 | * been transmitted (or if the transport is ready | ||
660 | * for the next transmission call; or if the | ||
661 | * peer disconnected...) | ||
662 | * @param cont_cls closure for @a cont | ||
663 | * @return on success the number of bytes written, RETRY for retry, -1 on errors | ||
664 | */ | ||
665 | static ssize_t | ||
666 | unix_real_send (void *cls, | ||
667 | struct GNUNET_NETWORK_Handle *send_handle, | ||
668 | const struct GNUNET_PeerIdentity *target, | ||
669 | const char *msgbuf, | ||
670 | size_t msgbuf_size, | ||
671 | unsigned int priority, | ||
672 | struct GNUNET_TIME_Absolute timeout, | ||
673 | const struct UnixAddress *addr, | ||
674 | size_t addrlen, | ||
675 | size_t payload, | ||
676 | GNUNET_TRANSPORT_TransmitContinuation cont, | ||
677 | void *cont_cls) | ||
678 | { | ||
679 | struct Plugin *plugin = cls; | ||
680 | ssize_t sent; | ||
681 | struct sockaddr_un *un; | ||
682 | socklen_t un_len; | ||
683 | const char *unixpath; | ||
684 | |||
685 | if (NULL == send_handle) | ||
686 | { | ||
687 | GNUNET_break (0); /* We do not have a send handle */ | ||
688 | return GNUNET_SYSERR; | ||
689 | } | ||
690 | if ((NULL == addr) || (0 == addrlen)) | ||
691 | { | ||
692 | GNUNET_break (0); /* Can never send if we don't have an address */ | ||
693 | return GNUNET_SYSERR; | ||
694 | } | ||
695 | |||
696 | /* Prepare address */ | ||
697 | unixpath = (const char *) &addr[1]; | ||
698 | if (NULL == (un = unix_address_to_sockaddr (unixpath, &un_len))) | ||
699 | { | ||
700 | GNUNET_break (0); | ||
701 | return -1; | ||
702 | } | ||
703 | |||
704 | if ((GNUNET_YES == plugin->is_abstract) && | ||
705 | (0 != (UNIX_OPTIONS_USE_ABSTRACT_SOCKETS & ntohl (addr->options)))) | ||
706 | { | ||
707 | un->sun_path[0] = '\0'; | ||
708 | } | ||
709 | resend: | ||
710 | /* Send the data */ | ||
711 | sent = GNUNET_NETWORK_socket_sendto (send_handle, | ||
712 | msgbuf, | ||
713 | msgbuf_size, | ||
714 | (const struct sockaddr *) un, | ||
715 | un_len); | ||
716 | if (GNUNET_SYSERR == sent) | ||
717 | { | ||
718 | if ((EAGAIN == errno) || (ENOBUFS == errno)) | ||
719 | { | ||
720 | GNUNET_free (un); | ||
721 | return RETRY; /* We have to retry later */ | ||
722 | } | ||
723 | if (EMSGSIZE == errno) | ||
724 | { | ||
725 | socklen_t size = 0; | ||
726 | socklen_t len = sizeof(size); | ||
727 | |||
728 | GNUNET_NETWORK_socket_getsockopt ((struct GNUNET_NETWORK_Handle *) | ||
729 | send_handle, | ||
730 | SOL_SOCKET, | ||
731 | SO_SNDBUF, | ||
732 | &size, | ||
733 | &len); | ||
734 | if (size < msgbuf_size) | ||
735 | { | ||
736 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
737 | "Trying to increase socket buffer size from %u to %u for message size %u\n", | ||
738 | (unsigned int) size, | ||
739 | (unsigned int) ((msgbuf_size / 1000) + 2) * 1000, | ||
740 | (unsigned int) msgbuf_size); | ||
741 | size = ((msgbuf_size / 1000) + 2) * 1000; | ||
742 | if (GNUNET_OK == | ||
743 | GNUNET_NETWORK_socket_setsockopt ((struct GNUNET_NETWORK_Handle *) | ||
744 | send_handle, | ||
745 | SOL_SOCKET, | ||
746 | SO_SNDBUF, | ||
747 | &size, | ||
748 | sizeof(size))) | ||
749 | goto resend; /* Increased buffer size, retry sending */ | ||
750 | else | ||
751 | { | ||
752 | /* Could not increase buffer size: error, no retry */ | ||
753 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt"); | ||
754 | GNUNET_free (un); | ||
755 | return GNUNET_SYSERR; | ||
756 | } | ||
757 | } | ||
758 | else | ||
759 | { | ||
760 | /* Buffer is bigger than message: error, no retry | ||
761 | * This should never happen!*/ | ||
762 | GNUNET_break (0); | ||
763 | GNUNET_free (un); | ||
764 | return GNUNET_SYSERR; | ||
765 | } | ||
766 | } | ||
767 | } | ||
768 | |||
769 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
770 | "UNIX transmitted %u-byte message to %s (%d: %s)\n", | ||
771 | (unsigned int) msgbuf_size, | ||
772 | GNUNET_a2s ((const struct sockaddr *) un, un_len), | ||
773 | (int) sent, | ||
774 | (sent < 0) ? strerror (errno) : "ok"); | ||
775 | GNUNET_free (un); | ||
776 | return sent; | ||
777 | } | ||
778 | |||
779 | |||
780 | /** | ||
781 | * Function obtain the network type for a session | ||
782 | * | ||
783 | * @param cls closure ('struct Plugin*') | ||
784 | * @param session the session | ||
785 | * @return the network type in HBO or #GNUNET_SYSERR | ||
786 | */ | ||
787 | static enum GNUNET_NetworkType | ||
788 | unix_plugin_get_network (void *cls, struct GNUNET_ATS_Session *session) | ||
789 | { | ||
790 | GNUNET_assert (NULL != session); | ||
791 | return GNUNET_NT_LOOPBACK; | ||
792 | } | ||
793 | |||
794 | |||
795 | /** | ||
796 | * Function obtain the network type for a session | ||
797 | * | ||
798 | * @param cls closure (`struct Plugin *`) | ||
799 | * @param address the address | ||
800 | * @return the network type | ||
801 | */ | ||
802 | static enum GNUNET_NetworkType | ||
803 | unix_plugin_get_network_for_address (void *cls, | ||
804 | const struct GNUNET_HELLO_Address *address) | ||
805 | { | ||
806 | return GNUNET_NT_LOOPBACK; | ||
807 | } | ||
808 | |||
809 | |||
810 | /** | ||
811 | * Creates a new outbound session the transport service will use to send data to the | ||
812 | * peer | ||
813 | * | ||
814 | * @param cls the plugin | ||
815 | * @param address the address | ||
816 | * @return the session or NULL of max connections exceeded | ||
817 | */ | ||
818 | static struct GNUNET_ATS_Session * | ||
819 | unix_plugin_get_session (void *cls, const struct GNUNET_HELLO_Address *address) | ||
820 | { | ||
821 | struct Plugin *plugin = cls; | ||
822 | struct GNUNET_ATS_Session *session; | ||
823 | struct UnixAddress *ua; | ||
824 | char *addrstr; | ||
825 | uint32_t addr_str_len; | ||
826 | uint32_t addr_option; | ||
827 | |||
828 | ua = (struct UnixAddress *) address->address; | ||
829 | if ((NULL == address->address) || (0 == address->address_length) || | ||
830 | (sizeof(struct UnixAddress) > address->address_length)) | ||
831 | { | ||
832 | GNUNET_break (0); | ||
833 | return NULL; | ||
834 | } | ||
835 | addrstr = (char *) &ua[1]; | ||
836 | addr_str_len = ntohl (ua->addrlen); | ||
837 | addr_option = ntohl (ua->options); | ||
838 | |||
839 | if ((0 != (UNIX_OPTIONS_USE_ABSTRACT_SOCKETS & addr_option)) && | ||
840 | (GNUNET_NO == plugin->is_abstract)) | ||
841 | { | ||
842 | return NULL; | ||
843 | } | ||
844 | |||
845 | if (addr_str_len != address->address_length - sizeof(struct UnixAddress)) | ||
846 | { | ||
847 | return NULL; /* This can be a legacy address */ | ||
848 | } | ||
849 | |||
850 | if ('\0' != addrstr[addr_str_len - 1]) | ||
851 | { | ||
852 | GNUNET_break (0); | ||
853 | return NULL; | ||
854 | } | ||
855 | if (strlen (addrstr) + 1 != addr_str_len) | ||
856 | { | ||
857 | GNUNET_break (0); | ||
858 | return NULL; | ||
859 | } | ||
860 | |||
861 | /* Check if a session for this address already exists */ | ||
862 | if (NULL != (session = lookup_session (plugin, address))) | ||
863 | { | ||
864 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
865 | "Found existing session %p for address `%s'\n", | ||
866 | session, | ||
867 | unix_plugin_address_to_string (NULL, | ||
868 | address->address, | ||
869 | address->address_length)); | ||
870 | return session; | ||
871 | } | ||
872 | |||
873 | /* create a new session */ | ||
874 | session = GNUNET_new (struct GNUNET_ATS_Session); | ||
875 | session->target = address->peer; | ||
876 | session->address = GNUNET_HELLO_address_copy (address); | ||
877 | session->plugin = plugin; | ||
878 | session->timeout = | ||
879 | GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT); | ||
880 | session->timeout_task = | ||
881 | GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, | ||
882 | &session_timeout, | ||
883 | session); | ||
884 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
885 | "Creating a new session %p for address `%s'\n", | ||
886 | session, | ||
887 | unix_plugin_address_to_string (NULL, | ||
888 | address->address, | ||
889 | address->address_length)); | ||
890 | (void) GNUNET_CONTAINER_multipeermap_put ( | ||
891 | plugin->session_map, | ||
892 | &address->peer, | ||
893 | session, | ||
894 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); | ||
895 | GNUNET_STATISTICS_set (plugin->env->stats, | ||
896 | "# UNIX sessions active", | ||
897 | GNUNET_CONTAINER_multipeermap_size ( | ||
898 | plugin->session_map), | ||
899 | GNUNET_NO); | ||
900 | notify_session_monitor (plugin, session, GNUNET_TRANSPORT_SS_INIT); | ||
901 | notify_session_monitor (plugin, session, GNUNET_TRANSPORT_SS_UP); | ||
902 | return session; | ||
903 | } | ||
904 | |||
905 | |||
906 | /** | ||
907 | * Function that will be called whenever the transport service wants | ||
908 | * to notify the plugin that a session is still active and in use and | ||
909 | * therefore the session timeout for this session has to be updated | ||
910 | * | ||
911 | * @param cls closure with the `struct Plugin *` | ||
912 | * @param peer which peer was the session for | ||
913 | * @param session which session is being updated | ||
914 | */ | ||
915 | static void | ||
916 | unix_plugin_update_session_timeout (void *cls, | ||
917 | const struct GNUNET_PeerIdentity *peer, | ||
918 | struct GNUNET_ATS_Session *session) | ||
919 | { | ||
920 | struct Plugin *plugin = cls; | ||
921 | |||
922 | if (GNUNET_OK != | ||
923 | GNUNET_CONTAINER_multipeermap_contains_value (plugin->session_map, | ||
924 | &session->target, | ||
925 | session)) | ||
926 | { | ||
927 | GNUNET_break (0); | ||
928 | return; | ||
929 | } | ||
930 | reschedule_session_timeout (session); | ||
931 | } | ||
932 | |||
933 | |||
934 | /** | ||
935 | * Demultiplexer for UNIX messages | ||
936 | * | ||
937 | * @param plugin the main plugin for this transport | ||
938 | * @param sender from which peer the message was received | ||
939 | * @param currhdr pointer to the header of the message | ||
940 | * @param ua address to look for | ||
941 | * @param ua_len length of the address @a ua | ||
942 | */ | ||
943 | static void | ||
944 | unix_demultiplexer (struct Plugin *plugin, | ||
945 | struct GNUNET_PeerIdentity *sender, | ||
946 | const struct GNUNET_MessageHeader *currhdr, | ||
947 | const struct UnixAddress *ua, | ||
948 | size_t ua_len) | ||
949 | { | ||
950 | struct GNUNET_ATS_Session *session; | ||
951 | struct GNUNET_HELLO_Address *address; | ||
952 | |||
953 | GNUNET_assert (ua_len >= sizeof(struct UnixAddress)); | ||
954 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
955 | "Received message from %s\n", | ||
956 | unix_plugin_address_to_string (NULL, ua, ua_len)); | ||
957 | GNUNET_STATISTICS_update (plugin->env->stats, | ||
958 | "# bytes received via UNIX", | ||
959 | ntohs (currhdr->size), | ||
960 | GNUNET_NO); | ||
961 | |||
962 | /* Look for existing session */ | ||
963 | address = GNUNET_HELLO_address_allocate ( | ||
964 | sender, | ||
965 | PLUGIN_NAME, | ||
966 | ua, | ||
967 | ua_len, | ||
968 | GNUNET_HELLO_ADDRESS_INFO_NONE); /* UNIX does not have "inbound" sessions */ | ||
969 | session = lookup_session (plugin, address); | ||
970 | if (NULL == session) | ||
971 | { | ||
972 | session = unix_plugin_get_session (plugin, address); | ||
973 | /* Notify transport and ATS about new inbound session */ | ||
974 | plugin->env->session_start (NULL, | ||
975 | session->address, | ||
976 | session, | ||
977 | GNUNET_NT_LOOPBACK); | ||
978 | } | ||
979 | else | ||
980 | { | ||
981 | reschedule_session_timeout (session); | ||
982 | } | ||
983 | GNUNET_HELLO_address_free (address); | ||
984 | plugin->env->receive (plugin->env->cls, session->address, session, currhdr); | ||
985 | } | ||
986 | |||
987 | |||
988 | /** | ||
989 | * Read from UNIX domain socket (it is ready). | ||
990 | * | ||
991 | * @param plugin the plugin | ||
992 | */ | ||
993 | static void | ||
994 | unix_plugin_do_read (struct Plugin *plugin) | ||
995 | { | ||
996 | char buf[65536] GNUNET_ALIGN; | ||
997 | struct UnixAddress *ua; | ||
998 | struct UNIXMessage *msg; | ||
999 | struct GNUNET_PeerIdentity sender; | ||
1000 | struct sockaddr_un un; | ||
1001 | socklen_t addrlen; | ||
1002 | ssize_t ret; | ||
1003 | int offset; | ||
1004 | int tsize; | ||
1005 | int is_abstract; | ||
1006 | char *msgbuf; | ||
1007 | const struct GNUNET_MessageHeader *currhdr; | ||
1008 | uint16_t csize; | ||
1009 | size_t ua_len; | ||
1010 | |||
1011 | addrlen = sizeof(un); | ||
1012 | memset (&un, 0, sizeof(un)); | ||
1013 | ret = GNUNET_NETWORK_socket_recvfrom (plugin->unix_sock.desc, | ||
1014 | buf, | ||
1015 | sizeof(buf), | ||
1016 | (struct sockaddr *) &un, | ||
1017 | &addrlen); | ||
1018 | if ((GNUNET_SYSERR == ret) && ((errno == EAGAIN) || (errno == ENOBUFS))) | ||
1019 | return; | ||
1020 | if (GNUNET_SYSERR == ret) | ||
1021 | { | ||
1022 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom"); | ||
1023 | return; | ||
1024 | } | ||
1025 | else | ||
1026 | { | ||
1027 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1028 | "Read %d bytes from socket %s\n", | ||
1029 | (int) ret, | ||
1030 | un.sun_path); | ||
1031 | } | ||
1032 | |||
1033 | GNUNET_assert (AF_UNIX == (un.sun_family)); | ||
1034 | is_abstract = GNUNET_NO; | ||
1035 | if ('\0' == un.sun_path[0]) | ||
1036 | { | ||
1037 | un.sun_path[0] = '@'; | ||
1038 | is_abstract = GNUNET_YES; | ||
1039 | } | ||
1040 | |||
1041 | ua_len = sizeof(struct UnixAddress) + strlen (un.sun_path) + 1; | ||
1042 | ua = GNUNET_malloc (ua_len); | ||
1043 | ua->addrlen = htonl (strlen (&un.sun_path[0]) + 1); | ||
1044 | GNUNET_memcpy (&ua[1], &un.sun_path[0], strlen (un.sun_path) + 1); | ||
1045 | if (is_abstract) | ||
1046 | ua->options = htonl (UNIX_OPTIONS_USE_ABSTRACT_SOCKETS); | ||
1047 | else | ||
1048 | ua->options = htonl (UNIX_OPTIONS_NONE); | ||
1049 | |||
1050 | msg = (struct UNIXMessage *) buf; | ||
1051 | csize = ntohs (msg->header.size); | ||
1052 | if ((csize < sizeof(struct UNIXMessage)) || (csize > ret)) | ||
1053 | { | ||
1054 | GNUNET_break_op (0); | ||
1055 | GNUNET_free (ua); | ||
1056 | return; | ||
1057 | } | ||
1058 | msgbuf = (char *) &msg[1]; | ||
1059 | GNUNET_memcpy (&sender, &msg->sender, sizeof(struct GNUNET_PeerIdentity)); | ||
1060 | offset = 0; | ||
1061 | tsize = csize - sizeof(struct UNIXMessage); | ||
1062 | while (offset + sizeof(struct GNUNET_MessageHeader) <= tsize) | ||
1063 | { | ||
1064 | currhdr = (struct GNUNET_MessageHeader *) &msgbuf[offset]; | ||
1065 | csize = ntohs (currhdr->size); | ||
1066 | if ((csize < sizeof(struct GNUNET_MessageHeader)) || | ||
1067 | (csize > tsize - offset)) | ||
1068 | { | ||
1069 | GNUNET_break_op (0); | ||
1070 | break; | ||
1071 | } | ||
1072 | unix_demultiplexer (plugin, &sender, currhdr, ua, ua_len); | ||
1073 | offset += csize; | ||
1074 | } | ||
1075 | GNUNET_free (ua); | ||
1076 | } | ||
1077 | |||
1078 | |||
1079 | /** | ||
1080 | * Write to UNIX domain socket (it is ready). | ||
1081 | * | ||
1082 | * @param plugin handle to the plugin | ||
1083 | */ | ||
1084 | static void | ||
1085 | unix_plugin_do_write (struct Plugin *plugin) | ||
1086 | { | ||
1087 | ssize_t sent = 0; | ||
1088 | struct UNIXMessageWrapper *msgw; | ||
1089 | struct GNUNET_ATS_Session *session; | ||
1090 | int did_delete; | ||
1091 | |||
1092 | session = NULL; | ||
1093 | did_delete = GNUNET_NO; | ||
1094 | while (NULL != (msgw = plugin->msg_head)) | ||
1095 | { | ||
1096 | if (GNUNET_TIME_absolute_get_remaining (msgw->timeout).rel_value_us > 0) | ||
1097 | break; /* Message is ready for sending */ | ||
1098 | /* Message has a timeout */ | ||
1099 | did_delete = GNUNET_YES; | ||
1100 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1101 | "Timeout for message with %u bytes \n", | ||
1102 | (unsigned int) msgw->msgsize); | ||
1103 | GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw); | ||
1104 | session = msgw->session; | ||
1105 | session->msgs_in_queue--; | ||
1106 | GNUNET_assert (session->bytes_in_queue >= msgw->msgsize); | ||
1107 | session->bytes_in_queue -= msgw->msgsize; | ||
1108 | GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize); | ||
1109 | plugin->bytes_in_queue -= msgw->msgsize; | ||
1110 | GNUNET_STATISTICS_set (plugin->env->stats, | ||
1111 | "# bytes currently in UNIX buffers", | ||
1112 | plugin->bytes_in_queue, | ||
1113 | GNUNET_NO); | ||
1114 | GNUNET_STATISTICS_update (plugin->env->stats, | ||
1115 | "# UNIX bytes discarded", | ||
1116 | msgw->msgsize, | ||
1117 | GNUNET_NO); | ||
1118 | if (NULL != msgw->cont) | ||
1119 | msgw->cont (msgw->cont_cls, | ||
1120 | &msgw->session->target, | ||
1121 | GNUNET_SYSERR, | ||
1122 | msgw->payload, | ||
1123 | 0); | ||
1124 | GNUNET_free (msgw->msg); | ||
1125 | GNUNET_free (msgw); | ||
1126 | } | ||
1127 | if (NULL == msgw) | ||
1128 | { | ||
1129 | if (GNUNET_YES == did_delete) | ||
1130 | notify_session_monitor (plugin, session, GNUNET_TRANSPORT_SS_UPDATE); | ||
1131 | return; /* Nothing to send at the moment */ | ||
1132 | } | ||
1133 | session = msgw->session; | ||
1134 | sent = unix_real_send (plugin, | ||
1135 | plugin->unix_sock.desc, | ||
1136 | &session->target, | ||
1137 | (const char *) msgw->msg, | ||
1138 | msgw->msgsize, | ||
1139 | msgw->priority, | ||
1140 | msgw->timeout, | ||
1141 | msgw->session->address->address, | ||
1142 | msgw->session->address->address_length, | ||
1143 | msgw->payload, | ||
1144 | msgw->cont, | ||
1145 | msgw->cont_cls); | ||
1146 | if (RETRY == sent) | ||
1147 | { | ||
1148 | GNUNET_STATISTICS_update (plugin->env->stats, | ||
1149 | "# UNIX retry attempts", | ||
1150 | 1, | ||
1151 | GNUNET_NO); | ||
1152 | notify_session_monitor (plugin, session, GNUNET_TRANSPORT_SS_UPDATE); | ||
1153 | return; | ||
1154 | } | ||
1155 | GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw); | ||
1156 | session->msgs_in_queue--; | ||
1157 | GNUNET_assert (session->bytes_in_queue >= msgw->msgsize); | ||
1158 | session->bytes_in_queue -= msgw->msgsize; | ||
1159 | GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize); | ||
1160 | plugin->bytes_in_queue -= msgw->msgsize; | ||
1161 | GNUNET_STATISTICS_set (plugin->env->stats, | ||
1162 | "# bytes currently in UNIX buffers", | ||
1163 | plugin->bytes_in_queue, | ||
1164 | GNUNET_NO); | ||
1165 | notify_session_monitor (plugin, session, GNUNET_TRANSPORT_SS_UPDATE); | ||
1166 | if (GNUNET_SYSERR == sent) | ||
1167 | { | ||
1168 | /* failed and no retry */ | ||
1169 | if (NULL != msgw->cont) | ||
1170 | msgw->cont (msgw->cont_cls, | ||
1171 | &msgw->session->target, | ||
1172 | GNUNET_SYSERR, | ||
1173 | msgw->payload, | ||
1174 | 0); | ||
1175 | GNUNET_STATISTICS_update (plugin->env->stats, | ||
1176 | "# UNIX bytes discarded", | ||
1177 | msgw->msgsize, | ||
1178 | GNUNET_NO); | ||
1179 | GNUNET_free (msgw->msg); | ||
1180 | GNUNET_free (msgw); | ||
1181 | return; | ||
1182 | } | ||
1183 | /* successfully sent bytes */ | ||
1184 | GNUNET_break (sent > 0); | ||
1185 | GNUNET_STATISTICS_update (plugin->env->stats, | ||
1186 | "# bytes transmitted via UNIX", | ||
1187 | msgw->msgsize, | ||
1188 | GNUNET_NO); | ||
1189 | if (NULL != msgw->cont) | ||
1190 | msgw->cont (msgw->cont_cls, | ||
1191 | &msgw->session->target, | ||
1192 | GNUNET_OK, | ||
1193 | msgw->payload, | ||
1194 | msgw->msgsize); | ||
1195 | GNUNET_free (msgw->msg); | ||
1196 | GNUNET_free (msgw); | ||
1197 | } | ||
1198 | |||
1199 | |||
1200 | /** | ||
1201 | * We have been notified that our socket has something to read. | ||
1202 | * Then reschedule this function to be called again once more is available. | ||
1203 | * | ||
1204 | * @param cls the plugin handle | ||
1205 | */ | ||
1206 | static void | ||
1207 | unix_plugin_select_read (void *cls) | ||
1208 | { | ||
1209 | struct Plugin *plugin = cls; | ||
1210 | const struct GNUNET_SCHEDULER_TaskContext *tc; | ||
1211 | |||
1212 | plugin->read_task = NULL; | ||
1213 | tc = GNUNET_SCHEDULER_get_task_context (); | ||
1214 | if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) | ||
1215 | unix_plugin_do_read (plugin); | ||
1216 | plugin->read_task = | ||
1217 | GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | ||
1218 | plugin->unix_sock.desc, | ||
1219 | &unix_plugin_select_read, | ||
1220 | plugin); | ||
1221 | } | ||
1222 | |||
1223 | |||
1224 | /** | ||
1225 | * We have been notified that our socket is ready to write. | ||
1226 | * Then reschedule this function to be called again once more is available. | ||
1227 | * | ||
1228 | * @param cls the plugin handle | ||
1229 | */ | ||
1230 | static void | ||
1231 | unix_plugin_select_write (void *cls) | ||
1232 | { | ||
1233 | struct Plugin *plugin = cls; | ||
1234 | const struct GNUNET_SCHEDULER_TaskContext *tc; | ||
1235 | |||
1236 | plugin->write_task = NULL; | ||
1237 | tc = GNUNET_SCHEDULER_get_task_context (); | ||
1238 | if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) | ||
1239 | unix_plugin_do_write (plugin); | ||
1240 | if (NULL == plugin->msg_head) | ||
1241 | return; /* write queue empty */ | ||
1242 | plugin->write_task = | ||
1243 | GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, | ||
1244 | plugin->unix_sock.desc, | ||
1245 | &unix_plugin_select_write, | ||
1246 | plugin); | ||
1247 | } | ||
1248 | |||
1249 | |||
1250 | /** | ||
1251 | * Function that can be used by the transport service to transmit | ||
1252 | * a message using the plugin. Note that in the case of a | ||
1253 | * peer disconnecting, the continuation MUST be called | ||
1254 | * prior to the disconnect notification itself. This function | ||
1255 | * will be called with this peer's HELLO message to initiate | ||
1256 | * a fresh connection to another peer. | ||
1257 | * | ||
1258 | * @param cls closure | ||
1259 | * @param session which session must be used | ||
1260 | * @param msgbuf the message to transmit | ||
1261 | * @param msgbuf_size number of bytes in @a msgbuf | ||
1262 | * @param priority how important is the message (most plugins will | ||
1263 | * ignore message priority and just FIFO) | ||
1264 | * @param to how long to wait at most for the transmission (does not | ||
1265 | * require plugins to discard the message after the timeout, | ||
1266 | * just advisory for the desired delay; most plugins will ignore | ||
1267 | * this as well) | ||
1268 | * @param cont continuation to call once the message has | ||
1269 | * been transmitted (or if the transport is ready | ||
1270 | * for the next transmission call; or if the | ||
1271 | * peer disconnected...); can be NULL | ||
1272 | * @param cont_cls closure for @a cont | ||
1273 | * @return number of bytes used (on the physical network, with overheads); | ||
1274 | * -1 on hard errors (i.e. address invalid); 0 is a legal value | ||
1275 | * and does NOT mean that the message was not transmitted (DV) | ||
1276 | */ | ||
1277 | static ssize_t | ||
1278 | unix_plugin_send (void *cls, | ||
1279 | struct GNUNET_ATS_Session *session, | ||
1280 | const char *msgbuf, | ||
1281 | size_t msgbuf_size, | ||
1282 | unsigned int priority, | ||
1283 | struct GNUNET_TIME_Relative to, | ||
1284 | GNUNET_TRANSPORT_TransmitContinuation cont, | ||
1285 | void *cont_cls) | ||
1286 | { | ||
1287 | struct Plugin *plugin = cls; | ||
1288 | struct UNIXMessageWrapper *wrapper; | ||
1289 | struct UNIXMessage *message; | ||
1290 | int ssize; | ||
1291 | |||
1292 | if (GNUNET_OK != | ||
1293 | GNUNET_CONTAINER_multipeermap_contains_value (plugin->session_map, | ||
1294 | &session->target, | ||
1295 | session)) | ||
1296 | { | ||
1297 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
1298 | "Invalid session for peer `%s' `%s'\n", | ||
1299 | GNUNET_i2s (&session->target), | ||
1300 | unix_plugin_address_to_string (NULL, | ||
1301 | session->address->address, | ||
1302 | session->address->address_length)); | ||
1303 | GNUNET_break (0); | ||
1304 | return GNUNET_SYSERR; | ||
1305 | } | ||
1306 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1307 | "Sending %lu bytes with session for peer `%s' `%s'\n", | ||
1308 | (unsigned long) msgbuf_size, | ||
1309 | GNUNET_i2s (&session->target), | ||
1310 | unix_plugin_address_to_string (NULL, | ||
1311 | session->address->address, | ||
1312 | session->address->address_length)); | ||
1313 | ssize = sizeof(struct UNIXMessage) + msgbuf_size; | ||
1314 | message = GNUNET_malloc (sizeof(struct UNIXMessage) + msgbuf_size); | ||
1315 | message->header.size = htons (ssize); | ||
1316 | message->header.type = htons (0); | ||
1317 | GNUNET_memcpy (&message->sender, | ||
1318 | plugin->env->my_identity, | ||
1319 | sizeof(struct GNUNET_PeerIdentity)); | ||
1320 | GNUNET_memcpy (&message[1], msgbuf, msgbuf_size); | ||
1321 | wrapper = GNUNET_new (struct UNIXMessageWrapper); | ||
1322 | wrapper->msg = message; | ||
1323 | wrapper->msgsize = ssize; | ||
1324 | wrapper->payload = msgbuf_size; | ||
1325 | wrapper->priority = priority; | ||
1326 | wrapper->timeout = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), to); | ||
1327 | wrapper->cont = cont; | ||
1328 | wrapper->cont_cls = cont_cls; | ||
1329 | wrapper->session = session; | ||
1330 | GNUNET_CONTAINER_DLL_insert_tail (plugin->msg_head, | ||
1331 | plugin->msg_tail, | ||
1332 | wrapper); | ||
1333 | plugin->bytes_in_queue += ssize; | ||
1334 | session->bytes_in_queue += ssize; | ||
1335 | session->msgs_in_queue++; | ||
1336 | GNUNET_STATISTICS_set (plugin->env->stats, | ||
1337 | "# bytes currently in UNIX buffers", | ||
1338 | plugin->bytes_in_queue, | ||
1339 | GNUNET_NO); | ||
1340 | notify_session_monitor (plugin, session, GNUNET_TRANSPORT_SS_UPDATE); | ||
1341 | if (NULL == plugin->write_task) | ||
1342 | plugin->write_task = | ||
1343 | GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, | ||
1344 | plugin->unix_sock.desc, | ||
1345 | &unix_plugin_select_write, | ||
1346 | plugin); | ||
1347 | return ssize; | ||
1348 | } | ||
1349 | |||
1350 | |||
1351 | /** | ||
1352 | * Create a slew of UNIX sockets. If possible, use IPv6 and IPv4. | ||
1353 | * | ||
1354 | * @param cls closure for server start, should be a `struct Plugin *` | ||
1355 | * @return number of sockets created or #GNUNET_SYSERR on error | ||
1356 | */ | ||
1357 | static int | ||
1358 | unix_transport_server_start (void *cls) | ||
1359 | { | ||
1360 | struct Plugin *plugin = cls; | ||
1361 | struct sockaddr_un *un; | ||
1362 | socklen_t un_len; | ||
1363 | |||
1364 | un = unix_address_to_sockaddr (plugin->unix_socket_path, &un_len); | ||
1365 | if (GNUNET_YES == plugin->is_abstract) | ||
1366 | { | ||
1367 | plugin->unix_socket_path[0] = '@'; | ||
1368 | un->sun_path[0] = '\0'; | ||
1369 | } | ||
1370 | plugin->unix_sock.desc = | ||
1371 | GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_DGRAM, 0); | ||
1372 | if (NULL == plugin->unix_sock.desc) | ||
1373 | { | ||
1374 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket"); | ||
1375 | GNUNET_free (un); | ||
1376 | return GNUNET_SYSERR; | ||
1377 | } | ||
1378 | if ('\0' != un->sun_path[0]) | ||
1379 | { | ||
1380 | if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (un->sun_path)) | ||
1381 | { | ||
1382 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
1383 | _ ("Cannot create path to `%s'\n"), | ||
1384 | un->sun_path); | ||
1385 | GNUNET_NETWORK_socket_close (plugin->unix_sock.desc); | ||
1386 | plugin->unix_sock.desc = NULL; | ||
1387 | GNUNET_free (un); | ||
1388 | return GNUNET_SYSERR; | ||
1389 | } | ||
1390 | } | ||
1391 | if (GNUNET_OK != GNUNET_NETWORK_socket_bind (plugin->unix_sock.desc, | ||
1392 | (const struct sockaddr *) un, | ||
1393 | un_len)) | ||
1394 | { | ||
1395 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind"); | ||
1396 | LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Cannot bind to `%s'\n"), un->sun_path); | ||
1397 | GNUNET_NETWORK_socket_close (plugin->unix_sock.desc); | ||
1398 | plugin->unix_sock.desc = NULL; | ||
1399 | GNUNET_free (un); | ||
1400 | return GNUNET_SYSERR; | ||
1401 | } | ||
1402 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Bound to `%s'\n", plugin->unix_socket_path); | ||
1403 | plugin->read_task = | ||
1404 | GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | ||
1405 | plugin->unix_sock.desc, | ||
1406 | &unix_plugin_select_read, | ||
1407 | plugin); | ||
1408 | GNUNET_free (un); | ||
1409 | return 1; | ||
1410 | } | ||
1411 | |||
1412 | |||
1413 | /** | ||
1414 | * Function that will be called to check if a binary address for this | ||
1415 | * plugin is well-formed and corresponds to an address for THIS peer | ||
1416 | * (as per our configuration). Naturally, if absolutely necessary, | ||
1417 | * plugins can be a bit conservative in their answer, but in general | ||
1418 | * plugins should make sure that the address does not redirect | ||
1419 | * traffic to a 3rd party that might try to man-in-the-middle our | ||
1420 | * traffic. | ||
1421 | * | ||
1422 | * @param cls closure, should be our handle to the Plugin | ||
1423 | * @param addr pointer to the address | ||
1424 | * @param addrlen length of @a addr | ||
1425 | * @return #GNUNET_OK if this is a plausible address for this peer | ||
1426 | * and transport, #GNUNET_SYSERR if not | ||
1427 | * | ||
1428 | */ | ||
1429 | static int | ||
1430 | unix_plugin_check_address (void *cls, const void *addr, size_t addrlen) | ||
1431 | { | ||
1432 | struct Plugin *plugin = cls; | ||
1433 | const struct UnixAddress *ua = addr; | ||
1434 | char *addrstr; | ||
1435 | size_t addr_str_len; | ||
1436 | |||
1437 | if ((NULL == addr) || (0 == addrlen) || | ||
1438 | (sizeof(struct UnixAddress) > addrlen)) | ||
1439 | { | ||
1440 | GNUNET_break (0); | ||
1441 | return GNUNET_SYSERR; | ||
1442 | } | ||
1443 | addrstr = (char *) &ua[1]; | ||
1444 | addr_str_len = ntohl (ua->addrlen); | ||
1445 | if ('\0' != addrstr[addr_str_len - 1]) | ||
1446 | { | ||
1447 | GNUNET_break (0); | ||
1448 | return GNUNET_SYSERR; | ||
1449 | } | ||
1450 | if (strlen (addrstr) + 1 != addr_str_len) | ||
1451 | { | ||
1452 | GNUNET_break (0); | ||
1453 | return GNUNET_SYSERR; | ||
1454 | } | ||
1455 | |||
1456 | if (0 == strcmp (plugin->unix_socket_path, addrstr)) | ||
1457 | return GNUNET_OK; | ||
1458 | return GNUNET_SYSERR; | ||
1459 | } | ||
1460 | |||
1461 | |||
1462 | /** | ||
1463 | * Convert the transports address to a nice, human-readable | ||
1464 | * format. | ||
1465 | * | ||
1466 | * @param cls closure | ||
1467 | * @param type name of the transport that generated the address | ||
1468 | * @param addr one of the addresses of the host, NULL for the last address | ||
1469 | * the specific address format depends on the transport | ||
1470 | * @param addrlen length of the @a addr | ||
1471 | * @param numeric should (IP) addresses be displayed in numeric form? | ||
1472 | * @param timeout after how long should we give up? | ||
1473 | * @param asc function to call on each string | ||
1474 | * @param asc_cls closure for @a asc | ||
1475 | */ | ||
1476 | static void | ||
1477 | unix_plugin_address_pretty_printer (void *cls, | ||
1478 | const char *type, | ||
1479 | const void *addr, | ||
1480 | size_t addrlen, | ||
1481 | int numeric, | ||
1482 | struct GNUNET_TIME_Relative timeout, | ||
1483 | GNUNET_TRANSPORT_AddressStringCallback asc, | ||
1484 | void *asc_cls) | ||
1485 | { | ||
1486 | const char *ret; | ||
1487 | |||
1488 | if ((NULL != addr) && (addrlen > 0)) | ||
1489 | ret = unix_plugin_address_to_string (NULL, addr, addrlen); | ||
1490 | else | ||
1491 | ret = NULL; | ||
1492 | asc (asc_cls, ret, (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK); | ||
1493 | asc (asc_cls, NULL, GNUNET_OK); | ||
1494 | } | ||
1495 | |||
1496 | |||
1497 | /** | ||
1498 | * Function called to convert a string address to | ||
1499 | * a binary address. | ||
1500 | * | ||
1501 | * @param cls closure (`struct Plugin *`) | ||
1502 | * @param addr string address | ||
1503 | * @param addrlen length of the @a addr (strlen(addr) + '\0') | ||
1504 | * @param buf location to store the buffer | ||
1505 | * If the function returns #GNUNET_SYSERR, its contents are undefined. | ||
1506 | * @param added length of created address | ||
1507 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure | ||
1508 | */ | ||
1509 | static int | ||
1510 | unix_plugin_string_to_address (void *cls, | ||
1511 | const char *addr, | ||
1512 | uint16_t addrlen, | ||
1513 | void **buf, | ||
1514 | size_t *added) | ||
1515 | { | ||
1516 | struct UnixAddress *ua; | ||
1517 | char *address; | ||
1518 | char *plugin; | ||
1519 | char *optionstr; | ||
1520 | uint32_t options; | ||
1521 | size_t ua_size; | ||
1522 | |||
1523 | /* Format unix.options.address */ | ||
1524 | address = NULL; | ||
1525 | plugin = NULL; | ||
1526 | optionstr = NULL; | ||
1527 | |||
1528 | if ((NULL == addr) || (addrlen == 0)) | ||
1529 | { | ||
1530 | GNUNET_break (0); | ||
1531 | return GNUNET_SYSERR; | ||
1532 | } | ||
1533 | if ('\0' != addr[addrlen - 1]) | ||
1534 | { | ||
1535 | GNUNET_break (0); | ||
1536 | return GNUNET_SYSERR; | ||
1537 | } | ||
1538 | if (strlen (addr) != addrlen - 1) | ||
1539 | { | ||
1540 | GNUNET_break (0); | ||
1541 | return GNUNET_SYSERR; | ||
1542 | } | ||
1543 | plugin = GNUNET_strdup (addr); | ||
1544 | optionstr = strchr (plugin, '.'); | ||
1545 | if (NULL == optionstr) | ||
1546 | { | ||
1547 | GNUNET_break (0); | ||
1548 | GNUNET_free (plugin); | ||
1549 | return GNUNET_SYSERR; | ||
1550 | } | ||
1551 | optionstr[0] = '\0'; | ||
1552 | optionstr++; | ||
1553 | options = atol (optionstr); | ||
1554 | address = strchr (optionstr, '.'); | ||
1555 | if (NULL == address) | ||
1556 | { | ||
1557 | GNUNET_break (0); | ||
1558 | GNUNET_free (plugin); | ||
1559 | return GNUNET_SYSERR; | ||
1560 | } | ||
1561 | address[0] = '\0'; | ||
1562 | address++; | ||
1563 | if (0 != strcmp (plugin, PLUGIN_NAME)) | ||
1564 | { | ||
1565 | GNUNET_break (0); | ||
1566 | GNUNET_free (plugin); | ||
1567 | return GNUNET_SYSERR; | ||
1568 | } | ||
1569 | |||
1570 | ua_size = sizeof(struct UnixAddress) + strlen (address) + 1; | ||
1571 | ua = GNUNET_malloc (ua_size); | ||
1572 | ua->options = htonl (options); | ||
1573 | ua->addrlen = htonl (strlen (address) + 1); | ||
1574 | GNUNET_memcpy (&ua[1], address, strlen (address) + 1); | ||
1575 | GNUNET_free (plugin); | ||
1576 | |||
1577 | (*buf) = ua; | ||
1578 | (*added) = ua_size; | ||
1579 | return GNUNET_OK; | ||
1580 | } | ||
1581 | |||
1582 | |||
1583 | /** | ||
1584 | * Notify transport service about address | ||
1585 | * | ||
1586 | * @param cls the plugin | ||
1587 | */ | ||
1588 | static void | ||
1589 | address_notification (void *cls) | ||
1590 | { | ||
1591 | struct Plugin *plugin = cls; | ||
1592 | struct GNUNET_HELLO_Address *address; | ||
1593 | size_t len; | ||
1594 | struct UnixAddress *ua; | ||
1595 | char *unix_path; | ||
1596 | |||
1597 | len = sizeof(struct UnixAddress) + strlen (plugin->unix_socket_path) + 1; | ||
1598 | ua = GNUNET_malloc (len); | ||
1599 | ua->options = htonl (plugin->myoptions); | ||
1600 | ua->addrlen = htonl (strlen (plugin->unix_socket_path) + 1); | ||
1601 | unix_path = (char *) &ua[1]; | ||
1602 | GNUNET_memcpy (unix_path, | ||
1603 | plugin->unix_socket_path, | ||
1604 | strlen (plugin->unix_socket_path) + 1); | ||
1605 | |||
1606 | plugin->address_update_task = NULL; | ||
1607 | address = GNUNET_HELLO_address_allocate (plugin->env->my_identity, | ||
1608 | PLUGIN_NAME, | ||
1609 | ua, | ||
1610 | len, | ||
1611 | GNUNET_HELLO_ADDRESS_INFO_NONE); | ||
1612 | plugin->env->notify_address (plugin->env->cls, GNUNET_YES, address); | ||
1613 | GNUNET_free (ua); | ||
1614 | GNUNET_free (address); | ||
1615 | } | ||
1616 | |||
1617 | |||
1618 | /** | ||
1619 | * Function called on sessions to disconnect | ||
1620 | * | ||
1621 | * @param cls the plugin | ||
1622 | * @param key peer identity (unused) | ||
1623 | * @param value the `struct GNUNET_ATS_Session *` to disconnect | ||
1624 | * @return #GNUNET_YES (always, continue to iterate) | ||
1625 | */ | ||
1626 | static int | ||
1627 | get_session_delete_it (void *cls, | ||
1628 | const struct GNUNET_PeerIdentity *key, | ||
1629 | void *value) | ||
1630 | { | ||
1631 | struct Plugin *plugin = cls; | ||
1632 | struct GNUNET_ATS_Session *session = value; | ||
1633 | |||
1634 | unix_plugin_session_disconnect (plugin, session); | ||
1635 | return GNUNET_YES; | ||
1636 | } | ||
1637 | |||
1638 | |||
1639 | /** | ||
1640 | * Disconnect from a remote node. Clean up session if we have one for this peer | ||
1641 | * | ||
1642 | * @param cls closure for this call (should be handle to Plugin) | ||
1643 | * @param target the peeridentity of the peer to disconnect | ||
1644 | * @return #GNUNET_OK on success, #GNUNET_SYSERR if the operation failed | ||
1645 | */ | ||
1646 | static void | ||
1647 | unix_plugin_peer_disconnect (void *cls, | ||
1648 | const struct GNUNET_PeerIdentity *target) | ||
1649 | { | ||
1650 | struct Plugin *plugin = cls; | ||
1651 | |||
1652 | GNUNET_CONTAINER_multipeermap_get_multiple (plugin->session_map, | ||
1653 | target, | ||
1654 | &get_session_delete_it, | ||
1655 | plugin); | ||
1656 | } | ||
1657 | |||
1658 | |||
1659 | /** | ||
1660 | * Return information about the given session to the | ||
1661 | * monitor callback. | ||
1662 | * | ||
1663 | * @param cls the `struct Plugin` with the monitor callback (`sic`) | ||
1664 | * @param peer peer we send information about | ||
1665 | * @param value our `struct GNUNET_ATS_Session` to send information about | ||
1666 | * @return #GNUNET_OK (continue to iterate) | ||
1667 | */ | ||
1668 | static int | ||
1669 | send_session_info_iter (void *cls, | ||
1670 | const struct GNUNET_PeerIdentity *peer, | ||
1671 | void *value) | ||
1672 | { | ||
1673 | struct Plugin *plugin = cls; | ||
1674 | struct GNUNET_ATS_Session *session = value; | ||
1675 | |||
1676 | notify_session_monitor (plugin, session, GNUNET_TRANSPORT_SS_INIT); | ||
1677 | notify_session_monitor (plugin, session, GNUNET_TRANSPORT_SS_UP); | ||
1678 | return GNUNET_OK; | ||
1679 | } | ||
1680 | |||
1681 | |||
1682 | /** | ||
1683 | * Begin monitoring sessions of a plugin. There can only | ||
1684 | * be one active monitor per plugin (i.e. if there are | ||
1685 | * multiple monitors, the transport service needs to | ||
1686 | * multiplex the generated events over all of them). | ||
1687 | * | ||
1688 | * @param cls closure of the plugin | ||
1689 | * @param sic callback to invoke, NULL to disable monitor; | ||
1690 | * plugin will being by iterating over all active | ||
1691 | * sessions immediately and then enter monitor mode | ||
1692 | * @param sic_cls closure for @a sic | ||
1693 | */ | ||
1694 | static void | ||
1695 | unix_plugin_setup_monitor (void *cls, | ||
1696 | GNUNET_TRANSPORT_SessionInfoCallback sic, | ||
1697 | void *sic_cls) | ||
1698 | { | ||
1699 | struct Plugin *plugin = cls; | ||
1700 | |||
1701 | plugin->sic = sic; | ||
1702 | plugin->sic_cls = sic_cls; | ||
1703 | if (NULL != sic) | ||
1704 | { | ||
1705 | GNUNET_CONTAINER_multipeermap_iterate (plugin->session_map, | ||
1706 | &send_session_info_iter, | ||
1707 | plugin); | ||
1708 | /* signal end of first iteration */ | ||
1709 | sic (sic_cls, NULL, NULL); | ||
1710 | } | ||
1711 | } | ||
1712 | |||
1713 | |||
1714 | /** | ||
1715 | * The exported method. Initializes the plugin and returns a | ||
1716 | * struct with the callbacks. | ||
1717 | * | ||
1718 | * @param cls the plugin's execution environment | ||
1719 | * @return NULL on error, plugin functions otherwise | ||
1720 | */ | ||
1721 | void * | ||
1722 | libgnunet_plugin_transport_unix_init (void *cls) | ||
1723 | { | ||
1724 | struct GNUNET_TRANSPORT_PluginEnvironment *env = cls; | ||
1725 | struct GNUNET_TRANSPORT_PluginFunctions *api; | ||
1726 | struct Plugin *plugin; | ||
1727 | int sockets_created; | ||
1728 | |||
1729 | if (NULL == env->receive) | ||
1730 | { | ||
1731 | /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully | ||
1732 | initialize the plugin or the API */ | ||
1733 | api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions); | ||
1734 | api->cls = NULL; | ||
1735 | api->address_pretty_printer = &unix_plugin_address_pretty_printer; | ||
1736 | api->address_to_string = &unix_plugin_address_to_string; | ||
1737 | api->string_to_address = &unix_plugin_string_to_address; | ||
1738 | return api; | ||
1739 | } | ||
1740 | |||
1741 | plugin = GNUNET_new (struct Plugin); | ||
1742 | if (GNUNET_OK != | ||
1743 | GNUNET_CONFIGURATION_get_value_filename (env->cfg, | ||
1744 | "transport-unix", | ||
1745 | "UNIXPATH", | ||
1746 | &plugin->unix_socket_path)) | ||
1747 | { | ||
1748 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | ||
1749 | "transport-unix", | ||
1750 | "UNIXPATH"); | ||
1751 | GNUNET_free (plugin); | ||
1752 | return NULL; | ||
1753 | } | ||
1754 | |||
1755 | plugin->env = env; | ||
1756 | |||
1757 | /* Initialize my flags */ | ||
1758 | #ifdef __linux__ | ||
1759 | plugin->is_abstract = | ||
1760 | GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, | ||
1761 | "testing", | ||
1762 | "USE_ABSTRACT_SOCKETS"); | ||
1763 | #endif | ||
1764 | plugin->myoptions = UNIX_OPTIONS_NONE; | ||
1765 | if (GNUNET_YES == plugin->is_abstract) | ||
1766 | plugin->myoptions = UNIX_OPTIONS_USE_ABSTRACT_SOCKETS; | ||
1767 | |||
1768 | api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions); | ||
1769 | api->cls = plugin; | ||
1770 | api->get_session = &unix_plugin_get_session; | ||
1771 | api->send = &unix_plugin_send; | ||
1772 | api->disconnect_peer = &unix_plugin_peer_disconnect; | ||
1773 | api->disconnect_session = &unix_plugin_session_disconnect; | ||
1774 | api->query_keepalive_factor = &unix_plugin_query_keepalive_factor; | ||
1775 | api->address_pretty_printer = &unix_plugin_address_pretty_printer; | ||
1776 | api->address_to_string = &unix_plugin_address_to_string; | ||
1777 | api->check_address = &unix_plugin_check_address; | ||
1778 | api->string_to_address = &unix_plugin_string_to_address; | ||
1779 | api->get_network = &unix_plugin_get_network; | ||
1780 | api->get_network_for_address = &unix_plugin_get_network_for_address; | ||
1781 | api->update_session_timeout = &unix_plugin_update_session_timeout; | ||
1782 | api->setup_monitor = &unix_plugin_setup_monitor; | ||
1783 | sockets_created = unix_transport_server_start (plugin); | ||
1784 | if ((0 == sockets_created) || (GNUNET_SYSERR == sockets_created)) | ||
1785 | { | ||
1786 | LOG (GNUNET_ERROR_TYPE_WARNING, _ ("Failed to open UNIX listen socket\n")); | ||
1787 | GNUNET_free (api); | ||
1788 | GNUNET_free (plugin->unix_socket_path); | ||
1789 | GNUNET_free (plugin); | ||
1790 | return NULL; | ||
1791 | } | ||
1792 | plugin->session_map = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO); | ||
1793 | plugin->address_update_task = | ||
1794 | GNUNET_SCHEDULER_add_now (&address_notification, plugin); | ||
1795 | return api; | ||
1796 | } | ||
1797 | |||
1798 | |||
1799 | /** | ||
1800 | * Shutdown the plugin. | ||
1801 | * | ||
1802 | * @param cls the plugin API returned from the initialization function | ||
1803 | * @return NULL (always) | ||
1804 | */ | ||
1805 | void * | ||
1806 | libgnunet_plugin_transport_unix_done (void *cls) | ||
1807 | { | ||
1808 | struct GNUNET_TRANSPORT_PluginFunctions *api = cls; | ||
1809 | struct Plugin *plugin = api->cls; | ||
1810 | struct GNUNET_HELLO_Address *address; | ||
1811 | struct UNIXMessageWrapper *msgw; | ||
1812 | struct UnixAddress *ua; | ||
1813 | size_t len; | ||
1814 | struct GNUNET_ATS_Session *session; | ||
1815 | |||
1816 | if (NULL == plugin) | ||
1817 | { | ||
1818 | GNUNET_free (api); | ||
1819 | return NULL; | ||
1820 | } | ||
1821 | len = sizeof(struct UnixAddress) + strlen (plugin->unix_socket_path) + 1; | ||
1822 | ua = GNUNET_malloc (len); | ||
1823 | ua->options = htonl (plugin->myoptions); | ||
1824 | ua->addrlen = htonl (strlen (plugin->unix_socket_path) + 1); | ||
1825 | GNUNET_memcpy (&ua[1], | ||
1826 | plugin->unix_socket_path, | ||
1827 | strlen (plugin->unix_socket_path) + 1); | ||
1828 | address = GNUNET_HELLO_address_allocate (plugin->env->my_identity, | ||
1829 | PLUGIN_NAME, | ||
1830 | ua, | ||
1831 | len, | ||
1832 | GNUNET_HELLO_ADDRESS_INFO_NONE); | ||
1833 | plugin->env->notify_address (plugin->env->cls, GNUNET_NO, address); | ||
1834 | |||
1835 | GNUNET_free (address); | ||
1836 | GNUNET_free (ua); | ||
1837 | |||
1838 | while (NULL != (msgw = plugin->msg_head)) | ||
1839 | { | ||
1840 | GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw); | ||
1841 | session = msgw->session; | ||
1842 | session->msgs_in_queue--; | ||
1843 | GNUNET_assert (session->bytes_in_queue >= msgw->msgsize); | ||
1844 | session->bytes_in_queue -= msgw->msgsize; | ||
1845 | GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize); | ||
1846 | plugin->bytes_in_queue -= msgw->msgsize; | ||
1847 | if (NULL != msgw->cont) | ||
1848 | msgw->cont (msgw->cont_cls, | ||
1849 | &msgw->session->target, | ||
1850 | GNUNET_SYSERR, | ||
1851 | msgw->payload, | ||
1852 | 0); | ||
1853 | GNUNET_free (msgw->msg); | ||
1854 | GNUNET_free (msgw); | ||
1855 | } | ||
1856 | |||
1857 | if (NULL != plugin->read_task) | ||
1858 | { | ||
1859 | GNUNET_SCHEDULER_cancel (plugin->read_task); | ||
1860 | plugin->read_task = NULL; | ||
1861 | } | ||
1862 | if (NULL != plugin->write_task) | ||
1863 | { | ||
1864 | GNUNET_SCHEDULER_cancel (plugin->write_task); | ||
1865 | plugin->write_task = NULL; | ||
1866 | } | ||
1867 | if (NULL != plugin->address_update_task) | ||
1868 | { | ||
1869 | GNUNET_SCHEDULER_cancel (plugin->address_update_task); | ||
1870 | plugin->address_update_task = NULL; | ||
1871 | } | ||
1872 | if (NULL != plugin->unix_sock.desc) | ||
1873 | { | ||
1874 | GNUNET_break (GNUNET_OK == | ||
1875 | GNUNET_NETWORK_socket_close (plugin->unix_sock.desc)); | ||
1876 | plugin->unix_sock.desc = NULL; | ||
1877 | } | ||
1878 | GNUNET_CONTAINER_multipeermap_iterate (plugin->session_map, | ||
1879 | &get_session_delete_it, | ||
1880 | plugin); | ||
1881 | GNUNET_CONTAINER_multipeermap_destroy (plugin->session_map); | ||
1882 | GNUNET_break (0 == plugin->bytes_in_queue); | ||
1883 | GNUNET_free (plugin->unix_socket_path); | ||
1884 | GNUNET_free (plugin); | ||
1885 | GNUNET_free (api); | ||
1886 | return NULL; | ||
1887 | } | ||
1888 | |||
1889 | |||
1890 | /* end of plugin_transport_unix.c */ | ||