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]);
821 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
822 "Func wlan_plugin_address_to_string made string: %s\n",
829 * Function for the scheduler if a session times out
830 * @param cls pointer to the Sessionqueue
831 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
834 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
836 struct Sessionqueue *queue = cls;
838 GNUNET_assert (queue != NULL);
839 queue->content->timeout_task = GNUNET_SCHEDULER_NO_TASK;
840 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
844 if (GNUNET_TIME_absolute_get_remaining
845 (GNUNET_TIME_absolute_add
846 (queue->content->last_activity, SESSION_TIMEOUT)).rel_value == 0)
848 free_session (queue->content->mac->plugin, queue, GNUNET_YES);
852 queue->content->timeout_task =
853 GNUNET_SCHEDULER_add_delayed (SESSION_TIMEOUT, &session_timeout, queue);
858 * create a new session
860 * @param plugin pointer to the plugin struct
861 * @param endpoint pointer to the mac endpoint of the peer
862 * @param peer peer identity to use for this session
863 * @return returns the session
866 static struct Session *
867 create_session (struct Plugin *plugin, struct MacEndpoint *endpoint,
868 const struct GNUNET_PeerIdentity *peer)
870 GNUNET_assert (endpoint != NULL);
871 struct Sessionqueue *queue =
872 GNUNET_malloc (sizeof (struct Sessionqueue) + sizeof (struct Session));
874 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
875 endpoint->sessions_tail, queue);
877 queue->content = (struct Session *) &queue[1];
878 queue->content->mac = endpoint;
879 memcpy (&(queue->content->target), peer, sizeof (struct GNUNET_PeerIdentity));
880 queue->content->last_activity = GNUNET_TIME_absolute_get ();
881 queue->content->timeout_task =
882 GNUNET_SCHEDULER_add_delayed (SESSION_TIMEOUT, &session_timeout, queue);
885 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
886 "New session %p with endpoint %p: %s\n", queue->content,
887 endpoint, wlan_plugin_address_to_string (NULL,
892 return queue->content;
896 * Get session from address, create if no session exists
898 * @param plugin pointer to the plugin struct
899 * @param addr pointer to the mac address of the peer
900 * @param peer pointer to the peerid
901 * @return returns the session
903 static struct Session *
904 get_session (struct Plugin *plugin, const struct MacAddress *addr,
905 const struct GNUNET_PeerIdentity *peer)
907 struct MacEndpoint *mac;
909 mac = get_macendpoint (plugin, addr, GNUNET_YES);
910 struct Session *session = search_session (plugin, mac, peer);
914 return create_session (plugin, mac, peer);
918 * Queue the session to send data
919 * checks if there is a message pending
920 * checks if this session is not allready in the queue
921 * @param plugin pointer to the plugin
922 * @param session pointer to the session to add
925 queue_session (struct Plugin *plugin, struct Session *session)
927 struct Sessionqueue *queue = plugin->pending_Sessions_head;
929 if (session->pending_message_head != NULL)
931 while (queue != NULL)
933 // content is never NULL
934 GNUNET_assert (queue->content != NULL);
935 // is session already in queue?
936 if (session == queue->content)
944 // Session is not in the queue
946 queue = GNUNET_malloc (sizeof (struct Sessionqueue));
947 queue->content = session;
950 GNUNET_CONTAINER_DLL_insert_tail (plugin->pending_Sessions_head,
951 plugin->pending_Sessions_tail, queue);
952 plugin->pendingsessions++;
958 * Function to schedule the write task, executed after a delay
959 * @param cls pointer to the plugin struct
960 * @param tc GNUNET_SCHEDULER_TaskContext pointer
963 delay_fragment_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
965 struct Plugin *plugin = cls;
967 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
969 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
972 // GNUNET_TIME_UNIT_FOREVER_REL is needed to clean up old msg
973 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
975 plugin->server_write_task =
976 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
977 plugin->server_stdin_handle,
978 &do_transmit, plugin);
983 * Function to calculate the time of the next periodic "hello-beacon"
984 * @param plugin pointer to the plugin struct
988 set_next_beacon_time (struct Plugin *const plugin)
990 //under 10 known peers: once a second
991 if (plugin->mac_count < 10)
993 plugin->beacon_time =
994 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
995 GNUNET_TIME_relative_multiply
996 (GNUNET_TIME_UNIT_SECONDS,
997 HALLO_BEACON_SCALING_FACTOR));
999 //under 30 known peers: every 10 seconds
1000 else if (plugin->mac_count < 30)
1002 plugin->beacon_time =
1003 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1004 GNUNET_TIME_relative_multiply
1005 (GNUNET_TIME_UNIT_SECONDS,
1006 10 * HALLO_BEACON_SCALING_FACTOR));
1008 //over 30 known peers: once a minute
1011 plugin->beacon_time =
1012 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1013 GNUNET_TIME_relative_multiply
1014 (GNUNET_TIME_UNIT_MINUTES,
1015 HALLO_BEACON_SCALING_FACTOR));
1020 * Function to set the timer for the next timeout of the fragment queue
1021 * @param plugin the handle to the plugin struct
1025 set_next_send (struct Plugin *const plugin)
1027 struct GNUNET_TIME_Relative next_send;
1030 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
1032 GNUNET_SCHEDULER_cancel (plugin->server_write_delay_task);
1033 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
1036 //check if some acks are in the queue
1037 if (plugin->ack_send_queue_head != NULL)
1039 next_send = GNUNET_TIME_UNIT_ZERO;
1042 //check if there are some fragments in the queue
1043 else if (plugin->sending_messages_head != NULL)
1045 next_send = GNUNET_TIME_UNIT_ZERO;
1049 next_send = GNUNET_TIME_absolute_get_remaining (plugin->beacon_time);
1053 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1054 "Next packet is send in: %u\n", next_send.rel_value);
1057 if (next_send.rel_value == GNUNET_TIME_UNIT_ZERO.rel_value)
1059 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
1061 plugin->server_write_task =
1062 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1063 plugin->server_stdin_handle,
1064 &do_transmit, plugin);
1069 if (plugin->server_write_delay_task == GNUNET_SCHEDULER_NO_TASK)
1071 plugin->server_write_delay_task =
1072 GNUNET_SCHEDULER_add_delayed (next_send, &delay_fragment_task,
1079 * Function to get the next queued Session, removes the session from the queue
1080 * @param plugin pointer to the plugin struct
1081 * @return pointer to the session found, returns NULL if there is now session in the queue
1083 static struct Session *
1084 get_next_queue_session (struct Plugin *plugin)
1086 struct Session *session;
1087 struct Sessionqueue *sessionqueue;
1088 struct Sessionqueue *sessionqueue_alt;
1089 struct PendingMessage *pm;
1091 sessionqueue = plugin->pending_Sessions_head;
1093 while (sessionqueue != NULL)
1095 session = sessionqueue->content;
1097 GNUNET_assert (session != NULL);
1098 pm = session->pending_message_head;
1103 GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1104 "pending message is empty, should not happen. session %p\n",
1107 sessionqueue_alt = sessionqueue;
1108 sessionqueue = sessionqueue->next;
1109 plugin->pendingsessions--;
1110 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1111 plugin->pending_Sessions_tail,
1114 GNUNET_free (sessionqueue_alt);
1119 //check for message timeout
1120 if (GNUNET_TIME_absolute_get_remaining (pm->timeout).rel_value > 0)
1122 //check if session has no message in the fragment queue
1123 if ((session->mac->fragment_messages_out_count <
1124 FRAGMENT_QUEUE_MESSAGES_OUT_PER_MACENDPOINT) &&
1125 (session->fragment_messages_out_count <
1126 FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION))
1128 plugin->pendingsessions--;
1129 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1130 plugin->pending_Sessions_tail,
1132 GNUNET_free (sessionqueue);
1138 sessionqueue = sessionqueue->next;
1143 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
1144 session->pending_message_tail, pm);
1146 //call the cont func that it did not work
1147 if (pm->transmit_cont != NULL)
1148 pm->transmit_cont (pm->transmit_cont_cls, &(session->target),
1150 GNUNET_free (pm->msg);
1153 if (session->pending_message_head == NULL)
1155 sessionqueue_alt = sessionqueue;
1156 sessionqueue = sessionqueue->next;
1157 plugin->pendingsessions--;
1158 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1159 plugin->pending_Sessions_tail,
1162 GNUNET_free (sessionqueue_alt);
1171 * frees the space of a message in the fragment queue (send queue)
1172 * @param plugin the plugin struct
1173 * @param fm message to free
1176 free_fragment_message (struct Plugin *plugin, struct FragmentMessage *fm)
1178 struct Session *session = fm->session;
1179 struct MacEndpoint *endpoint = session->mac;
1180 struct FragmentMessage_queue *fmq;
1181 struct FragmentMessage_queue *fmq_next;
1185 fmq = plugin->sending_messages_head;
1188 fmq_next = fmq->next;
1189 if (fmq->content == fm)
1191 GNUNET_CONTAINER_DLL_remove (plugin->sending_messages_head,
1192 plugin->sending_messages_tail, fmq);
1198 (session->mac->fragment_messages_out_count)--;
1199 session->fragment_messages_out_count--;
1200 plugin->pending_Fragment_Messages--;
1201 GNUNET_CONTAINER_DLL_remove (endpoint->sending_messages_head,
1202 endpoint->sending_messages_tail, fm);
1203 GNUNET_FRAGMENT_context_destroy (fm->fragcontext);
1204 if (fm->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1205 GNUNET_SCHEDULER_cancel (fm->timeout_task);
1208 queue_session (plugin, session);
1210 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1211 "Free pending fragment messages %p, session %p\n", fm,
1218 * function to fill the radiotap header
1219 * @param plugin pointer to the plugin struct
1220 * @param endpoint pointer to the endpoint
1221 * @param header pointer to the radiotap header
1222 * @return GNUNET_YES at success
1225 getRadiotapHeader (struct Plugin *plugin, struct MacEndpoint *endpoint,
1226 struct Radiotap_Send *header)
1229 if (endpoint != NULL)
1231 header->rate = endpoint->rate;
1232 header->tx_power = endpoint->tx_power;
1233 header->antenna = endpoint->antenna;
1238 header->tx_power = 0;
1239 header->antenna = 0;
1246 * function to generate the wlan hardware header for one packet
1247 * @param Header address to write the header to
1248 * @param to_mac_addr address of the recipient
1249 * @param plugin pointer to the plugin struct
1250 * @param size size of the whole packet, needed to calculate the time to send the packet
1251 * @return GNUNET_YES if there was no error
1254 getWlanHeader (struct ieee80211_frame *Header,
1255 const struct MacAddress *to_mac_addr, struct Plugin *plugin,
1259 const int rate = 11000000;
1261 Header->i_fc[0] = IEEE80211_FC0_TYPE_DATA;
1262 Header->i_fc[1] = 0x00;
1263 memcpy (&Header->i_addr3, &mac_bssid, sizeof (mac_bssid));
1264 memcpy (&Header->i_addr2, plugin->mac_address.mac,
1265 sizeof (plugin->mac_address));
1266 memcpy (&Header->i_addr1, to_mac_addr, sizeof (struct MacAddress));
1268 tmp16 = (uint16_t *) Header->i_dur;
1269 *tmp16 = (uint16_t) htole16 ((size * 1000000) / rate + 290);
1270 Header->llc[0] = WLAN_LLC_DSAP_FIELD;
1271 Header->llc[1] = WLAN_LLC_SSAP_FIELD;
1273 #if DEBUG_wlan_ip_udp_packets_on_air
1278 Header->ip.ip_dst.s_addr = *((uint32_t *) & to_mac_addr->mac[2]);
1279 Header->ip.ip_src.s_addr = *((uint32_t *) & plugin->mac_address.mac[2]);
1280 Header->ip.ip_v = 4;
1281 Header->ip.ip_hl = 5;
1282 Header->ip.ip_p = 17;
1283 Header->ip.ip_ttl = 1;
1284 Header->ip.ip_len = htons (size + 8);
1285 Header->ip.ip_sum = 0;
1286 x = (uint16_t *) & Header->ip;
1287 count = sizeof (struct iph);
1290 /* This is the inner loop */
1291 crc += (unsigned short) *x++;
1294 /* Add left-over byte, if any */
1296 crc += *(unsigned char *) x;
1297 crc = (crc & 0xffff) + (crc >> 16);
1298 Header->ip.ip_sum = htons (~(unsigned short) crc);
1299 Header->udp.len = htons (size - sizeof (struct ieee80211_frame));
1309 * @param msgbuf pointer tor the data
1310 * @param msgbuf_size size of the data
1312 * @return 32bit crc value
1316 getcrc32 (const char *msgbuf, size_t msgbuf_size)
1319 return GNUNET_CRYPTO_crc32_n (msgbuf, msgbuf_size);;
1325 * @param msgbuf pointer tor the data
1326 * @param msgbuf_size size of the data
1328 * @return 16bit crc value
1332 getcrc16 (const char *msgbuf, size_t msgbuf_size)
1334 //TODO calc some crc
1339 * function to add a fragment of a message to send
1340 * @param cls FragmentMessage this message belongs to
1341 * @param hdr pointer to the start of the message
1345 add_message_for_send (void *cls, const struct GNUNET_MessageHeader *hdr)
1348 struct FragmentMessage *fm = cls;
1349 struct FragmentMessage_queue *fmqueue;
1351 GNUNET_assert (cls != NULL);
1352 GNUNET_assert (fm->frag == NULL);
1353 struct MacEndpoint *endpoint = fm->session->mac;
1354 struct Plugin *plugin = endpoint->plugin;
1355 struct GNUNET_MessageHeader *msgheader;
1356 struct GNUNET_MessageHeader *msgheader2;
1359 #if DEBUG_wlan_retransmission
1360 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1361 "Adding fragment of message %p to send, session %p, endpoint %p, type %u\n",
1362 fm, fm->session, endpoint, hdr->type);
1366 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1367 sizeof (struct ieee80211_frame) + ntohs (hdr->size);
1368 fm->frag = GNUNET_malloc (size);
1371 msgheader = (struct GNUNET_MessageHeader *) fm->frag;
1372 msgheader->size = htons (size);
1373 msgheader->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1375 fm->radioHeader = (struct Radiotap_Send *) &msgheader[1];
1376 fm->ieeewlanheader = (struct ieee80211_frame *) &fm->radioHeader[1];
1377 msgheader2 = (struct GNUNET_MessageHeader *) &fm->ieeewlanheader[1];
1378 memcpy (msgheader2, hdr, ntohs (hdr->size));
1380 fmqueue = GNUNET_malloc (sizeof (struct FragmentMessage_queue));
1381 fmqueue->content = fm;
1383 GNUNET_CONTAINER_DLL_insert_tail (plugin->sending_messages_head,
1384 plugin->sending_messages_tail, fmqueue);
1385 set_next_send (plugin);
1389 * function to send a hallo beacon
1390 * @param plugin pointer to the plugin struct
1393 send_hello_beacon (struct Plugin *plugin)
1397 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1398 "Sending hello beacon\n");
1403 uint16_t hallo_size;
1404 struct GNUNET_MessageHeader *msgheader;
1405 struct ieee80211_frame *ieeewlanheader;
1406 struct Radiotap_Send *radioHeader;
1407 struct GNUNET_MessageHeader *msgheader2;
1408 const struct GNUNET_MessageHeader *hello;
1410 hello = plugin->env->get_our_hello ();
1411 hallo_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1412 GNUNET_assert (sizeof (struct WlanHeader) + hallo_size <= WLAN_MTU);
1414 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1415 sizeof (struct ieee80211_frame) + hallo_size;
1417 msgheader = GNUNET_malloc (size);
1418 msgheader->size = htons (size);
1419 msgheader->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1421 radioHeader = (struct Radiotap_Send *) &msgheader[1];
1422 getRadiotapHeader (plugin, NULL, radioHeader);
1423 ieeewlanheader = (struct ieee80211_frame *) &radioHeader[1];
1424 getWlanHeader (ieeewlanheader, &bc_all_mac, plugin, size);
1426 msgheader2 = (struct GNUNET_MessageHeader *) &ieeewlanheader[1];
1427 /*msgheader2->size =
1428 htons (GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello) +
1429 sizeof (struct GNUNET_MessageHeader));
1431 msgheader2->type = htons (GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT);*/
1432 memcpy (msgheader2, hello, hallo_size);
1434 bytes = GNUNET_DISK_file_write (plugin->server_stdin_handle, msgheader, size);
1436 if (bytes == GNUNET_SYSERR)
1438 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1440 ("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1441 errno, strerror (errno));
1444 GNUNET_assert (bytes != GNUNET_SYSERR);
1445 GNUNET_assert (bytes == size);
1446 GNUNET_free (msgheader);
1448 set_next_beacon_time (plugin);
1449 set_next_send (plugin);
1453 * function to add an ack to send it for a received fragment
1454 * @param cls MacEndpoint this ack belongs to
1455 * @param msg_id id of the message
1456 * @param hdr pointer to the hdr where the ack is stored
1461 add_ack_for_send (void *cls, uint32_t msg_id,
1462 const struct GNUNET_MessageHeader *hdr)
1465 struct AckSendQueue *ack;
1467 GNUNET_assert (cls != NULL);
1468 struct MacEndpoint *endpoint = cls;
1469 struct Plugin *plugin = endpoint->plugin;
1470 struct GNUNET_MessageHeader *msgheader;
1471 struct GNUNET_MessageHeader *msgheader2;
1475 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1476 sizeof (struct ieee80211_frame) + ntohs (hdr->size) +
1477 sizeof (struct AckSendQueue);
1479 ack = GNUNET_malloc (size);
1480 ack->message_id = msg_id;
1481 ack->endpoint = endpoint;
1484 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1485 sizeof (struct ieee80211_frame) + ntohs (hdr->size);
1487 msgheader = (struct GNUNET_MessageHeader *) &ack[1];
1488 ack->hdr = (struct GNUNET_MessageHeader *) &ack[1];
1489 msgheader->size = htons (size);
1490 msgheader->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1492 ack->radioHeader = (struct Radiotap_Send *) &msgheader[1];
1493 ack->ieeewlanheader = (struct ieee80211_frame *) &(ack->radioHeader)[1];
1494 msgheader2 = (struct GNUNET_MessageHeader *) &(ack->ieeewlanheader)[1];
1495 memcpy (msgheader2, hdr, ntohs (hdr->size));
1497 GNUNET_CONTAINER_DLL_insert_tail (plugin->ack_send_queue_head,
1498 plugin->ack_send_queue_tail, ack);
1500 #if DEBUG_wlan_retransmission
1501 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1502 "Adding ack with message id %u to send, AckSendQueue %p, endpoint %p\n",
1503 msg_id, ack, endpoint);
1506 set_next_send (plugin);
1510 * Function for the scheduler if a FragmentMessage times out
1511 * @param cls pointer to the FragmentMessage
1512 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
1515 fragmentmessage_timeout (void *cls,
1516 const struct GNUNET_SCHEDULER_TaskContext *tc)
1518 struct FragmentMessage *fm = cls;
1520 GNUNET_assert (fm != NULL);
1521 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1522 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1526 free_fragment_message (fm->session->mac->plugin, fm);
1530 * Function to check if there is some space in the fragment queue
1531 * inserts a message if space is available
1532 * @param plugin the plugin struct
1536 check_fragment_queue (struct Plugin *plugin)
1538 struct Session *session;
1539 struct FragmentMessage *fm;
1540 struct GNUNET_PeerIdentity pid;
1542 struct PendingMessage *pm;
1544 if (plugin->pending_Fragment_Messages < FRAGMENT_QUEUE_SIZE)
1546 session = get_next_queue_session (plugin);
1547 if (session != NULL)
1549 pm = session->pending_message_head;
1550 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
1551 session->pending_message_tail, pm);
1552 session->mac->fragment_messages_out_count++;
1553 session->fragment_messages_out_count++;
1554 plugin->pending_Fragment_Messages++;
1555 GNUNET_assert (pm != NULL);
1557 fm = GNUNET_malloc (sizeof (struct FragmentMessage));
1558 fm->session = session;
1559 fm->timeout.abs_value = pm->timeout.abs_value;
1562 GNUNET_FRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
1564 GNUNET_TIME_UNIT_SECONDS,
1566 &add_message_for_send, fm);
1568 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
1569 (fm->timeout), fragmentmessage_timeout,
1571 GNUNET_CONTAINER_DLL_insert_tail (session->mac->sending_messages_head,
1572 session->mac->sending_messages_tail,
1575 if (pm->transmit_cont != NULL)
1577 pid = session->target;
1578 pm->transmit_cont (pm->transmit_cont_cls, &pid, GNUNET_OK);
1580 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1581 "called pm->transmit_cont for %p\n", session);
1587 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1588 "no pm->transmit_cont for %p\n", session);
1593 if (session->pending_message_head != NULL)
1596 queue_session (plugin, session);
1602 //check if timeout changed
1603 set_next_send (plugin);
1607 * Function to send an ack, does not free the ack
1608 * @param plugin pointer to the plugin
1609 * @param ack pointer to the ack to send
1612 send_ack (struct Plugin *plugin, struct AckSendQueue *ack)
1618 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1619 "Sending ack for message_id %u for mac endpoint %p, size %u\n",
1620 ack->message_id, ack->endpoint,
1621 ntohs (ack->hdr->size) - sizeof (struct Radiotap_Send));
1624 getRadiotapHeader (plugin, ack->endpoint, ack->radioHeader);
1625 getWlanHeader (ack->ieeewlanheader, &ack->endpoint->addr, plugin,
1626 ntohs (ack->hdr->size));
1629 GNUNET_DISK_file_write (plugin->server_stdin_handle, ack->hdr,
1630 ntohs (ack->hdr->size));
1631 if (bytes == GNUNET_SYSERR)
1633 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1635 ("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1636 errno, strerror (errno));
1639 GNUNET_assert (bytes != GNUNET_SYSERR);
1640 GNUNET_assert (bytes == ntohs (ack->hdr->size));
1641 set_next_send (plugin);
1645 * function to finish a sending if not all could have been writen befor
1646 * @param cls pointer to the Finish_send struct
1647 * @param tc TaskContext
1650 finish_sending (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1652 struct Finish_send *finish = cls;
1653 struct Plugin *plugin;
1656 plugin = finish->plugin;
1657 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1659 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1661 GNUNET_free (finish->msgstart);
1662 GNUNET_free (finish);
1666 GNUNET_DISK_file_write (plugin->server_stdin_handle, finish->msgheader,
1668 GNUNET_assert (bytes != GNUNET_SYSERR);
1670 if (bytes != finish->size)
1672 finish->msgheader = finish->msgheader + bytes;
1673 finish->size = finish->size - bytes;
1674 plugin->server_write_task =
1675 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1676 plugin->server_stdin_handle,
1677 &finish_sending, finish);
1681 GNUNET_free (finish->msgstart);
1682 GNUNET_free (finish);
1683 set_next_send (plugin);
1688 * Function called when wlan helper is ready to get some data
1690 * @param cls closure
1691 * @param tc GNUNET_SCHEDULER_TaskContext
1694 do_transmit (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1696 struct Plugin *plugin = cls;
1698 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1699 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1702 struct Session *session;
1703 struct FragmentMessage *fm;
1704 struct Finish_send *finish;
1705 struct FragmentMessage_queue *fmq;
1706 struct AckSendQueue *ack;
1709 if (plugin->ack_send_queue_head != NULL)
1711 ack = plugin->ack_send_queue_head;
1712 GNUNET_CONTAINER_DLL_remove (plugin->ack_send_queue_head,
1713 plugin->ack_send_queue_tail, ack);
1714 send_ack (plugin, ack);
1719 //test if a "hello-beacon" has to be send
1720 if (GNUNET_TIME_absolute_get_remaining (plugin->beacon_time).rel_value == 0)
1722 send_hello_beacon (plugin);
1726 if (plugin->sending_messages_head != NULL)
1728 fmq = plugin->sending_messages_head;
1730 GNUNET_CONTAINER_DLL_remove (plugin->sending_messages_head,
1731 plugin->sending_messages_tail, fmq);
1734 session = fm->session;
1735 GNUNET_assert (session != NULL);
1738 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1739 "Sending GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT for fragment message %p, size: %u\n",
1743 getRadiotapHeader (plugin, session->mac, fm->radioHeader);
1744 getWlanHeader (fm->ieeewlanheader, &(fm->session->mac->addr), plugin,
1748 GNUNET_DISK_file_write (plugin->server_stdin_handle, fm->frag,
1750 if (bytes == GNUNET_SYSERR)
1752 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1754 ("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1755 errno, strerror (errno));
1758 GNUNET_assert (bytes != GNUNET_SYSERR);
1760 if (bytes != fm->size)
1762 finish = GNUNET_malloc (sizeof (struct Finish_send));
1763 finish->plugin = plugin;
1764 finish->msgheader = fm->frag + bytes;
1765 finish->size = fm->size - bytes;
1766 finish->msgstart = (struct GNUNET_MessageHeader *) fm->frag;
1768 GNUNET_assert (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK);
1770 plugin->server_write_task =
1771 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1772 plugin->server_stdin_handle,
1773 &finish_sending, finish);
1778 GNUNET_free (fm->frag);
1780 set_next_send (plugin);
1782 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
1786 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
1787 "do_transmit did nothing, should not happen!\n");
1791 * Another peer has suggested an address for this
1792 * peer and transport plugin. Check that this could be a valid
1793 * address. If so, consider adding it to the list
1796 * @param cls closure
1797 * @param addr pointer to the address
1798 * @param addrlen length of addr
1799 * @return GNUNET_OK if this is a plausible address for this peer
1803 wlan_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1805 //struct Plugin *plugin = cls;
1807 /* check if the address is plausible; if so,
1808 * add it to our list! */
1810 GNUNET_assert (cls != NULL);
1811 //FIXME mitm is not checked
1812 //Mac Address has 6 bytes
1815 /* TODO check for bad addresses like multicast, broadcast, etc */
1817 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1818 "wlan_plugin_address_suggested got good address, size %u!\n", addrlen);
1823 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1824 "wlan_plugin_address_suggested got bad address, size %u!\n", addrlen);
1826 return GNUNET_SYSERR;
1830 * Function that can be used by the transport service to transmit
1831 * a message using the plugin.
1833 * @param cls closure
1834 * @param target who should receive this message
1835 * @param priority how important is the message
1836 * @param msgbuf the message to transmit
1837 * @param msgbuf_size number of bytes in 'msgbuf'
1838 * @param timeout when should we time out
1839 * @param session which session must be used (or NULL for "any")
1840 * @param addr the address to use (can be NULL if the plugin
1841 * is "on its own" (i.e. re-use existing TCP connection))
1842 * @param addrlen length of the address in bytes
1843 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1844 * otherwise the plugin may use other addresses or
1845 * existing connections (if available)
1846 * @param cont continuation to call once the message has
1847 * been transmitted (or if the transport is ready
1848 * for the next transmission call; or if the
1849 * peer disconnected...)
1850 * @param cont_cls closure for cont
1851 * @return number of bytes used (on the physical network, with overheads);
1852 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1853 * and does NOT mean that the message was not transmitted (DV)
1856 wlan_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
1857 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
1858 struct GNUNET_TIME_Relative timeout, struct Session *session,
1859 const void *addr, size_t addrlen, int force_address,
1860 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1862 struct Plugin *plugin = cls;
1863 struct PendingMessage *newmsg;
1864 struct WlanHeader *wlanheader;
1866 //check if msglen > 0
1867 GNUNET_assert (msgbuf_size > 0);
1869 //get session if needed
1870 if (session == NULL)
1872 if (wlan_plugin_address_suggested (plugin, addr, addrlen) == GNUNET_OK)
1874 session = get_session (plugin, addr, target);
1878 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1879 _("Wlan Address len %d is wrong\n"), addrlen);
1886 //queue message in session
1887 //test if there is no other message in the "queue"
1888 //FIXME: to many send requests
1889 if (session->pending_message_head != NULL)
1891 newmsg = session->pending_message_head;
1892 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1893 "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",
1894 GNUNET_TIME_absolute_get_remaining (newmsg->
1896 session->mac->fragment_messages_out_count);
1899 newmsg = GNUNET_malloc (sizeof (struct PendingMessage));
1900 newmsg->msg = GNUNET_malloc (msgbuf_size + sizeof (struct WlanHeader));
1901 wlanheader = newmsg->msg;
1902 //copy msg to buffer, not fragmented / segmented yet, but with message header
1903 wlanheader->header.size = htons (msgbuf_size + sizeof (struct WlanHeader));
1904 wlanheader->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA);
1905 memcpy (&(wlanheader->target), target, sizeof (struct GNUNET_PeerIdentity));
1906 memcpy (&(wlanheader->source), plugin->env->my_identity,
1907 sizeof (struct GNUNET_PeerIdentity));
1908 wlanheader->crc = 0;
1909 memcpy (&wlanheader[1], msgbuf, msgbuf_size);
1912 ((char *) wlanheader, msgbuf_size + sizeof (struct WlanHeader)));
1913 //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);
1914 //hexdump(newmsg->msg, msgbuf_size + sizeof(struct WlanHeader));
1916 newmsg->transmit_cont = cont;
1917 newmsg->transmit_cont_cls = cont_cls;
1918 newmsg->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1920 newmsg->timeout.abs_value = newmsg->timeout.abs_value - 500;
1922 newmsg->message_size = msgbuf_size + sizeof (struct WlanHeader);
1924 GNUNET_CONTAINER_DLL_insert_tail (session->pending_message_head,
1925 session->pending_message_tail, newmsg);
1928 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1929 "New message for %p with size (incl wlan header) %u added\n",
1930 session, newmsg->message_size);
1932 #if DEBUG_wlan_msg_dump
1933 hexdump (msgbuf, GNUNET_MIN (msgbuf_size, 256));
1936 queue_session (plugin, session);
1938 check_fragment_queue (plugin);
1939 //FIXME not the correct size
1945 * function to free a mac endpoint
1946 * @param plugin pointer to the plugin struct
1947 * @param endpoint pointer to the MacEndpoint to free
1950 free_macendpoint (struct Plugin *plugin, struct MacEndpoint *endpoint)
1952 struct Sessionqueue *sessions;
1953 struct Sessionqueue *sessions_next;
1955 GNUNET_assert (endpoint != NULL);
1957 sessions = endpoint->sessions_head;
1958 while (sessions != NULL)
1960 sessions_next = sessions->next;
1961 free_session (plugin, sessions, GNUNET_NO);
1962 sessions = sessions_next;
1965 GNUNET_CONTAINER_DLL_remove (plugin->mac_head, plugin->mac_tail, endpoint);
1966 if (endpoint->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1967 GNUNET_SCHEDULER_cancel (endpoint->timeout_task);
1968 GNUNET_free (endpoint);
1973 * function to free a session
1974 * @param plugin pointer to the plugin
1975 * @param queue pointer to the sessionqueue element to free
1976 * @param free_macendpoint if GNUNET_YES and mac endpoint would be empty, free mac endpoint
1979 free_session (struct Plugin *plugin, struct Sessionqueue *queue,
1980 int do_free_macendpoint)
1982 struct Sessionqueue *pendingsession;
1983 struct Sessionqueue *pendingsession_tmp;
1984 struct PendingMessage *pm;
1985 struct MacEndpoint *endpoint;
1986 struct FragmentMessage *fm;
1987 struct FragmentMessage *fmnext;
1990 GNUNET_assert (queue != NULL);
1991 GNUNET_assert (queue->content != NULL);
1994 //is this session pending for send
1995 pendingsession = plugin->pending_Sessions_head;
1996 while (pendingsession != NULL)
1998 pendingsession_tmp = pendingsession->next;
1999 if (pendingsession->content == queue->content)
2001 plugin->pendingsessions--;
2002 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
2003 plugin->pending_Sessions_tail,
2005 GNUNET_free (pendingsession);
2007 GNUNET_assert (check == 0);
2010 pendingsession = pendingsession_tmp;
2013 endpoint = queue->content->mac;
2014 fm = endpoint->sending_messages_head;
2018 if (fm->session == queue->content)
2020 free_fragment_message (plugin, fm);
2025 // remove PendingMessage
2026 pm = queue->content->pending_message_head;
2029 GNUNET_CONTAINER_DLL_remove (queue->content->pending_message_head,
2030 queue->content->pending_message_tail, pm);
2031 GNUNET_free (pm->msg);
2033 pm = queue->content->pending_message_head;
2036 GNUNET_CONTAINER_DLL_remove (endpoint->sessions_head, endpoint->sessions_tail,
2039 if (endpoint->sessions_head == NULL && do_free_macendpoint == GNUNET_YES)
2041 free_macendpoint (plugin, endpoint);
2044 if (queue->content->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2045 GNUNET_SCHEDULER_cancel (queue->content->timeout_task);
2046 GNUNET_free (queue);
2047 check_fragment_queue (plugin);
2051 * Function that can be used to force the plugin to disconnect
2052 * from the given peer and cancel all previous transmissions
2053 * (and their continuation).
2055 * @param cls closure
2056 * @param target peer from which to disconnect
2059 wlan_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
2061 struct Plugin *plugin = cls;
2062 struct Sessionqueue *queue;
2063 struct Sessionqueue *queue_next;
2064 struct MacEndpoint *endpoint = plugin->mac_head;
2065 struct MacEndpoint *endpoint_next;
2067 // just look at all the session for the needed one
2068 while (endpoint != NULL)
2070 queue = endpoint->sessions_head;
2071 endpoint_next = endpoint->next;
2072 while (queue != NULL)
2074 // content is never NULL
2075 GNUNET_assert (queue->content != NULL);
2076 queue_next = queue->next;
2078 (target, &(queue->content->target),
2079 sizeof (struct GNUNET_PeerIdentity)) == 0)
2081 free_session (plugin, queue, GNUNET_YES);
2086 endpoint = endpoint_next;
2091 * Convert the transports address to a nice, human-readable
2094 * @param cls closure
2095 * @param type name of the transport that generated the address
2096 * @param addr one of the addresses of the host, NULL for the last address
2097 * the specific address format depends on the transport
2098 * @param addrlen length of the address
2099 * @param numeric should (IP) addresses be displayed in numeric form?
2100 * @param timeout after how long should we give up?
2101 * @param asc function to call on each string
2102 * @param asc_cls closure for asc
2105 wlan_plugin_address_pretty_printer (void *cls, const char *type,
2106 const void *addr, size_t addrlen,
2108 struct GNUNET_TIME_Relative timeout,
2109 GNUNET_TRANSPORT_AddressStringCallback asc,
2113 const unsigned char *input;
2115 //GNUNET_assert(cls !=NULL);
2116 if (addrlen != sizeof(struct MacAddress))
2118 /* invalid address (MAC addresses have 6 bytes) */
2121 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2122 "Func wlan_plugin_address_pretty_printer got size: %u, worng size!\n",
2128 input = (const unsigned char *) addr;
2129 GNUNET_asprintf (&ret,
2130 "Transport %s: %s Mac-Address %.2X:%.2X:%.2X:%.2X:%.2X:%.2X",type,
2131 PROTOCOL_PREFIX, input[0], input[1], input[2], input[3],
2132 input[4], input[5]);
2134 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2135 "Func wlan_plugin_address_pretty_printer got size: %u, nummeric %u, type %s; made string: %s\n",
2136 addrlen, numeric, type, ret);
2138 asc ( asc_cls, ret);
2139 //only one mac address per plugin
2140 asc ( asc_cls, NULL);
2146 * handels the data after all fragments are put together
2147 * @param cls macendpoint this messages belongs to
2148 * @param hdr pointer to the data
2151 wlan_data_message_handler (void *cls, const struct GNUNET_MessageHeader *hdr)
2153 struct MacEndpoint *endpoint = (struct MacEndpoint *) cls;
2154 struct Plugin *plugin = endpoint->plugin;
2155 struct WlanHeader *wlanheader;
2156 struct Session *session;
2158 const struct GNUNET_MessageHeader *temp_hdr;
2159 struct GNUNET_PeerIdentity tmpsource;
2162 if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_DATA)
2166 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2167 "Func wlan_data_message_handler got GNUNET_MESSAGE_TYPE_WLAN_DATA size: %u\n",
2171 if (ntohs (hdr->size) <
2172 sizeof (struct WlanHeader) + sizeof (struct GNUNET_MessageHeader))
2174 //packet not big enought
2178 wlanheader = (struct WlanHeader *) hdr;
2180 session = search_session (plugin, endpoint, &wlanheader->source);
2182 temp_hdr = (const struct GNUNET_MessageHeader *) &wlanheader[1];
2183 crc = ntohl (wlanheader->crc);
2184 wlanheader->crc = 0;
2185 if (getcrc32 ((char *) wlanheader, ntohs (wlanheader->header.size)) != crc)
2187 //wrong crc, dispose message
2188 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2189 "Wlan message header crc was wrong: %u != %u\n",
2190 getcrc32 ((char *) wlanheader,
2191 ntohs (wlanheader->header.size)), crc);
2192 hexdump ((void *) hdr, ntohs (hdr->size));
2196 //if not in session list
2197 if (session == NULL)
2200 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2201 "WLAN client not in session list: packet size = %u, inner size = %u, header size = %u\n",
2202 ntohs (wlanheader->header.size), ntohs (temp_hdr->size),
2203 sizeof (struct WlanHeader));
2205 //try if it is a hello message
2206 if (ntohs (wlanheader->header.size) >=
2207 ntohs (temp_hdr->size) + sizeof (struct WlanHeader))
2209 if (ntohs (temp_hdr->type) == GNUNET_MESSAGE_TYPE_HELLO)
2211 if (GNUNET_HELLO_get_id
2212 ((const struct GNUNET_HELLO_Message *) temp_hdr,
2213 &tmpsource) == GNUNET_OK)
2215 session = create_session (plugin, endpoint, &tmpsource);
2219 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2220 "WLAN client not in session list and hello message is not okay\n");
2227 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2228 "WLAN client not in session list and not a hello message\n");
2234 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2235 "WLAN client not in session list and message size in does not fit\npacket size = %u, inner size = %u, header size = %u\n",
2236 ntohs (wlanheader->header.size),
2237 ntohs (temp_hdr->size), sizeof (struct WlanHeader));
2242 //"receive" the message
2245 (&wlanheader->source, &session->target,
2246 sizeof (struct GNUNET_PeerIdentity)) != 0)
2250 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2251 "WLAN peer source id doesn't match packet peer source id: session %p\n",
2258 (&wlanheader->target, plugin->env->my_identity,
2259 sizeof (struct GNUNET_PeerIdentity)) != 0)
2263 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2264 "WLAN peer target id doesn't match our peer id: session %p\n",
2270 GNUNET_SERVER_mst_receive (plugin->data_tokenizer, session,
2271 (const char *) temp_hdr,
2272 ntohs (hdr->size) - sizeof (struct WlanHeader),
2273 GNUNET_YES, GNUNET_NO);
2279 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2280 "wlan_data_message_handler got wrong message type: %u\n",
2287 * function to process the a message, give it to the higher layer
2288 * @param cls pointer to the plugin
2289 * @param client pointer to the session this message belongs to
2290 * @param hdr start of the message
2292 //TODO ATS informations
2294 process_data (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
2297 GNUNET_assert (client != NULL);
2298 GNUNET_assert (cls != NULL);
2299 struct Session *session = (struct Session *) client;
2300 struct Plugin *plugin = (struct Plugin *) cls;
2302 struct GNUNET_TRANSPORT_ATS_Information distance[2];
2304 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2305 distance[0].value = htonl (1);
2306 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2307 distance[1].value = htonl (0);
2310 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2311 "Calling plugin->env->receive for session %p; %s; size: %u\n",
2312 session, wlan_plugin_address_to_string (NULL,
2318 plugin->env->receive (plugin->env->cls, &(session->target), hdr,
2319 (const struct GNUNET_TRANSPORT_ATS_Information *)
2320 &distance, 2, session,
2321 (const char *) &session->mac->addr,
2322 sizeof (session->mac->addr));
2326 * Function used for to process the data received from the wlan interface
2328 * @param cls the plugin handle
2329 * @param session_light pointer to the struct holding known informations
2330 * @param hdr hdr of the GNUNET_MessageHeader
2331 * @param rxinfo pointer to the radiotap informations got with this packet
2334 wlan_data_helper (void *cls, struct Session_light *session_light,
2335 const struct GNUNET_MessageHeader *hdr,
2336 const struct Radiotap_rx *rxinfo)
2338 struct Plugin *plugin = cls;
2339 struct FragmentMessage *fm;
2340 struct FragmentMessage *fm2;
2341 struct GNUNET_PeerIdentity tmpsource;
2344 if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_HELLO)
2347 //TODO better DOS protection, error handling
2348 //TODO test first than create session
2349 GNUNET_assert (session_light != NULL);
2352 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2353 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_HELLO size: %u; %s\n",
2354 ntohs (hdr->size), wlan_plugin_address_to_string (NULL,
2355 session_light->addr.
2359 if (session_light->macendpoint == NULL)
2361 session_light->macendpoint =
2362 get_macendpoint (plugin, &session_light->addr, GNUNET_YES);
2366 if (GNUNET_HELLO_get_id
2367 ((const struct GNUNET_HELLO_Message *) hdr,
2368 &tmpsource) == GNUNET_OK)
2370 session_light->session = create_session (plugin, session_light->macendpoint, &tmpsource);
2371 plugin->env->receive(plugin->env->cls,&session_light->session->target,hdr, NULL, 0, session_light->session,
2372 (const char *) &session_light->session->mac->addr,
2373 sizeof (session_light->session->mac->addr));
2377 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2378 "WLAN client not in session list and hello message is not okay\n");
2385 else if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_FRAGMENT)
2388 GNUNET_assert (session_light != NULL);
2389 if (session_light->macendpoint == NULL)
2391 session_light->macendpoint =
2392 get_macendpoint (plugin, &session_light->addr, GNUNET_YES);
2396 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2397 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_FRAGMENT with size: %u; mac endpoint %p: %s\n",
2398 ntohs (hdr->size), session_light->macendpoint,
2399 wlan_plugin_address_to_string (NULL,
2400 session_light->addr.mac,
2405 GNUNET_DEFRAGMENT_process_fragment (session_light->macendpoint->defrag,
2408 if (ret == GNUNET_NO)
2410 session_light->macendpoint->dups++;
2412 else if (ret == GNUNET_OK)
2414 session_light->macendpoint->fragc++;
2416 set_next_send (plugin);
2422 else if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_FRAGMENT_ACK)
2424 GNUNET_assert (session_light != NULL);
2425 if (session_light->macendpoint == NULL)
2427 session_light->macendpoint =
2428 get_macendpoint (plugin, &session_light->addr, GNUNET_NO);
2431 if (session_light->macendpoint == NULL)
2434 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2435 "Macendpoint does not exist for this GNUNET_MESSAGE_TYPE_FRAGMENT_ACK size: %u; %s\n",
2436 ntohs (hdr->size), wlan_plugin_address_to_string (NULL,
2437 session_light->addr.mac,
2444 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2445 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_FRAGMENT_ACK size: %u; mac endpoint: %p; %s\n",
2446 ntohs (hdr->size), session_light->macendpoint,
2447 wlan_plugin_address_to_string (NULL,
2448 session_light->addr.mac,
2451 fm = session_light->macendpoint->sending_messages_head;
2455 int ret = GNUNET_FRAGMENT_process_ack (fm->fragcontext, hdr);
2457 if (ret == GNUNET_OK)
2459 #if DEBUG_wlan_retransmission
2460 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2461 "Got last ack, finished fragment message %p\n", fm);
2463 session_light->macendpoint->acks++;
2464 fm->session->last_activity = GNUNET_TIME_absolute_get ();
2465 session_light->macendpoint->last_activity = fm->session->last_activity;
2466 free_fragment_message (plugin, fm);
2467 check_fragment_queue (plugin);
2470 if (ret == GNUNET_NO)
2472 #if DEBUG_wlan_retransmission
2473 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2474 "Got ack for: %p\n", fm);
2476 session_light->macendpoint->acks++;
2479 if (ret == GNUNET_SYSERR)
2487 #if DEBUG_wlan_retransmission
2488 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2489 "WLAN fragment not in fragment list\n");
2497 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2498 "WLAN packet inside the WLAN helper packet has not the right type: %u size: %u\n",
2499 ntohs (hdr->type), ntohs (hdr->size));
2505 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2506 "Helper finished\n");
2512 * Function to print mac addresses nice *
2513 * @param pointer to 6 byte with the mac address
2514 * @return pointer to the chars which hold the print out
2517 macprinter (const u_int8_t * mac)
2519 static char macstr[20];
2521 GNUNET_snprintf (macstr, sizeof (macstr), "%X:%X:%X:%X:%X:%X", mac[0], mac[1],
2522 mac[2], mac[3], mac[4], mac[5]);
2527 * Function for the scheduler if a mac endpoint times out
2528 * @param cls pointer to the MacEndpoint
2529 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
2532 macendpoint_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2534 struct MacEndpoint *endpoint = cls;
2536 GNUNET_assert (endpoint != NULL);
2537 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2538 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2542 if (GNUNET_TIME_absolute_get_remaining
2543 (GNUNET_TIME_absolute_add
2544 (endpoint->last_activity, MACENDPOINT_TIMEOUT)).rel_value == 0)
2546 free_macendpoint (endpoint->plugin, endpoint);
2550 endpoint->timeout_task =
2551 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
2557 * function to create an macendpoint
2558 * @param plugin pointer to the plugin struct
2559 * @param addr pointer to the macaddress
2560 * @return returns a macendpoint
2562 static struct MacEndpoint *
2563 create_macendpoint (struct Plugin *plugin, const struct MacAddress *addr)
2565 struct MacEndpoint *newend = GNUNET_malloc (sizeof (struct MacEndpoint));
2567 newend->addr = *addr;
2568 newend->plugin = plugin;
2569 newend->addr = *addr;
2570 newend->fragment_messages_out_count = 0;
2572 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
2573 MESSAGES_IN_DEFRAG_QUEUE_PER_MAC,
2574 newend, &wlan_data_message_handler,
2576 newend->last_activity = GNUNET_TIME_absolute_get ();
2577 newend->timeout_task =
2578 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
2581 plugin->mac_count++;
2582 GNUNET_CONTAINER_DLL_insert_tail (plugin->mac_head, plugin->mac_tail, newend);
2584 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2585 "New Mac Endpoint %p: %s\n", newend,
2586 wlan_plugin_address_to_string (NULL, newend->addr.mac, 6));
2592 * Function used for to process the data from the suid process
2594 * @param cls the plugin handle
2595 * @param client client that send the data (not used)
2596 * @param hdr header of the GNUNET_MessageHeader
2599 wlan_process_helper (void *cls, void *client,
2600 const struct GNUNET_MessageHeader *hdr)
2602 struct Plugin *plugin = cls;
2603 struct ieee80211_frame *wlanIeeeHeader = NULL;
2604 struct Session_light *session_light = NULL;
2605 struct Radiotap_rx *rxinfo;
2606 const struct GNUNET_MessageHeader *temp_hdr = NULL;
2611 switch (ntohs (hdr->type))
2613 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA:
2615 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2616 "Func wlan_process_helper got GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA size: %u\n",
2620 //call wlan_process_helper with the message inside, later with wlan: analyze signal
2621 if (ntohs (hdr->size) <
2622 sizeof (struct ieee80211_frame) + 2*sizeof (struct GNUNET_MessageHeader) +
2623 sizeof (struct Radiotap_rx))
2626 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2627 "Size of packet is too small; size: %u min size: %u\n",
2629 sizeof (struct ieee80211_frame) +
2630 sizeof (struct GNUNET_MessageHeader));
2633 /* FIXME: restart SUID process */
2637 rxinfo = (struct Radiotap_rx *) &hdr[1];
2638 wlanIeeeHeader = (struct ieee80211_frame *) &rxinfo[1];
2640 //process only if it is an broadcast or for this computer both with the gnunet bssid
2644 (&(wlanIeeeHeader->i_addr3), &mac_bssid,
2645 sizeof (struct MacAddress)) == 0)
2647 //check for broadcast or mac
2649 (&(wlanIeeeHeader->i_addr1), &bc_all_mac,
2650 sizeof (struct MacAddress)) == 0) ||
2651 (memcmp (&(wlanIeeeHeader->i_addr1), &(plugin->mac_address),
2652 sizeof (struct MacAddress)) == 0))
2654 //if packet is from us return
2655 if ((memcmp (&(wlanIeeeHeader->i_addr2), &(plugin->mac_address),
2656 sizeof (struct MacAddress)) == 0)){
2659 // process the inner data
2663 ntohs (hdr->size) - sizeof (struct ieee80211_frame) -
2664 sizeof (struct GNUNET_MessageHeader) - sizeof (struct Radiotap_rx);
2666 session_light = GNUNET_malloc (sizeof (struct Session_light));
2667 memcpy (&session_light->addr, &(wlanIeeeHeader->i_addr2),
2668 sizeof (struct MacAddress));
2669 //session_light->session = search_session(plugin,session_light->addr);
2672 while (pos < datasize)
2674 temp_hdr = (struct GNUNET_MessageHeader *) &wlanIeeeHeader[1] + pos;
2675 if (ntohs(temp_hdr->size) <= datasize + pos)
2677 wlan_data_helper (plugin, session_light, temp_hdr, rxinfo);
2682 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2683 "Size of packet is too small; size: %u > size of packet: %u\n",
2684 ntohs(temp_hdr->size),datasize + pos);
2687 pos += ntohs (temp_hdr->size);
2692 GNUNET_free (session_light);
2697 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2698 "Func wlan_process_helper got wrong MAC: %s\n",
2699 macprinter (wlanIeeeHeader->i_addr1));
2706 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2707 "Func wlan_process_helper got wrong BSSID: %s\n",
2708 macprinter (wlanIeeeHeader->i_addr2));
2712 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
2713 //TODO more control messages
2714 if (ntohs (hdr->size) != sizeof (struct Wlan_Helper_Control_Message))
2717 /* FIXME: restart SUID process */
2720 memcpy (&plugin->mac_address, &hdr[1], sizeof (struct MacAddress));
2722 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2723 "Received WLAN_HELPER_CONTROL message with transport of address %s\n",
2724 wlan_plugin_address_to_string (cls, &plugin->mac_address,
2728 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2729 &plugin->mac_address,
2730 sizeof (struct MacAddress));
2734 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2735 "Func wlan_process_helper got unknown message with number %u, size %u\n",
2736 ntohs (hdr->type), ntohs (hdr->size));
2739 #if DEBUG_wlan_msg_dump
2740 hexdump (hdr, GNUNET_MIN (ntohs (hdr->size), 256));
2748 * We have been notified that wlan-helper has written something to stdout.
2749 * Handle the output, then reschedule this function to be called again once
2750 * more is available.
2752 * @param cls the plugin handle
2753 * @param tc the scheduling context
2756 wlan_plugin_helper_read (void *cls,
2757 const struct GNUNET_SCHEDULER_TaskContext *tc)
2759 struct Plugin *plugin = cls;
2761 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2763 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2766 char mybuf[WLAN_MTU + sizeof (struct GNUNET_MessageHeader)];
2770 GNUNET_DISK_file_read (plugin->server_stdout_handle, mybuf,
2775 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2777 ("Finished reading from wlan-helper stdout with code: %d\n"),
2782 GNUNET_SERVER_mst_receive (plugin->suid_tokenizer, NULL, mybuf, bytes,
2783 GNUNET_NO, GNUNET_NO);
2785 GNUNET_assert (plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2786 plugin->server_read_task =
2787 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2788 plugin->server_stdout_handle,
2789 &wlan_plugin_helper_read, plugin);
2793 * Start the gnunet-wlan-helper process.
2795 * @param plugin the transport plugin
2796 * @param testmode should we use the dummy driver for testing?
2797 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
2800 wlan_transport_start_wlan_helper (struct Plugin *plugin, int testmode)
2802 const char *filenamehw = "gnunet-transport-wlan-helper";
2803 const char *filenameloopback = "gnunet-transport-wlan-helper-dummy";
2805 plugin->server_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
2806 if (plugin->server_stdout == NULL)
2807 return GNUNET_SYSERR;
2809 plugin->server_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);
2810 if (plugin->server_stdin == NULL)
2811 return GNUNET_SYSERR;
2813 /* Start the server process */
2819 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2820 "Starting gnunet-wlan-helper process cmd: %s %s %i\n",
2821 filenamehw, plugin->interface, testmode);
2824 if (GNUNET_OS_check_helper_binary (filenamehw) == GNUNET_YES)
2826 plugin->server_proc =
2827 GNUNET_OS_start_process (plugin->server_stdin, plugin->server_stdout,
2828 filenamehw, filenamehw, plugin->interface,
2831 else if (GNUNET_OS_check_helper_binary (filenamehw) == GNUNET_NO)
2833 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
2834 "gnunet-transport-wlan-helper is not suid, please change it or look at the doku\n");
2839 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
2840 "gnunet-transport-wlan-helper not found, please look if it exists and is the $PATH variable!\n");
2845 else if (testmode == 1)
2849 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2850 "Starting gnunet-wlan-helper loopback 1 process cmd: %s %s %i\n",
2851 filenameloopback, plugin->interface, testmode);
2854 if (GNUNET_OS_check_helper_binary (filenameloopback) != GNUNET_SYSERR)
2856 plugin->server_proc =
2857 GNUNET_OS_start_process (plugin->server_stdin, plugin->server_stdout,
2858 filenameloopback, filenameloopback, "1",
2863 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
2864 "gnunet-transport-wlan-helper-dummy not found, please look if it exists and is the $PATH variable!\n");
2868 else if (testmode == 2)
2871 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2872 "Starting gnunet-wlan-helper loopback 2 process cmd: %s %s %i\n",
2873 filenameloopback, plugin->interface, testmode);
2875 if (GNUNET_OS_check_helper_binary (filenameloopback) != GNUNET_SYSERR)
2877 plugin->server_proc =
2878 GNUNET_OS_start_process (plugin->server_stdin, plugin->server_stdout,
2879 filenameloopback, filenameloopback, "2",
2884 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
2885 "gnunet-transport-wlan-helper-dummy not found, please look if it exists and is in the $PATH variable!\n");
2889 if (plugin->server_proc == NULL)
2892 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2893 "Failed to start gnunet-wlan-helper process\n");
2895 return GNUNET_SYSERR;
2898 /* Close the write end of the read pipe */
2899 GNUNET_DISK_pipe_close_end (plugin->server_stdout,
2900 GNUNET_DISK_PIPE_END_WRITE);
2902 /* Close the read end of the write pipe */
2903 GNUNET_DISK_pipe_close_end (plugin->server_stdin, GNUNET_DISK_PIPE_END_READ);
2905 plugin->server_stdout_handle =
2906 GNUNET_DISK_pipe_handle (plugin->server_stdout,
2907 GNUNET_DISK_PIPE_END_READ);
2908 plugin->server_stdin_handle =
2909 GNUNET_DISK_pipe_handle (plugin->server_stdin,
2910 GNUNET_DISK_PIPE_END_WRITE);
2912 GNUNET_assert (plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2915 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2916 "Adding server_read_task for the wlan-helper\n");
2919 plugin->server_read_task =
2920 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2921 plugin->server_stdout_handle,
2922 &wlan_plugin_helper_read, plugin);
2928 * Exit point from the plugin.
2929 * @param cls pointer to the api struct
2934 libgnunet_plugin_transport_wlan_done (void *cls)
2936 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2937 struct Plugin *plugin = api->cls;
2938 struct MacEndpoint *endpoint = plugin->mac_head;
2939 struct MacEndpoint *endpoint_next;
2942 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2943 "libgnunet_plugin_transport_wlan_done started\n");
2946 GNUNET_DISK_pipe_close (plugin->server_stdout);
2947 GNUNET_DISK_pipe_close (plugin->server_stdin);
2948 GNUNET_OS_process_kill (plugin->server_proc, 9);
2949 GNUNET_OS_process_close (plugin->server_proc);
2951 GNUNET_assert (cls != NULL);
2953 while (endpoint != NULL)
2955 endpoint_next = endpoint->next;
2956 free_macendpoint (plugin, endpoint);
2957 endpoint = endpoint_next;
2960 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
2962 GNUNET_SCHEDULER_cancel (plugin->server_write_delay_task);
2963 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
2965 if (plugin->server_write_task != GNUNET_SCHEDULER_NO_TASK)
2967 GNUNET_SCHEDULER_cancel (plugin->server_write_task);
2968 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
2970 if (plugin->server_read_task != GNUNET_SCHEDULER_NO_TASK)
2972 GNUNET_SCHEDULER_cancel (plugin->server_read_task);
2973 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2976 if (plugin->suid_tokenizer != NULL)
2977 GNUNET_SERVER_mst_destroy (plugin->suid_tokenizer);
2979 if (plugin->data_tokenizer != NULL)
2980 GNUNET_SERVER_mst_destroy (plugin->data_tokenizer);
2982 GNUNET_free_non_null (plugin->interface);
2983 GNUNET_free (plugin);
2989 * Entry point for the plugin.
2991 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2992 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2995 libgnunet_plugin_transport_wlan_init (void *cls)
2997 //struct GNUNET_SERVICE_Context *service;
2998 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2999 struct GNUNET_TRANSPORT_PluginFunctions *api;
3000 struct Plugin *plugin;
3001 static unsigned long long testmode = 0;
3003 GNUNET_assert (cls != NULL);
3005 plugin = GNUNET_malloc (sizeof (struct Plugin));
3007 plugin->pendingsessions = 0;
3008 plugin->mac_count = 0;
3009 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
3010 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
3011 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
3012 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
3013 GNUNET_BANDWIDTH_value_init (100 * 1024 *
3016 plugin->suid_tokenizer =
3017 GNUNET_SERVER_mst_create (&wlan_process_helper, plugin);
3019 plugin->data_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
3021 //plugin->sessions = GNUNET_malloc (sizeof (struct Sessionqueue));
3022 //plugin->pending_Sessions_head = GNUNET_malloc (sizeof (struct Sessionqueue));
3024 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3026 api->send = &wlan_plugin_send;
3027 api->disconnect = &wlan_plugin_disconnect;
3028 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
3029 api->check_address = &wlan_plugin_address_suggested;
3030 api->address_to_string = &wlan_plugin_address_to_string;
3033 if (GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "TESTMODE"))
3035 if (GNUNET_SYSERR ==
3036 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-wlan",
3037 "TESTMODE", &testmode))
3038 testmode = 0; //default value
3041 if (GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "INTERFACE"))
3043 if (GNUNET_CONFIGURATION_get_value_string
3044 (env->cfg, "transport-wlan", "INTERFACE",
3045 &(plugin->interface)) != GNUNET_YES)
3047 libgnunet_plugin_transport_wlan_done (api);
3053 wlan_transport_start_wlan_helper (plugin, testmode);
3054 set_next_beacon_time (plugin);
3055 set_next_send(plugin);
3057 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
3058 "wlan init finished\n");
3064 /* end of plugin_transport_wlan.c */