2 This file is part of GNUnet
3 (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 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
27 //TODO split rx and tx structures for better handling
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_util_lib.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_transport_service.h"
35 #include "gnunet_transport_plugin.h"
36 #include "plugin_transport_wlan.h"
37 #include "gnunet_common.h"
38 #include "gnunet_crypto_lib.h"
39 #include "gnunet_fragmentation_lib.h"
40 //#include "wlan/ieee80211.h"
41 //#include <netinet/ip.h>
45 #define PROTOCOL_PREFIX "wlan"
47 #define PLUGIN_LOG_NAME "wlan-plugin"
55 * time out of a session
57 #define SESSION_TIMEOUT GNUNET_TIME_UNIT_MINUTES
60 * time out of a mac endpoint
62 #define MACENDPOINT_TIMEOUT GNUNET_TIME_UNIT_MINUTES
65 * scaling factor for hello beacon
67 #define HALLO_BEACON_SCALING_FACTOR 1
70 * max size of fragment queue
72 #define FRAGMENT_QUEUE_SIZE 10
74 * max messages in fragment queue per session/client
76 #define FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION 1
79 * max messages in fragment queue per MAC
81 #define FRAGMENT_QUEUE_MESSAGES_OUT_PER_MACENDPOINT 1
84 * max messages in in queue
86 #define MESSAGES_IN_QUEUE_SIZE 10
88 * max messages in in queue per session/client
90 #define MESSAGES_IN_DEFRAG_QUEUE_PER_MAC 1
93 * LLC fields for better compatibility
95 #define WLAN_LLC_DSAP_FIELD 0xf
96 #define WLAN_LLC_SSAP_FIELD 0xf
102 #define DEBUG_wlan GNUNET_YES
103 #define DEBUG_wlan_retransmission GNUNET_NO
104 #define DEBUG_wlan_ip_udp_packets_on_air GNUNET_NO
105 #define DEBUG_wlan_msg_dump GNUNET_NO
108 #define IEEE80211_ADDR_LEN 6 /* size of 802.11 address */
110 #define IEEE80211_FC0_VERSION_MASK 0x03
111 #define IEEE80211_FC0_VERSION_SHIFT 0
112 #define IEEE80211_FC0_VERSION_0 0x00
113 #define IEEE80211_FC0_TYPE_MASK 0x0c
114 #define IEEE80211_FC0_TYPE_SHIFT 2
115 #define IEEE80211_FC0_TYPE_MGT 0x00
116 #define IEEE80211_FC0_TYPE_CTL 0x04
117 #define IEEE80211_FC0_TYPE_DATA 0x08
120 * Structure of an internet header, naked of options.
124 #if __BYTE_ORDER == __LITTLE_ENDIAN
125 unsigned int ip_hl:4; /* header length */
126 unsigned int ip_v:4; /* version */
128 #if __BYTE_ORDER == __BIG_ENDIAN
129 unsigned int ip_v:4; /* version */
130 unsigned int ip_hl:4; /* header length */
132 u_int8_t ip_tos; /* type of service */
133 u_short ip_len; /* total length */
134 u_short ip_id; /* identification */
135 u_short ip_off; /* fragment offset field */
136 #define IP_RF 0x8000 /* reserved fragment flag */
137 #define IP_DF 0x4000 /* dont fragment flag */
138 #define IP_MF 0x2000 /* more fragments flag */
139 #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
140 u_int8_t ip_ttl; /* time to live */
141 u_int8_t ip_p; /* protocol */
142 u_short ip_sum; /* checksum */
143 struct in_addr ip_src, ip_dst; /* source and dest address */
155 * generic definitions for IEEE 802.11 frames
157 struct ieee80211_frame
161 u_int8_t i_addr1[IEEE80211_ADDR_LEN];
162 u_int8_t i_addr2[IEEE80211_ADDR_LEN];
163 u_int8_t i_addr3[IEEE80211_ADDR_LEN];
166 #if DEBUG_wlan_ip_udp_packets_on_air
173 * Encapsulation of all of the state of the plugin.
180 struct GNUNET_TRANSPORT_PluginEnvironment *env;
183 * List of open connections. head
185 struct MacEndpoint *mac_head;
188 * List of open connections. tail
190 struct MacEndpoint *mac_tail;
193 * Number of connections
195 unsigned int mac_count;
198 * encapsulation of data from the local wlan helper program
200 struct GNUNET_SERVER_MessageStreamTokenizer *suid_tokenizer;
203 * encapsulation of packets received from the wlan helper
205 struct GNUNET_SERVER_MessageStreamTokenizer *data_tokenizer;
208 * stdout pipe handle for the gnunet-wlan-helper process
210 struct GNUNET_DISK_PipeHandle *server_stdout;
213 * stdout file handle for the gnunet-wlan-helper process
215 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
218 * stdin pipe handle for the gnunet-wlan-helper process
220 struct GNUNET_DISK_PipeHandle *server_stdin;
223 * stdin file handle for the gnunet-wlan-helper process
225 const struct GNUNET_DISK_FileHandle *server_stdin_handle;
228 * ID of the gnunet-wlan-server std read task
230 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
233 * ID of the gnunet-wlan-server std read task
235 GNUNET_SCHEDULER_TaskIdentifier server_write_task;
238 * ID of the delay task for writing
240 GNUNET_SCHEDULER_TaskIdentifier server_write_delay_task;
243 * The process id of the wlan process
245 struct GNUNET_OS_Process *server_proc;
248 * The interface of the wlan card given to us by the user.
253 * The mac_address of the wlan card given to us by the helper.
255 struct MacAddress mac_address;
258 * Sessions currently pending for transmission
261 struct Sessionqueue *pending_Sessions_head;
264 * Sessions currently pending for transmission
265 * to a peer (tail), if any.
267 struct Sessionqueue *pending_Sessions_tail;
270 * number of pending sessions
272 unsigned int pendingsessions;
275 * Messages in the sending queues
277 int pending_Fragment_Messages;
280 * messages ready for send, head
282 struct FragmentMessage_queue *sending_messages_head;
284 * messages ready for send, tail
286 struct FragmentMessage_queue *sending_messages_tail;
288 * time of the next "hello-beacon"
290 struct GNUNET_TIME_Absolute beacon_time;
293 * queue to send acks for received fragments (head)
295 struct AckSendQueue *ack_send_queue_head;
298 * queue to send acks for received fragments (tail)
300 struct AckSendQueue *ack_send_queue_tail;
303 * Tracker for bandwidth limit
305 struct GNUNET_BANDWIDTH_Tracker tracker;
309 * Struct to store data if file write did not accept the whole packet
313 struct Plugin *plugin;
315 struct GNUNET_MessageHeader *msgstart;
320 * Queue of sessions, for the general session queue and the pending session queue
325 struct Sessionqueue *next;
326 struct Sessionqueue *prev;
327 struct Session *content;
331 * Queue of fragmented messages, for the sending queue of the plugin
334 struct FragmentMessage_queue
336 struct FragmentMessage_queue *next;
337 struct FragmentMessage_queue *prev;
338 struct FragmentMessage *content;
342 * Queue for the fragments received
345 struct Receive_Fragment_Queue
347 struct Receive_Fragment_Queue *next;
348 struct Receive_Fragment_Queue *prev;
352 struct Radiotap_rx rxinfo;
356 struct MacEndpoint_id_fragment_triple
358 struct MacEndpoint *endpoint;
360 struct FragmentMessage *fm;
364 struct Plugin_Session_pair
366 struct Plugin *plugin;
367 struct Session *session;
371 * Information kept for each message that is yet to
374 struct PendingMessage
379 struct PendingMessage *next;
383 struct PendingMessage *prev;
386 * The pending message
388 struct WlanHeader *msg;
391 * Size of the message
396 * Continuation function to call once the message
397 * has been sent. Can be NULL if there is no
398 * continuation to call.
400 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
403 * Cls for transmit_cont
405 void *transmit_cont_cls;
408 * Timeout value for the pending message.
410 struct GNUNET_TIME_Absolute timeout;
415 * Queue for acks to send for fragments recived
421 * next ack in the ack send queue
423 struct AckSendQueue *next;
425 * previous ack in the ack send queue
427 struct AckSendQueue *prev;
429 * pointer to the session this ack belongs to
431 struct MacEndpoint *endpoint;
433 * ID of message, to distinguish between the messages, picked randomly.
440 struct GNUNET_MessageHeader *hdr;
442 * pointer to the ieee wlan header
444 struct ieee80211_frame *ieeewlanheader;
446 * pointer to the radiotap header
448 struct Radiotap_Send *radioHeader;
452 * Session infos gathered from a messages
458 * the session this message belongs to
460 struct Session *session;
464 struct MacAddress addr;
469 struct MacEndpoint *macendpoint;
473 * Session handle for connections.
481 struct SessionHeader header;
484 * Message currently pending for transmission
485 * to this peer, if any. head
487 struct PendingMessage *pending_message_head;
490 * Message currently pending for transmission
491 * to this peer, if any. tail
493 struct PendingMessage *pending_message_tail;
496 * To whom are we talking to (set to our identity
497 * if we are still waiting for the welcome message)
499 struct GNUNET_PeerIdentity target;
502 * Address of the other peer (either based on our 'connect'
503 * call or on our 'accept' call).
508 * Last activity on this connection. Used to select preferred
509 * connection and timeout
511 struct GNUNET_TIME_Absolute last_activity;
516 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
521 struct MacEndpoint *mac;
524 * count of messages in the fragment out queue for this session
527 int fragment_messages_out_count;
532 * Struct to represent one network card connection
537 * Pointer to the global plugin struct.
539 struct Plugin *plugin;
541 * Struct to hold the session reachable over this mac; head
543 struct Sessionqueue *sessions_head;
545 * Struct to hold the session reachable over this mac; tail
547 struct Sessionqueue *sessions_tail;
549 * Messages currently sending
552 struct FragmentMessage *sending_messages_head;
555 * Messages currently sending
556 * to a peer (tail), if any.
558 struct FragmentMessage *sending_messages_tail;
562 struct MacEndpoint *next;
566 struct MacEndpoint *prev;
571 struct MacAddress addr;
574 * Defrag context for this mac endpoint
576 struct GNUNET_DEFRAGMENT_Context *defrag;
579 * count of messages in the fragment out queue for this mac endpoint
582 int fragment_messages_out_count;
590 * Duplicates received
605 * Last activity on this endpoint. Used to select preferred
608 struct GNUNET_TIME_Absolute last_activity;
613 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
617 * Struct for Messages in the fragment queue
620 struct FragmentMessage
624 * Session this message belongs to
627 struct Session *session;
630 * This is a doubly-linked list.
632 struct FragmentMessage *next;
635 * This is a doubly-linked list.
637 struct FragmentMessage *prev;
640 * Fragmentation context
642 struct GNUNET_FRAGMENT_Context *fragcontext;
645 * Timeout value for the message.
647 struct GNUNET_TIME_Absolute timeout;
652 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
665 * pointer to the ieee wlan header
667 struct ieee80211_frame *ieeewlanheader;
669 * pointer to the radiotap header
671 struct Radiotap_Send *radioHeader;
675 do_transmit (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
677 free_session (struct Plugin *plugin, struct Sessionqueue *queue,
678 int do_free_macendpoint);
679 static struct MacEndpoint *
680 create_macendpoint (struct Plugin *plugin, const struct MacAddress *addr);
683 * Generates a nice hexdump of a memory area.
685 * \param mem pointer to memory to dump
686 * \param length how many bytes to dump
689 hexdump (const void *mem, unsigned length)
692 char *src = (char *) mem;
694 printf ("dumping %u bytes from %p\r\n"
695 " 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF\r\n",
700 for (i = 0; i < length; i += 16, src += 16)
704 t += sprintf (t, "%04x: ", i);
705 for (j = 0; j < 16; j++)
708 t += sprintf (t, "%02X", src[j] & 0xff);
710 t += sprintf (t, " ");
711 t += sprintf (t, j % 2 ? " " : "-");
714 t += sprintf (t, " ");
715 for (j = 0; j < 16; j++)
719 if (isprint ((unsigned char) src[j]))
720 t += sprintf (t, "%c", src[j]);
722 t += sprintf (t, ".");
726 t += sprintf (t, " ");
730 t += sprintf (t, "\r\n");
736 * Function to find a MacEndpoint with a specific mac addr
737 * @param plugin pointer to the plugin struct
738 * @param addr pointer to the mac address
739 * @param create_new GNUNET_YES if a new end point should be created
742 static struct MacEndpoint *
743 get_macendpoint (struct Plugin *plugin, const struct MacAddress *addr,
746 struct MacEndpoint *queue = plugin->mac_head;
748 while (queue != NULL)
750 GNUNET_assert (queue->sessions_head != NULL);
751 if (memcmp (addr, &queue->addr, sizeof (struct MacAddress)) == 0)
752 return queue; /* session found */
756 if (create_new == GNUNET_YES)
758 return create_macendpoint (plugin, addr);
768 * search for a session with the macendpoint and peer id
770 * @param plugin pointer to the plugin struct
771 * @param endpoint pointer to the mac endpoint of the peer
772 * @param peer pointer to the peerid
773 * @return returns the session
775 static struct Session *
776 search_session (struct Plugin *plugin, const struct MacEndpoint *endpoint,
777 const struct GNUNET_PeerIdentity *peer)
779 GNUNET_assert (endpoint != NULL);
780 struct Sessionqueue *queue = endpoint->sessions_head;
782 while (queue != NULL)
784 GNUNET_assert (queue->content != NULL);
786 (peer, &queue->content->target,
787 sizeof (struct GNUNET_PeerIdentity)) == 0)
788 return queue->content; /* session found */
795 * Function called for a quick conversion of the binary address to
796 * a numeric address. Note that the caller must not free the
797 * address and that the next call to this function is allowed
798 * to override the address again.
801 * @param addr binary address
802 * @param addrlen length of the address
803 * @return string representing the same address
806 wlan_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
809 const struct MacAddress *mac;
811 if (addrlen != sizeof (struct MacAddress))
817 GNUNET_snprintf (ret, sizeof (ret), "%s Mac-Address %X:%X:%X:%X:%X:%X",
818 PROTOCOL_PREFIX, mac->mac[0], mac->mac[1], mac->mac[2],
819 mac->mac[3], mac->mac[4], mac->mac[5]);
824 * Function for the scheduler if a session times out
825 * @param cls pointer to the Sessionqueue
826 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
829 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
831 struct Sessionqueue *queue = cls;
833 GNUNET_assert (queue != NULL);
834 queue->content->timeout_task = GNUNET_SCHEDULER_NO_TASK;
835 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
839 if (GNUNET_TIME_absolute_get_remaining
840 (GNUNET_TIME_absolute_add
841 (queue->content->last_activity, SESSION_TIMEOUT)).rel_value == 0)
843 free_session (queue->content->mac->plugin, queue, GNUNET_YES);
847 queue->content->timeout_task =
848 GNUNET_SCHEDULER_add_delayed (SESSION_TIMEOUT, &session_timeout, queue);
853 * create a new session
855 * @param plugin pointer to the plugin struct
856 * @param endpoint pointer to the mac endpoint of the peer
857 * @param peer peer identity to use for this session
858 * @return returns the session
861 static struct Session *
862 create_session (struct Plugin *plugin, struct MacEndpoint *endpoint,
863 const struct GNUNET_PeerIdentity *peer)
865 GNUNET_assert (endpoint != NULL);
866 struct Sessionqueue *queue =
867 GNUNET_malloc (sizeof (struct Sessionqueue) + sizeof (struct Session));
869 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
870 endpoint->sessions_tail, queue);
872 queue->content = (struct Session *) &queue[1];
873 queue->content->mac = endpoint;
874 memcpy (&(queue->content->target), peer, sizeof (struct GNUNET_PeerIdentity));
875 queue->content->last_activity = GNUNET_TIME_absolute_get ();
876 queue->content->timeout_task =
877 GNUNET_SCHEDULER_add_delayed (SESSION_TIMEOUT, &session_timeout, queue);
880 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
881 "New session %p with endpoint %p: %s\n", queue->content,
882 endpoint, wlan_plugin_address_to_string (NULL,
887 return queue->content;
891 * Get session from address, create if no session exists
893 * @param plugin pointer to the plugin struct
894 * @param addr pointer to the mac address of the peer
895 * @param peer pointer to the peerid
896 * @return returns the session
898 static struct Session *
899 get_session (struct Plugin *plugin, const struct MacAddress *addr,
900 const struct GNUNET_PeerIdentity *peer)
902 struct MacEndpoint *mac;
904 mac = get_macendpoint (plugin, addr, GNUNET_YES);
905 struct Session *session = search_session (plugin, mac, peer);
909 return create_session (plugin, mac, peer);
913 * Queue the session to send data
914 * checks if there is a message pending
915 * checks if this session is not allready in the queue
916 * @param plugin pointer to the plugin
917 * @param session pointer to the session to add
920 queue_session (struct Plugin *plugin, struct Session *session)
922 struct Sessionqueue *queue = plugin->pending_Sessions_head;
924 if (session->pending_message_head != NULL)
926 while (queue != NULL)
928 // content is never NULL
929 GNUNET_assert (queue->content != NULL);
930 // is session already in queue?
931 if (session == queue->content)
939 // Session is not in the queue
941 queue = GNUNET_malloc (sizeof (struct Sessionqueue));
942 queue->content = session;
945 GNUNET_CONTAINER_DLL_insert_tail (plugin->pending_Sessions_head,
946 plugin->pending_Sessions_tail, queue);
947 plugin->pendingsessions++;
953 * Function to schedule the write task, executed after a delay
954 * @param cls pointer to the plugin struct
955 * @param tc GNUNET_SCHEDULER_TaskContext pointer
958 delay_fragment_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
960 struct Plugin *plugin = cls;
962 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
964 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
967 // GNUNET_TIME_UNIT_FOREVER_REL is needed to clean up old msg
968 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
970 plugin->server_write_task =
971 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
972 plugin->server_stdin_handle,
973 &do_transmit, plugin);
978 * Function to calculate the time of the next periodic "hello-beacon"
979 * @param plugin pointer to the plugin struct
983 set_next_beacon_time (struct Plugin *const plugin)
985 //under 10 known peers: once a second
986 if (plugin->mac_count < 10)
988 plugin->beacon_time =
989 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
990 GNUNET_TIME_relative_multiply
991 (GNUNET_TIME_UNIT_SECONDS,
992 HALLO_BEACON_SCALING_FACTOR));
994 //under 30 known peers: every 10 seconds
995 else if (plugin->mac_count < 30)
997 plugin->beacon_time =
998 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
999 GNUNET_TIME_relative_multiply
1000 (GNUNET_TIME_UNIT_SECONDS,
1001 10 * HALLO_BEACON_SCALING_FACTOR));
1003 //over 30 known peers: once a minute
1006 plugin->beacon_time =
1007 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1008 GNUNET_TIME_relative_multiply
1009 (GNUNET_TIME_UNIT_MINUTES,
1010 HALLO_BEACON_SCALING_FACTOR));
1015 * Function to set the timer for the next timeout of the fragment queue
1016 * @param plugin the handle to the plugin struct
1020 set_next_send (struct Plugin *const plugin)
1022 struct GNUNET_TIME_Relative next_send;
1025 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
1027 GNUNET_SCHEDULER_cancel (plugin->server_write_delay_task);
1028 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
1031 //check if some acks are in the queue
1032 if (plugin->ack_send_queue_head != NULL)
1034 next_send = GNUNET_TIME_UNIT_ZERO;
1037 //check if there are some fragments in the queue
1038 else if (plugin->sending_messages_head != NULL)
1040 next_send = GNUNET_TIME_UNIT_ZERO;
1044 next_send = GNUNET_TIME_absolute_get_remaining (plugin->beacon_time);
1048 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1049 "Next packet is send in: %u\n", next_send.rel_value);
1052 if (next_send.rel_value == GNUNET_TIME_UNIT_ZERO.rel_value)
1054 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
1056 plugin->server_write_task =
1057 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1058 plugin->server_stdin_handle,
1059 &do_transmit, plugin);
1064 if (plugin->server_write_delay_task == GNUNET_SCHEDULER_NO_TASK)
1066 plugin->server_write_delay_task =
1067 GNUNET_SCHEDULER_add_delayed (next_send, &delay_fragment_task,
1074 * Function to get the next queued Session, removes the session from the queue
1075 * @param plugin pointer to the plugin struct
1076 * @return pointer to the session found, returns NULL if there is now session in the queue
1078 static struct Session *
1079 get_next_queue_session (struct Plugin *plugin)
1081 struct Session *session;
1082 struct Sessionqueue *sessionqueue;
1083 struct Sessionqueue *sessionqueue_alt;
1084 struct PendingMessage *pm;
1086 sessionqueue = plugin->pending_Sessions_head;
1088 while (sessionqueue != NULL)
1090 session = sessionqueue->content;
1092 GNUNET_assert (session != NULL);
1093 pm = session->pending_message_head;
1098 GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1099 "pending message is empty, should not happen. session %p\n",
1102 sessionqueue_alt = sessionqueue;
1103 sessionqueue = sessionqueue->next;
1104 plugin->pendingsessions--;
1105 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1106 plugin->pending_Sessions_tail,
1109 GNUNET_free (sessionqueue_alt);
1114 //check for message timeout
1115 if (GNUNET_TIME_absolute_get_remaining (pm->timeout).rel_value > 0)
1117 //check if session has no message in the fragment queue
1118 if ((session->mac->fragment_messages_out_count <
1119 FRAGMENT_QUEUE_MESSAGES_OUT_PER_MACENDPOINT) &&
1120 (session->fragment_messages_out_count <
1121 FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION))
1123 plugin->pendingsessions--;
1124 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1125 plugin->pending_Sessions_tail,
1127 GNUNET_free (sessionqueue);
1133 sessionqueue = sessionqueue->next;
1138 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
1139 session->pending_message_tail, pm);
1141 //call the cont func that it did not work
1142 if (pm->transmit_cont != NULL)
1143 pm->transmit_cont (pm->transmit_cont_cls, &(session->target),
1145 GNUNET_free (pm->msg);
1148 if (session->pending_message_head == NULL)
1150 sessionqueue_alt = sessionqueue;
1151 sessionqueue = sessionqueue->next;
1152 plugin->pendingsessions--;
1153 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1154 plugin->pending_Sessions_tail,
1157 GNUNET_free (sessionqueue_alt);
1166 * frees the space of a message in the fragment queue (send queue)
1167 * @param plugin the plugin struct
1168 * @param fm message to free
1171 free_fragment_message (struct Plugin *plugin, struct FragmentMessage *fm)
1173 struct Session *session = fm->session;
1174 struct MacEndpoint *endpoint = session->mac;
1175 struct FragmentMessage_queue *fmq;
1176 struct FragmentMessage_queue *fmq_next;
1180 fmq = plugin->sending_messages_head;
1183 fmq_next = fmq->next;
1184 if (fmq->content == fm)
1186 GNUNET_CONTAINER_DLL_remove (plugin->sending_messages_head,
1187 plugin->sending_messages_tail, fmq);
1193 (session->mac->fragment_messages_out_count)--;
1194 session->fragment_messages_out_count--;
1195 plugin->pending_Fragment_Messages--;
1196 GNUNET_CONTAINER_DLL_remove (endpoint->sending_messages_head,
1197 endpoint->sending_messages_tail, fm);
1198 GNUNET_FRAGMENT_context_destroy (fm->fragcontext);
1199 if (fm->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1200 GNUNET_SCHEDULER_cancel (fm->timeout_task);
1203 queue_session (plugin, session);
1205 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1206 "Free pending fragment messages %p, session %p\n", fm,
1213 * function to fill the radiotap header
1214 * @param plugin pointer to the plugin struct
1215 * @param endpoint pointer to the endpoint
1216 * @param header pointer to the radiotap header
1217 * @return GNUNET_YES at success
1220 getRadiotapHeader (struct Plugin *plugin, struct MacEndpoint *endpoint,
1221 struct Radiotap_Send *header)
1224 if (endpoint != NULL)
1226 header->rate = endpoint->rate;
1227 header->tx_power = endpoint->tx_power;
1228 header->antenna = endpoint->antenna;
1233 header->tx_power = 0;
1234 header->antenna = 0;
1241 * function to generate the wlan hardware header for one packet
1242 * @param Header address to write the header to
1243 * @param to_mac_addr address of the recipient
1244 * @param plugin pointer to the plugin struct
1245 * @param size size of the whole packet, needed to calculate the time to send the packet
1246 * @return GNUNET_YES if there was no error
1249 getWlanHeader (struct ieee80211_frame *Header,
1250 const struct MacAddress *to_mac_addr, struct Plugin *plugin,
1254 const int rate = 11000000;
1256 Header->i_fc[0] = IEEE80211_FC0_TYPE_DATA;
1257 Header->i_fc[1] = 0x00;
1258 memcpy (&Header->i_addr3, &mac_bssid, sizeof (mac_bssid));
1259 memcpy (&Header->i_addr2, plugin->mac_address.mac,
1260 sizeof (plugin->mac_address));
1261 memcpy (&Header->i_addr1, to_mac_addr, sizeof (struct MacAddress));
1263 tmp16 = (uint16_t *) Header->i_dur;
1264 *tmp16 = (uint16_t) htole16 ((size * 1000000) / rate + 290);
1265 Header->llc[0] = WLAN_LLC_DSAP_FIELD;
1266 Header->llc[1] = WLAN_LLC_SSAP_FIELD;
1268 #if DEBUG_wlan_ip_udp_packets_on_air
1273 Header->ip.ip_dst.s_addr = *((uint32_t *) & to_mac_addr->mac[2]);
1274 Header->ip.ip_src.s_addr = *((uint32_t *) & plugin->mac_address.mac[2]);
1275 Header->ip.ip_v = 4;
1276 Header->ip.ip_hl = 5;
1277 Header->ip.ip_p = 17;
1278 Header->ip.ip_ttl = 1;
1279 Header->ip.ip_len = htons (size + 8);
1280 Header->ip.ip_sum = 0;
1281 x = (uint16_t *) & Header->ip;
1282 count = sizeof (struct iph);
1285 /* This is the inner loop */
1286 crc += (unsigned short) *x++;
1289 /* Add left-over byte, if any */
1291 crc += *(unsigned char *) x;
1292 crc = (crc & 0xffff) + (crc >> 16);
1293 Header->ip.ip_sum = htons (~(unsigned short) crc);
1294 Header->udp.len = htons (size - sizeof (struct ieee80211_frame));
1304 * @param msgbuf pointer tor the data
1305 * @param msgbuf_size size of the data
1307 * @return 32bit crc value
1311 getcrc32 (const char *msgbuf, size_t msgbuf_size)
1314 return GNUNET_CRYPTO_crc32_n (msgbuf, msgbuf_size);;
1320 * @param msgbuf pointer tor the data
1321 * @param msgbuf_size size of the data
1323 * @return 16bit crc value
1327 getcrc16 (const char *msgbuf, size_t msgbuf_size)
1329 //TODO calc some crc
1334 * function to add a fragment of a message to send
1335 * @param cls FragmentMessage this message belongs to
1336 * @param hdr pointer to the start of the message
1340 add_message_for_send (void *cls, const struct GNUNET_MessageHeader *hdr)
1343 struct FragmentMessage *fm = cls;
1344 struct FragmentMessage_queue *fmqueue;
1346 GNUNET_assert (cls != NULL);
1347 GNUNET_assert (fm->frag == NULL);
1348 struct MacEndpoint *endpoint = fm->session->mac;
1349 struct Plugin *plugin = endpoint->plugin;
1350 struct GNUNET_MessageHeader *msgheader;
1351 struct GNUNET_MessageHeader *msgheader2;
1354 #if DEBUG_wlan_retransmission
1355 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1356 "Adding fragment of message %p to send, session %p, endpoint %p, type %u\n",
1357 fm, fm->session, endpoint, hdr->type);
1361 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1362 sizeof (struct ieee80211_frame) + ntohs (hdr->size);
1363 fm->frag = GNUNET_malloc (size);
1366 msgheader = (struct GNUNET_MessageHeader *) fm->frag;
1367 msgheader->size = htons (size);
1368 msgheader->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1370 fm->radioHeader = (struct Radiotap_Send *) &msgheader[1];
1371 fm->ieeewlanheader = (struct ieee80211_frame *) &fm->radioHeader[1];
1372 msgheader2 = (struct GNUNET_MessageHeader *) &fm->ieeewlanheader[1];
1373 memcpy (msgheader2, hdr, ntohs (hdr->size));
1375 fmqueue = GNUNET_malloc (sizeof (struct FragmentMessage_queue));
1376 fmqueue->content = fm;
1378 GNUNET_CONTAINER_DLL_insert_tail (plugin->sending_messages_head,
1379 plugin->sending_messages_tail, fmqueue);
1380 set_next_send (plugin);
1384 * function to send a hallo beacon
1385 * @param plugin pointer to the plugin struct
1388 send_hello_beacon (struct Plugin *plugin)
1392 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1393 "Sending hello beacon\n");
1398 uint16_t hallo_size;
1399 struct GNUNET_MessageHeader *msgheader;
1400 struct ieee80211_frame *ieeewlanheader;
1401 struct Radiotap_Send *radioHeader;
1402 struct GNUNET_MessageHeader *msgheader2;
1403 const struct GNUNET_MessageHeader *hello;
1405 hello = plugin->env->get_our_hello ();
1406 hallo_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1407 GNUNET_assert (sizeof (struct WlanHeader) + hallo_size <= WLAN_MTU);
1409 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1410 sizeof (struct ieee80211_frame) + hallo_size;
1412 msgheader = GNUNET_malloc (size);
1413 msgheader->size = htons (size);
1414 msgheader->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1416 radioHeader = (struct Radiotap_Send *) &msgheader[1];
1417 getRadiotapHeader (plugin, NULL, radioHeader);
1418 ieeewlanheader = (struct ieee80211_frame *) &radioHeader[1];
1419 getWlanHeader (ieeewlanheader, &bc_all_mac, plugin, size);
1421 msgheader2 = (struct GNUNET_MessageHeader *) &ieeewlanheader[1];
1422 /*msgheader2->size =
1423 htons (GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello) +
1424 sizeof (struct GNUNET_MessageHeader));
1426 msgheader2->type = htons (GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT);*/
1427 memcpy (msgheader2, hello, hallo_size);
1429 bytes = GNUNET_DISK_file_write (plugin->server_stdin_handle, msgheader, size);
1431 if (bytes == GNUNET_SYSERR)
1433 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1435 ("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1436 errno, strerror (errno));
1439 GNUNET_assert (bytes != GNUNET_SYSERR);
1440 GNUNET_assert (bytes == size);
1441 GNUNET_free (msgheader);
1443 set_next_beacon_time (plugin);
1444 set_next_send (plugin);
1448 * function to add an ack to send it for a received fragment
1449 * @param cls MacEndpoint this ack belongs to
1450 * @param msg_id id of the message
1451 * @param hdr pointer to the hdr where the ack is stored
1456 add_ack_for_send (void *cls, uint32_t msg_id,
1457 const struct GNUNET_MessageHeader *hdr)
1460 struct AckSendQueue *ack;
1462 GNUNET_assert (cls != NULL);
1463 struct MacEndpoint *endpoint = cls;
1464 struct Plugin *plugin = endpoint->plugin;
1465 struct GNUNET_MessageHeader *msgheader;
1466 struct GNUNET_MessageHeader *msgheader2;
1470 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1471 sizeof (struct ieee80211_frame) + ntohs (hdr->size) +
1472 sizeof (struct AckSendQueue);
1474 ack = GNUNET_malloc (size);
1475 ack->message_id = msg_id;
1476 ack->endpoint = endpoint;
1479 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1480 sizeof (struct ieee80211_frame) + ntohs (hdr->size);
1482 msgheader = (struct GNUNET_MessageHeader *) &ack[1];
1483 ack->hdr = (struct GNUNET_MessageHeader *) &ack[1];
1484 msgheader->size = htons (size);
1485 msgheader->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1487 ack->radioHeader = (struct Radiotap_Send *) &msgheader[1];
1488 ack->ieeewlanheader = (struct ieee80211_frame *) &(ack->radioHeader)[1];
1489 msgheader2 = (struct GNUNET_MessageHeader *) &(ack->ieeewlanheader)[1];
1490 memcpy (msgheader2, hdr, ntohs (hdr->size));
1492 GNUNET_CONTAINER_DLL_insert_tail (plugin->ack_send_queue_head,
1493 plugin->ack_send_queue_tail, ack);
1495 #if DEBUG_wlan_retransmission
1496 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1497 "Adding ack with message id %u to send, AckSendQueue %p, endpoint %p\n",
1498 msg_id, ack, endpoint);
1501 set_next_send (plugin);
1505 * Function for the scheduler if a FragmentMessage times out
1506 * @param cls pointer to the FragmentMessage
1507 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
1510 fragmentmessage_timeout (void *cls,
1511 const struct GNUNET_SCHEDULER_TaskContext *tc)
1513 struct FragmentMessage *fm = cls;
1515 GNUNET_assert (fm != NULL);
1516 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1517 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1521 free_fragment_message (fm->session->mac->plugin, fm);
1525 * Function to check if there is some space in the fragment queue
1526 * inserts a message if space is available
1527 * @param plugin the plugin struct
1531 check_fragment_queue (struct Plugin *plugin)
1533 struct Session *session;
1534 struct FragmentMessage *fm;
1535 struct GNUNET_PeerIdentity pid;
1537 struct PendingMessage *pm;
1539 if (plugin->pending_Fragment_Messages < FRAGMENT_QUEUE_SIZE)
1541 session = get_next_queue_session (plugin);
1542 if (session != NULL)
1544 pm = session->pending_message_head;
1545 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
1546 session->pending_message_tail, pm);
1547 session->mac->fragment_messages_out_count++;
1548 session->fragment_messages_out_count++;
1549 plugin->pending_Fragment_Messages++;
1550 GNUNET_assert (pm != NULL);
1552 fm = GNUNET_malloc (sizeof (struct FragmentMessage));
1553 fm->session = session;
1554 fm->timeout.abs_value = pm->timeout.abs_value;
1557 GNUNET_FRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
1559 GNUNET_TIME_UNIT_SECONDS,
1561 &add_message_for_send, fm);
1563 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
1564 (fm->timeout), fragmentmessage_timeout,
1566 GNUNET_CONTAINER_DLL_insert_tail (session->mac->sending_messages_head,
1567 session->mac->sending_messages_tail,
1570 if (pm->transmit_cont != NULL)
1572 pid = session->target;
1573 pm->transmit_cont (pm->transmit_cont_cls, &pid, GNUNET_OK);
1575 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1576 "called pm->transmit_cont for %p\n", session);
1582 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1583 "no pm->transmit_cont for %p\n", session);
1588 if (session->pending_message_head != NULL)
1591 queue_session (plugin, session);
1597 //check if timeout changed
1598 set_next_send (plugin);
1602 * Function to send an ack, does not free the ack
1603 * @param plugin pointer to the plugin
1604 * @param ack pointer to the ack to send
1607 send_ack (struct Plugin *plugin, struct AckSendQueue *ack)
1613 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1614 "Sending ack for message_id %u for mac endpoint %p, size %u\n",
1615 ack->message_id, ack->endpoint,
1616 ntohs (ack->hdr->size) - sizeof (struct Radiotap_Send));
1619 getRadiotapHeader (plugin, ack->endpoint, ack->radioHeader);
1620 getWlanHeader (ack->ieeewlanheader, &ack->endpoint->addr, plugin,
1621 ntohs (ack->hdr->size));
1624 GNUNET_DISK_file_write (plugin->server_stdin_handle, ack->hdr,
1625 ntohs (ack->hdr->size));
1626 if (bytes == GNUNET_SYSERR)
1628 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1630 ("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1631 errno, strerror (errno));
1634 GNUNET_assert (bytes != GNUNET_SYSERR);
1635 GNUNET_assert (bytes == ntohs (ack->hdr->size));
1636 set_next_send (plugin);
1640 * function to finish a sending if not all could have been writen befor
1641 * @param cls pointer to the Finish_send struct
1642 * @param tc TaskContext
1645 finish_sending (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1647 struct Finish_send *finish = cls;
1648 struct Plugin *plugin;
1651 plugin = finish->plugin;
1652 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1654 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1656 GNUNET_free (finish->msgstart);
1657 GNUNET_free (finish);
1661 GNUNET_DISK_file_write (plugin->server_stdin_handle, finish->msgheader,
1663 GNUNET_assert (bytes != GNUNET_SYSERR);
1665 if (bytes != finish->size)
1667 finish->msgheader = finish->msgheader + bytes;
1668 finish->size = finish->size - bytes;
1669 plugin->server_write_task =
1670 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1671 plugin->server_stdin_handle,
1672 &finish_sending, finish);
1676 GNUNET_free (finish->msgstart);
1677 GNUNET_free (finish);
1678 set_next_send (plugin);
1683 * Function called when wlan helper is ready to get some data
1685 * @param cls closure
1686 * @param tc GNUNET_SCHEDULER_TaskContext
1689 do_transmit (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1691 struct Plugin *plugin = cls;
1693 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1694 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1697 struct Session *session;
1698 struct FragmentMessage *fm;
1699 struct Finish_send *finish;
1700 struct FragmentMessage_queue *fmq;
1701 struct AckSendQueue *ack;
1704 if (plugin->ack_send_queue_head != NULL)
1706 ack = plugin->ack_send_queue_head;
1707 GNUNET_CONTAINER_DLL_remove (plugin->ack_send_queue_head,
1708 plugin->ack_send_queue_tail, ack);
1709 send_ack (plugin, ack);
1714 //test if a "hello-beacon" has to be send
1715 if (GNUNET_TIME_absolute_get_remaining (plugin->beacon_time).rel_value == 0)
1717 send_hello_beacon (plugin);
1721 if (plugin->sending_messages_head != NULL)
1723 fmq = plugin->sending_messages_head;
1725 GNUNET_CONTAINER_DLL_remove (plugin->sending_messages_head,
1726 plugin->sending_messages_tail, fmq);
1729 session = fm->session;
1730 GNUNET_assert (session != NULL);
1733 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1734 "Sending GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT for fragment message %p, size: %u\n",
1738 getRadiotapHeader (plugin, session->mac, fm->radioHeader);
1739 getWlanHeader (fm->ieeewlanheader, &(fm->session->mac->addr), plugin,
1743 GNUNET_DISK_file_write (plugin->server_stdin_handle, fm->frag,
1745 if (bytes == GNUNET_SYSERR)
1747 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1749 ("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1750 errno, strerror (errno));
1753 GNUNET_assert (bytes != GNUNET_SYSERR);
1755 if (bytes != fm->size)
1757 finish = GNUNET_malloc (sizeof (struct Finish_send));
1758 finish->plugin = plugin;
1759 finish->msgheader = fm->frag + bytes;
1760 finish->size = fm->size - bytes;
1761 finish->msgstart = (struct GNUNET_MessageHeader *) fm->frag;
1763 GNUNET_assert (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK);
1765 plugin->server_write_task =
1766 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1767 plugin->server_stdin_handle,
1768 &finish_sending, finish);
1773 GNUNET_free (fm->frag);
1775 set_next_send (plugin);
1777 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
1781 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
1782 "do_transmit did nothing, should not happen!\n");
1786 * Another peer has suggested an address for this
1787 * peer and transport plugin. Check that this could be a valid
1788 * address. If so, consider adding it to the list
1791 * @param cls closure
1792 * @param addr pointer to the address
1793 * @param addrlen length of addr
1794 * @return GNUNET_OK if this is a plausible address for this peer
1798 wlan_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1800 //struct Plugin *plugin = cls;
1802 /* check if the address is plausible; if so,
1803 * add it to our list! */
1805 GNUNET_assert (cls != NULL);
1806 //FIXME mitm is not checked
1807 //Mac Address has 6 bytes
1810 /* TODO check for bad addresses like multicast, broadcast, etc */
1814 return GNUNET_SYSERR;
1818 * Function that can be used by the transport service to transmit
1819 * a message using the plugin.
1821 * @param cls closure
1822 * @param target who should receive this message
1823 * @param priority how important is the message
1824 * @param msgbuf the message to transmit
1825 * @param msgbuf_size number of bytes in 'msgbuf'
1826 * @param timeout when should we time out
1827 * @param session which session must be used (or NULL for "any")
1828 * @param addr the address to use (can be NULL if the plugin
1829 * is "on its own" (i.e. re-use existing TCP connection))
1830 * @param addrlen length of the address in bytes
1831 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1832 * otherwise the plugin may use other addresses or
1833 * existing connections (if available)
1834 * @param cont continuation to call once the message has
1835 * been transmitted (or if the transport is ready
1836 * for the next transmission call; or if the
1837 * peer disconnected...)
1838 * @param cont_cls closure for cont
1839 * @return number of bytes used (on the physical network, with overheads);
1840 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1841 * and does NOT mean that the message was not transmitted (DV)
1844 wlan_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
1845 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
1846 struct GNUNET_TIME_Relative timeout, struct Session *session,
1847 const void *addr, size_t addrlen, int force_address,
1848 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1850 struct Plugin *plugin = cls;
1851 struct PendingMessage *newmsg;
1852 struct WlanHeader *wlanheader;
1854 //check if msglen > 0
1855 GNUNET_assert (msgbuf_size > 0);
1857 //get session if needed
1858 if (session == NULL)
1860 if (wlan_plugin_address_suggested (plugin, addr, addrlen) == GNUNET_OK)
1862 session = get_session (plugin, addr, target);
1866 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1867 _("Wlan Address len %d is wrong\n"), addrlen);
1874 //queue message in session
1875 //test if there is no other message in the "queue"
1876 //FIXME: to many send requests
1877 if (session->pending_message_head != NULL)
1879 newmsg = session->pending_message_head;
1880 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1881 "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",
1882 GNUNET_TIME_absolute_get_remaining (newmsg->
1884 session->mac->fragment_messages_out_count);
1887 newmsg = GNUNET_malloc (sizeof (struct PendingMessage));
1888 newmsg->msg = GNUNET_malloc (msgbuf_size + sizeof (struct WlanHeader));
1889 wlanheader = newmsg->msg;
1890 //copy msg to buffer, not fragmented / segmented yet, but with message header
1891 wlanheader->header.size = htons (msgbuf_size + sizeof (struct WlanHeader));
1892 wlanheader->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA);
1893 memcpy (&(wlanheader->target), target, sizeof (struct GNUNET_PeerIdentity));
1894 memcpy (&(wlanheader->source), plugin->env->my_identity,
1895 sizeof (struct GNUNET_PeerIdentity));
1896 wlanheader->crc = 0;
1897 memcpy (&wlanheader[1], msgbuf, msgbuf_size);
1900 ((char *) wlanheader, msgbuf_size + sizeof (struct WlanHeader)));
1901 //GNUNET_log_from(GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME, "Wlan message Header crc: %u, %u\n",getcrc32((char*) wlanheader, msgbuf_size + sizeof(struct WlanHeader)), wlanheader->crc);
1902 //hexdump(newmsg->msg, msgbuf_size + sizeof(struct WlanHeader));
1904 newmsg->transmit_cont = cont;
1905 newmsg->transmit_cont_cls = cont_cls;
1906 newmsg->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1908 newmsg->timeout.abs_value = newmsg->timeout.abs_value - 500;
1910 newmsg->message_size = msgbuf_size + sizeof (struct WlanHeader);
1912 GNUNET_CONTAINER_DLL_insert_tail (session->pending_message_head,
1913 session->pending_message_tail, newmsg);
1916 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1917 "New message for %p with size (incl wlan header) %u added\n",
1918 session, newmsg->message_size);
1920 #if DEBUG_wlan_msg_dump
1921 hexdump (msgbuf, GNUNET_MIN (msgbuf_size, 256));
1924 queue_session (plugin, session);
1926 check_fragment_queue (plugin);
1927 //FIXME not the correct size
1933 * function to free a mac endpoint
1934 * @param plugin pointer to the plugin struct
1935 * @param endpoint pointer to the MacEndpoint to free
1938 free_macendpoint (struct Plugin *plugin, struct MacEndpoint *endpoint)
1940 struct Sessionqueue *sessions;
1941 struct Sessionqueue *sessions_next;
1943 GNUNET_assert (endpoint != NULL);
1945 sessions = endpoint->sessions_head;
1946 while (sessions != NULL)
1948 sessions_next = sessions->next;
1949 free_session (plugin, sessions, GNUNET_NO);
1950 sessions = sessions_next;
1953 GNUNET_CONTAINER_DLL_remove (plugin->mac_head, plugin->mac_tail, endpoint);
1954 if (endpoint->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1955 GNUNET_SCHEDULER_cancel (endpoint->timeout_task);
1956 GNUNET_free (endpoint);
1961 * function to free a session
1962 * @param plugin pointer to the plugin
1963 * @param queue pointer to the sessionqueue element to free
1964 * @param free_macendpoint if GNUNET_YES and mac endpoint would be empty, free mac endpoint
1967 free_session (struct Plugin *plugin, struct Sessionqueue *queue,
1968 int do_free_macendpoint)
1970 struct Sessionqueue *pendingsession;
1971 struct Sessionqueue *pendingsession_tmp;
1972 struct PendingMessage *pm;
1973 struct MacEndpoint *endpoint;
1974 struct FragmentMessage *fm;
1975 struct FragmentMessage *fmnext;
1978 GNUNET_assert (queue != NULL);
1979 GNUNET_assert (queue->content != NULL);
1982 //is this session pending for send
1983 pendingsession = plugin->pending_Sessions_head;
1984 while (pendingsession != NULL)
1986 pendingsession_tmp = pendingsession->next;
1987 if (pendingsession->content == queue->content)
1989 plugin->pendingsessions--;
1990 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1991 plugin->pending_Sessions_tail,
1993 GNUNET_free (pendingsession);
1995 GNUNET_assert (check == 0);
1998 pendingsession = pendingsession_tmp;
2001 endpoint = queue->content->mac;
2002 fm = endpoint->sending_messages_head;
2006 if (fm->session == queue->content)
2008 free_fragment_message (plugin, fm);
2013 // remove PendingMessage
2014 pm = queue->content->pending_message_head;
2017 GNUNET_CONTAINER_DLL_remove (queue->content->pending_message_head,
2018 queue->content->pending_message_tail, pm);
2019 GNUNET_free (pm->msg);
2021 pm = queue->content->pending_message_head;
2024 GNUNET_CONTAINER_DLL_remove (endpoint->sessions_head, endpoint->sessions_tail,
2027 if (endpoint->sessions_head == NULL && do_free_macendpoint == GNUNET_YES)
2029 free_macendpoint (plugin, endpoint);
2032 if (queue->content->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2033 GNUNET_SCHEDULER_cancel (queue->content->timeout_task);
2034 GNUNET_free (queue);
2035 check_fragment_queue (plugin);
2039 * Function that can be used to force the plugin to disconnect
2040 * from the given peer and cancel all previous transmissions
2041 * (and their continuation).
2043 * @param cls closure
2044 * @param target peer from which to disconnect
2047 wlan_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
2049 struct Plugin *plugin = cls;
2050 struct Sessionqueue *queue;
2051 struct Sessionqueue *queue_next;
2052 struct MacEndpoint *endpoint = plugin->mac_head;
2053 struct MacEndpoint *endpoint_next;
2055 // just look at all the session for the needed one
2056 while (endpoint != NULL)
2058 queue = endpoint->sessions_head;
2059 endpoint_next = endpoint->next;
2060 while (queue != NULL)
2062 // content is never NULL
2063 GNUNET_assert (queue->content != NULL);
2064 queue_next = queue->next;
2066 (target, &(queue->content->target),
2067 sizeof (struct GNUNET_PeerIdentity)) == 0)
2069 free_session (plugin, queue, GNUNET_YES);
2074 endpoint = endpoint_next;
2079 * Convert the transports address to a nice, human-readable
2082 * @param cls closure
2083 * @param type name of the transport that generated the address
2084 * @param addr one of the addresses of the host, NULL for the last address
2085 * the specific address format depends on the transport
2086 * @param addrlen length of the address
2087 * @param numeric should (IP) addresses be displayed in numeric form?
2088 * @param timeout after how long should we give up?
2089 * @param asc function to call on each string
2090 * @param asc_cls closure for asc
2093 wlan_plugin_address_pretty_printer (void *cls, const char *type,
2094 const void *addr, size_t addrlen,
2096 struct GNUNET_TIME_Relative timeout,
2097 GNUNET_TRANSPORT_AddressStringCallback asc,
2101 const unsigned char *input;
2103 //GNUNET_assert(cls !=NULL);
2106 /* invalid address (MAC addresses have 6 bytes) */ GNUNET_break (0);
2107 asc (asc_cls, NULL);
2110 input = (const unsigned char *) addr;
2111 GNUNET_snprintf (ret, sizeof (ret),
2112 "%s Mac-Address %.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
2113 PROTOCOL_PREFIX, input[0], input[1], input[2], input[3],
2114 input[4], input[5]);
2119 * handels the data after all fragments are put together
2120 * @param cls macendpoint this messages belongs to
2121 * @param hdr pointer to the data
2124 wlan_data_message_handler (void *cls, const struct GNUNET_MessageHeader *hdr)
2126 struct MacEndpoint *endpoint = (struct MacEndpoint *) cls;
2127 struct Plugin *plugin = endpoint->plugin;
2128 struct WlanHeader *wlanheader;
2129 struct Session *session;
2131 //const char * tempmsg;
2132 const struct GNUNET_MessageHeader *temp_hdr;
2133 struct GNUNET_PeerIdentity tmpsource;
2136 if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_DATA)
2140 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2141 "Func wlan_data_message_handler got GNUNET_MESSAGE_TYPE_WLAN_DATA size: %u\n",
2145 if (ntohs (hdr->size) <
2146 sizeof (struct WlanHeader) + sizeof (struct GNUNET_MessageHeader))
2148 //packet not big enought
2152 wlanheader = (struct WlanHeader *) hdr;
2154 session = search_session (plugin, endpoint, &wlanheader->source);
2156 //tempmsg = (char*) &wlanheader[1];
2157 temp_hdr = (const struct GNUNET_MessageHeader *) &wlanheader[1];
2158 crc = ntohl (wlanheader->crc);
2159 wlanheader->crc = 0;
2160 if (getcrc32 ((char *) wlanheader, ntohs (wlanheader->header.size)) != crc)
2162 //wrong crc, dispose message
2163 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2164 "Wlan message header crc was wrong: %u != %u\n",
2165 getcrc32 ((char *) wlanheader,
2166 ntohs (wlanheader->header.size)), crc);
2167 hexdump ((void *) hdr, ntohs (hdr->size));
2171 //if not in session list
2172 if (session == NULL)
2175 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2176 "WLAN client not in session list: packet size = %u, inner size = %u, header size = %u\n",
2177 ntohs (wlanheader->header.size), ntohs (temp_hdr->size),
2178 sizeof (struct WlanHeader));
2180 //try if it is a hello message
2181 if (ntohs (wlanheader->header.size) >=
2182 ntohs (temp_hdr->size) + sizeof (struct WlanHeader))
2184 if (ntohs (temp_hdr->type) == GNUNET_MESSAGE_TYPE_HELLO)
2186 if (GNUNET_HELLO_get_id
2187 ((const struct GNUNET_HELLO_Message *) temp_hdr,
2188 &tmpsource) == GNUNET_OK)
2190 session = create_session (plugin, endpoint, &tmpsource);
2194 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2195 "WLAN client not in session list and hello message is not okay\n");
2202 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2203 "WLAN client not in session list and not a hello message\n");
2209 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2210 "WLAN client not in session list and message size in does not fit\npacket size = %u, inner size = %u, header size = %u\n",
2211 ntohs (wlanheader->header.size),
2212 ntohs (temp_hdr->size), sizeof (struct WlanHeader));
2217 //"receive" the message
2220 (&wlanheader->source, &session->target,
2221 sizeof (struct GNUNET_PeerIdentity)) != 0)
2225 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2226 "WLAN peer source id doesn't match packet peer source id: session %p\n",
2233 (&wlanheader->target, plugin->env->my_identity,
2234 sizeof (struct GNUNET_PeerIdentity)) != 0)
2238 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2239 "WLAN peer target id doesn't match our peer id: session %p\n",
2245 GNUNET_SERVER_mst_receive (plugin->data_tokenizer, session,
2246 (const char *) temp_hdr,
2247 ntohs (hdr->size) - sizeof (struct WlanHeader),
2248 GNUNET_YES, GNUNET_NO);
2254 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2255 "wlan_data_message_handler got wrong message type: %u\n",
2262 * function to process the a message, give it to the higher layer
2263 * @param cls pointer to the plugin
2264 * @param client pointer to the session this message belongs to
2265 * @param hdr start of the message
2267 //TODO ATS informations
2269 process_data (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
2272 GNUNET_assert (client != NULL);
2273 GNUNET_assert (cls != NULL);
2274 struct Session *session = (struct Session *) client;
2275 struct Plugin *plugin = (struct Plugin *) cls;
2277 struct GNUNET_TRANSPORT_ATS_Information distance[2];
2279 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2280 distance[0].value = htonl (1);
2281 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2282 distance[1].value = htonl (0);
2285 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2286 "Calling plugin->env->receive for session %p; %s; size: %u\n",
2287 session, wlan_plugin_address_to_string (NULL,
2293 plugin->env->receive (plugin->env->cls, &(session->target), hdr,
2294 (const struct GNUNET_TRANSPORT_ATS_Information *)
2295 &distance, 2, session,
2296 (const char *) &session->mac->addr,
2297 sizeof (session->mac->addr));
2301 * Function used for to process the data received from the wlan interface
2303 * @param cls the plugin handle
2304 * @param session_light pointer to the struct holding known informations
2305 * @param hdr hdr of the GNUNET_MessageHeader
2306 * @param rxinfo pointer to the radiotap informations got with this packet
2309 wlan_data_helper (void *cls, struct Session_light *session_light,
2310 const struct GNUNET_MessageHeader *hdr,
2311 const struct Radiotap_rx *rxinfo)
2313 struct Plugin *plugin = cls;
2314 struct FragmentMessage *fm;
2315 struct FragmentMessage *fm2;
2318 if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_HELLO)
2321 //TODO better DOS protection, error handling
2322 //TODO test first than create session
2323 GNUNET_assert (session_light != NULL);
2326 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2327 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT size: %u; %s\n",
2328 ntohs (hdr->size), wlan_plugin_address_to_string (NULL,
2329 session_light->addr.
2333 plugin->env->receive(NULL,NULL,hdr, NULL, 0, NULL, NULL, 0);
2338 else if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_FRAGMENT)
2341 GNUNET_assert (session_light != NULL);
2342 if (session_light->macendpoint == NULL)
2344 session_light->macendpoint =
2345 get_macendpoint (plugin, &session_light->addr, GNUNET_YES);
2349 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2350 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_FRAGMENT with size: %u; mac endpoint %p: %s\n",
2351 ntohs (hdr->size), session_light->macendpoint,
2352 wlan_plugin_address_to_string (NULL,
2353 session_light->addr.mac,
2358 GNUNET_DEFRAGMENT_process_fragment (session_light->macendpoint->defrag,
2361 if (ret == GNUNET_NO)
2363 session_light->macendpoint->dups++;
2365 else if (ret == GNUNET_OK)
2367 session_light->macendpoint->fragc++;
2369 set_next_send (plugin);
2375 else if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_FRAGMENT_ACK)
2377 GNUNET_assert (session_light != NULL);
2378 if (session_light->macendpoint == NULL)
2380 session_light->macendpoint =
2381 get_macendpoint (plugin, &session_light->addr, GNUNET_NO);
2384 if (session_light->macendpoint == NULL)
2387 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2388 "Macendpoint does not exist for this GNUNET_MESSAGE_TYPE_FRAGMENT_ACK size: %u; %s\n",
2389 ntohs (hdr->size), wlan_plugin_address_to_string (NULL,
2390 session_light->addr.mac,
2397 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2398 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_FRAGMENT_ACK size: %u; mac endpoint: %p; %s\n",
2399 ntohs (hdr->size), session_light->macendpoint,
2400 wlan_plugin_address_to_string (NULL,
2401 session_light->addr.mac,
2404 fm = session_light->macendpoint->sending_messages_head;
2408 int ret = GNUNET_FRAGMENT_process_ack (fm->fragcontext, hdr);
2410 if (ret == GNUNET_OK)
2412 #if DEBUG_wlan_retransmission
2413 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2414 "Got last ack, finished fragment message %p\n", fm);
2416 session_light->macendpoint->acks++;
2417 fm->session->last_activity = GNUNET_TIME_absolute_get ();
2418 session_light->macendpoint->last_activity = fm->session->last_activity;
2419 free_fragment_message (plugin, fm);
2420 check_fragment_queue (plugin);
2423 if (ret == GNUNET_NO)
2425 #if DEBUG_wlan_retransmission
2426 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2427 "Got ack for: %p\n", fm);
2429 session_light->macendpoint->acks++;
2432 if (ret == GNUNET_SYSERR)
2440 #if DEBUG_wlan_retransmission
2441 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2442 "WLAN fragment not in fragment list\n");
2450 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2451 "WLAN packet inside the WLAN helper packet has not the right type: %u size: %u\n",
2452 ntohs (hdr->type), ntohs (hdr->size));
2458 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2459 "Helper finished\n");
2465 * Function to print mac addresses nice *
2466 * @param pointer to 6 byte with the mac address
2467 * @return pointer to the chars which hold the print out
2470 macprinter (const u_int8_t * mac)
2472 static char macstr[20];
2474 GNUNET_snprintf (macstr, sizeof (macstr), "%X:%X:%X:%X:%X:%X", mac[0], mac[1],
2475 mac[2], mac[3], mac[4], mac[5]);
2480 * Function for the scheduler if a mac endpoint times out
2481 * @param cls pointer to the MacEndpoint
2482 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
2485 macendpoint_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2487 struct MacEndpoint *endpoint = cls;
2489 GNUNET_assert (endpoint != NULL);
2490 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2491 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2495 if (GNUNET_TIME_absolute_get_remaining
2496 (GNUNET_TIME_absolute_add
2497 (endpoint->last_activity, MACENDPOINT_TIMEOUT)).rel_value == 0)
2499 free_macendpoint (endpoint->plugin, endpoint);
2503 endpoint->timeout_task =
2504 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
2510 * function to create an macendpoint
2511 * @param plugin pointer to the plugin struct
2512 * @param addr pointer to the macaddress
2513 * @return returns a macendpoint
2515 static struct MacEndpoint *
2516 create_macendpoint (struct Plugin *plugin, const struct MacAddress *addr)
2518 struct MacEndpoint *newend = GNUNET_malloc (sizeof (struct MacEndpoint));
2520 newend->addr = *addr;
2521 newend->plugin = plugin;
2522 newend->addr = *addr;
2523 newend->fragment_messages_out_count = 0;
2525 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
2526 MESSAGES_IN_DEFRAG_QUEUE_PER_MAC,
2527 newend, &wlan_data_message_handler,
2529 newend->last_activity = GNUNET_TIME_absolute_get ();
2530 newend->timeout_task =
2531 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
2534 plugin->mac_count++;
2535 GNUNET_CONTAINER_DLL_insert_tail (plugin->mac_head, plugin->mac_tail, newend);
2537 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2538 "New Mac Endpoint %p: %s\n", newend,
2539 wlan_plugin_address_to_string (NULL, newend->addr.mac, 6));
2545 * Function used for to process the data from the suid process
2547 * @param cls the plugin handle
2548 * @param client client that send the data (not used)
2549 * @param hdr header of the GNUNET_MessageHeader
2552 wlan_process_helper (void *cls, void *client,
2553 const struct GNUNET_MessageHeader *hdr)
2555 struct Plugin *plugin = cls;
2556 struct ieee80211_frame *wlanIeeeHeader = NULL;
2557 struct Session_light *session_light = NULL;
2558 struct Radiotap_rx *rxinfo;
2559 const struct GNUNET_MessageHeader *temp_hdr = NULL;
2564 switch (ntohs (hdr->type))
2566 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA:
2568 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2569 "Func wlan_process_helper got GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA size: %u\n",
2573 //call wlan_process_helper with the message inside, later with wlan: analyze signal
2574 if (ntohs (hdr->size) <
2575 sizeof (struct ieee80211_frame) + 2*sizeof (struct GNUNET_MessageHeader) +
2576 sizeof (struct Radiotap_rx))
2579 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2580 "Size of packet is too small; size: %u min size: %u\n",
2582 sizeof (struct ieee80211_frame) +
2583 sizeof (struct GNUNET_MessageHeader));
2586 /* FIXME: restart SUID process */
2590 rxinfo = (struct Radiotap_rx *) &hdr[1];
2591 wlanIeeeHeader = (struct ieee80211_frame *) &rxinfo[1];
2593 //process only if it is an broadcast or for this computer both with the gnunet bssid
2597 (&(wlanIeeeHeader->i_addr3), &mac_bssid,
2598 sizeof (struct MacAddress)) == 0)
2600 //check for broadcast or mac
2602 (&(wlanIeeeHeader->i_addr1), &bc_all_mac,
2603 sizeof (struct MacAddress)) == 0) ||
2604 (memcmp (&(wlanIeeeHeader->i_addr1), &(plugin->mac_address),
2605 sizeof (struct MacAddress)) == 0))
2607 //if packet is from us return
2608 if ((memcmp (&(wlanIeeeHeader->i_addr2), &(plugin->mac_address),
2609 sizeof (struct MacAddress)) == 0)){
2612 // process the inner data
2616 ntohs (hdr->size) - sizeof (struct ieee80211_frame) -
2617 sizeof (struct GNUNET_MessageHeader) - sizeof (struct Radiotap_rx);
2619 session_light = GNUNET_malloc (sizeof (struct Session_light));
2620 memcpy (&session_light->addr, &(wlanIeeeHeader->i_addr2),
2621 sizeof (struct MacAddress));
2622 //session_light->session = search_session(plugin,session_light->addr);
2625 while (pos < datasize)
2627 temp_hdr = (struct GNUNET_MessageHeader *) &wlanIeeeHeader[1] + pos;
2628 if (ntohs(temp_hdr->size) <= datasize + pos)
2630 wlan_data_helper (plugin, session_light, temp_hdr, rxinfo);
2635 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2636 "Size of packet is too small; size: %u > size of packet: %u\n",
2637 ntohs(temp_hdr->size),datasize + pos);
2640 pos += ntohs (temp_hdr->size);
2645 GNUNET_free (session_light);
2650 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2651 "Func wlan_process_helper got wrong MAC: %s\n",
2652 macprinter (wlanIeeeHeader->i_addr1));
2659 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2660 "Func wlan_process_helper got wrong BSSID: %s\n",
2661 macprinter (wlanIeeeHeader->i_addr2));
2665 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
2666 //TODO more control messages
2667 if (ntohs (hdr->size) != sizeof (struct Wlan_Helper_Control_Message))
2670 /* FIXME: restart SUID process */
2673 memcpy (&plugin->mac_address, &hdr[1], sizeof (struct MacAddress));
2675 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2676 "Received WLAN_HELPER_CONTROL message with transport of address %s\n",
2677 wlan_plugin_address_to_string (cls, &plugin->mac_address,
2681 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2682 &plugin->mac_address,
2683 sizeof (struct MacAddress));
2687 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2688 "Func wlan_process_helper got unknown message with number %u, size %u\n",
2689 ntohs (hdr->type), ntohs (hdr->size));
2692 #if DEBUG_wlan_msg_dump
2693 hexdump (hdr, GNUNET_MIN (ntohs (hdr->size), 256));
2701 * We have been notified that wlan-helper has written something to stdout.
2702 * Handle the output, then reschedule this function to be called again once
2703 * more is available.
2705 * @param cls the plugin handle
2706 * @param tc the scheduling context
2709 wlan_plugin_helper_read (void *cls,
2710 const struct GNUNET_SCHEDULER_TaskContext *tc)
2712 struct Plugin *plugin = cls;
2714 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2716 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2719 char mybuf[WLAN_MTU + sizeof (struct GNUNET_MessageHeader)];
2723 GNUNET_DISK_file_read (plugin->server_stdout_handle, mybuf,
2728 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2730 ("Finished reading from wlan-helper stdout with code: %d\n"),
2735 GNUNET_SERVER_mst_receive (plugin->suid_tokenizer, NULL, mybuf, bytes,
2736 GNUNET_NO, GNUNET_NO);
2738 GNUNET_assert (plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2739 plugin->server_read_task =
2740 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2741 plugin->server_stdout_handle,
2742 &wlan_plugin_helper_read, plugin);
2746 * Start the gnunet-wlan-helper process.
2748 * @param plugin the transport plugin
2749 * @param testmode should we use the dummy driver for testing?
2750 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
2753 wlan_transport_start_wlan_helper (struct Plugin *plugin, int testmode)
2755 const char *filenamehw = "gnunet-transport-wlan-helper";
2756 const char *filenameloopback = "gnunet-transport-wlan-helper-dummy";
2758 plugin->server_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
2759 if (plugin->server_stdout == NULL)
2760 return GNUNET_SYSERR;
2762 plugin->server_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);
2763 if (plugin->server_stdin == NULL)
2764 return GNUNET_SYSERR;
2766 /* Start the server process */
2772 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2773 "Starting gnunet-wlan-helper process cmd: %s %s %i\n",
2774 filenamehw, plugin->interface, testmode);
2777 if (GNUNET_OS_check_helper_binary (filenamehw) == GNUNET_YES)
2779 plugin->server_proc =
2780 GNUNET_OS_start_process (plugin->server_stdin, plugin->server_stdout,
2781 filenamehw, filenamehw, plugin->interface,
2784 else if (GNUNET_OS_check_helper_binary (filenamehw) == GNUNET_NO)
2786 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
2787 "gnunet-transport-wlan-helper is not suid, please change it or look at the doku\n");
2792 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
2793 "gnunet-transport-wlan-helper not found, please look if it exists and is the $PATH variable!\n");
2798 else if (testmode == 1)
2802 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2803 "Starting gnunet-wlan-helper loopback 1 process cmd: %s %s %i\n",
2804 filenameloopback, plugin->interface, testmode);
2807 if (GNUNET_OS_check_helper_binary (filenameloopback) != GNUNET_SYSERR)
2809 plugin->server_proc =
2810 GNUNET_OS_start_process (plugin->server_stdin, plugin->server_stdout,
2811 filenameloopback, filenameloopback, "1",
2816 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
2817 "gnunet-transport-wlan-helper-dummy not found, please look if it exists and is the $PATH variable!\n");
2821 else if (testmode == 2)
2824 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2825 "Starting gnunet-wlan-helper loopback 2 process cmd: %s %s %i\n",
2826 filenameloopback, plugin->interface, testmode);
2828 if (GNUNET_OS_check_helper_binary (filenameloopback) != GNUNET_SYSERR)
2830 plugin->server_proc =
2831 GNUNET_OS_start_process (plugin->server_stdin, plugin->server_stdout,
2832 filenameloopback, filenameloopback, "2",
2837 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
2838 "gnunet-transport-wlan-helper-dummy not found, please look if it exists and is in the $PATH variable!\n");
2842 if (plugin->server_proc == NULL)
2845 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2846 "Failed to start gnunet-wlan-helper process\n");
2848 return GNUNET_SYSERR;
2851 /* Close the write end of the read pipe */
2852 GNUNET_DISK_pipe_close_end (plugin->server_stdout,
2853 GNUNET_DISK_PIPE_END_WRITE);
2855 /* Close the read end of the write pipe */
2856 GNUNET_DISK_pipe_close_end (plugin->server_stdin, GNUNET_DISK_PIPE_END_READ);
2858 plugin->server_stdout_handle =
2859 GNUNET_DISK_pipe_handle (plugin->server_stdout,
2860 GNUNET_DISK_PIPE_END_READ);
2861 plugin->server_stdin_handle =
2862 GNUNET_DISK_pipe_handle (plugin->server_stdin,
2863 GNUNET_DISK_PIPE_END_WRITE);
2865 GNUNET_assert (plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2868 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2869 "Adding server_read_task for the wlan-helper\n");
2872 plugin->server_read_task =
2873 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2874 plugin->server_stdout_handle,
2875 &wlan_plugin_helper_read, plugin);
2881 * Exit point from the plugin.
2882 * @param cls pointer to the api struct
2887 libgnunet_plugin_transport_wlan_done (void *cls)
2889 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2890 struct Plugin *plugin = api->cls;
2891 struct MacEndpoint *endpoint = plugin->mac_head;
2892 struct MacEndpoint *endpoint_next;
2895 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2896 "libgnunet_plugin_transport_wlan_done started\n");
2899 GNUNET_DISK_pipe_close (plugin->server_stdout);
2900 GNUNET_DISK_pipe_close (plugin->server_stdin);
2901 GNUNET_OS_process_kill (plugin->server_proc, 9);
2902 GNUNET_OS_process_close (plugin->server_proc);
2904 GNUNET_assert (cls != NULL);
2906 while (endpoint != NULL)
2908 endpoint_next = endpoint->next;
2909 free_macendpoint (plugin, endpoint);
2910 endpoint = endpoint_next;
2913 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
2915 GNUNET_SCHEDULER_cancel (plugin->server_write_delay_task);
2916 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
2918 if (plugin->server_write_task != GNUNET_SCHEDULER_NO_TASK)
2920 GNUNET_SCHEDULER_cancel (plugin->server_write_task);
2921 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
2923 if (plugin->server_read_task != GNUNET_SCHEDULER_NO_TASK)
2925 GNUNET_SCHEDULER_cancel (plugin->server_read_task);
2926 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2929 if (plugin->suid_tokenizer != NULL)
2930 GNUNET_SERVER_mst_destroy (plugin->suid_tokenizer);
2932 if (plugin->data_tokenizer != NULL)
2933 GNUNET_SERVER_mst_destroy (plugin->data_tokenizer);
2935 GNUNET_free_non_null (plugin->interface);
2936 GNUNET_free (plugin);
2942 * Entry point for the plugin.
2944 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2945 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2948 libgnunet_plugin_transport_wlan_init (void *cls)
2950 //struct GNUNET_SERVICE_Context *service;
2951 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2952 struct GNUNET_TRANSPORT_PluginFunctions *api;
2953 struct Plugin *plugin;
2954 static unsigned long long testmode = 0;
2956 GNUNET_assert (cls != NULL);
2958 plugin = GNUNET_malloc (sizeof (struct Plugin));
2960 plugin->pendingsessions = 0;
2961 plugin->mac_count = 0;
2962 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
2963 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2964 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
2965 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
2966 GNUNET_BANDWIDTH_value_init (100 * 1024 *
2969 plugin->suid_tokenizer =
2970 GNUNET_SERVER_mst_create (&wlan_process_helper, plugin);
2972 plugin->data_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
2974 //plugin->sessions = GNUNET_malloc (sizeof (struct Sessionqueue));
2975 //plugin->pending_Sessions_head = GNUNET_malloc (sizeof (struct Sessionqueue));
2977 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2979 api->send = &wlan_plugin_send;
2980 api->disconnect = &wlan_plugin_disconnect;
2981 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2982 api->check_address = &wlan_plugin_address_suggested;
2983 api->address_to_string = &wlan_plugin_address_to_string;
2986 if (GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "TESTMODE"))
2988 if (GNUNET_SYSERR ==
2989 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-wlan",
2990 "TESTMODE", &testmode))
2991 testmode = 0; //default value
2994 if (GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "INTERFACE"))
2996 if (GNUNET_CONFIGURATION_get_value_string
2997 (env->cfg, "transport-wlan", "INTERFACE",
2998 &(plugin->interface)) != GNUNET_YES)
3000 libgnunet_plugin_transport_wlan_done (api);
3006 wlan_transport_start_wlan_helper (plugin, testmode);
3007 set_next_beacon_time (plugin);
3008 set_next_send(plugin);
3010 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
3011 "wlan init finished\n");
3017 /* end of plugin_transport_wlan.c */