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;
228 * Stored in a linked list.
230 struct PeerSession *next;
233 * Pointer to the global plugin struct.
235 struct Plugin *plugin;
238 * To whom are we talking to (set to our identity
239 * if we are still waiting for the welcome message)
241 struct GNUNET_PeerIdentity target;
244 * Address of the other peer (either based on our 'connect'
245 * call or on our 'accept' call).
250 * Length of connect_addr.
255 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
257 int expecting_welcome;
260 * From which socket do we need to send to this peer?
262 struct GNUNET_NETWORK_Handle *sock;
265 * Queue of messages for this peer, in the case that
266 * we have to await a connection...
268 struct MessageQueue *messages;
273 * Information we keep for each of our listen sockets.
275 struct UNIX_Sock_Info
280 struct GNUNET_NETWORK_Handle *desc;
283 * The port we bound to
290 * Encapsulation of all of the state of the plugin.
297 struct GNUNET_TRANSPORT_PluginEnvironment *env;
300 * Session of peers with whom we are currently connected
302 struct PeerSession *sessions;
305 * ID of task used to update our addresses when one expires.
307 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
312 GNUNET_SCHEDULER_TaskIdentifier select_task;
315 * Integer to append to unix domain socket.
322 struct GNUNET_NETWORK_FDSet *rs;
325 * socket that we transmit all data with
327 struct UNIX_Sock_Info unix_sock;
330 * Path of our unix domain socket (/tmp/unix-plugin-PORT)
332 char *unix_socket_path;
339 static struct RetryList *retry_list_head;
344 static struct RetryList *retry_list_tail;
348 * Disconnect from a remote node. Clean up session if we have one for this peer
350 * @param cls closure for this call (should be handle to Plugin)
351 * @param target the peeridentity of the peer to disconnect
352 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
355 unix_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
357 /** TODO: Implement! */
362 * Shutdown the server process (stop receiving inbound traffic). Maybe
365 * @param cls Handle to the plugin for this transport
367 * @return returns the number of sockets successfully closed,
368 * should equal the number of sockets successfully opened
371 unix_transport_server_stop (void *cls)
373 struct Plugin *plugin = cls;
374 struct RetryList *pos;
376 pos = retry_list_head;
378 while(NULL != (pos = retry_list_head))
380 GNUNET_CONTAINER_DLL_remove(retry_list_head, retry_list_tail, pos);
381 if (GNUNET_SCHEDULER_NO_TASK != pos->retry_ctx->retry_task)
383 GNUNET_SCHEDULER_cancel(pos->retry_ctx->retry_task);
385 GNUNET_free(pos->retry_ctx->msg);
386 GNUNET_free(pos->retry_ctx->addr);
387 GNUNET_free(pos->retry_ctx);
391 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
393 GNUNET_SCHEDULER_cancel (plugin->select_task);
394 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
397 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->unix_sock.desc));
398 plugin->unix_sock.desc = NULL;
405 find_session (struct Plugin *plugin,
406 const struct GNUNET_PeerIdentity *peer)
408 struct PeerSession *pos;
410 pos = plugin->sessions;
413 if (memcmp(&pos->target, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
421 /* Forward Declaration */
423 unix_real_send (void *cls,
424 struct RetrySendContext *incoming_retry_context,
425 struct GNUNET_NETWORK_Handle *send_handle,
426 const struct GNUNET_PeerIdentity *target,
429 unsigned int priority,
430 struct GNUNET_TIME_Relative timeout,
433 GNUNET_TRANSPORT_TransmitContinuation cont,
437 * Retry sending a message.
439 * @param cls closure a struct RetrySendContext
440 * @param tc context information
442 void retry_send_message (void *cls,
443 const struct GNUNET_SCHEDULER_TaskContext * tc)
445 struct RetrySendContext *retry_ctx = cls;
447 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
449 GNUNET_free(retry_ctx->msg);
450 GNUNET_free(retry_ctx->addr);
451 GNUNET_free(retry_ctx);
455 unix_real_send (retry_ctx->plugin,
457 retry_ctx->send_handle,
462 GNUNET_TIME_absolute_get_remaining (retry_ctx->timeout),
466 retry_ctx->cont_cls);
471 * Actually send out the message, assume we've got the address and
472 * send_handle squared away!
475 * @param incoming_retry_context the retry context to use
476 * @param send_handle which handle to send message on
477 * @param target who should receive this message (ignored by UNIX)
478 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
479 * @param msgbuf_size the size of the msgbuf to send
480 * @param priority how important is the message (ignored by UNIX)
481 * @param timeout when should we time out (give up) if we can not transmit?
482 * @param addr the addr to send the message to, needs to be a sockaddr for us
483 * @param addrlen the len of addr
484 * @param cont continuation to call once the message has
485 * been transmitted (or if the transport is ready
486 * for the next transmission call; or if the
487 * peer disconnected...)
488 * @param cont_cls closure for cont
490 * @return the number of bytes written, -1 on errors
493 unix_real_send (void *cls,
494 struct RetrySendContext *incoming_retry_context,
495 struct GNUNET_NETWORK_Handle *send_handle,
496 const struct GNUNET_PeerIdentity *target,
499 unsigned int priority,
500 struct GNUNET_TIME_Relative timeout,
503 GNUNET_TRANSPORT_TransmitContinuation cont,
506 struct Plugin *plugin = cls;
507 struct UNIXMessage *message;
508 struct RetrySendContext *retry_ctx;
513 struct sockaddr_un un;
515 struct RetryList *retry_list_entry;
518 if (send_handle == NULL)
521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
522 "unix_real_send with send_handle NULL!\n");
524 /* failed to open send socket for AF */
526 cont (cont_cls, target, GNUNET_SYSERR);
529 if ((addr == NULL) || (addrlen == 0))
532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
533 "unix_real_send called without address, returning!\n");
536 cont (cont_cls, target, GNUNET_SYSERR);
537 return 0; /* Can never send if we don't have an address!! */
540 /* Build the message to be sent */
541 message = GNUNET_malloc (sizeof (struct UNIXMessage) + msgbuf_size);
542 ssize = sizeof (struct UNIXMessage) + msgbuf_size;
544 message->header.size = htons (ssize);
545 message->header.type = htons (0);
546 memcpy (&message->sender, plugin->env->my_identity,
547 sizeof (struct GNUNET_PeerIdentity));
548 memcpy (&message[1], msgbuf, msgbuf_size);
550 memset(&un, 0, sizeof(un));
551 un.sun_family = AF_UNIX;
552 slen = strlen (addr) + 1;
553 if (slen >= sizeof (un.sun_path))
554 slen = sizeof (un.sun_path) - 1;
556 GNUNET_assert(slen < sizeof(un.sun_path));
557 memcpy (un.sun_path, addr, slen);
558 un.sun_path[slen] = '\0';
559 slen = sizeof (struct sockaddr_un);
561 un.sun_path[0] = '\0';
563 #if HAVE_SOCKADDR_IN_SIN_LEN
564 un.sun_len = (u_char) slen;
566 sb = (struct sockaddr*) &un;
570 sent = GNUNET_NETWORK_socket_sendto(send_handle, message, ssize, sb, sbs);
572 if ((GNUNET_SYSERR == sent) && (errno == EAGAIN))
575 if ((GNUNET_SYSERR == sent) && (errno == EMSGSIZE))
578 socklen_t len = sizeof (size);
579 GNUNET_NETWORK_socket_getsockopt ((struct GNUNET_NETWORK_Handle * ) send_handle,
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
588 "Trying to increase socket buffer size from %i to %i for message size %i\n",
590 ((ssize / 1000) + 2) * 1000, ssize);
592 size = ((ssize / 1000) + 2) * 1000;
593 if (GNUNET_NETWORK_socket_setsockopt ((struct GNUNET_NETWORK_Handle * ) send_handle,
596 &size, sizeof(size)) == GNUNET_OK)
599 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
603 if (retry == GNUNET_YES)
605 if (incoming_retry_context == NULL)
607 retry_list_entry = GNUNET_malloc(sizeof(struct RetryList));
608 retry_ctx = GNUNET_malloc(sizeof(struct RetrySendContext));
609 retry_ctx->addr = GNUNET_malloc(addrlen);
610 retry_ctx->msg = GNUNET_malloc(msgbuf_size);
611 retry_ctx->plugin = plugin;
612 memcpy(retry_ctx->addr, addr, addrlen);
613 memcpy(retry_ctx->msg, msgbuf, msgbuf_size);
614 retry_ctx->msg_size = msgbuf_size;
615 retry_ctx->addrlen = addrlen;
616 retry_ctx->send_handle = send_handle;
617 retry_ctx->cont = cont;
618 retry_ctx->cont_cls = cont_cls;
619 retry_ctx->priority = priority;
620 retry_ctx->timeout = GNUNET_TIME_relative_to_absolute(timeout);
621 memcpy(&retry_ctx->target, target, sizeof(struct GNUNET_PeerIdentity));
622 retry_ctx->delay = GNUNET_TIME_UNIT_MILLISECONDS;
623 retry_ctx->retry_list_entry = retry_list_entry;
624 retry_list_entry->retry_ctx = retry_ctx;
625 GNUNET_CONTAINER_DLL_insert(retry_list_head, retry_list_tail, retry_list_entry);
629 retry_ctx = incoming_retry_context;
630 retry_ctx->delay = GNUNET_TIME_relative_multiply(retry_ctx->delay, 2);
632 retry_ctx->retry_task = GNUNET_SCHEDULER_add_delayed(retry_ctx->delay, &retry_send_message, retry_ctx);
634 //GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
635 GNUNET_free(message);
639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
640 "UNIX transmit %u-byte message to %s (%d: %s)\n",
641 (unsigned int) ssize,
642 GNUNET_a2s (sb, sbs),
644 (sent < 0) ? STRERROR (errno) : "ok");
648 if (sent == GNUNET_SYSERR)
649 cont (cont_cls, target, GNUNET_SYSERR);
652 cont (cont_cls, target, GNUNET_OK);
656 if (incoming_retry_context != NULL)
658 GNUNET_CONTAINER_DLL_remove(retry_list_head, retry_list_tail, incoming_retry_context->retry_list_entry);
659 GNUNET_free(incoming_retry_context->retry_list_entry);
660 GNUNET_free(incoming_retry_context->msg);
661 GNUNET_free(incoming_retry_context->addr);
662 GNUNET_free(incoming_retry_context);
665 GNUNET_free (message);
671 * Function that can be used by the transport service to transmit
672 * a message using the plugin.
675 * @param target who should receive this message (ignored by UNIX)
676 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
677 * @param msgbuf_size the size of the msgbuf to send
678 * @param priority how important is the message (ignored by UNIX)
679 * @param timeout when should we time out (give up) if we can not transmit?
680 * @param session identifier used for this session (can be NULL)
681 * @param addr the addr to send the message to, needs to be a sockaddr for us
682 * @param addrlen the len of addr
683 * @param force_address not used, we had better have an address to send to
684 * because we are stateless!!
685 * @param cont continuation to call once the message has
686 * been transmitted (or if the transport is ready
687 * for the next transmission call; or if the
688 * peer disconnected...)
689 * @param cont_cls closure for cont
691 * @return the number of bytes written (may return 0 and the message can
692 * still be transmitted later!)
695 unix_plugin_send (void *cls,
696 const struct GNUNET_PeerIdentity *target,
699 unsigned int priority,
700 struct GNUNET_TIME_Relative timeout,
701 struct Session *session,
705 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
707 struct Plugin *plugin = cls;
710 if (force_address == GNUNET_SYSERR)
711 return GNUNET_SYSERR;
712 GNUNET_assert (NULL == session);
715 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Asked to send message to `%s'\n", (char *)addr);
717 sent = unix_real_send(cls,
719 plugin->unix_sock.desc,
722 priority, timeout, addr, addrlen,
725 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sent %d bytes to `%s'\n", sent, (char *)addr);
727 if (sent == GNUNET_SYSERR)
734 * Demultiplexer for UNIX messages
736 * @param plugin the main plugin for this transport
737 * @param sender from which peer the message was received
738 * @param currhdr pointer to the header of the message
739 * @param un the address from which the message was received
740 * @param fromlen the length of the address
743 unix_demultiplexer(struct Plugin *plugin,
744 struct GNUNET_PeerIdentity *sender,
745 const struct GNUNET_MessageHeader *currhdr,
746 const struct sockaddr_un *un,
749 struct GNUNET_TRANSPORT_ATS_Information distance[2];
751 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
752 distance[0].value = htonl (UNIX_DIRECT_DISTANCE);
753 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
754 distance[1].value = htonl (0);
756 GNUNET_assert(fromlen >= sizeof(struct sockaddr_un));
759 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received message from %s\n", un->sun_path);
761 plugin->env->receive (plugin->env->cls, sender, currhdr,
762 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance, 2,
763 NULL, un->sun_path, strlen(un->sun_path) + 1);
768 * @param cls the plugin handle
769 * @param tc the scheduling context (for rescheduling this function again)
771 * We have been notified that our writeset has something to read. We don't
772 * know which socket needs to be read, so we have to check each one
773 * Then reschedule this function to be called again once more is available.
777 unix_plugin_select (void *cls,
778 const struct GNUNET_SCHEDULER_TaskContext *tc)
780 struct Plugin *plugin = cls;
782 struct UNIXMessage *msg;
783 struct GNUNET_PeerIdentity sender;
784 struct sockaddr_un un;
790 const struct GNUNET_MessageHeader *currhdr;
793 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
794 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
797 addrlen = sizeof(un);
798 memset(&un, 0, sizeof(un));
799 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->unix_sock.desc));
801 GNUNET_NETWORK_socket_recvfrom (plugin->unix_sock.desc, buf, sizeof (buf),
802 (struct sockaddr *)&un, &addrlen);
804 if (ret == GNUNET_SYSERR)
806 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom");
807 plugin->select_task =
808 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
809 GNUNET_SCHEDULER_NO_TASK,
810 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
811 NULL, &unix_plugin_select, plugin);
817 un.sun_path[0] = '/';
820 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Read %d bytes from socket %s\n", ret, &un.sun_path[0]);
824 GNUNET_assert (AF_UNIX == (un.sun_family));
826 msg = (struct UNIXMessage *) buf;
827 csize = ntohs (msg->header.size);
828 if ( (csize < sizeof (struct UNIXMessage)) ||
832 plugin->select_task =
833 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
834 GNUNET_SCHEDULER_NO_TASK,
835 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
836 NULL, &unix_plugin_select, plugin);
839 msgbuf = (char *)&msg[1];
840 memcpy (&sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
842 tsize = csize - sizeof (struct UNIXMessage);
843 while (offset + sizeof (struct GNUNET_MessageHeader) <= tsize)
845 currhdr = (struct GNUNET_MessageHeader *)&msgbuf[offset];
846 csize = ntohs (currhdr->size);
847 if ( (csize < sizeof (struct GNUNET_MessageHeader)) ||
848 (csize > tsize - offset) )
853 unix_demultiplexer(plugin, &sender, currhdr,
857 plugin->select_task =
858 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
859 GNUNET_SCHEDULER_NO_TASK,
860 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
861 NULL, &unix_plugin_select, plugin);
865 * Create a slew of UNIX sockets. If possible, use IPv6 and IPv4.
867 * @param cls closure for server start, should be a struct Plugin *
868 * @return number of sockets created or GNUNET_SYSERR on error
871 unix_transport_server_start (void *cls)
873 struct Plugin *plugin = cls;
874 struct sockaddr *serverAddr;
876 struct sockaddr_un un;
879 memset(&un, 0, sizeof(un));
880 un.sun_family = AF_UNIX;
881 slen = strlen (plugin->unix_socket_path) + 1;
882 if (slen >= sizeof (un.sun_path))
883 slen = sizeof (un.sun_path) - 1;
885 memcpy (un.sun_path, plugin->unix_socket_path, slen);
886 un.sun_path[slen] = '\0';
887 slen = sizeof (struct sockaddr_un);
888 #if HAVE_SOCKADDR_IN_SIN_LEN
889 un.sun_len = (u_char) slen;
892 serverAddr = (struct sockaddr*) &un;
895 un.sun_path[0] = '\0';
898 plugin->unix_sock.desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_DGRAM, 0);
899 if (NULL == plugin->unix_sock.desc)
901 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
902 return GNUNET_SYSERR;
904 if (GNUNET_NETWORK_socket_bind (plugin->unix_sock.desc, serverAddr, addrlen) !=
907 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
908 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc);
909 plugin->unix_sock.desc = NULL;
910 return GNUNET_SYSERR;
913 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
918 plugin->rs = GNUNET_NETWORK_fdset_create ();
919 GNUNET_NETWORK_fdset_zero (plugin->rs);
920 GNUNET_NETWORK_fdset_set (plugin->rs,
921 plugin->unix_sock.desc);
923 plugin->select_task =
924 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
925 GNUNET_SCHEDULER_NO_TASK,
926 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
927 NULL, &unix_plugin_select, plugin);
933 * Function that will be called to check if a binary address for this
934 * plugin is well-formed and corresponds to an address for THIS peer
935 * (as per our configuration). Naturally, if absolutely necessary,
936 * plugins can be a bit conservative in their answer, but in general
937 * plugins should make sure that the address does not redirect
938 * traffic to a 3rd party that might try to man-in-the-middle our
941 * @param cls closure, should be our handle to the Plugin
942 * @param addr pointer to the address
943 * @param addrlen length of addr
944 * @return GNUNET_OK if this is a plausible address for this peer
945 * and transport, GNUNET_SYSERR if not
949 unix_check_address (void *cls,
955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
956 "Informing transport service about my address `%s'\n",
964 * Append our port and forward the result.
967 append_port (void *cls, const char *hostname)
969 struct PrettyPrinterContext *ppc = cls;
972 if (hostname == NULL)
974 ppc->asc (ppc->asc_cls, NULL);
978 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
979 ppc->asc (ppc->asc_cls, ret);
985 * Convert the transports address to a nice, human-readable
989 * @param type name of the transport that generated the address
990 * @param addr one of the addresses of the host, NULL for the last address
991 * the specific address format depends on the transport
992 * @param addrlen length of the address
993 * @param numeric should (IP) addresses be displayed in numeric form?
994 * @param timeout after how long should we give up?
995 * @param asc function to call on each string
996 * @param asc_cls closure for asc
999 unix_plugin_address_pretty_printer (void *cls,
1004 struct GNUNET_TIME_Relative timeout,
1005 GNUNET_TRANSPORT_AddressStringCallback asc,
1008 struct PrettyPrinterContext *ppc;
1011 struct sockaddr_in a4;
1012 struct sockaddr_in6 a6;
1013 const struct IPv4UdpAddress *u4;
1014 const struct IPv6UdpAddress *u6;
1017 if (addrlen == sizeof (struct IPv6UdpAddress))
1020 memset (&a6, 0, sizeof (a6));
1021 a6.sin6_family = AF_INET6;
1022 a6.sin6_port = u6->u6_port;
1023 memcpy (&a6.sin6_addr,
1025 sizeof (struct in6_addr));
1026 port = ntohs (u6->u6_port);
1030 else if (addrlen == sizeof (struct IPv4UdpAddress))
1033 memset (&a4, 0, sizeof (a4));
1034 a4.sin_family = AF_INET;
1035 a4.sin_port = u4->u_port;
1036 a4.sin_addr.s_addr = u4->ipv4_addr;
1037 port = ntohs (u4->u_port);
1043 /* invalid address */
1044 GNUNET_break_op (0);
1045 asc (asc_cls, NULL);
1048 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1050 ppc->asc_cls = asc_cls;
1052 GNUNET_RESOLVER_hostname_get (sb,
1054 !numeric, timeout, &append_port, ppc);
1058 * Function called for a quick conversion of the binary address to
1059 * a numeric address. Note that the caller must not free the
1060 * address and that the next call to this function is allowed
1061 * to override the address again.
1063 * @param cls closure
1064 * @param addr binary address
1065 * @param addrlen length of the address
1066 * @return string representing the same address
1069 unix_address_to_string (void *cls,
1073 if ((addr != NULL) && (addrlen > 0))
1074 return (const char *) addr;
1080 * Notify transport service about address
1082 * @param cls the plugin
1086 address_notification (void *cls,
1087 const struct GNUNET_SCHEDULER_TaskContext *tc)
1089 struct Plugin *plugin = cls;
1090 plugin->env->notify_address(plugin->env->cls,
1092 plugin->unix_socket_path,
1093 strlen(plugin->unix_socket_path) + 1);
1097 * The exported method. Makes the core api available via a global and
1098 * returns the unix transport API.
1101 libgnunet_plugin_transport_unix_init (void *cls)
1103 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1104 unsigned long long port;
1105 struct GNUNET_TRANSPORT_PluginFunctions *api;
1106 struct Plugin *plugin;
1107 int sockets_created;
1110 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1114 port = UNIX_NAT_DEFAULT_PORT;
1115 plugin = GNUNET_malloc (sizeof (struct Plugin));
1116 plugin->port = port;
1118 GNUNET_asprintf (&plugin->unix_socket_path,
1119 "/tmp/unix-plugin-sock.%d",
1122 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1125 api->send = &unix_plugin_send;
1126 api->disconnect = &unix_disconnect;
1127 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1128 api->address_to_string = &unix_address_to_string;
1129 api->check_address = &unix_check_address;
1130 sockets_created = unix_transport_server_start (plugin);
1131 if (sockets_created == 0)
1132 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1133 _("Failed to open UNIX sockets\n"));
1135 GNUNET_SCHEDULER_add_now(address_notification, plugin);
1140 libgnunet_plugin_transport_unix_done (void *cls)
1142 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1143 struct Plugin *plugin = api->cls;
1145 unix_transport_server_stop (plugin);
1147 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1148 GNUNET_free (plugin->unix_socket_path);
1149 GNUNET_free (plugin);
1154 /* end of plugin_transport_unix.c */