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.
17 * @file transport/plugin_transport_wlan.c
18 * @brief transport plugin for wlan and/or bluetooth
19 * @author David Brodski
20 * @author Christian Grothoff
22 * BUILD_WLAN or BUILD_BLUETOOTH must be defined such that the respective
23 * variant of this code is compiled.
26 #include "gnunet_util_lib.h"
27 #include "gnunet_hello_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_statistics_service.h"
30 #include "gnunet_transport_service.h"
31 #include "gnunet_transport_plugin.h"
32 #include "plugin_transport_wlan.h"
33 #include "gnunet_fragmentation_lib.h"
34 #include "gnunet_constants.h"
39 #define PLUGIN_NAME "wlan"
40 #define CONFIG_NAME "transport-wlan"
41 #define HELPER_NAME "gnunet-helper-transport-wlan"
42 #define DUMMY_HELPER_NAME "gnunet-helper-transport-wlan-dummy"
43 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_wlan_init
44 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_wlan_done
45 #define LOG(kind,...) GNUNET_log_from (kind, "transport-wlan",__VA_ARGS__)
49 * time out of a mac endpoint
51 #define MACENDPOINT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 2)
54 * We reduce the frequence of HELLO beacons in relation to
55 * the number of MAC addresses currently visible to us.
56 * This is the multiplication factor.
58 #define HELLO_BEACON_SCALING_FACTOR GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
63 /* begin case bluetooth */
65 #define PLUGIN_NAME "bluetooth"
66 #define CONFIG_NAME "transport-bluetooth"
67 #define HELPER_NAME "gnunet-helper-transport-bluetooth"
68 /* yes, this is correct, we use the same dummy driver as 'wlan' */
69 #define DUMMY_HELPER_NAME "gnunet-helper-transport-wlan-dummy"
70 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_bluetooth_init
71 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_bluetooth_done
72 #define LOG(kind,...) GNUNET_log_from (kind, "transport-bluetooth",__VA_ARGS__)
75 * time out of a mac endpoint
77 #define MACENDPOINT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 60)
81 * We reduce the frequence of HELLO beacons in relation to
82 * the number of MAC addresses currently visible to us.
83 * This is the multiplication factor.
85 #define HELLO_BEACON_SCALING_FACTOR GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
87 /* end case bluetooth */
89 #error need to build wlan or bluetooth
95 * Functions with this signature are called whenever a
96 * complete message is received by the tokenizer.
98 * Do not call #GNUNET_SERVER_mst_destroy from within
99 * the scope of this callback.
102 * @param client identification of the client
103 * @param message the actual message
104 * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
107 (*GNUNET_SERVER_MessageTokenizerCallback) (void *cls,
109 const struct GNUNET_MessageHeader *message);
112 /* Include legacy message stream tokenizer that was removed from util (for now) */
113 #include "tcp_server_mst_legacy.c"
117 * Max size of packet (that we give to the WLAN driver for transmission)
119 #define WLAN_MTU 1430
123 * Which network scope do we belong to?
126 static const enum GNUNET_ATS_Network_Type scope = GNUNET_ATS_NET_WLAN;
128 static const enum GNUNET_ATS_Network_Type scope = GNUNET_ATS_NET_BT;
133 * Maximum number of messages in defragmentation queue per MAC
135 #define MESSAGES_IN_DEFRAG_QUEUE_PER_MAC 2
138 * Link layer control fields for better compatibility
139 * (i.e. GNUnet over WLAN is not IP-over-WLAN).
141 #define WLAN_LLC_DSAP_FIELD 0x1f
142 #define WLAN_LLC_SSAP_FIELD 0x1f
145 GNUNET_NETWORK_STRUCT_BEGIN
147 * Header for messages which need fragmentation. This is the format of
148 * a message we obtain AFTER defragmentation. We then need to check
149 * the CRC and then tokenize the payload and pass it to the
150 * 'receive' callback.
156 * Message type is #GNUNET_MESSAGE_TYPE_WLAN_DATA.
158 struct GNUNET_MessageHeader header;
161 * CRC32 checksum (only over the payload), in NBO.
163 uint32_t crc GNUNET_PACKED;
166 * Sender of the message.
168 struct GNUNET_PeerIdentity sender;
171 * Target of the message.
173 struct GNUNET_PeerIdentity target;
175 /* followed by payload, possibly including
176 multiple messages! */
182 * Address format for WLAN.
187 * Options set for the WLAN, in NBO.
189 uint32_t options GNUNET_PACKED;
192 * WLAN addresses using MACs.
194 struct GNUNET_TRANSPORT_WLAN_MacAddress mac;
198 GNUNET_NETWORK_STRUCT_END
202 * Information kept for each message that is yet to be fragmented and
205 struct PendingMessage
208 * next entry in the DLL
210 struct PendingMessage *next;
213 * previous entry in the DLL
215 struct PendingMessage *prev;
218 * The pending message
220 struct WlanHeader *msg;
223 * Continuation function to call once the message
224 * has been sent. Can be NULL if there is no
225 * continuation to call.
227 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
230 * Cls for @e transmit_cont
232 void *transmit_cont_cls;
235 * Timeout task (for this message).
237 struct GNUNET_SCHEDULER_Task * timeout_task;
243 * Session handle for connections with other peers.
245 struct GNUNET_ATS_Session
248 * To whom are we talking to (set to our identity
249 * if we are still waiting for the welcome message)
251 struct GNUNET_PeerIdentity target;
254 * We keep all sessions in a DLL at their respective
255 * `struct MACEndpoint *`.
257 struct GNUNET_ATS_Session *next;
260 * We keep all sessions in a DLL at their respective
261 * `struct MACEndpoint *`.
263 struct GNUNET_ATS_Session *prev;
266 * MAC endpoint with the address of this peer.
268 struct MacEndpoint *mac;
271 * Address associated with this session and MAC endpoint
273 struct GNUNET_HELLO_Address *address;
276 * When should this session time out?
278 struct GNUNET_TIME_Absolute timeout;
281 * Timeout task (for the session).
283 struct GNUNET_SCHEDULER_Task * timeout_task;
289 * Struct for messages that are being fragmented in a MAC's transmission queue.
291 struct FragmentMessage
295 * This is a doubly-linked list.
297 struct FragmentMessage *next;
300 * This is a doubly-linked list.
302 struct FragmentMessage *prev;
305 * MAC endpoint this message belongs to
307 struct MacEndpoint *macendpoint;
310 * Fragmentation context
312 struct GNUNET_FRAGMENT_Context *fragcontext;
315 * Transmission handle to helper (to cancel if the frag context
316 * is destroyed early for some reason).
318 struct GNUNET_HELPER_SendHandle *sh;
321 * Intended recipient.
323 struct GNUNET_PeerIdentity target;
326 * Timeout value for the message.
328 struct GNUNET_TIME_Absolute timeout;
333 struct GNUNET_SCHEDULER_Task * timeout_task;
336 * Continuation to call when we're done with this message.
338 GNUNET_TRANSPORT_TransmitContinuation cont;
341 * Message we need to fragment and transmit, NULL after the
342 * @e fragmentcontext has been created.
344 struct GNUNET_MessageHeader *msg;
347 * Closure for @e cont
352 * Size of original message
357 * Number of bytes used to transmit message
365 * Struct to represent one network card connection
371 * We keep all MACs in a DLL in the plugin.
373 struct MacEndpoint *next;
376 * We keep all MACs in a DLL in the plugin.
378 struct MacEndpoint *prev;
381 * Pointer to the global plugin struct.
383 struct Plugin *plugin;
386 * Head of sessions that use this MAC.
388 struct GNUNET_ATS_Session *sessions_head;
391 * Tail of sessions that use this MAC.
393 struct GNUNET_ATS_Session *sessions_tail;
396 * Head of messages we are currently sending to this MAC.
398 struct FragmentMessage *sending_messages_head;
401 * Tail of messages we are currently sending to this MAC.
403 struct FragmentMessage *sending_messages_tail;
406 * Defrag context for this MAC
408 struct GNUNET_DEFRAGMENT_Context *defrag;
411 * When should this endpoint time out?
413 struct GNUNET_TIME_Absolute timeout;
418 struct GNUNET_SCHEDULER_Task * timeout_task;
421 * count of messages in the fragment out queue for this mac endpoint
423 unsigned int fragment_messages_out_count;
428 struct WlanAddress wlan_addr;
431 * Message delay for fragmentation context
433 struct GNUNET_TIME_Relative msg_delay;
436 * ACK delay for fragmentation context
438 struct GNUNET_TIME_Relative ack_delay;
441 * Desired transmission power for this MAC
446 * Desired transmission rate for this MAC
451 * Antenna we should use for this MAC
459 * Encapsulation of all of the state of the plugin.
466 struct GNUNET_TRANSPORT_PluginEnvironment *env;
469 * Handle to helper process for priviledged operations.
471 struct GNUNET_HELPER_Handle *suid_helper;
474 * Function to call about session status changes.
476 GNUNET_TRANSPORT_SessionInfoCallback sic;
479 * Closure for @e sic.
484 * ARGV-vector for the helper (all helpers take only the binary
485 * name, one actual argument, plus the NULL terminator for 'argv').
487 char *helper_argv[3];
490 * The interface of the wlan card given to us by the user.
492 char *wlan_interface;
495 * Tokenizer for demultiplexing of data packets resulting from
498 struct GNUNET_SERVER_MessageStreamTokenizer *fragment_data_tokenizer;
501 * Tokenizer for demultiplexing of data packets received from the suid helper
503 struct GNUNET_SERVER_MessageStreamTokenizer *helper_payload_tokenizer;
506 * Tokenizer for demultiplexing of data packets that follow the WLAN Header
508 struct GNUNET_SERVER_MessageStreamTokenizer *wlan_header_payload_tokenizer;
511 * Head of list of open connections.
513 struct MacEndpoint *mac_head;
516 * Tail of list of open connections.
518 struct MacEndpoint *mac_tail;
521 * Task that periodically sends a HELLO beacon via the helper.
523 struct GNUNET_SCHEDULER_Task *beacon_task;
526 * Tracker for bandwidth limit
528 struct GNUNET_BANDWIDTH_Tracker tracker;
531 * The mac_address of the wlan card given to us by the helper.
533 struct GNUNET_TRANSPORT_WLAN_MacAddress mac_address;
536 * Have we received a control message with our MAC address yet?
541 * Number of connections
543 unsigned int mac_count;
546 * Options for addresses
554 * Information associated with a message. Can contain
555 * the session or the MAC endpoint associated with the
561 * NULL if the identity of the other peer is not known.
563 struct GNUNET_ATS_Session *session;
566 * MAC address of the other peer, NULL if not known.
568 struct MacEndpoint *endpoint;
573 * Print MAC addresses nicely.
575 * @param mac the mac address
576 * @return string to a static buffer with
577 * the human-readable mac, will be overwritten during the next call to
581 mac_to_string (const struct GNUNET_TRANSPORT_WLAN_MacAddress * mac)
583 static char macstr[20];
585 GNUNET_snprintf (macstr,
587 "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
588 mac->mac[0], mac->mac[1],
589 mac->mac[2], mac->mac[3],
590 mac->mac[4], mac->mac[5]);
596 * Function called for a quick conversion of the binary address to
597 * a numeric address. Note that the caller must not free the
598 * address and that the next call to this function is allowed
599 * to override the address again.
602 * @param addr binary address
603 * @param addrlen length of the address
604 * @return string representing the same address
607 wlan_plugin_address_to_string (void *cls,
611 const struct GNUNET_TRANSPORT_WLAN_MacAddress *mac;
612 static char macstr[36];
614 if (sizeof (struct WlanAddress) != addrlen)
619 mac = &((struct WlanAddress *) addr)->mac;
620 GNUNET_snprintf (macstr,
624 ntohl (((struct WlanAddress *) addr)->options),
625 mac_to_string (mac));
631 * If a session monitor is attached, notify it about the new
634 * @param plugin our plugin
635 * @param session session that changed state
636 * @param state new state of the session
639 notify_session_monitor (struct Plugin *plugin,
640 struct GNUNET_ATS_Session *session,
641 enum GNUNET_TRANSPORT_SessionState state)
643 struct GNUNET_TRANSPORT_SessionInfo info;
645 if (NULL == plugin->sic)
647 memset (&info, 0, sizeof (info));
649 info.is_inbound = GNUNET_SYSERR; /* hard to say */
650 info.num_msg_pending = 0; /* we queue per MAC, not per peer */
651 info.num_bytes_pending = 0; /* we queue per MAC, not per peer */
652 info.receive_delay = GNUNET_TIME_UNIT_ZERO_ABS; /* not supported by WLAN */
653 info.session_timeout = session->timeout;
654 info.address = session->address;
655 plugin->sic (plugin->sic_cls,
662 * Fill the radiotap header
664 * @param endpoint pointer to the endpoint, can be NULL
665 * @param header pointer to the radiotap header
666 * @param size total message size
669 get_radiotap_header (struct MacEndpoint *endpoint,
670 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *header,
673 header->header.type = ntohs (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER);
674 header->header.size = ntohs (size);
675 if (NULL != endpoint)
677 header->rate = endpoint->rate;
678 header->tx_power = endpoint->tx_power;
679 header->antenna = endpoint->antenna;
684 header->tx_power = 0;
691 * Generate the WLAN hardware header for one packet
693 * @param plugin the plugin handle
694 * @param header address to write the header to
695 * @param to_mac_addr address of the recipient
696 * @param size size of the whole packet, needed to calculate the time to send the packet
699 get_wlan_header (struct Plugin *plugin,
700 struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *header,
701 const struct GNUNET_TRANSPORT_WLAN_MacAddress *to_mac_addr,
704 const int rate = 11000000;
706 header->frame_control = htons (IEEE80211_FC0_TYPE_DATA);
707 header->addr1 = *to_mac_addr;
708 header->addr2 = plugin->mac_address;
709 header->addr3 = mac_bssid_gnunet;
710 header->duration = GNUNET_htole16 ((size * 1000000) / rate + 290);
711 header->sequence_control = 0; // FIXME?
712 header->llc[0] = WLAN_LLC_DSAP_FIELD;
713 header->llc[1] = WLAN_LLC_SSAP_FIELD;
714 header->llc[2] = 0; // FIXME?
715 header->llc[3] = 0; // FIXME?
720 * Send an ACK for a fragment we received.
722 * @param cls the `struct MacEndpoint *` the ACK must be sent to
723 * @param msg_id id of the message
724 * @param hdr pointer to the hdr where the ack is stored
729 const struct GNUNET_MessageHeader *hdr)
731 struct MacEndpoint *endpoint = cls;
732 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage* radio_header;
733 uint16_t msize = ntohs (hdr->size);
734 size_t size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + msize;
737 if (NULL == endpoint)
742 if (size >= GNUNET_MAX_MESSAGE_SIZE)
747 LOG (GNUNET_ERROR_TYPE_DEBUG,
748 "Sending ACK to %s\n",
749 mac_to_string (&endpoint->wlan_addr.mac));
750 radio_header = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) buf;
751 get_radiotap_header (endpoint, radio_header, size);
752 get_wlan_header (endpoint->plugin,
753 &radio_header->frame,
754 &endpoint->wlan_addr.mac,
755 sizeof (endpoint->wlan_addr.mac));
756 GNUNET_memcpy (&radio_header[1], hdr, msize);
758 GNUNET_HELPER_send (endpoint->plugin->suid_helper,
759 &radio_header->header,
760 GNUNET_NO /* dropping ACKs is bad */,
762 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
769 * Handles the data after all fragments are put together
771 * @param cls macendpoint this messages belongs to
772 * @param hdr pointer to the data
775 wlan_data_message_handler (void *cls,
776 const struct GNUNET_MessageHeader *hdr)
778 struct MacEndpoint *endpoint = cls;
779 struct Plugin *plugin = endpoint->plugin;
780 struct MacAndSession mas;
782 GNUNET_STATISTICS_update (plugin->env->stats,
783 _("# Messages defragmented"),
787 mas.endpoint = endpoint;
788 (void) GNUNET_SERVER_mst_receive (plugin->fragment_data_tokenizer,
792 GNUNET_YES, GNUNET_NO);
799 * @param cls our `struct Plugin`.
800 * @param session the session free
803 wlan_plugin_disconnect_session (void *cls,
804 struct GNUNET_ATS_Session *session)
806 struct MacEndpoint *endpoint = session->mac;
807 struct Plugin *plugin = endpoint->plugin;
809 plugin->env->session_end (plugin->env->cls,
812 notify_session_monitor (plugin,
814 GNUNET_TRANSPORT_SS_DONE);
815 GNUNET_CONTAINER_DLL_remove (endpoint->sessions_head,
816 endpoint->sessions_tail,
818 if (session->timeout_task != NULL)
820 GNUNET_SCHEDULER_cancel (session->timeout_task);
821 session->timeout_task = NULL;
823 GNUNET_STATISTICS_update (plugin->env->stats,
824 _("# Sessions allocated"),
827 GNUNET_HELLO_address_free (session->address);
828 GNUNET_free (session);
834 * Function that is called to get the keepalive factor.
835 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
836 * calculate the interval between keepalive packets.
838 * @param cls closure with the `struct Plugin`
839 * @return keepalive factor
842 wlan_plugin_query_keepalive_factor (void *cls)
849 * A session is timing out. Clean up.
851 * @param cls pointer to the Session
854 session_timeout (void *cls)
856 struct GNUNET_ATS_Session *session = cls;
857 struct GNUNET_TIME_Relative left;
859 session->timeout_task = NULL;
860 left = GNUNET_TIME_absolute_get_remaining (session->timeout);
861 if (0 != left.rel_value_us)
863 session->timeout_task =
864 GNUNET_SCHEDULER_add_delayed (left,
869 wlan_plugin_disconnect_session (session->mac->plugin,
876 * Lookup a new session
878 * @param endpoint pointer to the mac endpoint of the peer
879 * @param peer peer identity to use for this session
880 * @return returns the session or NULL
882 static struct GNUNET_ATS_Session *
883 lookup_session (struct MacEndpoint *endpoint,
884 const struct GNUNET_PeerIdentity *peer)
886 struct GNUNET_ATS_Session *session;
888 for (session = endpoint->sessions_head; NULL != session; session = session->next)
889 if (0 == memcmp (peer, &session->target, sizeof (struct GNUNET_PeerIdentity)))
896 * Create a new session
898 * @param endpoint pointer to the mac endpoint of the peer
899 * @param peer peer identity to use for this session
900 * @return returns the session or NULL
902 static struct GNUNET_ATS_Session *
903 create_session (struct MacEndpoint *endpoint,
904 const struct GNUNET_PeerIdentity *peer)
906 struct GNUNET_ATS_Session *session;
908 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
909 _("# Sessions allocated"),
912 session = GNUNET_new (struct GNUNET_ATS_Session);
913 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
914 endpoint->sessions_tail,
916 session->address = GNUNET_HELLO_address_allocate (peer,
918 &endpoint->wlan_addr,
919 sizeof (endpoint->wlan_addr),
920 GNUNET_HELLO_ADDRESS_INFO_NONE);
921 session->mac = endpoint;
922 session->target = *peer;
923 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
924 session->timeout_task =
925 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &session_timeout,
927 notify_session_monitor (endpoint->plugin,
929 GNUNET_TRANSPORT_SS_INIT);
930 notify_session_monitor (endpoint->plugin,
932 GNUNET_TRANSPORT_SS_UP);
933 LOG (GNUNET_ERROR_TYPE_DEBUG,
934 "Created new session %p for peer `%s' with endpoint %s\n",
937 mac_to_string (&endpoint->wlan_addr.mac));
944 * Look up a session for a peer and create a new session if none is found
946 * @param endpoint pointer to the mac endpoint of the peer
947 * @param peer peer identity to use for this session
948 * @return returns the session
950 static struct GNUNET_ATS_Session *
951 get_session (struct MacEndpoint *endpoint,
952 const struct GNUNET_PeerIdentity *peer)
954 struct GNUNET_ATS_Session *session;
956 if (NULL != (session = lookup_session (endpoint, peer)))
958 return create_session (endpoint, peer);
963 * Function called once we have successfully given the fragment
964 * message to the SUID helper process and we are thus ready for
967 * @param cls the `struct FragmentMessage *`
968 * @param result result of the operation (#GNUNET_OK on success,
969 * #GNUNET_NO if the helper died, #GNUNET_SYSERR
970 * if the helper was stopped)
973 fragment_transmission_done (void *cls,
976 struct FragmentMessage *fm = cls;
979 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
984 * Transmit a fragment of a message.
986 * @param cls `struct FragmentMessage *` this fragment message belongs to
987 * @param hdr pointer to the start of the fragment message
990 transmit_fragment (void *cls,
991 const struct GNUNET_MessageHeader *hdr)
993 struct FragmentMessage *fm = cls;
994 struct MacEndpoint *endpoint = fm->macendpoint;
998 if (NULL == endpoint)
1003 msize = ntohs (hdr->size);
1004 size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + msize;
1007 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radio_header;
1009 radio_header = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) buf;
1010 get_radiotap_header (endpoint, radio_header, size);
1011 LOG (GNUNET_ERROR_TYPE_DEBUG,
1012 "Sending %u bytes of data to MAC `%s'\n",
1013 (unsigned int) msize,
1014 mac_to_string (&endpoint->wlan_addr.mac));
1016 get_wlan_header (endpoint->plugin,
1017 &radio_header->frame,
1018 &endpoint->wlan_addr.mac,
1019 sizeof (endpoint->wlan_addr.mac));
1020 GNUNET_memcpy (&radio_header[1], hdr, msize);
1021 GNUNET_assert (NULL == fm->sh);
1022 fm->sh = GNUNET_HELPER_send (endpoint->plugin->suid_helper,
1023 &radio_header->header,
1025 &fragment_transmission_done, fm);
1026 fm->size_on_wire += size;
1029 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
1030 _("# message fragments sent"),
1036 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
1038 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
1039 "# bytes currently in buffers",
1041 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
1042 "# bytes transmitted",
1049 * Frees the space of a message in the fragment queue (send queue)
1051 * @param fm message to free
1054 free_fragment_message (struct FragmentMessage *fm)
1056 struct MacEndpoint *endpoint = fm->macendpoint;
1058 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
1059 _("# messages pending (with fragmentation)"),
1061 GNUNET_CONTAINER_DLL_remove (endpoint->sending_messages_head,
1062 endpoint->sending_messages_tail,
1066 GNUNET_HELPER_send_cancel (fm->sh);
1069 if (NULL != fm->msg)
1071 GNUNET_free (fm->msg);
1074 if (NULL != fm->fragcontext)
1076 GNUNET_FRAGMENT_context_destroy (fm->fragcontext,
1077 &endpoint->msg_delay,
1078 &endpoint->ack_delay);
1079 fm->fragcontext = NULL;
1081 if (NULL != fm->timeout_task)
1083 GNUNET_SCHEDULER_cancel (fm->timeout_task);
1084 fm->timeout_task = NULL;
1091 * A FragmentMessage has timed out. Remove it.
1093 * @param cls pointer to the 'struct FragmentMessage'
1096 fragmentmessage_timeout (void *cls)
1098 struct FragmentMessage *fm = cls;
1100 fm->timeout_task = NULL;
1101 if (NULL != fm->cont)
1103 fm->cont (fm->cont_cls,
1110 free_fragment_message (fm);
1115 * Transmit a message to the given destination with fragmentation.
1117 * @param endpoint desired destination
1118 * @param timeout how long can the message wait?
1119 * @param target peer that should receive the message
1120 * @param msg message to transmit
1121 * @param payload_size bytes of payload
1122 * @param cont continuation to call once the message has
1123 * been transmitted (or if the transport is ready
1124 * for the next transmission call; or if the
1125 * peer disconnected...); can be NULL
1126 * @param cont_cls closure for @a cont
1129 send_with_fragmentation (struct MacEndpoint *endpoint,
1130 struct GNUNET_TIME_Relative timeout,
1131 const struct GNUNET_PeerIdentity *target,
1132 const struct GNUNET_MessageHeader *msg,
1133 size_t payload_size,
1134 GNUNET_TRANSPORT_TransmitContinuation cont,
1138 struct FragmentMessage *fm;
1139 struct Plugin *plugin;
1141 plugin = endpoint->plugin;
1142 fm = GNUNET_new (struct FragmentMessage);
1143 fm->macendpoint = endpoint;
1144 fm->target = *target;
1145 fm->size_payload = payload_size;
1146 fm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1148 fm->cont_cls = cont_cls;
1149 /* 1 MBit/s typical data rate, 1430 byte fragments => ~100 ms per message */
1151 GNUNET_SCHEDULER_add_delayed (timeout,
1152 &fragmentmessage_timeout,
1154 if (GNUNET_YES == plugin->have_mac)
1157 GNUNET_FRAGMENT_context_create (plugin->env->stats,
1160 fm->macendpoint->msg_delay,
1161 fm->macendpoint->ack_delay,
1163 &transmit_fragment, fm);
1167 fm->msg = GNUNET_copy_message (msg);
1169 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sending_messages_head,
1170 endpoint->sending_messages_tail,
1176 * Free a MAC endpoint.
1178 * @param endpoint pointer to the MacEndpoint to free
1181 free_macendpoint (struct MacEndpoint *endpoint)
1183 struct Plugin *plugin = endpoint->plugin;
1184 struct FragmentMessage *fm;
1185 struct GNUNET_ATS_Session *session;
1187 GNUNET_STATISTICS_update (plugin->env->stats,
1188 _("# MAC endpoints allocated"),
1191 while (NULL != (session = endpoint->sessions_head))
1192 wlan_plugin_disconnect_session (plugin,
1194 while (NULL != (fm = endpoint->sending_messages_head))
1195 free_fragment_message (fm);
1196 GNUNET_CONTAINER_DLL_remove (plugin->mac_head,
1200 if (NULL != endpoint->defrag)
1202 GNUNET_DEFRAGMENT_context_destroy(endpoint->defrag);
1203 endpoint->defrag = NULL;
1206 plugin->mac_count--;
1207 if (NULL != endpoint->timeout_task)
1209 GNUNET_SCHEDULER_cancel (endpoint->timeout_task);
1210 endpoint->timeout_task = NULL;
1212 GNUNET_free (endpoint);
1217 * A MAC endpoint is timing out. Clean up.
1219 * @param cls pointer to the `struct MacEndpoint *`
1222 macendpoint_timeout (void *cls)
1224 struct MacEndpoint *endpoint = cls;
1225 struct GNUNET_TIME_Relative timeout;
1227 endpoint->timeout_task = NULL;
1228 timeout = GNUNET_TIME_absolute_get_remaining (endpoint->timeout);
1229 if (0 == timeout.rel_value_us)
1231 free_macendpoint (endpoint);
1234 endpoint->timeout_task =
1235 GNUNET_SCHEDULER_add_delayed (timeout,
1236 &macendpoint_timeout,
1242 * Find (or create) a MacEndpoint with a specific MAC address
1244 * @param plugin pointer to the plugin struct
1245 * @param mac the MAC address of the endpoint
1246 * @return handle to our data structure for this MAC
1248 static struct MacEndpoint *
1249 create_macendpoint (struct Plugin *plugin,
1250 struct WlanAddress *mac)
1252 struct MacEndpoint *pos;
1254 for (pos = plugin->mac_head; NULL != pos; pos = pos->next)
1255 if (0 == memcmp (mac, &pos->wlan_addr, sizeof (pos->wlan_addr)))
1257 pos = GNUNET_new (struct MacEndpoint);
1258 pos->wlan_addr = (*mac);
1259 pos->plugin = plugin;
1261 GNUNET_DEFRAGMENT_context_create (plugin->env->stats,
1263 MESSAGES_IN_DEFRAG_QUEUE_PER_MAC,
1265 &wlan_data_message_handler,
1268 pos->msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1269 pos->ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
1270 pos->timeout = GNUNET_TIME_relative_to_absolute (MACENDPOINT_TIMEOUT);
1272 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
1274 GNUNET_CONTAINER_DLL_insert (plugin->mac_head,
1277 plugin->mac_count++;
1278 GNUNET_STATISTICS_update (plugin->env->stats,
1279 _("# MAC endpoints allocated"),
1281 LOG (GNUNET_ERROR_TYPE_DEBUG,
1282 "New MAC endpoint `%s'\n",
1283 wlan_plugin_address_to_string (NULL,
1285 sizeof (struct WlanAddress)));
1291 * Function obtain the network type for a session
1293 * @param cls closure (`struct Plugin*`)
1294 * @param session the session
1295 * @return the network type in HBO or #GNUNET_SYSERR
1297 static enum GNUNET_ATS_Network_Type
1298 wlan_plugin_get_network (void *cls,
1299 struct GNUNET_ATS_Session *session)
1302 return GNUNET_ATS_NET_WLAN;
1304 return GNUNET_ATS_NET_BT;
1310 * Function obtain the network type for an address.
1312 * @param cls closure (`struct Plugin *`)
1313 * @param address the address
1314 * @return the network type
1316 static enum GNUNET_ATS_Network_Type
1317 wlan_plugin_get_network_for_address (void *cls,
1318 const struct GNUNET_HELLO_Address *address)
1321 return GNUNET_ATS_NET_WLAN;
1323 return GNUNET_ATS_NET_BT;
1329 * Creates a new outbound session the transport service will use to
1330 * send data to the peer
1332 * @param cls the `struct Plugin *`
1333 * @param address the address
1334 * @return the session or NULL of max connections exceeded
1336 static struct GNUNET_ATS_Session *
1337 wlan_plugin_get_session (void *cls,
1338 const struct GNUNET_HELLO_Address *address)
1340 struct Plugin *plugin = cls;
1341 struct MacEndpoint *endpoint;
1343 if (NULL == address)
1345 if (sizeof (struct WlanAddress) != address->address_length)
1350 LOG (GNUNET_ERROR_TYPE_DEBUG,
1351 "Service asked to create session for peer `%s' with MAC `%s'\n",
1352 GNUNET_i2s (&address->peer),
1353 wlan_plugin_address_to_string (NULL,
1355 address->address_length));
1356 endpoint = create_macendpoint (plugin,
1357 (struct WlanAddress *) address->address);
1358 return get_session (endpoint, &address->peer);
1363 * Function that can be used to force the plugin to disconnect
1364 * from the given peer and cancel all previous transmissions
1365 * (and their continuation).
1367 * @param cls closure
1368 * @param target peer from which to disconnect
1371 wlan_plugin_disconnect_peer (void *cls,
1372 const struct GNUNET_PeerIdentity *target)
1374 struct Plugin *plugin = cls;
1375 struct GNUNET_ATS_Session *session;
1376 struct MacEndpoint *endpoint;
1378 for (endpoint = plugin->mac_head; NULL != endpoint; endpoint = endpoint->next)
1379 for (session = endpoint->sessions_head; NULL != session; session = session->next)
1380 if (0 == memcmp (target, &session->target,
1381 sizeof (struct GNUNET_PeerIdentity)))
1383 wlan_plugin_disconnect_session (plugin, session);
1384 break; /* inner-loop only (in case peer has another MAC as well!) */
1390 * Function that can be used by the transport service to transmit
1391 * a message using the plugin. Note that in the case of a
1392 * peer disconnecting, the continuation MUST be called
1393 * prior to the disconnect notification itself. This function
1394 * will be called with this peer's HELLO message to initiate
1395 * a fresh connection to another peer.
1397 * @param cls closure
1398 * @param session which session must be used
1399 * @param msgbuf the message to transmit
1400 * @param msgbuf_size number of bytes in @a msgbuf
1401 * @param priority how important is the message (most plugins will
1402 * ignore message priority and just FIFO)
1403 * @param to how long to wait at most for the transmission (does not
1404 * require plugins to discard the message after the timeout,
1405 * just advisory for the desired delay; most plugins will ignore
1407 * @param cont continuation to call once the message has
1408 * been transmitted (or if the transport is ready
1409 * for the next transmission call; or if the
1410 * peer disconnected...); can be NULL
1411 * @param cont_cls closure for @a cont
1412 * @return number of bytes used (on the physical network, with overheads);
1413 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1414 * and does NOT mean that the message was not transmitted (DV)
1417 wlan_plugin_send (void *cls,
1418 struct GNUNET_ATS_Session *session,
1419 const char *msgbuf, size_t msgbuf_size,
1420 unsigned int priority,
1421 struct GNUNET_TIME_Relative to,
1422 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1424 struct Plugin *plugin = cls;
1425 struct WlanHeader *wlanheader;
1426 size_t size = msgbuf_size + sizeof (struct WlanHeader);
1427 char buf[size] GNUNET_ALIGN;
1429 LOG (GNUNET_ERROR_TYPE_DEBUG,
1430 "Transmitting %u bytes of payload to peer `%s' (starting with %u byte message of type %u)\n",
1432 GNUNET_i2s (&session->target),
1433 (unsigned int) ntohs (((struct GNUNET_MessageHeader*)msgbuf)->size),
1434 (unsigned int) ntohs (((struct GNUNET_MessageHeader*)msgbuf)->type));
1435 wlanheader = (struct WlanHeader *) buf;
1436 wlanheader->header.size = htons (msgbuf_size + sizeof (struct WlanHeader));
1437 wlanheader->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA);
1438 wlanheader->sender = *plugin->env->my_identity;
1439 wlanheader->target = session->target;
1440 wlanheader->crc = htonl (GNUNET_CRYPTO_crc32_n (msgbuf, msgbuf_size));
1441 GNUNET_memcpy (&wlanheader[1],
1444 GNUNET_STATISTICS_update (plugin->env->stats,
1445 "# bytes currently in buffers",
1448 send_with_fragmentation (session->mac,
1451 &wlanheader->header,
1459 * We have received data from the WLAN via some session. Process depending
1460 * on the message type (HELLO, DATA, FRAGMENTATION or FRAGMENTATION-ACK).
1462 * @param cls pointer to the plugin
1463 * @param client pointer to the session this message belongs to
1464 * @param hdr start of the message
1467 process_data (void *cls,
1469 const struct GNUNET_MessageHeader *hdr)
1471 struct Plugin *plugin = cls;
1472 struct GNUNET_HELLO_Address *address;
1473 struct MacAndSession *mas = client;
1474 struct FragmentMessage *fm;
1475 struct GNUNET_PeerIdentity tmpsource;
1476 const struct WlanHeader *wlanheader;
1480 msize = ntohs (hdr->size);
1482 GNUNET_STATISTICS_update (plugin->env->stats,
1486 switch (ntohs (hdr->type))
1488 case GNUNET_MESSAGE_TYPE_HELLO:
1491 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hdr,
1494 GNUNET_break_op (0);
1497 if (NULL == mas->endpoint)
1503 LOG (GNUNET_ERROR_TYPE_DEBUG,
1504 "Processing %u bytes of HELLO from peer `%s' at MAC %s\n",
1505 (unsigned int) msize,
1506 GNUNET_i2s (&tmpsource),
1507 wlan_plugin_address_to_string (NULL,
1508 &mas->endpoint->wlan_addr,
1509 sizeof (mas->endpoint->wlan_addr)));
1511 GNUNET_STATISTICS_update (plugin->env->stats,
1512 _("# HELLO messages received"), 1,
1514 address = GNUNET_HELLO_address_allocate (&tmpsource,
1516 &mas->endpoint->wlan_addr,
1517 sizeof (mas->endpoint->wlan_addr),
1518 GNUNET_HELLO_ADDRESS_INFO_NONE);
1519 mas->session = lookup_session (mas->endpoint,
1521 if (NULL == mas->session)
1523 mas->session = create_session (mas->endpoint,
1525 plugin->env->session_start (plugin->env->cls,
1530 plugin->env->receive (plugin->env->cls,
1534 GNUNET_HELLO_address_free (address);
1536 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1537 if (NULL == mas->endpoint)
1542 LOG (GNUNET_ERROR_TYPE_DEBUG,
1543 "Processing %u bytes of FRAGMENT from MAC %s\n",
1544 (unsigned int) msize,
1545 wlan_plugin_address_to_string (NULL,
1546 &mas->endpoint->wlan_addr,
1547 sizeof (mas->endpoint->wlan_addr)));
1548 GNUNET_STATISTICS_update (plugin->env->stats,
1549 _("# fragments received"),
1552 (void) GNUNET_DEFRAGMENT_process_fragment (mas->endpoint->defrag,
1555 case GNUNET_MESSAGE_TYPE_FRAGMENT_ACK:
1556 if (NULL == mas->endpoint)
1561 GNUNET_STATISTICS_update (plugin->env->stats,
1562 _("# ACKs received"),
1564 for (fm = mas->endpoint->sending_messages_head; NULL != fm; fm = fm->next)
1566 ret = GNUNET_FRAGMENT_process_ack (fm->fragcontext, hdr);
1567 if (GNUNET_OK == ret)
1569 LOG (GNUNET_ERROR_TYPE_DEBUG,
1570 "Got last ACK, finished message transmission to `%s' (%p)\n",
1571 wlan_plugin_address_to_string (NULL,
1572 &mas->endpoint->wlan_addr,
1573 sizeof (mas->endpoint->wlan_addr)),
1575 mas->endpoint->timeout = GNUNET_TIME_relative_to_absolute (MACENDPOINT_TIMEOUT);
1576 if (NULL != fm->cont)
1578 fm->cont (fm->cont_cls,
1585 free_fragment_message (fm);
1588 if (GNUNET_NO == ret)
1590 LOG (GNUNET_ERROR_TYPE_DEBUG,
1591 "Got an ACK, message transmission to `%s' not yet finished\n",
1592 wlan_plugin_address_to_string (NULL,
1593 &mas->endpoint->wlan_addr,
1594 sizeof (mas->endpoint->wlan_addr)));
1599 LOG (GNUNET_ERROR_TYPE_DEBUG,
1600 "ACK not matched against any active fragmentation with MAC `%s'\n",
1601 wlan_plugin_address_to_string (NULL,
1602 &mas->endpoint->wlan_addr,
1603 sizeof (mas->endpoint->wlan_addr)));
1605 case GNUNET_MESSAGE_TYPE_WLAN_DATA:
1606 if (NULL == mas->endpoint)
1611 if (msize < sizeof (struct WlanHeader))
1616 wlanheader = (const struct WlanHeader *) hdr;
1617 if (0 != memcmp (&wlanheader->target,
1618 plugin->env->my_identity,
1619 sizeof (struct GNUNET_PeerIdentity)))
1621 LOG (GNUNET_ERROR_TYPE_DEBUG,
1622 "Data for `%s', not for me, ignoring\n",
1623 GNUNET_i2s (&wlanheader->target));
1626 if (ntohl (wlanheader->crc) !=
1627 GNUNET_CRYPTO_crc32_n (&wlanheader[1],
1628 msize - sizeof (struct WlanHeader)))
1630 GNUNET_STATISTICS_update (plugin->env->stats,
1631 _("# DATA messages discarded due to CRC32 error"),
1636 mas->session = lookup_session (mas->endpoint,
1637 &wlanheader->sender);
1638 if (NULL == mas->session)
1640 mas->session = create_session (mas->endpoint,
1641 &wlanheader->sender);
1642 address = GNUNET_HELLO_address_allocate (&wlanheader->sender,
1644 &mas->endpoint->wlan_addr,
1645 sizeof (struct WlanAddress),
1646 GNUNET_HELLO_ADDRESS_INFO_NONE);
1647 plugin->env->session_start (plugin->env->cls,
1651 LOG (GNUNET_ERROR_TYPE_DEBUG,
1652 "Notifying transport about peer `%s''s new session %p \n",
1653 GNUNET_i2s (&wlanheader->sender),
1655 GNUNET_HELLO_address_free (address);
1657 LOG (GNUNET_ERROR_TYPE_DEBUG,
1658 "Processing %u bytes of DATA from peer `%s'\n",
1659 (unsigned int) msize,
1660 GNUNET_i2s (&wlanheader->sender));
1661 mas->session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1662 (void) GNUNET_SERVER_mst_receive (plugin->wlan_header_payload_tokenizer,
1664 (const char *) &wlanheader[1],
1665 msize - sizeof (struct WlanHeader),
1670 if (NULL == mas->endpoint)
1675 if (NULL == mas->session)
1680 LOG (GNUNET_ERROR_TYPE_DEBUG,
1681 "Received packet with %u bytes of type %u from peer %s\n",
1682 (unsigned int) msize,
1683 (unsigned int) ntohs (hdr->type),
1684 GNUNET_i2s (&mas->session->target));
1685 plugin->env->receive (plugin->env->cls,
1686 mas->session->address,
1696 * Task to (periodically) send a HELLO beacon
1698 * @param cls pointer to the plugin struct
1701 send_hello_beacon (void *cls)
1703 struct Plugin *plugin = cls;
1705 uint16_t hello_size;
1706 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
1707 const struct GNUNET_MessageHeader *hello;
1709 hello = plugin->env->get_our_hello ();
1712 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1713 GNUNET_assert (sizeof (struct WlanHeader) + hello_size <= WLAN_MTU);
1714 size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + hello_size;
1716 char buf[size] GNUNET_ALIGN;
1718 LOG (GNUNET_ERROR_TYPE_DEBUG,
1719 "Sending %u byte HELLO beacon\n",
1720 (unsigned int) size);
1721 radioHeader = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage*) buf;
1722 get_radiotap_header (NULL, radioHeader, size);
1723 LOG (GNUNET_ERROR_TYPE_DEBUG,
1724 "Broadcasting %u bytes of data to MAC `%s'\n",
1725 (unsigned int) size,
1726 mac_to_string (&bc_all_mac));
1727 get_wlan_header (plugin, &radioHeader->frame, &bc_all_mac, size);
1728 GNUNET_memcpy (&radioHeader[1], hello, hello_size);
1730 GNUNET_HELPER_send (plugin->suid_helper,
1731 &radioHeader->header,
1732 GNUNET_YES /* can drop */,
1734 GNUNET_STATISTICS_update (plugin->env->stats,
1735 _("# HELLO beacons sent"),
1738 plugin->beacon_task =
1739 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1740 (HELLO_BEACON_SCALING_FACTOR,
1741 plugin->mac_count + 1),
1749 * Function used for to process the data from the suid process
1751 * @param cls the plugin handle
1752 * @param hdr header of the GNUNET_MessageHeader
1755 handle_helper_message (void *cls,
1756 const struct GNUNET_MessageHeader *hdr)
1758 struct Plugin *plugin = cls;
1759 struct GNUNET_HELLO_Address *my_address;
1760 const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rxinfo;
1761 const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *cm;
1762 struct WlanAddress wa;
1763 struct MacAndSession mas;
1765 struct FragmentMessage *fm;
1766 struct MacEndpoint *endpoint;
1768 msize = ntohs (hdr->size);
1769 switch (ntohs (hdr->type))
1771 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
1772 if (msize != sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage))
1777 cm = (const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *) hdr;
1778 if (GNUNET_YES == plugin->have_mac)
1780 if (0 == memcmp (&plugin->mac_address,
1782 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1783 break; /* no change */
1784 /* remove old address */
1785 memset (&wa, 0, sizeof (struct WlanAddress));
1786 wa.mac = plugin->mac_address;
1787 wa.options = htonl(plugin->options);
1788 my_address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1791 GNUNET_HELLO_ADDRESS_INFO_NONE);
1792 plugin->env->notify_address (plugin->env->cls,
1795 GNUNET_HELLO_address_free (my_address);
1796 plugin->mac_address = cm->mac;
1800 plugin->mac_address = cm->mac;
1801 plugin->have_mac = GNUNET_YES;
1802 for (endpoint = plugin->mac_head; NULL != endpoint; endpoint = endpoint->next)
1804 for (fm = endpoint->sending_messages_head; NULL != fm; fm = fm->next)
1806 if (NULL != fm->fragcontext)
1808 GNUNET_break (0); /* should not happen */
1812 GNUNET_FRAGMENT_context_create (plugin->env->stats,
1815 fm->macendpoint->msg_delay,
1816 fm->macendpoint->ack_delay,
1818 &transmit_fragment, fm);
1819 GNUNET_free (fm->msg);
1823 GNUNET_break (NULL == plugin->beacon_task);
1824 plugin->beacon_task = GNUNET_SCHEDULER_add_now (&send_hello_beacon,
1829 memset (&wa, 0, sizeof (struct WlanAddress));
1830 wa.mac = plugin->mac_address;
1831 wa.options = htonl(plugin->options);
1832 my_address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1835 GNUNET_HELLO_ADDRESS_INFO_NONE);
1837 LOG (GNUNET_ERROR_TYPE_DEBUG,
1838 "Received WLAN_HELPER_CONTROL message with MAC address `%s' for peer `%s'\n",
1839 mac_to_string (&cm->mac),
1840 GNUNET_i2s (plugin->env->my_identity));
1841 plugin->env->notify_address (plugin->env->cls,
1844 GNUNET_HELLO_address_free (my_address);
1846 case GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER:
1847 LOG (GNUNET_ERROR_TYPE_DEBUG,
1848 "Got data message from helper with %u bytes\n",
1850 GNUNET_STATISTICS_update (plugin->env->stats,
1851 _("# DATA messages received"), 1,
1853 if (msize < sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage))
1855 LOG (GNUNET_ERROR_TYPE_DEBUG,
1856 "Size of packet is too small (%u bytes < %u)\n",
1857 msize, sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage));
1860 rxinfo = (const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) hdr;
1862 /* check if message is actually for us */
1863 if (0 != memcmp (&rxinfo->frame.addr3, &mac_bssid_gnunet,
1864 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1866 /* Not the GNUnet BSSID */
1869 if ( (0 != memcmp (&rxinfo->frame.addr1, &bc_all_mac,
1870 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))) &&
1871 (0 != memcmp (&rxinfo->frame.addr1, &plugin->mac_address,
1872 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))) )
1874 /* Neither broadcast nor specifically for us */
1877 if (0 == memcmp (&rxinfo->frame.addr2, &plugin->mac_address,
1878 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1880 /* packet is FROM us, thus not FOR us */
1884 GNUNET_STATISTICS_update (plugin->env->stats,
1885 _("# DATA messages processed"),
1887 LOG (GNUNET_ERROR_TYPE_DEBUG,
1888 "Receiving %u bytes of data from MAC `%s'\n",
1889 (unsigned int) (msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)),
1890 mac_to_string (&rxinfo->frame.addr2));
1891 LOG (GNUNET_ERROR_TYPE_DEBUG,
1892 "Receiving %u bytes of data to MAC `%s'\n",
1893 (unsigned int) (msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)),
1894 mac_to_string (&rxinfo->frame.addr1));
1895 LOG (GNUNET_ERROR_TYPE_DEBUG,
1896 "Receiving %u bytes of data with BSSID MAC `%s'\n",
1897 (unsigned int) (msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)),
1898 mac_to_string (&rxinfo->frame.addr3));
1899 wa.mac = rxinfo->frame.addr2;
1900 wa.options = htonl (0);
1901 mas.endpoint = create_macendpoint (plugin, &wa);
1903 (void) GNUNET_SERVER_mst_receive (plugin->helper_payload_tokenizer,
1905 (const char*) &rxinfo[1],
1906 msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage),
1907 GNUNET_YES, GNUNET_NO);
1911 LOG (GNUNET_ERROR_TYPE_ERROR,
1912 "Unexpected message of type %u (%u bytes)",
1922 * Another peer has suggested an address for this
1923 * peer and transport plugin. Check that this could be a valid
1924 * address. If so, consider adding it to the list
1927 * @param cls closure
1928 * @param addr pointer to the address
1929 * @param addrlen length of @a addr
1930 * @return #GNUNET_OK if this is a plausible address for this peer
1934 wlan_plugin_address_suggested (void *cls,
1938 struct Plugin *plugin = cls;
1939 struct WlanAddress *wa = (struct WlanAddress *) addr;
1941 if (addrlen != sizeof (struct WlanAddress))
1943 GNUNET_break_op (0);
1944 return GNUNET_SYSERR;
1946 if (GNUNET_YES != plugin->have_mac)
1948 LOG (GNUNET_ERROR_TYPE_DEBUG,
1949 "Rejecting MAC `%s': I don't know my MAC!\n",
1950 mac_to_string (addr));
1951 return GNUNET_NO; /* don't know my MAC */
1953 if (0 != memcmp (&wa->mac,
1954 &plugin->mac_address,
1957 LOG (GNUNET_ERROR_TYPE_DEBUG,
1958 "Rejecting MAC `%s': not my MAC!\n",
1959 mac_to_string (addr));
1960 return GNUNET_NO; /* not my MAC */
1967 * Convert the transports address to a nice, human-readable format.
1969 * @param cls closure
1970 * @param type name of the transport that generated the address
1971 * @param addr one of the addresses of the host, NULL for the last address
1972 * the specific address format depends on the transport
1973 * @param addrlen length of the address
1974 * @param numeric should (IP) addresses be displayed in numeric form?
1975 * @param timeout after how long should we give up?
1976 * @param asc function to call on each string
1977 * @param asc_cls closure for @a asc
1980 wlan_plugin_address_pretty_printer (void *cls,
1985 struct GNUNET_TIME_Relative timeout,
1986 GNUNET_TRANSPORT_AddressStringCallback asc,
1991 if (sizeof (struct WlanAddress) == addrlen)
1992 ret = wlan_plugin_address_to_string (NULL,
1999 (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
2000 asc (asc_cls, NULL, GNUNET_OK);
2005 * Exit point from the plugin.
2007 * @param cls pointer to the api struct
2010 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2012 struct WlanAddress wa;
2013 struct GNUNET_HELLO_Address *address;
2014 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2015 struct Plugin *plugin = api->cls;
2016 struct MacEndpoint *endpoint;
2017 struct MacEndpoint *endpoint_next;
2024 if (GNUNET_YES == plugin->have_mac)
2026 memset (&wa, 0, sizeof(wa));
2027 wa.options = htonl (plugin->options);
2028 wa.mac = plugin->mac_address;
2029 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2031 &wa, sizeof (struct WlanAddress),
2032 GNUNET_HELLO_ADDRESS_INFO_NONE);
2034 plugin->env->notify_address (plugin->env->cls,
2037 plugin->have_mac = GNUNET_NO;
2038 GNUNET_HELLO_address_free (address);
2041 if (NULL != plugin->beacon_task)
2043 GNUNET_SCHEDULER_cancel (plugin->beacon_task);
2044 plugin->beacon_task = NULL;
2046 if (NULL != plugin->suid_helper)
2048 GNUNET_HELPER_stop (plugin->suid_helper,
2050 plugin->suid_helper = NULL;
2052 endpoint_next = plugin->mac_head;
2053 while (NULL != (endpoint = endpoint_next))
2055 endpoint_next = endpoint->next;
2056 free_macendpoint (endpoint);
2058 if (NULL != plugin->fragment_data_tokenizer)
2060 GNUNET_SERVER_mst_destroy (plugin->fragment_data_tokenizer);
2061 plugin->fragment_data_tokenizer = NULL;
2063 if (NULL != plugin->wlan_header_payload_tokenizer)
2065 GNUNET_SERVER_mst_destroy (plugin->wlan_header_payload_tokenizer);
2066 plugin->wlan_header_payload_tokenizer = NULL;
2068 if (NULL != plugin->helper_payload_tokenizer)
2070 GNUNET_SERVER_mst_destroy (plugin->helper_payload_tokenizer);
2071 plugin->helper_payload_tokenizer = NULL;
2073 GNUNET_free_non_null (plugin->wlan_interface);
2074 GNUNET_free (plugin);
2081 * Function called to convert a string address to
2084 * @param cls closure (`struct Plugin *`)
2085 * @param addr string address
2086 * @param addrlen length of the address
2087 * @param buf location to store the buffer
2088 * @param added location to store the number of bytes in the buffer.
2089 * If the function returns #GNUNET_SYSERR, its contents are undefined.
2090 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2093 wlan_plugin_string_to_address (void *cls,
2099 struct WlanAddress *wa;
2105 if ((NULL == addr) || (0 == addrlen))
2108 return GNUNET_SYSERR;
2110 if ('\0' != addr[addrlen - 1])
2113 return GNUNET_SYSERR;
2115 if (strlen (addr) != addrlen - 1)
2118 return GNUNET_SYSERR;
2121 if (8 != SSCANF (addr,
2122 "%4s.%u.%X:%X:%X:%X:%X:%X",
2124 &a[0], &a[1], &a[2],
2125 &a[3], &a[4], &a[5]))
2128 return GNUNET_SYSERR;
2130 wa = GNUNET_new (struct WlanAddress);
2132 wa->mac.mac[i] = a[i];
2133 wa->options = htonl (0);
2135 *added = sizeof (struct WlanAddress);
2141 * Begin monitoring sessions of a plugin. There can only
2142 * be one active monitor per plugin (i.e. if there are
2143 * multiple monitors, the transport service needs to
2144 * multiplex the generated events over all of them).
2146 * @param cls closure of the plugin
2147 * @param sic callback to invoke, NULL to disable monitor;
2148 * plugin will being by iterating over all active
2149 * sessions immediately and then enter monitor mode
2150 * @param sic_cls closure for @a sic
2153 wlan_plugin_setup_monitor (void *cls,
2154 GNUNET_TRANSPORT_SessionInfoCallback sic,
2157 struct Plugin *plugin = cls;
2158 struct MacEndpoint *mac;
2159 struct GNUNET_ATS_Session *session;
2162 plugin->sic_cls = sic_cls;
2165 for (mac = plugin->mac_head; NULL != mac; mac = mac->next)
2166 for (session = mac->sessions_head; NULL != session; session = session->next)
2168 notify_session_monitor (plugin,
2170 GNUNET_TRANSPORT_SS_INIT);
2171 notify_session_monitor (plugin,
2173 GNUNET_TRANSPORT_SS_UP);
2175 sic (sic_cls, NULL, NULL);
2182 * Function that will be called whenever the transport service wants to
2183 * notify the plugin that a session is still active and in use and
2184 * therefore the session timeout for this session has to be updated
2186 * @param cls closure
2187 * @param peer which peer was the session for
2188 * @param session which session is being updated
2191 wlan_plugin_update_session_timeout (void *cls,
2192 const struct GNUNET_PeerIdentity *peer,
2193 struct GNUNET_ATS_Session *session)
2195 GNUNET_assert (NULL != session->timeout_task);
2196 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2201 * Function that will be called whenever the transport service wants to
2202 * notify the plugin that the inbound quota changed and that the plugin
2203 * should update it's delay for the next receive value
2205 * @param cls closure
2206 * @param peer which peer was the session for
2207 * @param session which session is being updated
2208 * @param delay new delay to use for receiving
2211 wlan_plugin_update_inbound_delay (void *cls,
2212 const struct GNUNET_PeerIdentity *peer,
2213 struct GNUNET_ATS_Session *session,
2214 struct GNUNET_TIME_Relative delay)
2216 /* does nothing, as inbound delay is not supported by WLAN */
2221 * Entry point for the plugin.
2223 * @param cls closure, the `struct GNUNET_TRANSPORT_PluginEnvironment *`
2224 * @return the `struct GNUNET_TRANSPORT_PluginFunctions *` or NULL on error
2227 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2229 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2230 struct GNUNET_TRANSPORT_PluginFunctions *api;
2231 struct Plugin *plugin;
2232 char *wlan_interface;
2233 unsigned long long testmode;
2236 /* check for 'special' mode */
2237 if (NULL == env->receive)
2239 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2240 initialze the plugin or the API */
2241 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2243 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2244 api->address_to_string = &wlan_plugin_address_to_string;
2245 api->string_to_address = &wlan_plugin_string_to_address;
2250 /* check configuration */
2252 GNUNET_CONFIGURATION_have_value (env->cfg,
2256 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2262 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2267 binary = GNUNET_OS_get_libexec_binary_path (HELPER_NAME);
2268 if ( (0 == testmode) &&
2270 GNUNET_OS_check_helper_binary (binary,
2274 LOG (GNUNET_ERROR_TYPE_ERROR,
2275 _("Helper binary `%s' not SUID, cannot run WLAN transport\n"),
2277 GNUNET_free (binary);
2280 GNUNET_free (binary);
2282 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2287 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2293 plugin = GNUNET_new (struct Plugin);
2294 plugin->wlan_interface = wlan_interface;
2296 GNUNET_STATISTICS_set (plugin->env->stats,
2297 _("# sessions allocated"),
2299 GNUNET_STATISTICS_set (plugin->env->stats,
2300 _("# MAC endpoints allocated"),
2302 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker, NULL, NULL,
2303 GNUNET_BANDWIDTH_value_init (100 * 1024 *
2306 plugin->fragment_data_tokenizer = GNUNET_SERVER_mst_create (&process_data,
2308 plugin->wlan_header_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data,
2310 plugin->helper_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data,
2313 plugin->options = 0;
2315 /* some compilers do not like switch on 'long long'... */
2316 switch ((unsigned int) testmode)
2318 case 0: /* normal */
2319 plugin->helper_argv[0] = (char *) HELPER_NAME;
2320 plugin->helper_argv[1] = wlan_interface;
2321 plugin->helper_argv[2] = NULL;
2322 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
2324 plugin->helper_argv,
2325 &handle_helper_message,
2329 case 1: /* testmode, peer 1 */
2330 plugin->helper_argv[0] = (char *) DUMMY_HELPER_NAME;
2331 plugin->helper_argv[1] = (char *) "1";
2332 plugin->helper_argv[2] = NULL;
2333 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
2335 plugin->helper_argv,
2336 &handle_helper_message,
2340 case 2: /* testmode, peer 2 */
2341 plugin->helper_argv[0] = (char *) DUMMY_HELPER_NAME;
2342 plugin->helper_argv[1] = (char *) "2";
2343 plugin->helper_argv[2] = NULL;
2344 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
2346 plugin->helper_argv,
2347 &handle_helper_message,
2355 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2357 api->send = &wlan_plugin_send;
2358 api->get_session = &wlan_plugin_get_session;
2359 api->disconnect_peer = &wlan_plugin_disconnect_peer;
2360 api->disconnect_session = &wlan_plugin_disconnect_session;
2361 api->query_keepalive_factor = &wlan_plugin_query_keepalive_factor;
2362 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2363 api->check_address = &wlan_plugin_address_suggested;
2364 api->address_to_string = &wlan_plugin_address_to_string;
2365 api->string_to_address = &wlan_plugin_string_to_address;
2366 api->get_network = &wlan_plugin_get_network;
2367 api->get_network_for_address = &wlan_plugin_get_network_for_address;
2368 api->update_session_timeout = &wlan_plugin_update_session_timeout;
2369 api->update_inbound_delay = &wlan_plugin_update_inbound_delay;
2370 api->setup_monitor = &wlan_plugin_setup_monitor;
2375 /* end of plugin_transport_wlan.c */