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