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;
243 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
247 reschedule_select (struct Plugin * plugin)
250 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
252 GNUNET_SCHEDULER_cancel (plugin->select_task);
253 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
256 if (NULL != plugin->msg_head)
258 plugin->select_task =
259 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
260 GNUNET_TIME_UNIT_FOREVER_REL,
263 &unix_plugin_select, plugin);
264 plugin->with_ws = GNUNET_YES;
268 plugin->select_task =
269 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
270 GNUNET_TIME_UNIT_FOREVER_REL,
273 &unix_plugin_select, plugin);
274 plugin->with_ws = GNUNET_NO;
280 get_session_delete_it (void *cls, const GNUNET_HashCode * key, void *value)
282 struct Session *s = value;
283 struct UNIXMessageWrapper * msgw;
284 struct Plugin *plugin = cls;
286 GNUNET_assert (plugin != NULL);
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting session for peer `%s' `%s' \n", GNUNET_i2s (&s->target), s->addr);
289 plugin->env->session_end (plugin->env->cls, &s->target, s);
291 msgw = plugin->msg_head;
293 while (NULL != (msgw = plugin->msg_head))
295 if (msgw->session == s)
297 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
298 if (msgw->cont != NULL)
299 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR);
300 GNUNET_free (msgw->msg);
302 removed = GNUNET_YES;
305 if ((GNUNET_YES == removed) && (NULL == plugin->msg_head))
306 reschedule_select (plugin);
308 GNUNET_assert (GNUNET_YES ==
309 GNUNET_CONTAINER_multihashmap_remove(plugin->session_map, &s->target.hashPubKey, s));
311 GNUNET_STATISTICS_set(plugin->env->stats,
312 "# UNIX sessions active",
313 GNUNET_CONTAINER_multihashmap_size(plugin->session_map),
322 * Disconnect from a remote node. Clean up session if we have one for this peer
324 * @param cls closure for this call (should be handle to Plugin)
325 * @param target the peeridentity of the peer to disconnect
326 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
329 unix_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
331 struct Plugin *plugin = cls;
332 GNUNET_assert (plugin != NULL);
334 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map, &target->hashPubKey, &get_session_delete_it, plugin);
339 * Shutdown the server process (stop receiving inbound traffic). Maybe
342 * @param cls Handle to the plugin for this transport
344 * @return returns the number of sockets successfully closed,
345 * should equal the number of sockets successfully opened
348 unix_transport_server_stop (void *cls)
350 struct Plugin *plugin = cls;
352 struct UNIXMessageWrapper * msgw = plugin->msg_head;
354 while (NULL != (msgw = plugin->msg_head))
356 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
357 if (msgw->cont != NULL)
358 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR);
359 GNUNET_free (msgw->msg);
363 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
365 GNUNET_SCHEDULER_cancel (plugin->select_task);
366 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
369 if (NULL != plugin->unix_sock.desc)
371 GNUNET_break (GNUNET_OK ==
372 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc));
373 plugin->unix_sock.desc = NULL;
374 plugin->with_ws = GNUNET_NO;
381 * Actually send out the message, assume we've got the address and
382 * send_handle squared away!
385 * @param send_handle which handle to send message on
386 * @param target who should receive this message (ignored by UNIX)
387 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
388 * @param msgbuf_size the size of the msgbuf to send
389 * @param priority how important is the message (ignored by UNIX)
390 * @param timeout when should we time out (give up) if we can not transmit?
391 * @param addr the addr to send the message to, needs to be a sockaddr for us
392 * @param addrlen the len of addr
393 * @param cont continuation to call once the message has
394 * been transmitted (or if the transport is ready
395 * for the next transmission call; or if the
396 * peer disconnected...)
397 * @param cont_cls closure for cont
399 * @return the number of bytes written, -1 on errors
402 unix_real_send (void *cls,
403 struct GNUNET_NETWORK_Handle *send_handle,
404 const struct GNUNET_PeerIdentity *target, const char *msgbuf,
405 size_t msgbuf_size, unsigned int priority,
406 struct GNUNET_TIME_Relative timeout, const void *addr,
407 size_t addrlen, GNUNET_TRANSPORT_TransmitContinuation cont,
410 struct Plugin *plugin = cls;
414 struct sockaddr_un un;
418 GNUNET_assert (NULL != plugin);
420 if (send_handle == NULL)
422 /* We do not have a send handle */
425 cont (cont_cls, target, GNUNET_SYSERR);
428 if ((addr == NULL) || (addrlen == 0))
430 /* Can never send if we don't have an address */
433 cont (cont_cls, target, GNUNET_SYSERR);
437 /* Prepare address */
438 memset (&un, 0, sizeof (un));
439 un.sun_family = AF_UNIX;
440 slen = strlen (addr) + 1;
441 if (slen >= sizeof (un.sun_path))
442 slen = sizeof (un.sun_path) - 1;
443 GNUNET_assert (slen < sizeof (un.sun_path));
444 memcpy (un.sun_path, addr, slen);
445 un.sun_path[slen] = '\0';
446 slen = sizeof (struct sockaddr_un);
448 un.sun_path[0] = '\0';
450 #if HAVE_SOCKADDR_IN_SIN_LEN
451 un.sun_len = (u_char) slen;
453 sb = (struct sockaddr *) &un;
459 sent = GNUNET_NETWORK_socket_sendto (send_handle, msgbuf, msgbuf_size, sb, sbs);
461 if ((GNUNET_SYSERR == sent) && ((errno == EAGAIN) || (errno == ENOBUFS)))
463 /* We have to retry later: retry */
467 if ((GNUNET_SYSERR == sent) && (errno == EMSGSIZE))
470 socklen_t len = sizeof (size);
472 GNUNET_NETWORK_socket_getsockopt ((struct GNUNET_NETWORK_Handle *)
473 send_handle, SOL_SOCKET, SO_SNDBUF, &size,
476 if (size < msgbuf_size)
478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
479 "Trying to increase socket buffer size from %i to %i for message size %i\n",
481 ((msgbuf_size / 1000) + 2) * 1000,
483 size = ((msgbuf_size / 1000) + 2) * 1000;
484 if (GNUNET_NETWORK_socket_setsockopt
485 ((struct GNUNET_NETWORK_Handle *) send_handle, SOL_SOCKET, SO_SNDBUF,
486 &size, sizeof (size)) == GNUNET_OK)
488 /* Increased buffer size, retry sending */
493 /* Could not increase buffer size: error, no retry */
494 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
500 /* Buffer is bigger than message: error, no retry
501 * This should never happen!*/
507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
508 "UNIX transmit %u-byte message to %s (%d: %s)\n",
509 (unsigned int) msgbuf_size, GNUNET_a2s (sb, sbs), (int) sent,
510 (sent < 0) ? STRERROR (errno) : "ok");
512 /* Calling continuation */
515 if ((sent == GNUNET_SYSERR) && (retry == GNUNET_NO))
516 cont (cont_cls, target, GNUNET_SYSERR);
518 cont (cont_cls, target, GNUNET_OK);
521 /* return number of bytes successfully sent */
526 /* That should never happen */
530 /* failed and retry: return 0 */
531 if ((GNUNET_SYSERR == sent) && (retry == GNUNET_YES))
533 /* failed and no retry: return -1 */
534 if ((GNUNET_SYSERR == sent) && (retry == GNUNET_NO))
549 get_session_it (void *cls, const GNUNET_HashCode * key, void *value)
551 struct gsi_ctx *gsi = cls;
552 struct Session *s = value;
554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing session %s %s\n", gsi->address, s->addr);
555 if ((gsi->addrlen == s->addrlen) &&
556 (0 == memcmp (gsi->address, s->addr, s->addrlen)))
565 * Creates a new outbound session the transport service will use to send data to the
568 * @param cls the plugin
569 * @param address the address
570 * @return the session or NULL of max connections exceeded
572 static struct Session *
573 unix_plugin_get_session (void *cls,
574 const struct GNUNET_HELLO_Address *address)
576 struct Session * s = NULL;
577 struct Plugin *plugin = cls;
581 GNUNET_assert (plugin != NULL);
582 GNUNET_assert (address != NULL);
584 /* Check if already existing */
585 gsi.address = (char *) address->address;
586 gsi.addrlen = address->address_length;
588 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map, &address->peer.hashPubKey, &get_session_it, &gsi);
591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found existing session\n");
595 /* Create a new session */
597 s = GNUNET_malloc (sizeof (struct Session) + address->address_length);
599 s->addrlen = address->address_length;
600 memcpy(s->addr, address->address, s->addrlen);
601 memcpy(&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
603 GNUNET_CONTAINER_multihashmap_put (plugin->session_map,
604 &address->peer.hashPubKey, s,
605 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
607 GNUNET_STATISTICS_set(plugin->env->stats,
608 "# UNIX sessions active",
609 GNUNET_CONTAINER_multihashmap_size(plugin->session_map),
611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating new session\n");
616 * @param cls the plugin handle
617 * @param tc the scheduling context (for rescheduling this function again)
619 * We have been notified that our writeset has something to read. We don't
620 * know which socket needs to be read, so we have to check each one
621 * Then reschedule this function to be called again once more is available.
625 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
628 * Function that can be used by the transport service to transmit
629 * a message using the plugin. Note that in the case of a
630 * peer disconnecting, the continuation MUST be called
631 * prior to the disconnect notification itself. This function
632 * will be called with this peer's HELLO message to initiate
633 * a fresh connection to another peer.
636 * @param session which session must be used
637 * @param msgbuf the message to transmit
638 * @param msgbuf_size number of bytes in 'msgbuf'
639 * @param priority how important is the message (most plugins will
640 * ignore message priority and just FIFO)
641 * @param to how long to wait at most for the transmission (does not
642 * require plugins to discard the message after the timeout,
643 * just advisory for the desired delay; most plugins will ignore
645 * @param cont continuation to call once the message has
646 * been transmitted (or if the transport is ready
647 * for the next transmission call; or if the
648 * peer disconnected...); can be NULL
649 * @param cont_cls closure for cont
650 * @return number of bytes used (on the physical network, with overheads);
651 * -1 on hard errors (i.e. address invalid); 0 is a legal value
652 * and does NOT mean that the message was not transmitted (DV)
655 unix_plugin_send (void *cls,
656 struct Session *session,
657 const char *msgbuf, size_t msgbuf_size,
658 unsigned int priority,
659 struct GNUNET_TIME_Relative to,
660 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
662 struct Plugin *plugin = cls;
663 struct UNIXMessageWrapper *wrapper;
664 struct UNIXMessage *message;
667 GNUNET_assert (plugin != NULL);
668 GNUNET_assert (session != NULL);
670 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_contains_value(plugin->session_map,
671 &session->target.hashPubKey, session))
674 return GNUNET_SYSERR;
677 ssize = sizeof (struct UNIXMessage) + msgbuf_size;
678 message = GNUNET_malloc (sizeof (struct UNIXMessage) + msgbuf_size);
679 message->header.size = htons (ssize);
680 message->header.type = htons (0);
681 memcpy (&message->sender, plugin->env->my_identity,
682 sizeof (struct GNUNET_PeerIdentity));
683 memcpy (&message[1], msgbuf, msgbuf_size);
686 wrapper = GNUNET_malloc (sizeof (struct UNIXMessageWrapper));
687 wrapper->msg = message;
688 wrapper->msgsize = ssize;
689 wrapper->priority = priority;
690 wrapper->timeout = to;
691 wrapper->cont = cont;
692 wrapper->cont_cls = cont_cls;
693 wrapper->session = session;
695 GNUNET_CONTAINER_DLL_insert(plugin->msg_head, plugin->msg_tail, wrapper);
697 plugin->bytes_in_queue += ssize;
698 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes in send queue",
699 plugin->bytes_in_queue, GNUNET_NO);
701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent %d bytes to `%s'\n", ssize,
702 (char *) session->addr);
703 if (plugin->with_ws == GNUNET_NO)
705 reschedule_select (plugin);
712 * Demultiplexer for UNIX messages
714 * @param plugin the main plugin for this transport
715 * @param sender from which peer the message was received
716 * @param currhdr pointer to the header of the message
717 * @param un the address from which the message was received
718 * @param fromlen the length of the address
721 unix_demultiplexer (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender,
722 const struct GNUNET_MessageHeader *currhdr,
723 const struct sockaddr_un *un, size_t fromlen)
725 struct GNUNET_ATS_Information ats[2];
727 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
728 ats[0].value = htonl (UNIX_DIRECT_DISTANCE);
729 ats[1] = plugin->ats_network;
730 GNUNET_break (ntohl(plugin->ats_network.value) != GNUNET_ATS_NET_UNSPECIFIED);
732 GNUNET_assert (fromlen >= sizeof (struct sockaddr_un));
734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from %s\n",
736 plugin->env->receive (plugin->env->cls, sender, currhdr,
737 (const struct GNUNET_ATS_Information *) &ats, 2,
738 NULL, un->sun_path, strlen (un->sun_path) + 1);
743 unix_plugin_select_read (struct Plugin * plugin)
745 char buf[65536] GNUNET_ALIGN;
746 struct UNIXMessage *msg;
747 struct GNUNET_PeerIdentity sender;
748 struct sockaddr_un un;
754 const struct GNUNET_MessageHeader *currhdr;
757 addrlen = sizeof (un);
758 memset (&un, 0, sizeof (un));
761 GNUNET_NETWORK_socket_recvfrom (plugin->unix_sock.desc, buf, sizeof (buf),
762 (struct sockaddr *) &un, &addrlen);
764 if ((GNUNET_SYSERR == ret) && ((errno == EAGAIN) || (errno == ENOBUFS)))
767 if (ret == GNUNET_SYSERR)
769 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom");
775 un.sun_path[0] = '/';
777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Read %d bytes from socket %s\n", ret,
781 GNUNET_assert (AF_UNIX == (un.sun_family));
783 msg = (struct UNIXMessage *) buf;
784 csize = ntohs (msg->header.size);
785 if ((csize < sizeof (struct UNIXMessage)) || (csize > ret))
790 msgbuf = (char *) &msg[1];
791 memcpy (&sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
793 tsize = csize - sizeof (struct UNIXMessage);
794 while (offset + sizeof (struct GNUNET_MessageHeader) <= tsize)
796 currhdr = (struct GNUNET_MessageHeader *) &msgbuf[offset];
797 csize = ntohs (currhdr->size);
798 if ((csize < sizeof (struct GNUNET_MessageHeader)) ||
799 (csize > tsize - offset))
804 unix_demultiplexer (plugin, &sender, currhdr, &un, sizeof (un));
811 unix_plugin_select_write (struct Plugin * plugin)
813 static int retry_counter;
815 struct UNIXMessageWrapper * msgw = plugin->msg_head;
817 sent = unix_real_send (plugin,
818 plugin->unix_sock.desc,
819 &msgw->session->target,
820 (const char *) msgw->msg,
825 msgw->session->addrlen,
826 msgw->cont, msgw->cont_cls);
830 /* failed and retry */
832 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX retry attempt",
833 retry_counter, GNUNET_NO);
837 if (retry_counter > 0 )
839 /* no retry: reset counter */
841 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX retry attempt",
842 retry_counter, GNUNET_NO);
847 /* failed and no retry */
848 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
850 GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
851 plugin->bytes_in_queue -= msgw->msgsize;
852 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes in send queue",
853 plugin->bytes_in_queue, GNUNET_NO);
855 GNUNET_free (msgw->msg);
862 /* successfully sent bytes */
863 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
865 GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
866 plugin->bytes_in_queue -= msgw->msgsize;
867 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes in send queue",
868 plugin->bytes_in_queue, GNUNET_NO);
870 GNUNET_free (msgw->msg);
879 * We have been notified that our writeset has something to read. We don't
880 * know which socket needs to be read, so we have to check each one
881 * Then reschedule this function to be called again once more is available.
883 * @param cls the plugin handle
884 * @param tc the scheduling context (for rescheduling this function again)
887 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
889 struct Plugin *plugin = cls;
891 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
892 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
895 if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
897 /* Ready to send data */
898 GNUNET_assert (GNUNET_NETWORK_fdset_isset
899 (tc->write_ready, plugin->unix_sock.desc));
900 if (plugin->msg_head != NULL)
901 unix_plugin_select_write (plugin);
904 if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
906 /* Ready to receive data */
907 GNUNET_assert (GNUNET_NETWORK_fdset_isset
908 (tc->read_ready, plugin->unix_sock.desc));
909 unix_plugin_select_read (plugin);
912 reschedule_select (plugin);
917 * Create a slew of UNIX sockets. If possible, use IPv6 and IPv4.
919 * @param cls closure for server start, should be a struct Plugin *
920 * @return number of sockets created or GNUNET_SYSERR on error
923 unix_transport_server_start (void *cls)
925 struct Plugin *plugin = cls;
926 struct sockaddr *serverAddr;
928 struct sockaddr_un un;
931 memset (&un, 0, sizeof (un));
932 un.sun_family = AF_UNIX;
933 slen = strlen (plugin->unix_socket_path) + 1;
934 if (slen >= sizeof (un.sun_path))
935 slen = sizeof (un.sun_path) - 1;
937 memcpy (un.sun_path, plugin->unix_socket_path, slen);
938 un.sun_path[slen] = '\0';
939 slen = sizeof (struct sockaddr_un);
940 #if HAVE_SOCKADDR_IN_SIN_LEN
941 un.sun_len = (u_char) slen;
944 serverAddr = (struct sockaddr *) &un;
947 un.sun_path[0] = '\0';
949 plugin->ats_network = plugin->env->get_address_type (plugin->env->cls, serverAddr, addrlen);
950 plugin->unix_sock.desc =
951 GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_DGRAM, 0);
952 if (NULL == plugin->unix_sock.desc)
954 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
955 return GNUNET_SYSERR;
957 if (GNUNET_NETWORK_socket_bind (plugin->unix_sock.desc, serverAddr, addrlen)
960 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
961 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc);
962 plugin->unix_sock.desc = NULL;
963 return GNUNET_SYSERR;
965 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "unix", "Bound to `%s'\n",
967 plugin->rs = GNUNET_NETWORK_fdset_create ();
968 plugin->ws = GNUNET_NETWORK_fdset_create ();
969 GNUNET_NETWORK_fdset_zero (plugin->rs);
970 GNUNET_NETWORK_fdset_zero (plugin->ws);
971 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->unix_sock.desc);
972 GNUNET_NETWORK_fdset_set (plugin->ws, plugin->unix_sock.desc);
974 reschedule_select (plugin);
981 * Function that will be called to check if a binary address for this
982 * plugin is well-formed and corresponds to an address for THIS peer
983 * (as per our configuration). Naturally, if absolutely necessary,
984 * plugins can be a bit conservative in their answer, but in general
985 * plugins should make sure that the address does not redirect
986 * traffic to a 3rd party that might try to man-in-the-middle our
989 * @param cls closure, should be our handle to the Plugin
990 * @param addr pointer to the address
991 * @param addrlen length of addr
992 * @return GNUNET_OK if this is a plausible address for this peer
993 * and transport, GNUNET_SYSERR if not
997 unix_check_address (void *cls, const void *addr, size_t addrlen)
999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1000 "Informing transport service about my address `%s'\n",
1007 * Convert the transports address to a nice, human-readable
1010 * @param cls closure
1011 * @param type name of the transport that generated the address
1012 * @param addr one of the addresses of the host, NULL for the last address
1013 * the specific address format depends on the transport
1014 * @param addrlen length of the address
1015 * @param numeric should (IP) addresses be displayed in numeric form?
1016 * @param timeout after how long should we give up?
1017 * @param asc function to call on each string
1018 * @param asc_cls closure for asc
1021 unix_plugin_address_pretty_printer (void *cls, const char *type,
1022 const void *addr, size_t addrlen,
1024 struct GNUNET_TIME_Relative timeout,
1025 GNUNET_TRANSPORT_AddressStringCallback asc,
1028 if ((NULL != addr) && (addrlen > 0))
1030 asc (asc_cls, (const char *) addr);
1035 asc (asc_cls, "<invalid UNIX address>");
1037 asc (asc_cls, NULL);
1042 * Function called to convert a string address to
1045 * @param cls closure ('struct Plugin*')
1046 * @param addr string address
1047 * @param addrlen length of the address (strlen(addr) + '\0')
1048 * @param buf location to store the buffer
1049 * If the function returns GNUNET_SYSERR, its contents are undefined.
1050 * @param added length of created address
1051 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1054 unix_string_to_address (void *cls, const char *addr, uint16_t addrlen,
1055 void **buf, size_t *added)
1057 if ((NULL == addr) || (0 == addrlen))
1060 return GNUNET_SYSERR;
1063 if ('\0' != addr[addrlen - 1])
1066 return GNUNET_SYSERR;
1069 if (strlen (addr) != addrlen - 1)
1072 return GNUNET_SYSERR;
1075 (*buf) = strdup (addr);
1076 (*added) = strlen (addr) + 1;
1082 * Function called for a quick conversion of the binary address to
1083 * a numeric address. Note that the caller must not free the
1084 * address and that the next call to this function is allowed
1085 * to override the address again.
1087 * @param cls closure
1088 * @param addr binary address
1089 * @param addrlen length of the address
1090 * @return string representing the same address
1093 unix_address_to_string (void *cls, const void *addr, size_t addrlen)
1095 if ((addr != NULL) && (addrlen > 0))
1096 return (const char *) addr;
1102 * Notify transport service about address
1104 * @param cls the plugin
1108 address_notification (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1110 struct Plugin *plugin = cls;
1112 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
1113 plugin->unix_socket_path,
1114 strlen (plugin->unix_socket_path) + 1);
1118 * The exported method. Makes the core api available via a global and
1119 * returns the unix transport API.
1122 libgnunet_plugin_transport_unix_init (void *cls)
1124 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1125 unsigned long long port;
1126 struct GNUNET_TRANSPORT_PluginFunctions *api;
1127 struct Plugin *plugin;
1128 int sockets_created;
1130 if (NULL == env->receive)
1132 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1133 initialze the plugin or the API */
1134 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1136 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1137 api->address_to_string = &unix_address_to_string;
1138 api->string_to_address = &unix_string_to_address;
1141 GNUNET_assert( NULL != env->stats);
1144 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-unix", "PORT",
1146 port = UNIX_NAT_DEFAULT_PORT;
1147 plugin = GNUNET_malloc (sizeof (struct Plugin));
1148 plugin->port = port;
1150 GNUNET_asprintf (&plugin->unix_socket_path, "/tmp/unix-plugin-sock.%d",
1153 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1156 api->get_session = &unix_plugin_get_session;
1157 api->send = &unix_plugin_send;
1158 api->disconnect = &unix_disconnect;
1159 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1160 api->address_to_string = &unix_address_to_string;
1161 api->check_address = &unix_check_address;
1162 api->string_to_address = &unix_string_to_address;
1163 sockets_created = unix_transport_server_start (plugin);
1164 if (sockets_created == 0)
1165 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UNIX sockets\n"));
1167 plugin->session_map = GNUNET_CONTAINER_multihashmap_create(10);
1169 GNUNET_SCHEDULER_add_now (address_notification, plugin);
1174 libgnunet_plugin_transport_unix_done (void *cls)
1176 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1177 struct Plugin *plugin = api->cls;
1184 unix_transport_server_stop (plugin);
1186 GNUNET_CONTAINER_multihashmap_iterate (plugin->session_map, &get_session_delete_it, plugin);
1187 GNUNET_CONTAINER_multihashmap_destroy (plugin->session_map);
1189 if (NULL != plugin->rs)
1190 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1191 if (NULL != plugin->ws)
1192 GNUNET_NETWORK_fdset_destroy (plugin->ws);
1193 GNUNET_free (plugin->unix_socket_path);
1194 GNUNET_free (plugin);
1199 /* end of plugin_transport_unix.c */