error msg
[oweals/gnunet.git] / src / transport / plugin_transport_unix.c
1 /*
2      This file is part of GNUnet
3      (C) 2010 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
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
28  */
29
30 #include "platform.h"
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"
44
45 #define MAX_PROBES 20
46 #define MAX_RETRIES 3
47 #define RETRY 0
48
49 #define LOG(kind,...) GNUNET_log_from (kind, "transport-unix",__VA_ARGS__)
50
51 /*
52  * Transport cost to peer, always 1 for UNIX (direct connection)
53  */
54 #define UNIX_DIRECT_DISTANCE 1
55
56 #define DEFAULT_NAT_PORT 0
57
58 /**
59  * How long until we give up on transmitting the welcome message?
60  */
61 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
62
63 /**
64  * Starting port for listening and sending, eventually a config value
65  */
66 #define UNIX_NAT_DEFAULT_PORT 22086
67
68 GNUNET_NETWORK_STRUCT_BEGIN
69
70 /**
71  * UNIX Message-Packet header.
72  */
73 struct UNIXMessage
74 {
75   /**
76    * Message header.
77    */
78   struct GNUNET_MessageHeader header;
79
80   /**
81    * What is the identity of the sender (GNUNET_hash of public key)
82    */
83   struct GNUNET_PeerIdentity sender;
84
85 };
86
87 struct Session
88 {
89   struct GNUNET_PeerIdentity target;
90
91   void *addr;
92   size_t addrlen;
93
94   /**
95    * Session timeout task
96    */
97   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
98
99   struct Plugin * plugin;
100 };
101
102 struct UNIXMessageWrapper
103 {
104   struct UNIXMessageWrapper *next;
105   struct UNIXMessageWrapper *prev;
106
107   struct UNIXMessage * msg;
108   size_t msgsize;
109   size_t payload;
110
111   struct GNUNET_TIME_Absolute timeout;
112   unsigned int priority;
113
114   struct Session *session;
115   GNUNET_TRANSPORT_TransmitContinuation cont;
116   void *cont_cls;
117 };
118
119 /* Forward definition */
120 struct Plugin;
121
122
123 /**
124  * UNIX NAT "Session"
125  */
126 struct PeerSession
127 {
128
129   /**
130    * Stored in a linked list.
131    */
132   struct PeerSession *next;
133
134   /**
135    * Pointer to the global plugin struct.
136    */
137   struct Plugin *plugin;
138
139   /**
140    * To whom are we talking to (set to our identity
141    * if we are still waiting for the welcome message)
142    */
143   struct GNUNET_PeerIdentity target;
144
145   /**
146    * Address of the other peer (either based on our 'connect'
147    * call or on our 'accept' call).
148    */
149   void *connect_addr;
150
151   /**
152    * Length of connect_addr.
153    */
154   size_t connect_alen;
155
156   /**
157    * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
158    */
159   int expecting_welcome;
160
161   /**
162    * From which socket do we need to send to this peer?
163    */
164   struct GNUNET_NETWORK_Handle *sock;
165
166   /*
167    * Queue of messages for this peer, in the case that
168    * we have to await a connection...
169    */
170   struct MessageQueue *messages;
171
172 };
173
174 /**
175  * Information we keep for each of our listen sockets.
176  */
177 struct UNIX_Sock_Info
178 {
179   /**
180    * The network handle
181    */
182   struct GNUNET_NETWORK_Handle *desc;
183
184   /**
185    * The port we bound to
186    */
187   uint16_t port;
188 };
189
190
191 /**
192  * Encapsulation of all of the state of the plugin.
193  */
194 struct Plugin
195 {
196   /**
197    * Our environment.
198    */
199   struct GNUNET_TRANSPORT_PluginEnvironment *env;
200
201   /**
202    * Sessions
203    */
204   struct GNUNET_CONTAINER_MultiHashMap *session_map;
205
206   /**
207    * ID of task used to update our addresses when one expires.
208    */
209   GNUNET_SCHEDULER_TaskIdentifier address_update_task;
210
211   /**
212    * ID of select task
213    */
214   GNUNET_SCHEDULER_TaskIdentifier select_task;
215
216   /**
217    * Integer to append to unix domain socket.
218    */
219   uint16_t port;
220
221   /**
222    * FD Read set
223    */
224   struct GNUNET_NETWORK_FDSet *rs;
225
226   /**
227    * FD Write set
228    */
229   struct GNUNET_NETWORK_FDSet *ws;
230
231   int with_ws;
232
233   /**
234    * socket that we transmit all data with
235    */
236   struct UNIX_Sock_Info unix_sock;
237
238   /**
239    * Path of our unix domain socket (/tmp/unix-plugin-PORT)
240    */
241   char *unix_socket_path;
242
243   struct UNIXMessageWrapper *msg_head;
244   struct UNIXMessageWrapper *msg_tail;
245
246   /**
247    * ATS network
248    */
249   struct GNUNET_ATS_Information ats_network;
250
251   unsigned int bytes_in_queue;
252   unsigned int bytes_in_sent;
253   unsigned int bytes_in_recv;
254   unsigned int bytes_discarded;
255 };
256
257 /**
258  * Start session timeout
259  */
260 static void
261 start_session_timeout (struct Session *s);
262
263 /**
264  * Increment session timeout due to activity
265  */
266 static void
267 reschedule_session_timeout (struct Session *s);
268
269 /**
270  * Cancel timeout
271  */
272 static void
273 stop_session_timeout (struct Session *s);
274
275
276 static void
277 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
278
279
280 static void
281 reschedule_select (struct Plugin * plugin)
282 {
283
284   if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
285   {
286     GNUNET_SCHEDULER_cancel (plugin->select_task);
287     plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
288   }
289
290   if (NULL != plugin->msg_head)
291   {
292     plugin->select_task =
293       GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
294                                    GNUNET_TIME_UNIT_FOREVER_REL,
295                                    plugin->rs,
296                                    plugin->ws,
297                                    &unix_plugin_select, plugin);
298     plugin->with_ws = GNUNET_YES;
299   }
300   else
301   {
302     plugin->select_task =
303       GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
304                                    GNUNET_TIME_UNIT_FOREVER_REL,
305                                    plugin->rs,
306                                    NULL,
307                                    &unix_plugin_select, plugin);
308     plugin->with_ws = GNUNET_NO;
309   }
310 }
311
312 struct LookupCtx
313 {
314   struct Session *s;
315   const struct sockaddr_un *addr;
316 };
317
318 int lookup_session_it (void *cls,
319                        const struct GNUNET_HashCode * key,
320                        void *value)
321 {
322   struct LookupCtx *lctx = cls;
323   struct Session *t = value;
324
325   if (0 == strcmp (t->addr, lctx->addr->sun_path))
326   {
327     lctx->s = t;
328     return GNUNET_NO;
329   }
330   return GNUNET_YES;
331 }
332
333
334 static struct Session *
335 lookup_session (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender, const struct sockaddr_un *addr)
336 {
337   struct LookupCtx lctx;
338
339   GNUNET_assert (NULL != plugin);
340   GNUNET_assert (NULL != sender);
341   GNUNET_assert (NULL != addr);
342
343   lctx.s = NULL;
344   lctx.addr = addr;
345
346   GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map, &sender->hashPubKey, &lookup_session_it, &lctx);
347
348   return lctx.s;
349 }
350
351 /**
352  * Functions with this signature are called whenever we need
353  * to close a session due to a disconnect or failure to
354  * establish a connection.
355  *
356  * @param s session to close down
357  */
358 static void
359 disconnect_session (struct Session *s)
360 {
361   struct UNIXMessageWrapper *msgw;
362   struct UNIXMessageWrapper *next;
363   struct Plugin * plugin = s->plugin;
364   int removed;
365   GNUNET_assert (plugin != NULL);
366   GNUNET_assert (s != NULL);
367
368   LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting session for peer `%s' `%s' \n", GNUNET_i2s (&s->target), s->addr);
369   stop_session_timeout (s);
370   plugin->env->session_end (plugin->env->cls, &s->target, s);
371
372   msgw = plugin->msg_head;
373   removed = GNUNET_NO;
374   next = plugin->msg_head;
375   while (NULL != next)
376   {
377     msgw = next;
378     next = msgw->next;
379     if (msgw->session != s)
380       continue;
381     GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
382     if (NULL != msgw->cont)
383       msgw->cont (msgw->cont_cls,  &msgw->session->target, GNUNET_SYSERR,
384                   msgw->payload, 0);
385     GNUNET_free (msgw->msg);
386     GNUNET_free (msgw);
387     removed = GNUNET_YES;    
388   }
389   if ((GNUNET_YES == removed) && (NULL == plugin->msg_head))
390     reschedule_select (plugin);
391
392   GNUNET_assert (GNUNET_YES ==
393                  GNUNET_CONTAINER_multihashmap_remove(plugin->session_map, &s->target.hashPubKey, s));
394
395   GNUNET_STATISTICS_set(plugin->env->stats,
396                         "# UNIX sessions active",
397                         GNUNET_CONTAINER_multihashmap_size(plugin->session_map),
398                         GNUNET_NO);
399
400   GNUNET_free (s);
401 }
402
403 static int
404 get_session_delete_it (void *cls, const struct GNUNET_HashCode * key, void *value)
405 {
406   struct Session *s = value;
407   disconnect_session (s);
408   return GNUNET_YES;
409 }
410
411
412 /**
413  * Disconnect from a remote node.  Clean up session if we have one for this peer
414  *
415  * @param cls closure for this call (should be handle to Plugin)
416  * @param target the peeridentity of the peer to disconnect
417  * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
418  */
419 static void
420 unix_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
421 {
422   struct Plugin *plugin = cls;
423   GNUNET_assert (plugin != NULL);
424
425   GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map, &target->hashPubKey, &get_session_delete_it, plugin);
426   return;
427 }
428
429 /**
430  * Shutdown the server process (stop receiving inbound traffic). Maybe
431  * restarted later!
432  *
433  * @param cls Handle to the plugin for this transport
434  *
435  * @return returns the number of sockets successfully closed,
436  *         should equal the number of sockets successfully opened
437  */
438 static int
439 unix_transport_server_stop (void *cls)
440 {
441   struct Plugin *plugin = cls;
442   struct UNIXMessageWrapper * msgw;
443
444   while (NULL != (msgw = plugin->msg_head))
445   {
446     GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
447     if (msgw->cont != NULL)
448       msgw->cont (msgw->cont_cls,  &msgw->session->target, GNUNET_SYSERR,
449                   msgw->payload, 0);
450     GNUNET_free (msgw->msg);
451     GNUNET_free (msgw);
452   }
453
454   if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
455   {
456     GNUNET_SCHEDULER_cancel (plugin->select_task);
457     plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
458   }
459
460   if (NULL != plugin->unix_sock.desc)
461   {
462     GNUNET_break (GNUNET_OK ==
463                   GNUNET_NETWORK_socket_close (plugin->unix_sock.desc));
464     plugin->unix_sock.desc = NULL;
465     plugin->with_ws = GNUNET_NO;
466   }
467   return GNUNET_OK;
468 }
469
470
471 /**
472  * Actually send out the message, assume we've got the address and
473  * send_handle squared away!
474  *
475  * @param cls closure
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 payload bytes payload to send
485  * @param cont continuation to call once the message has
486  *        been transmitted (or if the transport is ready
487  *        for the next transmission call; or if the
488  *        peer disconnected...)
489  * @param cont_cls closure for cont
490  *
491  * @return on success : the number of bytes written, 0 n retry, -1 on errors
492  */
493 static ssize_t
494 unix_real_send (void *cls,
495                 struct GNUNET_NETWORK_Handle *send_handle,
496                 const struct GNUNET_PeerIdentity *target, const char *msgbuf,
497                 size_t msgbuf_size, unsigned int priority,
498                 struct GNUNET_TIME_Absolute timeout,
499                 const void *addr,
500                 size_t addrlen,
501                 size_t payload,
502                 GNUNET_TRANSPORT_TransmitContinuation cont,
503                 void *cont_cls)
504 {
505   struct Plugin *plugin = cls;
506   ssize_t sent;
507   const void *sb;
508   size_t sbs;
509   struct sockaddr_un un;
510   size_t slen;
511
512   GNUNET_assert (NULL != plugin);
513
514   if (send_handle == NULL)
515   {
516     GNUNET_break (0); /* We do not have a send handle */
517     return GNUNET_SYSERR;
518   }
519   if ((addr == NULL) || (addrlen == 0))
520   {
521     GNUNET_break (0); /* Can never send if we don't have an address */
522     return GNUNET_SYSERR;
523   }
524
525   /* Prepare address */
526   memset (&un, 0, sizeof (un));
527   un.sun_family = AF_UNIX;
528   slen = strlen (addr) + 1;
529   if (slen >= sizeof (un.sun_path))
530     slen = sizeof (un.sun_path) - 1;
531   GNUNET_assert (slen < sizeof (un.sun_path));
532   memcpy (un.sun_path, addr, slen);
533   un.sun_path[slen] = '\0';
534   slen = sizeof (struct sockaddr_un);
535 #if LINUX
536   un.sun_path[0] = '\0';
537 #endif
538 #if HAVE_SOCKADDR_IN_SIN_LEN
539   un.sun_len = (u_char) slen;
540 #endif
541   sb = (struct sockaddr *) &un;
542   sbs = slen;
543
544 resend:
545   /* Send the data */
546   sent = 0;
547   sent = GNUNET_NETWORK_socket_sendto (send_handle, msgbuf, msgbuf_size, sb, sbs);
548
549   if (GNUNET_SYSERR == sent)
550   {
551     if (errno == EAGAIN)
552     {
553       return RETRY; /* We have to retry later  */
554     }
555     if (errno == ENOBUFS)
556     {
557       return RETRY; /* We have to retry later  */
558     }
559     if (errno == EMSGSIZE)
560     {
561       socklen_t size = 0;
562       socklen_t len = sizeof (size);
563
564       GNUNET_NETWORK_socket_getsockopt ((struct GNUNET_NETWORK_Handle *)
565                                         send_handle, SOL_SOCKET, SO_SNDBUF, &size,
566                                         &len);
567       if (size < msgbuf_size)
568       {
569         LOG (GNUNET_ERROR_TYPE_DEBUG,
570                     "Trying to increase socket buffer size from %i to %i for message size %i\n",
571                     size, ((msgbuf_size / 1000) + 2) * 1000, msgbuf_size);
572         size = ((msgbuf_size / 1000) + 2) * 1000;
573         if (GNUNET_OK == GNUNET_NETWORK_socket_setsockopt
574             ((struct GNUNET_NETWORK_Handle *) send_handle, SOL_SOCKET, SO_SNDBUF,
575              &size, sizeof (size)))
576           goto resend; /* Increased buffer size, retry sending */
577         else
578         {
579           /* Could not increase buffer size: error, no retry */
580           GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
581           return GNUNET_SYSERR;
582         }
583       }
584       else
585       {
586         /* Buffer is bigger than message:  error, no retry
587          * This should never happen!*/
588         GNUNET_break (0);
589         return GNUNET_SYSERR;
590       }
591     }
592   }
593
594   LOG (GNUNET_ERROR_TYPE_DEBUG,
595               "UNIX transmit %u-byte message to %s (%d: %s)\n",
596               (unsigned int) msgbuf_size, GNUNET_a2s (sb, sbs), (int) sent,
597               (sent < 0) ? STRERROR (errno) : "ok");
598   return sent;
599 }
600
601 struct gsi_ctx
602 {
603   char *address;
604   size_t addrlen;
605   struct Session *res;
606 };
607
608
609 static int
610 get_session_it (void *cls, const struct GNUNET_HashCode * key, void *value)
611 {
612   struct gsi_ctx *gsi = cls;
613   struct Session *s = value;
614
615   LOG (GNUNET_ERROR_TYPE_DEBUG, "Comparing session %s %s\n", gsi->address, s->addr);
616   if ((gsi->addrlen == s->addrlen) &&
617       (0 == memcmp (gsi->address, s->addr, s->addrlen)))
618   {
619     gsi->res = s;
620     return GNUNET_NO;
621   }
622   return GNUNET_YES;
623 }
624
625 /**
626  * Creates a new outbound session the transport service will use to send data to the
627  * peer
628  *
629  * @param cls the plugin
630  * @param address the address
631  * @return the session or NULL of max connections exceeded
632  */
633 static struct Session *
634 unix_plugin_get_session (void *cls,
635                   const struct GNUNET_HELLO_Address *address)
636 {
637   struct Session * s = NULL;
638   struct Plugin *plugin = cls;
639   struct gsi_ctx gsi;
640
641   /* Checks */
642   GNUNET_assert (plugin != NULL);
643   GNUNET_assert (address != NULL);
644
645   /* Check if already existing */
646   gsi.address = (char *) address->address;
647   gsi.addrlen = address->address_length;
648   gsi.res = NULL;
649   GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map, &address->peer.hashPubKey, &get_session_it, &gsi);
650   if (gsi.res != NULL)
651   {
652     LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing session\n");
653     return gsi.res;
654   }
655
656   /* Create a new session */
657   s = GNUNET_malloc (sizeof (struct Session) + address->address_length);
658   s->addr = &s[1];
659   s->addrlen = address->address_length;
660   s->plugin = plugin;
661   memcpy(s->addr, address->address, s->addrlen);
662   memcpy(&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
663
664   start_session_timeout (s);
665
666   GNUNET_CONTAINER_multihashmap_put (plugin->session_map,
667       &address->peer.hashPubKey, s,
668       GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
669
670   GNUNET_STATISTICS_set(plugin->env->stats,
671                         "# UNIX sessions active",
672                         GNUNET_CONTAINER_multihashmap_size(plugin->session_map),
673                         GNUNET_NO);
674   LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating new session\n");
675   return s;
676 }
677
678 /*
679  * @param cls the plugin handle
680  * @param tc the scheduling context (for rescheduling this function again)
681  *
682  * We have been notified that our writeset has something to read.  We don't
683  * know which socket needs to be read, so we have to check each one
684  * Then reschedule this function to be called again once more is available.
685  *
686  */
687 static void
688 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
689
690 /**
691  * Function that can be used by the transport service to transmit
692  * a message using the plugin.   Note that in the case of a
693  * peer disconnecting, the continuation MUST be called
694  * prior to the disconnect notification itself.  This function
695  * will be called with this peer's HELLO message to initiate
696  * a fresh connection to another peer.
697  *
698  * @param cls closure
699  * @param session which session must be used
700  * @param msgbuf the message to transmit
701  * @param msgbuf_size number of bytes in 'msgbuf'
702  * @param priority how important is the message (most plugins will
703  *                 ignore message priority and just FIFO)
704  * @param to how long to wait at most for the transmission (does not
705  *                require plugins to discard the message after the timeout,
706  *                just advisory for the desired delay; most plugins will ignore
707  *                this as well)
708  * @param cont continuation to call once the message has
709  *        been transmitted (or if the transport is ready
710  *        for the next transmission call; or if the
711  *        peer disconnected...); can be NULL
712  * @param cont_cls closure for cont
713  * @return number of bytes used (on the physical network, with overheads);
714  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
715  *         and does NOT mean that the message was not transmitted (DV)
716  */
717 static ssize_t
718 unix_plugin_send (void *cls,
719                   struct Session *session,
720                   const char *msgbuf, size_t msgbuf_size,
721                   unsigned int priority,
722                   struct GNUNET_TIME_Relative to,
723                   GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
724 {
725   struct Plugin *plugin = cls;
726   struct UNIXMessageWrapper *wrapper;
727   struct UNIXMessage *message;
728   int ssize;
729   
730   GNUNET_assert (plugin != NULL);
731   GNUNET_assert (session != NULL);
732
733   if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_contains_value(plugin->session_map,
734       &session->target.hashPubKey, session))
735   {
736     LOG (GNUNET_ERROR_TYPE_ERROR, "Invalid session for peer `%s' `%s'\n",
737                 GNUNET_i2s (&session->target),
738                 (char *) session->addr);
739     GNUNET_break (0);
740
741     return GNUNET_SYSERR;
742   }
743   LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending %u bytes with session for peer `%s' `%s'\n",
744                 msgbuf_size,
745         GNUNET_i2s (&session->target),
746         (char *) session->addr);
747
748   ssize = sizeof (struct UNIXMessage) + msgbuf_size;
749   message = GNUNET_malloc (sizeof (struct UNIXMessage) + msgbuf_size);
750   message->header.size = htons (ssize);
751   message->header.type = htons (0);
752   memcpy (&message->sender, plugin->env->my_identity,
753           sizeof (struct GNUNET_PeerIdentity));
754   memcpy (&message[1], msgbuf, msgbuf_size);
755
756   reschedule_session_timeout (session);
757
758   wrapper = GNUNET_malloc (sizeof (struct UNIXMessageWrapper));
759   wrapper->msg = message;
760   wrapper->msgsize = ssize;
761   wrapper->payload = msgbuf_size;
762   wrapper->priority = priority;
763   wrapper->timeout = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), to);
764   wrapper->cont = cont;
765   wrapper->cont_cls = cont_cls;
766   wrapper->session = session;
767
768   GNUNET_CONTAINER_DLL_insert(plugin->msg_head, plugin->msg_tail, wrapper);
769
770   plugin->bytes_in_queue += ssize;
771   GNUNET_STATISTICS_set (plugin->env->stats,"# bytes currently in UNIX buffers",
772       plugin->bytes_in_queue, GNUNET_NO);
773
774   LOG (GNUNET_ERROR_TYPE_DEBUG, "Sent %d bytes to `%s'\n", ssize,
775               (char *) session->addr);
776   if (plugin->with_ws == GNUNET_NO)
777   {
778     reschedule_select (plugin);
779   }
780
781   return ssize;
782 }
783
784
785 /**
786  * Demultiplexer for UNIX messages
787  *
788  * @param plugin the main plugin for this transport
789  * @param sender from which peer the message was received
790  * @param currhdr pointer to the header of the message
791  * @param un the address from which the message was received
792  * @param fromlen the length of the address
793  */
794 static void
795 unix_demultiplexer (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender,
796                     const struct GNUNET_MessageHeader *currhdr,
797                     const struct sockaddr_un *un, size_t fromlen)
798 {
799   struct GNUNET_ATS_Information ats[2];
800   struct Session *s = NULL;
801   struct GNUNET_HELLO_Address * addr;
802
803   ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
804   ats[0].value = htonl (UNIX_DIRECT_DISTANCE);
805   ats[1] = plugin->ats_network;
806   GNUNET_break (ntohl(plugin->ats_network.value) != GNUNET_ATS_NET_UNSPECIFIED);
807
808   GNUNET_assert (fromlen >= sizeof (struct sockaddr_un));
809
810   LOG (GNUNET_ERROR_TYPE_DEBUG, "Received message from %s\n",
811               un->sun_path);
812
813
814
815   plugin->bytes_in_recv += ntohs(currhdr->size);
816   GNUNET_STATISTICS_set (plugin->env->stats,"# bytes received via UNIX",
817       plugin->bytes_in_recv, GNUNET_NO);
818
819   addr = GNUNET_HELLO_address_allocate(sender, "unix", un->sun_path, strlen (un->sun_path) + 1);
820   s = lookup_session (plugin, sender, un);
821   if (NULL == s)
822     s = unix_plugin_get_session (plugin, addr);
823   reschedule_session_timeout (s);
824
825   plugin->env->receive (plugin->env->cls, sender, currhdr,
826                         (const struct GNUNET_ATS_Information *) &ats, 2,
827                         s, un->sun_path, strlen (un->sun_path) + 1);
828   GNUNET_free (addr);
829 }
830
831
832 static void
833 unix_plugin_select_read (struct Plugin * plugin)
834 {
835   char buf[65536] GNUNET_ALIGN;
836   struct UNIXMessage *msg;
837   struct GNUNET_PeerIdentity sender;
838   struct sockaddr_un un;
839   socklen_t addrlen;
840   ssize_t ret;
841   int offset;
842   int tsize;
843   char *msgbuf;
844   const struct GNUNET_MessageHeader *currhdr;
845   uint16_t csize;
846
847   addrlen = sizeof (un);
848   memset (&un, 0, sizeof (un));
849
850   ret =
851       GNUNET_NETWORK_socket_recvfrom (plugin->unix_sock.desc, buf, sizeof (buf),
852                                       (struct sockaddr *) &un, &addrlen);
853
854   if ((GNUNET_SYSERR == ret) && ((errno == EAGAIN) || (errno == ENOBUFS)))
855     return;
856
857   if (ret == GNUNET_SYSERR)
858   {
859     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom");
860     return;
861   }
862   else
863   {
864 #if LINUX
865     un.sun_path[0] = '/';
866 #endif
867     LOG (GNUNET_ERROR_TYPE_DEBUG, "Read %d bytes from socket %s\n", ret,
868                 &un.sun_path[0]);
869   }
870
871   GNUNET_assert (AF_UNIX == (un.sun_family));
872
873   msg = (struct UNIXMessage *) buf;
874   csize = ntohs (msg->header.size);
875   if ((csize < sizeof (struct UNIXMessage)) || (csize > ret))
876   {
877     GNUNET_break_op (0);
878     return;
879   }
880   msgbuf = (char *) &msg[1];
881   memcpy (&sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
882   offset = 0;
883   tsize = csize - sizeof (struct UNIXMessage);
884   while (offset + sizeof (struct GNUNET_MessageHeader) <= tsize)
885   {
886     currhdr = (struct GNUNET_MessageHeader *) &msgbuf[offset];
887     csize = ntohs (currhdr->size);
888     if ((csize < sizeof (struct GNUNET_MessageHeader)) ||
889         (csize > tsize - offset))
890     {
891       GNUNET_break_op (0);
892       break;
893     }
894
895     unix_demultiplexer (plugin, &sender, currhdr, &un, sizeof (un));
896     offset += csize;
897   }
898 }
899
900
901 static void
902 unix_plugin_select_write (struct Plugin * plugin)
903 {
904   int sent = 0;
905
906   struct UNIXMessageWrapper * msgw = plugin->msg_tail;
907   while (NULL != msgw)
908   {
909       if (GNUNET_TIME_absolute_get_remaining (msgw->timeout).rel_value > 0)
910         break; /* Message is ready for sending */
911       else
912       {
913           /* Message has a timeout */
914             LOG (GNUNET_ERROR_TYPE_DEBUG,
915               "Timeout for message with %llu bytes \n", msgw->msgsize);
916           GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
917           if (NULL != msgw->cont)
918             msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR, msgw->payload, 0);
919
920           plugin->bytes_in_queue -= msgw->msgsize;
921           GNUNET_STATISTICS_set (plugin->env->stats, "# bytes currently in UNIX buffers",
922               plugin->bytes_in_queue, GNUNET_NO);
923
924           plugin->bytes_discarded += msgw->msgsize;
925           GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes discarded",
926               plugin->bytes_discarded, GNUNET_NO);
927
928           GNUNET_free (msgw->msg);
929           GNUNET_free (msgw);
930       }
931       msgw = plugin->msg_tail;
932   }
933   if (NULL == msgw)
934     return; /* Nothing to send at the moment */
935
936   sent = unix_real_send (plugin,
937                          plugin->unix_sock.desc,
938                          &msgw->session->target,
939                          (const char *) msgw->msg,
940                          msgw->msgsize,
941                          msgw->priority,
942                          msgw->timeout,
943                          msgw->session->addr,
944                          msgw->session->addrlen,
945                          msgw->payload,
946                          msgw->cont, msgw->cont_cls);
947
948   if (RETRY == sent)
949   {
950     GNUNET_STATISTICS_update (plugin->env->stats,"# UNIX retry attempts",
951           1, GNUNET_NO);
952
953   }
954   else if (GNUNET_SYSERR == sent)
955   {
956     /* failed and no retry */
957     if (NULL != msgw->cont)
958       msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_SYSERR, msgw->payload, 0);
959
960     GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
961
962     GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
963     plugin->bytes_in_queue -= msgw->msgsize;
964     GNUNET_STATISTICS_set (plugin->env->stats, "# bytes currently in UNIX buffers",
965         plugin->bytes_in_queue, GNUNET_NO);
966     plugin->bytes_discarded += msgw->msgsize;
967     GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes discarded",
968         plugin->bytes_discarded, GNUNET_NO);
969
970     GNUNET_free (msgw->msg);
971     GNUNET_free (msgw);
972     return;
973   }
974   else if (sent > 0)
975   {
976     /* successfully sent bytes */
977     if (NULL != msgw->cont)
978       msgw->cont (msgw->cont_cls, &msgw->session->target, GNUNET_OK, msgw->payload, msgw->msgsize);
979
980     GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
981
982     GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
983     plugin->bytes_in_queue -= msgw->msgsize;
984     GNUNET_STATISTICS_set (plugin->env->stats,"# bytes currently in UNIX buffers",
985         plugin->bytes_in_queue, GNUNET_NO);
986     plugin->bytes_in_sent += msgw->msgsize;
987     GNUNET_STATISTICS_set (plugin->env->stats,"# bytes transmitted via UNIX",
988         plugin->bytes_in_sent, GNUNET_NO);
989
990     GNUNET_free (msgw->msg);
991     GNUNET_free (msgw);
992     return;
993   }
994 }
995
996
997 /**
998  * We have been notified that our writeset has something to read.  We don't
999  * know which socket needs to be read, so we have to check each one
1000  * Then reschedule this function to be called again once more is available.
1001  *
1002  * @param cls the plugin handle
1003  * @param tc the scheduling context (for rescheduling this function again)
1004  */
1005 static void
1006 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1007 {
1008   struct Plugin *plugin = cls;
1009
1010   plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1011   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1012     return;
1013
1014   if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
1015   {
1016     /* Ready to send data */
1017     GNUNET_assert (GNUNET_NETWORK_fdset_isset
1018                    (tc->write_ready, plugin->unix_sock.desc));
1019     if (plugin->msg_head != NULL)
1020       unix_plugin_select_write (plugin);
1021   }
1022
1023   if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
1024   {
1025     /* Ready to receive data */
1026     GNUNET_assert (GNUNET_NETWORK_fdset_isset
1027                    (tc->read_ready, plugin->unix_sock.desc));
1028     unix_plugin_select_read (plugin);
1029   }
1030
1031   reschedule_select (plugin);
1032 }
1033
1034
1035 /**
1036  * Create a slew of UNIX sockets.  If possible, use IPv6 and IPv4.
1037  *
1038  * @param cls closure for server start, should be a struct Plugin *
1039  * @return number of sockets created or GNUNET_SYSERR on error
1040  */
1041 static int
1042 unix_transport_server_start (void *cls)
1043 {
1044   struct Plugin *plugin = cls;
1045   struct sockaddr *serverAddr;
1046   socklen_t addrlen;
1047   struct sockaddr_un un;
1048   size_t slen;
1049
1050   memset (&un, 0, sizeof (un));
1051   un.sun_family = AF_UNIX;
1052   slen = strlen (plugin->unix_socket_path) + 1;
1053   if (slen >= sizeof (un.sun_path))
1054     slen = sizeof (un.sun_path) - 1;
1055
1056   memcpy (un.sun_path, plugin->unix_socket_path, slen);
1057   un.sun_path[slen] = '\0';
1058   slen = sizeof (struct sockaddr_un);
1059 #if HAVE_SOCKADDR_IN_SIN_LEN
1060   un.sun_len = (u_char) slen;
1061 #endif
1062
1063   serverAddr = (struct sockaddr *) &un;
1064   addrlen = slen;
1065 #if LINUX
1066   un.sun_path[0] = '\0';
1067 #endif
1068   plugin->ats_network = plugin->env->get_address_type (plugin->env->cls, serverAddr, addrlen);
1069   plugin->unix_sock.desc =
1070       GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_DGRAM, 0);
1071   if (NULL == plugin->unix_sock.desc)
1072   {
1073     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
1074     return GNUNET_SYSERR;
1075   }
1076   if (GNUNET_NETWORK_socket_bind (plugin->unix_sock.desc, serverAddr, addrlen)
1077       != GNUNET_OK)
1078   {
1079     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
1080     GNUNET_NETWORK_socket_close (plugin->unix_sock.desc);
1081     plugin->unix_sock.desc = NULL;
1082     return GNUNET_SYSERR;
1083   }
1084   LOG (GNUNET_ERROR_TYPE_DEBUG, "Bound to `%s'\n", plugin->unix_socket_path);
1085   plugin->rs = GNUNET_NETWORK_fdset_create ();
1086   plugin->ws = GNUNET_NETWORK_fdset_create ();
1087   GNUNET_NETWORK_fdset_zero (plugin->rs);
1088   GNUNET_NETWORK_fdset_zero (plugin->ws);
1089   GNUNET_NETWORK_fdset_set (plugin->rs, plugin->unix_sock.desc);
1090   GNUNET_NETWORK_fdset_set (plugin->ws, plugin->unix_sock.desc);
1091
1092   reschedule_select (plugin);
1093
1094   return 1;
1095 }
1096
1097
1098 /**
1099  * Function that will be called to check if a binary address for this
1100  * plugin is well-formed and corresponds to an address for THIS peer
1101  * (as per our configuration).  Naturally, if absolutely necessary,
1102  * plugins can be a bit conservative in their answer, but in general
1103  * plugins should make sure that the address does not redirect
1104  * traffic to a 3rd party that might try to man-in-the-middle our
1105  * traffic.
1106  *
1107  * @param cls closure, should be our handle to the Plugin
1108  * @param addr pointer to the address
1109  * @param addrlen length of addr
1110  * @return GNUNET_OK if this is a plausible address for this peer
1111  *         and transport, GNUNET_SYSERR if not
1112  *
1113  */
1114 static int
1115 unix_check_address (void *cls, const void *addr, size_t addrlen)
1116 {
1117   LOG (GNUNET_ERROR_TYPE_DEBUG,
1118               "Informing transport service about my address `%s'\n",
1119               (char *) addr);
1120   return GNUNET_OK;
1121 }
1122
1123
1124 /**
1125  * Convert the transports address to a nice, human-readable
1126  * format.
1127  *
1128  * @param cls closure
1129  * @param type name of the transport that generated the address
1130  * @param addr one of the addresses of the host, NULL for the last address
1131  *        the specific address format depends on the transport
1132  * @param addrlen length of the address
1133  * @param numeric should (IP) addresses be displayed in numeric form?
1134  * @param timeout after how long should we give up?
1135  * @param asc function to call on each string
1136  * @param asc_cls closure for asc
1137  */
1138 static void
1139 unix_plugin_address_pretty_printer (void *cls, const char *type,
1140                                     const void *addr, size_t addrlen,
1141                                     int numeric,
1142                                     struct GNUNET_TIME_Relative timeout,
1143                                     GNUNET_TRANSPORT_AddressStringCallback asc,
1144                                     void *asc_cls)
1145 {
1146   if ((NULL != addr) && (addrlen > 0))
1147   {
1148     asc (asc_cls, (const char *) addr);
1149   }
1150   else
1151   {
1152     GNUNET_break (0);
1153     asc (asc_cls, "<invalid UNIX address>");
1154   }
1155   asc (asc_cls, NULL);
1156 }
1157
1158
1159 /**
1160  * Function called to convert a string address to
1161  * a binary address.
1162  *
1163  * @param cls closure ('struct Plugin*')
1164  * @param addr string address
1165  * @param addrlen length of the address (strlen(addr) + '\0')
1166  * @param buf location to store the buffer
1167  *        If the function returns GNUNET_SYSERR, its contents are undefined.
1168  * @param added length of created address
1169  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1170  */
1171 static int
1172 unix_string_to_address (void *cls, const char *addr, uint16_t addrlen,
1173     void **buf, size_t *added)
1174 {
1175   if ((NULL == addr) || (0 == addrlen))
1176   {
1177     GNUNET_break (0);
1178     return GNUNET_SYSERR;
1179   }
1180
1181   if ('\0' != addr[addrlen - 1])
1182   {
1183     GNUNET_break (0);
1184     return GNUNET_SYSERR;
1185   }
1186
1187   if (strlen (addr) != addrlen - 1)
1188   {
1189     GNUNET_break (0);
1190     return GNUNET_SYSERR;
1191   }
1192
1193   (*buf) = strdup (addr);
1194   (*added) = strlen (addr) + 1;
1195   return GNUNET_OK;
1196 }
1197
1198
1199 /**
1200  * Function called for a quick conversion of the binary address to
1201  * a numeric address.  Note that the caller must not free the
1202  * address and that the next call to this function is allowed
1203  * to override the address again.
1204  *
1205  * @param cls closure
1206  * @param addr binary address
1207  * @param addrlen length of the address
1208  * @return string representing the same address
1209  */
1210 static const char *
1211 unix_address_to_string (void *cls, const void *addr, size_t addrlen)
1212 {
1213   if ((addr != NULL) && (addrlen > 0))
1214     return (const char *) addr;
1215   return NULL;
1216 }
1217
1218
1219 /**
1220  * Notify transport service about address
1221  *
1222  * @param cls the plugin
1223  * @param tc unused
1224  */
1225 static void
1226 address_notification (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1227 {
1228   struct Plugin *plugin = cls;
1229
1230   plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
1231                                plugin->unix_socket_path,
1232                                strlen (plugin->unix_socket_path) + 1,
1233                                "unix");
1234 }
1235
1236
1237 /**
1238  * Session was idle, so disconnect it
1239  */
1240 static void
1241 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1242 {
1243   GNUNET_assert (NULL != cls);
1244   struct Session *s = cls;
1245
1246   s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1247   LOG (GNUNET_ERROR_TYPE_DEBUG,
1248               "Session %p was idle for %llu ms, disconnecting\n",
1249               s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1250   /* call session destroy function */
1251   disconnect_session(s);
1252 }
1253
1254
1255 /**
1256  * Start session timeout
1257  */
1258 static void
1259 start_session_timeout (struct Session *s)
1260 {
1261   GNUNET_assert (NULL != s);
1262   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1263   s->timeout_task =  GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1264                                                    &session_timeout,
1265                                                    s);
1266   LOG (GNUNET_ERROR_TYPE_DEBUG,
1267               "Timeout for session %p set to %llu ms\n",
1268               s,  (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1269 }
1270
1271
1272 /**
1273  * Increment session timeout due to activity
1274  */
1275 static void
1276 reschedule_session_timeout (struct Session *s)
1277 {
1278   GNUNET_assert (NULL != s);
1279   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1280
1281   GNUNET_SCHEDULER_cancel (s->timeout_task);
1282   s->timeout_task =  GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1283                                                    &session_timeout,
1284                                                    s);
1285   LOG (GNUNET_ERROR_TYPE_DEBUG,
1286               "Timeout rescheduled for session %p set to %llu ms\n",
1287               s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1288 }
1289
1290
1291 /**
1292  * Cancel timeout
1293  */
1294 static void
1295 stop_session_timeout (struct Session *s)
1296 {
1297   GNUNET_assert (NULL != s);
1298
1299   if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1300   {
1301     GNUNET_SCHEDULER_cancel (s->timeout_task);
1302     s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1303     LOG (GNUNET_ERROR_TYPE_DEBUG,
1304                 "Timeout stopped for session %p canceled\n",
1305                 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1306   }
1307 }
1308
1309 /**
1310  * The exported method. Makes the core api available via a global and
1311  * returns the unix transport API.
1312  */
1313 void *
1314 libgnunet_plugin_transport_unix_init (void *cls)
1315 {
1316   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1317   unsigned long long port;
1318   struct GNUNET_TRANSPORT_PluginFunctions *api;
1319   struct Plugin *plugin;
1320   int sockets_created;
1321
1322   if (NULL == env->receive)
1323   {
1324     /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1325        initialze the plugin or the API */
1326     api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1327     api->cls = NULL;
1328     api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1329     api->address_to_string = &unix_address_to_string;
1330     api->string_to_address = &unix_string_to_address;
1331     return api;
1332   }
1333   if (GNUNET_OK !=
1334       GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-unix", "PORT",
1335                                              &port))
1336     port = UNIX_NAT_DEFAULT_PORT;
1337   plugin = GNUNET_malloc (sizeof (struct Plugin));
1338   plugin->port = port;
1339   plugin->env = env;
1340   GNUNET_asprintf (&plugin->unix_socket_path, "/tmp/unix-plugin-sock.%d",
1341                    plugin->port);
1342
1343   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1344   api->cls = plugin;
1345
1346   api->get_session = &unix_plugin_get_session;
1347   api->send = &unix_plugin_send;
1348   api->disconnect = &unix_disconnect;
1349   api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1350   api->address_to_string = &unix_address_to_string;
1351   api->check_address = &unix_check_address;
1352   api->string_to_address = &unix_string_to_address;
1353   sockets_created = unix_transport_server_start (plugin);
1354   if (sockets_created == 0)
1355     LOG (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UNIX sockets\n"));
1356
1357   plugin->session_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
1358
1359   GNUNET_SCHEDULER_add_now (address_notification, plugin);
1360   return api;
1361 }
1362
1363 void *
1364 libgnunet_plugin_transport_unix_done (void *cls)
1365 {
1366   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1367   struct Plugin *plugin = api->cls;
1368
1369   if (NULL == plugin)
1370   {
1371     GNUNET_free (api);
1372     return NULL;
1373   }
1374
1375   plugin->env->notify_address (plugin->env->cls, GNUNET_NO,
1376                                plugin->unix_socket_path,
1377                                strlen (plugin->unix_socket_path) + 1,
1378                                "unix");
1379
1380   unix_transport_server_stop (plugin);
1381
1382
1383   GNUNET_CONTAINER_multihashmap_iterate (plugin->session_map, &get_session_delete_it, plugin);
1384   GNUNET_CONTAINER_multihashmap_destroy (plugin->session_map);
1385
1386
1387   if (NULL != plugin->rs)
1388     GNUNET_NETWORK_fdset_destroy (plugin->rs);
1389   if (NULL != plugin->ws)
1390     GNUNET_NETWORK_fdset_destroy (plugin->ws);
1391   GNUNET_free (plugin->unix_socket_path);
1392   GNUNET_free (plugin);
1393   GNUNET_free (api);
1394   return NULL;
1395 }
1396
1397 /* end of plugin_transport_unix.c */