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;
189 * Session of peers with whom we are currently connected
191 struct PeerSession *sessions;
196 struct GNUNET_CONTAINER_MultiHashMap *session_map;
199 * ID of task used to update our addresses when one expires.
201 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
206 GNUNET_SCHEDULER_TaskIdentifier select_task;
209 * Integer to append to unix domain socket.
216 struct GNUNET_NETWORK_FDSet *rs;
221 struct GNUNET_NETWORK_FDSet *ws;
226 * socket that we transmit all data with
228 struct UNIX_Sock_Info unix_sock;
231 * Path of our unix domain socket (/tmp/unix-plugin-PORT)
233 char *unix_socket_path;
235 struct UNIXMessageWrapper *msg_head;
236 struct UNIXMessageWrapper *msg_tail;
241 struct GNUNET_ATS_Information ats_network;
243 unsigned int bytes_in_queue;
248 get_session_delete_it (void *cls, const GNUNET_HashCode * key, void *value)
250 struct Session *s = value;
251 struct Plugin *plugin = cls;
252 GNUNET_assert (plugin != NULL);
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting session for peer `%s' `%s' \n", GNUNET_i2s (&s->target), s->addr);
255 plugin->env->session_end (plugin->env->cls, &s->target, s);
257 GNUNET_assert (GNUNET_YES ==
258 GNUNET_CONTAINER_multihashmap_remove(plugin->session_map, &s->target.hashPubKey, s));
260 GNUNET_STATISTICS_set(plugin->env->stats,
261 "# UNIX sessions active",
262 GNUNET_CONTAINER_multihashmap_size(plugin->session_map),
271 * Disconnect from a remote node. Clean up session if we have one for this peer
273 * @param cls closure for this call (should be handle to Plugin)
274 * @param target the peeridentity of the peer to disconnect
275 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
278 unix_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
280 struct Plugin *plugin = cls;
281 GNUNET_assert (plugin != NULL);
283 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map, &target->hashPubKey, &get_session_delete_it, plugin);
288 * Shutdown the server process (stop receiving inbound traffic). Maybe
291 * @param cls Handle to the plugin for this transport
293 * @return returns the number of sockets successfully closed,
294 * should equal the number of sockets successfully opened
297 unix_transport_server_stop (void *cls)
299 struct Plugin *plugin = cls;
301 struct UNIXMessageWrapper * msgw = plugin->msg_head;
303 while (NULL != (msgw = plugin->msg_head))
305 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
306 if (msgw->cont != NULL)
307 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR);
308 GNUNET_free (msgw->msg);
312 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
314 GNUNET_SCHEDULER_cancel (plugin->select_task);
315 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
318 if (NULL != plugin->unix_sock.desc)
320 GNUNET_break (GNUNET_OK ==
321 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc));
322 plugin->unix_sock.desc = NULL;
323 plugin->with_ws = GNUNET_NO;
330 find_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *peer)
332 struct PeerSession *pos;
334 pos = plugin->sessions;
337 if (memcmp (&pos->target, peer, sizeof (struct GNUNET_PeerIdentity)) == 0)
347 * Actually send out the message, assume we've got the address and
348 * send_handle squared away!
351 * @param send_handle which handle to send message on
352 * @param target who should receive this message (ignored by UNIX)
353 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
354 * @param msgbuf_size the size of the msgbuf to send
355 * @param priority how important is the message (ignored by UNIX)
356 * @param timeout when should we time out (give up) if we can not transmit?
357 * @param addr the addr to send the message to, needs to be a sockaddr for us
358 * @param addrlen the len of addr
359 * @param cont continuation to call once the message has
360 * been transmitted (or if the transport is ready
361 * for the next transmission call; or if the
362 * peer disconnected...)
363 * @param cont_cls closure for cont
365 * @return the number of bytes written, -1 on errors
368 unix_real_send (void *cls,
369 struct GNUNET_NETWORK_Handle *send_handle,
370 const struct GNUNET_PeerIdentity *target, const char *msgbuf,
371 size_t msgbuf_size, unsigned int priority,
372 struct GNUNET_TIME_Relative timeout, const void *addr,
373 size_t addrlen, GNUNET_TRANSPORT_TransmitContinuation cont,
376 struct Plugin *plugin = cls;
380 struct sockaddr_un un;
384 GNUNET_assert (NULL != plugin);
386 if (send_handle == NULL)
388 /* We do not have a send handle */
391 cont (cont_cls, target, GNUNET_SYSERR);
394 if ((addr == NULL) || (addrlen == 0))
396 /* Can never send if we don't have an address */
399 cont (cont_cls, target, GNUNET_SYSERR);
403 /* Prepare address */
404 memset (&un, 0, sizeof (un));
405 un.sun_family = AF_UNIX;
406 slen = strlen (addr) + 1;
407 if (slen >= sizeof (un.sun_path))
408 slen = sizeof (un.sun_path) - 1;
409 GNUNET_assert (slen < sizeof (un.sun_path));
410 memcpy (un.sun_path, addr, slen);
411 un.sun_path[slen] = '\0';
412 slen = sizeof (struct sockaddr_un);
414 un.sun_path[0] = '\0';
416 #if HAVE_SOCKADDR_IN_SIN_LEN
417 un.sun_len = (u_char) slen;
419 sb = (struct sockaddr *) &un;
425 sent = GNUNET_NETWORK_socket_sendto (send_handle, msgbuf, msgbuf_size, sb, sbs);
427 if ((GNUNET_SYSERR == sent) && ((errno == EAGAIN) || (errno == ENOBUFS)))
429 /* We have to retry later: retry */
433 if ((GNUNET_SYSERR == sent) && (errno == EMSGSIZE))
436 socklen_t len = sizeof (size);
438 GNUNET_NETWORK_socket_getsockopt ((struct GNUNET_NETWORK_Handle *)
439 send_handle, SOL_SOCKET, SO_SNDBUF, &size,
442 if (size < msgbuf_size)
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
445 "Trying to increase socket buffer size from %i to %i for message size %i\n",
447 ((msgbuf_size / 1000) + 2) * 1000,
449 size = ((msgbuf_size / 1000) + 2) * 1000;
450 if (GNUNET_NETWORK_socket_setsockopt
451 ((struct GNUNET_NETWORK_Handle *) send_handle, SOL_SOCKET, SO_SNDBUF,
452 &size, sizeof (size)) == GNUNET_OK)
454 /* Increased buffer size, retry sending */
459 /* Could not increase buffer size: error, no retry */
460 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
466 /* Buffer is bigger than message: error, no retry
467 * This should never happen!*/
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474 "UNIX transmit %u-byte message to %s (%d: %s)\n",
475 (unsigned int) msgbuf_size, GNUNET_a2s (sb, sbs), (int) sent,
476 (sent < 0) ? STRERROR (errno) : "ok");
478 /* Calling continuation */
481 if ((sent == GNUNET_SYSERR) && (retry == GNUNET_NO))
482 cont (cont_cls, target, GNUNET_SYSERR);
484 cont (cont_cls, target, GNUNET_OK);
487 /* return number of bytes successfully sent */
492 /* That should never happen */
496 /* failed and retry: return 0 */
497 if ((GNUNET_SYSERR == sent) && (retry == GNUNET_YES))
499 /* failed and no retry: return -1 */
500 if ((GNUNET_SYSERR == sent) && (retry == GNUNET_NO))
514 get_session_it (void *cls, const GNUNET_HashCode * key, void *value)
516 struct gsi_ctx *gsi = cls;
517 struct Session *s = value;
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing session %s %s\n", gsi->address, s->addr);
520 if ((gsi->addrlen == s->addrlen) &&
521 (0 == memcmp (gsi->address, s->addr, s->addrlen)))
530 * Creates a new outbound session the transport service will use to send data to the
533 * @param cls the plugin
534 * @param address the address
535 * @return the session or NULL of max connections exceeded
537 static struct Session *
538 unix_plugin_get_session (void *cls,
539 const struct GNUNET_HELLO_Address *address)
541 struct Session * s = NULL;
542 struct Plugin *plugin = cls;
546 GNUNET_assert (plugin != NULL);
547 GNUNET_assert (address != NULL);
549 /* Check if already existing */
550 gsi.address = (char *) address->address;
551 gsi.addrlen = address->address_length;
553 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map, &address->peer.hashPubKey, &get_session_it, &gsi);
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found existing session\n");
560 /* Create a new session */
562 s = GNUNET_malloc (sizeof (struct Session) + address->address_length);
564 s->addrlen = address->address_length;
565 memcpy(s->addr, address->address, s->addrlen);
566 memcpy(&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
568 GNUNET_CONTAINER_multihashmap_put (plugin->session_map,
569 &address->peer.hashPubKey, s,
570 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
572 GNUNET_STATISTICS_set(plugin->env->stats,
573 "# UNIX sessions active",
574 GNUNET_CONTAINER_multihashmap_size(plugin->session_map),
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating new session\n");
581 * @param cls the plugin handle
582 * @param tc the scheduling context (for rescheduling this function again)
584 * We have been notified that our writeset has something to read. We don't
585 * know which socket needs to be read, so we have to check each one
586 * Then reschedule this function to be called again once more is available.
590 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
593 * Function that can be used by the transport service to transmit
594 * a message using the plugin. Note that in the case of a
595 * peer disconnecting, the continuation MUST be called
596 * prior to the disconnect notification itself. This function
597 * will be called with this peer's HELLO message to initiate
598 * a fresh connection to another peer.
601 * @param session which session must be used
602 * @param msgbuf the message to transmit
603 * @param msgbuf_size number of bytes in 'msgbuf'
604 * @param priority how important is the message (most plugins will
605 * ignore message priority and just FIFO)
606 * @param to how long to wait at most for the transmission (does not
607 * require plugins to discard the message after the timeout,
608 * just advisory for the desired delay; most plugins will ignore
610 * @param cont continuation to call once the message has
611 * been transmitted (or if the transport is ready
612 * for the next transmission call; or if the
613 * peer disconnected...); can be NULL
614 * @param cont_cls closure for cont
615 * @return number of bytes used (on the physical network, with overheads);
616 * -1 on hard errors (i.e. address invalid); 0 is a legal value
617 * and does NOT mean that the message was not transmitted (DV)
620 unix_plugin_send (void *cls,
621 struct Session *session,
622 const char *msgbuf, size_t msgbuf_size,
623 unsigned int priority,
624 struct GNUNET_TIME_Relative to,
625 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
627 struct Plugin *plugin = cls;
628 struct UNIXMessageWrapper *wrapper;
629 struct UNIXMessage *message;
632 GNUNET_assert (plugin != NULL);
633 GNUNET_assert (session != NULL);
635 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_contains_value(plugin->session_map,
636 &session->target.hashPubKey, session))
639 return GNUNET_SYSERR;
642 ssize = sizeof (struct UNIXMessage) + msgbuf_size;
643 message = GNUNET_malloc (sizeof (struct UNIXMessage) + msgbuf_size);
644 message->header.size = htons (ssize);
645 message->header.type = htons (0);
646 memcpy (&message->sender, plugin->env->my_identity,
647 sizeof (struct GNUNET_PeerIdentity));
648 memcpy (&message[1], msgbuf, msgbuf_size);
651 wrapper = GNUNET_malloc (sizeof (struct UNIXMessageWrapper));
652 wrapper->msg = message;
653 wrapper->msgsize = ssize;
654 wrapper->priority = priority;
655 wrapper->timeout = to;
656 wrapper->cont = cont;
657 wrapper->cont_cls = cont_cls;
658 wrapper->session = session;
660 GNUNET_CONTAINER_DLL_insert(plugin->msg_head, plugin->msg_tail, wrapper);
662 plugin->bytes_in_queue += ssize;
663 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes in send queue",
664 plugin->bytes_in_queue, GNUNET_NO);
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent %d bytes to `%s'\n", ssize,
667 (char *) session->addr);
668 if (plugin->with_ws == GNUNET_NO)
670 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
671 GNUNET_SCHEDULER_cancel(plugin->select_task);
673 plugin->select_task =
674 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
675 GNUNET_TIME_UNIT_FOREVER_REL,
678 &unix_plugin_select, plugin);
679 plugin->with_ws = GNUNET_YES;
686 * Demultiplexer for UNIX messages
688 * @param plugin the main plugin for this transport
689 * @param sender from which peer the message was received
690 * @param currhdr pointer to the header of the message
691 * @param un the address from which the message was received
692 * @param fromlen the length of the address
695 unix_demultiplexer (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender,
696 const struct GNUNET_MessageHeader *currhdr,
697 const struct sockaddr_un *un, size_t fromlen)
699 struct GNUNET_ATS_Information ats[2];
701 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
702 ats[0].value = htonl (UNIX_DIRECT_DISTANCE);
703 ats[1] = plugin->ats_network;
704 GNUNET_break (ntohl(plugin->ats_network.value) != GNUNET_ATS_NET_UNSPECIFIED);
706 GNUNET_assert (fromlen >= sizeof (struct sockaddr_un));
708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from %s\n",
710 plugin->env->receive (plugin->env->cls, sender, currhdr,
711 (const struct GNUNET_ATS_Information *) &ats, 2,
712 NULL, un->sun_path, strlen (un->sun_path) + 1);
717 unix_plugin_select_read (struct Plugin * plugin)
719 char buf[65536] GNUNET_ALIGN;
720 struct UNIXMessage *msg;
721 struct GNUNET_PeerIdentity sender;
722 struct sockaddr_un un;
728 const struct GNUNET_MessageHeader *currhdr;
731 addrlen = sizeof (un);
732 memset (&un, 0, sizeof (un));
735 GNUNET_NETWORK_socket_recvfrom (plugin->unix_sock.desc, buf, sizeof (buf),
736 (struct sockaddr *) &un, &addrlen);
738 if ((GNUNET_SYSERR == ret) && ((errno == EAGAIN) || (errno == ENOBUFS)))
741 if (ret == GNUNET_SYSERR)
743 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom");
749 un.sun_path[0] = '/';
751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Read %d bytes from socket %s\n", ret,
755 GNUNET_assert (AF_UNIX == (un.sun_family));
757 msg = (struct UNIXMessage *) buf;
758 csize = ntohs (msg->header.size);
759 if ((csize < sizeof (struct UNIXMessage)) || (csize > ret))
764 msgbuf = (char *) &msg[1];
765 memcpy (&sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
767 tsize = csize - sizeof (struct UNIXMessage);
768 while (offset + sizeof (struct GNUNET_MessageHeader) <= tsize)
770 currhdr = (struct GNUNET_MessageHeader *) &msgbuf[offset];
771 csize = ntohs (currhdr->size);
772 if ((csize < sizeof (struct GNUNET_MessageHeader)) ||
773 (csize > tsize - offset))
778 unix_demultiplexer (plugin, &sender, currhdr, &un, sizeof (un));
784 unix_plugin_select_write (struct Plugin * plugin)
786 static int retry_counter;
788 struct UNIXMessageWrapper * msgw = plugin->msg_head;
790 sent = unix_real_send (plugin,
791 plugin->unix_sock.desc,
792 &msgw->session->target,
793 (const char *) msgw->msg,
798 msgw->session->addrlen,
799 msgw->cont, msgw->cont_cls);
803 /* failed and retry */
805 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX retry attempt",
806 retry_counter, GNUNET_NO);
810 if (retry_counter > 0 )
812 /* no retry: reset counter */
814 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX retry attempt",
815 retry_counter, GNUNET_NO);
820 /* failed and no retry */
821 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
823 GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
824 plugin->bytes_in_queue -= msgw->msgsize;
825 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes in send queue",
826 plugin->bytes_in_queue, GNUNET_NO);
828 GNUNET_free (msgw->msg);
835 /* successfully sent bytes */
836 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
838 GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
839 plugin->bytes_in_queue -= msgw->msgsize;
840 GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes in send queue",
841 plugin->bytes_in_queue, GNUNET_NO);
843 GNUNET_free (msgw->msg);
851 * @param cls the plugin handle
852 * @param tc the scheduling context (for rescheduling this function again)
854 * We have been notified that our writeset has something to read. We don't
855 * know which socket needs to be read, so we have to check each one
856 * Then reschedule this function to be called again once more is available.
860 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
862 struct Plugin *plugin = cls;
864 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
865 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
868 if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
870 /* Ready to send data */
871 GNUNET_assert (GNUNET_NETWORK_fdset_isset
872 (tc->write_ready, plugin->unix_sock.desc));
873 if (plugin->msg_head != NULL)
874 unix_plugin_select_write (plugin);
877 if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
879 /* Ready to receive data */
880 GNUNET_assert (GNUNET_NETWORK_fdset_isset
881 (tc->read_ready, plugin->unix_sock.desc));
882 unix_plugin_select_read (plugin);
885 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
886 GNUNET_SCHEDULER_cancel (plugin->select_task);
888 if (NULL != plugin->msg_head)
890 plugin->select_task =
891 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
892 GNUNET_TIME_UNIT_FOREVER_REL,
895 &unix_plugin_select, plugin);
896 plugin->with_ws = GNUNET_YES;
900 plugin->select_task =
901 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
902 GNUNET_TIME_UNIT_FOREVER_REL,
905 &unix_plugin_select, plugin);
906 plugin->with_ws = GNUNET_NO;
911 * Create a slew of UNIX sockets. If possible, use IPv6 and IPv4.
913 * @param cls closure for server start, should be a struct Plugin *
914 * @return number of sockets created or GNUNET_SYSERR on error
917 unix_transport_server_start (void *cls)
919 struct Plugin *plugin = cls;
920 struct sockaddr *serverAddr;
922 struct sockaddr_un un;
925 memset (&un, 0, sizeof (un));
926 un.sun_family = AF_UNIX;
927 slen = strlen (plugin->unix_socket_path) + 1;
928 if (slen >= sizeof (un.sun_path))
929 slen = sizeof (un.sun_path) - 1;
931 memcpy (un.sun_path, plugin->unix_socket_path, slen);
932 un.sun_path[slen] = '\0';
933 slen = sizeof (struct sockaddr_un);
934 #if HAVE_SOCKADDR_IN_SIN_LEN
935 un.sun_len = (u_char) slen;
938 serverAddr = (struct sockaddr *) &un;
941 un.sun_path[0] = '\0';
943 plugin->ats_network = plugin->env->get_address_type (plugin->env->cls, serverAddr, addrlen);
944 plugin->unix_sock.desc =
945 GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_DGRAM, 0);
946 if (NULL == plugin->unix_sock.desc)
948 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
949 return GNUNET_SYSERR;
951 if (GNUNET_NETWORK_socket_bind (plugin->unix_sock.desc, serverAddr, addrlen)
954 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
955 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc);
956 plugin->unix_sock.desc = NULL;
957 return GNUNET_SYSERR;
959 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "unix", "Bound to `%s'\n",
961 plugin->rs = GNUNET_NETWORK_fdset_create ();
962 plugin->ws = GNUNET_NETWORK_fdset_create ();
963 GNUNET_NETWORK_fdset_zero (plugin->rs);
964 GNUNET_NETWORK_fdset_zero (plugin->ws);
965 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->unix_sock.desc);
966 GNUNET_NETWORK_fdset_set (plugin->ws, plugin->unix_sock.desc);
968 plugin->select_task =
969 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
970 GNUNET_TIME_UNIT_FOREVER_REL,
973 &unix_plugin_select, plugin);
974 plugin->with_ws = GNUNET_NO;
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 ((addr != NULL) && (addrlen > 0))
1029 asc (asc_cls, (const char *) addr);
1033 asc (asc_cls, "Invalid UNIX address");
1039 * Function called to convert a string address to
1042 * @param cls closure ('struct Plugin*')
1043 * @param addr string address
1044 * @param addrlen length of the address (strlen(addr) + '\0')
1045 * @param buf location to store the buffer
1046 * If the function returns GNUNET_SYSERR, its contents are undefined.
1047 * @param added length of created address
1048 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1051 unix_string_to_address (void *cls, const char *addr, uint16_t addrlen,
1052 void **buf, size_t *added)
1054 if ((NULL == addr) || (addrlen == 0))
1057 return GNUNET_SYSERR;
1060 if ('\0' != addr[addrlen - 1])
1063 return GNUNET_SYSERR;
1066 if (strlen (addr) != addrlen - 1)
1069 return GNUNET_SYSERR;
1072 (*buf) = strdup (addr);
1073 (*added) = strlen (addr) + 1;
1081 * Function called for a quick conversion of the binary address to
1082 * a numeric address. Note that the caller must not free the
1083 * address and that the next call to this function is allowed
1084 * to override the address again.
1086 * @param cls closure
1087 * @param addr binary address
1088 * @param addrlen length of the address
1089 * @return string representing the same address
1092 unix_address_to_string (void *cls, const void *addr, size_t addrlen)
1094 if ((addr != NULL) && (addrlen > 0))
1095 return (const char *) addr;
1101 * Notify transport service about address
1103 * @param cls the plugin
1107 address_notification (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1109 struct Plugin *plugin = cls;
1111 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
1112 plugin->unix_socket_path,
1113 strlen (plugin->unix_socket_path) + 1);
1117 * The exported method. Makes the core api available via a global and
1118 * returns the unix transport API.
1121 libgnunet_plugin_transport_unix_init (void *cls)
1123 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1124 unsigned long long port;
1125 struct GNUNET_TRANSPORT_PluginFunctions *api;
1126 struct Plugin *plugin;
1127 int sockets_created;
1129 if (NULL == env->receive)
1131 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1132 initialze the plugin or the API */
1133 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1135 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1136 api->address_to_string = &unix_address_to_string;
1137 api->string_to_address = &unix_string_to_address;
1140 GNUNET_assert( NULL != env->stats);
1143 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-unix", "PORT",
1145 port = UNIX_NAT_DEFAULT_PORT;
1146 plugin = GNUNET_malloc (sizeof (struct Plugin));
1147 plugin->port = port;
1149 GNUNET_asprintf (&plugin->unix_socket_path, "/tmp/unix-plugin-sock.%d",
1152 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1155 api->get_session = &unix_plugin_get_session;
1156 api->send = &unix_plugin_send;
1157 api->disconnect = &unix_disconnect;
1158 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1159 api->address_to_string = &unix_address_to_string;
1160 api->check_address = &unix_check_address;
1161 api->string_to_address = &unix_string_to_address;
1162 sockets_created = unix_transport_server_start (plugin);
1163 if (sockets_created == 0)
1164 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UNIX sockets\n"));
1166 plugin->session_map = GNUNET_CONTAINER_multihashmap_create(10);
1168 GNUNET_SCHEDULER_add_now (address_notification, plugin);
1173 libgnunet_plugin_transport_unix_done (void *cls)
1175 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1176 struct Plugin *plugin = api->cls;
1183 unix_transport_server_stop (plugin);
1185 GNUNET_CONTAINER_multihashmap_iterate (plugin->session_map, &get_session_delete_it, plugin);
1186 GNUNET_CONTAINER_multihashmap_destroy (plugin->session_map);
1188 if (NULL != plugin->rs)
1189 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1190 if (NULL != plugin->ws)
1191 GNUNET_NETWORK_fdset_destroy (plugin->ws);
1192 GNUNET_free (plugin->unix_socket_path);
1193 GNUNET_free (plugin);
1198 /* end of plugin_transport_unix.c */