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 * We keep all sessions in a DLL at their respective
181 * `struct MACEndpoint *`.
183 struct Session *next;
186 * We keep all sessions in a DLL at their respective
187 * 'struct MACEndpoint'.
189 struct Session *prev;
192 * MAC endpoint with the address of this peer.
194 struct MacEndpoint *mac;
197 * The address for this session
199 struct GNUNET_HELLO_Address *address;
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;
277 * Closure for @e 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;
353 struct WlanAddress addr;
356 * Message delay for fragmentation context
358 struct GNUNET_TIME_Relative msg_delay;
361 * ACK delay for fragmentation context
363 struct GNUNET_TIME_Relative ack_delay;
366 * count of messages in the fragment out queue for this mac endpoint
368 unsigned int fragment_messages_out_count;
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 @a addr
499 * @return string representing the same address
502 bluetooth_plugin_address_to_string (void *cls,
507 * Print MAC addresses nicely.
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
515 mac_to_string (const struct GNUNET_TRANSPORT_WLAN_MacAddress * mac)
517 static char macstr[20];
519 GNUNET_snprintf (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]);
530 * Fill the radiotap header
532 * @param endpoint pointer to the endpoint, can be NULL
533 * @param header pointer to the radiotap header
534 * @param size total message size
537 get_radiotap_header (struct MacEndpoint *endpoint,
538 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *header,
541 header->header.type = ntohs (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER);
542 header->header.size = ntohs (size);
543 if (NULL != endpoint)
545 header->rate = endpoint->rate;
546 header->tx_power = endpoint->tx_power;
547 header->antenna = endpoint->antenna;
552 header->tx_power = 0;
559 * Generate the WLAN hardware header for one packet
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
567 get_wlan_header (struct Plugin *plugin,
568 struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *header,
569 const struct GNUNET_TRANSPORT_WLAN_MacAddress *to_mac_addr,
572 const int rate = 11000000;
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?
588 * Send an ACK for a fragment we received.
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
595 send_ack (void *cls, uint32_t msg_id,
596 const struct GNUNET_MessageHeader *hdr)
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;
604 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
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,
616 sizeof (endpoint->addr.mac));
617 memcpy (&radio_header[1], hdr, msize);
619 GNUNET_HELPER_send (endpoint->plugin->suid_helper,
620 &radio_header->header,
621 GNUNET_NO /* dropping ACKs is bad */,
623 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
624 _("# Bluetooth ACKs sent"),
630 * Handles the data after all fragments are put together
632 * @param cls macendpoint this messages belongs to
633 * @param hdr pointer to the data
636 bluetooth_data_message_handler (void *cls,
637 const struct GNUNET_MessageHeader *hdr)
639 struct MacEndpoint *endpoint = cls;
640 struct Plugin *plugin = endpoint->plugin;
641 struct MacAndSession mas;
643 GNUNET_STATISTICS_update (plugin->env->stats,
644 _("# Bluetooth messages defragmented"), 1,
647 mas.endpoint = endpoint;
648 (void) GNUNET_SERVER_mst_receive (plugin->fragment_data_tokenizer,
652 GNUNET_YES, GNUNET_NO);
659 * @param session the session free
662 free_session (struct Session *session)
664 struct MacEndpoint *endpoint = session->mac;
665 struct PendingMessage *pm;
667 endpoint->plugin->env->session_end (endpoint->plugin->env->cls,
670 while (NULL != (pm = session->pending_message_head))
672 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
673 session->pending_message_tail, pm);
674 if (GNUNET_SCHEDULER_NO_TASK != pm->timeout_task)
676 GNUNET_SCHEDULER_cancel (pm->timeout_task);
677 pm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
679 GNUNET_free (pm->msg);
682 GNUNET_CONTAINER_DLL_remove (endpoint->sessions_head,
683 endpoint->sessions_tail,
685 if (session->timeout_task != GNUNET_SCHEDULER_NO_TASK)
687 GNUNET_SCHEDULER_cancel (session->timeout_task);
688 session->timeout_task = GNUNET_SCHEDULER_NO_TASK;
690 GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# Bluetooth sessions allocated"), -1,
692 GNUNET_HELLO_address_free (session->address);
693 GNUNET_free (session);
698 * A session is timing out. Clean up.
700 * @param cls pointer to the Session
704 session_timeout (void *cls,
705 const struct GNUNET_SCHEDULER_TaskContext *tc)
707 struct Session * session = cls;
708 struct GNUNET_TIME_Relative timeout;
710 session->timeout_task = GNUNET_SCHEDULER_NO_TASK;
711 timeout = GNUNET_TIME_absolute_get_remaining (session->timeout);
712 if (0 == timeout.rel_value_us)
714 free_session (session);
717 session->timeout_task =
718 GNUNET_SCHEDULER_add_delayed (timeout, &session_timeout, session);
723 * Create a new session
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
729 static struct Session *
730 create_session (struct MacEndpoint *endpoint,
731 const struct GNUNET_PeerIdentity *peer)
733 struct Session *session;
735 for (session = endpoint->sessions_head; NULL != session; session = session->next)
736 if (0 == memcmp (peer, &session->target,
737 sizeof (struct GNUNET_PeerIdentity)))
739 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
742 GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# Bluetooth sessions allocated"), 1,
744 session = GNUNET_new (struct Session);
745 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
746 endpoint->sessions_tail,
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",
758 mac_to_string (&endpoint->addr.mac));
764 * Lookup a new session
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
770 static struct Session *
771 lookup_session (struct MacEndpoint *endpoint,
772 const struct GNUNET_PeerIdentity *peer)
774 struct Session *session;
776 for (session = endpoint->sessions_head; NULL != session; session = session->next)
777 if (0 == memcmp (peer, &session->target, sizeof (struct GNUNET_PeerIdentity)))
779 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
787 * Function called once we have successfully given the fragment
788 * message to the SUID helper process and we are thus ready for
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)
796 fragment_transmission_done (void *cls,
799 struct FragmentMessage *fm = cls;
802 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
807 * Transmit a fragment of a message.
809 * @param cls `struct FragmentMessage *` this fragment message belongs to
810 * @param hdr pointer to the start of the fragment message
813 transmit_fragment (void *cls,
814 const struct GNUNET_MessageHeader *hdr)
816 struct FragmentMessage *fm = cls;
817 struct MacEndpoint *endpoint = fm->macendpoint;
821 msize = ntohs (hdr->size);
822 size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + msize;
825 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radio_header;
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,
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,
838 &fragment_transmission_done, fm);
839 fm->size_on_wire += size;
841 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
842 _("# Bluetooth message fragments sent"),
845 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
846 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
847 "# bytes currently in Bluetooth buffers",
849 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
850 "# bytes transmitted via Bluetooth",
857 * Frees the space of a message in the fragment queue (send queue)
859 * @param fm message to free
862 free_fragment_message (struct FragmentMessage *fm)
864 struct MacEndpoint *endpoint = fm->macendpoint;
866 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
867 _("# Bluetooth messages pending (with fragmentation)"),
869 GNUNET_CONTAINER_DLL_remove (endpoint->sending_messages_head,
870 endpoint->sending_messages_tail, fm);
873 GNUNET_HELPER_send_cancel (fm->sh);
876 GNUNET_FRAGMENT_context_destroy (fm->fragcontext,
877 &endpoint->msg_delay,
878 &endpoint->ack_delay);
879 if (fm->timeout_task != GNUNET_SCHEDULER_NO_TASK)
881 GNUNET_SCHEDULER_cancel (fm->timeout_task);
882 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
889 * A FragmentMessage has timed out. Remove it.
891 * @param cls pointer to the 'struct FragmentMessage'
892 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
895 fragmentmessage_timeout (void *cls,
896 const struct GNUNET_SCHEDULER_TaskContext *tc)
898 struct FragmentMessage *fm = cls;
900 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
901 if (NULL != fm->cont)
903 fm->cont (fm->cont_cls,
910 free_fragment_message (fm);
915 * Transmit a message to the given destination with fragmentation.
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
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,
934 GNUNET_TRANSPORT_TransmitContinuation cont,
938 struct FragmentMessage *fm;
939 struct Plugin *plugin;
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);
949 fm->cont_cls = cont_cls;
950 /* 1 MBit/s typical data rate, 1430 byte fragments => ~100 ms per message */
952 GNUNET_FRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
957 &transmit_fragment, fm);
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,
968 * Free a MAC endpoint.
970 * @param endpoint pointer to the MacEndpoint to free
973 free_macendpoint (struct MacEndpoint *endpoint)
975 struct Plugin *plugin = endpoint->plugin;
976 struct FragmentMessage *fm;
977 struct Session *session;
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,
989 if (NULL != endpoint->defrag)
991 GNUNET_DEFRAGMENT_context_destroy(endpoint->defrag);
992 endpoint->defrag = NULL;
996 if (GNUNET_SCHEDULER_NO_TASK != endpoint->timeout_task)
998 GNUNET_SCHEDULER_cancel (endpoint->timeout_task);
999 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1001 GNUNET_free (endpoint);
1006 * A MAC endpoint is timing out. Clean up.
1008 * @param cls pointer to the MacEndpoint
1009 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
1012 macendpoint_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1014 struct MacEndpoint *endpoint = cls;
1015 struct GNUNET_TIME_Relative timeout;
1017 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1018 timeout = GNUNET_TIME_absolute_get_remaining (endpoint->timeout);
1019 if (0 == timeout.rel_value_us)
1021 free_macendpoint (endpoint);
1024 endpoint->timeout_task =
1025 GNUNET_SCHEDULER_add_delayed (timeout, &macendpoint_timeout,
1031 * Find (or create) a MacEndpoint with a specific MAC address
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
1037 static struct MacEndpoint *
1038 create_macendpoint (struct Plugin *plugin,
1039 struct WlanAddress *addr)
1041 struct MacEndpoint *pos;
1043 for (pos = plugin->mac_head; NULL != pos; pos = pos->next)
1044 if (0 == memcmp (addr, &pos->addr, sizeof (struct WlanAddress)))
1046 pos = GNUNET_new (struct MacEndpoint);
1047 pos->addr = (*addr);
1048 pos->plugin = plugin;
1050 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
1051 MESSAGES_IN_DEFRAG_QUEUE_PER_MAC,
1053 &bluetooth_data_message_handler,
1056 pos->msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1057 pos->ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1059 pos->timeout = GNUNET_TIME_relative_to_absolute (MACENDPOINT_TIMEOUT);
1061 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
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"),
1068 LOG (GNUNET_ERROR_TYPE_DEBUG,
1069 "New MAC endpoint `%s'\n",
1070 bluetooth_plugin_address_to_string(NULL,
1072 sizeof (struct WlanAddress)));
1078 * Function obtain the network type for a session
1080 * @param cls closure ('struct Plugin*')
1081 * @param session the session
1082 * @return the network type in HBO or GNUNET_SYSERR
1084 static enum GNUNET_ATS_Network_Type
1085 bluetooth_get_network (void *cls,
1086 struct Session *session)
1088 GNUNET_assert (NULL != session);
1089 return GNUNET_ATS_NET_BT;
1094 * Look up a session for a peer and create a new session if none is found
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
1100 static struct Session *
1101 get_session (struct MacEndpoint *endpoint,
1102 const struct GNUNET_PeerIdentity *peer)
1104 struct Session *session;
1105 if (NULL != (session = lookup_session (endpoint, peer)))
1107 return create_session (endpoint, peer);
1112 * Creates a new outbound session the transport service will use to send data to the
1115 * @param cls the plugin
1116 * @param address the address
1117 * @return the session or NULL of max connections exceeded
1119 static struct Session *
1120 bluetooth_plugin_get_session (void *cls,
1121 const struct GNUNET_HELLO_Address *address)
1123 struct Plugin *plugin = cls;
1124 struct MacEndpoint *endpoint;
1126 if (NULL == address)
1128 if (sizeof (struct WlanAddress) != address->address_length)
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,
1138 address->address_length));
1139 endpoint = create_macendpoint (plugin,
1140 (struct WlanAddress *) address->address);
1141 return get_session (endpoint, &address->peer);
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).
1150 * @param cls closure
1151 * @param target peer from which to disconnect
1154 bluetooth_plugin_disconnect_peer (void *cls, const struct GNUNET_PeerIdentity *target)
1156 struct Plugin *plugin = cls;
1157 struct Session *session;
1158 struct MacEndpoint *endpoint;
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)))
1165 free_session (session);
1166 break; /* inner-loop only (in case peer has another MAC as well!) */
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).
1176 * @param cls closure
1177 * @param session session to disconnect
1180 bluetooth_plugin_disconnect_session (void *cls,
1181 struct Session *session)
1183 free_session (session);
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.
1193 * @param cls closure with the `struct Plugin`
1194 * @return keepalive factor
1197 bluetooth_query_keepalive_factor (void *cls)
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.
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
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)
1231 bluetooth_plugin_send (void *cls,
1232 struct Session *session,
1235 unsigned int priority,
1236 struct GNUNET_TIME_Relative to,
1237 GNUNET_TRANSPORT_TransmitContinuation cont,
1240 struct Plugin *plugin = cls;
1241 struct WlanHeader *wlanheader;
1242 size_t size = msgbuf_size + sizeof (struct WlanHeader);
1243 char buf[size] GNUNET_ALIGN;
1245 LOG (GNUNET_ERROR_TYPE_DEBUG,
1246 "Transmitting %u bytes of payload to peer `%s' (starting with %u byte message of type %u)\n",
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);
1259 GNUNET_STATISTICS_update (plugin->env->stats,
1260 "# bytes currently in Bluetooth buffers",
1261 msgbuf_size, GNUNET_NO);
1263 send_with_fragmentation (session->mac,
1266 &wlanheader->header,
1274 * We have received data from the WLAN via some session. Process depending
1275 * on the message type (HELLO, DATA, FRAGMENTATION or FRAGMENTATION-ACK).
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
1282 process_data (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
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;
1295 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1296 ats.value = htonl (GNUNET_ATS_NET_BT);
1297 msize = ntohs (hdr->size);
1299 GNUNET_STATISTICS_update (plugin->env->stats,
1300 "# bytes received via Bluetooth",
1303 switch (ntohs (hdr->type))
1305 case GNUNET_MESSAGE_TYPE_HELLO:
1307 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hdr, &tmpsource))
1309 GNUNET_break_op (0);
1312 if (NULL == mas->endpoint)
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)));
1326 GNUNET_STATISTICS_update (plugin->env->stats,
1327 _("# HELLO messages received via Bluetooth"), 1,
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);
1339 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1340 if (NULL == mas->endpoint)
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"),
1355 (void) GNUNET_DEFRAGMENT_process_fragment (mas->endpoint->defrag,
1358 case GNUNET_MESSAGE_TYPE_FRAGMENT_ACK:
1359 if (NULL == mas->endpoint)
1364 GNUNET_STATISTICS_update (plugin->env->stats, _("# ACKs received via Bluetooth"),
1366 for (fm = mas->endpoint->sending_messages_head; NULL != fm; fm = fm->next)
1368 ret = GNUNET_FRAGMENT_process_ack (fm->fragcontext, hdr);
1369 if (GNUNET_OK == ret)
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)),
1377 mas->endpoint->timeout = GNUNET_TIME_relative_to_absolute (MACENDPOINT_TIMEOUT);
1378 if (NULL != fm->cont)
1380 fm->cont (fm->cont_cls, &fm->target, GNUNET_OK, fm->size_payload, fm->size_on_wire);
1383 free_fragment_message (fm);
1386 if (GNUNET_NO == ret)
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)));
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)));
1402 case GNUNET_MESSAGE_TYPE_WLAN_DATA:
1403 if (NULL == mas->endpoint)
1408 if (msize < sizeof (struct WlanHeader))
1413 wlanheader = (const struct WlanHeader *) hdr;
1414 if (0 != memcmp (&wlanheader->target,
1415 plugin->env->my_identity,
1416 sizeof (struct GNUNET_PeerIdentity)))
1418 LOG (GNUNET_ERROR_TYPE_DEBUG,
1419 "Bluetooth data for `%s', not for me, ignoring\n",
1420 GNUNET_i2s (&wlanheader->target));
1423 if (ntohl (wlanheader->crc) !=
1424 GNUNET_CRYPTO_crc32_n (&wlanheader[1], msize - sizeof (struct WlanHeader)))
1426 GNUNET_STATISTICS_update (plugin->env->stats,
1427 _("# Bluetooth DATA messages discarded due to CRC32 error"), 1,
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,
1439 (const char *) &wlanheader[1],
1440 msize - sizeof (struct WlanHeader),
1441 GNUNET_YES, GNUNET_NO);
1444 if (NULL == mas->endpoint)
1449 if (NULL == mas->session)
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,
1463 plugin->env->update_address_metrics (plugin->env->cls,
1464 mas->session->address,
1474 * Function used for to process the data from the suid process
1476 * @param cls the plugin handle
1477 * @param client client that send the data (not used)
1478 * @param hdr header of the GNUNET_MessageHeader
1481 handle_helper_message (void *cls, void *client,
1482 const struct GNUNET_MessageHeader *hdr)
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;
1492 msize = ntohs (hdr->size);
1493 switch (ntohs (hdr->type))
1495 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
1496 if (msize != sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage))
1501 cm = (const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *) hdr;
1502 if (GNUNET_YES == plugin->have_mac)
1504 if (0 == memcmp (&plugin->mac_address,
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);
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);
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);
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",
1537 GNUNET_STATISTICS_update (plugin->env->stats,
1538 _("# DATA messages received via Bluetooth"), 1,
1540 if (msize < sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage))
1543 LOG (GNUNET_ERROR_TYPE_DEBUG,
1544 "Size of packet is too small (%u bytes)\n",
1548 rxinfo = (const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) hdr;
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)))
1554 /* Not the GNUnet BSSID */
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))) )
1562 /* Neither broadcast nor specifically for us */
1565 if (0 == memcmp (&rxinfo->frame.addr2, &plugin->mac_address,
1566 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1568 /* packet is FROM us, thus not FOR us */
1572 GNUNET_STATISTICS_update (plugin->env->stats,
1573 _("# Bluetooth DATA messages processed"),
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);
1583 (void) GNUNET_SERVER_mst_receive (plugin->helper_payload_tokenizer,
1585 (const char*) &rxinfo[1],
1586 msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage),
1587 GNUNET_YES, GNUNET_NO);
1591 LOG (GNUNET_ERROR_TYPE_DEBUG,
1592 "Unexpected message of type %u (%u bytes)",
1593 ntohs (hdr->type), ntohs (hdr->size));
1602 * Task to (periodically) send a HELLO beacon
1604 * @param cls pointer to the plugin struct
1605 * @param tc scheduler context
1608 send_hello_beacon (void *cls,
1609 const struct GNUNET_SCHEDULER_TaskContext *tc)
1611 struct Plugin *plugin = cls;
1613 uint16_t hello_size;
1614 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
1615 const struct GNUNET_MessageHeader *hello;
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;
1622 char buf[size] GNUNET_ALIGN;
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);
1632 GNUNET_HELPER_send (plugin->suid_helper,
1633 &radioHeader->header,
1634 GNUNET_YES /* can drop */,
1636 GNUNET_STATISTICS_update (plugin->env->stats,
1637 _("# HELLO beacons sent via Bluetooth"),
1640 plugin->beacon_task =
1641 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1642 (HELLO_BEACON_SCALING_FACTOR,
1643 plugin->mac_count + 1),
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
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
1663 bluetooth_plugin_address_suggested (void *cls,
1667 struct Plugin *plugin = cls;
1668 struct WlanAddress *wa = (struct WlanAddress *) addr;
1670 if (addrlen != sizeof (struct WlanAddress))
1672 GNUNET_break_op (0);
1673 return GNUNET_SYSERR;
1675 if (GNUNET_YES != plugin->have_mac)
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 */
1682 if (0 != memcmp (&wa->mac,
1683 &plugin->mac_address,
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 */
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.
1701 * @param cls closure
1702 * @param addr binary address
1703 * @param addrlen length of the @a addr
1704 * @return string representing the same address
1707 bluetooth_plugin_address_to_string (void *cls,
1711 const struct GNUNET_TRANSPORT_WLAN_MacAddress *mac;
1712 static char macstr[36];
1714 if (sizeof (struct WlanAddress) != addrlen)
1719 mac = &((struct WlanAddress *) addr)->mac;
1720 GNUNET_snprintf (macstr,
1724 ntohl (((struct WlanAddress *) addr)->options),
1725 mac_to_string (mac));
1731 * Convert the transports address to a nice, human-readable format.
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
1744 bluetooth_plugin_address_pretty_printer (void *cls,
1749 struct GNUNET_TIME_Relative timeout,
1750 GNUNET_TRANSPORT_AddressStringCallback asc,
1755 if (sizeof (struct WlanAddress) == addrlen)
1756 ret = bluetooth_plugin_address_to_string(NULL, addr, addrlen);
1761 (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
1762 asc (asc_cls, NULL, GNUNET_OK);
1767 * Exit point from the plugin.
1769 * @param cls pointer to the api struct
1772 libgnunet_plugin_transport_bluetooth_done (void *cls)
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;
1787 if (GNUNET_YES == plugin->have_mac)
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);
1796 plugin->env->notify_address (plugin->env->cls, GNUNET_NO, address);
1797 plugin->have_mac = GNUNET_NO;
1799 GNUNET_HELLO_address_free (address);
1802 if (GNUNET_SCHEDULER_NO_TASK != plugin->beacon_task)
1804 GNUNET_SCHEDULER_cancel (plugin->beacon_task);
1805 plugin->beacon_task = GNUNET_SCHEDULER_NO_TASK;
1807 if (NULL != plugin->suid_helper)
1809 GNUNET_HELPER_stop (plugin->suid_helper, GNUNET_NO);
1810 plugin->suid_helper = NULL;
1812 endpoint_next = plugin->mac_head;
1813 while (NULL != (endpoint = endpoint_next))
1815 endpoint_next = endpoint->next;
1816 free_macendpoint (endpoint);
1818 if (NULL != plugin->fragment_data_tokenizer)
1820 GNUNET_SERVER_mst_destroy (plugin->fragment_data_tokenizer);
1821 plugin->fragment_data_tokenizer = NULL;
1823 if (NULL != plugin->wlan_header_payload_tokenizer)
1825 GNUNET_SERVER_mst_destroy (plugin->wlan_header_payload_tokenizer);
1826 plugin->wlan_header_payload_tokenizer = NULL;
1828 if (NULL != plugin->helper_payload_tokenizer)
1830 GNUNET_SERVER_mst_destroy (plugin->helper_payload_tokenizer);
1831 plugin->helper_payload_tokenizer = NULL;
1833 GNUNET_free_non_null (plugin->interface);
1834 GNUNET_free (plugin);
1841 * Function called to convert a string address to
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
1853 bluetooth_string_to_address (void *cls,
1859 struct WlanAddress *wa;
1865 if ((NULL == addr) || (addrlen == 0))
1868 return GNUNET_SYSERR;
1870 if ('\0' != addr[addrlen - 1])
1873 return GNUNET_SYSERR;
1875 if (strlen (addr) != addrlen - 1)
1878 return GNUNET_SYSERR;
1881 if (8 != SSCANF (addr,
1882 "%9s.%u.%X:%X:%X:%X:%X:%X",
1884 &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]))
1887 return GNUNET_SYSERR;
1889 wa = GNUNET_new (struct WlanAddress);
1891 wa->mac.mac[i] = a[i];
1892 wa->options = htonl (0);
1894 *added = sizeof (struct WlanAddress);
1900 bluetooth_plugin_update_session_timeout (void *cls,
1901 const struct GNUNET_PeerIdentity *peer,
1902 struct Session *session)
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,
1914 * Entry point for the plugin.
1916 * @param cls closure, the `struct GNUNET_TRANSPORT_PluginEnvironment *`
1917 * @return the `struct GNUNET_TRANSPORT_PluginFunctions *` or NULL on error
1920 libgnunet_plugin_transport_bluetooth_init (void *cls)
1922 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1923 struct GNUNET_TRANSPORT_PluginFunctions *api;
1924 struct Plugin *plugin;
1926 unsigned long long testmode;
1929 /* check for 'special' mode */
1930 if (NULL == env->receive)
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);
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;
1943 /* check configuration */
1945 GNUNET_CONFIGURATION_have_value (env->cfg, "transport-bluetooth", "TESTMODE")) &&
1947 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-bluetooth",
1948 "TESTMODE", &testmode)) ||
1951 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1952 "transport-bluetooth", "TESTMODE");
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)) )
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);
1965 GNUNET_free (binary);
1967 GNUNET_CONFIGURATION_get_value_string
1968 (env->cfg, "transport-bluetooth", "INTERFACE",
1971 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1972 "transport-bluetooth", "INTERFACE");
1976 plugin = GNUNET_new (struct Plugin);
1977 plugin->interface = interface;
1979 GNUNET_STATISTICS_set (plugin->env->stats, _("# Bluetooth sessions allocated"),
1981 GNUNET_STATISTICS_set (plugin->env->stats, _("# Bluetooth MAC endpoints allocated"),
1983 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker, NULL, NULL,
1984 GNUNET_BANDWIDTH_value_init (100 * 1024 *
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,
1992 plugin->options = 0;
1994 /* some compilers do not like switch on 'long long'... */
1995 switch ((unsigned int) testmode)
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,
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,
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,
2034 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
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;
2052 /* end of plugin_transport_bluetooth.c */