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_NO
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
68 * UNIX Message-Packet header.
75 struct GNUNET_MessageHeader header;
78 * What is the identity of the sender (GNUNET_hash of public key)
80 struct GNUNET_PeerIdentity sender;
87 * Pointer to next element.
89 struct RetryList *next;
92 * Pointer to previous element.
94 struct RetryList *prev;
97 * The actual retry context.
99 struct RetrySendContext *retry_ctx;
103 * Network format for IPv4 addresses.
105 struct IPv4UdpAddress
108 * IPv4 address, in network byte order.
110 uint32_t ipv4_addr GNUNET_PACKED;
113 * Port number, in network byte order.
115 uint16_t u_port GNUNET_PACKED;
120 * Network format for IPv6 addresses.
122 struct IPv6UdpAddress
127 struct in6_addr ipv6_addr GNUNET_PACKED;
130 * Port number, in network byte order.
132 uint16_t u6_port GNUNET_PACKED;
135 /* Forward definition */
138 struct PrettyPrinterContext
140 GNUNET_TRANSPORT_AddressStringCallback asc;
145 struct RetrySendContext
149 * Main plugin handle.
151 struct Plugin *plugin;
154 * Address of recipient.
169 * Size of the message.
174 * Handle to send message out on.
176 struct GNUNET_NETWORK_Handle *send_handle;
179 * Continuation to call on success or
182 GNUNET_TRANSPORT_TransmitContinuation cont;
185 * Closure for continuation.
190 * The peer the message is destined for.
192 struct GNUNET_PeerIdentity target;
195 * How long before not retrying any longer.
197 struct GNUNET_TIME_Absolute timeout;
200 * How long the last message was delayed.
202 struct GNUNET_TIME_Relative delay;
205 * The actual retry task.
207 GNUNET_SCHEDULER_TaskIdentifier retry_task;
210 * The priority of the message.
212 unsigned int priority;
215 * Entry in the DLL of retry items.
217 struct RetryList *retry_list_entry;
221 * Local network addresses (actual unix path follows).
227 * This is a doubly linked list.
229 struct LocalAddrList *next;
232 * This is a doubly linked list.
234 struct LocalAddrList *prev;
237 * Number of bytes of the address that follow
251 * Stored in a linked list.
253 struct PeerSession *next;
256 * Pointer to the global plugin struct.
258 struct Plugin *plugin;
261 * To whom are we talking to (set to our identity
262 * if we are still waiting for the welcome message)
264 struct GNUNET_PeerIdentity target;
267 * Address of the other peer (either based on our 'connect'
268 * call or on our 'accept' call).
273 * Length of connect_addr.
278 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
280 int expecting_welcome;
283 * From which socket do we need to send to this peer?
285 struct GNUNET_NETWORK_Handle *sock;
288 * Queue of messages for this peer, in the case that
289 * we have to await a connection...
291 struct MessageQueue *messages;
296 * Information we keep for each of our listen sockets.
298 struct UNIX_Sock_Info
303 struct GNUNET_NETWORK_Handle *desc;
306 * The port we bound to
313 * Encapsulation of all of the state of the plugin.
320 struct GNUNET_TRANSPORT_PluginEnvironment *env;
323 * Session of peers with whom we are currently connected
325 struct PeerSession *sessions;
328 * ID of task used to update our addresses when one expires.
330 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
335 GNUNET_SCHEDULER_TaskIdentifier select_task;
338 * Integer to append to unix domain socket.
343 * List of our IP addresses.
345 struct LocalAddrList *lal_head;
348 * Tail of our IP address list.
350 struct LocalAddrList *lal_tail;
355 struct GNUNET_NETWORK_FDSet *rs;
358 * socket that we transmit all data with
360 struct UNIX_Sock_Info unix_sock;
363 * Path of our unix domain socket (/tmp/unix-plugin-PORT)
365 char *unix_socket_path;
372 static struct RetryList *retry_list_head;
377 static struct RetryList *retry_list_tail;
381 * Disconnect from a remote node. Clean up session if we have one for this peer
383 * @param cls closure for this call (should be handle to Plugin)
384 * @param target the peeridentity of the peer to disconnect
385 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
388 unix_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
390 /** TODO: Implement! */
395 * Shutdown the server process (stop receiving inbound traffic). Maybe
398 * @param cls Handle to the plugin for this transport
400 * @return returns the number of sockets successfully closed,
401 * should equal the number of sockets successfully opened
404 unix_transport_server_stop (void *cls)
406 struct Plugin *plugin = cls;
407 struct RetryList *pos;
409 pos = retry_list_head;
411 while(NULL != (pos = retry_list_head))
413 GNUNET_CONTAINER_DLL_remove(retry_list_head, retry_list_tail, pos);
414 if (GNUNET_SCHEDULER_NO_TASK != pos->retry_ctx->retry_task)
416 GNUNET_SCHEDULER_cancel(pos->retry_ctx->retry_task);
418 GNUNET_free(pos->retry_ctx->msg);
419 GNUNET_free(pos->retry_ctx->addr);
420 GNUNET_free(pos->retry_ctx);
424 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
426 GNUNET_SCHEDULER_cancel (plugin->select_task);
427 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
430 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->unix_sock.desc));
431 plugin->unix_sock.desc = NULL;
438 find_session (struct Plugin *plugin,
439 const struct GNUNET_PeerIdentity *peer)
441 struct PeerSession *pos;
443 pos = plugin->sessions;
446 if (memcmp(&pos->target, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
454 /* Forward Declaration */
456 unix_real_send (void *cls,
457 struct RetrySendContext *incoming_retry_context,
458 struct GNUNET_NETWORK_Handle *send_handle,
459 const struct GNUNET_PeerIdentity *target,
462 unsigned int priority,
463 struct GNUNET_TIME_Relative timeout,
466 GNUNET_TRANSPORT_TransmitContinuation cont,
470 * Retry sending a message.
472 * @param cls closure a struct RetrySendContext
473 * @param tc context information
475 void retry_send_message (void *cls,
476 const struct GNUNET_SCHEDULER_TaskContext * tc)
478 struct RetrySendContext *retry_ctx = cls;
480 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
482 GNUNET_free(retry_ctx->msg);
483 GNUNET_free(retry_ctx->addr);
484 GNUNET_free(retry_ctx);
488 unix_real_send (retry_ctx->plugin,
490 retry_ctx->send_handle,
495 GNUNET_TIME_absolute_get_remaining (retry_ctx->timeout),
499 retry_ctx->cont_cls);
504 * Actually send out the message, assume we've got the address and
505 * send_handle squared away!
508 * @param incoming_retry_context the retry context to use
509 * @param send_handle which handle to send message on
510 * @param target who should receive this message (ignored by UNIX)
511 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
512 * @param msgbuf_size the size of the msgbuf to send
513 * @param priority how important is the message (ignored by UNIX)
514 * @param timeout when should we time out (give up) if we can not transmit?
515 * @param addr the addr to send the message to, needs to be a sockaddr for us
516 * @param addrlen the len of addr
517 * @param cont continuation to call once the message has
518 * been transmitted (or if the transport is ready
519 * for the next transmission call; or if the
520 * peer disconnected...)
521 * @param cont_cls closure for cont
523 * @return the number of bytes written, -1 on errors
526 unix_real_send (void *cls,
527 struct RetrySendContext *incoming_retry_context,
528 struct GNUNET_NETWORK_Handle *send_handle,
529 const struct GNUNET_PeerIdentity *target,
532 unsigned int priority,
533 struct GNUNET_TIME_Relative timeout,
536 GNUNET_TRANSPORT_TransmitContinuation cont,
539 struct Plugin *plugin = cls;
540 struct UNIXMessage *message;
541 struct RetrySendContext *retry_ctx;
546 struct sockaddr_un un;
548 struct RetryList *retry_list_entry;
551 if (send_handle == NULL)
554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
555 "unix_real_send with send_handle NULL!\n");
557 /* failed to open send socket for AF */
559 cont (cont_cls, target, GNUNET_SYSERR);
562 if ((addr == NULL) || (addrlen == 0))
565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
566 "unix_real_send called without address, returning!\n");
569 cont (cont_cls, target, GNUNET_SYSERR);
570 return 0; /* Can never send if we don't have an address!! */
573 /* Build the message to be sent */
574 message = GNUNET_malloc (sizeof (struct UNIXMessage) + msgbuf_size);
575 ssize = sizeof (struct UNIXMessage) + msgbuf_size;
577 message->header.size = htons (ssize);
578 message->header.type = htons (0);
579 memcpy (&message->sender, plugin->env->my_identity,
580 sizeof (struct GNUNET_PeerIdentity));
581 memcpy (&message[1], msgbuf, msgbuf_size);
583 memset(&un, 0, sizeof(un));
584 un.sun_family = AF_UNIX;
585 slen = strlen (addr) + 1;
586 if (slen >= sizeof (un.sun_path))
587 slen = sizeof (un.sun_path) - 1;
589 GNUNET_assert(slen < sizeof(un.sun_path));
590 memcpy (un.sun_path, addr, slen);
591 un.sun_path[slen] = '\0';
592 slen = sizeof (struct sockaddr_un);
594 un.sun_path[0] = '\0';
596 #if HAVE_SOCKADDR_IN_SIN_LEN
597 un.sun_len = (u_char) slen;
599 sb = (struct sockaddr*) &un;
603 sent = GNUNET_NETWORK_socket_sendto(send_handle, message, ssize, sb, sbs);
605 if ((GNUNET_SYSERR == sent) && (errno == EAGAIN))
608 if ((GNUNET_SYSERR == sent) && (errno == EMSGSIZE))
611 socklen_t len = sizeof (size);
612 GNUNET_NETWORK_socket_getsockopt ((struct GNUNET_NETWORK_Handle * ) send_handle,
620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
621 "Trying to increase socket buffer size from %i to %i for message size %i\n",
623 ((ssize / 1000) + 2) * 1000, ssize);
625 size = ((ssize / 1000) + 2) * 1000;
626 if (GNUNET_NETWORK_socket_setsockopt ((struct GNUNET_NETWORK_Handle * ) send_handle,
629 &size, sizeof(size)) == GNUNET_OK)
632 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
636 if (retry == GNUNET_YES)
638 if (incoming_retry_context == NULL)
640 retry_list_entry = GNUNET_malloc(sizeof(struct RetryList));
641 retry_ctx = GNUNET_malloc(sizeof(struct RetrySendContext));
642 retry_ctx->addr = GNUNET_malloc(addrlen);
643 retry_ctx->msg = GNUNET_malloc(msgbuf_size);
644 retry_ctx->plugin = plugin;
645 memcpy(retry_ctx->addr, addr, addrlen);
646 memcpy(retry_ctx->msg, msgbuf, msgbuf_size);
647 retry_ctx->msg_size = msgbuf_size;
648 retry_ctx->addrlen = addrlen;
649 retry_ctx->send_handle = send_handle;
650 retry_ctx->cont = cont;
651 retry_ctx->cont_cls = cont_cls;
652 retry_ctx->priority = priority;
653 retry_ctx->timeout = GNUNET_TIME_relative_to_absolute(timeout);
654 memcpy(&retry_ctx->target, target, sizeof(struct GNUNET_PeerIdentity));
655 retry_ctx->delay = GNUNET_TIME_UNIT_MILLISECONDS;
656 retry_ctx->retry_list_entry = retry_list_entry;
657 retry_list_entry->retry_ctx = retry_ctx;
658 GNUNET_CONTAINER_DLL_insert(retry_list_head, retry_list_tail, retry_list_entry);
662 retry_ctx = incoming_retry_context;
663 retry_ctx->delay = GNUNET_TIME_relative_multiply(retry_ctx->delay, 2);
665 retry_ctx->retry_task = GNUNET_SCHEDULER_add_delayed(retry_ctx->delay, &retry_send_message, retry_ctx);
667 //GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
668 GNUNET_free(message);
672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
673 "UNIX transmit %u-byte message to %s (%d: %s)\n",
674 (unsigned int) ssize,
675 GNUNET_a2s (sb, sbs),
677 (sent < 0) ? STRERROR (errno) : "ok");
681 if (sent == GNUNET_SYSERR)
682 cont (cont_cls, target, GNUNET_SYSERR);
685 cont (cont_cls, target, GNUNET_OK);
689 if (incoming_retry_context != NULL)
691 GNUNET_CONTAINER_DLL_remove(retry_list_head, retry_list_tail, incoming_retry_context->retry_list_entry);
692 GNUNET_free(incoming_retry_context->retry_list_entry);
693 GNUNET_free(incoming_retry_context->msg);
694 GNUNET_free(incoming_retry_context->addr);
695 GNUNET_free(incoming_retry_context);
698 GNUNET_free (message);
704 * Function that can be used by the transport service to transmit
705 * a message using the plugin.
708 * @param target who should receive this message (ignored by UNIX)
709 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
710 * @param msgbuf_size the size of the msgbuf to send
711 * @param priority how important is the message (ignored by UNIX)
712 * @param timeout when should we time out (give up) if we can not transmit?
713 * @param session identifier used for this session (can be NULL)
714 * @param addr the addr to send the message to, needs to be a sockaddr for us
715 * @param addrlen the len of addr
716 * @param force_address not used, we had better have an address to send to
717 * because we are stateless!!
718 * @param cont continuation to call once the message has
719 * been transmitted (or if the transport is ready
720 * for the next transmission call; or if the
721 * peer disconnected...)
722 * @param cont_cls closure for cont
724 * @return the number of bytes written (may return 0 and the message can
725 * still be transmitted later!)
728 unix_plugin_send (void *cls,
729 const struct GNUNET_PeerIdentity *target,
732 unsigned int priority,
733 struct GNUNET_TIME_Relative timeout,
734 struct Session *session,
738 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
740 struct Plugin *plugin = cls;
743 if (force_address == GNUNET_SYSERR)
744 return GNUNET_SYSERR;
745 GNUNET_assert (NULL == session);
748 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Asked to send message to `%s'\n", (char *)addr);
750 sent = unix_real_send(cls,
752 plugin->unix_sock.desc,
755 priority, timeout, addr, addrlen,
758 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sent %d bytes to `%s'\n", sent, (char *)addr);
760 if (sent == GNUNET_SYSERR)
767 add_to_address_list (struct Plugin *plugin,
771 struct LocalAddrList *lal;
773 lal = plugin->lal_head;
776 if ( (lal->size == arg_size) &&
777 (0 == memcmp (&lal[1], arg, arg_size)) )
781 lal = GNUNET_malloc (sizeof (struct LocalAddrList) + arg_size);
782 lal->size = arg_size;
783 memcpy (&lal[1], arg, arg_size);
784 GNUNET_CONTAINER_DLL_insert (plugin->lal_head,
791 * Demultiplexer for UNIX messages
793 * @param plugin the main plugin for this transport
794 * @param sender from which peer the message was received
795 * @param currhdr pointer to the header of the message
796 * @param un the address from which the message was received
797 * @param fromlen the length of the address
800 unix_demultiplexer(struct Plugin *plugin,
801 struct GNUNET_PeerIdentity *sender,
802 const struct GNUNET_MessageHeader *currhdr,
803 const struct sockaddr_un *un,
806 struct GNUNET_TRANSPORT_ATS_Information distance[2];
808 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
809 distance[0].value = htonl (UNIX_DIRECT_DISTANCE);
810 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
811 distance[1].value = htonl (0);
813 GNUNET_assert(fromlen >= sizeof(struct sockaddr_un));
816 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received message from %s\n", un->sun_path);
818 plugin->env->receive (plugin->env->cls, sender, currhdr,
819 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance, 2,
820 NULL, un->sun_path, strlen(un->sun_path) + 1);
825 * @param cls the plugin handle
826 * @param tc the scheduling context (for rescheduling this function again)
828 * We have been notified that our writeset has something to read. We don't
829 * know which socket needs to be read, so we have to check each one
830 * Then reschedule this function to be called again once more is available.
834 unix_plugin_select (void *cls,
835 const struct GNUNET_SCHEDULER_TaskContext *tc)
837 struct Plugin *plugin = cls;
839 struct UNIXMessage *msg;
840 struct GNUNET_PeerIdentity sender;
841 struct sockaddr_un un;
847 const struct GNUNET_MessageHeader *currhdr;
850 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
851 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
854 addrlen = sizeof(un);
855 memset(&un, 0, sizeof(un));
856 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->unix_sock.desc));
858 GNUNET_NETWORK_socket_recvfrom (plugin->unix_sock.desc, buf, sizeof (buf),
859 (struct sockaddr *)&un, &addrlen);
861 if (ret == GNUNET_SYSERR)
863 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom");
864 plugin->select_task =
865 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
866 GNUNET_SCHEDULER_NO_TASK,
867 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
868 NULL, &unix_plugin_select, plugin);
874 un.sun_path[0] = '/';
877 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Read %d bytes from socket %s\n", ret, &un.sun_path[0]);
881 GNUNET_assert (AF_UNIX == (un.sun_family));
883 msg = (struct UNIXMessage *) buf;
884 csize = ntohs (msg->header.size);
885 if ( (csize < sizeof (struct UNIXMessage)) ||
889 plugin->select_task =
890 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
891 GNUNET_SCHEDULER_NO_TASK,
892 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
893 NULL, &unix_plugin_select, plugin);
896 msgbuf = (char *)&msg[1];
897 memcpy (&sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
899 tsize = csize - sizeof (struct UNIXMessage);
900 while (offset + sizeof (struct GNUNET_MessageHeader) <= tsize)
902 currhdr = (struct GNUNET_MessageHeader *)&msgbuf[offset];
903 csize = ntohs (currhdr->size);
904 if ( (csize < sizeof (struct GNUNET_MessageHeader)) ||
905 (csize > tsize - offset) )
910 unix_demultiplexer(plugin, &sender, currhdr,
914 plugin->select_task =
915 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
916 GNUNET_SCHEDULER_NO_TASK,
917 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
918 NULL, &unix_plugin_select, plugin);
922 * Create a slew of UNIX sockets. If possible, use IPv6 and IPv4.
924 * @param cls closure for server start, should be a struct Plugin *
925 * @return number of sockets created or GNUNET_SYSERR on error
928 unix_transport_server_start (void *cls)
930 struct Plugin *plugin = cls;
931 struct sockaddr *serverAddr;
933 struct sockaddr_un un;
936 memset(&un, 0, sizeof(un));
937 un.sun_family = AF_UNIX;
938 slen = strlen (plugin->unix_socket_path) + 1;
939 if (slen >= sizeof (un.sun_path))
940 slen = sizeof (un.sun_path) - 1;
942 memcpy (un.sun_path, plugin->unix_socket_path, slen);
943 un.sun_path[slen] = '\0';
944 slen = sizeof (struct sockaddr_un);
945 #if HAVE_SOCKADDR_IN_SIN_LEN
946 un.sun_len = (u_char) slen;
949 serverAddr = (struct sockaddr*) &un;
952 un.sun_path[0] = '\0';
955 plugin->unix_sock.desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_DGRAM, 0);
956 if (NULL == plugin->unix_sock.desc)
958 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
959 return GNUNET_SYSERR;
961 if (GNUNET_NETWORK_socket_bind (plugin->unix_sock.desc, serverAddr, addrlen) !=
964 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
965 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc);
966 plugin->unix_sock.desc = NULL;
967 return GNUNET_SYSERR;
970 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
975 plugin->rs = GNUNET_NETWORK_fdset_create ();
976 GNUNET_NETWORK_fdset_zero (plugin->rs);
977 GNUNET_NETWORK_fdset_set (plugin->rs,
978 plugin->unix_sock.desc);
980 plugin->select_task =
981 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
982 GNUNET_SCHEDULER_NO_TASK,
983 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
984 NULL, &unix_plugin_select, plugin);
990 * Function that will be called to check if a binary address for this
991 * plugin is well-formed and corresponds to an address for THIS peer
992 * (as per our configuration). Naturally, if absolutely necessary,
993 * plugins can be a bit conservative in their answer, but in general
994 * plugins should make sure that the address does not redirect
995 * traffic to a 3rd party that might try to man-in-the-middle our
998 * @param cls closure, should be our handle to the Plugin
999 * @param addr pointer to the address
1000 * @param addrlen length of addr
1001 * @return GNUNET_OK if this is a plausible address for this peer
1002 * and transport, GNUNET_SYSERR if not
1006 unix_check_address (void *cls,
1012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1013 "Informing transport service about my address `%s'\n",
1021 * Append our port and forward the result.
1024 append_port (void *cls, const char *hostname)
1026 struct PrettyPrinterContext *ppc = cls;
1029 if (hostname == NULL)
1031 ppc->asc (ppc->asc_cls, NULL);
1035 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1036 ppc->asc (ppc->asc_cls, ret);
1042 * Convert the transports address to a nice, human-readable
1045 * @param cls closure
1046 * @param type name of the transport that generated the address
1047 * @param addr one of the addresses of the host, NULL for the last address
1048 * the specific address format depends on the transport
1049 * @param addrlen length of the address
1050 * @param numeric should (IP) addresses be displayed in numeric form?
1051 * @param timeout after how long should we give up?
1052 * @param asc function to call on each string
1053 * @param asc_cls closure for asc
1056 unix_plugin_address_pretty_printer (void *cls,
1061 struct GNUNET_TIME_Relative timeout,
1062 GNUNET_TRANSPORT_AddressStringCallback asc,
1065 struct PrettyPrinterContext *ppc;
1068 struct sockaddr_in a4;
1069 struct sockaddr_in6 a6;
1070 const struct IPv4UdpAddress *u4;
1071 const struct IPv6UdpAddress *u6;
1074 if (addrlen == sizeof (struct IPv6UdpAddress))
1077 memset (&a6, 0, sizeof (a6));
1078 a6.sin6_family = AF_INET6;
1079 a6.sin6_port = u6->u6_port;
1080 memcpy (&a6.sin6_addr,
1082 sizeof (struct in6_addr));
1083 port = ntohs (u6->u6_port);
1087 else if (addrlen == sizeof (struct IPv4UdpAddress))
1090 memset (&a4, 0, sizeof (a4));
1091 a4.sin_family = AF_INET;
1092 a4.sin_port = u4->u_port;
1093 a4.sin_addr.s_addr = u4->ipv4_addr;
1094 port = ntohs (u4->u_port);
1100 /* invalid address */
1101 GNUNET_break_op (0);
1102 asc (asc_cls, NULL);
1105 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1107 ppc->asc_cls = asc_cls;
1109 GNUNET_RESOLVER_hostname_get (sb,
1111 !numeric, timeout, &append_port, ppc);
1115 * Function called for a quick conversion of the binary address to
1116 * a numeric address. Note that the caller must not free the
1117 * address and that the next call to this function is allowed
1118 * to override the address again.
1120 * @param cls closure
1121 * @param addr binary address
1122 * @param addrlen length of the address
1123 * @return string representing the same address
1126 unix_address_to_string (void *cls,
1130 static char rbuf[INET6_ADDRSTRLEN + 10];
1131 char buf[INET6_ADDRSTRLEN];
1135 const struct IPv4UdpAddress *t4;
1136 const struct IPv6UdpAddress *t6;
1140 if (addrlen == sizeof (struct IPv6UdpAddress))
1144 port = ntohs (t6->u6_port);
1145 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
1148 else if (addrlen == sizeof (struct IPv4UdpAddress))
1152 port = ntohs (t4->u_port);
1153 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
1158 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
1159 GNUNET_snprintf (rbuf,
1168 * The exported method. Makes the core api available via a global and
1169 * returns the unix transport API.
1172 libgnunet_plugin_transport_unix_init (void *cls)
1174 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1175 unsigned long long port;
1176 struct GNUNET_TRANSPORT_PluginFunctions *api;
1177 struct Plugin *plugin;
1178 int sockets_created;
1181 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1185 port = UNIX_NAT_DEFAULT_PORT;
1186 else if (port > 65535)
1188 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1189 _("Given `%s' option is out of range: %llu > %u\n"),
1197 plugin = GNUNET_malloc (sizeof (struct Plugin));
1198 plugin->port = port;
1200 GNUNET_asprintf(&plugin->unix_socket_path, "/tmp/unix-plugin-sock.%d", plugin->port);
1202 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1205 api->send = &unix_plugin_send;
1206 api->disconnect = &unix_disconnect;
1207 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1208 api->address_to_string = &unix_address_to_string;
1209 api->check_address = &unix_check_address;
1211 add_to_address_list (plugin, plugin->unix_socket_path, strlen(plugin->unix_socket_path) + 1);
1213 sockets_created = unix_transport_server_start (plugin);
1214 if (sockets_created == 0)
1215 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1216 _("Failed to open UNIX sockets\n"));
1218 plugin->env->notify_address(plugin->env->cls,
1220 plugin->unix_socket_path,
1221 strlen(plugin->unix_socket_path) + 1,
1222 GNUNET_TIME_UNIT_FOREVER_REL);
1227 libgnunet_plugin_transport_unix_done (void *cls)
1229 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1230 struct Plugin *plugin = api->cls;
1231 struct LocalAddrList *lal;
1233 unix_transport_server_stop (plugin);
1235 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1236 while (NULL != (lal = plugin->lal_head))
1238 GNUNET_CONTAINER_DLL_remove (plugin->lal_head,
1243 GNUNET_free (plugin);
1248 /* end of plugin_transport_unix.c */