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))
1697 LOG (GNUNET_ERROR_TYPE_DEBUG,
1698 "Size of packet is too small (%u bytes < %u)\n",
1699 msize, sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage));
1702 rxinfo = (const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) hdr;
1704 /* check if message is actually for us */
1705 if (0 != memcmp (&rxinfo->frame.addr3, &mac_bssid_gnunet,
1706 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1708 /* Not the GNUnet BSSID */
1711 if ( (0 != memcmp (&rxinfo->frame.addr1, &bc_all_mac,
1712 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))) &&
1713 (0 != memcmp (&rxinfo->frame.addr1, &plugin->mac_address,
1714 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))) )
1716 /* Neither broadcast nor specifically for us */
1719 if (0 == memcmp (&rxinfo->frame.addr2, &plugin->mac_address,
1720 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1722 /* packet is FROM us, thus not FOR us */
1726 GNUNET_STATISTICS_update (plugin->env->stats,
1727 _("# DATA messages processed"),
1729 LOG (GNUNET_ERROR_TYPE_DEBUG,
1730 "Receiving %u bytes of data from MAC `%s'\n",
1731 (unsigned int) (msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)),
1732 mac_to_string (&rxinfo->frame.addr2));
1733 wa.mac = rxinfo->frame.addr2;
1734 wa.options = htonl (0);
1735 mas.endpoint = create_macendpoint (plugin, &wa);
1737 (void) GNUNET_SERVER_mst_receive (plugin->helper_payload_tokenizer,
1739 (const char*) &rxinfo[1],
1740 msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage),
1741 GNUNET_YES, GNUNET_NO);
1745 LOG (GNUNET_ERROR_TYPE_DEBUG,
1746 "Unexpected message of type %u (%u bytes)",
1747 ntohs (hdr->type), ntohs (hdr->size));
1755 * Task to (periodically) send a HELLO beacon
1757 * @param cls pointer to the plugin struct
1758 * @param tc scheduler context
1761 send_hello_beacon (void *cls,
1762 const struct GNUNET_SCHEDULER_TaskContext *tc)
1764 struct Plugin *plugin = cls;
1766 uint16_t hello_size;
1767 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
1768 const struct GNUNET_MessageHeader *hello;
1770 hello = plugin->env->get_our_hello ();
1771 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1772 GNUNET_assert (sizeof (struct WlanHeader) + hello_size <= WLAN_MTU);
1773 size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + hello_size;
1775 char buf[size] GNUNET_ALIGN;
1777 LOG (GNUNET_ERROR_TYPE_DEBUG,
1778 "Sending %u byte HELLO beacon\n",
1779 (unsigned int) size);
1780 radioHeader = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage*) buf;
1781 get_radiotap_header (NULL, radioHeader, size);
1782 get_wlan_header (plugin, &radioHeader->frame, &bc_all_mac, size);
1783 memcpy (&radioHeader[1], hello, hello_size);
1785 GNUNET_HELPER_send (plugin->suid_helper,
1786 &radioHeader->header,
1787 GNUNET_YES /* can drop */,
1789 GNUNET_STATISTICS_update (plugin->env->stats,
1790 _("# HELLO beacons sent"),
1793 plugin->beacon_task =
1794 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1795 (HELLO_BEACON_SCALING_FACTOR,
1796 plugin->mac_count + 1),
1804 * Another peer has suggested an address for this
1805 * peer and transport plugin. Check that this could be a valid
1806 * address. If so, consider adding it to the list
1809 * @param cls closure
1810 * @param addr pointer to the address
1811 * @param addrlen length of @a addr
1812 * @return #GNUNET_OK if this is a plausible address for this peer
1816 wlan_plugin_address_suggested (void *cls,
1820 struct Plugin *plugin = cls;
1821 struct WlanAddress *wa = (struct WlanAddress *) addr;
1823 if (addrlen != sizeof (struct WlanAddress))
1825 GNUNET_break_op (0);
1826 return GNUNET_SYSERR;
1828 if (GNUNET_YES != plugin->have_mac)
1830 LOG (GNUNET_ERROR_TYPE_DEBUG,
1831 "Rejecting MAC `%s': I don't know my MAC!\n",
1832 mac_to_string (addr));
1833 return GNUNET_NO; /* don't know my MAC */
1835 if (0 != memcmp (&wa->mac,
1836 &plugin->mac_address,
1839 LOG (GNUNET_ERROR_TYPE_DEBUG,
1840 "Rejecting MAC `%s': not my MAC!\n",
1841 mac_to_string (addr));
1842 return GNUNET_NO; /* not my MAC */
1849 * Convert the transports address to a nice, human-readable format.
1851 * @param cls closure
1852 * @param type name of the transport that generated the address
1853 * @param addr one of the addresses of the host, NULL for the last address
1854 * the specific address format depends on the transport
1855 * @param addrlen length of the address
1856 * @param numeric should (IP) addresses be displayed in numeric form?
1857 * @param timeout after how long should we give up?
1858 * @param asc function to call on each string
1859 * @param asc_cls closure for @a asc
1862 wlan_plugin_address_pretty_printer (void *cls,
1867 struct GNUNET_TIME_Relative timeout,
1868 GNUNET_TRANSPORT_AddressStringCallback asc,
1873 if (sizeof (struct WlanAddress) == addrlen)
1874 ret = wlan_plugin_address_to_string (NULL,
1881 (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
1882 asc (asc_cls, NULL, GNUNET_OK);
1887 * Exit point from the plugin.
1889 * @param cls pointer to the api struct
1892 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1894 struct WlanAddress wa;
1895 struct GNUNET_HELLO_Address *address;
1896 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1897 struct Plugin *plugin = api->cls;
1898 struct MacEndpoint *endpoint;
1899 struct MacEndpoint *endpoint_next;
1906 if (GNUNET_YES == plugin->have_mac)
1908 memset (&wa, 0, sizeof(wa));
1909 wa.options = htonl (plugin->options);
1910 wa.mac = plugin->mac_address;
1911 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1913 &wa, sizeof (struct WlanAddress),
1914 GNUNET_HELLO_ADDRESS_INFO_NONE);
1916 plugin->env->notify_address (plugin->env->cls,
1919 plugin->have_mac = GNUNET_NO;
1920 GNUNET_HELLO_address_free (address);
1923 if (GNUNET_SCHEDULER_NO_TASK != plugin->beacon_task)
1925 GNUNET_SCHEDULER_cancel (plugin->beacon_task);
1926 plugin->beacon_task = GNUNET_SCHEDULER_NO_TASK;
1928 if (NULL != plugin->suid_helper)
1930 GNUNET_HELPER_stop (plugin->suid_helper,
1932 plugin->suid_helper = NULL;
1934 endpoint_next = plugin->mac_head;
1935 while (NULL != (endpoint = endpoint_next))
1937 endpoint_next = endpoint->next;
1938 free_macendpoint (endpoint);
1940 if (NULL != plugin->fragment_data_tokenizer)
1942 GNUNET_SERVER_mst_destroy (plugin->fragment_data_tokenizer);
1943 plugin->fragment_data_tokenizer = NULL;
1945 if (NULL != plugin->wlan_header_payload_tokenizer)
1947 GNUNET_SERVER_mst_destroy (plugin->wlan_header_payload_tokenizer);
1948 plugin->wlan_header_payload_tokenizer = NULL;
1950 if (NULL != plugin->helper_payload_tokenizer)
1952 GNUNET_SERVER_mst_destroy (plugin->helper_payload_tokenizer);
1953 plugin->helper_payload_tokenizer = NULL;
1955 GNUNET_free_non_null (plugin->wlan_interface);
1956 GNUNET_free (plugin);
1963 * Function called to convert a string address to
1966 * @param cls closure (`struct Plugin *`)
1967 * @param addr string address
1968 * @param addrlen length of the address
1969 * @param buf location to store the buffer
1970 * @param added location to store the number of bytes in the buffer.
1971 * If the function returns #GNUNET_SYSERR, its contents are undefined.
1972 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1975 wlan_plugin_string_to_address (void *cls,
1981 struct WlanAddress *wa;
1987 if ((NULL == addr) || (0 == addrlen))
1990 return GNUNET_SYSERR;
1992 if ('\0' != addr[addrlen - 1])
1995 return GNUNET_SYSERR;
1997 if (strlen (addr) != addrlen - 1)
2000 return GNUNET_SYSERR;
2003 if (8 != SSCANF (addr,
2004 "%4s.%u.%X:%X:%X:%X:%X:%X",
2006 &a[0], &a[1], &a[2],
2007 &a[3], &a[4], &a[5]))
2010 return GNUNET_SYSERR;
2012 wa = GNUNET_new (struct WlanAddress);
2014 wa->mac.mac[i] = a[i];
2015 wa->options = htonl (0);
2017 *added = sizeof (struct WlanAddress);
2023 * Begin monitoring sessions of a plugin. There can only
2024 * be one active monitor per plugin (i.e. if there are
2025 * multiple monitors, the transport service needs to
2026 * multiplex the generated events over all of them).
2028 * @param cls closure of the plugin
2029 * @param sic callback to invoke, NULL to disable monitor;
2030 * plugin will being by iterating over all active
2031 * sessions immediately and then enter monitor mode
2032 * @param sic_cls closure for @a sic
2035 wlan_plugin_setup_monitor (void *cls,
2036 GNUNET_TRANSPORT_SessionInfoCallback sic,
2039 struct Plugin *plugin = cls;
2040 struct MacEndpoint *mac;
2041 struct Session *session;
2044 plugin->sic_cls = sic_cls;
2047 for (mac = plugin->mac_head; NULL != mac; mac = mac->next)
2048 for (session = mac->sessions_head; NULL != session; session = session->next)
2049 notify_session_monitor (plugin,
2051 GNUNET_TRANSPORT_SS_UP);
2052 sic (sic_cls, NULL, NULL);
2059 * Function that will be called whenever the transport service wants to
2060 * notify the plugin that a session is still active and in use and
2061 * therefore the session timeout for this session has to be updated
2063 * @param cls closure
2064 * @param peer which peer was the session for
2065 * @param session which session is being updated
2068 wlan_plugin_update_session_timeout (void *cls,
2069 const struct GNUNET_PeerIdentity *peer,
2070 struct Session *session)
2072 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != session->timeout_task);
2073 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2078 * Function that will be called whenever the transport service wants to
2079 * notify the plugin that the inbound quota changed and that the plugin
2080 * should update it's delay for the next receive value
2082 * @param cls closure
2083 * @param peer which peer was the session for
2084 * @param session which session is being updated
2085 * @param delay new delay to use for receiving
2088 wlan_plugin_update_inbound_delay (void *cls,
2089 const struct GNUNET_PeerIdentity *peer,
2090 struct Session *session,
2091 struct GNUNET_TIME_Relative delay)
2093 /* does nothing, as inbound delay is not supported by WLAN */
2098 * Entry point for the plugin.
2100 * @param cls closure, the `struct GNUNET_TRANSPORT_PluginEnvironment *`
2101 * @return the `struct GNUNET_TRANSPORT_PluginFunctions *` or NULL on error
2104 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2106 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2107 struct GNUNET_TRANSPORT_PluginFunctions *api;
2108 struct Plugin *plugin;
2109 char *wlan_interface;
2110 unsigned long long testmode;
2113 /* check for 'special' mode */
2114 if (NULL == env->receive)
2116 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2117 initialze the plugin or the API */
2118 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2120 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2121 api->address_to_string = &wlan_plugin_address_to_string;
2122 api->string_to_address = &wlan_plugin_string_to_address;
2127 /* check configuration */
2129 GNUNET_CONFIGURATION_have_value (env->cfg,
2133 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2139 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2144 binary = GNUNET_OS_get_libexec_binary_path (HELPER_NAME);
2145 if ( (0 == testmode) &&
2147 GNUNET_OS_check_helper_binary (binary,
2151 LOG (GNUNET_ERROR_TYPE_ERROR,
2152 _("Helper binary `%s' not SUID, cannot run WLAN transport\n"),
2154 GNUNET_free (binary);
2157 GNUNET_free (binary);
2159 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2164 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2170 plugin = GNUNET_new (struct Plugin);
2171 plugin->wlan_interface = wlan_interface;
2173 GNUNET_STATISTICS_set (plugin->env->stats,
2174 _("# sessions allocated"),
2176 GNUNET_STATISTICS_set (plugin->env->stats,
2177 _("# MAC endpoints allocated"),
2179 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker, NULL, NULL,
2180 GNUNET_BANDWIDTH_value_init (100 * 1024 *
2183 plugin->fragment_data_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
2184 plugin->wlan_header_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
2185 plugin->helper_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
2186 plugin->beacon_task = GNUNET_SCHEDULER_add_now (&send_hello_beacon,
2189 plugin->options = 0;
2191 /* some compilers do not like switch on 'long long'... */
2192 switch ((unsigned int) testmode)
2194 case 0: /* normal */
2195 plugin->helper_argv[0] = (char *) HELPER_NAME;
2196 plugin->helper_argv[1] = wlan_interface;
2197 plugin->helper_argv[2] = NULL;
2198 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
2200 plugin->helper_argv,
2201 &handle_helper_message,
2205 case 1: /* testmode, peer 1 */
2206 plugin->helper_argv[0] = (char *) DUMMY_HELPER_NAME;
2207 plugin->helper_argv[1] = (char *) "1";
2208 plugin->helper_argv[2] = NULL;
2209 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
2211 plugin->helper_argv,
2212 &handle_helper_message,
2216 case 2: /* testmode, peer 2 */
2217 plugin->helper_argv[0] = (char *) DUMMY_HELPER_NAME;
2218 plugin->helper_argv[1] = (char *) "2";
2219 plugin->helper_argv[2] = NULL;
2220 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
2222 plugin->helper_argv,
2223 &handle_helper_message,
2231 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2233 api->send = &wlan_plugin_send;
2234 api->get_session = &wlan_plugin_get_session;
2235 api->disconnect_peer = &wlan_plugin_disconnect_peer;
2236 api->disconnect_session = &wlan_plugin_disconnect_session;
2237 api->query_keepalive_factor = &wlan_plugin_query_keepalive_factor;
2238 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2239 api->check_address = &wlan_plugin_address_suggested;
2240 api->address_to_string = &wlan_plugin_address_to_string;
2241 api->string_to_address = &wlan_plugin_string_to_address;
2242 api->get_network = &wlan_plugin_get_network;
2243 api->update_session_timeout = &wlan_plugin_update_session_timeout;
2244 api->update_inbound_delay = &wlan_plugin_update_inbound_delay;
2245 api->setup_monitor = &wlan_plugin_setup_monitor;
2250 /* end of plugin_transport_wlan.c */