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_EXTRALOGGING
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
69 GNUNET_NETWORK_STRUCT_BEGIN
72 * UNIX Message-Packet header.
79 struct GNUNET_MessageHeader header;
82 * What is the identity of the sender (GNUNET_hash of public key)
84 struct GNUNET_PeerIdentity sender;
92 struct GNUNET_PeerIdentity target;
95 struct UNIXMessageWrapper
97 struct UNIXMessageWrapper *next;
98 struct UNIXMessageWrapper *prev;
100 struct UNIXMessage * msg;
105 struct GNUNET_TIME_Relative timeout;
106 unsigned int priority;
108 struct Session *session;
109 GNUNET_TRANSPORT_TransmitContinuation cont;
113 /* Forward definition */
124 * Stored in a linked list.
126 struct PeerSession *next;
129 * Pointer to the global plugin struct.
131 struct Plugin *plugin;
134 * To whom are we talking to (set to our identity
135 * if we are still waiting for the welcome message)
137 struct GNUNET_PeerIdentity target;
140 * Address of the other peer (either based on our 'connect'
141 * call or on our 'accept' call).
146 * Length of connect_addr.
151 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
153 int expecting_welcome;
156 * From which socket do we need to send to this peer?
158 struct GNUNET_NETWORK_Handle *sock;
161 * Queue of messages for this peer, in the case that
162 * we have to await a connection...
164 struct MessageQueue *messages;
169 * Information we keep for each of our listen sockets.
171 struct UNIX_Sock_Info
176 struct GNUNET_NETWORK_Handle *desc;
179 * The port we bound to
186 * Encapsulation of all of the state of the plugin.
193 struct GNUNET_TRANSPORT_PluginEnvironment *env;
196 * Session of peers with whom we are currently connected
198 struct PeerSession *sessions;
203 struct GNUNET_CONTAINER_MultiHashMap *session_map;
206 * ID of task used to update our addresses when one expires.
208 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
213 GNUNET_SCHEDULER_TaskIdentifier select_task;
216 * Integer to append to unix domain socket.
223 struct GNUNET_NETWORK_FDSet *rs;
228 struct GNUNET_NETWORK_FDSet *ws;
231 * socket that we transmit all data with
233 struct UNIX_Sock_Info unix_sock;
236 * Path of our unix domain socket (/tmp/unix-plugin-PORT)
238 char *unix_socket_path;
240 struct UNIXMessageWrapper *msg_head;
241 struct UNIXMessageWrapper *msg_tail;
246 struct GNUNET_ATS_Information ats_network;
251 get_session_delete_it (void *cls, const GNUNET_HashCode * key, void *value)
253 struct Session *s = value;
254 struct Plugin *plugin = cls;
255 GNUNET_assert (plugin != NULL);
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting session for peer `%s' `%s' \n", GNUNET_i2s (&s->target), s->addr);
261 plugin->env->session_end (plugin->env->cls, &s->target, s);
263 GNUNET_assert (GNUNET_YES ==
264 GNUNET_CONTAINER_multihashmap_remove(plugin->session_map, &s->target.hashPubKey, s));
272 * Disconnect from a remote node. Clean up session if we have one for this peer
274 * @param cls closure for this call (should be handle to Plugin)
275 * @param target the peeridentity of the peer to disconnect
276 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
279 unix_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
281 struct Plugin *plugin = cls;
282 GNUNET_assert (plugin != NULL);
284 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map, &target->hashPubKey, &get_session_delete_it, plugin);
289 * Shutdown the server process (stop receiving inbound traffic). Maybe
292 * @param cls Handle to the plugin for this transport
294 * @return returns the number of sockets successfully closed,
295 * should equal the number of sockets successfully opened
298 unix_transport_server_stop (void *cls)
300 struct Plugin *plugin = cls;
302 struct UNIXMessageWrapper * msgw = plugin->msg_head;
304 while (NULL != (msgw = plugin->msg_head))
306 GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
307 if (msgw->cont != NULL)
308 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR);
309 GNUNET_free (msgw->msg);
313 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
315 GNUNET_SCHEDULER_cancel (plugin->select_task);
316 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
319 GNUNET_break (GNUNET_OK ==
320 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc));
321 plugin->unix_sock.desc = NULL;
328 find_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *peer)
330 struct PeerSession *pos;
332 pos = plugin->sessions;
335 if (memcmp (&pos->target, peer, sizeof (struct GNUNET_PeerIdentity)) == 0)
345 * Actually send out the message, assume we've got the address and
346 * send_handle squared away!
349 * @param incoming_retry_context the retry context to use
350 * @param send_handle which handle to send message on
351 * @param target who should receive this message (ignored by UNIX)
352 * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
353 * @param msgbuf_size the size of the msgbuf to send
354 * @param priority how important is the message (ignored by UNIX)
355 * @param timeout when should we time out (give up) if we can not transmit?
356 * @param addr the addr to send the message to, needs to be a sockaddr for us
357 * @param addrlen the len of addr
358 * @param cont continuation to call once the message has
359 * been transmitted (or if the transport is ready
360 * for the next transmission call; or if the
361 * peer disconnected...)
362 * @param cont_cls closure for cont
364 * @return the number of bytes written, -1 on errors
367 unix_real_send (void *cls,
368 struct GNUNET_NETWORK_Handle *send_handle,
369 const struct GNUNET_PeerIdentity *target, const char *msgbuf,
370 size_t msgbuf_size, unsigned int priority,
371 struct GNUNET_TIME_Relative timeout, const void *addr,
372 size_t addrlen, GNUNET_TRANSPORT_TransmitContinuation cont,
379 struct sockaddr_un un;
383 if (send_handle == NULL)
386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
387 "unix_real_send with send_handle NULL!\n");
389 /* failed to open send socket for AF */
391 cont (cont_cls, target, GNUNET_SYSERR);
394 if ((addr == NULL) || (addrlen == 0))
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
398 "unix_real_send called without address, returning!\n");
401 cont (cont_cls, target, GNUNET_SYSERR);
402 return 0; /* Can never send if we don't have an address!! */
405 memset (&un, 0, sizeof (un));
406 un.sun_family = AF_UNIX;
407 slen = strlen (addr) + 1;
408 if (slen >= sizeof (un.sun_path))
409 slen = sizeof (un.sun_path) - 1;
411 GNUNET_assert (slen < sizeof (un.sun_path));
412 memcpy (un.sun_path, addr, slen);
413 un.sun_path[slen] = '\0';
414 slen = sizeof (struct sockaddr_un);
416 un.sun_path[0] = '\0';
418 #if HAVE_SOCKADDR_IN_SIN_LEN
419 un.sun_len = (u_char) slen;
421 sb = (struct sockaddr *) &un;
424 sent = GNUNET_NETWORK_socket_sendto (send_handle, msgbuf, msgbuf_size, sb, sbs);
426 if ((GNUNET_SYSERR == sent) && ((errno == EAGAIN) || (errno == ENOBUFS)))
429 if ((GNUNET_SYSERR == sent) && (errno == EMSGSIZE))
432 socklen_t len = sizeof (size);
434 GNUNET_NETWORK_socket_getsockopt ((struct GNUNET_NETWORK_Handle *)
435 send_handle, SOL_SOCKET, SO_SNDBUF, &size,
438 if (size < msgbuf_size)
441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
442 "Trying to increase socket buffer size from %i to %i for message size %i\n",
443 size, ((msgbuf_size / 1000) + 2) * 1000, msgbuf_size);
445 size = ((msgbuf_size / 1000) + 2) * 1000;
446 if (GNUNET_NETWORK_socket_setsockopt
447 ((struct GNUNET_NETWORK_Handle *) send_handle, SOL_SOCKET, SO_SNDBUF,
448 &size, sizeof (size)) == GNUNET_OK)
451 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457 "UNIX transmit %u-byte message to %s (%d: %s)\n",
458 (unsigned int) msgbuf_size, GNUNET_a2s (sb, sbs), (int) sent,
459 (sent < 0) ? STRERROR (errno) : "ok");
461 /* Calling continuation */
464 if ((sent == GNUNET_SYSERR) && (retry == GNUNET_NO))
465 cont (cont_cls, target, GNUNET_SYSERR);
467 cont (cont_cls, target, GNUNET_OK);
470 /* return number of bytes successfully sent */
473 /* failed and retry: return 0 */
474 if ((GNUNET_SYSERR == sent) && (retry == GNUNET_YES))
476 /* failed and no retry: return -1 */
477 if ((GNUNET_SYSERR == sent) && (retry == GNUNET_NO))
491 get_session_it (void *cls, const GNUNET_HashCode * key, void *value)
493 struct gsi_ctx *gsi = cls;
494 struct Session *s = value;
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing session %s %s\n", gsi->address, s->addr);
499 if ((gsi->addrlen == s->addrlen) &&
500 (0 == memcmp (gsi->address, s->addr, s->addrlen)))
509 * Creates a new outbound session the transport service will use to send data to the
512 * @param cls the plugin
513 * @param address the address
514 * @return the session or NULL of max connections exceeded
516 static struct Session *
517 unix_plugin_get_session (void *cls,
518 const struct GNUNET_HELLO_Address *address)
520 struct Session * s = NULL;
521 struct Plugin *plugin = cls;
525 GNUNET_assert (plugin != NULL);
526 GNUNET_assert (address != NULL);
528 /* Check if already existing */
529 gsi.address = (char *) address->address;
530 gsi.addrlen = address->address_length;
532 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map, &address->peer.hashPubKey, &get_session_it, &gsi);
536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found existing session\n");
541 /* Create a new session */
543 s = GNUNET_malloc (sizeof (struct Session) + address->address_length);
545 s->addrlen = address->address_length;
546 memcpy(s->addr, address->address, s->addrlen);
547 memcpy(&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
549 GNUNET_CONTAINER_multihashmap_put (plugin->session_map,
550 &address->peer.hashPubKey, s,
551 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating new session\n");
560 * Function that can be used by the transport service to transmit
561 * a message using the plugin. Note that in the case of a
562 * peer disconnecting, the continuation MUST be called
563 * prior to the disconnect notification itself. This function
564 * will be called with this peer's HELLO message to initiate
565 * a fresh connection to another peer.
568 * @param session which session must be used
569 * @param msgbuf the message to transmit
570 * @param msgbuf_size number of bytes in 'msgbuf'
571 * @param priority how important is the message (most plugins will
572 * ignore message priority and just FIFO)
573 * @param to how long to wait at most for the transmission (does not
574 * require plugins to discard the message after the timeout,
575 * just advisory for the desired delay; most plugins will ignore
577 * @param cont continuation to call once the message has
578 * been transmitted (or if the transport is ready
579 * for the next transmission call; or if the
580 * peer disconnected...); can be NULL
581 * @param cont_cls closure for cont
582 * @return number of bytes used (on the physical network, with overheads);
583 * -1 on hard errors (i.e. address invalid); 0 is a legal value
584 * and does NOT mean that the message was not transmitted (DV)
587 unix_plugin_send (void *cls,
588 struct Session *session,
589 const char *msgbuf, size_t msgbuf_size,
590 unsigned int priority,
591 struct GNUNET_TIME_Relative to,
592 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
594 struct Plugin *plugin = cls;
595 struct UNIXMessageWrapper *wrapper;
596 struct UNIXMessage *message;
599 GNUNET_assert (plugin != NULL);
600 GNUNET_assert (session != NULL);
602 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_contains_value(plugin->session_map,
603 &session->target.hashPubKey, session))
606 return GNUNET_SYSERR;
609 ssize = sizeof (struct UNIXMessage) + msgbuf_size;
610 message = GNUNET_malloc (sizeof (struct UNIXMessage) + msgbuf_size);
611 message->header.size = htons (ssize);
612 message->header.type = htons (0);
613 memcpy (&message->sender, plugin->env->my_identity,
614 sizeof (struct GNUNET_PeerIdentity));
615 memcpy (&message[1], msgbuf, msgbuf_size);
617 wrapper = GNUNET_malloc (sizeof (struct UNIXMessageWrapper));
618 wrapper->msg = message;
619 wrapper->msgsize = ssize;
620 wrapper->priority = priority;
621 wrapper->timeout = to;
622 wrapper->cont = cont;
623 wrapper->cont_cls = cont_cls;
624 wrapper->retry_counter = 0;
625 wrapper->session = session;
627 GNUNET_CONTAINER_DLL_insert(plugin->msg_head, plugin->msg_tail, wrapper);
630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent %d bytes to `%s'\n", ssize,
631 (char *) session->addr);
639 * Demultiplexer for UNIX messages
641 * @param plugin the main plugin for this transport
642 * @param sender from which peer the message was received
643 * @param currhdr pointer to the header of the message
644 * @param un the address from which the message was received
645 * @param fromlen the length of the address
648 unix_demultiplexer (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender,
649 const struct GNUNET_MessageHeader *currhdr,
650 const struct sockaddr_un *un, size_t fromlen)
652 struct GNUNET_ATS_Information ats[2];
654 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
655 ats[0].value = htonl (UNIX_DIRECT_DISTANCE);
656 ats[1] = plugin->ats_network;
657 GNUNET_break (ntohl(plugin->ats_network.value) != GNUNET_ATS_NET_UNSPECIFIED);
659 GNUNET_assert (fromlen >= sizeof (struct sockaddr_un));
662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from %s\n",
665 plugin->env->receive (plugin->env->cls, sender, currhdr,
666 (const struct GNUNET_ATS_Information *) &ats, 2,
667 NULL, un->sun_path, strlen (un->sun_path) + 1);
672 unix_plugin_select_read (struct Plugin * plugin)
675 struct UNIXMessage *msg;
676 struct GNUNET_PeerIdentity sender;
677 struct sockaddr_un un;
683 const struct GNUNET_MessageHeader *currhdr;
686 addrlen = sizeof (un);
687 memset (&un, 0, sizeof (un));
690 GNUNET_NETWORK_socket_recvfrom (plugin->unix_sock.desc, buf, sizeof (buf),
691 (struct sockaddr *) &un, &addrlen);
693 if (ret == GNUNET_SYSERR)
695 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom");
701 un.sun_path[0] = '/';
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Read %d bytes from socket %s\n", ret,
709 GNUNET_assert (AF_UNIX == (un.sun_family));
711 msg = (struct UNIXMessage *) buf;
712 csize = ntohs (msg->header.size);
713 if ((csize < sizeof (struct UNIXMessage)) || (csize > ret))
718 msgbuf = (char *) &msg[1];
719 memcpy (&sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
721 tsize = csize - sizeof (struct UNIXMessage);
722 while (offset + sizeof (struct GNUNET_MessageHeader) <= tsize)
724 currhdr = (struct GNUNET_MessageHeader *) &msgbuf[offset];
725 csize = ntohs (currhdr->size);
726 if ((csize < sizeof (struct GNUNET_MessageHeader)) ||
727 (csize > tsize - offset))
732 unix_demultiplexer (plugin, &sender, currhdr, &un, sizeof (un));
738 unix_plugin_select_write (struct Plugin * plugin)
741 struct UNIXMessageWrapper * msgw = plugin->msg_head;
743 sent = unix_real_send (plugin,
744 plugin->unix_sock.desc,
745 &msgw->session->target,
746 (const char *) msgw->msg,
751 msgw->session->addrlen,
752 msgw->cont, msgw->cont_cls);
754 /* successfully sent bytes */
757 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
758 GNUNET_free (msgw->msg);
764 if (msgw->retry_counter > MAX_RETRIES)
766 msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR);
767 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
769 GNUNET_free (msgw->msg);
774 /* failed and no retry */
777 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
778 GNUNET_free (msgw->msg);
783 /* failed and retry */
786 msgw->retry_counter++;
793 * @param cls the plugin handle
794 * @param tc the scheduling context (for rescheduling this function again)
796 * We have been notified that our writeset has something to read. We don't
797 * know which socket needs to be read, so we have to check each one
798 * Then reschedule this function to be called again once more is available.
802 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
804 struct Plugin *plugin = cls;
806 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
807 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
811 if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
813 GNUNET_assert (GNUNET_NETWORK_fdset_isset
814 (tc->write_ready, plugin->unix_sock.desc));
815 if (plugin->msg_head != NULL)
816 unix_plugin_select_write (plugin);
819 if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
821 GNUNET_assert (GNUNET_NETWORK_fdset_isset
822 (tc->read_ready, plugin->unix_sock.desc));
823 unix_plugin_select_read (plugin);
826 plugin->select_task =
827 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
828 GNUNET_SCHEDULER_NO_TASK,
829 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
830 plugin->ws, &unix_plugin_select, plugin);
834 * Create a slew of UNIX sockets. If possible, use IPv6 and IPv4.
836 * @param cls closure for server start, should be a struct Plugin *
837 * @return number of sockets created or GNUNET_SYSERR on error
840 unix_transport_server_start (void *cls)
842 struct Plugin *plugin = cls;
843 struct sockaddr *serverAddr;
845 struct sockaddr_un un;
848 memset (&un, 0, sizeof (un));
849 un.sun_family = AF_UNIX;
850 slen = strlen (plugin->unix_socket_path) + 1;
851 if (slen >= sizeof (un.sun_path))
852 slen = sizeof (un.sun_path) - 1;
854 memcpy (un.sun_path, plugin->unix_socket_path, slen);
855 un.sun_path[slen] = '\0';
856 slen = sizeof (struct sockaddr_un);
857 #if HAVE_SOCKADDR_IN_SIN_LEN
858 un.sun_len = (u_char) slen;
861 serverAddr = (struct sockaddr *) &un;
864 un.sun_path[0] = '\0';
866 plugin->ats_network = plugin->env->get_address_type (plugin->env->cls, serverAddr, addrlen);
867 plugin->unix_sock.desc =
868 GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_DGRAM, 0);
869 if (NULL == plugin->unix_sock.desc)
871 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
872 return GNUNET_SYSERR;
874 if (GNUNET_NETWORK_socket_bind (plugin->unix_sock.desc, serverAddr, addrlen)
877 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
878 GNUNET_NETWORK_socket_close (plugin->unix_sock.desc);
879 plugin->unix_sock.desc = NULL;
880 return GNUNET_SYSERR;
883 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "unix", "Bound to `%s'\n",
886 plugin->rs = GNUNET_NETWORK_fdset_create ();
887 plugin->ws = GNUNET_NETWORK_fdset_create ();
888 GNUNET_NETWORK_fdset_zero (plugin->rs);
889 GNUNET_NETWORK_fdset_zero (plugin->ws);
890 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->unix_sock.desc);
891 GNUNET_NETWORK_fdset_set (plugin->ws, plugin->unix_sock.desc);
893 plugin->select_task =
894 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
895 GNUNET_SCHEDULER_NO_TASK,
896 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
897 plugin->ws, &unix_plugin_select, plugin);
903 * Function that will be called to check if a binary address for this
904 * plugin is well-formed and corresponds to an address for THIS peer
905 * (as per our configuration). Naturally, if absolutely necessary,
906 * plugins can be a bit conservative in their answer, but in general
907 * plugins should make sure that the address does not redirect
908 * traffic to a 3rd party that might try to man-in-the-middle our
911 * @param cls closure, should be our handle to the Plugin
912 * @param addr pointer to the address
913 * @param addrlen length of addr
914 * @return GNUNET_OK if this is a plausible address for this peer
915 * and transport, GNUNET_SYSERR if not
919 unix_check_address (void *cls, const void *addr, size_t addrlen)
923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
924 "Informing transport service about my address `%s'\n",
932 * Convert the transports address to a nice, human-readable
936 * @param type name of the transport that generated the address
937 * @param addr one of the addresses of the host, NULL for the last address
938 * the specific address format depends on the transport
939 * @param addrlen length of the address
940 * @param numeric should (IP) addresses be displayed in numeric form?
941 * @param timeout after how long should we give up?
942 * @param asc function to call on each string
943 * @param asc_cls closure for asc
946 unix_plugin_address_pretty_printer (void *cls, const char *type,
947 const void *addr, size_t addrlen,
949 struct GNUNET_TIME_Relative timeout,
950 GNUNET_TRANSPORT_AddressStringCallback asc,
953 if ((addr != NULL) && (addrlen > 0))
954 asc (asc_cls, (const char *) addr);
958 asc (asc_cls, "Invalid UNIX address");
964 * Function called for a quick conversion of the binary address to
965 * a numeric address. Note that the caller must not free the
966 * address and that the next call to this function is allowed
967 * to override the address again.
970 * @param addr binary address
971 * @param addrlen length of the address
972 * @return string representing the same address
975 unix_address_to_string (void *cls, const void *addr, size_t addrlen)
977 if ((addr != NULL) && (addrlen > 0))
978 return (const char *) addr;
984 * Notify transport service about address
986 * @param cls the plugin
990 address_notification (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
992 struct Plugin *plugin = cls;
994 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
995 plugin->unix_socket_path,
996 strlen (plugin->unix_socket_path) + 1);
1000 * The exported method. Makes the core api available via a global and
1001 * returns the unix transport API.
1004 libgnunet_plugin_transport_unix_init (void *cls)
1006 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1007 unsigned long long port;
1008 struct GNUNET_TRANSPORT_PluginFunctions *api;
1009 struct Plugin *plugin;
1010 int sockets_created;
1013 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-unix", "PORT",
1015 port = UNIX_NAT_DEFAULT_PORT;
1016 plugin = GNUNET_malloc (sizeof (struct Plugin));
1017 plugin->port = port;
1019 GNUNET_asprintf (&plugin->unix_socket_path, "/tmp/unix-plugin-sock.%d",
1022 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1025 api->get_session = &unix_plugin_get_session;
1026 api->send = &unix_plugin_send;
1027 api->disconnect = &unix_disconnect;
1028 api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1029 api->address_to_string = &unix_address_to_string;
1030 api->check_address = &unix_check_address;
1031 sockets_created = unix_transport_server_start (plugin);
1032 if (sockets_created == 0)
1033 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UNIX sockets\n"));
1035 plugin->session_map = GNUNET_CONTAINER_multihashmap_create(10);
1037 GNUNET_SCHEDULER_add_now (address_notification, plugin);
1042 libgnunet_plugin_transport_unix_done (void *cls)
1044 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1045 struct Plugin *plugin = api->cls;
1047 unix_transport_server_stop (plugin);
1049 GNUNET_CONTAINER_multihashmap_iterate (plugin->session_map, &get_session_delete_it, plugin);
1050 GNUNET_CONTAINER_multihashmap_destroy (plugin->session_map);
1052 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1053 GNUNET_NETWORK_fdset_destroy (plugin->ws);
1054 GNUNET_free (plugin->unix_socket_path);
1055 GNUNET_free (plugin);
1060 /* end of plugin_transport_unix.c */