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_YES
50 * Transport cost to peer, always 1 for UNIX (direct connection)
52 #define UNIX_DIRECT_DISTANCE 1
54 #define DEFAULT_NAT_PORT 0
57 * How long until we give up on transmitting the welcome message?
59 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
62 * Starting port for listening and sending, eventually a config value
64 #define UNIX_NAT_DEFAULT_PORT 22086
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;
84 * Network format for IPv4 addresses.
89 * IPv4 address, in network byte order.
91 uint32_t ipv4_addr GNUNET_PACKED;
94 * Port number, in network byte order.
96 uint16_t u_port GNUNET_PACKED;
101 * Network format for IPv6 addresses.
103 struct IPv6UdpAddress
108 struct in6_addr ipv6_addr GNUNET_PACKED;
111 * Port number, in network byte order.
113 uint16_t u6_port GNUNET_PACKED;
116 /* Forward definition */
119 struct PrettyPrinterContext
121 GNUNET_TRANSPORT_AddressStringCallback asc;
126 struct RetrySendContext
130 * Main plugin handle.
132 struct Plugin *plugin;
135 * Address of recipient.
150 * Size of the message.
155 * Handle to send message out on.
157 struct GNUNET_NETWORK_Handle *send_handle;
160 * Continuation to call on success or
163 GNUNET_TRANSPORT_TransmitContinuation cont;
166 * Closure for continuation.
171 * The peer the message is destined for.
173 struct GNUNET_PeerIdentity target;
176 * How long before not retrying any longer.
178 struct GNUNET_TIME_Absolute timeout;
181 * How long the last message was delayed.
183 struct GNUNET_TIME_Relative delay;
186 * The actual retry task.
188 GNUNET_SCHEDULER_TaskIdentifier retry_task;
191 * The priority of the message.
193 unsigned int priority;
197 * Local network addresses (actual unix path follows).
203 * This is a doubly linked list.
205 struct LocalAddrList *next;
208 * This is a doubly linked list.
210 struct LocalAddrList *prev;
213 * Number of bytes of the address that follow
227 * Stored in a linked list.
229 struct PeerSession *next;
232 * Pointer to the global plugin struct.
234 struct Plugin *plugin;
237 * To whom are we talking to (set to our identity
238 * if we are still waiting for the welcome message)
240 struct GNUNET_PeerIdentity target;
243 * Address of the other peer (either based on our 'connect'
244 * call or on our 'accept' call).
249 * Length of connect_addr.
254 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
256 int expecting_welcome;
259 * From which socket do we need to send to this peer?
261 struct GNUNET_NETWORK_Handle *sock;
264 * Queue of messages for this peer, in the case that
265 * we have to await a connection...
267 struct MessageQueue *messages;
272 * Information we keep for each of our listen sockets.
274 struct UNIX_Sock_Info
279 struct GNUNET_NETWORK_Handle *desc;
282 * The port we bound to
289 * Encapsulation of all of the state of the plugin.
296 struct GNUNET_TRANSPORT_PluginEnvironment *env;
299 * Session of peers with whom we are currently connected
301 struct PeerSession *sessions;
304 * ID of task used to update our addresses when one expires.
306 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
311 GNUNET_SCHEDULER_TaskIdentifier select_task;
314 * Integer to append to unix domain socket.
319 * List of our IP addresses.
321 struct LocalAddrList *lal_head;
324 * Tail of our IP address list.
326 struct LocalAddrList *lal_tail;
331 struct GNUNET_NETWORK_FDSet *rs;
334 * socket that we transmit all data with
336 struct UNIX_Sock_Info unix_sock;
339 * Path of our unix domain socket (/tmp/unix-plugin-PORT)
341 char *unix_socket_path;
347 * Disconnect from a remote node. Clean up session if we have one for this peer
349 * @param cls closure for this call (should be handle to Plugin)
350 * @param target the peeridentity of the peer to disconnect
351 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
354 unix_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
356 /** TODO: Implement! */
361 * Shutdown the server process (stop receiving inbound traffic). Maybe
364 * @param cls Handle to the plugin for this transport
366 * @return returns the number of sockets successfully closed,
367 * should equal the number of sockets successfully opened
370 unix_transport_server_stop (void *cls)
372 struct Plugin *plugin = cls;
374 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
376 GNUNET_SCHEDULER_cancel (plugin->select_task);
377 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
380 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->unix_sock.desc));
381 plugin->unix_sock.desc = NULL;
388 find_session (struct Plugin *plugin,
389 const struct GNUNET_PeerIdentity *peer)
391 struct PeerSession *pos;
393 pos = plugin->sessions;
396 if (memcmp(&pos->target, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
404 /* Forward Declaration */
406 unix_real_send (void *cls,
407 struct RetrySendContext *incoming_retry_context,
408 struct GNUNET_NETWORK_Handle *send_handle,
409 const struct GNUNET_PeerIdentity *target,
412 unsigned int priority,
413 struct GNUNET_TIME_Relative timeout,
416 GNUNET_TRANSPORT_TransmitContinuation cont,
420 * Retry sending a message.
422 * @param cls closure a struct RetrySendContext
423 * @param tc context information
425 void retry_send_message (void *cls,
426 const struct GNUNET_SCHEDULER_TaskContext * tc)
428 struct RetrySendContext *retry_ctx = cls;
430 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
432 unix_real_send (retry_ctx->plugin,
434 retry_ctx->send_handle,
439 GNUNET_TIME_absolute_get_remaining (retry_ctx->timeout),
443 retry_ctx->cont_cls);
448 * Actually send out the message, assume we've got the address and
449 * send_handle squared away!
452 * @param incoming_retry_context the retry context to use
453 * @param send_handle which handle to send message on
454 * @param target who should receive this message (ignored by UNIX)
455 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
456 * @param msgbuf_size the size of the msgbuf to send
457 * @param priority how important is the message (ignored by UNIX)
458 * @param timeout when should we time out (give up) if we can not transmit?
459 * @param addr the addr to send the message to, needs to be a sockaddr for us
460 * @param addrlen the len of addr
461 * @param cont continuation to call once the message has
462 * been transmitted (or if the transport is ready
463 * for the next transmission call; or if the
464 * peer disconnected...)
465 * @param cont_cls closure for cont
467 * @return the number of bytes written, -1 on errors
470 unix_real_send (void *cls,
471 struct RetrySendContext *incoming_retry_context,
472 struct GNUNET_NETWORK_Handle *send_handle,
473 const struct GNUNET_PeerIdentity *target,
476 unsigned int priority,
477 struct GNUNET_TIME_Relative timeout,
480 GNUNET_TRANSPORT_TransmitContinuation cont,
483 struct Plugin *plugin = cls;
484 struct UNIXMessage *message;
485 struct RetrySendContext *retry_ctx;
490 struct sockaddr_un *un;
493 if (send_handle == NULL)
496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
497 "unix_real_send with send_handle NULL!\n");
499 /* failed to open send socket for AF */
501 cont (cont_cls, target, GNUNET_SYSERR);
504 if ((addr == NULL) || (addrlen == 0))
507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
508 "unix_real_send called without address, returning!\n");
511 cont (cont_cls, target, GNUNET_SYSERR);
512 return 0; /* Can never send if we don't have an address!! */
515 /* Build the message to be sent */
516 message = GNUNET_malloc (sizeof (struct UNIXMessage) + msgbuf_size);
517 ssize = sizeof (struct UNIXMessage) + msgbuf_size;
519 message->header.size = htons (ssize);
520 message->header.type = htons (0);
521 memcpy (&message->sender, plugin->env->my_identity,
522 sizeof (struct GNUNET_PeerIdentity));
523 memcpy (&message[1], msgbuf, msgbuf_size);
525 un = GNUNET_malloc (sizeof (struct sockaddr_un));
526 un->sun_family = AF_UNIX;
527 slen = strlen (addr) + 1;
529 GNUNET_assert(slen < sizeof(un->sun_path));
530 memcpy (un->sun_path, addr, slen);
531 un->sun_path[slen] = '\0';
533 un->sun_path[0] = '\0';
535 slen += sizeof (sa_family_t);
536 sb = (struct sockaddr*) un;
539 sent = GNUNET_NETWORK_socket_sendto(send_handle, message, ssize, sb, sbs);
541 if (GNUNET_SYSERR == sent)
543 if (incoming_retry_context == NULL)
545 retry_ctx = GNUNET_malloc(sizeof(struct RetrySendContext));
546 retry_ctx->addr = GNUNET_malloc(addrlen);
547 retry_ctx->msg = GNUNET_malloc(msgbuf_size);
548 retry_ctx->plugin = plugin;
549 memcpy(retry_ctx->addr, addr, addrlen);
550 memcpy(retry_ctx->msg, msgbuf, msgbuf_size);
551 retry_ctx->msg_size = msgbuf_size;
552 retry_ctx->addrlen = addrlen;
553 retry_ctx->send_handle = send_handle;
554 retry_ctx->cont = cont;
555 retry_ctx->cont_cls = cont_cls;
556 retry_ctx->priority = priority;
557 retry_ctx->timeout = GNUNET_TIME_relative_to_absolute(timeout);
558 memcpy(&retry_ctx->target, target, sizeof(struct GNUNET_PeerIdentity));
559 retry_ctx->delay = GNUNET_TIME_UNIT_MILLISECONDS;
563 retry_ctx = incoming_retry_context;
564 retry_ctx->delay = GNUNET_TIME_relative_multiply(retry_ctx->delay, 2);
566 retry_ctx->retry_task = GNUNET_SCHEDULER_add_delayed(retry_ctx->delay, &retry_send_message, retry_ctx);
568 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Error when trying to send %d byte message to %s\n", retry_ctx->msg_size, &un->sun_path[1]);
569 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
570 "UNIX transmit %u-byte message to %s (%d: %s)\n",
571 (unsigned int) ssize,
572 GNUNET_a2s (sb, sbs),
574 (sent < 0) ? STRERROR (errno) : "ok");
576 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
579 if (incoming_retry_context != NULL)
581 GNUNET_free(incoming_retry_context->msg);
582 GNUNET_free(incoming_retry_context->addr);
583 GNUNET_free(incoming_retry_context);
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
587 "UNIX transmit %u-byte message to %s (%d: %s)\n",
588 (unsigned int) ssize,
589 GNUNET_a2s (sb, sbs),
591 (sent < 0) ? STRERROR (errno) : "ok");
595 if (sent == GNUNET_SYSERR)
596 cont (cont_cls, target, GNUNET_SYSERR);
599 cont (cont_cls, target, GNUNET_OK);
603 GNUNET_free (message);
609 * Function that can be used by the transport service to transmit
610 * a message using the plugin.
613 * @param target who should receive this message (ignored by UNIX)
614 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
615 * @param msgbuf_size the size of the msgbuf to send
616 * @param priority how important is the message (ignored by UNIX)
617 * @param timeout when should we time out (give up) if we can not transmit?
618 * @param session identifier used for this session (can be NULL)
619 * @param addr the addr to send the message to, needs to be a sockaddr for us
620 * @param addrlen the len of addr
621 * @param force_address not used, we had better have an address to send to
622 * because we are stateless!!
623 * @param cont continuation to call once the message has
624 * been transmitted (or if the transport is ready
625 * for the next transmission call; or if the
626 * peer disconnected...)
627 * @param cont_cls closure for cont
629 * @return the number of bytes written (may return 0 and the message can
630 * still be transmitted later!)
633 unix_plugin_send (void *cls,
634 const struct GNUNET_PeerIdentity *target,
637 unsigned int priority,
638 struct GNUNET_TIME_Relative timeout,
639 struct Session *session,
643 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
645 struct Plugin *plugin = cls;
648 if (force_address == GNUNET_SYSERR)
649 return GNUNET_SYSERR;
650 GNUNET_assert (NULL == session);
653 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Asked to send message to `%s'\n", (char *)addr);
655 sent = unix_real_send(cls,
657 plugin->unix_sock.desc,
660 priority, timeout, addr, addrlen,
663 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sent %d bytes to `%s'\n", sent, (char *)addr);
665 if (sent == GNUNET_SYSERR)
672 add_to_address_list (struct Plugin *plugin,
676 struct LocalAddrList *lal;
678 lal = plugin->lal_head;
681 if ( (lal->size == arg_size) &&
682 (0 == memcmp (&lal[1], arg, arg_size)) )
686 lal = GNUNET_malloc (sizeof (struct LocalAddrList) + arg_size);
687 lal->size = arg_size;
688 memcpy (&lal[1], arg, arg_size);
689 GNUNET_CONTAINER_DLL_insert (plugin->lal_head,
696 * Demultiplexer for UNIX messages
698 * @param plugin the main plugin for this transport
699 * @param sender from which peer the message was received
700 * @param currhdr pointer to the header of the message
701 * @param un the address from which the message was received
702 * @param fromlen the length of the address
705 unix_demultiplexer(struct Plugin *plugin,
706 struct GNUNET_PeerIdentity *sender,
707 const struct GNUNET_MessageHeader *currhdr,
708 const struct sockaddr_un *un,
711 struct GNUNET_TRANSPORT_ATS_Information distance[2];
713 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
714 distance[0].value = htonl (UNIX_DIRECT_DISTANCE);
715 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
716 distance[1].value = htonl (0);
718 GNUNET_assert(fromlen >= sizeof(struct sockaddr_un));
721 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received message from %s\n", un->sun_path);
723 plugin->env->receive (plugin->env->cls, sender, currhdr,
724 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance, 2,
725 NULL, un->sun_path, strlen(un->sun_path) + 1);
730 * @param cls the plugin handle
731 * @param tc the scheduling context (for rescheduling this function again)
733 * We have been notified that our writeset has something to read. We don't
734 * know which socket needs to be read, so we have to check each one
735 * Then reschedule this function to be called again once more is available.
739 unix_plugin_select (void *cls,
740 const struct GNUNET_SCHEDULER_TaskContext *tc)
742 struct Plugin *plugin = cls;
744 struct UNIXMessage *msg;
745 struct GNUNET_PeerIdentity sender;
746 struct sockaddr_un un;
752 const struct GNUNET_MessageHeader *currhdr;
755 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
756 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
759 addrlen = sizeof(un);
760 memset(&un, 0, sizeof(un));
761 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->unix_sock.desc));
763 GNUNET_NETWORK_socket_recvfrom (plugin->unix_sock.desc, buf, sizeof (buf),
764 (struct sockaddr *)&un, &addrlen);
766 if (ret == GNUNET_SYSERR)
768 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom");
769 plugin->select_task =
770 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
771 GNUNET_SCHEDULER_NO_TASK,
772 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
773 NULL, &unix_plugin_select, plugin);
779 un.sun_path[0] = '/';
782 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Read %d bytes from socket %s\n", ret, &un.sun_path[0]);
786 GNUNET_assert (AF_UNIX == (un.sun_family));
788 msg = (struct UNIXMessage *) buf;
789 csize = ntohs (msg->header.size);
790 if ( (csize < sizeof (struct UNIXMessage)) ||
794 plugin->select_task =
795 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
796 GNUNET_SCHEDULER_NO_TASK,
797 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
798 NULL, &unix_plugin_select, plugin);
801 msgbuf = (char *)&msg[1];
802 memcpy (&sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
804 tsize = csize - sizeof (struct UNIXMessage);
805 while (offset + sizeof (struct GNUNET_MessageHeader) <= tsize)
807 currhdr = (struct GNUNET_MessageHeader *)&msgbuf[offset];
808 csize = ntohs (currhdr->size);
809 if ( (csize < sizeof (struct GNUNET_MessageHeader)) ||
810 (csize > tsize - offset) )
815 unix_demultiplexer(plugin, &sender, currhdr,
819 plugin->select_task =
820 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
821 GNUNET_SCHEDULER_NO_TASK,
822 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
823 NULL, &unix_plugin_select, plugin);
827 * Create a slew of UNIX sockets. If possible, use IPv6 and IPv4.
829 * @param cls closure for server start, should be a struct Plugin *
830 * @return number of sockets created or GNUNET_SYSERR on error
833 unix_transport_server_start (void *cls)
835 struct Plugin *plugin = cls;
837 struct sockaddr *serverAddr;
840 struct sockaddr_un *un;
843 un = GNUNET_malloc (sizeof (struct sockaddr_un));
844 un->sun_family = AF_UNIX;
845 slen = strlen (plugin->unix_socket_path) + 1;
847 GNUNET_assert(slen < sizeof(un->sun_path));
848 memcpy (un->sun_path, plugin->unix_socket_path, slen);
849 un->sun_path[slen] = '\0';
850 slen += sizeof (sa_family_t);
851 serverAddr = (struct sockaddr*) un;
855 un->sun_path[0] = '\0';
858 plugin->unix_sock.desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_DGRAM, 0);
859 if (NULL == plugin->unix_sock.desc)
861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "socket");
865 if (GNUNET_NETWORK_socket_bind (plugin->unix_sock.desc, serverAddr, addrlen) !=
869 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
870 "UNIX Binding failed!\n");
874 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Bound to `%s'\n", &un->sun_path[0]);
875 if (plugin->unix_sock.desc != NULL)
879 plugin->rs = GNUNET_NETWORK_fdset_create ();
880 GNUNET_NETWORK_fdset_zero (plugin->rs);
881 GNUNET_NETWORK_fdset_set (plugin->rs,
882 plugin->unix_sock.desc);
884 plugin->select_task =
885 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
886 GNUNET_SCHEDULER_NO_TASK,
887 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
888 NULL, &unix_plugin_select, plugin);
889 return sockets_created;
894 * Function that will be called to check if a binary address for this
895 * plugin is well-formed and corresponds to an address for THIS peer
896 * (as per our configuration). Naturally, if absolutely necessary,
897 * plugins can be a bit conservative in their answer, but in general
898 * plugins should make sure that the address does not redirect
899 * traffic to a 3rd party that might try to man-in-the-middle our
902 * @param cls closure, should be our handle to the Plugin
903 * @param addr pointer to the address
904 * @param addrlen length of addr
905 * @return GNUNET_OK if this is a plausible address for this peer
906 * and transport, GNUNET_SYSERR if not
910 unix_check_address (void *cls,
916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
917 "Informing transport service about my address `%s'\n",
925 * Append our port and forward the result.
928 append_port (void *cls, const char *hostname)
930 struct PrettyPrinterContext *ppc = cls;
933 if (hostname == NULL)
935 ppc->asc (ppc->asc_cls, NULL);
939 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
940 ppc->asc (ppc->asc_cls, ret);
946 * Convert the transports address to a nice, human-readable
950 * @param type name of the transport that generated the address
951 * @param addr one of the addresses of the host, NULL for the last address
952 * the specific address format depends on the transport
953 * @param addrlen length of the address
954 * @param numeric should (IP) addresses be displayed in numeric form?
955 * @param timeout after how long should we give up?
956 * @param asc function to call on each string
957 * @param asc_cls closure for asc
960 unix_plugin_address_pretty_printer (void *cls,
965 struct GNUNET_TIME_Relative timeout,
966 GNUNET_TRANSPORT_AddressStringCallback asc,
969 struct Plugin *plugin = cls;
970 struct PrettyPrinterContext *ppc;
973 struct sockaddr_in a4;
974 struct sockaddr_in6 a6;
975 const struct IPv4UdpAddress *u4;
976 const struct IPv6UdpAddress *u6;
979 if (addrlen == sizeof (struct IPv6UdpAddress))
982 memset (&a6, 0, sizeof (a6));
983 a6.sin6_family = AF_INET6;
984 a6.sin6_port = u6->u6_port;
985 memcpy (&a6.sin6_addr,
987 sizeof (struct in6_addr));
988 port = ntohs (u6->u6_port);
992 else if (addrlen == sizeof (struct IPv4UdpAddress))
995 memset (&a4, 0, sizeof (a4));
996 a4.sin_family = AF_INET;
997 a4.sin_port = u4->u_port;
998 a4.sin_addr.s_addr = u4->ipv4_addr;
999 port = ntohs (u4->u_port);
1005 /* invalid address */
1006 GNUNET_break_op (0);
1007 asc (asc_cls, NULL);
1010 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1012 ppc->asc_cls = asc_cls;
1014 GNUNET_RESOLVER_hostname_get (plugin->env->cfg,
1017 !numeric, timeout, &append_port, ppc);
1021 * Function called for a quick conversion of the binary address to
1022 * a numeric address. Note that the caller must not free the
1023 * address and that the next call to this function is allowed
1024 * to override the address again.
1026 * @param cls closure
1027 * @param addr binary address
1028 * @param addrlen length of the address
1029 * @return string representing the same address
1032 unix_address_to_string (void *cls,
1036 static char rbuf[INET6_ADDRSTRLEN + 10];
1037 char buf[INET6_ADDRSTRLEN];
1041 const struct IPv4UdpAddress *t4;
1042 const struct IPv6UdpAddress *t6;
1046 if (addrlen == sizeof (struct IPv6UdpAddress))
1050 port = ntohs (t6->u6_port);
1051 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
1054 else if (addrlen == sizeof (struct IPv4UdpAddress))
1058 port = ntohs (t4->u_port);
1059 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
1064 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
1065 GNUNET_snprintf (rbuf,
1074 * The exported method. Makes the core api available via a global and
1075 * returns the unix transport API.
1078 libgnunet_plugin_transport_unix_init (void *cls)
1080 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1081 unsigned long long port;
1082 struct GNUNET_TRANSPORT_PluginFunctions *api;
1083 struct Plugin *plugin;
1084 int sockets_created;
1087 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1091 port = UNIX_NAT_DEFAULT_PORT;
1092 else if (port > 65535)
1094 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1095 _("Given `%s' option is out of range: %llu > %u\n"),
1103 plugin = GNUNET_malloc (sizeof (struct Plugin));
1104 plugin->port = port;
1106 GNUNET_asprintf(&plugin->unix_socket_path, "/tmp/unix-plugin-sock.%d", plugin->port);
1108 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1111 api->send = &unix_plugin_send;
1112 api->disconnect = &unix_disconnect;
1113 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1114 api->address_to_string = &unix_address_to_string;
1115 api->check_address = &unix_check_address;
1117 add_to_address_list (plugin, plugin->unix_socket_path, strlen(plugin->unix_socket_path) + 1);
1119 sockets_created = unix_transport_server_start (plugin);
1120 if (sockets_created == 0)
1121 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1122 _("Failed to open UNIX sockets\n"));
1124 plugin->env->notify_address(plugin->env->cls,
1126 plugin->unix_socket_path,
1127 strlen(plugin->unix_socket_path) + 1,
1128 GNUNET_TIME_UNIT_FOREVER_REL);
1133 libgnunet_plugin_transport_unix_done (void *cls)
1135 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1136 struct Plugin *plugin = api->cls;
1137 struct LocalAddrList *lal;
1139 unix_transport_server_stop (plugin);
1141 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1142 while (NULL != (lal = plugin->lal_head))
1144 GNUNET_CONTAINER_DLL_remove (plugin->lal_head,
1149 GNUNET_free (plugin);
1154 /* end of plugin_transport_unix.c */