2 This file is part of GNUnet
3 (C) 2010-2014 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/plugin_transport_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"
43 #define PLUGIN_NAME "wlan"
44 #define CONFIG_NAME "transport-wlan"
45 #define HELPER_NAME "gnunet-helper-transport-wlan"
46 #define DUMMY_HELPER_NAME "gnunet-helper-transport-wlan-dummy"
47 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_wlan_init
48 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_wlan_done
49 #define LOG(kind,...) GNUNET_log_from (kind, "transport-wlan",__VA_ARGS__)
52 * time out of a mac endpoint
54 #define MACENDPOINT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 2)
57 * We reduce the frequence of HELLO beacons in relation to
58 * the number of MAC addresses currently visible to us.
59 * This is the multiplication factor.
61 #define HELLO_BEACON_SCALING_FACTOR GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
66 /* begin case bluetooth */
68 #define PLUGIN_NAME "bluetooth"
69 #define CONFIG_NAME "transport-bluetooth"
70 #define HELPER_NAME "gnunet-helper-transport-bluetooth"
71 /* yes, this is correct, we use the same dummy driver as 'wlan' */
72 #define DUMMY_HELPER_NAME "gnunet-helper-transport-wlan-dummy"
73 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_bluetooth_init
74 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_bluetooth_done
75 #define LOG(kind,...) GNUNET_log_from (kind, "transport-bluetooth",__VA_ARGS__)
78 * time out of a mac endpoint
80 #define MACENDPOINT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 60)
84 * We reduce the frequence of HELLO beacons in relation to
85 * the number of MAC addresses currently visible to us.
86 * This is the multiplication factor.
88 #define HELLO_BEACON_SCALING_FACTOR GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
90 /* end case bluetooth */
92 #error need to build wlan or bluetooth
96 * Max size of packet (that we give to the WLAN driver for transmission)
103 * Maximum number of messages in defragmentation queue per MAC
105 #define MESSAGES_IN_DEFRAG_QUEUE_PER_MAC 2
108 * Link layer control fields for better compatibility
109 * (i.e. GNUnet over WLAN is not IP-over-WLAN).
111 #define WLAN_LLC_DSAP_FIELD 0x1f
112 #define WLAN_LLC_SSAP_FIELD 0x1f
115 GNUNET_NETWORK_STRUCT_BEGIN
117 * Header for messages which need fragmentation. This is the format of
118 * a message we obtain AFTER defragmentation. We then need to check
119 * the CRC and then tokenize the payload and pass it to the
120 * 'receive' callback.
126 * Message type is #GNUNET_MESSAGE_TYPE_WLAN_DATA.
128 struct GNUNET_MessageHeader header;
131 * CRC32 checksum (only over the payload), in NBO.
133 uint32_t crc GNUNET_PACKED;
136 * Sender of the message.
138 struct GNUNET_PeerIdentity sender;
141 * Target of the message.
143 struct GNUNET_PeerIdentity target;
145 /* followed by payload, possibly including
146 multiple messages! */
152 * Address format for WLAN.
157 * Options set for the WLAN, in NBO.
159 uint32_t options GNUNET_PACKED;
162 * WLAN addresses using MACs.
164 struct GNUNET_TRANSPORT_WLAN_MacAddress mac;
168 GNUNET_NETWORK_STRUCT_END
172 * Information kept for each message that is yet to be fragmented and
175 struct PendingMessage
178 * next entry in the DLL
180 struct PendingMessage *next;
183 * previous entry in the DLL
185 struct PendingMessage *prev;
188 * The pending message
190 struct WlanHeader *msg;
193 * Continuation function to call once the message
194 * has been sent. Can be NULL if there is no
195 * continuation to call.
197 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
200 * Cls for @e transmit_cont
202 void *transmit_cont_cls;
205 * Timeout task (for this message).
207 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
213 * Session handle for connections with other peers.
218 * To whom are we talking to (set to our identity
219 * if we are still waiting for the welcome message)
221 struct GNUNET_PeerIdentity target;
224 * We keep all sessions in a DLL at their respective
225 * `struct MACEndpoint *`.
227 struct Session *next;
230 * We keep all sessions in a DLL at their respective
231 * `struct MACEndpoint *`.
233 struct Session *prev;
236 * MAC endpoint with the address of this peer.
238 struct MacEndpoint *mac;
241 * Address associated with this session and MAC endpoint
243 struct GNUNET_HELLO_Address *address;
246 * When should this session time out?
248 struct GNUNET_TIME_Absolute timeout;
251 * Timeout task (for the session).
253 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
259 * Struct for messages that are being fragmented in a MAC's transmission queue.
261 struct FragmentMessage
265 * This is a doubly-linked list.
267 struct FragmentMessage *next;
270 * This is a doubly-linked list.
272 struct FragmentMessage *prev;
275 * MAC endpoint this message belongs to
277 struct MacEndpoint *macendpoint;
280 * Fragmentation context
282 struct GNUNET_FRAGMENT_Context *fragcontext;
285 * Transmission handle to helper (to cancel if the frag context
286 * is destroyed early for some reason).
288 struct GNUNET_HELPER_SendHandle *sh;
291 * Intended recipient.
293 struct GNUNET_PeerIdentity target;
296 * Timeout value for the message.
298 struct GNUNET_TIME_Absolute timeout;
303 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
306 * Continuation to call when we're done with this message.
308 GNUNET_TRANSPORT_TransmitContinuation cont;
311 * Closure for @e cont
316 * Size of original message
321 * Number of bytes used to transmit message
329 * Struct to represent one network card connection
335 * We keep all MACs in a DLL in the plugin.
337 struct MacEndpoint *next;
340 * We keep all MACs in a DLL in the plugin.
342 struct MacEndpoint *prev;
345 * Pointer to the global plugin struct.
347 struct Plugin *plugin;
350 * Head of sessions that use this MAC.
352 struct Session *sessions_head;
355 * Tail of sessions that use this MAC.
357 struct Session *sessions_tail;
360 * Head of messages we are currently sending to this MAC.
362 struct FragmentMessage *sending_messages_head;
365 * Tail of messages we are currently sending to this MAC.
367 struct FragmentMessage *sending_messages_tail;
370 * Defrag context for this MAC
372 struct GNUNET_DEFRAGMENT_Context *defrag;
375 * When should this endpoint time out?
377 struct GNUNET_TIME_Absolute timeout;
382 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
385 * count of messages in the fragment out queue for this mac endpoint
387 unsigned int fragment_messages_out_count;
392 struct WlanAddress wlan_addr;
395 * Message delay for fragmentation context
397 struct GNUNET_TIME_Relative msg_delay;
400 * ACK delay for fragmentation context
402 struct GNUNET_TIME_Relative ack_delay;
405 * Desired transmission power for this MAC
410 * Desired transmission rate for this MAC
415 * Antenna we should use for this MAC
423 * Encapsulation of all of the state of the plugin.
430 struct GNUNET_TRANSPORT_PluginEnvironment *env;
433 * Handle to helper process for priviledged operations.
435 struct GNUNET_HELPER_Handle *suid_helper;
438 * Function to call about session status changes.
440 GNUNET_TRANSPORT_SessionInfoCallback sic;
443 * Closure for @e sic.
448 * ARGV-vector for the helper (all helpers take only the binary
449 * name, one actual argument, plus the NULL terminator for 'argv').
451 char *helper_argv[3];
454 * The interface of the wlan card given to us by the user.
456 char *wlan_interface;
459 * Tokenizer for demultiplexing of data packets resulting from
462 struct GNUNET_SERVER_MessageStreamTokenizer *fragment_data_tokenizer;
465 * Tokenizer for demultiplexing of data packets received from the suid helper
467 struct GNUNET_SERVER_MessageStreamTokenizer *helper_payload_tokenizer;
470 * Tokenizer for demultiplexing of data packets that follow the WLAN Header
472 struct GNUNET_SERVER_MessageStreamTokenizer *wlan_header_payload_tokenizer;
475 * Head of list of open connections.
477 struct MacEndpoint *mac_head;
480 * Tail of list of open connections.
482 struct MacEndpoint *mac_tail;
485 * Task that periodically sends a HELLO beacon via the helper.
487 GNUNET_SCHEDULER_TaskIdentifier beacon_task;
490 * Tracker for bandwidth limit
492 struct GNUNET_BANDWIDTH_Tracker tracker;
495 * The mac_address of the wlan card given to us by the helper.
497 struct GNUNET_TRANSPORT_WLAN_MacAddress mac_address;
500 * Have we received a control message with our MAC address yet?
505 * Number of connections
507 unsigned int mac_count;
510 * Options for addresses
518 * Information associated with a message. Can contain
519 * the session or the MAC endpoint associated with the
525 * NULL if the identity of the other peer is not known.
527 struct Session *session;
530 * MAC address of the other peer, NULL if not known.
532 struct MacEndpoint *endpoint;
537 * Print MAC addresses nicely.
539 * @param mac the mac address
540 * @return string to a static buffer with
541 * the human-readable mac, will be overwritten during the next call to
545 mac_to_string (const struct GNUNET_TRANSPORT_WLAN_MacAddress * mac)
547 static char macstr[20];
549 GNUNET_snprintf (macstr,
551 "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
552 mac->mac[0], mac->mac[1],
553 mac->mac[2], mac->mac[3],
554 mac->mac[4], mac->mac[5]);
560 * Function called for a quick conversion of the binary address to
561 * a numeric address. Note that the caller must not free the
562 * address and that the next call to this function is allowed
563 * to override the address again.
566 * @param addr binary address
567 * @param addrlen length of the address
568 * @return string representing the same address
571 wlan_plugin_address_to_string (void *cls,
575 const struct GNUNET_TRANSPORT_WLAN_MacAddress *mac;
576 static char macstr[36];
578 if (sizeof (struct WlanAddress) != addrlen)
583 mac = &((struct WlanAddress *) addr)->mac;
584 GNUNET_snprintf (macstr,
588 ntohl (((struct WlanAddress *) addr)->options),
589 mac_to_string (mac));
595 * If a session monitor is attached, notify it about the new
598 * @param plugin our plugin
599 * @param session session that changed state
600 * @param state new state of the session
603 notify_session_monitor (struct Plugin *plugin,
604 struct Session *session,
605 enum GNUNET_TRANSPORT_SessionState state)
607 struct GNUNET_TRANSPORT_SessionInfo info;
609 if (NULL == plugin->sic)
611 memset (&info, 0, sizeof (info));
613 info.is_inbound = GNUNET_SYSERR; /* hard to say */
614 info.num_msg_pending = 0; /* we queue per MAC, not per peer */
615 info.num_bytes_pending = 0; /* we queue per MAC, not per peer */
616 info.receive_delay = GNUNET_TIME_UNIT_ZERO_ABS; /* not supported by WLAN */
617 info.session_timeout = session->timeout;
618 info.address = session->address;
619 plugin->sic (plugin->sic_cls,
626 * Fill the radiotap header
628 * @param endpoint pointer to the endpoint, can be NULL
629 * @param header pointer to the radiotap header
630 * @param size total message size
633 get_radiotap_header (struct MacEndpoint *endpoint,
634 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *header,
637 header->header.type = ntohs (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER);
638 header->header.size = ntohs (size);
639 if (NULL != endpoint)
641 header->rate = endpoint->rate;
642 header->tx_power = endpoint->tx_power;
643 header->antenna = endpoint->antenna;
648 header->tx_power = 0;
655 * Generate the WLAN hardware header for one packet
657 * @param plugin the plugin handle
658 * @param header address to write the header to
659 * @param to_mac_addr address of the recipient
660 * @param size size of the whole packet, needed to calculate the time to send the packet
663 get_wlan_header (struct Plugin *plugin,
664 struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *header,
665 const struct GNUNET_TRANSPORT_WLAN_MacAddress *to_mac_addr,
668 const int rate = 11000000;
670 header->frame_control = htons (IEEE80211_FC0_TYPE_DATA);
671 header->addr1 = *to_mac_addr;
672 header->addr2 = plugin->mac_address;
673 header->addr3 = mac_bssid_gnunet;
674 header->duration = GNUNET_htole16 ((size * 1000000) / rate + 290);
675 header->sequence_control = 0; // FIXME?
676 header->llc[0] = WLAN_LLC_DSAP_FIELD;
677 header->llc[1] = WLAN_LLC_SSAP_FIELD;
678 header->llc[2] = 0; // FIXME?
679 header->llc[3] = 0; // FIXME?
684 * Send an ACK for a fragment we received.
686 * @param cls the `struct MacEndpoint *` the ACK must be sent to
687 * @param msg_id id of the message
688 * @param hdr pointer to the hdr where the ack is stored
691 send_ack (void *cls, uint32_t msg_id,
692 const struct GNUNET_MessageHeader *hdr)
694 struct MacEndpoint *endpoint = cls;
695 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage* radio_header;
696 uint16_t msize = ntohs (hdr->size);
697 size_t size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + msize;
700 if (NULL == endpoint)
705 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
710 LOG (GNUNET_ERROR_TYPE_DEBUG,
711 "Sending ACK to helper\n");
712 radio_header = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) buf;
713 get_radiotap_header (endpoint, radio_header, size);
714 get_wlan_header (endpoint->plugin,
715 &radio_header->frame,
716 &endpoint->wlan_addr.mac,
717 sizeof (endpoint->wlan_addr.mac));
718 memcpy (&radio_header[1], hdr, msize);
720 GNUNET_HELPER_send (endpoint->plugin->suid_helper,
721 &radio_header->header,
722 GNUNET_NO /* dropping ACKs is bad */,
724 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
731 * Handles the data after all fragments are put together
733 * @param cls macendpoint this messages belongs to
734 * @param hdr pointer to the data
737 wlan_data_message_handler (void *cls,
738 const struct GNUNET_MessageHeader *hdr)
740 struct MacEndpoint *endpoint = cls;
741 struct Plugin *plugin = endpoint->plugin;
742 struct MacAndSession mas;
744 GNUNET_STATISTICS_update (plugin->env->stats,
745 _("# Messages defragmented"),
749 mas.endpoint = endpoint;
750 (void) GNUNET_SERVER_mst_receive (plugin->fragment_data_tokenizer,
754 GNUNET_YES, GNUNET_NO);
761 * @param cls our `struct Plugin`.
762 * @param session the session free
765 wlan_plugin_disconnect_session (void *cls,
766 struct Session *session)
768 struct MacEndpoint *endpoint = session->mac;
769 struct Plugin *plugin = endpoint->plugin;
771 plugin->env->session_end (plugin->env->cls,
774 notify_session_monitor (plugin,
776 GNUNET_TRANSPORT_SS_DONE);
777 GNUNET_CONTAINER_DLL_remove (endpoint->sessions_head,
778 endpoint->sessions_tail,
780 if (session->timeout_task != GNUNET_SCHEDULER_NO_TASK)
782 GNUNET_SCHEDULER_cancel (session->timeout_task);
783 session->timeout_task = GNUNET_SCHEDULER_NO_TASK;
785 GNUNET_STATISTICS_update (plugin->env->stats,
786 _("# Sessions allocated"),
789 GNUNET_HELLO_address_free (session->address);
790 GNUNET_free (session);
796 * Function that is called to get the keepalive factor.
797 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
798 * calculate the interval between keepalive packets.
800 * @param cls closure with the `struct Plugin`
801 * @return keepalive factor
804 wlan_plugin_query_keepalive_factor (void *cls)
811 * A session is timing out. Clean up.
813 * @param cls pointer to the Session
817 session_timeout (void *cls,
818 const struct GNUNET_SCHEDULER_TaskContext *tc)
820 struct Session *session = cls;
821 struct GNUNET_TIME_Relative left;
823 session->timeout_task = GNUNET_SCHEDULER_NO_TASK;
824 left = GNUNET_TIME_absolute_get_remaining (session->timeout);
825 if (0 != left.rel_value_us)
827 session->timeout_task =
828 GNUNET_SCHEDULER_add_delayed (left,
833 wlan_plugin_disconnect_session (session->mac->plugin,
840 * Lookup a new session
842 * @param endpoint pointer to the mac endpoint of the peer
843 * @param peer peer identity to use for this session
844 * @return returns the session or NULL
846 static struct Session *
847 lookup_session (struct MacEndpoint *endpoint,
848 const struct GNUNET_PeerIdentity *peer)
850 struct Session *session;
852 for (session = endpoint->sessions_head; NULL != session; session = session->next)
853 if (0 == memcmp (peer, &session->target, sizeof (struct GNUNET_PeerIdentity)))
860 * Create a new session
862 * @param endpoint pointer to the mac endpoint of the peer
863 * @param peer peer identity to use for this session
864 * @return returns the session or NULL
866 static struct Session *
867 create_session (struct MacEndpoint *endpoint,
868 const struct GNUNET_PeerIdentity *peer)
870 struct Session *session;
872 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
873 _("# Sessions allocated"),
876 session = GNUNET_new (struct Session);
877 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
878 endpoint->sessions_tail,
880 session->address = GNUNET_HELLO_address_allocate (peer,
882 &endpoint->wlan_addr,
883 sizeof (endpoint->wlan_addr),
884 GNUNET_HELLO_ADDRESS_INFO_NONE);
885 session->mac = endpoint;
886 session->target = *peer;
887 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
888 session->timeout_task =
889 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &session_timeout,
891 notify_session_monitor (endpoint->plugin,
893 GNUNET_TRANSPORT_SS_INIT);
894 notify_session_monitor (endpoint->plugin,
896 GNUNET_TRANSPORT_SS_UP);
897 LOG (GNUNET_ERROR_TYPE_DEBUG,
898 "Created new session %p for peer `%s' with endpoint %s\n",
901 mac_to_string (&endpoint->wlan_addr.mac));
908 * Look up a session for a peer and create a new session if none is found
910 * @param endpoint pointer to the mac endpoint of the peer
911 * @param peer peer identity to use for this session
912 * @return returns the session
914 static struct Session *
915 get_session (struct MacEndpoint *endpoint,
916 const struct GNUNET_PeerIdentity *peer)
918 struct Session *session;
920 if (NULL != (session = lookup_session (endpoint, peer)))
922 return create_session (endpoint, peer);
927 * Function called once we have successfully given the fragment
928 * message to the SUID helper process and we are thus ready for
931 * @param cls the `struct FragmentMessage *`
932 * @param result result of the operation (#GNUNET_OK on success,
933 * #GNUNET_NO if the helper died, #GNUNET_SYSERR
934 * if the helper was stopped)
937 fragment_transmission_done (void *cls,
940 struct FragmentMessage *fm = cls;
944 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
949 * Transmit a fragment of a message.
951 * @param cls `struct FragmentMessage *` this fragment message belongs to
952 * @param hdr pointer to the start of the fragment message
955 transmit_fragment (void *cls,
956 const struct GNUNET_MessageHeader *hdr)
958 struct FragmentMessage *fm = cls;
959 struct MacEndpoint *endpoint = fm->macendpoint;
963 if (NULL == endpoint)
968 msize = ntohs (hdr->size);
969 size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + msize;
972 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radio_header;
974 radio_header = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) buf;
975 get_radiotap_header (endpoint, radio_header, size);
976 get_wlan_header (endpoint->plugin,
977 &radio_header->frame,
978 &endpoint->wlan_addr.mac,
979 sizeof (endpoint->wlan_addr.mac));
980 memcpy (&radio_header[1], hdr, msize);
981 GNUNET_assert (NULL == fm->sh);
982 fm->sh = GNUNET_HELPER_send (endpoint->plugin->suid_helper,
983 &radio_header->header,
985 &fragment_transmission_done, fm);
986 fm->size_on_wire += size;
988 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
989 _("# message fragments sent"),
993 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
994 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
995 "# bytes currently in buffers",
997 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
998 "# bytes transmitted",
1005 * Frees the space of a message in the fragment queue (send queue)
1007 * @param fm message to free
1010 free_fragment_message (struct FragmentMessage *fm)
1012 struct MacEndpoint *endpoint = fm->macendpoint;
1014 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
1015 _("# messages pending (with fragmentation)"),
1017 GNUNET_CONTAINER_DLL_remove (endpoint->sending_messages_head,
1018 endpoint->sending_messages_tail,
1022 GNUNET_HELPER_send_cancel (fm->sh);
1025 GNUNET_FRAGMENT_context_destroy (fm->fragcontext,
1026 &endpoint->msg_delay,
1027 &endpoint->ack_delay);
1028 if (fm->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1030 GNUNET_SCHEDULER_cancel (fm->timeout_task);
1031 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1038 * A FragmentMessage has timed out. Remove it.
1040 * @param cls pointer to the 'struct FragmentMessage'
1044 fragmentmessage_timeout (void *cls,
1045 const struct GNUNET_SCHEDULER_TaskContext *tc)
1047 struct FragmentMessage *fm = cls;
1049 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1050 if (NULL != fm->cont)
1052 fm->cont (fm->cont_cls,
1059 free_fragment_message (fm);
1064 * Transmit a message to the given destination with fragmentation.
1066 * @param endpoint desired destination
1067 * @param timeout how long can the message wait?
1068 * @param target peer that should receive the message
1069 * @param msg message to transmit
1070 * @param payload_size bytes of payload
1071 * @param cont continuation to call once the message has
1072 * been transmitted (or if the transport is ready
1073 * for the next transmission call; or if the
1074 * peer disconnected...); can be NULL
1075 * @param cont_cls closure for @a cont
1078 send_with_fragmentation (struct MacEndpoint *endpoint,
1079 struct GNUNET_TIME_Relative timeout,
1080 const struct GNUNET_PeerIdentity *target,
1081 const struct GNUNET_MessageHeader *msg,
1082 size_t payload_size,
1083 GNUNET_TRANSPORT_TransmitContinuation cont,
1087 struct FragmentMessage *fm;
1088 struct Plugin *plugin;
1090 plugin = endpoint->plugin;
1091 fm = GNUNET_new (struct FragmentMessage);
1092 fm->macendpoint = endpoint;
1093 fm->target = *target;
1094 fm->size_payload = payload_size;
1095 fm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1097 fm->cont_cls = cont_cls;
1098 /* 1 MBit/s typical data rate, 1430 byte fragments => ~100 ms per message */
1100 GNUNET_FRAGMENT_context_create (plugin->env->stats,
1103 endpoint->msg_delay,
1104 endpoint->ack_delay,
1106 &transmit_fragment, fm);
1108 GNUNET_SCHEDULER_add_delayed (timeout,
1109 &fragmentmessage_timeout, fm);
1110 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sending_messages_head,
1111 endpoint->sending_messages_tail,
1117 * Free a MAC endpoint.
1119 * @param endpoint pointer to the MacEndpoint to free
1122 free_macendpoint (struct MacEndpoint *endpoint)
1124 struct Plugin *plugin = endpoint->plugin;
1125 struct FragmentMessage *fm;
1126 struct Session *session;
1128 GNUNET_STATISTICS_update (plugin->env->stats,
1129 _("# MAC endpoints allocated"),
1132 while (NULL != (session = endpoint->sessions_head))
1133 wlan_plugin_disconnect_session (plugin,
1135 while (NULL != (fm = endpoint->sending_messages_head))
1136 free_fragment_message (fm);
1137 GNUNET_CONTAINER_DLL_remove (plugin->mac_head,
1141 if (NULL != endpoint->defrag)
1143 GNUNET_DEFRAGMENT_context_destroy(endpoint->defrag);
1144 endpoint->defrag = NULL;
1147 plugin->mac_count--;
1148 if (GNUNET_SCHEDULER_NO_TASK != endpoint->timeout_task)
1150 GNUNET_SCHEDULER_cancel (endpoint->timeout_task);
1151 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1153 GNUNET_free (endpoint);
1158 * A MAC endpoint is timing out. Clean up.
1160 * @param cls pointer to the `struct MacEndpoint *`
1161 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
1164 macendpoint_timeout (void *cls,
1165 const struct GNUNET_SCHEDULER_TaskContext *tc)
1167 struct MacEndpoint *endpoint = cls;
1168 struct GNUNET_TIME_Relative timeout;
1170 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1171 timeout = GNUNET_TIME_absolute_get_remaining (endpoint->timeout);
1172 if (0 == timeout.rel_value_us)
1174 free_macendpoint (endpoint);
1177 endpoint->timeout_task =
1178 GNUNET_SCHEDULER_add_delayed (timeout,
1179 &macendpoint_timeout,
1185 * Find (or create) a MacEndpoint with a specific MAC address
1187 * @param plugin pointer to the plugin struct
1188 * @param mac the MAC address of the endpoint
1189 * @return handle to our data structure for this MAC
1191 static struct MacEndpoint *
1192 create_macendpoint (struct Plugin *plugin,
1193 struct WlanAddress *mac)
1195 struct MacEndpoint *pos;
1197 for (pos = plugin->mac_head; NULL != pos; pos = pos->next)
1198 if (0 == memcmp (mac, &pos->wlan_addr, sizeof (pos->wlan_addr)))
1200 pos = GNUNET_new (struct MacEndpoint);
1201 pos->wlan_addr = (*mac);
1202 pos->plugin = plugin;
1204 GNUNET_DEFRAGMENT_context_create (plugin->env->stats,
1206 MESSAGES_IN_DEFRAG_QUEUE_PER_MAC,
1208 &wlan_data_message_handler,
1211 pos->msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1212 pos->ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
1213 pos->timeout = GNUNET_TIME_relative_to_absolute (MACENDPOINT_TIMEOUT);
1215 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
1217 GNUNET_CONTAINER_DLL_insert (plugin->mac_head,
1220 plugin->mac_count++;
1221 GNUNET_STATISTICS_update (plugin->env->stats,
1222 _("# MAC endpoints allocated"),
1224 LOG (GNUNET_ERROR_TYPE_DEBUG,
1225 "New MAC endpoint `%s'\n",
1226 wlan_plugin_address_to_string (NULL,
1228 sizeof (struct WlanAddress)));
1234 * Function obtain the network type for a session
1236 * @param cls closure (`struct Plugin*`)
1237 * @param session the session
1238 * @return the network type in HBO or #GNUNET_SYSERR
1240 static enum GNUNET_ATS_Network_Type
1241 wlan_plugin_get_network (void *cls,
1242 struct Session *session)
1245 return GNUNET_ATS_NET_WLAN;
1247 return GNUNET_ATS_NET_BT;
1253 * Creates a new outbound session the transport service will use to
1254 * send data to the peer
1256 * @param cls the `struct Plugin *`
1257 * @param address the address
1258 * @return the session or NULL of max connections exceeded
1260 static struct Session *
1261 wlan_plugin_get_session (void *cls,
1262 const struct GNUNET_HELLO_Address *address)
1264 struct Plugin *plugin = cls;
1265 struct MacEndpoint *endpoint;
1267 if (NULL == address)
1269 if (sizeof (struct WlanAddress) != address->address_length)
1274 LOG (GNUNET_ERROR_TYPE_DEBUG,
1275 "Service asked to create session for peer `%s' with MAC `%s'\n",
1276 GNUNET_i2s (&address->peer),
1277 wlan_plugin_address_to_string (NULL,
1279 address->address_length));
1280 endpoint = create_macendpoint (plugin,
1281 (struct WlanAddress *) address->address);
1282 return get_session (endpoint, &address->peer);
1287 * Function that can be used to force the plugin to disconnect
1288 * from the given peer and cancel all previous transmissions
1289 * (and their continuation).
1291 * @param cls closure
1292 * @param target peer from which to disconnect
1295 wlan_plugin_disconnect_peer (void *cls,
1296 const struct GNUNET_PeerIdentity *target)
1298 struct Plugin *plugin = cls;
1299 struct Session *session;
1300 struct MacEndpoint *endpoint;
1302 for (endpoint = plugin->mac_head; NULL != endpoint; endpoint = endpoint->next)
1303 for (session = endpoint->sessions_head; NULL != session; session = session->next)
1304 if (0 == memcmp (target, &session->target,
1305 sizeof (struct GNUNET_PeerIdentity)))
1307 wlan_plugin_disconnect_session (plugin, session);
1308 break; /* inner-loop only (in case peer has another MAC as well!) */
1314 * Function that can be used by the transport service to transmit
1315 * a message using the plugin. Note that in the case of a
1316 * peer disconnecting, the continuation MUST be called
1317 * prior to the disconnect notification itself. This function
1318 * will be called with this peer's HELLO message to initiate
1319 * a fresh connection to another peer.
1321 * @param cls closure
1322 * @param session which session must be used
1323 * @param msgbuf the message to transmit
1324 * @param msgbuf_size number of bytes in @a msgbuf
1325 * @param priority how important is the message (most plugins will
1326 * ignore message priority and just FIFO)
1327 * @param to how long to wait at most for the transmission (does not
1328 * require plugins to discard the message after the timeout,
1329 * just advisory for the desired delay; most plugins will ignore
1331 * @param cont continuation to call once the message has
1332 * been transmitted (or if the transport is ready
1333 * for the next transmission call; or if the
1334 * peer disconnected...); can be NULL
1335 * @param cont_cls closure for @a cont
1336 * @return number of bytes used (on the physical network, with overheads);
1337 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1338 * and does NOT mean that the message was not transmitted (DV)
1341 wlan_plugin_send (void *cls,
1342 struct Session *session,
1343 const char *msgbuf, size_t msgbuf_size,
1344 unsigned int priority,
1345 struct GNUNET_TIME_Relative to,
1346 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1348 struct Plugin *plugin = cls;
1349 struct WlanHeader *wlanheader;
1350 size_t size = msgbuf_size + sizeof (struct WlanHeader);
1351 char buf[size] GNUNET_ALIGN;
1353 LOG (GNUNET_ERROR_TYPE_DEBUG,
1354 "Transmitting %u bytes of payload to peer `%s' (starting with %u byte message of type %u)\n",
1356 GNUNET_i2s (&session->target),
1357 (unsigned int) ntohs (((struct GNUNET_MessageHeader*)msgbuf)->size),
1358 (unsigned int) ntohs (((struct GNUNET_MessageHeader*)msgbuf)->type));
1359 wlanheader = (struct WlanHeader *) buf;
1360 wlanheader->header.size = htons (msgbuf_size + sizeof (struct WlanHeader));
1361 wlanheader->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA);
1362 wlanheader->sender = *plugin->env->my_identity;
1363 wlanheader->target = session->target;
1364 wlanheader->crc = htonl (GNUNET_CRYPTO_crc32_n (msgbuf, msgbuf_size));
1365 memcpy (&wlanheader[1], msgbuf, msgbuf_size);
1367 GNUNET_STATISTICS_update (plugin->env->stats,
1368 "# bytes currently in buffers",
1372 send_with_fragmentation (session->mac,
1375 &wlanheader->header,
1383 * We have received data from the WLAN via some session. Process depending
1384 * on the message type (HELLO, DATA, FRAGMENTATION or FRAGMENTATION-ACK).
1386 * @param cls pointer to the plugin
1387 * @param client pointer to the session this message belongs to
1388 * @param hdr start of the message
1391 process_data (void *cls,
1393 const struct GNUNET_MessageHeader *hdr)
1395 struct Plugin *plugin = cls;
1396 struct GNUNET_HELLO_Address *address;
1397 struct MacAndSession *mas = client;
1398 struct MacAndSession xmas;
1399 struct GNUNET_ATS_Information ats;
1400 struct FragmentMessage *fm;
1401 struct GNUNET_PeerIdentity tmpsource;
1402 const struct WlanHeader *wlanheader;
1406 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1408 ats.value = htonl (GNUNET_ATS_NET_WLAN);
1410 ats.value = htonl (GNUNET_ATS_NET_BT);
1412 msize = ntohs (hdr->size);
1414 GNUNET_STATISTICS_update (plugin->env->stats,
1418 switch (ntohs (hdr->type))
1420 case GNUNET_MESSAGE_TYPE_HELLO:
1423 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hdr,
1426 GNUNET_break_op (0);
1429 if (NULL == mas->endpoint)
1435 LOG (GNUNET_ERROR_TYPE_DEBUG,
1436 "Processing %u bytes of HELLO from peer `%s' at MAC %s\n",
1437 (unsigned int) msize,
1438 GNUNET_i2s (&tmpsource),
1439 wlan_plugin_address_to_string (NULL,
1440 &mas->endpoint->wlan_addr,
1441 sizeof (mas->endpoint->wlan_addr)));
1443 GNUNET_STATISTICS_update (plugin->env->stats,
1444 _("# HELLO messages received"), 1,
1446 address = GNUNET_HELLO_address_allocate (&tmpsource,
1448 &mas->endpoint->wlan_addr,
1449 sizeof (mas->endpoint->wlan_addr),
1450 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
1451 plugin->env->receive (plugin->env->cls,
1455 plugin->env->update_address_metrics (plugin->env->cls,
1459 GNUNET_HELLO_address_free (address);
1461 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1462 if (NULL == mas->endpoint)
1467 LOG (GNUNET_ERROR_TYPE_DEBUG,
1468 "Processing %u bytes of FRAGMENT from MAC %s\n",
1469 (unsigned int) msize,
1470 wlan_plugin_address_to_string (NULL,
1471 &mas->endpoint->wlan_addr,
1472 sizeof (mas->endpoint->wlan_addr)));
1473 GNUNET_STATISTICS_update (plugin->env->stats,
1474 _("# fragments received"),
1477 (void) GNUNET_DEFRAGMENT_process_fragment (mas->endpoint->defrag,
1480 case GNUNET_MESSAGE_TYPE_FRAGMENT_ACK:
1481 if (NULL == mas->endpoint)
1486 GNUNET_STATISTICS_update (plugin->env->stats,
1487 _("# ACKs received"),
1489 for (fm = mas->endpoint->sending_messages_head; NULL != fm; fm = fm->next)
1491 ret = GNUNET_FRAGMENT_process_ack (fm->fragcontext, hdr);
1492 if (GNUNET_OK == ret)
1494 LOG (GNUNET_ERROR_TYPE_DEBUG,
1495 "Got last ACK, finished message transmission to `%s' (%p)\n",
1496 wlan_plugin_address_to_string (NULL,
1497 &mas->endpoint->wlan_addr,
1498 sizeof (mas->endpoint->wlan_addr)),
1500 mas->endpoint->timeout = GNUNET_TIME_relative_to_absolute (MACENDPOINT_TIMEOUT);
1501 if (NULL != fm->cont)
1503 fm->cont (fm->cont_cls,
1510 free_fragment_message (fm);
1513 if (GNUNET_NO == ret)
1515 LOG (GNUNET_ERROR_TYPE_DEBUG,
1516 "Got an ACK, message transmission to `%s' not yet finished\n",
1517 wlan_plugin_address_to_string (NULL,
1518 &mas->endpoint->wlan_addr,
1519 sizeof (mas->endpoint->wlan_addr)));
1523 LOG (GNUNET_ERROR_TYPE_DEBUG,
1524 "ACK not matched against any active fragmentation with MAC `%s'\n",
1525 wlan_plugin_address_to_string (NULL,
1526 &mas->endpoint->wlan_addr,
1527 sizeof (mas->endpoint->wlan_addr)));
1529 case GNUNET_MESSAGE_TYPE_WLAN_DATA:
1530 if (NULL == mas->endpoint)
1535 if (msize < sizeof (struct WlanHeader))
1540 wlanheader = (const struct WlanHeader *) hdr;
1541 if (0 != memcmp (&wlanheader->target,
1542 plugin->env->my_identity,
1543 sizeof (struct GNUNET_PeerIdentity)))
1545 LOG (GNUNET_ERROR_TYPE_DEBUG,
1546 "Data for `%s', not for me, ignoring\n",
1547 GNUNET_i2s (&wlanheader->target));
1550 if (ntohl (wlanheader->crc) !=
1551 GNUNET_CRYPTO_crc32_n (&wlanheader[1],
1552 msize - sizeof (struct WlanHeader)))
1554 GNUNET_STATISTICS_update (plugin->env->stats,
1555 _("# DATA messages discarded due to CRC32 error"),
1560 xmas.endpoint = mas->endpoint;
1561 if (NULL == (xmas.session = lookup_session (mas->endpoint,
1562 &wlanheader->sender)))
1564 xmas.session = create_session (mas->endpoint,
1565 &wlanheader->sender);
1566 address = GNUNET_HELLO_address_allocate (&wlanheader->sender,
1568 &mas->endpoint->wlan_addr,
1569 sizeof (struct WlanAddress),
1570 GNUNET_HELLO_ADDRESS_INFO_NONE);
1571 plugin->env->session_start (NULL,
1575 LOG (GNUNET_ERROR_TYPE_DEBUG,
1576 "Notifying transport about peer `%s''s new session %p \n",
1577 GNUNET_i2s (&wlanheader->sender),
1579 GNUNET_HELLO_address_free (address);
1581 LOG (GNUNET_ERROR_TYPE_DEBUG,
1582 "Processing %u bytes of DATA from peer `%s'\n",
1583 (unsigned int) msize,
1584 GNUNET_i2s (&wlanheader->sender));
1585 xmas.session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1586 (void) GNUNET_SERVER_mst_receive (plugin->wlan_header_payload_tokenizer,
1588 (const char *) &wlanheader[1],
1589 msize - sizeof (struct WlanHeader),
1594 if (NULL == mas->endpoint)
1599 if (NULL == mas->session)
1604 LOG (GNUNET_ERROR_TYPE_DEBUG,
1605 "Received packet with %u bytes of type %u from peer %s\n",
1606 (unsigned int) msize,
1607 (unsigned int) ntohs (hdr->type),
1608 GNUNET_i2s (&mas->session->target));
1609 plugin->env->receive (plugin->env->cls,
1610 mas->session->address,
1613 plugin->env->update_address_metrics (plugin->env->cls,
1614 mas->session->address,
1624 * Function used for to process the data from the suid process
1626 * @param cls the plugin handle
1627 * @param client client that send the data (not used)
1628 * @param hdr header of the GNUNET_MessageHeader
1631 handle_helper_message (void *cls, void *client,
1632 const struct GNUNET_MessageHeader *hdr)
1634 struct Plugin *plugin = cls;
1635 struct GNUNET_HELLO_Address *my_address;
1636 const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rxinfo;
1637 const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *cm;
1638 struct WlanAddress wa;
1639 struct MacAndSession mas;
1642 msize = ntohs (hdr->size);
1643 switch (ntohs (hdr->type))
1645 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
1646 if (msize != sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage))
1651 cm = (const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *) hdr;
1652 if (GNUNET_YES == plugin->have_mac)
1654 if (0 == memcmp (&plugin->mac_address,
1656 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1657 break; /* no change */
1658 /* remove old address */
1659 memset (&wa, 0, sizeof (struct WlanAddress));
1660 wa.mac = plugin->mac_address;
1661 wa.options = htonl(plugin->options);
1662 my_address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1665 GNUNET_HELLO_ADDRESS_INFO_NONE);
1666 plugin->env->notify_address (plugin->env->cls,
1669 GNUNET_HELLO_address_free (my_address);
1671 plugin->mac_address = cm->mac;
1672 plugin->have_mac = GNUNET_YES;
1674 memset (&wa, 0, sizeof (struct WlanAddress));
1675 wa.mac = plugin->mac_address;
1676 wa.options = htonl(plugin->options);
1677 my_address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1680 GNUNET_HELLO_ADDRESS_INFO_NONE);
1682 LOG (GNUNET_ERROR_TYPE_DEBUG,
1683 "Received WLAN_HELPER_CONTROL message with MAC address `%s' for peer `%s'\n",
1684 mac_to_string (&cm->mac),
1685 GNUNET_i2s (plugin->env->my_identity));
1686 plugin->env->notify_address (plugin->env->cls,
1689 GNUNET_HELLO_address_free (my_address);
1691 case GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER:
1692 LOG (GNUNET_ERROR_TYPE_DEBUG,
1693 "Got data message from helper with %u bytes\n",
1695 GNUNET_STATISTICS_update (plugin->env->stats,
1696 _("# DATA messages received"), 1,
1698 if (msize < sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage))
1700 LOG (GNUNET_ERROR_TYPE_DEBUG,
1701 "Size of packet is too small (%u bytes < %u)\n",
1702 msize, sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage));
1705 rxinfo = (const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) hdr;
1707 /* check if message is actually for us */
1708 if (0 != memcmp (&rxinfo->frame.addr3, &mac_bssid_gnunet,
1709 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1711 /* Not the GNUnet BSSID */
1714 if ( (0 != memcmp (&rxinfo->frame.addr1, &bc_all_mac,
1715 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))) &&
1716 (0 != memcmp (&rxinfo->frame.addr1, &plugin->mac_address,
1717 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))) )
1719 /* Neither broadcast nor specifically for us */
1722 if (0 == memcmp (&rxinfo->frame.addr2, &plugin->mac_address,
1723 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1725 /* packet is FROM us, thus not FOR us */
1729 GNUNET_STATISTICS_update (plugin->env->stats,
1730 _("# DATA messages processed"),
1732 LOG (GNUNET_ERROR_TYPE_DEBUG,
1733 "Receiving %u bytes of data from MAC `%s'\n",
1734 (unsigned int) (msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)),
1735 mac_to_string (&rxinfo->frame.addr2));
1736 wa.mac = rxinfo->frame.addr2;
1737 wa.options = htonl (0);
1738 mas.endpoint = create_macendpoint (plugin, &wa);
1740 (void) GNUNET_SERVER_mst_receive (plugin->helper_payload_tokenizer,
1742 (const char*) &rxinfo[1],
1743 msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage),
1744 GNUNET_YES, GNUNET_NO);
1748 LOG (GNUNET_ERROR_TYPE_DEBUG,
1749 "Unexpected message of type %u (%u bytes)",
1750 ntohs (hdr->type), ntohs (hdr->size));
1758 * Task to (periodically) send a HELLO beacon
1760 * @param cls pointer to the plugin struct
1761 * @param tc scheduler context
1764 send_hello_beacon (void *cls,
1765 const struct GNUNET_SCHEDULER_TaskContext *tc)
1767 struct Plugin *plugin = cls;
1769 uint16_t hello_size;
1770 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
1771 const struct GNUNET_MessageHeader *hello;
1773 hello = plugin->env->get_our_hello ();
1774 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1775 GNUNET_assert (sizeof (struct WlanHeader) + hello_size <= WLAN_MTU);
1776 size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + hello_size;
1778 char buf[size] GNUNET_ALIGN;
1780 LOG (GNUNET_ERROR_TYPE_DEBUG,
1781 "Sending %u byte HELLO beacon\n",
1782 (unsigned int) size);
1783 radioHeader = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage*) buf;
1784 get_radiotap_header (NULL, radioHeader, size);
1785 get_wlan_header (plugin, &radioHeader->frame, &bc_all_mac, size);
1786 memcpy (&radioHeader[1], hello, hello_size);
1788 GNUNET_HELPER_send (plugin->suid_helper,
1789 &radioHeader->header,
1790 GNUNET_YES /* can drop */,
1792 GNUNET_STATISTICS_update (plugin->env->stats,
1793 _("# HELLO beacons sent"),
1796 plugin->beacon_task =
1797 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1798 (HELLO_BEACON_SCALING_FACTOR,
1799 plugin->mac_count + 1),
1807 * Another peer has suggested an address for this
1808 * peer and transport plugin. Check that this could be a valid
1809 * address. If so, consider adding it to the list
1812 * @param cls closure
1813 * @param addr pointer to the address
1814 * @param addrlen length of @a addr
1815 * @return #GNUNET_OK if this is a plausible address for this peer
1819 wlan_plugin_address_suggested (void *cls,
1823 struct Plugin *plugin = cls;
1824 struct WlanAddress *wa = (struct WlanAddress *) addr;
1826 if (addrlen != sizeof (struct WlanAddress))
1828 GNUNET_break_op (0);
1829 return GNUNET_SYSERR;
1831 if (GNUNET_YES != plugin->have_mac)
1833 LOG (GNUNET_ERROR_TYPE_DEBUG,
1834 "Rejecting MAC `%s': I don't know my MAC!\n",
1835 mac_to_string (addr));
1836 return GNUNET_NO; /* don't know my MAC */
1838 if (0 != memcmp (&wa->mac,
1839 &plugin->mac_address,
1842 LOG (GNUNET_ERROR_TYPE_DEBUG,
1843 "Rejecting MAC `%s': not my MAC!\n",
1844 mac_to_string (addr));
1845 return GNUNET_NO; /* not my MAC */
1852 * Convert the transports address to a nice, human-readable format.
1854 * @param cls closure
1855 * @param type name of the transport that generated the address
1856 * @param addr one of the addresses of the host, NULL for the last address
1857 * the specific address format depends on the transport
1858 * @param addrlen length of the address
1859 * @param numeric should (IP) addresses be displayed in numeric form?
1860 * @param timeout after how long should we give up?
1861 * @param asc function to call on each string
1862 * @param asc_cls closure for @a asc
1865 wlan_plugin_address_pretty_printer (void *cls,
1870 struct GNUNET_TIME_Relative timeout,
1871 GNUNET_TRANSPORT_AddressStringCallback asc,
1876 if (sizeof (struct WlanAddress) == addrlen)
1877 ret = wlan_plugin_address_to_string (NULL,
1884 (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
1885 asc (asc_cls, NULL, GNUNET_OK);
1890 * Exit point from the plugin.
1892 * @param cls pointer to the api struct
1895 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1897 struct WlanAddress wa;
1898 struct GNUNET_HELLO_Address *address;
1899 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1900 struct Plugin *plugin = api->cls;
1901 struct MacEndpoint *endpoint;
1902 struct MacEndpoint *endpoint_next;
1909 if (GNUNET_YES == plugin->have_mac)
1911 memset (&wa, 0, sizeof(wa));
1912 wa.options = htonl (plugin->options);
1913 wa.mac = plugin->mac_address;
1914 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1916 &wa, sizeof (struct WlanAddress),
1917 GNUNET_HELLO_ADDRESS_INFO_NONE);
1919 plugin->env->notify_address (plugin->env->cls,
1922 plugin->have_mac = GNUNET_NO;
1923 GNUNET_HELLO_address_free (address);
1926 if (GNUNET_SCHEDULER_NO_TASK != plugin->beacon_task)
1928 GNUNET_SCHEDULER_cancel (plugin->beacon_task);
1929 plugin->beacon_task = GNUNET_SCHEDULER_NO_TASK;
1931 if (NULL != plugin->suid_helper)
1933 GNUNET_HELPER_stop (plugin->suid_helper,
1935 plugin->suid_helper = NULL;
1937 endpoint_next = plugin->mac_head;
1938 while (NULL != (endpoint = endpoint_next))
1940 endpoint_next = endpoint->next;
1941 free_macendpoint (endpoint);
1943 if (NULL != plugin->fragment_data_tokenizer)
1945 GNUNET_SERVER_mst_destroy (plugin->fragment_data_tokenizer);
1946 plugin->fragment_data_tokenizer = NULL;
1948 if (NULL != plugin->wlan_header_payload_tokenizer)
1950 GNUNET_SERVER_mst_destroy (plugin->wlan_header_payload_tokenizer);
1951 plugin->wlan_header_payload_tokenizer = NULL;
1953 if (NULL != plugin->helper_payload_tokenizer)
1955 GNUNET_SERVER_mst_destroy (plugin->helper_payload_tokenizer);
1956 plugin->helper_payload_tokenizer = NULL;
1958 GNUNET_free_non_null (plugin->wlan_interface);
1959 GNUNET_free (plugin);
1966 * Function called to convert a string address to
1969 * @param cls closure (`struct Plugin *`)
1970 * @param addr string address
1971 * @param addrlen length of the address
1972 * @param buf location to store the buffer
1973 * @param added location to store the number of bytes in the buffer.
1974 * If the function returns #GNUNET_SYSERR, its contents are undefined.
1975 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1978 wlan_plugin_string_to_address (void *cls,
1984 struct WlanAddress *wa;
1990 if ((NULL == addr) || (0 == addrlen))
1993 return GNUNET_SYSERR;
1995 if ('\0' != addr[addrlen - 1])
1998 return GNUNET_SYSERR;
2000 if (strlen (addr) != addrlen - 1)
2003 return GNUNET_SYSERR;
2006 if (8 != SSCANF (addr,
2007 "%4s.%u.%X:%X:%X:%X:%X:%X",
2009 &a[0], &a[1], &a[2],
2010 &a[3], &a[4], &a[5]))
2013 return GNUNET_SYSERR;
2015 wa = GNUNET_new (struct WlanAddress);
2017 wa->mac.mac[i] = a[i];
2018 wa->options = htonl (0);
2020 *added = sizeof (struct WlanAddress);
2026 * Begin monitoring sessions of a plugin. There can only
2027 * be one active monitor per plugin (i.e. if there are
2028 * multiple monitors, the transport service needs to
2029 * multiplex the generated events over all of them).
2031 * @param cls closure of the plugin
2032 * @param sic callback to invoke, NULL to disable monitor;
2033 * plugin will being by iterating over all active
2034 * sessions immediately and then enter monitor mode
2035 * @param sic_cls closure for @a sic
2038 wlan_plugin_setup_monitor (void *cls,
2039 GNUNET_TRANSPORT_SessionInfoCallback sic,
2042 struct Plugin *plugin = cls;
2043 struct MacEndpoint *mac;
2044 struct Session *session;
2047 plugin->sic_cls = sic_cls;
2050 for (mac = plugin->mac_head; NULL != mac; mac = mac->next)
2051 for (session = mac->sessions_head; NULL != session; session = session->next)
2053 notify_session_monitor (plugin,
2055 GNUNET_TRANSPORT_SS_INIT);
2056 notify_session_monitor (plugin,
2058 GNUNET_TRANSPORT_SS_UP);
2060 sic (sic_cls, NULL, NULL);
2067 * Function that will be called whenever the transport service wants to
2068 * notify the plugin that a session is still active and in use and
2069 * therefore the session timeout for this session has to be updated
2071 * @param cls closure
2072 * @param peer which peer was the session for
2073 * @param session which session is being updated
2076 wlan_plugin_update_session_timeout (void *cls,
2077 const struct GNUNET_PeerIdentity *peer,
2078 struct Session *session)
2080 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != session->timeout_task);
2081 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2086 * Function that will be called whenever the transport service wants to
2087 * notify the plugin that the inbound quota changed and that the plugin
2088 * should update it's delay for the next receive value
2090 * @param cls closure
2091 * @param peer which peer was the session for
2092 * @param session which session is being updated
2093 * @param delay new delay to use for receiving
2096 wlan_plugin_update_inbound_delay (void *cls,
2097 const struct GNUNET_PeerIdentity *peer,
2098 struct Session *session,
2099 struct GNUNET_TIME_Relative delay)
2101 /* does nothing, as inbound delay is not supported by WLAN */
2106 * Entry point for the plugin.
2108 * @param cls closure, the `struct GNUNET_TRANSPORT_PluginEnvironment *`
2109 * @return the `struct GNUNET_TRANSPORT_PluginFunctions *` or NULL on error
2112 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2114 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2115 struct GNUNET_TRANSPORT_PluginFunctions *api;
2116 struct Plugin *plugin;
2117 char *wlan_interface;
2118 unsigned long long testmode;
2121 /* check for 'special' mode */
2122 if (NULL == env->receive)
2124 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2125 initialze the plugin or the API */
2126 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2128 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2129 api->address_to_string = &wlan_plugin_address_to_string;
2130 api->string_to_address = &wlan_plugin_string_to_address;
2135 /* check configuration */
2137 GNUNET_CONFIGURATION_have_value (env->cfg,
2141 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2147 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2152 binary = GNUNET_OS_get_libexec_binary_path (HELPER_NAME);
2153 if ( (0 == testmode) &&
2155 GNUNET_OS_check_helper_binary (binary,
2159 LOG (GNUNET_ERROR_TYPE_ERROR,
2160 _("Helper binary `%s' not SUID, cannot run WLAN transport\n"),
2162 GNUNET_free (binary);
2165 GNUNET_free (binary);
2167 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2172 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2178 plugin = GNUNET_new (struct Plugin);
2179 plugin->wlan_interface = wlan_interface;
2181 GNUNET_STATISTICS_set (plugin->env->stats,
2182 _("# sessions allocated"),
2184 GNUNET_STATISTICS_set (plugin->env->stats,
2185 _("# MAC endpoints allocated"),
2187 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker, NULL, NULL,
2188 GNUNET_BANDWIDTH_value_init (100 * 1024 *
2191 plugin->fragment_data_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
2192 plugin->wlan_header_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
2193 plugin->helper_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
2194 plugin->beacon_task = GNUNET_SCHEDULER_add_now (&send_hello_beacon,
2197 plugin->options = 0;
2199 /* some compilers do not like switch on 'long long'... */
2200 switch ((unsigned int) testmode)
2202 case 0: /* normal */
2203 plugin->helper_argv[0] = (char *) HELPER_NAME;
2204 plugin->helper_argv[1] = wlan_interface;
2205 plugin->helper_argv[2] = NULL;
2206 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
2208 plugin->helper_argv,
2209 &handle_helper_message,
2213 case 1: /* testmode, peer 1 */
2214 plugin->helper_argv[0] = (char *) DUMMY_HELPER_NAME;
2215 plugin->helper_argv[1] = (char *) "1";
2216 plugin->helper_argv[2] = NULL;
2217 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
2219 plugin->helper_argv,
2220 &handle_helper_message,
2224 case 2: /* testmode, peer 2 */
2225 plugin->helper_argv[0] = (char *) DUMMY_HELPER_NAME;
2226 plugin->helper_argv[1] = (char *) "2";
2227 plugin->helper_argv[2] = NULL;
2228 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
2230 plugin->helper_argv,
2231 &handle_helper_message,
2239 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2241 api->send = &wlan_plugin_send;
2242 api->get_session = &wlan_plugin_get_session;
2243 api->disconnect_peer = &wlan_plugin_disconnect_peer;
2244 api->disconnect_session = &wlan_plugin_disconnect_session;
2245 api->query_keepalive_factor = &wlan_plugin_query_keepalive_factor;
2246 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2247 api->check_address = &wlan_plugin_address_suggested;
2248 api->address_to_string = &wlan_plugin_address_to_string;
2249 api->string_to_address = &wlan_plugin_string_to_address;
2250 api->get_network = &wlan_plugin_get_network;
2251 api->update_session_timeout = &wlan_plugin_update_session_timeout;
2252 api->update_inbound_delay = &wlan_plugin_update_inbound_delay;
2253 api->setup_monitor = &wlan_plugin_setup_monitor;
2258 /* end of plugin_transport_wlan.c */