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