overhead reporting
[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
47 #define LOG(kind,...) GNUNET_log_from (kind, "transport-unix",__VA_ARGS__)
48
49 /*
50  * Transport cost to peer, always 1 for UNIX (direct connection)
51  */
52 #define UNIX_DIRECT_DISTANCE 1
53
54 #define DEFAULT_NAT_PORT 0
55
56 /**
57  * How long until we give up on transmitting the welcome message?
58  */
59 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
60
61 /**
62  * Starting port for listening and sending, eventually a config value
63  */
64 #define UNIX_NAT_DEFAULT_PORT 22086
65
66 GNUNET_NETWORK_STRUCT_BEGIN
67
68 /**
69  * UNIX Message-Packet header.
70  */
71 struct UNIXMessage
72 {
73   /**
74    * Message header.
75    */
76   struct GNUNET_MessageHeader header;
77
78   /**
79    * What is the identity of the sender (GNUNET_hash of public key)
80    */
81   struct GNUNET_PeerIdentity sender;
82
83 };
84
85 struct Session
86 {
87   struct GNUNET_PeerIdentity target;
88
89   void *addr;
90   size_t addrlen;
91
92   /**
93    * Session timeout task
94    */
95   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
96
97   struct Plugin * plugin;
98 };
99
100 struct UNIXMessageWrapper
101 {
102   struct UNIXMessageWrapper *next;
103   struct UNIXMessageWrapper *prev;
104
105   struct UNIXMessage * msg;
106   size_t msgsize;
107
108   struct GNUNET_TIME_Relative timeout;
109   unsigned int priority;
110
111   struct Session *session;
112   GNUNET_TRANSPORT_TransmitContinuation cont;
113   void *cont_cls;
114 };
115
116 /* Forward definition */
117 struct Plugin;
118
119
120 /**
121  * UNIX NAT "Session"
122  */
123 struct PeerSession
124 {
125
126   /**
127    * Stored in a linked list.
128    */
129   struct PeerSession *next;
130
131   /**
132    * Pointer to the global plugin struct.
133    */
134   struct Plugin *plugin;
135
136   /**
137    * To whom are we talking to (set to our identity
138    * if we are still waiting for the welcome message)
139    */
140   struct GNUNET_PeerIdentity target;
141
142   /**
143    * Address of the other peer (either based on our 'connect'
144    * call or on our 'accept' call).
145    */
146   void *connect_addr;
147
148   /**
149    * Length of connect_addr.
150    */
151   size_t connect_alen;
152
153   /**
154    * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
155    */
156   int expecting_welcome;
157
158   /**
159    * From which socket do we need to send to this peer?
160    */
161   struct GNUNET_NETWORK_Handle *sock;
162
163   /*
164    * Queue of messages for this peer, in the case that
165    * we have to await a connection...
166    */
167   struct MessageQueue *messages;
168
169 };
170
171 /**
172  * Information we keep for each of our listen sockets.
173  */
174 struct UNIX_Sock_Info
175 {
176   /**
177    * The network handle
178    */
179   struct GNUNET_NETWORK_Handle *desc;
180
181   /**
182    * The port we bound to
183    */
184   uint16_t port;
185 };
186
187
188 /**
189  * Encapsulation of all of the state of the plugin.
190  */
191 struct Plugin
192 {
193   /**
194    * Our environment.
195    */
196   struct GNUNET_TRANSPORT_PluginEnvironment *env;
197
198   /**
199    * Sessions
200    */
201   struct GNUNET_CONTAINER_MultiHashMap *session_map;
202
203   /**
204    * ID of task used to update our addresses when one expires.
205    */
206   GNUNET_SCHEDULER_TaskIdentifier address_update_task;
207
208   /**
209    * ID of select task
210    */
211   GNUNET_SCHEDULER_TaskIdentifier select_task;
212
213   /**
214    * Integer to append to unix domain socket.
215    */
216   uint16_t port;
217
218   /**
219    * FD Read set
220    */
221   struct GNUNET_NETWORK_FDSet *rs;
222
223   /**
224    * FD Write set
225    */
226   struct GNUNET_NETWORK_FDSet *ws;
227
228   int with_ws;
229
230   /**
231    * socket that we transmit all data with
232    */
233   struct UNIX_Sock_Info unix_sock;
234
235   /**
236    * Path of our unix domain socket (/tmp/unix-plugin-PORT)
237    */
238   char *unix_socket_path;
239
240   struct UNIXMessageWrapper *msg_head;
241   struct UNIXMessageWrapper *msg_tail;
242
243   /**
244    * ATS network
245    */
246   struct GNUNET_ATS_Information ats_network;
247
248   unsigned int bytes_in_queue;
249   unsigned int bytes_in_sent;
250   unsigned int bytes_in_recv;
251   unsigned int bytes_discarded;
252 };
253
254 /**
255  * Start session timeout
256  */
257 static void
258 start_session_timeout (struct Session *s);
259
260 /**
261  * Increment session timeout due to activity
262  */
263 static void
264 reschedule_session_timeout (struct Session *s);
265
266 /**
267  * Cancel timeout
268  */
269 static void
270 stop_session_timeout (struct Session *s);
271
272
273 static void
274 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
275
276
277 static void
278 reschedule_select (struct Plugin * plugin)
279 {
280
281   if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
282   {
283     GNUNET_SCHEDULER_cancel (plugin->select_task);
284     plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
285   }
286
287   if (NULL != plugin->msg_head)
288   {
289     plugin->select_task =
290       GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
291                                    GNUNET_TIME_UNIT_FOREVER_REL,
292                                    plugin->rs,
293                                    plugin->ws,
294                                    &unix_plugin_select, plugin);
295     plugin->with_ws = GNUNET_YES;
296   }
297   else
298   {
299     plugin->select_task =
300       GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
301                                    GNUNET_TIME_UNIT_FOREVER_REL,
302                                    plugin->rs,
303                                    NULL,
304                                    &unix_plugin_select, plugin);
305     plugin->with_ws = GNUNET_NO;
306   }
307 }
308
309 struct LookupCtx
310 {
311   struct Session *s;
312   const struct sockaddr_un *addr;
313 };
314
315 int lookup_session_it (void *cls,
316                        const struct GNUNET_HashCode * key,
317                        void *value)
318 {
319   struct LookupCtx *lctx = cls;
320   struct Session *t = value;
321
322   if (0 == strcmp (t->addr, lctx->addr->sun_path))
323   {
324     lctx->s = t;
325     return GNUNET_NO;
326   }
327   return GNUNET_YES;
328 }
329
330
331 static struct Session *
332 lookup_session (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender, const struct sockaddr_un *addr)
333 {
334   struct LookupCtx lctx;
335
336   GNUNET_assert (NULL != plugin);
337   GNUNET_assert (NULL != sender);
338   GNUNET_assert (NULL != addr);
339
340   lctx.s = NULL;
341   lctx.addr = addr;
342
343   GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map, &sender->hashPubKey, &lookup_session_it, &lctx);
344
345   return lctx.s;
346 }
347
348 /**
349  * Functions with this signature are called whenever we need
350  * to close a session due to a disconnect or failure to
351  * establish a connection.
352  *
353  * @param s session to close down
354  */
355 static void
356 disconnect_session (struct Session *s)
357 {
358   struct UNIXMessageWrapper *msgw;
359   struct UNIXMessageWrapper *next;
360   struct Plugin * plugin = s->plugin;
361   int removed;
362   GNUNET_assert (plugin != NULL);
363   GNUNET_assert (s != NULL);
364
365   LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting session for peer `%s' `%s' \n", GNUNET_i2s (&s->target), s->addr);
366   stop_session_timeout (s);
367   plugin->env->session_end (plugin->env->cls, &s->target, s);
368
369   msgw = plugin->msg_head;
370   removed = GNUNET_NO;
371   next = plugin->msg_head;
372   while (NULL != next)
373   {
374     msgw = next;
375     next = msgw->next;
376     if (msgw->session != s)
377       continue;
378     GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
379     if (NULL != msgw->cont)
380       msgw->cont (msgw->cont_cls,  &msgw->session->target, GNUNET_SYSERR,
381                   msgw->msgsize, 0);
382     GNUNET_free (msgw->msg);
383     GNUNET_free (msgw);
384     removed = GNUNET_YES;    
385   }
386   if ((GNUNET_YES == removed) && (NULL == plugin->msg_head))
387     reschedule_select (plugin);
388
389   GNUNET_assert (GNUNET_YES ==
390                  GNUNET_CONTAINER_multihashmap_remove(plugin->session_map, &s->target.hashPubKey, s));
391
392   GNUNET_STATISTICS_set(plugin->env->stats,
393                         "# UNIX sessions active",
394                         GNUNET_CONTAINER_multihashmap_size(plugin->session_map),
395                         GNUNET_NO);
396
397   GNUNET_free (s);
398 }
399
400 static int
401 get_session_delete_it (void *cls, const struct GNUNET_HashCode * key, void *value)
402 {
403   struct Session *s = value;
404   disconnect_session (s);
405   return GNUNET_YES;
406 }
407
408
409 /**
410  * Disconnect from a remote node.  Clean up session if we have one for this peer
411  *
412  * @param cls closure for this call (should be handle to Plugin)
413  * @param target the peeridentity of the peer to disconnect
414  * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
415  */
416 static void
417 unix_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
418 {
419   struct Plugin *plugin = cls;
420   GNUNET_assert (plugin != NULL);
421
422   GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map, &target->hashPubKey, &get_session_delete_it, plugin);
423   return;
424 }
425
426 /**
427  * Shutdown the server process (stop receiving inbound traffic). Maybe
428  * restarted later!
429  *
430  * @param cls Handle to the plugin for this transport
431  *
432  * @return returns the number of sockets successfully closed,
433  *         should equal the number of sockets successfully opened
434  */
435 static int
436 unix_transport_server_stop (void *cls)
437 {
438   struct Plugin *plugin = cls;
439
440   struct UNIXMessageWrapper * msgw = plugin->msg_head;
441
442   while (NULL != (msgw = plugin->msg_head))
443   {
444     GNUNET_CONTAINER_DLL_remove (plugin->msg_head, plugin->msg_tail, msgw);
445     if (msgw->cont != NULL)
446       msgw->cont (msgw->cont_cls,  &msgw->session->target, GNUNET_SYSERR,
447                   msgw->msgsize, 0);
448     GNUNET_free (msgw->msg);
449     GNUNET_free (msgw);
450   }
451
452   if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
453   {
454     GNUNET_SCHEDULER_cancel (plugin->select_task);
455     plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
456   }
457
458   if (NULL != plugin->unix_sock.desc)
459   {
460     GNUNET_break (GNUNET_OK ==
461                   GNUNET_NETWORK_socket_close (plugin->unix_sock.desc));
462     plugin->unix_sock.desc = NULL;
463     plugin->with_ws = GNUNET_NO;
464   }
465   return GNUNET_OK;
466 }
467
468
469 /**
470  * Actually send out the message, assume we've got the address and
471  * send_handle squared away!
472  *
473  * @param cls closure
474  * @param send_handle which handle to send message on
475  * @param target who should receive this message (ignored by UNIX)
476  * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
477  * @param msgbuf_size the size of the msgbuf to send
478  * @param priority how important is the message (ignored by UNIX)
479  * @param timeout when should we time out (give up) if we can not transmit?
480  * @param addr the addr to send the message to, needs to be a sockaddr for us
481  * @param addrlen the len of addr
482  * @param cont continuation to call once the message has
483  *        been transmitted (or if the transport is ready
484  *        for the next transmission call; or if the
485  *        peer disconnected...)
486  * @param cont_cls closure for cont
487  *
488  * @return the number of bytes written, -1 on errors
489  */
490 static ssize_t
491 unix_real_send (void *cls,
492                 struct GNUNET_NETWORK_Handle *send_handle,
493                 const struct GNUNET_PeerIdentity *target, const char *msgbuf,
494                 size_t msgbuf_size, unsigned int priority,
495                 struct GNUNET_TIME_Relative timeout, const void *addr,
496                 size_t addrlen, GNUNET_TRANSPORT_TransmitContinuation cont,
497                 void *cont_cls)
498 {
499   struct Plugin *plugin = cls;
500   ssize_t sent;
501   const void *sb;
502   size_t sbs;
503   struct sockaddr_un un;
504   size_t slen;
505
506   GNUNET_assert (NULL != plugin);
507
508   if (send_handle == NULL)
509   {
510     /* We do not have a send handle */
511     GNUNET_break (0);
512     if (cont != NULL)
513       cont (cont_cls, target, GNUNET_SYSERR, msgbuf_size, 0);
514     return -1;
515   }
516   if ((addr == NULL) || (addrlen == 0))
517   {
518     /* Can never send if we don't have an address */
519     GNUNET_break (0);
520     if (cont != NULL)
521       cont (cont_cls, target, GNUNET_SYSERR, msgbuf_size, 0);
522     return -1;
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   /* Send the data */
545   sent = 0;
546   sent = GNUNET_NETWORK_socket_sendto (send_handle, msgbuf, msgbuf_size, sb, sbs);
547
548   if ((GNUNET_SYSERR == sent) && ((errno == EAGAIN) || (errno == ENOBUFS)))
549   {
550     /* We have to retry later: retry */
551     return 0;
552   }
553
554   if ((GNUNET_SYSERR == sent) && (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
563     if (size < msgbuf_size)
564     {
565       LOG (GNUNET_ERROR_TYPE_DEBUG,
566                   "Trying to increase socket buffer size from %i to %i for message size %i\n",
567                   size,
568                   ((msgbuf_size / 1000) + 2) * 1000,
569                   msgbuf_size);
570       size = ((msgbuf_size / 1000) + 2) * 1000;
571       if (GNUNET_NETWORK_socket_setsockopt
572           ((struct GNUNET_NETWORK_Handle *) send_handle, SOL_SOCKET, SO_SNDBUF,
573            &size, sizeof (size)) == GNUNET_OK)
574       {
575         /* Increased buffer size, retry sending */
576         return 0;
577       }
578       else
579       {
580         /* Could not increase buffer size: error, no retry */
581         GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsockopt");
582         return -1;
583       }
584     }
585     else
586     {
587       /* Buffer is bigger than message:  error, no retry
588        * This should never happen!*/
589       GNUNET_break (0);
590       return -1;
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
599   /* Calling continuation */
600   if (cont != NULL)
601   {
602     if (sent == GNUNET_SYSERR)
603       cont (cont_cls, target, GNUNET_SYSERR, msgbuf_size, 0);
604     if (sent > 0)
605       cont (cont_cls, target, GNUNET_OK, msgbuf_size, msgbuf_size);
606   }
607
608   /* return number of bytes successfully sent */
609   if (sent > 0)
610     return sent;
611   if (sent == 0)
612   {
613     /* That should never happen */
614     GNUNET_break (0);
615     return -1;
616   }
617   /* failed and retry: return 0 */
618   if (GNUNET_SYSERR == sent)
619     return 0;
620   /* default */
621   return -1;
622 }
623
624 struct gsi_ctx
625 {
626   char *address;
627   size_t addrlen;
628   struct Session *res;
629 };
630
631
632 static int
633 get_session_it (void *cls, const struct GNUNET_HashCode * key, void *value)
634 {
635   struct gsi_ctx *gsi = cls;
636   struct Session *s = value;
637
638   LOG (GNUNET_ERROR_TYPE_DEBUG, "Comparing session %s %s\n", gsi->address, s->addr);
639   if ((gsi->addrlen == s->addrlen) &&
640       (0 == memcmp (gsi->address, s->addr, s->addrlen)))
641   {
642     gsi->res = s;
643     return GNUNET_NO;
644   }
645   return GNUNET_YES;
646 }
647
648 /**
649  * Creates a new outbound session the transport service will use to send data to the
650  * peer
651  *
652  * @param cls the plugin
653  * @param address the address
654  * @return the session or NULL of max connections exceeded
655  */
656 static struct Session *
657 unix_plugin_get_session (void *cls,
658                   const struct GNUNET_HELLO_Address *address)
659 {
660   struct Session * s = NULL;
661   struct Plugin *plugin = cls;
662   struct gsi_ctx gsi;
663
664   /* Checks */
665   GNUNET_assert (plugin != NULL);
666   GNUNET_assert (address != NULL);
667
668   /* Check if already existing */
669   gsi.address = (char *) address->address;
670   gsi.addrlen = address->address_length;
671   gsi.res = NULL;
672   GNUNET_CONTAINER_multihashmap_get_multiple (plugin->session_map, &address->peer.hashPubKey, &get_session_it, &gsi);
673   if (gsi.res != NULL)
674   {
675     LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing session\n");
676     return gsi.res;
677   }
678
679   /* Create a new session */
680   s = GNUNET_malloc (sizeof (struct Session) + address->address_length);
681   s->addr = &s[1];
682   s->addrlen = address->address_length;
683   s->plugin = plugin;
684   memcpy(s->addr, address->address, s->addrlen);
685   memcpy(&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
686
687   start_session_timeout (s);
688
689   GNUNET_CONTAINER_multihashmap_put (plugin->session_map,
690       &address->peer.hashPubKey, s,
691       GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
692
693   GNUNET_STATISTICS_set(plugin->env->stats,
694                         "# UNIX sessions active",
695                         GNUNET_CONTAINER_multihashmap_size(plugin->session_map),
696                         GNUNET_NO);
697   LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating new session\n");
698   return s;
699 }
700
701 /*
702  * @param cls the plugin handle
703  * @param tc the scheduling context (for rescheduling this function again)
704  *
705  * We have been notified that our writeset has something to read.  We don't
706  * know which socket needs to be read, so we have to check each one
707  * Then reschedule this function to be called again once more is available.
708  *
709  */
710 static void
711 unix_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
712
713 /**
714  * Function that can be used by the transport service to transmit
715  * a message using the plugin.   Note that in the case of a
716  * peer disconnecting, the continuation MUST be called
717  * prior to the disconnect notification itself.  This function
718  * will be called with this peer's HELLO message to initiate
719  * a fresh connection to another peer.
720  *
721  * @param cls closure
722  * @param session which session must be used
723  * @param msgbuf the message to transmit
724  * @param msgbuf_size number of bytes in 'msgbuf'
725  * @param priority how important is the message (most plugins will
726  *                 ignore message priority and just FIFO)
727  * @param to how long to wait at most for the transmission (does not
728  *                require plugins to discard the message after the timeout,
729  *                just advisory for the desired delay; most plugins will ignore
730  *                this as well)
731  * @param cont continuation to call once the message has
732  *        been transmitted (or if the transport is ready
733  *        for the next transmission call; or if the
734  *        peer disconnected...); can be NULL
735  * @param cont_cls closure for cont
736  * @return number of bytes used (on the physical network, with overheads);
737  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
738  *         and does NOT mean that the message was not transmitted (DV)
739  */
740 static ssize_t
741 unix_plugin_send (void *cls,
742                   struct Session *session,
743                   const char *msgbuf, size_t msgbuf_size,
744                   unsigned int priority,
745                   struct GNUNET_TIME_Relative to,
746                   GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
747 {
748   struct Plugin *plugin = cls;
749   struct UNIXMessageWrapper *wrapper;
750   struct UNIXMessage *message;
751   int ssize;
752   
753   GNUNET_assert (plugin != NULL);
754   GNUNET_assert (session != NULL);
755
756   if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_contains_value(plugin->session_map,
757       &session->target.hashPubKey, session))
758   {
759     LOG (GNUNET_ERROR_TYPE_ERROR, "Invalid session for peer `%s' `%s'\n",
760                 GNUNET_i2s (&session->target),
761                 (char *) session->addr);
762     GNUNET_break (0);
763
764     return GNUNET_SYSERR;
765   }
766   LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending %u bytes with session for peer `%s' `%s'\n",
767                 msgbuf_size,
768         GNUNET_i2s (&session->target),
769         (char *) session->addr);
770
771   ssize = sizeof (struct UNIXMessage) + msgbuf_size;
772   message = GNUNET_malloc (sizeof (struct UNIXMessage) + msgbuf_size);
773   message->header.size = htons (ssize);
774   message->header.type = htons (0);
775   memcpy (&message->sender, plugin->env->my_identity,
776           sizeof (struct GNUNET_PeerIdentity));
777   memcpy (&message[1], msgbuf, msgbuf_size);
778
779   reschedule_session_timeout (session);
780
781   wrapper = GNUNET_malloc (sizeof (struct UNIXMessageWrapper));
782   wrapper->msg = message;
783   wrapper->msgsize = ssize;
784   wrapper->priority = priority;
785   wrapper->timeout = to;
786   wrapper->cont = cont;
787   wrapper->cont_cls = cont_cls;
788   wrapper->session = session;
789
790   GNUNET_CONTAINER_DLL_insert(plugin->msg_head, plugin->msg_tail, wrapper);
791
792   plugin->bytes_in_queue += ssize;
793   GNUNET_STATISTICS_set (plugin->env->stats,"# bytes currently in UNIX buffers",
794       plugin->bytes_in_queue, GNUNET_NO);
795
796   LOG (GNUNET_ERROR_TYPE_DEBUG, "Sent %d bytes to `%s'\n", ssize,
797               (char *) session->addr);
798   if (plugin->with_ws == GNUNET_NO)
799   {
800     reschedule_select (plugin);
801   }
802
803   return ssize;
804 }
805
806
807 /**
808  * Demultiplexer for UNIX messages
809  *
810  * @param plugin the main plugin for this transport
811  * @param sender from which peer the message was received
812  * @param currhdr pointer to the header of the message
813  * @param un the address from which the message was received
814  * @param fromlen the length of the address
815  */
816 static void
817 unix_demultiplexer (struct Plugin *plugin, struct GNUNET_PeerIdentity *sender,
818                     const struct GNUNET_MessageHeader *currhdr,
819                     const struct sockaddr_un *un, size_t fromlen)
820 {
821   struct GNUNET_ATS_Information ats[2];
822   struct Session *s = NULL;
823   struct GNUNET_HELLO_Address * addr;
824
825   ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
826   ats[0].value = htonl (UNIX_DIRECT_DISTANCE);
827   ats[1] = plugin->ats_network;
828   GNUNET_break (ntohl(plugin->ats_network.value) != GNUNET_ATS_NET_UNSPECIFIED);
829
830   GNUNET_assert (fromlen >= sizeof (struct sockaddr_un));
831
832   LOG (GNUNET_ERROR_TYPE_DEBUG, "Received message from %s\n",
833               un->sun_path);
834
835
836
837   plugin->bytes_in_recv += ntohs(currhdr->size);
838   GNUNET_STATISTICS_set (plugin->env->stats,"# bytes received via UNIX",
839       plugin->bytes_in_recv, GNUNET_NO);
840
841   addr = GNUNET_HELLO_address_allocate(sender, "unix", un->sun_path, strlen (un->sun_path) + 1);
842   s = lookup_session (plugin, sender, un);
843   if (NULL == s)
844     s = unix_plugin_get_session (plugin, addr);
845   reschedule_session_timeout (s);
846
847   plugin->env->receive (plugin->env->cls, sender, currhdr,
848                         (const struct GNUNET_ATS_Information *) &ats, 2,
849                         s, un->sun_path, strlen (un->sun_path) + 1);
850   GNUNET_free (addr);
851 }
852
853
854 static void
855 unix_plugin_select_read (struct Plugin * plugin)
856 {
857   char buf[65536] GNUNET_ALIGN;
858   struct UNIXMessage *msg;
859   struct GNUNET_PeerIdentity sender;
860   struct sockaddr_un un;
861   socklen_t addrlen;
862   ssize_t ret;
863   int offset;
864   int tsize;
865   char *msgbuf;
866   const struct GNUNET_MessageHeader *currhdr;
867   uint16_t csize;
868
869   addrlen = sizeof (un);
870   memset (&un, 0, sizeof (un));
871
872   ret =
873       GNUNET_NETWORK_socket_recvfrom (plugin->unix_sock.desc, buf, sizeof (buf),
874                                       (struct sockaddr *) &un, &addrlen);
875
876   if ((GNUNET_SYSERR == ret) && ((errno == EAGAIN) || (errno == ENOBUFS)))
877     return;
878
879   if (ret == GNUNET_SYSERR)
880   {
881     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom");
882     return;
883   }
884   else
885   {
886 #if LINUX
887     un.sun_path[0] = '/';
888 #endif
889     LOG (GNUNET_ERROR_TYPE_DEBUG, "Read %d bytes from socket %s\n", ret,
890                 &un.sun_path[0]);
891   }
892
893   GNUNET_assert (AF_UNIX == (un.sun_family));
894
895   msg = (struct UNIXMessage *) buf;
896   csize = ntohs (msg->header.size);
897   if ((csize < sizeof (struct UNIXMessage)) || (csize > ret))
898   {
899     GNUNET_break_op (0);
900     return;
901   }
902   msgbuf = (char *) &msg[1];
903   memcpy (&sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
904   offset = 0;
905   tsize = csize - sizeof (struct UNIXMessage);
906   while (offset + sizeof (struct GNUNET_MessageHeader) <= tsize)
907   {
908     currhdr = (struct GNUNET_MessageHeader *) &msgbuf[offset];
909     csize = ntohs (currhdr->size);
910     if ((csize < sizeof (struct GNUNET_MessageHeader)) ||
911         (csize > tsize - offset))
912     {
913       GNUNET_break_op (0);
914       break;
915     }
916
917     unix_demultiplexer (plugin, &sender, currhdr, &un, sizeof (un));
918     offset += csize;
919   }
920 }
921
922
923 static void
924 unix_plugin_select_write (struct Plugin * plugin)
925 {
926   static int retry_counter;
927   int sent = 0;
928   struct UNIXMessageWrapper * msgw = plugin->msg_head;
929
930   sent = unix_real_send (plugin,
931                          plugin->unix_sock.desc,
932                          &msgw->session->target,
933                          (const char *) msgw->msg,
934                          msgw->msgsize,
935                          msgw->priority,
936                          msgw->timeout,
937                          msgw->session->addr,
938                          msgw->session->addrlen,
939                          msgw->cont, msgw->cont_cls);
940
941   if (sent == 0)
942   {
943     /* failed and retry */
944     retry_counter++;
945     GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX retry attempt",
946         retry_counter, GNUNET_NO);
947     return;
948   }
949
950   if (retry_counter > 0 )
951   {
952     /* no retry: reset counter */
953     retry_counter = 0;
954     GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX retry attempt",
955         retry_counter, GNUNET_NO);
956   }
957
958   if (sent == -1)
959   {
960     /* failed and no retry */
961     GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
962
963     GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
964     plugin->bytes_in_queue -= msgw->msgsize;
965     GNUNET_STATISTICS_set (plugin->env->stats, "# bytes currently in UNIX buffers",
966         plugin->bytes_in_queue, GNUNET_NO);
967     plugin->bytes_discarded += msgw->msgsize;
968     GNUNET_STATISTICS_set (plugin->env->stats,"# UNIX bytes discarded",
969         plugin->bytes_discarded, GNUNET_NO);
970
971     GNUNET_free (msgw->msg);
972     GNUNET_free (msgw);
973     return;
974   }
975
976   if (sent > 0)
977   {
978     /* successfully sent bytes */
979     GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, msgw);
980
981     GNUNET_assert (plugin->bytes_in_queue >= msgw->msgsize);
982     plugin->bytes_in_queue -= msgw->msgsize;
983     GNUNET_STATISTICS_set (plugin->env->stats,"# bytes currently in UNIX buffers",
984         plugin->bytes_in_queue, GNUNET_NO);
985     plugin->bytes_in_sent += msgw->msgsize;
986     GNUNET_STATISTICS_set (plugin->env->stats,"# bytes transmitted via UNIX",
987         plugin->bytes_in_sent, GNUNET_NO);
988
989     GNUNET_free (msgw->msg);
990     GNUNET_free (msgw);
991     return;
992   }
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   GNUNET_assert( NULL != env->stats);
1334
1335   if (GNUNET_OK !=
1336       GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-unix", "PORT",
1337                                              &port))
1338     port = UNIX_NAT_DEFAULT_PORT;
1339   plugin = GNUNET_malloc (sizeof (struct Plugin));
1340   plugin->port = port;
1341   plugin->env = env;
1342   GNUNET_asprintf (&plugin->unix_socket_path, "/tmp/unix-plugin-sock.%d",
1343                    plugin->port);
1344
1345   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1346   api->cls = plugin;
1347
1348   api->get_session = &unix_plugin_get_session;
1349   api->send = &unix_plugin_send;
1350   api->disconnect = &unix_disconnect;
1351   api->address_pretty_printer = &unix_plugin_address_pretty_printer;
1352   api->address_to_string = &unix_address_to_string;
1353   api->check_address = &unix_check_address;
1354   api->string_to_address = &unix_string_to_address;
1355   sockets_created = unix_transport_server_start (plugin);
1356   if (sockets_created == 0)
1357     LOG (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UNIX sockets\n"));
1358
1359   plugin->session_map = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
1360
1361   GNUNET_SCHEDULER_add_now (address_notification, plugin);
1362   return api;
1363 }
1364
1365 void *
1366 libgnunet_plugin_transport_unix_done (void *cls)
1367 {
1368   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1369   struct Plugin *plugin = api->cls;
1370
1371   if (NULL == plugin)
1372   {
1373     GNUNET_free (api);
1374     return NULL;
1375   }
1376
1377   plugin->env->notify_address (plugin->env->cls, GNUNET_NO,
1378                                plugin->unix_socket_path,
1379                                strlen (plugin->unix_socket_path) + 1,
1380                                "unix");
1381
1382   unix_transport_server_stop (plugin);
1383
1384
1385   GNUNET_CONTAINER_multihashmap_iterate (plugin->session_map, &get_session_delete_it, plugin);
1386   GNUNET_CONTAINER_multihashmap_destroy (plugin->session_map);
1387
1388
1389   if (NULL != plugin->rs)
1390     GNUNET_NETWORK_fdset_destroy (plugin->rs);
1391   if (NULL != plugin->ws)
1392     GNUNET_NETWORK_fdset_destroy (plugin->ws);
1393   GNUNET_free (plugin->unix_socket_path);
1394   GNUNET_free (plugin);
1395   GNUNET_free (api);
1396   return NULL;
1397 }
1398
1399 /* end of plugin_transport_unix.c */