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;
85 struct GNUNET_PeerIdentity target;
91 * Session timeout task
93 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
95 struct Plugin * plugin;
98 struct UNIXMessageWrapper
100 struct UNIXMessageWrapper *next;
101 struct UNIXMessageWrapper *prev;
103 struct UNIXMessage * msg;
106 struct GNUNET_TIME_Relative timeout;
107 unsigned int priority;
109 struct Session *session;
110 GNUNET_TRANSPORT_TransmitContinuation cont;
114 /* Forward definition */
125 * Stored in a linked list.
127 struct PeerSession *next;
130 * Pointer to the global plugin struct.
132 struct Plugin *plugin;
135 * To whom are we talking to (set to our identity
136 * if we are still waiting for the welcome message)
138 struct GNUNET_PeerIdentity target;
141 * Address of the other peer (either based on our 'connect'
142 * call or on our 'accept' call).
147 * Length of connect_addr.
152 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
154 int expecting_welcome;
157 * From which socket do we need to send to this peer?
159 struct GNUNET_NETWORK_Handle *sock;
162 * Queue of messages for this peer, in the case that
163 * we have to await a connection...
165 struct MessageQueue *messages;
170 * Information we keep for each of our listen sockets.
172 struct UNIX_Sock_Info
177 struct GNUNET_NETWORK_Handle *desc;
180 * The port we bound to
187 * Encapsulation of all of the state of the plugin.
194 struct GNUNET_TRANSPORT_PluginEnvironment *env;
199 struct GNUNET_CONTAINER_MultiHashMap *session_map;
202 * ID of task used to update our addresses when one expires.
204 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
209 GNUNET_SCHEDULER_TaskIdentifier select_task;
212 * Integer to append to unix domain socket.
219 struct GNUNET_NETWORK_FDSet *rs;
224 struct GNUNET_NETWORK_FDSet *ws;
229 * socket that we transmit all data with
231 struct UNIX_Sock_Info unix_sock;
234 * Path of our unix domain socket (/tmp/unix-plugin-PORT)
236 char *unix_socket_path;
238 struct UNIXMessageWrapper *msg_head;
239 struct UNIXMessageWrapper *msg_tail;
244 struct GNUNET_ATS_Information ats_network;
246 unsigned int bytes_in_queue;
247 unsigned int bytes_in_sent;
248 unsigned int bytes_in_recv;
249 unsigned int bytes_discarded;
253 * Start session timeout
256 start_session_timeout (struct Session *s);
259 * Increment session timeout due to activity
262 reschedule_session_timeout (struct Session *s);
268 stop_session_timeout (struct Session *s);
272 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
276 reschedule_select (struct Plugin * plugin)
279 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
281 GNUNET_SCHEDULER_cancel (plugin->select_task);
282 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
285 if (NULL != plugin->msg_head)
287 plugin->select_task =
288 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
289 GNUNET_TIME_UNIT_FOREVER_REL,
292 &unix_plugin_select, plugin);
293 plugin->with_ws = GNUNET_YES;
297 plugin->select_task =
298 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
299 GNUNET_TIME_UNIT_FOREVER_REL,
302 &unix_plugin_select, plugin);
303 plugin->with_ws = GNUNET_NO;
310 const struct sockaddr_un *addr;
313 int lookup_session_it (void *cls,
314 const struct GNUNET_HashCode * key,
317 struct LookupCtx *lctx = cls;
318 struct Session *t = value;
320 if (0 == strcmp (t->addr, lctx->addr->sun_path))
329 static struct Session *
330 lookup_session (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender, const struct sockaddr_un *addr)
332 struct LookupCtx lctx;
334 GNUNET_assert (NULL != plugin);
335 GNUNET_assert (NULL != sender);
336 GNUNET_assert (NULL != addr);
341 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map, &sender->hashPubKey, &lookup_session_it, &lctx);
347 * Functions with this signature are called whenever we need
348 * to close a session due to a disconnect or failure to
349 * establish a connection.
351 * @param s session to close down
354 disconnect_session (struct Session *s)
356 struct UNIXMessageWrapper *msgw;
357 struct UNIXMessageWrapper *next;
358 struct Plugin * plugin = s->plugin;
360 GNUNET_assert (plugin != NULL);
361 GNUNET_assert (s != NULL);
363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting session for peer `%s' `%s' \n", GNUNET_i2s (&s->target), s->addr);
364 stop_session_timeout (s);
365 plugin->env->session_end (plugin->env->cls, &s->target, s);
367 msgw = plugin->msg_head;
369 next = plugin->msg_head;
374 if (msgw->session != s)
376 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
377 if (NULL != msgw->cont)
378 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR);
379 GNUNET_free (msgw->msg);
381 removed = GNUNET_YES;
383 if ((GNUNET_YES == removed) && (NULL == plugin->msg_head))
384 reschedule_select (plugin);
386 GNUNET_assert (GNUNET_YES ==
387 GNUNET_CONTAINER_multihashmap_remove(plugin->session_map, &s->target.hashPubKey, s));
389 GNUNET_STATISTICS_set(plugin->env->stats,
390 "# UNIX sessions active",
391 GNUNET_CONTAINER_multihashmap_size(plugin->session_map),
398 get_session_delete_it (void *cls, const struct GNUNET_HashCode * key, void *value)
400 struct Session *s = value;
401 disconnect_session (s);
407 * Disconnect from a remote node. Clean up session if we have one for this peer
409 * @param cls closure for this call (should be handle to Plugin)
410 * @param target the peeridentity of the peer to disconnect
411 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
414 unix_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
416 struct Plugin *plugin = cls;
417 GNUNET_assert (plugin != NULL);
419 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map, &target->hashPubKey, &get_session_delete_it, plugin);
424 * Shutdown the server process (stop receiving inbound traffic). Maybe
427 * @param cls Handle to the plugin for this transport
429 * @return returns the number of sockets successfully closed,
430 * should equal the number of sockets successfully opened
433 unix_transport_server_stop (void *cls)
435 struct Plugin *plugin = cls;
437 struct UNIXMessageWrapper * msgw = plugin->msg_head;
439 while (NULL != (msgw = plugin->msg_head))
441 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
442 if (msgw->cont != NULL)
443 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR);
444 GNUNET_free (msgw->msg);
448 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
450 GNUNET_SCHEDULER_cancel (plugin->select_task);
451 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
454 if (NULL != plugin->unix_sock.desc)
456 GNUNET_break (GNUNET_OK ==
457 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc));
458 plugin->unix_sock.desc = NULL;
459 plugin->with_ws = GNUNET_NO;
466 * Actually send out the message, assume we've got the address and
467 * send_handle squared away!
470 * @param send_handle which handle to send message on
471 * @param target who should receive this message (ignored by UNIX)
472 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
473 * @param msgbuf_size the size of the msgbuf to send
474 * @param priority how important is the message (ignored by UNIX)
475 * @param timeout when should we time out (give up) if we can not transmit?
476 * @param addr the addr to send the message to, needs to be a sockaddr for us
477 * @param addrlen the len of addr
478 * @param cont continuation to call once the message has
479 * been transmitted (or if the transport is ready
480 * for the next transmission call; or if the
481 * peer disconnected...)
482 * @param cont_cls closure for cont
484 * @return the number of bytes written, -1 on errors
487 unix_real_send (void *cls,
488 struct GNUNET_NETWORK_Handle *send_handle,
489 const struct GNUNET_PeerIdentity *target, const char *msgbuf,
490 size_t msgbuf_size, unsigned int priority,
491 struct GNUNET_TIME_Relative timeout, const void *addr,
492 size_t addrlen, GNUNET_TRANSPORT_TransmitContinuation cont,
495 struct Plugin *plugin = cls;
499 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;
542 sent = GNUNET_NETWORK_socket_sendto (send_handle, msgbuf, msgbuf_size, sb, sbs);
544 if ((GNUNET_SYSERR == sent) && ((errno == EAGAIN) || (errno == ENOBUFS)))
546 /* We have to retry later: retry */
550 if ((GNUNET_SYSERR == sent) && (errno == EMSGSIZE))
553 socklen_t len = sizeof (size);
555 GNUNET_NETWORK_socket_getsockopt ((struct GNUNET_NETWORK_Handle *)
556 send_handle, SOL_SOCKET, SO_SNDBUF, &size,
559 if (size < msgbuf_size)
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
562 "Trying to increase socket buffer size from %i to %i for message size %i\n",
564 ((msgbuf_size / 1000) + 2) * 1000,
566 size = ((msgbuf_size / 1000) + 2) * 1000;
567 if (GNUNET_NETWORK_socket_setsockopt
568 ((struct GNUNET_NETWORK_Handle *) send_handle, SOL_SOCKET, SO_SNDBUF,
569 &size, sizeof (size)) == GNUNET_OK)
571 /* Increased buffer size, retry sending */
576 /* Could not increase buffer size: error, no retry */
577 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
583 /* Buffer is bigger than message: error, no retry
584 * This should never happen!*/
590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
591 "UNIX transmit %u-byte message to %s (%d: %s)\n",
592 (unsigned int) msgbuf_size, GNUNET_a2s (sb, sbs), (int) sent,
593 (sent < 0) ? STRERROR (errno) : "ok");
595 /* Calling continuation */
598 if (sent == GNUNET_SYSERR)
599 cont (cont_cls, target, GNUNET_SYSERR);
601 cont (cont_cls, target, GNUNET_OK);
604 /* return number of bytes successfully sent */
609 /* That should never happen */
613 /* failed and retry: return 0 */
614 if (GNUNET_SYSERR == sent)
629 get_session_it (void *cls, const struct GNUNET_HashCode * key, void *value)
631 struct gsi_ctx *gsi = cls;
632 struct Session *s = value;
634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing session %s %s\n", gsi->address, s->addr);
635 if ((gsi->addrlen == s->addrlen) &&
636 (0 == memcmp (gsi->address, s->addr, s->addrlen)))
645 * Creates a new outbound session the transport service will use to send data to the
648 * @param cls the plugin
649 * @param address the address
650 * @return the session or NULL of max connections exceeded
652 static struct Session *
653 unix_plugin_get_session (void *cls,
654 const struct GNUNET_HELLO_Address *address)
656 struct Session * s = NULL;
657 struct Plugin *plugin = cls;
661 GNUNET_assert (plugin != NULL);
662 GNUNET_assert (address != NULL);
664 /* Check if already existing */
665 gsi.address = (char *) address->address;
666 gsi.addrlen = address->address_length;
668 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map, &address->peer.hashPubKey, &get_session_it, &gsi);
671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found existing session\n");
675 /* Create a new session */
676 s = GNUNET_malloc (sizeof (struct Session) + address->address_length);
678 s->addrlen = address->address_length;
680 memcpy(s->addr, address->address, s->addrlen);
681 memcpy(&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
683 start_session_timeout (s);
685 GNUNET_CONTAINER_multihashmap_put (plugin->session_map,
686 &address->peer.hashPubKey, s,
687 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
689 GNUNET_STATISTICS_set(plugin->env->stats,
690 "# UNIX sessions active",
691 GNUNET_CONTAINER_multihashmap_size(plugin->session_map),
693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating new session\n");
698 * @param cls the plugin handle
699 * @param tc the scheduling context (for rescheduling this function again)
701 * We have been notified that our writeset has something to read. We don't
702 * know which socket needs to be read, so we have to check each one
703 * Then reschedule this function to be called again once more is available.
707 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
710 * Function that can be used by the transport service to transmit
711 * a message using the plugin. Note that in the case of a
712 * peer disconnecting, the continuation MUST be called
713 * prior to the disconnect notification itself. This function
714 * will be called with this peer's HELLO message to initiate
715 * a fresh connection to another peer.
718 * @param session which session must be used
719 * @param msgbuf the message to transmit
720 * @param msgbuf_size number of bytes in 'msgbuf'
721 * @param priority how important is the message (most plugins will
722 * ignore message priority and just FIFO)
723 * @param to how long to wait at most for the transmission (does not
724 * require plugins to discard the message after the timeout,
725 * just advisory for the desired delay; most plugins will ignore
727 * @param cont continuation to call once the message has
728 * been transmitted (or if the transport is ready
729 * for the next transmission call; or if the
730 * peer disconnected...); can be NULL
731 * @param cont_cls closure for cont
732 * @return number of bytes used (on the physical network, with overheads);
733 * -1 on hard errors (i.e. address invalid); 0 is a legal value
734 * and does NOT mean that the message was not transmitted (DV)
737 unix_plugin_send (void *cls,
738 struct Session *session,
739 const char *msgbuf, size_t msgbuf_size,
740 unsigned int priority,
741 struct GNUNET_TIME_Relative to,
742 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
744 struct Plugin *plugin = cls;
745 struct UNIXMessageWrapper *wrapper;
746 struct UNIXMessage *message;
749 GNUNET_assert (plugin != NULL);
750 GNUNET_assert (session != NULL);
752 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_contains_value(plugin->session_map,
753 &session->target.hashPubKey, session))
755 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid session for peer `%s' `%s'\n",
756 GNUNET_i2s (&session->target),
757 (char *) session->addr);
760 return GNUNET_SYSERR;
763 ssize = sizeof (struct UNIXMessage) + msgbuf_size;
764 message = GNUNET_malloc (sizeof (struct UNIXMessage) + msgbuf_size);
765 message->header.size = htons (ssize);
766 message->header.type = htons (0);
767 memcpy (&message->sender, plugin->env->my_identity,
768 sizeof (struct GNUNET_PeerIdentity));
769 memcpy (&message[1], msgbuf, msgbuf_size);
771 reschedule_session_timeout (session);
773 wrapper = GNUNET_malloc (sizeof (struct UNIXMessageWrapper));
774 wrapper->msg = message;
775 wrapper->msgsize = ssize;
776 wrapper->priority = priority;
777 wrapper->timeout = to;
778 wrapper->cont = cont;
779 wrapper->cont_cls = cont_cls;
780 wrapper->session = session;
782 GNUNET_CONTAINER_DLL_insert(plugin->msg_head, plugin->msg_tail, wrapper);
784 plugin->bytes_in_queue += ssize;
785 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes in send queue",
786 plugin->bytes_in_queue, GNUNET_NO);
788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent %d bytes to `%s'\n", ssize,
789 (char *) session->addr);
790 if (plugin->with_ws == GNUNET_NO)
792 reschedule_select (plugin);
799 * Demultiplexer for UNIX messages
801 * @param plugin the main plugin for this transport
802 * @param sender from which peer the message was received
803 * @param currhdr pointer to the header of the message
804 * @param un the address from which the message was received
805 * @param fromlen the length of the address
808 unix_demultiplexer (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender,
809 const struct GNUNET_MessageHeader *currhdr,
810 const struct sockaddr_un *un, size_t fromlen)
812 struct GNUNET_ATS_Information ats[2];
813 struct Session *s = NULL;
814 struct GNUNET_HELLO_Address * addr;
816 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
817 ats[0].value = htonl (UNIX_DIRECT_DISTANCE);
818 ats[1] = plugin->ats_network;
819 GNUNET_break (ntohl(plugin->ats_network.value) != GNUNET_ATS_NET_UNSPECIFIED);
821 GNUNET_assert (fromlen >= sizeof (struct sockaddr_un));
823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from %s\n",
826 plugin->bytes_in_recv += ntohs(currhdr->size);
827 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes received",
828 plugin->bytes_in_recv, GNUNET_NO);
830 addr = GNUNET_HELLO_address_allocate(sender, "unix", un->sun_path, strlen (un->sun_path) + 1);
831 s = lookup_session (plugin, sender, un);
833 s = unix_plugin_get_session (plugin, addr);
834 reschedule_session_timeout (s);
836 plugin->env->receive (plugin->env->cls, sender, currhdr,
837 (const struct GNUNET_ATS_Information *) &ats, 2,
838 s, un->sun_path, strlen (un->sun_path) + 1);
844 unix_plugin_select_read (struct Plugin * plugin)
846 char buf[65536] GNUNET_ALIGN;
847 struct UNIXMessage *msg;
848 struct GNUNET_PeerIdentity sender;
849 struct sockaddr_un un;
855 const struct GNUNET_MessageHeader *currhdr;
858 addrlen = sizeof (un);
859 memset (&un, 0, sizeof (un));
862 GNUNET_NETWORK_socket_recvfrom (plugin->unix_sock.desc, buf, sizeof (buf),
863 (struct sockaddr *) &un, &addrlen);
865 if ((GNUNET_SYSERR == ret) && ((errno == EAGAIN) || (errno == ENOBUFS)))
868 if (ret == GNUNET_SYSERR)
870 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom");
876 un.sun_path[0] = '/';
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Read %d bytes from socket %s\n", ret,
882 GNUNET_assert (AF_UNIX == (un.sun_family));
884 msg = (struct UNIXMessage *) buf;
885 csize = ntohs (msg->header.size);
886 if ((csize < sizeof (struct UNIXMessage)) || (csize > ret))
891 msgbuf = (char *) &msg[1];
892 memcpy (&sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
894 tsize = csize - sizeof (struct UNIXMessage);
895 while (offset + sizeof (struct GNUNET_MessageHeader) <= tsize)
897 currhdr = (struct GNUNET_MessageHeader *) &msgbuf[offset];
898 csize = ntohs (currhdr->size);
899 if ((csize < sizeof (struct GNUNET_MessageHeader)) ||
900 (csize > tsize - offset))
906 unix_demultiplexer (plugin, &sender, currhdr, &un, sizeof (un));
913 unix_plugin_select_write (struct Plugin * plugin)
915 static int retry_counter;
917 struct UNIXMessageWrapper * msgw = plugin->msg_head;
919 sent = unix_real_send (plugin,
920 plugin->unix_sock.desc,
921 &msgw->session->target,
922 (const char *) msgw->msg,
927 msgw->session->addrlen,
928 msgw->cont, msgw->cont_cls);
932 /* failed and retry */
934 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX retry attempt",
935 retry_counter, GNUNET_NO);
939 if (retry_counter > 0 )
941 /* no retry: reset counter */
943 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX retry attempt",
944 retry_counter, GNUNET_NO);
949 /* failed and no retry */
950 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
952 GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
953 plugin->bytes_in_queue -= msgw->msgsize;
954 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes in send queue",
955 plugin->bytes_in_queue, GNUNET_NO);
956 plugin->bytes_discarded += msgw->msgsize;
957 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes discarded",
958 plugin->bytes_discarded, GNUNET_NO);
960 GNUNET_free (msgw->msg);
967 /* successfully sent bytes */
968 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
970 GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
971 plugin->bytes_in_queue -= msgw->msgsize;
972 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes in send queue",
973 plugin->bytes_in_queue, GNUNET_NO);
974 plugin->bytes_in_sent += msgw->msgsize;
975 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes sent",
976 plugin->bytes_in_sent, GNUNET_NO);
978 GNUNET_free (msgw->msg);
987 * We have been notified that our writeset has something to read. We don't
988 * know which socket needs to be read, so we have to check each one
989 * Then reschedule this function to be called again once more is available.
991 * @param cls the plugin handle
992 * @param tc the scheduling context (for rescheduling this function again)
995 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
997 struct Plugin *plugin = cls;
999 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1000 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1003 if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
1005 /* Ready to send data */
1006 GNUNET_assert (GNUNET_NETWORK_fdset_isset
1007 (tc->write_ready, plugin->unix_sock.desc));
1008 if (plugin->msg_head != NULL)
1009 unix_plugin_select_write (plugin);
1012 if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
1014 /* Ready to receive data */
1015 GNUNET_assert (GNUNET_NETWORK_fdset_isset
1016 (tc->read_ready, plugin->unix_sock.desc));
1017 unix_plugin_select_read (plugin);
1020 reschedule_select (plugin);
1025 * Create a slew of UNIX sockets. If possible, use IPv6 and IPv4.
1027 * @param cls closure for server start, should be a struct Plugin *
1028 * @return number of sockets created or GNUNET_SYSERR on error
1031 unix_transport_server_start (void *cls)
1033 struct Plugin *plugin = cls;
1034 struct sockaddr *serverAddr;
1036 struct sockaddr_un un;
1039 memset (&un, 0, sizeof (un));
1040 un.sun_family = AF_UNIX;
1041 slen = strlen (plugin->unix_socket_path) + 1;
1042 if (slen >= sizeof (un.sun_path))
1043 slen = sizeof (un.sun_path) - 1;
1045 memcpy (un.sun_path, plugin->unix_socket_path, slen);
1046 un.sun_path[slen] = '\0';
1047 slen = sizeof (struct sockaddr_un);
1048 #if HAVE_SOCKADDR_IN_SIN_LEN
1049 un.sun_len = (u_char) slen;
1052 serverAddr = (struct sockaddr *) &un;
1055 un.sun_path[0] = '\0';
1057 plugin->ats_network = plugin->env->get_address_type (plugin->env->cls, serverAddr, addrlen);
1058 plugin->unix_sock.desc =
1059 GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_DGRAM, 0);
1060 if (NULL == plugin->unix_sock.desc)
1062 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
1063 return GNUNET_SYSERR;
1065 if (GNUNET_NETWORK_socket_bind (plugin->unix_sock.desc, serverAddr, addrlen)
1068 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
1069 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc);
1070 plugin->unix_sock.desc = NULL;
1071 return GNUNET_SYSERR;
1073 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "unix", "Bound to `%s'\n",
1075 plugin->rs = GNUNET_NETWORK_fdset_create ();
1076 plugin->ws = GNUNET_NETWORK_fdset_create ();
1077 GNUNET_NETWORK_fdset_zero (plugin->rs);
1078 GNUNET_NETWORK_fdset_zero (plugin->ws);
1079 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->unix_sock.desc);
1080 GNUNET_NETWORK_fdset_set (plugin->ws, plugin->unix_sock.desc);
1082 reschedule_select (plugin);
1089 * Function that will be called to check if a binary address for this
1090 * plugin is well-formed and corresponds to an address for THIS peer
1091 * (as per our configuration). Naturally, if absolutely necessary,
1092 * plugins can be a bit conservative in their answer, but in general
1093 * plugins should make sure that the address does not redirect
1094 * traffic to a 3rd party that might try to man-in-the-middle our
1097 * @param cls closure, should be our handle to the Plugin
1098 * @param addr pointer to the address
1099 * @param addrlen length of addr
1100 * @return GNUNET_OK if this is a plausible address for this peer
1101 * and transport, GNUNET_SYSERR if not
1105 unix_check_address (void *cls, const void *addr, size_t addrlen)
1107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1108 "Informing transport service about my address `%s'\n",
1115 * Convert the transports address to a nice, human-readable
1118 * @param cls closure
1119 * @param type name of the transport that generated the address
1120 * @param addr one of the addresses of the host, NULL for the last address
1121 * the specific address format depends on the transport
1122 * @param addrlen length of the address
1123 * @param numeric should (IP) addresses be displayed in numeric form?
1124 * @param timeout after how long should we give up?
1125 * @param asc function to call on each string
1126 * @param asc_cls closure for asc
1129 unix_plugin_address_pretty_printer (void *cls, const char *type,
1130 const void *addr, size_t addrlen,
1132 struct GNUNET_TIME_Relative timeout,
1133 GNUNET_TRANSPORT_AddressStringCallback asc,
1136 if ((NULL != addr) && (addrlen > 0))
1138 asc (asc_cls, (const char *) addr);
1143 asc (asc_cls, "<invalid UNIX address>");
1145 asc (asc_cls, NULL);
1150 * Function called to convert a string address to
1153 * @param cls closure ('struct Plugin*')
1154 * @param addr string address
1155 * @param addrlen length of the address (strlen(addr) + '\0')
1156 * @param buf location to store the buffer
1157 * If the function returns GNUNET_SYSERR, its contents are undefined.
1158 * @param added length of created address
1159 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1162 unix_string_to_address (void *cls, const char *addr, uint16_t addrlen,
1163 void **buf, size_t *added)
1165 if ((NULL == addr) || (0 == addrlen))
1168 return GNUNET_SYSERR;
1171 if ('\0' != addr[addrlen - 1])
1174 return GNUNET_SYSERR;
1177 if (strlen (addr) != addrlen - 1)
1180 return GNUNET_SYSERR;
1183 (*buf) = strdup (addr);
1184 (*added) = strlen (addr) + 1;
1190 * Function called for a quick conversion of the binary address to
1191 * a numeric address. Note that the caller must not free the
1192 * address and that the next call to this function is allowed
1193 * to override the address again.
1195 * @param cls closure
1196 * @param addr binary address
1197 * @param addrlen length of the address
1198 * @return string representing the same address
1201 unix_address_to_string (void *cls, const void *addr, size_t addrlen)
1203 if ((addr != NULL) && (addrlen > 0))
1204 return (const char *) addr;
1210 * Notify transport service about address
1212 * @param cls the plugin
1216 address_notification (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1218 struct Plugin *plugin = cls;
1220 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
1221 plugin->unix_socket_path,
1222 strlen (plugin->unix_socket_path) + 1,
1228 * Session was idle, so disconnect it
1231 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1233 GNUNET_assert (NULL != cls);
1234 struct Session *s = cls;
1236 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1238 "Session %p was idle for %llu ms, disconnecting\n",
1239 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1240 /* call session destroy function */
1241 disconnect_session(s);
1246 * Start session timeout
1249 start_session_timeout (struct Session *s)
1251 GNUNET_assert (NULL != s);
1252 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1253 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1257 "Timeout for session %p set to %llu ms\n",
1258 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1263 * Increment session timeout due to activity
1266 reschedule_session_timeout (struct Session *s)
1268 GNUNET_assert (NULL != s);
1269 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1271 GNUNET_SCHEDULER_cancel (s->timeout_task);
1272 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1276 "Timeout rescheduled for session %p set to %llu ms\n",
1277 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1285 stop_session_timeout (struct Session *s)
1287 GNUNET_assert (NULL != s);
1289 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1291 GNUNET_SCHEDULER_cancel (s->timeout_task);
1292 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1294 "Timeout stopped for session %p canceled\n",
1295 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1300 * The exported method. Makes the core api available via a global and
1301 * returns the unix transport API.
1304 libgnunet_plugin_transport_unix_init (void *cls)
1306 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1307 unsigned long long port;
1308 struct GNUNET_TRANSPORT_PluginFunctions *api;
1309 struct Plugin *plugin;
1310 int sockets_created;
1312 if (NULL == env->receive)
1314 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1315 initialze the plugin or the API */
1316 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1318 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1319 api->address_to_string = &unix_address_to_string;
1320 api->string_to_address = &unix_string_to_address;
1323 GNUNET_assert( NULL != env->stats);
1326 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-unix", "PORT",
1328 port = UNIX_NAT_DEFAULT_PORT;
1329 plugin = GNUNET_malloc (sizeof (struct Plugin));
1330 plugin->port = port;
1332 GNUNET_asprintf (&plugin->unix_socket_path, "/tmp/unix-plugin-sock.%d",
1335 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1338 api->get_session = &unix_plugin_get_session;
1339 api->send = &unix_plugin_send;
1340 api->disconnect = &unix_disconnect;
1341 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1342 api->address_to_string = &unix_address_to_string;
1343 api->check_address = &unix_check_address;
1344 api->string_to_address = &unix_string_to_address;
1345 sockets_created = unix_transport_server_start (plugin);
1346 if (sockets_created == 0)
1347 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UNIX sockets\n"));
1349 plugin->session_map = GNUNET_CONTAINER_multihashmap_create(10);
1351 GNUNET_SCHEDULER_add_now (address_notification, plugin);
1356 libgnunet_plugin_transport_unix_done (void *cls)
1358 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1359 struct Plugin *plugin = api->cls;
1366 unix_transport_server_stop (plugin);
1368 GNUNET_CONTAINER_multihashmap_iterate (plugin->session_map, &get_session_delete_it, plugin);
1369 GNUNET_CONTAINER_multihashmap_destroy (plugin->session_map);
1371 if (NULL != plugin->rs)
1372 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1373 if (NULL != plugin->ws)
1374 GNUNET_NETWORK_fdset_destroy (plugin->ws);
1375 GNUNET_free (plugin->unix_socket_path);
1376 GNUNET_free (plugin);
1381 /* end of plugin_transport_unix.c */