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 * Function that is called to get the keepalive factor.
604 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
605 * calculate the interval between keepalive packets.
607 * @param cls closure with the `struct Plugin`
608 * @return keepalive factor
611 unix_query_keepalive_factor (void *cls)
618 * Actually send out the message, assume we've got the address and
619 * send_handle squared away!
622 * @param send_handle which handle to send message on
623 * @param target who should receive this message (ignored by UNIX)
624 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
625 * @param msgbuf_size the size of the msgbuf to send
626 * @param priority how important is the message (ignored by UNIX)
627 * @param timeout when should we time out (give up) if we can not transmit?
628 * @param addr the addr to send the message to, needs to be a sockaddr for us
629 * @param addrlen the len of @a addr
630 * @param payload bytes payload to send
631 * @param cont continuation to call once the message has
632 * been transmitted (or if the transport is ready
633 * for the next transmission call; or if the
634 * peer disconnected...)
635 * @param cont_cls closure for @a cont
636 * @return on success the number of bytes written, RETRY for retry, -1 on errors
639 unix_real_send (void *cls,
640 struct GNUNET_NETWORK_Handle *send_handle,
641 const struct GNUNET_PeerIdentity *target, const char *msgbuf,
642 size_t msgbuf_size, unsigned int priority,
643 struct GNUNET_TIME_Absolute timeout,
644 const struct UnixAddress *addr,
647 GNUNET_TRANSPORT_TransmitContinuation cont,
650 struct Plugin *plugin = cls;
652 struct sockaddr_un *un;
654 const char *unixpath;
656 GNUNET_assert (NULL != plugin);
657 if (NULL == send_handle)
659 GNUNET_break (0); /* We do not have a send handle */
660 return GNUNET_SYSERR;
662 if ((NULL == addr) || (0 == addrlen))
664 GNUNET_break (0); /* Can never send if we don't have an address */
665 return GNUNET_SYSERR;
668 /* Prepare address */
669 unixpath = (const char *) &addr[1];
670 if (NULL == (un = unix_address_to_sockaddr (unixpath, &un_len)))
678 sent = GNUNET_NETWORK_socket_sendto (send_handle, msgbuf, msgbuf_size,
679 (const struct sockaddr *) un, un_len);
680 if (GNUNET_SYSERR == sent)
682 if ( (EAGAIN == errno) ||
686 return RETRY; /* We have to retry later */
688 if (EMSGSIZE == errno)
691 socklen_t len = sizeof (size);
693 GNUNET_NETWORK_socket_getsockopt ((struct GNUNET_NETWORK_Handle *)
694 send_handle, SOL_SOCKET, SO_SNDBUF, &size,
696 if (size < msgbuf_size)
698 LOG (GNUNET_ERROR_TYPE_DEBUG,
699 "Trying to increase socket buffer size from %i to %i for message size %i\n",
700 size, ((msgbuf_size / 1000) + 2) * 1000, msgbuf_size);
701 size = ((msgbuf_size / 1000) + 2) * 1000;
702 if (GNUNET_OK == GNUNET_NETWORK_socket_setsockopt
703 ((struct GNUNET_NETWORK_Handle *) send_handle, SOL_SOCKET, SO_SNDBUF,
704 &size, sizeof (size)))
705 goto resend; /* Increased buffer size, retry sending */
708 /* Could not increase buffer size: error, no retry */
709 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
711 return GNUNET_SYSERR;
716 /* Buffer is bigger than message: error, no retry
717 * This should never happen!*/
720 return GNUNET_SYSERR;
725 LOG (GNUNET_ERROR_TYPE_DEBUG,
726 "UNIX transmit %u-byte message to %s (%d: %s)\n",
727 (unsigned int) msgbuf_size,
728 GNUNET_a2s ((const struct sockaddr *)un, un_len),
730 (sent < 0) ? STRERROR (errno) : "ok");
737 * Closure for 'get_session_it'.
739 struct GetSessionIteratorContext
742 * Location to store the session, if found.
747 * Address information.
752 * Number of bytes in @e address
759 * Function called to find a session by address.
761 * @param cls the 'struct LookupCtx'
762 * @param key peer we are looking for (unused)
763 * @param value a session
764 * @return #GNUNET_YES if not found (continue looking), #GNUNET_NO on success
767 get_session_it (void *cls,
768 const struct GNUNET_PeerIdentity *key,
771 struct GetSessionIteratorContext *gsi = cls;
772 struct Session *s = value;
774 if ((GNUNET_NO == s->inbound) && (gsi->addrlen == s->addrlen) &&
775 (0 == memcmp (gsi->address, s->addr, s->addrlen)) )
785 * Session was idle for too long, so disconnect it
787 * @param cls the 'struct Session' to disconnect
788 * @param tc scheduler context
791 session_timeout (void *cls,
792 const struct GNUNET_SCHEDULER_TaskContext *tc)
794 struct Session *s = cls;
796 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
797 LOG (GNUNET_ERROR_TYPE_DEBUG,
798 "Session %p was idle for %s, disconnecting\n",
800 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
802 unix_session_disconnect (s->plugin, s);
807 * Function obtain the network type for a session
809 * @param cls closure ('struct Plugin*')
810 * @param session the session
811 * @return the network type in HBO or #GNUNET_SYSERR
813 static enum GNUNET_ATS_Network_Type
814 unix_get_network (void *cls,
815 struct Session *session)
817 GNUNET_assert (NULL != session);
818 return GNUNET_ATS_NET_LOOPBACK;
823 * Creates a new outbound session the transport service will use to send data to the
826 * @param cls the plugin
827 * @param address the address
828 * @return the session or NULL of max connections exceeded
830 static struct Session *
831 unix_plugin_get_session (void *cls,
832 const struct GNUNET_HELLO_Address *address)
834 struct Plugin *plugin = cls;
836 struct GetSessionIteratorContext gsi;
837 struct UnixAddress *ua;
839 uint32_t addr_str_len;
841 GNUNET_assert (NULL != plugin);
842 GNUNET_assert (NULL != address);
844 ua = (struct UnixAddress *) address->address;
845 if ((NULL == address->address) || (0 == address->address_length) ||
846 (sizeof (struct UnixAddress) > address->address_length))
851 addrstr = (char *) &ua[1];
852 addr_str_len = ntohl (ua->addrlen);
853 if (addr_str_len != address->address_length - sizeof (struct UnixAddress))
855 /* This can be a legacy address */
859 if ('\0' != addrstr[addr_str_len - 1])
864 if (strlen (addrstr) + 1 != addr_str_len)
870 /* Check if already existing */
871 gsi.address = (const char *) address->address;
872 gsi.addrlen = address->address_length;
874 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->session_map,
876 &get_session_it, &gsi);
879 LOG (GNUNET_ERROR_TYPE_DEBUG,
880 "Found existing session\n");
884 /* create a new session */
885 s = GNUNET_malloc (sizeof (struct Session) + address->address_length);
886 s->addr = (struct UnixAddress *) &s[1];
887 s->addrlen = address->address_length;
889 s->inbound = GNUNET_NO;
890 memcpy (s->addr, address->address, address->address_length);
891 memcpy (&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
892 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
893 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
896 LOG (GNUNET_ERROR_TYPE_DEBUG,
897 "Creating a new session %p for address `%s'\n",
898 s, unix_address_to_string (NULL, address->address, address->address_length));
899 (void) GNUNET_CONTAINER_multipeermap_put (plugin->session_map,
901 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
902 GNUNET_STATISTICS_set (plugin->env->stats,
903 "# UNIX sessions active",
904 GNUNET_CONTAINER_multipeermap_size (plugin->session_map),
911 * Function that can be used by the transport service to transmit
912 * a message using the plugin. Note that in the case of a
913 * peer disconnecting, the continuation MUST be called
914 * prior to the disconnect notification itself. This function
915 * will be called with this peer's HELLO message to initiate
916 * a fresh connection to another peer.
919 * @param session which session must be used
920 * @param msgbuf the message to transmit
921 * @param msgbuf_size number of bytes in @a msgbuf
922 * @param priority how important is the message (most plugins will
923 * ignore message priority and just FIFO)
924 * @param to how long to wait at most for the transmission (does not
925 * require plugins to discard the message after the timeout,
926 * just advisory for the desired delay; most plugins will ignore
928 * @param cont continuation to call once the message has
929 * been transmitted (or if the transport is ready
930 * for the next transmission call; or if the
931 * peer disconnected...); can be NULL
932 * @param cont_cls closure for @a cont
933 * @return number of bytes used (on the physical network, with overheads);
934 * -1 on hard errors (i.e. address invalid); 0 is a legal value
935 * and does NOT mean that the message was not transmitted (DV)
938 unix_plugin_send (void *cls,
939 struct Session *session,
940 const char *msgbuf, size_t msgbuf_size,
941 unsigned int priority,
942 struct GNUNET_TIME_Relative to,
943 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
945 struct Plugin *plugin = cls;
946 struct UNIXMessageWrapper *wrapper;
947 struct UNIXMessage *message;
951 GNUNET_CONTAINER_multipeermap_contains_value (plugin->session_map,
955 LOG (GNUNET_ERROR_TYPE_ERROR,
956 "Invalid session for peer `%s' `%s'\n",
957 GNUNET_i2s (&session->target),
958 (const char *) session->addr);
960 return GNUNET_SYSERR;
962 LOG (GNUNET_ERROR_TYPE_DEBUG,
963 "Sending %u bytes with session for peer `%s' `%s'\n",
965 GNUNET_i2s (&session->target),
966 (const char *) session->addr);
967 ssize = sizeof (struct UNIXMessage) + msgbuf_size;
968 message = GNUNET_malloc (sizeof (struct UNIXMessage) + msgbuf_size);
969 message->header.size = htons (ssize);
970 message->header.type = htons (0);
971 memcpy (&message->sender, plugin->env->my_identity,
972 sizeof (struct GNUNET_PeerIdentity));
973 memcpy (&message[1], msgbuf, msgbuf_size);
974 reschedule_session_timeout (session);
975 wrapper = GNUNET_new (struct UNIXMessageWrapper);
976 wrapper->msg = message;
977 wrapper->msgsize = ssize;
978 wrapper->payload = msgbuf_size;
979 wrapper->priority = priority;
980 wrapper->timeout = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), to);
981 wrapper->cont = cont;
982 wrapper->cont_cls = cont_cls;
983 wrapper->session = session;
984 GNUNET_CONTAINER_DLL_insert (plugin->msg_head,
987 plugin->bytes_in_queue += ssize;
988 GNUNET_STATISTICS_set (plugin->env->stats,
989 "# bytes currently in UNIX buffers",
990 plugin->bytes_in_queue,
992 if (GNUNET_NO == plugin->with_ws)
993 reschedule_select (plugin);
999 * Demultiplexer for UNIX messages
1001 * @param plugin the main plugin for this transport
1002 * @param sender from which peer the message was received
1003 * @param currhdr pointer to the header of the message
1004 * @param ua address to look for
1005 * @param ua_len length of the address @a ua
1008 unix_demultiplexer (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender,
1009 const struct GNUNET_MessageHeader *currhdr,
1010 const struct UnixAddress *ua, size_t ua_len)
1012 struct Session *s = NULL;
1013 struct GNUNET_HELLO_Address * addr;
1015 GNUNET_break (ntohl(plugin->ats_network.value) != GNUNET_ATS_NET_UNSPECIFIED);
1016 GNUNET_assert (ua_len >= sizeof (struct UnixAddress));
1017 LOG (GNUNET_ERROR_TYPE_DEBUG,
1018 "Received message from %s\n",
1019 unix_address_to_string(NULL, ua, ua_len));
1020 GNUNET_STATISTICS_update (plugin->env->stats,
1021 "# bytes received via UNIX",
1022 ntohs (currhdr->size),
1025 addr = GNUNET_HELLO_address_allocate (sender,
1029 s = lookup_session (plugin, sender, ua, ua_len);
1032 s = unix_plugin_get_session (plugin, addr);
1033 s->inbound = GNUNET_YES;
1034 /* Notify transport and ATS about new inbound session */
1035 plugin->env->session_start (NULL, sender,
1036 PLUGIN_NAME, ua, ua_len, s, &plugin->ats_network, 1);
1038 reschedule_session_timeout (s);
1040 plugin->env->receive (plugin->env->cls, sender, currhdr, s,
1041 (GNUNET_YES == s->inbound) ? NULL : (const char *) ua,
1042 (GNUNET_YES == s->inbound) ? 0 : ua_len);
1044 plugin->env->update_address_metrics (plugin->env->cls, sender,
1045 (GNUNET_YES == s->inbound) ? NULL : (const char *) ua,
1046 (GNUNET_YES == s->inbound) ? 0 : ua_len,
1047 s, &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;
1071 const struct GNUNET_MessageHeader *currhdr;
1075 addrlen = sizeof (un);
1076 memset (&un, 0, sizeof (un));
1079 GNUNET_NETWORK_socket_recvfrom (plugin->unix_sock.desc, buf, sizeof (buf),
1080 (struct sockaddr *) &un, &addrlen);
1082 if ((GNUNET_SYSERR == ret) && ((errno == EAGAIN) || (errno == ENOBUFS)))
1085 if (ret == GNUNET_SYSERR)
1087 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom");
1092 LOG (GNUNET_ERROR_TYPE_DEBUG,
1093 "Read %d bytes from socket %s\n",
1098 GNUNET_assert (AF_UNIX == (un.sun_family));
1099 ua_len = sizeof (struct UnixAddress) + strlen (un.sun_path) + 1;
1100 ua = GNUNET_malloc (ua_len);
1101 ua->addrlen = htonl (strlen (&un.sun_path[0]) +1);
1102 ua->options = htonl (0);
1103 memcpy (&ua[1], &un.sun_path[0], strlen (un.sun_path) + 1);
1105 msg = (struct UNIXMessage *) buf;
1106 csize = ntohs (msg->header.size);
1107 if ((csize < sizeof (struct UNIXMessage)) || (csize > ret))
1109 GNUNET_break_op (0);
1113 msgbuf = (char *) &msg[1];
1114 memcpy (&sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
1116 tsize = csize - sizeof (struct UNIXMessage);
1117 while (offset + sizeof (struct GNUNET_MessageHeader) <= tsize)
1119 currhdr = (struct GNUNET_MessageHeader *) &msgbuf[offset];
1120 csize = ntohs (currhdr->size);
1121 if ((csize < sizeof (struct GNUNET_MessageHeader)) ||
1122 (csize > tsize - offset))
1124 GNUNET_break_op (0);
1127 unix_demultiplexer (plugin, &sender, currhdr, ua, ua_len);
1135 * Write to UNIX domain socket (it is ready).
1137 * @param plugin the plugin
1140 unix_plugin_select_write (struct Plugin *plugin)
1143 struct UNIXMessageWrapper * msgw;
1145 while (NULL != (msgw = plugin->msg_tail))
1147 if (GNUNET_TIME_absolute_get_remaining (msgw->timeout).rel_value_us > 0)
1148 break; /* Message is ready for sending */
1149 /* Message has a timeout */
1150 LOG (GNUNET_ERROR_TYPE_DEBUG,
1151 "Timeout for message with %u bytes \n",
1152 (unsigned int) msgw->msgsize);
1153 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
1154 plugin->bytes_in_queue -= msgw->msgsize;
1155 GNUNET_STATISTICS_set (plugin->env->stats,
1156 "# bytes currently in UNIX buffers",
1157 plugin->bytes_in_queue, GNUNET_NO);
1158 GNUNET_STATISTICS_update (plugin->env->stats,
1159 "# UNIX bytes discarded",
1162 if (NULL != msgw->cont)
1163 msgw->cont (msgw->cont_cls,
1164 &msgw->session->target,
1168 GNUNET_free (msgw->msg);
1172 return; /* Nothing to send at the moment */
1174 sent = unix_real_send (plugin,
1175 plugin->unix_sock.desc,
1176 &msgw->session->target,
1177 (const char *) msgw->msg,
1181 msgw->session->addr,
1182 msgw->session->addrlen,
1184 msgw->cont, msgw->cont_cls);
1188 GNUNET_STATISTICS_update (plugin->env->stats,
1189 "# UNIX retry attempts",
1193 if (GNUNET_SYSERR == sent)
1195 /* failed and no retry */
1196 if (NULL != msgw->cont)
1197 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR, msgw->payload, 0);
1199 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
1201 GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
1202 plugin->bytes_in_queue -= msgw->msgsize;
1203 GNUNET_STATISTICS_set (plugin->env->stats,
1204 "# bytes currently in UNIX buffers",
1205 plugin->bytes_in_queue, GNUNET_NO);
1206 GNUNET_STATISTICS_update (plugin->env->stats,
1207 "# UNIX bytes discarded",
1211 GNUNET_free (msgw->msg);
1215 /* successfully sent bytes */
1216 GNUNET_break (sent > 0);
1217 GNUNET_CONTAINER_DLL_remove (plugin->msg_head,
1220 GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
1221 plugin->bytes_in_queue -= msgw->msgsize;
1222 GNUNET_STATISTICS_set (plugin->env->stats,
1223 "# bytes currently in UNIX buffers",
1224 plugin->bytes_in_queue,
1226 GNUNET_STATISTICS_update (plugin->env->stats,
1227 "# bytes transmitted via UNIX",
1230 if (NULL != msgw->cont)
1231 msgw->cont (msgw->cont_cls, &msgw->session->target,
1235 GNUNET_free (msgw->msg);
1241 * We have been notified that our writeset has something to read. We don't
1242 * know which socket needs to be read, so we have to check each one
1243 * Then reschedule this function to be called again once more is available.
1245 * @param cls the plugin handle
1246 * @param tc the scheduling context (for rescheduling this function again)
1249 unix_plugin_select (void *cls,
1250 const struct GNUNET_SCHEDULER_TaskContext *tc)
1252 struct Plugin *plugin = cls;
1254 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1255 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1258 if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
1260 /* Ready to send data */
1261 GNUNET_assert (GNUNET_NETWORK_fdset_isset
1262 (tc->write_ready, plugin->unix_sock.desc));
1263 if (NULL != plugin->msg_head)
1264 unix_plugin_select_write (plugin);
1267 if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
1269 /* Ready to receive data */
1270 GNUNET_assert (GNUNET_NETWORK_fdset_isset
1271 (tc->read_ready, plugin->unix_sock.desc));
1272 unix_plugin_select_read (plugin);
1274 reschedule_select (plugin);
1279 * Create a slew of UNIX sockets. If possible, use IPv6 and IPv4.
1281 * @param cls closure for server start, should be a struct Plugin *
1282 * @return number of sockets created or #GNUNET_SYSERR on error
1285 unix_transport_server_start (void *cls)
1287 struct Plugin *plugin = cls;
1288 struct sockaddr_un *un;
1291 un = unix_address_to_sockaddr (plugin->unix_socket_path, &un_len);
1292 plugin->ats_network = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) un, un_len);
1293 plugin->unix_sock.desc =
1294 GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_DGRAM, 0);
1295 if (NULL == plugin->unix_sock.desc)
1297 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
1298 return GNUNET_SYSERR;
1301 GNUNET_NETWORK_socket_bind (plugin->unix_sock.desc, (const struct sockaddr *) un, un_len))
1303 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
1304 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc);
1305 plugin->unix_sock.desc = NULL;
1307 return GNUNET_SYSERR;
1309 LOG (GNUNET_ERROR_TYPE_DEBUG, "Bound to `%s'\n", plugin->unix_socket_path);
1310 plugin->rs = GNUNET_NETWORK_fdset_create ();
1311 plugin->ws = GNUNET_NETWORK_fdset_create ();
1312 GNUNET_NETWORK_fdset_zero (plugin->rs);
1313 GNUNET_NETWORK_fdset_zero (plugin->ws);
1314 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->unix_sock.desc);
1315 GNUNET_NETWORK_fdset_set (plugin->ws, plugin->unix_sock.desc);
1317 reschedule_select (plugin);
1324 * Function that will be called to check if a binary address for this
1325 * plugin is well-formed and corresponds to an address for THIS peer
1326 * (as per our configuration). Naturally, if absolutely necessary,
1327 * plugins can be a bit conservative in their answer, but in general
1328 * plugins should make sure that the address does not redirect
1329 * traffic to a 3rd party that might try to man-in-the-middle our
1332 * @param cls closure, should be our handle to the Plugin
1333 * @param addr pointer to the address
1334 * @param addrlen length of addr
1335 * @return GNUNET_OK if this is a plausible address for this peer
1336 * and transport, GNUNET_SYSERR if not
1340 unix_check_address (void *cls, const void *addr, size_t addrlen)
1342 struct Plugin* plugin = cls;
1343 struct UnixAddress *ua = (struct UnixAddress *) addr;
1345 size_t addr_str_len;
1347 if ((NULL == addr) || (0 == addrlen) || (sizeof (struct UnixAddress) > addrlen))
1350 return GNUNET_SYSERR;
1352 addrstr = (char *) &ua[1];
1353 addr_str_len = ntohl (ua->addrlen);
1354 if ('\0' != addrstr[addr_str_len - 1])
1357 return GNUNET_SYSERR;
1359 if (strlen (addrstr) + 1 != addr_str_len)
1362 return GNUNET_SYSERR;
1365 if (0 == strcmp (plugin->unix_socket_path, addrstr))
1367 return GNUNET_SYSERR;
1372 * Convert the transports address to a nice, human-readable
1375 * @param cls closure
1376 * @param type name of the transport that generated the address
1377 * @param addr one of the addresses of the host, NULL for the last address
1378 * the specific address format depends on the transport
1379 * @param addrlen length of the @a addr
1380 * @param numeric should (IP) addresses be displayed in numeric form?
1381 * @param timeout after how long should we give up?
1382 * @param asc function to call on each string
1383 * @param asc_cls closure for @a asc
1386 unix_plugin_address_pretty_printer (void *cls, const char *type,
1387 const void *addr, size_t addrlen,
1389 struct GNUNET_TIME_Relative timeout,
1390 GNUNET_TRANSPORT_AddressStringCallback asc,
1393 if ((NULL != addr) && (addrlen > 0))
1395 asc (asc_cls, unix_address_to_string (NULL, addr, addrlen));
1397 else if (0 == addrlen)
1399 asc (asc_cls, TRANSPORT_SESSION_INBOUND_STRING);
1404 asc (asc_cls, "<invalid UNIX address>");
1406 asc (asc_cls, NULL);
1411 * Function called to convert a string address to
1414 * @param cls closure ('struct Plugin*')
1415 * @param addr string address
1416 * @param addrlen length of the @a addr (strlen(addr) + '\0')
1417 * @param buf location to store the buffer
1418 * If the function returns #GNUNET_SYSERR, its contents are undefined.
1419 * @param added length of created address
1420 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1423 unix_string_to_address (void *cls,
1424 const char *addr, uint16_t addrlen,
1425 void **buf, size_t *added)
1427 struct UnixAddress *ua;
1434 /* Format unix.options.address */
1439 if ((NULL == addr) || (addrlen == 0))
1442 return GNUNET_SYSERR;
1444 if ('\0' != addr[addrlen - 1])
1447 return GNUNET_SYSERR;
1449 if (strlen (addr) != addrlen - 1)
1452 return GNUNET_SYSERR;
1454 plugin = GNUNET_strdup (addr);
1455 optionstr = strchr (plugin, '.');
1456 if (NULL == optionstr)
1459 GNUNET_free (plugin);
1460 return GNUNET_SYSERR;
1462 optionstr[0] = '\0';
1464 options = atol (optionstr);
1465 address = strchr (optionstr, '.');
1466 if (NULL == address)
1469 GNUNET_free (plugin);
1470 return GNUNET_SYSERR;
1474 if (0 != strcmp(plugin, PLUGIN_NAME))
1477 GNUNET_free (plugin);
1478 return GNUNET_SYSERR;
1481 ua_size = sizeof (struct UnixAddress) + strlen (address) + 1;
1482 ua = GNUNET_malloc (ua_size);
1483 ua->options = htonl (options);
1484 ua->addrlen = htonl (strlen (address) + 1);
1485 memcpy (&ua[1], address, strlen (address) + 1);
1486 GNUNET_free (plugin);
1495 * Notify transport service about address
1497 * @param cls the plugin
1501 address_notification (void *cls,
1502 const struct GNUNET_SCHEDULER_TaskContext *tc)
1504 struct Plugin *plugin = cls;
1506 struct UnixAddress *ua;
1508 len = sizeof (struct UnixAddress) + strlen (plugin->unix_socket_path) + 1;
1509 ua = GNUNET_malloc (len);
1510 ua->options = htonl (myoptions);
1511 ua->addrlen = htonl(strlen (plugin->unix_socket_path) + 1);
1512 memcpy (&ua[1], plugin->unix_socket_path, strlen (plugin->unix_socket_path) + 1);
1514 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1515 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
1522 * Increment session timeout due to activity
1524 * @param s session for which the timeout should be moved
1527 reschedule_session_timeout (struct Session *s)
1529 GNUNET_assert (NULL != s);
1530 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1531 GNUNET_SCHEDULER_cancel (s->timeout_task);
1532 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1535 LOG (GNUNET_ERROR_TYPE_DEBUG,
1536 "Timeout rescheduled for session %p set to %s\n",
1538 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1544 * Function called on sessions to disconnect
1546 * @param cls the plugin
1547 * @param key peer identity (unused)
1548 * @param value the 'struct Session' to disconnect
1549 * @return #GNUNET_YES (always, continue to iterate)
1552 get_session_delete_it (void *cls,
1553 const struct GNUNET_PeerIdentity *key,
1556 struct Plugin *plugin = cls;
1557 struct Session *s = value;
1559 unix_session_disconnect (plugin, s);
1565 * Disconnect from a remote node. Clean up session if we have one for this peer
1567 * @param cls closure for this call (should be handle to Plugin)
1568 * @param target the peeridentity of the peer to disconnect
1569 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the operation failed
1572 unix_peer_disconnect (void *cls,
1573 const struct GNUNET_PeerIdentity *target)
1575 struct Plugin *plugin = cls;
1577 GNUNET_assert (plugin != NULL);
1578 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->session_map,
1580 &get_session_delete_it, plugin);
1585 * The exported method. Initializes the plugin and returns a
1586 * struct with the callbacks.
1588 * @param cls the plugin's execution environment
1589 * @return NULL on error, plugin functions otherwise
1592 libgnunet_plugin_transport_unix_init (void *cls)
1594 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1595 unsigned long long port;
1596 struct GNUNET_TRANSPORT_PluginFunctions *api;
1597 struct Plugin *plugin;
1598 int sockets_created;
1600 if (NULL == env->receive)
1602 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1603 initialze the plugin or the API */
1604 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
1606 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1607 api->address_to_string = &unix_address_to_string;
1608 api->string_to_address = &unix_string_to_address;
1612 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-unix", "PORT",
1614 port = UNIX_NAT_DEFAULT_PORT;
1615 plugin = GNUNET_new (struct Plugin);
1616 plugin->port = port;
1618 GNUNET_asprintf (&plugin->unix_socket_path,
1619 "/tmp/unix-plugin-sock.%d",
1622 /* Initialize my flags */
1625 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
1628 api->get_session = &unix_plugin_get_session;
1629 api->send = &unix_plugin_send;
1630 api->disconnect_peer = &unix_peer_disconnect;
1631 api->disconnect_session = &unix_session_disconnect;
1632 api->query_keepalive_factor = &unix_query_keepalive_factor;
1633 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1634 api->address_to_string = &unix_address_to_string;
1635 api->check_address = &unix_check_address;
1636 api->string_to_address = &unix_string_to_address;
1637 api->get_network = &unix_get_network;
1638 sockets_created = unix_transport_server_start (plugin);
1639 if (0 == sockets_created)
1640 LOG (GNUNET_ERROR_TYPE_WARNING,
1641 _("Failed to open UNIX listen socket\n"));
1642 plugin->session_map = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1643 plugin->address_update_task = GNUNET_SCHEDULER_add_now (&address_notification, plugin);
1649 * Shutdown the plugin.
1651 * @param cls the plugin API returned from the initialization function
1652 * @return NULL (always)
1655 libgnunet_plugin_transport_unix_done (void *cls)
1657 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1658 struct Plugin *plugin = api->cls;
1659 struct UNIXMessageWrapper * msgw;
1660 struct UnixAddress *ua;
1669 len = sizeof (struct UnixAddress) + strlen (plugin->unix_socket_path) + 1;
1670 ua = GNUNET_malloc (len);
1671 ua->options = htonl (myoptions);
1672 ua->addrlen = htonl(strlen (plugin->unix_socket_path) + 1);
1673 memcpy (&ua[1], plugin->unix_socket_path, strlen (plugin->unix_socket_path) + 1);
1675 plugin->env->notify_address (plugin->env->cls, GNUNET_NO,
1678 while (NULL != (msgw = plugin->msg_head))
1680 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
1681 if (msgw->cont != NULL)
1682 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR,
1684 GNUNET_free (msgw->msg);
1688 if (GNUNET_SCHEDULER_NO_TASK != plugin->select_task)
1690 GNUNET_SCHEDULER_cancel (plugin->select_task);
1691 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1693 if (GNUNET_SCHEDULER_NO_TASK != plugin->address_update_task)
1695 GNUNET_SCHEDULER_cancel (plugin->address_update_task);
1696 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1698 if (NULL != plugin->unix_sock.desc)
1700 GNUNET_break (GNUNET_OK ==
1701 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc));
1702 plugin->unix_sock.desc = NULL;
1703 plugin->with_ws = GNUNET_NO;
1705 GNUNET_CONTAINER_multipeermap_iterate (plugin->session_map,
1706 &get_session_delete_it, plugin);
1707 GNUNET_CONTAINER_multipeermap_destroy (plugin->session_map);
1708 if (NULL != plugin->rs)
1709 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1710 if (NULL != plugin->ws)
1711 GNUNET_NETWORK_fdset_destroy (plugin->ws);
1712 GNUNET_free (plugin->unix_socket_path);
1713 GNUNET_free (plugin);
1718 /* end of plugin_transport_unix.c */