-fix time assertion introduce in last patch
[oweals/gnunet.git] / src / transport / plugin_transport_bluetooth.c
1 /*
2   This file is part of GNUnet
3   (C) 2010, 2011, 2012 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_bluetooth.c
23  * @brief transport plugin for bluetooth
24  * @author David Brodski
25  * @author Christian Grothoff
26  *
27  * THIS IS A COPY OF plugin_transport_wlan.c
28  */
29 #include "platform.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_util_lib.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_transport_service.h"
35 #include "gnunet_transport_plugin.h"
36 #include "plugin_transport_wlan.h"
37 #include "gnunet_common.h"
38 #include "gnunet_crypto_lib.h"
39 #include "gnunet_fragmentation_lib.h"
40 #include "gnunet_constants.h"
41
42 #ifdef MINGW
43  #undef interface
44 #endif
45
46 #define LOG(kind,...) GNUNET_log_from (kind, "transport-bluetooth",__VA_ARGS__)
47
48 #define PLUGIN_NAME "bluetooth"
49
50 /**
51  * Max size of packet (that we give to the WLAN driver for transmission)
52  */
53 #define WLAN_MTU 1430
54
55 /**
56  * time out of a mac endpoint
57  */
58 #define MACENDPOINT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 60)
59
60 /**
61  * We reduce the frequence of HELLO beacons in relation to
62  * the number of MAC addresses currently visible to us.
63  * This is the multiplication factor.
64  */
65 #define HELLO_BEACON_SCALING_FACTOR GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
66
67 /**
68  * Maximum number of messages in defragmentation queue per MAC
69  */
70 #define MESSAGES_IN_DEFRAG_QUEUE_PER_MAC 2
71
72 /**
73  * Link layer control fields for better compatibility
74  * (i.e. GNUnet over WLAN is not IP-over-WLAN).
75  */
76 #define WLAN_LLC_DSAP_FIELD 0x1f
77 #define WLAN_LLC_SSAP_FIELD 0x1f
78
79
80 GNUNET_NETWORK_STRUCT_BEGIN
81 /**
82  * Header for messages which need fragmentation.  This is the format of
83  * a message we obtain AFTER defragmentation.  We then need to check
84  * the CRC and then tokenize the payload and pass it to the
85  * 'receive' callback.
86  */
87 struct WlanHeader
88 {
89
90   /**
91    * Message type is GNUNET_MESSAGE_TYPE_WLAN_DATA.
92    */
93   struct GNUNET_MessageHeader header;
94
95   /**
96    * CRC32 checksum (only over the payload), in NBO.
97    */
98   uint32_t crc GNUNET_PACKED;
99
100   /**
101    * Sender of the message.
102    */
103   struct GNUNET_PeerIdentity sender;
104
105   /**
106    * Target of the message.
107    */
108   struct GNUNET_PeerIdentity target;
109
110   /* followed by payload, possibly including
111      multiple messages! */
112
113 };
114
115
116 struct WlanAddress
117 {
118   uint32_t options GNUNET_PACKED;
119
120   struct GNUNET_TRANSPORT_WLAN_MacAddress mac;
121 };
122
123
124 GNUNET_NETWORK_STRUCT_END
125
126
127 /**
128  * Information kept for each message that is yet to be fragmented and
129  * transmitted.
130  */
131 struct PendingMessage
132 {
133   /**
134    * next entry in the DLL
135    */
136   struct PendingMessage *next;
137
138   /**
139    * previous entry in the DLL
140    */
141   struct PendingMessage *prev;
142
143   /**
144    * The pending message
145    */
146   struct WlanHeader *msg;
147
148   /**
149    * Continuation function to call once the message
150    * has been sent.  Can be NULL if there is no
151    * continuation to call.
152    */
153   GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
154
155   /**
156    * Cls for transmit_cont
157    */
158   void *transmit_cont_cls;
159
160   /**
161    * Timeout task (for this message).
162    */
163   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
164
165 };
166
167
168 /**
169  * Session handle for connections with other peers.
170  */
171 struct Session
172 {
173   /**
174    * To whom are we talking to (set to our identity
175    * if we are still waiting for the welcome message)
176    */
177   struct GNUNET_PeerIdentity target;
178
179   /**
180    * API requirement (must be first).
181    */
182   struct SessionHeader header;
183
184   /**
185    * We keep all sessions in a DLL at their respective
186    * 'struct MACEndpoint'.
187    */
188   struct Session *next;
189
190   /**
191    * We keep all sessions in a DLL at their respective
192    * 'struct MACEndpoint'.
193    */
194   struct Session *prev;
195
196   /**
197    * MAC endpoint with the address of this peer.
198    */
199   struct MacEndpoint *mac;
200
201   /**
202    * The address for this session
203    */
204   struct GNUNET_HELLO_Address *address;
205
206   /**
207    * Head of messages currently pending for transmission to this peer.
208    */
209   struct PendingMessage *pending_message_head;
210
211   /**
212    * Tail of messages currently pending for transmission to this peer.
213    */
214   struct PendingMessage *pending_message_tail;
215
216   /**
217    * When should this session time out?
218    */
219   struct GNUNET_TIME_Absolute timeout;
220
221   /**
222    * Timeout task (for the session).
223    */
224   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
225
226 };
227
228
229 /**
230  * Struct for messages that are being fragmented in a MAC's transmission queue.
231  */
232 struct FragmentMessage
233 {
234
235   /**
236    * This is a doubly-linked list.
237    */
238   struct FragmentMessage *next;
239
240   /**
241    * This is a doubly-linked list.
242    */
243   struct FragmentMessage *prev;
244
245   /**
246    * MAC endpoint this message belongs to
247    */
248   struct MacEndpoint *macendpoint;
249
250   /**
251    * Fragmentation context
252    */
253   struct GNUNET_FRAGMENT_Context *fragcontext;
254
255   /**
256    * Transmission handle to helper (to cancel if the frag context
257    * is destroyed early for some reason).
258    */
259   struct GNUNET_HELPER_SendHandle *sh;
260
261   /**
262    * Intended recipient.
263    */
264   struct GNUNET_PeerIdentity target;
265
266   /**
267    * Timeout value for the message.
268    */
269   struct GNUNET_TIME_Absolute timeout;
270
271   /**
272    * Timeout task.
273    */
274   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
275
276   /**
277    * Continuation to call when we're done with this message.
278    */
279   GNUNET_TRANSPORT_TransmitContinuation cont;
280
281   /**
282    * Closure for 'cont'
283    */
284   void *cont_cls;
285
286   /**
287    * Size of original message
288    */
289   size_t size_payload;
290
291   /**
292    * Number of bytes used to transmit message
293    */
294   size_t size_on_wire;
295
296 };
297
298
299 /**
300  * Struct to represent one network card connection
301  */
302 struct MacEndpoint
303 {
304
305   /**
306    * We keep all MACs in a DLL in the plugin.
307    */
308   struct MacEndpoint *next;
309
310   /**
311    * We keep all MACs in a DLL in the plugin.
312    */
313   struct MacEndpoint *prev;
314
315   /**
316    * Pointer to the global plugin struct.
317    */
318   struct Plugin *plugin;
319
320   /**
321    * Head of sessions that use this MAC.
322    */
323   struct Session *sessions_head;
324
325   /**
326    * Tail of sessions that use this MAC.
327    */
328   struct Session *sessions_tail;
329
330   /**
331    * Head of messages we are currently sending to this MAC.
332    */
333   struct FragmentMessage *sending_messages_head;
334
335   /**
336    * Tail of messages we are currently sending to this MAC.
337    */
338   struct FragmentMessage *sending_messages_tail;
339
340   /**
341    * Defrag context for this MAC
342    */
343   struct GNUNET_DEFRAGMENT_Context *defrag;
344
345   /**
346    * When should this endpoint time out?
347    */
348   struct GNUNET_TIME_Absolute timeout;
349
350   /**
351    * Timeout task.
352    */
353   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
354
355   /**
356    * count of messages in the fragment out queue for this mac endpoint
357    */
358   unsigned int fragment_messages_out_count;
359
360   /**
361    * peer mac address
362    */
363   struct WlanAddress addr;
364
365   /**
366    * Message delay for fragmentation context
367    */
368   struct GNUNET_TIME_Relative msg_delay;
369
370   /**
371    * ACK delay for fragmentation context
372    */
373   struct GNUNET_TIME_Relative ack_delay;
374
375   /**
376    * Desired transmission power for this MAC
377    */
378   uint16_t tx_power;
379
380   /**
381    * Desired transmission rate for this MAC
382    */
383   uint8_t rate;
384
385   /**
386    * Antenna we should use for this MAC
387    */
388   uint8_t antenna;
389
390 };
391
392
393 /**
394  * Encapsulation of all of the state of the plugin.
395  */
396 struct Plugin
397 {
398   /**
399    * Our environment.
400    */
401   struct GNUNET_TRANSPORT_PluginEnvironment *env;
402
403   /**
404    * Handle to helper process for priviledged operations.
405    */
406   struct GNUNET_HELPER_Handle *suid_helper;
407
408   /**
409    * ARGV-vector for the helper (all helpers take only the binary
410    * name, one actual argument, plus the NULL terminator for 'argv').
411    */
412   char * helper_argv[3];
413
414   /**
415    * The interface of the wlan card given to us by the user.
416    */
417   char *interface;
418
419   /**
420    * Tokenizer for demultiplexing of data packets resulting from defragmentation.
421    */
422   struct GNUNET_SERVER_MessageStreamTokenizer *fragment_data_tokenizer;
423
424   /**
425    * Tokenizer for demultiplexing of data packets received from the suid helper
426    */
427   struct GNUNET_SERVER_MessageStreamTokenizer *helper_payload_tokenizer;
428
429   /**
430    * Tokenizer for demultiplexing of data packets that follow the WLAN Header
431    */
432   struct GNUNET_SERVER_MessageStreamTokenizer *wlan_header_payload_tokenizer;
433
434   /**
435    * Head of list of open connections.
436    */
437   struct MacEndpoint *mac_head;
438
439   /**
440    * Tail of list of open connections.
441    */
442   struct MacEndpoint *mac_tail;
443
444   /**
445    * Number of connections
446    */
447   unsigned int mac_count;
448
449   /**
450    * Task that periodically sends a HELLO beacon via the helper.
451    */
452   GNUNET_SCHEDULER_TaskIdentifier beacon_task;
453
454   /**
455    * Tracker for bandwidth limit
456    */
457   struct GNUNET_BANDWIDTH_Tracker tracker;
458
459   /**
460    * The mac_address of the wlan card given to us by the helper.
461    */
462   struct GNUNET_TRANSPORT_WLAN_MacAddress mac_address;
463
464   /**
465    * Have we received a control message with our MAC address yet?
466    */
467   int have_mac;
468
469   /**
470    * Options for addresses
471    */
472   uint32_t options;
473
474 };
475
476
477 /**
478  * Information associated with a message.  Can contain
479  * the session or the MAC endpoint associated with the
480  * message (or both).
481  */
482 struct MacAndSession
483 {
484   /**
485    * NULL if the identity of the other peer is not known.
486    */
487   struct Session *session;
488
489   /**
490    * MAC address of the other peer, NULL if not known.
491    */
492   struct MacEndpoint *endpoint;
493 };
494
495 /**
496  * Function called for a quick conversion of the binary address to
497  * a numeric address.  Note that the caller must not free the
498  * address and that the next call to this function is allowed
499  * to override the address again.
500  *
501  * @param cls closure
502  * @param addr binary address
503  * @param addrlen length of the address
504  * @return string representing the same address
505  */
506 static const char *
507 bluetooth_plugin_address_to_string (void *cls,
508                                     const void *addr,
509                                     size_t addrlen);
510
511 /**
512  * Print MAC addresses nicely.
513  *
514  * @param mac the mac address
515  * @return string to a static buffer with the human-readable mac, will be overwritten during the next call to this function
516  */
517 static const char *
518 mac_to_string (const struct GNUNET_TRANSPORT_WLAN_MacAddress * mac)
519 {
520   static char macstr[20];
521
522   GNUNET_snprintf (macstr, sizeof (macstr), "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
523                                                                  mac->mac[0], mac->mac[1],
524                    mac->mac[2], mac->mac[3], mac->mac[4], mac->mac[5]);
525   return macstr;
526 }
527
528
529 /**
530  * Fill the radiotap header
531  *
532  * @param endpoint pointer to the endpoint, can be NULL
533  * @param header pointer to the radiotap header
534  * @param size total message size
535  */
536 static void
537 get_radiotap_header (struct MacEndpoint *endpoint,
538                      struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *header,
539                      uint16_t size)
540 {
541   header->header.type = ntohs (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER);
542   header->header.size = ntohs (size);
543   if (NULL != endpoint)
544   {
545     header->rate = endpoint->rate;
546     header->tx_power = endpoint->tx_power;
547     header->antenna = endpoint->antenna;
548   }
549   else
550   {
551     header->rate = 255;
552     header->tx_power = 0;
553     header->antenna = 0;
554   }
555 }
556
557
558 /**
559  * Generate the WLAN hardware header for one packet
560  *
561  * @param plugin the plugin handle
562  * @param header address to write the header to
563  * @param to_mac_addr address of the recipient
564  * @param size size of the whole packet, needed to calculate the time to send the packet
565  */
566 static void
567 get_wlan_header (struct Plugin *plugin,
568                  struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *header,
569                  const struct GNUNET_TRANSPORT_WLAN_MacAddress *to_mac_addr,
570                  unsigned int size)
571 {
572   const int rate = 11000000;
573
574   header->frame_control = htons (IEEE80211_FC0_TYPE_DATA);
575   header->addr1 = *to_mac_addr;
576   header->addr2 = plugin->mac_address;
577   header->addr3 = mac_bssid_gnunet;
578   header->duration = GNUNET_htole16 ((size * 1000000) / rate + 290);
579   header->sequence_control = 0; // FIXME?
580   header->llc[0] = WLAN_LLC_DSAP_FIELD;
581   header->llc[1] = WLAN_LLC_SSAP_FIELD;
582   header->llc[2] = 0;  // FIXME?
583   header->llc[3] = 0;  // FIXME?
584 }
585
586
587 /**
588  * Send an ACK for a fragment we received.
589  *
590  * @param cls the 'struct MacEndpoint' the ACK must be sent to
591  * @param msg_id id of the message
592  * @param hdr pointer to the hdr where the ack is stored
593  */
594 static void
595 send_ack (void *cls, uint32_t msg_id,
596           const struct GNUNET_MessageHeader *hdr)
597 {
598   struct MacEndpoint *endpoint = cls;
599   struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage* radio_header;
600   uint16_t msize = ntohs (hdr->size);
601   size_t size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + msize;
602   char buf[size];
603
604   if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
605   {
606     GNUNET_break (0);
607     return;
608   }
609   LOG (GNUNET_ERROR_TYPE_DEBUG,
610        "Sending ACK to helper\n");
611   radio_header = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) buf;
612   get_radiotap_header (endpoint, radio_header, size);
613   get_wlan_header (endpoint->plugin,
614                    &radio_header->frame,
615                    &endpoint->addr.mac,
616                    sizeof (endpoint->addr.mac));
617   memcpy (&radio_header[1], hdr, msize);
618   if (NULL !=
619       GNUNET_HELPER_send (endpoint->plugin->suid_helper,
620                           &radio_header->header,
621                           GNUNET_NO /* dropping ACKs is bad */,
622                           NULL, NULL))
623     GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# Bluetooth ACKs sent"),
624                               1, GNUNET_NO);
625 }
626
627
628 /**
629  * Handles the data after all fragments are put together
630  *
631  * @param cls macendpoint this messages belongs to
632  * @param hdr pointer to the data
633  */
634 static void
635 bluetooth_data_message_handler (void *cls, const struct GNUNET_MessageHeader *hdr)
636 {
637   struct MacEndpoint *endpoint = cls;
638   struct Plugin *plugin = endpoint->plugin;
639   struct MacAndSession mas;
640
641   GNUNET_STATISTICS_update (plugin->env->stats,
642                             _("# Bluetooth messages defragmented"), 1,
643                             GNUNET_NO);
644   mas.session = NULL;
645   mas.endpoint = endpoint;
646   (void) GNUNET_SERVER_mst_receive (plugin->fragment_data_tokenizer,
647                                     &mas,
648                                     (const char *) hdr,
649                                     ntohs (hdr->size),
650                                     GNUNET_YES, GNUNET_NO);
651 }
652
653
654 /**
655  * Free a session
656  *
657  * @param session the session free
658  */
659 static void
660 free_session (struct Session *session)
661 {
662   struct MacEndpoint *endpoint = session->mac;
663   struct PendingMessage *pm;
664
665   endpoint->plugin->env->session_end (endpoint->plugin->env->cls,
666                                       session->address,
667                                       session);
668   while (NULL != (pm = session->pending_message_head))
669   {
670     GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
671                                  session->pending_message_tail, pm);
672     if (GNUNET_SCHEDULER_NO_TASK != pm->timeout_task)
673     {
674       GNUNET_SCHEDULER_cancel (pm->timeout_task);
675       pm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
676     }
677     GNUNET_free (pm->msg);
678     GNUNET_free (pm);
679   }
680   GNUNET_CONTAINER_DLL_remove (endpoint->sessions_head,
681                                endpoint->sessions_tail,
682                                session);
683   if (session->timeout_task != GNUNET_SCHEDULER_NO_TASK)
684   {
685     GNUNET_SCHEDULER_cancel (session->timeout_task);
686     session->timeout_task = GNUNET_SCHEDULER_NO_TASK;
687   }
688   GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# Bluetooth sessions allocated"), -1,
689                             GNUNET_NO);
690   GNUNET_HELLO_address_free (session->address);
691   GNUNET_free (session);
692 }
693
694
695 /**
696  * A session is timing out.  Clean up.
697  *
698  * @param cls pointer to the Session
699  * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
700  */
701 static void
702 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
703 {
704   struct Session * session = cls;
705   struct GNUNET_TIME_Relative timeout;
706
707   session->timeout_task = GNUNET_SCHEDULER_NO_TASK;
708   timeout = GNUNET_TIME_absolute_get_remaining (session->timeout);
709   if (0 == timeout.rel_value_us)
710   {
711     free_session (session);
712     return;
713   }
714   session->timeout_task =
715     GNUNET_SCHEDULER_add_delayed (timeout, &session_timeout, session);
716 }
717
718
719 /**
720  * Create a new session
721  *
722  * @param endpoint pointer to the mac endpoint of the peer
723  * @param peer peer identity to use for this session
724  * @return returns the session
725  */
726 static struct Session *
727 create_session (struct MacEndpoint *endpoint,
728                 const struct GNUNET_PeerIdentity *peer)
729 {
730   struct Session *session;
731
732   for (session = endpoint->sessions_head; NULL != session; session = session->next)
733     if (0 == memcmp (peer, &session->target,
734                      sizeof (struct GNUNET_PeerIdentity)))
735     {
736       session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
737       return session;
738     }
739   GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# Bluetooth sessions allocated"), 1,
740                             GNUNET_NO);
741   session = GNUNET_new (struct Session);
742   GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
743                                     endpoint->sessions_tail,
744                                     session);
745   session->address = GNUNET_HELLO_address_allocate (peer, PLUGIN_NAME,
746      &endpoint->addr, sizeof (endpoint->addr), GNUNET_HELLO_ADDRESS_INFO_NONE);
747   session->mac = endpoint;
748   session->target = *peer;
749   session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
750   session->timeout_task =
751       GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &session_timeout, session);
752   LOG (GNUNET_ERROR_TYPE_DEBUG,
753        "Created new session for peer `%s' with endpoint %s\n",
754        GNUNET_i2s (peer),
755        mac_to_string (&endpoint->addr.mac));
756   return session;
757 }
758
759
760 /**
761  * Lookup a new session
762  *
763  * @param endpoint pointer to the mac endpoint of the peer
764  * @param peer peer identity to use for this session
765  * @return returns the session or NULL
766  */
767 static struct Session *
768 lookup_session (struct MacEndpoint *endpoint,
769                 const struct GNUNET_PeerIdentity *peer)
770 {
771   struct Session *session;
772
773   for (session = endpoint->sessions_head; NULL != session; session = session->next)
774     if (0 == memcmp (peer, &session->target, sizeof (struct GNUNET_PeerIdentity)))
775     {
776       session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
777       return session;
778     }
779   return NULL;
780 }
781
782
783 /**
784  * Function called once we have successfully given the fragment
785  * message to the SUID helper process and we are thus ready for
786  * the next fragment.
787  *
788  * @param cls the 'struct FragmentMessage'
789  * @param result result of the operation (GNUNET_OK on success, GNUNET_NO if the helper died, GNUNET_SYSERR
790  *        if the helper was stopped)
791  */
792 static void
793 fragment_transmission_done (void *cls,
794                             int result)
795 {
796   struct FragmentMessage *fm = cls;
797
798
799   fm->sh = NULL;
800   GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
801 }
802
803
804 /**
805  * Transmit a fragment of a message.
806  *
807  * @param cls 'struct FragmentMessage' this fragment message belongs to
808  * @param hdr pointer to the start of the fragment message
809  */
810 static void
811 transmit_fragment (void *cls,
812                    const struct GNUNET_MessageHeader *hdr)
813 {
814   struct FragmentMessage *fm = cls;
815   struct MacEndpoint *endpoint = fm->macendpoint;
816   size_t size;
817   uint16_t msize;
818
819   msize = ntohs (hdr->size);
820   size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + msize;
821   {
822     char buf[size];
823     struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radio_header;
824
825     radio_header = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) buf;
826     get_radiotap_header (endpoint, radio_header, size);
827     get_wlan_header (endpoint->plugin,
828                      &radio_header->frame,
829                      &endpoint->addr.mac,
830                      sizeof (endpoint->addr.mac));
831     memcpy (&radio_header[1], hdr, msize);
832     GNUNET_assert (NULL == fm->sh);
833     fm->sh = GNUNET_HELPER_send (endpoint->plugin->suid_helper,
834                                  &radio_header->header,
835                                  GNUNET_NO,
836                                  &fragment_transmission_done, fm);
837     fm->size_on_wire += size;
838     if (NULL != fm->sh)
839       GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# Bluetooth message fragments sent"),
840                                 1, GNUNET_NO);
841     else
842       GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
843     GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
844                               "# bytes currently in Bluetooth buffers",
845                               -msize, GNUNET_NO);
846     GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
847                               "# bytes transmitted via Bluetooth",
848                               msize, GNUNET_NO);
849   }
850 }
851
852
853 /**
854  * Frees the space of a message in the fragment queue (send queue)
855  *
856  * @param fm message to free
857  */
858 static void
859 free_fragment_message (struct FragmentMessage *fm)
860 {
861   struct MacEndpoint *endpoint = fm->macendpoint;
862
863   GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# Bluetooth messages pending (with fragmentation)"),
864                             -1, GNUNET_NO);
865   GNUNET_CONTAINER_DLL_remove (endpoint->sending_messages_head,
866                                endpoint->sending_messages_tail, fm);
867   if (NULL != fm->sh)
868   {
869     GNUNET_HELPER_send_cancel (fm->sh);
870     fm->sh = NULL;
871   }
872   GNUNET_FRAGMENT_context_destroy (fm->fragcontext,
873                                                                    &endpoint->msg_delay,
874                                                                    &endpoint->ack_delay);
875   if (fm->timeout_task != GNUNET_SCHEDULER_NO_TASK)
876   {
877     GNUNET_SCHEDULER_cancel (fm->timeout_task);
878     fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
879   }
880   GNUNET_free (fm);
881 }
882
883
884 /**
885  * A FragmentMessage has timed out.  Remove it.
886  *
887  * @param cls pointer to the 'struct FragmentMessage'
888  * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
889  */
890 static void
891 fragmentmessage_timeout (void *cls,
892                          const struct GNUNET_SCHEDULER_TaskContext *tc)
893 {
894   struct FragmentMessage *fm = cls;
895
896   fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
897   if (NULL != fm->cont)
898   {
899     fm->cont (fm->cont_cls, &fm->target, GNUNET_SYSERR, fm->size_payload, fm->size_on_wire);
900     fm->cont = NULL;
901   }
902   free_fragment_message (fm);
903 }
904
905
906 /**
907  * Transmit a message to the given destination with fragmentation.
908  *
909  * @param endpoint desired destination
910  * @param timeout how long can the message wait?
911  * @param target peer that should receive the message
912  * @param msg message to transmit
913  * @param payload_size bytes of payload
914  * @param cont continuation to call once the message has
915  *        been transmitted (or if the transport is ready
916  *        for the next transmission call; or if the
917  *        peer disconnected...); can be NULL
918  * @param cont_cls closure for cont
919  */
920 static void
921 send_with_fragmentation (struct MacEndpoint *endpoint,
922                          struct GNUNET_TIME_Relative timeout,
923                          const struct GNUNET_PeerIdentity *target,
924                          const struct GNUNET_MessageHeader *msg,
925                          size_t payload_size,
926                          GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
927
928 {
929   struct FragmentMessage *fm;
930   struct Plugin *plugin;
931
932   plugin = endpoint->plugin;
933   fm = GNUNET_new (struct FragmentMessage);
934   fm->macendpoint = endpoint;
935   fm->target = *target;
936   fm->size_payload = payload_size;
937   fm->size_on_wire = 0;
938   fm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
939   fm->cont = cont;
940   fm->cont_cls = cont_cls;
941   /* 1 MBit/s typical data rate, 1430 byte fragments => ~100 ms per message */
942   fm->fragcontext =
943     GNUNET_FRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
944                                     &plugin->tracker,
945                                     endpoint->msg_delay,
946                                     endpoint->ack_delay,
947                                     msg,
948                                     &transmit_fragment, fm);
949   fm->timeout_task =
950     GNUNET_SCHEDULER_add_delayed (timeout,
951                                   &fragmentmessage_timeout, fm);
952   GNUNET_CONTAINER_DLL_insert_tail (endpoint->sending_messages_head,
953                                     endpoint->sending_messages_tail,
954                                     fm);
955 }
956
957
958 /**
959  * Free a MAC endpoint.
960  *
961  * @param endpoint pointer to the MacEndpoint to free
962  */
963 static void
964 free_macendpoint (struct MacEndpoint *endpoint)
965 {
966   struct Plugin *plugin = endpoint->plugin;
967   struct FragmentMessage *fm;
968   struct Session *session;
969
970   GNUNET_STATISTICS_update (plugin->env->stats,
971                             _("# Bluetooth MAC endpoints allocated"), -1, GNUNET_NO);
972   while (NULL != (session = endpoint->sessions_head))
973     free_session (session);
974   while (NULL != (fm = endpoint->sending_messages_head))
975     free_fragment_message (fm);
976   GNUNET_CONTAINER_DLL_remove (plugin->mac_head,
977                                plugin->mac_tail,
978                                endpoint);
979
980   if (NULL != endpoint->defrag)
981   {
982     GNUNET_DEFRAGMENT_context_destroy(endpoint->defrag);
983     endpoint->defrag = NULL;
984   }
985
986   plugin->mac_count--;
987   if (GNUNET_SCHEDULER_NO_TASK != endpoint->timeout_task)
988   {
989     GNUNET_SCHEDULER_cancel (endpoint->timeout_task);
990     endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
991   }
992   GNUNET_free (endpoint);
993 }
994
995
996 /**
997  * A MAC endpoint is timing out.  Clean up.
998  *
999  * @param cls pointer to the MacEndpoint
1000  * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
1001  */
1002 static void
1003 macendpoint_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1004 {
1005   struct MacEndpoint *endpoint = cls;
1006   struct GNUNET_TIME_Relative timeout;
1007
1008   endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1009   timeout = GNUNET_TIME_absolute_get_remaining (endpoint->timeout);
1010   if (0 == timeout.rel_value_us)
1011   {
1012     free_macendpoint (endpoint);
1013     return;
1014   }
1015   endpoint->timeout_task =
1016     GNUNET_SCHEDULER_add_delayed (timeout, &macendpoint_timeout,
1017                                   endpoint);
1018 }
1019
1020
1021 /**
1022  * Find (or create) a MacEndpoint with a specific MAC address
1023  *
1024  * @param plugin pointer to the plugin struct
1025  * @param addr the MAC address of the endpoint
1026  * @return handle to our data structure for this MAC
1027  */
1028 static struct MacEndpoint *
1029 create_macendpoint (struct Plugin *plugin,
1030                     struct WlanAddress *addr)
1031 {
1032   struct MacEndpoint *pos;
1033
1034   for (pos = plugin->mac_head; NULL != pos; pos = pos->next)
1035     if (0 == memcmp (addr, &pos->addr, sizeof (struct WlanAddress)))
1036       return pos;
1037   pos = GNUNET_new (struct MacEndpoint);
1038   pos->addr = (*addr);
1039   pos->plugin = plugin;
1040   pos->defrag =
1041     GNUNET_DEFRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
1042                                       MESSAGES_IN_DEFRAG_QUEUE_PER_MAC,
1043                                       pos,
1044                                       &bluetooth_data_message_handler,
1045                                       &send_ack);
1046
1047   pos->msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1048   pos->ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1049                                                                                                   100);
1050   pos->timeout = GNUNET_TIME_relative_to_absolute (MACENDPOINT_TIMEOUT);
1051   pos->timeout_task =
1052       GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
1053                                     pos);
1054   GNUNET_CONTAINER_DLL_insert (plugin->mac_head, plugin->mac_tail, pos);
1055   plugin->mac_count++;
1056   GNUNET_STATISTICS_update (plugin->env->stats, _("# Bluetooth MAC endpoints allocated"),
1057                             1, GNUNET_NO);
1058   LOG (GNUNET_ERROR_TYPE_DEBUG,
1059        "New MAC endpoint `%s'\n",
1060        bluetooth_plugin_address_to_string(NULL,
1061                                           addr,
1062                                           sizeof (struct WlanAddress)));
1063   return pos;
1064 }
1065
1066
1067 /**
1068  * Function obtain the network type for a session
1069  *
1070  * @param cls closure ('struct Plugin*')
1071  * @param session the session
1072  * @return the network type in HBO or GNUNET_SYSERR
1073  */
1074 static enum GNUNET_ATS_Network_Type
1075 bluetooth_get_network (void *cls,
1076                        struct Session *session)
1077 {
1078   GNUNET_assert (NULL != session);
1079   return GNUNET_ATS_NET_BT;
1080 }
1081
1082
1083 /**
1084  * Look up a session for a peer and create a new session if none is found
1085  *
1086  * @param endpoint pointer to the mac endpoint of the peer
1087  * @param peer peer identity to use for this session
1088  * @return returns the session
1089  */
1090 static struct Session *
1091 get_session (struct MacEndpoint *endpoint,
1092                 const struct GNUNET_PeerIdentity *peer)
1093 {
1094   struct Session *session;
1095   if (NULL != (session = lookup_session (endpoint, peer)))
1096         return session;
1097   return create_session (endpoint, peer);
1098 }
1099
1100
1101 /**
1102  * Creates a new outbound session the transport service will use to send data to the
1103  * peer
1104  *
1105  * @param cls the plugin
1106  * @param address the address
1107  * @return the session or NULL of max connections exceeded
1108  */
1109 static struct Session *
1110 bluetooth_plugin_get_session (void *cls,
1111                          const struct GNUNET_HELLO_Address *address)
1112 {
1113   struct Plugin *plugin = cls;
1114   struct MacEndpoint *endpoint;
1115
1116   if (NULL == address)
1117     return NULL;
1118   if (sizeof (struct WlanAddress) != address->address_length)
1119   {
1120     GNUNET_break (0);
1121     return NULL;
1122   }
1123   LOG (GNUNET_ERROR_TYPE_DEBUG,
1124        "Service asked to create session for peer `%s' with MAC `%s'\n",
1125        GNUNET_i2s (&address->peer),
1126        bluetooth_plugin_address_to_string(NULL,
1127                                           address->address,
1128                                           address->address_length));
1129   endpoint = create_macendpoint (plugin,
1130                                  (struct WlanAddress *) address->address);
1131   return get_session (endpoint, &address->peer);
1132 }
1133
1134
1135 /**
1136  * Function that can be used to force the plugin to disconnect
1137  * from the given peer and cancel all previous transmissions
1138  * (and their continuation).
1139  *
1140  * @param cls closure
1141  * @param target peer from which to disconnect
1142  */
1143 static void
1144 bluetooth_plugin_disconnect_peer (void *cls, const struct GNUNET_PeerIdentity *target)
1145 {
1146   struct Plugin *plugin = cls;
1147   struct Session *session;
1148   struct MacEndpoint *endpoint;
1149
1150   for (endpoint = plugin->mac_head; NULL != endpoint; endpoint = endpoint->next)
1151     for (session = endpoint->sessions_head; NULL != session; session = session->next)
1152       if (0 == memcmp (target, &session->target,
1153         sizeof (struct GNUNET_PeerIdentity)))
1154       {
1155         free_session (session);
1156         break; /* inner-loop only (in case peer has another MAC as well!) */
1157       }
1158 }
1159
1160
1161 /**
1162  * Function that can be used to force the plugin to disconnect
1163  * from the given peer and cancel all previous transmissions
1164  * (and their continuation).
1165  *
1166  * @param cls closure
1167  * @param session session to disconnect
1168  */
1169 static int
1170 bluetooth_plugin_disconnect_session (void *cls,
1171                                      struct Session *session)
1172 {
1173   free_session (session);
1174   return GNUNET_OK;
1175 }
1176
1177
1178 /**
1179  * Function that is called to get the keepalive factor.
1180  * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
1181  * calculate the interval between keepalive packets.
1182  *
1183  * @param cls closure with the `struct Plugin`
1184  * @return keepalive factor
1185  */
1186 static unsigned int
1187 bluetooth_query_keepalive_factor (void *cls)
1188 {
1189   return 3;
1190 }
1191
1192
1193 /**
1194  * Function that can be used by the transport service to transmit
1195  * a message using the plugin.   Note that in the case of a
1196  * peer disconnecting, the continuation MUST be called
1197  * prior to the disconnect notification itself.  This function
1198  * will be called with this peer's HELLO message to initiate
1199  * a fresh connection to another peer.
1200  *
1201  * @param cls closure
1202  * @param session which session must be used
1203  * @param msgbuf the message to transmit
1204  * @param msgbuf_size number of bytes in 'msgbuf'
1205  * @param priority how important is the message (most plugins will
1206  *                 ignore message priority and just FIFO)
1207  * @param to how long to wait at most for the transmission (does not
1208  *                require plugins to discard the message after the timeout,
1209  *                just advisory for the desired delay; most plugins will ignore
1210  *                this as well)
1211  * @param cont continuation to call once the message has
1212  *        been transmitted (or if the transport is ready
1213  *        for the next transmission call; or if the
1214  *        peer disconnected...); can be NULL
1215  * @param cont_cls closure for cont
1216  * @return number of bytes used (on the physical network, with overheads);
1217  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
1218  *         and does NOT mean that the message was not transmitted (DV)
1219  */
1220 static ssize_t
1221 bluetooth_plugin_send (void *cls,
1222                   struct Session *session,
1223                   const char *msgbuf, size_t msgbuf_size,
1224                   unsigned int priority,
1225                   struct GNUNET_TIME_Relative to,
1226                   GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1227 {
1228   struct Plugin *plugin = cls;
1229   struct WlanHeader *wlanheader;
1230   size_t size = msgbuf_size + sizeof (struct WlanHeader);
1231   char buf[size] GNUNET_ALIGN;
1232
1233   LOG (GNUNET_ERROR_TYPE_DEBUG,
1234        "Transmitting %u bytes of payload to peer `%s' (starting with %u byte message of type %u)\n",
1235        msgbuf_size,
1236        GNUNET_i2s (&session->target),
1237        (unsigned int) ntohs (((struct GNUNET_MessageHeader*)msgbuf)->size),
1238        (unsigned int) ntohs (((struct GNUNET_MessageHeader*)msgbuf)->type));
1239   wlanheader = (struct WlanHeader *) buf;
1240   wlanheader->header.size = htons (msgbuf_size + sizeof (struct WlanHeader));
1241   wlanheader->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA);
1242   wlanheader->sender = *plugin->env->my_identity;
1243   wlanheader->target = session->target;
1244   wlanheader->crc = htonl (GNUNET_CRYPTO_crc32_n (msgbuf, msgbuf_size));
1245   memcpy (&wlanheader[1], msgbuf, msgbuf_size);
1246
1247   GNUNET_STATISTICS_update (plugin->env->stats,
1248                             "# bytes currently in Bluetooth buffers",
1249                             msgbuf_size, GNUNET_NO);
1250
1251   send_with_fragmentation (session->mac,
1252                            to,
1253                            &session->target,
1254                            &wlanheader->header,
1255                            msgbuf_size,
1256                            cont, cont_cls);
1257   return size;
1258 }
1259
1260
1261 /**
1262  * We have received data from the WLAN via some session.  Process depending
1263  * on the message type (HELLO, DATA, FRAGMENTATION or FRAGMENTATION-ACK).
1264  *
1265  * @param cls pointer to the plugin
1266  * @param client pointer to the session this message belongs to
1267  * @param hdr start of the message
1268  */
1269 static int
1270 process_data (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
1271 {
1272   struct Plugin *plugin = cls;
1273   struct GNUNET_HELLO_Address *address;
1274   struct MacAndSession *mas = client;
1275   struct MacAndSession xmas;
1276   struct GNUNET_ATS_Information ats;
1277   struct FragmentMessage *fm;
1278   struct GNUNET_PeerIdentity tmpsource;
1279   const struct WlanHeader *wlanheader;
1280   int ret;
1281   uint16_t msize;
1282
1283   ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1284   ats.value = htonl (GNUNET_ATS_NET_BT);
1285   msize = ntohs (hdr->size);
1286
1287   GNUNET_STATISTICS_update (plugin->env->stats,
1288                             "# bytes received via Bluetooth",
1289                             msize, GNUNET_NO);
1290
1291   switch (ntohs (hdr->type))
1292   {
1293   case GNUNET_MESSAGE_TYPE_HELLO:
1294     if (GNUNET_OK !=
1295         GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hdr, &tmpsource))
1296     {
1297       GNUNET_break_op (0);
1298       break;
1299     }
1300     if (NULL == mas->endpoint)
1301     {
1302       GNUNET_break (0);
1303       break;
1304     }
1305
1306     LOG (GNUNET_ERROR_TYPE_DEBUG,
1307          "Processing %u bytes of HELLO from peer `%s' at MAC %s\n",
1308          (unsigned int) msize,
1309          GNUNET_i2s (&tmpsource),
1310          bluetooth_plugin_address_to_string (NULL,
1311                                              &mas->endpoint->addr,
1312                                              sizeof (mas->endpoint->addr)));
1313
1314     GNUNET_STATISTICS_update (plugin->env->stats,
1315                               _("# HELLO messages received via Bluetooth"), 1,
1316                               GNUNET_NO);
1317
1318     address = GNUNET_HELLO_address_allocate (&tmpsource, PLUGIN_NAME,
1319         &mas->endpoint->addr, sizeof (mas->endpoint->addr),
1320         GNUNET_HELLO_ADDRESS_INFO_INBOUND);
1321     plugin->env->receive (plugin->env->cls,
1322         address, mas->session, hdr);
1323     plugin->env->update_address_metrics (plugin->env->cls,
1324         address, mas->session, &ats, 1);
1325     GNUNET_HELLO_address_free (address);
1326     break;
1327   case GNUNET_MESSAGE_TYPE_FRAGMENT:
1328     if (NULL == mas->endpoint)
1329     {
1330       GNUNET_break (0);
1331       break;
1332     }
1333     LOG (GNUNET_ERROR_TYPE_DEBUG,
1334          "Processing %u bytes of FRAGMENT from MAC %s\n",
1335          (unsigned int) msize,
1336          bluetooth_plugin_address_to_string (NULL,
1337                                              &mas->endpoint->addr,
1338                                              sizeof (mas->endpoint->addr)));
1339     GNUNET_STATISTICS_update (plugin->env->stats,
1340                               _("# fragments received via Bluetooth"),
1341                               1,
1342                               GNUNET_NO);
1343     (void) GNUNET_DEFRAGMENT_process_fragment (mas->endpoint->defrag,
1344                                               hdr);
1345     break;
1346   case GNUNET_MESSAGE_TYPE_FRAGMENT_ACK:
1347     if (NULL == mas->endpoint)
1348     {
1349       GNUNET_break (0);
1350       break;
1351     }
1352     GNUNET_STATISTICS_update (plugin->env->stats, _("# ACKs received via Bluetooth"),
1353                               1, GNUNET_NO);
1354     for (fm = mas->endpoint->sending_messages_head; NULL != fm; fm = fm->next)
1355     {
1356       ret = GNUNET_FRAGMENT_process_ack (fm->fragcontext, hdr);
1357       if (GNUNET_OK == ret)
1358       {
1359         LOG (GNUNET_ERROR_TYPE_DEBUG,
1360              "Got last ACK, finished message transmission to `%s' (%p)\n",
1361              bluetooth_plugin_address_to_string (NULL,
1362                                                  &mas->endpoint->addr,
1363                                                  sizeof (mas->endpoint->addr)),
1364              fm);
1365         mas->endpoint->timeout = GNUNET_TIME_relative_to_absolute (MACENDPOINT_TIMEOUT);
1366         if (NULL != fm->cont)
1367         {
1368           fm->cont (fm->cont_cls, &fm->target, GNUNET_OK, fm->size_payload, fm->size_on_wire);
1369           fm->cont = NULL;
1370         }
1371         free_fragment_message (fm);
1372         break;
1373       }
1374       if (GNUNET_NO == ret)
1375       {
1376         LOG (GNUNET_ERROR_TYPE_DEBUG,
1377              "Got an ACK, message transmission to `%s' not yet finished\n",
1378              bluetooth_plugin_address_to_string (NULL,
1379                                                  &mas->endpoint->addr,
1380                                                  sizeof (mas->endpoint->addr)));
1381         break;
1382       }
1383     }
1384     LOG (GNUNET_ERROR_TYPE_DEBUG,
1385          "ACK not matched against any active fragmentation with MAC `%s'\n",
1386          bluetooth_plugin_address_to_string (NULL,
1387                                              &mas->endpoint->addr,
1388                                              sizeof (mas->endpoint->addr)));
1389     break;
1390   case GNUNET_MESSAGE_TYPE_WLAN_DATA:
1391     if (NULL == mas->endpoint)
1392     {
1393       GNUNET_break (0);
1394       break;
1395     }
1396     if (msize < sizeof (struct WlanHeader))
1397     {
1398       GNUNET_break (0);
1399       break;
1400     }
1401     wlanheader = (const struct WlanHeader *) hdr;
1402     if (0 != memcmp (&wlanheader->target,
1403                      plugin->env->my_identity,
1404                      sizeof (struct GNUNET_PeerIdentity)))
1405     {
1406       LOG (GNUNET_ERROR_TYPE_DEBUG,
1407            "Bluetooth data for `%s', not for me, ignoring\n",
1408            GNUNET_i2s (&wlanheader->target));
1409       break;
1410     }
1411     if (ntohl (wlanheader->crc) !=
1412         GNUNET_CRYPTO_crc32_n (&wlanheader[1], msize - sizeof (struct WlanHeader)))
1413     {
1414       GNUNET_STATISTICS_update (plugin->env->stats,
1415                                 _("# Bluetooth DATA messages discarded due to CRC32 error"), 1,
1416                                 GNUNET_NO);
1417       break;
1418     }
1419     xmas.endpoint = mas->endpoint;
1420     xmas.session = create_session (mas->endpoint, &wlanheader->sender);
1421     LOG (GNUNET_ERROR_TYPE_DEBUG,
1422          "Processing %u bytes of BLUETOOTH DATA from peer `%s'\n",
1423          (unsigned int) msize,
1424          GNUNET_i2s (&wlanheader->sender));
1425     (void) GNUNET_SERVER_mst_receive (plugin->wlan_header_payload_tokenizer,
1426                                       &xmas,
1427                                       (const char *) &wlanheader[1],
1428                                       msize - sizeof (struct WlanHeader),
1429                                       GNUNET_YES, GNUNET_NO);
1430     break;
1431   default:
1432     if (NULL == mas->endpoint)
1433     {
1434       GNUNET_break (0);
1435       break;
1436     }
1437     if (NULL == mas->session)
1438     {
1439       GNUNET_break (0);
1440       break;
1441     }
1442     LOG (GNUNET_ERROR_TYPE_DEBUG,
1443          "Received packet with %u bytes of type %u from peer %s\n",
1444          (unsigned int) msize,
1445          (unsigned int) ntohs (hdr->type),
1446          GNUNET_i2s (&mas->session->target));
1447     plugin->env->receive (plugin->env->cls,
1448         mas->session->address,
1449         mas->session,
1450         hdr);
1451     plugin->env->update_address_metrics (plugin->env->cls,
1452         mas->session->address,
1453         mas->session,
1454         &ats, 1);
1455     break;
1456   }
1457   return GNUNET_OK;
1458 }
1459
1460
1461 /**
1462  * Function used for to process the data from the suid process
1463  *
1464  * @param cls the plugin handle
1465  * @param client client that send the data (not used)
1466  * @param hdr header of the GNUNET_MessageHeader
1467  */
1468 static int
1469 handle_helper_message (void *cls, void *client,
1470                        const struct GNUNET_MessageHeader *hdr)
1471 {
1472   struct Plugin *plugin = cls;
1473   struct GNUNET_HELLO_Address *address;
1474   const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rxinfo;
1475   const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *cm;
1476   struct WlanAddress wa;
1477   struct MacAndSession mas;
1478   uint16_t msize;
1479
1480   msize = ntohs (hdr->size);
1481   switch (ntohs (hdr->type))
1482   {
1483   case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
1484     if (msize != sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage))
1485     {
1486       GNUNET_break (0);
1487       break;
1488     }
1489     cm = (const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *) hdr;
1490     if (GNUNET_YES == plugin->have_mac)
1491     {
1492       if (0 == memcmp (&plugin->mac_address,
1493                        &cm->mac,
1494                        sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1495         break; /* no change */
1496       /* remove old address */
1497       memset (&wa, 0, sizeof (struct WlanAddress));
1498       wa.mac = plugin->mac_address;
1499       wa.options = htonl(plugin->options);
1500       address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1501           PLUGIN_NAME, &wa, sizeof (wa), GNUNET_HELLO_ADDRESS_INFO_NONE);
1502       plugin->env->notify_address (plugin->env->cls, GNUNET_NO, address);
1503       GNUNET_HELLO_address_free (address);
1504     }
1505     plugin->mac_address = cm->mac;
1506     plugin->have_mac = GNUNET_YES;
1507     memset (&wa, 0, sizeof (struct WlanAddress));
1508     wa.mac = plugin->mac_address;
1509     wa.options = htonl(plugin->options);
1510     address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1511         PLUGIN_NAME, &wa, sizeof (wa), GNUNET_HELLO_ADDRESS_INFO_NONE);
1512
1513     LOG (GNUNET_ERROR_TYPE_DEBUG,
1514          "Received BT_HELPER_CONTROL message with MAC address `%s' for peer `%s'\n",
1515          mac_to_string (&cm->mac),
1516          GNUNET_i2s (plugin->env->my_identity));
1517     plugin->env->notify_address (plugin->env->cls, GNUNET_YES, address);
1518     GNUNET_HELLO_address_free (address);
1519
1520     break;
1521   case GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER:
1522     LOG (GNUNET_ERROR_TYPE_DEBUG,
1523          "Got data message from helper with %u bytes\n",
1524          msize);
1525     GNUNET_STATISTICS_update (plugin->env->stats,
1526                               _("# DATA messages received via Bluetooth"), 1,
1527                               GNUNET_NO);
1528     if (msize < sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage))
1529     {
1530       GNUNET_break (0);
1531       LOG (GNUNET_ERROR_TYPE_DEBUG,
1532            "Size of packet is too small (%u bytes)\n",
1533            msize);
1534       break;
1535     }
1536     rxinfo = (const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) hdr;
1537
1538     /* check if message is actually for us */
1539     if (0 != memcmp (&rxinfo->frame.addr3, &mac_bssid_gnunet,
1540                      sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1541     {
1542       /* Not the GNUnet BSSID */
1543       break;
1544     }
1545     if ( (0 != memcmp (&rxinfo->frame.addr1, &bc_all_mac,
1546                        sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))) &&
1547          (0 != memcmp (&rxinfo->frame.addr1, &plugin->mac_address,
1548                        sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))) )
1549     {
1550       /* Neither broadcast nor specifically for us */
1551       break;
1552     }
1553     if (0 == memcmp (&rxinfo->frame.addr2, &plugin->mac_address,
1554                      sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1555     {
1556       /* packet is FROM us, thus not FOR us */
1557       break;
1558     }
1559
1560     GNUNET_STATISTICS_update (plugin->env->stats,
1561                               _("# Bluetooth DATA messages processed"),
1562                               1, GNUNET_NO);
1563     LOG (GNUNET_ERROR_TYPE_DEBUG,
1564          "Receiving %u bytes of data from MAC `%s'\n",
1565          (unsigned int) (msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)),
1566          mac_to_string (&rxinfo->frame.addr2));
1567     wa.mac = rxinfo->frame.addr2;
1568     wa.options = htonl (0);
1569     mas.endpoint = create_macendpoint (plugin, &wa);
1570     mas.session = NULL;
1571     (void) GNUNET_SERVER_mst_receive (plugin->helper_payload_tokenizer,
1572                                       &mas,
1573                                       (const char*) &rxinfo[1],
1574                                       msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage),
1575                                       GNUNET_YES, GNUNET_NO);
1576     break;
1577   default:
1578     GNUNET_break (0);
1579     LOG (GNUNET_ERROR_TYPE_DEBUG,
1580          "Unexpected message of type %u (%u bytes)",
1581          ntohs (hdr->type), ntohs (hdr->size));
1582     break;
1583   }
1584   return GNUNET_OK;
1585 }
1586
1587
1588
1589 /**
1590  * Task to (periodically) send a HELLO beacon
1591  *
1592  * @param cls pointer to the plugin struct
1593  * @param tc scheduler context
1594  */
1595 static void
1596 send_hello_beacon (void *cls,
1597                    const struct GNUNET_SCHEDULER_TaskContext *tc)
1598 {
1599   struct Plugin *plugin = cls;
1600   uint16_t size;
1601   uint16_t hello_size;
1602   struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
1603   const struct GNUNET_MessageHeader *hello;
1604
1605   hello = plugin->env->get_our_hello ();
1606   hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1607   GNUNET_assert (sizeof (struct WlanHeader) + hello_size <= WLAN_MTU);
1608   size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + hello_size;
1609   {
1610     char buf[size] GNUNET_ALIGN;
1611
1612     LOG (GNUNET_ERROR_TYPE_DEBUG,
1613          "Sending %u byte HELLO beacon\n",
1614          (unsigned int) size);
1615     radioHeader = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage*) buf;
1616     get_radiotap_header (NULL, radioHeader, size);
1617     get_wlan_header (plugin, &radioHeader->frame, &bc_all_mac, size);
1618     memcpy (&radioHeader[1], hello, hello_size);
1619     if (NULL !=
1620         GNUNET_HELPER_send (plugin->suid_helper,
1621                             &radioHeader->header,
1622                             GNUNET_YES /* can drop */,
1623                             NULL, NULL))
1624       GNUNET_STATISTICS_update (plugin->env->stats, _("# HELLO beacons sent via Bluetooth"),
1625                                 1, GNUNET_NO);
1626   }
1627   plugin->beacon_task =
1628     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1629                                   (HELLO_BEACON_SCALING_FACTOR,
1630                                    plugin->mac_count + 1),
1631                                   &send_hello_beacon,
1632                                   plugin);
1633
1634 }
1635
1636
1637 /**
1638  * Another peer has suggested an address for this
1639  * peer and transport plugin.  Check that this could be a valid
1640  * address.  If so, consider adding it to the list
1641  * of addresses.
1642  *
1643  * @param cls closure
1644  * @param addr pointer to the address
1645  * @param addrlen length of addr
1646  * @return GNUNET_OK if this is a plausible address for this peer
1647  *         and transport
1648  */
1649 static int
1650 bluetooth_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1651 {
1652   struct Plugin *plugin = cls;
1653   struct WlanAddress *wa = (struct WlanAddress *) addr;
1654
1655   if (addrlen != sizeof (struct WlanAddress))
1656   {
1657     GNUNET_break_op (0);
1658     return GNUNET_SYSERR;
1659   }
1660   if (GNUNET_YES != plugin->have_mac)
1661   {
1662     LOG (GNUNET_ERROR_TYPE_DEBUG,
1663          "Rejecting MAC `%s': I don't know my MAC!\n",
1664          mac_to_string (addr));
1665     return GNUNET_NO; /* don't know my MAC */
1666   }
1667   if (0 != memcmp (&wa->mac,
1668                    &plugin->mac_address,
1669                    sizeof (wa->mac)))
1670   {
1671     LOG (GNUNET_ERROR_TYPE_DEBUG,
1672          "Rejecting MAC `%s': not my MAC!\n",
1673          mac_to_string (addr));
1674     return GNUNET_NO; /* not my MAC */
1675   }
1676   return GNUNET_OK;
1677 }
1678
1679
1680 /**
1681  * Function called for a quick conversion of the binary address to
1682  * a numeric address.  Note that the caller must not free the
1683  * address and that the next call to this function is allowed
1684  * to override the address again.
1685  *
1686  * @param cls closure
1687  * @param addr binary address
1688  * @param addrlen length of the address
1689  * @return string representing the same address
1690  */
1691 static const char *
1692 bluetooth_plugin_address_to_string (void *cls,
1693                                     const void *addr,
1694                                     size_t addrlen)
1695 {
1696   const struct GNUNET_TRANSPORT_WLAN_MacAddress *mac;
1697   static char macstr[36];
1698
1699   if (sizeof (struct WlanAddress) != addrlen)
1700   {
1701     GNUNET_break (0);
1702     return NULL;
1703   }
1704   mac = &((struct WlanAddress *) addr)->mac;
1705   GNUNET_snprintf (macstr,
1706                    sizeof (macstr),
1707                    "%s.%u.%s",
1708                    PLUGIN_NAME,
1709                    ntohl (((struct WlanAddress *) addr)->options),
1710                    mac_to_string (mac));
1711   return macstr;
1712 }
1713
1714
1715 /**
1716  * Convert the transports address to a nice, human-readable format.
1717  *
1718  * @param cls closure
1719  * @param type name of the transport that generated the address
1720  * @param addr one of the addresses of the host, NULL for the last address
1721  *        the specific address format depends on the transport
1722  * @param addrlen length of the address
1723  * @param numeric should (IP) addresses be displayed in numeric form?
1724  * @param timeout after how long should we give up?
1725  * @param asc function to call on each string
1726  * @param asc_cls closure for @a asc
1727  */
1728 static void
1729 bluetooth_plugin_address_pretty_printer (void *cls, const char *type,
1730                                     const void *addr, size_t addrlen,
1731                                     int numeric,
1732                                     struct GNUNET_TIME_Relative timeout,
1733                                     GNUNET_TRANSPORT_AddressStringCallback asc,
1734                                     void *asc_cls)
1735 {
1736   const char *ret;
1737
1738   if (sizeof (struct WlanAddress) == addrlen)
1739     ret = bluetooth_plugin_address_to_string(NULL, addr, addrlen);
1740   else
1741     ret = NULL;
1742   asc (asc_cls,
1743        ret,
1744        (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
1745   asc (asc_cls, NULL, GNUNET_OK);
1746 }
1747
1748
1749 /**
1750  * Exit point from the plugin.
1751  *
1752  * @param cls pointer to the api struct
1753  */
1754 void *
1755 libgnunet_plugin_transport_bluetooth_done (void *cls)
1756 {
1757   struct WlanAddress wa;
1758   struct GNUNET_HELLO_Address *address;
1759   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1760   struct Plugin *plugin = api->cls;
1761   struct MacEndpoint *endpoint;
1762   struct MacEndpoint *endpoint_next;
1763
1764   if (NULL == plugin)
1765   {
1766     GNUNET_free (api);
1767     return NULL;
1768   }
1769
1770   if (GNUNET_YES == plugin->have_mac)
1771   {
1772     memset (&wa, 0, sizeof(wa));
1773     wa.options = htonl (plugin->options);
1774     wa.mac = plugin->mac_address;
1775     address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1776         PLUGIN_NAME, &wa, sizeof (struct WlanAddress),
1777         GNUNET_HELLO_ADDRESS_INFO_NONE);
1778
1779     plugin->env->notify_address (plugin->env->cls, GNUNET_NO, address);
1780     plugin->have_mac = GNUNET_NO;
1781
1782     GNUNET_HELLO_address_free (address);
1783   }
1784
1785   if (GNUNET_SCHEDULER_NO_TASK != plugin->beacon_task)
1786   {
1787     GNUNET_SCHEDULER_cancel (plugin->beacon_task);
1788     plugin->beacon_task = GNUNET_SCHEDULER_NO_TASK;
1789   }
1790   if (NULL != plugin->suid_helper)
1791   {
1792     GNUNET_HELPER_stop (plugin->suid_helper, GNUNET_NO);
1793     plugin->suid_helper = NULL;
1794   }
1795   endpoint_next = plugin->mac_head;
1796   while (NULL != (endpoint = endpoint_next))
1797   {
1798     endpoint_next = endpoint->next;
1799     free_macendpoint (endpoint);
1800   }
1801   if (NULL != plugin->fragment_data_tokenizer)
1802   {
1803     GNUNET_SERVER_mst_destroy (plugin->fragment_data_tokenizer);
1804     plugin->fragment_data_tokenizer = NULL;
1805   }
1806   if (NULL != plugin->wlan_header_payload_tokenizer)
1807   {
1808     GNUNET_SERVER_mst_destroy (plugin->wlan_header_payload_tokenizer);
1809     plugin->wlan_header_payload_tokenizer = NULL;
1810   }
1811   if (NULL != plugin->helper_payload_tokenizer)
1812   {
1813     GNUNET_SERVER_mst_destroy (plugin->helper_payload_tokenizer);
1814     plugin->helper_payload_tokenizer = NULL;
1815   }
1816   GNUNET_free_non_null (plugin->interface);
1817   GNUNET_free (plugin);
1818   GNUNET_free (api);
1819   return NULL;
1820 }
1821
1822
1823 /**
1824  * Function called to convert a string address to
1825  * a binary address.
1826  *
1827  * @param cls closure ('struct Plugin*')
1828  * @param addr string address
1829  * @param addrlen length of the address
1830  * @param buf location to store the buffer
1831  * @param added location to store the number of bytes in the buffer.
1832  *        If the function returns GNUNET_SYSERR, its contents are undefined.
1833  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1834  */
1835 static int
1836 bluetooth_string_to_address (void *cls, const char *addr, uint16_t addrlen,
1837                         void **buf, size_t *added)
1838 {
1839   struct WlanAddress *wa;
1840   unsigned int a[6];
1841   unsigned int i;
1842   char plugin[10];
1843   uint32_t options;
1844
1845   if ((NULL == addr) || (addrlen == 0))
1846   {
1847     GNUNET_break (0);
1848     return GNUNET_SYSERR;
1849   }
1850   if ('\0' != addr[addrlen - 1])
1851   {
1852     GNUNET_break (0);
1853     return GNUNET_SYSERR;
1854   }
1855   if (strlen (addr) != addrlen - 1)
1856   {
1857     GNUNET_break (0);
1858     return GNUNET_SYSERR;
1859   }
1860
1861   if (8 != SSCANF (addr,
1862                    "%9s.%u.%X:%X:%X:%X:%X:%X",
1863                    plugin, &options,
1864                    &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]))
1865   {
1866     GNUNET_break (0);
1867     return GNUNET_SYSERR;
1868   }
1869   wa = GNUNET_new (struct WlanAddress);
1870   for (i=0;i<6;i++)
1871     wa->mac.mac[i] = a[i];
1872   wa->options = htonl (0);
1873   *buf = wa;
1874   *added = sizeof (struct WlanAddress);
1875   return GNUNET_OK;
1876 }
1877
1878 static void
1879 bluetooth_plugin_update_session_timeout (void *cls,
1880                                   const struct GNUNET_PeerIdentity *peer,
1881                                   struct Session *session)
1882 {
1883   if (GNUNET_SCHEDULER_NO_TASK != session->timeout_task)
1884     GNUNET_SCHEDULER_cancel (session->timeout_task);
1885   session->timeout_task = GNUNET_SCHEDULER_add_delayed (
1886       GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &session_timeout, session);
1887 }
1888
1889
1890
1891 /**
1892  * Entry point for the plugin.
1893  *
1894  * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
1895  * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
1896  */
1897 void *
1898 libgnunet_plugin_transport_bluetooth_init (void *cls)
1899 {
1900   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1901   struct GNUNET_TRANSPORT_PluginFunctions *api;
1902   struct Plugin *plugin;
1903   char *interface;
1904   unsigned long long testmode;
1905   char *binary;
1906
1907   /* check for 'special' mode */
1908   if (NULL == env->receive)
1909   {
1910     /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1911        initialze the plugin or the API */
1912     api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
1913     api->cls = NULL;
1914     api->address_pretty_printer = &bluetooth_plugin_address_pretty_printer;
1915     api->address_to_string = &bluetooth_plugin_address_to_string;
1916     api->string_to_address = &bluetooth_string_to_address;
1917     return api;
1918   }
1919
1920   testmode = 0;
1921   /* check configuration */
1922   if ( (GNUNET_YES ==
1923         GNUNET_CONFIGURATION_have_value (env->cfg, "transport-bluetooth", "TESTMODE")) &&
1924        ( (GNUNET_SYSERR ==
1925           GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-bluetooth",
1926                                                  "TESTMODE", &testmode)) ||
1927          (testmode > 2) ) )
1928   {
1929     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1930                                "transport-bluetooth", "TESTMODE");
1931     return NULL;
1932   }
1933   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-transport-bluetooth");
1934   if ( (0 == testmode) &&
1935        (GNUNET_YES != GNUNET_OS_check_helper_binary (binary, GNUNET_YES, NULL)) )
1936   {
1937     LOG (GNUNET_ERROR_TYPE_ERROR,
1938          _("Helper binary `%s' not SUID, cannot run bluetooth transport\n"),
1939          "gnunet-helper-transport-bluetooth");
1940     GNUNET_free (binary);
1941     return NULL;
1942   }
1943     GNUNET_free (binary);
1944   if (GNUNET_YES !=
1945       GNUNET_CONFIGURATION_get_value_string
1946       (env->cfg, "transport-bluetooth", "INTERFACE",
1947        &interface))
1948   {
1949     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1950                                "transport-bluetooth", "INTERFACE");
1951     return NULL;
1952   }
1953
1954   plugin = GNUNET_new (struct Plugin);
1955   plugin->interface = interface;
1956   plugin->env = env;
1957   GNUNET_STATISTICS_set (plugin->env->stats, _("# Bluetooth sessions allocated"),
1958                          0, GNUNET_NO);
1959   GNUNET_STATISTICS_set (plugin->env->stats, _("# Bluetooth MAC endpoints allocated"),
1960                          0, 0);
1961   GNUNET_BANDWIDTH_tracker_init (&plugin->tracker, NULL, NULL,
1962                                  GNUNET_BANDWIDTH_value_init (100 * 1024 *
1963                                                               1024 / 8), 100);
1964   plugin->fragment_data_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
1965   plugin->wlan_header_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
1966   plugin->helper_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
1967   plugin->beacon_task = GNUNET_SCHEDULER_add_now (&send_hello_beacon,
1968                                                   plugin);
1969
1970   plugin->options = 0;
1971
1972   /* some compilers do not like switch on 'long long'... */
1973   switch ((unsigned int) testmode)
1974   {
1975   case 0: /* normal */
1976     plugin->helper_argv[0] = (char *) "gnunet-helper-transport-bluetooth";
1977     plugin->helper_argv[1] = interface;
1978     plugin->helper_argv[2] = NULL;
1979     plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
1980                                                "gnunet-helper-transport-bluetooth",
1981                                                plugin->helper_argv,
1982                                                &handle_helper_message,
1983                                                NULL,
1984                                                plugin);
1985     break;
1986   case 1: /* testmode, peer 1 */
1987     plugin->helper_argv[0] = (char *) "gnunet-helper-transport-wlan-dummy";
1988     plugin->helper_argv[1] = (char *) "1";
1989     plugin->helper_argv[2] = NULL;
1990     plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
1991                  "gnunet-helper-transport-wlan-dummy",
1992                  plugin->helper_argv,
1993                  &handle_helper_message,
1994                  NULL,
1995                  plugin);
1996     break;
1997   case 2: /* testmode, peer 2 */
1998     plugin->helper_argv[0] = (char *) "gnunet-helper-transport-wlan-dummy";
1999     plugin->helper_argv[1] = (char *) "2";
2000     plugin->helper_argv[2] = NULL;
2001     plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
2002                  "gnunet-helper-transport-wlan-dummy",
2003                  plugin->helper_argv,
2004                  &handle_helper_message,
2005                  NULL,
2006                  plugin);
2007     break;
2008   default:
2009     GNUNET_assert (0);
2010   }
2011
2012   api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2013   api->cls = plugin;
2014   api->send = &bluetooth_plugin_send;
2015   api->get_session = &bluetooth_plugin_get_session;
2016   api->disconnect_peer = &bluetooth_plugin_disconnect_peer;
2017   api->disconnect_session = &bluetooth_plugin_disconnect_session;
2018   api->query_keepalive_factor = &bluetooth_query_keepalive_factor;
2019   api->address_pretty_printer = &bluetooth_plugin_address_pretty_printer;
2020   api->check_address = &bluetooth_plugin_address_suggested;
2021   api->address_to_string = &bluetooth_plugin_address_to_string;;
2022   api->string_to_address = &bluetooth_string_to_address;
2023   api->get_network = &bluetooth_get_network;
2024   api->update_session_timeout = &bluetooth_plugin_update_session_timeout;
2025
2026   return api;
2027 }
2028
2029
2030 /* end of plugin_transport_bluetooth.c */