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 struct GNUNET_SCHEDULER_Task * 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 struct GNUNET_SCHEDULER_Task * 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 struct GNUNET_SCHEDULER_Task * timeout_task;
306 * Continuation to call when we're done with this message.
308 GNUNET_TRANSPORT_TransmitContinuation cont;
311 * Message we need to fragment and transmit, NULL after the
312 * @e fragmentcontext has been created.
314 struct GNUNET_MessageHeader *msg;
317 * Closure for @e cont
322 * Size of original message
327 * Number of bytes used to transmit message
335 * Struct to represent one network card connection
341 * We keep all MACs in a DLL in the plugin.
343 struct MacEndpoint *next;
346 * We keep all MACs in a DLL in the plugin.
348 struct MacEndpoint *prev;
351 * Pointer to the global plugin struct.
353 struct Plugin *plugin;
356 * Head of sessions that use this MAC.
358 struct Session *sessions_head;
361 * Tail of sessions that use this MAC.
363 struct Session *sessions_tail;
366 * Head of messages we are currently sending to this MAC.
368 struct FragmentMessage *sending_messages_head;
371 * Tail of messages we are currently sending to this MAC.
373 struct FragmentMessage *sending_messages_tail;
376 * Defrag context for this MAC
378 struct GNUNET_DEFRAGMENT_Context *defrag;
381 * When should this endpoint time out?
383 struct GNUNET_TIME_Absolute timeout;
388 struct GNUNET_SCHEDULER_Task * timeout_task;
391 * count of messages in the fragment out queue for this mac endpoint
393 unsigned int fragment_messages_out_count;
398 struct WlanAddress wlan_addr;
401 * Message delay for fragmentation context
403 struct GNUNET_TIME_Relative msg_delay;
406 * ACK delay for fragmentation context
408 struct GNUNET_TIME_Relative ack_delay;
411 * Desired transmission power for this MAC
416 * Desired transmission rate for this MAC
421 * Antenna we should use for this MAC
429 * Encapsulation of all of the state of the plugin.
436 struct GNUNET_TRANSPORT_PluginEnvironment *env;
439 * Handle to helper process for priviledged operations.
441 struct GNUNET_HELPER_Handle *suid_helper;
444 * Function to call about session status changes.
446 GNUNET_TRANSPORT_SessionInfoCallback sic;
449 * Closure for @e sic.
454 * ARGV-vector for the helper (all helpers take only the binary
455 * name, one actual argument, plus the NULL terminator for 'argv').
457 char *helper_argv[3];
460 * The interface of the wlan card given to us by the user.
462 char *wlan_interface;
465 * Tokenizer for demultiplexing of data packets resulting from
468 struct GNUNET_SERVER_MessageStreamTokenizer *fragment_data_tokenizer;
471 * Tokenizer for demultiplexing of data packets received from the suid helper
473 struct GNUNET_SERVER_MessageStreamTokenizer *helper_payload_tokenizer;
476 * Tokenizer for demultiplexing of data packets that follow the WLAN Header
478 struct GNUNET_SERVER_MessageStreamTokenizer *wlan_header_payload_tokenizer;
481 * Head of list of open connections.
483 struct MacEndpoint *mac_head;
486 * Tail of list of open connections.
488 struct MacEndpoint *mac_tail;
491 * Task that periodically sends a HELLO beacon via the helper.
493 struct GNUNET_SCHEDULER_Task *beacon_task;
496 * Tracker for bandwidth limit
498 struct GNUNET_BANDWIDTH_Tracker tracker;
501 * The mac_address of the wlan card given to us by the helper.
503 struct GNUNET_TRANSPORT_WLAN_MacAddress mac_address;
506 * Have we received a control message with our MAC address yet?
511 * Number of connections
513 unsigned int mac_count;
516 * Options for addresses
524 * Information associated with a message. Can contain
525 * the session or the MAC endpoint associated with the
531 * NULL if the identity of the other peer is not known.
533 struct Session *session;
536 * MAC address of the other peer, NULL if not known.
538 struct MacEndpoint *endpoint;
543 * Print MAC addresses nicely.
545 * @param mac the mac address
546 * @return string to a static buffer with
547 * the human-readable mac, will be overwritten during the next call to
551 mac_to_string (const struct GNUNET_TRANSPORT_WLAN_MacAddress * mac)
553 static char macstr[20];
555 GNUNET_snprintf (macstr,
557 "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
558 mac->mac[0], mac->mac[1],
559 mac->mac[2], mac->mac[3],
560 mac->mac[4], mac->mac[5]);
566 * Function called for a quick conversion of the binary address to
567 * a numeric address. Note that the caller must not free the
568 * address and that the next call to this function is allowed
569 * to override the address again.
572 * @param addr binary address
573 * @param addrlen length of the address
574 * @return string representing the same address
577 wlan_plugin_address_to_string (void *cls,
581 const struct GNUNET_TRANSPORT_WLAN_MacAddress *mac;
582 static char macstr[36];
584 if (sizeof (struct WlanAddress) != addrlen)
589 mac = &((struct WlanAddress *) addr)->mac;
590 GNUNET_snprintf (macstr,
594 ntohl (((struct WlanAddress *) addr)->options),
595 mac_to_string (mac));
601 * If a session monitor is attached, notify it about the new
604 * @param plugin our plugin
605 * @param session session that changed state
606 * @param state new state of the session
609 notify_session_monitor (struct Plugin *plugin,
610 struct Session *session,
611 enum GNUNET_TRANSPORT_SessionState state)
613 struct GNUNET_TRANSPORT_SessionInfo info;
615 if (NULL == plugin->sic)
617 memset (&info, 0, sizeof (info));
619 info.is_inbound = GNUNET_SYSERR; /* hard to say */
620 info.num_msg_pending = 0; /* we queue per MAC, not per peer */
621 info.num_bytes_pending = 0; /* we queue per MAC, not per peer */
622 info.receive_delay = GNUNET_TIME_UNIT_ZERO_ABS; /* not supported by WLAN */
623 info.session_timeout = session->timeout;
624 info.address = session->address;
625 plugin->sic (plugin->sic_cls,
632 * Fill the radiotap header
634 * @param endpoint pointer to the endpoint, can be NULL
635 * @param header pointer to the radiotap header
636 * @param size total message size
639 get_radiotap_header (struct MacEndpoint *endpoint,
640 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *header,
643 header->header.type = ntohs (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER);
644 header->header.size = ntohs (size);
645 if (NULL != endpoint)
647 header->rate = endpoint->rate;
648 header->tx_power = endpoint->tx_power;
649 header->antenna = endpoint->antenna;
654 header->tx_power = 0;
661 * Generate the WLAN hardware header for one packet
663 * @param plugin the plugin handle
664 * @param header address to write the header to
665 * @param to_mac_addr address of the recipient
666 * @param size size of the whole packet, needed to calculate the time to send the packet
669 get_wlan_header (struct Plugin *plugin,
670 struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *header,
671 const struct GNUNET_TRANSPORT_WLAN_MacAddress *to_mac_addr,
674 const int rate = 11000000;
676 header->frame_control = htons (IEEE80211_FC0_TYPE_DATA);
677 header->addr1 = *to_mac_addr;
678 header->addr2 = plugin->mac_address;
679 header->addr3 = mac_bssid_gnunet;
680 header->duration = GNUNET_htole16 ((size * 1000000) / rate + 290);
681 header->sequence_control = 0; // FIXME?
682 header->llc[0] = WLAN_LLC_DSAP_FIELD;
683 header->llc[1] = WLAN_LLC_SSAP_FIELD;
684 header->llc[2] = 0; // FIXME?
685 header->llc[3] = 0; // FIXME?
690 * Send an ACK for a fragment we received.
692 * @param cls the `struct MacEndpoint *` the ACK must be sent to
693 * @param msg_id id of the message
694 * @param hdr pointer to the hdr where the ack is stored
699 const struct GNUNET_MessageHeader *hdr)
701 struct MacEndpoint *endpoint = cls;
702 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage* radio_header;
703 uint16_t msize = ntohs (hdr->size);
704 size_t size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + msize;
707 if (NULL == endpoint)
712 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
717 LOG (GNUNET_ERROR_TYPE_DEBUG,
718 "Sending ACK to %s\n",
719 mac_to_string (&endpoint->wlan_addr.mac));
720 radio_header = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) buf;
721 get_radiotap_header (endpoint, radio_header, size);
722 get_wlan_header (endpoint->plugin,
723 &radio_header->frame,
724 &endpoint->wlan_addr.mac,
725 sizeof (endpoint->wlan_addr.mac));
726 memcpy (&radio_header[1], hdr, msize);
728 GNUNET_HELPER_send (endpoint->plugin->suid_helper,
729 &radio_header->header,
730 GNUNET_NO /* dropping ACKs is bad */,
732 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
739 * Handles the data after all fragments are put together
741 * @param cls macendpoint this messages belongs to
742 * @param hdr pointer to the data
745 wlan_data_message_handler (void *cls,
746 const struct GNUNET_MessageHeader *hdr)
748 struct MacEndpoint *endpoint = cls;
749 struct Plugin *plugin = endpoint->plugin;
750 struct MacAndSession mas;
752 GNUNET_STATISTICS_update (plugin->env->stats,
753 _("# Messages defragmented"),
757 mas.endpoint = endpoint;
758 (void) GNUNET_SERVER_mst_receive (plugin->fragment_data_tokenizer,
762 GNUNET_YES, GNUNET_NO);
769 * @param cls our `struct Plugin`.
770 * @param session the session free
773 wlan_plugin_disconnect_session (void *cls,
774 struct Session *session)
776 struct MacEndpoint *endpoint = session->mac;
777 struct Plugin *plugin = endpoint->plugin;
779 plugin->env->session_end (plugin->env->cls,
782 notify_session_monitor (plugin,
784 GNUNET_TRANSPORT_SS_DONE);
785 GNUNET_CONTAINER_DLL_remove (endpoint->sessions_head,
786 endpoint->sessions_tail,
788 if (session->timeout_task != NULL)
790 GNUNET_SCHEDULER_cancel (session->timeout_task);
791 session->timeout_task = NULL;
793 GNUNET_STATISTICS_update (plugin->env->stats,
794 _("# Sessions allocated"),
797 GNUNET_HELLO_address_free (session->address);
798 GNUNET_free (session);
804 * Function that is called to get the keepalive factor.
805 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
806 * calculate the interval between keepalive packets.
808 * @param cls closure with the `struct Plugin`
809 * @return keepalive factor
812 wlan_plugin_query_keepalive_factor (void *cls)
819 * A session is timing out. Clean up.
821 * @param cls pointer to the Session
825 session_timeout (void *cls,
826 const struct GNUNET_SCHEDULER_TaskContext *tc)
828 struct Session *session = cls;
829 struct GNUNET_TIME_Relative left;
831 session->timeout_task = NULL;
832 left = GNUNET_TIME_absolute_get_remaining (session->timeout);
833 if (0 != left.rel_value_us)
835 session->timeout_task =
836 GNUNET_SCHEDULER_add_delayed (left,
841 wlan_plugin_disconnect_session (session->mac->plugin,
848 * Lookup a new session
850 * @param endpoint pointer to the mac endpoint of the peer
851 * @param peer peer identity to use for this session
852 * @return returns the session or NULL
854 static struct Session *
855 lookup_session (struct MacEndpoint *endpoint,
856 const struct GNUNET_PeerIdentity *peer)
858 struct Session *session;
860 for (session = endpoint->sessions_head; NULL != session; session = session->next)
861 if (0 == memcmp (peer, &session->target, sizeof (struct GNUNET_PeerIdentity)))
868 * Create a new session
870 * @param endpoint pointer to the mac endpoint of the peer
871 * @param peer peer identity to use for this session
872 * @return returns the session or NULL
874 static struct Session *
875 create_session (struct MacEndpoint *endpoint,
876 const struct GNUNET_PeerIdentity *peer)
878 struct Session *session;
880 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
881 _("# Sessions allocated"),
884 session = GNUNET_new (struct Session);
885 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
886 endpoint->sessions_tail,
888 session->address = GNUNET_HELLO_address_allocate (peer,
890 &endpoint->wlan_addr,
891 sizeof (endpoint->wlan_addr),
892 GNUNET_HELLO_ADDRESS_INFO_NONE);
893 session->mac = endpoint;
894 session->target = *peer;
895 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
896 session->timeout_task =
897 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &session_timeout,
899 notify_session_monitor (endpoint->plugin,
901 GNUNET_TRANSPORT_SS_INIT);
902 notify_session_monitor (endpoint->plugin,
904 GNUNET_TRANSPORT_SS_UP);
905 LOG (GNUNET_ERROR_TYPE_DEBUG,
906 "Created new session %p for peer `%s' with endpoint %s\n",
909 mac_to_string (&endpoint->wlan_addr.mac));
916 * Look up a session for a peer and create a new session if none is found
918 * @param endpoint pointer to the mac endpoint of the peer
919 * @param peer peer identity to use for this session
920 * @return returns the session
922 static struct Session *
923 get_session (struct MacEndpoint *endpoint,
924 const struct GNUNET_PeerIdentity *peer)
926 struct Session *session;
928 if (NULL != (session = lookup_session (endpoint, peer)))
930 return create_session (endpoint, peer);
935 * Function called once we have successfully given the fragment
936 * message to the SUID helper process and we are thus ready for
939 * @param cls the `struct FragmentMessage *`
940 * @param result result of the operation (#GNUNET_OK on success,
941 * #GNUNET_NO if the helper died, #GNUNET_SYSERR
942 * if the helper was stopped)
945 fragment_transmission_done (void *cls,
948 struct FragmentMessage *fm = cls;
951 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
956 * Transmit a fragment of a message.
958 * @param cls `struct FragmentMessage *` this fragment message belongs to
959 * @param hdr pointer to the start of the fragment message
962 transmit_fragment (void *cls,
963 const struct GNUNET_MessageHeader *hdr)
965 struct FragmentMessage *fm = cls;
966 struct MacEndpoint *endpoint = fm->macendpoint;
970 if (NULL == endpoint)
975 msize = ntohs (hdr->size);
976 size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + msize;
979 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radio_header;
981 radio_header = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) buf;
982 get_radiotap_header (endpoint, radio_header, size);
983 LOG (GNUNET_ERROR_TYPE_DEBUG,
984 "Sending %u bytes of data to MAC `%s'\n",
985 (unsigned int) msize,
986 mac_to_string (&endpoint->wlan_addr.mac));
988 get_wlan_header (endpoint->plugin,
989 &radio_header->frame,
990 &endpoint->wlan_addr.mac,
991 sizeof (endpoint->wlan_addr.mac));
992 memcpy (&radio_header[1], hdr, msize);
993 GNUNET_assert (NULL == fm->sh);
994 fm->sh = GNUNET_HELPER_send (endpoint->plugin->suid_helper,
995 &radio_header->header,
997 &fragment_transmission_done, fm);
998 fm->size_on_wire += size;
1001 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
1002 _("# message fragments sent"),
1008 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
1010 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
1011 "# bytes currently in buffers",
1013 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
1014 "# bytes transmitted",
1021 * Frees the space of a message in the fragment queue (send queue)
1023 * @param fm message to free
1026 free_fragment_message (struct FragmentMessage *fm)
1028 struct MacEndpoint *endpoint = fm->macendpoint;
1030 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
1031 _("# messages pending (with fragmentation)"),
1033 GNUNET_CONTAINER_DLL_remove (endpoint->sending_messages_head,
1034 endpoint->sending_messages_tail,
1038 GNUNET_HELPER_send_cancel (fm->sh);
1041 if (NULL != fm->msg)
1043 GNUNET_free (fm->msg);
1046 if (NULL != fm->fragcontext)
1048 GNUNET_FRAGMENT_context_destroy (fm->fragcontext,
1049 &endpoint->msg_delay,
1050 &endpoint->ack_delay);
1051 fm->fragcontext = NULL;
1053 if (NULL != fm->timeout_task)
1055 GNUNET_SCHEDULER_cancel (fm->timeout_task);
1056 fm->timeout_task = NULL;
1063 * A FragmentMessage has timed out. Remove it.
1065 * @param cls pointer to the 'struct FragmentMessage'
1069 fragmentmessage_timeout (void *cls,
1070 const struct GNUNET_SCHEDULER_TaskContext *tc)
1072 struct FragmentMessage *fm = cls;
1074 fm->timeout_task = NULL;
1075 if (NULL != fm->cont)
1077 fm->cont (fm->cont_cls,
1084 free_fragment_message (fm);
1089 * Transmit a message to the given destination with fragmentation.
1091 * @param endpoint desired destination
1092 * @param timeout how long can the message wait?
1093 * @param target peer that should receive the message
1094 * @param msg message to transmit
1095 * @param payload_size bytes of payload
1096 * @param cont continuation to call once the message has
1097 * been transmitted (or if the transport is ready
1098 * for the next transmission call; or if the
1099 * peer disconnected...); can be NULL
1100 * @param cont_cls closure for @a cont
1103 send_with_fragmentation (struct MacEndpoint *endpoint,
1104 struct GNUNET_TIME_Relative timeout,
1105 const struct GNUNET_PeerIdentity *target,
1106 const struct GNUNET_MessageHeader *msg,
1107 size_t payload_size,
1108 GNUNET_TRANSPORT_TransmitContinuation cont,
1112 struct FragmentMessage *fm;
1113 struct Plugin *plugin;
1115 plugin = endpoint->plugin;
1116 fm = GNUNET_new (struct FragmentMessage);
1117 fm->macendpoint = endpoint;
1118 fm->target = *target;
1119 fm->size_payload = payload_size;
1120 fm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1122 fm->cont_cls = cont_cls;
1123 /* 1 MBit/s typical data rate, 1430 byte fragments => ~100 ms per message */
1125 GNUNET_SCHEDULER_add_delayed (timeout,
1126 &fragmentmessage_timeout,
1128 if (GNUNET_YES == plugin->have_mac)
1131 GNUNET_FRAGMENT_context_create (plugin->env->stats,
1134 fm->macendpoint->msg_delay,
1135 fm->macendpoint->ack_delay,
1137 &transmit_fragment, fm);
1141 fm->msg = GNUNET_copy_message (msg);
1143 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sending_messages_head,
1144 endpoint->sending_messages_tail,
1150 * Free a MAC endpoint.
1152 * @param endpoint pointer to the MacEndpoint to free
1155 free_macendpoint (struct MacEndpoint *endpoint)
1157 struct Plugin *plugin = endpoint->plugin;
1158 struct FragmentMessage *fm;
1159 struct Session *session;
1161 GNUNET_STATISTICS_update (plugin->env->stats,
1162 _("# MAC endpoints allocated"),
1165 while (NULL != (session = endpoint->sessions_head))
1166 wlan_plugin_disconnect_session (plugin,
1168 while (NULL != (fm = endpoint->sending_messages_head))
1169 free_fragment_message (fm);
1170 GNUNET_CONTAINER_DLL_remove (plugin->mac_head,
1174 if (NULL != endpoint->defrag)
1176 GNUNET_DEFRAGMENT_context_destroy(endpoint->defrag);
1177 endpoint->defrag = NULL;
1180 plugin->mac_count--;
1181 if (NULL != endpoint->timeout_task)
1183 GNUNET_SCHEDULER_cancel (endpoint->timeout_task);
1184 endpoint->timeout_task = NULL;
1186 GNUNET_free (endpoint);
1191 * A MAC endpoint is timing out. Clean up.
1193 * @param cls pointer to the `struct MacEndpoint *`
1194 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
1197 macendpoint_timeout (void *cls,
1198 const struct GNUNET_SCHEDULER_TaskContext *tc)
1200 struct MacEndpoint *endpoint = cls;
1201 struct GNUNET_TIME_Relative timeout;
1203 endpoint->timeout_task = NULL;
1204 timeout = GNUNET_TIME_absolute_get_remaining (endpoint->timeout);
1205 if (0 == timeout.rel_value_us)
1207 free_macendpoint (endpoint);
1210 endpoint->timeout_task =
1211 GNUNET_SCHEDULER_add_delayed (timeout,
1212 &macendpoint_timeout,
1218 * Find (or create) a MacEndpoint with a specific MAC address
1220 * @param plugin pointer to the plugin struct
1221 * @param mac the MAC address of the endpoint
1222 * @return handle to our data structure for this MAC
1224 static struct MacEndpoint *
1225 create_macendpoint (struct Plugin *plugin,
1226 struct WlanAddress *mac)
1228 struct MacEndpoint *pos;
1230 for (pos = plugin->mac_head; NULL != pos; pos = pos->next)
1231 if (0 == memcmp (mac, &pos->wlan_addr, sizeof (pos->wlan_addr)))
1233 pos = GNUNET_new (struct MacEndpoint);
1234 pos->wlan_addr = (*mac);
1235 pos->plugin = plugin;
1237 GNUNET_DEFRAGMENT_context_create (plugin->env->stats,
1239 MESSAGES_IN_DEFRAG_QUEUE_PER_MAC,
1241 &wlan_data_message_handler,
1244 pos->msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1245 pos->ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
1246 pos->timeout = GNUNET_TIME_relative_to_absolute (MACENDPOINT_TIMEOUT);
1248 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
1250 GNUNET_CONTAINER_DLL_insert (plugin->mac_head,
1253 plugin->mac_count++;
1254 GNUNET_STATISTICS_update (plugin->env->stats,
1255 _("# MAC endpoints allocated"),
1257 LOG (GNUNET_ERROR_TYPE_DEBUG,
1258 "New MAC endpoint `%s'\n",
1259 wlan_plugin_address_to_string (NULL,
1261 sizeof (struct WlanAddress)));
1267 * Function obtain the network type for a session
1269 * @param cls closure (`struct Plugin*`)
1270 * @param session the session
1271 * @return the network type in HBO or #GNUNET_SYSERR
1273 static enum GNUNET_ATS_Network_Type
1274 wlan_plugin_get_network (void *cls,
1275 struct Session *session)
1278 return GNUNET_ATS_NET_WLAN;
1280 return GNUNET_ATS_NET_BT;
1286 * Creates a new outbound session the transport service will use to
1287 * send data to the peer
1289 * @param cls the `struct Plugin *`
1290 * @param address the address
1291 * @return the session or NULL of max connections exceeded
1293 static struct Session *
1294 wlan_plugin_get_session (void *cls,
1295 const struct GNUNET_HELLO_Address *address)
1297 struct Plugin *plugin = cls;
1298 struct MacEndpoint *endpoint;
1300 if (NULL == address)
1302 if (sizeof (struct WlanAddress) != address->address_length)
1307 LOG (GNUNET_ERROR_TYPE_DEBUG,
1308 "Service asked to create session for peer `%s' with MAC `%s'\n",
1309 GNUNET_i2s (&address->peer),
1310 wlan_plugin_address_to_string (NULL,
1312 address->address_length));
1313 endpoint = create_macendpoint (plugin,
1314 (struct WlanAddress *) address->address);
1315 return get_session (endpoint, &address->peer);
1320 * Function that can be used to force the plugin to disconnect
1321 * from the given peer and cancel all previous transmissions
1322 * (and their continuation).
1324 * @param cls closure
1325 * @param target peer from which to disconnect
1328 wlan_plugin_disconnect_peer (void *cls,
1329 const struct GNUNET_PeerIdentity *target)
1331 struct Plugin *plugin = cls;
1332 struct Session *session;
1333 struct MacEndpoint *endpoint;
1335 for (endpoint = plugin->mac_head; NULL != endpoint; endpoint = endpoint->next)
1336 for (session = endpoint->sessions_head; NULL != session; session = session->next)
1337 if (0 == memcmp (target, &session->target,
1338 sizeof (struct GNUNET_PeerIdentity)))
1340 wlan_plugin_disconnect_session (plugin, session);
1341 break; /* inner-loop only (in case peer has another MAC as well!) */
1347 * Function that can be used by the transport service to transmit
1348 * a message using the plugin. Note that in the case of a
1349 * peer disconnecting, the continuation MUST be called
1350 * prior to the disconnect notification itself. This function
1351 * will be called with this peer's HELLO message to initiate
1352 * a fresh connection to another peer.
1354 * @param cls closure
1355 * @param session which session must be used
1356 * @param msgbuf the message to transmit
1357 * @param msgbuf_size number of bytes in @a msgbuf
1358 * @param priority how important is the message (most plugins will
1359 * ignore message priority and just FIFO)
1360 * @param to how long to wait at most for the transmission (does not
1361 * require plugins to discard the message after the timeout,
1362 * just advisory for the desired delay; most plugins will ignore
1364 * @param cont continuation to call once the message has
1365 * been transmitted (or if the transport is ready
1366 * for the next transmission call; or if the
1367 * peer disconnected...); can be NULL
1368 * @param cont_cls closure for @a cont
1369 * @return number of bytes used (on the physical network, with overheads);
1370 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1371 * and does NOT mean that the message was not transmitted (DV)
1374 wlan_plugin_send (void *cls,
1375 struct Session *session,
1376 const char *msgbuf, size_t msgbuf_size,
1377 unsigned int priority,
1378 struct GNUNET_TIME_Relative to,
1379 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1381 struct Plugin *plugin = cls;
1382 struct WlanHeader *wlanheader;
1383 size_t size = msgbuf_size + sizeof (struct WlanHeader);
1384 char buf[size] GNUNET_ALIGN;
1386 LOG (GNUNET_ERROR_TYPE_DEBUG,
1387 "Transmitting %u bytes of payload to peer `%s' (starting with %u byte message of type %u)\n",
1389 GNUNET_i2s (&session->target),
1390 (unsigned int) ntohs (((struct GNUNET_MessageHeader*)msgbuf)->size),
1391 (unsigned int) ntohs (((struct GNUNET_MessageHeader*)msgbuf)->type));
1392 wlanheader = (struct WlanHeader *) buf;
1393 wlanheader->header.size = htons (msgbuf_size + sizeof (struct WlanHeader));
1394 wlanheader->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA);
1395 wlanheader->sender = *plugin->env->my_identity;
1396 wlanheader->target = session->target;
1397 wlanheader->crc = htonl (GNUNET_CRYPTO_crc32_n (msgbuf, msgbuf_size));
1398 memcpy (&wlanheader[1],
1401 GNUNET_STATISTICS_update (plugin->env->stats,
1402 "# bytes currently in buffers",
1405 send_with_fragmentation (session->mac,
1408 &wlanheader->header,
1416 * We have received data from the WLAN via some session. Process depending
1417 * on the message type (HELLO, DATA, FRAGMENTATION or FRAGMENTATION-ACK).
1419 * @param cls pointer to the plugin
1420 * @param client pointer to the session this message belongs to
1421 * @param hdr start of the message
1424 process_data (void *cls,
1426 const struct GNUNET_MessageHeader *hdr)
1428 struct Plugin *plugin = cls;
1429 struct GNUNET_HELLO_Address *address;
1430 struct MacAndSession *mas = client;
1431 struct GNUNET_ATS_Information ats;
1432 struct FragmentMessage *fm;
1433 struct GNUNET_PeerIdentity tmpsource;
1434 const struct WlanHeader *wlanheader;
1438 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1440 ats.value = htonl (GNUNET_ATS_NET_WLAN);
1442 ats.value = htonl (GNUNET_ATS_NET_BT);
1444 msize = ntohs (hdr->size);
1446 GNUNET_STATISTICS_update (plugin->env->stats,
1450 switch (ntohs (hdr->type))
1452 case GNUNET_MESSAGE_TYPE_HELLO:
1455 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hdr,
1458 GNUNET_break_op (0);
1461 if (NULL == mas->endpoint)
1467 LOG (GNUNET_ERROR_TYPE_DEBUG,
1468 "Processing %u bytes of HELLO from peer `%s' at MAC %s\n",
1469 (unsigned int) msize,
1470 GNUNET_i2s (&tmpsource),
1471 wlan_plugin_address_to_string (NULL,
1472 &mas->endpoint->wlan_addr,
1473 sizeof (mas->endpoint->wlan_addr)));
1475 GNUNET_STATISTICS_update (plugin->env->stats,
1476 _("# HELLO messages received"), 1,
1478 address = GNUNET_HELLO_address_allocate (&tmpsource,
1480 &mas->endpoint->wlan_addr,
1481 sizeof (mas->endpoint->wlan_addr),
1482 GNUNET_HELLO_ADDRESS_INFO_NONE);
1483 mas->session = lookup_session (mas->endpoint,
1485 if (NULL == mas->session)
1487 mas->session = create_session (mas->endpoint,
1489 plugin->env->session_start (plugin->env->cls,
1494 plugin->env->receive (plugin->env->cls,
1498 plugin->env->update_address_metrics (plugin->env->cls,
1502 GNUNET_HELLO_address_free (address);
1504 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1505 if (NULL == mas->endpoint)
1510 LOG (GNUNET_ERROR_TYPE_DEBUG,
1511 "Processing %u bytes of FRAGMENT from MAC %s\n",
1512 (unsigned int) msize,
1513 wlan_plugin_address_to_string (NULL,
1514 &mas->endpoint->wlan_addr,
1515 sizeof (mas->endpoint->wlan_addr)));
1516 GNUNET_STATISTICS_update (plugin->env->stats,
1517 _("# fragments received"),
1520 (void) GNUNET_DEFRAGMENT_process_fragment (mas->endpoint->defrag,
1523 case GNUNET_MESSAGE_TYPE_FRAGMENT_ACK:
1524 if (NULL == mas->endpoint)
1529 GNUNET_STATISTICS_update (plugin->env->stats,
1530 _("# ACKs received"),
1532 for (fm = mas->endpoint->sending_messages_head; NULL != fm; fm = fm->next)
1534 ret = GNUNET_FRAGMENT_process_ack (fm->fragcontext, hdr);
1535 if (GNUNET_OK == ret)
1537 LOG (GNUNET_ERROR_TYPE_DEBUG,
1538 "Got last ACK, finished message transmission to `%s' (%p)\n",
1539 wlan_plugin_address_to_string (NULL,
1540 &mas->endpoint->wlan_addr,
1541 sizeof (mas->endpoint->wlan_addr)),
1543 mas->endpoint->timeout = GNUNET_TIME_relative_to_absolute (MACENDPOINT_TIMEOUT);
1544 if (NULL != fm->cont)
1546 fm->cont (fm->cont_cls,
1553 free_fragment_message (fm);
1556 if (GNUNET_NO == ret)
1558 LOG (GNUNET_ERROR_TYPE_DEBUG,
1559 "Got an ACK, message transmission to `%s' not yet finished\n",
1560 wlan_plugin_address_to_string (NULL,
1561 &mas->endpoint->wlan_addr,
1562 sizeof (mas->endpoint->wlan_addr)));
1567 LOG (GNUNET_ERROR_TYPE_DEBUG,
1568 "ACK not matched against any active fragmentation with MAC `%s'\n",
1569 wlan_plugin_address_to_string (NULL,
1570 &mas->endpoint->wlan_addr,
1571 sizeof (mas->endpoint->wlan_addr)));
1573 case GNUNET_MESSAGE_TYPE_WLAN_DATA:
1574 if (NULL == mas->endpoint)
1579 if (msize < sizeof (struct WlanHeader))
1584 wlanheader = (const struct WlanHeader *) hdr;
1585 if (0 != memcmp (&wlanheader->target,
1586 plugin->env->my_identity,
1587 sizeof (struct GNUNET_PeerIdentity)))
1589 LOG (GNUNET_ERROR_TYPE_DEBUG,
1590 "Data for `%s', not for me, ignoring\n",
1591 GNUNET_i2s (&wlanheader->target));
1594 if (ntohl (wlanheader->crc) !=
1595 GNUNET_CRYPTO_crc32_n (&wlanheader[1],
1596 msize - sizeof (struct WlanHeader)))
1598 GNUNET_STATISTICS_update (plugin->env->stats,
1599 _("# DATA messages discarded due to CRC32 error"),
1604 mas->session = lookup_session (mas->endpoint,
1605 &wlanheader->sender);
1606 if (NULL == mas->session)
1608 mas->session = create_session (mas->endpoint,
1609 &wlanheader->sender);
1610 address = GNUNET_HELLO_address_allocate (&wlanheader->sender,
1612 &mas->endpoint->wlan_addr,
1613 sizeof (struct WlanAddress),
1614 GNUNET_HELLO_ADDRESS_INFO_NONE);
1615 plugin->env->session_start (plugin->env->cls,
1619 LOG (GNUNET_ERROR_TYPE_DEBUG,
1620 "Notifying transport about peer `%s''s new session %p \n",
1621 GNUNET_i2s (&wlanheader->sender),
1623 GNUNET_HELLO_address_free (address);
1625 LOG (GNUNET_ERROR_TYPE_DEBUG,
1626 "Processing %u bytes of DATA from peer `%s'\n",
1627 (unsigned int) msize,
1628 GNUNET_i2s (&wlanheader->sender));
1629 mas->session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1630 (void) GNUNET_SERVER_mst_receive (plugin->wlan_header_payload_tokenizer,
1632 (const char *) &wlanheader[1],
1633 msize - sizeof (struct WlanHeader),
1638 if (NULL == mas->endpoint)
1643 if (NULL == mas->session)
1648 LOG (GNUNET_ERROR_TYPE_DEBUG,
1649 "Received packet with %u bytes of type %u from peer %s\n",
1650 (unsigned int) msize,
1651 (unsigned int) ntohs (hdr->type),
1652 GNUNET_i2s (&mas->session->target));
1653 plugin->env->receive (plugin->env->cls,
1654 mas->session->address,
1657 plugin->env->update_address_metrics (plugin->env->cls,
1658 mas->session->address,
1668 * Task to (periodically) send a HELLO beacon
1670 * @param cls pointer to the plugin struct
1671 * @param tc scheduler context
1674 send_hello_beacon (void *cls,
1675 const struct GNUNET_SCHEDULER_TaskContext *tc)
1677 struct Plugin *plugin = cls;
1679 uint16_t hello_size;
1680 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
1681 const struct GNUNET_MessageHeader *hello;
1683 hello = plugin->env->get_our_hello ();
1684 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1685 GNUNET_assert (sizeof (struct WlanHeader) + hello_size <= WLAN_MTU);
1686 size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + hello_size;
1688 char buf[size] GNUNET_ALIGN;
1690 LOG (GNUNET_ERROR_TYPE_DEBUG,
1691 "Sending %u byte HELLO beacon\n",
1692 (unsigned int) size);
1693 radioHeader = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage*) buf;
1694 get_radiotap_header (NULL, radioHeader, size);
1695 LOG (GNUNET_ERROR_TYPE_DEBUG,
1696 "Broadcasting %u bytes of data to MAC `%s'\n",
1697 (unsigned int) size,
1698 mac_to_string (&bc_all_mac));
1699 get_wlan_header (plugin, &radioHeader->frame, &bc_all_mac, size);
1700 memcpy (&radioHeader[1], hello, hello_size);
1702 GNUNET_HELPER_send (plugin->suid_helper,
1703 &radioHeader->header,
1704 GNUNET_YES /* can drop */,
1706 GNUNET_STATISTICS_update (plugin->env->stats,
1707 _("# HELLO beacons sent"),
1710 plugin->beacon_task =
1711 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1712 (HELLO_BEACON_SCALING_FACTOR,
1713 plugin->mac_count + 1),
1721 * Function used for to process the data from the suid process
1723 * @param cls the plugin handle
1724 * @param client client that send the data (not used)
1725 * @param hdr header of the GNUNET_MessageHeader
1728 handle_helper_message (void *cls, void *client,
1729 const struct GNUNET_MessageHeader *hdr)
1731 struct Plugin *plugin = cls;
1732 struct GNUNET_HELLO_Address *my_address;
1733 const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rxinfo;
1734 const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *cm;
1735 struct WlanAddress wa;
1736 struct MacAndSession mas;
1738 struct FragmentMessage *fm;
1739 struct MacEndpoint *endpoint;
1741 msize = ntohs (hdr->size);
1742 switch (ntohs (hdr->type))
1744 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
1745 if (msize != sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage))
1750 cm = (const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *) hdr;
1751 if (GNUNET_YES == plugin->have_mac)
1753 if (0 == memcmp (&plugin->mac_address,
1755 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1756 break; /* no change */
1757 /* remove old address */
1758 memset (&wa, 0, sizeof (struct WlanAddress));
1759 wa.mac = plugin->mac_address;
1760 wa.options = htonl(plugin->options);
1761 my_address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1764 GNUNET_HELLO_ADDRESS_INFO_NONE);
1765 plugin->env->notify_address (plugin->env->cls,
1768 GNUNET_HELLO_address_free (my_address);
1769 plugin->mac_address = cm->mac;
1773 plugin->mac_address = cm->mac;
1774 plugin->have_mac = GNUNET_YES;
1775 for (endpoint = plugin->mac_head; NULL != endpoint; endpoint = endpoint->next)
1777 for (fm = endpoint->sending_messages_head; NULL != fm; fm = fm->next)
1779 if (NULL != fm->fragcontext)
1781 GNUNET_break (0); /* should not happen */
1785 GNUNET_FRAGMENT_context_create (plugin->env->stats,
1788 fm->macendpoint->msg_delay,
1789 fm->macendpoint->ack_delay,
1791 &transmit_fragment, fm);
1792 GNUNET_free (fm->msg);
1796 GNUNET_break (NULL == plugin->beacon_task);
1797 plugin->beacon_task = GNUNET_SCHEDULER_add_now (&send_hello_beacon,
1802 memset (&wa, 0, sizeof (struct WlanAddress));
1803 wa.mac = plugin->mac_address;
1804 wa.options = htonl(plugin->options);
1805 my_address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1808 GNUNET_HELLO_ADDRESS_INFO_NONE);
1810 LOG (GNUNET_ERROR_TYPE_DEBUG,
1811 "Received WLAN_HELPER_CONTROL message with MAC address `%s' for peer `%s'\n",
1812 mac_to_string (&cm->mac),
1813 GNUNET_i2s (plugin->env->my_identity));
1814 plugin->env->notify_address (plugin->env->cls,
1817 GNUNET_HELLO_address_free (my_address);
1819 case GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER:
1820 LOG (GNUNET_ERROR_TYPE_DEBUG,
1821 "Got data message from helper with %u bytes\n",
1823 GNUNET_STATISTICS_update (plugin->env->stats,
1824 _("# DATA messages received"), 1,
1826 if (msize < sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage))
1828 LOG (GNUNET_ERROR_TYPE_DEBUG,
1829 "Size of packet is too small (%u bytes < %u)\n",
1830 msize, sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage));
1833 rxinfo = (const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) hdr;
1835 /* check if message is actually for us */
1836 if (0 != memcmp (&rxinfo->frame.addr3, &mac_bssid_gnunet,
1837 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1839 /* Not the GNUnet BSSID */
1842 if ( (0 != memcmp (&rxinfo->frame.addr1, &bc_all_mac,
1843 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))) &&
1844 (0 != memcmp (&rxinfo->frame.addr1, &plugin->mac_address,
1845 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))) )
1847 /* Neither broadcast nor specifically for us */
1850 if (0 == memcmp (&rxinfo->frame.addr2, &plugin->mac_address,
1851 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1853 /* packet is FROM us, thus not FOR us */
1857 GNUNET_STATISTICS_update (plugin->env->stats,
1858 _("# DATA messages processed"),
1860 LOG (GNUNET_ERROR_TYPE_DEBUG,
1861 "Receiving %u bytes of data from MAC `%s'\n",
1862 (unsigned int) (msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)),
1863 mac_to_string (&rxinfo->frame.addr2));
1864 LOG (GNUNET_ERROR_TYPE_DEBUG,
1865 "Receiving %u bytes of data to MAC `%s'\n",
1866 (unsigned int) (msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)),
1867 mac_to_string (&rxinfo->frame.addr1));
1868 LOG (GNUNET_ERROR_TYPE_DEBUG,
1869 "Receiving %u bytes of data with BSSID MAC `%s'\n",
1870 (unsigned int) (msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)),
1871 mac_to_string (&rxinfo->frame.addr3));
1872 wa.mac = rxinfo->frame.addr2;
1873 wa.options = htonl (0);
1874 mas.endpoint = create_macendpoint (plugin, &wa);
1876 (void) GNUNET_SERVER_mst_receive (plugin->helper_payload_tokenizer,
1878 (const char*) &rxinfo[1],
1879 msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage),
1880 GNUNET_YES, GNUNET_NO);
1884 LOG (GNUNET_ERROR_TYPE_DEBUG,
1885 "Unexpected message of type %u (%u bytes)",
1886 ntohs (hdr->type), ntohs (hdr->size));
1894 * Another peer has suggested an address for this
1895 * peer and transport plugin. Check that this could be a valid
1896 * address. If so, consider adding it to the list
1899 * @param cls closure
1900 * @param addr pointer to the address
1901 * @param addrlen length of @a addr
1902 * @return #GNUNET_OK if this is a plausible address for this peer
1906 wlan_plugin_address_suggested (void *cls,
1910 struct Plugin *plugin = cls;
1911 struct WlanAddress *wa = (struct WlanAddress *) addr;
1913 if (addrlen != sizeof (struct WlanAddress))
1915 GNUNET_break_op (0);
1916 return GNUNET_SYSERR;
1918 if (GNUNET_YES != plugin->have_mac)
1920 LOG (GNUNET_ERROR_TYPE_DEBUG,
1921 "Rejecting MAC `%s': I don't know my MAC!\n",
1922 mac_to_string (addr));
1923 return GNUNET_NO; /* don't know my MAC */
1925 if (0 != memcmp (&wa->mac,
1926 &plugin->mac_address,
1929 LOG (GNUNET_ERROR_TYPE_DEBUG,
1930 "Rejecting MAC `%s': not my MAC!\n",
1931 mac_to_string (addr));
1932 return GNUNET_NO; /* not my MAC */
1939 * Convert the transports address to a nice, human-readable format.
1941 * @param cls closure
1942 * @param type name of the transport that generated the address
1943 * @param addr one of the addresses of the host, NULL for the last address
1944 * the specific address format depends on the transport
1945 * @param addrlen length of the address
1946 * @param numeric should (IP) addresses be displayed in numeric form?
1947 * @param timeout after how long should we give up?
1948 * @param asc function to call on each string
1949 * @param asc_cls closure for @a asc
1952 wlan_plugin_address_pretty_printer (void *cls,
1957 struct GNUNET_TIME_Relative timeout,
1958 GNUNET_TRANSPORT_AddressStringCallback asc,
1963 if (sizeof (struct WlanAddress) == addrlen)
1964 ret = wlan_plugin_address_to_string (NULL,
1971 (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
1972 asc (asc_cls, NULL, GNUNET_OK);
1977 * Exit point from the plugin.
1979 * @param cls pointer to the api struct
1982 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1984 struct WlanAddress wa;
1985 struct GNUNET_HELLO_Address *address;
1986 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1987 struct Plugin *plugin = api->cls;
1988 struct MacEndpoint *endpoint;
1989 struct MacEndpoint *endpoint_next;
1996 if (GNUNET_YES == plugin->have_mac)
1998 memset (&wa, 0, sizeof(wa));
1999 wa.options = htonl (plugin->options);
2000 wa.mac = plugin->mac_address;
2001 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2003 &wa, sizeof (struct WlanAddress),
2004 GNUNET_HELLO_ADDRESS_INFO_NONE);
2006 plugin->env->notify_address (plugin->env->cls,
2009 plugin->have_mac = GNUNET_NO;
2010 GNUNET_HELLO_address_free (address);
2013 if (NULL != plugin->beacon_task)
2015 GNUNET_SCHEDULER_cancel (plugin->beacon_task);
2016 plugin->beacon_task = NULL;
2018 if (NULL != plugin->suid_helper)
2020 GNUNET_HELPER_stop (plugin->suid_helper,
2022 plugin->suid_helper = NULL;
2024 endpoint_next = plugin->mac_head;
2025 while (NULL != (endpoint = endpoint_next))
2027 endpoint_next = endpoint->next;
2028 free_macendpoint (endpoint);
2030 if (NULL != plugin->fragment_data_tokenizer)
2032 GNUNET_SERVER_mst_destroy (plugin->fragment_data_tokenizer);
2033 plugin->fragment_data_tokenizer = NULL;
2035 if (NULL != plugin->wlan_header_payload_tokenizer)
2037 GNUNET_SERVER_mst_destroy (plugin->wlan_header_payload_tokenizer);
2038 plugin->wlan_header_payload_tokenizer = NULL;
2040 if (NULL != plugin->helper_payload_tokenizer)
2042 GNUNET_SERVER_mst_destroy (plugin->helper_payload_tokenizer);
2043 plugin->helper_payload_tokenizer = NULL;
2045 GNUNET_free_non_null (plugin->wlan_interface);
2046 GNUNET_free (plugin);
2053 * Function called to convert a string address to
2056 * @param cls closure (`struct Plugin *`)
2057 * @param addr string address
2058 * @param addrlen length of the address
2059 * @param buf location to store the buffer
2060 * @param added location to store the number of bytes in the buffer.
2061 * If the function returns #GNUNET_SYSERR, its contents are undefined.
2062 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2065 wlan_plugin_string_to_address (void *cls,
2071 struct WlanAddress *wa;
2077 if ((NULL == addr) || (0 == addrlen))
2080 return GNUNET_SYSERR;
2082 if ('\0' != addr[addrlen - 1])
2085 return GNUNET_SYSERR;
2087 if (strlen (addr) != addrlen - 1)
2090 return GNUNET_SYSERR;
2093 if (8 != SSCANF (addr,
2094 "%4s.%u.%X:%X:%X:%X:%X:%X",
2096 &a[0], &a[1], &a[2],
2097 &a[3], &a[4], &a[5]))
2100 return GNUNET_SYSERR;
2102 wa = GNUNET_new (struct WlanAddress);
2104 wa->mac.mac[i] = a[i];
2105 wa->options = htonl (0);
2107 *added = sizeof (struct WlanAddress);
2113 * Begin monitoring sessions of a plugin. There can only
2114 * be one active monitor per plugin (i.e. if there are
2115 * multiple monitors, the transport service needs to
2116 * multiplex the generated events over all of them).
2118 * @param cls closure of the plugin
2119 * @param sic callback to invoke, NULL to disable monitor;
2120 * plugin will being by iterating over all active
2121 * sessions immediately and then enter monitor mode
2122 * @param sic_cls closure for @a sic
2125 wlan_plugin_setup_monitor (void *cls,
2126 GNUNET_TRANSPORT_SessionInfoCallback sic,
2129 struct Plugin *plugin = cls;
2130 struct MacEndpoint *mac;
2131 struct Session *session;
2134 plugin->sic_cls = sic_cls;
2137 for (mac = plugin->mac_head; NULL != mac; mac = mac->next)
2138 for (session = mac->sessions_head; NULL != session; session = session->next)
2140 notify_session_monitor (plugin,
2142 GNUNET_TRANSPORT_SS_INIT);
2143 notify_session_monitor (plugin,
2145 GNUNET_TRANSPORT_SS_UP);
2147 sic (sic_cls, NULL, NULL);
2154 * Function that will be called whenever the transport service wants to
2155 * notify the plugin that a session is still active and in use and
2156 * therefore the session timeout for this session has to be updated
2158 * @param cls closure
2159 * @param peer which peer was the session for
2160 * @param session which session is being updated
2163 wlan_plugin_update_session_timeout (void *cls,
2164 const struct GNUNET_PeerIdentity *peer,
2165 struct Session *session)
2167 GNUNET_assert (NULL != session->timeout_task);
2168 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2173 * Function that will be called whenever the transport service wants to
2174 * notify the plugin that the inbound quota changed and that the plugin
2175 * should update it's delay for the next receive value
2177 * @param cls closure
2178 * @param peer which peer was the session for
2179 * @param session which session is being updated
2180 * @param delay new delay to use for receiving
2183 wlan_plugin_update_inbound_delay (void *cls,
2184 const struct GNUNET_PeerIdentity *peer,
2185 struct Session *session,
2186 struct GNUNET_TIME_Relative delay)
2188 /* does nothing, as inbound delay is not supported by WLAN */
2193 * Entry point for the plugin.
2195 * @param cls closure, the `struct GNUNET_TRANSPORT_PluginEnvironment *`
2196 * @return the `struct GNUNET_TRANSPORT_PluginFunctions *` or NULL on error
2199 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2201 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2202 struct GNUNET_TRANSPORT_PluginFunctions *api;
2203 struct Plugin *plugin;
2204 char *wlan_interface;
2205 unsigned long long testmode;
2208 /* check for 'special' mode */
2209 if (NULL == env->receive)
2211 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2212 initialze the plugin or the API */
2213 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2215 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2216 api->address_to_string = &wlan_plugin_address_to_string;
2217 api->string_to_address = &wlan_plugin_string_to_address;
2222 /* check configuration */
2224 GNUNET_CONFIGURATION_have_value (env->cfg,
2228 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2234 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2239 binary = GNUNET_OS_get_libexec_binary_path (HELPER_NAME);
2240 if ( (0 == testmode) &&
2242 GNUNET_OS_check_helper_binary (binary,
2246 LOG (GNUNET_ERROR_TYPE_ERROR,
2247 _("Helper binary `%s' not SUID, cannot run WLAN transport\n"),
2249 GNUNET_free (binary);
2252 GNUNET_free (binary);
2254 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2259 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2265 plugin = GNUNET_new (struct Plugin);
2266 plugin->wlan_interface = wlan_interface;
2268 GNUNET_STATISTICS_set (plugin->env->stats,
2269 _("# sessions allocated"),
2271 GNUNET_STATISTICS_set (plugin->env->stats,
2272 _("# MAC endpoints allocated"),
2274 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker, NULL, NULL,
2275 GNUNET_BANDWIDTH_value_init (100 * 1024 *
2278 plugin->fragment_data_tokenizer = GNUNET_SERVER_mst_create (&process_data,
2280 plugin->wlan_header_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data,
2282 plugin->helper_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data,
2285 plugin->options = 0;
2287 /* some compilers do not like switch on 'long long'... */
2288 switch ((unsigned int) testmode)
2290 case 0: /* normal */
2291 plugin->helper_argv[0] = (char *) HELPER_NAME;
2292 plugin->helper_argv[1] = wlan_interface;
2293 plugin->helper_argv[2] = NULL;
2294 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
2296 plugin->helper_argv,
2297 &handle_helper_message,
2301 case 1: /* testmode, peer 1 */
2302 plugin->helper_argv[0] = (char *) DUMMY_HELPER_NAME;
2303 plugin->helper_argv[1] = (char *) "1";
2304 plugin->helper_argv[2] = NULL;
2305 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
2307 plugin->helper_argv,
2308 &handle_helper_message,
2312 case 2: /* testmode, peer 2 */
2313 plugin->helper_argv[0] = (char *) DUMMY_HELPER_NAME;
2314 plugin->helper_argv[1] = (char *) "2";
2315 plugin->helper_argv[2] = NULL;
2316 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
2318 plugin->helper_argv,
2319 &handle_helper_message,
2327 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2329 api->send = &wlan_plugin_send;
2330 api->get_session = &wlan_plugin_get_session;
2331 api->disconnect_peer = &wlan_plugin_disconnect_peer;
2332 api->disconnect_session = &wlan_plugin_disconnect_session;
2333 api->query_keepalive_factor = &wlan_plugin_query_keepalive_factor;
2334 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2335 api->check_address = &wlan_plugin_address_suggested;
2336 api->address_to_string = &wlan_plugin_address_to_string;
2337 api->string_to_address = &wlan_plugin_string_to_address;
2338 api->get_network = &wlan_plugin_get_network;
2339 api->update_session_timeout = &wlan_plugin_update_session_timeout;
2340 api->update_inbound_delay = &wlan_plugin_update_inbound_delay;
2341 api->setup_monitor = &wlan_plugin_setup_monitor;
2346 /* end of plugin_transport_wlan.c */