2 This file is part of GNUnet
3 (C) 2010 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
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_connection_lib.h"
33 #include "gnunet_container_lib.h"
34 #include "gnunet_os_lib.h"
35 #include "gnunet_peerinfo_service.h"
36 #include "gnunet_protocols.h"
37 #include "gnunet_resolver_service.h"
38 #include "gnunet_server_lib.h"
39 #include "gnunet_signatures.h"
40 #include "gnunet_statistics_service.h"
41 #include "gnunet_transport_service.h"
42 #include "gnunet_transport_plugin.h"
43 #include "transport.h"
48 * Transport cost to peer, always 1 for UNIX (direct connection)
50 #define UNIX_DIRECT_DISTANCE 1
52 #define DEFAULT_NAT_PORT 0
55 * How long until we give up on transmitting the welcome message?
57 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
60 * Starting port for listening and sending, eventually a config value
62 #define UNIX_NAT_DEFAULT_PORT 22086
64 GNUNET_NETWORK_STRUCT_BEGIN
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;
87 struct GNUNET_PeerIdentity target;
90 * Session timeout task
92 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
94 struct Plugin * plugin;
97 struct UNIXMessageWrapper
99 struct UNIXMessageWrapper *next;
100 struct UNIXMessageWrapper *prev;
102 struct UNIXMessage * msg;
105 struct GNUNET_TIME_Relative timeout;
106 unsigned int priority;
108 struct Session *session;
109 GNUNET_TRANSPORT_TransmitContinuation cont;
113 /* Forward definition */
124 * Stored in a linked list.
126 struct PeerSession *next;
129 * Pointer to the global plugin struct.
131 struct Plugin *plugin;
134 * To whom are we talking to (set to our identity
135 * if we are still waiting for the welcome message)
137 struct GNUNET_PeerIdentity target;
140 * Address of the other peer (either based on our 'connect'
141 * call or on our 'accept' call).
146 * Length of connect_addr.
151 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
153 int expecting_welcome;
156 * From which socket do we need to send to this peer?
158 struct GNUNET_NETWORK_Handle *sock;
161 * Queue of messages for this peer, in the case that
162 * we have to await a connection...
164 struct MessageQueue *messages;
169 * Information we keep for each of our listen sockets.
171 struct UNIX_Sock_Info
176 struct GNUNET_NETWORK_Handle *desc;
179 * The port we bound to
186 * Encapsulation of all of the state of the plugin.
193 struct GNUNET_TRANSPORT_PluginEnvironment *env;
198 struct GNUNET_CONTAINER_MultiHashMap *session_map;
201 * ID of task used to update our addresses when one expires.
203 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
208 GNUNET_SCHEDULER_TaskIdentifier select_task;
211 * Integer to append to unix domain socket.
218 struct GNUNET_NETWORK_FDSet *rs;
223 struct GNUNET_NETWORK_FDSet *ws;
228 * socket that we transmit all data with
230 struct UNIX_Sock_Info unix_sock;
233 * Path of our unix domain socket (/tmp/unix-plugin-PORT)
235 char *unix_socket_path;
237 struct UNIXMessageWrapper *msg_head;
238 struct UNIXMessageWrapper *msg_tail;
243 struct GNUNET_ATS_Information ats_network;
245 unsigned int bytes_in_queue;
246 unsigned int bytes_in_sent;
247 unsigned int bytes_in_recv;
248 unsigned int bytes_discarded;
252 * Start session timeout
255 start_session_timeout (struct Session *s);
258 * Increment session timeout due to activity
261 reschedule_session_timeout (struct Session *s);
267 stop_session_timeout (struct Session *s);
271 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
275 reschedule_select (struct Plugin * plugin)
278 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
280 GNUNET_SCHEDULER_cancel (plugin->select_task);
281 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
284 if (NULL != plugin->msg_head)
286 plugin->select_task =
287 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
288 GNUNET_TIME_UNIT_FOREVER_REL,
291 &unix_plugin_select, plugin);
292 plugin->with_ws = GNUNET_YES;
296 plugin->select_task =
297 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
298 GNUNET_TIME_UNIT_FOREVER_REL,
301 &unix_plugin_select, plugin);
302 plugin->with_ws = GNUNET_NO;
309 const struct sockaddr_un *addr;
312 int lookup_session_it (void *cls,
313 const GNUNET_HashCode * key,
316 struct LookupCtx *lctx = cls;
317 struct Session *t = value;
319 if (0 == strcmp (t->addr, lctx->addr->sun_path))
328 static struct Session *
329 lookup_session (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender, const struct sockaddr_un *addr)
331 struct LookupCtx lctx;
333 GNUNET_assert (NULL != plugin);
334 GNUNET_assert (NULL != sender);
335 GNUNET_assert (NULL != addr);
340 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map, &sender->hashPubKey, &lookup_session_it, &lctx);
346 * Functions with this signature are called whenever we need
347 * to close a session due to a disconnect or failure to
348 * establish a connection.
350 * @param s session to close down
353 disconnect_session (struct Session *s)
355 struct UNIXMessageWrapper *msgw;
356 struct UNIXMessageWrapper *next;
357 struct Plugin * plugin = s->plugin;
359 GNUNET_assert (plugin != NULL);
360 GNUNET_assert (s != NULL);
362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting session for peer `%s' `%s' \n", GNUNET_i2s (&s->target), s->addr);
363 stop_session_timeout (s);
364 plugin->env->session_end (plugin->env->cls, &s->target, s);
366 msgw = plugin->msg_head;
368 next = plugin->msg_head;
373 if (msgw->session != s)
375 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
376 if (NULL != msgw->cont)
377 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR);
378 GNUNET_free (msgw->msg);
380 removed = GNUNET_YES;
382 if ((GNUNET_YES == removed) && (NULL == plugin->msg_head))
383 reschedule_select (plugin);
385 GNUNET_assert (GNUNET_YES ==
386 GNUNET_CONTAINER_multihashmap_remove(plugin->session_map, &s->target.hashPubKey, s));
388 GNUNET_STATISTICS_set(plugin->env->stats,
389 "# UNIX sessions active",
390 GNUNET_CONTAINER_multihashmap_size(plugin->session_map),
397 get_session_delete_it (void *cls, const GNUNET_HashCode * key, void *value)
399 struct Session *s = value;
400 disconnect_session (s);
406 * Disconnect from a remote node. Clean up session if we have one for this peer
408 * @param cls closure for this call (should be handle to Plugin)
409 * @param target the peeridentity of the peer to disconnect
410 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
413 unix_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
415 struct Plugin *plugin = cls;
416 GNUNET_assert (plugin != NULL);
418 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map, &target->hashPubKey, &get_session_delete_it, plugin);
423 * Shutdown the server process (stop receiving inbound traffic). Maybe
426 * @param cls Handle to the plugin for this transport
428 * @return returns the number of sockets successfully closed,
429 * should equal the number of sockets successfully opened
432 unix_transport_server_stop (void *cls)
434 struct Plugin *plugin = cls;
436 struct UNIXMessageWrapper * msgw = plugin->msg_head;
438 while (NULL != (msgw = plugin->msg_head))
440 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
441 if (msgw->cont != NULL)
442 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR);
443 GNUNET_free (msgw->msg);
447 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
449 GNUNET_SCHEDULER_cancel (plugin->select_task);
450 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
453 if (NULL != plugin->unix_sock.desc)
455 GNUNET_break (GNUNET_OK ==
456 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc));
457 plugin->unix_sock.desc = NULL;
458 plugin->with_ws = GNUNET_NO;
465 * Actually send out the message, assume we've got the address and
466 * send_handle squared away!
469 * @param send_handle which handle to send message on
470 * @param target who should receive this message (ignored by UNIX)
471 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
472 * @param msgbuf_size the size of the msgbuf to send
473 * @param priority how important is the message (ignored by UNIX)
474 * @param timeout when should we time out (give up) if we can not transmit?
475 * @param addr the addr to send the message to, needs to be a sockaddr for us
476 * @param addrlen the len of addr
477 * @param cont continuation to call once the message has
478 * been transmitted (or if the transport is ready
479 * for the next transmission call; or if the
480 * peer disconnected...)
481 * @param cont_cls closure for cont
483 * @return the number of bytes written, -1 on errors
486 unix_real_send (void *cls,
487 struct GNUNET_NETWORK_Handle *send_handle,
488 const struct GNUNET_PeerIdentity *target, const char *msgbuf,
489 size_t msgbuf_size, unsigned int priority,
490 struct GNUNET_TIME_Relative timeout, const void *addr,
491 size_t addrlen, GNUNET_TRANSPORT_TransmitContinuation cont,
494 struct Plugin *plugin = cls;
498 struct sockaddr_un un;
502 GNUNET_assert (NULL != plugin);
504 if (send_handle == NULL)
506 /* We do not have a send handle */
509 cont (cont_cls, target, GNUNET_SYSERR);
512 if ((addr == NULL) || (addrlen == 0))
514 /* Can never send if we don't have an address */
517 cont (cont_cls, target, GNUNET_SYSERR);
521 /* Prepare address */
522 memset (&un, 0, sizeof (un));
523 un.sun_family = AF_UNIX;
524 slen = strlen (addr) + 1;
525 if (slen >= sizeof (un.sun_path))
526 slen = sizeof (un.sun_path) - 1;
527 GNUNET_assert (slen < sizeof (un.sun_path));
528 memcpy (un.sun_path, addr, slen);
529 un.sun_path[slen] = '\0';
530 slen = sizeof (struct sockaddr_un);
532 un.sun_path[0] = '\0';
534 #if HAVE_SOCKADDR_IN_SIN_LEN
535 un.sun_len = (u_char) slen;
537 sb = (struct sockaddr *) &un;
543 sent = GNUNET_NETWORK_socket_sendto (send_handle, msgbuf, msgbuf_size, sb, sbs);
545 if ((GNUNET_SYSERR == sent) && ((errno == EAGAIN) || (errno == ENOBUFS)))
547 /* We have to retry later: retry */
551 if ((GNUNET_SYSERR == sent) && (errno == EMSGSIZE))
554 socklen_t len = sizeof (size);
556 GNUNET_NETWORK_socket_getsockopt ((struct GNUNET_NETWORK_Handle *)
557 send_handle, SOL_SOCKET, SO_SNDBUF, &size,
560 if (size < msgbuf_size)
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
563 "Trying to increase socket buffer size from %i to %i for message size %i\n",
565 ((msgbuf_size / 1000) + 2) * 1000,
567 size = ((msgbuf_size / 1000) + 2) * 1000;
568 if (GNUNET_NETWORK_socket_setsockopt
569 ((struct GNUNET_NETWORK_Handle *) send_handle, SOL_SOCKET, SO_SNDBUF,
570 &size, sizeof (size)) == GNUNET_OK)
572 /* Increased buffer size, retry sending */
577 /* Could not increase buffer size: error, no retry */
578 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
584 /* Buffer is bigger than message: error, no retry
585 * This should never happen!*/
591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
592 "UNIX transmit %u-byte message to %s (%d: %s)\n",
593 (unsigned int) msgbuf_size, GNUNET_a2s (sb, sbs), (int) sent,
594 (sent < 0) ? STRERROR (errno) : "ok");
596 /* Calling continuation */
599 if ((sent == GNUNET_SYSERR) && (retry == GNUNET_NO))
600 cont (cont_cls, target, GNUNET_SYSERR);
602 cont (cont_cls, target, GNUNET_OK);
605 /* return number of bytes successfully sent */
610 /* That should never happen */
614 /* failed and retry: return 0 */
615 if ((GNUNET_SYSERR == sent) && (retry == GNUNET_YES))
617 /* failed and no retry: return -1 */
618 if ((GNUNET_SYSERR == sent) && (retry == GNUNET_NO))
633 get_session_it (void *cls, const GNUNET_HashCode * key, void *value)
635 struct gsi_ctx *gsi = cls;
636 struct Session *s = value;
638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing session %s %s\n", gsi->address, s->addr);
639 if ((gsi->addrlen == s->addrlen) &&
640 (0 == memcmp (gsi->address, s->addr, s->addrlen)))
649 * Creates a new outbound session the transport service will use to send data to the
652 * @param cls the plugin
653 * @param address the address
654 * @return the session or NULL of max connections exceeded
656 static struct Session *
657 unix_plugin_get_session (void *cls,
658 const struct GNUNET_HELLO_Address *address)
660 struct Session * s = NULL;
661 struct Plugin *plugin = cls;
665 GNUNET_assert (plugin != NULL);
666 GNUNET_assert (address != NULL);
668 /* Check if already existing */
669 gsi.address = (char *) address->address;
670 gsi.addrlen = address->address_length;
672 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map, &address->peer.hashPubKey, &get_session_it, &gsi);
675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found existing session\n");
679 /* Create a new session */
680 s = GNUNET_malloc (sizeof (struct Session) + address->address_length);
682 s->addrlen = address->address_length;
684 memcpy(s->addr, address->address, s->addrlen);
685 memcpy(&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
687 start_session_timeout (s);
689 GNUNET_CONTAINER_multihashmap_put (plugin->session_map,
690 &address->peer.hashPubKey, s,
691 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
693 GNUNET_STATISTICS_set(plugin->env->stats,
694 "# UNIX sessions active",
695 GNUNET_CONTAINER_multihashmap_size(plugin->session_map),
697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating new session\n");
702 * @param cls the plugin handle
703 * @param tc the scheduling context (for rescheduling this function again)
705 * We have been notified that our writeset has something to read. We don't
706 * know which socket needs to be read, so we have to check each one
707 * Then reschedule this function to be called again once more is available.
711 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
714 * Function that can be used by the transport service to transmit
715 * a message using the plugin. Note that in the case of a
716 * peer disconnecting, the continuation MUST be called
717 * prior to the disconnect notification itself. This function
718 * will be called with this peer's HELLO message to initiate
719 * a fresh connection to another peer.
722 * @param session which session must be used
723 * @param msgbuf the message to transmit
724 * @param msgbuf_size number of bytes in 'msgbuf'
725 * @param priority how important is the message (most plugins will
726 * ignore message priority and just FIFO)
727 * @param to how long to wait at most for the transmission (does not
728 * require plugins to discard the message after the timeout,
729 * just advisory for the desired delay; most plugins will ignore
731 * @param cont continuation to call once the message has
732 * been transmitted (or if the transport is ready
733 * for the next transmission call; or if the
734 * peer disconnected...); can be NULL
735 * @param cont_cls closure for cont
736 * @return number of bytes used (on the physical network, with overheads);
737 * -1 on hard errors (i.e. address invalid); 0 is a legal value
738 * and does NOT mean that the message was not transmitted (DV)
741 unix_plugin_send (void *cls,
742 struct Session *session,
743 const char *msgbuf, size_t msgbuf_size,
744 unsigned int priority,
745 struct GNUNET_TIME_Relative to,
746 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
748 struct Plugin *plugin = cls;
749 struct UNIXMessageWrapper *wrapper;
750 struct UNIXMessage *message;
753 GNUNET_assert (plugin != NULL);
754 GNUNET_assert (session != NULL);
756 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_contains_value(plugin->session_map,
757 &session->target.hashPubKey, session))
759 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid session for peer `%s' `%s'\n",
760 GNUNET_i2s (&session->target),
761 (char *) session->addr);
764 return GNUNET_SYSERR;
767 ssize = sizeof (struct UNIXMessage) + msgbuf_size;
768 message = GNUNET_malloc (sizeof (struct UNIXMessage) + msgbuf_size);
769 message->header.size = htons (ssize);
770 message->header.type = htons (0);
771 memcpy (&message->sender, plugin->env->my_identity,
772 sizeof (struct GNUNET_PeerIdentity));
773 memcpy (&message[1], msgbuf, msgbuf_size);
775 reschedule_session_timeout (session);
777 wrapper = GNUNET_malloc (sizeof (struct UNIXMessageWrapper));
778 wrapper->msg = message;
779 wrapper->msgsize = ssize;
780 wrapper->priority = priority;
781 wrapper->timeout = to;
782 wrapper->cont = cont;
783 wrapper->cont_cls = cont_cls;
784 wrapper->session = session;
786 GNUNET_CONTAINER_DLL_insert(plugin->msg_head, plugin->msg_tail, wrapper);
788 plugin->bytes_in_queue += ssize;
789 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes in send queue",
790 plugin->bytes_in_queue, GNUNET_NO);
792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent %d bytes to `%s'\n", ssize,
793 (char *) session->addr);
794 if (plugin->with_ws == GNUNET_NO)
796 reschedule_select (plugin);
803 * Demultiplexer for UNIX messages
805 * @param plugin the main plugin for this transport
806 * @param sender from which peer the message was received
807 * @param currhdr pointer to the header of the message
808 * @param un the address from which the message was received
809 * @param fromlen the length of the address
812 unix_demultiplexer (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender,
813 const struct GNUNET_MessageHeader *currhdr,
814 const struct sockaddr_un *un, size_t fromlen)
816 struct GNUNET_ATS_Information ats[2];
817 struct Session *s = NULL;
818 struct GNUNET_HELLO_Address * addr;
820 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
821 ats[0].value = htonl (UNIX_DIRECT_DISTANCE);
822 ats[1] = plugin->ats_network;
823 GNUNET_break (ntohl(plugin->ats_network.value) != GNUNET_ATS_NET_UNSPECIFIED);
825 GNUNET_assert (fromlen >= sizeof (struct sockaddr_un));
827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from %s\n",
830 plugin->bytes_in_recv += ntohs(currhdr->size);
831 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes received",
832 plugin->bytes_in_recv, GNUNET_NO);
834 addr = GNUNET_HELLO_address_allocate(sender, "unix", un->sun_path, strlen (un->sun_path) + 1);
835 s = lookup_session (plugin, sender, un);
837 s = unix_plugin_get_session (plugin, addr);
838 reschedule_session_timeout (s);
840 plugin->env->receive (plugin->env->cls, sender, currhdr,
841 (const struct GNUNET_ATS_Information *) &ats, 2,
842 s, un->sun_path, strlen (un->sun_path) + 1);
848 unix_plugin_select_read (struct Plugin * plugin)
850 char buf[65536] GNUNET_ALIGN;
851 struct UNIXMessage *msg;
852 struct GNUNET_PeerIdentity sender;
853 struct sockaddr_un un;
859 const struct GNUNET_MessageHeader *currhdr;
862 addrlen = sizeof (un);
863 memset (&un, 0, sizeof (un));
866 GNUNET_NETWORK_socket_recvfrom (plugin->unix_sock.desc, buf, sizeof (buf),
867 (struct sockaddr *) &un, &addrlen);
869 if ((GNUNET_SYSERR == ret) && ((errno == EAGAIN) || (errno == ENOBUFS)))
872 if (ret == GNUNET_SYSERR)
874 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom");
880 un.sun_path[0] = '/';
882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Read %d bytes from socket %s\n", ret,
886 GNUNET_assert (AF_UNIX == (un.sun_family));
888 msg = (struct UNIXMessage *) buf;
889 csize = ntohs (msg->header.size);
890 if ((csize < sizeof (struct UNIXMessage)) || (csize > ret))
895 msgbuf = (char *) &msg[1];
896 memcpy (&sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
898 tsize = csize - sizeof (struct UNIXMessage);
899 while (offset + sizeof (struct GNUNET_MessageHeader) <= tsize)
901 currhdr = (struct GNUNET_MessageHeader *) &msgbuf[offset];
902 csize = ntohs (currhdr->size);
903 if ((csize < sizeof (struct GNUNET_MessageHeader)) ||
904 (csize > tsize - offset))
910 unix_demultiplexer (plugin, &sender, currhdr, &un, sizeof (un));
917 unix_plugin_select_write (struct Plugin * plugin)
919 static int retry_counter;
921 struct UNIXMessageWrapper * msgw = plugin->msg_head;
923 sent = unix_real_send (plugin,
924 plugin->unix_sock.desc,
925 &msgw->session->target,
926 (const char *) msgw->msg,
931 msgw->session->addrlen,
932 msgw->cont, msgw->cont_cls);
936 /* failed and retry */
938 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX retry attempt",
939 retry_counter, GNUNET_NO);
943 if (retry_counter > 0 )
945 /* no retry: reset counter */
947 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX retry attempt",
948 retry_counter, GNUNET_NO);
953 /* failed and no retry */
954 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
956 GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
957 plugin->bytes_in_queue -= msgw->msgsize;
958 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes in send queue",
959 plugin->bytes_in_queue, GNUNET_NO);
960 plugin->bytes_discarded += msgw->msgsize;
961 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes discarded",
962 plugin->bytes_discarded, GNUNET_NO);
964 GNUNET_free (msgw->msg);
971 /* successfully sent bytes */
972 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
974 GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
975 plugin->bytes_in_queue -= msgw->msgsize;
976 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes in send queue",
977 plugin->bytes_in_queue, GNUNET_NO);
978 plugin->bytes_in_sent += msgw->msgsize;
979 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes sent",
980 plugin->bytes_in_sent, GNUNET_NO);
982 GNUNET_free (msgw->msg);
991 * We have been notified that our writeset has something to read. We don't
992 * know which socket needs to be read, so we have to check each one
993 * Then reschedule this function to be called again once more is available.
995 * @param cls the plugin handle
996 * @param tc the scheduling context (for rescheduling this function again)
999 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1001 struct Plugin *plugin = cls;
1003 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1004 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1007 if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
1009 /* Ready to send data */
1010 GNUNET_assert (GNUNET_NETWORK_fdset_isset
1011 (tc->write_ready, plugin->unix_sock.desc));
1012 if (plugin->msg_head != NULL)
1013 unix_plugin_select_write (plugin);
1016 if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
1018 /* Ready to receive data */
1019 GNUNET_assert (GNUNET_NETWORK_fdset_isset
1020 (tc->read_ready, plugin->unix_sock.desc));
1021 unix_plugin_select_read (plugin);
1024 reschedule_select (plugin);
1029 * Create a slew of UNIX sockets. If possible, use IPv6 and IPv4.
1031 * @param cls closure for server start, should be a struct Plugin *
1032 * @return number of sockets created or GNUNET_SYSERR on error
1035 unix_transport_server_start (void *cls)
1037 struct Plugin *plugin = cls;
1038 struct sockaddr *serverAddr;
1040 struct sockaddr_un un;
1043 memset (&un, 0, sizeof (un));
1044 un.sun_family = AF_UNIX;
1045 slen = strlen (plugin->unix_socket_path) + 1;
1046 if (slen >= sizeof (un.sun_path))
1047 slen = sizeof (un.sun_path) - 1;
1049 memcpy (un.sun_path, plugin->unix_socket_path, slen);
1050 un.sun_path[slen] = '\0';
1051 slen = sizeof (struct sockaddr_un);
1052 #if HAVE_SOCKADDR_IN_SIN_LEN
1053 un.sun_len = (u_char) slen;
1056 serverAddr = (struct sockaddr *) &un;
1059 un.sun_path[0] = '\0';
1061 plugin->ats_network = plugin->env->get_address_type (plugin->env->cls, serverAddr, addrlen);
1062 plugin->unix_sock.desc =
1063 GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_DGRAM, 0);
1064 if (NULL == plugin->unix_sock.desc)
1066 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
1067 return GNUNET_SYSERR;
1069 if (GNUNET_NETWORK_socket_bind (plugin->unix_sock.desc, serverAddr, addrlen)
1072 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
1073 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc);
1074 plugin->unix_sock.desc = NULL;
1075 return GNUNET_SYSERR;
1077 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "unix", "Bound to `%s'\n",
1079 plugin->rs = GNUNET_NETWORK_fdset_create ();
1080 plugin->ws = GNUNET_NETWORK_fdset_create ();
1081 GNUNET_NETWORK_fdset_zero (plugin->rs);
1082 GNUNET_NETWORK_fdset_zero (plugin->ws);
1083 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->unix_sock.desc);
1084 GNUNET_NETWORK_fdset_set (plugin->ws, plugin->unix_sock.desc);
1086 reschedule_select (plugin);
1093 * Function that will be called to check if a binary address for this
1094 * plugin is well-formed and corresponds to an address for THIS peer
1095 * (as per our configuration). Naturally, if absolutely necessary,
1096 * plugins can be a bit conservative in their answer, but in general
1097 * plugins should make sure that the address does not redirect
1098 * traffic to a 3rd party that might try to man-in-the-middle our
1101 * @param cls closure, should be our handle to the Plugin
1102 * @param addr pointer to the address
1103 * @param addrlen length of addr
1104 * @return GNUNET_OK if this is a plausible address for this peer
1105 * and transport, GNUNET_SYSERR if not
1109 unix_check_address (void *cls, const void *addr, size_t addrlen)
1111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1112 "Informing transport service about my address `%s'\n",
1119 * Convert the transports address to a nice, human-readable
1122 * @param cls closure
1123 * @param type name of the transport that generated the address
1124 * @param addr one of the addresses of the host, NULL for the last address
1125 * the specific address format depends on the transport
1126 * @param addrlen length of the address
1127 * @param numeric should (IP) addresses be displayed in numeric form?
1128 * @param timeout after how long should we give up?
1129 * @param asc function to call on each string
1130 * @param asc_cls closure for asc
1133 unix_plugin_address_pretty_printer (void *cls, const char *type,
1134 const void *addr, size_t addrlen,
1136 struct GNUNET_TIME_Relative timeout,
1137 GNUNET_TRANSPORT_AddressStringCallback asc,
1140 if ((NULL != addr) && (addrlen > 0))
1142 asc (asc_cls, (const char *) addr);
1147 asc (asc_cls, "<invalid UNIX address>");
1149 asc (asc_cls, NULL);
1154 * Function called to convert a string address to
1157 * @param cls closure ('struct Plugin*')
1158 * @param addr string address
1159 * @param addrlen length of the address (strlen(addr) + '\0')
1160 * @param buf location to store the buffer
1161 * If the function returns GNUNET_SYSERR, its contents are undefined.
1162 * @param added length of created address
1163 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1166 unix_string_to_address (void *cls, const char *addr, uint16_t addrlen,
1167 void **buf, size_t *added)
1169 if ((NULL == addr) || (0 == addrlen))
1172 return GNUNET_SYSERR;
1175 if ('\0' != addr[addrlen - 1])
1178 return GNUNET_SYSERR;
1181 if (strlen (addr) != addrlen - 1)
1184 return GNUNET_SYSERR;
1187 (*buf) = strdup (addr);
1188 (*added) = strlen (addr) + 1;
1194 * Function called for a quick conversion of the binary address to
1195 * a numeric address. Note that the caller must not free the
1196 * address and that the next call to this function is allowed
1197 * to override the address again.
1199 * @param cls closure
1200 * @param addr binary address
1201 * @param addrlen length of the address
1202 * @return string representing the same address
1205 unix_address_to_string (void *cls, const void *addr, size_t addrlen)
1207 if ((addr != NULL) && (addrlen > 0))
1208 return (const char *) addr;
1214 * Notify transport service about address
1216 * @param cls the plugin
1220 address_notification (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1222 struct Plugin *plugin = cls;
1224 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
1225 plugin->unix_socket_path,
1226 strlen (plugin->unix_socket_path) + 1);
1231 * Session was idle, so disconnect it
1234 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1236 GNUNET_assert (NULL != cls);
1237 struct Session *s = cls;
1239 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Session %p was idle for %llu, disconnecting\n",
1242 s, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1244 /* call session destroy function */
1245 disconnect_session(s);
1250 * Start session timeout
1253 start_session_timeout (struct Session *s)
1255 GNUNET_assert (NULL != s);
1256 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1258 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Timeout for session %p set to %llu\n",
1263 s, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1267 * Increment session timeout due to activity
1270 reschedule_session_timeout (struct Session *s)
1272 GNUNET_assert (NULL != s);
1273 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1275 GNUNET_SCHEDULER_cancel (s->timeout_task);
1276 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Timeout rescheduled for session %p set to %llu\n",
1281 s, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1288 stop_session_timeout (struct Session *s)
1290 GNUNET_assert (NULL != s);
1292 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1294 GNUNET_SCHEDULER_cancel (s->timeout_task);
1295 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Timeout rescheduled for session %p canceled\n",
1298 s, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Timeout for session %p was not active\n",
1309 * The exported method. Makes the core api available via a global and
1310 * returns the unix transport API.
1313 libgnunet_plugin_transport_unix_init (void *cls)
1315 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1316 unsigned long long port;
1317 struct GNUNET_TRANSPORT_PluginFunctions *api;
1318 struct Plugin *plugin;
1319 int sockets_created;
1321 if (NULL == env->receive)
1323 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1324 initialze the plugin or the API */
1325 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1327 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1328 api->address_to_string = &unix_address_to_string;
1329 api->string_to_address = &unix_string_to_address;
1332 GNUNET_assert( NULL != env->stats);
1335 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-unix", "PORT",
1337 port = UNIX_NAT_DEFAULT_PORT;
1338 plugin = GNUNET_malloc (sizeof (struct Plugin));
1339 plugin->port = port;
1341 GNUNET_asprintf (&plugin->unix_socket_path, "/tmp/unix-plugin-sock.%d",
1344 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1347 api->get_session = &unix_plugin_get_session;
1348 api->send = &unix_plugin_send;
1349 api->disconnect = &unix_disconnect;
1350 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1351 api->address_to_string = &unix_address_to_string;
1352 api->check_address = &unix_check_address;
1353 api->string_to_address = &unix_string_to_address;
1354 sockets_created = unix_transport_server_start (plugin);
1355 if (sockets_created == 0)
1356 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UNIX sockets\n"));
1358 plugin->session_map = GNUNET_CONTAINER_multihashmap_create(10);
1360 GNUNET_SCHEDULER_add_now (address_notification, plugin);
1365 libgnunet_plugin_transport_unix_done (void *cls)
1367 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1368 struct Plugin *plugin = api->cls;
1375 unix_transport_server_stop (plugin);
1377 GNUNET_CONTAINER_multihashmap_iterate (plugin->session_map, &get_session_delete_it, plugin);
1378 GNUNET_CONTAINER_multihashmap_destroy (plugin->session_map);
1380 if (NULL != plugin->rs)
1381 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1382 if (NULL != plugin->ws)
1383 GNUNET_NETWORK_fdset_destroy (plugin->ws);
1384 GNUNET_free (plugin->unix_socket_path);
1385 GNUNET_free (plugin);
1390 /* end of plugin_transport_unix.c */