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"
45 #define DEBUG_UNIX GNUNET_EXTRA_LOGGING
46 #define DETAILS GNUNET_NO
51 * Transport cost to peer, always 1 for UNIX (direct connection)
53 #define UNIX_DIRECT_DISTANCE 1
55 #define DEFAULT_NAT_PORT 0
58 * How long until we give up on transmitting the welcome message?
60 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
63 * Starting port for listening and sending, eventually a config value
65 #define UNIX_NAT_DEFAULT_PORT 22086
67 GNUNET_NETWORK_STRUCT_BEGIN
70 * UNIX Message-Packet header.
77 struct GNUNET_MessageHeader header;
80 * What is the identity of the sender (GNUNET_hash of public key)
82 struct GNUNET_PeerIdentity sender;
89 * Pointer to next element.
91 struct RetryList *next;
94 * Pointer to previous element.
96 struct RetryList *prev;
99 * The actual retry context.
101 struct RetrySendContext *retry_ctx;
105 * Network format for IPv4 addresses.
107 struct IPv4UdpAddress
110 * IPv4 address, in network byte order.
112 uint32_t ipv4_addr GNUNET_PACKED;
115 * Port number, in network byte order.
117 uint16_t u_port GNUNET_PACKED;
122 * Network format for IPv6 addresses.
124 struct IPv6UdpAddress
129 struct in6_addr ipv6_addr GNUNET_PACKED;
132 * Port number, in network byte order.
134 uint16_t u6_port GNUNET_PACKED;
136 GNUNET_NETWORK_STRUCT_END
138 /* Forward definition */
141 struct PrettyPrinterContext
143 GNUNET_TRANSPORT_AddressStringCallback asc;
148 struct RetrySendContext
152 * Main plugin handle.
154 struct Plugin *plugin;
157 * Address of recipient.
172 * Size of the message.
177 * Handle to send message out on.
179 struct GNUNET_NETWORK_Handle *send_handle;
182 * Continuation to call on success or
185 GNUNET_TRANSPORT_TransmitContinuation cont;
188 * Closure for continuation.
193 * The peer the message is destined for.
195 struct GNUNET_PeerIdentity target;
198 * How long before not retrying any longer.
200 struct GNUNET_TIME_Absolute timeout;
203 * How long the last message was delayed.
205 struct GNUNET_TIME_Relative delay;
208 * The actual retry task.
210 GNUNET_SCHEDULER_TaskIdentifier retry_task;
213 * The priority of the message.
215 unsigned int priority;
218 * Entry in the DLL of retry items.
220 struct RetryList *retry_list_entry;
231 * Stored in a linked list.
233 struct PeerSession *next;
236 * Pointer to the global plugin struct.
238 struct Plugin *plugin;
241 * To whom are we talking to (set to our identity
242 * if we are still waiting for the welcome message)
244 struct GNUNET_PeerIdentity target;
247 * Address of the other peer (either based on our 'connect'
248 * call or on our 'accept' call).
253 * Length of connect_addr.
258 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
260 int expecting_welcome;
263 * From which socket do we need to send to this peer?
265 struct GNUNET_NETWORK_Handle *sock;
268 * Queue of messages for this peer, in the case that
269 * we have to await a connection...
271 struct MessageQueue *messages;
276 * Information we keep for each of our listen sockets.
278 struct UNIX_Sock_Info
283 struct GNUNET_NETWORK_Handle *desc;
286 * The port we bound to
293 * Encapsulation of all of the state of the plugin.
300 struct GNUNET_TRANSPORT_PluginEnvironment *env;
303 * Session of peers with whom we are currently connected
305 struct PeerSession *sessions;
308 * ID of task used to update our addresses when one expires.
310 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
315 GNUNET_SCHEDULER_TaskIdentifier select_task;
318 * Integer to append to unix domain socket.
325 struct GNUNET_NETWORK_FDSet *rs;
328 * socket that we transmit all data with
330 struct UNIX_Sock_Info unix_sock;
333 * Path of our unix domain socket (/tmp/unix-plugin-PORT)
335 char *unix_socket_path;
340 struct GNUNET_ATS_Information ats_network;
346 static struct RetryList *retry_list_head;
351 static struct RetryList *retry_list_tail;
355 * Disconnect from a remote node. Clean up session if we have one for this peer
357 * @param cls closure for this call (should be handle to Plugin)
358 * @param target the peeridentity of the peer to disconnect
359 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
362 unix_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
364 /** TODO: Implement! */
369 * Shutdown the server process (stop receiving inbound traffic). Maybe
372 * @param cls Handle to the plugin for this transport
374 * @return returns the number of sockets successfully closed,
375 * should equal the number of sockets successfully opened
378 unix_transport_server_stop (void *cls)
380 struct Plugin *plugin = cls;
381 struct RetryList *pos;
383 pos = retry_list_head;
385 while (NULL != (pos = retry_list_head))
387 GNUNET_CONTAINER_DLL_remove (retry_list_head, retry_list_tail, pos);
388 if (GNUNET_SCHEDULER_NO_TASK != pos->retry_ctx->retry_task)
390 GNUNET_SCHEDULER_cancel (pos->retry_ctx->retry_task);
392 GNUNET_free (pos->retry_ctx->msg);
393 GNUNET_free (pos->retry_ctx->addr);
394 GNUNET_free (pos->retry_ctx);
398 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
400 GNUNET_SCHEDULER_cancel (plugin->select_task);
401 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
404 GNUNET_break (GNUNET_OK ==
405 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc));
406 plugin->unix_sock.desc = NULL;
413 find_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *peer)
415 struct PeerSession *pos;
417 pos = plugin->sessions;
420 if (memcmp (&pos->target, peer, sizeof (struct GNUNET_PeerIdentity)) == 0)
428 /* Forward Declaration */
430 unix_real_send (void *cls, struct RetrySendContext *incoming_retry_context,
431 struct GNUNET_NETWORK_Handle *send_handle,
432 const struct GNUNET_PeerIdentity *target, const char *msgbuf,
433 size_t msgbuf_size, unsigned int priority,
434 struct GNUNET_TIME_Relative timeout, const void *addr,
435 size_t addrlen, GNUNET_TRANSPORT_TransmitContinuation cont,
439 * Retry sending a message.
441 * @param cls closure a struct RetrySendContext
442 * @param tc context information
445 retry_send_message (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
447 struct RetrySendContext *retry_ctx = cls;
449 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
451 GNUNET_free (retry_ctx->msg);
452 GNUNET_free (retry_ctx->addr);
453 GNUNET_free (retry_ctx);
457 unix_real_send (retry_ctx->plugin, retry_ctx, retry_ctx->send_handle,
458 &retry_ctx->target, retry_ctx->msg, retry_ctx->msg_size,
460 GNUNET_TIME_absolute_get_remaining (retry_ctx->timeout),
461 retry_ctx->addr, retry_ctx->addrlen, retry_ctx->cont,
462 retry_ctx->cont_cls);
467 * Actually send out the message, assume we've got the address and
468 * send_handle squared away!
471 * @param incoming_retry_context the retry context to use
472 * @param send_handle which handle to send message on
473 * @param target who should receive this message (ignored by UNIX)
474 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
475 * @param msgbuf_size the size of the msgbuf to send
476 * @param priority how important is the message (ignored by UNIX)
477 * @param timeout when should we time out (give up) if we can not transmit?
478 * @param addr the addr to send the message to, needs to be a sockaddr for us
479 * @param addrlen the len of addr
480 * @param cont continuation to call once the message has
481 * been transmitted (or if the transport is ready
482 * for the next transmission call; or if the
483 * peer disconnected...)
484 * @param cont_cls closure for cont
486 * @return the number of bytes written, -1 on errors
489 unix_real_send (void *cls, struct RetrySendContext *incoming_retry_context,
490 struct GNUNET_NETWORK_Handle *send_handle,
491 const struct GNUNET_PeerIdentity *target, const char *msgbuf,
492 size_t msgbuf_size, unsigned int priority,
493 struct GNUNET_TIME_Relative timeout, const void *addr,
494 size_t addrlen, GNUNET_TRANSPORT_TransmitContinuation cont,
497 struct Plugin *plugin = cls;
498 struct UNIXMessage *message;
499 struct RetrySendContext *retry_ctx;
504 struct sockaddr_un un;
506 struct RetryList *retry_list_entry;
509 if (send_handle == NULL)
512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
513 "unix_real_send with send_handle NULL!\n");
515 /* failed to open send socket for AF */
517 cont (cont_cls, target, GNUNET_SYSERR);
520 if ((addr == NULL) || (addrlen == 0))
523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
524 "unix_real_send called without address, returning!\n");
527 cont (cont_cls, target, GNUNET_SYSERR);
528 return 0; /* Can never send if we don't have an address!! */
531 /* Build the message to be sent */
532 message = GNUNET_malloc (sizeof (struct UNIXMessage) + msgbuf_size);
533 ssize = sizeof (struct UNIXMessage) + msgbuf_size;
535 message->header.size = htons (ssize);
536 message->header.type = htons (0);
537 memcpy (&message->sender, plugin->env->my_identity,
538 sizeof (struct GNUNET_PeerIdentity));
539 memcpy (&message[1], msgbuf, msgbuf_size);
541 memset (&un, 0, sizeof (un));
542 un.sun_family = AF_UNIX;
543 slen = strlen (addr) + 1;
544 if (slen >= sizeof (un.sun_path))
545 slen = sizeof (un.sun_path) - 1;
547 GNUNET_assert (slen < sizeof (un.sun_path));
548 memcpy (un.sun_path, addr, slen);
549 un.sun_path[slen] = '\0';
550 slen = sizeof (struct sockaddr_un);
552 un.sun_path[0] = '\0';
554 #if HAVE_SOCKADDR_IN_SIN_LEN
555 un.sun_len = (u_char) slen;
557 sb = (struct sockaddr *) &un;
561 sent = GNUNET_NETWORK_socket_sendto (send_handle, message, ssize, sb, sbs);
563 if ((GNUNET_SYSERR == sent) && ((errno == EAGAIN) || (errno == ENOBUFS)))
566 if ((GNUNET_SYSERR == sent) && (errno == EMSGSIZE))
569 socklen_t len = sizeof (size);
571 GNUNET_NETWORK_socket_getsockopt ((struct GNUNET_NETWORK_Handle *)
572 send_handle, SOL_SOCKET, SO_SNDBUF, &size,
578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
579 "Trying to increase socket buffer size from %i to %i for message size %i\n",
580 size, ((ssize / 1000) + 2) * 1000, ssize);
582 size = ((ssize / 1000) + 2) * 1000;
583 if (GNUNET_NETWORK_socket_setsockopt
584 ((struct GNUNET_NETWORK_Handle *) send_handle, SOL_SOCKET, SO_SNDBUF,
585 &size, sizeof (size)) == GNUNET_OK)
588 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
592 if (retry == GNUNET_YES)
594 if (incoming_retry_context == NULL)
596 retry_list_entry = GNUNET_malloc (sizeof (struct RetryList));
597 retry_ctx = GNUNET_malloc (sizeof (struct RetrySendContext));
598 retry_ctx->addr = GNUNET_malloc (addrlen);
599 retry_ctx->msg = GNUNET_malloc (msgbuf_size);
600 retry_ctx->plugin = plugin;
601 memcpy (retry_ctx->addr, addr, addrlen);
602 memcpy (retry_ctx->msg, msgbuf, msgbuf_size);
603 retry_ctx->msg_size = msgbuf_size;
604 retry_ctx->addrlen = addrlen;
605 retry_ctx->send_handle = send_handle;
606 retry_ctx->cont = cont;
607 retry_ctx->cont_cls = cont_cls;
608 retry_ctx->priority = priority;
609 retry_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
610 memcpy (&retry_ctx->target, target, sizeof (struct GNUNET_PeerIdentity));
611 retry_ctx->delay = GNUNET_TIME_UNIT_MILLISECONDS;
612 retry_ctx->retry_list_entry = retry_list_entry;
613 retry_list_entry->retry_ctx = retry_ctx;
614 GNUNET_CONTAINER_DLL_insert (retry_list_head, retry_list_tail,
619 retry_ctx = incoming_retry_context;
620 retry_ctx->delay = GNUNET_TIME_relative_multiply (retry_ctx->delay, 2);
622 retry_ctx->retry_task =
623 GNUNET_SCHEDULER_add_delayed (retry_ctx->delay, &retry_send_message,
626 //GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
627 GNUNET_free (message);
631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632 "UNIX transmit %u-byte message to %s (%d: %s)\n",
633 (unsigned int) ssize, GNUNET_a2s (sb, sbs), (int) sent,
634 (sent < 0) ? STRERROR (errno) : "ok");
638 if (sent == GNUNET_SYSERR)
639 cont (cont_cls, target, GNUNET_SYSERR);
642 cont (cont_cls, target, GNUNET_OK);
646 if (incoming_retry_context != NULL)
648 GNUNET_CONTAINER_DLL_remove (retry_list_head, retry_list_tail,
649 incoming_retry_context->retry_list_entry);
650 GNUNET_free (incoming_retry_context->retry_list_entry);
651 GNUNET_free (incoming_retry_context->msg);
652 GNUNET_free (incoming_retry_context->addr);
653 GNUNET_free (incoming_retry_context);
656 GNUNET_free (message);
662 * Function that can be used by the transport service to transmit
663 * a message using the plugin.
666 * @param target who should receive this message (ignored by UNIX)
667 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
668 * @param msgbuf_size the size of the msgbuf to send
669 * @param priority how important is the message (ignored by UNIX)
670 * @param timeout when should we time out (give up) if we can not transmit?
671 * @param session identifier used for this session (can be NULL)
672 * @param addr the addr to send the message to, needs to be a sockaddr for us
673 * @param addrlen the len of addr
674 * @param force_address not used, we had better have an address to send to
675 * because we are stateless!!
676 * @param cont continuation to call once the message has
677 * been transmitted (or if the transport is ready
678 * for the next transmission call; or if the
679 * peer disconnected...)
680 * @param cont_cls closure for cont
682 * @return the number of bytes written (may return 0 and the message can
683 * still be transmitted later!)
686 unix_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
687 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
688 struct GNUNET_TIME_Relative timeout, struct Session *session,
689 const void *addr, size_t addrlen, int force_address,
690 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
692 struct Plugin *plugin = cls;
695 GNUNET_assert (NULL == session);
698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asked to send message to `%s'\n",
702 unix_real_send (cls, NULL, plugin->unix_sock.desc, target, msgbuf,
703 msgbuf_size, priority, timeout, addr, addrlen, cont,
706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent %d bytes to `%s'\n", sent,
709 if (sent == GNUNET_SYSERR)
716 * Demultiplexer for UNIX messages
718 * @param plugin the main plugin for this transport
719 * @param sender from which peer the message was received
720 * @param currhdr pointer to the header of the message
721 * @param un the address from which the message was received
722 * @param fromlen the length of the address
725 unix_demultiplexer (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender,
726 const struct GNUNET_MessageHeader *currhdr,
727 const struct sockaddr_un *un, size_t fromlen)
729 struct GNUNET_ATS_Information ats[2];
731 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
732 ats[0].value = htonl (UNIX_DIRECT_DISTANCE);
733 ats[1] = plugin->ats_network;
734 GNUNET_break (ntohl(plugin->ats_network.value) != GNUNET_ATS_NET_UNSPECIFIED);
736 GNUNET_assert (fromlen >= sizeof (struct sockaddr_un));
739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from %s\n",
742 plugin->env->receive (plugin->env->cls, sender, currhdr,
743 (const struct GNUNET_ATS_Information *) &ats, 2,
744 NULL, un->sun_path, strlen (un->sun_path) + 1);
749 * @param cls the plugin handle
750 * @param tc the scheduling context (for rescheduling this function again)
752 * We have been notified that our writeset has something to read. We don't
753 * know which socket needs to be read, so we have to check each one
754 * Then reschedule this function to be called again once more is available.
758 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
760 struct Plugin *plugin = cls;
762 struct UNIXMessage *msg;
763 struct GNUNET_PeerIdentity sender;
764 struct sockaddr_un un;
770 const struct GNUNET_MessageHeader *currhdr;
773 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
774 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
777 addrlen = sizeof (un);
778 memset (&un, 0, sizeof (un));
779 GNUNET_assert (GNUNET_NETWORK_fdset_isset
780 (tc->read_ready, plugin->unix_sock.desc));
782 GNUNET_NETWORK_socket_recvfrom (plugin->unix_sock.desc, buf, sizeof (buf),
783 (struct sockaddr *) &un, &addrlen);
785 if (ret == GNUNET_SYSERR)
787 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom");
788 plugin->select_task =
789 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
790 GNUNET_SCHEDULER_NO_TASK,
791 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
792 NULL, &unix_plugin_select, plugin);
798 un.sun_path[0] = '/';
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Read %d bytes from socket %s\n", ret,
806 GNUNET_assert (AF_UNIX == (un.sun_family));
808 msg = (struct UNIXMessage *) buf;
809 csize = ntohs (msg->header.size);
810 if ((csize < sizeof (struct UNIXMessage)) || (csize > ret))
813 plugin->select_task =
814 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
815 GNUNET_SCHEDULER_NO_TASK,
816 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
817 NULL, &unix_plugin_select, plugin);
820 msgbuf = (char *) &msg[1];
821 memcpy (&sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
823 tsize = csize - sizeof (struct UNIXMessage);
824 while (offset + sizeof (struct GNUNET_MessageHeader) <= tsize)
826 currhdr = (struct GNUNET_MessageHeader *) &msgbuf[offset];
827 csize = ntohs (currhdr->size);
828 if ((csize < sizeof (struct GNUNET_MessageHeader)) ||
829 (csize > tsize - offset))
834 unix_demultiplexer (plugin, &sender, currhdr, &un, sizeof (un));
837 plugin->select_task =
838 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
839 GNUNET_SCHEDULER_NO_TASK,
840 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
841 NULL, &unix_plugin_select, plugin);
845 * Create a slew of UNIX sockets. If possible, use IPv6 and IPv4.
847 * @param cls closure for server start, should be a struct Plugin *
848 * @return number of sockets created or GNUNET_SYSERR on error
851 unix_transport_server_start (void *cls)
853 struct Plugin *plugin = cls;
854 struct sockaddr *serverAddr;
856 struct sockaddr_un un;
859 memset (&un, 0, sizeof (un));
860 un.sun_family = AF_UNIX;
861 slen = strlen (plugin->unix_socket_path) + 1;
862 if (slen >= sizeof (un.sun_path))
863 slen = sizeof (un.sun_path) - 1;
865 memcpy (un.sun_path, plugin->unix_socket_path, slen);
866 un.sun_path[slen] = '\0';
867 slen = sizeof (struct sockaddr_un);
868 #if HAVE_SOCKADDR_IN_SIN_LEN
869 un.sun_len = (u_char) slen;
872 serverAddr = (struct sockaddr *) &un;
875 un.sun_path[0] = '\0';
877 plugin->ats_network = plugin->env->get_address_type (plugin->env->cls, serverAddr, addrlen);
878 plugin->unix_sock.desc =
879 GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_DGRAM, 0);
880 if (NULL == plugin->unix_sock.desc)
882 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
883 return GNUNET_SYSERR;
885 if (GNUNET_NETWORK_socket_bind (plugin->unix_sock.desc, serverAddr, addrlen)
888 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
889 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc);
890 plugin->unix_sock.desc = NULL;
891 return GNUNET_SYSERR;
894 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "unix", "Bound to `%s'\n",
897 plugin->rs = GNUNET_NETWORK_fdset_create ();
898 GNUNET_NETWORK_fdset_zero (plugin->rs);
899 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->unix_sock.desc);
901 plugin->select_task =
902 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
903 GNUNET_SCHEDULER_NO_TASK,
904 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
905 NULL, &unix_plugin_select, plugin);
911 * Function that will be called to check if a binary address for this
912 * plugin is well-formed and corresponds to an address for THIS peer
913 * (as per our configuration). Naturally, if absolutely necessary,
914 * plugins can be a bit conservative in their answer, but in general
915 * plugins should make sure that the address does not redirect
916 * traffic to a 3rd party that might try to man-in-the-middle our
919 * @param cls closure, should be our handle to the Plugin
920 * @param addr pointer to the address
921 * @param addrlen length of addr
922 * @return GNUNET_OK if this is a plausible address for this peer
923 * and transport, GNUNET_SYSERR if not
927 unix_check_address (void *cls, const void *addr, size_t addrlen)
931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
932 "Informing transport service about my address `%s'\n",
940 * Append our port and forward the result.
943 append_port (void *cls, const char *hostname)
945 struct PrettyPrinterContext *ppc = cls;
948 if (hostname == NULL)
950 ppc->asc (ppc->asc_cls, NULL);
954 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
955 ppc->asc (ppc->asc_cls, ret);
961 * Convert the transports address to a nice, human-readable
965 * @param type name of the transport that generated the address
966 * @param addr one of the addresses of the host, NULL for the last address
967 * the specific address format depends on the transport
968 * @param addrlen length of the address
969 * @param numeric should (IP) addresses be displayed in numeric form?
970 * @param timeout after how long should we give up?
971 * @param asc function to call on each string
972 * @param asc_cls closure for asc
975 unix_plugin_address_pretty_printer (void *cls, const char *type,
976 const void *addr, size_t addrlen,
978 struct GNUNET_TIME_Relative timeout,
979 GNUNET_TRANSPORT_AddressStringCallback asc,
982 struct PrettyPrinterContext *ppc;
985 struct sockaddr_in a4;
986 struct sockaddr_in6 a6;
987 const struct IPv4UdpAddress *u4;
988 const struct IPv6UdpAddress *u6;
991 if (addrlen == sizeof (struct IPv6UdpAddress))
994 memset (&a6, 0, sizeof (a6));
995 a6.sin6_family = AF_INET6;
996 a6.sin6_port = u6->u6_port;
997 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
998 port = ntohs (u6->u6_port);
1002 else if (addrlen == sizeof (struct IPv4UdpAddress))
1005 memset (&a4, 0, sizeof (a4));
1006 a4.sin_family = AF_INET;
1007 a4.sin_port = u4->u_port;
1008 a4.sin_addr.s_addr = u4->ipv4_addr;
1009 port = ntohs (u4->u_port);
1015 /* invalid address */
1016 GNUNET_break_op (0);
1017 asc (asc_cls, NULL);
1020 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1022 ppc->asc_cls = asc_cls;
1024 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
1028 * Function called for a quick conversion of the binary address to
1029 * a numeric address. Note that the caller must not free the
1030 * address and that the next call to this function is allowed
1031 * to override the address again.
1033 * @param cls closure
1034 * @param addr binary address
1035 * @param addrlen length of the address
1036 * @return string representing the same address
1039 unix_address_to_string (void *cls, const void *addr, size_t addrlen)
1041 if ((addr != NULL) && (addrlen > 0))
1042 return (const char *) addr;
1048 * Notify transport service about address
1050 * @param cls the plugin
1054 address_notification (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1056 struct Plugin *plugin = cls;
1058 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
1059 plugin->unix_socket_path,
1060 strlen (plugin->unix_socket_path) + 1);
1064 * The exported method. Makes the core api available via a global and
1065 * returns the unix transport API.
1068 libgnunet_plugin_transport_unix_init (void *cls)
1070 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1071 unsigned long long port;
1072 struct GNUNET_TRANSPORT_PluginFunctions *api;
1073 struct Plugin *plugin;
1074 int sockets_created;
1077 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-unix", "PORT",
1079 port = UNIX_NAT_DEFAULT_PORT;
1080 plugin = GNUNET_malloc (sizeof (struct Plugin));
1081 plugin->port = port;
1083 GNUNET_asprintf (&plugin->unix_socket_path, "/tmp/unix-plugin-sock.%d",
1086 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1089 api->send = &unix_plugin_send;
1090 api->disconnect = &unix_disconnect;
1091 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1092 api->address_to_string = &unix_address_to_string;
1093 api->check_address = &unix_check_address;
1094 sockets_created = unix_transport_server_start (plugin);
1095 if (sockets_created == 0)
1096 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UNIX sockets\n"));
1098 GNUNET_SCHEDULER_add_now (address_notification, plugin);
1103 libgnunet_plugin_transport_unix_done (void *cls)
1105 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1106 struct Plugin *plugin = api->cls;
1108 unix_transport_server_stop (plugin);
1110 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1111 GNUNET_free (plugin->unix_socket_path);
1112 GNUNET_free (plugin);
1117 /* end of plugin_transport_unix.c */