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"
46 #define DEBUG_WLAN GNUNET_EXTRA_LOGGING
49 #define PROTOCOL_PREFIX "wlan"
51 #define PLUGIN_LOG_NAME "wlan-plugin"
59 * time out of a session
61 #define SESSION_TIMEOUT GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT
64 * time out of a mac endpoint
66 #define MACENDPOINT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 2)
69 * scaling factor for hello beacon
71 #define HELLO_BEACON_SCALING_FACTOR 30
74 * scaling factor for restarting the helper
76 #define HELPER_RESTART_SCALING_FACTOR 2
79 * max size of fragment queue
81 #define FRAGMENT_QUEUE_SIZE 10
83 * max messages in fragment queue per session/client
85 #define FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION 1
88 * max messages in fragment queue per MAC
90 #define FRAGMENT_QUEUE_MESSAGES_OUT_PER_MACENDPOINT 1
93 * max messages in in queue
95 #define MESSAGES_IN_QUEUE_SIZE 10
98 * max messages in in queue per session/client
100 #define MESSAGES_IN_DEFRAG_QUEUE_PER_MAC 1
103 * Link layer control fields for better compatibility
104 * (i.e. GNUnet over WLAN is not IP-over-WLAN).
106 #define WLAN_LLC_DSAP_FIELD 0x1f
107 #define WLAN_LLC_SSAP_FIELD 0x1f
112 * Encapsulation of all of the state of the plugin.
119 struct GNUNET_TRANSPORT_PluginEnvironment *env;
122 * List of open connections. head
124 struct MacEndpoint *mac_head;
127 * List of open connections. tail
129 struct MacEndpoint *mac_tail;
132 * Number of connections
134 unsigned int mac_count;
137 * encapsulation of data from the local wlan helper program
139 struct GNUNET_SERVER_MessageStreamTokenizer *suid_tokenizer;
142 * encapsulation of packets received from the wlan helper
144 struct GNUNET_SERVER_MessageStreamTokenizer *data_tokenizer;
147 * stdout pipe handle for the gnunet-helper-transport-wlan process
149 struct GNUNET_DISK_PipeHandle *server_stdout;
152 * stdout file handle for the gnunet-helper-transport-wlan process
154 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
157 * stdin pipe handle for the gnunet-helper-transport-wlan process
159 struct GNUNET_DISK_PipeHandle *server_stdin;
162 * stdin file handle for the gnunet-helper-transport-wlan process
164 const struct GNUNET_DISK_FileHandle *server_stdin_handle;
167 * ID of the gnunet-wlan-server std read task
169 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
172 * ID of the gnunet-wlan-server std read task
174 GNUNET_SCHEDULER_TaskIdentifier server_write_task;
177 * ID of the delay task for writing
179 GNUNET_SCHEDULER_TaskIdentifier server_write_delay_task;
182 * The process id of the wlan process
184 struct GNUNET_OS_Process *server_proc;
187 * The interface of the wlan card given to us by the user.
192 * Mode of operation for the helper, 0 = normal, 1 = first loopback, 2 = second loopback
194 long long unsigned int testmode;
197 * The mac_address of the wlan card given to us by the helper.
199 struct GNUNET_TRANSPORT_WLAN_MacAddress mac_address;
202 * Sessions currently pending for transmission
205 struct Sessionqueue *pending_Sessions_head;
208 * Sessions currently pending for transmission
209 * to a peer (tail), if any.
211 struct Sessionqueue *pending_Sessions_tail;
214 * number of pending sessions
216 unsigned int pendingsessions;
219 * Messages in the sending queues
221 int pending_Fragment_Messages;
224 * messages ready for send, head
226 struct FragmentMessage_queue *sending_messages_head;
229 * messages ready for send, tail
231 struct FragmentMessage_queue *sending_messages_tail;
234 * time of the next "hello-beacon"
236 struct GNUNET_TIME_Absolute beacon_time;
239 * queue to send acks for received fragments (head)
241 struct AckSendQueue *ack_send_queue_head;
244 * queue to send acks for received fragments (tail)
246 struct AckSendQueue *ack_send_queue_tail;
249 * Tracker for bandwidth limit
251 struct GNUNET_BANDWIDTH_Tracker tracker;
254 * saves the current state of the helper process
256 int helper_is_running;
260 * Struct to store data if file write did not accept the whole packet
265 * pointer to the global plugin struct
267 struct Plugin *plugin;
270 * head of the next part to send to the helper
272 const char *head_of_next_write;
275 * Start of the message to send, needed for free
277 struct GNUNET_MessageHeader *msgstart;
286 * Queue of sessions, for the general session queue and the pending session queue
291 struct Sessionqueue *next;
292 struct Sessionqueue *prev;
293 struct Session *content;
294 #if !HAVE_UNALIGNED_64_ACCESS
295 void *dummy; /* for alignment, see #1909 */
301 * Queue of fragmented messages, for the sending queue of the plugin
304 struct FragmentMessage_queue
306 struct FragmentMessage_queue *next;
307 struct FragmentMessage_queue *prev;
308 struct FragmentMessage *content;
313 * Queue for the fragments received
316 struct Receive_Fragment_Queue
318 struct Receive_Fragment_Queue *next;
319 struct Receive_Fragment_Queue *prev;
324 struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rx_msg;
329 struct MacEndpoint_id_fragment_triple
331 struct MacEndpoint *endpoint;
333 struct FragmentMessage *fm;
337 struct Plugin_Session_pair
339 struct Plugin *plugin;
340 struct Session *session;
344 GNUNET_NETWORK_STRUCT_BEGIN
347 * Header for messages which need fragmentation
353 * Message type is GNUNET_MESSAGE_TYPE_WLAN_DATA.
355 struct GNUNET_MessageHeader header;
358 * checksum/error correction
360 uint32_t crc GNUNET_PACKED;
363 * To whom are we talking to (set to our identity
364 * if we are still waiting for the welcome message)
366 struct GNUNET_PeerIdentity target;
369 * Where the packet came from
371 struct GNUNET_PeerIdentity source;
373 // followed by payload
376 GNUNET_NETWORK_STRUCT_END
379 * Information kept for each message that is yet to
382 struct PendingMessage
387 struct PendingMessage *next;
392 struct PendingMessage *prev;
395 * The pending message
397 struct WlanHeader *msg;
400 * Size of the message
405 * Continuation function to call once the message
406 * has been sent. Can be NULL if there is no
407 * continuation to call.
409 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
412 * Cls for transmit_cont
414 void *transmit_cont_cls;
417 * Timeout value for the pending message.
419 struct GNUNET_TIME_Absolute timeout;
425 * Queue for acks to send for fragments recived
431 * next ack in the ack send queue
433 struct AckSendQueue *next;
436 * previous ack in the ack send queue
438 struct AckSendQueue *prev;
441 * pointer to the session this ack belongs to
443 struct MacEndpoint *endpoint;
446 * ID of message, to distinguish between the messages, picked randomly.
451 * pointer to the radiotap header with the ACK Message.
453 const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
458 * Session handle for connections.
466 struct SessionHeader header;
469 * Message currently pending for transmission
470 * to this peer, if any. head
472 struct PendingMessage *pending_message_head;
475 * Message currently pending for transmission
476 * to this peer, if any. tail
478 struct PendingMessage *pending_message_tail;
481 * To whom are we talking to (set to our identity
482 * if we are still waiting for the welcome message)
484 struct GNUNET_PeerIdentity target;
487 * Address of the other peer (either based on our 'connect'
488 * call or on our 'accept' call).
493 * Last activity on this connection. Used to select preferred
494 * connection and timeout
496 struct GNUNET_TIME_Absolute last_activity;
501 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
506 struct MacEndpoint *mac;
509 * count of messages in the fragment out queue for this session
512 int fragment_messages_out_count;
517 * Struct to represent one network card connection
522 * Pointer to the global plugin struct.
524 struct Plugin *plugin;
527 * Struct to hold the session reachable over this mac; head
529 struct Sessionqueue *sessions_head;
532 * Struct to hold the session reachable over this mac; tail
534 struct Sessionqueue *sessions_tail;
537 * Messages currently sending
540 struct FragmentMessage *sending_messages_head;
543 * Messages currently sending
544 * to a peer (tail), if any.
546 struct FragmentMessage *sending_messages_tail;
551 struct MacEndpoint *next;
556 struct MacEndpoint *prev;
561 struct GNUNET_TRANSPORT_WLAN_MacAddress addr;
564 * Defrag context for this mac endpoint
566 struct GNUNET_DEFRAGMENT_Context *defrag;
569 * count of messages in the fragment out queue for this mac endpoint
571 int fragment_messages_out_count;
579 * Duplicates received
594 * Last activity on this endpoint. Used to select preferred
597 struct GNUNET_TIME_Absolute last_activity;
602 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
606 * Struct for Messages in the fragment queue
608 struct FragmentMessage
612 * Session this message belongs to
614 struct Session *session;
617 * This is a doubly-linked list.
619 struct FragmentMessage *next;
622 * This is a doubly-linked list.
624 struct FragmentMessage *prev;
627 * Fragmentation context
629 struct GNUNET_FRAGMENT_Context *fragcontext;
632 * Timeout value for the message.
634 struct GNUNET_TIME_Absolute timeout;
639 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
642 * pointer to the radiotap header
644 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
649 do_transmit (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
653 free_session (struct Plugin *plugin, struct Sessionqueue *queue,
654 int do_free_macendpoint);
657 static struct MacEndpoint *
658 create_macendpoint (struct Plugin *plugin, const struct GNUNET_TRANSPORT_WLAN_MacAddress *addr);
662 finish_sending (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
666 * Generates a nice hexdump of a memory area.
668 * \param mem pointer to memory to dump
669 * \param length how many bytes to dump
672 hexdump (const void *mem, unsigned length)
675 char *src = (char *) mem;
677 printf ("dumping %u bytes from %p\r\n"
678 " 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF\r\n",
683 for (i = 0; i < length; i += 16, src += 16)
687 t += sprintf (t, "%04x: ", i);
688 for (j = 0; j < 16; j++)
691 t += sprintf (t, "%02X", src[j] & 0xff);
693 t += sprintf (t, " ");
695 t += sprintf (t, (j % 2) ? " " : "-");
698 t += sprintf (t, " ");
699 for (j = 0; j < 16; j++)
703 if (isprint ((unsigned char) src[j]))
704 t += sprintf (t, "%c", src[j]);
706 t += sprintf (t, ".");
710 t += sprintf (t, " ");
714 t += sprintf (t, "\r\n");
721 * Function to find a MacEndpoint with a specific mac addr
722 * @param plugin pointer to the plugin struct
723 * @param addr pointer to the mac address
724 * @param create_new GNUNET_YES if a new end point should be created
727 static struct MacEndpoint *
728 get_macendpoint (struct Plugin *plugin, const struct GNUNET_TRANSPORT_WLAN_MacAddress *addr,
731 struct MacEndpoint *queue = plugin->mac_head;
733 while (queue != NULL)
735 //GNUNET_assert (queue->sessions_head != NULL);
736 if (memcmp (addr, &queue->addr, sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0)
737 return queue; /* session found */
741 if (create_new == GNUNET_YES)
743 return create_macendpoint (plugin, addr);
753 * search for a session with the macendpoint and peer id
755 * @param plugin pointer to the plugin struct
756 * @param endpoint pointer to the mac endpoint of the peer
757 * @param peer pointer to the peerid
758 * @return returns the session
760 static struct Session *
761 search_session (struct Plugin *plugin, const struct MacEndpoint *endpoint,
762 const struct GNUNET_PeerIdentity *peer)
764 GNUNET_assert (endpoint != NULL);
765 struct Sessionqueue *queue = endpoint->sessions_head;
767 while (queue != NULL)
769 GNUNET_assert (queue->content != NULL);
771 (peer, &queue->content->target,
772 sizeof (struct GNUNET_PeerIdentity)) == 0)
773 return queue->content; /* session found */
780 * Function called for a quick conversion of the binary address to
781 * a numeric address. Note that the caller must not free the
782 * address and that the next call to this function is allowed
783 * to override the address again.
786 * @param addr binary address
787 * @param addrlen length of the address
788 * @return string representing the same address
791 wlan_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
794 const struct GNUNET_TRANSPORT_WLAN_MacAddress *mac;
796 if (addrlen != sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))
802 GNUNET_snprintf (ret, sizeof (ret), "%s Mac-Address %X:%X:%X:%X:%X:%X",
803 PROTOCOL_PREFIX, mac->mac[0], mac->mac[1], mac->mac[2],
804 mac->mac[3], mac->mac[4], mac->mac[5]);
810 * Function for the scheduler if a session times out
811 * @param cls pointer to the Sessionqueue
812 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
815 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
817 struct Sessionqueue *queue = cls;
819 GNUNET_assert (queue != NULL);
820 GNUNET_assert (queue->content != NULL);
821 queue->content->timeout_task = GNUNET_SCHEDULER_NO_TASK;
822 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
826 if (GNUNET_TIME_absolute_get_remaining
827 (GNUNET_TIME_absolute_add
828 (queue->content->last_activity, SESSION_TIMEOUT)).rel_value == 0)
831 GNUNET_assert (queue->content->mac != NULL);
832 GNUNET_assert (queue->content->mac->plugin != NULL);
833 GNUNET_STATISTICS_update (queue->content->mac->plugin->env->stats,
834 _("# wlan session timeouts"), 1, GNUNET_NO);
835 free_session (queue->content->mac->plugin, queue, GNUNET_YES);
839 queue->content->timeout_task =
840 GNUNET_SCHEDULER_add_delayed (SESSION_TIMEOUT, &session_timeout, queue);
845 * create a new session
847 * @param plugin pointer to the plugin struct
848 * @param endpoint pointer to the mac endpoint of the peer
849 * @param peer peer identity to use for this session
850 * @return returns the session
852 static struct Session *
853 create_session (struct Plugin *plugin, struct MacEndpoint *endpoint,
854 const struct GNUNET_PeerIdentity *peer)
856 GNUNET_assert (endpoint != NULL);
857 GNUNET_assert (plugin != NULL);
858 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan session created"), 1,
860 struct Sessionqueue *queue =
861 GNUNET_malloc (sizeof (struct Sessionqueue) + sizeof (struct Session));
863 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
864 endpoint->sessions_tail, queue);
866 queue->content = (struct Session *) &queue[1];
867 queue->content->mac = endpoint;
868 queue->content->target = *peer;
869 queue->content->last_activity = GNUNET_TIME_absolute_get ();
870 queue->content->timeout_task =
871 GNUNET_SCHEDULER_add_delayed (SESSION_TIMEOUT, &session_timeout, queue);
873 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
874 "New session %p with endpoint %p: %s\n", queue->content,
875 endpoint, wlan_plugin_address_to_string (NULL,
878 return queue->content;
882 * Get session from address, create if no session exists
884 * @param plugin pointer to the plugin struct
885 * @param addr pointer to the mac address of the peer
886 * @param peer pointer to the peerid
887 * @return returns the session
889 static struct Session *
890 get_session (struct Plugin *plugin, const struct GNUNET_TRANSPORT_WLAN_MacAddress *addr,
891 const struct GNUNET_PeerIdentity *peer)
893 struct MacEndpoint *mac;
895 mac = get_macendpoint (plugin, addr, GNUNET_YES);
896 struct Session *session = search_session (plugin, mac, peer);
900 return create_session (plugin, mac, peer);
904 * Queue the session to send data
905 * checks if there is a message pending
906 * checks if this session is not allready in the queue
907 * @param plugin pointer to the plugin
908 * @param session pointer to the session to add
911 queue_session (struct Plugin *plugin, struct Session *session)
913 struct Sessionqueue *queue = plugin->pending_Sessions_head;
915 if (session->pending_message_head != NULL)
917 while (queue != NULL)
919 // content is never NULL
920 GNUNET_assert (queue->content != NULL);
921 // is session already in queue?
922 if (session == queue->content)
930 // Session is not in the queue
932 queue = GNUNET_malloc (sizeof (struct Sessionqueue));
933 queue->content = session;
936 GNUNET_CONTAINER_DLL_insert_tail (plugin->pending_Sessions_head,
937 plugin->pending_Sessions_tail, queue);
938 plugin->pendingsessions++;
939 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
940 plugin->pendingsessions, GNUNET_NO);
946 * Function to schedule the write task, executed after a delay
947 * @param cls pointer to the plugin struct
948 * @param tc GNUNET_SCHEDULER_TaskContext pointer
951 delay_fragment_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
953 struct Plugin *plugin = cls;
955 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
957 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
960 // GNUNET_TIME_UNIT_FOREVER_REL is needed to clean up old msg
961 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
963 plugin->server_write_task =
964 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
965 plugin->server_stdin_handle,
966 &do_transmit, plugin);
971 * Function to calculate the time of the next periodic "hello-beacon"
972 * @param plugin pointer to the plugin struct
975 set_next_beacon_time (struct Plugin *const plugin)
977 //under 10 known peers: once a second
978 if (plugin->mac_count < 10)
980 plugin->beacon_time =
981 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
982 GNUNET_TIME_relative_multiply
983 (GNUNET_TIME_UNIT_SECONDS,
984 HELLO_BEACON_SCALING_FACTOR));
986 //under 30 known peers: every 10 seconds
987 else if (plugin->mac_count < 30)
989 plugin->beacon_time =
990 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
991 GNUNET_TIME_relative_multiply
992 (GNUNET_TIME_UNIT_SECONDS,
993 10 * HELLO_BEACON_SCALING_FACTOR));
995 //over 30 known peers: once a minute
998 plugin->beacon_time =
999 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1000 GNUNET_TIME_relative_multiply
1001 (GNUNET_TIME_UNIT_MINUTES,
1002 HELLO_BEACON_SCALING_FACTOR));
1007 * Function to set the timer for the next timeout of the fragment queue
1008 * @param plugin the handle to the plugin struct
1011 set_next_send (struct Plugin *const plugin)
1013 struct GNUNET_TIME_Relative next_send;
1015 //abort if helper is not running
1016 if (plugin->helper_is_running == GNUNET_NO)
1022 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
1024 GNUNET_SCHEDULER_cancel (plugin->server_write_delay_task);
1025 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
1028 //check if some acks are in the queue
1029 if (plugin->ack_send_queue_head != NULL)
1031 next_send = GNUNET_TIME_UNIT_ZERO;
1034 //check if there are some fragments in the queue
1035 else if (plugin->sending_messages_head != NULL)
1037 next_send = GNUNET_TIME_UNIT_ZERO;
1041 next_send = GNUNET_TIME_absolute_get_remaining (plugin->beacon_time);
1044 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1045 "Next packet is send in: %u\n", next_send.rel_value);
1046 if (next_send.rel_value == GNUNET_TIME_UNIT_ZERO.rel_value)
1048 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
1050 plugin->server_write_task =
1051 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1052 plugin->server_stdin_handle,
1053 &do_transmit, plugin);
1058 if (plugin->server_write_delay_task == GNUNET_SCHEDULER_NO_TASK)
1060 plugin->server_write_delay_task =
1061 GNUNET_SCHEDULER_add_delayed (next_send, &delay_fragment_task,
1068 * Function to get the next queued Session, removes the session from the queue
1069 * @param plugin pointer to the plugin struct
1070 * @return pointer to the session found, returns NULL if there is now session in the queue
1072 static struct Session *
1073 get_next_queue_session (struct Plugin *plugin)
1075 struct Session *session;
1076 struct Sessionqueue *sessionqueue;
1077 struct Sessionqueue *sessionqueue_alt;
1078 struct PendingMessage *pm;
1080 sessionqueue = plugin->pending_Sessions_head;
1082 while (sessionqueue != NULL)
1084 session = sessionqueue->content;
1086 GNUNET_assert (session != NULL);
1087 pm = session->pending_message_head;
1091 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1092 "pending message is empty, should not happen. session %p\n",
1094 sessionqueue_alt = sessionqueue;
1095 sessionqueue = sessionqueue->next;
1096 plugin->pendingsessions--;
1097 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
1098 plugin->pendingsessions, GNUNET_NO);
1099 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1100 plugin->pending_Sessions_tail,
1103 GNUNET_free (sessionqueue_alt);
1108 //check for message timeout
1109 if (GNUNET_TIME_absolute_get_remaining (pm->timeout).rel_value > 0)
1111 //check if session has no message in the fragment queue
1112 if ((session->mac->fragment_messages_out_count <
1113 FRAGMENT_QUEUE_MESSAGES_OUT_PER_MACENDPOINT) &&
1114 (session->fragment_messages_out_count <
1115 FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION))
1117 plugin->pendingsessions--;
1118 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
1119 plugin->pendingsessions, GNUNET_NO);
1120 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1121 plugin->pending_Sessions_tail,
1123 GNUNET_free (sessionqueue);
1129 sessionqueue = sessionqueue->next;
1134 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
1135 session->pending_message_tail, pm);
1137 //call the cont func that it did not work
1138 if (pm->transmit_cont != NULL)
1139 pm->transmit_cont (pm->transmit_cont_cls, &(session->target),
1141 GNUNET_free (pm->msg);
1144 if (session->pending_message_head == NULL)
1146 sessionqueue_alt = sessionqueue;
1147 sessionqueue = sessionqueue->next;
1148 plugin->pendingsessions--;
1149 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
1150 plugin->pendingsessions, GNUNET_NO);
1151 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1152 plugin->pending_Sessions_tail,
1155 GNUNET_free (sessionqueue_alt);
1164 * frees the space of a message in the fragment queue (send queue)
1165 * @param plugin the plugin struct
1166 * @param fm message to free
1169 free_fragment_message (struct Plugin *plugin, struct FragmentMessage *fm)
1171 struct Session *session = fm->session;
1172 struct MacEndpoint *endpoint = session->mac;
1173 struct FragmentMessage_queue *fmq;
1174 struct FragmentMessage_queue *fmq_next;
1176 fmq = plugin->sending_messages_head;
1179 fmq_next = fmq->next;
1180 if (fmq->content == fm)
1182 GNUNET_CONTAINER_DLL_remove (plugin->sending_messages_head,
1183 plugin->sending_messages_tail, fmq);
1189 session->mac->fragment_messages_out_count--;
1190 session->fragment_messages_out_count--;
1191 plugin->pending_Fragment_Messages--;
1192 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending fragments"),
1193 plugin->pending_Fragment_Messages, GNUNET_NO);
1194 GNUNET_CONTAINER_DLL_remove (endpoint->sending_messages_head,
1195 endpoint->sending_messages_tail, fm);
1196 GNUNET_FRAGMENT_context_destroy (fm->fragcontext);
1197 if (fm->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1199 GNUNET_SCHEDULER_cancel (fm->timeout_task);
1200 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1203 queue_session (plugin, session);
1204 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1205 "Free pending fragment messages %p, session %p\n", fm,
1211 * function to fill the radiotap header
1213 * @param plugin pointer to the plugin struct
1214 * @param endpoint pointer to the endpoint
1215 * @param header pointer to the radiotap header
1216 * @param size total message size
1217 * @return GNUNET_YES at success
1220 getRadiotapHeader (struct Plugin *plugin, struct MacEndpoint *endpoint,
1221 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *header,
1224 header->header.type = ntohs (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER);
1225 header->header.size = ntohs (size);
1226 if (endpoint != NULL)
1228 header->rate = endpoint->rate;
1229 header->tx_power = endpoint->tx_power;
1230 header->antenna = endpoint->antenna;
1235 header->tx_power = 0;
1236 header->antenna = 0;
1244 * function to generate the wlan hardware header for one packet
1245 * @param Header address to write the header to
1246 * @param to_mac_addr address of the recipient
1247 * @param plugin pointer to the plugin struct
1248 * @param size size of the whole packet, needed to calculate the time to send the packet
1249 * FIXME: 'size' is initialized inconsistently throughout the code (sometimes payload, sometimes everything)
1250 * @return GNUNET_YES if there was no error
1253 getWlanHeader (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *Header,
1254 const struct GNUNET_TRANSPORT_WLAN_MacAddress *to_mac_addr, struct Plugin *plugin,
1257 const int rate = 11000000;
1259 Header->frame_control = htons (IEEE80211_FC0_TYPE_DATA);
1260 Header->addr1 = *to_mac_addr;
1261 Header->addr2 = plugin->mac_address;
1262 Header->addr3 = mac_bssid_gnunet;
1263 Header->duration = GNUNET_htole16 ((size * 1000000) / rate + 290);
1264 Header->sequence_control = 0; // FIXME?
1265 Header->llc[0] = WLAN_LLC_DSAP_FIELD;
1266 Header->llc[1] = WLAN_LLC_SSAP_FIELD;
1267 Header->llc[2] = 0; // FIXME?
1268 Header->llc[3] = 0; // FIXME?
1274 * function to add a fragment of a message to send
1275 * @param cls FragmentMessage this message belongs to
1276 * @param hdr pointer to the start of the message
1279 add_message_for_send (void *cls, const struct GNUNET_MessageHeader *hdr)
1281 struct FragmentMessage *fm = cls;
1282 struct FragmentMessage_queue *fmqueue;
1283 struct MacEndpoint *endpoint;
1284 struct Plugin *plugin;
1287 GNUNET_assert (cls != NULL);
1288 endpoint = fm->session->mac;
1289 plugin = endpoint->plugin;
1291 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1292 "Adding fragment of message %p to send, session %p, endpoint %p, type %u\n",
1293 fm, fm->session, endpoint, hdr->type);
1294 size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + ntohs (hdr->size);
1295 fm->radioHeader = GNUNET_malloc (size);
1296 getRadiotapHeader (plugin, fm->session->mac, fm->radioHeader, size);
1297 getWlanHeader (&fm->radioHeader->frame, &(fm->session->mac->addr), plugin,
1299 memcpy (&fm->radioHeader[1], hdr, ntohs (hdr->size));
1300 // FIXME: yucky allocation structure!
1301 fmqueue = GNUNET_malloc (sizeof (struct FragmentMessage_queue));
1302 fmqueue->content = fm;
1303 GNUNET_CONTAINER_DLL_insert_tail (plugin->sending_messages_head,
1304 plugin->sending_messages_tail, fmqueue);
1305 set_next_send (plugin);
1310 * We have been notified that gnunet-helper-transport-wlan has written something to stdout.
1311 * Handle the output, then reschedule this function to be called again once
1312 * more is available.
1314 * @param cls the plugin handle
1315 * @param tc the scheduling context
1318 wlan_plugin_helper_read (void *cls,
1319 const struct GNUNET_SCHEDULER_TaskContext *tc)
1321 struct Plugin *plugin = cls;
1323 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
1325 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1328 char mybuf[WLAN_MTU + sizeof (struct GNUNET_MessageHeader)];
1332 GNUNET_DISK_file_read (plugin->server_stdout_handle, mybuf,
1336 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1338 ("Finished reading from gnunet-helper-transport-wlan stdout with code: %d\n"),
1342 GNUNET_SERVER_mst_receive (plugin->suid_tokenizer, NULL, mybuf, bytes,
1343 GNUNET_NO, GNUNET_NO);
1345 GNUNET_assert (plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
1346 plugin->server_read_task =
1347 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1348 plugin->server_stdout_handle,
1349 &wlan_plugin_helper_read, plugin);
1353 * Start the gnunet-helper-transport-wlan process.
1355 * @param plugin the transport plugin
1356 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
1359 wlan_transport_start_wlan_helper (struct Plugin *plugin)
1361 const char *filenamehw = "gnunet-helper-transport-wlan";
1362 const char *filenameloopback = "gnunet-helper-transport-wlan-dummy";
1363 char *absolute_filename = NULL;
1365 if (plugin->helper_is_running == GNUNET_YES)
1367 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1368 "wlan_transport_start_wlan_helper not needed, helper already running!");
1372 plugin->server_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
1373 if (plugin->server_stdout == NULL)
1374 return GNUNET_SYSERR;
1376 plugin->server_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
1377 if (plugin->server_stdin == NULL)
1378 return GNUNET_SYSERR;
1380 if ((plugin->testmode == 1) || (plugin->testmode == 2))
1382 if (GNUNET_OS_check_helper_binary (filenameloopback) == GNUNET_YES)
1384 absolute_filename = GNUNET_strdup (filenameloopback);
1388 char cwd[FILENAME_MAX];
1390 GNUNET_assert (getcwd (cwd, sizeof (cwd)) != NULL);
1392 GNUNET_asprintf (&absolute_filename, "%s%s%s", cwd, DIR_SEPARATOR_STR,
1395 if (GNUNET_DISK_file_test (filenameloopback) != GNUNET_YES)
1397 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1398 "Helper `%s' not found! %i\n", absolute_filename);
1404 /* Start the server process */
1406 if (plugin->testmode == 0)
1409 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1410 "Starting gnunet-helper-transport-wlan process cmd: %s %s %i\n",
1411 filenamehw, plugin->interface, plugin->testmode);
1412 if (GNUNET_OS_check_helper_binary (filenamehw) == GNUNET_YES)
1414 plugin->server_proc =
1415 GNUNET_OS_start_process (GNUNET_NO, plugin->server_stdin, plugin->server_stdout,
1416 filenamehw, filenamehw, plugin->interface,
1419 else if (GNUNET_OS_check_helper_binary (filenamehw) == GNUNET_NO)
1421 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1422 "gnunet-helper-transport-wlan is not suid, please change it or look at the doku\n");
1427 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1428 "gnunet-helper-transport-wlan not found, please look if it exists and is the $PATH variable!\n");
1433 else if (plugin->testmode == 1)
1436 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
1437 "Starting gnunet-helper-transport-wlan-dummy loopback 1 process cmd: %s %s %i\n",
1438 absolute_filename, plugin->interface, plugin->testmode);
1439 plugin->server_proc =
1440 GNUNET_OS_start_process (GNUNET_NO, plugin->server_stdin, plugin->server_stdout,
1441 absolute_filename, absolute_filename, "1",
1443 if (plugin->server_proc == NULL)
1445 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1446 "`%s' not found, please look if it exists and is in the $PATH variable!\n",
1451 else if (plugin->testmode == 2)
1453 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
1454 "Starting gnunet-helper-transport-wlan-dummy loopback 2 process cmd: %s %s %i\n",
1455 absolute_filename, plugin->interface, plugin->testmode);
1456 plugin->server_proc =
1457 GNUNET_OS_start_process (GNUNET_NO, plugin->server_stdin, plugin->server_stdout,
1458 absolute_filename, absolute_filename, "2",
1460 if (plugin->server_proc == NULL)
1462 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1463 "`%s' not found, please look if it exists and is in the $PATH variable!\n",
1468 if (absolute_filename != NULL)
1469 GNUNET_free (absolute_filename);
1470 if (plugin->server_proc == NULL)
1472 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1473 "Failed to start gnunet-helper-transport-wlan process\n");
1474 return GNUNET_SYSERR;
1479 /* Close the write end of the read pipe */
1480 GNUNET_DISK_pipe_close_end (plugin->server_stdout,
1481 GNUNET_DISK_PIPE_END_WRITE);
1483 /* Close the read end of the write pipe */
1484 GNUNET_DISK_pipe_close_end (plugin->server_stdin, GNUNET_DISK_PIPE_END_READ);
1486 plugin->server_stdout_handle =
1487 GNUNET_DISK_pipe_handle (plugin->server_stdout,
1488 GNUNET_DISK_PIPE_END_READ);
1489 plugin->server_stdin_handle =
1490 GNUNET_DISK_pipe_handle (plugin->server_stdin,
1491 GNUNET_DISK_PIPE_END_WRITE);
1493 GNUNET_assert (plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
1495 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1496 "Adding server_read_task for the gnunet-helper-transport-wlan\n");
1497 plugin->server_read_task =
1498 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1499 plugin->server_stdout_handle,
1500 &wlan_plugin_helper_read, plugin);
1502 plugin->helper_is_running = GNUNET_YES;
1507 * Stops the gnunet-helper-transport-wlan process.
1509 * @param plugin the transport plugin
1510 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
1513 wlan_transport_stop_wlan_helper (struct Plugin *plugin)
1515 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1516 "Stoping WLAN helper process\n");
1518 if (plugin->helper_is_running == GNUNET_NO)
1520 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1521 "wlan_transport_stop_wlan_helper not needed, helper already stopped!");
1525 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
1527 GNUNET_SCHEDULER_cancel (plugin->server_write_delay_task);
1528 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
1531 if (plugin->server_write_task != GNUNET_SCHEDULER_NO_TASK)
1533 GNUNET_SCHEDULER_cancel (plugin->server_write_task);
1534 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1537 if (plugin->server_read_task != GNUNET_SCHEDULER_NO_TASK)
1539 GNUNET_SCHEDULER_cancel (plugin->server_read_task);
1540 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
1543 GNUNET_DISK_pipe_close (plugin->server_stdout);
1544 GNUNET_DISK_pipe_close (plugin->server_stdin);
1545 GNUNET_OS_process_kill (plugin->server_proc, SIGKILL);
1546 GNUNET_OS_process_wait (plugin->server_proc);
1547 GNUNET_OS_process_close (plugin->server_proc);
1549 plugin->helper_is_running = GNUNET_NO;
1555 * function for delayed restart of the helper process
1556 * @param cls Finish_send struct if message should be finished
1557 * @param tc TaskContext
1560 delay_restart_helper (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1562 struct Finish_send *finish = cls;
1563 struct Plugin *plugin;
1565 plugin = finish->plugin;
1567 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1568 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1570 GNUNET_free_non_null (finish->msgstart);
1571 GNUNET_free (finish);
1575 wlan_transport_start_wlan_helper (plugin);
1577 if (finish->size != 0)
1579 plugin->server_write_task =
1580 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1581 plugin->server_stdin_handle,
1582 &finish_sending, finish);
1586 set_next_send (plugin);
1587 GNUNET_free_non_null (finish->msgstart);
1588 GNUNET_free (finish);
1594 * Function to restart the helper
1595 * @param plugin pointer to the global plugin struct
1596 * @param finish pointer to the Finish_send struct to finish
1599 restart_helper (struct Plugin *plugin, struct Finish_send *finish)
1601 static struct GNUNET_TIME_Relative next_try = { 1000 };
1602 GNUNET_assert (finish != NULL);
1604 wlan_transport_stop_wlan_helper (plugin);
1605 plugin->server_write_task =
1606 GNUNET_SCHEDULER_add_delayed (next_try, &delay_restart_helper, finish);
1607 GNUNET_TIME_relative_multiply (next_try, HELPER_RESTART_SCALING_FACTOR);
1612 * function to finish a sending if not all could have been writen befor
1613 * @param cls pointer to the Finish_send struct
1614 * @param tc TaskContext
1617 finish_sending (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1619 struct Finish_send *finish = cls;
1620 struct Plugin *plugin;
1623 plugin = finish->plugin;
1624 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1626 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1628 GNUNET_free (finish->msgstart);
1629 GNUNET_free (finish);
1633 GNUNET_DISK_file_write (plugin->server_stdin_handle,
1634 finish->head_of_next_write, finish->size);
1636 if (bytes != finish->size)
1638 if (bytes != GNUNET_SYSERR)
1640 finish->head_of_next_write += bytes;
1641 finish->size -= bytes;
1642 plugin->server_write_task =
1643 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1644 plugin->server_stdin_handle,
1645 &finish_sending, finish);
1649 restart_helper (plugin, finish);
1654 GNUNET_free (finish->msgstart);
1655 GNUNET_free (finish);
1656 set_next_send (plugin);
1662 * function to send a hello beacon
1663 * @param plugin pointer to the plugin struct
1666 send_hello_beacon (struct Plugin *plugin)
1670 uint16_t hello_size;
1671 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
1672 const struct GNUNET_MessageHeader *hello;
1673 struct Finish_send *finish;
1675 GNUNET_assert (plugin != NULL);
1676 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1677 "Sending hello beacon\n");
1678 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan hello beacons send"),
1680 hello = plugin->env->get_our_hello ();
1681 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1682 GNUNET_assert (sizeof (struct WlanHeader) + hello_size <= WLAN_MTU);
1684 size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + hello_size;
1685 radioHeader = GNUNET_malloc (size);
1686 getRadiotapHeader (plugin, NULL, radioHeader, size);
1687 getWlanHeader (&radioHeader->frame, &bc_all_mac, plugin, size);
1688 memcpy (&radioHeader[1], hello, hello_size);
1689 bytes = GNUNET_DISK_file_write (plugin->server_stdin_handle, radioHeader, size);
1690 GNUNET_free (radioHeader);
1691 if (bytes == GNUNET_SYSERR)
1693 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1695 ("Error writing to wlan helper. errno == %d, ERROR: %s\n"),
1696 errno, strerror (errno));
1697 finish = GNUNET_malloc (sizeof (struct Finish_send));
1698 finish->plugin = plugin;
1699 finish->head_of_next_write = NULL;
1701 finish->msgstart = NULL;
1702 restart_helper (plugin, finish);
1703 set_next_beacon_time (plugin);
1707 GNUNET_assert (bytes == size);
1708 set_next_beacon_time (plugin);
1709 set_next_send (plugin);
1715 * function to add an ack to send it for a received fragment
1717 * @param cls MacEndpoint this ack belongs to
1718 * @param msg_id id of the message
1719 * @param hdr pointer to the hdr where the ack is stored
1722 add_ack_for_send (void *cls, uint32_t msg_id,
1723 const struct GNUNET_MessageHeader *hdr)
1725 struct MacEndpoint *endpoint = cls;
1726 struct Plugin *plugin = endpoint->plugin;
1727 struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage* radioHeader;
1728 struct AckSendQueue *ack;
1729 struct GNUNET_MessageHeader *msgheader;
1732 GNUNET_assert (endpoint != NULL);
1733 size = sizeof (struct AckSendQueue) + sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + ntohs (hdr->size);
1734 ack = GNUNET_malloc (size);
1735 ack->message_id = msg_id;
1736 ack->endpoint = endpoint;
1737 radioHeader = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage*) &ack[1];
1738 ack->radioHeader = radioHeader;
1739 getRadiotapHeader (plugin, ack->endpoint, radioHeader, size - sizeof (struct AckSendQueue));
1740 size = ntohs (hdr->size);
1741 getWlanHeader (&radioHeader->frame,
1742 &ack->endpoint->addr,
1745 msgheader = (struct GNUNET_MessageHeader *) &radioHeader[1];
1746 memcpy (msgheader, hdr, size);
1747 GNUNET_CONTAINER_DLL_insert_tail (plugin->ack_send_queue_head,
1748 plugin->ack_send_queue_tail, ack);
1749 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1750 "Adding ack with message id %u to send, AckSendQueue %p, endpoint %p\n",
1751 msg_id, ack, endpoint);
1752 set_next_send (plugin);
1757 * Function for the scheduler if a FragmentMessage times out
1758 * @param cls pointer to the FragmentMessage
1759 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
1762 fragmentmessage_timeout (void *cls,
1763 const struct GNUNET_SCHEDULER_TaskContext *tc)
1765 struct FragmentMessage *fm = cls;
1767 GNUNET_assert (fm != NULL);
1768 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1769 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1773 free_fragment_message (fm->session->mac->plugin, fm);
1777 * Function to check if there is some space in the fragment queue
1778 * inserts a message if space is available
1779 * @param plugin the plugin struct
1783 check_fragment_queue (struct Plugin *plugin)
1785 struct Session *session;
1786 struct FragmentMessage *fm;
1787 struct GNUNET_PeerIdentity pid;
1789 struct PendingMessage *pm;
1791 if (plugin->pending_Fragment_Messages < FRAGMENT_QUEUE_SIZE)
1793 session = get_next_queue_session (plugin);
1794 if (session != NULL)
1796 pm = session->pending_message_head;
1797 GNUNET_assert (pm != NULL);
1798 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
1799 session->pending_message_tail, pm);
1800 session->mac->fragment_messages_out_count++;
1801 session->fragment_messages_out_count++;
1802 plugin->pending_Fragment_Messages++;
1803 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending fragments"),
1804 plugin->pending_Fragment_Messages, GNUNET_NO);
1806 fm = GNUNET_malloc (sizeof (struct FragmentMessage));
1807 fm->session = session;
1808 fm->timeout.abs_value = pm->timeout.abs_value;
1810 GNUNET_FRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
1812 GNUNET_TIME_UNIT_SECONDS,
1814 &add_message_for_send, fm);
1816 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
1817 (fm->timeout), fragmentmessage_timeout,
1819 GNUNET_CONTAINER_DLL_insert_tail (session->mac->sending_messages_head,
1820 session->mac->sending_messages_tail,
1823 if (pm->transmit_cont != NULL)
1825 pid = session->target;
1826 pm->transmit_cont (pm->transmit_cont_cls, &pid, GNUNET_OK);
1827 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1828 "called pm->transmit_cont for %p\n", session);
1832 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1833 "no pm->transmit_cont for %p\n", session);
1837 if (session->pending_message_head != NULL)
1840 queue_session (plugin, session);
1846 //check if timeout changed
1847 set_next_send (plugin);
1852 * Function to send an ack, does not free the ack
1853 * @param plugin pointer to the plugin
1856 send_ack (struct Plugin *plugin)
1859 struct AckSendQueue *ack;
1860 struct Finish_send *finish;
1862 GNUNET_assert (plugin != NULL);
1863 ack = plugin->ack_send_queue_head;
1864 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1865 "Sending ack for message_id %u for mac endpoint %p, size %u\n",
1866 ack->message_id, ack->endpoint,
1867 ntohs (ack->radioHeader->header.size));
1868 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan acks send"), 1,
1871 GNUNET_DISK_file_write (plugin->server_stdin_handle, ack->radioHeader,
1872 ntohs (ack->radioHeader->header.size));
1873 if (bytes == GNUNET_SYSERR)
1875 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1877 ("Error writing to wlan helper. errno == %d, ERROR: %s\n"),
1878 errno, strerror (errno));
1879 finish = GNUNET_malloc (sizeof (struct Finish_send));
1880 finish->plugin = plugin;
1881 finish->head_of_next_write = NULL;
1883 finish->msgstart = NULL;
1884 restart_helper (plugin, finish);
1887 GNUNET_assert (bytes == ntohs (ack->radioHeader->header.size));
1888 GNUNET_CONTAINER_DLL_remove (plugin->ack_send_queue_head,
1889 plugin->ack_send_queue_tail, ack);
1891 set_next_send (plugin);
1896 * Function called when wlan helper is ready to get some data
1898 * @param cls closure
1899 * @param tc GNUNET_SCHEDULER_TaskContext
1902 do_transmit (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1904 struct Plugin *plugin = cls;
1905 struct Session *session;
1906 struct FragmentMessage *fm;
1907 struct Finish_send *finish;
1908 struct FragmentMessage_queue *fmq;
1912 GNUNET_assert (plugin != NULL);
1913 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1914 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1916 if (plugin->ack_send_queue_head != NULL)
1921 //test if a "hello-beacon" has to be send
1922 if (GNUNET_TIME_absolute_get_remaining (plugin->beacon_time).rel_value == 0)
1924 send_hello_beacon (plugin);
1927 if (NULL == plugin->sending_messages_head)
1929 /* do_transmit did nothing, should not happen */
1931 set_next_send (plugin);
1934 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan fragments send"), 1,
1937 fmq = plugin->sending_messages_head;
1939 GNUNET_CONTAINER_DLL_remove (plugin->sending_messages_head,
1940 plugin->sending_messages_tail, fmq);
1943 session = fm->session;
1944 GNUNET_assert (session != NULL);
1945 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1946 "Sending GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT for fragment message %p, size: %u\n",
1947 fm, ntohs (fm->radioHeader->header.size));
1949 GNUNET_DISK_file_write (plugin->server_stdin_handle,
1951 ntohs (fm->radioHeader->header.size));
1952 if (bytes != ntohs (fm->radioHeader->header.size))
1954 finish = GNUNET_malloc (sizeof (struct Finish_send));
1955 finish->plugin = plugin;
1956 finish->msgstart = &fm->radioHeader->header;
1957 GNUNET_assert (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK);
1958 if (bytes == GNUNET_SYSERR)
1960 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1962 ("Error writing to wlan helper. errno == %d, ERROR: %s\n"),
1963 errno, strerror (errno));
1964 finish->head_of_next_write = (char*) fm->radioHeader;
1965 finish->size = ntohs (fm->radioHeader->header.size);
1966 restart_helper (plugin, finish);
1970 finish->head_of_next_write = ((char*) fm->radioHeader) + bytes;
1971 finish->size = ntohs (fm->radioHeader->header.size) - bytes;
1972 plugin->server_write_task =
1973 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1974 plugin->server_stdin_handle,
1975 &finish_sending, finish);
1980 GNUNET_free (fm->radioHeader);
1981 fm->radioHeader = NULL;
1982 set_next_send (plugin);
1984 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
1989 * Another peer has suggested an address for this
1990 * peer and transport plugin. Check that this could be a valid
1991 * address. If so, consider adding it to the list
1994 * @param cls closure
1995 * @param addr pointer to the address
1996 * @param addrlen length of addr
1997 * @return GNUNET_OK if this is a plausible address for this peer
2001 wlan_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
2003 //struct Plugin *plugin = cls;
2005 /* check if the address is plausible; if so,
2006 * add it to our list! */
2008 GNUNET_assert (cls != NULL);
2009 //FIXME mitm is not checked
2010 //Mac Address has 6 bytes
2013 /* TODO check for bad addresses like multicast, broadcast, etc */
2014 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2015 "wlan_plugin_address_suggested got good address, size %u!\n",
2019 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2020 "wlan_plugin_address_suggested got bad address, size %u!\n",
2022 return GNUNET_SYSERR;
2027 * Creates a new outbound session the transport service will use to send data to the
2030 * @param cls the plugin
2031 * @param address the address
2032 * @return the session or NULL of max connections exceeded
2035 static struct Session *
2036 wlan_plugin_get_session (void *cls,
2037 const struct GNUNET_HELLO_Address *address)
2039 struct Plugin *plugin = cls;
2040 struct Session * s = NULL;
2042 GNUNET_assert (plugin != NULL);
2043 GNUNET_assert (address != NULL);
2045 if (GNUNET_OK == wlan_plugin_address_suggested (plugin,
2047 address->address_length))
2049 s = get_session (plugin, address->address, &address->peer);
2053 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
2054 _("Wlan Address len %d is wrong\n"), address->address_length);
2062 * Function that can be used by the transport service to transmit
2063 * a message using the plugin. Note that in the case of a
2064 * peer disconnecting, the continuation MUST be called
2065 * prior to the disconnect notification itself. This function
2066 * will be called with this peer's HELLO message to initiate
2067 * a fresh connection to another peer.
2069 * @param cls closure
2070 * @param session which session must be used
2071 * @param msgbuf the message to transmit
2072 * @param msgbuf_size number of bytes in 'msgbuf'
2073 * @param priority how important is the message (most plugins will
2074 * ignore message priority and just FIFO)
2075 * @param to how long to wait at most for the transmission (does not
2076 * require plugins to discard the message after the timeout,
2077 * just advisory for the desired delay; most plugins will ignore
2079 * @param cont continuation to call once the message has
2080 * been transmitted (or if the transport is ready
2081 * for the next transmission call; or if the
2082 * peer disconnected...); can be NULL
2083 * @param cont_cls closure for cont
2084 * @return number of bytes used (on the physical network, with overheads);
2085 * -1 on hard errors (i.e. address invalid); 0 is a legal value
2086 * and does NOT mean that the message was not transmitted (DV)
2089 wlan_plugin_send (void *cls,
2090 struct Session *session,
2091 const char *msgbuf, size_t msgbuf_size,
2092 unsigned int priority,
2093 struct GNUNET_TIME_Relative to,
2094 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
2096 struct Plugin *plugin = cls;
2097 struct PendingMessage *newmsg;
2098 struct WlanHeader *wlanheader;
2100 GNUNET_assert (plugin != NULL);
2101 GNUNET_assert (session != NULL);
2102 GNUNET_assert (msgbuf_size > 0);
2106 //queue message in session
2107 //test if there is no other message in the "queue"
2108 //FIXME: to many send requests
2109 if (session->pending_message_head != NULL)
2111 newmsg = session->pending_message_head;
2112 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2113 "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",
2114 GNUNET_TIME_absolute_get_remaining (newmsg->
2116 session->mac->fragment_messages_out_count);
2119 newmsg = GNUNET_malloc (sizeof (struct PendingMessage));
2120 newmsg->msg = GNUNET_malloc (msgbuf_size + sizeof (struct WlanHeader));
2121 wlanheader = newmsg->msg;
2122 //copy msg to buffer, not fragmented / segmented yet, but with message header
2123 wlanheader->header.size = htons (msgbuf_size + sizeof (struct WlanHeader));
2124 wlanheader->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA);
2125 memcpy (&(wlanheader->target), &session->target, sizeof (struct GNUNET_PeerIdentity));
2126 memcpy (&(wlanheader->source), plugin->env->my_identity,
2127 sizeof (struct GNUNET_PeerIdentity));
2128 wlanheader->crc = 0;
2129 memcpy (&wlanheader[1], msgbuf, msgbuf_size);
2131 htonl (GNUNET_CRYPTO_crc32_n
2132 ((char *) wlanheader, msgbuf_size + sizeof (struct WlanHeader)));
2134 newmsg->transmit_cont = cont;
2135 newmsg->transmit_cont_cls = cont_cls;
2136 newmsg->timeout = GNUNET_TIME_relative_to_absolute (to);
2138 newmsg->timeout.abs_value = newmsg->timeout.abs_value - 500;
2140 newmsg->message_size = msgbuf_size + sizeof (struct WlanHeader);
2142 GNUNET_CONTAINER_DLL_insert_tail (session->pending_message_head,
2143 session->pending_message_tail, newmsg);
2145 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2146 "New message for %p with size (incl wlan header) %u added\n",
2147 session, newmsg->message_size);
2149 hexdump (msgbuf, GNUNET_MIN (msgbuf_size, 256));
2152 queue_session (plugin, session);
2154 check_fragment_queue (plugin);
2155 //FIXME not the correct size
2161 * function to free a mac endpoint
2162 * @param plugin pointer to the plugin struct
2163 * @param endpoint pointer to the MacEndpoint to free
2166 free_macendpoint (struct Plugin *plugin, struct MacEndpoint *endpoint)
2168 struct Sessionqueue *sessions;
2169 struct Sessionqueue *sessions_next;
2171 GNUNET_assert (endpoint != NULL);
2173 sessions = endpoint->sessions_head;
2174 while (sessions != NULL)
2176 sessions_next = sessions->next;
2177 free_session (plugin, sessions, GNUNET_NO);
2178 sessions = sessions_next;
2181 GNUNET_CONTAINER_DLL_remove (plugin->mac_head, plugin->mac_tail, endpoint);
2182 if (endpoint->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2184 GNUNET_SCHEDULER_cancel (endpoint->timeout_task);
2185 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2187 plugin->mac_count--;
2188 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan mac endpoints"),
2189 plugin->mac_count, GNUNET_NO);
2190 GNUNET_free (endpoint);
2195 * function to free a session
2196 * @param plugin pointer to the plugin
2197 * @param queue pointer to the sessionqueue element to free
2198 * @param do_free_macendpoint if GNUNET_YES and mac endpoint would be empty, free mac endpoint
2201 free_session (struct Plugin *plugin, struct Sessionqueue *queue,
2202 int do_free_macendpoint)
2204 struct Sessionqueue *pendingsession;
2205 struct Sessionqueue *pendingsession_tmp;
2206 struct PendingMessage *pm;
2207 struct MacEndpoint *endpoint;
2208 struct FragmentMessage *fm;
2209 struct FragmentMessage *fmnext;
2212 GNUNET_assert (plugin != NULL);
2213 GNUNET_assert (queue != NULL);
2214 GNUNET_assert (queue->content != NULL);
2217 //is this session pending for send
2218 pendingsession = plugin->pending_Sessions_head;
2219 while (pendingsession != NULL)
2221 pendingsession_tmp = pendingsession;
2222 pendingsession = pendingsession->next;
2223 GNUNET_assert (pendingsession_tmp->content != NULL);
2224 if (pendingsession_tmp->content == queue->content)
2226 plugin->pendingsessions--;
2227 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
2228 plugin->pendingsessions, GNUNET_NO);
2229 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
2230 plugin->pending_Sessions_tail,
2231 pendingsession_tmp);
2232 GNUNET_free (pendingsession_tmp);
2234 GNUNET_assert (check == 0);
2239 endpoint = queue->content->mac;
2240 fm = endpoint->sending_messages_head;
2244 if (fm->session == queue->content)
2246 free_fragment_message (plugin, fm);
2251 // remove PendingMessage
2252 pm = queue->content->pending_message_head;
2255 GNUNET_CONTAINER_DLL_remove (queue->content->pending_message_head,
2256 queue->content->pending_message_tail, pm);
2257 GNUNET_free (pm->msg);
2259 pm = queue->content->pending_message_head;
2262 GNUNET_CONTAINER_DLL_remove (endpoint->sessions_head, endpoint->sessions_tail,
2264 //Check that no ohter session on this endpoint for this session exits
2265 GNUNET_assert (search_session (plugin, endpoint, &queue->content->target) ==
2267 if (endpoint->sessions_head == NULL && do_free_macendpoint == GNUNET_YES)
2269 free_macendpoint (plugin, endpoint);
2270 //check if no endpoint with the same address exists
2271 GNUNET_assert (get_macendpoint (plugin, &endpoint->addr, GNUNET_NO) ==
2275 if (queue->content->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2277 GNUNET_SCHEDULER_cancel (queue->content->timeout_task);
2278 queue->content->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2280 GNUNET_free (queue);
2282 check_fragment_queue (plugin);
2286 * Function that can be used to force the plugin to disconnect
2287 * from the given peer and cancel all previous transmissions
2288 * (and their continuation).
2290 * @param cls closure
2291 * @param target peer from which to disconnect
2294 wlan_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
2296 struct Plugin *plugin = cls;
2297 struct Sessionqueue *queue;
2298 struct Sessionqueue *queue_next;
2299 struct MacEndpoint *endpoint = plugin->mac_head;
2300 struct MacEndpoint *endpoint_next;
2302 // just look at all the session for the needed one
2303 while (endpoint != NULL)
2305 queue = endpoint->sessions_head;
2306 endpoint_next = endpoint->next;
2307 while (queue != NULL)
2309 // content is never NULL
2310 GNUNET_assert (queue->content != NULL);
2311 queue_next = queue->next;
2313 (target, &(queue->content->target),
2314 sizeof (struct GNUNET_PeerIdentity)) == 0)
2316 free_session (plugin, queue, GNUNET_YES);
2321 endpoint = endpoint_next;
2326 * Convert the transports address to a nice, human-readable
2329 * @param cls closure
2330 * @param type name of the transport that generated the address
2331 * @param addr one of the addresses of the host, NULL for the last address
2332 * the specific address format depends on the transport
2333 * @param addrlen length of the address
2334 * @param numeric should (IP) addresses be displayed in numeric form?
2335 * @param timeout after how long should we give up?
2336 * @param asc function to call on each string
2337 * @param asc_cls closure for asc
2340 wlan_plugin_address_pretty_printer (void *cls, const char *type,
2341 const void *addr, size_t addrlen,
2343 struct GNUNET_TIME_Relative timeout,
2344 GNUNET_TRANSPORT_AddressStringCallback asc,
2348 const unsigned char *input;
2350 //GNUNET_assert(cls !=NULL);
2351 if (addrlen != sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))
2353 /* invalid address (MAC addresses have 6 bytes) */
2355 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2356 "Func wlan_plugin_address_pretty_printer got size: %u, worng size!\n",
2358 asc (asc_cls, NULL);
2361 input = (const unsigned char *) addr;
2362 GNUNET_asprintf (&ret,
2363 "Transport %s: %s Mac-Address %.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
2364 type, PROTOCOL_PREFIX, input[0], input[1], input[2],
2365 input[3], input[4], input[5]);
2366 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2367 "Func wlan_plugin_address_pretty_printer got size: %u, nummeric %u, type %s; made string: %s\n",
2368 addrlen, numeric, type, ret);
2370 //only one mac address per plugin
2371 asc (asc_cls, NULL);
2377 * handels the data after all fragments are put together
2378 * @param cls macendpoint this messages belongs to
2379 * @param hdr pointer to the data
2382 wlan_data_message_handler (void *cls, const struct GNUNET_MessageHeader *hdr)
2384 struct MacEndpoint *endpoint = (struct MacEndpoint *) cls;
2385 struct Plugin *plugin = endpoint->plugin;
2386 struct WlanHeader *wlanheader;
2387 struct Session *session;
2389 const struct GNUNET_MessageHeader *temp_hdr;
2390 struct GNUNET_PeerIdentity tmpsource;
2393 GNUNET_assert (plugin != NULL);
2395 if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_DATA)
2398 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2399 "Func wlan_data_message_handler got GNUNET_MESSAGE_TYPE_WLAN_DATA size: %u\n",
2402 if (ntohs (hdr->size) <
2403 sizeof (struct WlanHeader) + sizeof (struct GNUNET_MessageHeader))
2405 //packet not big enought
2409 GNUNET_STATISTICS_update (plugin->env->stats,
2410 _("# wlan whole messages received"), 1,
2412 wlanheader = (struct WlanHeader *) hdr;
2414 session = search_session (plugin, endpoint, &wlanheader->source);
2416 temp_hdr = (const struct GNUNET_MessageHeader *) &wlanheader[1];
2417 crc = ntohl (wlanheader->crc);
2418 wlanheader->crc = 0;
2419 if (GNUNET_CRYPTO_crc32_n
2420 ((char *) wlanheader, ntohs (wlanheader->header.size)) != crc)
2422 //wrong crc, dispose message
2423 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2424 "Wlan message header crc was wrong: %u != %u\n",
2425 GNUNET_CRYPTO_crc32_n ((char *) wlanheader,
2426 ntohs (wlanheader->header.size)),
2428 hexdump ((void *) hdr, ntohs (hdr->size));
2432 //if not in session list
2433 if (session == NULL)
2435 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2436 "WLAN client not in session list: packet size = %u, inner size = %u, header size = %u\n",
2437 ntohs (wlanheader->header.size), ntohs (temp_hdr->size),
2438 sizeof (struct WlanHeader));
2439 //try if it is a hello message
2440 if (ntohs (wlanheader->header.size) >=
2441 ntohs (temp_hdr->size) + sizeof (struct WlanHeader))
2443 if (ntohs (temp_hdr->type) == GNUNET_MESSAGE_TYPE_HELLO)
2445 if (GNUNET_HELLO_get_id
2446 ((const struct GNUNET_HELLO_Message *) temp_hdr,
2447 &tmpsource) == GNUNET_OK)
2449 session = create_session (plugin, endpoint, &tmpsource);
2453 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2454 "WLAN client not in session list and hello message is not okay\n");
2461 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2462 "WLAN client not in session list and not a hello message\n");
2468 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2469 "WLAN client not in session list and message size in does not fit\npacket size = %u, inner size = %u, header size = %u\n",
2470 ntohs (wlanheader->header.size),
2471 ntohs (temp_hdr->size), sizeof (struct WlanHeader));
2476 //"receive" the message
2479 (&wlanheader->source, &session->target,
2480 sizeof (struct GNUNET_PeerIdentity)) != 0)
2483 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2484 "WLAN peer source id doesn't match packet peer source id: session %p\n",
2490 (&wlanheader->target, plugin->env->my_identity,
2491 sizeof (struct GNUNET_PeerIdentity)) != 0)
2494 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2495 "WLAN peer target id doesn't match our peer id: session %p\n",
2500 GNUNET_SERVER_mst_receive (plugin->data_tokenizer, session,
2501 (const char *) temp_hdr,
2502 ntohs (hdr->size) - sizeof (struct WlanHeader),
2503 GNUNET_YES, GNUNET_NO);
2509 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2510 "wlan_data_message_handler got wrong message type: %u\n",
2517 * function to process the a message, give it to the higher layer
2518 * @param cls pointer to the plugin
2519 * @param client pointer to the session this message belongs to
2520 * @param hdr start of the message
2522 //TODO ATS informations
2524 process_data (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
2527 GNUNET_assert (client != NULL);
2528 GNUNET_assert (cls != NULL);
2529 struct Session *session = (struct Session *) client;
2530 struct Plugin *plugin = (struct Plugin *) cls;
2531 struct GNUNET_ATS_Information ats[2];
2533 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
2534 ats[0].value = htonl (1);
2535 ats[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
2536 ats[1].value = htonl (GNUNET_ATS_NET_WLAN);
2538 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2539 "Calling plugin->env->receive for session %p; %s; size: %u\n",
2540 session, wlan_plugin_address_to_string (NULL,
2544 plugin->env->receive (plugin->env->cls, &(session->target), hdr,
2545 (const struct GNUNET_ATS_Information *) &ats, 2,
2546 session, (const char *) &session->mac->addr,
2547 sizeof (session->mac->addr));
2551 * Function used for to process the data received from the wlan interface
2553 * @param cls the plugin handle
2554 * @param hdr hdr of the GNUNET_MessageHeader
2555 * @param rxinfo pointer to the radiotap informations got with this packet FIXME: give ATS info
2558 wlan_data_helper (void *cls,
2559 const struct GNUNET_MessageHeader *hdr,
2560 const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rxinfo)
2562 struct Plugin *plugin = cls;
2563 struct FragmentMessage *fm;
2564 struct FragmentMessage *fm2;
2565 struct GNUNET_PeerIdentity tmpsource;
2566 struct MacEndpoint *macendpoint;
2567 struct Session *session;
2569 // NOTE: session_light->addr = rxinfo->frame.addr2;
2570 macendpoint = get_macendpoint (plugin, &rxinfo->frame.addr2, GNUNET_YES);
2572 switch (ntohs (hdr->type))
2574 case GNUNET_MESSAGE_TYPE_HELLO:
2576 //TODO better DOS protection, error handling
2577 //TODO test first then create session
2578 if (GNUNET_HELLO_get_id
2579 ((const struct GNUNET_HELLO_Message *) hdr, &tmpsource) == GNUNET_OK)
2581 session = search_session (plugin, macendpoint, &tmpsource);
2582 if (session == NULL)
2584 session = create_session (plugin, macendpoint, &tmpsource);
2586 GNUNET_STATISTICS_update (plugin->env->stats,
2587 _("# wlan hello messages received"), 1,
2589 plugin->env->receive (plugin->env->cls, &session->target,
2590 hdr, NULL, 0, session,
2591 (const char *) &session->mac->addr,
2592 sizeof (session->mac->addr));
2596 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2597 "WLAN client not in session list and HELLO message is not okay\n");
2601 case GNUNET_MESSAGE_TYPE_FRAGMENT:
2602 macendpoint = get_macendpoint (plugin, &rxinfo->frame.addr2, GNUNET_YES);
2603 GNUNET_STATISTICS_update (plugin->env->stats,
2604 _("# wlan fragments received"), 1, GNUNET_NO);
2606 GNUNET_DEFRAGMENT_process_fragment (macendpoint->defrag,
2609 if (ret == GNUNET_NO)
2611 macendpoint->dups++;
2613 else if (ret == GNUNET_OK)
2615 macendpoint->fragc++;
2617 set_next_send (plugin);
2619 case GNUNET_MESSAGE_TYPE_FRAGMENT_ACK:
2620 if (NULL == macendpoint)
2622 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2623 "Macendpoint does not exist for this GNUNET_MESSAGE_TYPE_FRAGMENT_ACK size: %u; %s\n",
2624 ntohs (hdr->size), wlan_plugin_address_to_string (NULL,
2625 &rxinfo->frame.addr2.mac,
2629 fm = macendpoint->sending_messages_head;
2633 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan acks received"),
2635 int ret = GNUNET_FRAGMENT_process_ack (fm->fragcontext, hdr);
2637 if (ret == GNUNET_OK)
2639 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2640 "Got last ack, finished fragment message %p\n", fm);
2641 macendpoint->acks++;
2642 fm->session->last_activity = GNUNET_TIME_absolute_get ();
2643 macendpoint->last_activity = fm->session->last_activity;
2644 free_fragment_message (plugin, fm);
2645 check_fragment_queue (plugin);
2648 if (ret == GNUNET_NO)
2650 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2651 "Got ack for: %p\n", fm);
2652 macendpoint->acks++;
2657 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2658 "WLAN fragment not in fragment list\n");
2662 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2663 "WLAN packet inside the WLAN helper packet has not the right type: %u size: %u\n",
2664 ntohs (hdr->type), ntohs (hdr->size));
2668 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2669 "Helper finished\n");
2673 * Function to print mac addresses nicely.
2675 * @param mac the mac address
2676 * @return string to a static buffer with the human-readable mac, will be overwritten during the next call to this function
2679 macprinter (const struct GNUNET_TRANSPORT_WLAN_MacAddress * mac)
2681 static char macstr[20];
2683 GNUNET_snprintf (macstr, sizeof (macstr), "%2X:%2X:%2X:%2X:%2X:%2X", mac->mac[0], mac->mac[1],
2684 mac->mac[2], mac->mac[3], mac->mac[4], mac->mac[5]);
2689 * Function for the scheduler if a mac endpoint times out
2690 * @param cls pointer to the MacEndpoint
2691 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
2694 macendpoint_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2696 struct MacEndpoint *endpoint = cls;
2698 GNUNET_assert (endpoint != NULL);
2699 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2700 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2704 if (GNUNET_TIME_absolute_get_remaining
2705 (GNUNET_TIME_absolute_add
2706 (endpoint->last_activity, MACENDPOINT_TIMEOUT)).rel_value == 0)
2708 GNUNET_assert (endpoint->plugin != NULL);
2709 GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
2710 _("# wlan mac endpoints timeouts"), 1, GNUNET_NO);
2711 free_macendpoint (endpoint->plugin, endpoint);
2715 endpoint->timeout_task =
2716 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
2722 * function to create an macendpoint
2723 * @param plugin pointer to the plugin struct
2724 * @param addr pointer to the macaddress
2725 * @return returns a macendpoint
2727 static struct MacEndpoint *
2728 create_macendpoint (struct Plugin *plugin, const struct GNUNET_TRANSPORT_WLAN_MacAddress *addr)
2730 struct MacEndpoint *newend = GNUNET_malloc (sizeof (struct MacEndpoint));
2732 GNUNET_assert (plugin != NULL);
2733 GNUNET_STATISTICS_update (plugin->env->stats,
2734 _("# wlan mac endpoints created"), 1, GNUNET_NO);
2735 newend->addr = *addr;
2736 newend->plugin = plugin;
2737 newend->addr = *addr;
2738 newend->fragment_messages_out_count = 0;
2740 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
2741 MESSAGES_IN_DEFRAG_QUEUE_PER_MAC,
2742 newend, &wlan_data_message_handler,
2744 newend->last_activity = GNUNET_TIME_absolute_get ();
2745 newend->timeout_task =
2746 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
2749 plugin->mac_count++;
2750 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan mac endpoints"),
2751 plugin->mac_count, GNUNET_NO);
2752 GNUNET_CONTAINER_DLL_insert_tail (plugin->mac_head, plugin->mac_tail, newend);
2753 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2754 "New Mac Endpoint %p: %s\n", newend,
2755 wlan_plugin_address_to_string (NULL, newend->addr.mac, 6));
2760 * Function used for to process the data from the suid process
2762 * @param cls the plugin handle
2763 * @param client client that send the data (not used)
2764 * @param hdr header of the GNUNET_MessageHeader
2767 wlan_process_helper (void *cls, void *client,
2768 const struct GNUNET_MessageHeader *hdr)
2770 struct Plugin *plugin = cls;
2771 const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rxinfo;
2772 const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *cm;
2773 const struct GNUNET_MessageHeader *temp_hdr = NULL;
2778 GNUNET_assert (plugin != NULL);
2779 switch (ntohs (hdr->type))
2781 case GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER:
2782 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2783 "Got data message from helper with %u bytes\n",
2785 GNUNET_STATISTICS_update (plugin->env->stats,
2786 _("# wlan data messages received"), 1,
2788 //call wlan_process_helper with the message inside, later with wlan: analyze signal
2789 if (ntohs (hdr->size) < sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage))
2791 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2792 "Size of packet is too small; size: %u\n",
2795 /* FIXME: restart SUID process */
2799 rxinfo = (const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) hdr;
2800 //process only if it is an broadcast or for this computer both with the gnunet bssid
2803 (&rxinfo->frame.addr3, &mac_bssid_gnunet,
2804 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0)
2806 //check for broadcast or mac
2808 (&rxinfo->frame.addr1, &bc_all_mac,
2809 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0) ||
2811 (&rxinfo->frame.addr1, &plugin->mac_address,
2812 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0))
2814 //if packet is from us return
2816 (&rxinfo->frame.addr2, &plugin->mac_address,
2817 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0))
2822 datasize = ntohs (hdr->size) - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage);
2823 GNUNET_STATISTICS_update (plugin->env->stats,
2824 _("# wlan messages for this client received"),
2826 // FIXME: this is a job for SERVER_mst --
2827 // what we are doing here is not good for alignment...
2829 end = (const char*) &rxinfo[1];
2830 while (pos < datasize - sizeof (struct GNUNET_MessageHeader))
2832 temp_hdr = (struct GNUNET_MessageHeader *) &end[pos];
2833 if (ntohs (temp_hdr->size) <= datasize - pos)
2835 GNUNET_STATISTICS_update (plugin->env->stats,
2837 ("# wlan messages inside WLAN_HELPER_DATA received"),
2839 wlan_data_helper (plugin, temp_hdr, rxinfo);
2843 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2844 "Size of packet is too small; size: %u > size of packet: %u\n",
2845 ntohs (temp_hdr->size), datasize - pos);
2847 pos += ntohs (temp_hdr->size);
2852 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2853 "Func wlan_process_helper got wrong MAC: %s\n",
2854 macprinter (&rxinfo->frame.addr1));
2859 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2860 "Func wlan_process_helper got wrong BSSID: %s\n",
2861 macprinter (&rxinfo->frame.addr2));
2864 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
2865 //TODO more control messages
2866 if (ntohs (hdr->size) != sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage))
2869 /* FIXME: restart SUID process */
2872 cm = (const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *) hdr;
2873 plugin->mac_address = cm->mac;
2874 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2875 "Received WLAN_HELPER_CONTROL message with transport of address %s\n",
2876 wlan_plugin_address_to_string (cls, &plugin->mac_address,
2877 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)));
2878 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2879 &plugin->mac_address,
2880 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress));
2883 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2884 "Func wlan_process_helper got unknown message with number %u, size %u\n",
2885 ntohs (hdr->type), ntohs (hdr->size));
2894 * Exit point from the plugin.
2895 * @param cls pointer to the api struct
2899 libgnunet_plugin_transport_wlan_done (void *cls)
2901 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2902 struct Plugin *plugin = api->cls;
2903 struct MacEndpoint *endpoint;
2904 struct MacEndpoint *endpoint_next;
2911 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2912 "libgnunet_plugin_transport_wlan_done started\n");
2913 wlan_transport_stop_wlan_helper (plugin);
2915 GNUNET_assert (cls != NULL);
2917 endpoint = plugin->mac_head;
2918 while (endpoint != NULL)
2920 endpoint_next = endpoint->next;
2921 free_macendpoint (plugin, endpoint);
2922 endpoint = endpoint_next;
2925 if (plugin->suid_tokenizer != NULL)
2926 GNUNET_SERVER_mst_destroy (plugin->suid_tokenizer);
2928 if (plugin->data_tokenizer != NULL)
2929 GNUNET_SERVER_mst_destroy (plugin->data_tokenizer);
2931 GNUNET_free_non_null (plugin->interface);
2932 GNUNET_free (plugin);
2939 * Entry point for the plugin.
2941 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2942 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2945 libgnunet_plugin_transport_wlan_init (void *cls)
2947 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2948 struct GNUNET_TRANSPORT_PluginFunctions *api;
2949 struct Plugin *plugin;
2951 if (NULL == env->receive)
2953 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2954 initialze the plugin or the API */
2955 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2957 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2958 api->address_to_string = &wlan_plugin_address_to_string;
2959 api->string_to_address = NULL; // FIXME!
2963 plugin = GNUNET_malloc (sizeof (struct Plugin));
2965 plugin->pendingsessions = 0;
2966 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
2967 plugin->pendingsessions, GNUNET_NO);
2968 plugin->mac_count = 0;
2969 GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan mac endpoints"),
2970 plugin->mac_count, GNUNET_NO);
2971 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
2972 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2973 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
2974 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
2975 GNUNET_BANDWIDTH_value_init (100 * 1024 *
2978 plugin->suid_tokenizer =
2979 GNUNET_SERVER_mst_create (&wlan_process_helper, plugin);
2981 plugin->data_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
2983 //plugin->sessions = GNUNET_malloc (sizeof (struct Sessionqueue));
2984 //plugin->pending_Sessions_head = GNUNET_malloc (sizeof (struct Sessionqueue));
2986 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2988 api->send = &wlan_plugin_send;
2989 api->get_session = &wlan_plugin_get_session;
2990 api->disconnect = &wlan_plugin_disconnect;
2991 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2992 api->check_address = &wlan_plugin_address_suggested;
2993 api->address_to_string = &wlan_plugin_address_to_string;
2997 if (GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "TESTMODE"))
2999 if (GNUNET_SYSERR ==
3000 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-wlan",
3001 "TESTMODE", &(plugin->testmode)))
3002 plugin->testmode = 0; //default value
3005 if (GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "INTERFACE"))
3007 if (GNUNET_CONFIGURATION_get_value_string
3008 (env->cfg, "transport-wlan", "INTERFACE",
3009 &(plugin->interface)) != GNUNET_YES)
3011 libgnunet_plugin_transport_wlan_done (api);
3017 wlan_transport_start_wlan_helper (plugin);
3018 set_next_beacon_time (plugin);
3019 set_next_send (plugin);
3020 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
3021 "wlan init finished\n");
3028 /* end of plugin_transport_wlan.c */