2 This file is part of GNUnet
3 Copyright (C) 2010-2014 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file transport/plugin_transport_wlan.c
23 * @brief transport plugin for wlan and/or bluetooth
24 * @author David Brodski
25 * @author Christian Grothoff
27 * BUILD_WLAN or BUILD_BLUETOOTH must be defined such that the respective
28 * variant of this code is compiled.
31 #include "gnunet_util_lib.h"
32 #include "gnunet_hello_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_statistics_service.h"
35 #include "gnunet_transport_service.h"
36 #include "gnunet_transport_plugin.h"
37 #include "plugin_transport_wlan.h"
38 #include "gnunet_fragmentation_lib.h"
39 #include "gnunet_constants.h"
44 #define PLUGIN_NAME "wlan"
45 #define CONFIG_NAME "transport-wlan"
46 #define HELPER_NAME "gnunet-helper-transport-wlan"
47 #define DUMMY_HELPER_NAME "gnunet-helper-transport-wlan-dummy"
48 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_wlan_init
49 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_wlan_done
50 #define LOG(kind, ...) GNUNET_log_from (kind, "transport-wlan", __VA_ARGS__)
54 * time out of a mac endpoint
56 #define MACENDPOINT_TIMEOUT GNUNET_TIME_relative_multiply ( \
57 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 2)
60 * We reduce the frequence of HELLO beacons in relation to
61 * the number of MAC addresses currently visible to us.
62 * This is the multiplication factor.
64 #define HELLO_BEACON_SCALING_FACTOR GNUNET_TIME_relative_multiply ( \
65 GNUNET_TIME_UNIT_SECONDS, 2)
70 /* begin case bluetooth */
72 #define PLUGIN_NAME "bluetooth"
73 #define CONFIG_NAME "transport-bluetooth"
74 #define HELPER_NAME "gnunet-helper-transport-bluetooth"
75 /* yes, this is correct, we use the same dummy driver as 'wlan' */
76 #define DUMMY_HELPER_NAME "gnunet-helper-transport-wlan-dummy"
77 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT \
78 libgnunet_plugin_transport_bluetooth_init
79 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE \
80 libgnunet_plugin_transport_bluetooth_done
81 #define LOG(kind, ...) GNUNET_log_from (kind, "transport-bluetooth", \
85 * time out of a mac endpoint
87 #define MACENDPOINT_TIMEOUT GNUNET_TIME_relative_multiply ( \
88 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 60)
92 * We reduce the frequence of HELLO beacons in relation to
93 * the number of MAC addresses currently visible to us.
94 * This is the multiplication factor.
96 #define HELLO_BEACON_SCALING_FACTOR GNUNET_TIME_relative_multiply ( \
97 GNUNET_TIME_UNIT_SECONDS, 60)
99 /* end case bluetooth */
101 #error need to build wlan or bluetooth
106 * Functions with this signature are called whenever a
107 * complete message is received by the tokenizer.
109 * Do not call #GNUNET_SERVER_mst_destroy from within
110 * the scope of this callback.
113 * @param client identification of the client
114 * @param message the actual message
115 * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
118 (*GNUNET_SERVER_MessageTokenizerCallback) (void *cls,
121 GNUNET_MessageHeader *message);
124 /* Include legacy message stream tokenizer that was removed from util (for now) */
125 #include "tcp_server_mst_legacy.c"
129 * Max size of packet (that we give to the WLAN driver for transmission)
131 #define WLAN_MTU 1430
135 * Which network scope do we belong to?
138 static const enum GNUNET_NetworkType scope = GNUNET_NT_WLAN;
140 static const enum GNUNET_NetworkType scope = GNUNET_NT_BT;
145 * Maximum number of messages in defragmentation queue per MAC
147 #define MESSAGES_IN_DEFRAG_QUEUE_PER_MAC 2
150 * Link layer control fields for better compatibility
151 * (i.e. GNUnet over WLAN is not IP-over-WLAN).
153 #define WLAN_LLC_DSAP_FIELD 0x1f
154 #define WLAN_LLC_SSAP_FIELD 0x1f
157 GNUNET_NETWORK_STRUCT_BEGIN
159 * Header for messages which need fragmentation. This is the format of
160 * a message we obtain AFTER defragmentation. We then need to check
161 * the CRC and then tokenize the payload and pass it to the
162 * 'receive' callback.
167 * Message type is #GNUNET_MESSAGE_TYPE_WLAN_DATA.
169 struct GNUNET_MessageHeader header;
172 * CRC32 checksum (only over the payload), in NBO.
174 uint32_t crc GNUNET_PACKED;
177 * Sender of the message.
179 struct GNUNET_PeerIdentity sender;
182 * Target of the message.
184 struct GNUNET_PeerIdentity target;
186 /* followed by payload, possibly including
187 multiple messages! */
192 * Address format for WLAN.
197 * Options set for the WLAN, in NBO.
199 uint32_t options GNUNET_PACKED;
202 * WLAN addresses using MACs.
204 struct GNUNET_TRANSPORT_WLAN_MacAddress mac;
208 GNUNET_NETWORK_STRUCT_END
212 * Information kept for each message that is yet to be fragmented and
215 struct PendingMessage
218 * next entry in the DLL
220 struct PendingMessage *next;
223 * previous entry in the DLL
225 struct PendingMessage *prev;
228 * The pending message
230 struct WlanHeader *msg;
233 * Continuation function to call once the message
234 * has been sent. Can be NULL if there is no
235 * continuation to call.
237 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
240 * Cls for @e transmit_cont
242 void *transmit_cont_cls;
245 * Timeout task (for this message).
247 struct GNUNET_SCHEDULER_Task *timeout_task;
252 * Session handle for connections with other peers.
254 struct GNUNET_ATS_Session
257 * To whom are we talking to (set to our identity
258 * if we are still waiting for the welcome message)
260 struct GNUNET_PeerIdentity target;
263 * We keep all sessions in a DLL at their respective
264 * `struct MACEndpoint *`.
266 struct GNUNET_ATS_Session *next;
269 * We keep all sessions in a DLL at their respective
270 * `struct MACEndpoint *`.
272 struct GNUNET_ATS_Session *prev;
275 * MAC endpoint with the address of this peer.
277 struct MacEndpoint *mac;
280 * Address associated with this session and MAC endpoint
282 struct GNUNET_HELLO_Address *address;
285 * When should this session time out?
287 struct GNUNET_TIME_Absolute timeout;
290 * Timeout task (for the session).
292 struct GNUNET_SCHEDULER_Task *timeout_task;
297 * Struct for messages that are being fragmented in a MAC's transmission queue.
299 struct FragmentMessage
302 * This is a doubly-linked list.
304 struct FragmentMessage *next;
307 * This is a doubly-linked list.
309 struct FragmentMessage *prev;
312 * MAC endpoint this message belongs to
314 struct MacEndpoint *macendpoint;
317 * Fragmentation context
319 struct GNUNET_FRAGMENT_Context *fragcontext;
322 * Transmission handle to helper (to cancel if the frag context
323 * is destroyed early for some reason).
325 struct GNUNET_HELPER_SendHandle *sh;
328 * Intended recipient.
330 struct GNUNET_PeerIdentity target;
333 * Timeout value for the message.
335 struct GNUNET_TIME_Absolute timeout;
340 struct GNUNET_SCHEDULER_Task *timeout_task;
343 * Continuation to call when we're done with this message.
345 GNUNET_TRANSPORT_TransmitContinuation cont;
348 * Message we need to fragment and transmit, NULL after the
349 * @e fragmentcontext has been created.
351 struct GNUNET_MessageHeader *msg;
354 * Closure for @e cont
359 * Size of original message
364 * Number of bytes used to transmit message
371 * Struct to represent one network card connection
376 * We keep all MACs in a DLL in the plugin.
378 struct MacEndpoint *next;
381 * We keep all MACs in a DLL in the plugin.
383 struct MacEndpoint *prev;
386 * Pointer to the global plugin struct.
388 struct Plugin *plugin;
391 * Head of sessions that use this MAC.
393 struct GNUNET_ATS_Session *sessions_head;
396 * Tail of sessions that use this MAC.
398 struct GNUNET_ATS_Session *sessions_tail;
401 * Head of messages we are currently sending to this MAC.
403 struct FragmentMessage *sending_messages_head;
406 * Tail of messages we are currently sending to this MAC.
408 struct FragmentMessage *sending_messages_tail;
411 * Defrag context for this MAC
413 struct GNUNET_DEFRAGMENT_Context *defrag;
416 * When should this endpoint time out?
418 struct GNUNET_TIME_Absolute timeout;
423 struct GNUNET_SCHEDULER_Task *timeout_task;
426 * count of messages in the fragment out queue for this mac endpoint
428 unsigned int fragment_messages_out_count;
433 struct WlanAddress wlan_addr;
436 * Message delay for fragmentation context
438 struct GNUNET_TIME_Relative msg_delay;
441 * ACK delay for fragmentation context
443 struct GNUNET_TIME_Relative ack_delay;
446 * Desired transmission power for this MAC
451 * Desired transmission rate for this MAC
456 * Antenna we should use for this MAC
463 * Encapsulation of all of the state of the plugin.
470 struct GNUNET_TRANSPORT_PluginEnvironment *env;
473 * Handle to helper process for priviledged operations.
475 struct GNUNET_HELPER_Handle *suid_helper;
478 * Function to call about session status changes.
480 GNUNET_TRANSPORT_SessionInfoCallback sic;
483 * Closure for @e sic.
488 * ARGV-vector for the helper (all helpers take only the binary
489 * name, one actual argument, plus the NULL terminator for 'argv').
491 char *helper_argv[3];
494 * The interface of the wlan card given to us by the user.
496 char *wlan_interface;
499 * Tokenizer for demultiplexing of data packets resulting from
502 struct GNUNET_SERVER_MessageStreamTokenizer *fragment_data_tokenizer;
505 * Tokenizer for demultiplexing of data packets received from the suid helper
507 struct GNUNET_SERVER_MessageStreamTokenizer *helper_payload_tokenizer;
510 * Tokenizer for demultiplexing of data packets that follow the WLAN Header
512 struct GNUNET_SERVER_MessageStreamTokenizer *wlan_header_payload_tokenizer;
515 * Head of list of open connections.
517 struct MacEndpoint *mac_head;
520 * Tail of list of open connections.
522 struct MacEndpoint *mac_tail;
525 * Task that periodically sends a HELLO beacon via the helper.
527 struct GNUNET_SCHEDULER_Task *beacon_task;
530 * Tracker for bandwidth limit
532 struct GNUNET_BANDWIDTH_Tracker tracker;
535 * The mac_address of the wlan card given to us by the helper.
537 struct GNUNET_TRANSPORT_WLAN_MacAddress mac_address;
540 * Have we received a control message with our MAC address yet?
545 * Number of connections
547 unsigned int mac_count;
550 * Options for addresses
557 * Information associated with a message. Can contain
558 * the session or the MAC endpoint associated with the
564 * NULL if the identity of the other peer is not known.
566 struct GNUNET_ATS_Session *session;
569 * MAC address of the other peer, NULL if not known.
571 struct MacEndpoint *endpoint;
576 * Print MAC addresses nicely.
578 * @param mac the mac address
579 * @return string to a static buffer with
580 * the human-readable mac, will be overwritten during the next call to
584 mac_to_string (const struct GNUNET_TRANSPORT_WLAN_MacAddress *mac)
586 static char macstr[20];
588 GNUNET_snprintf (macstr,
590 "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
591 mac->mac[0], mac->mac[1],
592 mac->mac[2], mac->mac[3],
593 mac->mac[4], mac->mac[5]);
599 * Function called for a quick conversion of the binary address to
600 * a numeric address. Note that the caller must not free the
601 * address and that the next call to this function is allowed
602 * to override the address again.
605 * @param addr binary address
606 * @param addrlen length of the address
607 * @return string representing the same address
610 wlan_plugin_address_to_string (void *cls,
614 const struct GNUNET_TRANSPORT_WLAN_MacAddress *mac;
615 static char macstr[36];
617 if (sizeof(struct WlanAddress) != addrlen)
622 mac = &((struct WlanAddress *) addr)->mac;
623 GNUNET_snprintf (macstr,
627 ntohl (((struct WlanAddress *) addr)->options),
628 mac_to_string (mac));
634 * If a session monitor is attached, notify it about the new
637 * @param plugin our plugin
638 * @param session session that changed state
639 * @param state new state of the session
642 notify_session_monitor (struct Plugin *plugin,
643 struct GNUNET_ATS_Session *session,
644 enum GNUNET_TRANSPORT_SessionState state)
646 struct GNUNET_TRANSPORT_SessionInfo info;
648 if (NULL == plugin->sic)
650 memset (&info, 0, sizeof(info));
652 info.is_inbound = GNUNET_SYSERR; /* hard to say */
653 info.num_msg_pending = 0; /* we queue per MAC, not per peer */
654 info.num_bytes_pending = 0; /* we queue per MAC, not per peer */
655 info.receive_delay = GNUNET_TIME_UNIT_ZERO_ABS; /* not supported by WLAN */
656 info.session_timeout = session->timeout;
657 info.address = session->address;
658 plugin->sic (plugin->sic_cls,
665 * Fill the radiotap header
667 * @param endpoint pointer to the endpoint, can be NULL
668 * @param header pointer to the radiotap header
669 * @param size total message size
672 get_radiotap_header (struct MacEndpoint *endpoint,
673 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *header,
676 header->header.type = ntohs (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER);
677 header->header.size = ntohs (size);
678 if (NULL != endpoint)
680 header->rate = endpoint->rate;
681 header->tx_power = endpoint->tx_power;
682 header->antenna = endpoint->antenna;
687 header->tx_power = 0;
694 * Generate the WLAN hardware header for one packet
696 * @param plugin the plugin handle
697 * @param header address to write the header to
698 * @param to_mac_addr address of the recipient
699 * @param size size of the whole packet, needed to calculate the time to send the packet
702 get_wlan_header (struct Plugin *plugin,
703 struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *header,
704 const struct GNUNET_TRANSPORT_WLAN_MacAddress *to_mac_addr,
707 const int rate = 11000000;
709 header->frame_control = htons (IEEE80211_FC0_TYPE_DATA);
710 header->addr1 = *to_mac_addr;
711 header->addr2 = plugin->mac_address;
712 header->addr3 = mac_bssid_gnunet;
713 header->duration = GNUNET_htole16 ((size * 1000000) / rate + 290);
714 header->sequence_control = 0; // FIXME?
715 header->llc[0] = WLAN_LLC_DSAP_FIELD;
716 header->llc[1] = WLAN_LLC_SSAP_FIELD;
717 header->llc[2] = 0; // FIXME?
718 header->llc[3] = 0; // FIXME?
723 * Send an ACK for a fragment we received.
725 * @param cls the `struct MacEndpoint *` the ACK must be sent to
726 * @param msg_id id of the message
727 * @param hdr pointer to the hdr where the ack is stored
732 const struct GNUNET_MessageHeader *hdr)
734 struct MacEndpoint *endpoint = cls;
735 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage*radio_header;
736 uint16_t msize = ntohs (hdr->size);
737 size_t size = sizeof(struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage)
741 if (NULL == endpoint)
746 if (size >= GNUNET_MAX_MESSAGE_SIZE)
751 LOG (GNUNET_ERROR_TYPE_DEBUG,
752 "Sending ACK to %s\n",
753 mac_to_string (&endpoint->wlan_addr.mac));
754 radio_header = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) buf;
755 get_radiotap_header (endpoint, radio_header, size);
756 get_wlan_header (endpoint->plugin,
757 &radio_header->frame,
758 &endpoint->wlan_addr.mac,
759 sizeof(endpoint->wlan_addr.mac));
760 GNUNET_memcpy (&radio_header[1], hdr, msize);
762 GNUNET_HELPER_send (endpoint->plugin->suid_helper,
763 &radio_header->header,
764 GNUNET_NO /* dropping ACKs is bad */,
766 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
773 * Handles the data after all fragments are put together
775 * @param cls macendpoint this messages belongs to
776 * @param hdr pointer to the data
779 wlan_data_message_handler (void *cls,
780 const struct GNUNET_MessageHeader *hdr)
782 struct MacEndpoint *endpoint = cls;
783 struct Plugin *plugin = endpoint->plugin;
784 struct MacAndSession mas;
786 GNUNET_STATISTICS_update (plugin->env->stats,
787 _ ("# Messages defragmented"),
791 mas.endpoint = endpoint;
792 (void) GNUNET_SERVER_mst_receive (plugin->fragment_data_tokenizer,
796 GNUNET_YES, GNUNET_NO);
803 * @param cls our `struct Plugin`.
804 * @param session the session free
807 wlan_plugin_disconnect_session (void *cls,
808 struct GNUNET_ATS_Session *session)
810 struct MacEndpoint *endpoint = session->mac;
811 struct Plugin *plugin = endpoint->plugin;
813 plugin->env->session_end (plugin->env->cls,
816 notify_session_monitor (plugin,
818 GNUNET_TRANSPORT_SS_DONE);
819 GNUNET_CONTAINER_DLL_remove (endpoint->sessions_head,
820 endpoint->sessions_tail,
822 if (session->timeout_task != NULL)
824 GNUNET_SCHEDULER_cancel (session->timeout_task);
825 session->timeout_task = NULL;
827 GNUNET_STATISTICS_update (plugin->env->stats,
828 _ ("# Sessions allocated"),
831 GNUNET_HELLO_address_free (session->address);
832 GNUNET_free (session);
838 * Function that is called to get the keepalive factor.
839 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
840 * calculate the interval between keepalive packets.
842 * @param cls closure with the `struct Plugin`
843 * @return keepalive factor
846 wlan_plugin_query_keepalive_factor (void *cls)
853 * A session is timing out. Clean up.
855 * @param cls pointer to the Session
858 session_timeout (void *cls)
860 struct GNUNET_ATS_Session *session = cls;
861 struct GNUNET_TIME_Relative left;
863 session->timeout_task = NULL;
864 left = GNUNET_TIME_absolute_get_remaining (session->timeout);
865 if (0 != left.rel_value_us)
867 session->timeout_task =
868 GNUNET_SCHEDULER_add_delayed (left,
873 wlan_plugin_disconnect_session (session->mac->plugin,
879 * Lookup a new session
881 * @param endpoint pointer to the mac endpoint of the peer
882 * @param peer peer identity to use for this session
883 * @return returns the session or NULL
885 static struct GNUNET_ATS_Session *
886 lookup_session (struct MacEndpoint *endpoint,
887 const struct GNUNET_PeerIdentity *peer)
889 struct GNUNET_ATS_Session *session;
891 for (session = endpoint->sessions_head; NULL != session; session =
893 if (0 == memcmp (peer, &session->target, sizeof(struct
894 GNUNET_PeerIdentity)))
901 * Create a new session
903 * @param endpoint pointer to the mac endpoint of the peer
904 * @param peer peer identity to use for this session
905 * @return returns the session or NULL
907 static struct GNUNET_ATS_Session *
908 create_session (struct MacEndpoint *endpoint,
909 const struct GNUNET_PeerIdentity *peer)
911 struct GNUNET_ATS_Session *session;
913 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
914 _ ("# Sessions allocated"),
917 session = GNUNET_new (struct GNUNET_ATS_Session);
918 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
919 endpoint->sessions_tail,
921 session->address = GNUNET_HELLO_address_allocate (peer,
923 &endpoint->wlan_addr,
924 sizeof(endpoint->wlan_addr),
925 GNUNET_HELLO_ADDRESS_INFO_NONE);
926 session->mac = endpoint;
927 session->target = *peer;
928 session->timeout = GNUNET_TIME_relative_to_absolute (
929 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
930 session->timeout_task =
931 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
934 notify_session_monitor (endpoint->plugin,
936 GNUNET_TRANSPORT_SS_INIT);
937 notify_session_monitor (endpoint->plugin,
939 GNUNET_TRANSPORT_SS_UP);
940 LOG (GNUNET_ERROR_TYPE_DEBUG,
941 "Created new session %p for peer `%s' with endpoint %s\n",
944 mac_to_string (&endpoint->wlan_addr.mac));
951 * Look up a session for a peer and create a new session if none is found
953 * @param endpoint pointer to the mac endpoint of the peer
954 * @param peer peer identity to use for this session
955 * @return returns the session
957 static struct GNUNET_ATS_Session *
958 get_session (struct MacEndpoint *endpoint,
959 const struct GNUNET_PeerIdentity *peer)
961 struct GNUNET_ATS_Session *session;
963 if (NULL != (session = lookup_session (endpoint, peer)))
965 return create_session (endpoint, peer);
970 * Function called once we have successfully given the fragment
971 * message to the SUID helper process and we are thus ready for
974 * @param cls the `struct FragmentMessage *`
975 * @param result result of the operation (#GNUNET_OK on success,
976 * #GNUNET_NO if the helper died, #GNUNET_SYSERR
977 * if the helper was stopped)
980 fragment_transmission_done (void *cls,
983 struct FragmentMessage *fm = cls;
986 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
991 * Transmit a fragment of a message.
993 * @param cls `struct FragmentMessage *` this fragment message belongs to
994 * @param hdr pointer to the start of the fragment message
997 transmit_fragment (void *cls,
998 const struct GNUNET_MessageHeader *hdr)
1000 struct FragmentMessage *fm = cls;
1001 struct MacEndpoint *endpoint = fm->macendpoint;
1005 if (NULL == endpoint)
1010 msize = ntohs (hdr->size);
1011 size = sizeof(struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + msize;
1014 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radio_header;
1016 radio_header = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) buf;
1017 get_radiotap_header (endpoint, radio_header, size);
1018 LOG (GNUNET_ERROR_TYPE_DEBUG,
1019 "Sending %u bytes of data to MAC `%s'\n",
1020 (unsigned int) msize,
1021 mac_to_string (&endpoint->wlan_addr.mac));
1023 get_wlan_header (endpoint->plugin,
1024 &radio_header->frame,
1025 &endpoint->wlan_addr.mac,
1026 sizeof(endpoint->wlan_addr.mac));
1027 GNUNET_memcpy (&radio_header[1], hdr, msize);
1028 GNUNET_assert (NULL == fm->sh);
1029 fm->sh = GNUNET_HELPER_send (endpoint->plugin->suid_helper,
1030 &radio_header->header,
1032 &fragment_transmission_done, fm);
1033 fm->size_on_wire += size;
1036 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
1037 _ ("# message fragments sent"),
1043 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
1045 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
1046 "# bytes currently in buffers",
1048 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
1049 "# bytes transmitted",
1056 * Frees the space of a message in the fragment queue (send queue)
1058 * @param fm message to free
1061 free_fragment_message (struct FragmentMessage *fm)
1063 struct MacEndpoint *endpoint = fm->macendpoint;
1065 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
1066 _ ("# messages pending (with fragmentation)"),
1068 GNUNET_CONTAINER_DLL_remove (endpoint->sending_messages_head,
1069 endpoint->sending_messages_tail,
1073 GNUNET_HELPER_send_cancel (fm->sh);
1076 if (NULL != fm->msg)
1078 GNUNET_free (fm->msg);
1081 if (NULL != fm->fragcontext)
1083 GNUNET_FRAGMENT_context_destroy (fm->fragcontext,
1084 &endpoint->msg_delay,
1085 &endpoint->ack_delay);
1086 fm->fragcontext = NULL;
1088 if (NULL != fm->timeout_task)
1090 GNUNET_SCHEDULER_cancel (fm->timeout_task);
1091 fm->timeout_task = NULL;
1098 * A FragmentMessage has timed out. Remove it.
1100 * @param cls pointer to the 'struct FragmentMessage'
1103 fragmentmessage_timeout (void *cls)
1105 struct FragmentMessage *fm = cls;
1107 fm->timeout_task = NULL;
1108 if (NULL != fm->cont)
1110 fm->cont (fm->cont_cls,
1117 free_fragment_message (fm);
1122 * Transmit a message to the given destination with fragmentation.
1124 * @param endpoint desired destination
1125 * @param timeout how long can the message wait?
1126 * @param target peer that should receive the message
1127 * @param msg message to transmit
1128 * @param payload_size bytes of payload
1129 * @param cont continuation to call once the message has
1130 * been transmitted (or if the transport is ready
1131 * for the next transmission call; or if the
1132 * peer disconnected...); can be NULL
1133 * @param cont_cls closure for @a cont
1136 send_with_fragmentation (struct MacEndpoint *endpoint,
1137 struct GNUNET_TIME_Relative timeout,
1138 const struct GNUNET_PeerIdentity *target,
1139 const struct GNUNET_MessageHeader *msg,
1140 size_t payload_size,
1141 GNUNET_TRANSPORT_TransmitContinuation cont,
1145 struct FragmentMessage *fm;
1146 struct Plugin *plugin;
1148 plugin = endpoint->plugin;
1149 fm = GNUNET_new (struct FragmentMessage);
1150 fm->macendpoint = endpoint;
1151 fm->target = *target;
1152 fm->size_payload = payload_size;
1153 fm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1155 fm->cont_cls = cont_cls;
1156 /* 1 MBit/s typical data rate, 1430 byte fragments => ~100 ms per message */
1158 GNUNET_SCHEDULER_add_delayed (timeout,
1159 &fragmentmessage_timeout,
1161 if (GNUNET_YES == plugin->have_mac)
1164 GNUNET_FRAGMENT_context_create (plugin->env->stats,
1167 fm->macendpoint->msg_delay,
1168 fm->macendpoint->ack_delay,
1170 &transmit_fragment, fm);
1174 fm->msg = GNUNET_copy_message (msg);
1176 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sending_messages_head,
1177 endpoint->sending_messages_tail,
1183 * Free a MAC endpoint.
1185 * @param endpoint pointer to the MacEndpoint to free
1188 free_macendpoint (struct MacEndpoint *endpoint)
1190 struct Plugin *plugin = endpoint->plugin;
1191 struct FragmentMessage *fm;
1192 struct GNUNET_ATS_Session *session;
1194 GNUNET_STATISTICS_update (plugin->env->stats,
1195 _ ("# MAC endpoints allocated"),
1198 while (NULL != (session = endpoint->sessions_head))
1199 wlan_plugin_disconnect_session (plugin,
1201 while (NULL != (fm = endpoint->sending_messages_head))
1202 free_fragment_message (fm);
1203 GNUNET_CONTAINER_DLL_remove (plugin->mac_head,
1207 if (NULL != endpoint->defrag)
1209 GNUNET_DEFRAGMENT_context_destroy (endpoint->defrag);
1210 endpoint->defrag = NULL;
1213 plugin->mac_count--;
1214 if (NULL != endpoint->timeout_task)
1216 GNUNET_SCHEDULER_cancel (endpoint->timeout_task);
1217 endpoint->timeout_task = NULL;
1219 GNUNET_free (endpoint);
1224 * A MAC endpoint is timing out. Clean up.
1226 * @param cls pointer to the `struct MacEndpoint *`
1229 macendpoint_timeout (void *cls)
1231 struct MacEndpoint *endpoint = cls;
1232 struct GNUNET_TIME_Relative timeout;
1234 endpoint->timeout_task = NULL;
1235 timeout = GNUNET_TIME_absolute_get_remaining (endpoint->timeout);
1236 if (0 == timeout.rel_value_us)
1238 free_macendpoint (endpoint);
1241 endpoint->timeout_task =
1242 GNUNET_SCHEDULER_add_delayed (timeout,
1243 &macendpoint_timeout,
1249 * Find (or create) a MacEndpoint with a specific MAC address
1251 * @param plugin pointer to the plugin struct
1252 * @param mac the MAC address of the endpoint
1253 * @return handle to our data structure for this MAC
1255 static struct MacEndpoint *
1256 create_macendpoint (struct Plugin *plugin,
1257 struct WlanAddress *mac)
1259 struct MacEndpoint *pos;
1261 for (pos = plugin->mac_head; NULL != pos; pos = pos->next)
1262 if (0 == memcmp (mac, &pos->wlan_addr, sizeof(pos->wlan_addr)))
1264 pos = GNUNET_new (struct MacEndpoint);
1265 pos->wlan_addr = (*mac);
1266 pos->plugin = plugin;
1268 GNUNET_DEFRAGMENT_context_create (plugin->env->stats,
1270 MESSAGES_IN_DEFRAG_QUEUE_PER_MAC,
1272 &wlan_data_message_handler,
1275 pos->msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1276 pos->ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1278 pos->timeout = GNUNET_TIME_relative_to_absolute (MACENDPOINT_TIMEOUT);
1280 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
1282 GNUNET_CONTAINER_DLL_insert (plugin->mac_head,
1285 plugin->mac_count++;
1286 GNUNET_STATISTICS_update (plugin->env->stats,
1287 _ ("# MAC endpoints allocated"),
1289 LOG (GNUNET_ERROR_TYPE_DEBUG,
1290 "New MAC endpoint `%s'\n",
1291 wlan_plugin_address_to_string (NULL,
1293 sizeof(struct WlanAddress)));
1299 * Function obtain the network type for a session
1301 * @param cls closure (`struct Plugin*`)
1302 * @param session the session
1303 * @return the network type in HBO or #GNUNET_SYSERR
1305 static enum GNUNET_NetworkType
1306 wlan_plugin_get_network (void *cls,
1307 struct GNUNET_ATS_Session *session)
1310 return GNUNET_NT_WLAN;
1312 return GNUNET_NT_BT;
1318 * Function obtain the network type for an address.
1320 * @param cls closure (`struct Plugin *`)
1321 * @param address the address
1322 * @return the network type
1324 static enum GNUNET_NetworkType
1325 wlan_plugin_get_network_for_address (void *cls,
1326 const struct GNUNET_HELLO_Address *address)
1329 return GNUNET_NT_WLAN;
1331 return GNUNET_NT_BT;
1337 * Creates a new outbound session the transport service will use to
1338 * send data to the peer
1340 * @param cls the `struct Plugin *`
1341 * @param address the address
1342 * @return the session or NULL of max connections exceeded
1344 static struct GNUNET_ATS_Session *
1345 wlan_plugin_get_session (void *cls,
1346 const struct GNUNET_HELLO_Address *address)
1348 struct Plugin *plugin = cls;
1349 struct MacEndpoint *endpoint;
1351 if (NULL == address)
1353 if (sizeof(struct WlanAddress) != address->address_length)
1358 LOG (GNUNET_ERROR_TYPE_DEBUG,
1359 "Service asked to create session for peer `%s' with MAC `%s'\n",
1360 GNUNET_i2s (&address->peer),
1361 wlan_plugin_address_to_string (NULL,
1363 address->address_length));
1364 endpoint = create_macendpoint (plugin,
1365 (struct WlanAddress *) address->address);
1366 return get_session (endpoint, &address->peer);
1371 * Function that can be used to force the plugin to disconnect
1372 * from the given peer and cancel all previous transmissions
1373 * (and their continuation).
1375 * @param cls closure
1376 * @param target peer from which to disconnect
1379 wlan_plugin_disconnect_peer (void *cls,
1380 const struct GNUNET_PeerIdentity *target)
1382 struct Plugin *plugin = cls;
1383 struct GNUNET_ATS_Session *session;
1384 struct MacEndpoint *endpoint;
1386 for (endpoint = plugin->mac_head; NULL != endpoint; endpoint = endpoint->next)
1387 for (session = endpoint->sessions_head; NULL != session; session =
1389 if (0 == memcmp (target, &session->target,
1390 sizeof(struct GNUNET_PeerIdentity)))
1392 wlan_plugin_disconnect_session (plugin, session);
1393 break; /* inner-loop only (in case peer has another MAC as well!) */
1399 * Function that can be used by the transport service to transmit
1400 * a message using the plugin. Note that in the case of a
1401 * peer disconnecting, the continuation MUST be called
1402 * prior to the disconnect notification itself. This function
1403 * will be called with this peer's HELLO message to initiate
1404 * a fresh connection to another peer.
1406 * @param cls closure
1407 * @param session which session must be used
1408 * @param msgbuf the message to transmit
1409 * @param msgbuf_size number of bytes in @a msgbuf
1410 * @param priority how important is the message (most plugins will
1411 * ignore message priority and just FIFO)
1412 * @param to how long to wait at most for the transmission (does not
1413 * require plugins to discard the message after the timeout,
1414 * just advisory for the desired delay; most plugins will ignore
1416 * @param cont continuation to call once the message has
1417 * been transmitted (or if the transport is ready
1418 * for the next transmission call; or if the
1419 * peer disconnected...); can be NULL
1420 * @param cont_cls closure for @a cont
1421 * @return number of bytes used (on the physical network, with overheads);
1422 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1423 * and does NOT mean that the message was not transmitted (DV)
1426 wlan_plugin_send (void *cls,
1427 struct GNUNET_ATS_Session *session,
1428 const char *msgbuf, size_t msgbuf_size,
1429 unsigned int priority,
1430 struct GNUNET_TIME_Relative to,
1431 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1433 struct Plugin *plugin = cls;
1434 struct WlanHeader *wlanheader;
1435 size_t size = msgbuf_size + sizeof(struct WlanHeader);
1436 char buf[size] GNUNET_ALIGN;
1438 LOG (GNUNET_ERROR_TYPE_DEBUG,
1439 "Transmitting %u bytes of payload to peer `%s' (starting with %u byte message of type %u)\n",
1441 GNUNET_i2s (&session->target),
1442 (unsigned int) ntohs (((struct GNUNET_MessageHeader*) msgbuf)->size),
1443 (unsigned int) ntohs (((struct GNUNET_MessageHeader*) msgbuf)->type));
1444 wlanheader = (struct WlanHeader *) buf;
1445 wlanheader->header.size = htons (msgbuf_size + sizeof(struct WlanHeader));
1446 wlanheader->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA);
1447 wlanheader->sender = *plugin->env->my_identity;
1448 wlanheader->target = session->target;
1449 wlanheader->crc = htonl (GNUNET_CRYPTO_crc32_n (msgbuf, msgbuf_size));
1450 GNUNET_memcpy (&wlanheader[1],
1453 GNUNET_STATISTICS_update (plugin->env->stats,
1454 "# bytes currently in buffers",
1457 send_with_fragmentation (session->mac,
1460 &wlanheader->header,
1468 * We have received data from the WLAN via some session. Process depending
1469 * on the message type (HELLO, DATA, FRAGMENTATION or FRAGMENTATION-ACK).
1471 * @param cls pointer to the plugin
1472 * @param client pointer to the session this message belongs to
1473 * @param hdr start of the message
1476 process_data (void *cls,
1478 const struct GNUNET_MessageHeader *hdr)
1480 struct Plugin *plugin = cls;
1481 struct GNUNET_HELLO_Address *address;
1482 struct MacAndSession *mas = client;
1483 struct FragmentMessage *fm;
1484 struct GNUNET_PeerIdentity tmpsource;
1485 const struct WlanHeader *wlanheader;
1489 msize = ntohs (hdr->size);
1491 GNUNET_STATISTICS_update (plugin->env->stats,
1495 switch (ntohs (hdr->type))
1497 case GNUNET_MESSAGE_TYPE_HELLO:
1500 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hdr,
1503 GNUNET_break_op (0);
1506 if (NULL == mas->endpoint)
1512 LOG (GNUNET_ERROR_TYPE_DEBUG,
1513 "Processing %u bytes of HELLO from peer `%s' at MAC %s\n",
1514 (unsigned int) msize,
1515 GNUNET_i2s (&tmpsource),
1516 wlan_plugin_address_to_string (NULL,
1517 &mas->endpoint->wlan_addr,
1518 sizeof(mas->endpoint->wlan_addr)));
1520 GNUNET_STATISTICS_update (plugin->env->stats,
1521 _ ("# HELLO messages received"), 1,
1523 address = GNUNET_HELLO_address_allocate (&tmpsource,
1525 &mas->endpoint->wlan_addr,
1526 sizeof(mas->endpoint->wlan_addr),
1527 GNUNET_HELLO_ADDRESS_INFO_NONE);
1528 mas->session = lookup_session (mas->endpoint,
1530 if (NULL == mas->session)
1532 mas->session = create_session (mas->endpoint,
1534 plugin->env->session_start (plugin->env->cls,
1539 plugin->env->receive (plugin->env->cls,
1543 GNUNET_HELLO_address_free (address);
1546 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1547 if (NULL == mas->endpoint)
1552 LOG (GNUNET_ERROR_TYPE_DEBUG,
1553 "Processing %u bytes of FRAGMENT from MAC %s\n",
1554 (unsigned int) msize,
1555 wlan_plugin_address_to_string (NULL,
1556 &mas->endpoint->wlan_addr,
1557 sizeof(mas->endpoint->wlan_addr)));
1558 GNUNET_STATISTICS_update (plugin->env->stats,
1559 _ ("# fragments received"),
1562 (void) GNUNET_DEFRAGMENT_process_fragment (mas->endpoint->defrag,
1566 case GNUNET_MESSAGE_TYPE_FRAGMENT_ACK:
1567 if (NULL == mas->endpoint)
1572 GNUNET_STATISTICS_update (plugin->env->stats,
1573 _ ("# ACKs received"),
1575 for (fm = mas->endpoint->sending_messages_head; NULL != fm; fm = fm->next)
1577 ret = GNUNET_FRAGMENT_process_ack (fm->fragcontext, hdr);
1578 if (GNUNET_OK == ret)
1580 LOG (GNUNET_ERROR_TYPE_DEBUG,
1581 "Got last ACK, finished message transmission to `%s' (%p)\n",
1582 wlan_plugin_address_to_string (NULL,
1583 &mas->endpoint->wlan_addr,
1584 sizeof(mas->endpoint->wlan_addr)),
1586 mas->endpoint->timeout = GNUNET_TIME_relative_to_absolute (
1587 MACENDPOINT_TIMEOUT);
1588 if (NULL != fm->cont)
1590 fm->cont (fm->cont_cls,
1597 free_fragment_message (fm);
1600 if (GNUNET_NO == ret)
1602 LOG (GNUNET_ERROR_TYPE_DEBUG,
1603 "Got an ACK, message transmission to `%s' not yet finished\n",
1604 wlan_plugin_address_to_string (NULL,
1605 &mas->endpoint->wlan_addr,
1606 sizeof(mas->endpoint->wlan_addr)));
1611 LOG (GNUNET_ERROR_TYPE_DEBUG,
1612 "ACK not matched against any active fragmentation with MAC `%s'\n",
1613 wlan_plugin_address_to_string (NULL,
1614 &mas->endpoint->wlan_addr,
1615 sizeof(mas->endpoint->wlan_addr)));
1618 case GNUNET_MESSAGE_TYPE_WLAN_DATA:
1619 if (NULL == mas->endpoint)
1624 if (msize < sizeof(struct WlanHeader))
1629 wlanheader = (const struct WlanHeader *) hdr;
1630 if (0 != memcmp (&wlanheader->target,
1631 plugin->env->my_identity,
1632 sizeof(struct GNUNET_PeerIdentity)))
1634 LOG (GNUNET_ERROR_TYPE_DEBUG,
1635 "Data for `%s', not for me, ignoring\n",
1636 GNUNET_i2s (&wlanheader->target));
1639 if (ntohl (wlanheader->crc) !=
1640 GNUNET_CRYPTO_crc32_n (&wlanheader[1],
1641 msize - sizeof(struct WlanHeader)))
1643 GNUNET_STATISTICS_update (plugin->env->stats,
1645 "# DATA messages discarded due to CRC32 error"),
1650 mas->session = lookup_session (mas->endpoint,
1651 &wlanheader->sender);
1652 if (NULL == mas->session)
1654 mas->session = create_session (mas->endpoint,
1655 &wlanheader->sender);
1656 address = GNUNET_HELLO_address_allocate (&wlanheader->sender,
1658 &mas->endpoint->wlan_addr,
1659 sizeof(struct WlanAddress),
1660 GNUNET_HELLO_ADDRESS_INFO_NONE);
1661 plugin->env->session_start (plugin->env->cls,
1665 LOG (GNUNET_ERROR_TYPE_DEBUG,
1666 "Notifying transport about peer `%s''s new session %p \n",
1667 GNUNET_i2s (&wlanheader->sender),
1669 GNUNET_HELLO_address_free (address);
1671 LOG (GNUNET_ERROR_TYPE_DEBUG,
1672 "Processing %u bytes of DATA from peer `%s'\n",
1673 (unsigned int) msize,
1674 GNUNET_i2s (&wlanheader->sender));
1675 mas->session->timeout = GNUNET_TIME_relative_to_absolute (
1676 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1677 (void) GNUNET_SERVER_mst_receive (plugin->wlan_header_payload_tokenizer,
1679 (const char *) &wlanheader[1],
1680 msize - sizeof(struct WlanHeader),
1686 if (NULL == mas->endpoint)
1691 if (NULL == mas->session)
1696 LOG (GNUNET_ERROR_TYPE_DEBUG,
1697 "Received packet with %u bytes of type %u from peer %s\n",
1698 (unsigned int) msize,
1699 (unsigned int) ntohs (hdr->type),
1700 GNUNET_i2s (&mas->session->target));
1701 plugin->env->receive (plugin->env->cls,
1702 mas->session->address,
1712 * Task to (periodically) send a HELLO beacon
1714 * @param cls pointer to the plugin struct
1717 send_hello_beacon (void *cls)
1719 struct Plugin *plugin = cls;
1721 uint16_t hello_size;
1722 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
1723 const struct GNUNET_MessageHeader *hello;
1725 hello = plugin->env->get_our_hello ();
1728 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1729 GNUNET_assert (sizeof(struct WlanHeader) + hello_size <= WLAN_MTU);
1730 size = sizeof(struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage)
1733 char buf[size] GNUNET_ALIGN;
1735 LOG (GNUNET_ERROR_TYPE_DEBUG,
1736 "Sending %u byte HELLO beacon\n",
1737 (unsigned int) size);
1738 radioHeader = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage*) buf;
1739 get_radiotap_header (NULL, radioHeader, size);
1740 LOG (GNUNET_ERROR_TYPE_DEBUG,
1741 "Broadcasting %u bytes of data to MAC `%s'\n",
1742 (unsigned int) size,
1743 mac_to_string (&bc_all_mac));
1744 get_wlan_header (plugin, &radioHeader->frame, &bc_all_mac, size);
1745 GNUNET_memcpy (&radioHeader[1], hello, hello_size);
1747 GNUNET_HELPER_send (plugin->suid_helper,
1748 &radioHeader->header,
1749 GNUNET_YES /* can drop */,
1751 GNUNET_STATISTICS_update (plugin->env->stats,
1752 _ ("# HELLO beacons sent"),
1756 plugin->beacon_task =
1757 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1758 (HELLO_BEACON_SCALING_FACTOR,
1759 plugin->mac_count + 1),
1766 * Function used for to process the data from the suid process
1768 * @param cls the plugin handle
1769 * @param hdr header of the GNUNET_MessageHeader
1772 handle_helper_message (void *cls,
1773 const struct GNUNET_MessageHeader *hdr)
1775 struct Plugin *plugin = cls;
1776 struct GNUNET_HELLO_Address *my_address;
1777 const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rxinfo;
1778 const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *cm;
1779 struct WlanAddress wa;
1780 struct MacAndSession mas;
1782 struct FragmentMessage *fm;
1783 struct MacEndpoint *endpoint;
1785 msize = ntohs (hdr->size);
1786 switch (ntohs (hdr->type))
1788 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
1789 if (msize != sizeof(struct GNUNET_TRANSPORT_WLAN_HelperControlMessage))
1794 cm = (const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *) hdr;
1795 if (GNUNET_YES == plugin->have_mac)
1797 if (0 == memcmp (&plugin->mac_address,
1799 sizeof(struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1800 break; /* no change */
1801 /* remove old address */
1802 memset (&wa, 0, sizeof(struct WlanAddress));
1803 wa.mac = plugin->mac_address;
1804 wa.options = htonl (plugin->options);
1805 my_address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1808 GNUNET_HELLO_ADDRESS_INFO_NONE);
1809 plugin->env->notify_address (plugin->env->cls,
1812 GNUNET_HELLO_address_free (my_address);
1813 plugin->mac_address = cm->mac;
1817 plugin->mac_address = cm->mac;
1818 plugin->have_mac = GNUNET_YES;
1819 for (endpoint = plugin->mac_head; NULL != endpoint; endpoint =
1822 for (fm = endpoint->sending_messages_head; NULL != fm; fm = fm->next)
1824 if (NULL != fm->fragcontext)
1826 GNUNET_break (0); /* should not happen */
1830 GNUNET_FRAGMENT_context_create (plugin->env->stats,
1833 fm->macendpoint->msg_delay,
1834 fm->macendpoint->ack_delay,
1836 &transmit_fragment, fm);
1837 GNUNET_free (fm->msg);
1841 GNUNET_break (NULL == plugin->beacon_task);
1842 plugin->beacon_task = GNUNET_SCHEDULER_add_now (&send_hello_beacon,
1846 memset (&wa, 0, sizeof(struct WlanAddress));
1847 wa.mac = plugin->mac_address;
1848 wa.options = htonl (plugin->options);
1849 my_address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1852 GNUNET_HELLO_ADDRESS_INFO_NONE);
1854 LOG (GNUNET_ERROR_TYPE_DEBUG,
1855 "Received WLAN_HELPER_CONTROL message with MAC address `%s' for peer `%s'\n",
1856 mac_to_string (&cm->mac),
1857 GNUNET_i2s (plugin->env->my_identity));
1858 plugin->env->notify_address (plugin->env->cls,
1861 GNUNET_HELLO_address_free (my_address);
1864 case GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER:
1865 LOG (GNUNET_ERROR_TYPE_DEBUG,
1866 "Got data message from helper with %u bytes\n",
1868 GNUNET_STATISTICS_update (plugin->env->stats,
1869 _ ("# DATA messages received"), 1,
1871 if (msize < sizeof(struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage))
1873 LOG (GNUNET_ERROR_TYPE_DEBUG,
1874 "Size of packet is too small (%u bytes < %u)\n",
1875 msize, sizeof(struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage));
1878 rxinfo = (const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) hdr;
1880 /* check if message is actually for us */
1881 if (0 != memcmp (&rxinfo->frame.addr3, &mac_bssid_gnunet,
1882 sizeof(struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1884 /* Not the GNUnet BSSID */
1887 if ((0 != memcmp (&rxinfo->frame.addr1, &bc_all_mac,
1888 sizeof(struct GNUNET_TRANSPORT_WLAN_MacAddress))) &&
1889 (0 != memcmp (&rxinfo->frame.addr1, &plugin->mac_address,
1890 sizeof(struct GNUNET_TRANSPORT_WLAN_MacAddress))))
1892 /* Neither broadcast nor specifically for us */
1895 if (0 == memcmp (&rxinfo->frame.addr2, &plugin->mac_address,
1896 sizeof(struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1898 /* packet is FROM us, thus not FOR us */
1902 GNUNET_STATISTICS_update (plugin->env->stats,
1903 _ ("# DATA messages processed"),
1905 LOG (GNUNET_ERROR_TYPE_DEBUG,
1906 "Receiving %u bytes of data from MAC `%s'\n",
1907 (unsigned int) (msize - sizeof(struct
1908 GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)),
1909 mac_to_string (&rxinfo->frame.addr2));
1910 LOG (GNUNET_ERROR_TYPE_DEBUG,
1911 "Receiving %u bytes of data to MAC `%s'\n",
1912 (unsigned int) (msize - sizeof(struct
1913 GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)),
1914 mac_to_string (&rxinfo->frame.addr1));
1915 LOG (GNUNET_ERROR_TYPE_DEBUG,
1916 "Receiving %u bytes of data with BSSID MAC `%s'\n",
1917 (unsigned int) (msize - sizeof(struct
1918 GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)),
1919 mac_to_string (&rxinfo->frame.addr3));
1920 wa.mac = rxinfo->frame.addr2;
1921 wa.options = htonl (0);
1922 mas.endpoint = create_macendpoint (plugin, &wa);
1924 (void) GNUNET_SERVER_mst_receive (plugin->helper_payload_tokenizer,
1926 (const char *) &rxinfo[1],
1927 msize - sizeof(struct
1928 GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage),
1929 GNUNET_YES, GNUNET_NO);
1934 LOG (GNUNET_ERROR_TYPE_ERROR,
1935 "Unexpected message of type %u (%u bytes)",
1945 * Another peer has suggested an address for this
1946 * peer and transport plugin. Check that this could be a valid
1947 * address. If so, consider adding it to the list
1950 * @param cls closure
1951 * @param addr pointer to the address
1952 * @param addrlen length of @a addr
1953 * @return #GNUNET_OK if this is a plausible address for this peer
1957 wlan_plugin_address_suggested (void *cls,
1961 struct Plugin *plugin = cls;
1962 struct WlanAddress *wa = (struct WlanAddress *) addr;
1964 if (addrlen != sizeof(struct WlanAddress))
1966 GNUNET_break_op (0);
1967 return GNUNET_SYSERR;
1969 if (GNUNET_YES != plugin->have_mac)
1971 LOG (GNUNET_ERROR_TYPE_DEBUG,
1972 "Rejecting MAC `%s': I don't know my MAC!\n",
1973 mac_to_string (addr));
1974 return GNUNET_NO; /* don't know my MAC */
1976 if (0 != memcmp (&wa->mac,
1977 &plugin->mac_address,
1980 LOG (GNUNET_ERROR_TYPE_DEBUG,
1981 "Rejecting MAC `%s': not my MAC!\n",
1982 mac_to_string (addr));
1983 return GNUNET_NO; /* not my MAC */
1990 * Convert the transports address to a nice, human-readable format.
1992 * @param cls closure
1993 * @param type name of the transport that generated the address
1994 * @param addr one of the addresses of the host, NULL for the last address
1995 * the specific address format depends on the transport
1996 * @param addrlen length of the address
1997 * @param numeric should (IP) addresses be displayed in numeric form?
1998 * @param timeout after how long should we give up?
1999 * @param asc function to call on each string
2000 * @param asc_cls closure for @a asc
2003 wlan_plugin_address_pretty_printer (void *cls,
2008 struct GNUNET_TIME_Relative timeout,
2009 GNUNET_TRANSPORT_AddressStringCallback asc,
2014 if (sizeof(struct WlanAddress) == addrlen)
2015 ret = wlan_plugin_address_to_string (NULL,
2022 (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
2023 asc (asc_cls, NULL, GNUNET_OK);
2028 * Exit point from the plugin.
2030 * @param cls pointer to the api struct
2033 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2035 struct WlanAddress wa;
2036 struct GNUNET_HELLO_Address *address;
2037 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2038 struct Plugin *plugin = api->cls;
2039 struct MacEndpoint *endpoint;
2040 struct MacEndpoint *endpoint_next;
2047 if (GNUNET_YES == plugin->have_mac)
2049 memset (&wa, 0, sizeof(wa));
2050 wa.options = htonl (plugin->options);
2051 wa.mac = plugin->mac_address;
2052 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2054 &wa, sizeof(struct WlanAddress),
2055 GNUNET_HELLO_ADDRESS_INFO_NONE);
2057 plugin->env->notify_address (plugin->env->cls,
2060 plugin->have_mac = GNUNET_NO;
2061 GNUNET_HELLO_address_free (address);
2064 if (NULL != plugin->beacon_task)
2066 GNUNET_SCHEDULER_cancel (plugin->beacon_task);
2067 plugin->beacon_task = NULL;
2069 if (NULL != plugin->suid_helper)
2071 GNUNET_HELPER_stop (plugin->suid_helper,
2073 plugin->suid_helper = NULL;
2075 endpoint_next = plugin->mac_head;
2076 while (NULL != (endpoint = endpoint_next))
2078 endpoint_next = endpoint->next;
2079 free_macendpoint (endpoint);
2081 if (NULL != plugin->fragment_data_tokenizer)
2083 GNUNET_SERVER_mst_destroy (plugin->fragment_data_tokenizer);
2084 plugin->fragment_data_tokenizer = NULL;
2086 if (NULL != plugin->wlan_header_payload_tokenizer)
2088 GNUNET_SERVER_mst_destroy (plugin->wlan_header_payload_tokenizer);
2089 plugin->wlan_header_payload_tokenizer = NULL;
2091 if (NULL != plugin->helper_payload_tokenizer)
2093 GNUNET_SERVER_mst_destroy (plugin->helper_payload_tokenizer);
2094 plugin->helper_payload_tokenizer = NULL;
2096 GNUNET_free_non_null (plugin->wlan_interface);
2097 GNUNET_free (plugin);
2104 * Function called to convert a string address to
2107 * @param cls closure (`struct Plugin *`)
2108 * @param addr string address
2109 * @param addrlen length of the address
2110 * @param buf location to store the buffer
2111 * @param added location to store the number of bytes in the buffer.
2112 * If the function returns #GNUNET_SYSERR, its contents are undefined.
2113 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2116 wlan_plugin_string_to_address (void *cls,
2122 struct WlanAddress *wa;
2128 if ((NULL == addr) || (0 == addrlen))
2131 return GNUNET_SYSERR;
2133 if ('\0' != addr[addrlen - 1])
2136 return GNUNET_SYSERR;
2138 if (strlen (addr) != addrlen - 1)
2141 return GNUNET_SYSERR;
2144 if (8 != sscanf (addr,
2145 "%4s.%u.%X:%X:%X:%X:%X:%X",
2147 &a[0], &a[1], &a[2],
2148 &a[3], &a[4], &a[5]))
2151 return GNUNET_SYSERR;
2153 wa = GNUNET_new (struct WlanAddress);
2154 for (i = 0; i < 6; i++)
2155 wa->mac.mac[i] = a[i];
2156 wa->options = htonl (0);
2158 *added = sizeof(struct WlanAddress);
2164 * Begin monitoring sessions of a plugin. There can only
2165 * be one active monitor per plugin (i.e. if there are
2166 * multiple monitors, the transport service needs to
2167 * multiplex the generated events over all of them).
2169 * @param cls closure of the plugin
2170 * @param sic callback to invoke, NULL to disable monitor;
2171 * plugin will being by iterating over all active
2172 * sessions immediately and then enter monitor mode
2173 * @param sic_cls closure for @a sic
2176 wlan_plugin_setup_monitor (void *cls,
2177 GNUNET_TRANSPORT_SessionInfoCallback sic,
2180 struct Plugin *plugin = cls;
2181 struct MacEndpoint *mac;
2182 struct GNUNET_ATS_Session *session;
2185 plugin->sic_cls = sic_cls;
2188 for (mac = plugin->mac_head; NULL != mac; mac = mac->next)
2189 for (session = mac->sessions_head; NULL != session; session =
2192 notify_session_monitor (plugin,
2194 GNUNET_TRANSPORT_SS_INIT);
2195 notify_session_monitor (plugin,
2197 GNUNET_TRANSPORT_SS_UP);
2199 sic (sic_cls, NULL, NULL);
2205 * Function that will be called whenever the transport service wants to
2206 * notify the plugin that a session is still active and in use and
2207 * therefore the session timeout for this session has to be updated
2209 * @param cls closure
2210 * @param peer which peer was the session for
2211 * @param session which session is being updated
2214 wlan_plugin_update_session_timeout (void *cls,
2215 const struct GNUNET_PeerIdentity *peer,
2216 struct GNUNET_ATS_Session *session)
2218 GNUNET_assert (NULL != session->timeout_task);
2219 session->timeout = GNUNET_TIME_relative_to_absolute (
2220 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2225 * Function that will be called whenever the transport service wants to
2226 * notify the plugin that the inbound quota changed and that the plugin
2227 * should update it's delay for the next receive value
2229 * @param cls closure
2230 * @param peer which peer was the session for
2231 * @param session which session is being updated
2232 * @param delay new delay to use for receiving
2235 wlan_plugin_update_inbound_delay (void *cls,
2236 const struct GNUNET_PeerIdentity *peer,
2237 struct GNUNET_ATS_Session *session,
2238 struct GNUNET_TIME_Relative delay)
2240 /* does nothing, as inbound delay is not supported by WLAN */
2245 * Entry point for the plugin.
2247 * @param cls closure, the `struct GNUNET_TRANSPORT_PluginEnvironment *`
2248 * @return the `struct GNUNET_TRANSPORT_PluginFunctions *` or NULL on error
2251 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2253 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2254 struct GNUNET_TRANSPORT_PluginFunctions *api;
2255 struct Plugin *plugin;
2256 char *wlan_interface;
2257 unsigned long long testmode;
2260 /* check for 'special' mode */
2261 if (NULL == env->receive)
2263 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2264 initialze the plugin or the API */
2265 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2267 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2268 api->address_to_string = &wlan_plugin_address_to_string;
2269 api->string_to_address = &wlan_plugin_string_to_address;
2274 /* check configuration */
2276 GNUNET_CONFIGURATION_have_value (env->cfg,
2280 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2286 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2291 binary = GNUNET_OS_get_libexec_binary_path (HELPER_NAME);
2292 if ((0 == testmode) &&
2294 GNUNET_OS_check_helper_binary (binary,
2298 LOG (GNUNET_ERROR_TYPE_ERROR,
2299 _ ("Helper binary `%s' not SUID, cannot run WLAN transport\n"),
2301 GNUNET_free (binary);
2304 GNUNET_free (binary);
2306 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2311 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2317 plugin = GNUNET_new (struct Plugin);
2318 plugin->wlan_interface = wlan_interface;
2320 GNUNET_STATISTICS_set (plugin->env->stats,
2321 _ ("# sessions allocated"),
2323 GNUNET_STATISTICS_set (plugin->env->stats,
2324 _ ("# MAC endpoints allocated"),
2326 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker, NULL, NULL,
2327 GNUNET_BANDWIDTH_value_init (100 * 1024
2330 plugin->fragment_data_tokenizer = GNUNET_SERVER_mst_create (&process_data,
2332 plugin->wlan_header_payload_tokenizer = GNUNET_SERVER_mst_create (
2335 plugin->helper_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data,
2338 plugin->options = 0;
2340 /* some compilers do not like switch on 'long long'... */
2341 switch ((unsigned int) testmode)
2343 case 0: /* normal */
2344 plugin->helper_argv[0] = (char *) HELPER_NAME;
2345 plugin->helper_argv[1] = wlan_interface;
2346 plugin->helper_argv[2] = NULL;
2347 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
2349 plugin->helper_argv,
2350 &handle_helper_message,
2355 case 1: /* testmode, peer 1 */
2356 plugin->helper_argv[0] = (char *) DUMMY_HELPER_NAME;
2357 plugin->helper_argv[1] = (char *) "1";
2358 plugin->helper_argv[2] = NULL;
2359 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
2361 plugin->helper_argv,
2362 &handle_helper_message,
2367 case 2: /* testmode, peer 2 */
2368 plugin->helper_argv[0] = (char *) DUMMY_HELPER_NAME;
2369 plugin->helper_argv[1] = (char *) "2";
2370 plugin->helper_argv[2] = NULL;
2371 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
2373 plugin->helper_argv,
2374 &handle_helper_message,
2383 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2385 api->send = &wlan_plugin_send;
2386 api->get_session = &wlan_plugin_get_session;
2387 api->disconnect_peer = &wlan_plugin_disconnect_peer;
2388 api->disconnect_session = &wlan_plugin_disconnect_session;
2389 api->query_keepalive_factor = &wlan_plugin_query_keepalive_factor;
2390 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2391 api->check_address = &wlan_plugin_address_suggested;
2392 api->address_to_string = &wlan_plugin_address_to_string;
2393 api->string_to_address = &wlan_plugin_string_to_address;
2394 api->get_network = &wlan_plugin_get_network;
2395 api->get_network_for_address = &wlan_plugin_get_network_for_address;
2396 api->update_session_timeout = &wlan_plugin_update_session_timeout;
2397 api->update_inbound_delay = &wlan_plugin_update_inbound_delay;
2398 api->setup_monitor = &wlan_plugin_setup_monitor;
2403 /* end of plugin_transport_wlan.c */