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)
53 #define LOG(kind,...) GNUNET_log_from (kind, "transport-unix",__VA_ARGS__)
56 GNUNET_NETWORK_STRUCT_BEGIN
60 uint32_t options GNUNET_PACKED;
62 uint32_t addrlen GNUNET_PACKED;
67 * UNIX Message-Packet header.
74 struct GNUNET_MessageHeader header;
77 * What is the identity of the sender (GNUNET_hash of public key)
79 struct GNUNET_PeerIdentity sender;
83 GNUNET_NETWORK_STRUCT_END
88 static uint32_t myoptions;
91 * Handle for a session.
95 struct GNUNET_PeerIdentity target;
97 struct Plugin * plugin;
99 struct GNUNET_HELLO_Address *address;
102 * Session timeout task
104 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
108 struct UNIXMessageWrapper
111 * We keep messages in a doubly linked list.
113 struct UNIXMessageWrapper *next;
116 * We keep messages in a doubly linked list.
118 struct UNIXMessageWrapper *prev;
121 * The actual payload (allocated separately right now).
123 struct UNIXMessage * msg;
126 * Session this message belongs to.
128 struct Session *session;
131 * Function to call upon transmission.
133 GNUNET_TRANSPORT_TransmitContinuation cont;
136 * Closure for 'cont'.
141 * Timeout for this message.
143 struct GNUNET_TIME_Absolute timeout;
146 * Number of bytes in 'msg'.
151 * Number of bytes of payload encapsulated in 'msg'.
156 * Priority of the message (ignored, just dragged along in UNIX).
158 unsigned int priority;
163 * Encapsulation of all of the state of the plugin.
175 * Stored in a linked list.
177 struct PeerSession *next;
180 * Pointer to the global plugin struct.
182 struct Plugin *plugin;
185 * To whom are we talking to (set to our identity
186 * if we are still waiting for the welcome message)
188 struct GNUNET_PeerIdentity target;
191 * Address of the other peer (either based on our 'connect'
192 * call or on our 'accept' call).
197 * Length of connect_addr.
202 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
204 int expecting_welcome;
207 * From which socket do we need to send to this peer?
209 struct GNUNET_NETWORK_Handle *sock;
212 * Queue of messages for this peer, in the case that
213 * we have to await a connection...
215 struct MessageQueue *messages;
221 * Information we keep for each of our listen sockets.
223 struct UNIX_Sock_Info
228 struct GNUNET_NETWORK_Handle *desc;
231 * The port we bound to (not an actual PORT, as UNIX domain sockets
232 * don't have ports, but rather a number in the path name to make this
240 * Encapsulation of all of the state of the plugin.
246 * ID of task used to update our addresses when one expires.
248 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
253 GNUNET_SCHEDULER_TaskIdentifier select_task;
256 * Number of bytes we currently have in our write queue.
258 unsigned long long bytes_in_queue;
263 struct GNUNET_TRANSPORT_PluginEnvironment *env;
268 struct GNUNET_CONTAINER_MultiPeerMap *session_map;
273 struct GNUNET_NETWORK_FDSet *rs;
278 struct GNUNET_NETWORK_FDSet *ws;
281 * Path of our unix domain socket (/tmp/unix-plugin-PORT)
283 char *unix_socket_path;
286 * Head of queue of messages to transmit.
288 struct UNIXMessageWrapper *msg_head;
291 * Tail of queue of messages to transmit.
293 struct UNIXMessageWrapper *msg_tail;
296 * socket that we transmit all data with
298 struct UNIX_Sock_Info unix_sock;
303 struct GNUNET_ATS_Information ats_network;
306 * Is the write set in the current 'select' task? GNUNET_NO if the
307 * write queue was empty when the main task was scheduled,
308 * GNUNET_YES if we're already waiting for being allowed to write.
316 * Increment session timeout due to activity
318 * @param s session for which the timeout should be moved
321 reschedule_session_timeout (struct Session *s);
325 * We have been notified that our writeset has something to read. We don't
326 * know which socket needs to be read, so we have to check each one
327 * Then reschedule this function to be called again once more is available.
329 * @param cls the plugin handle
330 * @param tc the scheduling context (for rescheduling this function again)
333 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
337 * Function called for a quick conversion of the binary address to
338 * a numeric address. Note that the caller must not free the
339 * address and that the next call to this function is allowed
340 * to override the address again.
343 * @param addr binary address
344 * @param addrlen length of the address
345 * @return string representing the same address
348 unix_address_to_string (void *cls, const void *addr, size_t addrlen)
350 static char rbuf[1024];
351 struct UnixAddress *ua = (struct UnixAddress *) addr;
355 if ((NULL == addr) || (sizeof (struct UnixAddress) > addrlen))
360 addrstr = (char *) &ua[1];
361 addr_str_len = ntohl (ua->addrlen);
363 if (addr_str_len != addrlen - sizeof(struct UnixAddress))
369 if ('\0' != addrstr[addr_str_len - 1])
374 if (strlen (addrstr) + 1 != addr_str_len)
380 GNUNET_snprintf (rbuf, sizeof(rbuf), "%s.%u.%s", PLUGIN_NAME,
381 ntohl (ua->options), addrstr);
386 static struct sockaddr_un *
387 unix_address_to_sockaddr (const struct GNUNET_CONFIGURATION_Handle *cfg,
388 const char *unixpath,
391 struct sockaddr_un *un;
394 GNUNET_assert (0 < strlen (unixpath)); /* sanity check */
395 un = GNUNET_new (struct sockaddr_un);
396 un->sun_family = AF_UNIX;
397 slen = strlen (unixpath);
398 if (slen >= sizeof (un->sun_path))
399 slen = sizeof (un->sun_path) - 1;
400 memcpy (un->sun_path, unixpath, slen);
401 un->sun_path[slen] = '\0';
406 abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
408 "USE_ABSTRACT_SOCKETS");
409 if (GNUNET_YES == abstract)
410 un->sun_path[0] = '\0';
413 slen = sizeof (struct sockaddr_un);
414 #if HAVE_SOCKADDR_IN_SIN_LEN
415 un->sun_len = (u_char) slen;
423 * Re-schedule the main 'select' callback (unix_plugin_select)
426 * @param plugin the plugin context
429 reschedule_select (struct Plugin * plugin)
431 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
433 GNUNET_SCHEDULER_cancel (plugin->select_task);
434 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
436 if (NULL != plugin->msg_head)
438 plugin->select_task =
439 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
440 GNUNET_TIME_UNIT_FOREVER_REL,
443 &unix_plugin_select, plugin);
444 plugin->with_ws = GNUNET_YES;
448 plugin->select_task =
449 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
450 GNUNET_TIME_UNIT_FOREVER_REL,
453 &unix_plugin_select, plugin);
454 plugin->with_ws = GNUNET_NO;
460 * Closure to #lookup_session_it.
465 * Location to store the session, if found.
469 struct GNUNET_HELLO_Address *address;
474 * Function called to find a session by address.
476 * @param cls the 'struct LookupCtx'
477 * @param key peer we are looking for (unused)
478 * @param value a session
479 * @return #GNUNET_YES if not found (continue looking), #GNUNET_NO on success
482 lookup_session_it (void *cls,
483 const struct GNUNET_PeerIdentity * key,
486 struct LookupCtx *lctx = cls;
487 struct Session *s = value;
489 if (0 == GNUNET_HELLO_address_cmp (lctx->address, s->address))
499 * Find an existing session by address.
501 * @param plugin the plugin
502 * @param address the address to find
503 * @return NULL if session was not found
505 static struct Session *
506 lookup_session (struct Plugin *plugin,
507 struct GNUNET_HELLO_Address *address)
509 struct LookupCtx lctx;
511 lctx.address = address;
513 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->session_map,
515 &lookup_session_it, &lctx);
521 * Functions with this signature are called whenever we need
522 * to close a session due to a disconnect or failure to
523 * establish a connection.
525 * @param cls closure with the `struct Plugin`
526 * @param s session to close down
527 * @return #GNUNET_OK on success
530 unix_session_disconnect (void *cls,
533 struct Plugin *plugin = cls;
534 struct UNIXMessageWrapper *msgw;
535 struct UNIXMessageWrapper *next;
538 LOG (GNUNET_ERROR_TYPE_DEBUG,
539 "Disconnecting session for peer `%s' `%s'\n",
540 GNUNET_i2s (&s->target),
541 unix_address_to_string (NULL, s->address->address, s->address->address_length) );
542 plugin->env->session_end (plugin->env->cls, &s->target, s);
544 next = plugin->msg_head;
549 if (msgw->session != s)
551 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
552 if (NULL != msgw->cont)
553 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR,
555 GNUNET_free (msgw->msg);
557 removed = GNUNET_YES;
559 GNUNET_assert (GNUNET_YES ==
560 GNUNET_CONTAINER_multipeermap_remove (plugin->session_map,
563 GNUNET_STATISTICS_set (plugin->env->stats,
564 "# UNIX sessions active",
565 GNUNET_CONTAINER_multipeermap_size (plugin->session_map),
567 if ((GNUNET_YES == removed) && (NULL == plugin->msg_head))
568 reschedule_select (plugin);
569 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
571 GNUNET_SCHEDULER_cancel (s->timeout_task);
572 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
574 GNUNET_HELLO_address_free (s->address);
581 * Function that is called to get the keepalive factor.
582 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
583 * calculate the interval between keepalive packets.
585 * @param cls closure with the `struct Plugin`
586 * @return keepalive factor
589 unix_query_keepalive_factor (void *cls)
596 * Actually send out the message, assume we've got the address and
597 * send_handle squared away!
600 * @param send_handle which handle to send message on
601 * @param target who should receive this message (ignored by UNIX)
602 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
603 * @param msgbuf_size the size of the msgbuf to send
604 * @param priority how important is the message (ignored by UNIX)
605 * @param timeout when should we time out (give up) if we can not transmit?
606 * @param addr the addr to send the message to, needs to be a sockaddr for us
607 * @param addrlen the len of @a addr
608 * @param payload bytes payload to send
609 * @param cont continuation to call once the message has
610 * been transmitted (or if the transport is ready
611 * for the next transmission call; or if the
612 * peer disconnected...)
613 * @param cont_cls closure for @a cont
614 * @return on success the number of bytes written, RETRY for retry, -1 on errors
617 unix_real_send (void *cls,
618 struct GNUNET_NETWORK_Handle *send_handle,
619 const struct GNUNET_PeerIdentity *target, const char *msgbuf,
620 size_t msgbuf_size, unsigned int priority,
621 struct GNUNET_TIME_Absolute timeout,
622 const struct UnixAddress *addr,
625 GNUNET_TRANSPORT_TransmitContinuation cont,
628 struct Plugin *plugin = cls;
630 struct sockaddr_un *un;
632 const char *unixpath;
634 GNUNET_assert (NULL != plugin);
635 if (NULL == send_handle)
637 GNUNET_break (0); /* We do not have a send handle */
638 return GNUNET_SYSERR;
640 if ((NULL == addr) || (0 == addrlen))
642 GNUNET_break (0); /* Can never send if we don't have an address */
643 return GNUNET_SYSERR;
646 /* Prepare address */
647 unixpath = (const char *) &addr[1];
648 if (NULL == (un = unix_address_to_sockaddr (plugin->env->cfg,
658 sent = GNUNET_NETWORK_socket_sendto (send_handle, msgbuf, msgbuf_size,
659 (const struct sockaddr *) un, un_len);
660 if (GNUNET_SYSERR == sent)
662 if ( (EAGAIN == errno) ||
666 return RETRY; /* We have to retry later */
668 if (EMSGSIZE == errno)
671 socklen_t len = sizeof (size);
673 GNUNET_NETWORK_socket_getsockopt ((struct GNUNET_NETWORK_Handle *)
674 send_handle, SOL_SOCKET, SO_SNDBUF, &size,
676 if (size < msgbuf_size)
678 LOG (GNUNET_ERROR_TYPE_DEBUG,
679 "Trying to increase socket buffer size from %i to %i for message size %i\n",
680 size, ((msgbuf_size / 1000) + 2) * 1000, msgbuf_size);
681 size = ((msgbuf_size / 1000) + 2) * 1000;
682 if (GNUNET_OK == GNUNET_NETWORK_socket_setsockopt
683 ((struct GNUNET_NETWORK_Handle *) send_handle, SOL_SOCKET, SO_SNDBUF,
684 &size, sizeof (size)))
685 goto resend; /* Increased buffer size, retry sending */
688 /* Could not increase buffer size: error, no retry */
689 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
691 return GNUNET_SYSERR;
696 /* Buffer is bigger than message: error, no retry
697 * This should never happen!*/
700 return GNUNET_SYSERR;
705 LOG (GNUNET_ERROR_TYPE_DEBUG,
706 "UNIX transmit %u-byte message to %s (%d: %s)\n",
707 (unsigned int) msgbuf_size,
708 GNUNET_a2s ((const struct sockaddr *)un, un_len),
710 (sent < 0) ? STRERROR (errno) : "ok");
717 * Closure for 'get_session_it'.
719 struct GetSessionIteratorContext
722 * Location to store the session, if found.
727 * Address information.
729 const struct GNUNET_HELLO_Address *address;
734 * Function called to find a session by address.
736 * @param cls the 'struct LookupCtx'
737 * @param key peer we are looking for (unused)
738 * @param value a session
739 * @return #GNUNET_YES if not found (continue looking), #GNUNET_NO on success
742 get_session_it (void *cls,
743 const struct GNUNET_PeerIdentity *key,
746 struct GetSessionIteratorContext *gsi = cls;
747 struct Session *s = value;
749 if (0 == GNUNET_HELLO_address_cmp(s->address, gsi->address))
759 * Session was idle for too long, so disconnect it
761 * @param cls the 'struct Session' to disconnect
762 * @param tc scheduler context
765 session_timeout (void *cls,
766 const struct GNUNET_SCHEDULER_TaskContext *tc)
768 struct Session *s = cls;
770 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
771 LOG (GNUNET_ERROR_TYPE_DEBUG,
772 "Session %p was idle for %s, disconnecting\n",
774 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
776 unix_session_disconnect (s->plugin, s);
781 * Function obtain the network type for a session
783 * @param cls closure ('struct Plugin*')
784 * @param session the session
785 * @return the network type in HBO or #GNUNET_SYSERR
787 static enum GNUNET_ATS_Network_Type
788 unix_get_network (void *cls,
789 struct Session *session)
791 GNUNET_assert (NULL != session);
792 return GNUNET_ATS_NET_LOOPBACK;
797 * Creates a new outbound session the transport service will use to send data to the
800 * @param cls the plugin
801 * @param address the address
802 * @return the session or NULL of max connections exceeded
804 static struct Session *
805 unix_plugin_get_session (void *cls,
806 const struct GNUNET_HELLO_Address *address)
808 struct Plugin *plugin = cls;
810 struct GetSessionIteratorContext gsi;
811 struct UnixAddress *ua;
813 uint32_t addr_str_len;
815 GNUNET_assert (NULL != plugin);
816 GNUNET_assert (NULL != address);
818 ua = (struct UnixAddress *) address->address;
819 if ((NULL == address->address) || (0 == address->address_length) ||
820 (sizeof (struct UnixAddress) > address->address_length))
825 addrstr = (char *) &ua[1];
826 addr_str_len = ntohl (ua->addrlen);
827 if (addr_str_len != address->address_length - sizeof (struct UnixAddress))
829 /* This can be a legacy address */
833 if ('\0' != addrstr[addr_str_len - 1])
838 if (strlen (addrstr) + 1 != addr_str_len)
844 /* Check if already existing */
845 gsi.address = address;
847 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->session_map,
849 &get_session_it, &gsi);
852 LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p for address `%s'\n",
854 unix_address_to_string (NULL, address->address, address->address_length));
858 /* create a new session */
859 s = GNUNET_new (struct Session);
860 s->target = address->peer;
861 s->address = GNUNET_HELLO_address_copy (address);
862 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
863 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
866 LOG (GNUNET_ERROR_TYPE_DEBUG,
867 "Creating a new session %p for address `%s'\n",
868 s, unix_address_to_string (NULL, address->address, address->address_length));
869 (void) GNUNET_CONTAINER_multipeermap_put (plugin->session_map,
871 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
872 GNUNET_STATISTICS_set (plugin->env->stats,
873 "# UNIX sessions active",
874 GNUNET_CONTAINER_multipeermap_size (plugin->session_map),
880 unix_plugin_update_session_timeout (void *cls,
881 const struct GNUNET_PeerIdentity *peer,
882 struct Session *session)
884 struct Plugin *plugin = cls;
887 GNUNET_CONTAINER_multipeermap_contains_value (plugin->session_map,
891 reschedule_session_timeout (session);
895 * Function that can be used by the transport service to transmit
896 * a message using the plugin. Note that in the case of a
897 * peer disconnecting, the continuation MUST be called
898 * prior to the disconnect notification itself. This function
899 * will be called with this peer's HELLO message to initiate
900 * a fresh connection to another peer.
903 * @param session which session must be used
904 * @param msgbuf the message to transmit
905 * @param msgbuf_size number of bytes in @a msgbuf
906 * @param priority how important is the message (most plugins will
907 * ignore message priority and just FIFO)
908 * @param to how long to wait at most for the transmission (does not
909 * require plugins to discard the message after the timeout,
910 * just advisory for the desired delay; most plugins will ignore
912 * @param cont continuation to call once the message has
913 * been transmitted (or if the transport is ready
914 * for the next transmission call; or if the
915 * peer disconnected...); can be NULL
916 * @param cont_cls closure for @a cont
917 * @return number of bytes used (on the physical network, with overheads);
918 * -1 on hard errors (i.e. address invalid); 0 is a legal value
919 * and does NOT mean that the message was not transmitted (DV)
922 unix_plugin_send (void *cls,
923 struct Session *session,
924 const char *msgbuf, size_t msgbuf_size,
925 unsigned int priority,
926 struct GNUNET_TIME_Relative to,
927 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
929 struct Plugin *plugin = cls;
930 struct UNIXMessageWrapper *wrapper;
931 struct UNIXMessage *message;
935 GNUNET_CONTAINER_multipeermap_contains_value (plugin->session_map,
939 LOG (GNUNET_ERROR_TYPE_ERROR,
940 "Invalid session for peer `%s' `%s'\n",
941 GNUNET_i2s (&session->target),
942 unix_address_to_string(NULL, session->address->address,
943 session->address->address_length));
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 unix_address_to_string(NULL, session->address->address,
952 session->address->address_length));
953 ssize = sizeof (struct UNIXMessage) + msgbuf_size;
954 message = GNUNET_malloc (sizeof (struct UNIXMessage) + msgbuf_size);
955 message->header.size = htons (ssize);
956 message->header.type = htons (0);
957 memcpy (&message->sender, plugin->env->my_identity,
958 sizeof (struct GNUNET_PeerIdentity));
959 memcpy (&message[1], msgbuf, msgbuf_size);
961 wrapper = GNUNET_new (struct UNIXMessageWrapper);
962 wrapper->msg = message;
963 wrapper->msgsize = ssize;
964 wrapper->payload = msgbuf_size;
965 wrapper->priority = priority;
966 wrapper->timeout = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), to);
967 wrapper->cont = cont;
968 wrapper->cont_cls = cont_cls;
969 wrapper->session = session;
970 GNUNET_CONTAINER_DLL_insert (plugin->msg_head,
973 plugin->bytes_in_queue += ssize;
974 GNUNET_STATISTICS_set (plugin->env->stats,
975 "# bytes currently in UNIX buffers",
976 plugin->bytes_in_queue,
978 if (GNUNET_NO == plugin->with_ws)
979 reschedule_select (plugin);
985 * Demultiplexer for UNIX messages
987 * @param plugin the main plugin for this transport
988 * @param sender from which peer the message was received
989 * @param currhdr pointer to the header of the message
990 * @param ua address to look for
991 * @param ua_len length of the address @a ua
994 unix_demultiplexer (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender,
995 const struct GNUNET_MessageHeader *currhdr,
996 const struct UnixAddress *ua, size_t ua_len)
998 struct Session *s = NULL;
999 struct GNUNET_HELLO_Address *address;
1001 GNUNET_break (ntohl(plugin->ats_network.value) != GNUNET_ATS_NET_UNSPECIFIED);
1002 GNUNET_assert (ua_len >= sizeof (struct UnixAddress));
1003 LOG (GNUNET_ERROR_TYPE_DEBUG,
1004 "Received message from %s\n",
1005 unix_address_to_string(NULL, ua, ua_len));
1006 GNUNET_STATISTICS_update (plugin->env->stats,
1007 "# bytes received via UNIX",
1008 ntohs (currhdr->size),
1011 /* Look for existing session */
1012 address = GNUNET_HELLO_address_allocate (sender, PLUGIN_NAME, ua, ua_len,
1013 GNUNET_HELLO_ADDRESS_INFO_NONE); /* UNIX does not have "inbound" sessions */
1014 s = lookup_session (plugin, address);
1018 s = unix_plugin_get_session (plugin, address);
1019 /* Notify transport and ATS about new inbound session */
1020 plugin->env->session_start (NULL, s->address, s, &plugin->ats_network, 1);
1022 GNUNET_HELLO_address_free (address);
1023 reschedule_session_timeout (s);
1025 plugin->env->receive (plugin->env->cls, s->address, s, currhdr);
1026 plugin->env->update_address_metrics (plugin->env->cls, s->address, s,
1027 &plugin->ats_network, 1);
1032 * Read from UNIX domain socket (it is ready).
1034 * @param plugin the plugin
1037 unix_plugin_select_read (struct Plugin *plugin)
1039 char buf[65536] GNUNET_ALIGN;
1040 struct UnixAddress *ua;
1041 struct UNIXMessage *msg;
1042 struct GNUNET_PeerIdentity sender;
1043 struct sockaddr_un un;
1049 const struct GNUNET_MessageHeader *currhdr;
1053 addrlen = sizeof (un);
1054 memset (&un, 0, sizeof (un));
1057 GNUNET_NETWORK_socket_recvfrom (plugin->unix_sock.desc, buf, sizeof (buf),
1058 (struct sockaddr *) &un, &addrlen);
1060 if ((GNUNET_SYSERR == ret) && ((errno == EAGAIN) || (errno == ENOBUFS)))
1063 if (ret == GNUNET_SYSERR)
1065 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom");
1070 LOG (GNUNET_ERROR_TYPE_DEBUG,
1071 "Read %d bytes from socket %s\n",
1076 GNUNET_assert (AF_UNIX == (un.sun_family));
1077 ua_len = sizeof (struct UnixAddress) + strlen (un.sun_path) + 1;
1078 ua = GNUNET_malloc (ua_len);
1079 ua->addrlen = htonl (strlen (&un.sun_path[0]) +1);
1080 ua->options = htonl (0);
1081 memcpy (&ua[1], &un.sun_path[0], strlen (un.sun_path) + 1);
1083 msg = (struct UNIXMessage *) buf;
1084 csize = ntohs (msg->header.size);
1085 if ((csize < sizeof (struct UNIXMessage)) || (csize > ret))
1087 GNUNET_break_op (0);
1091 msgbuf = (char *) &msg[1];
1092 memcpy (&sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
1094 tsize = csize - sizeof (struct UNIXMessage);
1095 while (offset + sizeof (struct GNUNET_MessageHeader) <= tsize)
1097 currhdr = (struct GNUNET_MessageHeader *) &msgbuf[offset];
1098 csize = ntohs (currhdr->size);
1099 if ((csize < sizeof (struct GNUNET_MessageHeader)) ||
1100 (csize > tsize - offset))
1102 GNUNET_break_op (0);
1105 unix_demultiplexer (plugin, &sender, currhdr, ua, ua_len);
1113 * Write to UNIX domain socket (it is ready).
1115 * @param plugin the plugin
1118 unix_plugin_select_write (struct Plugin *plugin)
1121 struct UNIXMessageWrapper * msgw;
1123 while (NULL != (msgw = plugin->msg_tail))
1125 if (GNUNET_TIME_absolute_get_remaining (msgw->timeout).rel_value_us > 0)
1126 break; /* Message is ready for sending */
1127 /* Message has a timeout */
1128 LOG (GNUNET_ERROR_TYPE_DEBUG,
1129 "Timeout for message with %u bytes \n",
1130 (unsigned int) msgw->msgsize);
1131 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
1132 plugin->bytes_in_queue -= msgw->msgsize;
1133 GNUNET_STATISTICS_set (plugin->env->stats,
1134 "# bytes currently in UNIX buffers",
1135 plugin->bytes_in_queue, GNUNET_NO);
1136 GNUNET_STATISTICS_update (plugin->env->stats,
1137 "# UNIX bytes discarded",
1140 if (NULL != msgw->cont)
1141 msgw->cont (msgw->cont_cls,
1142 &msgw->session->target,
1146 GNUNET_free (msgw->msg);
1150 return; /* Nothing to send at the moment */
1152 sent = unix_real_send (plugin,
1153 plugin->unix_sock.desc,
1154 &msgw->session->target,
1155 (const char *) msgw->msg,
1159 msgw->session->address->address,
1160 msgw->session->address->address_length,
1162 msgw->cont, msgw->cont_cls);
1166 GNUNET_STATISTICS_update (plugin->env->stats,
1167 "# UNIX retry attempts",
1171 if (GNUNET_SYSERR == sent)
1173 /* failed and no retry */
1174 if (NULL != msgw->cont)
1175 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR, msgw->payload, 0);
1177 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
1179 GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
1180 plugin->bytes_in_queue -= msgw->msgsize;
1181 GNUNET_STATISTICS_set (plugin->env->stats,
1182 "# bytes currently in UNIX buffers",
1183 plugin->bytes_in_queue, GNUNET_NO);
1184 GNUNET_STATISTICS_update (plugin->env->stats,
1185 "# UNIX bytes discarded",
1189 GNUNET_free (msgw->msg);
1193 /* successfully sent bytes */
1194 GNUNET_break (sent > 0);
1195 GNUNET_CONTAINER_DLL_remove (plugin->msg_head,
1198 GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
1199 plugin->bytes_in_queue -= msgw->msgsize;
1200 GNUNET_STATISTICS_set (plugin->env->stats,
1201 "# bytes currently in UNIX buffers",
1202 plugin->bytes_in_queue,
1204 GNUNET_STATISTICS_update (plugin->env->stats,
1205 "# bytes transmitted via UNIX",
1208 if (NULL != msgw->cont)
1209 msgw->cont (msgw->cont_cls, &msgw->session->target,
1213 GNUNET_free (msgw->msg);
1219 * We have been notified that our writeset has something to read. We don't
1220 * know which socket needs to be read, so we have to check each one
1221 * Then reschedule this function to be called again once more is available.
1223 * @param cls the plugin handle
1224 * @param tc the scheduling context (for rescheduling this function again)
1227 unix_plugin_select (void *cls,
1228 const struct GNUNET_SCHEDULER_TaskContext *tc)
1230 struct Plugin *plugin = cls;
1232 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1233 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1236 if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
1238 /* Ready to send data */
1239 GNUNET_assert (GNUNET_NETWORK_fdset_isset
1240 (tc->write_ready, plugin->unix_sock.desc));
1241 if (NULL != plugin->msg_head)
1242 unix_plugin_select_write (plugin);
1245 if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
1247 /* Ready to receive data */
1248 GNUNET_assert (GNUNET_NETWORK_fdset_isset
1249 (tc->read_ready, plugin->unix_sock.desc));
1250 unix_plugin_select_read (plugin);
1252 reschedule_select (plugin);
1257 * Create a slew of UNIX sockets. If possible, use IPv6 and IPv4.
1259 * @param cls closure for server start, should be a struct Plugin *
1260 * @return number of sockets created or #GNUNET_SYSERR on error
1263 unix_transport_server_start (void *cls)
1265 struct Plugin *plugin = cls;
1266 struct sockaddr_un *un;
1269 un = unix_address_to_sockaddr (plugin->env->cfg,
1270 plugin->unix_socket_path,
1272 plugin->ats_network = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) un, un_len);
1273 plugin->unix_sock.desc =
1274 GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_DGRAM, 0);
1275 if (NULL == plugin->unix_sock.desc)
1277 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
1278 return GNUNET_SYSERR;
1280 if ('\0' != un->sun_path[0])
1281 GNUNET_DISK_directory_create_for_file (un->sun_path);
1283 GNUNET_NETWORK_socket_bind (plugin->unix_sock.desc, (const struct sockaddr *) un, un_len))
1285 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
1286 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc);
1287 plugin->unix_sock.desc = NULL;
1289 return GNUNET_SYSERR;
1291 LOG (GNUNET_ERROR_TYPE_DEBUG, "Bound to `%s'\n", plugin->unix_socket_path);
1292 plugin->rs = GNUNET_NETWORK_fdset_create ();
1293 plugin->ws = GNUNET_NETWORK_fdset_create ();
1294 GNUNET_NETWORK_fdset_zero (plugin->rs);
1295 GNUNET_NETWORK_fdset_zero (plugin->ws);
1296 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->unix_sock.desc);
1297 GNUNET_NETWORK_fdset_set (plugin->ws, plugin->unix_sock.desc);
1299 reschedule_select (plugin);
1306 * Function that will be called to check if a binary address for this
1307 * plugin is well-formed and corresponds to an address for THIS peer
1308 * (as per our configuration). Naturally, if absolutely necessary,
1309 * plugins can be a bit conservative in their answer, but in general
1310 * plugins should make sure that the address does not redirect
1311 * traffic to a 3rd party that might try to man-in-the-middle our
1314 * @param cls closure, should be our handle to the Plugin
1315 * @param addr pointer to the address
1316 * @param addrlen length of addr
1317 * @return GNUNET_OK if this is a plausible address for this peer
1318 * and transport, GNUNET_SYSERR if not
1322 unix_check_address (void *cls, const void *addr, size_t addrlen)
1324 struct Plugin* plugin = cls;
1325 struct UnixAddress *ua = (struct UnixAddress *) addr;
1327 size_t addr_str_len;
1329 if ((NULL == addr) || (0 == addrlen) || (sizeof (struct UnixAddress) > addrlen))
1332 return GNUNET_SYSERR;
1334 addrstr = (char *) &ua[1];
1335 addr_str_len = ntohl (ua->addrlen);
1336 if ('\0' != addrstr[addr_str_len - 1])
1339 return GNUNET_SYSERR;
1341 if (strlen (addrstr) + 1 != addr_str_len)
1344 return GNUNET_SYSERR;
1347 if (0 == strcmp (plugin->unix_socket_path, addrstr))
1349 return GNUNET_SYSERR;
1354 * Convert the transports address to a nice, human-readable
1357 * @param cls closure
1358 * @param type name of the transport that generated the address
1359 * @param addr one of the addresses of the host, NULL for the last address
1360 * the specific address format depends on the transport
1361 * @param addrlen length of the @a addr
1362 * @param numeric should (IP) addresses be displayed in numeric form?
1363 * @param timeout after how long should we give up?
1364 * @param asc function to call on each string
1365 * @param asc_cls closure for @a asc
1368 unix_plugin_address_pretty_printer (void *cls, const char *type,
1369 const void *addr, size_t addrlen,
1371 struct GNUNET_TIME_Relative timeout,
1372 GNUNET_TRANSPORT_AddressStringCallback asc,
1375 if ((NULL != addr) && (addrlen > 0))
1377 asc (asc_cls, unix_address_to_string (NULL, addr, addrlen));
1379 else if (0 == addrlen)
1381 asc (asc_cls, TRANSPORT_SESSION_INBOUND_STRING);
1386 asc (asc_cls, "<invalid UNIX address>");
1388 asc (asc_cls, NULL);
1393 * Function called to convert a string address to
1396 * @param cls closure ('struct Plugin*')
1397 * @param addr string address
1398 * @param addrlen length of the @a addr (strlen(addr) + '\0')
1399 * @param buf location to store the buffer
1400 * If the function returns #GNUNET_SYSERR, its contents are undefined.
1401 * @param added length of created address
1402 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1405 unix_string_to_address (void *cls,
1406 const char *addr, uint16_t addrlen,
1407 void **buf, size_t *added)
1409 struct UnixAddress *ua;
1416 /* Format unix.options.address */
1421 if ((NULL == addr) || (addrlen == 0))
1424 return GNUNET_SYSERR;
1426 if ('\0' != addr[addrlen - 1])
1429 return GNUNET_SYSERR;
1431 if (strlen (addr) != addrlen - 1)
1434 return GNUNET_SYSERR;
1436 plugin = GNUNET_strdup (addr);
1437 optionstr = strchr (plugin, '.');
1438 if (NULL == optionstr)
1441 GNUNET_free (plugin);
1442 return GNUNET_SYSERR;
1444 optionstr[0] = '\0';
1446 options = atol (optionstr);
1447 address = strchr (optionstr, '.');
1448 if (NULL == address)
1451 GNUNET_free (plugin);
1452 return GNUNET_SYSERR;
1456 if (0 != strcmp(plugin, PLUGIN_NAME))
1459 GNUNET_free (plugin);
1460 return GNUNET_SYSERR;
1463 ua_size = sizeof (struct UnixAddress) + strlen (address) + 1;
1464 ua = GNUNET_malloc (ua_size);
1465 ua->options = htonl (options);
1466 ua->addrlen = htonl (strlen (address) + 1);
1467 memcpy (&ua[1], address, strlen (address) + 1);
1468 GNUNET_free (plugin);
1477 * Notify transport service about address
1479 * @param cls the plugin
1483 address_notification (void *cls,
1484 const struct GNUNET_SCHEDULER_TaskContext *tc)
1486 struct Plugin *plugin = cls;
1487 struct GNUNET_HELLO_Address *address;
1489 struct UnixAddress *ua;
1491 len = sizeof (struct UnixAddress) + strlen (plugin->unix_socket_path) + 1;
1492 ua = GNUNET_malloc (len);
1493 ua->options = htonl (myoptions);
1494 ua->addrlen = htonl(strlen (plugin->unix_socket_path) + 1);
1495 memcpy (&ua[1], plugin->unix_socket_path, strlen (plugin->unix_socket_path) + 1);
1497 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1498 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1499 PLUGIN_NAME, ua, len, GNUNET_HELLO_ADDRESS_INFO_NONE);
1501 plugin->env->notify_address (plugin->env->cls, GNUNET_YES, address);
1503 GNUNET_free (address);
1508 * Increment session timeout due to activity
1510 * @param s session for which the timeout should be rescheduled
1513 reschedule_session_timeout (struct Session *s)
1515 GNUNET_assert (NULL != s);
1516 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1517 GNUNET_SCHEDULER_cancel (s->timeout_task);
1518 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1521 LOG (GNUNET_ERROR_TYPE_DEBUG,
1522 "Timeout rescheduled for session %p set to %s\n",
1524 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1530 * Function called on sessions to disconnect
1532 * @param cls the plugin
1533 * @param key peer identity (unused)
1534 * @param value the 'struct Session' to disconnect
1535 * @return #GNUNET_YES (always, continue to iterate)
1538 get_session_delete_it (void *cls,
1539 const struct GNUNET_PeerIdentity *key,
1542 struct Plugin *plugin = cls;
1543 struct Session *s = value;
1545 unix_session_disconnect (plugin, s);
1551 * Disconnect from a remote node. Clean up session if we have one for this peer
1553 * @param cls closure for this call (should be handle to Plugin)
1554 * @param target the peeridentity of the peer to disconnect
1555 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the operation failed
1558 unix_peer_disconnect (void *cls,
1559 const struct GNUNET_PeerIdentity *target)
1561 struct Plugin *plugin = cls;
1563 GNUNET_assert (plugin != NULL);
1564 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->session_map,
1566 &get_session_delete_it, plugin);
1571 * The exported method. Initializes the plugin and returns a
1572 * struct with the callbacks.
1574 * @param cls the plugin's execution environment
1575 * @return NULL on error, plugin functions otherwise
1578 libgnunet_plugin_transport_unix_init (void *cls)
1580 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
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 plugin = GNUNET_new (struct Plugin);
1599 GNUNET_CONFIGURATION_get_value_filename(env->cfg, "transport-unix", "UNIXPATH",
1600 &plugin->unix_socket_path))
1602 LOG (GNUNET_ERROR_TYPE_ERROR,
1603 _("No UNIXPATH given in configuration!\n"));
1604 GNUNET_free (plugin);
1610 /* Initialize my flags */
1613 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
1616 api->get_session = &unix_plugin_get_session;
1617 api->send = &unix_plugin_send;
1618 api->disconnect_peer = &unix_peer_disconnect;
1619 api->disconnect_session = &unix_session_disconnect;
1620 api->query_keepalive_factor = &unix_query_keepalive_factor;
1621 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1622 api->address_to_string = &unix_address_to_string;
1623 api->check_address = &unix_check_address;
1624 api->string_to_address = &unix_string_to_address;
1625 api->get_network = &unix_get_network;
1626 api->update_session_timeout = &unix_plugin_update_session_timeout;
1627 sockets_created = unix_transport_server_start (plugin);
1628 if (0 == sockets_created)
1629 LOG (GNUNET_ERROR_TYPE_WARNING,
1630 _("Failed to open UNIX listen socket\n"));
1631 plugin->session_map = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1632 plugin->address_update_task = GNUNET_SCHEDULER_add_now (&address_notification, plugin);
1638 * Shutdown the plugin.
1640 * @param cls the plugin API returned from the initialization function
1641 * @return NULL (always)
1644 libgnunet_plugin_transport_unix_done (void *cls)
1646 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1647 struct Plugin *plugin = api->cls;
1648 struct GNUNET_HELLO_Address *address;
1649 struct UNIXMessageWrapper * msgw;
1650 struct UnixAddress *ua;
1659 len = sizeof (struct UnixAddress) + strlen (plugin->unix_socket_path) + 1;
1660 ua = GNUNET_malloc (len);
1661 ua->options = htonl (myoptions);
1662 ua->addrlen = htonl(strlen (plugin->unix_socket_path) + 1);
1663 memcpy (&ua[1], plugin->unix_socket_path, strlen (plugin->unix_socket_path) + 1);
1664 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1665 PLUGIN_NAME, ua, len, GNUNET_HELLO_ADDRESS_INFO_NONE);
1666 plugin->env->notify_address (plugin->env->cls, GNUNET_NO, address);
1668 GNUNET_free (address);
1671 while (NULL != (msgw = plugin->msg_head))
1673 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
1674 if (msgw->cont != NULL)
1675 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR,
1677 GNUNET_free (msgw->msg);
1681 if (GNUNET_SCHEDULER_NO_TASK != plugin->select_task)
1683 GNUNET_SCHEDULER_cancel (plugin->select_task);
1684 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1686 if (GNUNET_SCHEDULER_NO_TASK != plugin->address_update_task)
1688 GNUNET_SCHEDULER_cancel (plugin->address_update_task);
1689 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1691 if (NULL != plugin->unix_sock.desc)
1693 GNUNET_break (GNUNET_OK ==
1694 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc));
1695 plugin->unix_sock.desc = NULL;
1696 plugin->with_ws = GNUNET_NO;
1698 GNUNET_CONTAINER_multipeermap_iterate (plugin->session_map,
1699 &get_session_delete_it, plugin);
1700 GNUNET_CONTAINER_multipeermap_destroy (plugin->session_map);
1701 if (NULL != plugin->rs)
1702 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1703 if (NULL != plugin->ws)
1704 GNUNET_NETWORK_fdset_destroy (plugin->ws);
1705 GNUNET_free (plugin->unix_socket_path);
1706 GNUNET_free (plugin);
1711 /* end of plugin_transport_unix.c */