2 This file is part of GNUnet
3 (C) 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
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
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_connection_lib.h"
33 #include "gnunet_container_lib.h"
34 #include "gnunet_os_lib.h"
35 #include "gnunet_peerinfo_service.h"
36 #include "gnunet_protocols.h"
37 #include "gnunet_resolver_service.h"
38 #include "gnunet_server_lib.h"
39 #include "gnunet_signatures.h"
40 #include "gnunet_statistics_service.h"
41 #include "gnunet_transport_service.h"
42 #include "gnunet_transport_plugin.h"
43 #include "transport.h"
47 #define LOG(kind,...) GNUNET_log_from (kind, "transport-unix",__VA_ARGS__)
50 * Transport cost to peer, always 1 for UNIX (direct connection)
52 #define UNIX_DIRECT_DISTANCE 1
54 #define DEFAULT_NAT_PORT 0
57 * How long until we give up on transmitting the welcome message?
59 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
62 * Starting port for listening and sending, eventually a config value
64 #define UNIX_NAT_DEFAULT_PORT 22086
66 GNUNET_NETWORK_STRUCT_BEGIN
69 * UNIX Message-Packet header.
76 struct GNUNET_MessageHeader header;
79 * What is the identity of the sender (GNUNET_hash of public key)
81 struct GNUNET_PeerIdentity sender;
87 struct GNUNET_PeerIdentity target;
93 * Session timeout task
95 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
97 struct Plugin * plugin;
100 struct UNIXMessageWrapper
102 struct UNIXMessageWrapper *next;
103 struct UNIXMessageWrapper *prev;
105 struct UNIXMessage * msg;
108 struct GNUNET_TIME_Relative timeout;
109 unsigned int priority;
111 struct Session *session;
112 GNUNET_TRANSPORT_TransmitContinuation cont;
116 /* Forward definition */
127 * Stored in a linked list.
129 struct PeerSession *next;
132 * Pointer to the global plugin struct.
134 struct Plugin *plugin;
137 * To whom are we talking to (set to our identity
138 * if we are still waiting for the welcome message)
140 struct GNUNET_PeerIdentity target;
143 * Address of the other peer (either based on our 'connect'
144 * call or on our 'accept' call).
149 * Length of connect_addr.
154 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
156 int expecting_welcome;
159 * From which socket do we need to send to this peer?
161 struct GNUNET_NETWORK_Handle *sock;
164 * Queue of messages for this peer, in the case that
165 * we have to await a connection...
167 struct MessageQueue *messages;
172 * Information we keep for each of our listen sockets.
174 struct UNIX_Sock_Info
179 struct GNUNET_NETWORK_Handle *desc;
182 * The port we bound to
189 * Encapsulation of all of the state of the plugin.
196 struct GNUNET_TRANSPORT_PluginEnvironment *env;
201 struct GNUNET_CONTAINER_MultiHashMap *session_map;
204 * ID of task used to update our addresses when one expires.
206 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
211 GNUNET_SCHEDULER_TaskIdentifier select_task;
214 * Integer to append to unix domain socket.
221 struct GNUNET_NETWORK_FDSet *rs;
226 struct GNUNET_NETWORK_FDSet *ws;
231 * socket that we transmit all data with
233 struct UNIX_Sock_Info unix_sock;
236 * Path of our unix domain socket (/tmp/unix-plugin-PORT)
238 char *unix_socket_path;
240 struct UNIXMessageWrapper *msg_head;
241 struct UNIXMessageWrapper *msg_tail;
246 struct GNUNET_ATS_Information ats_network;
248 unsigned int bytes_in_queue;
249 unsigned int bytes_in_sent;
250 unsigned int bytes_in_recv;
251 unsigned int bytes_discarded;
255 * Start session timeout
258 start_session_timeout (struct Session *s);
261 * Increment session timeout due to activity
264 reschedule_session_timeout (struct Session *s);
270 stop_session_timeout (struct Session *s);
274 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
278 reschedule_select (struct Plugin * plugin)
281 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
283 GNUNET_SCHEDULER_cancel (plugin->select_task);
284 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
287 if (NULL != plugin->msg_head)
289 plugin->select_task =
290 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
291 GNUNET_TIME_UNIT_FOREVER_REL,
294 &unix_plugin_select, plugin);
295 plugin->with_ws = GNUNET_YES;
299 plugin->select_task =
300 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
301 GNUNET_TIME_UNIT_FOREVER_REL,
304 &unix_plugin_select, plugin);
305 plugin->with_ws = GNUNET_NO;
312 const struct sockaddr_un *addr;
315 int lookup_session_it (void *cls,
316 const struct GNUNET_HashCode * key,
319 struct LookupCtx *lctx = cls;
320 struct Session *t = value;
322 if (0 == strcmp (t->addr, lctx->addr->sun_path))
331 static struct Session *
332 lookup_session (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender, const struct sockaddr_un *addr)
334 struct LookupCtx lctx;
336 GNUNET_assert (NULL != plugin);
337 GNUNET_assert (NULL != sender);
338 GNUNET_assert (NULL != addr);
343 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map, &sender->hashPubKey, &lookup_session_it, &lctx);
349 * Functions with this signature are called whenever we need
350 * to close a session due to a disconnect or failure to
351 * establish a connection.
353 * @param s session to close down
356 disconnect_session (struct Session *s)
358 struct UNIXMessageWrapper *msgw;
359 struct UNIXMessageWrapper *next;
360 struct Plugin * plugin = s->plugin;
362 GNUNET_assert (plugin != NULL);
363 GNUNET_assert (s != NULL);
365 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting session for peer `%s' `%s' \n", GNUNET_i2s (&s->target), s->addr);
366 stop_session_timeout (s);
367 plugin->env->session_end (plugin->env->cls, &s->target, s);
369 msgw = plugin->msg_head;
371 next = plugin->msg_head;
376 if (msgw->session != s)
378 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
379 if (NULL != msgw->cont)
380 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR);
381 GNUNET_free (msgw->msg);
383 removed = GNUNET_YES;
385 if ((GNUNET_YES == removed) && (NULL == plugin->msg_head))
386 reschedule_select (plugin);
388 GNUNET_assert (GNUNET_YES ==
389 GNUNET_CONTAINER_multihashmap_remove(plugin->session_map, &s->target.hashPubKey, s));
391 GNUNET_STATISTICS_set(plugin->env->stats,
392 "# UNIX sessions active",
393 GNUNET_CONTAINER_multihashmap_size(plugin->session_map),
400 get_session_delete_it (void *cls, const struct GNUNET_HashCode * key, void *value)
402 struct Session *s = value;
403 disconnect_session (s);
409 * Disconnect from a remote node. Clean up session if we have one for this peer
411 * @param cls closure for this call (should be handle to Plugin)
412 * @param target the peeridentity of the peer to disconnect
413 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
416 unix_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
418 struct Plugin *plugin = cls;
419 GNUNET_assert (plugin != NULL);
421 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map, &target->hashPubKey, &get_session_delete_it, plugin);
426 * Shutdown the server process (stop receiving inbound traffic). Maybe
429 * @param cls Handle to the plugin for this transport
431 * @return returns the number of sockets successfully closed,
432 * should equal the number of sockets successfully opened
435 unix_transport_server_stop (void *cls)
437 struct Plugin *plugin = cls;
439 struct UNIXMessageWrapper * msgw = plugin->msg_head;
441 while (NULL != (msgw = plugin->msg_head))
443 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
444 if (msgw->cont != NULL)
445 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR);
446 GNUNET_free (msgw->msg);
450 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
452 GNUNET_SCHEDULER_cancel (plugin->select_task);
453 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
456 if (NULL != plugin->unix_sock.desc)
458 GNUNET_break (GNUNET_OK ==
459 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc));
460 plugin->unix_sock.desc = NULL;
461 plugin->with_ws = GNUNET_NO;
468 * Actually send out the message, assume we've got the address and
469 * send_handle squared away!
472 * @param send_handle which handle to send message on
473 * @param target who should receive this message (ignored by UNIX)
474 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
475 * @param msgbuf_size the size of the msgbuf to send
476 * @param priority how important is the message (ignored by UNIX)
477 * @param timeout when should we time out (give up) if we can not transmit?
478 * @param addr the addr to send the message to, needs to be a sockaddr for us
479 * @param addrlen the len of addr
480 * @param cont continuation to call once the message has
481 * been transmitted (or if the transport is ready
482 * for the next transmission call; or if the
483 * peer disconnected...)
484 * @param cont_cls closure for cont
486 * @return the number of bytes written, -1 on errors
489 unix_real_send (void *cls,
490 struct GNUNET_NETWORK_Handle *send_handle,
491 const struct GNUNET_PeerIdentity *target, const char *msgbuf,
492 size_t msgbuf_size, unsigned int priority,
493 struct GNUNET_TIME_Relative timeout, const void *addr,
494 size_t addrlen, GNUNET_TRANSPORT_TransmitContinuation cont,
497 struct Plugin *plugin = cls;
501 struct sockaddr_un un;
504 GNUNET_assert (NULL != plugin);
506 if (send_handle == NULL)
508 /* We do not have a send handle */
511 cont (cont_cls, target, GNUNET_SYSERR);
514 if ((addr == NULL) || (addrlen == 0))
516 /* Can never send if we don't have an address */
519 cont (cont_cls, target, GNUNET_SYSERR);
523 /* Prepare address */
524 memset (&un, 0, sizeof (un));
525 un.sun_family = AF_UNIX;
526 slen = strlen (addr) + 1;
527 if (slen >= sizeof (un.sun_path))
528 slen = sizeof (un.sun_path) - 1;
529 GNUNET_assert (slen < sizeof (un.sun_path));
530 memcpy (un.sun_path, addr, slen);
531 un.sun_path[slen] = '\0';
532 slen = sizeof (struct sockaddr_un);
534 un.sun_path[0] = '\0';
536 #if HAVE_SOCKADDR_IN_SIN_LEN
537 un.sun_len = (u_char) slen;
539 sb = (struct sockaddr *) &un;
544 sent = GNUNET_NETWORK_socket_sendto (send_handle, msgbuf, msgbuf_size, sb, sbs);
546 if ((GNUNET_SYSERR == sent) && ((errno == EAGAIN) || (errno == ENOBUFS)))
548 /* We have to retry later: retry */
552 if ((GNUNET_SYSERR == sent) && (errno == EMSGSIZE))
555 socklen_t len = sizeof (size);
557 GNUNET_NETWORK_socket_getsockopt ((struct GNUNET_NETWORK_Handle *)
558 send_handle, SOL_SOCKET, SO_SNDBUF, &size,
561 if (size < msgbuf_size)
563 LOG (GNUNET_ERROR_TYPE_DEBUG,
564 "Trying to increase socket buffer size from %i to %i for message size %i\n",
566 ((msgbuf_size / 1000) + 2) * 1000,
568 size = ((msgbuf_size / 1000) + 2) * 1000;
569 if (GNUNET_NETWORK_socket_setsockopt
570 ((struct GNUNET_NETWORK_Handle *) send_handle, SOL_SOCKET, SO_SNDBUF,
571 &size, sizeof (size)) == GNUNET_OK)
573 /* Increased buffer size, retry sending */
578 /* Could not increase buffer size: error, no retry */
579 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
585 /* Buffer is bigger than message: error, no retry
586 * This should never happen!*/
592 LOG (GNUNET_ERROR_TYPE_DEBUG,
593 "UNIX transmit %u-byte message to %s (%d: %s)\n",
594 (unsigned int) msgbuf_size, GNUNET_a2s (sb, sbs), (int) sent,
595 (sent < 0) ? STRERROR (errno) : "ok");
597 /* Calling continuation */
600 if (sent == GNUNET_SYSERR)
601 cont (cont_cls, target, GNUNET_SYSERR);
603 cont (cont_cls, target, GNUNET_OK);
606 /* return number of bytes successfully sent */
611 /* That should never happen */
615 /* failed and retry: return 0 */
616 if (GNUNET_SYSERR == sent)
631 get_session_it (void *cls, const struct GNUNET_HashCode * key, void *value)
633 struct gsi_ctx *gsi = cls;
634 struct Session *s = value;
636 LOG (GNUNET_ERROR_TYPE_DEBUG, "Comparing session %s %s\n", gsi->address, s->addr);
637 if ((gsi->addrlen == s->addrlen) &&
638 (0 == memcmp (gsi->address, s->addr, s->addrlen)))
647 * Creates a new outbound session the transport service will use to send data to the
650 * @param cls the plugin
651 * @param address the address
652 * @return the session or NULL of max connections exceeded
654 static struct Session *
655 unix_plugin_get_session (void *cls,
656 const struct GNUNET_HELLO_Address *address)
658 struct Session * s = NULL;
659 struct Plugin *plugin = cls;
663 GNUNET_assert (plugin != NULL);
664 GNUNET_assert (address != NULL);
666 /* Check if already existing */
667 gsi.address = (char *) address->address;
668 gsi.addrlen = address->address_length;
670 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map, &address->peer.hashPubKey, &get_session_it, &gsi);
673 LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing session\n");
677 /* Create a new session */
678 s = GNUNET_malloc (sizeof (struct Session) + address->address_length);
680 s->addrlen = address->address_length;
682 memcpy(s->addr, address->address, s->addrlen);
683 memcpy(&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
685 start_session_timeout (s);
687 GNUNET_CONTAINER_multihashmap_put (plugin->session_map,
688 &address->peer.hashPubKey, s,
689 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
691 GNUNET_STATISTICS_set(plugin->env->stats,
692 "# UNIX sessions active",
693 GNUNET_CONTAINER_multihashmap_size(plugin->session_map),
695 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating new session\n");
700 * @param cls the plugin handle
701 * @param tc the scheduling context (for rescheduling this function again)
703 * We have been notified that our writeset has something to read. We don't
704 * know which socket needs to be read, so we have to check each one
705 * Then reschedule this function to be called again once more is available.
709 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
712 * Function that can be used by the transport service to transmit
713 * a message using the plugin. Note that in the case of a
714 * peer disconnecting, the continuation MUST be called
715 * prior to the disconnect notification itself. This function
716 * will be called with this peer's HELLO message to initiate
717 * a fresh connection to another peer.
720 * @param session which session must be used
721 * @param msgbuf the message to transmit
722 * @param msgbuf_size number of bytes in 'msgbuf'
723 * @param priority how important is the message (most plugins will
724 * ignore message priority and just FIFO)
725 * @param to how long to wait at most for the transmission (does not
726 * require plugins to discard the message after the timeout,
727 * just advisory for the desired delay; most plugins will ignore
729 * @param cont continuation to call once the message has
730 * been transmitted (or if the transport is ready
731 * for the next transmission call; or if the
732 * peer disconnected...); can be NULL
733 * @param cont_cls closure for cont
734 * @return number of bytes used (on the physical network, with overheads);
735 * -1 on hard errors (i.e. address invalid); 0 is a legal value
736 * and does NOT mean that the message was not transmitted (DV)
739 unix_plugin_send (void *cls,
740 struct Session *session,
741 const char *msgbuf, size_t msgbuf_size,
742 unsigned int priority,
743 struct GNUNET_TIME_Relative to,
744 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
746 struct Plugin *plugin = cls;
747 struct UNIXMessageWrapper *wrapper;
748 struct UNIXMessage *message;
751 GNUNET_assert (plugin != NULL);
752 GNUNET_assert (session != NULL);
754 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_contains_value(plugin->session_map,
755 &session->target.hashPubKey, session))
757 LOG (GNUNET_ERROR_TYPE_ERROR, "Invalid session for peer `%s' `%s'\n",
758 GNUNET_i2s (&session->target),
759 (char *) session->addr);
762 return GNUNET_SYSERR;
764 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending %u bytes with session for peer `%s' `%s'\n",
766 GNUNET_i2s (&session->target),
767 (char *) session->addr);
769 ssize = sizeof (struct UNIXMessage) + msgbuf_size;
770 message = GNUNET_malloc (sizeof (struct UNIXMessage) + msgbuf_size);
771 message->header.size = htons (ssize);
772 message->header.type = htons (0);
773 memcpy (&message->sender, plugin->env->my_identity,
774 sizeof (struct GNUNET_PeerIdentity));
775 memcpy (&message[1], msgbuf, msgbuf_size);
777 reschedule_session_timeout (session);
779 wrapper = GNUNET_malloc (sizeof (struct UNIXMessageWrapper));
780 wrapper->msg = message;
781 wrapper->msgsize = ssize;
782 wrapper->priority = priority;
783 wrapper->timeout = to;
784 wrapper->cont = cont;
785 wrapper->cont_cls = cont_cls;
786 wrapper->session = session;
788 GNUNET_CONTAINER_DLL_insert(plugin->msg_head, plugin->msg_tail, wrapper);
790 plugin->bytes_in_queue += ssize;
791 GNUNET_STATISTICS_set (plugin->env->stats,"# bytes currently in UNIX buffers",
792 plugin->bytes_in_queue, GNUNET_NO);
794 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sent %d bytes to `%s'\n", ssize,
795 (char *) session->addr);
796 if (plugin->with_ws == GNUNET_NO)
798 reschedule_select (plugin);
806 * Demultiplexer for UNIX messages
808 * @param plugin the main plugin for this transport
809 * @param sender from which peer the message was received
810 * @param currhdr pointer to the header of the message
811 * @param un the address from which the message was received
812 * @param fromlen the length of the address
815 unix_demultiplexer (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender,
816 const struct GNUNET_MessageHeader *currhdr,
817 const struct sockaddr_un *un, size_t fromlen)
819 struct GNUNET_ATS_Information ats[2];
820 struct Session *s = NULL;
821 struct GNUNET_HELLO_Address * addr;
823 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
824 ats[0].value = htonl (UNIX_DIRECT_DISTANCE);
825 ats[1] = plugin->ats_network;
826 GNUNET_break (ntohl(plugin->ats_network.value) != GNUNET_ATS_NET_UNSPECIFIED);
828 GNUNET_assert (fromlen >= sizeof (struct sockaddr_un));
830 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received message from %s\n",
835 plugin->bytes_in_recv += ntohs(currhdr->size);
836 GNUNET_STATISTICS_set (plugin->env->stats,"# bytes received via UNIX",
837 plugin->bytes_in_recv, GNUNET_NO);
839 addr = GNUNET_HELLO_address_allocate(sender, "unix", un->sun_path, strlen (un->sun_path) + 1);
840 s = lookup_session (plugin, sender, un);
842 s = unix_plugin_get_session (plugin, addr);
843 reschedule_session_timeout (s);
845 plugin->env->receive (plugin->env->cls, sender, currhdr,
846 (const struct GNUNET_ATS_Information *) &ats, 2,
847 s, un->sun_path, strlen (un->sun_path) + 1);
853 unix_plugin_select_read (struct Plugin * plugin)
855 char buf[65536] GNUNET_ALIGN;
856 struct UNIXMessage *msg;
857 struct GNUNET_PeerIdentity sender;
858 struct sockaddr_un un;
864 const struct GNUNET_MessageHeader *currhdr;
867 addrlen = sizeof (un);
868 memset (&un, 0, sizeof (un));
871 GNUNET_NETWORK_socket_recvfrom (plugin->unix_sock.desc, buf, sizeof (buf),
872 (struct sockaddr *) &un, &addrlen);
874 if ((GNUNET_SYSERR == ret) && ((errno == EAGAIN) || (errno == ENOBUFS)))
877 if (ret == GNUNET_SYSERR)
879 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom");
885 un.sun_path[0] = '/';
887 LOG (GNUNET_ERROR_TYPE_DEBUG, "Read %d bytes from socket %s\n", ret,
891 GNUNET_assert (AF_UNIX == (un.sun_family));
893 msg = (struct UNIXMessage *) buf;
894 csize = ntohs (msg->header.size);
895 if ((csize < sizeof (struct UNIXMessage)) || (csize > ret))
900 msgbuf = (char *) &msg[1];
901 memcpy (&sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
903 tsize = csize - sizeof (struct UNIXMessage);
904 while (offset + sizeof (struct GNUNET_MessageHeader) <= tsize)
906 currhdr = (struct GNUNET_MessageHeader *) &msgbuf[offset];
907 csize = ntohs (currhdr->size);
908 if ((csize < sizeof (struct GNUNET_MessageHeader)) ||
909 (csize > tsize - offset))
915 unix_demultiplexer (plugin, &sender, currhdr, &un, sizeof (un));
922 unix_plugin_select_write (struct Plugin * plugin)
924 static int retry_counter;
926 struct UNIXMessageWrapper * msgw = plugin->msg_head;
928 sent = unix_real_send (plugin,
929 plugin->unix_sock.desc,
930 &msgw->session->target,
931 (const char *) msgw->msg,
936 msgw->session->addrlen,
937 msgw->cont, msgw->cont_cls);
941 /* failed and retry */
943 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX retry attempt",
944 retry_counter, GNUNET_NO);
948 if (retry_counter > 0 )
950 /* no retry: reset counter */
952 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX retry attempt",
953 retry_counter, GNUNET_NO);
958 /* failed and no retry */
959 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
961 GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
962 plugin->bytes_in_queue -= msgw->msgsize;
963 GNUNET_STATISTICS_set (plugin->env->stats, "# bytes currently in UNIX buffers",
964 plugin->bytes_in_queue, GNUNET_NO);
965 plugin->bytes_discarded += msgw->msgsize;
966 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes discarded",
967 plugin->bytes_discarded, GNUNET_NO);
969 GNUNET_free (msgw->msg);
976 /* successfully sent bytes */
977 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
979 GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
980 plugin->bytes_in_queue -= msgw->msgsize;
981 GNUNET_STATISTICS_set (plugin->env->stats,"# bytes currently in UNIX buffers",
982 plugin->bytes_in_queue, GNUNET_NO);
983 plugin->bytes_in_sent += msgw->msgsize;
984 GNUNET_STATISTICS_set (plugin->env->stats,"# bytes transmitted via UNIX",
985 plugin->bytes_in_sent, GNUNET_NO);
987 GNUNET_free (msgw->msg);
996 * We have been notified that our writeset has something to read. We don't
997 * know which socket needs to be read, so we have to check each one
998 * Then reschedule this function to be called again once more is available.
1000 * @param cls the plugin handle
1001 * @param tc the scheduling context (for rescheduling this function again)
1004 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1006 struct Plugin *plugin = cls;
1008 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1009 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1012 if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
1014 /* Ready to send data */
1015 GNUNET_assert (GNUNET_NETWORK_fdset_isset
1016 (tc->write_ready, plugin->unix_sock.desc));
1017 if (plugin->msg_head != NULL)
1018 unix_plugin_select_write (plugin);
1021 if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
1023 /* Ready to receive data */
1024 GNUNET_assert (GNUNET_NETWORK_fdset_isset
1025 (tc->read_ready, plugin->unix_sock.desc));
1026 unix_plugin_select_read (plugin);
1029 reschedule_select (plugin);
1034 * Create a slew of UNIX sockets. If possible, use IPv6 and IPv4.
1036 * @param cls closure for server start, should be a struct Plugin *
1037 * @return number of sockets created or GNUNET_SYSERR on error
1040 unix_transport_server_start (void *cls)
1042 struct Plugin *plugin = cls;
1043 struct sockaddr *serverAddr;
1045 struct sockaddr_un un;
1048 memset (&un, 0, sizeof (un));
1049 un.sun_family = AF_UNIX;
1050 slen = strlen (plugin->unix_socket_path) + 1;
1051 if (slen >= sizeof (un.sun_path))
1052 slen = sizeof (un.sun_path) - 1;
1054 memcpy (un.sun_path, plugin->unix_socket_path, slen);
1055 un.sun_path[slen] = '\0';
1056 slen = sizeof (struct sockaddr_un);
1057 #if HAVE_SOCKADDR_IN_SIN_LEN
1058 un.sun_len = (u_char) slen;
1061 serverAddr = (struct sockaddr *) &un;
1064 un.sun_path[0] = '\0';
1066 plugin->ats_network = plugin->env->get_address_type (plugin->env->cls, serverAddr, addrlen);
1067 plugin->unix_sock.desc =
1068 GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_DGRAM, 0);
1069 if (NULL == plugin->unix_sock.desc)
1071 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
1072 return GNUNET_SYSERR;
1074 if (GNUNET_NETWORK_socket_bind (plugin->unix_sock.desc, serverAddr, addrlen)
1077 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
1078 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc);
1079 plugin->unix_sock.desc = NULL;
1080 return GNUNET_SYSERR;
1082 LOG (GNUNET_ERROR_TYPE_DEBUG, "Bound to `%s'\n", plugin->unix_socket_path);
1083 plugin->rs = GNUNET_NETWORK_fdset_create ();
1084 plugin->ws = GNUNET_NETWORK_fdset_create ();
1085 GNUNET_NETWORK_fdset_zero (plugin->rs);
1086 GNUNET_NETWORK_fdset_zero (plugin->ws);
1087 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->unix_sock.desc);
1088 GNUNET_NETWORK_fdset_set (plugin->ws, plugin->unix_sock.desc);
1090 reschedule_select (plugin);
1097 * Function that will be called to check if a binary address for this
1098 * plugin is well-formed and corresponds to an address for THIS peer
1099 * (as per our configuration). Naturally, if absolutely necessary,
1100 * plugins can be a bit conservative in their answer, but in general
1101 * plugins should make sure that the address does not redirect
1102 * traffic to a 3rd party that might try to man-in-the-middle our
1105 * @param cls closure, should be our handle to the Plugin
1106 * @param addr pointer to the address
1107 * @param addrlen length of addr
1108 * @return GNUNET_OK if this is a plausible address for this peer
1109 * and transport, GNUNET_SYSERR if not
1113 unix_check_address (void *cls, const void *addr, size_t addrlen)
1115 LOG (GNUNET_ERROR_TYPE_DEBUG,
1116 "Informing transport service about my address `%s'\n",
1123 * Convert the transports address to a nice, human-readable
1126 * @param cls closure
1127 * @param type name of the transport that generated the address
1128 * @param addr one of the addresses of the host, NULL for the last address
1129 * the specific address format depends on the transport
1130 * @param addrlen length of the address
1131 * @param numeric should (IP) addresses be displayed in numeric form?
1132 * @param timeout after how long should we give up?
1133 * @param asc function to call on each string
1134 * @param asc_cls closure for asc
1137 unix_plugin_address_pretty_printer (void *cls, const char *type,
1138 const void *addr, size_t addrlen,
1140 struct GNUNET_TIME_Relative timeout,
1141 GNUNET_TRANSPORT_AddressStringCallback asc,
1144 if ((NULL != addr) && (addrlen > 0))
1146 asc (asc_cls, (const char *) addr);
1151 asc (asc_cls, "<invalid UNIX address>");
1153 asc (asc_cls, NULL);
1158 * Function called to convert a string address to
1161 * @param cls closure ('struct Plugin*')
1162 * @param addr string address
1163 * @param addrlen length of the address (strlen(addr) + '\0')
1164 * @param buf location to store the buffer
1165 * If the function returns GNUNET_SYSERR, its contents are undefined.
1166 * @param added length of created address
1167 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1170 unix_string_to_address (void *cls, const char *addr, uint16_t addrlen,
1171 void **buf, size_t *added)
1173 if ((NULL == addr) || (0 == addrlen))
1176 return GNUNET_SYSERR;
1179 if ('\0' != addr[addrlen - 1])
1182 return GNUNET_SYSERR;
1185 if (strlen (addr) != addrlen - 1)
1188 return GNUNET_SYSERR;
1191 (*buf) = strdup (addr);
1192 (*added) = strlen (addr) + 1;
1198 * Function called for a quick conversion of the binary address to
1199 * a numeric address. Note that the caller must not free the
1200 * address and that the next call to this function is allowed
1201 * to override the address again.
1203 * @param cls closure
1204 * @param addr binary address
1205 * @param addrlen length of the address
1206 * @return string representing the same address
1209 unix_address_to_string (void *cls, const void *addr, size_t addrlen)
1211 if ((addr != NULL) && (addrlen > 0))
1212 return (const char *) addr;
1218 * Notify transport service about address
1220 * @param cls the plugin
1224 address_notification (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1226 struct Plugin *plugin = cls;
1228 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
1229 plugin->unix_socket_path,
1230 strlen (plugin->unix_socket_path) + 1,
1236 * Session was idle, so disconnect it
1239 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1241 GNUNET_assert (NULL != cls);
1242 struct Session *s = cls;
1244 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1245 LOG (GNUNET_ERROR_TYPE_DEBUG,
1246 "Session %p was idle for %llu ms, disconnecting\n",
1247 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1248 /* call session destroy function */
1249 disconnect_session(s);
1254 * Start session timeout
1257 start_session_timeout (struct Session *s)
1259 GNUNET_assert (NULL != s);
1260 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1261 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1264 LOG (GNUNET_ERROR_TYPE_DEBUG,
1265 "Timeout for session %p set to %llu ms\n",
1266 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1271 * Increment session timeout due to activity
1274 reschedule_session_timeout (struct Session *s)
1276 GNUNET_assert (NULL != s);
1277 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1279 GNUNET_SCHEDULER_cancel (s->timeout_task);
1280 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1283 LOG (GNUNET_ERROR_TYPE_DEBUG,
1284 "Timeout rescheduled for session %p set to %llu ms\n",
1285 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1293 stop_session_timeout (struct Session *s)
1295 GNUNET_assert (NULL != s);
1297 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1299 GNUNET_SCHEDULER_cancel (s->timeout_task);
1300 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1301 LOG (GNUNET_ERROR_TYPE_DEBUG,
1302 "Timeout stopped for session %p canceled\n",
1303 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1308 * The exported method. Makes the core api available via a global and
1309 * returns the unix transport API.
1312 libgnunet_plugin_transport_unix_init (void *cls)
1314 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1315 unsigned long long port;
1316 struct GNUNET_TRANSPORT_PluginFunctions *api;
1317 struct Plugin *plugin;
1318 int sockets_created;
1320 if (NULL == env->receive)
1322 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1323 initialze the plugin or the API */
1324 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1326 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1327 api->address_to_string = &unix_address_to_string;
1328 api->string_to_address = &unix_string_to_address;
1331 GNUNET_assert( NULL != env->stats);
1334 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-unix", "PORT",
1336 port = UNIX_NAT_DEFAULT_PORT;
1337 plugin = GNUNET_malloc (sizeof (struct Plugin));
1338 plugin->port = port;
1340 GNUNET_asprintf (&plugin->unix_socket_path, "/tmp/unix-plugin-sock.%d",
1343 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1346 api->get_session = &unix_plugin_get_session;
1347 api->send = &unix_plugin_send;
1348 api->disconnect = &unix_disconnect;
1349 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1350 api->address_to_string = &unix_address_to_string;
1351 api->check_address = &unix_check_address;
1352 api->string_to_address = &unix_string_to_address;
1353 sockets_created = unix_transport_server_start (plugin);
1354 if (sockets_created == 0)
1355 LOG (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UNIX sockets\n"));
1357 plugin->session_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
1359 GNUNET_SCHEDULER_add_now (address_notification, plugin);
1364 libgnunet_plugin_transport_unix_done (void *cls)
1366 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1367 struct Plugin *plugin = api->cls;
1375 plugin->env->notify_address (plugin->env->cls, GNUNET_NO,
1376 plugin->unix_socket_path,
1377 strlen (plugin->unix_socket_path) + 1,
1380 unix_transport_server_stop (plugin);
1383 GNUNET_CONTAINER_multihashmap_iterate (plugin->session_map, &get_session_delete_it, plugin);
1384 GNUNET_CONTAINER_multihashmap_destroy (plugin->session_map);
1387 if (NULL != plugin->rs)
1388 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1389 if (NULL != plugin->ws)
1390 GNUNET_NETWORK_fdset_destroy (plugin->ws);
1391 GNUNET_free (plugin->unix_socket_path);
1392 GNUNET_free (plugin);
1397 /* end of plugin_transport_unix.c */