2 This file is part of GNUnet
3 (C) 2010, 2011, 2012 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
24 * @author David Brodski
25 * @author Christian Grothoff
28 #include "gnunet_hello_lib.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_statistics_service.h"
32 #include "gnunet_transport_service.h"
33 #include "gnunet_transport_plugin.h"
34 #include "plugin_transport_wlan.h"
35 #include "gnunet_common.h"
36 #include "gnunet_crypto_lib.h"
37 #include "gnunet_fragmentation_lib.h"
38 #include "gnunet_constants.h"
40 #define LOG(kind,...) GNUNET_log_from (kind, "transport-wlan",__VA_ARGS__)
42 #define PLUGIN_NAME "wlan"
45 * Max size of packet (that we give to the WLAN driver for transmission)
50 * time out of a mac endpoint
52 #define MACENDPOINT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 2)
55 * We reduce the frequence of HELLO beacons in relation to
56 * the number of MAC addresses currently visible to us.
57 * This is the multiplication factor.
59 #define HELLO_BEACON_SCALING_FACTOR GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
62 * Maximum number of messages in defragmentation queue per MAC
64 #define MESSAGES_IN_DEFRAG_QUEUE_PER_MAC 2
67 * Link layer control fields for better compatibility
68 * (i.e. GNUnet over WLAN is not IP-over-WLAN).
70 #define WLAN_LLC_DSAP_FIELD 0x1f
71 #define WLAN_LLC_SSAP_FIELD 0x1f
74 GNUNET_NETWORK_STRUCT_BEGIN
76 * Header for messages which need fragmentation. This is the format of
77 * a message we obtain AFTER defragmentation. We then need to check
78 * the CRC and then tokenize the payload and pass it to the
85 * Message type is GNUNET_MESSAGE_TYPE_WLAN_DATA.
87 struct GNUNET_MessageHeader header;
90 * CRC32 checksum (only over the payload), in NBO.
92 uint32_t crc GNUNET_PACKED;
95 * Sender of the message.
97 struct GNUNET_PeerIdentity sender;
100 * Target of the message.
102 struct GNUNET_PeerIdentity target;
104 /* followed by payload, possibly including
105 multiple messages! */
112 uint32_t options GNUNET_PACKED;
114 struct GNUNET_TRANSPORT_WLAN_MacAddress mac;
118 GNUNET_NETWORK_STRUCT_END
122 * Information kept for each message that is yet to be fragmented and
125 struct PendingMessage
128 * next entry in the DLL
130 struct PendingMessage *next;
133 * previous entry in the DLL
135 struct PendingMessage *prev;
138 * The pending message
140 struct WlanHeader *msg;
143 * Continuation function to call once the message
144 * has been sent. Can be NULL if there is no
145 * continuation to call.
147 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
150 * Cls for transmit_cont
152 void *transmit_cont_cls;
155 * Timeout task (for this message).
157 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
163 * Session handle for connections with other peers.
168 * To whom are we talking to (set to our identity
169 * if we are still waiting for the welcome message)
171 struct GNUNET_PeerIdentity target;
174 * API requirement (must be first).
176 struct SessionHeader header;
179 * We keep all sessions in a DLL at their respective
180 * 'struct MACEndpoint'.
182 struct Session *next;
185 * We keep all sessions in a DLL at their respective
186 * 'struct MACEndpoint'.
188 struct Session *prev;
191 * MAC endpoint with the address of this peer.
193 struct MacEndpoint *mac;
196 * Address associated with this session and MAC endpoint
198 struct GNUNET_HELLO_Address *address;
201 * Head of messages currently pending for transmission to this peer.
203 struct PendingMessage *pending_message_head;
206 * Tail of messages currently pending for transmission to this peer.
208 struct PendingMessage *pending_message_tail;
211 * When should this session time out?
213 struct GNUNET_TIME_Absolute timeout;
216 * Timeout task (for the session).
218 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
223 * Struct for messages that are being fragmented in a MAC's transmission queue.
225 struct FragmentMessage
229 * This is a doubly-linked list.
231 struct FragmentMessage *next;
234 * This is a doubly-linked list.
236 struct FragmentMessage *prev;
239 * MAC endpoint this message belongs to
241 struct MacEndpoint *macendpoint;
244 * Fragmentation context
246 struct GNUNET_FRAGMENT_Context *fragcontext;
249 * Transmission handle to helper (to cancel if the frag context
250 * is destroyed early for some reason).
252 struct GNUNET_HELPER_SendHandle *sh;
255 * Intended recipient.
257 struct GNUNET_PeerIdentity target;
260 * Timeout value for the message.
262 struct GNUNET_TIME_Absolute timeout;
267 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
270 * Continuation to call when we're done with this message.
272 GNUNET_TRANSPORT_TransmitContinuation cont;
280 * Size of original message
285 * Number of bytes used to transmit message
293 * Struct to represent one network card connection
299 * We keep all MACs in a DLL in the plugin.
301 struct MacEndpoint *next;
304 * We keep all MACs in a DLL in the plugin.
306 struct MacEndpoint *prev;
309 * Pointer to the global plugin struct.
311 struct Plugin *plugin;
314 * Head of sessions that use this MAC.
316 struct Session *sessions_head;
319 * Tail of sessions that use this MAC.
321 struct Session *sessions_tail;
324 * Head of messages we are currently sending to this MAC.
326 struct FragmentMessage *sending_messages_head;
329 * Tail of messages we are currently sending to this MAC.
331 struct FragmentMessage *sending_messages_tail;
334 * Defrag context for this MAC
336 struct GNUNET_DEFRAGMENT_Context *defrag;
339 * When should this endpoint time out?
341 struct GNUNET_TIME_Absolute timeout;
346 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
349 * count of messages in the fragment out queue for this mac endpoint
351 unsigned int fragment_messages_out_count;
356 struct WlanAddress wlan_addr;
359 * Message delay for fragmentation context
361 struct GNUNET_TIME_Relative msg_delay;
364 * ACK delay for fragmentation context
366 struct GNUNET_TIME_Relative ack_delay;
369 * Desired transmission power for this MAC
374 * Desired transmission rate for this MAC
379 * Antenna we should use for this MAC
387 * Encapsulation of all of the state of the plugin.
394 struct GNUNET_TRANSPORT_PluginEnvironment *env;
397 * Handle to helper process for priviledged operations.
399 struct GNUNET_HELPER_Handle *suid_helper;
402 * ARGV-vector for the helper (all helpers take only the binary
403 * name, one actual argument, plus the NULL terminator for 'argv').
405 char * helper_argv[3];
408 * The interface of the wlan card given to us by the user.
413 * Tokenizer for demultiplexing of data packets resulting from defragmentation.
415 struct GNUNET_SERVER_MessageStreamTokenizer *fragment_data_tokenizer;
418 * Tokenizer for demultiplexing of data packets received from the suid helper
420 struct GNUNET_SERVER_MessageStreamTokenizer *helper_payload_tokenizer;
423 * Tokenizer for demultiplexing of data packets that follow the WLAN Header
425 struct GNUNET_SERVER_MessageStreamTokenizer *wlan_header_payload_tokenizer;
428 * Head of list of open connections.
430 struct MacEndpoint *mac_head;
433 * Tail of list of open connections.
435 struct MacEndpoint *mac_tail;
438 * Number of connections
440 unsigned int mac_count;
443 * Task that periodically sends a HELLO beacon via the helper.
445 GNUNET_SCHEDULER_TaskIdentifier beacon_task;
448 * Tracker for bandwidth limit
450 struct GNUNET_BANDWIDTH_Tracker tracker;
453 * The mac_address of the wlan card given to us by the helper.
455 struct GNUNET_TRANSPORT_WLAN_MacAddress mac_address;
458 * Have we received a control message with our MAC address yet?
463 * Options for addresses
471 * Information associated with a message. Can contain
472 * the session or the MAC endpoint associated with the
478 * NULL if the identity of the other peer is not known.
480 struct Session *session;
483 * MAC address of the other peer, NULL if not known.
485 struct MacEndpoint *endpoint;
489 * Function called for a quick conversion of the binary address to
490 * a numeric address. Note that the caller must not free the
491 * address and that the next call to this function is allowed
492 * to override the address again.
495 * @param addr binary address
496 * @param addrlen length of the address
497 * @return string representing the same address
500 wlan_plugin_address_to_string (void *cls, const void *addr, size_t addrlen);
503 * Print MAC addresses nicely.
505 * @param mac the mac address
506 * @return string to a static buffer with the human-readable mac, will be overwritten during the next call to this function
509 mac_to_string (const struct GNUNET_TRANSPORT_WLAN_MacAddress * mac)
511 static char macstr[20];
513 GNUNET_snprintf (macstr, sizeof (macstr), "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
514 mac->mac[0], mac->mac[1],
515 mac->mac[2], mac->mac[3], mac->mac[4], mac->mac[5]);
521 * Fill the radiotap header
523 * @param endpoint pointer to the endpoint, can be NULL
524 * @param header pointer to the radiotap header
525 * @param size total message size
528 get_radiotap_header (struct MacEndpoint *endpoint,
529 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *header,
532 header->header.type = ntohs (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER);
533 header->header.size = ntohs (size);
534 if (NULL != endpoint)
536 header->rate = endpoint->rate;
537 header->tx_power = endpoint->tx_power;
538 header->antenna = endpoint->antenna;
543 header->tx_power = 0;
550 * Generate the WLAN hardware header for one packet
552 * @param plugin the plugin handle
553 * @param header address to write the header to
554 * @param to_mac_addr address of the recipient
555 * @param size size of the whole packet, needed to calculate the time to send the packet
558 get_wlan_header (struct Plugin *plugin,
559 struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *header,
560 const struct GNUNET_TRANSPORT_WLAN_MacAddress *to_mac_addr,
563 const int rate = 11000000;
565 header->frame_control = htons (IEEE80211_FC0_TYPE_DATA);
566 header->addr1 = *to_mac_addr;
567 header->addr2 = plugin->mac_address;
568 header->addr3 = mac_bssid_gnunet;
569 header->duration = GNUNET_htole16 ((size * 1000000) / rate + 290);
570 header->sequence_control = 0; // FIXME?
571 header->llc[0] = WLAN_LLC_DSAP_FIELD;
572 header->llc[1] = WLAN_LLC_SSAP_FIELD;
573 header->llc[2] = 0; // FIXME?
574 header->llc[3] = 0; // FIXME?
579 * Send an ACK for a fragment we received.
581 * @param cls the 'struct MacEndpoint' the ACK must be sent to
582 * @param msg_id id of the message
583 * @param hdr pointer to the hdr where the ack is stored
586 send_ack (void *cls, uint32_t msg_id,
587 const struct GNUNET_MessageHeader *hdr)
589 struct MacEndpoint *endpoint = cls;
590 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage* radio_header;
591 uint16_t msize = ntohs (hdr->size);
592 size_t size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + msize;
595 if (NULL == endpoint)
601 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
606 LOG (GNUNET_ERROR_TYPE_DEBUG,
607 "Sending ACK to helper\n");
608 radio_header = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) buf;
609 get_radiotap_header (endpoint, radio_header, size);
610 get_wlan_header (endpoint->plugin,
611 &radio_header->frame,
612 &endpoint->wlan_addr.mac,
613 sizeof (endpoint->wlan_addr.mac));
614 memcpy (&radio_header[1], hdr, msize);
616 GNUNET_HELPER_send (endpoint->plugin->suid_helper,
617 &radio_header->header,
618 GNUNET_NO /* dropping ACKs is bad */,
620 GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# WLAN ACKs sent"),
626 * Handles the data after all fragments are put together
628 * @param cls macendpoint this messages belongs to
629 * @param hdr pointer to the data
632 wlan_data_message_handler (void *cls, const struct GNUNET_MessageHeader *hdr)
634 struct MacEndpoint *endpoint = cls;
635 struct Plugin *plugin = endpoint->plugin;
636 struct MacAndSession mas;
638 GNUNET_STATISTICS_update (plugin->env->stats,
639 _("# WLAN messages defragmented"), 1,
642 mas.endpoint = endpoint;
643 (void) GNUNET_SERVER_mst_receive (plugin->fragment_data_tokenizer,
647 GNUNET_YES, GNUNET_NO);
654 * @param cls our `struct Plugin`.
655 * @param session the session free
658 wlan_plugin_disconnect_session (void *cls,
659 struct Session *session)
661 struct MacEndpoint *endpoint = session->mac;
662 struct PendingMessage *pm;
664 endpoint->plugin->env->session_end (endpoint->plugin->env->cls,
667 while (NULL != (pm = session->pending_message_head))
669 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
670 session->pending_message_tail, pm);
671 if (GNUNET_SCHEDULER_NO_TASK != pm->timeout_task)
673 GNUNET_SCHEDULER_cancel (pm->timeout_task);
674 pm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
676 GNUNET_free (pm->msg);
679 GNUNET_CONTAINER_DLL_remove (endpoint->sessions_head,
680 endpoint->sessions_tail,
682 if (session->timeout_task != GNUNET_SCHEDULER_NO_TASK)
684 GNUNET_SCHEDULER_cancel (session->timeout_task);
685 session->timeout_task = GNUNET_SCHEDULER_NO_TASK;
687 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
688 _("# WLAN sessions allocated"), -1,
690 GNUNET_HELLO_address_free (session->address);
691 GNUNET_free (session);
697 * Function that is called to get the keepalive factor.
698 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
699 * calculate the interval between keepalive packets.
701 * @param cls closure with the `struct Plugin`
702 * @return keepalive factor
705 wlan_plugin_query_keepalive_factor (void *cls)
712 * A session is timing out. Clean up.
714 * @param cls pointer to the Session
715 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
718 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
720 struct Session *session = cls;
721 struct GNUNET_TIME_Relative timeout;
723 session->timeout_task = GNUNET_SCHEDULER_NO_TASK;
724 timeout = GNUNET_TIME_absolute_get_remaining (session->timeout);
725 if (0 == timeout.rel_value_us)
727 wlan_plugin_disconnect_session (session->mac->plugin,
731 session->timeout_task =
732 GNUNET_SCHEDULER_add_delayed (timeout, &session_timeout, session);
738 * Lookup a new session
740 * @param endpoint pointer to the mac endpoint of the peer
741 * @param peer peer identity to use for this session
742 * @return returns the session or NULL
744 static struct Session *
745 lookup_session (struct MacEndpoint *endpoint,
746 const struct GNUNET_PeerIdentity *peer)
748 struct Session *session;
750 for (session = endpoint->sessions_head; NULL != session; session = session->next)
751 if (0 == memcmp (peer, &session->target, sizeof (struct GNUNET_PeerIdentity)))
753 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
760 * Create a new session
762 * @param endpoint pointer to the mac endpoint of the peer
763 * @param peer peer identity to use for this session
764 * @return returns the session or NULL
766 static struct Session *
767 create_session (struct MacEndpoint *endpoint,
768 const struct GNUNET_PeerIdentity *peer)
770 struct Session *session;
772 GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# WLAN sessions allocated"), 1,
774 session = GNUNET_new (struct Session);
775 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
776 endpoint->sessions_tail,
778 session->address = GNUNET_HELLO_address_allocate (peer, PLUGIN_NAME,
779 &endpoint->wlan_addr, sizeof (endpoint->wlan_addr),
780 GNUNET_HELLO_ADDRESS_INFO_NONE);
781 session->mac = endpoint;
782 session->target = *peer;
783 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
784 session->timeout_task =
785 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &session_timeout, session);
786 LOG (GNUNET_ERROR_TYPE_DEBUG,
787 "Created new session %p for peer `%s' with endpoint %s\n",
790 mac_to_string (&endpoint->wlan_addr.mac));
797 * Look up a session for a peer and create a new session if none is found
799 * @param endpoint pointer to the mac endpoint of the peer
800 * @param peer peer identity to use for this session
801 * @return returns the session
803 static struct Session *
804 get_session (struct MacEndpoint *endpoint,
805 const struct GNUNET_PeerIdentity *peer)
807 struct Session *session;
808 if (NULL != (session = lookup_session (endpoint, peer)))
810 return create_session (endpoint, peer);
815 * Function called once we have successfully given the fragment
816 * message to the SUID helper process and we are thus ready for
819 * @param cls the 'struct FragmentMessage'
820 * @param result result of the operation (#GNUNET_OK on success, #GNUNET_NO if the helper died, #GNUNET_SYSERR
821 * if the helper was stopped)
824 fragment_transmission_done (void *cls,
827 struct FragmentMessage *fm = cls;
831 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
836 * Transmit a fragment of a message.
838 * @param cls 'struct FragmentMessage' this fragment message belongs to
839 * @param hdr pointer to the start of the fragment message
842 transmit_fragment (void *cls,
843 const struct GNUNET_MessageHeader *hdr)
845 struct FragmentMessage *fm = cls;
846 struct MacEndpoint *endpoint = fm->macendpoint;
850 if (NULL == endpoint)
856 msize = ntohs (hdr->size);
857 size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + msize;
860 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radio_header;
862 radio_header = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) buf;
863 get_radiotap_header (endpoint, radio_header, size);
864 get_wlan_header (endpoint->plugin,
865 &radio_header->frame,
866 &endpoint->wlan_addr.mac,
867 sizeof (endpoint->wlan_addr.mac));
868 memcpy (&radio_header[1], hdr, msize);
869 GNUNET_assert (NULL == fm->sh);
870 fm->sh = GNUNET_HELPER_send (endpoint->plugin->suid_helper,
871 &radio_header->header,
873 &fragment_transmission_done, fm);
874 fm->size_on_wire += size;
876 GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# WLAN message fragments sent"),
879 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
880 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
881 "# bytes currently in WLAN buffers",
883 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
884 "# bytes transmitted via WLAN",
891 * Frees the space of a message in the fragment queue (send queue)
893 * @param fm message to free
896 free_fragment_message (struct FragmentMessage *fm)
898 struct MacEndpoint *endpoint = fm->macendpoint;
900 GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# WLAN messages pending (with fragmentation)"),
902 GNUNET_CONTAINER_DLL_remove (endpoint->sending_messages_head,
903 endpoint->sending_messages_tail, fm);
906 GNUNET_HELPER_send_cancel (fm->sh);
909 GNUNET_FRAGMENT_context_destroy (fm->fragcontext,
910 &endpoint->msg_delay,
911 &endpoint->ack_delay);
912 if (fm->timeout_task != GNUNET_SCHEDULER_NO_TASK)
914 GNUNET_SCHEDULER_cancel (fm->timeout_task);
915 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
922 * A FragmentMessage has timed out. Remove it.
924 * @param cls pointer to the 'struct FragmentMessage'
925 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
928 fragmentmessage_timeout (void *cls,
929 const struct GNUNET_SCHEDULER_TaskContext *tc)
931 struct FragmentMessage *fm = cls;
933 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
934 if (NULL != fm->cont)
936 fm->cont (fm->cont_cls, &fm->target, GNUNET_SYSERR, fm->size_payload, fm->size_on_wire);
939 free_fragment_message (fm);
944 * Transmit a message to the given destination with fragmentation.
946 * @param endpoint desired destination
947 * @param timeout how long can the message wait?
948 * @param target peer that should receive the message
949 * @param msg message to transmit
950 * @param payload_size bytes of payload
951 * @param cont continuation to call once the message has
952 * been transmitted (or if the transport is ready
953 * for the next transmission call; or if the
954 * peer disconnected...); can be NULL
955 * @param cont_cls closure for cont
958 send_with_fragmentation (struct MacEndpoint *endpoint,
959 struct GNUNET_TIME_Relative timeout,
960 const struct GNUNET_PeerIdentity *target,
961 const struct GNUNET_MessageHeader *msg,
963 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
966 struct FragmentMessage *fm;
967 struct Plugin *plugin;
969 plugin = endpoint->plugin;
970 fm = GNUNET_new (struct FragmentMessage);
971 fm->macendpoint = endpoint;
972 fm->target = *target;
973 fm->size_payload = payload_size;
974 fm->size_on_wire = 0;
975 fm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
977 fm->cont_cls = cont_cls;
978 /* 1 MBit/s typical data rate, 1430 byte fragments => ~100 ms per message */
980 GNUNET_FRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
985 &transmit_fragment, fm);
987 GNUNET_SCHEDULER_add_delayed (timeout,
988 &fragmentmessage_timeout, fm);
989 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sending_messages_head,
990 endpoint->sending_messages_tail,
996 * Free a MAC endpoint.
998 * @param endpoint pointer to the MacEndpoint to free
1001 free_macendpoint (struct MacEndpoint *endpoint)
1003 struct Plugin *plugin = endpoint->plugin;
1004 struct FragmentMessage *fm;
1005 struct Session *session;
1007 GNUNET_STATISTICS_update (plugin->env->stats,
1008 _("# WLAN MAC endpoints allocated"), -1, GNUNET_NO);
1009 while (NULL != (session = endpoint->sessions_head))
1010 wlan_plugin_disconnect_session (plugin,
1012 while (NULL != (fm = endpoint->sending_messages_head))
1013 free_fragment_message (fm);
1014 GNUNET_CONTAINER_DLL_remove (plugin->mac_head,
1018 if (NULL != endpoint->defrag)
1020 GNUNET_DEFRAGMENT_context_destroy(endpoint->defrag);
1021 endpoint->defrag = NULL;
1024 plugin->mac_count--;
1025 if (GNUNET_SCHEDULER_NO_TASK != endpoint->timeout_task)
1027 GNUNET_SCHEDULER_cancel (endpoint->timeout_task);
1028 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1030 GNUNET_free (endpoint);
1035 * A MAC endpoint is timing out. Clean up.
1037 * @param cls pointer to the MacEndpoint
1038 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
1041 macendpoint_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1043 struct MacEndpoint *endpoint = cls;
1044 struct GNUNET_TIME_Relative timeout;
1046 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1047 timeout = GNUNET_TIME_absolute_get_remaining (endpoint->timeout);
1048 if (0 == timeout.rel_value_us)
1050 free_macendpoint (endpoint);
1053 endpoint->timeout_task =
1054 GNUNET_SCHEDULER_add_delayed (timeout, &macendpoint_timeout,
1060 * Find (or create) a MacEndpoint with a specific MAC address
1062 * @param plugin pointer to the plugin struct
1063 * @param mac the MAC address of the endpoint
1064 * @return handle to our data structure for this MAC
1066 static struct MacEndpoint *
1067 create_macendpoint (struct Plugin *plugin, struct WlanAddress *mac)
1069 struct MacEndpoint *pos;
1071 for (pos = plugin->mac_head; NULL != pos; pos = pos->next)
1073 if (0 == memcmp (mac, &pos->wlan_addr, sizeof (pos->wlan_addr)))
1075 LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing MAC endpoint `%s'\n",
1076 wlan_plugin_address_to_string(NULL, &pos->wlan_addr.mac, sizeof (pos->wlan_addr)));
1080 pos = GNUNET_new (struct MacEndpoint);
1081 pos->wlan_addr = (*mac);
1082 pos->plugin = plugin;
1084 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
1085 MESSAGES_IN_DEFRAG_QUEUE_PER_MAC,
1087 &wlan_data_message_handler,
1090 pos->msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1091 pos->ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
1092 pos->timeout = GNUNET_TIME_relative_to_absolute (MACENDPOINT_TIMEOUT);
1094 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
1096 GNUNET_CONTAINER_DLL_insert (plugin->mac_head, plugin->mac_tail, pos);
1097 plugin->mac_count++;
1098 GNUNET_STATISTICS_update (plugin->env->stats, _("# WLAN MAC endpoints allocated"),
1100 LOG (GNUNET_ERROR_TYPE_DEBUG, "New MAC endpoint `%s'\n",
1101 wlan_plugin_address_to_string(NULL, &pos->wlan_addr, sizeof (struct WlanAddress)));
1107 * Function obtain the network type for a session
1109 * @param cls closure ('struct Plugin*')
1110 * @param session the session
1111 * @return the network type in HBO or GNUNET_SYSERR
1113 static enum GNUNET_ATS_Network_Type
1114 wlan_get_network (void *cls,
1115 struct Session *session)
1117 GNUNET_assert (NULL != session);
1118 return GNUNET_ATS_NET_WLAN;
1123 * Creates a new outbound session the transport service will use to send data to the
1126 * @param cls the plugin
1127 * @param address the address
1128 * @return the session or NULL of max connections exceeded
1130 static struct Session *
1131 wlan_plugin_get_session (void *cls,
1132 const struct GNUNET_HELLO_Address *address)
1134 struct Plugin *plugin = cls;
1135 struct MacEndpoint *endpoint;
1137 if (NULL == address)
1139 if (sizeof (struct WlanAddress) != address->address_length)
1144 LOG (GNUNET_ERROR_TYPE_DEBUG,
1145 "Service asked to create session for peer `%s' with MAC `%s'\n",
1146 GNUNET_i2s (&address->peer),
1147 wlan_plugin_address_to_string(NULL, address->address, address->address_length));
1148 endpoint = create_macendpoint (plugin, (struct WlanAddress *) address->address);
1149 return get_session (endpoint, &address->peer);
1154 * Function that can be used to force the plugin to disconnect
1155 * from the given peer and cancel all previous transmissions
1156 * (and their continuation).
1158 * @param cls closure
1159 * @param target peer from which to disconnect
1162 wlan_plugin_disconnect_peer (void *cls,
1163 const struct GNUNET_PeerIdentity *target)
1165 struct Plugin *plugin = cls;
1166 struct Session *session;
1167 struct MacEndpoint *endpoint;
1169 for (endpoint = plugin->mac_head; NULL != endpoint; endpoint = endpoint->next)
1170 for (session = endpoint->sessions_head; NULL != session; session = session->next)
1171 if (0 == memcmp (target, &session->target,
1172 sizeof (struct GNUNET_PeerIdentity)))
1174 wlan_plugin_disconnect_session (plugin, session);
1175 break; /* inner-loop only (in case peer has another MAC as well!) */
1181 * Function that can be used by the transport service to transmit
1182 * a message using the plugin. Note that in the case of a
1183 * peer disconnecting, the continuation MUST be called
1184 * prior to the disconnect notification itself. This function
1185 * will be called with this peer's HELLO message to initiate
1186 * a fresh connection to another peer.
1188 * @param cls closure
1189 * @param session which session must be used
1190 * @param msgbuf the message to transmit
1191 * @param msgbuf_size number of bytes in 'msgbuf'
1192 * @param priority how important is the message (most plugins will
1193 * ignore message priority and just FIFO)
1194 * @param to how long to wait at most for the transmission (does not
1195 * require plugins to discard the message after the timeout,
1196 * just advisory for the desired delay; most plugins will ignore
1198 * @param cont continuation to call once the message has
1199 * been transmitted (or if the transport is ready
1200 * for the next transmission call; or if the
1201 * peer disconnected...); can be NULL
1202 * @param cont_cls closure for cont
1203 * @return number of bytes used (on the physical network, with overheads);
1204 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1205 * and does NOT mean that the message was not transmitted (DV)
1208 wlan_plugin_send (void *cls,
1209 struct Session *session,
1210 const char *msgbuf, size_t msgbuf_size,
1211 unsigned int priority,
1212 struct GNUNET_TIME_Relative to,
1213 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1215 struct Plugin *plugin = cls;
1216 struct WlanHeader *wlanheader;
1217 size_t size = msgbuf_size + sizeof (struct WlanHeader);
1218 char buf[size] GNUNET_ALIGN;
1220 LOG (GNUNET_ERROR_TYPE_DEBUG,
1221 "Transmitting %u bytes of payload to peer `%s' (starting with %u byte message of type %u)\n",
1223 GNUNET_i2s (&session->target),
1224 (unsigned int) ntohs (((struct GNUNET_MessageHeader*)msgbuf)->size),
1225 (unsigned int) ntohs (((struct GNUNET_MessageHeader*)msgbuf)->type));
1226 wlanheader = (struct WlanHeader *) buf;
1227 wlanheader->header.size = htons (msgbuf_size + sizeof (struct WlanHeader));
1228 wlanheader->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA);
1229 wlanheader->sender = *plugin->env->my_identity;
1230 wlanheader->target = session->target;
1231 wlanheader->crc = htonl (GNUNET_CRYPTO_crc32_n (msgbuf, msgbuf_size));
1232 memcpy (&wlanheader[1], msgbuf, msgbuf_size);
1234 GNUNET_STATISTICS_update (plugin->env->stats,
1235 "# bytes currently in WLAN buffers",
1236 msgbuf_size, GNUNET_NO);
1238 send_with_fragmentation (session->mac,
1241 &wlanheader->header,
1249 * We have received data from the WLAN via some session. Process depending
1250 * on the message type (HELLO, DATA, FRAGMENTATION or FRAGMENTATION-ACK).
1252 * @param cls pointer to the plugin
1253 * @param client pointer to the session this message belongs to
1254 * @param hdr start of the message
1257 process_data (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
1259 struct Plugin *plugin = cls;
1260 struct GNUNET_HELLO_Address *address;
1261 struct MacAndSession *mas = client;
1262 struct MacAndSession xmas;
1263 struct GNUNET_ATS_Information ats;
1264 struct FragmentMessage *fm;
1265 struct GNUNET_PeerIdentity tmpsource;
1266 const struct WlanHeader *wlanheader;
1270 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1271 ats.value = htonl (GNUNET_ATS_NET_WLAN);
1272 msize = ntohs (hdr->size);
1274 GNUNET_STATISTICS_update (plugin->env->stats,
1275 "# bytes received via WLAN",
1278 switch (ntohs (hdr->type))
1280 case GNUNET_MESSAGE_TYPE_HELLO:
1283 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hdr, &tmpsource))
1285 GNUNET_break_op (0);
1288 if (NULL == mas->endpoint)
1294 LOG (GNUNET_ERROR_TYPE_DEBUG,
1295 "Processing %u bytes of HELLO from peer `%s' at MAC %s\n",
1296 (unsigned int) msize,
1297 GNUNET_i2s (&tmpsource),
1298 wlan_plugin_address_to_string (NULL, &mas->endpoint->wlan_addr,
1299 sizeof (mas->endpoint->wlan_addr)));
1301 GNUNET_STATISTICS_update (plugin->env->stats,
1302 _("# HELLO messages received via WLAN"), 1,
1305 address = GNUNET_HELLO_address_allocate (&tmpsource, PLUGIN_NAME,
1306 &mas->endpoint->wlan_addr, sizeof (mas->endpoint->wlan_addr),
1307 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
1308 plugin->env->receive (plugin->env->cls,
1309 address, mas->session, hdr);
1310 plugin->env->update_address_metrics (plugin->env->cls,
1311 address, mas->session, &ats, 1);
1312 GNUNET_HELLO_address_free (address);
1314 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1315 if (NULL == mas->endpoint)
1320 LOG (GNUNET_ERROR_TYPE_DEBUG,
1321 "Processing %u bytes of FRAGMENT from MAC %s\n",
1322 (unsigned int) msize,
1323 wlan_plugin_address_to_string (NULL, &mas->endpoint->wlan_addr,
1324 sizeof (mas->endpoint->wlan_addr)));
1325 GNUNET_STATISTICS_update (plugin->env->stats,
1326 _("# fragments received via WLAN"), 1, GNUNET_NO);
1327 (void) GNUNET_DEFRAGMENT_process_fragment (mas->endpoint->defrag,
1330 case GNUNET_MESSAGE_TYPE_FRAGMENT_ACK:
1331 if (NULL == mas->endpoint)
1336 GNUNET_STATISTICS_update (plugin->env->stats, _("# ACKs received via WLAN"),
1338 for (fm = mas->endpoint->sending_messages_head; NULL != fm; fm = fm->next)
1340 ret = GNUNET_FRAGMENT_process_ack (fm->fragcontext, hdr);
1341 if (GNUNET_OK == ret)
1343 LOG (GNUNET_ERROR_TYPE_DEBUG,
1344 "Got last ACK, finished message transmission to `%s' (%p)\n",
1345 wlan_plugin_address_to_string (NULL, &mas->endpoint->wlan_addr,
1346 sizeof (mas->endpoint->wlan_addr)),
1348 mas->endpoint->timeout = GNUNET_TIME_relative_to_absolute (MACENDPOINT_TIMEOUT);
1349 if (NULL != fm->cont)
1351 fm->cont (fm->cont_cls, &fm->target, GNUNET_OK, fm->size_payload, fm->size_on_wire);
1354 free_fragment_message (fm);
1357 if (GNUNET_NO == ret)
1359 LOG (GNUNET_ERROR_TYPE_DEBUG,
1360 "Got an ACK, message transmission to `%s' not yet finished\n",
1361 wlan_plugin_address_to_string (NULL, &mas->endpoint->wlan_addr,
1362 sizeof (mas->endpoint->wlan_addr)));
1366 LOG (GNUNET_ERROR_TYPE_DEBUG,
1367 "ACK not matched against any active fragmentation with MAC `%s'\n",
1368 wlan_plugin_address_to_string (NULL, &mas->endpoint->wlan_addr,
1369 sizeof (mas->endpoint->wlan_addr)));
1371 case GNUNET_MESSAGE_TYPE_WLAN_DATA:
1372 if (NULL == mas->endpoint)
1377 if (msize < sizeof (struct WlanHeader))
1382 wlanheader = (const struct WlanHeader *) hdr;
1383 if (0 != memcmp (&wlanheader->target,
1384 plugin->env->my_identity,
1385 sizeof (struct GNUNET_PeerIdentity)))
1387 LOG (GNUNET_ERROR_TYPE_DEBUG,
1388 "WLAN data for `%s', not for me, ignoring\n",
1389 GNUNET_i2s (&wlanheader->target));
1392 if (ntohl (wlanheader->crc) !=
1393 GNUNET_CRYPTO_crc32_n (&wlanheader[1], msize - sizeof (struct WlanHeader)))
1395 GNUNET_STATISTICS_update (plugin->env->stats,
1396 _("# WLAN DATA messages discarded due to CRC32 error"), 1,
1400 xmas.endpoint = mas->endpoint;
1401 if (NULL == (xmas.session = lookup_session (mas->endpoint, &wlanheader->sender)))
1403 xmas.session = create_session (mas->endpoint, &wlanheader->sender);
1404 address = GNUNET_HELLO_address_allocate (&wlanheader->sender, PLUGIN_NAME,
1405 &mas->endpoint->wlan_addr, sizeof (struct WlanAddress),
1406 GNUNET_HELLO_ADDRESS_INFO_NONE);
1407 plugin->env->session_start (NULL, address, xmas.session, NULL, 0);
1408 LOG (GNUNET_ERROR_TYPE_DEBUG,
1409 "Notifying transport about peer `%s''s new session %p \n",
1410 GNUNET_i2s (&wlanheader->sender), xmas.session);
1411 GNUNET_HELLO_address_free (address);
1413 LOG (GNUNET_ERROR_TYPE_DEBUG,
1414 "Processing %u bytes of WLAN DATA from peer `%s'\n",
1415 (unsigned int) msize,
1416 GNUNET_i2s (&wlanheader->sender));
1417 (void) GNUNET_SERVER_mst_receive (plugin->wlan_header_payload_tokenizer,
1419 (const char *) &wlanheader[1],
1420 msize - sizeof (struct WlanHeader),
1421 GNUNET_YES, GNUNET_NO);
1424 if (NULL == mas->endpoint)
1429 if (NULL == mas->session)
1434 LOG (GNUNET_ERROR_TYPE_DEBUG,
1435 "Received packet with %u bytes of type %u from peer %s\n",
1436 (unsigned int) msize,
1437 (unsigned int) ntohs (hdr->type),
1438 GNUNET_i2s (&mas->session->target));
1439 plugin->env->receive (plugin->env->cls,
1440 mas->session->address,
1443 plugin->env->update_address_metrics (plugin->env->cls,
1444 mas->session->address,
1454 * Function used for to process the data from the suid process
1456 * @param cls the plugin handle
1457 * @param client client that send the data (not used)
1458 * @param hdr header of the GNUNET_MessageHeader
1461 handle_helper_message (void *cls, void *client,
1462 const struct GNUNET_MessageHeader *hdr)
1464 struct Plugin *plugin = cls;
1465 struct GNUNET_HELLO_Address *my_address;
1466 const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rxinfo;
1467 const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *cm;
1468 struct WlanAddress wa;
1469 struct MacAndSession mas;
1472 msize = ntohs (hdr->size);
1473 switch (ntohs (hdr->type))
1475 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
1476 if (msize != sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage))
1481 cm = (const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *) hdr;
1482 if (GNUNET_YES == plugin->have_mac)
1484 if (0 == memcmp (&plugin->mac_address,
1486 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1487 break; /* no change */
1488 /* remove old address */
1489 memset (&wa, 0, sizeof (struct WlanAddress));
1490 wa.mac = plugin->mac_address;
1491 wa.options = htonl(plugin->options);
1492 my_address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1493 PLUGIN_NAME, &wa, sizeof (wa), GNUNET_HELLO_ADDRESS_INFO_NONE);
1494 plugin->env->notify_address (plugin->env->cls, GNUNET_NO, my_address);
1495 GNUNET_HELLO_address_free (my_address);
1497 plugin->mac_address = cm->mac;
1498 plugin->have_mac = GNUNET_YES;
1500 memset (&wa, 0, sizeof (struct WlanAddress));
1501 wa.mac = plugin->mac_address;
1502 wa.options = htonl(plugin->options);
1503 my_address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1504 PLUGIN_NAME, &wa, sizeof (wa), GNUNET_HELLO_ADDRESS_INFO_NONE);
1506 LOG (GNUNET_ERROR_TYPE_DEBUG,
1507 "Received WLAN_HELPER_CONTROL message with MAC address `%s' for peer `%s'\n",
1508 mac_to_string (&cm->mac),
1509 GNUNET_i2s (plugin->env->my_identity));
1510 plugin->env->notify_address (plugin->env->cls, GNUNET_YES, my_address);
1511 GNUNET_HELLO_address_free (my_address);
1513 case GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER:
1514 LOG (GNUNET_ERROR_TYPE_DEBUG,
1515 "Got data message from helper with %u bytes\n",
1517 GNUNET_STATISTICS_update (plugin->env->stats,
1518 _("# DATA messages received via WLAN"), 1,
1520 if (msize < sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage))
1523 LOG (GNUNET_ERROR_TYPE_DEBUG,
1524 "Size of packet is too small (%u bytes)\n",
1528 rxinfo = (const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) hdr;
1530 /* check if message is actually for us */
1531 if (0 != memcmp (&rxinfo->frame.addr3, &mac_bssid_gnunet,
1532 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1534 /* Not the GNUnet BSSID */
1537 if ( (0 != memcmp (&rxinfo->frame.addr1, &bc_all_mac,
1538 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))) &&
1539 (0 != memcmp (&rxinfo->frame.addr1, &plugin->mac_address,
1540 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))) )
1542 /* Neither broadcast nor specifically for us */
1545 if (0 == memcmp (&rxinfo->frame.addr2, &plugin->mac_address,
1546 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1548 /* packet is FROM us, thus not FOR us */
1552 GNUNET_STATISTICS_update (plugin->env->stats,
1553 _("# WLAN DATA messages processed"),
1555 LOG (GNUNET_ERROR_TYPE_DEBUG,
1556 "Receiving %u bytes of data from MAC `%s'\n",
1557 (unsigned int) (msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)),
1558 mac_to_string (&rxinfo->frame.addr2));
1559 wa.mac = rxinfo->frame.addr2;
1560 wa.options = htonl (0);
1561 mas.endpoint = create_macendpoint (plugin, &wa);
1563 (void) GNUNET_SERVER_mst_receive (plugin->helper_payload_tokenizer,
1565 (const char*) &rxinfo[1],
1566 msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage),
1567 GNUNET_YES, GNUNET_NO);
1571 LOG (GNUNET_ERROR_TYPE_DEBUG,
1572 "Unexpected message of type %u (%u bytes)",
1573 ntohs (hdr->type), ntohs (hdr->size));
1582 * Task to (periodically) send a HELLO beacon
1584 * @param cls pointer to the plugin struct
1585 * @param tc scheduler context
1588 send_hello_beacon (void *cls,
1589 const struct GNUNET_SCHEDULER_TaskContext *tc)
1591 struct Plugin *plugin = cls;
1593 uint16_t hello_size;
1594 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
1595 const struct GNUNET_MessageHeader *hello;
1597 hello = plugin->env->get_our_hello ();
1598 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1599 GNUNET_assert (sizeof (struct WlanHeader) + hello_size <= WLAN_MTU);
1600 size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + hello_size;
1602 char buf[size] GNUNET_ALIGN;
1604 LOG (GNUNET_ERROR_TYPE_DEBUG,
1605 "Sending %u byte HELLO beacon\n",
1606 (unsigned int) size);
1607 radioHeader = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage*) buf;
1608 get_radiotap_header (NULL, radioHeader, size);
1609 get_wlan_header (plugin, &radioHeader->frame, &bc_all_mac, size);
1610 memcpy (&radioHeader[1], hello, hello_size);
1612 GNUNET_HELPER_send (plugin->suid_helper,
1613 &radioHeader->header,
1614 GNUNET_YES /* can drop */,
1616 GNUNET_STATISTICS_update (plugin->env->stats, _("# HELLO beacons sent via WLAN"),
1619 plugin->beacon_task =
1620 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1621 (HELLO_BEACON_SCALING_FACTOR,
1622 plugin->mac_count + 1),
1630 * Another peer has suggested an address for this
1631 * peer and transport plugin. Check that this could be a valid
1632 * address. If so, consider adding it to the list
1635 * @param cls closure
1636 * @param addr pointer to the address
1637 * @param addrlen length of addr
1638 * @return GNUNET_OK if this is a plausible address for this peer
1642 wlan_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1644 struct Plugin *plugin = cls;
1645 struct WlanAddress *wa = (struct WlanAddress *) addr;
1647 if (addrlen != sizeof (struct WlanAddress))
1649 GNUNET_break_op (0);
1650 return GNUNET_SYSERR;
1652 if (GNUNET_YES != plugin->have_mac)
1654 LOG (GNUNET_ERROR_TYPE_DEBUG,
1655 "Rejecting MAC `%s': I don't know my MAC!\n",
1656 mac_to_string (addr));
1657 return GNUNET_NO; /* don't know my MAC */
1659 if (0 != memcmp (&wa->mac,
1660 &plugin->mac_address,
1663 LOG (GNUNET_ERROR_TYPE_DEBUG,
1664 "Rejecting MAC `%s': not my MAC!\n",
1665 mac_to_string (addr));
1666 return GNUNET_NO; /* not my MAC */
1673 * Function called for a quick conversion of the binary address to
1674 * a numeric address. Note that the caller must not free the
1675 * address and that the next call to this function is allowed
1676 * to override the address again.
1678 * @param cls closure
1679 * @param addr binary address
1680 * @param addrlen length of the address
1681 * @return string representing the same address
1684 wlan_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
1686 const struct GNUNET_TRANSPORT_WLAN_MacAddress *mac;
1687 static char macstr[36];
1689 if (sizeof (struct WlanAddress) != addrlen)
1694 mac = &((struct WlanAddress *) addr)->mac;
1695 GNUNET_snprintf (macstr, sizeof (macstr), "%s.%u.%s",
1696 PLUGIN_NAME, ntohl (((struct WlanAddress *) addr)->options),
1697 mac_to_string (mac));
1703 * Convert the transports address to a nice, human-readable format.
1705 * @param cls closure
1706 * @param type name of the transport that generated the address
1707 * @param addr one of the addresses of the host, NULL for the last address
1708 * the specific address format depends on the transport
1709 * @param addrlen length of the address
1710 * @param numeric should (IP) addresses be displayed in numeric form?
1711 * @param timeout after how long should we give up?
1712 * @param asc function to call on each string
1713 * @param asc_cls closure for asc
1716 wlan_plugin_address_pretty_printer (void *cls, const char *type,
1717 const void *addr, size_t addrlen,
1719 struct GNUNET_TIME_Relative timeout,
1720 GNUNET_TRANSPORT_AddressStringCallback asc,
1725 if (sizeof (struct WlanAddress) != addrlen)
1727 /* invalid address */
1728 LOG (GNUNET_ERROR_TYPE_WARNING,
1729 _("WLAN address with invalid size encountered\n"));
1730 asc (asc_cls, NULL);
1733 ret = GNUNET_strdup (wlan_plugin_address_to_string(NULL, addr, addrlen));
1736 asc (asc_cls, NULL);
1741 * Exit point from the plugin.
1743 * @param cls pointer to the api struct
1746 libgnunet_plugin_transport_wlan_done (void *cls)
1748 struct WlanAddress wa;
1749 struct GNUNET_HELLO_Address *address;
1750 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1751 struct Plugin *plugin = api->cls;
1752 struct MacEndpoint *endpoint;
1753 struct MacEndpoint *endpoint_next;
1761 if (GNUNET_YES == plugin->have_mac)
1763 memset (&wa, 0, sizeof(wa));
1764 wa.options = htonl (plugin->options);
1765 wa.mac = plugin->mac_address;
1766 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1767 PLUGIN_NAME, &wa, sizeof (struct WlanAddress),
1768 GNUNET_HELLO_ADDRESS_INFO_NONE);
1770 plugin->env->notify_address (plugin->env->cls, GNUNET_NO, address);
1771 plugin->have_mac = GNUNET_NO;
1773 GNUNET_HELLO_address_free (address);
1776 if (GNUNET_SCHEDULER_NO_TASK != plugin->beacon_task)
1778 GNUNET_SCHEDULER_cancel (plugin->beacon_task);
1779 plugin->beacon_task = GNUNET_SCHEDULER_NO_TASK;
1781 if (NULL != plugin->suid_helper)
1783 GNUNET_HELPER_stop (plugin->suid_helper, GNUNET_NO);
1784 plugin->suid_helper = NULL;
1786 endpoint_next = plugin->mac_head;
1787 while (NULL != (endpoint = endpoint_next))
1789 endpoint_next = endpoint->next;
1790 free_macendpoint (endpoint);
1792 if (NULL != plugin->fragment_data_tokenizer)
1794 GNUNET_SERVER_mst_destroy (plugin->fragment_data_tokenizer);
1795 plugin->fragment_data_tokenizer = NULL;
1797 if (NULL != plugin->wlan_header_payload_tokenizer)
1799 GNUNET_SERVER_mst_destroy (plugin->wlan_header_payload_tokenizer);
1800 plugin->wlan_header_payload_tokenizer = NULL;
1802 if (NULL != plugin->helper_payload_tokenizer)
1804 GNUNET_SERVER_mst_destroy (plugin->helper_payload_tokenizer);
1805 plugin->helper_payload_tokenizer = NULL;
1807 GNUNET_free_non_null (plugin->interface);
1808 GNUNET_free (plugin);
1815 * Function called to convert a string address to
1818 * @param cls closure ('struct Plugin*')
1819 * @param addr string address
1820 * @param addrlen length of the address
1821 * @param buf location to store the buffer
1822 * @param added location to store the number of bytes in the buffer.
1823 * If the function returns GNUNET_SYSERR, its contents are undefined.
1824 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1827 wlan_string_to_address (void *cls, const char *addr, uint16_t addrlen,
1828 void **buf, size_t *added)
1830 struct WlanAddress *wa;
1836 if ((NULL == addr) || (addrlen == 0))
1839 return GNUNET_SYSERR;
1841 if ('\0' != addr[addrlen - 1])
1844 return GNUNET_SYSERR;
1846 if (strlen (addr) != addrlen - 1)
1849 return GNUNET_SYSERR;
1852 if (8 != SSCANF (addr,
1853 "%4s.%u.%X:%X:%X:%X:%X:%X",
1855 &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]))
1858 return GNUNET_SYSERR;
1860 wa = GNUNET_new (struct WlanAddress);
1862 wa->mac.mac[i] = a[i];
1863 wa->options = htonl (0);
1865 *added = sizeof (struct WlanAddress);
1871 wlan_plugin_update_session_timeout (void *cls,
1872 const struct GNUNET_PeerIdentity *peer,
1873 struct Session *session)
1875 if (session->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1876 GNUNET_SCHEDULER_cancel (session->timeout_task);
1878 session->timeout_task = GNUNET_SCHEDULER_add_delayed (
1879 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &session_timeout, session);
1883 * Entry point for the plugin.
1885 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
1886 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
1889 libgnunet_plugin_transport_wlan_init (void *cls)
1891 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1892 struct GNUNET_TRANSPORT_PluginFunctions *api;
1893 struct Plugin *plugin;
1895 unsigned long long testmode;
1898 /* check for 'special' mode */
1899 if (NULL == env->receive)
1901 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1902 initialze the plugin or the API */
1903 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
1905 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
1906 api->address_to_string = &wlan_plugin_address_to_string;
1907 api->string_to_address = &wlan_string_to_address;
1912 /* check configuration */
1914 GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "TESTMODE")) &&
1916 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-wlan",
1917 "TESTMODE", &testmode)) ||
1920 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1921 "transport-wlan", "TESTMODE");
1924 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-transport-wlan");
1925 if ( (0 == testmode) &&
1926 (GNUNET_YES != GNUNET_OS_check_helper_binary (binary, GNUNET_YES, NULL)) )
1928 LOG (GNUNET_ERROR_TYPE_ERROR,
1929 _("Helper binary `%s' not SUID, cannot run WLAN transport\n"),
1930 "gnunet-helper-transport-wlan");
1931 GNUNET_free (binary);
1934 GNUNET_free (binary);
1936 GNUNET_CONFIGURATION_get_value_string
1937 (env->cfg, "transport-wlan", "INTERFACE",
1940 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1941 "transport-wlan", "INTERFACE");
1945 plugin = GNUNET_new (struct Plugin);
1946 plugin->interface = interface;
1948 GNUNET_STATISTICS_set (plugin->env->stats, _("# WLAN sessions allocated"),
1950 GNUNET_STATISTICS_set (plugin->env->stats, _("# WLAN MAC endpoints allocated"),
1952 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker, NULL, NULL,
1953 GNUNET_BANDWIDTH_value_init (100 * 1024 *
1955 plugin->fragment_data_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
1956 plugin->wlan_header_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
1957 plugin->helper_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
1958 plugin->beacon_task = GNUNET_SCHEDULER_add_now (&send_hello_beacon,
1961 plugin->options = 0;
1963 /* some compilers do not like switch on 'long long'... */
1964 switch ((unsigned int) testmode)
1966 case 0: /* normal */
1967 plugin->helper_argv[0] = (char *) "gnunet-helper-transport-wlan";
1968 plugin->helper_argv[1] = interface;
1969 plugin->helper_argv[2] = NULL;
1970 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
1971 "gnunet-helper-transport-wlan",
1972 plugin->helper_argv,
1973 &handle_helper_message,
1977 case 1: /* testmode, peer 1 */
1978 plugin->helper_argv[0] = (char *) "gnunet-helper-transport-wlan-dummy";
1979 plugin->helper_argv[1] = (char *) "1";
1980 plugin->helper_argv[2] = NULL;
1981 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
1982 "gnunet-helper-transport-wlan-dummy",
1983 plugin->helper_argv,
1984 &handle_helper_message,
1988 case 2: /* testmode, peer 2 */
1989 plugin->helper_argv[0] = (char *) "gnunet-helper-transport-wlan-dummy";
1990 plugin->helper_argv[1] = (char *) "2";
1991 plugin->helper_argv[2] = NULL;
1992 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
1993 "gnunet-helper-transport-wlan-dummy",
1994 plugin->helper_argv,
1995 &handle_helper_message,
2003 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2005 api->send = &wlan_plugin_send;
2006 api->get_session = &wlan_plugin_get_session;
2007 api->disconnect_peer = &wlan_plugin_disconnect_peer;
2008 api->disconnect_session = &wlan_plugin_disconnect_session;
2009 api->query_keepalive_factor = &wlan_plugin_query_keepalive_factor;
2010 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2011 api->check_address = &wlan_plugin_address_suggested;
2012 api->address_to_string = &wlan_plugin_address_to_string;
2013 api->string_to_address = &wlan_string_to_address;
2014 api->get_network = &wlan_get_network;
2015 api->update_session_timeout = &wlan_plugin_update_session_timeout;
2020 /* end of plugin_transport_wlan.c */