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
520 struct MacEndpoint *next;
525 struct MacEndpoint *prev;
528 * Pointer to the global plugin struct.
530 struct Plugin *plugin;
533 * Struct to hold the session reachable over this mac; head
535 struct Sessionqueue *sessions_head;
538 * Struct to hold the session reachable over this mac; tail
540 struct Sessionqueue *sessions_tail;
543 * Messages currently sending
546 struct FragmentMessage *sending_messages_head;
549 * Messages currently sending
550 * to a peer (tail), if any.
552 struct FragmentMessage *sending_messages_tail;
557 struct GNUNET_TRANSPORT_WLAN_MacAddress addr;
560 * Defrag context for this mac endpoint
562 struct GNUNET_DEFRAGMENT_Context *defrag;
565 * count of messages in the fragment out queue for this mac endpoint
567 int fragment_messages_out_count;
575 * Duplicates received
590 * Last activity on this endpoint. Used to select preferred
593 struct GNUNET_TIME_Absolute last_activity;
598 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
602 * Struct for Messages in the fragment queue
604 struct FragmentMessage
608 * Session this message belongs to
610 struct Session *session;
613 * This is a doubly-linked list.
615 struct FragmentMessage *next;
618 * This is a doubly-linked list.
620 struct FragmentMessage *prev;
623 * Fragmentation context
625 struct GNUNET_FRAGMENT_Context *fragcontext;
628 * Timeout value for the message.
630 struct GNUNET_TIME_Absolute timeout;
635 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
638 * pointer to the radiotap header
640 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
645 do_transmit (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
649 free_session (struct Plugin *plugin, struct Sessionqueue *queue,
650 int do_free_macendpoint);
653 static struct MacEndpoint *
654 create_macendpoint (struct Plugin *plugin, const struct GNUNET_TRANSPORT_WLAN_MacAddress *addr);
658 finish_sending (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
663 * Function to find a MacEndpoint with a specific mac addr
664 * @param plugin pointer to the plugin struct
665 * @param addr pointer to the mac address
666 * @param create_new GNUNET_YES if a new end point should be created
669 static struct MacEndpoint *
670 get_macendpoint (struct Plugin *plugin, const struct GNUNET_TRANSPORT_WLAN_MacAddress *addr,
673 struct MacEndpoint *queue = plugin->mac_head;
675 while (queue != NULL)
677 //GNUNET_assert (queue->sessions_head != NULL);
678 if (memcmp (addr, &queue->addr, sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0)
679 return queue; /* session found */
683 if (create_new == GNUNET_YES)
685 return create_macendpoint (plugin, addr);
695 * search for a session with the macendpoint and peer id
697 * @param plugin pointer to the plugin struct
698 * @param endpoint pointer to the mac endpoint of the peer
699 * @param peer pointer to the peerid
700 * @return returns the session
702 static struct Session *
703 search_session (struct Plugin *plugin, const struct MacEndpoint *endpoint,
704 const struct GNUNET_PeerIdentity *peer)
706 GNUNET_assert (endpoint != NULL);
707 struct Sessionqueue *queue = endpoint->sessions_head;
709 while (queue != NULL)
711 GNUNET_assert (queue->content != NULL);
713 (peer, &queue->content->target,
714 sizeof (struct GNUNET_PeerIdentity)) == 0)
715 return queue->content; /* session found */
722 * Function called for a quick conversion of the binary address to
723 * a numeric address. Note that the caller must not free the
724 * address and that the next call to this function is allowed
725 * to override the address again.
728 * @param addr binary address
729 * @param addrlen length of the address
730 * @return string representing the same address
733 wlan_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
736 const struct GNUNET_TRANSPORT_WLAN_MacAddress *mac;
738 if (addrlen != sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))
744 GNUNET_snprintf (ret, sizeof (ret), "%s Mac-Address %X:%X:%X:%X:%X:%X",
745 PROTOCOL_PREFIX, mac->mac[0], mac->mac[1], mac->mac[2],
746 mac->mac[3], mac->mac[4], mac->mac[5]);
752 * Function for the scheduler if a session times out
753 * @param cls pointer to the Sessionqueue
754 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
757 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
759 struct Sessionqueue *queue = cls;
761 GNUNET_assert (queue != NULL);
762 GNUNET_assert (queue->content != NULL);
763 queue->content->timeout_task = GNUNET_SCHEDULER_NO_TASK;
764 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
768 if (GNUNET_TIME_absolute_get_remaining
769 (GNUNET_TIME_absolute_add
770 (queue->content->last_activity, SESSION_TIMEOUT)).rel_value == 0)
773 GNUNET_assert (queue->content->mac != NULL);
774 GNUNET_assert (queue->content->mac->plugin != NULL);
775 GNUNET_STATISTICS_update (queue->content->mac->plugin->env->stats,
776 _("# wlan session timeouts"), 1, GNUNET_NO);
777 free_session (queue->content->mac->plugin, queue, GNUNET_YES);
781 queue->content->timeout_task =
782 GNUNET_SCHEDULER_add_delayed (SESSION_TIMEOUT, &session_timeout, queue);
787 * create a new session
789 * @param plugin pointer to the plugin struct
790 * @param endpoint pointer to the mac endpoint of the peer
791 * @param peer peer identity to use for this session
792 * @return returns the session
794 static struct Session *
795 create_session (struct Plugin *plugin, struct MacEndpoint *endpoint,
796 const struct GNUNET_PeerIdentity *peer)
798 GNUNET_assert (endpoint != NULL);
799 GNUNET_assert (plugin != NULL);
800 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan session created"), 1,
802 struct Sessionqueue *queue =
803 GNUNET_malloc (sizeof (struct Sessionqueue) + sizeof (struct Session));
805 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
806 endpoint->sessions_tail, queue);
808 queue->content = (struct Session *) &queue[1];
809 queue->content->mac = endpoint;
810 queue->content->target = *peer;
811 queue->content->last_activity = GNUNET_TIME_absolute_get ();
812 queue->content->timeout_task =
813 GNUNET_SCHEDULER_add_delayed (SESSION_TIMEOUT, &session_timeout, queue);
815 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
816 "New session %p with endpoint %p: %s\n", queue->content,
817 endpoint, wlan_plugin_address_to_string (NULL,
820 return queue->content;
824 * Get session from address, create if no session exists
826 * @param plugin pointer to the plugin struct
827 * @param addr pointer to the mac address of the peer
828 * @param peer pointer to the peerid
829 * @return returns the session
831 static struct Session *
832 get_session (struct Plugin *plugin, const struct GNUNET_TRANSPORT_WLAN_MacAddress *addr,
833 const struct GNUNET_PeerIdentity *peer)
835 struct MacEndpoint *mac;
837 mac = get_macendpoint (plugin, addr, GNUNET_YES);
838 struct Session *session = search_session (plugin, mac, peer);
842 return create_session (plugin, mac, peer);
846 * Queue the session to send data
847 * checks if there is a message pending
848 * checks if this session is not allready in the queue
849 * @param plugin pointer to the plugin
850 * @param session pointer to the session to add
853 queue_session (struct Plugin *plugin, struct Session *session)
855 struct Sessionqueue *queue = plugin->pending_Sessions_head;
857 if (session->pending_message_head != NULL)
859 while (queue != NULL)
861 // content is never NULL
862 GNUNET_assert (queue->content != NULL);
863 // is session already in queue?
864 if (session == queue->content)
872 // Session is not in the queue
874 queue = GNUNET_malloc (sizeof (struct Sessionqueue));
875 queue->content = session;
878 GNUNET_CONTAINER_DLL_insert_tail (plugin->pending_Sessions_head,
879 plugin->pending_Sessions_tail, queue);
880 plugin->pendingsessions++;
881 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
882 plugin->pendingsessions, GNUNET_NO);
888 * Function to schedule the write task, executed after a delay
889 * @param cls pointer to the plugin struct
890 * @param tc GNUNET_SCHEDULER_TaskContext pointer
893 delay_fragment_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
895 struct Plugin *plugin = cls;
897 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
899 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
902 // GNUNET_TIME_UNIT_FOREVER_REL is needed to clean up old msg
903 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
905 plugin->server_write_task =
906 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
907 plugin->server_stdin_handle,
908 &do_transmit, plugin);
913 * Function to calculate the time of the next periodic "hello-beacon"
914 * @param plugin pointer to the plugin struct
917 set_next_beacon_time (struct Plugin *const plugin)
919 //under 10 known peers: once a second
920 if (plugin->mac_count < 10)
922 plugin->beacon_time =
923 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
924 GNUNET_TIME_relative_multiply
925 (GNUNET_TIME_UNIT_SECONDS,
926 HELLO_BEACON_SCALING_FACTOR));
928 //under 30 known peers: every 10 seconds
929 else if (plugin->mac_count < 30)
931 plugin->beacon_time =
932 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
933 GNUNET_TIME_relative_multiply
934 (GNUNET_TIME_UNIT_SECONDS,
935 10 * HELLO_BEACON_SCALING_FACTOR));
937 //over 30 known peers: once a minute
940 plugin->beacon_time =
941 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
942 GNUNET_TIME_relative_multiply
943 (GNUNET_TIME_UNIT_MINUTES,
944 HELLO_BEACON_SCALING_FACTOR));
950 * Function to set the timer for the next timeout of the fragment queue
951 * @param plugin the handle to the plugin struct
954 set_next_send (struct Plugin *plugin)
956 struct GNUNET_TIME_Relative next_send;
958 //abort if helper is not running
959 if (plugin->helper_is_running == GNUNET_NO)
965 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
967 GNUNET_SCHEDULER_cancel (plugin->server_write_delay_task);
968 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
971 //check if some acks are in the queue
972 if (plugin->ack_send_queue_head != NULL)
974 next_send = GNUNET_TIME_UNIT_ZERO;
977 //check if there are some fragments in the queue
978 else if (plugin->sending_messages_head != NULL)
980 next_send = GNUNET_TIME_UNIT_ZERO;
984 next_send = GNUNET_TIME_absolute_get_remaining (plugin->beacon_time);
987 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
988 "Next packet is send in: %u\n", next_send.rel_value);
989 if (next_send.rel_value == GNUNET_TIME_UNIT_ZERO.rel_value)
991 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
993 plugin->server_write_task =
994 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
995 plugin->server_stdin_handle,
996 &do_transmit, plugin);
1001 if (plugin->server_write_delay_task == GNUNET_SCHEDULER_NO_TASK)
1003 plugin->server_write_delay_task =
1004 GNUNET_SCHEDULER_add_delayed (next_send, &delay_fragment_task,
1012 * Function to get the next queued Session, removes the session from the queue
1013 * @param plugin pointer to the plugin struct
1014 * @return pointer to the session found, returns NULL if there is now session in the queue
1016 static struct Session *
1017 get_next_queue_session (struct Plugin *plugin)
1019 struct Session *session;
1020 struct Sessionqueue *sessionqueue;
1021 struct Sessionqueue *sessionqueue_old;
1022 struct PendingMessage *pm;
1024 sessionqueue = plugin->pending_Sessions_head;
1026 while (sessionqueue != NULL)
1028 session = sessionqueue->content;
1030 GNUNET_assert (session != NULL);
1031 pm = session->pending_message_head;
1035 sessionqueue_old = sessionqueue;
1036 sessionqueue = sessionqueue->next;
1037 plugin->pendingsessions--;
1038 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
1039 plugin->pendingsessions, GNUNET_NO);
1040 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1041 plugin->pending_Sessions_tail,
1044 GNUNET_free (sessionqueue_old);
1048 //check for message timeout
1049 if (GNUNET_TIME_absolute_get_remaining (pm->timeout).rel_value > 0)
1051 //check if session has no message in the fragment queue
1052 if ((session->mac->fragment_messages_out_count <
1053 FRAGMENT_QUEUE_MESSAGES_OUT_PER_MACENDPOINT) &&
1054 (session->fragment_messages_out_count <
1055 FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION))
1057 plugin->pendingsessions--;
1058 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
1059 plugin->pendingsessions, GNUNET_NO);
1060 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1061 plugin->pending_Sessions_tail,
1063 GNUNET_free (sessionqueue);
1069 sessionqueue = sessionqueue->next;
1074 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
1075 session->pending_message_tail, pm);
1077 //call the cont func that it did not work
1078 if (pm->transmit_cont != NULL)
1079 pm->transmit_cont (pm->transmit_cont_cls, &(session->target),
1081 GNUNET_free (pm->msg);
1084 if (session->pending_message_head == NULL)
1086 sessionqueue_old = sessionqueue;
1087 sessionqueue = sessionqueue->next;
1088 plugin->pendingsessions--;
1089 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
1090 plugin->pendingsessions, GNUNET_NO);
1091 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1092 plugin->pending_Sessions_tail,
1095 GNUNET_free (sessionqueue_old);
1105 * frees the space of a message in the fragment queue (send queue)
1106 * @param plugin the plugin struct
1107 * @param fm message to free
1110 free_fragment_message (struct Plugin *plugin, struct FragmentMessage *fm)
1112 struct Session *session = fm->session;
1113 struct MacEndpoint *endpoint = session->mac;
1114 struct FragmentMessage_queue *fmq;
1115 struct FragmentMessage_queue *fmq_next;
1117 fmq = plugin->sending_messages_head;
1120 fmq_next = fmq->next;
1121 if (fmq->content == fm)
1123 GNUNET_CONTAINER_DLL_remove (plugin->sending_messages_head,
1124 plugin->sending_messages_tail, fmq);
1129 session->mac->fragment_messages_out_count--;
1130 session->fragment_messages_out_count--;
1131 plugin->pending_Fragment_Messages--;
1132 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending fragments"),
1133 plugin->pending_Fragment_Messages, GNUNET_NO);
1134 GNUNET_CONTAINER_DLL_remove (endpoint->sending_messages_head,
1135 endpoint->sending_messages_tail, fm);
1136 GNUNET_FRAGMENT_context_destroy (fm->fragcontext);
1137 if (fm->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1139 GNUNET_SCHEDULER_cancel (fm->timeout_task);
1140 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1143 queue_session (plugin, session);
1144 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1145 "Free pending fragment messages %p, session %p\n", fm,
1151 * function to fill the radiotap header
1153 * @param plugin pointer to the plugin struct
1154 * @param endpoint pointer to the endpoint
1155 * @param header pointer to the radiotap header
1156 * @param size total message size
1157 * @return GNUNET_YES at success
1160 getRadiotapHeader (struct Plugin *plugin, struct MacEndpoint *endpoint,
1161 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *header,
1164 header->header.type = ntohs (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER);
1165 header->header.size = ntohs (size);
1166 if (endpoint != NULL)
1168 header->rate = endpoint->rate;
1169 header->tx_power = endpoint->tx_power;
1170 header->antenna = endpoint->antenna;
1175 header->tx_power = 0;
1176 header->antenna = 0;
1184 * function to generate the wlan hardware header for one packet
1185 * @param Header address to write the header to
1186 * @param to_mac_addr address of the recipient
1187 * @param plugin pointer to the plugin struct
1188 * @param size size of the whole packet, needed to calculate the time to send the packet
1189 * FIXME: 'size' is initialized inconsistently throughout the code (sometimes payload, sometimes everything)
1190 * @return GNUNET_YES if there was no error
1193 getWlanHeader (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *Header,
1194 const struct GNUNET_TRANSPORT_WLAN_MacAddress *to_mac_addr, struct Plugin *plugin,
1197 const int rate = 11000000;
1199 Header->frame_control = htons (IEEE80211_FC0_TYPE_DATA);
1200 Header->addr1 = *to_mac_addr;
1201 Header->addr2 = plugin->mac_address;
1202 Header->addr3 = mac_bssid_gnunet;
1203 Header->duration = GNUNET_htole16 ((size * 1000000) / rate + 290);
1204 Header->sequence_control = 0; // FIXME?
1205 Header->llc[0] = WLAN_LLC_DSAP_FIELD;
1206 Header->llc[1] = WLAN_LLC_SSAP_FIELD;
1207 Header->llc[2] = 0; // FIXME?
1208 Header->llc[3] = 0; // FIXME?
1214 * function to add a fragment of a message to send
1215 * @param cls FragmentMessage this message belongs to
1216 * @param hdr pointer to the start of the message
1219 add_message_for_send (void *cls, const struct GNUNET_MessageHeader *hdr)
1221 struct FragmentMessage *fm = cls;
1222 struct FragmentMessage_queue *fmqueue;
1223 struct MacEndpoint *endpoint;
1224 struct Plugin *plugin;
1227 GNUNET_assert (cls != NULL);
1228 endpoint = fm->session->mac;
1229 plugin = endpoint->plugin;
1231 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1232 "Adding fragment of message %p to send, session %p, endpoint %p, type %u\n",
1233 fm, fm->session, endpoint, hdr->type);
1234 size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + ntohs (hdr->size);
1235 fm->radioHeader = GNUNET_malloc (size);
1236 getRadiotapHeader (plugin, fm->session->mac, fm->radioHeader, size);
1237 getWlanHeader (&fm->radioHeader->frame, &(fm->session->mac->addr), plugin,
1239 memcpy (&fm->radioHeader[1], hdr, ntohs (hdr->size));
1240 // FIXME: yucky allocation structure!
1241 fmqueue = GNUNET_malloc (sizeof (struct FragmentMessage_queue));
1242 fmqueue->content = fm;
1243 GNUNET_CONTAINER_DLL_insert_tail (plugin->sending_messages_head,
1244 plugin->sending_messages_tail, fmqueue);
1245 set_next_send (plugin);
1250 * We have been notified that gnunet-helper-transport-wlan has written something to stdout.
1251 * Handle the output, then reschedule this function to be called again once
1252 * more is available.
1254 * @param cls the plugin handle
1255 * @param tc the scheduling context
1258 wlan_plugin_helper_read (void *cls,
1259 const struct GNUNET_SCHEDULER_TaskContext *tc)
1261 struct Plugin *plugin = cls;
1263 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
1265 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1268 char mybuf[WLAN_MTU + sizeof (struct GNUNET_MessageHeader)];
1272 GNUNET_DISK_file_read (plugin->server_stdout_handle, mybuf,
1276 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1278 ("Finished reading from gnunet-helper-transport-wlan stdout with code: %d\n"),
1282 GNUNET_SERVER_mst_receive (plugin->suid_tokenizer, NULL, mybuf, bytes,
1283 GNUNET_NO, GNUNET_NO);
1285 GNUNET_assert (plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
1286 plugin->server_read_task =
1287 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1288 plugin->server_stdout_handle,
1289 &wlan_plugin_helper_read, plugin);
1293 * Start the gnunet-helper-transport-wlan process.
1295 * @param plugin the transport plugin
1296 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
1299 wlan_transport_start_wlan_helper (struct Plugin *plugin)
1301 const char *filenamehw = "gnunet-helper-transport-wlan";
1302 const char *filenameloopback = "gnunet-helper-transport-wlan-dummy";
1303 char *absolute_filename = NULL;
1305 if (plugin->helper_is_running == GNUNET_YES)
1307 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1308 "wlan_transport_start_wlan_helper not needed, helper already running!");
1312 plugin->server_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
1313 if (plugin->server_stdout == NULL)
1314 return GNUNET_SYSERR;
1316 plugin->server_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
1317 if (plugin->server_stdin == NULL)
1318 return GNUNET_SYSERR;
1320 if ((plugin->testmode == 1) || (plugin->testmode == 2))
1322 if (GNUNET_OS_check_helper_binary (filenameloopback) == GNUNET_YES)
1324 absolute_filename = GNUNET_strdup (filenameloopback);
1328 char cwd[FILENAME_MAX];
1330 GNUNET_assert (getcwd (cwd, sizeof (cwd)) != NULL);
1332 GNUNET_asprintf (&absolute_filename, "%s%s%s", cwd, DIR_SEPARATOR_STR,
1335 if (GNUNET_DISK_file_test (filenameloopback) != GNUNET_YES)
1337 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1338 "Helper `%s' not found! %i\n", absolute_filename);
1344 /* Start the server process */
1346 if (plugin->testmode == 0)
1349 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1350 "Starting gnunet-helper-transport-wlan process cmd: %s %s %i\n",
1351 filenamehw, plugin->interface, plugin->testmode);
1352 if (GNUNET_OS_check_helper_binary (filenamehw) == GNUNET_YES)
1354 plugin->server_proc =
1355 GNUNET_OS_start_process (GNUNET_NO, plugin->server_stdin, plugin->server_stdout,
1356 filenamehw, filenamehw, plugin->interface,
1359 else if (GNUNET_OS_check_helper_binary (filenamehw) == GNUNET_NO)
1361 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1362 "gnunet-helper-transport-wlan is not suid, please change it or look at the doku\n");
1367 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1368 "gnunet-helper-transport-wlan not found, please look if it exists and is the $PATH variable!\n");
1373 else if (plugin->testmode == 1)
1376 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
1377 "Starting gnunet-helper-transport-wlan-dummy loopback 1 process cmd: %s %s %i\n",
1378 absolute_filename, plugin->interface, plugin->testmode);
1379 plugin->server_proc =
1380 GNUNET_OS_start_process (GNUNET_NO, plugin->server_stdin, plugin->server_stdout,
1381 absolute_filename, absolute_filename, "1",
1383 if (plugin->server_proc == NULL)
1385 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1386 "`%s' not found, please look if it exists and is in the $PATH variable!\n",
1391 else if (plugin->testmode == 2)
1393 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
1394 "Starting gnunet-helper-transport-wlan-dummy loopback 2 process cmd: %s %s %i\n",
1395 absolute_filename, plugin->interface, plugin->testmode);
1396 plugin->server_proc =
1397 GNUNET_OS_start_process (GNUNET_NO, plugin->server_stdin, plugin->server_stdout,
1398 absolute_filename, absolute_filename, "2",
1400 if (plugin->server_proc == NULL)
1402 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1403 "`%s' not found, please look if it exists and is in the $PATH variable!\n",
1408 if (absolute_filename != NULL)
1409 GNUNET_free (absolute_filename);
1410 if (plugin->server_proc == NULL)
1412 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1413 "Failed to start gnunet-helper-transport-wlan process\n");
1414 return GNUNET_SYSERR;
1419 /* Close the write end of the read pipe */
1420 GNUNET_DISK_pipe_close_end (plugin->server_stdout,
1421 GNUNET_DISK_PIPE_END_WRITE);
1423 /* Close the read end of the write pipe */
1424 GNUNET_DISK_pipe_close_end (plugin->server_stdin, GNUNET_DISK_PIPE_END_READ);
1426 plugin->server_stdout_handle =
1427 GNUNET_DISK_pipe_handle (plugin->server_stdout,
1428 GNUNET_DISK_PIPE_END_READ);
1429 plugin->server_stdin_handle =
1430 GNUNET_DISK_pipe_handle (plugin->server_stdin,
1431 GNUNET_DISK_PIPE_END_WRITE);
1433 GNUNET_assert (plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
1435 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1436 "Adding server_read_task for the gnunet-helper-transport-wlan\n");
1437 plugin->server_read_task =
1438 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1439 plugin->server_stdout_handle,
1440 &wlan_plugin_helper_read, plugin);
1442 plugin->helper_is_running = GNUNET_YES;
1447 * Stops the gnunet-helper-transport-wlan process.
1449 * @param plugin the transport plugin
1450 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
1453 wlan_transport_stop_wlan_helper (struct Plugin *plugin)
1455 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1456 "Stoping WLAN helper process\n");
1458 if (plugin->helper_is_running == GNUNET_NO)
1460 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1461 "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;
1469 if (plugin->server_write_task != GNUNET_SCHEDULER_NO_TASK)
1471 GNUNET_SCHEDULER_cancel (plugin->server_write_task);
1472 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1474 if (plugin->server_read_task != GNUNET_SCHEDULER_NO_TASK)
1476 GNUNET_SCHEDULER_cancel (plugin->server_read_task);
1477 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
1479 GNUNET_DISK_pipe_close (plugin->server_stdout);
1480 GNUNET_DISK_pipe_close (plugin->server_stdin);
1481 GNUNET_OS_process_kill (plugin->server_proc, SIGKILL);
1482 GNUNET_OS_process_wait (plugin->server_proc);
1483 GNUNET_OS_process_destroy (plugin->server_proc);
1484 plugin->helper_is_running = GNUNET_NO;
1489 * function for delayed restart of the helper process
1490 * @param cls Finish_send struct if message should be finished
1491 * @param tc TaskContext
1494 delay_restart_helper (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1496 struct Finish_send *finish = cls;
1497 struct Plugin *plugin;
1499 plugin = finish->plugin;
1501 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1502 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1504 GNUNET_free_non_null (finish->msgstart);
1505 GNUNET_free (finish);
1509 wlan_transport_start_wlan_helper (plugin);
1511 if (finish->size != 0)
1513 plugin->server_write_task =
1514 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1515 plugin->server_stdin_handle,
1516 &finish_sending, finish);
1520 set_next_send (plugin);
1521 GNUNET_free_non_null (finish->msgstart);
1522 GNUNET_free (finish);
1528 * Function to restart the helper
1529 * @param plugin pointer to the global plugin struct
1530 * @param finish pointer to the Finish_send struct to finish
1533 restart_helper (struct Plugin *plugin, struct Finish_send *finish)
1535 static struct GNUNET_TIME_Relative next_try = { 1000 };
1536 GNUNET_assert (finish != NULL);
1538 wlan_transport_stop_wlan_helper (plugin);
1539 plugin->server_write_task =
1540 GNUNET_SCHEDULER_add_delayed (next_try, &delay_restart_helper, finish);
1541 GNUNET_TIME_relative_multiply (next_try, HELPER_RESTART_SCALING_FACTOR);
1546 * function to finish a sending if not all could have been writen befor
1547 * @param cls pointer to the Finish_send struct
1548 * @param tc TaskContext
1551 finish_sending (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1553 struct Finish_send *finish = cls;
1554 struct Plugin *plugin;
1557 plugin = finish->plugin;
1558 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1560 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1562 GNUNET_free (finish->msgstart);
1563 GNUNET_free (finish);
1567 GNUNET_DISK_file_write (plugin->server_stdin_handle,
1568 finish->head_of_next_write, finish->size);
1570 if (bytes != finish->size)
1572 if (bytes != GNUNET_SYSERR)
1574 finish->head_of_next_write += bytes;
1575 finish->size -= bytes;
1576 plugin->server_write_task =
1577 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1578 plugin->server_stdin_handle,
1579 &finish_sending, finish);
1583 restart_helper (plugin, finish);
1588 GNUNET_free (finish->msgstart);
1589 GNUNET_free (finish);
1590 set_next_send (plugin);
1596 * Function to send a HELLO beacon
1598 * @param plugin pointer to the plugin struct
1601 send_hello_beacon (struct Plugin *plugin)
1605 uint16_t hello_size;
1606 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
1607 const struct GNUNET_MessageHeader *hello;
1608 struct Finish_send *finish;
1610 GNUNET_assert (plugin != NULL);
1611 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1612 "Sending hello beacon\n");
1613 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan hello beacons send"),
1615 hello = plugin->env->get_our_hello ();
1616 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1617 GNUNET_assert (sizeof (struct WlanHeader) + hello_size <= WLAN_MTU);
1619 size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + hello_size;
1620 radioHeader = GNUNET_malloc (size);
1621 getRadiotapHeader (plugin, NULL, radioHeader, size);
1622 getWlanHeader (&radioHeader->frame, &bc_all_mac, plugin, size);
1623 memcpy (&radioHeader[1], hello, hello_size);
1624 bytes = GNUNET_DISK_file_write (plugin->server_stdin_handle, radioHeader, size);
1625 GNUNET_free (radioHeader);
1626 if (bytes == GNUNET_SYSERR)
1628 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1629 _("Error writing to WLAN helper: %s\n"),
1631 finish = GNUNET_malloc (sizeof (struct Finish_send));
1632 finish->plugin = plugin;
1633 finish->head_of_next_write = NULL;
1635 finish->msgstart = NULL;
1636 restart_helper (plugin, finish);
1637 set_next_beacon_time (plugin);
1641 GNUNET_assert (bytes == size);
1642 set_next_beacon_time (plugin);
1643 set_next_send (plugin);
1649 * function to add an ack to send it for a received fragment
1651 * @param cls MacEndpoint this ack belongs to
1652 * @param msg_id id of the message
1653 * @param hdr pointer to the hdr where the ack is stored
1656 add_ack_for_send (void *cls, uint32_t msg_id,
1657 const struct GNUNET_MessageHeader *hdr)
1659 struct MacEndpoint *endpoint = cls;
1660 struct Plugin *plugin = endpoint->plugin;
1661 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage* radioHeader;
1662 struct AckSendQueue *ack;
1663 struct GNUNET_MessageHeader *msgheader;
1666 GNUNET_assert (endpoint != NULL);
1667 size = sizeof (struct AckSendQueue) + sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + ntohs (hdr->size);
1668 ack = GNUNET_malloc (size);
1669 ack->message_id = msg_id;
1670 ack->endpoint = endpoint;
1671 radioHeader = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage*) &ack[1];
1672 ack->radioHeader = radioHeader;
1673 getRadiotapHeader (plugin, ack->endpoint, radioHeader, size - sizeof (struct AckSendQueue));
1674 size = ntohs (hdr->size);
1675 getWlanHeader (&radioHeader->frame,
1676 &ack->endpoint->addr,
1679 msgheader = (struct GNUNET_MessageHeader *) &radioHeader[1];
1680 memcpy (msgheader, hdr, size);
1681 GNUNET_CONTAINER_DLL_insert_tail (plugin->ack_send_queue_head,
1682 plugin->ack_send_queue_tail, ack);
1683 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1684 "Adding ack with message id %u to send, AckSendQueue %p, endpoint %p\n",
1685 msg_id, ack, endpoint);
1686 set_next_send (plugin);
1691 * Function for the scheduler if a FragmentMessage times out
1692 * @param cls pointer to the FragmentMessage
1693 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
1696 fragmentmessage_timeout (void *cls,
1697 const struct GNUNET_SCHEDULER_TaskContext *tc)
1699 struct FragmentMessage *fm = cls;
1701 GNUNET_assert (fm != NULL);
1702 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1703 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1705 free_fragment_message (fm->session->mac->plugin, fm);
1710 * Function to check if there is some space in the fragment queue
1711 * inserts a message if space is available
1712 * @param plugin the plugin struct
1715 check_fragment_queue (struct Plugin *plugin)
1717 struct Session *session;
1718 struct FragmentMessage *fm;
1719 struct GNUNET_PeerIdentity pid;
1721 struct PendingMessage *pm;
1723 if (plugin->pending_Fragment_Messages < FRAGMENT_QUEUE_SIZE)
1725 session = get_next_queue_session (plugin);
1726 if (session != NULL)
1728 pm = session->pending_message_head;
1729 GNUNET_assert (pm != NULL);
1730 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
1731 session->pending_message_tail, pm);
1732 session->mac->fragment_messages_out_count++;
1733 session->fragment_messages_out_count++;
1734 plugin->pending_Fragment_Messages++;
1735 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending fragments"),
1736 plugin->pending_Fragment_Messages, GNUNET_NO);
1738 fm = GNUNET_malloc (sizeof (struct FragmentMessage));
1739 fm->session = session;
1740 fm->timeout.abs_value = pm->timeout.abs_value;
1742 GNUNET_FRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
1744 GNUNET_TIME_UNIT_SECONDS,
1746 &add_message_for_send, fm);
1748 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
1749 (fm->timeout), fragmentmessage_timeout,
1751 GNUNET_CONTAINER_DLL_insert_tail (session->mac->sending_messages_head,
1752 session->mac->sending_messages_tail,
1755 if (pm->transmit_cont != NULL)
1757 pid = session->target;
1758 pm->transmit_cont (pm->transmit_cont_cls, &pid, GNUNET_OK);
1759 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1760 "called pm->transmit_cont for %p\n", session);
1764 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1765 "no pm->transmit_cont for %p\n", session);
1769 if (session->pending_message_head != NULL)
1772 queue_session (plugin, session);
1778 //check if timeout changed
1779 set_next_send (plugin);
1784 * Function to send an ack, does not free the ack
1785 * @param plugin pointer to the plugin
1788 send_ack (struct Plugin *plugin)
1791 struct AckSendQueue *ack;
1792 struct Finish_send *finish;
1794 GNUNET_assert (plugin != NULL);
1795 ack = plugin->ack_send_queue_head;
1796 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1797 "Sending ack for message_id %u for mac endpoint %p, size %u\n",
1798 ack->message_id, ack->endpoint,
1799 ntohs (ack->radioHeader->header.size));
1800 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan acks send"), 1,
1803 GNUNET_DISK_file_write (plugin->server_stdin_handle, ack->radioHeader,
1804 ntohs (ack->radioHeader->header.size));
1805 if (bytes == GNUNET_SYSERR)
1807 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1809 ("Error writing to wlan helper. errno == %d, ERROR: %s\n"),
1810 errno, strerror (errno));
1811 finish = GNUNET_malloc (sizeof (struct Finish_send));
1812 finish->plugin = plugin;
1813 finish->head_of_next_write = NULL;
1815 finish->msgstart = NULL;
1816 restart_helper (plugin, finish);
1819 GNUNET_assert (bytes == ntohs (ack->radioHeader->header.size));
1820 GNUNET_CONTAINER_DLL_remove (plugin->ack_send_queue_head,
1821 plugin->ack_send_queue_tail, ack);
1823 set_next_send (plugin);
1828 * Function called when wlan helper is ready to get some data
1830 * @param cls closure
1831 * @param tc GNUNET_SCHEDULER_TaskContext
1834 do_transmit (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1836 struct Plugin *plugin = cls;
1837 struct Session *session;
1838 struct FragmentMessage *fm;
1839 struct Finish_send *finish;
1840 struct FragmentMessage_queue *fmq;
1843 GNUNET_assert (plugin != NULL);
1844 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1845 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1847 if (plugin->ack_send_queue_head != NULL)
1852 //test if a "hello-beacon" has to be send
1853 if (GNUNET_TIME_absolute_get_remaining (plugin->beacon_time).rel_value == 0)
1855 send_hello_beacon (plugin);
1858 if (NULL == plugin->sending_messages_head)
1860 /* nothing to do right now, check when to go again */
1861 set_next_send (plugin);
1864 GNUNET_STATISTICS_update (plugin->env->stats, _("# WLAN fragments sent"), 1,
1867 fmq = plugin->sending_messages_head;
1869 GNUNET_CONTAINER_DLL_remove (plugin->sending_messages_head,
1870 plugin->sending_messages_tail, fmq);
1873 session = fm->session;
1874 GNUNET_assert (session != NULL);
1875 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1876 "Sending GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT for fragment message %p, size: %u\n",
1877 fm, ntohs (fm->radioHeader->header.size));
1879 GNUNET_DISK_file_write (plugin->server_stdin_handle,
1881 ntohs (fm->radioHeader->header.size));
1882 if (bytes != ntohs (fm->radioHeader->header.size))
1884 finish = GNUNET_malloc (sizeof (struct Finish_send));
1885 finish->plugin = plugin;
1886 finish->msgstart = &fm->radioHeader->header;
1887 GNUNET_assert (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK);
1888 if (bytes == GNUNET_SYSERR)
1890 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1891 _("Error writing to WLAN helper: %s\n"),
1893 finish->head_of_next_write = (char*) fm->radioHeader;
1894 finish->size = ntohs (fm->radioHeader->header.size);
1895 restart_helper (plugin, finish);
1899 finish->head_of_next_write = ((char*) fm->radioHeader) + bytes;
1900 finish->size = ntohs (fm->radioHeader->header.size) - bytes;
1901 plugin->server_write_task =
1902 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1903 plugin->server_stdin_handle,
1904 &finish_sending, finish);
1909 GNUNET_free (fm->radioHeader);
1910 fm->radioHeader = NULL;
1911 set_next_send (plugin);
1913 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
1918 * Another peer has suggested an address for this
1919 * peer and transport plugin. Check that this could be a valid
1920 * address. If so, consider adding it to the list
1923 * @param cls closure
1924 * @param addr pointer to the address
1925 * @param addrlen length of addr
1926 * @return GNUNET_OK if this is a plausible address for this peer
1930 wlan_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1932 //struct Plugin *plugin = cls;
1934 /* check if the address is plausible; if so,
1935 * add it to our list! */
1937 GNUNET_assert (cls != NULL);
1938 //FIXME mitm is not checked
1939 //Mac Address has 6 bytes
1942 /* TODO check for bad addresses like multicast, broadcast, etc */
1943 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1944 "wlan_plugin_address_suggested got good address, size %u!\n",
1948 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1949 "wlan_plugin_address_suggested got bad address, size %u!\n",
1951 return GNUNET_SYSERR;
1956 * Creates a new outbound session the transport service will use to send data to the
1959 * @param cls the plugin
1960 * @param address the address
1961 * @return the session or NULL of max connections exceeded
1964 static struct Session *
1965 wlan_plugin_get_session (void *cls,
1966 const struct GNUNET_HELLO_Address *address)
1968 struct Plugin *plugin = cls;
1969 struct Session * s = NULL;
1971 GNUNET_assert (plugin != NULL);
1972 GNUNET_assert (address != NULL);
1974 if (GNUNET_OK == wlan_plugin_address_suggested (plugin,
1976 address->address_length))
1978 s = get_session (plugin, address->address, &address->peer);
1982 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1983 _("Wlan Address len %d is wrong\n"), address->address_length);
1991 * Function that can be used by the transport service to transmit
1992 * a message using the plugin. Note that in the case of a
1993 * peer disconnecting, the continuation MUST be called
1994 * prior to the disconnect notification itself. This function
1995 * will be called with this peer's HELLO message to initiate
1996 * a fresh connection to another peer.
1998 * @param cls closure
1999 * @param session which session must be used
2000 * @param msgbuf the message to transmit
2001 * @param msgbuf_size number of bytes in 'msgbuf'
2002 * @param priority how important is the message (most plugins will
2003 * ignore message priority and just FIFO)
2004 * @param to how long to wait at most for the transmission (does not
2005 * require plugins to discard the message after the timeout,
2006 * just advisory for the desired delay; most plugins will ignore
2008 * @param cont continuation to call once the message has
2009 * been transmitted (or if the transport is ready
2010 * for the next transmission call; or if the
2011 * peer disconnected...); can be NULL
2012 * @param cont_cls closure for cont
2013 * @return number of bytes used (on the physical network, with overheads);
2014 * -1 on hard errors (i.e. address invalid); 0 is a legal value
2015 * and does NOT mean that the message was not transmitted (DV)
2018 wlan_plugin_send (void *cls,
2019 struct Session *session,
2020 const char *msgbuf, size_t msgbuf_size,
2021 unsigned int priority,
2022 struct GNUNET_TIME_Relative to,
2023 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
2025 struct Plugin *plugin = cls;
2026 struct PendingMessage *newmsg;
2027 struct WlanHeader *wlanheader;
2029 GNUNET_assert (plugin != NULL);
2030 GNUNET_assert (session != NULL);
2031 GNUNET_assert (msgbuf_size > 0);
2035 //queue message in session
2036 //test if there is no other message in the "queue"
2037 //FIXME: to many send requests
2038 if (session->pending_message_head != NULL)
2040 newmsg = session->pending_message_head;
2041 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2042 "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",
2043 GNUNET_TIME_absolute_get_remaining (newmsg->
2045 session->mac->fragment_messages_out_count);
2048 newmsg = GNUNET_malloc (sizeof (struct PendingMessage));
2049 newmsg->msg = GNUNET_malloc (msgbuf_size + sizeof (struct WlanHeader));
2050 wlanheader = newmsg->msg;
2051 //copy msg to buffer, not fragmented / segmented yet, but with message header
2052 wlanheader->header.size = htons (msgbuf_size + sizeof (struct WlanHeader));
2053 wlanheader->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA);
2054 memcpy (&(wlanheader->target), &session->target, sizeof (struct GNUNET_PeerIdentity));
2055 memcpy (&(wlanheader->source), plugin->env->my_identity,
2056 sizeof (struct GNUNET_PeerIdentity));
2057 wlanheader->crc = 0;
2058 memcpy (&wlanheader[1], msgbuf, msgbuf_size);
2060 htonl (GNUNET_CRYPTO_crc32_n
2061 ((char *) wlanheader, msgbuf_size + sizeof (struct WlanHeader)));
2063 newmsg->transmit_cont = cont;
2064 newmsg->transmit_cont_cls = cont_cls;
2065 newmsg->timeout = GNUNET_TIME_relative_to_absolute (to);
2067 newmsg->timeout.abs_value = newmsg->timeout.abs_value - 500;
2069 newmsg->message_size = msgbuf_size + sizeof (struct WlanHeader);
2071 GNUNET_CONTAINER_DLL_insert_tail (session->pending_message_head,
2072 session->pending_message_tail, newmsg);
2074 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2075 "New message for %p with size (incl wlan header) %u added\n",
2076 session, newmsg->message_size);
2078 queue_session (plugin, session);
2080 check_fragment_queue (plugin);
2081 //FIXME not the correct size
2087 * function to free a mac endpoint
2088 * @param plugin pointer to the plugin struct
2089 * @param endpoint pointer to the MacEndpoint to free
2092 free_macendpoint (struct Plugin *plugin, struct MacEndpoint *endpoint)
2094 struct Sessionqueue *sessions;
2095 struct Sessionqueue *sessions_next;
2097 GNUNET_assert (endpoint != NULL);
2099 sessions = endpoint->sessions_head;
2100 while (sessions != NULL)
2102 sessions_next = sessions->next;
2103 free_session (plugin, sessions, GNUNET_NO);
2104 sessions = sessions_next;
2107 GNUNET_CONTAINER_DLL_remove (plugin->mac_head, plugin->mac_tail, endpoint);
2108 if (endpoint->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2110 GNUNET_SCHEDULER_cancel (endpoint->timeout_task);
2111 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2113 plugin->mac_count--;
2114 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan mac endpoints"),
2115 plugin->mac_count, GNUNET_NO);
2116 GNUNET_free (endpoint);
2121 * function to free a session
2122 * @param plugin pointer to the plugin
2123 * @param queue pointer to the sessionqueue element to free
2124 * @param do_free_macendpoint if GNUNET_YES and mac endpoint would be empty, free mac endpoint
2127 free_session (struct Plugin *plugin, struct Sessionqueue *queue,
2128 int do_free_macendpoint)
2130 struct Sessionqueue *pendingsession;
2131 struct Sessionqueue *pendingsession_tmp;
2132 struct PendingMessage *pm;
2133 struct MacEndpoint *endpoint;
2134 struct FragmentMessage *fm;
2135 struct FragmentMessage *fmnext;
2138 GNUNET_assert (plugin != NULL);
2139 GNUNET_assert (queue != NULL);
2140 GNUNET_assert (queue->content != NULL);
2143 //is this session pending for send
2144 pendingsession = plugin->pending_Sessions_head;
2145 while (pendingsession != NULL)
2147 pendingsession_tmp = pendingsession;
2148 pendingsession = pendingsession->next;
2149 GNUNET_assert (pendingsession_tmp->content != NULL);
2150 if (pendingsession_tmp->content == queue->content)
2152 plugin->pendingsessions--;
2153 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
2154 plugin->pendingsessions, GNUNET_NO);
2155 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
2156 plugin->pending_Sessions_tail,
2157 pendingsession_tmp);
2158 GNUNET_free (pendingsession_tmp);
2160 GNUNET_assert (check == 0);
2165 endpoint = queue->content->mac;
2166 fm = endpoint->sending_messages_head;
2170 if (fm->session == queue->content)
2171 free_fragment_message (plugin, fm);
2175 // remove PendingMessage
2176 pm = queue->content->pending_message_head;
2179 GNUNET_CONTAINER_DLL_remove (queue->content->pending_message_head,
2180 queue->content->pending_message_tail, pm);
2181 GNUNET_free (pm->msg);
2183 pm = queue->content->pending_message_head;
2186 GNUNET_CONTAINER_DLL_remove (endpoint->sessions_head, endpoint->sessions_tail,
2188 //Check that no ohter session on this endpoint for this session exits
2189 GNUNET_assert (search_session (plugin, endpoint, &queue->content->target) ==
2191 if (endpoint->sessions_head == NULL && do_free_macendpoint == GNUNET_YES)
2193 free_macendpoint (plugin, endpoint);
2194 //check if no endpoint with the same address exists
2195 GNUNET_assert (get_macendpoint (plugin, &endpoint->addr, GNUNET_NO) ==
2199 if (queue->content->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2201 GNUNET_SCHEDULER_cancel (queue->content->timeout_task);
2202 queue->content->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2204 GNUNET_free (queue);
2206 check_fragment_queue (plugin);
2210 * Function that can be used to force the plugin to disconnect
2211 * from the given peer and cancel all previous transmissions
2212 * (and their continuation).
2214 * @param cls closure
2215 * @param target peer from which to disconnect
2218 wlan_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
2220 struct Plugin *plugin = cls;
2221 struct Sessionqueue *queue;
2222 struct Sessionqueue *queue_next;
2223 struct MacEndpoint *endpoint = plugin->mac_head;
2224 struct MacEndpoint *endpoint_next;
2226 // just look at all the session for the needed one
2227 while (endpoint != NULL)
2229 queue = endpoint->sessions_head;
2230 endpoint_next = endpoint->next;
2231 while (queue != NULL)
2233 // content is never NULL
2234 GNUNET_assert (queue->content != NULL);
2235 queue_next = queue->next;
2237 (target, &(queue->content->target),
2238 sizeof (struct GNUNET_PeerIdentity)) == 0)
2240 free_session (plugin, queue, GNUNET_YES);
2245 endpoint = endpoint_next;
2250 * Convert the transports address to a nice, human-readable
2253 * @param cls closure
2254 * @param type name of the transport that generated the address
2255 * @param addr one of the addresses of the host, NULL for the last address
2256 * the specific address format depends on the transport
2257 * @param addrlen length of the address
2258 * @param numeric should (IP) addresses be displayed in numeric form?
2259 * @param timeout after how long should we give up?
2260 * @param asc function to call on each string
2261 * @param asc_cls closure for asc
2264 wlan_plugin_address_pretty_printer (void *cls, const char *type,
2265 const void *addr, size_t addrlen,
2267 struct GNUNET_TIME_Relative timeout,
2268 GNUNET_TRANSPORT_AddressStringCallback asc,
2272 const unsigned char *input;
2274 //GNUNET_assert(cls !=NULL);
2275 if (addrlen != sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))
2277 /* invalid address (MAC addresses have 6 bytes) */
2279 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2280 "Func wlan_plugin_address_pretty_printer got size: %u, worng size!\n",
2282 asc (asc_cls, NULL);
2285 input = (const unsigned char *) addr;
2286 GNUNET_asprintf (&ret,
2287 "Transport %s: %s Mac-Address %.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
2288 type, PROTOCOL_PREFIX, input[0], input[1], input[2],
2289 input[3], input[4], input[5]);
2290 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2291 "Func wlan_plugin_address_pretty_printer got size: %u, nummeric %u, type %s; made string: %s\n",
2292 addrlen, numeric, type, ret);
2294 //only one mac address per plugin
2295 asc (asc_cls, NULL);
2301 * handels the data after all fragments are put together
2302 * @param cls macendpoint this messages belongs to
2303 * @param hdr pointer to the data
2306 wlan_data_message_handler (void *cls, const struct GNUNET_MessageHeader *hdr)
2308 struct MacEndpoint *endpoint = (struct MacEndpoint *) cls;
2309 struct Plugin *plugin = endpoint->plugin;
2310 struct WlanHeader *wlanheader;
2311 struct Session *session;
2313 const struct GNUNET_MessageHeader *temp_hdr;
2314 struct GNUNET_PeerIdentity tmpsource;
2317 GNUNET_assert (plugin != NULL);
2319 if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_DATA)
2322 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2323 "Func wlan_data_message_handler got GNUNET_MESSAGE_TYPE_WLAN_DATA size: %u\n",
2326 if (ntohs (hdr->size) <
2327 sizeof (struct WlanHeader) + sizeof (struct GNUNET_MessageHeader))
2329 //packet not big enought
2333 GNUNET_STATISTICS_update (plugin->env->stats,
2334 _("# wlan whole messages received"), 1,
2336 wlanheader = (struct WlanHeader *) hdr;
2338 session = search_session (plugin, endpoint, &wlanheader->source);
2340 temp_hdr = (const struct GNUNET_MessageHeader *) &wlanheader[1];
2341 crc = ntohl (wlanheader->crc);
2342 wlanheader->crc = 0;
2343 if (GNUNET_CRYPTO_crc32_n
2344 ((char *) wlanheader, ntohs (wlanheader->header.size)) != crc)
2346 //wrong crc, dispose message
2347 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2348 "Wlan message header crc was wrong: %u != %u\n",
2349 GNUNET_CRYPTO_crc32_n ((char *) wlanheader,
2350 ntohs (wlanheader->header.size)),
2355 //if not in session list
2356 if (session == NULL)
2358 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2359 "WLAN client not in session list: packet size = %u, inner size = %u, header size = %u\n",
2360 ntohs (wlanheader->header.size), ntohs (temp_hdr->size),
2361 sizeof (struct WlanHeader));
2362 //try if it is a hello message
2363 if (ntohs (wlanheader->header.size) >=
2364 ntohs (temp_hdr->size) + sizeof (struct WlanHeader))
2366 if (ntohs (temp_hdr->type) == GNUNET_MESSAGE_TYPE_HELLO)
2368 if (GNUNET_HELLO_get_id
2369 ((const struct GNUNET_HELLO_Message *) temp_hdr,
2370 &tmpsource) == GNUNET_OK)
2372 session = create_session (plugin, endpoint, &tmpsource);
2376 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2377 "WLAN client not in session list and hello message is not okay\n");
2384 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2385 "WLAN client not in session list and not a hello message\n");
2391 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2392 "WLAN client not in session list and message size in does not fit\npacket size = %u, inner size = %u, header size = %u\n",
2393 ntohs (wlanheader->header.size),
2394 ntohs (temp_hdr->size), sizeof (struct WlanHeader));
2399 //"receive" the message
2402 (&wlanheader->source, &session->target,
2403 sizeof (struct GNUNET_PeerIdentity)) != 0)
2406 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2407 "WLAN peer source id doesn't match packet peer source id: session %p\n",
2413 (&wlanheader->target, plugin->env->my_identity,
2414 sizeof (struct GNUNET_PeerIdentity)) != 0)
2417 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2418 "WLAN peer target id doesn't match our peer id: session %p\n",
2423 GNUNET_SERVER_mst_receive (plugin->data_tokenizer, session,
2424 (const char *) temp_hdr,
2425 ntohs (hdr->size) - sizeof (struct WlanHeader),
2426 GNUNET_YES, GNUNET_NO);
2432 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2433 "wlan_data_message_handler got wrong message type: %u\n",
2440 * function to process the a message, give it to the higher layer
2441 * @param cls pointer to the plugin
2442 * @param client pointer to the session this message belongs to
2443 * @param hdr start of the message
2445 //TODO ATS informations
2447 process_data (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
2450 GNUNET_assert (client != NULL);
2451 GNUNET_assert (cls != NULL);
2452 struct Session *session = (struct Session *) client;
2453 struct Plugin *plugin = (struct Plugin *) cls;
2454 struct GNUNET_ATS_Information ats[2];
2456 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
2457 ats[0].value = htonl (1);
2458 ats[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
2459 ats[1].value = htonl (GNUNET_ATS_NET_WLAN);
2461 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2462 "Calling plugin->env->receive for session %p; %s; size: %u\n",
2463 session, wlan_plugin_address_to_string (NULL,
2467 plugin->env->receive (plugin->env->cls, &(session->target), hdr,
2468 (const struct GNUNET_ATS_Information *) &ats, 2,
2469 session, (const char *) &session->mac->addr,
2470 sizeof (session->mac->addr));
2474 * Function used for to process the data received from the wlan interface
2476 * @param cls the plugin handle
2477 * @param hdr hdr of the GNUNET_MessageHeader
2478 * @param rxinfo pointer to the radiotap informations got with this packet FIXME: give ATS info
2481 wlan_data_helper (void *cls,
2482 const struct GNUNET_MessageHeader *hdr,
2483 const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rxinfo)
2485 struct Plugin *plugin = cls;
2486 struct FragmentMessage *fm;
2487 struct FragmentMessage *fm2;
2488 struct GNUNET_PeerIdentity tmpsource;
2489 struct MacEndpoint *macendpoint;
2490 struct Session *session;
2492 // NOTE: session_light->addr = rxinfo->frame.addr2;
2493 macendpoint = get_macendpoint (plugin, &rxinfo->frame.addr2, GNUNET_YES);
2495 switch (ntohs (hdr->type))
2497 case GNUNET_MESSAGE_TYPE_HELLO:
2499 //TODO better DOS protection, error handling
2500 //TODO test first then create session
2501 if (GNUNET_HELLO_get_id
2502 ((const struct GNUNET_HELLO_Message *) hdr, &tmpsource) == GNUNET_OK)
2504 session = search_session (plugin, macendpoint, &tmpsource);
2505 if (session == NULL)
2507 session = create_session (plugin, macendpoint, &tmpsource);
2509 GNUNET_STATISTICS_update (plugin->env->stats,
2510 _("# wlan hello messages received"), 1,
2512 plugin->env->receive (plugin->env->cls, &session->target,
2513 hdr, NULL, 0, session,
2514 (const char *) &session->mac->addr,
2515 sizeof (session->mac->addr));
2519 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2520 "WLAN client not in session list and HELLO message is not okay\n");
2524 case GNUNET_MESSAGE_TYPE_FRAGMENT:
2525 macendpoint = get_macendpoint (plugin, &rxinfo->frame.addr2, GNUNET_YES);
2526 GNUNET_STATISTICS_update (plugin->env->stats,
2527 _("# wlan fragments received"), 1, GNUNET_NO);
2529 GNUNET_DEFRAGMENT_process_fragment (macendpoint->defrag,
2532 if (ret == GNUNET_NO)
2534 macendpoint->dups++;
2536 else if (ret == GNUNET_OK)
2538 macendpoint->fragc++;
2540 set_next_send (plugin);
2542 case GNUNET_MESSAGE_TYPE_FRAGMENT_ACK:
2543 if (NULL == macendpoint)
2545 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2546 "Macendpoint does not exist for this GNUNET_MESSAGE_TYPE_FRAGMENT_ACK size: %u; %s\n",
2547 ntohs (hdr->size), wlan_plugin_address_to_string (NULL,
2548 &rxinfo->frame.addr2.mac,
2552 fm = macendpoint->sending_messages_head;
2556 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan acks received"),
2558 int ret = GNUNET_FRAGMENT_process_ack (fm->fragcontext, hdr);
2560 if (ret == GNUNET_OK)
2562 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2563 "Got last ack, finished fragment message %p\n", fm);
2564 macendpoint->acks++;
2565 fm->session->last_activity = GNUNET_TIME_absolute_get ();
2566 macendpoint->last_activity = fm->session->last_activity;
2567 free_fragment_message (plugin, fm);
2568 check_fragment_queue (plugin);
2571 if (ret == GNUNET_NO)
2573 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2574 "Got ack for: %p\n", fm);
2575 macendpoint->acks++;
2580 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2581 "WLAN fragment not in fragment list\n");
2585 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2586 "WLAN packet inside the WLAN helper packet has not the right type: %u size: %u\n",
2587 ntohs (hdr->type), ntohs (hdr->size));
2591 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2592 "Helper finished\n");
2596 * Function to print mac addresses nicely.
2598 * @param mac the mac address
2599 * @return string to a static buffer with the human-readable mac, will be overwritten during the next call to this function
2602 macprinter (const struct GNUNET_TRANSPORT_WLAN_MacAddress * mac)
2604 static char macstr[20];
2606 GNUNET_snprintf (macstr, sizeof (macstr), "%2X:%2X:%2X:%2X:%2X:%2X", mac->mac[0], mac->mac[1],
2607 mac->mac[2], mac->mac[3], mac->mac[4], mac->mac[5]);
2612 * Function for the scheduler if a mac endpoint times out
2613 * @param cls pointer to the MacEndpoint
2614 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
2617 macendpoint_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2619 struct MacEndpoint *endpoint = cls;
2621 GNUNET_assert (endpoint != NULL);
2622 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2623 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2627 if (GNUNET_TIME_absolute_get_remaining
2628 (GNUNET_TIME_absolute_add
2629 (endpoint->last_activity, MACENDPOINT_TIMEOUT)).rel_value == 0)
2631 GNUNET_assert (endpoint->plugin != NULL);
2632 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
2633 _("# wlan mac endpoints timeouts"), 1, GNUNET_NO);
2634 free_macendpoint (endpoint->plugin, endpoint);
2638 endpoint->timeout_task =
2639 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
2645 * function to create an macendpoint
2646 * @param plugin pointer to the plugin struct
2647 * @param addr pointer to the macaddress
2648 * @return returns a macendpoint
2650 static struct MacEndpoint *
2651 create_macendpoint (struct Plugin *plugin, const struct GNUNET_TRANSPORT_WLAN_MacAddress *addr)
2653 struct MacEndpoint *newend = GNUNET_malloc (sizeof (struct MacEndpoint));
2655 GNUNET_assert (plugin != NULL);
2656 GNUNET_STATISTICS_update (plugin->env->stats,
2657 _("# wlan mac endpoints created"), 1, GNUNET_NO);
2658 newend->addr = *addr;
2659 newend->plugin = plugin;
2660 newend->addr = *addr;
2661 newend->fragment_messages_out_count = 0;
2663 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
2664 MESSAGES_IN_DEFRAG_QUEUE_PER_MAC,
2665 newend, &wlan_data_message_handler,
2667 newend->last_activity = GNUNET_TIME_absolute_get ();
2668 newend->timeout_task =
2669 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
2672 plugin->mac_count++;
2673 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan mac endpoints"),
2674 plugin->mac_count, GNUNET_NO);
2675 GNUNET_CONTAINER_DLL_insert_tail (plugin->mac_head, plugin->mac_tail, newend);
2676 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2677 "New Mac Endpoint %p: %s\n", newend,
2678 wlan_plugin_address_to_string (NULL, newend->addr.mac, 6));
2683 * Function used for to process the data from the suid process
2685 * @param cls the plugin handle
2686 * @param client client that send the data (not used)
2687 * @param hdr header of the GNUNET_MessageHeader
2690 wlan_process_helper (void *cls, void *client,
2691 const struct GNUNET_MessageHeader *hdr)
2693 struct Plugin *plugin = cls;
2694 const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rxinfo;
2695 const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *cm;
2696 const struct GNUNET_MessageHeader *temp_hdr = NULL;
2701 GNUNET_assert (plugin != NULL);
2702 switch (ntohs (hdr->type))
2704 case GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER:
2705 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2706 "Got data message from helper with %u bytes\n",
2708 GNUNET_STATISTICS_update (plugin->env->stats,
2709 _("# wlan data messages received"), 1,
2711 //call wlan_process_helper with the message inside, later with wlan: analyze signal
2712 if (ntohs (hdr->size) < sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage))
2714 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2715 "Size of packet is too small; size: %u\n",
2718 /* FIXME: restart SUID process */
2722 rxinfo = (const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) hdr;
2723 //process only if it is an broadcast or for this computer both with the gnunet bssid
2726 (&rxinfo->frame.addr3, &mac_bssid_gnunet,
2727 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0)
2729 //check for broadcast or mac
2731 (&rxinfo->frame.addr1, &bc_all_mac,
2732 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0) ||
2734 (&rxinfo->frame.addr1, &plugin->mac_address,
2735 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0))
2737 //if packet is from us return
2739 (&rxinfo->frame.addr2, &plugin->mac_address,
2740 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0))
2745 datasize = ntohs (hdr->size) - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage);
2746 GNUNET_STATISTICS_update (plugin->env->stats,
2747 _("# wlan messages for this client received"),
2749 // FIXME: this is a job for SERVER_mst --
2750 // what we are doing here is not good for alignment...
2752 end = (const char*) &rxinfo[1];
2753 while (pos < datasize - sizeof (struct GNUNET_MessageHeader))
2755 temp_hdr = (struct GNUNET_MessageHeader *) &end[pos];
2756 if (ntohs (temp_hdr->size) <= datasize - pos)
2758 GNUNET_STATISTICS_update (plugin->env->stats,
2760 ("# wlan messages inside WLAN_HELPER_DATA received"),
2762 wlan_data_helper (plugin, temp_hdr, rxinfo);
2766 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2767 "Size of packet is too small; size: %u > size of packet: %u\n",
2768 ntohs (temp_hdr->size), datasize - pos);
2770 pos += ntohs (temp_hdr->size);
2775 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2776 "Func wlan_process_helper got wrong MAC: %s\n",
2777 macprinter (&rxinfo->frame.addr1));
2782 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2783 "Func wlan_process_helper got wrong BSSID: %s\n",
2784 macprinter (&rxinfo->frame.addr2));
2787 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
2788 //TODO more control messages
2789 if (ntohs (hdr->size) != sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage))
2792 /* FIXME: restart SUID process */
2795 cm = (const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *) hdr;
2796 plugin->mac_address = cm->mac;
2797 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2798 "Received WLAN_HELPER_CONTROL message with transport of address %s\n",
2799 wlan_plugin_address_to_string (cls, &plugin->mac_address,
2800 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)));
2801 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2802 &plugin->mac_address,
2803 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress));
2806 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2807 "Func wlan_process_helper got unknown message with number %u, size %u\n",
2808 ntohs (hdr->type), ntohs (hdr->size));
2817 * Exit point from the plugin.
2818 * @param cls pointer to the api struct
2822 libgnunet_plugin_transport_wlan_done (void *cls)
2824 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2825 struct Plugin *plugin = api->cls;
2826 struct MacEndpoint *endpoint;
2827 struct MacEndpoint *endpoint_next;
2834 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2835 "libgnunet_plugin_transport_wlan_done started\n");
2836 wlan_transport_stop_wlan_helper (plugin);
2838 GNUNET_assert (cls != NULL);
2840 endpoint = plugin->mac_head;
2841 while (endpoint != NULL)
2843 endpoint_next = endpoint->next;
2844 free_macendpoint (plugin, endpoint);
2845 endpoint = endpoint_next;
2848 if (plugin->suid_tokenizer != NULL)
2849 GNUNET_SERVER_mst_destroy (plugin->suid_tokenizer);
2851 if (plugin->data_tokenizer != NULL)
2852 GNUNET_SERVER_mst_destroy (plugin->data_tokenizer);
2854 GNUNET_free_non_null (plugin->interface);
2855 GNUNET_free (plugin);
2862 * Entry point for the plugin.
2864 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2865 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2868 libgnunet_plugin_transport_wlan_init (void *cls)
2870 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2871 struct GNUNET_TRANSPORT_PluginFunctions *api;
2872 struct Plugin *plugin;
2874 if (NULL == env->receive)
2876 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2877 initialze the plugin or the API */
2878 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2880 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2881 api->address_to_string = &wlan_plugin_address_to_string;
2882 api->string_to_address = NULL; // FIXME!
2886 plugin = GNUNET_malloc (sizeof (struct Plugin));
2888 plugin->pendingsessions = 0;
2889 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
2890 plugin->pendingsessions, GNUNET_NO);
2891 plugin->mac_count = 0;
2892 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan mac endpoints"),
2893 plugin->mac_count, GNUNET_NO);
2894 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
2895 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2896 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
2897 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
2898 GNUNET_BANDWIDTH_value_init (100 * 1024 *
2901 plugin->suid_tokenizer =
2902 GNUNET_SERVER_mst_create (&wlan_process_helper, plugin);
2904 plugin->data_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
2906 //plugin->sessions = GNUNET_malloc (sizeof (struct Sessionqueue));
2907 //plugin->pending_Sessions_head = GNUNET_malloc (sizeof (struct Sessionqueue));
2909 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2911 api->send = &wlan_plugin_send;
2912 api->get_session = &wlan_plugin_get_session;
2913 api->disconnect = &wlan_plugin_disconnect;
2914 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2915 api->check_address = &wlan_plugin_address_suggested;
2916 api->address_to_string = &wlan_plugin_address_to_string;
2920 if (GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "TESTMODE"))
2922 if (GNUNET_SYSERR ==
2923 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-wlan",
2924 "TESTMODE", &(plugin->testmode)))
2925 plugin->testmode = 0; //default value
2928 if (GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "INTERFACE"))
2930 if (GNUNET_CONFIGURATION_get_value_string
2931 (env->cfg, "transport-wlan", "INTERFACE",
2932 &(plugin->interface)) != GNUNET_YES)
2934 libgnunet_plugin_transport_wlan_done (api);
2940 wlan_transport_start_wlan_helper (plugin);
2941 set_next_beacon_time (plugin);
2942 set_next_send (plugin);
2943 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2944 "wlan init finished\n");
2951 /* end of plugin_transport_wlan.c */