2 This file is part of GNUnet
3 (C) 2010, 2013 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
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"
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!?).
46 #define PLUGIN_NAME "unix"
49 * How long until we give up on transmitting the welcome message?
51 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
54 * Default "port" to use, if configuration does not specify.
55 * Essentially just a number appended to the UNIX path.
57 #define UNIX_NAT_DEFAULT_PORT 22086
60 #define LOG(kind,...) GNUNET_log_from (kind, "transport-unix",__VA_ARGS__)
63 GNUNET_NETWORK_STRUCT_BEGIN
67 uint32_t options GNUNET_PACKED;
69 uint32_t addrlen GNUNET_PACKED;
74 * UNIX Message-Packet header.
81 struct GNUNET_MessageHeader header;
84 * What is the identity of the sender (GNUNET_hash of public key)
86 struct GNUNET_PeerIdentity sender;
90 GNUNET_NETWORK_STRUCT_END
95 static uint32_t myoptions;
98 * Handle for a session.
102 struct GNUNET_PeerIdentity target;
104 struct Plugin * plugin;
106 struct UnixAddress *addr;
113 * Session timeout task
115 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
119 struct UNIXMessageWrapper
122 * We keep messages in a doubly linked list.
124 struct UNIXMessageWrapper *next;
127 * We keep messages in a doubly linked list.
129 struct UNIXMessageWrapper *prev;
132 * The actual payload (allocated separately right now).
134 struct UNIXMessage * msg;
137 * Session this message belongs to.
139 struct Session *session;
142 * Function to call upon transmission.
144 GNUNET_TRANSPORT_TransmitContinuation cont;
147 * Closure for 'cont'.
152 * Timeout for this message.
154 struct GNUNET_TIME_Absolute timeout;
157 * Number of bytes in 'msg'.
162 * Number of bytes of payload encapsulated in 'msg'.
167 * Priority of the message (ignored, just dragged along in UNIX).
169 unsigned int priority;
174 * Encapsulation of all of the state of the plugin.
186 * Stored in a linked list.
188 struct PeerSession *next;
191 * Pointer to the global plugin struct.
193 struct Plugin *plugin;
196 * To whom are we talking to (set to our identity
197 * if we are still waiting for the welcome message)
199 struct GNUNET_PeerIdentity target;
202 * Address of the other peer (either based on our 'connect'
203 * call or on our 'accept' call).
208 * Length of connect_addr.
213 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
215 int expecting_welcome;
218 * From which socket do we need to send to this peer?
220 struct GNUNET_NETWORK_Handle *sock;
223 * Queue of messages for this peer, in the case that
224 * we have to await a connection...
226 struct MessageQueue *messages;
232 * Information we keep for each of our listen sockets.
234 struct UNIX_Sock_Info
239 struct GNUNET_NETWORK_Handle *desc;
242 * The port we bound to (not an actual PORT, as UNIX domain sockets
243 * don't have ports, but rather a number in the path name to make this
251 * Encapsulation of all of the state of the plugin.
257 * ID of task used to update our addresses when one expires.
259 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
264 GNUNET_SCHEDULER_TaskIdentifier select_task;
267 * Number of bytes we currently have in our write queue.
269 unsigned long long bytes_in_queue;
274 struct GNUNET_TRANSPORT_PluginEnvironment *env;
279 struct GNUNET_CONTAINER_MultiPeerMap *session_map;
284 struct GNUNET_NETWORK_FDSet *rs;
289 struct GNUNET_NETWORK_FDSet *ws;
292 * Path of our unix domain socket (/tmp/unix-plugin-PORT)
294 char *unix_socket_path;
297 * Head of queue of messages to transmit.
299 struct UNIXMessageWrapper *msg_head;
302 * Tail of queue of messages to transmit.
304 struct UNIXMessageWrapper *msg_tail;
307 * socket that we transmit all data with
309 struct UNIX_Sock_Info unix_sock;
314 struct GNUNET_ATS_Information ats_network;
317 * Is the write set in the current 'select' task? GNUNET_NO if the
318 * write queue was empty when the main task was scheduled,
319 * GNUNET_YES if we're already waiting for being allowed to write.
324 * Integer to append to unix domain socket.
332 * Increment session timeout due to activity
334 * @param s session for which the timeout should be moved
337 reschedule_session_timeout (struct Session *s);
341 * We have been notified that our writeset has something to read. We don't
342 * know which socket needs to be read, so we have to check each one
343 * Then reschedule this function to be called again once more is available.
345 * @param cls the plugin handle
346 * @param tc the scheduling context (for rescheduling this function again)
349 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
353 * Function called for a quick conversion of the binary address to
354 * a numeric address. Note that the caller must not free the
355 * address and that the next call to this function is allowed
356 * to override the address again.
359 * @param addr binary address
360 * @param addrlen length of the address
361 * @return string representing the same address
364 unix_address_to_string (void *cls, const void *addr, size_t addrlen)
366 static char rbuf[1024];
367 struct UnixAddress *ua = (struct UnixAddress *) addr;
371 if ((NULL == addr) || (sizeof (struct UnixAddress) > addrlen))
376 addrstr = (char *) &ua[1];
377 addr_str_len = ntohl (ua->addrlen);
379 if (addr_str_len != addrlen - sizeof(struct UnixAddress))
385 if ('\0' != addrstr[addr_str_len - 1])
390 if (strlen (addrstr) + 1 != addr_str_len)
396 GNUNET_snprintf (rbuf, sizeof(rbuf), "%s.%u.%s", PLUGIN_NAME,
397 ntohl (ua->options), addrstr);
402 static struct sockaddr_un *
403 unix_address_to_sockaddr (const char *unixpath,
406 struct sockaddr_un *un;
409 GNUNET_assert (0 < strlen (unixpath)); /* sanity check */
410 un = GNUNET_new (struct sockaddr_un);
411 un->sun_family = AF_UNIX;
412 slen = strlen (unixpath);
413 if (slen >= sizeof (un->sun_path))
414 slen = sizeof (un->sun_path) - 1;
415 memcpy (un->sun_path, unixpath, slen);
416 un->sun_path[slen] = '\0';
417 slen = sizeof (struct sockaddr_un);
418 #if HAVE_SOCKADDR_IN_SIN_LEN
419 un->sun_len = (u_char) slen;
427 * Re-schedule the main 'select' callback (unix_plugin_select)
430 * @param plugin the plugin context
433 reschedule_select (struct Plugin * plugin)
435 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
437 GNUNET_SCHEDULER_cancel (plugin->select_task);
438 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
440 if (NULL != plugin->msg_head)
442 plugin->select_task =
443 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
444 GNUNET_TIME_UNIT_FOREVER_REL,
447 &unix_plugin_select, plugin);
448 plugin->with_ws = GNUNET_YES;
452 plugin->select_task =
453 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
454 GNUNET_TIME_UNIT_FOREVER_REL,
457 &unix_plugin_select, plugin);
458 plugin->with_ws = GNUNET_NO;
464 * Closure to #lookup_session_it.
469 * Location to store the session, if found.
474 * Address we are looking for.
476 const struct UnixAddress *ua;
479 * Number of bytes in @e ua
486 * Function called to find a session by address.
488 * @param cls the 'struct LookupCtx'
489 * @param key peer we are looking for (unused)
490 * @param value a session
491 * @return #GNUNET_YES if not found (continue looking), #GNUNET_NO on success
494 lookup_session_it (void *cls,
495 const struct GNUNET_PeerIdentity * key,
498 struct LookupCtx *lctx = cls;
499 struct Session *t = value;
501 if (t->addrlen != lctx->ua_len)
507 if (0 == memcmp (t->addr, lctx->ua, lctx->ua_len))
518 * Find an existing session by address.
520 * @param plugin the plugin
521 * @param sender for which peer should the session be?
522 * @param ua address to look for
523 * @param ua_len length of the address
524 * @return NULL if session was not found
526 static struct Session *
527 lookup_session (struct Plugin *plugin,
528 const struct GNUNET_PeerIdentity *sender,
529 const struct UnixAddress *ua, size_t ua_len)
531 struct LookupCtx lctx;
535 lctx.ua_len = ua_len;
536 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->session_map,
538 &lookup_session_it, &lctx);
544 * Functions with this signature are called whenever we need
545 * to close a session due to a disconnect or failure to
546 * establish a connection.
548 * @param cls closure with the `struct Plugin`
549 * @param s session to close down
550 * @return #GNUNET_OK on success
553 unix_session_disconnect (void *cls,
556 struct Plugin *plugin = cls;
557 struct UNIXMessageWrapper *msgw;
558 struct UNIXMessageWrapper *next;
561 LOG (GNUNET_ERROR_TYPE_DEBUG,
562 "Disconnecting session for peer `%s' `%s'\n",
563 GNUNET_i2s (&s->target),
565 plugin->env->session_end (plugin->env->cls, &s->target, s);
567 next = plugin->msg_head;
572 if (msgw->session != s)
574 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
575 if (NULL != msgw->cont)
576 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR,
578 GNUNET_free (msgw->msg);
580 removed = GNUNET_YES;
582 GNUNET_assert (GNUNET_YES ==
583 GNUNET_CONTAINER_multipeermap_remove (plugin->session_map,
586 GNUNET_STATISTICS_set (plugin->env->stats,
587 "# UNIX sessions active",
588 GNUNET_CONTAINER_multipeermap_size (plugin->session_map),
590 if ((GNUNET_YES == removed) && (NULL == plugin->msg_head))
591 reschedule_select (plugin);
592 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
594 GNUNET_SCHEDULER_cancel (s->timeout_task);
595 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
603 * Actually send out the message, assume we've got the address and
604 * send_handle squared away!
607 * @param send_handle which handle to send message on
608 * @param target who should receive this message (ignored by UNIX)
609 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
610 * @param msgbuf_size the size of the msgbuf to send
611 * @param priority how important is the message (ignored by UNIX)
612 * @param timeout when should we time out (give up) if we can not transmit?
613 * @param addr the addr to send the message to, needs to be a sockaddr for us
614 * @param addrlen the len of @a addr
615 * @param payload bytes payload to send
616 * @param cont continuation to call once the message has
617 * been transmitted (or if the transport is ready
618 * for the next transmission call; or if the
619 * peer disconnected...)
620 * @param cont_cls closure for @a cont
621 * @return on success the number of bytes written, RETRY for retry, -1 on errors
624 unix_real_send (void *cls,
625 struct GNUNET_NETWORK_Handle *send_handle,
626 const struct GNUNET_PeerIdentity *target, const char *msgbuf,
627 size_t msgbuf_size, unsigned int priority,
628 struct GNUNET_TIME_Absolute timeout,
629 const struct UnixAddress *addr,
632 GNUNET_TRANSPORT_TransmitContinuation cont,
635 struct Plugin *plugin = cls;
637 struct sockaddr_un *un;
639 const char *unixpath;
641 GNUNET_assert (NULL != plugin);
642 if (NULL == send_handle)
644 GNUNET_break (0); /* We do not have a send handle */
645 return GNUNET_SYSERR;
647 if ((NULL == addr) || (0 == addrlen))
649 GNUNET_break (0); /* Can never send if we don't have an address */
650 return GNUNET_SYSERR;
653 /* Prepare address */
654 unixpath = (const char *) &addr[1];
655 if (NULL == (un = unix_address_to_sockaddr (unixpath, &un_len)))
663 sent = GNUNET_NETWORK_socket_sendto (send_handle, msgbuf, msgbuf_size,
664 (const struct sockaddr *) un, un_len);
665 if (GNUNET_SYSERR == sent)
667 if ( (EAGAIN == errno) ||
671 return RETRY; /* We have to retry later */
673 if (EMSGSIZE == errno)
676 socklen_t len = sizeof (size);
678 GNUNET_NETWORK_socket_getsockopt ((struct GNUNET_NETWORK_Handle *)
679 send_handle, SOL_SOCKET, SO_SNDBUF, &size,
681 if (size < msgbuf_size)
683 LOG (GNUNET_ERROR_TYPE_DEBUG,
684 "Trying to increase socket buffer size from %i to %i for message size %i\n",
685 size, ((msgbuf_size / 1000) + 2) * 1000, msgbuf_size);
686 size = ((msgbuf_size / 1000) + 2) * 1000;
687 if (GNUNET_OK == GNUNET_NETWORK_socket_setsockopt
688 ((struct GNUNET_NETWORK_Handle *) send_handle, SOL_SOCKET, SO_SNDBUF,
689 &size, sizeof (size)))
690 goto resend; /* Increased buffer size, retry sending */
693 /* Could not increase buffer size: error, no retry */
694 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
696 return GNUNET_SYSERR;
701 /* Buffer is bigger than message: error, no retry
702 * This should never happen!*/
705 return GNUNET_SYSERR;
710 LOG (GNUNET_ERROR_TYPE_DEBUG,
711 "UNIX transmit %u-byte message to %s (%d: %s)\n",
712 (unsigned int) msgbuf_size,
713 GNUNET_a2s ((const struct sockaddr *)un, un_len),
715 (sent < 0) ? STRERROR (errno) : "ok");
722 * Closure for 'get_session_it'.
724 struct GetSessionIteratorContext
727 * Location to store the session, if found.
732 * Address information.
737 * Number of bytes in @e address
744 * Function called to find a session by address.
746 * @param cls the 'struct LookupCtx'
747 * @param key peer we are looking for (unused)
748 * @param value a session
749 * @return #GNUNET_YES if not found (continue looking), #GNUNET_NO on success
752 get_session_it (void *cls,
753 const struct GNUNET_PeerIdentity *key,
756 struct GetSessionIteratorContext *gsi = cls;
757 struct Session *s = value;
759 if ((GNUNET_NO == s->inbound) && (gsi->addrlen == s->addrlen) &&
760 (0 == memcmp (gsi->address, s->addr, s->addrlen)) )
770 * Session was idle for too long, so disconnect it
772 * @param cls the 'struct Session' to disconnect
773 * @param tc scheduler context
776 session_timeout (void *cls,
777 const struct GNUNET_SCHEDULER_TaskContext *tc)
779 struct Session *s = cls;
781 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
782 LOG (GNUNET_ERROR_TYPE_DEBUG,
783 "Session %p was idle for %s, disconnecting\n",
785 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
787 unix_session_disconnect (s->plugin, s);
792 * Function obtain the network type for a session
794 * @param cls closure ('struct Plugin*')
795 * @param session the session
796 * @return the network type in HBO or #GNUNET_SYSERR
798 static enum GNUNET_ATS_Network_Type
799 unix_get_network (void *cls,
800 struct Session *session)
802 GNUNET_assert (NULL != session);
803 return GNUNET_ATS_NET_LOOPBACK;
808 * Creates a new outbound session the transport service will use to send data to the
811 * @param cls the plugin
812 * @param address the address
813 * @return the session or NULL of max connections exceeded
815 static struct Session *
816 unix_plugin_get_session (void *cls,
817 const struct GNUNET_HELLO_Address *address)
819 struct Plugin *plugin = cls;
821 struct GetSessionIteratorContext gsi;
822 struct UnixAddress *ua;
824 uint32_t addr_str_len;
826 GNUNET_assert (NULL != plugin);
827 GNUNET_assert (NULL != address);
829 ua = (struct UnixAddress *) address->address;
830 if ((NULL == address->address) || (0 == address->address_length) ||
831 (sizeof (struct UnixAddress) > address->address_length))
836 addrstr = (char *) &ua[1];
837 addr_str_len = ntohl (ua->addrlen);
838 if (addr_str_len != address->address_length - sizeof (struct UnixAddress))
840 /* This can be a legacy address */
844 if ('\0' != addrstr[addr_str_len - 1])
849 if (strlen (addrstr) + 1 != addr_str_len)
855 /* Check if already existing */
856 gsi.address = (const char *) address->address;
857 gsi.addrlen = address->address_length;
859 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->session_map,
861 &get_session_it, &gsi);
864 LOG (GNUNET_ERROR_TYPE_DEBUG,
865 "Found existing session\n");
869 /* create a new session */
870 s = GNUNET_malloc (sizeof (struct Session) + address->address_length);
871 s->addr = (struct UnixAddress *) &s[1];
872 s->addrlen = address->address_length;
874 s->inbound = GNUNET_NO;
875 memcpy (s->addr, address->address, address->address_length);
876 memcpy (&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
877 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
878 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
881 LOG (GNUNET_ERROR_TYPE_DEBUG,
882 "Creating a new session %p for address `%s'\n",
883 s, unix_address_to_string (NULL, address->address, address->address_length));
884 (void) GNUNET_CONTAINER_multipeermap_put (plugin->session_map,
886 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
887 GNUNET_STATISTICS_set (plugin->env->stats,
888 "# UNIX sessions active",
889 GNUNET_CONTAINER_multipeermap_size (plugin->session_map),
896 * Function that can be used by the transport service to transmit
897 * a message using the plugin. Note that in the case of a
898 * peer disconnecting, the continuation MUST be called
899 * prior to the disconnect notification itself. This function
900 * will be called with this peer's HELLO message to initiate
901 * a fresh connection to another peer.
904 * @param session which session must be used
905 * @param msgbuf the message to transmit
906 * @param msgbuf_size number of bytes in @a msgbuf
907 * @param priority how important is the message (most plugins will
908 * ignore message priority and just FIFO)
909 * @param to how long to wait at most for the transmission (does not
910 * require plugins to discard the message after the timeout,
911 * just advisory for the desired delay; most plugins will ignore
913 * @param cont continuation to call once the message has
914 * been transmitted (or if the transport is ready
915 * for the next transmission call; or if the
916 * peer disconnected...); can be NULL
917 * @param cont_cls closure for @a cont
918 * @return number of bytes used (on the physical network, with overheads);
919 * -1 on hard errors (i.e. address invalid); 0 is a legal value
920 * and does NOT mean that the message was not transmitted (DV)
923 unix_plugin_send (void *cls,
924 struct Session *session,
925 const char *msgbuf, size_t msgbuf_size,
926 unsigned int priority,
927 struct GNUNET_TIME_Relative to,
928 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
930 struct Plugin *plugin = cls;
931 struct UNIXMessageWrapper *wrapper;
932 struct UNIXMessage *message;
936 GNUNET_CONTAINER_multipeermap_contains_value (plugin->session_map,
940 LOG (GNUNET_ERROR_TYPE_ERROR,
941 "Invalid session for peer `%s' `%s'\n",
942 GNUNET_i2s (&session->target),
943 (const char *) session->addr);
945 return GNUNET_SYSERR;
947 LOG (GNUNET_ERROR_TYPE_DEBUG,
948 "Sending %u bytes with session for peer `%s' `%s'\n",
950 GNUNET_i2s (&session->target),
951 (const char *) session->addr);
952 ssize = sizeof (struct UNIXMessage) + msgbuf_size;
953 message = GNUNET_malloc (sizeof (struct UNIXMessage) + msgbuf_size);
954 message->header.size = htons (ssize);
955 message->header.type = htons (0);
956 memcpy (&message->sender, plugin->env->my_identity,
957 sizeof (struct GNUNET_PeerIdentity));
958 memcpy (&message[1], msgbuf, msgbuf_size);
959 reschedule_session_timeout (session);
960 wrapper = GNUNET_new (struct UNIXMessageWrapper);
961 wrapper->msg = message;
962 wrapper->msgsize = ssize;
963 wrapper->payload = msgbuf_size;
964 wrapper->priority = priority;
965 wrapper->timeout = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), to);
966 wrapper->cont = cont;
967 wrapper->cont_cls = cont_cls;
968 wrapper->session = session;
969 GNUNET_CONTAINER_DLL_insert (plugin->msg_head,
972 plugin->bytes_in_queue += ssize;
973 GNUNET_STATISTICS_set (plugin->env->stats,
974 "# bytes currently in UNIX buffers",
975 plugin->bytes_in_queue,
977 if (GNUNET_NO == plugin->with_ws)
978 reschedule_select (plugin);
984 * Demultiplexer for UNIX messages
986 * @param plugin the main plugin for this transport
987 * @param sender from which peer the message was received
988 * @param currhdr pointer to the header of the message
989 * @param ua address to look for
990 * @param ua_len length of the address @a ua
993 unix_demultiplexer (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender,
994 const struct GNUNET_MessageHeader *currhdr,
995 const struct UnixAddress *ua, size_t ua_len)
997 struct Session *s = NULL;
998 struct GNUNET_HELLO_Address * addr;
1000 GNUNET_break (ntohl(plugin->ats_network.value) != GNUNET_ATS_NET_UNSPECIFIED);
1001 GNUNET_assert (ua_len >= sizeof (struct UnixAddress));
1002 LOG (GNUNET_ERROR_TYPE_DEBUG,
1003 "Received message from %s\n",
1004 unix_address_to_string(NULL, ua, ua_len));
1005 GNUNET_STATISTICS_update (plugin->env->stats,
1006 "# bytes received via UNIX",
1007 ntohs (currhdr->size),
1010 addr = GNUNET_HELLO_address_allocate (sender,
1014 s = lookup_session (plugin, sender, ua, ua_len);
1017 s = unix_plugin_get_session (plugin, addr);
1018 s->inbound = GNUNET_YES;
1019 /* Notify transport and ATS about new inbound session */
1020 plugin->env->session_start (NULL, sender,
1021 PLUGIN_NAME, ua, ua_len, s, &plugin->ats_network, 1);
1023 reschedule_session_timeout (s);
1025 plugin->env->receive (plugin->env->cls, sender, currhdr, s,
1026 (GNUNET_YES == s->inbound) ? NULL : (const char *) ua,
1027 (GNUNET_YES == s->inbound) ? 0 : ua_len);
1029 plugin->env->update_address_metrics (plugin->env->cls, sender,
1030 (GNUNET_YES == s->inbound) ? NULL : (const char *) ua,
1031 (GNUNET_YES == s->inbound) ? 0 : ua_len,
1032 s, &plugin->ats_network, 1);
1039 * Read from UNIX domain socket (it is ready).
1041 * @param plugin the plugin
1044 unix_plugin_select_read (struct Plugin *plugin)
1046 char buf[65536] GNUNET_ALIGN;
1047 struct UnixAddress *ua;
1048 struct UNIXMessage *msg;
1049 struct GNUNET_PeerIdentity sender;
1050 struct sockaddr_un un;
1056 const struct GNUNET_MessageHeader *currhdr;
1060 addrlen = sizeof (un);
1061 memset (&un, 0, sizeof (un));
1064 GNUNET_NETWORK_socket_recvfrom (plugin->unix_sock.desc, buf, sizeof (buf),
1065 (struct sockaddr *) &un, &addrlen);
1067 if ((GNUNET_SYSERR == ret) && ((errno == EAGAIN) || (errno == ENOBUFS)))
1070 if (ret == GNUNET_SYSERR)
1072 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom");
1077 LOG (GNUNET_ERROR_TYPE_DEBUG,
1078 "Read %d bytes from socket %s\n",
1083 GNUNET_assert (AF_UNIX == (un.sun_family));
1084 ua_len = sizeof (struct UnixAddress) + strlen (un.sun_path) + 1;
1085 ua = GNUNET_malloc (ua_len);
1086 ua->addrlen = htonl (strlen (&un.sun_path[0]) +1);
1087 ua->options = htonl (0);
1088 memcpy (&ua[1], &un.sun_path[0], strlen (un.sun_path) + 1);
1090 msg = (struct UNIXMessage *) buf;
1091 csize = ntohs (msg->header.size);
1092 if ((csize < sizeof (struct UNIXMessage)) || (csize > ret))
1094 GNUNET_break_op (0);
1098 msgbuf = (char *) &msg[1];
1099 memcpy (&sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
1101 tsize = csize - sizeof (struct UNIXMessage);
1102 while (offset + sizeof (struct GNUNET_MessageHeader) <= tsize)
1104 currhdr = (struct GNUNET_MessageHeader *) &msgbuf[offset];
1105 csize = ntohs (currhdr->size);
1106 if ((csize < sizeof (struct GNUNET_MessageHeader)) ||
1107 (csize > tsize - offset))
1109 GNUNET_break_op (0);
1112 unix_demultiplexer (plugin, &sender, currhdr, ua, ua_len);
1120 * Write to UNIX domain socket (it is ready).
1122 * @param plugin the plugin
1125 unix_plugin_select_write (struct Plugin *plugin)
1128 struct UNIXMessageWrapper * msgw;
1130 while (NULL != (msgw = plugin->msg_tail))
1132 if (GNUNET_TIME_absolute_get_remaining (msgw->timeout).rel_value_us > 0)
1133 break; /* Message is ready for sending */
1134 /* Message has a timeout */
1135 LOG (GNUNET_ERROR_TYPE_DEBUG,
1136 "Timeout for message with %u bytes \n",
1137 (unsigned int) msgw->msgsize);
1138 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
1139 plugin->bytes_in_queue -= msgw->msgsize;
1140 GNUNET_STATISTICS_set (plugin->env->stats,
1141 "# bytes currently in UNIX buffers",
1142 plugin->bytes_in_queue, GNUNET_NO);
1143 GNUNET_STATISTICS_update (plugin->env->stats,
1144 "# UNIX bytes discarded",
1147 if (NULL != msgw->cont)
1148 msgw->cont (msgw->cont_cls,
1149 &msgw->session->target,
1153 GNUNET_free (msgw->msg);
1157 return; /* Nothing to send at the moment */
1159 sent = unix_real_send (plugin,
1160 plugin->unix_sock.desc,
1161 &msgw->session->target,
1162 (const char *) msgw->msg,
1166 msgw->session->addr,
1167 msgw->session->addrlen,
1169 msgw->cont, msgw->cont_cls);
1173 GNUNET_STATISTICS_update (plugin->env->stats,
1174 "# UNIX retry attempts",
1178 if (GNUNET_SYSERR == sent)
1180 /* failed and no retry */
1181 if (NULL != msgw->cont)
1182 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR, msgw->payload, 0);
1184 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
1186 GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
1187 plugin->bytes_in_queue -= msgw->msgsize;
1188 GNUNET_STATISTICS_set (plugin->env->stats,
1189 "# bytes currently in UNIX buffers",
1190 plugin->bytes_in_queue, GNUNET_NO);
1191 GNUNET_STATISTICS_update (plugin->env->stats,
1192 "# UNIX bytes discarded",
1196 GNUNET_free (msgw->msg);
1200 /* successfully sent bytes */
1201 GNUNET_break (sent > 0);
1202 GNUNET_CONTAINER_DLL_remove (plugin->msg_head,
1205 GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
1206 plugin->bytes_in_queue -= msgw->msgsize;
1207 GNUNET_STATISTICS_set (plugin->env->stats,
1208 "# bytes currently in UNIX buffers",
1209 plugin->bytes_in_queue,
1211 GNUNET_STATISTICS_update (plugin->env->stats,
1212 "# bytes transmitted via UNIX",
1215 if (NULL != msgw->cont)
1216 msgw->cont (msgw->cont_cls, &msgw->session->target,
1220 GNUNET_free (msgw->msg);
1226 * We have been notified that our writeset has something to read. We don't
1227 * know which socket needs to be read, so we have to check each one
1228 * Then reschedule this function to be called again once more is available.
1230 * @param cls the plugin handle
1231 * @param tc the scheduling context (for rescheduling this function again)
1234 unix_plugin_select (void *cls,
1235 const struct GNUNET_SCHEDULER_TaskContext *tc)
1237 struct Plugin *plugin = cls;
1239 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1240 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1243 if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
1245 /* Ready to send data */
1246 GNUNET_assert (GNUNET_NETWORK_fdset_isset
1247 (tc->write_ready, plugin->unix_sock.desc));
1248 if (NULL != plugin->msg_head)
1249 unix_plugin_select_write (plugin);
1252 if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
1254 /* Ready to receive data */
1255 GNUNET_assert (GNUNET_NETWORK_fdset_isset
1256 (tc->read_ready, plugin->unix_sock.desc));
1257 unix_plugin_select_read (plugin);
1259 reschedule_select (plugin);
1264 * Create a slew of UNIX sockets. If possible, use IPv6 and IPv4.
1266 * @param cls closure for server start, should be a struct Plugin *
1267 * @return number of sockets created or #GNUNET_SYSERR on error
1270 unix_transport_server_start (void *cls)
1272 struct Plugin *plugin = cls;
1273 struct sockaddr_un *un;
1276 un = unix_address_to_sockaddr (plugin->unix_socket_path, &un_len);
1277 plugin->ats_network = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) un, un_len);
1278 plugin->unix_sock.desc =
1279 GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_DGRAM, 0);
1280 if (NULL == plugin->unix_sock.desc)
1282 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
1283 return GNUNET_SYSERR;
1286 GNUNET_NETWORK_socket_bind (plugin->unix_sock.desc, (const struct sockaddr *) un, un_len))
1288 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
1289 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc);
1290 plugin->unix_sock.desc = NULL;
1292 return GNUNET_SYSERR;
1294 LOG (GNUNET_ERROR_TYPE_DEBUG, "Bound to `%s'\n", plugin->unix_socket_path);
1295 plugin->rs = GNUNET_NETWORK_fdset_create ();
1296 plugin->ws = GNUNET_NETWORK_fdset_create ();
1297 GNUNET_NETWORK_fdset_zero (plugin->rs);
1298 GNUNET_NETWORK_fdset_zero (plugin->ws);
1299 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->unix_sock.desc);
1300 GNUNET_NETWORK_fdset_set (plugin->ws, plugin->unix_sock.desc);
1302 reschedule_select (plugin);
1309 * Function that will be called to check if a binary address for this
1310 * plugin is well-formed and corresponds to an address for THIS peer
1311 * (as per our configuration). Naturally, if absolutely necessary,
1312 * plugins can be a bit conservative in their answer, but in general
1313 * plugins should make sure that the address does not redirect
1314 * traffic to a 3rd party that might try to man-in-the-middle our
1317 * @param cls closure, should be our handle to the Plugin
1318 * @param addr pointer to the address
1319 * @param addrlen length of addr
1320 * @return GNUNET_OK if this is a plausible address for this peer
1321 * and transport, GNUNET_SYSERR if not
1325 unix_check_address (void *cls, const void *addr, size_t addrlen)
1327 struct Plugin* plugin = cls;
1328 struct UnixAddress *ua = (struct UnixAddress *) addr;
1330 size_t addr_str_len;
1332 if ((NULL == addr) || (0 == addrlen) || (sizeof (struct UnixAddress) > addrlen))
1335 return GNUNET_SYSERR;
1337 addrstr = (char *) &ua[1];
1338 addr_str_len = ntohl (ua->addrlen);
1339 if ('\0' != addrstr[addr_str_len - 1])
1342 return GNUNET_SYSERR;
1344 if (strlen (addrstr) + 1 != addr_str_len)
1347 return GNUNET_SYSERR;
1350 if (0 == strcmp (plugin->unix_socket_path, addrstr))
1352 return GNUNET_SYSERR;
1357 * Convert the transports address to a nice, human-readable
1360 * @param cls closure
1361 * @param type name of the transport that generated the address
1362 * @param addr one of the addresses of the host, NULL for the last address
1363 * the specific address format depends on the transport
1364 * @param addrlen length of the @a addr
1365 * @param numeric should (IP) addresses be displayed in numeric form?
1366 * @param timeout after how long should we give up?
1367 * @param asc function to call on each string
1368 * @param asc_cls closure for @a asc
1371 unix_plugin_address_pretty_printer (void *cls, const char *type,
1372 const void *addr, size_t addrlen,
1374 struct GNUNET_TIME_Relative timeout,
1375 GNUNET_TRANSPORT_AddressStringCallback asc,
1378 if ((NULL != addr) && (addrlen > 0))
1380 asc (asc_cls, unix_address_to_string (NULL, addr, addrlen));
1382 else if (0 == addrlen)
1384 asc (asc_cls, TRANSPORT_SESSION_INBOUND_STRING);
1389 asc (asc_cls, "<invalid UNIX address>");
1391 asc (asc_cls, NULL);
1396 * Function called to convert a string address to
1399 * @param cls closure ('struct Plugin*')
1400 * @param addr string address
1401 * @param addrlen length of the @a addr (strlen(addr) + '\0')
1402 * @param buf location to store the buffer
1403 * If the function returns #GNUNET_SYSERR, its contents are undefined.
1404 * @param added length of created address
1405 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1408 unix_string_to_address (void *cls,
1409 const char *addr, uint16_t addrlen,
1410 void **buf, size_t *added)
1412 struct UnixAddress *ua;
1419 /* Format unix.options.address */
1424 if ((NULL == addr) || (addrlen == 0))
1427 return GNUNET_SYSERR;
1429 if ('\0' != addr[addrlen - 1])
1432 return GNUNET_SYSERR;
1434 if (strlen (addr) != addrlen - 1)
1437 return GNUNET_SYSERR;
1439 plugin = GNUNET_strdup (addr);
1440 optionstr = strchr (plugin, '.');
1441 if (NULL == optionstr)
1444 GNUNET_free (plugin);
1445 return GNUNET_SYSERR;
1447 optionstr[0] = '\0';
1449 options = atol (optionstr);
1450 address = strchr (optionstr, '.');
1451 if (NULL == address)
1454 GNUNET_free (plugin);
1455 return GNUNET_SYSERR;
1459 if (0 != strcmp(plugin, PLUGIN_NAME))
1462 GNUNET_free (plugin);
1463 return GNUNET_SYSERR;
1466 ua_size = sizeof (struct UnixAddress) + strlen (address) + 1;
1467 ua = GNUNET_malloc (ua_size);
1468 ua->options = htonl (options);
1469 ua->addrlen = htonl (strlen (address) + 1);
1470 memcpy (&ua[1], address, strlen (address) + 1);
1471 GNUNET_free (plugin);
1480 * Notify transport service about address
1482 * @param cls the plugin
1486 address_notification (void *cls,
1487 const struct GNUNET_SCHEDULER_TaskContext *tc)
1489 struct Plugin *plugin = cls;
1491 struct UnixAddress *ua;
1493 len = sizeof (struct UnixAddress) + strlen (plugin->unix_socket_path) + 1;
1494 ua = GNUNET_malloc (len);
1495 ua->options = htonl (myoptions);
1496 ua->addrlen = htonl(strlen (plugin->unix_socket_path) + 1);
1497 memcpy (&ua[1], plugin->unix_socket_path, strlen (plugin->unix_socket_path) + 1);
1499 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1500 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
1507 * Increment session timeout due to activity
1509 * @param s session for which the timeout should be moved
1512 reschedule_session_timeout (struct Session *s)
1514 GNUNET_assert (NULL != s);
1515 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1516 GNUNET_SCHEDULER_cancel (s->timeout_task);
1517 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1520 LOG (GNUNET_ERROR_TYPE_DEBUG,
1521 "Timeout rescheduled for session %p set to %s\n",
1523 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1529 * Function called on sessions to disconnect
1531 * @param cls the plugin
1532 * @param key peer identity (unused)
1533 * @param value the 'struct Session' to disconnect
1534 * @return #GNUNET_YES (always, continue to iterate)
1537 get_session_delete_it (void *cls,
1538 const struct GNUNET_PeerIdentity *key,
1541 struct Plugin *plugin = cls;
1542 struct Session *s = value;
1544 unix_session_disconnect (plugin, s);
1550 * Disconnect from a remote node. Clean up session if we have one for this peer
1552 * @param cls closure for this call (should be handle to Plugin)
1553 * @param target the peeridentity of the peer to disconnect
1554 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the operation failed
1557 unix_peer_disconnect (void *cls,
1558 const struct GNUNET_PeerIdentity *target)
1560 struct Plugin *plugin = cls;
1562 GNUNET_assert (plugin != NULL);
1563 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->session_map,
1565 &get_session_delete_it, plugin);
1570 * The exported method. Initializes the plugin and returns a
1571 * struct with the callbacks.
1573 * @param cls the plugin's execution environment
1574 * @return NULL on error, plugin functions otherwise
1577 libgnunet_plugin_transport_unix_init (void *cls)
1579 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1580 unsigned long long port;
1581 struct GNUNET_TRANSPORT_PluginFunctions *api;
1582 struct Plugin *plugin;
1583 int sockets_created;
1585 if (NULL == env->receive)
1587 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1588 initialze the plugin or the API */
1589 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
1591 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1592 api->address_to_string = &unix_address_to_string;
1593 api->string_to_address = &unix_string_to_address;
1597 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-unix", "PORT",
1599 port = UNIX_NAT_DEFAULT_PORT;
1600 plugin = GNUNET_new (struct Plugin);
1601 plugin->port = port;
1603 GNUNET_asprintf (&plugin->unix_socket_path,
1604 "/tmp/unix-plugin-sock.%d",
1607 /* Initialize my flags */
1610 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
1613 api->get_session = &unix_plugin_get_session;
1614 api->send = &unix_plugin_send;
1615 api->disconnect_peer = &unix_peer_disconnect;
1616 api->disconnect_session = &unix_session_disconnect;
1617 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1618 api->address_to_string = &unix_address_to_string;
1619 api->check_address = &unix_check_address;
1620 api->string_to_address = &unix_string_to_address;
1621 api->get_network = &unix_get_network;
1622 sockets_created = unix_transport_server_start (plugin);
1623 if (0 == sockets_created)
1624 LOG (GNUNET_ERROR_TYPE_WARNING,
1625 _("Failed to open UNIX listen socket\n"));
1626 plugin->session_map = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1627 plugin->address_update_task = GNUNET_SCHEDULER_add_now (&address_notification, plugin);
1633 * Shutdown the plugin.
1635 * @param cls the plugin API returned from the initialization function
1636 * @return NULL (always)
1639 libgnunet_plugin_transport_unix_done (void *cls)
1641 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1642 struct Plugin *plugin = api->cls;
1643 struct UNIXMessageWrapper * msgw;
1644 struct UnixAddress *ua;
1653 len = sizeof (struct UnixAddress) + strlen (plugin->unix_socket_path) + 1;
1654 ua = GNUNET_malloc (len);
1655 ua->options = htonl (myoptions);
1656 ua->addrlen = htonl(strlen (plugin->unix_socket_path) + 1);
1657 memcpy (&ua[1], plugin->unix_socket_path, strlen (plugin->unix_socket_path) + 1);
1659 plugin->env->notify_address (plugin->env->cls, GNUNET_NO,
1662 while (NULL != (msgw = plugin->msg_head))
1664 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
1665 if (msgw->cont != NULL)
1666 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR,
1668 GNUNET_free (msgw->msg);
1672 if (GNUNET_SCHEDULER_NO_TASK != plugin->select_task)
1674 GNUNET_SCHEDULER_cancel (plugin->select_task);
1675 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1677 if (GNUNET_SCHEDULER_NO_TASK != plugin->address_update_task)
1679 GNUNET_SCHEDULER_cancel (plugin->address_update_task);
1680 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1682 if (NULL != plugin->unix_sock.desc)
1684 GNUNET_break (GNUNET_OK ==
1685 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc));
1686 plugin->unix_sock.desc = NULL;
1687 plugin->with_ws = GNUNET_NO;
1689 GNUNET_CONTAINER_multipeermap_iterate (plugin->session_map,
1690 &get_session_delete_it, plugin);
1691 GNUNET_CONTAINER_multipeermap_destroy (plugin->session_map);
1692 if (NULL != plugin->rs)
1693 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1694 if (NULL != plugin->ws)
1695 GNUNET_NETWORK_fdset_destroy (plugin->ws);
1696 GNUNET_free (plugin->unix_socket_path);
1697 GNUNET_free (plugin);
1702 /* end of plugin_transport_unix.c */