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"
48 enum UNIX_ADDRESS_OPTIONS
50 UNIX_OPTIONS_NONE = 0,
51 UNIX_OPTIONS_USE_ABSTRACT_SOCKETS = 1
56 * How long until we give up on transmitting the welcome message?
58 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
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
92 * Handle for a session.
96 struct GNUNET_PeerIdentity target;
98 struct Plugin * plugin;
100 struct GNUNET_HELLO_Address *address;
103 * Session timeout task
105 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
109 struct UNIXMessageWrapper
112 * We keep messages in a doubly linked list.
114 struct UNIXMessageWrapper *next;
117 * We keep messages in a doubly linked list.
119 struct UNIXMessageWrapper *prev;
122 * The actual payload (allocated separately right now).
124 struct UNIXMessage * msg;
127 * Session this message belongs to.
129 struct Session *session;
132 * Function to call upon transmission.
134 GNUNET_TRANSPORT_TransmitContinuation cont;
137 * Closure for 'cont'.
142 * Timeout for this message.
144 struct GNUNET_TIME_Absolute timeout;
147 * Number of bytes in 'msg'.
152 * Number of bytes of payload encapsulated in 'msg'.
157 * Priority of the message (ignored, just dragged along in UNIX).
159 unsigned int priority;
164 * Encapsulation of all of the state of the plugin.
176 * Stored in a linked list.
178 struct PeerSession *next;
181 * Pointer to the global plugin struct.
183 struct Plugin *plugin;
186 * To whom are we talking to (set to our identity
187 * if we are still waiting for the welcome message)
189 struct GNUNET_PeerIdentity target;
192 * Address of the other peer (either based on our 'connect'
193 * call or on our 'accept' call).
198 * Length of connect_addr.
203 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
205 int expecting_welcome;
208 * From which socket do we need to send to this peer?
210 struct GNUNET_NETWORK_Handle *sock;
213 * Queue of messages for this peer, in the case that
214 * we have to await a connection...
216 struct MessageQueue *messages;
222 * Information we keep for each of our listen sockets.
224 struct UNIX_Sock_Info
229 struct GNUNET_NETWORK_Handle *desc;
232 * The port we bound to (not an actual PORT, as UNIX domain sockets
233 * don't have ports, but rather a number in the path name to make this
241 * Encapsulation of all of the state of the plugin.
247 * ID of task used to update our addresses when one expires.
249 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
254 GNUNET_SCHEDULER_TaskIdentifier select_task;
257 * Number of bytes we currently have in our write queue.
259 unsigned long long bytes_in_queue;
264 struct GNUNET_TRANSPORT_PluginEnvironment *env;
269 struct GNUNET_CONTAINER_MultiPeerMap *session_map;
274 struct GNUNET_NETWORK_FDSet *rs;
279 struct GNUNET_NETWORK_FDSet *ws;
282 * Path of our unix domain socket (/tmp/unix-plugin-PORT)
284 char *unix_socket_path;
287 * Head of queue of messages to transmit.
289 struct UNIXMessageWrapper *msg_head;
292 * Tail of queue of messages to transmit.
294 struct UNIXMessageWrapper *msg_tail;
297 * socket that we transmit all data with
299 struct UNIX_Sock_Info unix_sock;
302 * Address options in HBO
310 struct GNUNET_ATS_Information ats_network;
313 * Is the write set in the current 'select' task? GNUNET_NO if the
314 * write queue was empty when the main task was scheduled,
315 * GNUNET_YES if we're already waiting for being allowed to write.
324 * Increment session timeout due to activity
326 * @param s session for which the timeout should be moved
329 reschedule_session_timeout (struct Session *s);
333 * We have been notified that our writeset has something to read. We don't
334 * know which socket needs to be read, so we have to check each one
335 * Then reschedule this function to be called again once more is available.
337 * @param cls the plugin handle
338 * @param tc the scheduling context (for rescheduling this function again)
341 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
343 static struct sockaddr_un *
344 unix_address_to_sockaddr (const char *unixpath,
347 struct sockaddr_un *un;
350 GNUNET_assert (0 < strlen (unixpath)); /* sanity check */
351 un = GNUNET_new (struct sockaddr_un);
352 un->sun_family = AF_UNIX;
353 slen = strlen (unixpath);
354 if (slen >= sizeof (un->sun_path))
355 slen = sizeof (un->sun_path) - 1;
356 memcpy (un->sun_path, unixpath, slen);
357 un->sun_path[slen] = '\0';
358 slen = sizeof (struct sockaddr_un);
359 #if HAVE_SOCKADDR_IN_SIN_LEN
360 un->sun_len = (u_char) slen;
368 * Function called for a quick conversion of the binary address to
369 * a numeric address. Note that the caller must not free the
370 * address and that the next call to this function is allowed
371 * to override the address again.
374 * @param addr binary address
375 * @param addrlen length of the address
376 * @return string representing the same address
379 unix_address_to_string (void *cls, const void *addr, size_t addrlen)
381 static char rbuf[1024];
382 struct UnixAddress *ua = (struct UnixAddress *) addr;
387 if ((NULL == addr) || (sizeof (struct UnixAddress) > addrlen))
392 addrstr = (char *) &ua[1];
393 addr_str_len = ntohl (ua->addrlen);
395 if (addr_str_len != addrlen - sizeof(struct UnixAddress))
401 if ('\0' != addrstr[addr_str_len - 1])
406 if (strlen (addrstr) + 1 != addr_str_len)
413 if ('\0' == addrstr[0])
415 memset (rbuf, 0, sizeof (rbuf));
416 GNUNET_snprintf (rbuf, sizeof (rbuf) - 1, "%s.%u.%s%.*s",
419 (off == 1) ? "@" : "",
420 (int) (addr_str_len - off),
423 GNUNET_snprintf (rbuf, sizeof(rbuf), "%s.%u.%s", PLUGIN_NAME,
424 ntohl (ua->options), addrstr);*/
432 * Re-schedule the main 'select' callback (unix_plugin_select)
435 * @param plugin the plugin context
438 reschedule_select (struct Plugin * plugin)
440 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
442 GNUNET_SCHEDULER_cancel (plugin->select_task);
443 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
445 if (NULL != plugin->msg_head)
447 plugin->select_task =
448 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
449 GNUNET_TIME_UNIT_FOREVER_REL,
452 &unix_plugin_select, plugin);
453 plugin->with_ws = GNUNET_YES;
457 plugin->select_task =
458 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
459 GNUNET_TIME_UNIT_FOREVER_REL,
462 &unix_plugin_select, plugin);
463 plugin->with_ws = GNUNET_NO;
469 * Closure to #lookup_session_it.
474 * Location to store the session, if found.
478 struct GNUNET_HELLO_Address *address;
483 * Function called to find a session by address.
485 * @param cls the 'struct LookupCtx'
486 * @param key peer we are looking for (unused)
487 * @param value a session
488 * @return #GNUNET_YES if not found (continue looking), #GNUNET_NO on success
491 lookup_session_it (void *cls,
492 const struct GNUNET_PeerIdentity * key,
495 struct LookupCtx *lctx = cls;
496 struct Session *s = value;
498 if (0 == GNUNET_HELLO_address_cmp (lctx->address, s->address))
508 * Find an existing session by address.
510 * @param plugin the plugin
511 * @param address the address to find
512 * @return NULL if session was not found
514 static struct Session *
515 lookup_session (struct Plugin *plugin,
516 struct GNUNET_HELLO_Address *address)
518 struct LookupCtx lctx;
520 lctx.address = address;
522 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->session_map,
524 &lookup_session_it, &lctx);
530 * Functions with this signature are called whenever we need
531 * to close a session due to a disconnect or failure to
532 * establish a connection.
534 * @param cls closure with the `struct Plugin`
535 * @param s session to close down
536 * @return #GNUNET_OK on success
539 unix_session_disconnect (void *cls,
542 struct Plugin *plugin = cls;
543 struct UNIXMessageWrapper *msgw;
544 struct UNIXMessageWrapper *next;
547 LOG (GNUNET_ERROR_TYPE_DEBUG,
548 "Disconnecting session for peer `%s' `%s'\n",
549 GNUNET_i2s (&s->target),
550 unix_address_to_string (NULL, s->address->address, s->address->address_length) );
551 plugin->env->session_end (plugin->env->cls, s->address, s);
553 next = plugin->msg_head;
558 if (msgw->session != s)
560 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
561 if (NULL != msgw->cont)
562 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR,
564 GNUNET_free (msgw->msg);
566 removed = GNUNET_YES;
568 GNUNET_assert (GNUNET_YES ==
569 GNUNET_CONTAINER_multipeermap_remove (plugin->session_map,
572 GNUNET_STATISTICS_set (plugin->env->stats,
573 "# UNIX sessions active",
574 GNUNET_CONTAINER_multipeermap_size (plugin->session_map),
576 if ((GNUNET_YES == removed) && (NULL == plugin->msg_head))
577 reschedule_select (plugin);
578 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
580 GNUNET_SCHEDULER_cancel (s->timeout_task);
581 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
583 GNUNET_HELLO_address_free (s->address);
590 * Function that is called to get the keepalive factor.
591 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
592 * calculate the interval between keepalive packets.
594 * @param cls closure with the `struct Plugin`
595 * @return keepalive factor
598 unix_query_keepalive_factor (void *cls)
605 * Actually send out the message, assume we've got the address and
606 * send_handle squared away!
609 * @param send_handle which handle to send message on
610 * @param target who should receive this message (ignored by UNIX)
611 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
612 * @param msgbuf_size the size of the msgbuf to send
613 * @param priority how important is the message (ignored by UNIX)
614 * @param timeout when should we time out (give up) if we can not transmit?
615 * @param addr the addr to send the message to, needs to be a sockaddr for us
616 * @param addrlen the len of @a addr
617 * @param payload bytes payload to send
618 * @param cont continuation to call once the message has
619 * been transmitted (or if the transport is ready
620 * for the next transmission call; or if the
621 * peer disconnected...)
622 * @param cont_cls closure for @a cont
623 * @return on success the number of bytes written, RETRY for retry, -1 on errors
626 unix_real_send (void *cls,
627 struct GNUNET_NETWORK_Handle *send_handle,
628 const struct GNUNET_PeerIdentity *target, const char *msgbuf,
629 size_t msgbuf_size, unsigned int priority,
630 struct GNUNET_TIME_Absolute timeout,
631 const struct UnixAddress *addr,
634 GNUNET_TRANSPORT_TransmitContinuation cont,
637 struct Plugin *plugin = cls;
639 struct sockaddr_un *un;
641 const char *unixpath;
643 GNUNET_assert (NULL != plugin);
644 if (NULL == send_handle)
646 GNUNET_break (0); /* We do not have a send handle */
647 return GNUNET_SYSERR;
649 if ((NULL == addr) || (0 == addrlen))
651 GNUNET_break (0); /* Can never send if we don't have an address */
652 return GNUNET_SYSERR;
655 /* Prepare address */
656 unixpath = (const char *) &addr[1];
657 if (NULL == (un = unix_address_to_sockaddr (unixpath,
664 if ((GNUNET_YES == plugin->abstract) &&
665 (0 != (UNIX_OPTIONS_USE_ABSTRACT_SOCKETS & ntohl(addr->options) )) )
667 un->sun_path[0] = '\0';
671 sent = GNUNET_NETWORK_socket_sendto (send_handle, msgbuf, msgbuf_size,
672 (const struct sockaddr *) un, un_len);
673 if (GNUNET_SYSERR == sent)
675 if ( (EAGAIN == errno) ||
679 return RETRY; /* We have to retry later */
681 if (EMSGSIZE == errno)
684 socklen_t len = sizeof (size);
686 GNUNET_NETWORK_socket_getsockopt ((struct GNUNET_NETWORK_Handle *)
687 send_handle, SOL_SOCKET, SO_SNDBUF, &size,
689 if (size < msgbuf_size)
691 LOG (GNUNET_ERROR_TYPE_DEBUG,
692 "Trying to increase socket buffer size from %i to %i for message size %i\n",
693 size, ((msgbuf_size / 1000) + 2) * 1000, msgbuf_size);
694 size = ((msgbuf_size / 1000) + 2) * 1000;
695 if (GNUNET_OK == GNUNET_NETWORK_socket_setsockopt
696 ((struct GNUNET_NETWORK_Handle *) send_handle, SOL_SOCKET, SO_SNDBUF,
697 &size, sizeof (size)))
698 goto resend; /* Increased buffer size, retry sending */
701 /* Could not increase buffer size: error, no retry */
702 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
704 return GNUNET_SYSERR;
709 /* Buffer is bigger than message: error, no retry
710 * This should never happen!*/
713 return GNUNET_SYSERR;
718 LOG (GNUNET_ERROR_TYPE_DEBUG,
719 "UNIX transmit %u-byte message to %s (%d: %s)\n",
720 (unsigned int) msgbuf_size,
721 GNUNET_a2s ((const struct sockaddr *)un, un_len),
723 (sent < 0) ? STRERROR (errno) : "ok");
730 * Closure for 'get_session_it'.
732 struct GetSessionIteratorContext
735 * Location to store the session, if found.
740 * Address information.
742 const struct GNUNET_HELLO_Address *address;
747 * Function called to find a session by address.
749 * @param cls the 'struct LookupCtx'
750 * @param key peer we are looking for (unused)
751 * @param value a session
752 * @return #GNUNET_YES if not found (continue looking), #GNUNET_NO on success
755 get_session_it (void *cls,
756 const struct GNUNET_PeerIdentity *key,
759 struct GetSessionIteratorContext *gsi = cls;
760 struct Session *s = value;
762 if (0 == GNUNET_HELLO_address_cmp(s->address, gsi->address))
772 * Session was idle for too long, so disconnect it
774 * @param cls the 'struct Session' to disconnect
775 * @param tc scheduler context
778 session_timeout (void *cls,
779 const struct GNUNET_SCHEDULER_TaskContext *tc)
781 struct Session *s = cls;
783 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
784 LOG (GNUNET_ERROR_TYPE_DEBUG,
785 "Session %p was idle for %s, disconnecting\n",
787 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
789 unix_session_disconnect (s->plugin, s);
794 * Function obtain the network type for a session
796 * @param cls closure ('struct Plugin*')
797 * @param session the session
798 * @return the network type in HBO or #GNUNET_SYSERR
800 static enum GNUNET_ATS_Network_Type
801 unix_get_network (void *cls,
802 struct Session *session)
804 GNUNET_assert (NULL != session);
805 return GNUNET_ATS_NET_LOOPBACK;
810 * Creates a new outbound session the transport service will use to send data to the
813 * @param cls the plugin
814 * @param address the address
815 * @return the session or NULL of max connections exceeded
817 static struct Session *
818 unix_plugin_get_session (void *cls,
819 const struct GNUNET_HELLO_Address *address)
821 struct Plugin *plugin = cls;
823 struct GetSessionIteratorContext gsi;
824 struct UnixAddress *ua;
826 uint32_t addr_str_len;
827 uint32_t addr_option;
829 GNUNET_assert (NULL != plugin);
830 GNUNET_assert (NULL != address);
832 ua = (struct UnixAddress *) address->address;
833 if ((NULL == address->address) || (0 == address->address_length) ||
834 (sizeof (struct UnixAddress) > address->address_length))
839 addrstr = (char *) &ua[1];
840 addr_str_len = ntohl (ua->addrlen);
841 addr_option = ntohl (ua->options);
843 if ( (0 != (UNIX_OPTIONS_USE_ABSTRACT_SOCKETS & addr_option)) &&
844 (GNUNET_NO == plugin->abstract))
849 if (addr_str_len != address->address_length - sizeof (struct UnixAddress))
851 return NULL; /* This can be a legacy address */
854 if ('\0' != addrstr[addr_str_len - 1])
859 if (strlen (addrstr) + 1 != addr_str_len)
865 /* Check if already existing */
866 gsi.address = address;
868 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->session_map,
870 &get_session_it, &gsi);
873 LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p for address `%s'\n",
875 unix_address_to_string (NULL, address->address, address->address_length));
879 /* create a new session */
880 s = GNUNET_new (struct Session);
881 s->target = address->peer;
882 s->address = GNUNET_HELLO_address_copy (address);
884 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
885 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
888 LOG (GNUNET_ERROR_TYPE_DEBUG,
889 "Creating a new session %p for address `%s'\n",
890 s, unix_address_to_string (NULL, address->address, address->address_length));
891 (void) GNUNET_CONTAINER_multipeermap_put (plugin->session_map,
893 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
894 GNUNET_STATISTICS_set (plugin->env->stats,
895 "# UNIX sessions active",
896 GNUNET_CONTAINER_multipeermap_size (plugin->session_map),
902 unix_plugin_update_session_timeout (void *cls,
903 const struct GNUNET_PeerIdentity *peer,
904 struct Session *session)
906 struct Plugin *plugin = cls;
909 GNUNET_CONTAINER_multipeermap_contains_value (plugin->session_map,
913 reschedule_session_timeout (session);
917 * Function that can be used by the transport service to transmit
918 * a message using the plugin. Note that in the case of a
919 * peer disconnecting, the continuation MUST be called
920 * prior to the disconnect notification itself. This function
921 * will be called with this peer's HELLO message to initiate
922 * a fresh connection to another peer.
925 * @param session which session must be used
926 * @param msgbuf the message to transmit
927 * @param msgbuf_size number of bytes in @a msgbuf
928 * @param priority how important is the message (most plugins will
929 * ignore message priority and just FIFO)
930 * @param to how long to wait at most for the transmission (does not
931 * require plugins to discard the message after the timeout,
932 * just advisory for the desired delay; most plugins will ignore
934 * @param cont continuation to call once the message has
935 * been transmitted (or if the transport is ready
936 * for the next transmission call; or if the
937 * peer disconnected...); can be NULL
938 * @param cont_cls closure for @a cont
939 * @return number of bytes used (on the physical network, with overheads);
940 * -1 on hard errors (i.e. address invalid); 0 is a legal value
941 * and does NOT mean that the message was not transmitted (DV)
944 unix_plugin_send (void *cls,
945 struct Session *session,
946 const char *msgbuf, size_t msgbuf_size,
947 unsigned int priority,
948 struct GNUNET_TIME_Relative to,
949 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
951 struct Plugin *plugin = cls;
952 struct UNIXMessageWrapper *wrapper;
953 struct UNIXMessage *message;
957 GNUNET_CONTAINER_multipeermap_contains_value (plugin->session_map,
961 LOG (GNUNET_ERROR_TYPE_ERROR,
962 "Invalid session for peer `%s' `%s'\n",
963 GNUNET_i2s (&session->target),
964 unix_address_to_string(NULL, session->address->address,
965 session->address->address_length));
967 return GNUNET_SYSERR;
969 LOG (GNUNET_ERROR_TYPE_DEBUG,
970 "Sending %u bytes with session for peer `%s' `%s'\n",
972 GNUNET_i2s (&session->target),
973 unix_address_to_string(NULL, session->address->address,
974 session->address->address_length));
975 ssize = sizeof (struct UNIXMessage) + msgbuf_size;
976 message = GNUNET_malloc (sizeof (struct UNIXMessage) + msgbuf_size);
977 message->header.size = htons (ssize);
978 message->header.type = htons (0);
979 memcpy (&message->sender, plugin->env->my_identity,
980 sizeof (struct GNUNET_PeerIdentity));
981 memcpy (&message[1], msgbuf, msgbuf_size);
983 wrapper = GNUNET_new (struct UNIXMessageWrapper);
984 wrapper->msg = message;
985 wrapper->msgsize = ssize;
986 wrapper->payload = msgbuf_size;
987 wrapper->priority = priority;
988 wrapper->timeout = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), to);
989 wrapper->cont = cont;
990 wrapper->cont_cls = cont_cls;
991 wrapper->session = session;
992 GNUNET_CONTAINER_DLL_insert (plugin->msg_head,
995 plugin->bytes_in_queue += ssize;
996 GNUNET_STATISTICS_set (plugin->env->stats,
997 "# bytes currently in UNIX buffers",
998 plugin->bytes_in_queue,
1000 if (GNUNET_NO == plugin->with_ws)
1001 reschedule_select (plugin);
1007 * Demultiplexer for UNIX messages
1009 * @param plugin the main plugin for this transport
1010 * @param sender from which peer the message was received
1011 * @param currhdr pointer to the header of the message
1012 * @param ua address to look for
1013 * @param ua_len length of the address @a ua
1016 unix_demultiplexer (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender,
1017 const struct GNUNET_MessageHeader *currhdr,
1018 const struct UnixAddress *ua, size_t ua_len)
1020 struct Session *s = NULL;
1021 struct GNUNET_HELLO_Address *address;
1023 GNUNET_break (ntohl(plugin->ats_network.value) != GNUNET_ATS_NET_UNSPECIFIED);
1024 GNUNET_assert (ua_len >= sizeof (struct UnixAddress));
1025 LOG (GNUNET_ERROR_TYPE_DEBUG,
1026 "Received message from %s\n",
1027 unix_address_to_string(NULL, ua, ua_len));
1028 GNUNET_STATISTICS_update (plugin->env->stats,
1029 "# bytes received via UNIX",
1030 ntohs (currhdr->size),
1033 /* Look for existing session */
1034 address = GNUNET_HELLO_address_allocate (sender, PLUGIN_NAME, ua, ua_len,
1035 GNUNET_HELLO_ADDRESS_INFO_NONE); /* UNIX does not have "inbound" sessions */
1036 s = lookup_session (plugin, address);
1040 s = unix_plugin_get_session (plugin, address);
1041 /* Notify transport and ATS about new inbound session */
1042 plugin->env->session_start (NULL, s->address, s, &plugin->ats_network, 1);
1044 GNUNET_HELLO_address_free (address);
1045 reschedule_session_timeout (s);
1047 plugin->env->receive (plugin->env->cls, s->address, s, currhdr);
1048 plugin->env->update_address_metrics (plugin->env->cls, s->address, s,
1049 &plugin->ats_network, 1);
1054 * Read from UNIX domain socket (it is ready).
1056 * @param plugin the plugin
1059 unix_plugin_select_read (struct Plugin *plugin)
1061 char buf[65536] GNUNET_ALIGN;
1062 struct UnixAddress *ua;
1063 struct UNIXMessage *msg;
1064 struct GNUNET_PeerIdentity sender;
1065 struct sockaddr_un un;
1072 const struct GNUNET_MessageHeader *currhdr;
1076 addrlen = sizeof (un);
1077 memset (&un, 0, sizeof (un));
1080 GNUNET_NETWORK_socket_recvfrom (plugin->unix_sock.desc, buf, sizeof (buf),
1081 (struct sockaddr *) &un, &addrlen);
1083 if ((GNUNET_SYSERR == ret) && ((errno == EAGAIN) || (errno == ENOBUFS)))
1086 if (ret == GNUNET_SYSERR)
1088 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom");
1093 LOG (GNUNET_ERROR_TYPE_DEBUG,
1094 "Read %d bytes from socket %s\n",
1099 GNUNET_assert (AF_UNIX == (un.sun_family));
1100 is_abstract = GNUNET_NO;
1101 if ('\0' == un.sun_path[0])
1103 un.sun_path[0] = '@';
1104 is_abstract = GNUNET_YES;
1107 ua_len = sizeof (struct UnixAddress) + strlen (un.sun_path) + 1;
1108 ua = GNUNET_malloc (ua_len);
1109 ua->addrlen = htonl (strlen (&un.sun_path[0]) +1);
1110 memcpy (&ua[1], &un.sun_path[0], strlen (un.sun_path) + 1);
1112 ua->options = htonl(UNIX_OPTIONS_USE_ABSTRACT_SOCKETS);
1114 ua->options = htonl(UNIX_OPTIONS_NONE);
1116 msg = (struct UNIXMessage *) buf;
1117 csize = ntohs (msg->header.size);
1118 if ((csize < sizeof (struct UNIXMessage)) || (csize > ret))
1120 GNUNET_break_op (0);
1124 msgbuf = (char *) &msg[1];
1125 memcpy (&sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
1127 tsize = csize - sizeof (struct UNIXMessage);
1128 while (offset + sizeof (struct GNUNET_MessageHeader) <= tsize)
1130 currhdr = (struct GNUNET_MessageHeader *) &msgbuf[offset];
1131 csize = ntohs (currhdr->size);
1132 if ((csize < sizeof (struct GNUNET_MessageHeader)) ||
1133 (csize > tsize - offset))
1135 GNUNET_break_op (0);
1138 unix_demultiplexer (plugin, &sender, currhdr, ua, ua_len);
1146 * Write to UNIX domain socket (it is ready).
1148 * @param plugin the plugin
1151 unix_plugin_select_write (struct Plugin *plugin)
1154 struct UNIXMessageWrapper * msgw;
1156 while (NULL != (msgw = plugin->msg_tail))
1158 if (GNUNET_TIME_absolute_get_remaining (msgw->timeout).rel_value_us > 0)
1159 break; /* Message is ready for sending */
1160 /* Message has a timeout */
1161 LOG (GNUNET_ERROR_TYPE_DEBUG,
1162 "Timeout for message with %u bytes \n",
1163 (unsigned int) msgw->msgsize);
1164 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
1165 plugin->bytes_in_queue -= msgw->msgsize;
1166 GNUNET_STATISTICS_set (plugin->env->stats,
1167 "# bytes currently in UNIX buffers",
1168 plugin->bytes_in_queue, GNUNET_NO);
1169 GNUNET_STATISTICS_update (plugin->env->stats,
1170 "# UNIX bytes discarded",
1173 if (NULL != msgw->cont)
1174 msgw->cont (msgw->cont_cls,
1175 &msgw->session->target,
1179 GNUNET_free (msgw->msg);
1183 return; /* Nothing to send at the moment */
1185 sent = unix_real_send (plugin,
1186 plugin->unix_sock.desc,
1187 &msgw->session->target,
1188 (const char *) msgw->msg,
1192 msgw->session->address->address,
1193 msgw->session->address->address_length,
1195 msgw->cont, msgw->cont_cls);
1199 GNUNET_STATISTICS_update (plugin->env->stats,
1200 "# UNIX retry attempts",
1204 if (GNUNET_SYSERR == sent)
1206 /* failed and no retry */
1207 if (NULL != msgw->cont)
1208 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR, msgw->payload, 0);
1210 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
1212 GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
1213 plugin->bytes_in_queue -= msgw->msgsize;
1214 GNUNET_STATISTICS_set (plugin->env->stats,
1215 "# bytes currently in UNIX buffers",
1216 plugin->bytes_in_queue, GNUNET_NO);
1217 GNUNET_STATISTICS_update (plugin->env->stats,
1218 "# UNIX bytes discarded",
1222 GNUNET_free (msgw->msg);
1226 /* successfully sent bytes */
1227 GNUNET_break (sent > 0);
1228 GNUNET_CONTAINER_DLL_remove (plugin->msg_head,
1231 GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
1232 plugin->bytes_in_queue -= msgw->msgsize;
1233 GNUNET_STATISTICS_set (plugin->env->stats,
1234 "# bytes currently in UNIX buffers",
1235 plugin->bytes_in_queue,
1237 GNUNET_STATISTICS_update (plugin->env->stats,
1238 "# bytes transmitted via UNIX",
1241 if (NULL != msgw->cont)
1242 msgw->cont (msgw->cont_cls, &msgw->session->target,
1246 GNUNET_free (msgw->msg);
1252 * We have been notified that our writeset has something to read. We don't
1253 * know which socket needs to be read, so we have to check each one
1254 * Then reschedule this function to be called again once more is available.
1256 * @param cls the plugin handle
1257 * @param tc the scheduling context (for rescheduling this function again)
1260 unix_plugin_select (void *cls,
1261 const struct GNUNET_SCHEDULER_TaskContext *tc)
1263 struct Plugin *plugin = cls;
1265 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1266 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1269 if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
1271 /* Ready to send data */
1272 GNUNET_assert (GNUNET_NETWORK_fdset_isset
1273 (tc->write_ready, plugin->unix_sock.desc));
1274 if (NULL != plugin->msg_head)
1275 unix_plugin_select_write (plugin);
1278 if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
1280 /* Ready to receive data */
1281 GNUNET_assert (GNUNET_NETWORK_fdset_isset
1282 (tc->read_ready, plugin->unix_sock.desc));
1283 unix_plugin_select_read (plugin);
1285 reschedule_select (plugin);
1290 * Create a slew of UNIX sockets. If possible, use IPv6 and IPv4.
1292 * @param cls closure for server start, should be a struct Plugin *
1293 * @return number of sockets created or #GNUNET_SYSERR on error
1296 unix_transport_server_start (void *cls)
1298 struct Plugin *plugin = cls;
1299 struct sockaddr_un *un;
1302 un = unix_address_to_sockaddr (plugin->unix_socket_path,
1304 if (GNUNET_YES == plugin->abstract)
1306 plugin->unix_socket_path[0] = '@';
1307 un->sun_path[0] = '\0';
1309 plugin->ats_network = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) un, un_len);
1310 plugin->unix_sock.desc =
1311 GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_DGRAM, 0);
1312 if (NULL == plugin->unix_sock.desc)
1314 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
1315 return GNUNET_SYSERR;
1317 if ('\0' != un->sun_path[0])
1319 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (un->sun_path))
1321 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot create path to `%s'\n"),
1323 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc);
1324 plugin->unix_sock.desc = NULL;
1326 return GNUNET_SYSERR;
1330 GNUNET_NETWORK_socket_bind (plugin->unix_sock.desc, (const struct sockaddr *) un, un_len))
1332 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
1333 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc);
1334 plugin->unix_sock.desc = NULL;
1336 return GNUNET_SYSERR;
1338 LOG (GNUNET_ERROR_TYPE_DEBUG, "Bound to `%s'\n", plugin->unix_socket_path);
1339 plugin->rs = GNUNET_NETWORK_fdset_create ();
1340 plugin->ws = GNUNET_NETWORK_fdset_create ();
1341 GNUNET_NETWORK_fdset_zero (plugin->rs);
1342 GNUNET_NETWORK_fdset_zero (plugin->ws);
1343 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->unix_sock.desc);
1344 GNUNET_NETWORK_fdset_set (plugin->ws, plugin->unix_sock.desc);
1346 reschedule_select (plugin);
1353 * Function that will be called to check if a binary address for this
1354 * plugin is well-formed and corresponds to an address for THIS peer
1355 * (as per our configuration). Naturally, if absolutely necessary,
1356 * plugins can be a bit conservative in their answer, but in general
1357 * plugins should make sure that the address does not redirect
1358 * traffic to a 3rd party that might try to man-in-the-middle our
1361 * @param cls closure, should be our handle to the Plugin
1362 * @param addr pointer to the address
1363 * @param addrlen length of addr
1364 * @return GNUNET_OK if this is a plausible address for this peer
1365 * and transport, GNUNET_SYSERR if not
1369 unix_check_address (void *cls, const void *addr, size_t addrlen)
1371 struct Plugin* plugin = cls;
1372 struct UnixAddress *ua = (struct UnixAddress *) addr;
1374 size_t addr_str_len;
1376 if ((NULL == addr) || (0 == addrlen) || (sizeof (struct UnixAddress) > addrlen))
1379 return GNUNET_SYSERR;
1381 addrstr = (char *) &ua[1];
1382 addr_str_len = ntohl (ua->addrlen);
1383 if ('\0' != addrstr[addr_str_len - 1])
1386 return GNUNET_SYSERR;
1388 if (strlen (addrstr) + 1 != addr_str_len)
1391 return GNUNET_SYSERR;
1394 if (0 == strcmp (plugin->unix_socket_path, addrstr))
1396 return GNUNET_SYSERR;
1401 * Convert the transports address to a nice, human-readable
1404 * @param cls closure
1405 * @param type name of the transport that generated the address
1406 * @param addr one of the addresses of the host, NULL for the last address
1407 * the specific address format depends on the transport
1408 * @param addrlen length of the @a addr
1409 * @param numeric should (IP) addresses be displayed in numeric form?
1410 * @param timeout after how long should we give up?
1411 * @param asc function to call on each string
1412 * @param asc_cls closure for @a asc
1415 unix_plugin_address_pretty_printer (void *cls, const char *type,
1416 const void *addr, size_t addrlen,
1418 struct GNUNET_TIME_Relative timeout,
1419 GNUNET_TRANSPORT_AddressStringCallback asc,
1422 if ((NULL != addr) && (addrlen > 0))
1424 asc (asc_cls, unix_address_to_string (NULL, addr, addrlen));
1426 else if (0 == addrlen)
1428 asc (asc_cls, TRANSPORT_SESSION_INBOUND_STRING);
1433 asc (asc_cls, "<invalid UNIX address>");
1435 asc (asc_cls, NULL);
1440 * Function called to convert a string address to
1443 * @param cls closure ('struct Plugin*')
1444 * @param addr string address
1445 * @param addrlen length of the @a addr (strlen(addr) + '\0')
1446 * @param buf location to store the buffer
1447 * If the function returns #GNUNET_SYSERR, its contents are undefined.
1448 * @param added length of created address
1449 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1452 unix_string_to_address (void *cls,
1453 const char *addr, uint16_t addrlen,
1454 void **buf, size_t *added)
1456 struct UnixAddress *ua;
1463 /* Format unix.options.address */
1468 if ((NULL == addr) || (addrlen == 0))
1471 return GNUNET_SYSERR;
1473 if ('\0' != addr[addrlen - 1])
1476 return GNUNET_SYSERR;
1478 if (strlen (addr) != addrlen - 1)
1481 return GNUNET_SYSERR;
1483 plugin = GNUNET_strdup (addr);
1484 optionstr = strchr (plugin, '.');
1485 if (NULL == optionstr)
1488 GNUNET_free (plugin);
1489 return GNUNET_SYSERR;
1491 optionstr[0] = '\0';
1493 options = atol (optionstr);
1494 address = strchr (optionstr, '.');
1495 if (NULL == address)
1498 GNUNET_free (plugin);
1499 return GNUNET_SYSERR;
1503 if (0 != strcmp(plugin, PLUGIN_NAME))
1506 GNUNET_free (plugin);
1507 return GNUNET_SYSERR;
1510 ua_size = sizeof (struct UnixAddress) + strlen (address) + 1;
1511 ua = GNUNET_malloc (ua_size);
1512 ua->options = htonl (options);
1513 ua->addrlen = htonl (strlen (address) + 1);
1514 memcpy (&ua[1], address, strlen (address) + 1);
1515 GNUNET_free (plugin);
1524 * Notify transport service about address
1526 * @param cls the plugin
1530 address_notification (void *cls,
1531 const struct GNUNET_SCHEDULER_TaskContext *tc)
1533 struct Plugin *plugin = cls;
1534 struct GNUNET_HELLO_Address *address;
1536 struct UnixAddress *ua;
1539 len = sizeof (struct UnixAddress) + strlen (plugin->unix_socket_path) + 1;
1540 ua = GNUNET_malloc (len);
1541 ua->options = htonl (plugin->myoptions);
1542 ua->addrlen = htonl(strlen (plugin->unix_socket_path) + 1);
1543 unix_path = (char *) &ua[1];
1544 memcpy (unix_path, plugin->unix_socket_path, strlen (plugin->unix_socket_path) + 1);
1546 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1547 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1548 PLUGIN_NAME, ua, len, GNUNET_HELLO_ADDRESS_INFO_NONE);
1550 plugin->env->notify_address (plugin->env->cls, GNUNET_YES, address);
1552 GNUNET_free (address);
1557 * Increment session timeout due to activity
1559 * @param s session for which the timeout should be rescheduled
1562 reschedule_session_timeout (struct Session *s)
1564 GNUNET_assert (NULL != s);
1565 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1566 GNUNET_SCHEDULER_cancel (s->timeout_task);
1567 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1570 LOG (GNUNET_ERROR_TYPE_DEBUG,
1571 "Timeout rescheduled for session %p set to %s\n",
1573 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1579 * Function called on sessions to disconnect
1581 * @param cls the plugin
1582 * @param key peer identity (unused)
1583 * @param value the 'struct Session' to disconnect
1584 * @return #GNUNET_YES (always, continue to iterate)
1587 get_session_delete_it (void *cls,
1588 const struct GNUNET_PeerIdentity *key,
1591 struct Plugin *plugin = cls;
1592 struct Session *s = value;
1594 unix_session_disconnect (plugin, s);
1600 * Disconnect from a remote node. Clean up session if we have one for this peer
1602 * @param cls closure for this call (should be handle to Plugin)
1603 * @param target the peeridentity of the peer to disconnect
1604 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the operation failed
1607 unix_peer_disconnect (void *cls,
1608 const struct GNUNET_PeerIdentity *target)
1610 struct Plugin *plugin = cls;
1612 GNUNET_assert (plugin != NULL);
1613 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->session_map,
1615 &get_session_delete_it, plugin);
1620 * The exported method. Initializes the plugin and returns a
1621 * struct with the callbacks.
1623 * @param cls the plugin's execution environment
1624 * @return NULL on error, plugin functions otherwise
1627 libgnunet_plugin_transport_unix_init (void *cls)
1629 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1630 struct GNUNET_TRANSPORT_PluginFunctions *api;
1631 struct Plugin *plugin;
1632 int sockets_created;
1634 if (NULL == env->receive)
1636 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1637 initialze the plugin or the API */
1638 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
1640 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1641 api->address_to_string = &unix_address_to_string;
1642 api->string_to_address = &unix_string_to_address;
1646 plugin = GNUNET_new (struct Plugin);
1648 GNUNET_CONFIGURATION_get_value_filename(env->cfg, "transport-unix", "UNIXPATH",
1649 &plugin->unix_socket_path))
1651 LOG (GNUNET_ERROR_TYPE_ERROR,
1652 _("No UNIXPATH given in configuration!\n"));
1653 GNUNET_free (plugin);
1659 /* Initialize my flags */
1661 plugin->abstract = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
1662 "testing", "USE_ABSTRACT_SOCKETS");
1664 plugin->myoptions = UNIX_OPTIONS_NONE;
1665 if (GNUNET_YES == plugin->abstract)
1667 plugin->myoptions = UNIX_OPTIONS_USE_ABSTRACT_SOCKETS;
1670 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
1673 api->get_session = &unix_plugin_get_session;
1674 api->send = &unix_plugin_send;
1675 api->disconnect_peer = &unix_peer_disconnect;
1676 api->disconnect_session = &unix_session_disconnect;
1677 api->query_keepalive_factor = &unix_query_keepalive_factor;
1678 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1679 api->address_to_string = &unix_address_to_string;
1680 api->check_address = &unix_check_address;
1681 api->string_to_address = &unix_string_to_address;
1682 api->get_network = &unix_get_network;
1683 api->update_session_timeout = &unix_plugin_update_session_timeout;
1684 sockets_created = unix_transport_server_start (plugin);
1685 if ((0 == sockets_created) || (GNUNET_SYSERR == sockets_created))
1687 LOG (GNUNET_ERROR_TYPE_WARNING,
1688 _("Failed to open UNIX listen socket\n"));
1690 GNUNET_free (plugin->unix_socket_path);
1691 GNUNET_free (plugin);
1694 plugin->session_map = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1695 plugin->address_update_task = GNUNET_SCHEDULER_add_now (&address_notification, plugin);
1701 * Shutdown the plugin.
1703 * @param cls the plugin API returned from the initialization function
1704 * @return NULL (always)
1707 libgnunet_plugin_transport_unix_done (void *cls)
1709 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1710 struct Plugin *plugin = api->cls;
1711 struct GNUNET_HELLO_Address *address;
1712 struct UNIXMessageWrapper * msgw;
1713 struct UnixAddress *ua;
1722 len = sizeof (struct UnixAddress) + strlen (plugin->unix_socket_path) + 1;
1723 ua = GNUNET_malloc (len);
1724 ua->options = htonl (plugin->myoptions);
1725 ua->addrlen = htonl(strlen (plugin->unix_socket_path) + 1);
1726 memcpy (&ua[1], plugin->unix_socket_path, strlen (plugin->unix_socket_path) + 1);
1727 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1728 PLUGIN_NAME, ua, len, GNUNET_HELLO_ADDRESS_INFO_NONE);
1729 plugin->env->notify_address (plugin->env->cls, GNUNET_NO, address);
1731 GNUNET_free (address);
1734 while (NULL != (msgw = plugin->msg_head))
1736 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
1737 if (msgw->cont != NULL)
1738 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR,
1740 GNUNET_free (msgw->msg);
1744 if (GNUNET_SCHEDULER_NO_TASK != plugin->select_task)
1746 GNUNET_SCHEDULER_cancel (plugin->select_task);
1747 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1749 if (GNUNET_SCHEDULER_NO_TASK != plugin->address_update_task)
1751 GNUNET_SCHEDULER_cancel (plugin->address_update_task);
1752 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1754 if (NULL != plugin->unix_sock.desc)
1756 GNUNET_break (GNUNET_OK ==
1757 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc));
1758 plugin->unix_sock.desc = NULL;
1759 plugin->with_ws = GNUNET_NO;
1761 GNUNET_CONTAINER_multipeermap_iterate (plugin->session_map,
1762 &get_session_delete_it, plugin);
1763 GNUNET_CONTAINER_multipeermap_destroy (plugin->session_map);
1764 if (NULL != plugin->rs)
1765 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1766 if (NULL != plugin->ws)
1767 GNUNET_NETWORK_fdset_destroy (plugin->ws);
1768 GNUNET_free (plugin->unix_socket_path);
1769 GNUNET_free (plugin);
1774 /* end of plugin_transport_unix.c */