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 struct UNIXMessageWrapper
92 struct UNIXMessageWrapper *next;
93 struct UNIXMessageWrapper *prev;
95 struct UNIXMessage * msg;
98 struct GNUNET_TIME_Relative timeout;
99 unsigned int priority;
101 struct Session *session;
102 GNUNET_TRANSPORT_TransmitContinuation cont;
106 /* Forward definition */
117 * Stored in a linked list.
119 struct PeerSession *next;
122 * Pointer to the global plugin struct.
124 struct Plugin *plugin;
127 * To whom are we talking to (set to our identity
128 * if we are still waiting for the welcome message)
130 struct GNUNET_PeerIdentity target;
133 * Address of the other peer (either based on our 'connect'
134 * call or on our 'accept' call).
139 * Length of connect_addr.
144 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
146 int expecting_welcome;
149 * From which socket do we need to send to this peer?
151 struct GNUNET_NETWORK_Handle *sock;
154 * Queue of messages for this peer, in the case that
155 * we have to await a connection...
157 struct MessageQueue *messages;
162 * Information we keep for each of our listen sockets.
164 struct UNIX_Sock_Info
169 struct GNUNET_NETWORK_Handle *desc;
172 * The port we bound to
179 * Encapsulation of all of the state of the plugin.
186 struct GNUNET_TRANSPORT_PluginEnvironment *env;
191 struct GNUNET_CONTAINER_MultiHashMap *session_map;
194 * ID of task used to update our addresses when one expires.
196 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
201 GNUNET_SCHEDULER_TaskIdentifier select_task;
204 * Integer to append to unix domain socket.
211 struct GNUNET_NETWORK_FDSet *rs;
216 struct GNUNET_NETWORK_FDSet *ws;
221 * socket that we transmit all data with
223 struct UNIX_Sock_Info unix_sock;
226 * Path of our unix domain socket (/tmp/unix-plugin-PORT)
228 char *unix_socket_path;
230 struct UNIXMessageWrapper *msg_head;
231 struct UNIXMessageWrapper *msg_tail;
236 struct GNUNET_ATS_Information ats_network;
238 unsigned int bytes_in_queue;
239 unsigned int bytes_in_sent;
240 unsigned int bytes_in_recv;
241 unsigned int bytes_discarded;
246 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
250 reschedule_select (struct Plugin * plugin)
253 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
255 GNUNET_SCHEDULER_cancel (plugin->select_task);
256 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
259 if (NULL != plugin->msg_head)
261 plugin->select_task =
262 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
263 GNUNET_TIME_UNIT_FOREVER_REL,
266 &unix_plugin_select, plugin);
267 plugin->with_ws = GNUNET_YES;
271 plugin->select_task =
272 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
273 GNUNET_TIME_UNIT_FOREVER_REL,
276 &unix_plugin_select, plugin);
277 plugin->with_ws = GNUNET_NO;
283 get_session_delete_it (void *cls, const GNUNET_HashCode * key, void *value)
285 struct Session *s = value;
286 struct UNIXMessageWrapper *msgw;
287 struct UNIXMessageWrapper *next;
288 struct Plugin *plugin = cls;
290 GNUNET_assert (plugin != NULL);
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting session for peer `%s' `%s' \n", GNUNET_i2s (&s->target), s->addr);
293 plugin->env->session_end (plugin->env->cls, &s->target, s);
295 msgw = plugin->msg_head;
297 next = plugin->msg_head;
302 if (msgw->session != s)
304 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
305 if (NULL != msgw->cont)
306 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR);
307 GNUNET_free (msgw->msg);
309 removed = GNUNET_YES;
311 if ((GNUNET_YES == removed) && (NULL == plugin->msg_head))
312 reschedule_select (plugin);
314 GNUNET_assert (GNUNET_YES ==
315 GNUNET_CONTAINER_multihashmap_remove(plugin->session_map, &s->target.hashPubKey, s));
317 GNUNET_STATISTICS_set(plugin->env->stats,
318 "# UNIX sessions active",
319 GNUNET_CONTAINER_multihashmap_size(plugin->session_map),
328 * Disconnect from a remote node. Clean up session if we have one for this peer
330 * @param cls closure for this call (should be handle to Plugin)
331 * @param target the peeridentity of the peer to disconnect
332 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
335 unix_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
337 struct Plugin *plugin = cls;
338 GNUNET_assert (plugin != NULL);
340 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map, &target->hashPubKey, &get_session_delete_it, plugin);
345 * Shutdown the server process (stop receiving inbound traffic). Maybe
348 * @param cls Handle to the plugin for this transport
350 * @return returns the number of sockets successfully closed,
351 * should equal the number of sockets successfully opened
354 unix_transport_server_stop (void *cls)
356 struct Plugin *plugin = cls;
358 struct UNIXMessageWrapper * msgw = plugin->msg_head;
360 while (NULL != (msgw = plugin->msg_head))
362 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
363 if (msgw->cont != NULL)
364 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR);
365 GNUNET_free (msgw->msg);
369 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
371 GNUNET_SCHEDULER_cancel (plugin->select_task);
372 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
375 if (NULL != plugin->unix_sock.desc)
377 GNUNET_break (GNUNET_OK ==
378 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc));
379 plugin->unix_sock.desc = NULL;
380 plugin->with_ws = GNUNET_NO;
387 * Actually send out the message, assume we've got the address and
388 * send_handle squared away!
391 * @param send_handle which handle to send message on
392 * @param target who should receive this message (ignored by UNIX)
393 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
394 * @param msgbuf_size the size of the msgbuf to send
395 * @param priority how important is the message (ignored by UNIX)
396 * @param timeout when should we time out (give up) if we can not transmit?
397 * @param addr the addr to send the message to, needs to be a sockaddr for us
398 * @param addrlen the len of addr
399 * @param cont continuation to call once the message has
400 * been transmitted (or if the transport is ready
401 * for the next transmission call; or if the
402 * peer disconnected...)
403 * @param cont_cls closure for cont
405 * @return the number of bytes written, -1 on errors
408 unix_real_send (void *cls,
409 struct GNUNET_NETWORK_Handle *send_handle,
410 const struct GNUNET_PeerIdentity *target, const char *msgbuf,
411 size_t msgbuf_size, unsigned int priority,
412 struct GNUNET_TIME_Relative timeout, const void *addr,
413 size_t addrlen, GNUNET_TRANSPORT_TransmitContinuation cont,
416 struct Plugin *plugin = cls;
420 struct sockaddr_un un;
424 GNUNET_assert (NULL != plugin);
426 if (send_handle == NULL)
428 /* We do not have a send handle */
431 cont (cont_cls, target, GNUNET_SYSERR);
434 if ((addr == NULL) || (addrlen == 0))
436 /* Can never send if we don't have an address */
439 cont (cont_cls, target, GNUNET_SYSERR);
443 /* Prepare address */
444 memset (&un, 0, sizeof (un));
445 un.sun_family = AF_UNIX;
446 slen = strlen (addr) + 1;
447 if (slen >= sizeof (un.sun_path))
448 slen = sizeof (un.sun_path) - 1;
449 GNUNET_assert (slen < sizeof (un.sun_path));
450 memcpy (un.sun_path, addr, slen);
451 un.sun_path[slen] = '\0';
452 slen = sizeof (struct sockaddr_un);
454 un.sun_path[0] = '\0';
456 #if HAVE_SOCKADDR_IN_SIN_LEN
457 un.sun_len = (u_char) slen;
459 sb = (struct sockaddr *) &un;
465 sent = GNUNET_NETWORK_socket_sendto (send_handle, msgbuf, msgbuf_size, sb, sbs);
467 if ((GNUNET_SYSERR == sent) && ((errno == EAGAIN) || (errno == ENOBUFS)))
469 /* We have to retry later: retry */
473 if ((GNUNET_SYSERR == sent) && (errno == EMSGSIZE))
476 socklen_t len = sizeof (size);
478 GNUNET_NETWORK_socket_getsockopt ((struct GNUNET_NETWORK_Handle *)
479 send_handle, SOL_SOCKET, SO_SNDBUF, &size,
482 if (size < msgbuf_size)
484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
485 "Trying to increase socket buffer size from %i to %i for message size %i\n",
487 ((msgbuf_size / 1000) + 2) * 1000,
489 size = ((msgbuf_size / 1000) + 2) * 1000;
490 if (GNUNET_NETWORK_socket_setsockopt
491 ((struct GNUNET_NETWORK_Handle *) send_handle, SOL_SOCKET, SO_SNDBUF,
492 &size, sizeof (size)) == GNUNET_OK)
494 /* Increased buffer size, retry sending */
499 /* Could not increase buffer size: error, no retry */
500 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
506 /* Buffer is bigger than message: error, no retry
507 * This should never happen!*/
513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
514 "UNIX transmit %u-byte message to %s (%d: %s)\n",
515 (unsigned int) msgbuf_size, GNUNET_a2s (sb, sbs), (int) sent,
516 (sent < 0) ? STRERROR (errno) : "ok");
518 /* Calling continuation */
521 if ((sent == GNUNET_SYSERR) && (retry == GNUNET_NO))
522 cont (cont_cls, target, GNUNET_SYSERR);
524 cont (cont_cls, target, GNUNET_OK);
527 /* return number of bytes successfully sent */
532 /* That should never happen */
536 /* failed and retry: return 0 */
537 if ((GNUNET_SYSERR == sent) && (retry == GNUNET_YES))
539 /* failed and no retry: return -1 */
540 if ((GNUNET_SYSERR == sent) && (retry == GNUNET_NO))
555 get_session_it (void *cls, const GNUNET_HashCode * key, void *value)
557 struct gsi_ctx *gsi = cls;
558 struct Session *s = value;
560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing session %s %s\n", gsi->address, s->addr);
561 if ((gsi->addrlen == s->addrlen) &&
562 (0 == memcmp (gsi->address, s->addr, s->addrlen)))
571 * Creates a new outbound session the transport service will use to send data to the
574 * @param cls the plugin
575 * @param address the address
576 * @return the session or NULL of max connections exceeded
578 static struct Session *
579 unix_plugin_get_session (void *cls,
580 const struct GNUNET_HELLO_Address *address)
582 struct Session * s = NULL;
583 struct Plugin *plugin = cls;
587 GNUNET_assert (plugin != NULL);
588 GNUNET_assert (address != NULL);
590 /* Check if already existing */
591 gsi.address = (char *) address->address;
592 gsi.addrlen = address->address_length;
594 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map, &address->peer.hashPubKey, &get_session_it, &gsi);
597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found existing session\n");
601 /* Create a new session */
603 s = GNUNET_malloc (sizeof (struct Session) + address->address_length);
605 s->addrlen = address->address_length;
606 memcpy(s->addr, address->address, s->addrlen);
607 memcpy(&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
609 GNUNET_CONTAINER_multihashmap_put (plugin->session_map,
610 &address->peer.hashPubKey, s,
611 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
613 GNUNET_STATISTICS_set(plugin->env->stats,
614 "# UNIX sessions active",
615 GNUNET_CONTAINER_multihashmap_size(plugin->session_map),
617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating new session\n");
622 * @param cls the plugin handle
623 * @param tc the scheduling context (for rescheduling this function again)
625 * We have been notified that our writeset has something to read. We don't
626 * know which socket needs to be read, so we have to check each one
627 * Then reschedule this function to be called again once more is available.
631 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
634 * Function that can be used by the transport service to transmit
635 * a message using the plugin. Note that in the case of a
636 * peer disconnecting, the continuation MUST be called
637 * prior to the disconnect notification itself. This function
638 * will be called with this peer's HELLO message to initiate
639 * a fresh connection to another peer.
642 * @param session which session must be used
643 * @param msgbuf the message to transmit
644 * @param msgbuf_size number of bytes in 'msgbuf'
645 * @param priority how important is the message (most plugins will
646 * ignore message priority and just FIFO)
647 * @param to how long to wait at most for the transmission (does not
648 * require plugins to discard the message after the timeout,
649 * just advisory for the desired delay; most plugins will ignore
651 * @param cont continuation to call once the message has
652 * been transmitted (or if the transport is ready
653 * for the next transmission call; or if the
654 * peer disconnected...); can be NULL
655 * @param cont_cls closure for cont
656 * @return number of bytes used (on the physical network, with overheads);
657 * -1 on hard errors (i.e. address invalid); 0 is a legal value
658 * and does NOT mean that the message was not transmitted (DV)
661 unix_plugin_send (void *cls,
662 struct Session *session,
663 const char *msgbuf, size_t msgbuf_size,
664 unsigned int priority,
665 struct GNUNET_TIME_Relative to,
666 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
668 struct Plugin *plugin = cls;
669 struct UNIXMessageWrapper *wrapper;
670 struct UNIXMessage *message;
673 GNUNET_assert (plugin != NULL);
674 GNUNET_assert (session != NULL);
676 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_contains_value(plugin->session_map,
677 &session->target.hashPubKey, session))
680 return GNUNET_SYSERR;
683 ssize = sizeof (struct UNIXMessage) + msgbuf_size;
684 message = GNUNET_malloc (sizeof (struct UNIXMessage) + msgbuf_size);
685 message->header.size = htons (ssize);
686 message->header.type = htons (0);
687 memcpy (&message->sender, plugin->env->my_identity,
688 sizeof (struct GNUNET_PeerIdentity));
689 memcpy (&message[1], msgbuf, msgbuf_size);
692 wrapper = GNUNET_malloc (sizeof (struct UNIXMessageWrapper));
693 wrapper->msg = message;
694 wrapper->msgsize = ssize;
695 wrapper->priority = priority;
696 wrapper->timeout = to;
697 wrapper->cont = cont;
698 wrapper->cont_cls = cont_cls;
699 wrapper->session = session;
701 GNUNET_CONTAINER_DLL_insert(plugin->msg_head, plugin->msg_tail, wrapper);
703 plugin->bytes_in_queue += ssize;
704 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes in send queue",
705 plugin->bytes_in_queue, GNUNET_NO);
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent %d bytes to `%s'\n", ssize,
708 (char *) session->addr);
709 if (plugin->with_ws == GNUNET_NO)
711 reschedule_select (plugin);
718 * Demultiplexer for UNIX messages
720 * @param plugin the main plugin for this transport
721 * @param sender from which peer the message was received
722 * @param currhdr pointer to the header of the message
723 * @param un the address from which the message was received
724 * @param fromlen the length of the address
727 unix_demultiplexer (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender,
728 const struct GNUNET_MessageHeader *currhdr,
729 const struct sockaddr_un *un, size_t fromlen)
731 struct GNUNET_ATS_Information ats[2];
733 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
734 ats[0].value = htonl (UNIX_DIRECT_DISTANCE);
735 ats[1] = plugin->ats_network;
736 GNUNET_break (ntohl(plugin->ats_network.value) != GNUNET_ATS_NET_UNSPECIFIED);
738 GNUNET_assert (fromlen >= sizeof (struct sockaddr_un));
740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from %s\n",
743 plugin->bytes_in_recv += ntohs(currhdr->size);
744 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes received",
745 plugin->bytes_in_recv, GNUNET_NO);
747 plugin->env->receive (plugin->env->cls, sender, currhdr,
748 (const struct GNUNET_ATS_Information *) &ats, 2,
749 NULL, un->sun_path, strlen (un->sun_path) + 1);
754 unix_plugin_select_read (struct Plugin * plugin)
756 char buf[65536] GNUNET_ALIGN;
757 struct UNIXMessage *msg;
758 struct GNUNET_PeerIdentity sender;
759 struct sockaddr_un un;
765 const struct GNUNET_MessageHeader *currhdr;
768 addrlen = sizeof (un);
769 memset (&un, 0, sizeof (un));
772 GNUNET_NETWORK_socket_recvfrom (plugin->unix_sock.desc, buf, sizeof (buf),
773 (struct sockaddr *) &un, &addrlen);
775 if ((GNUNET_SYSERR == ret) && ((errno == EAGAIN) || (errno == ENOBUFS)))
778 if (ret == GNUNET_SYSERR)
780 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom");
786 un.sun_path[0] = '/';
788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Read %d bytes from socket %s\n", ret,
792 GNUNET_assert (AF_UNIX == (un.sun_family));
794 msg = (struct UNIXMessage *) buf;
795 csize = ntohs (msg->header.size);
796 if ((csize < sizeof (struct UNIXMessage)) || (csize > ret))
801 msgbuf = (char *) &msg[1];
802 memcpy (&sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
804 tsize = csize - sizeof (struct UNIXMessage);
805 while (offset + sizeof (struct GNUNET_MessageHeader) <= tsize)
807 currhdr = (struct GNUNET_MessageHeader *) &msgbuf[offset];
808 csize = ntohs (currhdr->size);
809 if ((csize < sizeof (struct GNUNET_MessageHeader)) ||
810 (csize > tsize - offset))
816 unix_demultiplexer (plugin, &sender, currhdr, &un, sizeof (un));
823 unix_plugin_select_write (struct Plugin * plugin)
825 static int retry_counter;
827 struct UNIXMessageWrapper * msgw = plugin->msg_head;
829 sent = unix_real_send (plugin,
830 plugin->unix_sock.desc,
831 &msgw->session->target,
832 (const char *) msgw->msg,
837 msgw->session->addrlen,
838 msgw->cont, msgw->cont_cls);
842 /* failed and retry */
844 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX retry attempt",
845 retry_counter, GNUNET_NO);
849 if (retry_counter > 0 )
851 /* no retry: reset counter */
853 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX retry attempt",
854 retry_counter, GNUNET_NO);
859 /* failed and no retry */
860 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
862 GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
863 plugin->bytes_in_queue -= msgw->msgsize;
864 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes in send queue",
865 plugin->bytes_in_queue, GNUNET_NO);
866 plugin->bytes_discarded += msgw->msgsize;
867 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes discarded",
868 plugin->bytes_discarded, GNUNET_NO);
870 GNUNET_free (msgw->msg);
877 /* successfully sent bytes */
878 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
880 GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
881 plugin->bytes_in_queue -= msgw->msgsize;
882 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes in send queue",
883 plugin->bytes_in_queue, GNUNET_NO);
884 plugin->bytes_in_sent += msgw->msgsize;
885 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes sent",
886 plugin->bytes_in_sent, GNUNET_NO);
888 GNUNET_free (msgw->msg);
897 * We have been notified that our writeset has something to read. We don't
898 * know which socket needs to be read, so we have to check each one
899 * Then reschedule this function to be called again once more is available.
901 * @param cls the plugin handle
902 * @param tc the scheduling context (for rescheduling this function again)
905 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
907 struct Plugin *plugin = cls;
909 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
910 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
913 if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
915 /* Ready to send data */
916 GNUNET_assert (GNUNET_NETWORK_fdset_isset
917 (tc->write_ready, plugin->unix_sock.desc));
918 if (plugin->msg_head != NULL)
919 unix_plugin_select_write (plugin);
922 if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
924 /* Ready to receive data */
925 GNUNET_assert (GNUNET_NETWORK_fdset_isset
926 (tc->read_ready, plugin->unix_sock.desc));
927 unix_plugin_select_read (plugin);
930 reschedule_select (plugin);
935 * Create a slew of UNIX sockets. If possible, use IPv6 and IPv4.
937 * @param cls closure for server start, should be a struct Plugin *
938 * @return number of sockets created or GNUNET_SYSERR on error
941 unix_transport_server_start (void *cls)
943 struct Plugin *plugin = cls;
944 struct sockaddr *serverAddr;
946 struct sockaddr_un un;
949 memset (&un, 0, sizeof (un));
950 un.sun_family = AF_UNIX;
951 slen = strlen (plugin->unix_socket_path) + 1;
952 if (slen >= sizeof (un.sun_path))
953 slen = sizeof (un.sun_path) - 1;
955 memcpy (un.sun_path, plugin->unix_socket_path, slen);
956 un.sun_path[slen] = '\0';
957 slen = sizeof (struct sockaddr_un);
958 #if HAVE_SOCKADDR_IN_SIN_LEN
959 un.sun_len = (u_char) slen;
962 serverAddr = (struct sockaddr *) &un;
965 un.sun_path[0] = '\0';
967 plugin->ats_network = plugin->env->get_address_type (plugin->env->cls, serverAddr, addrlen);
968 plugin->unix_sock.desc =
969 GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_DGRAM, 0);
970 if (NULL == plugin->unix_sock.desc)
972 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
973 return GNUNET_SYSERR;
975 if (GNUNET_NETWORK_socket_bind (plugin->unix_sock.desc, serverAddr, addrlen)
978 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
979 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc);
980 plugin->unix_sock.desc = NULL;
981 return GNUNET_SYSERR;
983 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "unix", "Bound to `%s'\n",
985 plugin->rs = GNUNET_NETWORK_fdset_create ();
986 plugin->ws = GNUNET_NETWORK_fdset_create ();
987 GNUNET_NETWORK_fdset_zero (plugin->rs);
988 GNUNET_NETWORK_fdset_zero (plugin->ws);
989 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->unix_sock.desc);
990 GNUNET_NETWORK_fdset_set (plugin->ws, plugin->unix_sock.desc);
992 reschedule_select (plugin);
999 * Function that will be called to check if a binary address for this
1000 * plugin is well-formed and corresponds to an address for THIS peer
1001 * (as per our configuration). Naturally, if absolutely necessary,
1002 * plugins can be a bit conservative in their answer, but in general
1003 * plugins should make sure that the address does not redirect
1004 * traffic to a 3rd party that might try to man-in-the-middle our
1007 * @param cls closure, should be our handle to the Plugin
1008 * @param addr pointer to the address
1009 * @param addrlen length of addr
1010 * @return GNUNET_OK if this is a plausible address for this peer
1011 * and transport, GNUNET_SYSERR if not
1015 unix_check_address (void *cls, const void *addr, size_t addrlen)
1017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1018 "Informing transport service about my address `%s'\n",
1025 * Convert the transports address to a nice, human-readable
1028 * @param cls closure
1029 * @param type name of the transport that generated the address
1030 * @param addr one of the addresses of the host, NULL for the last address
1031 * the specific address format depends on the transport
1032 * @param addrlen length of the address
1033 * @param numeric should (IP) addresses be displayed in numeric form?
1034 * @param timeout after how long should we give up?
1035 * @param asc function to call on each string
1036 * @param asc_cls closure for asc
1039 unix_plugin_address_pretty_printer (void *cls, const char *type,
1040 const void *addr, size_t addrlen,
1042 struct GNUNET_TIME_Relative timeout,
1043 GNUNET_TRANSPORT_AddressStringCallback asc,
1046 if ((NULL != addr) && (addrlen > 0))
1048 asc (asc_cls, (const char *) addr);
1053 asc (asc_cls, "<invalid UNIX address>");
1055 asc (asc_cls, NULL);
1060 * Function called to convert a string address to
1063 * @param cls closure ('struct Plugin*')
1064 * @param addr string address
1065 * @param addrlen length of the address (strlen(addr) + '\0')
1066 * @param buf location to store the buffer
1067 * If the function returns GNUNET_SYSERR, its contents are undefined.
1068 * @param added length of created address
1069 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1072 unix_string_to_address (void *cls, const char *addr, uint16_t addrlen,
1073 void **buf, size_t *added)
1075 if ((NULL == addr) || (0 == addrlen))
1078 return GNUNET_SYSERR;
1081 if ('\0' != addr[addrlen - 1])
1084 return GNUNET_SYSERR;
1087 if (strlen (addr) != addrlen - 1)
1090 return GNUNET_SYSERR;
1093 (*buf) = strdup (addr);
1094 (*added) = strlen (addr) + 1;
1100 * Function called for a quick conversion of the binary address to
1101 * a numeric address. Note that the caller must not free the
1102 * address and that the next call to this function is allowed
1103 * to override the address again.
1105 * @param cls closure
1106 * @param addr binary address
1107 * @param addrlen length of the address
1108 * @return string representing the same address
1111 unix_address_to_string (void *cls, const void *addr, size_t addrlen)
1113 if ((addr != NULL) && (addrlen > 0))
1114 return (const char *) addr;
1120 * Notify transport service about address
1122 * @param cls the plugin
1126 address_notification (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1128 struct Plugin *plugin = cls;
1130 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
1131 plugin->unix_socket_path,
1132 strlen (plugin->unix_socket_path) + 1);
1136 * The exported method. Makes the core api available via a global and
1137 * returns the unix transport API.
1140 libgnunet_plugin_transport_unix_init (void *cls)
1142 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1143 unsigned long long port;
1144 struct GNUNET_TRANSPORT_PluginFunctions *api;
1145 struct Plugin *plugin;
1146 int sockets_created;
1148 if (NULL == env->receive)
1150 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1151 initialze the plugin or the API */
1152 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1154 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1155 api->address_to_string = &unix_address_to_string;
1156 api->string_to_address = &unix_string_to_address;
1159 GNUNET_assert( NULL != env->stats);
1162 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-unix", "PORT",
1164 port = UNIX_NAT_DEFAULT_PORT;
1165 plugin = GNUNET_malloc (sizeof (struct Plugin));
1166 plugin->port = port;
1168 GNUNET_asprintf (&plugin->unix_socket_path, "/tmp/unix-plugin-sock.%d",
1171 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1174 api->get_session = &unix_plugin_get_session;
1175 api->send = &unix_plugin_send;
1176 api->disconnect = &unix_disconnect;
1177 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1178 api->address_to_string = &unix_address_to_string;
1179 api->check_address = &unix_check_address;
1180 api->string_to_address = &unix_string_to_address;
1181 sockets_created = unix_transport_server_start (plugin);
1182 if (sockets_created == 0)
1183 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UNIX sockets\n"));
1185 plugin->session_map = GNUNET_CONTAINER_multihashmap_create(10);
1187 GNUNET_SCHEDULER_add_now (address_notification, plugin);
1192 libgnunet_plugin_transport_unix_done (void *cls)
1194 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1195 struct Plugin *plugin = api->cls;
1202 unix_transport_server_stop (plugin);
1204 GNUNET_CONTAINER_multihashmap_iterate (plugin->session_map, &get_session_delete_it, plugin);
1205 GNUNET_CONTAINER_multihashmap_destroy (plugin->session_map);
1207 if (NULL != plugin->rs)
1208 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1209 if (NULL != plugin->ws)
1210 GNUNET_NETWORK_fdset_destroy (plugin->ws);
1211 GNUNET_free (plugin->unix_socket_path);
1212 GNUNET_free (plugin);
1217 /* end of plugin_transport_unix.c */