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 * Head of messages currently pending for transmission to this peer.
198 struct PendingMessage *pending_message_head;
201 * Tail of messages currently pending for transmission to this peer.
203 struct PendingMessage *pending_message_tail;
206 * When should this session time out?
208 struct GNUNET_TIME_Absolute timeout;
211 * Timeout task (for the session).
213 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
221 * Struct for messages that are being fragmented in a MAC's transmission queue.
223 struct FragmentMessage
227 * This is a doubly-linked list.
229 struct FragmentMessage *next;
232 * This is a doubly-linked list.
234 struct FragmentMessage *prev;
237 * MAC endpoint this message belongs to
239 struct MacEndpoint *macendpoint;
242 * Fragmentation context
244 struct GNUNET_FRAGMENT_Context *fragcontext;
247 * Transmission handle to helper (to cancel if the frag context
248 * is destroyed early for some reason).
250 struct GNUNET_HELPER_SendHandle *sh;
253 * Intended recipient.
255 struct GNUNET_PeerIdentity target;
258 * Timeout value for the message.
260 struct GNUNET_TIME_Absolute timeout;
265 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
268 * Continuation to call when we're done with this message.
270 GNUNET_TRANSPORT_TransmitContinuation cont;
278 * Size of original message
283 * Number of bytes used to transmit message
291 * Struct to represent one network card connection
297 * We keep all MACs in a DLL in the plugin.
299 struct MacEndpoint *next;
302 * We keep all MACs in a DLL in the plugin.
304 struct MacEndpoint *prev;
307 * Pointer to the global plugin struct.
309 struct Plugin *plugin;
312 * Head of sessions that use this MAC.
314 struct Session *sessions_head;
317 * Tail of sessions that use this MAC.
319 struct Session *sessions_tail;
322 * Head of messages we are currently sending to this MAC.
324 struct FragmentMessage *sending_messages_head;
327 * Tail of messages we are currently sending to this MAC.
329 struct FragmentMessage *sending_messages_tail;
332 * Defrag context for this MAC
334 struct GNUNET_DEFRAGMENT_Context *defrag;
337 * When should this endpoint time out?
339 struct GNUNET_TIME_Absolute timeout;
344 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
347 * count of messages in the fragment out queue for this mac endpoint
349 unsigned int fragment_messages_out_count;
354 struct WlanAddress addr;
357 * Inbound or outbound session
362 * Message delay for fragmentation context
364 struct GNUNET_TIME_Relative msg_delay;
367 * ACK delay for fragmentation context
369 struct GNUNET_TIME_Relative ack_delay;
372 * Desired transmission power for this MAC
377 * Desired transmission rate for this MAC
382 * Antenna we should use for this MAC
390 * Encapsulation of all of the state of the plugin.
397 struct GNUNET_TRANSPORT_PluginEnvironment *env;
400 * Handle to helper process for priviledged operations.
402 struct GNUNET_HELPER_Handle *suid_helper;
405 * ARGV-vector for the helper (all helpers take only the binary
406 * name, one actual argument, plus the NULL terminator for 'argv').
408 char * helper_argv[3];
411 * The interface of the wlan card given to us by the user.
416 * Tokenizer for demultiplexing of data packets resulting from defragmentation.
418 struct GNUNET_SERVER_MessageStreamTokenizer *fragment_data_tokenizer;
421 * Tokenizer for demultiplexing of data packets received from the suid helper
423 struct GNUNET_SERVER_MessageStreamTokenizer *helper_payload_tokenizer;
426 * Tokenizer for demultiplexing of data packets that follow the WLAN Header
428 struct GNUNET_SERVER_MessageStreamTokenizer *wlan_header_payload_tokenizer;
431 * Head of list of open connections.
433 struct MacEndpoint *mac_head;
436 * Tail of list of open connections.
438 struct MacEndpoint *mac_tail;
441 * Number of connections
443 unsigned int mac_count;
446 * Task that periodically sends a HELLO beacon via the helper.
448 GNUNET_SCHEDULER_TaskIdentifier beacon_task;
451 * Tracker for bandwidth limit
453 struct GNUNET_BANDWIDTH_Tracker tracker;
456 * The mac_address of the wlan card given to us by the helper.
458 struct GNUNET_TRANSPORT_WLAN_MacAddress mac_address;
461 * Have we received a control message with our MAC address yet?
466 * Options for addresses
474 * Information associated with a message. Can contain
475 * the session or the MAC endpoint associated with the
481 * NULL if the identity of the other peer is not known.
483 struct Session *session;
486 * MAC address of the other peer, NULL if not known.
488 struct MacEndpoint *endpoint;
492 * Function called for a quick conversion of the binary address to
493 * a numeric address. Note that the caller must not free the
494 * address and that the next call to this function is allowed
495 * to override the address again.
498 * @param addr binary address
499 * @param addrlen length of the address
500 * @return string representing the same address
503 wlan_plugin_address_to_string (void *cls, const void *addr, size_t addrlen);
506 * Print MAC addresses nicely.
508 * @param mac the mac address
509 * @return string to a static buffer with the human-readable mac, will be overwritten during the next call to this function
512 mac_to_string (const struct GNUNET_TRANSPORT_WLAN_MacAddress * mac)
514 static char macstr[20];
516 GNUNET_snprintf (macstr, sizeof (macstr), "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
517 mac->mac[0], mac->mac[1],
518 mac->mac[2], mac->mac[3], mac->mac[4], mac->mac[5]);
524 * Fill the radiotap header
526 * @param endpoint pointer to the endpoint, can be NULL
527 * @param header pointer to the radiotap header
528 * @param size total message size
531 get_radiotap_header (struct MacEndpoint *endpoint,
532 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *header,
535 header->header.type = ntohs (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER);
536 header->header.size = ntohs (size);
537 if (NULL != endpoint)
539 header->rate = endpoint->rate;
540 header->tx_power = endpoint->tx_power;
541 header->antenna = endpoint->antenna;
546 header->tx_power = 0;
553 * Generate the WLAN hardware header for one packet
555 * @param plugin the plugin handle
556 * @param header address to write the header to
557 * @param to_mac_addr address of the recipient
558 * @param size size of the whole packet, needed to calculate the time to send the packet
561 get_wlan_header (struct Plugin *plugin,
562 struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *header,
563 const struct GNUNET_TRANSPORT_WLAN_MacAddress *to_mac_addr,
566 const int rate = 11000000;
568 header->frame_control = htons (IEEE80211_FC0_TYPE_DATA);
569 header->addr1 = *to_mac_addr;
570 header->addr2 = plugin->mac_address;
571 header->addr3 = mac_bssid_gnunet;
572 header->duration = GNUNET_htole16 ((size * 1000000) / rate + 290);
573 header->sequence_control = 0; // FIXME?
574 header->llc[0] = WLAN_LLC_DSAP_FIELD;
575 header->llc[1] = WLAN_LLC_SSAP_FIELD;
576 header->llc[2] = 0; // FIXME?
577 header->llc[3] = 0; // FIXME?
582 * Send an ACK for a fragment we received.
584 * @param cls the 'struct MacEndpoint' the ACK must be sent to
585 * @param msg_id id of the message
586 * @param hdr pointer to the hdr where the ack is stored
589 send_ack (void *cls, uint32_t msg_id,
590 const struct GNUNET_MessageHeader *hdr)
592 struct MacEndpoint *endpoint = cls;
593 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage* radio_header;
594 uint16_t msize = ntohs (hdr->size);
595 size_t size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + msize;
598 if (NULL == endpoint)
604 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
609 LOG (GNUNET_ERROR_TYPE_DEBUG,
610 "Sending ACK to helper\n");
611 radio_header = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) buf;
612 get_radiotap_header (endpoint, radio_header, size);
613 get_wlan_header (endpoint->plugin,
614 &radio_header->frame,
617 memcpy (&radio_header[1], hdr, msize);
619 GNUNET_HELPER_send (endpoint->plugin->suid_helper,
620 &radio_header->header,
621 GNUNET_NO /* dropping ACKs is bad */,
623 GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# WLAN ACKs sent"),
629 * Handles the data after all fragments are put together
631 * @param cls macendpoint this messages belongs to
632 * @param hdr pointer to the data
635 wlan_data_message_handler (void *cls, const struct GNUNET_MessageHeader *hdr)
637 struct MacEndpoint *endpoint = cls;
638 struct Plugin *plugin = endpoint->plugin;
639 struct MacAndSession mas;
641 GNUNET_STATISTICS_update (plugin->env->stats,
642 _("# WLAN messages defragmented"), 1,
645 mas.endpoint = endpoint;
646 (void) GNUNET_SERVER_mst_receive (plugin->fragment_data_tokenizer,
650 GNUNET_YES, GNUNET_NO);
657 * @param cls our `struct Plugin`.
658 * @param session the session free
661 wlan_plugin_disconnect_session (void *cls,
662 struct Session *session)
664 struct MacEndpoint *endpoint = session->mac;
665 struct PendingMessage *pm;
667 endpoint->plugin->env->session_end (endpoint->plugin->env->cls,
670 while (NULL != (pm = session->pending_message_head))
672 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
673 session->pending_message_tail, pm);
674 if (GNUNET_SCHEDULER_NO_TASK != pm->timeout_task)
676 GNUNET_SCHEDULER_cancel (pm->timeout_task);
677 pm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
679 GNUNET_free (pm->msg);
682 GNUNET_CONTAINER_DLL_remove (endpoint->sessions_head,
683 endpoint->sessions_tail,
685 if (session->timeout_task != GNUNET_SCHEDULER_NO_TASK)
687 GNUNET_SCHEDULER_cancel (session->timeout_task);
688 session->timeout_task = GNUNET_SCHEDULER_NO_TASK;
690 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
691 _("# WLAN sessions allocated"), -1,
693 GNUNET_free (session);
699 * Function that is called to get the keepalive factor.
700 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
701 * calculate the interval between keepalive packets.
703 * @param cls closure with the `struct Plugin`
704 * @return keepalive factor
707 wlan_plugin_query_keepalive_factor (void *cls)
714 * A session is timing out. Clean up.
716 * @param cls pointer to the Session
717 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
720 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
722 struct Session *session = cls;
723 struct GNUNET_TIME_Relative timeout;
725 session->timeout_task = GNUNET_SCHEDULER_NO_TASK;
726 timeout = GNUNET_TIME_absolute_get_remaining (session->timeout);
727 if (0 == timeout.rel_value_us)
729 wlan_plugin_disconnect_session (session->mac->plugin,
733 session->timeout_task =
734 GNUNET_SCHEDULER_add_delayed (timeout, &session_timeout, session);
740 * Lookup a new session
742 * @param endpoint pointer to the mac endpoint of the peer
743 * @param peer peer identity to use for this session
744 * @param inbound inbound session?
745 * @return returns the session or NULL
747 static struct Session *
748 lookup_session (struct MacEndpoint *endpoint,
749 const struct GNUNET_PeerIdentity *peer,
752 struct Session *session;
754 for (session = endpoint->sessions_head; NULL != session; session = session->next)
755 if (0 == memcmp (peer, &session->target, sizeof (struct GNUNET_PeerIdentity)) &&
756 (session->inbound == inbound))
758 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
765 * Create a new session
767 * @param endpoint pointer to the mac endpoint of the peer
768 * @param peer peer identity to use for this session
769 * @param inbound inbound session?
770 * @return returns the session or NULL
772 static struct Session *
773 create_session (struct MacEndpoint *endpoint,
774 const struct GNUNET_PeerIdentity *peer, int inbound)
776 struct Session *session;
778 GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# WLAN sessions allocated"), 1,
780 session = GNUNET_malloc (sizeof (struct Session));
781 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
782 endpoint->sessions_tail,
784 session->inbound = inbound;
785 session->mac = endpoint;
786 session->target = *peer;
787 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
788 session->timeout_task =
789 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &session_timeout, session);
790 LOG (GNUNET_ERROR_TYPE_DEBUG,
791 "Created new %s session %p for peer `%s' with endpoint %s\n",
792 (GNUNET_YES == inbound) ? "inbound" : "outbound",
795 mac_to_string (&endpoint->addr.mac));
802 * Look up a session for a peer and create a new session if none is found
804 * @param endpoint pointer to the mac endpoint of the peer
805 * @param peer peer identity to use for this session
806 * @param inbound inbound session?
807 * @return returns the session
809 static struct Session *
810 get_session (struct MacEndpoint *endpoint,
811 const struct GNUNET_PeerIdentity *peer,
814 struct Session *session;
815 if (NULL != (session = lookup_session (endpoint, peer, inbound)))
817 return create_session (endpoint, peer, inbound);
822 * Function called once we have successfully given the fragment
823 * message to the SUID helper process and we are thus ready for
826 * @param cls the 'struct FragmentMessage'
827 * @param result result of the operation (#GNUNET_OK on success, #GNUNET_NO if the helper died, #GNUNET_SYSERR
828 * if the helper was stopped)
831 fragment_transmission_done (void *cls,
834 struct FragmentMessage *fm = cls;
838 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
843 * Transmit a fragment of a message.
845 * @param cls 'struct FragmentMessage' this fragment message belongs to
846 * @param hdr pointer to the start of the fragment message
849 transmit_fragment (void *cls,
850 const struct GNUNET_MessageHeader *hdr)
852 struct FragmentMessage *fm = cls;
853 struct MacEndpoint *endpoint = fm->macendpoint;
857 if (NULL == endpoint)
863 msize = ntohs (hdr->size);
864 size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + msize;
867 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radio_header;
869 radio_header = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) buf;
870 get_radiotap_header (endpoint, radio_header, size);
871 get_wlan_header (endpoint->plugin,
872 &radio_header->frame,
875 memcpy (&radio_header[1], hdr, msize);
876 GNUNET_assert (NULL == fm->sh);
877 fm->sh = GNUNET_HELPER_send (endpoint->plugin->suid_helper,
878 &radio_header->header,
880 &fragment_transmission_done, fm);
881 fm->size_on_wire += size;
883 GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# WLAN message fragments sent"),
886 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
887 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
888 "# bytes currently in WLAN buffers",
890 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
891 "# bytes transmitted via WLAN",
898 * Frees the space of a message in the fragment queue (send queue)
900 * @param fm message to free
903 free_fragment_message (struct FragmentMessage *fm)
905 struct MacEndpoint *endpoint = fm->macendpoint;
907 GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# WLAN messages pending (with fragmentation)"),
909 GNUNET_CONTAINER_DLL_remove (endpoint->sending_messages_head,
910 endpoint->sending_messages_tail, fm);
913 GNUNET_HELPER_send_cancel (fm->sh);
916 GNUNET_FRAGMENT_context_destroy (fm->fragcontext,
917 &endpoint->msg_delay,
918 &endpoint->ack_delay);
919 if (fm->timeout_task != GNUNET_SCHEDULER_NO_TASK)
921 GNUNET_SCHEDULER_cancel (fm->timeout_task);
922 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
929 * A FragmentMessage has timed out. Remove it.
931 * @param cls pointer to the 'struct FragmentMessage'
932 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
935 fragmentmessage_timeout (void *cls,
936 const struct GNUNET_SCHEDULER_TaskContext *tc)
938 struct FragmentMessage *fm = cls;
940 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
941 if (NULL != fm->cont)
943 fm->cont (fm->cont_cls, &fm->target, GNUNET_SYSERR, fm->size_payload, fm->size_on_wire);
946 free_fragment_message (fm);
951 * Transmit a message to the given destination with fragmentation.
953 * @param endpoint desired destination
954 * @param timeout how long can the message wait?
955 * @param target peer that should receive the message
956 * @param msg message to transmit
957 * @param payload_size bytes of payload
958 * @param cont continuation to call once the message has
959 * been transmitted (or if the transport is ready
960 * for the next transmission call; or if the
961 * peer disconnected...); can be NULL
962 * @param cont_cls closure for cont
965 send_with_fragmentation (struct MacEndpoint *endpoint,
966 struct GNUNET_TIME_Relative timeout,
967 const struct GNUNET_PeerIdentity *target,
968 const struct GNUNET_MessageHeader *msg,
970 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
973 struct FragmentMessage *fm;
974 struct Plugin *plugin;
976 plugin = endpoint->plugin;
977 fm = GNUNET_malloc (sizeof (struct FragmentMessage));
978 fm->macendpoint = endpoint;
979 fm->target = *target;
980 fm->size_payload = payload_size;
981 fm->size_on_wire = 0;
982 fm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
984 fm->cont_cls = cont_cls;
985 /* 1 MBit/s typical data rate, 1430 byte fragments => ~100 ms per message */
987 GNUNET_FRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
992 &transmit_fragment, fm);
994 GNUNET_SCHEDULER_add_delayed (timeout,
995 &fragmentmessage_timeout, fm);
996 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sending_messages_head,
997 endpoint->sending_messages_tail,
1003 * Free a MAC endpoint.
1005 * @param endpoint pointer to the MacEndpoint to free
1008 free_macendpoint (struct MacEndpoint *endpoint)
1010 struct Plugin *plugin = endpoint->plugin;
1011 struct FragmentMessage *fm;
1012 struct Session *session;
1014 GNUNET_STATISTICS_update (plugin->env->stats,
1015 _("# WLAN MAC endpoints allocated"), -1, GNUNET_NO);
1016 while (NULL != (session = endpoint->sessions_head))
1017 wlan_plugin_disconnect_session (plugin,
1019 while (NULL != (fm = endpoint->sending_messages_head))
1020 free_fragment_message (fm);
1021 GNUNET_CONTAINER_DLL_remove (plugin->mac_head,
1025 if (NULL != endpoint->defrag)
1027 GNUNET_DEFRAGMENT_context_destroy(endpoint->defrag);
1028 endpoint->defrag = NULL;
1031 plugin->mac_count--;
1032 if (GNUNET_SCHEDULER_NO_TASK != endpoint->timeout_task)
1034 GNUNET_SCHEDULER_cancel (endpoint->timeout_task);
1035 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1037 GNUNET_free (endpoint);
1042 * A MAC endpoint is timing out. Clean up.
1044 * @param cls pointer to the MacEndpoint
1045 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
1048 macendpoint_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1050 struct MacEndpoint *endpoint = cls;
1051 struct GNUNET_TIME_Relative timeout;
1053 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1054 timeout = GNUNET_TIME_absolute_get_remaining (endpoint->timeout);
1055 if (0 == timeout.rel_value_us)
1057 free_macendpoint (endpoint);
1060 endpoint->timeout_task =
1061 GNUNET_SCHEDULER_add_delayed (timeout, &macendpoint_timeout,
1067 * Find (or create) a MacEndpoint with a specific MAC address
1069 * @param plugin pointer to the plugin struct
1070 * @param addr the MAC address of the endpoint
1071 * @return handle to our data structure for this MAC
1073 static struct MacEndpoint *
1074 create_macendpoint (struct Plugin *plugin,
1075 const struct WlanAddress *addr)
1077 struct MacEndpoint *pos;
1079 for (pos = plugin->mac_head; NULL != pos; pos = pos->next)
1080 if (0 == memcmp (addr, &pos->addr, sizeof (struct WlanAddress)))
1082 pos = GNUNET_malloc (sizeof (struct MacEndpoint));
1084 pos->plugin = plugin;
1086 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
1087 MESSAGES_IN_DEFRAG_QUEUE_PER_MAC,
1089 &wlan_data_message_handler,
1092 pos->msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1093 pos->ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1095 pos->timeout = GNUNET_TIME_relative_to_absolute (MACENDPOINT_TIMEOUT);
1097 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
1099 GNUNET_CONTAINER_DLL_insert (plugin->mac_head, plugin->mac_tail, pos);
1100 plugin->mac_count++;
1101 GNUNET_STATISTICS_update (plugin->env->stats, _("# WLAN MAC endpoints allocated"),
1103 LOG (GNUNET_ERROR_TYPE_DEBUG,
1104 "New MAC endpoint `%s'\n",
1105 wlan_plugin_address_to_string(NULL, addr, sizeof (struct WlanAddress)));
1111 * Function obtain the network type for a session
1113 * @param cls closure ('struct Plugin*')
1114 * @param session the session
1115 * @return the network type in HBO or GNUNET_SYSERR
1117 static enum GNUNET_ATS_Network_Type
1118 wlan_get_network (void *cls,
1119 struct Session *session)
1121 GNUNET_assert (NULL != session);
1122 return GNUNET_ATS_NET_WLAN;
1127 * Creates a new outbound session the transport service will use to send data to the
1130 * @param cls the plugin
1131 * @param address the address
1132 * @return the session or NULL of max connections exceeded
1134 static struct Session *
1135 wlan_plugin_get_session (void *cls,
1136 const struct GNUNET_HELLO_Address *address)
1138 struct Plugin *plugin = cls;
1139 struct MacEndpoint *endpoint;
1141 if (NULL == address)
1143 if (sizeof (struct WlanAddress) != address->address_length)
1148 LOG (GNUNET_ERROR_TYPE_DEBUG,
1149 "Service asked to create session for peer `%s' with MAC `%s'\n",
1150 GNUNET_i2s (&address->peer),
1151 wlan_plugin_address_to_string(NULL, address->address, address->address_length));
1152 endpoint = create_macendpoint (plugin, address->address);
1153 return get_session (endpoint, &address->peer, GNUNET_NO);
1158 * Function that can be used to force the plugin to disconnect
1159 * from the given peer and cancel all previous transmissions
1160 * (and their continuation).
1162 * @param cls closure
1163 * @param target peer from which to disconnect
1166 wlan_plugin_disconnect_peer (void *cls,
1167 const struct GNUNET_PeerIdentity *target)
1169 struct Plugin *plugin = cls;
1170 struct Session *session;
1171 struct MacEndpoint *endpoint;
1173 for (endpoint = plugin->mac_head; NULL != endpoint; endpoint = endpoint->next)
1174 for (session = endpoint->sessions_head; NULL != session; session = session->next)
1175 if (0 == memcmp (target, &session->target,
1176 sizeof (struct GNUNET_PeerIdentity)))
1178 wlan_plugin_disconnect_session (plugin, session);
1179 break; /* inner-loop only (in case peer has another MAC as well!) */
1185 * Function that can be used by the transport service to transmit
1186 * a message using the plugin. Note that in the case of a
1187 * peer disconnecting, the continuation MUST be called
1188 * prior to the disconnect notification itself. This function
1189 * will be called with this peer's HELLO message to initiate
1190 * a fresh connection to another peer.
1192 * @param cls closure
1193 * @param session which session must be used
1194 * @param msgbuf the message to transmit
1195 * @param msgbuf_size number of bytes in 'msgbuf'
1196 * @param priority how important is the message (most plugins will
1197 * ignore message priority and just FIFO)
1198 * @param to how long to wait at most for the transmission (does not
1199 * require plugins to discard the message after the timeout,
1200 * just advisory for the desired delay; most plugins will ignore
1202 * @param cont continuation to call once the message has
1203 * been transmitted (or if the transport is ready
1204 * for the next transmission call; or if the
1205 * peer disconnected...); can be NULL
1206 * @param cont_cls closure for cont
1207 * @return number of bytes used (on the physical network, with overheads);
1208 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1209 * and does NOT mean that the message was not transmitted (DV)
1212 wlan_plugin_send (void *cls,
1213 struct Session *session,
1214 const char *msgbuf, size_t msgbuf_size,
1215 unsigned int priority,
1216 struct GNUNET_TIME_Relative to,
1217 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1219 struct Plugin *plugin = cls;
1220 struct WlanHeader *wlanheader;
1221 size_t size = msgbuf_size + sizeof (struct WlanHeader);
1222 char buf[size] GNUNET_ALIGN;
1224 LOG (GNUNET_ERROR_TYPE_DEBUG,
1225 "Transmitting %u bytes of payload to peer `%s' (starting with %u byte message of type %u)\n",
1227 GNUNET_i2s (&session->target),
1228 (unsigned int) ntohs (((struct GNUNET_MessageHeader*)msgbuf)->size),
1229 (unsigned int) ntohs (((struct GNUNET_MessageHeader*)msgbuf)->type));
1230 wlanheader = (struct WlanHeader *) buf;
1231 wlanheader->header.size = htons (msgbuf_size + sizeof (struct WlanHeader));
1232 wlanheader->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA);
1233 wlanheader->sender = *plugin->env->my_identity;
1234 wlanheader->target = session->target;
1235 wlanheader->crc = htonl (GNUNET_CRYPTO_crc32_n (msgbuf, msgbuf_size));
1236 memcpy (&wlanheader[1], msgbuf, msgbuf_size);
1238 GNUNET_STATISTICS_update (plugin->env->stats,
1239 "# bytes currently in WLAN buffers",
1240 msgbuf_size, GNUNET_NO);
1242 send_with_fragmentation (session->mac,
1245 &wlanheader->header,
1253 * We have received data from the WLAN via some session. Process depending
1254 * on the message type (HELLO, DATA, FRAGMENTATION or FRAGMENTATION-ACK).
1256 * @param cls pointer to the plugin
1257 * @param client pointer to the session this message belongs to
1258 * @param hdr start of the message
1261 process_data (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
1263 struct Plugin *plugin = cls;
1264 struct MacAndSession *mas = client;
1265 struct MacAndSession xmas;
1266 struct GNUNET_ATS_Information ats;
1267 struct FragmentMessage *fm;
1268 struct GNUNET_PeerIdentity tmpsource;
1269 const struct WlanHeader *wlanheader;
1273 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1274 ats.value = htonl (GNUNET_ATS_NET_WLAN);
1275 msize = ntohs (hdr->size);
1277 GNUNET_STATISTICS_update (plugin->env->stats,
1278 "# bytes received via WLAN",
1281 switch (ntohs (hdr->type))
1283 case GNUNET_MESSAGE_TYPE_HELLO:
1285 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hdr, &tmpsource))
1287 GNUNET_break_op (0);
1290 LOG (GNUNET_ERROR_TYPE_DEBUG,
1291 "Processing %u bytes of HELLO from peer `%s' at MAC %s\n",
1292 (unsigned int) msize,
1293 GNUNET_i2s (&tmpsource),
1294 wlan_plugin_address_to_string (NULL, &mas->endpoint->addr, sizeof (struct WlanAddress)));
1296 GNUNET_STATISTICS_update (plugin->env->stats,
1297 _("# HELLO messages received via WLAN"), 1,
1299 plugin->env->receive (plugin->env->cls,
1303 (mas->endpoint == NULL) ? NULL : (const char *) &mas->endpoint->addr,
1304 (mas->endpoint == NULL) ? 0 : sizeof (struct WlanAddress));
1305 plugin->env->update_address_metrics (plugin->env->cls,
1307 (mas->endpoint == NULL) ? NULL : (const char *) &mas->endpoint->addr,
1308 (mas->endpoint == NULL) ? 0 : sizeof (struct WlanAddress),
1312 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1313 if (NULL == mas->endpoint)
1318 LOG (GNUNET_ERROR_TYPE_DEBUG,
1319 "Processing %u bytes of FRAGMENT from MAC %s\n",
1320 (unsigned int) msize,
1321 wlan_plugin_address_to_string (NULL, &mas->endpoint->addr, sizeof (struct WlanAddress)));
1322 GNUNET_STATISTICS_update (plugin->env->stats,
1323 _("# fragments received via WLAN"), 1, GNUNET_NO);
1324 (void) GNUNET_DEFRAGMENT_process_fragment (mas->endpoint->defrag,
1327 case GNUNET_MESSAGE_TYPE_FRAGMENT_ACK:
1328 if (NULL == mas->endpoint)
1333 GNUNET_STATISTICS_update (plugin->env->stats, _("# ACKs received via WLAN"),
1335 for (fm = mas->endpoint->sending_messages_head; NULL != fm; fm = fm->next)
1337 ret = GNUNET_FRAGMENT_process_ack (fm->fragcontext, hdr);
1338 if (GNUNET_OK == ret)
1340 LOG (GNUNET_ERROR_TYPE_DEBUG,
1341 "Got last ACK, finished message transmission to `%s' (%p)\n",
1342 wlan_plugin_address_to_string (NULL, &mas->endpoint->addr, sizeof (struct WlanAddress)),
1344 mas->endpoint->timeout = GNUNET_TIME_relative_to_absolute (MACENDPOINT_TIMEOUT);
1345 if (NULL != fm->cont)
1347 fm->cont (fm->cont_cls, &fm->target, GNUNET_OK, fm->size_payload, fm->size_on_wire);
1350 free_fragment_message (fm);
1353 if (GNUNET_NO == ret)
1355 LOG (GNUNET_ERROR_TYPE_DEBUG,
1356 "Got an ACK, message transmission to `%s' not yet finished\n",
1357 wlan_plugin_address_to_string (NULL, &mas->endpoint->addr, sizeof (struct WlanAddress)));
1361 LOG (GNUNET_ERROR_TYPE_DEBUG,
1362 "ACK not matched against any active fragmentation with MAC `%s'\n",
1363 wlan_plugin_address_to_string (NULL, &mas->endpoint->addr, sizeof (struct WlanAddress)));
1365 case GNUNET_MESSAGE_TYPE_WLAN_DATA:
1366 if (NULL == mas->endpoint)
1371 if (msize < sizeof (struct WlanHeader))
1376 wlanheader = (const struct WlanHeader *) hdr;
1377 if (0 != memcmp (&wlanheader->target,
1378 plugin->env->my_identity,
1379 sizeof (struct GNUNET_PeerIdentity)))
1381 LOG (GNUNET_ERROR_TYPE_DEBUG,
1382 "WLAN data for `%s', not for me, ignoring\n",
1383 GNUNET_i2s (&wlanheader->target));
1386 if (ntohl (wlanheader->crc) !=
1387 GNUNET_CRYPTO_crc32_n (&wlanheader[1], msize - sizeof (struct WlanHeader)))
1389 GNUNET_STATISTICS_update (plugin->env->stats,
1390 _("# WLAN DATA messages discarded due to CRC32 error"), 1,
1394 xmas.endpoint = mas->endpoint;
1395 if (NULL == (xmas.session = lookup_session (mas->endpoint, &wlanheader->sender, GNUNET_YES)))
1397 xmas.session = create_session (mas->endpoint, &wlanheader->sender, GNUNET_YES);
1398 plugin->env->session_start (NULL, &wlanheader->sender,
1399 PLUGIN_NAME, &mas->endpoint->addr,
1400 sizeof (struct WlanAddress), xmas.session, NULL, 0);
1401 LOG (GNUNET_ERROR_TYPE_DEBUG,
1402 "Notifying transport about peer `%s''s new inbound session %p \n",
1403 GNUNET_i2s (&wlanheader->sender), xmas.session);
1405 LOG (GNUNET_ERROR_TYPE_DEBUG,
1406 "Processing %u bytes of WLAN DATA from peer `%s'\n",
1407 (unsigned int) msize,
1408 GNUNET_i2s (&wlanheader->sender));
1409 (void) GNUNET_SERVER_mst_receive (plugin->wlan_header_payload_tokenizer,
1411 (const char *) &wlanheader[1],
1412 msize - sizeof (struct WlanHeader),
1413 GNUNET_YES, GNUNET_NO);
1416 if (NULL == mas->endpoint)
1421 if (NULL == mas->session)
1426 LOG (GNUNET_ERROR_TYPE_DEBUG,
1427 "Received packet with %u bytes of type %u from peer %s\n",
1428 (unsigned int) msize,
1429 (unsigned int) ntohs (hdr->type),
1430 GNUNET_i2s (&mas->session->target));
1431 plugin->env->receive (plugin->env->cls,
1432 &mas->session->target,
1435 (mas->endpoint == NULL) ? NULL : (const char *) &mas->endpoint->addr,
1436 (mas->endpoint == NULL) ? 0 : sizeof (struct WlanAddress));
1437 plugin->env->update_address_metrics (plugin->env->cls,
1438 &mas->session->target,
1439 (mas->endpoint == NULL) ? NULL : (const char *) &mas->endpoint->addr,
1440 (mas->endpoint == NULL) ? 0 : sizeof (struct WlanAddress),
1450 * Function used for to process the data from the suid process
1452 * @param cls the plugin handle
1453 * @param client client that send the data (not used)
1454 * @param hdr header of the GNUNET_MessageHeader
1457 handle_helper_message (void *cls, void *client,
1458 const struct GNUNET_MessageHeader *hdr)
1460 struct Plugin *plugin = cls;
1461 const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rxinfo;
1462 const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *cm;
1463 struct WlanAddress wa;
1464 struct MacAndSession mas;
1467 msize = ntohs (hdr->size);
1468 switch (ntohs (hdr->type))
1470 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
1471 if (msize != sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage))
1476 cm = (const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *) hdr;
1477 if (GNUNET_YES == plugin->have_mac)
1479 if (0 == memcmp (&plugin->mac_address,
1481 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1482 break; /* no change */
1483 /* remove old address */
1484 memset (&wa, 0, sizeof (struct WlanAddress));
1485 wa.mac = plugin->mac_address;
1486 wa.options = htonl(plugin->options);
1487 plugin->env->notify_address (plugin->env->cls, GNUNET_NO,
1492 plugin->mac_address = cm->mac;
1493 plugin->have_mac = GNUNET_YES;
1494 memset (&wa, 0, sizeof (struct WlanAddress));
1495 wa.mac = plugin->mac_address;
1496 wa.options = htonl(plugin->options);
1497 LOG (GNUNET_ERROR_TYPE_DEBUG,
1498 "Received WLAN_HELPER_CONTROL message with MAC address `%s' for peer `%s'\n",
1499 mac_to_string (&cm->mac),
1500 GNUNET_i2s (plugin->env->my_identity));
1501 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
1503 sizeof (struct WlanAddress),
1506 case GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER:
1507 LOG (GNUNET_ERROR_TYPE_DEBUG,
1508 "Got data message from helper with %u bytes\n",
1510 GNUNET_STATISTICS_update (plugin->env->stats,
1511 _("# DATA messages received via WLAN"), 1,
1513 if (msize < sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage))
1516 LOG (GNUNET_ERROR_TYPE_DEBUG,
1517 "Size of packet is too small (%u bytes)\n",
1521 rxinfo = (const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) hdr;
1523 /* check if message is actually for us */
1524 if (0 != memcmp (&rxinfo->frame.addr3, &mac_bssid_gnunet,
1525 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1527 /* Not the GNUnet BSSID */
1530 if ( (0 != memcmp (&rxinfo->frame.addr1, &bc_all_mac,
1531 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))) &&
1532 (0 != memcmp (&rxinfo->frame.addr1, &plugin->mac_address,
1533 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))) )
1535 /* Neither broadcast nor specifically for us */
1538 if (0 == memcmp (&rxinfo->frame.addr2, &plugin->mac_address,
1539 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1541 /* packet is FROM us, thus not FOR us */
1545 GNUNET_STATISTICS_update (plugin->env->stats,
1546 _("# WLAN DATA messages processed"),
1548 LOG (GNUNET_ERROR_TYPE_DEBUG,
1549 "Receiving %u bytes of data from MAC `%s'\n",
1550 (unsigned int) (msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)),
1551 mac_to_string (&rxinfo->frame.addr2));
1552 wa.mac = rxinfo->frame.addr2;
1553 wa.options = htonl (0);
1554 mas.endpoint = create_macendpoint (plugin, &wa);
1556 (void) GNUNET_SERVER_mst_receive (plugin->helper_payload_tokenizer,
1558 (const char*) &rxinfo[1],
1559 msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage),
1560 GNUNET_YES, GNUNET_NO);
1564 LOG (GNUNET_ERROR_TYPE_DEBUG,
1565 "Unexpected message of type %u (%u bytes)",
1566 ntohs (hdr->type), ntohs (hdr->size));
1575 * Task to (periodically) send a HELLO beacon
1577 * @param cls pointer to the plugin struct
1578 * @param tc scheduler context
1581 send_hello_beacon (void *cls,
1582 const struct GNUNET_SCHEDULER_TaskContext *tc)
1584 struct Plugin *plugin = cls;
1586 uint16_t hello_size;
1587 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
1588 const struct GNUNET_MessageHeader *hello;
1590 hello = plugin->env->get_our_hello ();
1591 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1592 GNUNET_assert (sizeof (struct WlanHeader) + hello_size <= WLAN_MTU);
1593 size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + hello_size;
1595 char buf[size] GNUNET_ALIGN;
1597 LOG (GNUNET_ERROR_TYPE_DEBUG,
1598 "Sending %u byte HELLO beacon\n",
1599 (unsigned int) size);
1600 radioHeader = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage*) buf;
1601 get_radiotap_header (NULL, radioHeader, size);
1602 get_wlan_header (plugin, &radioHeader->frame, &bc_all_mac, size);
1603 memcpy (&radioHeader[1], hello, hello_size);
1605 GNUNET_HELPER_send (plugin->suid_helper,
1606 &radioHeader->header,
1607 GNUNET_YES /* can drop */,
1609 GNUNET_STATISTICS_update (plugin->env->stats, _("# HELLO beacons sent via WLAN"),
1612 plugin->beacon_task =
1613 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1614 (HELLO_BEACON_SCALING_FACTOR,
1615 plugin->mac_count + 1),
1623 * Another peer has suggested an address for this
1624 * peer and transport plugin. Check that this could be a valid
1625 * address. If so, consider adding it to the list
1628 * @param cls closure
1629 * @param addr pointer to the address
1630 * @param addrlen length of addr
1631 * @return GNUNET_OK if this is a plausible address for this peer
1635 wlan_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1637 struct Plugin *plugin = cls;
1638 struct WlanAddress *wa = (struct WlanAddress *) addr;
1640 if (addrlen != sizeof (struct WlanAddress))
1642 GNUNET_break_op (0);
1643 return GNUNET_SYSERR;
1645 if (GNUNET_YES != plugin->have_mac)
1647 LOG (GNUNET_ERROR_TYPE_DEBUG,
1648 "Rejecting MAC `%s': I don't know my MAC!\n",
1649 mac_to_string (addr));
1650 return GNUNET_NO; /* don't know my MAC */
1652 if (0 != memcmp (&wa->mac,
1653 &plugin->mac_address,
1656 LOG (GNUNET_ERROR_TYPE_DEBUG,
1657 "Rejecting MAC `%s': not my MAC!\n",
1658 mac_to_string (addr));
1659 return GNUNET_NO; /* not my MAC */
1666 * Function called for a quick conversion of the binary address to
1667 * a numeric address. Note that the caller must not free the
1668 * address and that the next call to this function is allowed
1669 * to override the address again.
1671 * @param cls closure
1672 * @param addr binary address
1673 * @param addrlen length of the address
1674 * @return string representing the same address
1677 wlan_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
1679 const struct GNUNET_TRANSPORT_WLAN_MacAddress *mac;
1680 static char macstr[36];
1682 if (sizeof (struct WlanAddress) != addrlen)
1687 mac = &((struct WlanAddress *) addr)->mac;
1688 GNUNET_snprintf (macstr, sizeof (macstr), "%s.%u.%s",
1689 PLUGIN_NAME, ntohl (((struct WlanAddress *) addr)->options),
1690 mac_to_string (mac));
1696 * Convert the transports address to a nice, human-readable format.
1698 * @param cls closure
1699 * @param type name of the transport that generated the address
1700 * @param addr one of the addresses of the host, NULL for the last address
1701 * the specific address format depends on the transport
1702 * @param addrlen length of the address
1703 * @param numeric should (IP) addresses be displayed in numeric form?
1704 * @param timeout after how long should we give up?
1705 * @param asc function to call on each string
1706 * @param asc_cls closure for asc
1709 wlan_plugin_address_pretty_printer (void *cls, const char *type,
1710 const void *addr, size_t addrlen,
1712 struct GNUNET_TIME_Relative timeout,
1713 GNUNET_TRANSPORT_AddressStringCallback asc,
1718 if (sizeof (struct WlanAddress) != addrlen)
1720 /* invalid address */
1721 LOG (GNUNET_ERROR_TYPE_WARNING,
1722 _("WLAN address with invalid size encountered\n"));
1723 asc (asc_cls, NULL);
1726 ret = GNUNET_strdup (wlan_plugin_address_to_string(NULL, addr, addrlen));
1729 asc (asc_cls, NULL);
1734 * Exit point from the plugin.
1736 * @param cls pointer to the api struct
1739 libgnunet_plugin_transport_wlan_done (void *cls)
1741 struct WlanAddress wa;
1742 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1743 struct Plugin *plugin = api->cls;
1744 struct MacEndpoint *endpoint;
1745 struct MacEndpoint *endpoint_next;
1753 if (GNUNET_YES == plugin->have_mac)
1755 memset (&wa, 0, sizeof (wa));
1756 wa.options = htonl (plugin->options);
1757 wa.mac = plugin->mac_address;
1758 plugin->env->notify_address (plugin->env->cls, GNUNET_NO,
1760 sizeof (struct WlanAddress),
1762 plugin->have_mac = GNUNET_NO;
1765 if (GNUNET_SCHEDULER_NO_TASK != plugin->beacon_task)
1767 GNUNET_SCHEDULER_cancel (plugin->beacon_task);
1768 plugin->beacon_task = GNUNET_SCHEDULER_NO_TASK;
1770 if (NULL != plugin->suid_helper)
1772 GNUNET_HELPER_stop (plugin->suid_helper, GNUNET_NO);
1773 plugin->suid_helper = NULL;
1775 endpoint_next = plugin->mac_head;
1776 while (NULL != (endpoint = endpoint_next))
1778 endpoint_next = endpoint->next;
1779 free_macendpoint (endpoint);
1781 if (NULL != plugin->fragment_data_tokenizer)
1783 GNUNET_SERVER_mst_destroy (plugin->fragment_data_tokenizer);
1784 plugin->fragment_data_tokenizer = NULL;
1786 if (NULL != plugin->wlan_header_payload_tokenizer)
1788 GNUNET_SERVER_mst_destroy (plugin->wlan_header_payload_tokenizer);
1789 plugin->wlan_header_payload_tokenizer = NULL;
1791 if (NULL != plugin->helper_payload_tokenizer)
1793 GNUNET_SERVER_mst_destroy (plugin->helper_payload_tokenizer);
1794 plugin->helper_payload_tokenizer = NULL;
1796 GNUNET_free_non_null (plugin->interface);
1797 GNUNET_free (plugin);
1804 * Function called to convert a string address to
1807 * @param cls closure ('struct Plugin*')
1808 * @param addr string address
1809 * @param addrlen length of the address
1810 * @param buf location to store the buffer
1811 * @param added location to store the number of bytes in the buffer.
1812 * If the function returns GNUNET_SYSERR, its contents are undefined.
1813 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1816 wlan_string_to_address (void *cls, const char *addr, uint16_t addrlen,
1817 void **buf, size_t *added)
1819 struct WlanAddress *wa;
1825 if ((NULL == addr) || (addrlen == 0))
1828 return GNUNET_SYSERR;
1830 if ('\0' != addr[addrlen - 1])
1833 return GNUNET_SYSERR;
1835 if (strlen (addr) != addrlen - 1)
1838 return GNUNET_SYSERR;
1841 if (8 != SSCANF (addr,
1842 "%4s.%u.%X:%X:%X:%X:%X:%X",
1844 &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]))
1847 return GNUNET_SYSERR;
1849 wa = GNUNET_malloc (sizeof (struct WlanAddress));
1851 wa->mac.mac[i] = a[i];
1852 wa->options = htonl (0);
1854 *added = sizeof (struct WlanAddress);
1860 wlan_plugin_update_session_timeout (void *cls,
1861 const struct GNUNET_PeerIdentity *peer,
1862 struct Session *session)
1864 if (session->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1865 GNUNET_SCHEDULER_cancel (session->timeout_task);
1867 session->timeout_task = GNUNET_SCHEDULER_add_delayed (
1868 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &session_timeout, session);
1872 * Entry point for the plugin.
1874 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
1875 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
1878 libgnunet_plugin_transport_wlan_init (void *cls)
1880 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1881 struct GNUNET_TRANSPORT_PluginFunctions *api;
1882 struct Plugin *plugin;
1884 unsigned long long testmode;
1887 /* check for 'special' mode */
1888 if (NULL == env->receive)
1890 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1891 initialze the plugin or the API */
1892 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1894 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
1895 api->address_to_string = &wlan_plugin_address_to_string;
1896 api->string_to_address = &wlan_string_to_address;
1901 /* check configuration */
1903 GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "TESTMODE")) &&
1905 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-wlan",
1906 "TESTMODE", &testmode)) ||
1909 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1910 "transport-wlan", "TESTMODE");
1913 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-transport-wlan");
1914 if ( (0 == testmode) &&
1915 (GNUNET_YES != GNUNET_OS_check_helper_binary (binary, GNUNET_YES, NULL)) )
1917 LOG (GNUNET_ERROR_TYPE_ERROR,
1918 _("Helper binary `%s' not SUID, cannot run WLAN transport\n"),
1919 "gnunet-helper-transport-wlan");
1920 GNUNET_free (binary);
1923 GNUNET_free (binary);
1925 GNUNET_CONFIGURATION_get_value_string
1926 (env->cfg, "transport-wlan", "INTERFACE",
1929 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1930 "transport-wlan", "INTERFACE");
1934 plugin = GNUNET_malloc (sizeof (struct Plugin));
1935 plugin->interface = interface;
1937 GNUNET_STATISTICS_set (plugin->env->stats, _("# WLAN sessions allocated"),
1939 GNUNET_STATISTICS_set (plugin->env->stats, _("# WLAN MAC endpoints allocated"),
1941 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
1942 GNUNET_BANDWIDTH_value_init (100 * 1024 *
1944 plugin->fragment_data_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
1945 plugin->wlan_header_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
1946 plugin->helper_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
1947 plugin->beacon_task = GNUNET_SCHEDULER_add_now (&send_hello_beacon,
1950 plugin->options = 0;
1952 /* some compilers do not like switch on 'long long'... */
1953 switch ((unsigned int) testmode)
1955 case 0: /* normal */
1956 plugin->helper_argv[0] = (char *) "gnunet-helper-transport-wlan";
1957 plugin->helper_argv[1] = interface;
1958 plugin->helper_argv[2] = NULL;
1959 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
1960 "gnunet-helper-transport-wlan",
1961 plugin->helper_argv,
1962 &handle_helper_message,
1966 case 1: /* testmode, peer 1 */
1967 plugin->helper_argv[0] = (char *) "gnunet-helper-transport-wlan-dummy";
1968 plugin->helper_argv[1] = (char *) "1";
1969 plugin->helper_argv[2] = NULL;
1970 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
1971 "gnunet-helper-transport-wlan-dummy",
1972 plugin->helper_argv,
1973 &handle_helper_message,
1977 case 2: /* testmode, peer 2 */
1978 plugin->helper_argv[0] = (char *) "gnunet-helper-transport-wlan-dummy";
1979 plugin->helper_argv[1] = (char *) "2";
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,
1992 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1994 api->send = &wlan_plugin_send;
1995 api->get_session = &wlan_plugin_get_session;
1996 api->disconnect_peer = &wlan_plugin_disconnect_peer;
1997 api->disconnect_session = &wlan_plugin_disconnect_session;
1998 api->query_keepalive_factor = &wlan_plugin_query_keepalive_factor;
1999 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2000 api->check_address = &wlan_plugin_address_suggested;
2001 api->address_to_string = &wlan_plugin_address_to_string;
2002 api->string_to_address = &wlan_string_to_address;
2003 api->get_network = &wlan_get_network;
2004 api->update_session_timeout = &wlan_plugin_update_session_timeout;
2009 /* end of plugin_transport_wlan.c */