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_hello_lib.h"
31 #include "gnunet_util_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!?).
47 * How long until we give up on transmitting the welcome message?
49 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
52 * Default "port" to use, if configuration does not specify.
53 * Essentially just a number appended to the UNIX path.
55 #define UNIX_NAT_DEFAULT_PORT 22086
58 #define LOG(kind,...) GNUNET_log_from (kind, "transport-unix",__VA_ARGS__)
61 GNUNET_NETWORK_STRUCT_BEGIN
64 * UNIX Message-Packet header.
71 struct GNUNET_MessageHeader header;
74 * What is the identity of the sender (GNUNET_hash of public key)
76 struct GNUNET_PeerIdentity sender;
82 * Handle for a session.
86 struct GNUNET_PeerIdentity target;
88 struct Plugin * plugin;
95 * Session timeout task
97 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
101 struct UNIXMessageWrapper
104 * We keep messages in a doubly linked list.
106 struct UNIXMessageWrapper *next;
109 * We keep messages in a doubly linked list.
111 struct UNIXMessageWrapper *prev;
114 * The actual payload (allocated separately right now).
116 struct UNIXMessage * msg;
119 * Session this message belongs to.
121 struct Session *session;
124 * Function to call upon transmission.
126 GNUNET_TRANSPORT_TransmitContinuation cont;
129 * Closure for 'cont'.
134 * Timeout for this message.
136 struct GNUNET_TIME_Absolute timeout;
139 * Number of bytes in 'msg'.
144 * Number of bytes of payload encapsulated in 'msg'.
149 * Priority of the message (ignored, just dragged along in UNIX).
151 unsigned int priority;
156 * Encapsulation of all of the state of the plugin.
168 * Stored in a linked list.
170 struct PeerSession *next;
173 * Pointer to the global plugin struct.
175 struct Plugin *plugin;
178 * To whom are we talking to (set to our identity
179 * if we are still waiting for the welcome message)
181 struct GNUNET_PeerIdentity target;
184 * Address of the other peer (either based on our 'connect'
185 * call or on our 'accept' call).
190 * Length of connect_addr.
195 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
197 int expecting_welcome;
200 * From which socket do we need to send to this peer?
202 struct GNUNET_NETWORK_Handle *sock;
205 * Queue of messages for this peer, in the case that
206 * we have to await a connection...
208 struct MessageQueue *messages;
214 * Information we keep for each of our listen sockets.
216 struct UNIX_Sock_Info
221 struct GNUNET_NETWORK_Handle *desc;
224 * The port we bound to (not an actual PORT, as UNIX domain sockets
225 * don't have ports, but rather a number in the path name to make this
233 * Encapsulation of all of the state of the plugin.
239 * ID of task used to update our addresses when one expires.
241 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
246 GNUNET_SCHEDULER_TaskIdentifier select_task;
249 * Number of bytes we currently have in our write queue.
251 unsigned long long bytes_in_queue;
256 struct GNUNET_TRANSPORT_PluginEnvironment *env;
261 struct GNUNET_CONTAINER_MultiHashMap *session_map;
266 struct GNUNET_NETWORK_FDSet *rs;
271 struct GNUNET_NETWORK_FDSet *ws;
274 * Path of our unix domain socket (/tmp/unix-plugin-PORT)
276 char *unix_socket_path;
279 * Head of queue of messages to transmit.
281 struct UNIXMessageWrapper *msg_head;
284 * Tail of queue of messages to transmit.
286 struct UNIXMessageWrapper *msg_tail;
289 * socket that we transmit all data with
291 struct UNIX_Sock_Info unix_sock;
296 struct GNUNET_ATS_Information ats_network;
299 * Is the write set in the current 'select' task? GNUNET_NO if the
300 * write queue was empty when the main task was scheduled,
301 * GNUNET_YES if we're already waiting for being allowed to write.
306 * Integer to append to unix domain socket.
314 * Increment session timeout due to activity
316 * @param s session for which the timeout should be moved
319 reschedule_session_timeout (struct Session *s);
323 * We have been notified that our writeset has something to read. We don't
324 * know which socket needs to be read, so we have to check each one
325 * Then reschedule this function to be called again once more is available.
327 * @param cls the plugin handle
328 * @param tc the scheduling context (for rescheduling this function again)
331 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
335 * Re-schedule the main 'select' callback (unix_plugin_select)
338 * @param plugin the plugin context
341 reschedule_select (struct Plugin * plugin)
343 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
345 GNUNET_SCHEDULER_cancel (plugin->select_task);
346 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
348 if (NULL != plugin->msg_head)
350 plugin->select_task =
351 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
352 GNUNET_TIME_UNIT_FOREVER_REL,
355 &unix_plugin_select, plugin);
356 plugin->with_ws = GNUNET_YES;
360 plugin->select_task =
361 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
362 GNUNET_TIME_UNIT_FOREVER_REL,
365 &unix_plugin_select, plugin);
366 plugin->with_ws = GNUNET_NO;
372 * Closure to 'lookup_session_it'.
377 * Location to store the session, if found.
382 * Address we are looking for.
384 const struct sockaddr_un *addr;
389 * Function called to find a session by address.
391 * @param cls the 'struct LookupCtx'
392 * @param key peer we are looking for (unused)
393 * @param value a session
394 * @return GNUNET_YES if not found (continue looking), GNUNET_NO on success
397 lookup_session_it (void *cls,
398 const struct GNUNET_HashCode * key,
401 struct LookupCtx *lctx = cls;
402 struct Session *t = value;
404 if (0 == strcmp (t->addr, lctx->addr->sun_path))
414 * Find an existing session by address.
416 * @param plugin the plugin
417 * @param sender for which peer should the session be?
418 * @param addr address to look for
419 * @return NULL if session was not found
421 static struct Session *
422 lookup_session (struct Plugin *plugin,
423 const struct GNUNET_PeerIdentity *sender,
424 const struct sockaddr_un *addr)
426 struct LookupCtx lctx;
428 GNUNET_assert (NULL != plugin);
429 GNUNET_assert (NULL != sender);
430 GNUNET_assert (NULL != addr);
433 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map,
435 &lookup_session_it, &lctx);
441 * Functions with this signature are called whenever we need
442 * to close a session due to a disconnect or failure to
443 * establish a connection.
445 * @param s session to close down
448 disconnect_session (struct Session *s)
450 struct Plugin *plugin = s->plugin;
451 struct UNIXMessageWrapper *msgw;
452 struct UNIXMessageWrapper *next;
455 LOG (GNUNET_ERROR_TYPE_DEBUG,
456 "Disconnecting session for peer `%s' `%s'\n",
457 GNUNET_i2s (&s->target),
459 plugin->env->session_end (plugin->env->cls, &s->target, s);
461 next = plugin->msg_head;
466 if (msgw->session != s)
468 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
469 if (NULL != msgw->cont)
470 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR,
472 GNUNET_free (msgw->msg);
474 removed = GNUNET_YES;
476 GNUNET_assert (GNUNET_YES ==
477 GNUNET_CONTAINER_multihashmap_remove (plugin->session_map,
478 &s->target.hashPubKey,
480 GNUNET_STATISTICS_set (plugin->env->stats,
481 "# UNIX sessions active",
482 GNUNET_CONTAINER_multihashmap_size (plugin->session_map),
484 if ((GNUNET_YES == removed) && (NULL == plugin->msg_head))
485 reschedule_select (plugin);
486 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
488 GNUNET_SCHEDULER_cancel (s->timeout_task);
489 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
496 * Actually send out the message, assume we've got the address and
497 * send_handle squared away!
500 * @param send_handle which handle to send message on
501 * @param target who should receive this message (ignored by UNIX)
502 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
503 * @param msgbuf_size the size of the msgbuf to send
504 * @param priority how important is the message (ignored by UNIX)
505 * @param timeout when should we time out (give up) if we can not transmit?
506 * @param addr the addr to send the message to, needs to be a sockaddr for us
507 * @param addrlen the len of addr
508 * @param payload bytes payload to send
509 * @param cont continuation to call once the message has
510 * been transmitted (or if the transport is ready
511 * for the next transmission call; or if the
512 * peer disconnected...)
513 * @param cont_cls closure for cont
514 * @return on success the number of bytes written, RETRY for retry, -1 on errors
517 unix_real_send (void *cls,
518 struct GNUNET_NETWORK_Handle *send_handle,
519 const struct GNUNET_PeerIdentity *target, const char *msgbuf,
520 size_t msgbuf_size, unsigned int priority,
521 struct GNUNET_TIME_Absolute timeout,
525 GNUNET_TRANSPORT_TransmitContinuation cont,
528 struct Plugin *plugin = cls;
532 struct sockaddr_un un;
535 GNUNET_assert (NULL != plugin);
536 if (NULL == send_handle)
538 GNUNET_break (0); /* We do not have a send handle */
539 return GNUNET_SYSERR;
541 if ((NULL == addr) || (0 == addrlen))
543 GNUNET_break (0); /* Can never send if we don't have an address */
544 return GNUNET_SYSERR;
547 /* Prepare address */
548 memset (&un, 0, sizeof (un));
549 un.sun_family = AF_UNIX;
550 slen = strlen (addr) + 1;
551 if (slen >= sizeof (un.sun_path))
552 slen = sizeof (un.sun_path) - 1;
553 GNUNET_assert (slen < sizeof (un.sun_path));
554 memcpy (un.sun_path, addr, slen);
555 un.sun_path[slen] = '\0';
556 slen = sizeof (struct sockaddr_un);
558 un.sun_path[0] = '\0';
560 #if HAVE_SOCKADDR_IN_SIN_LEN
561 un.sun_len = (u_char) slen;
563 sb = (struct sockaddr *) &un;
569 sent = GNUNET_NETWORK_socket_sendto (send_handle, msgbuf, msgbuf_size, sb, sbs);
571 if (GNUNET_SYSERR == sent)
573 if ( (EAGAIN == errno) ||
575 return RETRY; /* We have to retry later */
576 if (EMSGSIZE == errno)
579 socklen_t len = sizeof (size);
581 GNUNET_NETWORK_socket_getsockopt ((struct GNUNET_NETWORK_Handle *)
582 send_handle, SOL_SOCKET, SO_SNDBUF, &size,
584 if (size < msgbuf_size)
586 LOG (GNUNET_ERROR_TYPE_DEBUG,
587 "Trying to increase socket buffer size from %i to %i for message size %i\n",
588 size, ((msgbuf_size / 1000) + 2) * 1000, msgbuf_size);
589 size = ((msgbuf_size / 1000) + 2) * 1000;
590 if (GNUNET_OK == GNUNET_NETWORK_socket_setsockopt
591 ((struct GNUNET_NETWORK_Handle *) send_handle, SOL_SOCKET, SO_SNDBUF,
592 &size, sizeof (size)))
593 goto resend; /* Increased buffer size, retry sending */
596 /* Could not increase buffer size: error, no retry */
597 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
598 return GNUNET_SYSERR;
603 /* Buffer is bigger than message: error, no retry
604 * This should never happen!*/
606 return GNUNET_SYSERR;
611 LOG (GNUNET_ERROR_TYPE_DEBUG,
612 "UNIX transmit %u-byte message to %s (%d: %s)\n",
613 (unsigned int) msgbuf_size,
614 GNUNET_a2s (sb, sbs),
616 (sent < 0) ? STRERROR (errno) : "ok");
622 * Closure for 'get_session_it'.
624 struct GetSessionIteratorContext
627 * Location to store the session, if found.
632 * Address information.
637 * Number of bytes in 'address'
644 * Function called to find a session by address.
646 * @param cls the 'struct LookupCtx'
647 * @param key peer we are looking for (unused)
648 * @param value a session
649 * @return GNUNET_YES if not found (continue looking), GNUNET_NO on success
652 get_session_it (void *cls,
653 const struct GNUNET_HashCode *key,
656 struct GetSessionIteratorContext *gsi = cls;
657 struct Session *s = value;
659 if ( (gsi->addrlen == s->addrlen) &&
660 (0 == memcmp (gsi->address, s->addr, s->addrlen)) )
670 * Session was idle for too long, so disconnect it
672 * @param cls the 'struct Session' to disconnect
673 * @param tc scheduler context
676 session_timeout (void *cls,
677 const struct GNUNET_SCHEDULER_TaskContext *tc)
679 struct Session *s = cls;
681 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
682 LOG (GNUNET_ERROR_TYPE_DEBUG,
683 "Session %p was idle for %s, disconnecting\n",
685 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
687 disconnect_session (s);
692 * Creates a new outbound session the transport service will use to send data to the
695 * @param cls the plugin
696 * @param address the address
697 * @return the session or NULL of max connections exceeded
699 static struct Session *
700 unix_plugin_get_session (void *cls,
701 const struct GNUNET_HELLO_Address *address)
703 struct Plugin *plugin = cls;
705 struct GetSessionIteratorContext gsi;
707 GNUNET_assert (NULL != plugin);
708 GNUNET_assert (NULL != address);
710 /* Check if already existing */
711 gsi.address = (const char *) address->address;
712 gsi.addrlen = address->address_length;
714 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map,
715 &address->peer.hashPubKey,
716 &get_session_it, &gsi);
719 LOG (GNUNET_ERROR_TYPE_DEBUG,
720 "Found existing session\n");
724 /* create a new session */
725 s = GNUNET_malloc (sizeof (struct Session) + address->address_length);
727 s->addrlen = address->address_length;
729 memcpy (s->addr, address->address, s->addrlen);
730 memcpy (&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
732 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
733 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
736 LOG (GNUNET_ERROR_TYPE_DEBUG,
737 "Creating a new session %p with timeout set to %s\n",
739 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
741 (void) GNUNET_CONTAINER_multihashmap_put (plugin->session_map,
742 &address->peer.hashPubKey, s,
743 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
744 GNUNET_STATISTICS_set (plugin->env->stats,
745 "# UNIX sessions active",
746 GNUNET_CONTAINER_multihashmap_size (plugin->session_map),
753 * Function that can be used by the transport service to transmit
754 * a message using the plugin. Note that in the case of a
755 * peer disconnecting, the continuation MUST be called
756 * prior to the disconnect notification itself. This function
757 * will be called with this peer's HELLO message to initiate
758 * a fresh connection to another peer.
761 * @param session which session must be used
762 * @param msgbuf the message to transmit
763 * @param msgbuf_size number of bytes in 'msgbuf'
764 * @param priority how important is the message (most plugins will
765 * ignore message priority and just FIFO)
766 * @param to how long to wait at most for the transmission (does not
767 * require plugins to discard the message after the timeout,
768 * just advisory for the desired delay; most plugins will ignore
770 * @param cont continuation to call once the message has
771 * been transmitted (or if the transport is ready
772 * for the next transmission call; or if the
773 * peer disconnected...); can be NULL
774 * @param cont_cls closure for cont
775 * @return number of bytes used (on the physical network, with overheads);
776 * -1 on hard errors (i.e. address invalid); 0 is a legal value
777 * and does NOT mean that the message was not transmitted (DV)
780 unix_plugin_send (void *cls,
781 struct Session *session,
782 const char *msgbuf, size_t msgbuf_size,
783 unsigned int priority,
784 struct GNUNET_TIME_Relative to,
785 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
787 struct Plugin *plugin = cls;
788 struct UNIXMessageWrapper *wrapper;
789 struct UNIXMessage *message;
792 GNUNET_assert (NULL != plugin);
793 GNUNET_assert (NULL != session);
796 GNUNET_CONTAINER_multihashmap_contains_value (plugin->session_map,
797 &session->target.hashPubKey,
800 LOG (GNUNET_ERROR_TYPE_ERROR,
801 "Invalid session for peer `%s' `%s'\n",
802 GNUNET_i2s (&session->target),
803 (const char *) session->addr);
805 return GNUNET_SYSERR;
807 LOG (GNUNET_ERROR_TYPE_DEBUG,
808 "Sending %u bytes with session for peer `%s' `%s'\n",
810 GNUNET_i2s (&session->target),
811 (const char *) session->addr);
812 ssize = sizeof (struct UNIXMessage) + msgbuf_size;
813 message = GNUNET_malloc (sizeof (struct UNIXMessage) + msgbuf_size);
814 message->header.size = htons (ssize);
815 message->header.type = htons (0);
816 memcpy (&message->sender, plugin->env->my_identity,
817 sizeof (struct GNUNET_PeerIdentity));
818 memcpy (&message[1], msgbuf, msgbuf_size);
819 reschedule_session_timeout (session);
820 wrapper = GNUNET_malloc (sizeof (struct UNIXMessageWrapper));
821 wrapper->msg = message;
822 wrapper->msgsize = ssize;
823 wrapper->payload = msgbuf_size;
824 wrapper->priority = priority;
825 wrapper->timeout = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), to);
826 wrapper->cont = cont;
827 wrapper->cont_cls = cont_cls;
828 wrapper->session = session;
829 GNUNET_CONTAINER_DLL_insert (plugin->msg_head,
832 plugin->bytes_in_queue += ssize;
833 GNUNET_STATISTICS_set (plugin->env->stats,
834 "# bytes currently in UNIX buffers",
835 plugin->bytes_in_queue,
837 if (GNUNET_NO == plugin->with_ws)
838 reschedule_select (plugin);
844 * Demultiplexer for UNIX messages
846 * @param plugin the main plugin for this transport
847 * @param sender from which peer the message was received
848 * @param currhdr pointer to the header of the message
849 * @param un the address from which the message was received
850 * @param fromlen the length of the address
853 unix_demultiplexer (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender,
854 const struct GNUNET_MessageHeader *currhdr,
855 const struct sockaddr_un *un, size_t fromlen)
857 struct Session *s = NULL;
858 struct GNUNET_HELLO_Address * addr;
860 GNUNET_break (ntohl(plugin->ats_network.value) != GNUNET_ATS_NET_UNSPECIFIED);
862 GNUNET_assert (fromlen >= sizeof (struct sockaddr_un));
864 LOG (GNUNET_ERROR_TYPE_DEBUG,
865 "Received message from %s\n",
867 GNUNET_STATISTICS_update (plugin->env->stats,
868 "# bytes received via UNIX",
869 ntohs (currhdr->size),
872 addr = GNUNET_HELLO_address_allocate (sender,
875 strlen (un->sun_path) + 1);
876 s = lookup_session (plugin, sender, un);
878 s = unix_plugin_get_session (plugin, addr);
879 reschedule_session_timeout (s);
881 plugin->env->receive (plugin->env->cls, sender, currhdr,
882 s, un->sun_path, strlen (un->sun_path) + 1);
884 plugin->env->update_address_metrics (plugin->env->cls,
887 strlen (un->sun_path) + 1,
889 &plugin->ats_network, 1);
896 * Read from UNIX domain socket (it is ready).
898 * @param plugin the plugin
901 unix_plugin_select_read (struct Plugin *plugin)
903 char buf[65536] GNUNET_ALIGN;
904 struct UNIXMessage *msg;
905 struct GNUNET_PeerIdentity sender;
906 struct sockaddr_un un;
912 const struct GNUNET_MessageHeader *currhdr;
915 addrlen = sizeof (un);
916 memset (&un, 0, sizeof (un));
919 GNUNET_NETWORK_socket_recvfrom (plugin->unix_sock.desc, buf, sizeof (buf),
920 (struct sockaddr *) &un, &addrlen);
922 if ((GNUNET_SYSERR == ret) && ((errno == EAGAIN) || (errno == ENOBUFS)))
925 if (ret == GNUNET_SYSERR)
927 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom");
933 un.sun_path[0] = '/';
935 LOG (GNUNET_ERROR_TYPE_DEBUG, "Read %d bytes from socket %s\n", ret,
939 GNUNET_assert (AF_UNIX == (un.sun_family));
941 msg = (struct UNIXMessage *) buf;
942 csize = ntohs (msg->header.size);
943 if ((csize < sizeof (struct UNIXMessage)) || (csize > ret))
948 msgbuf = (char *) &msg[1];
949 memcpy (&sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
951 tsize = csize - sizeof (struct UNIXMessage);
952 while (offset + sizeof (struct GNUNET_MessageHeader) <= tsize)
954 currhdr = (struct GNUNET_MessageHeader *) &msgbuf[offset];
955 csize = ntohs (currhdr->size);
956 if ((csize < sizeof (struct GNUNET_MessageHeader)) ||
957 (csize > tsize - offset))
962 unix_demultiplexer (plugin, &sender, currhdr, &un, sizeof (un));
969 * Write to UNIX domain socket (it is ready).
971 * @param plugin the plugin
974 unix_plugin_select_write (struct Plugin *plugin)
977 struct UNIXMessageWrapper * msgw;
979 while (NULL != (msgw = plugin->msg_tail))
981 if (GNUNET_TIME_absolute_get_remaining (msgw->timeout).rel_value > 0)
982 break; /* Message is ready for sending */
983 /* Message has a timeout */
984 LOG (GNUNET_ERROR_TYPE_DEBUG,
985 "Timeout for message with %u bytes \n",
986 (unsigned int) msgw->msgsize);
987 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
988 plugin->bytes_in_queue -= msgw->msgsize;
989 GNUNET_STATISTICS_set (plugin->env->stats,
990 "# bytes currently in UNIX buffers",
991 plugin->bytes_in_queue, GNUNET_NO);
992 GNUNET_STATISTICS_update (plugin->env->stats,
993 "# UNIX bytes discarded",
996 if (NULL != msgw->cont)
997 msgw->cont (msgw->cont_cls,
998 &msgw->session->target,
1002 GNUNET_free (msgw->msg);
1006 return; /* Nothing to send at the moment */
1008 sent = unix_real_send (plugin,
1009 plugin->unix_sock.desc,
1010 &msgw->session->target,
1011 (const char *) msgw->msg,
1015 msgw->session->addr,
1016 msgw->session->addrlen,
1018 msgw->cont, msgw->cont_cls);
1022 GNUNET_STATISTICS_update (plugin->env->stats,
1023 "# UNIX retry attempts",
1027 if (GNUNET_SYSERR == sent)
1029 /* failed and no retry */
1030 if (NULL != msgw->cont)
1031 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR, msgw->payload, 0);
1033 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
1035 GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
1036 plugin->bytes_in_queue -= msgw->msgsize;
1037 GNUNET_STATISTICS_set (plugin->env->stats,
1038 "# bytes currently in UNIX buffers",
1039 plugin->bytes_in_queue, GNUNET_NO);
1040 GNUNET_STATISTICS_update (plugin->env->stats,
1041 "# UNIX bytes discarded",
1045 GNUNET_free (msgw->msg);
1049 /* successfully sent bytes */
1050 GNUNET_break (sent > 0);
1051 GNUNET_CONTAINER_DLL_remove (plugin->msg_head,
1054 GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
1055 plugin->bytes_in_queue -= msgw->msgsize;
1056 GNUNET_STATISTICS_set (plugin->env->stats,
1057 "# bytes currently in UNIX buffers",
1058 plugin->bytes_in_queue,
1060 GNUNET_STATISTICS_update (plugin->env->stats,
1061 "# bytes transmitted via UNIX",
1064 if (NULL != msgw->cont)
1065 msgw->cont (msgw->cont_cls, &msgw->session->target,
1069 GNUNET_free (msgw->msg);
1075 * We have been notified that our writeset has something to read. We don't
1076 * know which socket needs to be read, so we have to check each one
1077 * Then reschedule this function to be called again once more is available.
1079 * @param cls the plugin handle
1080 * @param tc the scheduling context (for rescheduling this function again)
1083 unix_plugin_select (void *cls,
1084 const struct GNUNET_SCHEDULER_TaskContext *tc)
1086 struct Plugin *plugin = cls;
1088 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1089 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1092 if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
1094 /* Ready to send data */
1095 GNUNET_assert (GNUNET_NETWORK_fdset_isset
1096 (tc->write_ready, plugin->unix_sock.desc));
1097 if (NULL != plugin->msg_head)
1098 unix_plugin_select_write (plugin);
1101 if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
1103 /* Ready to receive data */
1104 GNUNET_assert (GNUNET_NETWORK_fdset_isset
1105 (tc->read_ready, plugin->unix_sock.desc));
1106 unix_plugin_select_read (plugin);
1108 reschedule_select (plugin);
1113 * Create a slew of UNIX sockets. If possible, use IPv6 and IPv4.
1115 * @param cls closure for server start, should be a struct Plugin *
1116 * @return number of sockets created or GNUNET_SYSERR on error
1119 unix_transport_server_start (void *cls)
1121 struct Plugin *plugin = cls;
1122 struct sockaddr *serverAddr;
1124 struct sockaddr_un un;
1127 memset (&un, 0, sizeof (un));
1128 un.sun_family = AF_UNIX;
1129 slen = strlen (plugin->unix_socket_path) + 1;
1130 if (slen >= sizeof (un.sun_path))
1131 slen = sizeof (un.sun_path) - 1;
1133 memcpy (un.sun_path, plugin->unix_socket_path, slen);
1134 un.sun_path[slen] = '\0';
1135 slen = sizeof (struct sockaddr_un);
1136 #if HAVE_SOCKADDR_IN_SIN_LEN
1137 un.sun_len = (u_char) slen;
1140 serverAddr = (struct sockaddr *) &un;
1143 un.sun_path[0] = '\0';
1145 plugin->ats_network = plugin->env->get_address_type (plugin->env->cls, serverAddr, addrlen);
1146 plugin->unix_sock.desc =
1147 GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_DGRAM, 0);
1148 if (NULL == plugin->unix_sock.desc)
1150 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
1151 return GNUNET_SYSERR;
1153 if (GNUNET_NETWORK_socket_bind (plugin->unix_sock.desc, serverAddr, addrlen)
1156 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
1157 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc);
1158 plugin->unix_sock.desc = NULL;
1159 return GNUNET_SYSERR;
1161 LOG (GNUNET_ERROR_TYPE_DEBUG, "Bound to `%s'\n", plugin->unix_socket_path);
1162 plugin->rs = GNUNET_NETWORK_fdset_create ();
1163 plugin->ws = GNUNET_NETWORK_fdset_create ();
1164 GNUNET_NETWORK_fdset_zero (plugin->rs);
1165 GNUNET_NETWORK_fdset_zero (plugin->ws);
1166 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->unix_sock.desc);
1167 GNUNET_NETWORK_fdset_set (plugin->ws, plugin->unix_sock.desc);
1169 reschedule_select (plugin);
1176 * Function that will be called to check if a binary address for this
1177 * plugin is well-formed and corresponds to an address for THIS peer
1178 * (as per our configuration). Naturally, if absolutely necessary,
1179 * plugins can be a bit conservative in their answer, but in general
1180 * plugins should make sure that the address does not redirect
1181 * traffic to a 3rd party that might try to man-in-the-middle our
1184 * @param cls closure, should be our handle to the Plugin
1185 * @param addr pointer to the address
1186 * @param addrlen length of addr
1187 * @return GNUNET_OK if this is a plausible address for this peer
1188 * and transport, GNUNET_SYSERR if not
1192 unix_check_address (void *cls, const void *addr, size_t addrlen)
1194 LOG (GNUNET_ERROR_TYPE_DEBUG,
1195 "Informing transport service about my address `%s'\n",
1202 * Convert the transports address to a nice, human-readable
1205 * @param cls closure
1206 * @param type name of the transport that generated the address
1207 * @param addr one of the addresses of the host, NULL for the last address
1208 * the specific address format depends on the transport
1209 * @param addrlen length of the address
1210 * @param numeric should (IP) addresses be displayed in numeric form?
1211 * @param timeout after how long should we give up?
1212 * @param asc function to call on each string
1213 * @param asc_cls closure for asc
1216 unix_plugin_address_pretty_printer (void *cls, const char *type,
1217 const void *addr, size_t addrlen,
1219 struct GNUNET_TIME_Relative timeout,
1220 GNUNET_TRANSPORT_AddressStringCallback asc,
1223 if ((NULL != addr) && (addrlen > 0))
1225 asc (asc_cls, (const char *) addr);
1230 asc (asc_cls, "<invalid UNIX address>");
1232 asc (asc_cls, NULL);
1237 * Function called to convert a string address to
1240 * @param cls closure ('struct Plugin*')
1241 * @param addr string address
1242 * @param addrlen length of the address (strlen(addr) + '\0')
1243 * @param buf location to store the buffer
1244 * If the function returns GNUNET_SYSERR, its contents are undefined.
1245 * @param added length of created address
1246 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1249 unix_string_to_address (void *cls, const char *addr, uint16_t addrlen,
1250 void **buf, size_t *added)
1252 if ((NULL == addr) || (0 == addrlen))
1255 return GNUNET_SYSERR;
1258 if ('\0' != addr[addrlen - 1])
1261 return GNUNET_SYSERR;
1264 if (strlen (addr) != addrlen - 1)
1267 return GNUNET_SYSERR;
1270 (*buf) = strdup (addr);
1271 (*added) = strlen (addr) + 1;
1277 * Function called for a quick conversion of the binary address to
1278 * a numeric address. Note that the caller must not free the
1279 * address and that the next call to this function is allowed
1280 * to override the address again.
1282 * @param cls closure
1283 * @param addr binary address
1284 * @param addrlen length of the address
1285 * @return string representing the same address
1288 unix_address_to_string (void *cls, const void *addr, size_t addrlen)
1290 if ((addr != NULL) && (addrlen > 0))
1291 return (const char *) addr;
1297 * Notify transport service about address
1299 * @param cls the plugin
1303 address_notification (void *cls,
1304 const struct GNUNET_SCHEDULER_TaskContext *tc)
1306 struct Plugin *plugin = cls;
1308 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1309 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
1310 plugin->unix_socket_path,
1311 strlen (plugin->unix_socket_path) + 1,
1317 * Increment session timeout due to activity
1319 * @param s session for which the timeout should be moved
1322 reschedule_session_timeout (struct Session *s)
1324 GNUNET_assert (NULL != s);
1325 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1326 GNUNET_SCHEDULER_cancel (s->timeout_task);
1327 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1330 LOG (GNUNET_ERROR_TYPE_DEBUG,
1331 "Timeout rescheduled for session %p set to %s\n",
1333 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1339 * Function called on sessions to disconnect
1341 * @param cls the plugin (unused)
1342 * @param key peer identity (unused)
1343 * @param value the 'struct Session' to disconnect
1344 * @return GNUNET_YES (always, continue to iterate)
1347 get_session_delete_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1349 struct Session *s = value;
1351 disconnect_session (s);
1357 * Disconnect from a remote node. Clean up session if we have one for this peer
1359 * @param cls closure for this call (should be handle to Plugin)
1360 * @param target the peeridentity of the peer to disconnect
1361 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
1364 unix_disconnect (void *cls,
1365 const struct GNUNET_PeerIdentity *target)
1367 struct Plugin *plugin = cls;
1369 GNUNET_assert (plugin != NULL);
1370 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map,
1371 &target->hashPubKey,
1372 &get_session_delete_it, plugin);
1377 * The exported method. Initializes the plugin and returns a
1378 * struct with the callbacks.
1380 * @param cls the plugin's execution environment
1381 * @return NULL on error, plugin functions otherwise
1384 libgnunet_plugin_transport_unix_init (void *cls)
1386 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1387 unsigned long long port;
1388 struct GNUNET_TRANSPORT_PluginFunctions *api;
1389 struct Plugin *plugin;
1390 int sockets_created;
1392 if (NULL == env->receive)
1394 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1395 initialze the plugin or the API */
1396 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1398 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1399 api->address_to_string = &unix_address_to_string;
1400 api->string_to_address = &unix_string_to_address;
1404 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-unix", "PORT",
1406 port = UNIX_NAT_DEFAULT_PORT;
1407 plugin = GNUNET_malloc (sizeof (struct Plugin));
1408 plugin->port = port;
1410 GNUNET_asprintf (&plugin->unix_socket_path,
1411 "/tmp/unix-plugin-sock.%d",
1414 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1417 api->get_session = &unix_plugin_get_session;
1418 api->send = &unix_plugin_send;
1419 api->disconnect = &unix_disconnect;
1420 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1421 api->address_to_string = &unix_address_to_string;
1422 api->check_address = &unix_check_address;
1423 api->string_to_address = &unix_string_to_address;
1424 sockets_created = unix_transport_server_start (plugin);
1425 if (0 == sockets_created)
1426 LOG (GNUNET_ERROR_TYPE_WARNING,
1427 _("Failed to open UNIX listen socket\n"));
1428 plugin->session_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
1429 plugin->address_update_task = GNUNET_SCHEDULER_add_now (&address_notification, plugin);
1435 * Shutdown the plugin.
1437 * @param cls the plugin API returned from the initialization function
1438 * @return NULL (always)
1441 libgnunet_plugin_transport_unix_done (void *cls)
1443 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1444 struct Plugin *plugin = api->cls;
1445 struct UNIXMessageWrapper * msgw;
1452 plugin->env->notify_address (plugin->env->cls, GNUNET_NO,
1453 plugin->unix_socket_path,
1454 strlen (plugin->unix_socket_path) + 1,
1456 while (NULL != (msgw = plugin->msg_head))
1458 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
1459 if (msgw->cont != NULL)
1460 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR,
1462 GNUNET_free (msgw->msg);
1466 if (GNUNET_SCHEDULER_NO_TASK != plugin->select_task)
1468 GNUNET_SCHEDULER_cancel (plugin->select_task);
1469 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1471 if (GNUNET_SCHEDULER_NO_TASK != plugin->address_update_task)
1473 GNUNET_SCHEDULER_cancel (plugin->address_update_task);
1474 plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1476 if (NULL != plugin->unix_sock.desc)
1478 GNUNET_break (GNUNET_OK ==
1479 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc));
1480 plugin->unix_sock.desc = NULL;
1481 plugin->with_ws = GNUNET_NO;
1483 GNUNET_CONTAINER_multihashmap_iterate (plugin->session_map,
1484 &get_session_delete_it, plugin);
1485 GNUNET_CONTAINER_multihashmap_destroy (plugin->session_map);
1486 if (NULL != plugin->rs)
1487 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1488 if (NULL != plugin->ws)
1489 GNUNET_NETWORK_fdset_destroy (plugin->ws);
1490 GNUNET_free (plugin->unix_socket_path);
1491 GNUNET_free (plugin);
1496 /* end of plugin_transport_unix.c */