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 //TODO split rx and tx structures for better handling
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_util_lib.h"
34 #include "gnunet_statistics_service.h"
35 #include "gnunet_transport_service.h"
36 #include "gnunet_transport_plugin.h"
37 #include "plugin_transport_wlan.h"
38 #include "gnunet_common.h"
39 #include "gnunet_crypto_lib.h"
40 #include "gnunet_fragmentation_lib.h"
41 #include "gnunet_constants.h"
44 #define PROTOCOL_PREFIX "wlan"
46 #define PLUGIN_LOG_NAME "wlan-plugin"
54 * time out of a session
56 #define SESSION_TIMEOUT GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT
59 * time out of a mac endpoint
61 #define MACENDPOINT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 2)
64 * scaling factor for hello beacon
66 #define HELLO_BEACON_SCALING_FACTOR 30
69 * scaling factor for restarting the helper
71 #define HELPER_RESTART_SCALING_FACTOR 2
74 * max size of fragment queue
76 #define FRAGMENT_QUEUE_SIZE 10
78 * max messages in fragment queue per session/client
80 #define FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION 1
83 * max messages in fragment queue per MAC
85 #define FRAGMENT_QUEUE_MESSAGES_OUT_PER_MACENDPOINT 1
88 * max messages in in queue
90 #define MESSAGES_IN_QUEUE_SIZE 10
93 * max messages in in queue per session/client
95 #define MESSAGES_IN_DEFRAG_QUEUE_PER_MAC 1
98 * Link layer control fields for better compatibility
99 * (i.e. GNUnet over WLAN is not IP-over-WLAN).
101 #define WLAN_LLC_DSAP_FIELD 0x1f
102 #define WLAN_LLC_SSAP_FIELD 0x1f
107 * Encapsulation of all of the state of the plugin.
114 struct GNUNET_TRANSPORT_PluginEnvironment *env;
117 * List of open connections. head
119 struct MacEndpoint *mac_head;
122 * List of open connections. tail
124 struct MacEndpoint *mac_tail;
127 * Number of connections
129 unsigned int mac_count;
132 * encapsulation of data from the local wlan helper program
134 struct GNUNET_SERVER_MessageStreamTokenizer *suid_tokenizer;
137 * encapsulation of packets received from the wlan helper
139 struct GNUNET_SERVER_MessageStreamTokenizer *data_tokenizer;
142 * stdout pipe handle for the gnunet-helper-transport-wlan process
144 struct GNUNET_DISK_PipeHandle *server_stdout;
147 * stdout file handle for the gnunet-helper-transport-wlan process
149 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
152 * stdin pipe handle for the gnunet-helper-transport-wlan process
154 struct GNUNET_DISK_PipeHandle *server_stdin;
157 * stdin file handle for the gnunet-helper-transport-wlan process
159 const struct GNUNET_DISK_FileHandle *server_stdin_handle;
162 * ID of the gnunet-wlan-server std read task
164 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
167 * ID of the gnunet-wlan-server std read task
169 GNUNET_SCHEDULER_TaskIdentifier server_write_task;
172 * ID of the delay task for writing
174 GNUNET_SCHEDULER_TaskIdentifier server_write_delay_task;
177 * The process id of the wlan process
179 struct GNUNET_OS_Process *server_proc;
182 * The interface of the wlan card given to us by the user.
187 * Mode of operation for the helper, 0 = normal, 1 = first loopback, 2 = second loopback
189 long long unsigned int testmode;
192 * The mac_address of the wlan card given to us by the helper.
194 struct GNUNET_TRANSPORT_WLAN_MacAddress mac_address;
197 * Sessions currently pending for transmission
200 struct Sessionqueue *pending_Sessions_head;
203 * Sessions currently pending for transmission
204 * to a peer (tail), if any.
206 struct Sessionqueue *pending_Sessions_tail;
209 * number of pending sessions
211 unsigned int pendingsessions;
214 * Messages in the sending queues
216 int pending_Fragment_Messages;
219 * messages ready for send, head
221 struct FragmentMessage_queue *sending_messages_head;
224 * messages ready for send, tail
226 struct FragmentMessage_queue *sending_messages_tail;
229 * time of the next "hello-beacon"
231 struct GNUNET_TIME_Absolute beacon_time;
234 * queue to send acks for received fragments (head)
236 struct AckSendQueue *ack_send_queue_head;
239 * queue to send acks for received fragments (tail)
241 struct AckSendQueue *ack_send_queue_tail;
244 * Tracker for bandwidth limit
246 struct GNUNET_BANDWIDTH_Tracker tracker;
249 * saves the current state of the helper process
251 int helper_is_running;
255 * Struct to store data if file write did not accept the whole packet
260 * pointer to the global plugin struct
262 struct Plugin *plugin;
265 * head of the next part to send to the helper
267 const char *head_of_next_write;
270 * Start of the message to send, needed for free
272 struct GNUNET_MessageHeader *msgstart;
281 * Queue of sessions, for the general session queue and the pending session queue
286 struct Sessionqueue *next;
287 struct Sessionqueue *prev;
288 struct Session *content;
289 #if !HAVE_UNALIGNED_64_ACCESS
290 void *dummy; /* for alignment, see #1909 */
296 * Queue of fragmented messages, for the sending queue of the plugin
299 struct FragmentMessage_queue
301 struct FragmentMessage_queue *next;
302 struct FragmentMessage_queue *prev;
303 struct FragmentMessage *content;
308 * Queue for the fragments received
311 struct Receive_Fragment_Queue
313 struct Receive_Fragment_Queue *next;
314 struct Receive_Fragment_Queue *prev;
319 struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rx_msg;
324 struct MacEndpoint_id_fragment_triple
326 struct MacEndpoint *endpoint;
328 struct FragmentMessage *fm;
332 struct Plugin_Session_pair
334 struct Plugin *plugin;
335 struct Session *session;
339 GNUNET_NETWORK_STRUCT_BEGIN
342 * Header for messages which need fragmentation
348 * Message type is GNUNET_MESSAGE_TYPE_WLAN_DATA.
350 struct GNUNET_MessageHeader header;
353 * checksum/error correction
355 uint32_t crc GNUNET_PACKED;
358 * To whom are we talking to (set to our identity
359 * if we are still waiting for the welcome message)
361 struct GNUNET_PeerIdentity target;
364 * Where the packet came from
366 struct GNUNET_PeerIdentity source;
368 // followed by payload
371 GNUNET_NETWORK_STRUCT_END
374 * Information kept for each message that is yet to
377 struct PendingMessage
382 struct PendingMessage *next;
387 struct PendingMessage *prev;
390 * The pending message
392 struct WlanHeader *msg;
395 * Size of the message
400 * Continuation function to call once the message
401 * has been sent. Can be NULL if there is no
402 * continuation to call.
404 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
407 * Cls for transmit_cont
409 void *transmit_cont_cls;
412 * Timeout value for the pending message.
414 struct GNUNET_TIME_Absolute timeout;
420 * Queue for acks to send for fragments recived
426 * next ack in the ack send queue
428 struct AckSendQueue *next;
431 * previous ack in the ack send queue
433 struct AckSendQueue *prev;
436 * pointer to the session this ack belongs to
438 struct MacEndpoint *endpoint;
441 * ID of message, to distinguish between the messages, picked randomly.
446 * pointer to the radiotap header with the ACK Message.
448 const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
453 * Session handle for connections.
461 struct SessionHeader header;
464 * Message currently pending for transmission
465 * to this peer, if any. head
467 struct PendingMessage *pending_message_head;
470 * Message currently pending for transmission
471 * to this peer, if any. tail
473 struct PendingMessage *pending_message_tail;
476 * To whom are we talking to (set to our identity
477 * if we are still waiting for the welcome message)
479 struct GNUNET_PeerIdentity target;
482 * Address of the other peer (either based on our 'connect'
483 * call or on our 'accept' call).
488 * Last activity on this connection. Used to select preferred
489 * connection and timeout
491 struct GNUNET_TIME_Absolute last_activity;
496 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
501 struct MacEndpoint *mac;
504 * count of messages in the fragment out queue for this session
507 int fragment_messages_out_count;
512 * Struct to represent one network card connection
517 * Pointer to the global plugin struct.
519 struct Plugin *plugin;
522 * Struct to hold the session reachable over this mac; head
524 struct Sessionqueue *sessions_head;
527 * Struct to hold the session reachable over this mac; tail
529 struct Sessionqueue *sessions_tail;
532 * Messages currently sending
535 struct FragmentMessage *sending_messages_head;
538 * Messages currently sending
539 * to a peer (tail), if any.
541 struct FragmentMessage *sending_messages_tail;
546 struct MacEndpoint *next;
551 struct MacEndpoint *prev;
556 struct GNUNET_TRANSPORT_WLAN_MacAddress addr;
559 * Defrag context for this mac endpoint
561 struct GNUNET_DEFRAGMENT_Context *defrag;
564 * count of messages in the fragment out queue for this mac endpoint
566 int fragment_messages_out_count;
574 * Duplicates received
589 * Last activity on this endpoint. Used to select preferred
592 struct GNUNET_TIME_Absolute last_activity;
597 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
601 * Struct for Messages in the fragment queue
603 struct FragmentMessage
607 * Session this message belongs to
609 struct Session *session;
612 * This is a doubly-linked list.
614 struct FragmentMessage *next;
617 * This is a doubly-linked list.
619 struct FragmentMessage *prev;
622 * Fragmentation context
624 struct GNUNET_FRAGMENT_Context *fragcontext;
627 * Timeout value for the message.
629 struct GNUNET_TIME_Absolute timeout;
634 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
637 * pointer to the radiotap header
639 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
644 do_transmit (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
648 free_session (struct Plugin *plugin, struct Sessionqueue *queue,
649 int do_free_macendpoint);
652 static struct MacEndpoint *
653 create_macendpoint (struct Plugin *plugin, const struct GNUNET_TRANSPORT_WLAN_MacAddress *addr);
657 finish_sending (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
662 * Function to find a MacEndpoint with a specific mac addr
663 * @param plugin pointer to the plugin struct
664 * @param addr pointer to the mac address
665 * @param create_new GNUNET_YES if a new end point should be created
668 static struct MacEndpoint *
669 get_macendpoint (struct Plugin *plugin, const struct GNUNET_TRANSPORT_WLAN_MacAddress *addr,
672 struct MacEndpoint *queue = plugin->mac_head;
674 while (queue != NULL)
676 //GNUNET_assert (queue->sessions_head != NULL);
677 if (memcmp (addr, &queue->addr, sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0)
678 return queue; /* session found */
682 if (create_new == GNUNET_YES)
684 return create_macendpoint (plugin, addr);
694 * search for a session with the macendpoint and peer id
696 * @param plugin pointer to the plugin struct
697 * @param endpoint pointer to the mac endpoint of the peer
698 * @param peer pointer to the peerid
699 * @return returns the session
701 static struct Session *
702 search_session (struct Plugin *plugin, const struct MacEndpoint *endpoint,
703 const struct GNUNET_PeerIdentity *peer)
705 GNUNET_assert (endpoint != NULL);
706 struct Sessionqueue *queue = endpoint->sessions_head;
708 while (queue != NULL)
710 GNUNET_assert (queue->content != NULL);
712 (peer, &queue->content->target,
713 sizeof (struct GNUNET_PeerIdentity)) == 0)
714 return queue->content; /* session found */
721 * Function called for a quick conversion of the binary address to
722 * a numeric address. Note that the caller must not free the
723 * address and that the next call to this function is allowed
724 * to override the address again.
727 * @param addr binary address
728 * @param addrlen length of the address
729 * @return string representing the same address
732 wlan_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
735 const struct GNUNET_TRANSPORT_WLAN_MacAddress *mac;
737 if (addrlen != sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))
743 GNUNET_snprintf (ret, sizeof (ret), "%s Mac-Address %X:%X:%X:%X:%X:%X",
744 PROTOCOL_PREFIX, mac->mac[0], mac->mac[1], mac->mac[2],
745 mac->mac[3], mac->mac[4], mac->mac[5]);
751 * Function for the scheduler if a session times out
752 * @param cls pointer to the Sessionqueue
753 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
756 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
758 struct Sessionqueue *queue = cls;
760 GNUNET_assert (queue != NULL);
761 GNUNET_assert (queue->content != NULL);
762 queue->content->timeout_task = GNUNET_SCHEDULER_NO_TASK;
763 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
767 if (GNUNET_TIME_absolute_get_remaining
768 (GNUNET_TIME_absolute_add
769 (queue->content->last_activity, SESSION_TIMEOUT)).rel_value == 0)
772 GNUNET_assert (queue->content->mac != NULL);
773 GNUNET_assert (queue->content->mac->plugin != NULL);
774 GNUNET_STATISTICS_update (queue->content->mac->plugin->env->stats,
775 _("# wlan session timeouts"), 1, GNUNET_NO);
776 free_session (queue->content->mac->plugin, queue, GNUNET_YES);
780 queue->content->timeout_task =
781 GNUNET_SCHEDULER_add_delayed (SESSION_TIMEOUT, &session_timeout, queue);
786 * create a new session
788 * @param plugin pointer to the plugin struct
789 * @param endpoint pointer to the mac endpoint of the peer
790 * @param peer peer identity to use for this session
791 * @return returns the session
793 static struct Session *
794 create_session (struct Plugin *plugin, struct MacEndpoint *endpoint,
795 const struct GNUNET_PeerIdentity *peer)
797 GNUNET_assert (endpoint != NULL);
798 GNUNET_assert (plugin != NULL);
799 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan session created"), 1,
801 struct Sessionqueue *queue =
802 GNUNET_malloc (sizeof (struct Sessionqueue) + sizeof (struct Session));
804 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
805 endpoint->sessions_tail, queue);
807 queue->content = (struct Session *) &queue[1];
808 queue->content->mac = endpoint;
809 queue->content->target = *peer;
810 queue->content->last_activity = GNUNET_TIME_absolute_get ();
811 queue->content->timeout_task =
812 GNUNET_SCHEDULER_add_delayed (SESSION_TIMEOUT, &session_timeout, queue);
814 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
815 "New session %p with endpoint %p: %s\n", queue->content,
816 endpoint, wlan_plugin_address_to_string (NULL,
819 return queue->content;
823 * Get session from address, create if no session exists
825 * @param plugin pointer to the plugin struct
826 * @param addr pointer to the mac address of the peer
827 * @param peer pointer to the peerid
828 * @return returns the session
830 static struct Session *
831 get_session (struct Plugin *plugin, const struct GNUNET_TRANSPORT_WLAN_MacAddress *addr,
832 const struct GNUNET_PeerIdentity *peer)
834 struct MacEndpoint *mac;
836 mac = get_macendpoint (plugin, addr, GNUNET_YES);
837 struct Session *session = search_session (plugin, mac, peer);
841 return create_session (plugin, mac, peer);
845 * Queue the session to send data
846 * checks if there is a message pending
847 * checks if this session is not allready in the queue
848 * @param plugin pointer to the plugin
849 * @param session pointer to the session to add
852 queue_session (struct Plugin *plugin, struct Session *session)
854 struct Sessionqueue *queue = plugin->pending_Sessions_head;
856 if (session->pending_message_head != NULL)
858 while (queue != NULL)
860 // content is never NULL
861 GNUNET_assert (queue->content != NULL);
862 // is session already in queue?
863 if (session == queue->content)
871 // Session is not in the queue
873 queue = GNUNET_malloc (sizeof (struct Sessionqueue));
874 queue->content = session;
877 GNUNET_CONTAINER_DLL_insert_tail (plugin->pending_Sessions_head,
878 plugin->pending_Sessions_tail, queue);
879 plugin->pendingsessions++;
880 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
881 plugin->pendingsessions, GNUNET_NO);
887 * Function to schedule the write task, executed after a delay
888 * @param cls pointer to the plugin struct
889 * @param tc GNUNET_SCHEDULER_TaskContext pointer
892 delay_fragment_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
894 struct Plugin *plugin = cls;
896 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
898 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
901 // GNUNET_TIME_UNIT_FOREVER_REL is needed to clean up old msg
902 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
904 plugin->server_write_task =
905 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
906 plugin->server_stdin_handle,
907 &do_transmit, plugin);
912 * Function to calculate the time of the next periodic "hello-beacon"
913 * @param plugin pointer to the plugin struct
916 set_next_beacon_time (struct Plugin *const plugin)
918 //under 10 known peers: once a second
919 if (plugin->mac_count < 10)
921 plugin->beacon_time =
922 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
923 GNUNET_TIME_relative_multiply
924 (GNUNET_TIME_UNIT_SECONDS,
925 HELLO_BEACON_SCALING_FACTOR));
927 //under 30 known peers: every 10 seconds
928 else if (plugin->mac_count < 30)
930 plugin->beacon_time =
931 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
932 GNUNET_TIME_relative_multiply
933 (GNUNET_TIME_UNIT_SECONDS,
934 10 * HELLO_BEACON_SCALING_FACTOR));
936 //over 30 known peers: once a minute
939 plugin->beacon_time =
940 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
941 GNUNET_TIME_relative_multiply
942 (GNUNET_TIME_UNIT_MINUTES,
943 HELLO_BEACON_SCALING_FACTOR));
949 * Function to set the timer for the next timeout of the fragment queue
950 * @param plugin the handle to the plugin struct
953 set_next_send (struct Plugin *plugin)
955 struct GNUNET_TIME_Relative next_send;
957 //abort if helper is not running
958 if (plugin->helper_is_running == GNUNET_NO)
964 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
966 GNUNET_SCHEDULER_cancel (plugin->server_write_delay_task);
967 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
970 //check if some acks are in the queue
971 if (plugin->ack_send_queue_head != NULL)
973 next_send = GNUNET_TIME_UNIT_ZERO;
976 //check if there are some fragments in the queue
977 else if (plugin->sending_messages_head != NULL)
979 next_send = GNUNET_TIME_UNIT_ZERO;
983 next_send = GNUNET_TIME_absolute_get_remaining (plugin->beacon_time);
986 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
987 "Next packet is send in: %u\n", next_send.rel_value);
988 if (next_send.rel_value == GNUNET_TIME_UNIT_ZERO.rel_value)
990 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
992 plugin->server_write_task =
993 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
994 plugin->server_stdin_handle,
995 &do_transmit, plugin);
1000 if (plugin->server_write_delay_task == GNUNET_SCHEDULER_NO_TASK)
1002 plugin->server_write_delay_task =
1003 GNUNET_SCHEDULER_add_delayed (next_send, &delay_fragment_task,
1011 * Function to get the next queued Session, removes the session from the queue
1012 * @param plugin pointer to the plugin struct
1013 * @return pointer to the session found, returns NULL if there is now session in the queue
1015 static struct Session *
1016 get_next_queue_session (struct Plugin *plugin)
1018 struct Session *session;
1019 struct Sessionqueue *sessionqueue;
1020 struct Sessionqueue *sessionqueue_old;
1021 struct PendingMessage *pm;
1023 sessionqueue = plugin->pending_Sessions_head;
1025 while (sessionqueue != NULL)
1027 session = sessionqueue->content;
1029 GNUNET_assert (session != NULL);
1030 pm = session->pending_message_head;
1034 sessionqueue_old = sessionqueue;
1035 sessionqueue = sessionqueue->next;
1036 plugin->pendingsessions--;
1037 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
1038 plugin->pendingsessions, GNUNET_NO);
1039 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1040 plugin->pending_Sessions_tail,
1043 GNUNET_free (sessionqueue_old);
1047 //check for message timeout
1048 if (GNUNET_TIME_absolute_get_remaining (pm->timeout).rel_value > 0)
1050 //check if session has no message in the fragment queue
1051 if ((session->mac->fragment_messages_out_count <
1052 FRAGMENT_QUEUE_MESSAGES_OUT_PER_MACENDPOINT) &&
1053 (session->fragment_messages_out_count <
1054 FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION))
1056 plugin->pendingsessions--;
1057 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
1058 plugin->pendingsessions, GNUNET_NO);
1059 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1060 plugin->pending_Sessions_tail,
1062 GNUNET_free (sessionqueue);
1068 sessionqueue = sessionqueue->next;
1073 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
1074 session->pending_message_tail, pm);
1076 //call the cont func that it did not work
1077 if (pm->transmit_cont != NULL)
1078 pm->transmit_cont (pm->transmit_cont_cls, &(session->target),
1080 GNUNET_free (pm->msg);
1083 if (session->pending_message_head == NULL)
1085 sessionqueue_old = sessionqueue;
1086 sessionqueue = sessionqueue->next;
1087 plugin->pendingsessions--;
1088 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
1089 plugin->pendingsessions, GNUNET_NO);
1090 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1091 plugin->pending_Sessions_tail,
1094 GNUNET_free (sessionqueue_old);
1104 * frees the space of a message in the fragment queue (send queue)
1105 * @param plugin the plugin struct
1106 * @param fm message to free
1109 free_fragment_message (struct Plugin *plugin, struct FragmentMessage *fm)
1111 struct Session *session = fm->session;
1112 struct MacEndpoint *endpoint = session->mac;
1113 struct FragmentMessage_queue *fmq;
1114 struct FragmentMessage_queue *fmq_next;
1116 fmq = plugin->sending_messages_head;
1119 fmq_next = fmq->next;
1120 if (fmq->content == fm)
1122 GNUNET_CONTAINER_DLL_remove (plugin->sending_messages_head,
1123 plugin->sending_messages_tail, fmq);
1128 session->mac->fragment_messages_out_count--;
1129 session->fragment_messages_out_count--;
1130 plugin->pending_Fragment_Messages--;
1131 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending fragments"),
1132 plugin->pending_Fragment_Messages, GNUNET_NO);
1133 GNUNET_CONTAINER_DLL_remove (endpoint->sending_messages_head,
1134 endpoint->sending_messages_tail, fm);
1135 GNUNET_FRAGMENT_context_destroy (fm->fragcontext);
1136 if (fm->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1138 GNUNET_SCHEDULER_cancel (fm->timeout_task);
1139 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1142 queue_session (plugin, session);
1143 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1144 "Free pending fragment messages %p, session %p\n", fm,
1150 * function to fill the radiotap header
1152 * @param plugin pointer to the plugin struct
1153 * @param endpoint pointer to the endpoint
1154 * @param header pointer to the radiotap header
1155 * @param size total message size
1156 * @return GNUNET_YES at success
1159 getRadiotapHeader (struct Plugin *plugin, struct MacEndpoint *endpoint,
1160 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *header,
1163 header->header.type = ntohs (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER);
1164 header->header.size = ntohs (size);
1165 if (endpoint != NULL)
1167 header->rate = endpoint->rate;
1168 header->tx_power = endpoint->tx_power;
1169 header->antenna = endpoint->antenna;
1174 header->tx_power = 0;
1175 header->antenna = 0;
1183 * function to generate the wlan hardware header for one packet
1184 * @param Header address to write the header to
1185 * @param to_mac_addr address of the recipient
1186 * @param plugin pointer to the plugin struct
1187 * @param size size of the whole packet, needed to calculate the time to send the packet
1188 * FIXME: 'size' is initialized inconsistently throughout the code (sometimes payload, sometimes everything)
1189 * @return GNUNET_YES if there was no error
1192 getWlanHeader (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *Header,
1193 const struct GNUNET_TRANSPORT_WLAN_MacAddress *to_mac_addr, struct Plugin *plugin,
1196 const int rate = 11000000;
1198 Header->frame_control = htons (IEEE80211_FC0_TYPE_DATA);
1199 Header->addr1 = *to_mac_addr;
1200 Header->addr2 = plugin->mac_address;
1201 Header->addr3 = mac_bssid_gnunet;
1202 Header->duration = GNUNET_htole16 ((size * 1000000) / rate + 290);
1203 Header->sequence_control = 0; // FIXME?
1204 Header->llc[0] = WLAN_LLC_DSAP_FIELD;
1205 Header->llc[1] = WLAN_LLC_SSAP_FIELD;
1206 Header->llc[2] = 0; // FIXME?
1207 Header->llc[3] = 0; // FIXME?
1213 * function to add a fragment of a message to send
1214 * @param cls FragmentMessage this message belongs to
1215 * @param hdr pointer to the start of the message
1218 add_message_for_send (void *cls, const struct GNUNET_MessageHeader *hdr)
1220 struct FragmentMessage *fm = cls;
1221 struct FragmentMessage_queue *fmqueue;
1222 struct MacEndpoint *endpoint;
1223 struct Plugin *plugin;
1226 GNUNET_assert (cls != NULL);
1227 endpoint = fm->session->mac;
1228 plugin = endpoint->plugin;
1230 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1231 "Adding fragment of message %p to send, session %p, endpoint %p, type %u\n",
1232 fm, fm->session, endpoint, hdr->type);
1233 size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + ntohs (hdr->size);
1234 fm->radioHeader = GNUNET_malloc (size);
1235 getRadiotapHeader (plugin, fm->session->mac, fm->radioHeader, size);
1236 getWlanHeader (&fm->radioHeader->frame, &(fm->session->mac->addr), plugin,
1238 memcpy (&fm->radioHeader[1], hdr, ntohs (hdr->size));
1239 // FIXME: yucky allocation structure!
1240 fmqueue = GNUNET_malloc (sizeof (struct FragmentMessage_queue));
1241 fmqueue->content = fm;
1242 GNUNET_CONTAINER_DLL_insert_tail (plugin->sending_messages_head,
1243 plugin->sending_messages_tail, fmqueue);
1244 set_next_send (plugin);
1249 * We have been notified that gnunet-helper-transport-wlan has written something to stdout.
1250 * Handle the output, then reschedule this function to be called again once
1251 * more is available.
1253 * @param cls the plugin handle
1254 * @param tc the scheduling context
1257 wlan_plugin_helper_read (void *cls,
1258 const struct GNUNET_SCHEDULER_TaskContext *tc)
1260 struct Plugin *plugin = cls;
1262 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
1264 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1267 char mybuf[WLAN_MTU + sizeof (struct GNUNET_MessageHeader)];
1271 GNUNET_DISK_file_read (plugin->server_stdout_handle, mybuf,
1275 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1277 ("Finished reading from gnunet-helper-transport-wlan stdout with code: %d\n"),
1281 GNUNET_SERVER_mst_receive (plugin->suid_tokenizer, NULL, mybuf, bytes,
1282 GNUNET_NO, GNUNET_NO);
1284 GNUNET_assert (plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
1285 plugin->server_read_task =
1286 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1287 plugin->server_stdout_handle,
1288 &wlan_plugin_helper_read, plugin);
1292 * Start the gnunet-helper-transport-wlan process.
1294 * @param plugin the transport plugin
1295 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
1298 wlan_transport_start_wlan_helper (struct Plugin *plugin)
1300 const char *filenamehw = "gnunet-helper-transport-wlan";
1301 const char *filenameloopback = "gnunet-helper-transport-wlan-dummy";
1302 char *absolute_filename = NULL;
1304 if (plugin->helper_is_running == GNUNET_YES)
1306 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1307 "wlan_transport_start_wlan_helper not needed, helper already running!");
1311 plugin->server_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
1312 if (plugin->server_stdout == NULL)
1313 return GNUNET_SYSERR;
1315 plugin->server_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
1316 if (plugin->server_stdin == NULL)
1317 return GNUNET_SYSERR;
1319 if ((plugin->testmode == 1) || (plugin->testmode == 2))
1321 if (GNUNET_OS_check_helper_binary (filenameloopback) == GNUNET_YES)
1323 absolute_filename = GNUNET_strdup (filenameloopback);
1327 char cwd[FILENAME_MAX];
1329 GNUNET_assert (getcwd (cwd, sizeof (cwd)) != NULL);
1331 GNUNET_asprintf (&absolute_filename, "%s%s%s", cwd, DIR_SEPARATOR_STR,
1334 if (GNUNET_DISK_file_test (filenameloopback) != GNUNET_YES)
1336 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1337 "Helper `%s' not found! %i\n", absolute_filename);
1343 /* Start the server process */
1345 if (plugin->testmode == 0)
1348 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1349 "Starting gnunet-helper-transport-wlan process cmd: %s %s %i\n",
1350 filenamehw, plugin->interface, plugin->testmode);
1351 if (GNUNET_OS_check_helper_binary (filenamehw) == GNUNET_YES)
1353 plugin->server_proc =
1354 GNUNET_OS_start_process (GNUNET_NO, plugin->server_stdin, plugin->server_stdout,
1355 filenamehw, filenamehw, plugin->interface,
1358 else if (GNUNET_OS_check_helper_binary (filenamehw) == GNUNET_NO)
1360 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1361 "gnunet-helper-transport-wlan is not suid, please change it or look at the doku\n");
1366 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1367 "gnunet-helper-transport-wlan not found, please look if it exists and is the $PATH variable!\n");
1372 else if (plugin->testmode == 1)
1375 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
1376 "Starting gnunet-helper-transport-wlan-dummy loopback 1 process cmd: %s %s %i\n",
1377 absolute_filename, plugin->interface, plugin->testmode);
1378 plugin->server_proc =
1379 GNUNET_OS_start_process (GNUNET_NO, plugin->server_stdin, plugin->server_stdout,
1380 absolute_filename, absolute_filename, "1",
1382 if (plugin->server_proc == NULL)
1384 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1385 "`%s' not found, please look if it exists and is in the $PATH variable!\n",
1390 else if (plugin->testmode == 2)
1392 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
1393 "Starting gnunet-helper-transport-wlan-dummy loopback 2 process cmd: %s %s %i\n",
1394 absolute_filename, plugin->interface, plugin->testmode);
1395 plugin->server_proc =
1396 GNUNET_OS_start_process (GNUNET_NO, plugin->server_stdin, plugin->server_stdout,
1397 absolute_filename, absolute_filename, "2",
1399 if (plugin->server_proc == NULL)
1401 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1402 "`%s' not found, please look if it exists and is in the $PATH variable!\n",
1407 if (absolute_filename != NULL)
1408 GNUNET_free (absolute_filename);
1409 if (plugin->server_proc == NULL)
1411 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1412 "Failed to start gnunet-helper-transport-wlan process\n");
1413 return GNUNET_SYSERR;
1418 /* Close the write end of the read pipe */
1419 GNUNET_DISK_pipe_close_end (plugin->server_stdout,
1420 GNUNET_DISK_PIPE_END_WRITE);
1422 /* Close the read end of the write pipe */
1423 GNUNET_DISK_pipe_close_end (plugin->server_stdin, GNUNET_DISK_PIPE_END_READ);
1425 plugin->server_stdout_handle =
1426 GNUNET_DISK_pipe_handle (plugin->server_stdout,
1427 GNUNET_DISK_PIPE_END_READ);
1428 plugin->server_stdin_handle =
1429 GNUNET_DISK_pipe_handle (plugin->server_stdin,
1430 GNUNET_DISK_PIPE_END_WRITE);
1432 GNUNET_assert (plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
1434 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1435 "Adding server_read_task for the gnunet-helper-transport-wlan\n");
1436 plugin->server_read_task =
1437 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1438 plugin->server_stdout_handle,
1439 &wlan_plugin_helper_read, plugin);
1441 plugin->helper_is_running = GNUNET_YES;
1446 * Stops the gnunet-helper-transport-wlan process.
1448 * @param plugin the transport plugin
1449 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
1452 wlan_transport_stop_wlan_helper (struct Plugin *plugin)
1454 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1455 "Stoping WLAN helper process\n");
1457 if (plugin->helper_is_running == GNUNET_NO)
1459 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1460 "wlan_transport_stop_wlan_helper not needed, helper already stopped!");
1464 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
1466 GNUNET_SCHEDULER_cancel (plugin->server_write_delay_task);
1467 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
1470 if (plugin->server_write_task != GNUNET_SCHEDULER_NO_TASK)
1472 GNUNET_SCHEDULER_cancel (plugin->server_write_task);
1473 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1476 if (plugin->server_read_task != GNUNET_SCHEDULER_NO_TASK)
1478 GNUNET_SCHEDULER_cancel (plugin->server_read_task);
1479 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
1482 GNUNET_DISK_pipe_close (plugin->server_stdout);
1483 GNUNET_DISK_pipe_close (plugin->server_stdin);
1484 GNUNET_OS_process_kill (plugin->server_proc, SIGKILL);
1485 GNUNET_OS_process_wait (plugin->server_proc);
1486 GNUNET_OS_process_close (plugin->server_proc);
1488 plugin->helper_is_running = GNUNET_NO;
1494 * function for delayed restart of the helper process
1495 * @param cls Finish_send struct if message should be finished
1496 * @param tc TaskContext
1499 delay_restart_helper (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1501 struct Finish_send *finish = cls;
1502 struct Plugin *plugin;
1504 plugin = finish->plugin;
1506 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1507 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1509 GNUNET_free_non_null (finish->msgstart);
1510 GNUNET_free (finish);
1514 wlan_transport_start_wlan_helper (plugin);
1516 if (finish->size != 0)
1518 plugin->server_write_task =
1519 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1520 plugin->server_stdin_handle,
1521 &finish_sending, finish);
1525 set_next_send (plugin);
1526 GNUNET_free_non_null (finish->msgstart);
1527 GNUNET_free (finish);
1533 * Function to restart the helper
1534 * @param plugin pointer to the global plugin struct
1535 * @param finish pointer to the Finish_send struct to finish
1538 restart_helper (struct Plugin *plugin, struct Finish_send *finish)
1540 static struct GNUNET_TIME_Relative next_try = { 1000 };
1541 GNUNET_assert (finish != NULL);
1543 wlan_transport_stop_wlan_helper (plugin);
1544 plugin->server_write_task =
1545 GNUNET_SCHEDULER_add_delayed (next_try, &delay_restart_helper, finish);
1546 GNUNET_TIME_relative_multiply (next_try, HELPER_RESTART_SCALING_FACTOR);
1551 * function to finish a sending if not all could have been writen befor
1552 * @param cls pointer to the Finish_send struct
1553 * @param tc TaskContext
1556 finish_sending (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1558 struct Finish_send *finish = cls;
1559 struct Plugin *plugin;
1562 plugin = finish->plugin;
1563 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1565 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1567 GNUNET_free (finish->msgstart);
1568 GNUNET_free (finish);
1572 GNUNET_DISK_file_write (plugin->server_stdin_handle,
1573 finish->head_of_next_write, finish->size);
1575 if (bytes != finish->size)
1577 if (bytes != GNUNET_SYSERR)
1579 finish->head_of_next_write += bytes;
1580 finish->size -= bytes;
1581 plugin->server_write_task =
1582 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1583 plugin->server_stdin_handle,
1584 &finish_sending, finish);
1588 restart_helper (plugin, finish);
1593 GNUNET_free (finish->msgstart);
1594 GNUNET_free (finish);
1595 set_next_send (plugin);
1601 * function to send a hello beacon
1602 * @param plugin pointer to the plugin struct
1605 send_hello_beacon (struct Plugin *plugin)
1609 uint16_t hello_size;
1610 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
1611 const struct GNUNET_MessageHeader *hello;
1612 struct Finish_send *finish;
1614 GNUNET_assert (plugin != NULL);
1615 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1616 "Sending hello beacon\n");
1617 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan hello beacons send"),
1619 hello = plugin->env->get_our_hello ();
1620 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1621 GNUNET_assert (sizeof (struct WlanHeader) + hello_size <= WLAN_MTU);
1623 size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + hello_size;
1624 radioHeader = GNUNET_malloc (size);
1625 getRadiotapHeader (plugin, NULL, radioHeader, size);
1626 getWlanHeader (&radioHeader->frame, &bc_all_mac, plugin, size);
1627 memcpy (&radioHeader[1], hello, hello_size);
1628 bytes = GNUNET_DISK_file_write (plugin->server_stdin_handle, radioHeader, size);
1629 GNUNET_free (radioHeader);
1630 if (bytes == GNUNET_SYSERR)
1632 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1634 ("Error writing to wlan helper. errno == %d, ERROR: %s\n"),
1635 errno, strerror (errno));
1636 finish = GNUNET_malloc (sizeof (struct Finish_send));
1637 finish->plugin = plugin;
1638 finish->head_of_next_write = NULL;
1640 finish->msgstart = NULL;
1641 restart_helper (plugin, finish);
1642 set_next_beacon_time (plugin);
1646 GNUNET_assert (bytes == size);
1647 set_next_beacon_time (plugin);
1648 set_next_send (plugin);
1654 * function to add an ack to send it for a received fragment
1656 * @param cls MacEndpoint this ack belongs to
1657 * @param msg_id id of the message
1658 * @param hdr pointer to the hdr where the ack is stored
1661 add_ack_for_send (void *cls, uint32_t msg_id,
1662 const struct GNUNET_MessageHeader *hdr)
1664 struct MacEndpoint *endpoint = cls;
1665 struct Plugin *plugin = endpoint->plugin;
1666 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage* radioHeader;
1667 struct AckSendQueue *ack;
1668 struct GNUNET_MessageHeader *msgheader;
1671 GNUNET_assert (endpoint != NULL);
1672 size = sizeof (struct AckSendQueue) + sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + ntohs (hdr->size);
1673 ack = GNUNET_malloc (size);
1674 ack->message_id = msg_id;
1675 ack->endpoint = endpoint;
1676 radioHeader = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage*) &ack[1];
1677 ack->radioHeader = radioHeader;
1678 getRadiotapHeader (plugin, ack->endpoint, radioHeader, size - sizeof (struct AckSendQueue));
1679 size = ntohs (hdr->size);
1680 getWlanHeader (&radioHeader->frame,
1681 &ack->endpoint->addr,
1684 msgheader = (struct GNUNET_MessageHeader *) &radioHeader[1];
1685 memcpy (msgheader, hdr, size);
1686 GNUNET_CONTAINER_DLL_insert_tail (plugin->ack_send_queue_head,
1687 plugin->ack_send_queue_tail, ack);
1688 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1689 "Adding ack with message id %u to send, AckSendQueue %p, endpoint %p\n",
1690 msg_id, ack, endpoint);
1691 set_next_send (plugin);
1696 * Function for the scheduler if a FragmentMessage times out
1697 * @param cls pointer to the FragmentMessage
1698 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
1701 fragmentmessage_timeout (void *cls,
1702 const struct GNUNET_SCHEDULER_TaskContext *tc)
1704 struct FragmentMessage *fm = cls;
1706 GNUNET_assert (fm != NULL);
1707 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1708 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1710 free_fragment_message (fm->session->mac->plugin, fm);
1715 * Function to check if there is some space in the fragment queue
1716 * inserts a message if space is available
1717 * @param plugin the plugin struct
1720 check_fragment_queue (struct Plugin *plugin)
1722 struct Session *session;
1723 struct FragmentMessage *fm;
1724 struct GNUNET_PeerIdentity pid;
1726 struct PendingMessage *pm;
1728 if (plugin->pending_Fragment_Messages < FRAGMENT_QUEUE_SIZE)
1730 session = get_next_queue_session (plugin);
1731 if (session != NULL)
1733 pm = session->pending_message_head;
1734 GNUNET_assert (pm != NULL);
1735 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
1736 session->pending_message_tail, pm);
1737 session->mac->fragment_messages_out_count++;
1738 session->fragment_messages_out_count++;
1739 plugin->pending_Fragment_Messages++;
1740 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending fragments"),
1741 plugin->pending_Fragment_Messages, GNUNET_NO);
1743 fm = GNUNET_malloc (sizeof (struct FragmentMessage));
1744 fm->session = session;
1745 fm->timeout.abs_value = pm->timeout.abs_value;
1747 GNUNET_FRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
1749 GNUNET_TIME_UNIT_SECONDS,
1751 &add_message_for_send, fm);
1753 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
1754 (fm->timeout), fragmentmessage_timeout,
1756 GNUNET_CONTAINER_DLL_insert_tail (session->mac->sending_messages_head,
1757 session->mac->sending_messages_tail,
1760 if (pm->transmit_cont != NULL)
1762 pid = session->target;
1763 pm->transmit_cont (pm->transmit_cont_cls, &pid, GNUNET_OK);
1764 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1765 "called pm->transmit_cont for %p\n", session);
1769 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1770 "no pm->transmit_cont for %p\n", session);
1774 if (session->pending_message_head != NULL)
1777 queue_session (plugin, session);
1783 //check if timeout changed
1784 set_next_send (plugin);
1789 * Function to send an ack, does not free the ack
1790 * @param plugin pointer to the plugin
1793 send_ack (struct Plugin *plugin)
1796 struct AckSendQueue *ack;
1797 struct Finish_send *finish;
1799 GNUNET_assert (plugin != NULL);
1800 ack = plugin->ack_send_queue_head;
1801 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1802 "Sending ack for message_id %u for mac endpoint %p, size %u\n",
1803 ack->message_id, ack->endpoint,
1804 ntohs (ack->radioHeader->header.size));
1805 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan acks send"), 1,
1808 GNUNET_DISK_file_write (plugin->server_stdin_handle, ack->radioHeader,
1809 ntohs (ack->radioHeader->header.size));
1810 if (bytes == GNUNET_SYSERR)
1812 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1814 ("Error writing to wlan helper. errno == %d, ERROR: %s\n"),
1815 errno, strerror (errno));
1816 finish = GNUNET_malloc (sizeof (struct Finish_send));
1817 finish->plugin = plugin;
1818 finish->head_of_next_write = NULL;
1820 finish->msgstart = NULL;
1821 restart_helper (plugin, finish);
1824 GNUNET_assert (bytes == ntohs (ack->radioHeader->header.size));
1825 GNUNET_CONTAINER_DLL_remove (plugin->ack_send_queue_head,
1826 plugin->ack_send_queue_tail, ack);
1828 set_next_send (plugin);
1833 * Function called when wlan helper is ready to get some data
1835 * @param cls closure
1836 * @param tc GNUNET_SCHEDULER_TaskContext
1839 do_transmit (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1841 struct Plugin *plugin = cls;
1842 struct Session *session;
1843 struct FragmentMessage *fm;
1844 struct Finish_send *finish;
1845 struct FragmentMessage_queue *fmq;
1848 GNUNET_assert (plugin != NULL);
1849 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1850 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1852 if (plugin->ack_send_queue_head != NULL)
1857 //test if a "hello-beacon" has to be send
1858 if (GNUNET_TIME_absolute_get_remaining (plugin->beacon_time).rel_value == 0)
1860 send_hello_beacon (plugin);
1863 if (NULL == plugin->sending_messages_head)
1865 /* nothing to do right now, check when to go again */
1866 set_next_send (plugin);
1869 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan fragments send"), 1,
1872 fmq = plugin->sending_messages_head;
1874 GNUNET_CONTAINER_DLL_remove (plugin->sending_messages_head,
1875 plugin->sending_messages_tail, fmq);
1878 session = fm->session;
1879 GNUNET_assert (session != NULL);
1880 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1881 "Sending GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT for fragment message %p, size: %u\n",
1882 fm, ntohs (fm->radioHeader->header.size));
1884 GNUNET_DISK_file_write (plugin->server_stdin_handle,
1886 ntohs (fm->radioHeader->header.size));
1887 if (bytes != ntohs (fm->radioHeader->header.size))
1889 finish = GNUNET_malloc (sizeof (struct Finish_send));
1890 finish->plugin = plugin;
1891 finish->msgstart = &fm->radioHeader->header;
1892 GNUNET_assert (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK);
1893 if (bytes == GNUNET_SYSERR)
1895 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1897 ("Error writing to wlan helper. errno == %d, ERROR: %s\n"),
1898 errno, strerror (errno));
1899 finish->head_of_next_write = (char*) fm->radioHeader;
1900 finish->size = ntohs (fm->radioHeader->header.size);
1901 restart_helper (plugin, finish);
1905 finish->head_of_next_write = ((char*) fm->radioHeader) + bytes;
1906 finish->size = ntohs (fm->radioHeader->header.size) - bytes;
1907 plugin->server_write_task =
1908 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1909 plugin->server_stdin_handle,
1910 &finish_sending, finish);
1915 GNUNET_free (fm->radioHeader);
1916 fm->radioHeader = NULL;
1917 set_next_send (plugin);
1919 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
1924 * Another peer has suggested an address for this
1925 * peer and transport plugin. Check that this could be a valid
1926 * address. If so, consider adding it to the list
1929 * @param cls closure
1930 * @param addr pointer to the address
1931 * @param addrlen length of addr
1932 * @return GNUNET_OK if this is a plausible address for this peer
1936 wlan_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1938 //struct Plugin *plugin = cls;
1940 /* check if the address is plausible; if so,
1941 * add it to our list! */
1943 GNUNET_assert (cls != NULL);
1944 //FIXME mitm is not checked
1945 //Mac Address has 6 bytes
1948 /* TODO check for bad addresses like multicast, broadcast, etc */
1949 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1950 "wlan_plugin_address_suggested got good address, size %u!\n",
1954 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1955 "wlan_plugin_address_suggested got bad address, size %u!\n",
1957 return GNUNET_SYSERR;
1962 * Creates a new outbound session the transport service will use to send data to the
1965 * @param cls the plugin
1966 * @param address the address
1967 * @return the session or NULL of max connections exceeded
1970 static struct Session *
1971 wlan_plugin_get_session (void *cls,
1972 const struct GNUNET_HELLO_Address *address)
1974 struct Plugin *plugin = cls;
1975 struct Session * s = NULL;
1977 GNUNET_assert (plugin != NULL);
1978 GNUNET_assert (address != NULL);
1980 if (GNUNET_OK == wlan_plugin_address_suggested (plugin,
1982 address->address_length))
1984 s = get_session (plugin, address->address, &address->peer);
1988 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1989 _("Wlan Address len %d is wrong\n"), address->address_length);
1997 * Function that can be used by the transport service to transmit
1998 * a message using the plugin. Note that in the case of a
1999 * peer disconnecting, the continuation MUST be called
2000 * prior to the disconnect notification itself. This function
2001 * will be called with this peer's HELLO message to initiate
2002 * a fresh connection to another peer.
2004 * @param cls closure
2005 * @param session which session must be used
2006 * @param msgbuf the message to transmit
2007 * @param msgbuf_size number of bytes in 'msgbuf'
2008 * @param priority how important is the message (most plugins will
2009 * ignore message priority and just FIFO)
2010 * @param to how long to wait at most for the transmission (does not
2011 * require plugins to discard the message after the timeout,
2012 * just advisory for the desired delay; most plugins will ignore
2014 * @param cont continuation to call once the message has
2015 * been transmitted (or if the transport is ready
2016 * for the next transmission call; or if the
2017 * peer disconnected...); can be NULL
2018 * @param cont_cls closure for cont
2019 * @return number of bytes used (on the physical network, with overheads);
2020 * -1 on hard errors (i.e. address invalid); 0 is a legal value
2021 * and does NOT mean that the message was not transmitted (DV)
2024 wlan_plugin_send (void *cls,
2025 struct Session *session,
2026 const char *msgbuf, size_t msgbuf_size,
2027 unsigned int priority,
2028 struct GNUNET_TIME_Relative to,
2029 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
2031 struct Plugin *plugin = cls;
2032 struct PendingMessage *newmsg;
2033 struct WlanHeader *wlanheader;
2035 GNUNET_assert (plugin != NULL);
2036 GNUNET_assert (session != NULL);
2037 GNUNET_assert (msgbuf_size > 0);
2041 //queue message in session
2042 //test if there is no other message in the "queue"
2043 //FIXME: to many send requests
2044 if (session->pending_message_head != NULL)
2046 newmsg = session->pending_message_head;
2047 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2048 "wlan_plugin_send: a pending message is already in the queue for this client\n remaining time to send this message is %u, queued fragment messages for this mac connection %u\n",
2049 GNUNET_TIME_absolute_get_remaining (newmsg->
2051 session->mac->fragment_messages_out_count);
2054 newmsg = GNUNET_malloc (sizeof (struct PendingMessage));
2055 newmsg->msg = GNUNET_malloc (msgbuf_size + sizeof (struct WlanHeader));
2056 wlanheader = newmsg->msg;
2057 //copy msg to buffer, not fragmented / segmented yet, but with message header
2058 wlanheader->header.size = htons (msgbuf_size + sizeof (struct WlanHeader));
2059 wlanheader->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA);
2060 memcpy (&(wlanheader->target), &session->target, sizeof (struct GNUNET_PeerIdentity));
2061 memcpy (&(wlanheader->source), plugin->env->my_identity,
2062 sizeof (struct GNUNET_PeerIdentity));
2063 wlanheader->crc = 0;
2064 memcpy (&wlanheader[1], msgbuf, msgbuf_size);
2066 htonl (GNUNET_CRYPTO_crc32_n
2067 ((char *) wlanheader, msgbuf_size + sizeof (struct WlanHeader)));
2069 newmsg->transmit_cont = cont;
2070 newmsg->transmit_cont_cls = cont_cls;
2071 newmsg->timeout = GNUNET_TIME_relative_to_absolute (to);
2073 newmsg->timeout.abs_value = newmsg->timeout.abs_value - 500;
2075 newmsg->message_size = msgbuf_size + sizeof (struct WlanHeader);
2077 GNUNET_CONTAINER_DLL_insert_tail (session->pending_message_head,
2078 session->pending_message_tail, newmsg);
2080 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2081 "New message for %p with size (incl wlan header) %u added\n",
2082 session, newmsg->message_size);
2084 queue_session (plugin, session);
2086 check_fragment_queue (plugin);
2087 //FIXME not the correct size
2093 * function to free a mac endpoint
2094 * @param plugin pointer to the plugin struct
2095 * @param endpoint pointer to the MacEndpoint to free
2098 free_macendpoint (struct Plugin *plugin, struct MacEndpoint *endpoint)
2100 struct Sessionqueue *sessions;
2101 struct Sessionqueue *sessions_next;
2103 GNUNET_assert (endpoint != NULL);
2105 sessions = endpoint->sessions_head;
2106 while (sessions != NULL)
2108 sessions_next = sessions->next;
2109 free_session (plugin, sessions, GNUNET_NO);
2110 sessions = sessions_next;
2113 GNUNET_CONTAINER_DLL_remove (plugin->mac_head, plugin->mac_tail, endpoint);
2114 if (endpoint->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2116 GNUNET_SCHEDULER_cancel (endpoint->timeout_task);
2117 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2119 plugin->mac_count--;
2120 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan mac endpoints"),
2121 plugin->mac_count, GNUNET_NO);
2122 GNUNET_free (endpoint);
2127 * function to free a session
2128 * @param plugin pointer to the plugin
2129 * @param queue pointer to the sessionqueue element to free
2130 * @param do_free_macendpoint if GNUNET_YES and mac endpoint would be empty, free mac endpoint
2133 free_session (struct Plugin *plugin, struct Sessionqueue *queue,
2134 int do_free_macendpoint)
2136 struct Sessionqueue *pendingsession;
2137 struct Sessionqueue *pendingsession_tmp;
2138 struct PendingMessage *pm;
2139 struct MacEndpoint *endpoint;
2140 struct FragmentMessage *fm;
2141 struct FragmentMessage *fmnext;
2144 GNUNET_assert (plugin != NULL);
2145 GNUNET_assert (queue != NULL);
2146 GNUNET_assert (queue->content != NULL);
2149 //is this session pending for send
2150 pendingsession = plugin->pending_Sessions_head;
2151 while (pendingsession != NULL)
2153 pendingsession_tmp = pendingsession;
2154 pendingsession = pendingsession->next;
2155 GNUNET_assert (pendingsession_tmp->content != NULL);
2156 if (pendingsession_tmp->content == queue->content)
2158 plugin->pendingsessions--;
2159 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
2160 plugin->pendingsessions, GNUNET_NO);
2161 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
2162 plugin->pending_Sessions_tail,
2163 pendingsession_tmp);
2164 GNUNET_free (pendingsession_tmp);
2166 GNUNET_assert (check == 0);
2171 endpoint = queue->content->mac;
2172 fm = endpoint->sending_messages_head;
2176 if (fm->session == queue->content)
2177 free_fragment_message (plugin, fm);
2181 // remove PendingMessage
2182 pm = queue->content->pending_message_head;
2185 GNUNET_CONTAINER_DLL_remove (queue->content->pending_message_head,
2186 queue->content->pending_message_tail, pm);
2187 GNUNET_free (pm->msg);
2189 pm = queue->content->pending_message_head;
2192 GNUNET_CONTAINER_DLL_remove (endpoint->sessions_head, endpoint->sessions_tail,
2194 //Check that no ohter session on this endpoint for this session exits
2195 GNUNET_assert (search_session (plugin, endpoint, &queue->content->target) ==
2197 if (endpoint->sessions_head == NULL && do_free_macendpoint == GNUNET_YES)
2199 free_macendpoint (plugin, endpoint);
2200 //check if no endpoint with the same address exists
2201 GNUNET_assert (get_macendpoint (plugin, &endpoint->addr, GNUNET_NO) ==
2205 if (queue->content->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2207 GNUNET_SCHEDULER_cancel (queue->content->timeout_task);
2208 queue->content->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2210 GNUNET_free (queue);
2212 check_fragment_queue (plugin);
2216 * Function that can be used to force the plugin to disconnect
2217 * from the given peer and cancel all previous transmissions
2218 * (and their continuation).
2220 * @param cls closure
2221 * @param target peer from which to disconnect
2224 wlan_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
2226 struct Plugin *plugin = cls;
2227 struct Sessionqueue *queue;
2228 struct Sessionqueue *queue_next;
2229 struct MacEndpoint *endpoint = plugin->mac_head;
2230 struct MacEndpoint *endpoint_next;
2232 // just look at all the session for the needed one
2233 while (endpoint != NULL)
2235 queue = endpoint->sessions_head;
2236 endpoint_next = endpoint->next;
2237 while (queue != NULL)
2239 // content is never NULL
2240 GNUNET_assert (queue->content != NULL);
2241 queue_next = queue->next;
2243 (target, &(queue->content->target),
2244 sizeof (struct GNUNET_PeerIdentity)) == 0)
2246 free_session (plugin, queue, GNUNET_YES);
2251 endpoint = endpoint_next;
2256 * Convert the transports address to a nice, human-readable
2259 * @param cls closure
2260 * @param type name of the transport that generated the address
2261 * @param addr one of the addresses of the host, NULL for the last address
2262 * the specific address format depends on the transport
2263 * @param addrlen length of the address
2264 * @param numeric should (IP) addresses be displayed in numeric form?
2265 * @param timeout after how long should we give up?
2266 * @param asc function to call on each string
2267 * @param asc_cls closure for asc
2270 wlan_plugin_address_pretty_printer (void *cls, const char *type,
2271 const void *addr, size_t addrlen,
2273 struct GNUNET_TIME_Relative timeout,
2274 GNUNET_TRANSPORT_AddressStringCallback asc,
2278 const unsigned char *input;
2280 //GNUNET_assert(cls !=NULL);
2281 if (addrlen != sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))
2283 /* invalid address (MAC addresses have 6 bytes) */
2285 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2286 "Func wlan_plugin_address_pretty_printer got size: %u, worng size!\n",
2288 asc (asc_cls, NULL);
2291 input = (const unsigned char *) addr;
2292 GNUNET_asprintf (&ret,
2293 "Transport %s: %s Mac-Address %.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
2294 type, PROTOCOL_PREFIX, input[0], input[1], input[2],
2295 input[3], input[4], input[5]);
2296 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2297 "Func wlan_plugin_address_pretty_printer got size: %u, nummeric %u, type %s; made string: %s\n",
2298 addrlen, numeric, type, ret);
2300 //only one mac address per plugin
2301 asc (asc_cls, NULL);
2307 * handels the data after all fragments are put together
2308 * @param cls macendpoint this messages belongs to
2309 * @param hdr pointer to the data
2312 wlan_data_message_handler (void *cls, const struct GNUNET_MessageHeader *hdr)
2314 struct MacEndpoint *endpoint = (struct MacEndpoint *) cls;
2315 struct Plugin *plugin = endpoint->plugin;
2316 struct WlanHeader *wlanheader;
2317 struct Session *session;
2319 const struct GNUNET_MessageHeader *temp_hdr;
2320 struct GNUNET_PeerIdentity tmpsource;
2323 GNUNET_assert (plugin != NULL);
2325 if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_DATA)
2328 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2329 "Func wlan_data_message_handler got GNUNET_MESSAGE_TYPE_WLAN_DATA size: %u\n",
2332 if (ntohs (hdr->size) <
2333 sizeof (struct WlanHeader) + sizeof (struct GNUNET_MessageHeader))
2335 //packet not big enought
2339 GNUNET_STATISTICS_update (plugin->env->stats,
2340 _("# wlan whole messages received"), 1,
2342 wlanheader = (struct WlanHeader *) hdr;
2344 session = search_session (plugin, endpoint, &wlanheader->source);
2346 temp_hdr = (const struct GNUNET_MessageHeader *) &wlanheader[1];
2347 crc = ntohl (wlanheader->crc);
2348 wlanheader->crc = 0;
2349 if (GNUNET_CRYPTO_crc32_n
2350 ((char *) wlanheader, ntohs (wlanheader->header.size)) != crc)
2352 //wrong crc, dispose message
2353 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2354 "Wlan message header crc was wrong: %u != %u\n",
2355 GNUNET_CRYPTO_crc32_n ((char *) wlanheader,
2356 ntohs (wlanheader->header.size)),
2361 //if not in session list
2362 if (session == NULL)
2364 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2365 "WLAN client not in session list: packet size = %u, inner size = %u, header size = %u\n",
2366 ntohs (wlanheader->header.size), ntohs (temp_hdr->size),
2367 sizeof (struct WlanHeader));
2368 //try if it is a hello message
2369 if (ntohs (wlanheader->header.size) >=
2370 ntohs (temp_hdr->size) + sizeof (struct WlanHeader))
2372 if (ntohs (temp_hdr->type) == GNUNET_MESSAGE_TYPE_HELLO)
2374 if (GNUNET_HELLO_get_id
2375 ((const struct GNUNET_HELLO_Message *) temp_hdr,
2376 &tmpsource) == GNUNET_OK)
2378 session = create_session (plugin, endpoint, &tmpsource);
2382 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2383 "WLAN client not in session list and hello message is not okay\n");
2390 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2391 "WLAN client not in session list and not a hello message\n");
2397 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2398 "WLAN client not in session list and message size in does not fit\npacket size = %u, inner size = %u, header size = %u\n",
2399 ntohs (wlanheader->header.size),
2400 ntohs (temp_hdr->size), sizeof (struct WlanHeader));
2405 //"receive" the message
2408 (&wlanheader->source, &session->target,
2409 sizeof (struct GNUNET_PeerIdentity)) != 0)
2412 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2413 "WLAN peer source id doesn't match packet peer source id: session %p\n",
2419 (&wlanheader->target, plugin->env->my_identity,
2420 sizeof (struct GNUNET_PeerIdentity)) != 0)
2423 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2424 "WLAN peer target id doesn't match our peer id: session %p\n",
2429 GNUNET_SERVER_mst_receive (plugin->data_tokenizer, session,
2430 (const char *) temp_hdr,
2431 ntohs (hdr->size) - sizeof (struct WlanHeader),
2432 GNUNET_YES, GNUNET_NO);
2438 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2439 "wlan_data_message_handler got wrong message type: %u\n",
2446 * function to process the a message, give it to the higher layer
2447 * @param cls pointer to the plugin
2448 * @param client pointer to the session this message belongs to
2449 * @param hdr start of the message
2451 //TODO ATS informations
2453 process_data (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
2456 GNUNET_assert (client != NULL);
2457 GNUNET_assert (cls != NULL);
2458 struct Session *session = (struct Session *) client;
2459 struct Plugin *plugin = (struct Plugin *) cls;
2460 struct GNUNET_ATS_Information ats[2];
2462 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
2463 ats[0].value = htonl (1);
2464 ats[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
2465 ats[1].value = htonl (GNUNET_ATS_NET_WLAN);
2467 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2468 "Calling plugin->env->receive for session %p; %s; size: %u\n",
2469 session, wlan_plugin_address_to_string (NULL,
2473 plugin->env->receive (plugin->env->cls, &(session->target), hdr,
2474 (const struct GNUNET_ATS_Information *) &ats, 2,
2475 session, (const char *) &session->mac->addr,
2476 sizeof (session->mac->addr));
2480 * Function used for to process the data received from the wlan interface
2482 * @param cls the plugin handle
2483 * @param hdr hdr of the GNUNET_MessageHeader
2484 * @param rxinfo pointer to the radiotap informations got with this packet FIXME: give ATS info
2487 wlan_data_helper (void *cls,
2488 const struct GNUNET_MessageHeader *hdr,
2489 const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rxinfo)
2491 struct Plugin *plugin = cls;
2492 struct FragmentMessage *fm;
2493 struct FragmentMessage *fm2;
2494 struct GNUNET_PeerIdentity tmpsource;
2495 struct MacEndpoint *macendpoint;
2496 struct Session *session;
2498 // NOTE: session_light->addr = rxinfo->frame.addr2;
2499 macendpoint = get_macendpoint (plugin, &rxinfo->frame.addr2, GNUNET_YES);
2501 switch (ntohs (hdr->type))
2503 case GNUNET_MESSAGE_TYPE_HELLO:
2505 //TODO better DOS protection, error handling
2506 //TODO test first then create session
2507 if (GNUNET_HELLO_get_id
2508 ((const struct GNUNET_HELLO_Message *) hdr, &tmpsource) == GNUNET_OK)
2510 session = search_session (plugin, macendpoint, &tmpsource);
2511 if (session == NULL)
2513 session = create_session (plugin, macendpoint, &tmpsource);
2515 GNUNET_STATISTICS_update (plugin->env->stats,
2516 _("# wlan hello messages received"), 1,
2518 plugin->env->receive (plugin->env->cls, &session->target,
2519 hdr, NULL, 0, session,
2520 (const char *) &session->mac->addr,
2521 sizeof (session->mac->addr));
2525 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2526 "WLAN client not in session list and HELLO message is not okay\n");
2530 case GNUNET_MESSAGE_TYPE_FRAGMENT:
2531 macendpoint = get_macendpoint (plugin, &rxinfo->frame.addr2, GNUNET_YES);
2532 GNUNET_STATISTICS_update (plugin->env->stats,
2533 _("# wlan fragments received"), 1, GNUNET_NO);
2535 GNUNET_DEFRAGMENT_process_fragment (macendpoint->defrag,
2538 if (ret == GNUNET_NO)
2540 macendpoint->dups++;
2542 else if (ret == GNUNET_OK)
2544 macendpoint->fragc++;
2546 set_next_send (plugin);
2548 case GNUNET_MESSAGE_TYPE_FRAGMENT_ACK:
2549 if (NULL == macendpoint)
2551 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2552 "Macendpoint does not exist for this GNUNET_MESSAGE_TYPE_FRAGMENT_ACK size: %u; %s\n",
2553 ntohs (hdr->size), wlan_plugin_address_to_string (NULL,
2554 &rxinfo->frame.addr2.mac,
2558 fm = macendpoint->sending_messages_head;
2562 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan acks received"),
2564 int ret = GNUNET_FRAGMENT_process_ack (fm->fragcontext, hdr);
2566 if (ret == GNUNET_OK)
2568 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2569 "Got last ack, finished fragment message %p\n", fm);
2570 macendpoint->acks++;
2571 fm->session->last_activity = GNUNET_TIME_absolute_get ();
2572 macendpoint->last_activity = fm->session->last_activity;
2573 free_fragment_message (plugin, fm);
2574 check_fragment_queue (plugin);
2577 if (ret == GNUNET_NO)
2579 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2580 "Got ack for: %p\n", fm);
2581 macendpoint->acks++;
2586 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2587 "WLAN fragment not in fragment list\n");
2591 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2592 "WLAN packet inside the WLAN helper packet has not the right type: %u size: %u\n",
2593 ntohs (hdr->type), ntohs (hdr->size));
2597 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2598 "Helper finished\n");
2602 * Function to print mac addresses nicely.
2604 * @param mac the mac address
2605 * @return string to a static buffer with the human-readable mac, will be overwritten during the next call to this function
2608 macprinter (const struct GNUNET_TRANSPORT_WLAN_MacAddress * mac)
2610 static char macstr[20];
2612 GNUNET_snprintf (macstr, sizeof (macstr), "%2X:%2X:%2X:%2X:%2X:%2X", mac->mac[0], mac->mac[1],
2613 mac->mac[2], mac->mac[3], mac->mac[4], mac->mac[5]);
2618 * Function for the scheduler if a mac endpoint times out
2619 * @param cls pointer to the MacEndpoint
2620 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
2623 macendpoint_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2625 struct MacEndpoint *endpoint = cls;
2627 GNUNET_assert (endpoint != NULL);
2628 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2629 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2633 if (GNUNET_TIME_absolute_get_remaining
2634 (GNUNET_TIME_absolute_add
2635 (endpoint->last_activity, MACENDPOINT_TIMEOUT)).rel_value == 0)
2637 GNUNET_assert (endpoint->plugin != NULL);
2638 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
2639 _("# wlan mac endpoints timeouts"), 1, GNUNET_NO);
2640 free_macendpoint (endpoint->plugin, endpoint);
2644 endpoint->timeout_task =
2645 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
2651 * function to create an macendpoint
2652 * @param plugin pointer to the plugin struct
2653 * @param addr pointer to the macaddress
2654 * @return returns a macendpoint
2656 static struct MacEndpoint *
2657 create_macendpoint (struct Plugin *plugin, const struct GNUNET_TRANSPORT_WLAN_MacAddress *addr)
2659 struct MacEndpoint *newend = GNUNET_malloc (sizeof (struct MacEndpoint));
2661 GNUNET_assert (plugin != NULL);
2662 GNUNET_STATISTICS_update (plugin->env->stats,
2663 _("# wlan mac endpoints created"), 1, GNUNET_NO);
2664 newend->addr = *addr;
2665 newend->plugin = plugin;
2666 newend->addr = *addr;
2667 newend->fragment_messages_out_count = 0;
2669 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
2670 MESSAGES_IN_DEFRAG_QUEUE_PER_MAC,
2671 newend, &wlan_data_message_handler,
2673 newend->last_activity = GNUNET_TIME_absolute_get ();
2674 newend->timeout_task =
2675 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
2678 plugin->mac_count++;
2679 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan mac endpoints"),
2680 plugin->mac_count, GNUNET_NO);
2681 GNUNET_CONTAINER_DLL_insert_tail (plugin->mac_head, plugin->mac_tail, newend);
2682 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2683 "New Mac Endpoint %p: %s\n", newend,
2684 wlan_plugin_address_to_string (NULL, newend->addr.mac, 6));
2689 * Function used for to process the data from the suid process
2691 * @param cls the plugin handle
2692 * @param client client that send the data (not used)
2693 * @param hdr header of the GNUNET_MessageHeader
2696 wlan_process_helper (void *cls, void *client,
2697 const struct GNUNET_MessageHeader *hdr)
2699 struct Plugin *plugin = cls;
2700 const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rxinfo;
2701 const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *cm;
2702 const struct GNUNET_MessageHeader *temp_hdr = NULL;
2707 GNUNET_assert (plugin != NULL);
2708 switch (ntohs (hdr->type))
2710 case GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER:
2711 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2712 "Got data message from helper with %u bytes\n",
2714 GNUNET_STATISTICS_update (plugin->env->stats,
2715 _("# wlan data messages received"), 1,
2717 //call wlan_process_helper with the message inside, later with wlan: analyze signal
2718 if (ntohs (hdr->size) < sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage))
2720 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2721 "Size of packet is too small; size: %u\n",
2724 /* FIXME: restart SUID process */
2728 rxinfo = (const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) hdr;
2729 //process only if it is an broadcast or for this computer both with the gnunet bssid
2732 (&rxinfo->frame.addr3, &mac_bssid_gnunet,
2733 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0)
2735 //check for broadcast or mac
2737 (&rxinfo->frame.addr1, &bc_all_mac,
2738 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0) ||
2740 (&rxinfo->frame.addr1, &plugin->mac_address,
2741 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0))
2743 //if packet is from us return
2745 (&rxinfo->frame.addr2, &plugin->mac_address,
2746 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0))
2751 datasize = ntohs (hdr->size) - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage);
2752 GNUNET_STATISTICS_update (plugin->env->stats,
2753 _("# wlan messages for this client received"),
2755 // FIXME: this is a job for SERVER_mst --
2756 // what we are doing here is not good for alignment...
2758 end = (const char*) &rxinfo[1];
2759 while (pos < datasize - sizeof (struct GNUNET_MessageHeader))
2761 temp_hdr = (struct GNUNET_MessageHeader *) &end[pos];
2762 if (ntohs (temp_hdr->size) <= datasize - pos)
2764 GNUNET_STATISTICS_update (plugin->env->stats,
2766 ("# wlan messages inside WLAN_HELPER_DATA received"),
2768 wlan_data_helper (plugin, temp_hdr, rxinfo);
2772 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2773 "Size of packet is too small; size: %u > size of packet: %u\n",
2774 ntohs (temp_hdr->size), datasize - pos);
2776 pos += ntohs (temp_hdr->size);
2781 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2782 "Func wlan_process_helper got wrong MAC: %s\n",
2783 macprinter (&rxinfo->frame.addr1));
2788 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2789 "Func wlan_process_helper got wrong BSSID: %s\n",
2790 macprinter (&rxinfo->frame.addr2));
2793 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
2794 //TODO more control messages
2795 if (ntohs (hdr->size) != sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage))
2798 /* FIXME: restart SUID process */
2801 cm = (const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *) hdr;
2802 plugin->mac_address = cm->mac;
2803 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2804 "Received WLAN_HELPER_CONTROL message with transport of address %s\n",
2805 wlan_plugin_address_to_string (cls, &plugin->mac_address,
2806 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)));
2807 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2808 &plugin->mac_address,
2809 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress));
2812 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2813 "Func wlan_process_helper got unknown message with number %u, size %u\n",
2814 ntohs (hdr->type), ntohs (hdr->size));
2823 * Exit point from the plugin.
2824 * @param cls pointer to the api struct
2828 libgnunet_plugin_transport_wlan_done (void *cls)
2830 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2831 struct Plugin *plugin = api->cls;
2832 struct MacEndpoint *endpoint;
2833 struct MacEndpoint *endpoint_next;
2840 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2841 "libgnunet_plugin_transport_wlan_done started\n");
2842 wlan_transport_stop_wlan_helper (plugin);
2844 GNUNET_assert (cls != NULL);
2846 endpoint = plugin->mac_head;
2847 while (endpoint != NULL)
2849 endpoint_next = endpoint->next;
2850 free_macendpoint (plugin, endpoint);
2851 endpoint = endpoint_next;
2854 if (plugin->suid_tokenizer != NULL)
2855 GNUNET_SERVER_mst_destroy (plugin->suid_tokenizer);
2857 if (plugin->data_tokenizer != NULL)
2858 GNUNET_SERVER_mst_destroy (plugin->data_tokenizer);
2860 GNUNET_free_non_null (plugin->interface);
2861 GNUNET_free (plugin);
2868 * Entry point for the plugin.
2870 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2871 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2874 libgnunet_plugin_transport_wlan_init (void *cls)
2876 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2877 struct GNUNET_TRANSPORT_PluginFunctions *api;
2878 struct Plugin *plugin;
2880 if (NULL == env->receive)
2882 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2883 initialze the plugin or the API */
2884 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2886 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2887 api->address_to_string = &wlan_plugin_address_to_string;
2888 api->string_to_address = NULL; // FIXME!
2892 plugin = GNUNET_malloc (sizeof (struct Plugin));
2894 plugin->pendingsessions = 0;
2895 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
2896 plugin->pendingsessions, GNUNET_NO);
2897 plugin->mac_count = 0;
2898 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan mac endpoints"),
2899 plugin->mac_count, GNUNET_NO);
2900 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
2901 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2902 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
2903 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
2904 GNUNET_BANDWIDTH_value_init (100 * 1024 *
2907 plugin->suid_tokenizer =
2908 GNUNET_SERVER_mst_create (&wlan_process_helper, plugin);
2910 plugin->data_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
2912 //plugin->sessions = GNUNET_malloc (sizeof (struct Sessionqueue));
2913 //plugin->pending_Sessions_head = GNUNET_malloc (sizeof (struct Sessionqueue));
2915 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2917 api->send = &wlan_plugin_send;
2918 api->get_session = &wlan_plugin_get_session;
2919 api->disconnect = &wlan_plugin_disconnect;
2920 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2921 api->check_address = &wlan_plugin_address_suggested;
2922 api->address_to_string = &wlan_plugin_address_to_string;
2926 if (GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "TESTMODE"))
2928 if (GNUNET_SYSERR ==
2929 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-wlan",
2930 "TESTMODE", &(plugin->testmode)))
2931 plugin->testmode = 0; //default value
2934 if (GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "INTERFACE"))
2936 if (GNUNET_CONFIGURATION_get_value_string
2937 (env->cfg, "transport-wlan", "INTERFACE",
2938 &(plugin->interface)) != GNUNET_YES)
2940 libgnunet_plugin_transport_wlan_done (api);
2946 wlan_transport_start_wlan_helper (plugin);
2947 set_next_beacon_time (plugin);
2948 set_next_send (plugin);
2949 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2950 "wlan init finished\n");
2957 /* end of plugin_transport_wlan.c */