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_DOWN);
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_UP);
894 LOG (GNUNET_ERROR_TYPE_DEBUG,
895 "Created new session %p for peer `%s' with endpoint %s\n",
898 mac_to_string (&endpoint->wlan_addr.mac));
905 * Look up a session for a peer and create a new session if none is found
907 * @param endpoint pointer to the mac endpoint of the peer
908 * @param peer peer identity to use for this session
909 * @return returns the session
911 static struct Session *
912 get_session (struct MacEndpoint *endpoint,
913 const struct GNUNET_PeerIdentity *peer)
915 struct Session *session;
917 if (NULL != (session = lookup_session (endpoint, peer)))
919 return create_session (endpoint, peer);
924 * Function called once we have successfully given the fragment
925 * message to the SUID helper process and we are thus ready for
928 * @param cls the `struct FragmentMessage *`
929 * @param result result of the operation (#GNUNET_OK on success,
930 * #GNUNET_NO if the helper died, #GNUNET_SYSERR
931 * if the helper was stopped)
934 fragment_transmission_done (void *cls,
937 struct FragmentMessage *fm = cls;
941 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
946 * Transmit a fragment of a message.
948 * @param cls `struct FragmentMessage *` this fragment message belongs to
949 * @param hdr pointer to the start of the fragment message
952 transmit_fragment (void *cls,
953 const struct GNUNET_MessageHeader *hdr)
955 struct FragmentMessage *fm = cls;
956 struct MacEndpoint *endpoint = fm->macendpoint;
960 if (NULL == endpoint)
965 msize = ntohs (hdr->size);
966 size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + msize;
969 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radio_header;
971 radio_header = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) buf;
972 get_radiotap_header (endpoint, radio_header, size);
973 get_wlan_header (endpoint->plugin,
974 &radio_header->frame,
975 &endpoint->wlan_addr.mac,
976 sizeof (endpoint->wlan_addr.mac));
977 memcpy (&radio_header[1], hdr, msize);
978 GNUNET_assert (NULL == fm->sh);
979 fm->sh = GNUNET_HELPER_send (endpoint->plugin->suid_helper,
980 &radio_header->header,
982 &fragment_transmission_done, fm);
983 fm->size_on_wire += size;
985 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
986 _("# message fragments sent"),
990 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
991 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
992 "# bytes currently in buffers",
994 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
995 "# bytes transmitted",
1002 * Frees the space of a message in the fragment queue (send queue)
1004 * @param fm message to free
1007 free_fragment_message (struct FragmentMessage *fm)
1009 struct MacEndpoint *endpoint = fm->macendpoint;
1011 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
1012 _("# messages pending (with fragmentation)"),
1014 GNUNET_CONTAINER_DLL_remove (endpoint->sending_messages_head,
1015 endpoint->sending_messages_tail,
1019 GNUNET_HELPER_send_cancel (fm->sh);
1022 GNUNET_FRAGMENT_context_destroy (fm->fragcontext,
1023 &endpoint->msg_delay,
1024 &endpoint->ack_delay);
1025 if (fm->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1027 GNUNET_SCHEDULER_cancel (fm->timeout_task);
1028 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1035 * A FragmentMessage has timed out. Remove it.
1037 * @param cls pointer to the 'struct FragmentMessage'
1041 fragmentmessage_timeout (void *cls,
1042 const struct GNUNET_SCHEDULER_TaskContext *tc)
1044 struct FragmentMessage *fm = cls;
1046 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1047 if (NULL != fm->cont)
1049 fm->cont (fm->cont_cls,
1056 free_fragment_message (fm);
1061 * Transmit a message to the given destination with fragmentation.
1063 * @param endpoint desired destination
1064 * @param timeout how long can the message wait?
1065 * @param target peer that should receive the message
1066 * @param msg message to transmit
1067 * @param payload_size bytes of payload
1068 * @param cont continuation to call once the message has
1069 * been transmitted (or if the transport is ready
1070 * for the next transmission call; or if the
1071 * peer disconnected...); can be NULL
1072 * @param cont_cls closure for @a cont
1075 send_with_fragmentation (struct MacEndpoint *endpoint,
1076 struct GNUNET_TIME_Relative timeout,
1077 const struct GNUNET_PeerIdentity *target,
1078 const struct GNUNET_MessageHeader *msg,
1079 size_t payload_size,
1080 GNUNET_TRANSPORT_TransmitContinuation cont,
1084 struct FragmentMessage *fm;
1085 struct Plugin *plugin;
1087 plugin = endpoint->plugin;
1088 fm = GNUNET_new (struct FragmentMessage);
1089 fm->macendpoint = endpoint;
1090 fm->target = *target;
1091 fm->size_payload = payload_size;
1092 fm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1094 fm->cont_cls = cont_cls;
1095 /* 1 MBit/s typical data rate, 1430 byte fragments => ~100 ms per message */
1097 GNUNET_FRAGMENT_context_create (plugin->env->stats,
1100 endpoint->msg_delay,
1101 endpoint->ack_delay,
1103 &transmit_fragment, fm);
1105 GNUNET_SCHEDULER_add_delayed (timeout,
1106 &fragmentmessage_timeout, fm);
1107 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sending_messages_head,
1108 endpoint->sending_messages_tail,
1114 * Free a MAC endpoint.
1116 * @param endpoint pointer to the MacEndpoint to free
1119 free_macendpoint (struct MacEndpoint *endpoint)
1121 struct Plugin *plugin = endpoint->plugin;
1122 struct FragmentMessage *fm;
1123 struct Session *session;
1125 GNUNET_STATISTICS_update (plugin->env->stats,
1126 _("# MAC endpoints allocated"),
1129 while (NULL != (session = endpoint->sessions_head))
1130 wlan_plugin_disconnect_session (plugin,
1132 while (NULL != (fm = endpoint->sending_messages_head))
1133 free_fragment_message (fm);
1134 GNUNET_CONTAINER_DLL_remove (plugin->mac_head,
1138 if (NULL != endpoint->defrag)
1140 GNUNET_DEFRAGMENT_context_destroy(endpoint->defrag);
1141 endpoint->defrag = NULL;
1144 plugin->mac_count--;
1145 if (GNUNET_SCHEDULER_NO_TASK != endpoint->timeout_task)
1147 GNUNET_SCHEDULER_cancel (endpoint->timeout_task);
1148 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1150 GNUNET_free (endpoint);
1155 * A MAC endpoint is timing out. Clean up.
1157 * @param cls pointer to the `struct MacEndpoint *`
1158 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
1161 macendpoint_timeout (void *cls,
1162 const struct GNUNET_SCHEDULER_TaskContext *tc)
1164 struct MacEndpoint *endpoint = cls;
1165 struct GNUNET_TIME_Relative timeout;
1167 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1168 timeout = GNUNET_TIME_absolute_get_remaining (endpoint->timeout);
1169 if (0 == timeout.rel_value_us)
1171 free_macendpoint (endpoint);
1174 endpoint->timeout_task =
1175 GNUNET_SCHEDULER_add_delayed (timeout,
1176 &macendpoint_timeout,
1182 * Find (or create) a MacEndpoint with a specific MAC address
1184 * @param plugin pointer to the plugin struct
1185 * @param mac the MAC address of the endpoint
1186 * @return handle to our data structure for this MAC
1188 static struct MacEndpoint *
1189 create_macendpoint (struct Plugin *plugin,
1190 struct WlanAddress *mac)
1192 struct MacEndpoint *pos;
1194 for (pos = plugin->mac_head; NULL != pos; pos = pos->next)
1195 if (0 == memcmp (mac, &pos->wlan_addr, sizeof (pos->wlan_addr)))
1197 pos = GNUNET_new (struct MacEndpoint);
1198 pos->wlan_addr = (*mac);
1199 pos->plugin = plugin;
1201 GNUNET_DEFRAGMENT_context_create (plugin->env->stats,
1203 MESSAGES_IN_DEFRAG_QUEUE_PER_MAC,
1205 &wlan_data_message_handler,
1208 pos->msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1209 pos->ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
1210 pos->timeout = GNUNET_TIME_relative_to_absolute (MACENDPOINT_TIMEOUT);
1212 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
1214 GNUNET_CONTAINER_DLL_insert (plugin->mac_head,
1217 plugin->mac_count++;
1218 GNUNET_STATISTICS_update (plugin->env->stats,
1219 _("# MAC endpoints allocated"),
1221 LOG (GNUNET_ERROR_TYPE_DEBUG,
1222 "New MAC endpoint `%s'\n",
1223 wlan_plugin_address_to_string (NULL,
1225 sizeof (struct WlanAddress)));
1231 * Function obtain the network type for a session
1233 * @param cls closure (`struct Plugin*`)
1234 * @param session the session
1235 * @return the network type in HBO or #GNUNET_SYSERR
1237 static enum GNUNET_ATS_Network_Type
1238 wlan_plugin_get_network (void *cls,
1239 struct Session *session)
1242 return GNUNET_ATS_NET_WLAN;
1244 return GNUNET_ATS_NET_BT;
1250 * Creates a new outbound session the transport service will use to
1251 * send data to the peer
1253 * @param cls the `struct Plugin *`
1254 * @param address the address
1255 * @return the session or NULL of max connections exceeded
1257 static struct Session *
1258 wlan_plugin_get_session (void *cls,
1259 const struct GNUNET_HELLO_Address *address)
1261 struct Plugin *plugin = cls;
1262 struct MacEndpoint *endpoint;
1264 if (NULL == address)
1266 if (sizeof (struct WlanAddress) != address->address_length)
1271 LOG (GNUNET_ERROR_TYPE_DEBUG,
1272 "Service asked to create session for peer `%s' with MAC `%s'\n",
1273 GNUNET_i2s (&address->peer),
1274 wlan_plugin_address_to_string (NULL,
1276 address->address_length));
1277 endpoint = create_macendpoint (plugin,
1278 (struct WlanAddress *) address->address);
1279 return get_session (endpoint, &address->peer);
1284 * Function that can be used to force the plugin to disconnect
1285 * from the given peer and cancel all previous transmissions
1286 * (and their continuation).
1288 * @param cls closure
1289 * @param target peer from which to disconnect
1292 wlan_plugin_disconnect_peer (void *cls,
1293 const struct GNUNET_PeerIdentity *target)
1295 struct Plugin *plugin = cls;
1296 struct Session *session;
1297 struct MacEndpoint *endpoint;
1299 for (endpoint = plugin->mac_head; NULL != endpoint; endpoint = endpoint->next)
1300 for (session = endpoint->sessions_head; NULL != session; session = session->next)
1301 if (0 == memcmp (target, &session->target,
1302 sizeof (struct GNUNET_PeerIdentity)))
1304 wlan_plugin_disconnect_session (plugin, session);
1305 break; /* inner-loop only (in case peer has another MAC as well!) */
1311 * Function that can be used by the transport service to transmit
1312 * a message using the plugin. Note that in the case of a
1313 * peer disconnecting, the continuation MUST be called
1314 * prior to the disconnect notification itself. This function
1315 * will be called with this peer's HELLO message to initiate
1316 * a fresh connection to another peer.
1318 * @param cls closure
1319 * @param session which session must be used
1320 * @param msgbuf the message to transmit
1321 * @param msgbuf_size number of bytes in @a msgbuf
1322 * @param priority how important is the message (most plugins will
1323 * ignore message priority and just FIFO)
1324 * @param to how long to wait at most for the transmission (does not
1325 * require plugins to discard the message after the timeout,
1326 * just advisory for the desired delay; most plugins will ignore
1328 * @param cont continuation to call once the message has
1329 * been transmitted (or if the transport is ready
1330 * for the next transmission call; or if the
1331 * peer disconnected...); can be NULL
1332 * @param cont_cls closure for @a cont
1333 * @return number of bytes used (on the physical network, with overheads);
1334 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1335 * and does NOT mean that the message was not transmitted (DV)
1338 wlan_plugin_send (void *cls,
1339 struct Session *session,
1340 const char *msgbuf, size_t msgbuf_size,
1341 unsigned int priority,
1342 struct GNUNET_TIME_Relative to,
1343 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1345 struct Plugin *plugin = cls;
1346 struct WlanHeader *wlanheader;
1347 size_t size = msgbuf_size + sizeof (struct WlanHeader);
1348 char buf[size] GNUNET_ALIGN;
1350 LOG (GNUNET_ERROR_TYPE_DEBUG,
1351 "Transmitting %u bytes of payload to peer `%s' (starting with %u byte message of type %u)\n",
1353 GNUNET_i2s (&session->target),
1354 (unsigned int) ntohs (((struct GNUNET_MessageHeader*)msgbuf)->size),
1355 (unsigned int) ntohs (((struct GNUNET_MessageHeader*)msgbuf)->type));
1356 wlanheader = (struct WlanHeader *) buf;
1357 wlanheader->header.size = htons (msgbuf_size + sizeof (struct WlanHeader));
1358 wlanheader->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA);
1359 wlanheader->sender = *plugin->env->my_identity;
1360 wlanheader->target = session->target;
1361 wlanheader->crc = htonl (GNUNET_CRYPTO_crc32_n (msgbuf, msgbuf_size));
1362 memcpy (&wlanheader[1], msgbuf, msgbuf_size);
1364 GNUNET_STATISTICS_update (plugin->env->stats,
1365 "# bytes currently in buffers",
1369 send_with_fragmentation (session->mac,
1372 &wlanheader->header,
1380 * We have received data from the WLAN via some session. Process depending
1381 * on the message type (HELLO, DATA, FRAGMENTATION or FRAGMENTATION-ACK).
1383 * @param cls pointer to the plugin
1384 * @param client pointer to the session this message belongs to
1385 * @param hdr start of the message
1388 process_data (void *cls,
1390 const struct GNUNET_MessageHeader *hdr)
1392 struct Plugin *plugin = cls;
1393 struct GNUNET_HELLO_Address *address;
1394 struct MacAndSession *mas = client;
1395 struct MacAndSession xmas;
1396 struct GNUNET_ATS_Information ats;
1397 struct FragmentMessage *fm;
1398 struct GNUNET_PeerIdentity tmpsource;
1399 const struct WlanHeader *wlanheader;
1403 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1405 ats.value = htonl (GNUNET_ATS_NET_WLAN);
1407 ats.value = htonl (GNUNET_ATS_NET_BT);
1409 msize = ntohs (hdr->size);
1411 GNUNET_STATISTICS_update (plugin->env->stats,
1415 switch (ntohs (hdr->type))
1417 case GNUNET_MESSAGE_TYPE_HELLO:
1420 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hdr,
1423 GNUNET_break_op (0);
1426 if (NULL == mas->endpoint)
1432 LOG (GNUNET_ERROR_TYPE_DEBUG,
1433 "Processing %u bytes of HELLO from peer `%s' at MAC %s\n",
1434 (unsigned int) msize,
1435 GNUNET_i2s (&tmpsource),
1436 wlan_plugin_address_to_string (NULL,
1437 &mas->endpoint->wlan_addr,
1438 sizeof (mas->endpoint->wlan_addr)));
1440 GNUNET_STATISTICS_update (plugin->env->stats,
1441 _("# HELLO messages received"), 1,
1443 address = GNUNET_HELLO_address_allocate (&tmpsource,
1445 &mas->endpoint->wlan_addr,
1446 sizeof (mas->endpoint->wlan_addr),
1447 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
1448 plugin->env->receive (plugin->env->cls,
1452 plugin->env->update_address_metrics (plugin->env->cls,
1456 GNUNET_HELLO_address_free (address);
1458 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1459 if (NULL == mas->endpoint)
1464 LOG (GNUNET_ERROR_TYPE_DEBUG,
1465 "Processing %u bytes of FRAGMENT from MAC %s\n",
1466 (unsigned int) msize,
1467 wlan_plugin_address_to_string (NULL,
1468 &mas->endpoint->wlan_addr,
1469 sizeof (mas->endpoint->wlan_addr)));
1470 GNUNET_STATISTICS_update (plugin->env->stats,
1471 _("# fragments received"),
1474 (void) GNUNET_DEFRAGMENT_process_fragment (mas->endpoint->defrag,
1477 case GNUNET_MESSAGE_TYPE_FRAGMENT_ACK:
1478 if (NULL == mas->endpoint)
1483 GNUNET_STATISTICS_update (plugin->env->stats,
1484 _("# ACKs received"),
1486 for (fm = mas->endpoint->sending_messages_head; NULL != fm; fm = fm->next)
1488 ret = GNUNET_FRAGMENT_process_ack (fm->fragcontext, hdr);
1489 if (GNUNET_OK == ret)
1491 LOG (GNUNET_ERROR_TYPE_DEBUG,
1492 "Got last ACK, finished message transmission to `%s' (%p)\n",
1493 wlan_plugin_address_to_string (NULL,
1494 &mas->endpoint->wlan_addr,
1495 sizeof (mas->endpoint->wlan_addr)),
1497 mas->endpoint->timeout = GNUNET_TIME_relative_to_absolute (MACENDPOINT_TIMEOUT);
1498 if (NULL != fm->cont)
1500 fm->cont (fm->cont_cls,
1507 free_fragment_message (fm);
1510 if (GNUNET_NO == ret)
1512 LOG (GNUNET_ERROR_TYPE_DEBUG,
1513 "Got an ACK, message transmission to `%s' not yet finished\n",
1514 wlan_plugin_address_to_string (NULL,
1515 &mas->endpoint->wlan_addr,
1516 sizeof (mas->endpoint->wlan_addr)));
1520 LOG (GNUNET_ERROR_TYPE_DEBUG,
1521 "ACK not matched against any active fragmentation with MAC `%s'\n",
1522 wlan_plugin_address_to_string (NULL,
1523 &mas->endpoint->wlan_addr,
1524 sizeof (mas->endpoint->wlan_addr)));
1526 case GNUNET_MESSAGE_TYPE_WLAN_DATA:
1527 if (NULL == mas->endpoint)
1532 if (msize < sizeof (struct WlanHeader))
1537 wlanheader = (const struct WlanHeader *) hdr;
1538 if (0 != memcmp (&wlanheader->target,
1539 plugin->env->my_identity,
1540 sizeof (struct GNUNET_PeerIdentity)))
1542 LOG (GNUNET_ERROR_TYPE_DEBUG,
1543 "Data for `%s', not for me, ignoring\n",
1544 GNUNET_i2s (&wlanheader->target));
1547 if (ntohl (wlanheader->crc) !=
1548 GNUNET_CRYPTO_crc32_n (&wlanheader[1],
1549 msize - sizeof (struct WlanHeader)))
1551 GNUNET_STATISTICS_update (plugin->env->stats,
1552 _("# DATA messages discarded due to CRC32 error"),
1557 xmas.endpoint = mas->endpoint;
1558 if (NULL == (xmas.session = lookup_session (mas->endpoint,
1559 &wlanheader->sender)))
1561 xmas.session = create_session (mas->endpoint,
1562 &wlanheader->sender);
1563 address = GNUNET_HELLO_address_allocate (&wlanheader->sender,
1565 &mas->endpoint->wlan_addr,
1566 sizeof (struct WlanAddress),
1567 GNUNET_HELLO_ADDRESS_INFO_NONE);
1568 plugin->env->session_start (NULL,
1572 LOG (GNUNET_ERROR_TYPE_DEBUG,
1573 "Notifying transport about peer `%s''s new session %p \n",
1574 GNUNET_i2s (&wlanheader->sender),
1576 GNUNET_HELLO_address_free (address);
1578 LOG (GNUNET_ERROR_TYPE_DEBUG,
1579 "Processing %u bytes of DATA from peer `%s'\n",
1580 (unsigned int) msize,
1581 GNUNET_i2s (&wlanheader->sender));
1582 xmas.session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1583 (void) GNUNET_SERVER_mst_receive (plugin->wlan_header_payload_tokenizer,
1585 (const char *) &wlanheader[1],
1586 msize - sizeof (struct WlanHeader),
1591 if (NULL == mas->endpoint)
1596 if (NULL == mas->session)
1601 LOG (GNUNET_ERROR_TYPE_DEBUG,
1602 "Received packet with %u bytes of type %u from peer %s\n",
1603 (unsigned int) msize,
1604 (unsigned int) ntohs (hdr->type),
1605 GNUNET_i2s (&mas->session->target));
1606 plugin->env->receive (plugin->env->cls,
1607 mas->session->address,
1610 plugin->env->update_address_metrics (plugin->env->cls,
1611 mas->session->address,
1621 * Function used for to process the data from the suid process
1623 * @param cls the plugin handle
1624 * @param client client that send the data (not used)
1625 * @param hdr header of the GNUNET_MessageHeader
1628 handle_helper_message (void *cls, void *client,
1629 const struct GNUNET_MessageHeader *hdr)
1631 struct Plugin *plugin = cls;
1632 struct GNUNET_HELLO_Address *my_address;
1633 const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rxinfo;
1634 const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *cm;
1635 struct WlanAddress wa;
1636 struct MacAndSession mas;
1639 msize = ntohs (hdr->size);
1640 switch (ntohs (hdr->type))
1642 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
1643 if (msize != sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage))
1648 cm = (const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *) hdr;
1649 if (GNUNET_YES == plugin->have_mac)
1651 if (0 == memcmp (&plugin->mac_address,
1653 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1654 break; /* no change */
1655 /* remove old address */
1656 memset (&wa, 0, sizeof (struct WlanAddress));
1657 wa.mac = plugin->mac_address;
1658 wa.options = htonl(plugin->options);
1659 my_address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1662 GNUNET_HELLO_ADDRESS_INFO_NONE);
1663 plugin->env->notify_address (plugin->env->cls,
1666 GNUNET_HELLO_address_free (my_address);
1668 plugin->mac_address = cm->mac;
1669 plugin->have_mac = GNUNET_YES;
1671 memset (&wa, 0, sizeof (struct WlanAddress));
1672 wa.mac = plugin->mac_address;
1673 wa.options = htonl(plugin->options);
1674 my_address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1677 GNUNET_HELLO_ADDRESS_INFO_NONE);
1679 LOG (GNUNET_ERROR_TYPE_DEBUG,
1680 "Received WLAN_HELPER_CONTROL message with MAC address `%s' for peer `%s'\n",
1681 mac_to_string (&cm->mac),
1682 GNUNET_i2s (plugin->env->my_identity));
1683 plugin->env->notify_address (plugin->env->cls,
1686 GNUNET_HELLO_address_free (my_address);
1688 case GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER:
1689 LOG (GNUNET_ERROR_TYPE_DEBUG,
1690 "Got data message from helper with %u bytes\n",
1692 GNUNET_STATISTICS_update (plugin->env->stats,
1693 _("# DATA messages received"), 1,
1695 if (msize < sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage))
1698 LOG (GNUNET_ERROR_TYPE_WARNING,
1699 "Size of packet is too small (%u bytes < %u)\n",
1700 msize, sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage));
1703 rxinfo = (const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) hdr;
1705 /* check if message is actually for us */
1706 if (0 != memcmp (&rxinfo->frame.addr3, &mac_bssid_gnunet,
1707 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1709 /* Not the GNUnet BSSID */
1712 if ( (0 != memcmp (&rxinfo->frame.addr1, &bc_all_mac,
1713 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))) &&
1714 (0 != memcmp (&rxinfo->frame.addr1, &plugin->mac_address,
1715 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))) )
1717 /* Neither broadcast nor specifically for us */
1720 if (0 == memcmp (&rxinfo->frame.addr2, &plugin->mac_address,
1721 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1723 /* packet is FROM us, thus not FOR us */
1727 GNUNET_STATISTICS_update (plugin->env->stats,
1728 _("# DATA messages processed"),
1730 LOG (GNUNET_ERROR_TYPE_DEBUG,
1731 "Receiving %u bytes of data from MAC `%s'\n",
1732 (unsigned int) (msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)),
1733 mac_to_string (&rxinfo->frame.addr2));
1734 wa.mac = rxinfo->frame.addr2;
1735 wa.options = htonl (0);
1736 mas.endpoint = create_macendpoint (plugin, &wa);
1738 (void) GNUNET_SERVER_mst_receive (plugin->helper_payload_tokenizer,
1740 (const char*) &rxinfo[1],
1741 msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage),
1742 GNUNET_YES, GNUNET_NO);
1746 LOG (GNUNET_ERROR_TYPE_DEBUG,
1747 "Unexpected message of type %u (%u bytes)",
1748 ntohs (hdr->type), ntohs (hdr->size));
1756 * Task to (periodically) send a HELLO beacon
1758 * @param cls pointer to the plugin struct
1759 * @param tc scheduler context
1762 send_hello_beacon (void *cls,
1763 const struct GNUNET_SCHEDULER_TaskContext *tc)
1765 struct Plugin *plugin = cls;
1767 uint16_t hello_size;
1768 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
1769 const struct GNUNET_MessageHeader *hello;
1771 hello = plugin->env->get_our_hello ();
1772 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1773 GNUNET_assert (sizeof (struct WlanHeader) + hello_size <= WLAN_MTU);
1774 size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + hello_size;
1776 char buf[size] GNUNET_ALIGN;
1778 LOG (GNUNET_ERROR_TYPE_DEBUG,
1779 "Sending %u byte HELLO beacon\n",
1780 (unsigned int) size);
1781 radioHeader = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage*) buf;
1782 get_radiotap_header (NULL, radioHeader, size);
1783 get_wlan_header (plugin, &radioHeader->frame, &bc_all_mac, size);
1784 memcpy (&radioHeader[1], hello, hello_size);
1786 GNUNET_HELPER_send (plugin->suid_helper,
1787 &radioHeader->header,
1788 GNUNET_YES /* can drop */,
1790 GNUNET_STATISTICS_update (plugin->env->stats,
1791 _("# HELLO beacons sent"),
1794 plugin->beacon_task =
1795 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1796 (HELLO_BEACON_SCALING_FACTOR,
1797 plugin->mac_count + 1),
1805 * Another peer has suggested an address for this
1806 * peer and transport plugin. Check that this could be a valid
1807 * address. If so, consider adding it to the list
1810 * @param cls closure
1811 * @param addr pointer to the address
1812 * @param addrlen length of @a addr
1813 * @return #GNUNET_OK if this is a plausible address for this peer
1817 wlan_plugin_address_suggested (void *cls,
1821 struct Plugin *plugin = cls;
1822 struct WlanAddress *wa = (struct WlanAddress *) addr;
1824 if (addrlen != sizeof (struct WlanAddress))
1826 GNUNET_break_op (0);
1827 return GNUNET_SYSERR;
1829 if (GNUNET_YES != plugin->have_mac)
1831 LOG (GNUNET_ERROR_TYPE_DEBUG,
1832 "Rejecting MAC `%s': I don't know my MAC!\n",
1833 mac_to_string (addr));
1834 return GNUNET_NO; /* don't know my MAC */
1836 if (0 != memcmp (&wa->mac,
1837 &plugin->mac_address,
1840 LOG (GNUNET_ERROR_TYPE_DEBUG,
1841 "Rejecting MAC `%s': not my MAC!\n",
1842 mac_to_string (addr));
1843 return GNUNET_NO; /* not my MAC */
1850 * Convert the transports address to a nice, human-readable format.
1852 * @param cls closure
1853 * @param type name of the transport that generated the address
1854 * @param addr one of the addresses of the host, NULL for the last address
1855 * the specific address format depends on the transport
1856 * @param addrlen length of the address
1857 * @param numeric should (IP) addresses be displayed in numeric form?
1858 * @param timeout after how long should we give up?
1859 * @param asc function to call on each string
1860 * @param asc_cls closure for @a asc
1863 wlan_plugin_address_pretty_printer (void *cls,
1868 struct GNUNET_TIME_Relative timeout,
1869 GNUNET_TRANSPORT_AddressStringCallback asc,
1874 if (sizeof (struct WlanAddress) == addrlen)
1875 ret = wlan_plugin_address_to_string (NULL,
1882 (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
1883 asc (asc_cls, NULL, GNUNET_OK);
1888 * Exit point from the plugin.
1890 * @param cls pointer to the api struct
1893 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1895 struct WlanAddress wa;
1896 struct GNUNET_HELLO_Address *address;
1897 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1898 struct Plugin *plugin = api->cls;
1899 struct MacEndpoint *endpoint;
1900 struct MacEndpoint *endpoint_next;
1907 if (GNUNET_YES == plugin->have_mac)
1909 memset (&wa, 0, sizeof(wa));
1910 wa.options = htonl (plugin->options);
1911 wa.mac = plugin->mac_address;
1912 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1914 &wa, sizeof (struct WlanAddress),
1915 GNUNET_HELLO_ADDRESS_INFO_NONE);
1917 plugin->env->notify_address (plugin->env->cls,
1920 plugin->have_mac = GNUNET_NO;
1921 GNUNET_HELLO_address_free (address);
1924 if (GNUNET_SCHEDULER_NO_TASK != plugin->beacon_task)
1926 GNUNET_SCHEDULER_cancel (plugin->beacon_task);
1927 plugin->beacon_task = GNUNET_SCHEDULER_NO_TASK;
1929 if (NULL != plugin->suid_helper)
1931 GNUNET_HELPER_stop (plugin->suid_helper,
1933 plugin->suid_helper = NULL;
1935 endpoint_next = plugin->mac_head;
1936 while (NULL != (endpoint = endpoint_next))
1938 endpoint_next = endpoint->next;
1939 free_macendpoint (endpoint);
1941 if (NULL != plugin->fragment_data_tokenizer)
1943 GNUNET_SERVER_mst_destroy (plugin->fragment_data_tokenizer);
1944 plugin->fragment_data_tokenizer = NULL;
1946 if (NULL != plugin->wlan_header_payload_tokenizer)
1948 GNUNET_SERVER_mst_destroy (plugin->wlan_header_payload_tokenizer);
1949 plugin->wlan_header_payload_tokenizer = NULL;
1951 if (NULL != plugin->helper_payload_tokenizer)
1953 GNUNET_SERVER_mst_destroy (plugin->helper_payload_tokenizer);
1954 plugin->helper_payload_tokenizer = NULL;
1956 GNUNET_free_non_null (plugin->wlan_interface);
1957 GNUNET_free (plugin);
1964 * Function called to convert a string address to
1967 * @param cls closure (`struct Plugin *`)
1968 * @param addr string address
1969 * @param addrlen length of the address
1970 * @param buf location to store the buffer
1971 * @param added location to store the number of bytes in the buffer.
1972 * If the function returns #GNUNET_SYSERR, its contents are undefined.
1973 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1976 wlan_plugin_string_to_address (void *cls,
1982 struct WlanAddress *wa;
1988 if ((NULL == addr) || (0 == addrlen))
1991 return GNUNET_SYSERR;
1993 if ('\0' != addr[addrlen - 1])
1996 return GNUNET_SYSERR;
1998 if (strlen (addr) != addrlen - 1)
2001 return GNUNET_SYSERR;
2004 if (8 != SSCANF (addr,
2005 "%4s.%u.%X:%X:%X:%X:%X:%X",
2007 &a[0], &a[1], &a[2],
2008 &a[3], &a[4], &a[5]))
2011 return GNUNET_SYSERR;
2013 wa = GNUNET_new (struct WlanAddress);
2015 wa->mac.mac[i] = a[i];
2016 wa->options = htonl (0);
2018 *added = sizeof (struct WlanAddress);
2024 * Begin monitoring sessions of a plugin. There can only
2025 * be one active monitor per plugin (i.e. if there are
2026 * multiple monitors, the transport service needs to
2027 * multiplex the generated events over all of them).
2029 * @param cls closure of the plugin
2030 * @param sic callback to invoke, NULL to disable monitor;
2031 * plugin will being by iterating over all active
2032 * sessions immediately and then enter monitor mode
2033 * @param sic_cls closure for @a sic
2036 wlan_plugin_setup_monitor (void *cls,
2037 GNUNET_TRANSPORT_SessionInfoCallback sic,
2040 struct Plugin *plugin = cls;
2041 struct MacEndpoint *mac;
2042 struct Session *session;
2045 plugin->sic_cls = sic_cls;
2048 for (mac = plugin->mac_head; NULL != mac; mac = mac->next)
2049 for (session = mac->sessions_head; NULL != session; session = session->next)
2050 notify_session_monitor (plugin,
2052 GNUNET_TRANSPORT_SS_UP);
2053 sic (sic_cls, NULL, NULL);
2060 * Function that will be called whenever the transport service wants to
2061 * notify the plugin that a session is still active and in use and
2062 * therefore the session timeout for this session has to be updated
2064 * @param cls closure
2065 * @param peer which peer was the session for
2066 * @param session which session is being updated
2069 wlan_plugin_update_session_timeout (void *cls,
2070 const struct GNUNET_PeerIdentity *peer,
2071 struct Session *session)
2073 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != session->timeout_task);
2074 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2079 * Function that will be called whenever the transport service wants to
2080 * notify the plugin that the inbound quota changed and that the plugin
2081 * should update it's delay for the next receive value
2083 * @param cls closure
2084 * @param peer which peer was the session for
2085 * @param session which session is being updated
2086 * @param delay new delay to use for receiving
2089 wlan_plugin_update_inbound_delay (void *cls,
2090 const struct GNUNET_PeerIdentity *peer,
2091 struct Session *session,
2092 struct GNUNET_TIME_Relative delay)
2094 /* does nothing, as inbound delay is not supported by WLAN */
2099 * Entry point for the plugin.
2101 * @param cls closure, the `struct GNUNET_TRANSPORT_PluginEnvironment *`
2102 * @return the `struct GNUNET_TRANSPORT_PluginFunctions *` or NULL on error
2105 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2107 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2108 struct GNUNET_TRANSPORT_PluginFunctions *api;
2109 struct Plugin *plugin;
2110 char *wlan_interface;
2111 unsigned long long testmode;
2114 /* check for 'special' mode */
2115 if (NULL == env->receive)
2117 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2118 initialze the plugin or the API */
2119 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2121 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2122 api->address_to_string = &wlan_plugin_address_to_string;
2123 api->string_to_address = &wlan_plugin_string_to_address;
2128 /* check configuration */
2130 GNUNET_CONFIGURATION_have_value (env->cfg,
2134 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2140 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2145 binary = GNUNET_OS_get_libexec_binary_path (HELPER_NAME);
2146 if ( (0 == testmode) &&
2148 GNUNET_OS_check_helper_binary (binary,
2152 LOG (GNUNET_ERROR_TYPE_ERROR,
2153 _("Helper binary `%s' not SUID, cannot run WLAN transport\n"),
2155 GNUNET_free (binary);
2158 GNUNET_free (binary);
2160 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2165 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2171 plugin = GNUNET_new (struct Plugin);
2172 plugin->wlan_interface = wlan_interface;
2174 GNUNET_STATISTICS_set (plugin->env->stats,
2175 _("# sessions allocated"),
2177 GNUNET_STATISTICS_set (plugin->env->stats,
2178 _("# MAC endpoints allocated"),
2180 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker, NULL, NULL,
2181 GNUNET_BANDWIDTH_value_init (100 * 1024 *
2184 plugin->fragment_data_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
2185 plugin->wlan_header_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
2186 plugin->helper_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
2187 plugin->beacon_task = GNUNET_SCHEDULER_add_now (&send_hello_beacon,
2190 plugin->options = 0;
2192 /* some compilers do not like switch on 'long long'... */
2193 switch ((unsigned int) testmode)
2195 case 0: /* normal */
2196 plugin->helper_argv[0] = (char *) HELPER_NAME;
2197 plugin->helper_argv[1] = wlan_interface;
2198 plugin->helper_argv[2] = NULL;
2199 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
2201 plugin->helper_argv,
2202 &handle_helper_message,
2206 case 1: /* testmode, peer 1 */
2207 plugin->helper_argv[0] = (char *) DUMMY_HELPER_NAME;
2208 plugin->helper_argv[1] = (char *) "1";
2209 plugin->helper_argv[2] = NULL;
2210 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
2212 plugin->helper_argv,
2213 &handle_helper_message,
2217 case 2: /* testmode, peer 2 */
2218 plugin->helper_argv[0] = (char *) DUMMY_HELPER_NAME;
2219 plugin->helper_argv[1] = (char *) "2";
2220 plugin->helper_argv[2] = NULL;
2221 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
2223 plugin->helper_argv,
2224 &handle_helper_message,
2232 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2234 api->send = &wlan_plugin_send;
2235 api->get_session = &wlan_plugin_get_session;
2236 api->disconnect_peer = &wlan_plugin_disconnect_peer;
2237 api->disconnect_session = &wlan_plugin_disconnect_session;
2238 api->query_keepalive_factor = &wlan_plugin_query_keepalive_factor;
2239 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2240 api->check_address = &wlan_plugin_address_suggested;
2241 api->address_to_string = &wlan_plugin_address_to_string;
2242 api->string_to_address = &wlan_plugin_string_to_address;
2243 api->get_network = &wlan_plugin_get_network;
2244 api->update_session_timeout = &wlan_plugin_update_session_timeout;
2245 api->update_inbound_delay = &wlan_plugin_update_inbound_delay;
2246 api->setup_monitor = &wlan_plugin_setup_monitor;
2251 /* end of plugin_transport_wlan.c */