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;
219 * Struct for messages that are being fragmented in a MAC's transmission queue.
221 struct FragmentMessage
225 * This is a doubly-linked list.
227 struct FragmentMessage *next;
230 * This is a doubly-linked list.
232 struct FragmentMessage *prev;
235 * MAC endpoint this message belongs to
237 struct MacEndpoint *macendpoint;
240 * Fragmentation context
242 struct GNUNET_FRAGMENT_Context *fragcontext;
245 * Transmission handle to helper (to cancel if the frag context
246 * is destroyed early for some reason).
248 struct GNUNET_HELPER_SendHandle *sh;
251 * Intended recipient.
253 struct GNUNET_PeerIdentity target;
256 * Timeout value for the message.
258 struct GNUNET_TIME_Absolute timeout;
263 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
266 * Continuation to call when we're done with this message.
268 GNUNET_TRANSPORT_TransmitContinuation cont;
276 * Size of original message
281 * Number of bytes used to transmit message
289 * Struct to represent one network card connection
295 * We keep all MACs in a DLL in the plugin.
297 struct MacEndpoint *next;
300 * We keep all MACs in a DLL in the plugin.
302 struct MacEndpoint *prev;
305 * Pointer to the global plugin struct.
307 struct Plugin *plugin;
310 * Head of sessions that use this MAC.
312 struct Session *sessions_head;
315 * Tail of sessions that use this MAC.
317 struct Session *sessions_tail;
320 * Head of messages we are currently sending to this MAC.
322 struct FragmentMessage *sending_messages_head;
325 * Tail of messages we are currently sending to this MAC.
327 struct FragmentMessage *sending_messages_tail;
330 * Defrag context for this MAC
332 struct GNUNET_DEFRAGMENT_Context *defrag;
335 * When should this endpoint time out?
337 struct GNUNET_TIME_Absolute timeout;
342 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
345 * count of messages in the fragment out queue for this mac endpoint
347 unsigned int fragment_messages_out_count;
352 struct WlanAddress addr;
355 * Message delay for fragmentation context
357 struct GNUNET_TIME_Relative msg_delay;
360 * ACK delay for fragmentation context
362 struct GNUNET_TIME_Relative ack_delay;
365 * Desired transmission power for this MAC
370 * Desired transmission rate for this MAC
375 * Antenna we should use for this MAC
383 * Encapsulation of all of the state of the plugin.
390 struct GNUNET_TRANSPORT_PluginEnvironment *env;
393 * Handle to helper process for priviledged operations.
395 struct GNUNET_HELPER_Handle *suid_helper;
398 * ARGV-vector for the helper (all helpers take only the binary
399 * name, one actual argument, plus the NULL terminator for 'argv').
401 char * helper_argv[3];
404 * The interface of the wlan card given to us by the user.
409 * Tokenizer for demultiplexing of data packets resulting from defragmentation.
411 struct GNUNET_SERVER_MessageStreamTokenizer *fragment_data_tokenizer;
414 * Tokenizer for demultiplexing of data packets received from the suid helper
416 struct GNUNET_SERVER_MessageStreamTokenizer *helper_payload_tokenizer;
419 * Tokenizer for demultiplexing of data packets that follow the WLAN Header
421 struct GNUNET_SERVER_MessageStreamTokenizer *wlan_header_payload_tokenizer;
424 * Head of list of open connections.
426 struct MacEndpoint *mac_head;
429 * Tail of list of open connections.
431 struct MacEndpoint *mac_tail;
434 * Number of connections
436 unsigned int mac_count;
439 * Task that periodically sends a HELLO beacon via the helper.
441 GNUNET_SCHEDULER_TaskIdentifier beacon_task;
444 * Tracker for bandwidth limit
446 struct GNUNET_BANDWIDTH_Tracker tracker;
449 * The mac_address of the wlan card given to us by the helper.
451 struct GNUNET_TRANSPORT_WLAN_MacAddress mac_address;
454 * Have we received a control message with our MAC address yet?
459 * Options for addresses
467 * Information associated with a message. Can contain
468 * the session or the MAC endpoint associated with the
474 * NULL if the identity of the other peer is not known.
476 struct Session *session;
479 * MAC address of the other peer, NULL if not known.
481 struct MacEndpoint *endpoint;
485 * Function called for a quick conversion of the binary address to
486 * a numeric address. Note that the caller must not free the
487 * address and that the next call to this function is allowed
488 * to override the address again.
491 * @param addr binary address
492 * @param addrlen length of the address
493 * @return string representing the same address
496 wlan_plugin_address_to_string (void *cls, const void *addr, size_t addrlen);
499 * Print MAC addresses nicely.
501 * @param mac the mac address
502 * @return string to a static buffer with the human-readable mac, will be overwritten during the next call to this function
505 mac_to_string (const struct GNUNET_TRANSPORT_WLAN_MacAddress * mac)
507 static char macstr[20];
509 GNUNET_snprintf (macstr, sizeof (macstr), "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
510 mac->mac[0], mac->mac[1],
511 mac->mac[2], mac->mac[3], mac->mac[4], mac->mac[5]);
517 * Fill the radiotap header
519 * @param endpoint pointer to the endpoint, can be NULL
520 * @param header pointer to the radiotap header
521 * @param size total message size
524 get_radiotap_header (struct MacEndpoint *endpoint,
525 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *header,
528 header->header.type = ntohs (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER);
529 header->header.size = ntohs (size);
530 if (NULL != endpoint)
532 header->rate = endpoint->rate;
533 header->tx_power = endpoint->tx_power;
534 header->antenna = endpoint->antenna;
539 header->tx_power = 0;
546 * Generate the WLAN hardware header for one packet
548 * @param plugin the plugin handle
549 * @param header address to write the header to
550 * @param to_mac_addr address of the recipient
551 * @param size size of the whole packet, needed to calculate the time to send the packet
554 get_wlan_header (struct Plugin *plugin,
555 struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *header,
556 const struct GNUNET_TRANSPORT_WLAN_MacAddress *to_mac_addr,
559 const int rate = 11000000;
561 header->frame_control = htons (IEEE80211_FC0_TYPE_DATA);
562 header->addr1 = *to_mac_addr;
563 header->addr2 = plugin->mac_address;
564 header->addr3 = mac_bssid_gnunet;
565 header->duration = GNUNET_htole16 ((size * 1000000) / rate + 290);
566 header->sequence_control = 0; // FIXME?
567 header->llc[0] = WLAN_LLC_DSAP_FIELD;
568 header->llc[1] = WLAN_LLC_SSAP_FIELD;
569 header->llc[2] = 0; // FIXME?
570 header->llc[3] = 0; // FIXME?
575 * Send an ACK for a fragment we received.
577 * @param cls the 'struct MacEndpoint' the ACK must be sent to
578 * @param msg_id id of the message
579 * @param hdr pointer to the hdr where the ack is stored
582 send_ack (void *cls, uint32_t msg_id,
583 const struct GNUNET_MessageHeader *hdr)
585 struct MacEndpoint *endpoint = cls;
586 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage* radio_header;
587 uint16_t msize = ntohs (hdr->size);
588 size_t size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + msize;
591 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
596 LOG (GNUNET_ERROR_TYPE_DEBUG,
597 "Sending ACK to helper\n");
598 radio_header = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) buf;
599 get_radiotap_header (endpoint, radio_header, size);
600 get_wlan_header (endpoint->plugin,
601 &radio_header->frame,
604 memcpy (&radio_header[1], hdr, msize);
606 GNUNET_HELPER_send (endpoint->plugin->suid_helper,
607 &radio_header->header,
608 GNUNET_NO /* dropping ACKs is bad */,
610 GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# WLAN ACKs sent"),
616 * Handles the data after all fragments are put together
618 * @param cls macendpoint this messages belongs to
619 * @param hdr pointer to the data
622 wlan_data_message_handler (void *cls, const struct GNUNET_MessageHeader *hdr)
624 struct MacEndpoint *endpoint = cls;
625 struct Plugin *plugin = endpoint->plugin;
626 struct MacAndSession mas;
628 GNUNET_STATISTICS_update (plugin->env->stats,
629 _("# WLAN messages defragmented"), 1,
632 mas.endpoint = endpoint;
633 (void) GNUNET_SERVER_mst_receive (plugin->fragment_data_tokenizer,
637 GNUNET_YES, GNUNET_NO);
644 * @param session the session free
647 free_session (struct Session *session)
649 struct MacEndpoint *endpoint = session->mac;
650 struct PendingMessage *pm;
652 endpoint->plugin->env->session_end (endpoint->plugin->env->cls,
655 while (NULL != (pm = session->pending_message_head))
657 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
658 session->pending_message_tail, pm);
659 if (GNUNET_SCHEDULER_NO_TASK != pm->timeout_task)
661 GNUNET_SCHEDULER_cancel (pm->timeout_task);
662 pm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
664 GNUNET_free (pm->msg);
667 GNUNET_CONTAINER_DLL_remove (endpoint->sessions_head,
668 endpoint->sessions_tail,
670 if (session->timeout_task != GNUNET_SCHEDULER_NO_TASK)
672 GNUNET_SCHEDULER_cancel (session->timeout_task);
673 session->timeout_task = GNUNET_SCHEDULER_NO_TASK;
675 GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# WLAN sessions allocated"), -1,
677 GNUNET_free (session);
682 * A session is timing out. Clean up.
684 * @param cls pointer to the Session
685 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
688 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
690 struct Session * session = cls;
691 struct GNUNET_TIME_Relative timeout;
693 session->timeout_task = GNUNET_SCHEDULER_NO_TASK;
694 timeout = GNUNET_TIME_absolute_get_remaining (session->timeout);
695 if (0 == timeout.rel_value)
697 free_session (session);
700 session->timeout_task =
701 GNUNET_SCHEDULER_add_delayed (timeout, &session_timeout, session);
707 * Lookup a new session
709 * @param endpoint pointer to the mac endpoint of the peer
710 * @param peer peer identity to use for this session
711 * @return returns the session or NULL
713 static struct Session *
714 lookup_session (struct MacEndpoint *endpoint,
715 const struct GNUNET_PeerIdentity *peer)
717 struct Session *session;
719 for (session = endpoint->sessions_head; NULL != session; session = session->next)
720 if (0 == memcmp (peer, &session->target,
721 sizeof (struct GNUNET_PeerIdentity)))
723 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
730 * Create a new session
732 * @param endpoint pointer to the mac endpoint of the peer
733 * @param peer peer identity to use for this session
734 * @return returns the session or NULL
736 static struct Session *
737 create_session (struct MacEndpoint *endpoint,
738 const struct GNUNET_PeerIdentity *peer)
740 struct Session *session;
742 GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# WLAN sessions allocated"), 1,
744 session = GNUNET_malloc (sizeof (struct Session));
745 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
746 endpoint->sessions_tail,
748 session->mac = endpoint;
749 session->target = *peer;
750 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
751 session->timeout_task =
752 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &session_timeout, session);
753 LOG (GNUNET_ERROR_TYPE_DEBUG,
754 "Created new session for peer `%s' with endpoint %s\n",
756 mac_to_string (&endpoint->addr.mac));
762 * Look up a session for a peer and create a new session if none is found
764 * @param endpoint pointer to the mac endpoint of the peer
765 * @param peer peer identity to use for this session
766 * @return returns the session
768 static struct Session *
769 get_session (struct MacEndpoint *endpoint,
770 const struct GNUNET_PeerIdentity *peer)
772 struct Session *session;
773 if (NULL != (session = lookup_session (endpoint, peer)))
775 return create_session (endpoint, peer);
780 * Function called once we have successfully given the fragment
781 * message to the SUID helper process and we are thus ready for
784 * @param cls the 'struct FragmentMessage'
785 * @param result result of the operation (GNUNET_OK on success, GNUNET_NO if the helper died, GNUNET_SYSERR
786 * if the helper was stopped)
789 fragment_transmission_done (void *cls,
792 struct FragmentMessage *fm = cls;
796 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
801 * Transmit a fragment of a message.
803 * @param cls 'struct FragmentMessage' this fragment message belongs to
804 * @param hdr pointer to the start of the fragment message
807 transmit_fragment (void *cls,
808 const struct GNUNET_MessageHeader *hdr)
810 struct FragmentMessage *fm = cls;
811 struct MacEndpoint *endpoint = fm->macendpoint;
815 msize = ntohs (hdr->size);
816 size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + msize;
819 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radio_header;
821 radio_header = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) buf;
822 get_radiotap_header (endpoint, radio_header, size);
823 get_wlan_header (endpoint->plugin,
824 &radio_header->frame,
827 memcpy (&radio_header[1], hdr, msize);
828 GNUNET_assert (NULL == fm->sh);
829 fm->sh = GNUNET_HELPER_send (endpoint->plugin->suid_helper,
830 &radio_header->header,
832 &fragment_transmission_done, fm);
833 fm->size_on_wire += size;
835 GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# WLAN message fragments sent"),
838 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
839 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
840 "# bytes currently in WLAN buffers",
842 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
843 "# bytes transmitted via WLAN",
850 * Frees the space of a message in the fragment queue (send queue)
852 * @param fm message to free
855 free_fragment_message (struct FragmentMessage *fm)
857 struct MacEndpoint *endpoint = fm->macendpoint;
859 GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# WLAN messages pending (with fragmentation)"),
861 GNUNET_CONTAINER_DLL_remove (endpoint->sending_messages_head,
862 endpoint->sending_messages_tail, fm);
865 GNUNET_HELPER_send_cancel (fm->sh);
868 GNUNET_FRAGMENT_context_destroy (fm->fragcontext,
869 &endpoint->msg_delay,
870 &endpoint->ack_delay);
871 if (fm->timeout_task != GNUNET_SCHEDULER_NO_TASK)
873 GNUNET_SCHEDULER_cancel (fm->timeout_task);
874 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
881 * A FragmentMessage has timed out. Remove it.
883 * @param cls pointer to the 'struct FragmentMessage'
884 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
887 fragmentmessage_timeout (void *cls,
888 const struct GNUNET_SCHEDULER_TaskContext *tc)
890 struct FragmentMessage *fm = cls;
892 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
893 if (NULL != fm->cont)
895 fm->cont (fm->cont_cls, &fm->target, GNUNET_SYSERR, fm->size_payload, fm->size_on_wire);
898 free_fragment_message (fm);
903 * Transmit a message to the given destination with fragmentation.
905 * @param endpoint desired destination
906 * @param timeout how long can the message wait?
907 * @param target peer that should receive the message
908 * @param msg message to transmit
909 * @param payload_size bytes of payload
910 * @param cont continuation to call once the message has
911 * been transmitted (or if the transport is ready
912 * for the next transmission call; or if the
913 * peer disconnected...); can be NULL
914 * @param cont_cls closure for cont
917 send_with_fragmentation (struct MacEndpoint *endpoint,
918 struct GNUNET_TIME_Relative timeout,
919 const struct GNUNET_PeerIdentity *target,
920 const struct GNUNET_MessageHeader *msg,
922 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
925 struct FragmentMessage *fm;
926 struct Plugin *plugin;
928 plugin = endpoint->plugin;
929 fm = GNUNET_malloc (sizeof (struct FragmentMessage));
930 fm->macendpoint = endpoint;
931 fm->target = *target;
932 fm->size_payload = payload_size;
933 fm->size_on_wire = 0;
934 fm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
936 fm->cont_cls = cont_cls;
937 /* 1 MBit/s typical data rate, 1430 byte fragments => ~100 ms per message */
939 GNUNET_FRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
944 &transmit_fragment, fm);
946 GNUNET_SCHEDULER_add_delayed (timeout,
947 &fragmentmessage_timeout, fm);
948 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sending_messages_head,
949 endpoint->sending_messages_tail,
955 * Free a MAC endpoint.
957 * @param endpoint pointer to the MacEndpoint to free
960 free_macendpoint (struct MacEndpoint *endpoint)
962 struct Plugin *plugin = endpoint->plugin;
963 struct FragmentMessage *fm;
964 struct Session *session;
966 GNUNET_STATISTICS_update (plugin->env->stats,
967 _("# WLAN MAC endpoints allocated"), -1, GNUNET_NO);
968 while (NULL != (session = endpoint->sessions_head))
969 free_session (session);
970 while (NULL != (fm = endpoint->sending_messages_head))
971 free_fragment_message (fm);
972 GNUNET_CONTAINER_DLL_remove (plugin->mac_head,
976 if (NULL != endpoint->defrag)
978 GNUNET_DEFRAGMENT_context_destroy(endpoint->defrag);
979 endpoint->defrag = NULL;
983 if (GNUNET_SCHEDULER_NO_TASK != endpoint->timeout_task)
985 GNUNET_SCHEDULER_cancel (endpoint->timeout_task);
986 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
988 GNUNET_free (endpoint);
993 * A MAC endpoint is timing out. Clean up.
995 * @param cls pointer to the MacEndpoint
996 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
999 macendpoint_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1001 struct MacEndpoint *endpoint = cls;
1002 struct GNUNET_TIME_Relative timeout;
1004 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1005 timeout = GNUNET_TIME_absolute_get_remaining (endpoint->timeout);
1006 if (0 == timeout.rel_value)
1008 free_macendpoint (endpoint);
1011 endpoint->timeout_task =
1012 GNUNET_SCHEDULER_add_delayed (timeout, &macendpoint_timeout,
1018 * Find (or create) a MacEndpoint with a specific MAC address
1020 * @param plugin pointer to the plugin struct
1021 * @param addr the MAC address of the endpoint
1022 * @return handle to our data structure for this MAC
1024 static struct MacEndpoint *
1025 create_macendpoint (struct Plugin *plugin,
1026 const struct WlanAddress *addr)
1028 struct MacEndpoint *pos;
1030 for (pos = plugin->mac_head; NULL != pos; pos = pos->next)
1031 if (0 == memcmp (addr, &pos->addr, sizeof (struct WlanAddress)))
1033 pos = GNUNET_malloc (sizeof (struct MacEndpoint));
1035 pos->plugin = plugin;
1037 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
1038 MESSAGES_IN_DEFRAG_QUEUE_PER_MAC,
1040 &wlan_data_message_handler,
1043 pos->msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1044 pos->ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1046 pos->timeout = GNUNET_TIME_relative_to_absolute (MACENDPOINT_TIMEOUT);
1048 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
1050 GNUNET_CONTAINER_DLL_insert (plugin->mac_head, plugin->mac_tail, pos);
1051 plugin->mac_count++;
1052 GNUNET_STATISTICS_update (plugin->env->stats, _("# WLAN MAC endpoints allocated"),
1054 LOG (GNUNET_ERROR_TYPE_DEBUG,
1055 "New MAC endpoint `%s'\n",
1056 wlan_plugin_address_to_string(NULL, addr, sizeof (struct WlanAddress)));
1062 * Function obtain the network type for a session
1064 * @param cls closure ('struct Plugin*')
1065 * @param session the session
1066 * @return the network type in HBO or GNUNET_SYSERR
1068 static enum GNUNET_ATS_Network_Type
1069 wlan_get_network (void *cls,
1070 struct Session *session)
1072 GNUNET_assert (NULL != session);
1073 return GNUNET_ATS_NET_WLAN;
1078 * Creates a new outbound session the transport service will use to send data to the
1081 * @param cls the plugin
1082 * @param address the address
1083 * @return the session or NULL of max connections exceeded
1085 static struct Session *
1086 wlan_plugin_get_session (void *cls,
1087 const struct GNUNET_HELLO_Address *address)
1089 struct Plugin *plugin = cls;
1090 struct MacEndpoint *endpoint;
1092 if (NULL == address)
1094 if (sizeof (struct WlanAddress) != address->address_length)
1099 LOG (GNUNET_ERROR_TYPE_DEBUG,
1100 "Service asked to create session for peer `%s' with MAC `%s'\n",
1101 GNUNET_i2s (&address->peer),
1102 wlan_plugin_address_to_string(NULL, address->address, address->address_length));
1103 endpoint = create_macendpoint (plugin, address->address);
1104 return get_session (endpoint, &address->peer);
1109 * Function that can be used to force the plugin to disconnect
1110 * from the given peer and cancel all previous transmissions
1111 * (and their continuation).
1113 * @param cls closure
1114 * @param target peer from which to disconnect
1117 wlan_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
1119 struct Plugin *plugin = cls;
1120 struct Session *session;
1121 struct MacEndpoint *endpoint;
1123 for (endpoint = plugin->mac_head; NULL != endpoint; endpoint = endpoint->next)
1124 for (session = endpoint->sessions_head; NULL != session; session = session->next)
1125 if (0 == memcmp (target, &session->target,
1126 sizeof (struct GNUNET_PeerIdentity)))
1128 free_session (session);
1129 break; /* inner-loop only (in case peer has another MAC as well!) */
1135 * Function that can be used by the transport service to transmit
1136 * a message using the plugin. Note that in the case of a
1137 * peer disconnecting, the continuation MUST be called
1138 * prior to the disconnect notification itself. This function
1139 * will be called with this peer's HELLO message to initiate
1140 * a fresh connection to another peer.
1142 * @param cls closure
1143 * @param session which session must be used
1144 * @param msgbuf the message to transmit
1145 * @param msgbuf_size number of bytes in 'msgbuf'
1146 * @param priority how important is the message (most plugins will
1147 * ignore message priority and just FIFO)
1148 * @param to how long to wait at most for the transmission (does not
1149 * require plugins to discard the message after the timeout,
1150 * just advisory for the desired delay; most plugins will ignore
1152 * @param cont continuation to call once the message has
1153 * been transmitted (or if the transport is ready
1154 * for the next transmission call; or if the
1155 * peer disconnected...); can be NULL
1156 * @param cont_cls closure for cont
1157 * @return number of bytes used (on the physical network, with overheads);
1158 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1159 * and does NOT mean that the message was not transmitted (DV)
1162 wlan_plugin_send (void *cls,
1163 struct Session *session,
1164 const char *msgbuf, size_t msgbuf_size,
1165 unsigned int priority,
1166 struct GNUNET_TIME_Relative to,
1167 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1169 struct Plugin *plugin = cls;
1170 struct WlanHeader *wlanheader;
1171 size_t size = msgbuf_size + sizeof (struct WlanHeader);
1172 char buf[size] GNUNET_ALIGN;
1174 LOG (GNUNET_ERROR_TYPE_DEBUG,
1175 "Transmitting %u bytes of payload to peer `%s' (starting with %u byte message of type %u)\n",
1177 GNUNET_i2s (&session->target),
1178 (unsigned int) ntohs (((struct GNUNET_MessageHeader*)msgbuf)->size),
1179 (unsigned int) ntohs (((struct GNUNET_MessageHeader*)msgbuf)->type));
1180 wlanheader = (struct WlanHeader *) buf;
1181 wlanheader->header.size = htons (msgbuf_size + sizeof (struct WlanHeader));
1182 wlanheader->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA);
1183 wlanheader->sender = *plugin->env->my_identity;
1184 wlanheader->target = session->target;
1185 wlanheader->crc = htonl (GNUNET_CRYPTO_crc32_n (msgbuf, msgbuf_size));
1186 memcpy (&wlanheader[1], msgbuf, msgbuf_size);
1188 GNUNET_STATISTICS_update (plugin->env->stats,
1189 "# bytes currently in WLAN buffers",
1190 msgbuf_size, GNUNET_NO);
1192 send_with_fragmentation (session->mac,
1195 &wlanheader->header,
1203 * We have received data from the WLAN via some session. Process depending
1204 * on the message type (HELLO, DATA, FRAGMENTATION or FRAGMENTATION-ACK).
1206 * @param cls pointer to the plugin
1207 * @param client pointer to the session this message belongs to
1208 * @param hdr start of the message
1211 process_data (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
1213 struct Plugin *plugin = cls;
1214 struct MacAndSession *mas = client;
1215 struct MacAndSession xmas;
1216 struct GNUNET_ATS_Information ats;
1217 struct FragmentMessage *fm;
1218 struct GNUNET_PeerIdentity tmpsource;
1219 const struct WlanHeader *wlanheader;
1223 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1224 ats.value = htonl (GNUNET_ATS_NET_WLAN);
1225 msize = ntohs (hdr->size);
1227 GNUNET_STATISTICS_update (plugin->env->stats,
1228 "# bytes received via WLAN",
1231 switch (ntohs (hdr->type))
1233 case GNUNET_MESSAGE_TYPE_HELLO:
1235 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hdr, &tmpsource))
1237 GNUNET_break_op (0);
1240 LOG (GNUNET_ERROR_TYPE_DEBUG,
1241 "Processing %u bytes of HELLO from peer `%s' at MAC %s\n",
1242 (unsigned int) msize,
1243 GNUNET_i2s (&tmpsource),
1244 wlan_plugin_address_to_string (NULL, &mas->endpoint->addr, sizeof (struct WlanAddress)));
1246 GNUNET_STATISTICS_update (plugin->env->stats,
1247 _("# HELLO messages received via WLAN"), 1,
1249 plugin->env->receive (plugin->env->cls,
1253 (mas->endpoint == NULL) ? NULL : (const char *) &mas->endpoint->addr,
1254 (mas->endpoint == NULL) ? 0 : sizeof (struct WlanAddress));
1255 plugin->env->update_address_metrics (plugin->env->cls,
1257 (mas->endpoint == NULL) ? NULL : (const char *) &mas->endpoint->addr,
1258 (mas->endpoint == NULL) ? 0 : sizeof (struct WlanAddress),
1262 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1263 if (NULL == mas->endpoint)
1268 LOG (GNUNET_ERROR_TYPE_DEBUG,
1269 "Processing %u bytes of FRAGMENT from MAC %s\n",
1270 (unsigned int) msize,
1271 wlan_plugin_address_to_string (NULL, &mas->endpoint->addr, sizeof (struct WlanAddress)));
1272 GNUNET_STATISTICS_update (plugin->env->stats,
1273 _("# fragments received via WLAN"), 1, GNUNET_NO);
1274 (void) GNUNET_DEFRAGMENT_process_fragment (mas->endpoint->defrag,
1277 case GNUNET_MESSAGE_TYPE_FRAGMENT_ACK:
1278 if (NULL == mas->endpoint)
1283 GNUNET_STATISTICS_update (plugin->env->stats, _("# ACKs received via WLAN"),
1285 for (fm = mas->endpoint->sending_messages_head; NULL != fm; fm = fm->next)
1287 ret = GNUNET_FRAGMENT_process_ack (fm->fragcontext, hdr);
1288 if (GNUNET_OK == ret)
1290 LOG (GNUNET_ERROR_TYPE_DEBUG,
1291 "Got last ACK, finished message transmission to `%s' (%p)\n",
1292 wlan_plugin_address_to_string (NULL, &mas->endpoint->addr, sizeof (struct WlanAddress)),
1294 mas->endpoint->timeout = GNUNET_TIME_relative_to_absolute (MACENDPOINT_TIMEOUT);
1295 if (NULL != fm->cont)
1297 fm->cont (fm->cont_cls, &fm->target, GNUNET_OK, fm->size_payload, fm->size_on_wire);
1300 free_fragment_message (fm);
1303 if (GNUNET_NO == ret)
1305 LOG (GNUNET_ERROR_TYPE_DEBUG,
1306 "Got an ACK, message transmission to `%s' not yet finished\n",
1307 wlan_plugin_address_to_string (NULL, &mas->endpoint->addr, sizeof (struct WlanAddress)));
1311 LOG (GNUNET_ERROR_TYPE_DEBUG,
1312 "ACK not matched against any active fragmentation with MAC `%s'\n",
1313 wlan_plugin_address_to_string (NULL, &mas->endpoint->addr, sizeof (struct WlanAddress)));
1315 case GNUNET_MESSAGE_TYPE_WLAN_DATA:
1316 if (NULL == mas->endpoint)
1321 if (msize < sizeof (struct WlanHeader))
1326 wlanheader = (const struct WlanHeader *) hdr;
1327 if (0 != memcmp (&wlanheader->target,
1328 plugin->env->my_identity,
1329 sizeof (struct GNUNET_PeerIdentity)))
1331 LOG (GNUNET_ERROR_TYPE_DEBUG,
1332 "WLAN data for `%s', not for me, ignoring\n",
1333 GNUNET_i2s (&wlanheader->target));
1336 if (ntohl (wlanheader->crc) !=
1337 GNUNET_CRYPTO_crc32_n (&wlanheader[1], msize - sizeof (struct WlanHeader)))
1339 GNUNET_STATISTICS_update (plugin->env->stats,
1340 _("# WLAN DATA messages discarded due to CRC32 error"), 1,
1344 xmas.endpoint = mas->endpoint;
1345 if (NULL == (xmas.session = lookup_session (mas->endpoint, &wlanheader->sender)))
1347 xmas.session = create_session (mas->endpoint, &wlanheader->sender);
1348 plugin->env->session_start (NULL, &wlanheader->sender,
1349 PLUGIN_NAME, NULL, 0, xmas.session, NULL, 0);
1350 LOG (GNUNET_ERROR_TYPE_DEBUG,
1351 "Notifying transport about peer `%s''s new inbound session %p \n",
1352 GNUNET_i2s (&wlanheader->sender), xmas.session);
1354 LOG (GNUNET_ERROR_TYPE_DEBUG,
1355 "Processing %u bytes of WLAN DATA from peer `%s'\n",
1356 (unsigned int) msize,
1357 GNUNET_i2s (&wlanheader->sender));
1358 (void) GNUNET_SERVER_mst_receive (plugin->wlan_header_payload_tokenizer,
1360 (const char *) &wlanheader[1],
1361 msize - sizeof (struct WlanHeader),
1362 GNUNET_YES, GNUNET_NO);
1365 if (NULL == mas->endpoint)
1370 if (NULL == mas->session)
1375 LOG (GNUNET_ERROR_TYPE_DEBUG,
1376 "Received packet with %u bytes of type %u from peer %s\n",
1377 (unsigned int) msize,
1378 (unsigned int) ntohs (hdr->type),
1379 GNUNET_i2s (&mas->session->target));
1380 plugin->env->receive (plugin->env->cls,
1381 &mas->session->target,
1384 (mas->endpoint == NULL) ? NULL : (const char *) &mas->endpoint->addr,
1385 (mas->endpoint == NULL) ? 0 : sizeof (struct WlanAddress));
1386 plugin->env->update_address_metrics (plugin->env->cls,
1387 &mas->session->target,
1388 (mas->endpoint == NULL) ? NULL : (const char *) &mas->endpoint->addr,
1389 (mas->endpoint == NULL) ? 0 : sizeof (struct WlanAddress),
1399 * Function used for to process the data from the suid process
1401 * @param cls the plugin handle
1402 * @param client client that send the data (not used)
1403 * @param hdr header of the GNUNET_MessageHeader
1406 handle_helper_message (void *cls, void *client,
1407 const struct GNUNET_MessageHeader *hdr)
1409 struct Plugin *plugin = cls;
1410 const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rxinfo;
1411 const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *cm;
1412 struct WlanAddress wa;
1413 struct MacAndSession mas;
1416 msize = ntohs (hdr->size);
1417 switch (ntohs (hdr->type))
1419 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
1420 if (msize != sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage))
1425 cm = (const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *) hdr;
1426 if (GNUNET_YES == plugin->have_mac)
1428 if (0 == memcmp (&plugin->mac_address,
1430 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1431 break; /* no change */
1432 /* remove old address */
1433 memset (&wa, 0, sizeof (struct WlanAddress));
1434 wa.mac = plugin->mac_address;
1435 wa.options = htonl(plugin->options);
1436 plugin->env->notify_address (plugin->env->cls, GNUNET_NO,
1441 plugin->mac_address = cm->mac;
1442 plugin->have_mac = GNUNET_YES;
1443 memset (&wa, 0, sizeof (struct WlanAddress));
1444 wa.mac = plugin->mac_address;
1445 wa.options = htonl(plugin->options);
1446 LOG (GNUNET_ERROR_TYPE_DEBUG,
1447 "Received WLAN_HELPER_CONTROL message with MAC address `%s' for peer `%s'\n",
1448 mac_to_string (&cm->mac),
1449 GNUNET_i2s (plugin->env->my_identity));
1450 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
1452 sizeof (struct WlanAddress),
1455 case GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER:
1456 LOG (GNUNET_ERROR_TYPE_DEBUG,
1457 "Got data message from helper with %u bytes\n",
1459 GNUNET_STATISTICS_update (plugin->env->stats,
1460 _("# DATA messages received via WLAN"), 1,
1462 if (msize < sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage))
1465 LOG (GNUNET_ERROR_TYPE_DEBUG,
1466 "Size of packet is too small (%u bytes)\n",
1470 rxinfo = (const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) hdr;
1472 /* check if message is actually for us */
1473 if (0 != memcmp (&rxinfo->frame.addr3, &mac_bssid_gnunet,
1474 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1476 /* Not the GNUnet BSSID */
1479 if ( (0 != memcmp (&rxinfo->frame.addr1, &bc_all_mac,
1480 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))) &&
1481 (0 != memcmp (&rxinfo->frame.addr1, &plugin->mac_address,
1482 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))) )
1484 /* Neither broadcast nor specifically for us */
1487 if (0 == memcmp (&rxinfo->frame.addr2, &plugin->mac_address,
1488 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1490 /* packet is FROM us, thus not FOR us */
1494 GNUNET_STATISTICS_update (plugin->env->stats,
1495 _("# WLAN DATA messages processed"),
1497 LOG (GNUNET_ERROR_TYPE_DEBUG,
1498 "Receiving %u bytes of data from MAC `%s'\n",
1499 (unsigned int) (msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)),
1500 mac_to_string (&rxinfo->frame.addr2));
1501 wa.mac = rxinfo->frame.addr2;
1502 wa.options = htonl (0);
1503 mas.endpoint = create_macendpoint (plugin, &wa);
1505 (void) GNUNET_SERVER_mst_receive (plugin->helper_payload_tokenizer,
1507 (const char*) &rxinfo[1],
1508 msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage),
1509 GNUNET_YES, GNUNET_NO);
1513 LOG (GNUNET_ERROR_TYPE_DEBUG,
1514 "Unexpected message of type %u (%u bytes)",
1515 ntohs (hdr->type), ntohs (hdr->size));
1524 * Task to (periodically) send a HELLO beacon
1526 * @param cls pointer to the plugin struct
1527 * @param tc scheduler context
1530 send_hello_beacon (void *cls,
1531 const struct GNUNET_SCHEDULER_TaskContext *tc)
1533 struct Plugin *plugin = cls;
1535 uint16_t hello_size;
1536 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
1537 const struct GNUNET_MessageHeader *hello;
1539 hello = plugin->env->get_our_hello ();
1540 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1541 GNUNET_assert (sizeof (struct WlanHeader) + hello_size <= WLAN_MTU);
1542 size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + hello_size;
1544 char buf[size] GNUNET_ALIGN;
1546 LOG (GNUNET_ERROR_TYPE_DEBUG,
1547 "Sending %u byte HELLO beacon\n",
1548 (unsigned int) size);
1549 radioHeader = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage*) buf;
1550 get_radiotap_header (NULL, radioHeader, size);
1551 get_wlan_header (plugin, &radioHeader->frame, &bc_all_mac, size);
1552 memcpy (&radioHeader[1], hello, hello_size);
1554 GNUNET_HELPER_send (plugin->suid_helper,
1555 &radioHeader->header,
1556 GNUNET_YES /* can drop */,
1558 GNUNET_STATISTICS_update (plugin->env->stats, _("# HELLO beacons sent via WLAN"),
1561 plugin->beacon_task =
1562 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1563 (HELLO_BEACON_SCALING_FACTOR,
1564 plugin->mac_count + 1),
1572 * Another peer has suggested an address for this
1573 * peer and transport plugin. Check that this could be a valid
1574 * address. If so, consider adding it to the list
1577 * @param cls closure
1578 * @param addr pointer to the address
1579 * @param addrlen length of addr
1580 * @return GNUNET_OK if this is a plausible address for this peer
1584 wlan_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1586 struct Plugin *plugin = cls;
1587 struct WlanAddress *wa = (struct WlanAddress *) addr;
1589 if (addrlen != sizeof (struct WlanAddress))
1591 GNUNET_break_op (0);
1592 return GNUNET_SYSERR;
1594 if (GNUNET_YES != plugin->have_mac)
1596 LOG (GNUNET_ERROR_TYPE_DEBUG,
1597 "Rejecting MAC `%s': I don't know my MAC!\n",
1598 mac_to_string (addr));
1599 return GNUNET_NO; /* don't know my MAC */
1601 if (0 != memcmp (&wa->mac,
1602 &plugin->mac_address,
1605 LOG (GNUNET_ERROR_TYPE_DEBUG,
1606 "Rejecting MAC `%s': not my MAC!\n",
1607 mac_to_string (addr));
1608 return GNUNET_NO; /* not my MAC */
1615 * Function called for a quick conversion of the binary address to
1616 * a numeric address. Note that the caller must not free the
1617 * address and that the next call to this function is allowed
1618 * to override the address again.
1620 * @param cls closure
1621 * @param addr binary address
1622 * @param addrlen length of the address
1623 * @return string representing the same address
1626 wlan_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
1628 const struct GNUNET_TRANSPORT_WLAN_MacAddress *mac;
1629 static char macstr[36];
1631 if (sizeof (struct WlanAddress) != addrlen)
1636 mac = &((struct WlanAddress *) addr)->mac;
1637 GNUNET_snprintf (macstr, sizeof (macstr), "%s.%u.%s",
1638 PLUGIN_NAME, ntohl (((struct WlanAddress *) addr)->options),
1639 mac_to_string (mac));
1645 * Convert the transports address to a nice, human-readable format.
1647 * @param cls closure
1648 * @param type name of the transport that generated the address
1649 * @param addr one of the addresses of the host, NULL for the last address
1650 * the specific address format depends on the transport
1651 * @param addrlen length of the address
1652 * @param numeric should (IP) addresses be displayed in numeric form?
1653 * @param timeout after how long should we give up?
1654 * @param asc function to call on each string
1655 * @param asc_cls closure for asc
1658 wlan_plugin_address_pretty_printer (void *cls, const char *type,
1659 const void *addr, size_t addrlen,
1661 struct GNUNET_TIME_Relative timeout,
1662 GNUNET_TRANSPORT_AddressStringCallback asc,
1667 if (sizeof (struct WlanAddress) != addrlen)
1669 /* invalid address */
1670 LOG (GNUNET_ERROR_TYPE_WARNING,
1671 _("WLAN address with invalid size encountered\n"));
1672 asc (asc_cls, NULL);
1675 ret = GNUNET_strdup (wlan_plugin_address_to_string(NULL, addr, addrlen));
1678 asc (asc_cls, NULL);
1683 * Exit point from the plugin.
1685 * @param cls pointer to the api struct
1688 libgnunet_plugin_transport_wlan_done (void *cls)
1690 struct WlanAddress wa;
1691 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1692 struct Plugin *plugin = api->cls;
1693 struct MacEndpoint *endpoint;
1694 struct MacEndpoint *endpoint_next;
1702 if (GNUNET_YES == plugin->have_mac)
1704 memset (&wa, 0, sizeof (wa));
1705 wa.options = htonl (plugin->options);
1706 wa.mac = plugin->mac_address;
1707 plugin->env->notify_address (plugin->env->cls, GNUNET_NO,
1709 sizeof (struct WlanAddress),
1711 plugin->have_mac = GNUNET_NO;
1714 if (GNUNET_SCHEDULER_NO_TASK != plugin->beacon_task)
1716 GNUNET_SCHEDULER_cancel (plugin->beacon_task);
1717 plugin->beacon_task = GNUNET_SCHEDULER_NO_TASK;
1719 if (NULL != plugin->suid_helper)
1721 GNUNET_HELPER_stop (plugin->suid_helper, GNUNET_NO);
1722 plugin->suid_helper = NULL;
1724 endpoint_next = plugin->mac_head;
1725 while (NULL != (endpoint = endpoint_next))
1727 endpoint_next = endpoint->next;
1728 free_macendpoint (endpoint);
1730 if (NULL != plugin->fragment_data_tokenizer)
1732 GNUNET_SERVER_mst_destroy (plugin->fragment_data_tokenizer);
1733 plugin->fragment_data_tokenizer = NULL;
1735 if (NULL != plugin->wlan_header_payload_tokenizer)
1737 GNUNET_SERVER_mst_destroy (plugin->wlan_header_payload_tokenizer);
1738 plugin->wlan_header_payload_tokenizer = NULL;
1740 if (NULL != plugin->helper_payload_tokenizer)
1742 GNUNET_SERVER_mst_destroy (plugin->helper_payload_tokenizer);
1743 plugin->helper_payload_tokenizer = NULL;
1745 GNUNET_free_non_null (plugin->interface);
1746 GNUNET_free (plugin);
1753 * Function called to convert a string address to
1756 * @param cls closure ('struct Plugin*')
1757 * @param addr string address
1758 * @param addrlen length of the address
1759 * @param buf location to store the buffer
1760 * @param added location to store the number of bytes in the buffer.
1761 * If the function returns GNUNET_SYSERR, its contents are undefined.
1762 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1765 wlan_string_to_address (void *cls, const char *addr, uint16_t addrlen,
1766 void **buf, size_t *added)
1768 struct WlanAddress *wa;
1774 if ((NULL == addr) || (addrlen == 0))
1777 return GNUNET_SYSERR;
1779 if ('\0' != addr[addrlen - 1])
1782 return GNUNET_SYSERR;
1784 if (strlen (addr) != addrlen - 1)
1787 return GNUNET_SYSERR;
1790 if (8 != SSCANF (addr,
1791 "%4s.%u.%X:%X:%X:%X:%X:%X",
1793 &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]))
1796 return GNUNET_SYSERR;
1798 wa = GNUNET_malloc (sizeof (struct WlanAddress));
1800 wa->mac.mac[i] = a[i];
1801 wa->options = htonl (0);
1803 *added = sizeof (struct WlanAddress);
1809 * Entry point for the plugin.
1811 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
1812 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
1815 libgnunet_plugin_transport_wlan_init (void *cls)
1817 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1818 struct GNUNET_TRANSPORT_PluginFunctions *api;
1819 struct Plugin *plugin;
1821 unsigned long long testmode;
1824 /* check for 'special' mode */
1825 if (NULL == env->receive)
1827 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
1828 initialze the plugin or the API */
1829 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1831 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
1832 api->address_to_string = &wlan_plugin_address_to_string;
1833 api->string_to_address = &wlan_string_to_address;
1838 /* check configuration */
1840 GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "TESTMODE")) &&
1842 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-wlan",
1843 "TESTMODE", &testmode)) ||
1846 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1847 "transport-wlan", "TESTMODE");
1850 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-transport-wlan");
1851 if ( (0 == testmode) &&
1852 (GNUNET_YES != GNUNET_OS_check_helper_binary (binary, GNUNET_YES, NULL)) )
1854 LOG (GNUNET_ERROR_TYPE_ERROR,
1855 _("Helper binary `%s' not SUID, cannot run WLAN transport\n"),
1856 "gnunet-helper-transport-wlan");
1857 GNUNET_free (binary);
1860 GNUNET_free (binary);
1862 GNUNET_CONFIGURATION_get_value_string
1863 (env->cfg, "transport-wlan", "INTERFACE",
1866 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1867 "transport-wlan", "INTERFACE");
1871 plugin = GNUNET_malloc (sizeof (struct Plugin));
1872 plugin->interface = interface;
1874 GNUNET_STATISTICS_set (plugin->env->stats, _("# WLAN sessions allocated"),
1876 GNUNET_STATISTICS_set (plugin->env->stats, _("# WLAN MAC endpoints allocated"),
1878 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
1879 GNUNET_BANDWIDTH_value_init (100 * 1024 *
1881 plugin->fragment_data_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
1882 plugin->wlan_header_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
1883 plugin->helper_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
1884 plugin->beacon_task = GNUNET_SCHEDULER_add_now (&send_hello_beacon,
1887 plugin->options = 0;
1889 /* some compilers do not like switch on 'long long'... */
1890 switch ((unsigned int) testmode)
1892 case 0: /* normal */
1893 plugin->helper_argv[0] = (char *) "gnunet-helper-transport-wlan";
1894 plugin->helper_argv[1] = interface;
1895 plugin->helper_argv[2] = NULL;
1896 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
1897 "gnunet-helper-transport-wlan",
1898 plugin->helper_argv,
1899 &handle_helper_message,
1903 case 1: /* testmode, peer 1 */
1904 plugin->helper_argv[0] = (char *) "gnunet-helper-transport-wlan-dummy";
1905 plugin->helper_argv[1] = (char *) "1";
1906 plugin->helper_argv[2] = NULL;
1907 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
1908 "gnunet-helper-transport-wlan-dummy",
1909 plugin->helper_argv,
1910 &handle_helper_message,
1914 case 2: /* testmode, peer 2 */
1915 plugin->helper_argv[0] = (char *) "gnunet-helper-transport-wlan-dummy";
1916 plugin->helper_argv[1] = (char *) "2";
1917 plugin->helper_argv[2] = NULL;
1918 plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
1919 "gnunet-helper-transport-wlan-dummy",
1920 plugin->helper_argv,
1921 &handle_helper_message,
1929 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1931 api->send = &wlan_plugin_send;
1932 api->get_session = &wlan_plugin_get_session;
1933 api->disconnect = &wlan_plugin_disconnect;
1934 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
1935 api->check_address = &wlan_plugin_address_suggested;
1936 api->address_to_string = &wlan_plugin_address_to_string;
1937 api->string_to_address = &wlan_string_to_address;
1938 api->get_network = &wlan_get_network;
1943 /* end of plugin_transport_wlan.c */