2 This file is part of GNUnet
3 (C) 2010, 2011, 2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file transport/plugin_transport_bluetooth.c
23 * @brief transport plugin for bluetooth
24 * @author David Brodski
25 * @author Christian Grothoff
27 * THIS IS A COPY OF plugin_transport_wlan.c
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"
46 #define LOG(kind,...) GNUNET_log_from (kind, "transport-bluetooth",__VA_ARGS__)
48 #define PLUGIN_NAME "bluetooth"
51 * Max size of packet (that we give to the WLAN driver for transmission)
56 * time out of a mac endpoint
58 #define MACENDPOINT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 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.
65 #define HELLO_BEACON_SCALING_FACTOR GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
68 * Maximum number of messages in defragmentation queue per MAC
70 #define MESSAGES_IN_DEFRAG_QUEUE_PER_MAC 2
73 * Link layer control fields for better compatibility
74 * (i.e. GNUnet over WLAN is not IP-over-WLAN).
76 #define WLAN_LLC_DSAP_FIELD 0x1f
77 #define WLAN_LLC_SSAP_FIELD 0x1f
80 GNUNET_NETWORK_STRUCT_BEGIN
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
91 * Message type is GNUNET_MESSAGE_TYPE_WLAN_DATA.
93 struct GNUNET_MessageHeader header;
96 * CRC32 checksum (only over the payload), in NBO.
98 uint32_t crc GNUNET_PACKED;
101 * Sender of the message.
103 struct GNUNET_PeerIdentity sender;
106 * Target of the message.
108 struct GNUNET_PeerIdentity target;
110 /* followed by payload, possibly including
111 multiple messages! */
118 uint32_t options GNUNET_PACKED;
120 struct GNUNET_TRANSPORT_WLAN_MacAddress mac;
124 GNUNET_NETWORK_STRUCT_END
128 * Information kept for each message that is yet to be fragmented and
131 struct PendingMessage
134 * next entry in the DLL
136 struct PendingMessage *next;
139 * previous entry in the DLL
141 struct PendingMessage *prev;
144 * The pending message
146 struct WlanHeader *msg;
149 * Continuation function to call once the message
150 * has been sent. Can be NULL if there is no
151 * continuation to call.
153 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
156 * Cls for transmit_cont
158 void *transmit_cont_cls;
161 * Timeout task (for this message).
163 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
169 * Session handle for connections with other peers.
174 * To whom are we talking to (set to our identity
175 * if we are still waiting for the welcome message)
177 struct GNUNET_PeerIdentity target;
180 * API requirement (must be first).
182 struct SessionHeader header;
185 * We keep all sessions in a DLL at their respective
186 * 'struct MACEndpoint'.
188 struct Session *next;
191 * We keep all sessions in a DLL at their respective
192 * 'struct MACEndpoint'.
194 struct Session *prev;
197 * MAC endpoint with the address of this peer.
199 struct MacEndpoint *mac;
202 * Head of messages currently pending for transmission to this peer.
204 struct PendingMessage *pending_message_head;
207 * Tail of messages currently pending for transmission to this peer.
209 struct PendingMessage *pending_message_tail;
212 * When should this session time out?
214 struct GNUNET_TIME_Absolute timeout;
217 * Timeout task (for the session).
219 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
225 * Struct for messages that are being fragmented in a MAC's transmission queue.
227 struct FragmentMessage
231 * This is a doubly-linked list.
233 struct FragmentMessage *next;
236 * This is a doubly-linked list.
238 struct FragmentMessage *prev;
241 * MAC endpoint this message belongs to
243 struct MacEndpoint *macendpoint;
246 * Fragmentation context
248 struct GNUNET_FRAGMENT_Context *fragcontext;
251 * Transmission handle to helper (to cancel if the frag context
252 * is destroyed early for some reason).
254 struct GNUNET_HELPER_SendHandle *sh;
257 * Intended recipient.
259 struct GNUNET_PeerIdentity target;
262 * Timeout value for the message.
264 struct GNUNET_TIME_Absolute timeout;
269 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
272 * Continuation to call when we're done with this message.
274 GNUNET_TRANSPORT_TransmitContinuation cont;
282 * Size of original message
287 * Number of bytes used to transmit message
295 * Struct to represent one network card connection
301 * We keep all MACs in a DLL in the plugin.
303 struct MacEndpoint *next;
306 * We keep all MACs in a DLL in the plugin.
308 struct MacEndpoint *prev;
311 * Pointer to the global plugin struct.
313 struct Plugin *plugin;
316 * Head of sessions that use this MAC.
318 struct Session *sessions_head;
321 * Tail of sessions that use this MAC.
323 struct Session *sessions_tail;
326 * Head of messages we are currently sending to this MAC.
328 struct FragmentMessage *sending_messages_head;
331 * Tail of messages we are currently sending to this MAC.
333 struct FragmentMessage *sending_messages_tail;
336 * Defrag context for this MAC
338 struct GNUNET_DEFRAGMENT_Context *defrag;
341 * When should this endpoint time out?
343 struct GNUNET_TIME_Absolute timeout;
348 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
351 * count of messages in the fragment out queue for this mac endpoint
353 unsigned int fragment_messages_out_count;
358 struct WlanAddress addr;
361 * Message delay for fragmentation context
363 struct GNUNET_TIME_Relative msg_delay;
366 * ACK delay for fragmentation context
368 struct GNUNET_TIME_Relative ack_delay;
371 * Desired transmission power for this MAC
376 * Desired transmission rate for this MAC
381 * Antenna we should use for this MAC
389 * Encapsulation of all of the state of the plugin.
396 struct GNUNET_TRANSPORT_PluginEnvironment *env;
399 * Handle to helper process for priviledged operations.
401 struct GNUNET_HELPER_Handle *suid_helper;
404 * ARGV-vector for the helper (all helpers take only the binary
405 * name, one actual argument, plus the NULL terminator for 'argv').
407 char * helper_argv[3];
410 * The interface of the wlan card given to us by the user.
415 * Tokenizer for demultiplexing of data packets resulting from defragmentation.
417 struct GNUNET_SERVER_MessageStreamTokenizer *fragment_data_tokenizer;
420 * Tokenizer for demultiplexing of data packets received from the suid helper
422 struct GNUNET_SERVER_MessageStreamTokenizer *helper_payload_tokenizer;
425 * Tokenizer for demultiplexing of data packets that follow the WLAN Header
427 struct GNUNET_SERVER_MessageStreamTokenizer *wlan_header_payload_tokenizer;
430 * Head of list of open connections.
432 struct MacEndpoint *mac_head;
435 * Tail of list of open connections.
437 struct MacEndpoint *mac_tail;
440 * Number of connections
442 unsigned int mac_count;
445 * Task that periodically sends a HELLO beacon via the helper.
447 GNUNET_SCHEDULER_TaskIdentifier beacon_task;
450 * Tracker for bandwidth limit
452 struct GNUNET_BANDWIDTH_Tracker tracker;
455 * The mac_address of the wlan card given to us by the helper.
457 struct GNUNET_TRANSPORT_WLAN_MacAddress mac_address;
460 * Have we received a control message with our MAC address yet?
465 * Options for addresses
473 * Information associated with a message. Can contain
474 * the session or the MAC endpoint associated with the
480 * NULL if the identity of the other peer is not known.
482 struct Session *session;
485 * MAC address of the other peer, NULL if not known.
487 struct MacEndpoint *endpoint;
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.
497 * @param addr binary address
498 * @param addrlen length of the address
499 * @return string representing the same address
502 bluetooth_plugin_address_to_string (void *cls, const void *addr, size_t addrlen);
505 * Print MAC addresses nicely.
507 * @param mac the mac address
508 * @return string to a static buffer with the human-readable mac, will be overwritten during the next call to this function
511 mac_to_string (const struct GNUNET_TRANSPORT_WLAN_MacAddress * mac)
513 static char macstr[20];
515 GNUNET_snprintf (macstr, sizeof (macstr), "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
516 mac->mac[0], mac->mac[1],
517 mac->mac[2], mac->mac[3], mac->mac[4], mac->mac[5]);
523 * Fill the radiotap header
525 * @param endpoint pointer to the endpoint, can be NULL
526 * @param header pointer to the radiotap header
527 * @param size total message size
530 get_radiotap_header (struct MacEndpoint *endpoint,
531 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *header,
534 header->header.type = ntohs (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER);
535 header->header.size = ntohs (size);
536 if (NULL != endpoint)
538 header->rate = endpoint->rate;
539 header->tx_power = endpoint->tx_power;
540 header->antenna = endpoint->antenna;
545 header->tx_power = 0;
552 * Generate the WLAN hardware header for one packet
554 * @param plugin the plugin handle
555 * @param header address to write the header to
556 * @param to_mac_addr address of the recipient
557 * @param size size of the whole packet, needed to calculate the time to send the packet
560 get_wlan_header (struct Plugin *plugin,
561 struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *header,
562 const struct GNUNET_TRANSPORT_WLAN_MacAddress *to_mac_addr,
565 const int rate = 11000000;
567 header->frame_control = htons (IEEE80211_FC0_TYPE_DATA);
568 header->addr1 = *to_mac_addr;
569 header->addr2 = plugin->mac_address;
570 header->addr3 = mac_bssid_gnunet;
571 header->duration = GNUNET_htole16 ((size * 1000000) / rate + 290);
572 header->sequence_control = 0; // FIXME?
573 header->llc[0] = WLAN_LLC_DSAP_FIELD;
574 header->llc[1] = WLAN_LLC_SSAP_FIELD;
575 header->llc[2] = 0; // FIXME?
576 header->llc[3] = 0; // FIXME?
581 * Send an ACK for a fragment we received.
583 * @param cls the 'struct MacEndpoint' the ACK must be sent to
584 * @param msg_id id of the message
585 * @param hdr pointer to the hdr where the ack is stored
588 send_ack (void *cls, uint32_t msg_id,
589 const struct GNUNET_MessageHeader *hdr)
591 struct MacEndpoint *endpoint = cls;
592 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage* radio_header;
593 uint16_t msize = ntohs (hdr->size);
594 size_t size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + msize;
597 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
602 LOG (GNUNET_ERROR_TYPE_DEBUG,
603 "Sending ACK to helper\n");
604 radio_header = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) buf;
605 get_radiotap_header (endpoint, radio_header, size);
606 get_wlan_header (endpoint->plugin,
607 &radio_header->frame,
610 memcpy (&radio_header[1], hdr, msize);
612 GNUNET_HELPER_send (endpoint->plugin->suid_helper,
613 &radio_header->header,
614 GNUNET_NO /* dropping ACKs is bad */,
616 GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# Bluetooth ACKs sent"),
622 * Handles the data after all fragments are put together
624 * @param cls macendpoint this messages belongs to
625 * @param hdr pointer to the data
628 bluetooth_data_message_handler (void *cls, const struct GNUNET_MessageHeader *hdr)
630 struct MacEndpoint *endpoint = cls;
631 struct Plugin *plugin = endpoint->plugin;
632 struct MacAndSession mas;
634 GNUNET_STATISTICS_update (plugin->env->stats,
635 _("# Bluetooth messages defragmented"), 1,
638 mas.endpoint = endpoint;
639 (void) GNUNET_SERVER_mst_receive (plugin->fragment_data_tokenizer,
643 GNUNET_YES, GNUNET_NO);
650 * @param session the session free
653 free_session (struct Session *session)
655 struct MacEndpoint *endpoint = session->mac;
656 struct PendingMessage *pm;
658 endpoint->plugin->env->session_end (endpoint->plugin->env->cls,
661 while (NULL != (pm = session->pending_message_head))
663 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
664 session->pending_message_tail, pm);
665 if (GNUNET_SCHEDULER_NO_TASK != pm->timeout_task)
667 GNUNET_SCHEDULER_cancel (pm->timeout_task);
668 pm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
670 GNUNET_free (pm->msg);
673 GNUNET_CONTAINER_DLL_remove (endpoint->sessions_head,
674 endpoint->sessions_tail,
676 if (session->timeout_task != GNUNET_SCHEDULER_NO_TASK)
678 GNUNET_SCHEDULER_cancel (session->timeout_task);
679 session->timeout_task = GNUNET_SCHEDULER_NO_TASK;
681 GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# Bluetooth sessions allocated"), -1,
683 GNUNET_free (session);
688 * A session is timing out. Clean up.
690 * @param cls pointer to the Session
691 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
694 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
696 struct Session * session = cls;
697 struct GNUNET_TIME_Relative timeout;
699 session->timeout_task = GNUNET_SCHEDULER_NO_TASK;
700 timeout = GNUNET_TIME_absolute_get_remaining (session->timeout);
701 if (0 == timeout.rel_value_us)
703 free_session (session);
706 session->timeout_task =
707 GNUNET_SCHEDULER_add_delayed (timeout, &session_timeout, session);
712 * Create a new session
714 * @param endpoint pointer to the mac endpoint of the peer
715 * @param peer peer identity to use for this session
716 * @return returns the session
718 static struct Session *
719 create_session (struct MacEndpoint *endpoint,
720 const struct GNUNET_PeerIdentity *peer)
722 struct Session *session;
724 for (session = endpoint->sessions_head; NULL != session; session = session->next)
725 if (0 == memcmp (peer, &session->target,
726 sizeof (struct GNUNET_PeerIdentity)))
728 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
731 GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# Bluetooth sessions allocated"), 1,
733 session = GNUNET_malloc (sizeof (struct Session));
734 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
735 endpoint->sessions_tail,
737 session->mac = endpoint;
738 session->target = *peer;
739 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
740 session->timeout_task =
741 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &session_timeout, session);
742 LOG (GNUNET_ERROR_TYPE_DEBUG,
743 "Created new session for peer `%s' with endpoint %s\n",
745 mac_to_string (&endpoint->addr.mac));
751 * Function called once we have successfully given the fragment
752 * message to the SUID helper process and we are thus ready for
755 * @param cls the 'struct FragmentMessage'
756 * @param result result of the operation (GNUNET_OK on success, GNUNET_NO if the helper died, GNUNET_SYSERR
757 * if the helper was stopped)
760 fragment_transmission_done (void *cls,
763 struct FragmentMessage *fm = cls;
767 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
772 * Transmit a fragment of a message.
774 * @param cls 'struct FragmentMessage' this fragment message belongs to
775 * @param hdr pointer to the start of the fragment message
778 transmit_fragment (void *cls,
779 const struct GNUNET_MessageHeader *hdr)
781 struct FragmentMessage *fm = cls;
782 struct MacEndpoint *endpoint = fm->macendpoint;
786 msize = ntohs (hdr->size);
787 size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + msize;
790 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radio_header;
792 radio_header = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) buf;
793 get_radiotap_header (endpoint, radio_header, size);
794 get_wlan_header (endpoint->plugin,
795 &radio_header->frame,
798 memcpy (&radio_header[1], hdr, msize);
799 GNUNET_assert (NULL == fm->sh);
800 fm->sh = GNUNET_HELPER_send (endpoint->plugin->suid_helper,
801 &radio_header->header,
803 &fragment_transmission_done, fm);
804 fm->size_on_wire += size;
806 GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# Bluetooth message fragments sent"),
809 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
810 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
811 "# bytes currently in Bluetooth buffers",
813 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
814 "# bytes transmitted via Bluetooth",
821 * Frees the space of a message in the fragment queue (send queue)
823 * @param fm message to free
826 free_fragment_message (struct FragmentMessage *fm)
828 struct MacEndpoint *endpoint = fm->macendpoint;
830 GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# Bluetooth messages pending (with fragmentation)"),
832 GNUNET_CONTAINER_DLL_remove (endpoint->sending_messages_head,
833 endpoint->sending_messages_tail, fm);
836 GNUNET_HELPER_send_cancel (fm->sh);
839 GNUNET_FRAGMENT_context_destroy (fm->fragcontext,
840 &endpoint->msg_delay,
841 &endpoint->ack_delay);
842 if (fm->timeout_task != GNUNET_SCHEDULER_NO_TASK)
844 GNUNET_SCHEDULER_cancel (fm->timeout_task);
845 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
852 * A FragmentMessage has timed out. Remove it.
854 * @param cls pointer to the 'struct FragmentMessage'
855 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
858 fragmentmessage_timeout (void *cls,
859 const struct GNUNET_SCHEDULER_TaskContext *tc)
861 struct FragmentMessage *fm = cls;
863 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
864 if (NULL != fm->cont)
866 fm->cont (fm->cont_cls, &fm->target, GNUNET_SYSERR, fm->size_payload, fm->size_on_wire);
869 free_fragment_message (fm);
874 * Transmit a message to the given destination with fragmentation.
876 * @param endpoint desired destination
877 * @param timeout how long can the message wait?
878 * @param target peer that should receive the message
879 * @param msg message to transmit
880 * @param payload_size bytes of payload
881 * @param cont continuation to call once the message has
882 * been transmitted (or if the transport is ready
883 * for the next transmission call; or if the
884 * peer disconnected...); can be NULL
885 * @param cont_cls closure for cont
888 send_with_fragmentation (struct MacEndpoint *endpoint,
889 struct GNUNET_TIME_Relative timeout,
890 const struct GNUNET_PeerIdentity *target,
891 const struct GNUNET_MessageHeader *msg,
893 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
896 struct FragmentMessage *fm;
897 struct Plugin *plugin;
899 plugin = endpoint->plugin;
900 fm = GNUNET_malloc (sizeof (struct FragmentMessage));
901 fm->macendpoint = endpoint;
902 fm->target = *target;
903 fm->size_payload = payload_size;
904 fm->size_on_wire = 0;
905 fm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
907 fm->cont_cls = cont_cls;
908 /* 1 MBit/s typical data rate, 1430 byte fragments => ~100 ms per message */
910 GNUNET_FRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
915 &transmit_fragment, fm);
917 GNUNET_SCHEDULER_add_delayed (timeout,
918 &fragmentmessage_timeout, fm);
919 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sending_messages_head,
920 endpoint->sending_messages_tail,
926 * Free a MAC endpoint.
928 * @param endpoint pointer to the MacEndpoint to free
931 free_macendpoint (struct MacEndpoint *endpoint)
933 struct Plugin *plugin = endpoint->plugin;
934 struct FragmentMessage *fm;
935 struct Session *session;
937 GNUNET_STATISTICS_update (plugin->env->stats,
938 _("# Bluetooth MAC endpoints allocated"), -1, GNUNET_NO);
939 while (NULL != (session = endpoint->sessions_head))
940 free_session (session);
941 while (NULL != (fm = endpoint->sending_messages_head))
942 free_fragment_message (fm);
943 GNUNET_CONTAINER_DLL_remove (plugin->mac_head,
947 if (NULL != endpoint->defrag)
949 GNUNET_DEFRAGMENT_context_destroy(endpoint->defrag);
950 endpoint->defrag = NULL;
954 if (GNUNET_SCHEDULER_NO_TASK != endpoint->timeout_task)
956 GNUNET_SCHEDULER_cancel (endpoint->timeout_task);
957 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
959 GNUNET_free (endpoint);
964 * A MAC endpoint is timing out. Clean up.
966 * @param cls pointer to the MacEndpoint
967 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
970 macendpoint_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
972 struct MacEndpoint *endpoint = cls;
973 struct GNUNET_TIME_Relative timeout;
975 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
976 timeout = GNUNET_TIME_absolute_get_remaining (endpoint->timeout);
977 if (0 == timeout.rel_value_us)
979 free_macendpoint (endpoint);
982 endpoint->timeout_task =
983 GNUNET_SCHEDULER_add_delayed (timeout, &macendpoint_timeout,
989 * Find (or create) a MacEndpoint with a specific MAC address
991 * @param plugin pointer to the plugin struct
992 * @param addr the MAC address of the endpoint
993 * @return handle to our data structure for this MAC
995 static struct MacEndpoint *
996 create_macendpoint (struct Plugin *plugin,
997 const struct WlanAddress *addr)
999 struct MacEndpoint *pos;
1001 for (pos = plugin->mac_head; NULL != pos; pos = pos->next)
1002 if (0 == memcmp (addr, &pos->addr, sizeof (struct WlanAddress)))
1004 pos = GNUNET_malloc (sizeof (struct MacEndpoint));
1006 pos->plugin = plugin;
1008 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
1009 MESSAGES_IN_DEFRAG_QUEUE_PER_MAC,
1011 &bluetooth_data_message_handler,
1014 pos->msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1015 pos->ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1017 pos->timeout = GNUNET_TIME_relative_to_absolute (MACENDPOINT_TIMEOUT);
1019 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
1021 GNUNET_CONTAINER_DLL_insert (plugin->mac_head, plugin->mac_tail, pos);
1022 plugin->mac_count++;
1023 GNUNET_STATISTICS_update (plugin->env->stats, _("# Bluetooth MAC endpoints allocated"),
1025 LOG (GNUNET_ERROR_TYPE_DEBUG,
1026 "New MAC endpoint `%s'\n",
1027 bluetooth_plugin_address_to_string(NULL, addr, sizeof (struct WlanAddress)));
1033 * Function obtain the network type for a session
1035 * @param cls closure ('struct Plugin*')
1036 * @param session the session
1037 * @return the network type in HBO or GNUNET_SYSERR
1039 static enum GNUNET_ATS_Network_Type
1040 bluetooth_get_network (void *cls,
1041 struct Session *session)
1043 GNUNET_assert (NULL != session);
1044 return GNUNET_ATS_NET_BT;
1049 * Creates a new outbound session the transport service will use to send data to the
1052 * @param cls the plugin
1053 * @param address the address
1054 * @return the session or NULL of max connections exceeded
1056 static struct Session *
1057 bluetooth_plugin_get_session (void *cls,
1058 const struct GNUNET_HELLO_Address *address)
1060 struct Plugin *plugin = cls;
1061 struct MacEndpoint *endpoint;
1063 if (NULL == address)
1065 if (sizeof (struct WlanAddress) != address->address_length)
1070 LOG (GNUNET_ERROR_TYPE_DEBUG,
1071 "Service asked to create session for peer `%s' with MAC `%s'\n",
1072 GNUNET_i2s (&address->peer),
1073 bluetooth_plugin_address_to_string(NULL, address->address, address->address_length));
1074 endpoint = create_macendpoint (plugin, address->address);
1075 return create_session (endpoint, &address->peer);
1080 * Function that can be used to force the plugin to disconnect
1081 * from the given peer and cancel all previous transmissions
1082 * (and their continuation).
1084 * @param cls closure
1085 * @param target peer from which to disconnect
1088 bluetooth_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
1090 struct Plugin *plugin = cls;
1091 struct Session *session;
1092 struct MacEndpoint *endpoint;
1094 for (endpoint = plugin->mac_head; NULL != endpoint; endpoint = endpoint->next)
1095 for (session = endpoint->sessions_head; NULL != session; session = session->next)
1096 if (0 == memcmp (target, &session->target,
1097 sizeof (struct GNUNET_PeerIdentity)))
1099 free_session (session);
1100 break; /* inner-loop only (in case peer has another MAC as well!) */
1106 * Function that can be used by the transport service to transmit
1107 * a message using the plugin. Note that in the case of a
1108 * peer disconnecting, the continuation MUST be called
1109 * prior to the disconnect notification itself. This function
1110 * will be called with this peer's HELLO message to initiate
1111 * a fresh connection to another peer.
1113 * @param cls closure
1114 * @param session which session must be used
1115 * @param msgbuf the message to transmit
1116 * @param msgbuf_size number of bytes in 'msgbuf'
1117 * @param priority how important is the message (most plugins will
1118 * ignore message priority and just FIFO)
1119 * @param to how long to wait at most for the transmission (does not
1120 * require plugins to discard the message after the timeout,
1121 * just advisory for the desired delay; most plugins will ignore
1123 * @param cont continuation to call once the message has
1124 * been transmitted (or if the transport is ready
1125 * for the next transmission call; or if the
1126 * peer disconnected...); can be NULL
1127 * @param cont_cls closure for cont
1128 * @return number of bytes used (on the physical network, with overheads);
1129 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1130 * and does NOT mean that the message was not transmitted (DV)
1133 bluetooth_plugin_send (void *cls,
1134 struct Session *session,
1135 const char *msgbuf, size_t msgbuf_size,
1136 unsigned int priority,
1137 struct GNUNET_TIME_Relative to,
1138 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1140 struct Plugin *plugin = cls;
1141 struct WlanHeader *wlanheader;
1142 size_t size = msgbuf_size + sizeof (struct WlanHeader);
1143 char buf[size] GNUNET_ALIGN;
1145 LOG (GNUNET_ERROR_TYPE_DEBUG,
1146 "Transmitting %u bytes of payload to peer `%s' (starting with %u byte message of type %u)\n",
1148 GNUNET_i2s (&session->target),
1149 (unsigned int) ntohs (((struct GNUNET_MessageHeader*)msgbuf)->size),
1150 (unsigned int) ntohs (((struct GNUNET_MessageHeader*)msgbuf)->type));
1151 wlanheader = (struct WlanHeader *) buf;
1152 wlanheader->header.size = htons (msgbuf_size + sizeof (struct WlanHeader));
1153 wlanheader->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA);
1154 wlanheader->sender = *plugin->env->my_identity;
1155 wlanheader->target = session->target;
1156 wlanheader->crc = htonl (GNUNET_CRYPTO_crc32_n (msgbuf, msgbuf_size));
1157 memcpy (&wlanheader[1], msgbuf, msgbuf_size);
1159 GNUNET_STATISTICS_update (plugin->env->stats,
1160 "# bytes currently in Bluetooth buffers",
1161 msgbuf_size, GNUNET_NO);
1163 send_with_fragmentation (session->mac,
1166 &wlanheader->header,
1174 * We have received data from the WLAN via some session. Process depending
1175 * on the message type (HELLO, DATA, FRAGMENTATION or FRAGMENTATION-ACK).
1177 * @param cls pointer to the plugin
1178 * @param client pointer to the session this message belongs to
1179 * @param hdr start of the message
1182 process_data (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
1184 struct Plugin *plugin = cls;
1185 struct MacAndSession *mas = client;
1186 struct MacAndSession xmas;
1187 struct GNUNET_ATS_Information ats;
1188 struct FragmentMessage *fm;
1189 struct GNUNET_PeerIdentity tmpsource;
1190 const struct WlanHeader *wlanheader;
1194 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1195 ats.value = htonl (GNUNET_ATS_NET_BT);
1196 msize = ntohs (hdr->size);
1198 GNUNET_STATISTICS_update (plugin->env->stats,
1199 "# bytes received via Bluetooth",
1202 switch (ntohs (hdr->type))
1204 case GNUNET_MESSAGE_TYPE_HELLO:
1206 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hdr, &tmpsource))
1208 GNUNET_break_op (0);
1211 LOG (GNUNET_ERROR_TYPE_DEBUG,
1212 "Processing %u bytes of HELLO from peer `%s' at MAC %s\n",
1213 (unsigned int) msize,
1214 GNUNET_i2s (&tmpsource),
1215 bluetooth_plugin_address_to_string (NULL, &mas->endpoint->addr, sizeof (struct WlanAddress)));
1217 GNUNET_STATISTICS_update (plugin->env->stats,
1218 _("# HELLO messages received via Bluetooth"), 1,
1220 plugin->env->receive (plugin->env->cls,
1224 (mas->endpoint == NULL) ? NULL : (const char *) &mas->endpoint->addr,
1225 (mas->endpoint == NULL) ? 0 : sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress));
1226 plugin->env->update_address_metrics (plugin->env->cls,
1228 (mas->endpoint == NULL) ? NULL : (const char *) &mas->endpoint->addr,
1229 (mas->endpoint == NULL) ? 0 : sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress),
1233 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1234 if (NULL == mas->endpoint)
1239 LOG (GNUNET_ERROR_TYPE_DEBUG,
1240 "Processing %u bytes of FRAGMENT from MAC %s\n",
1241 (unsigned int) msize,
1242 bluetooth_plugin_address_to_string (NULL, &mas->endpoint->addr, sizeof (struct WlanAddress)));
1243 GNUNET_STATISTICS_update (plugin->env->stats,
1244 _("# fragments received via Bluetooth"), 1, GNUNET_NO);
1245 (void) GNUNET_DEFRAGMENT_process_fragment (mas->endpoint->defrag,
1248 case GNUNET_MESSAGE_TYPE_FRAGMENT_ACK:
1249 if (NULL == mas->endpoint)
1254 GNUNET_STATISTICS_update (plugin->env->stats, _("# ACKs received via Bluetooth"),
1256 for (fm = mas->endpoint->sending_messages_head; NULL != fm; fm = fm->next)
1258 ret = GNUNET_FRAGMENT_process_ack (fm->fragcontext, hdr);
1259 if (GNUNET_OK == ret)
1261 LOG (GNUNET_ERROR_TYPE_DEBUG,
1262 "Got last ACK, finished message transmission to `%s' (%p)\n",
1263 bluetooth_plugin_address_to_string (NULL, &mas->endpoint->addr, sizeof (struct WlanAddress)),
1265 mas->endpoint->timeout = GNUNET_TIME_relative_to_absolute (MACENDPOINT_TIMEOUT);
1266 if (NULL != fm->cont)
1268 fm->cont (fm->cont_cls, &fm->target, GNUNET_OK, fm->size_payload, fm->size_on_wire);
1271 free_fragment_message (fm);
1274 if (GNUNET_NO == ret)
1276 LOG (GNUNET_ERROR_TYPE_DEBUG,
1277 "Got an ACK, message transmission to `%s' not yet finished\n",
1278 bluetooth_plugin_address_to_string (NULL, &mas->endpoint->addr, sizeof (struct WlanAddress)));
1282 LOG (GNUNET_ERROR_TYPE_DEBUG,
1283 "ACK not matched against any active fragmentation with MAC `%s'\n",
1284 bluetooth_plugin_address_to_string (NULL, &mas->endpoint->addr, sizeof (struct WlanAddress)));
1286 case GNUNET_MESSAGE_TYPE_WLAN_DATA:
1287 if (NULL == mas->endpoint)
1292 if (msize < sizeof (struct WlanHeader))
1297 wlanheader = (const struct WlanHeader *) hdr;
1298 if (0 != memcmp (&wlanheader->target,
1299 plugin->env->my_identity,
1300 sizeof (struct GNUNET_PeerIdentity)))
1302 LOG (GNUNET_ERROR_TYPE_DEBUG,
1303 "Bluetooth data for `%s', not for me, ignoring\n",
1304 GNUNET_i2s (&wlanheader->target));
1307 if (ntohl (wlanheader->crc) !=
1308 GNUNET_CRYPTO_crc32_n (&wlanheader[1], msize - sizeof (struct WlanHeader)))
1310 GNUNET_STATISTICS_update (plugin->env->stats,
1311 _("# Bluetooth DATA messages discarded due to CRC32 error"), 1,
1315 xmas.endpoint = mas->endpoint;
1316 xmas.session = create_session (mas->endpoint, &wlanheader->sender);
1317 LOG (GNUNET_ERROR_TYPE_DEBUG,
1318 "Processing %u bytes of BLUETOOTH DATA from peer `%s'\n",
1319 (unsigned int) msize,
1320 GNUNET_i2s (&wlanheader->sender));
1321 (void) GNUNET_SERVER_mst_receive (plugin->wlan_header_payload_tokenizer,
1323 (const char *) &wlanheader[1],
1324 msize - sizeof (struct WlanHeader),
1325 GNUNET_YES, GNUNET_NO);
1328 if (NULL == mas->endpoint)
1333 if (NULL == mas->session)
1338 LOG (GNUNET_ERROR_TYPE_DEBUG,
1339 "Received packet with %u bytes of type %u from peer %s\n",
1340 (unsigned int) msize,
1341 (unsigned int) ntohs (hdr->type),
1342 GNUNET_i2s (&mas->session->target));
1343 plugin->env->receive (plugin->env->cls,
1344 &mas->session->target,
1347 (mas->endpoint == NULL) ? NULL : (const char *) &mas->endpoint->addr,
1348 (mas->endpoint == NULL) ? 0 : sizeof (struct WlanAddress));
1349 plugin->env->update_address_metrics (plugin->env->cls,
1350 &mas->session->target,
1351 (mas->endpoint == NULL) ? NULL : (const char *) &mas->endpoint->addr,
1352 (mas->endpoint == NULL) ? 0 : sizeof (struct WlanAddress),
1362 * Function used for to process the data from the suid process
1364 * @param cls the plugin handle
1365 * @param client client that send the data (not used)
1366 * @param hdr header of the GNUNET_MessageHeader
1369 handle_helper_message (void *cls, void *client,
1370 const struct GNUNET_MessageHeader *hdr)
1372 struct Plugin *plugin = cls;
1373 const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rxinfo;
1374 const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *cm;
1375 struct WlanAddress wa;
1376 struct MacAndSession mas;
1379 msize = ntohs (hdr->size);
1380 switch (ntohs (hdr->type))
1382 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
1383 if (msize != sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage))
1388 cm = (const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *) hdr;
1389 if (GNUNET_YES == plugin->have_mac)
1391 if (0 == memcmp (&plugin->mac_address,
1393 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1394 break; /* no change */
1395 /* remove old address */
1396 memset (&wa, 0, sizeof (struct WlanAddress));
1397 wa.mac = plugin->mac_address;
1398 wa.options = htonl(plugin->options);
1399 plugin->env->notify_address (plugin->env->cls, GNUNET_NO,
1404 plugin->mac_address = cm->mac;
1405 plugin->have_mac = GNUNET_YES;
1406 memset (&wa, 0, sizeof (struct WlanAddress));
1407 wa.mac = plugin->mac_address;
1408 wa.options = htonl(plugin->options);
1409 LOG (GNUNET_ERROR_TYPE_DEBUG,
1410 "Received BT_HELPER_CONTROL message with MAC address `%s' for peer `%s'\n",
1411 mac_to_string (&cm->mac),
1412 GNUNET_i2s (plugin->env->my_identity));
1413 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
1415 sizeof (struct WlanAddress),
1418 case GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER:
1419 LOG (GNUNET_ERROR_TYPE_DEBUG,
1420 "Got data message from helper with %u bytes\n",
1422 GNUNET_STATISTICS_update (plugin->env->stats,
1423 _("# DATA messages received via Bluetooth"), 1,
1425 if (msize < sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage))
1428 LOG (GNUNET_ERROR_TYPE_DEBUG,
1429 "Size of packet is too small (%u bytes)\n",
1433 rxinfo = (const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) hdr;
1435 /* check if message is actually for us */
1436 if (0 != memcmp (&rxinfo->frame.addr3, &mac_bssid_gnunet,
1437 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1439 /* Not the GNUnet BSSID */
1442 if ( (0 != memcmp (&rxinfo->frame.addr1, &bc_all_mac,
1443 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))) &&
1444 (0 != memcmp (&rxinfo->frame.addr1, &plugin->mac_address,
1445 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))) )
1447 /* Neither broadcast nor specifically for us */
1450 if (0 == memcmp (&rxinfo->frame.addr2, &plugin->mac_address,
1451 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1453 /* packet is FROM us, thus not FOR us */
1457 GNUNET_STATISTICS_update (plugin->env->stats,
1458 _("# Bluetooth DATA messages processed"),
1460 LOG (GNUNET_ERROR_TYPE_DEBUG,
1461 "Receiving %u bytes of data from MAC `%s'\n",
1462 (unsigned int) (msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)),
1463 mac_to_string (&rxinfo->frame.addr2));
1464 wa.mac = rxinfo->frame.addr2;
1465 wa.options = htonl (0);
1466 mas.endpoint = create_macendpoint (plugin, &wa);
1468 (void) GNUNET_SERVER_mst_receive (plugin->helper_payload_tokenizer,
1470 (const char*) &rxinfo[1],
1471 msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage),
1472 GNUNET_YES, GNUNET_NO);
1476 LOG (GNUNET_ERROR_TYPE_DEBUG,
1477 "Unexpected message of type %u (%u bytes)",
1478 ntohs (hdr->type), ntohs (hdr->size));
1487 * Task to (periodically) send a HELLO beacon
1489 * @param cls pointer to the plugin struct
1490 * @param tc scheduler context
1493 send_hello_beacon (void *cls,
1494 const struct GNUNET_SCHEDULER_TaskContext *tc)
1496 struct Plugin *plugin = cls;
1498 uint16_t hello_size;
1499 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
1500 const struct GNUNET_MessageHeader *hello;
1502 hello = plugin->env->get_our_hello ();
1503 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1504 GNUNET_assert (sizeof (struct WlanHeader) + hello_size <= WLAN_MTU);
1505 size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + hello_size;
1507 char buf[size] GNUNET_ALIGN;
1509 LOG (GNUNET_ERROR_TYPE_DEBUG,
1510 "Sending %u byte HELLO beacon\n",
1511 (unsigned int) size);
1512 radioHeader = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage*) buf;
1513 get_radiotap_header (NULL, radioHeader, size);
1514 get_wlan_header (plugin, &radioHeader->frame, &bc_all_mac, size);
1515 memcpy (&radioHeader[1], hello, hello_size);
1517 GNUNET_HELPER_send (plugin->suid_helper,
1518 &radioHeader->header,
1519 GNUNET_YES /* can drop */,
1521 GNUNET_STATISTICS_update (plugin->env->stats, _("# HELLO beacons sent via Bluetooth"),
1524 plugin->beacon_task =
1525 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1526 (HELLO_BEACON_SCALING_FACTOR,
1527 plugin->mac_count + 1),
1535 * Another peer has suggested an address for this
1536 * peer and transport plugin. Check that this could be a valid
1537 * address. If so, consider adding it to the list
1540 * @param cls closure
1541 * @param addr pointer to the address
1542 * @param addrlen length of addr
1543 * @return GNUNET_OK if this is a plausible address for this peer
1547 bluetooth_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1549 struct Plugin *plugin = cls;
1550 struct WlanAddress *wa = (struct WlanAddress *) addr;
1552 if (addrlen != sizeof (struct WlanAddress))
1554 GNUNET_break_op (0);
1555 return GNUNET_SYSERR;
1557 if (GNUNET_YES != plugin->have_mac)
1559 LOG (GNUNET_ERROR_TYPE_DEBUG,
1560 "Rejecting MAC `%s': I don't know my MAC!\n",
1561 mac_to_string (addr));
1562 return GNUNET_NO; /* don't know my MAC */
1564 if (0 != memcmp (&wa->mac,
1565 &plugin->mac_address,
1568 LOG (GNUNET_ERROR_TYPE_DEBUG,
1569 "Rejecting MAC `%s': not my MAC!\n",
1570 mac_to_string (addr));
1571 return GNUNET_NO; /* not my MAC */
1578 * Function called for a quick conversion of the binary address to
1579 * a numeric address. Note that the caller must not free the
1580 * address and that the next call to this function is allowed
1581 * to override the address again.
1583 * @param cls closure
1584 * @param addr binary address
1585 * @param addrlen length of the address
1586 * @return string representing the same address
1589 bluetooth_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
1591 const struct GNUNET_TRANSPORT_WLAN_MacAddress *mac;
1592 static char macstr[36];
1594 if (sizeof (struct WlanAddress) != addrlen)
1599 mac = &((struct WlanAddress *) addr)->mac;
1600 GNUNET_snprintf (macstr, sizeof (macstr), "%s.%u.%s",
1601 PLUGIN_NAME, ntohl (((struct WlanAddress *) addr)->options),
1602 mac_to_string (mac));
1608 * Convert the transports address to a nice, human-readable format.
1610 * @param cls closure
1611 * @param type name of the transport that generated the address
1612 * @param addr one of the addresses of the host, NULL for the last address
1613 * the specific address format depends on the transport
1614 * @param addrlen length of the address
1615 * @param numeric should (IP) addresses be displayed in numeric form?
1616 * @param timeout after how long should we give up?
1617 * @param asc function to call on each string
1618 * @param asc_cls closure for asc
1621 bluetooth_plugin_address_pretty_printer (void *cls, const char *type,
1622 const void *addr, size_t addrlen,
1624 struct GNUNET_TIME_Relative timeout,
1625 GNUNET_TRANSPORT_AddressStringCallback asc,
1630 if (sizeof (struct WlanAddress) != addrlen)
1632 /* invalid address */
1633 LOG (GNUNET_ERROR_TYPE_WARNING,
1634 _("Bluetooth address with invalid size encountered\n"));
1635 asc (asc_cls, NULL);
1638 ret = GNUNET_strdup (bluetooth_plugin_address_to_string(NULL, addr, addrlen));
1641 asc (asc_cls, NULL);
1646 * Exit point from the plugin.
1648 * @param cls pointer to the api struct
1651 libgnunet_plugin_transport_bluetooth_done (void *cls)
1653 struct WlanAddress wa;
1654 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1655 struct Plugin *plugin = api->cls;
1656 struct MacEndpoint *endpoint;
1657 struct MacEndpoint *endpoint_next;
1665 if (GNUNET_YES == plugin->have_mac)
1667 memset (&wa, 0, sizeof (wa));
1668 wa.options = htonl (plugin->options);
1669 wa.mac = plugin->mac_address;
1670 plugin->env->notify_address (plugin->env->cls, GNUNET_NO,
1672 sizeof (struct WlanAddress),
1674 plugin->have_mac = GNUNET_NO;
1677 if (GNUNET_SCHEDULER_NO_TASK != plugin->beacon_task)
1679 GNUNET_SCHEDULER_cancel (plugin->beacon_task);
1680 plugin->beacon_task = GNUNET_SCHEDULER_NO_TASK;
1682 if (NULL != plugin->suid_helper)
1684 GNUNET_HELPER_stop (plugin->suid_helper, GNUNET_NO);
1685 plugin->suid_helper = NULL;
1687 endpoint_next = plugin->mac_head;
1688 while (NULL != (endpoint = endpoint_next))
1690 endpoint_next = endpoint->next;
1691 free_macendpoint (endpoint);
1693 if (NULL != plugin->fragment_data_tokenizer)
1695 GNUNET_SERVER_mst_destroy (plugin->fragment_data_tokenizer);
1696 plugin->fragment_data_tokenizer = NULL;
1698 if (NULL != plugin->wlan_header_payload_tokenizer)
1700 GNUNET_SERVER_mst_destroy (plugin->wlan_header_payload_tokenizer);
1701 plugin->wlan_header_payload_tokenizer = NULL;
1703 if (NULL != plugin->helper_payload_tokenizer)
1705 GNUNET_SERVER_mst_destroy (plugin->helper_payload_tokenizer);
1706 plugin->helper_payload_tokenizer = NULL;
1708 GNUNET_free_non_null (plugin->interface);
1709 GNUNET_free (plugin);
1716 * Function called to convert a string address to
1719 * @param cls closure ('struct Plugin*')
1720 * @param addr string address
1721 * @param addrlen length of the address
1722 * @param buf location to store the buffer
1723 * @param added location to store the number of bytes in the buffer.
1724 * If the function returns GNUNET_SYSERR, its contents are undefined.
1725 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1728 bluetooth_string_to_address (void *cls, const char *addr, uint16_t addrlen,
1729 void **buf, size_t *added)
1731 struct WlanAddress *wa;
1737 if ((NULL == addr) || (addrlen == 0))
1740 return GNUNET_SYSERR;
1742 if ('\0' != addr[addrlen - 1])
1745 return GNUNET_SYSERR;
1747 if (strlen (addr) != addrlen - 1)
1750 return GNUNET_SYSERR;
1753 if (8 != SSCANF (addr,
1754 "%9s.%u.%X:%X:%X:%X:%X:%X",
1756 &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]))
1759 return GNUNET_SYSERR;
1761 wa = GNUNET_malloc (sizeof (struct WlanAddress));
1763 wa->mac.mac[i] = a[i];
1764 wa->options = htonl (0);
1766 *added = sizeof (struct WlanAddress);
1772 * Entry point for the plugin.
1774 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
1775 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
1778 libgnunet_plugin_transport_bluetooth_init (void *cls)
1780 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1781 struct GNUNET_TRANSPORT_PluginFunctions *api;
1782 struct Plugin *plugin;
1784 unsigned long long testmode;
1787 /* check for 'special' mode */
1788 if (NULL == env->receive)
1790 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1791 initialze the plugin or the API */
1792 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1794 api->address_pretty_printer = &bluetooth_plugin_address_pretty_printer;
1795 api->address_to_string = &bluetooth_plugin_address_to_string;
1796 api->string_to_address = &bluetooth_string_to_address;
1801 /* check configuration */
1803 GNUNET_CONFIGURATION_have_value (env->cfg, "transport-bluetooth", "TESTMODE")) &&
1805 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-bluetooth",
1806 "TESTMODE", &testmode)) ||
1809 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1810 "transport-bluetooth", "TESTMODE");
1813 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-transport-bluetooth");
1814 if ( (0 == testmode) &&
1815 (GNUNET_YES != GNUNET_OS_check_helper_binary (binary, GNUNET_YES, NULL)) )
1817 LOG (GNUNET_ERROR_TYPE_ERROR,
1818 _("Helper binary `%s' not SUID, cannot run bluetooth transport\n"),
1819 "gnunet-helper-transport-bluetooth");
1820 GNUNET_free (binary);
1823 GNUNET_free (binary);
1825 GNUNET_CONFIGURATION_get_value_string
1826 (env->cfg, "transport-bluetooth", "INTERFACE",
1829 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1830 "transport-bluetooth", "INTERFACE");
1834 plugin = GNUNET_malloc (sizeof (struct Plugin));
1835 plugin->interface = interface;
1837 GNUNET_STATISTICS_set (plugin->env->stats, _("# Bluetooth sessions allocated"),
1839 GNUNET_STATISTICS_set (plugin->env->stats, _("# Bluetooth MAC endpoints allocated"),
1841 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
1842 GNUNET_BANDWIDTH_value_init (100 * 1024 *
1844 plugin->fragment_data_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
1845 plugin->wlan_header_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
1846 plugin->helper_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
1847 plugin->beacon_task = GNUNET_SCHEDULER_add_now (&send_hello_beacon,
1850 plugin->options = 0;
1852 /* some compilers do not like switch on 'long long'... */
1853 switch ((unsigned int) testmode)
1855 case 0: /* normal */
1856 plugin->helper_argv[0] = (char *) "gnunet-helper-transport-bluetooth";
1857 plugin->helper_argv[1] = interface;
1858 plugin->helper_argv[2] = NULL;
1859 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
1860 "gnunet-helper-transport-bluetooth",
1861 plugin->helper_argv,
1862 &handle_helper_message,
1866 case 1: /* testmode, peer 1 */
1867 plugin->helper_argv[0] = (char *) "gnunet-helper-transport-wlan-dummy";
1868 plugin->helper_argv[1] = (char *) "1";
1869 plugin->helper_argv[2] = NULL;
1870 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
1871 "gnunet-helper-transport-wlan-dummy",
1872 plugin->helper_argv,
1873 &handle_helper_message,
1877 case 2: /* testmode, peer 2 */
1878 plugin->helper_argv[0] = (char *) "gnunet-helper-transport-wlan-dummy";
1879 plugin->helper_argv[1] = (char *) "2";
1880 plugin->helper_argv[2] = NULL;
1881 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
1882 "gnunet-helper-transport-wlan-dummy",
1883 plugin->helper_argv,
1884 &handle_helper_message,
1892 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1894 api->send = &bluetooth_plugin_send;
1895 api->get_session = &bluetooth_plugin_get_session;
1896 api->disconnect = &bluetooth_plugin_disconnect;
1897 api->address_pretty_printer = &bluetooth_plugin_address_pretty_printer;
1898 api->check_address = &bluetooth_plugin_address_suggested;
1899 api->address_to_string = &bluetooth_plugin_address_to_string;;
1900 api->string_to_address = &bluetooth_string_to_address;
1901 api->get_network = &bluetooth_get_network;
1907 /* end of plugin_transport_bluetooth.c */