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 "gnunet_constants.h"
41 //#include "wlan/ieee80211.h"
42 //#include <netinet/ip.h>
46 #define PROTOCOL_PREFIX "wlan"
48 #define PLUGIN_LOG_NAME "wlan-plugin"
56 * time out of a session
58 #define SESSION_TIMEOUT GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT
61 * time out of a mac endpoint
63 #define MACENDPOINT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 2)
66 * scaling factor for hello beacon
68 #define HALLO_BEACON_SCALING_FACTOR 1
71 * max size of fragment queue
73 #define FRAGMENT_QUEUE_SIZE 10
75 * max messages in fragment queue per session/client
77 #define FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION 1
80 * max messages in fragment queue per MAC
82 #define FRAGMENT_QUEUE_MESSAGES_OUT_PER_MACENDPOINT 1
85 * max messages in in queue
87 #define MESSAGES_IN_QUEUE_SIZE 10
89 * max messages in in queue per session/client
91 #define MESSAGES_IN_DEFRAG_QUEUE_PER_MAC 1
94 * LLC fields for better compatibility
96 #define WLAN_LLC_DSAP_FIELD 0xf
97 #define WLAN_LLC_SSAP_FIELD 0xf
103 #define DEBUG_wlan GNUNET_YES
104 #define DEBUG_wlan_retransmission GNUNET_EXTRA_LOGGING
105 #define DEBUG_wlan_ip_udp_packets_on_air GNUNET_EXTRA_LOGGING
106 #define DEBUG_wlan_msg_dump GNUNET_EXTRA_LOGGING
109 #define IEEE80211_ADDR_LEN 6 /* size of 802.11 address */
111 #define IEEE80211_FC0_VERSION_MASK 0x03
112 #define IEEE80211_FC0_VERSION_SHIFT 0
113 #define IEEE80211_FC0_VERSION_0 0x00
114 #define IEEE80211_FC0_TYPE_MASK 0x0c
115 #define IEEE80211_FC0_TYPE_SHIFT 2
116 #define IEEE80211_FC0_TYPE_MGT 0x00
117 #define IEEE80211_FC0_TYPE_CTL 0x04
118 #define IEEE80211_FC0_TYPE_DATA 0x08
121 * Structure of an internet header, naked of options.
125 #if __BYTE_ORDER == __LITTLE_ENDIAN
126 unsigned int ip_hl:4; /* header length */
127 unsigned int ip_v:4; /* version */
129 #if __BYTE_ORDER == __BIG_ENDIAN
130 unsigned int ip_v:4; /* version */
131 unsigned int ip_hl:4; /* header length */
133 u_int8_t ip_tos; /* type of service */
134 u_short ip_len; /* total length */
135 u_short ip_id; /* identification */
136 u_short ip_off; /* fragment offset field */
137 #define IP_RF 0x8000 /* reserved fragment flag */
138 #define IP_DF 0x4000 /* dont fragment flag */
139 #define IP_MF 0x2000 /* more fragments flag */
140 #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
141 u_int8_t ip_ttl; /* time to live */
142 u_int8_t ip_p; /* protocol */
143 u_short ip_sum; /* checksum */
144 struct in_addr ip_src, ip_dst; /* source and dest address */
156 * generic definitions for IEEE 802.11 frames
158 struct ieee80211_frame
162 u_int8_t i_addr1[IEEE80211_ADDR_LEN];
163 u_int8_t i_addr2[IEEE80211_ADDR_LEN];
164 u_int8_t i_addr3[IEEE80211_ADDR_LEN];
167 #if DEBUG_wlan_ip_udp_packets_on_air
174 * Encapsulation of all of the state of the plugin.
181 struct GNUNET_TRANSPORT_PluginEnvironment *env;
184 * List of open connections. head
186 struct MacEndpoint *mac_head;
189 * List of open connections. tail
191 struct MacEndpoint *mac_tail;
194 * Number of connections
196 unsigned int mac_count;
199 * encapsulation of data from the local wlan helper program
201 struct GNUNET_SERVER_MessageStreamTokenizer *suid_tokenizer;
204 * encapsulation of packets received from the wlan helper
206 struct GNUNET_SERVER_MessageStreamTokenizer *data_tokenizer;
209 * stdout pipe handle for the gnunet-wlan-helper process
211 struct GNUNET_DISK_PipeHandle *server_stdout;
214 * stdout file handle for the gnunet-wlan-helper process
216 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
219 * stdin pipe handle for the gnunet-wlan-helper process
221 struct GNUNET_DISK_PipeHandle *server_stdin;
224 * stdin file handle for the gnunet-wlan-helper process
226 const struct GNUNET_DISK_FileHandle *server_stdin_handle;
229 * ID of the gnunet-wlan-server std read task
231 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
234 * ID of the gnunet-wlan-server std read task
236 GNUNET_SCHEDULER_TaskIdentifier server_write_task;
239 * ID of the delay task for writing
241 GNUNET_SCHEDULER_TaskIdentifier server_write_delay_task;
244 * The process id of the wlan process
246 struct GNUNET_OS_Process *server_proc;
249 * The interface of the wlan card given to us by the user.
254 * The mac_address of the wlan card given to us by the helper.
256 struct MacAddress mac_address;
259 * Sessions currently pending for transmission
262 struct Sessionqueue *pending_Sessions_head;
265 * Sessions currently pending for transmission
266 * to a peer (tail), if any.
268 struct Sessionqueue *pending_Sessions_tail;
271 * number of pending sessions
273 unsigned int pendingsessions;
276 * Messages in the sending queues
278 int pending_Fragment_Messages;
281 * messages ready for send, head
283 struct FragmentMessage_queue *sending_messages_head;
285 * messages ready for send, tail
287 struct FragmentMessage_queue *sending_messages_tail;
289 * time of the next "hello-beacon"
291 struct GNUNET_TIME_Absolute beacon_time;
294 * queue to send acks for received fragments (head)
296 struct AckSendQueue *ack_send_queue_head;
299 * queue to send acks for received fragments (tail)
301 struct AckSendQueue *ack_send_queue_tail;
304 * Tracker for bandwidth limit
306 struct GNUNET_BANDWIDTH_Tracker tracker;
310 * Struct to store data if file write did not accept the whole packet
314 struct Plugin *plugin;
316 struct GNUNET_MessageHeader *msgstart;
321 * Queue of sessions, for the general session queue and the pending session queue
326 struct Sessionqueue *next;
327 struct Sessionqueue *prev;
328 struct Session *content;
332 * Queue of fragmented messages, for the sending queue of the plugin
335 struct FragmentMessage_queue
337 struct FragmentMessage_queue *next;
338 struct FragmentMessage_queue *prev;
339 struct FragmentMessage *content;
343 * Queue for the fragments received
346 struct Receive_Fragment_Queue
348 struct Receive_Fragment_Queue *next;
349 struct Receive_Fragment_Queue *prev;
353 struct Radiotap_rx rxinfo;
357 struct MacEndpoint_id_fragment_triple
359 struct MacEndpoint *endpoint;
361 struct FragmentMessage *fm;
365 struct Plugin_Session_pair
367 struct Plugin *plugin;
368 struct Session *session;
372 * Information kept for each message that is yet to
375 struct PendingMessage
380 struct PendingMessage *next;
384 struct PendingMessage *prev;
387 * The pending message
389 struct WlanHeader *msg;
392 * Size of the message
397 * Continuation function to call once the message
398 * has been sent. Can be NULL if there is no
399 * continuation to call.
401 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
404 * Cls for transmit_cont
406 void *transmit_cont_cls;
409 * Timeout value for the pending message.
411 struct GNUNET_TIME_Absolute timeout;
416 * Queue for acks to send for fragments recived
422 * next ack in the ack send queue
424 struct AckSendQueue *next;
426 * previous ack in the ack send queue
428 struct AckSendQueue *prev;
430 * pointer to the session this ack belongs to
432 struct MacEndpoint *endpoint;
434 * ID of message, to distinguish between the messages, picked randomly.
441 struct GNUNET_MessageHeader *hdr;
443 * pointer to the ieee wlan header
445 struct ieee80211_frame *ieeewlanheader;
447 * pointer to the radiotap header
449 struct Radiotap_Send *radioHeader;
453 * Session infos gathered from a messages
459 * the session this message belongs to
461 struct Session *session;
465 struct MacAddress addr;
470 struct MacEndpoint *macendpoint;
474 * Session handle for connections.
482 struct SessionHeader header;
485 * Message currently pending for transmission
486 * to this peer, if any. head
488 struct PendingMessage *pending_message_head;
491 * Message currently pending for transmission
492 * to this peer, if any. tail
494 struct PendingMessage *pending_message_tail;
497 * To whom are we talking to (set to our identity
498 * if we are still waiting for the welcome message)
500 struct GNUNET_PeerIdentity target;
503 * Address of the other peer (either based on our 'connect'
504 * call or on our 'accept' call).
509 * Last activity on this connection. Used to select preferred
510 * connection and timeout
512 struct GNUNET_TIME_Absolute last_activity;
517 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
522 struct MacEndpoint *mac;
525 * count of messages in the fragment out queue for this session
528 int fragment_messages_out_count;
533 * Struct to represent one network card connection
538 * Pointer to the global plugin struct.
540 struct Plugin *plugin;
542 * Struct to hold the session reachable over this mac; head
544 struct Sessionqueue *sessions_head;
546 * Struct to hold the session reachable over this mac; tail
548 struct Sessionqueue *sessions_tail;
550 * Messages currently sending
553 struct FragmentMessage *sending_messages_head;
556 * Messages currently sending
557 * to a peer (tail), if any.
559 struct FragmentMessage *sending_messages_tail;
563 struct MacEndpoint *next;
567 struct MacEndpoint *prev;
572 struct MacAddress addr;
575 * Defrag context for this mac endpoint
577 struct GNUNET_DEFRAGMENT_Context *defrag;
580 * count of messages in the fragment out queue for this mac endpoint
583 int fragment_messages_out_count;
591 * Duplicates received
606 * Last activity on this endpoint. Used to select preferred
609 struct GNUNET_TIME_Absolute last_activity;
614 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
618 * Struct for Messages in the fragment queue
621 struct FragmentMessage
625 * Session this message belongs to
628 struct Session *session;
631 * This is a doubly-linked list.
633 struct FragmentMessage *next;
636 * This is a doubly-linked list.
638 struct FragmentMessage *prev;
641 * Fragmentation context
643 struct GNUNET_FRAGMENT_Context *fragcontext;
646 * Timeout value for the message.
648 struct GNUNET_TIME_Absolute timeout;
653 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
666 * pointer to the ieee wlan header
668 struct ieee80211_frame *ieeewlanheader;
670 * pointer to the radiotap header
672 struct Radiotap_Send *radioHeader;
676 do_transmit (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
678 free_session (struct Plugin *plugin, struct Sessionqueue *queue,
679 int do_free_macendpoint);
680 static struct MacEndpoint *
681 create_macendpoint (struct Plugin *plugin, const struct MacAddress *addr);
684 * Generates a nice hexdump of a memory area.
686 * \param mem pointer to memory to dump
687 * \param length how many bytes to dump
690 hexdump (const void *mem, unsigned length)
693 char *src = (char *) mem;
695 printf ("dumping %u bytes from %p\r\n"
696 " 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF\r\n",
701 for (i = 0; i < length; i += 16, src += 16)
705 t += sprintf (t, "%04x: ", i);
706 for (j = 0; j < 16; j++)
709 t += sprintf (t, "%02X", src[j] & 0xff);
711 t += sprintf (t, " ");
712 t += sprintf (t, j % 2 ? " " : "-");
715 t += sprintf (t, " ");
716 for (j = 0; j < 16; j++)
720 if (isprint ((unsigned char) src[j]))
721 t += sprintf (t, "%c", src[j]);
723 t += sprintf (t, ".");
727 t += sprintf (t, " ");
731 t += sprintf (t, "\r\n");
737 * Function to find a MacEndpoint with a specific mac addr
738 * @param plugin pointer to the plugin struct
739 * @param addr pointer to the mac address
740 * @param create_new GNUNET_YES if a new end point should be created
743 static struct MacEndpoint *
744 get_macendpoint (struct Plugin *plugin, const struct MacAddress *addr,
747 struct MacEndpoint *queue = plugin->mac_head;
749 while (queue != NULL)
751 GNUNET_assert (queue->sessions_head != NULL);
752 if (memcmp (addr, &queue->addr, sizeof (struct MacAddress)) == 0)
753 return queue; /* session found */
757 if (create_new == GNUNET_YES)
759 return create_macendpoint (plugin, addr);
769 * search for a session with the macendpoint and peer id
771 * @param plugin pointer to the plugin struct
772 * @param endpoint pointer to the mac endpoint of the peer
773 * @param peer pointer to the peerid
774 * @return returns the session
776 static struct Session *
777 search_session (struct Plugin *plugin, const struct MacEndpoint *endpoint,
778 const struct GNUNET_PeerIdentity *peer)
780 GNUNET_assert (endpoint != NULL);
781 struct Sessionqueue *queue = endpoint->sessions_head;
783 while (queue != NULL)
785 GNUNET_assert (queue->content != NULL);
787 (peer, &queue->content->target,
788 sizeof (struct GNUNET_PeerIdentity)) == 0)
789 return queue->content; /* session found */
796 * Function called for a quick conversion of the binary address to
797 * a numeric address. Note that the caller must not free the
798 * address and that the next call to this function is allowed
799 * to override the address again.
802 * @param addr binary address
803 * @param addrlen length of the address
804 * @return string representing the same address
807 wlan_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
810 const struct MacAddress *mac;
812 if (addrlen != sizeof (struct MacAddress))
818 GNUNET_snprintf (ret, sizeof (ret), "%s Mac-Address %X:%X:%X:%X:%X:%X",
819 PROTOCOL_PREFIX, mac->mac[0], mac->mac[1], mac->mac[2],
820 mac->mac[3], mac->mac[4], mac->mac[5]);
826 * Function for the scheduler if a session times out
827 * @param cls pointer to the Sessionqueue
828 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
831 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
833 struct Sessionqueue *queue = cls;
835 GNUNET_assert (queue != NULL);
836 GNUNET_assert(queue->content != NULL);
837 queue->content->timeout_task = GNUNET_SCHEDULER_NO_TASK;
838 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
842 if (GNUNET_TIME_absolute_get_remaining
843 (GNUNET_TIME_absolute_add
844 (queue->content->last_activity, SESSION_TIMEOUT)).rel_value == 0)
847 GNUNET_assert(queue->content->mac != NULL);
848 GNUNET_assert(queue->content->mac->plugin != NULL);
849 GNUNET_STATISTICS_update (queue->content->mac->plugin->env->stats, _("# wlan session timeouts"), 1, GNUNET_NO);
850 free_session (queue->content->mac->plugin, queue, GNUNET_YES);
854 queue->content->timeout_task =
855 GNUNET_SCHEDULER_add_delayed (SESSION_TIMEOUT, &session_timeout, queue);
860 * create a new session
862 * @param plugin pointer to the plugin struct
863 * @param endpoint pointer to the mac endpoint of the peer
864 * @param peer peer identity to use for this session
865 * @return returns the session
868 static struct Session *
869 create_session (struct Plugin *plugin, struct MacEndpoint *endpoint,
870 const struct GNUNET_PeerIdentity *peer)
872 GNUNET_assert (endpoint != NULL);
873 GNUNET_assert (plugin != NULL);
874 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan session created"), 1, GNUNET_NO);
875 struct Sessionqueue *queue =
876 GNUNET_malloc (sizeof (struct Sessionqueue) + sizeof (struct Session));
878 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
879 endpoint->sessions_tail, queue);
881 queue->content = (struct Session *) &queue[1];
882 queue->content->mac = endpoint;
883 memcpy (&(queue->content->target), peer, sizeof (struct GNUNET_PeerIdentity));
884 queue->content->last_activity = GNUNET_TIME_absolute_get ();
885 queue->content->timeout_task =
886 GNUNET_SCHEDULER_add_delayed (SESSION_TIMEOUT, &session_timeout, queue);
889 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
890 "New session %p with endpoint %p: %s\n", queue->content,
891 endpoint, wlan_plugin_address_to_string (NULL,
896 return queue->content;
900 * Get session from address, create if no session exists
902 * @param plugin pointer to the plugin struct
903 * @param addr pointer to the mac address of the peer
904 * @param peer pointer to the peerid
905 * @return returns the session
907 static struct Session *
908 get_session (struct Plugin *plugin, const struct MacAddress *addr,
909 const struct GNUNET_PeerIdentity *peer)
911 struct MacEndpoint *mac;
913 mac = get_macendpoint (plugin, addr, GNUNET_YES);
914 struct Session *session = search_session (plugin, mac, peer);
918 return create_session (plugin, mac, peer);
922 * Queue the session to send data
923 * checks if there is a message pending
924 * checks if this session is not allready in the queue
925 * @param plugin pointer to the plugin
926 * @param session pointer to the session to add
929 queue_session (struct Plugin *plugin, struct Session *session)
931 struct Sessionqueue *queue = plugin->pending_Sessions_head;
933 if (session->pending_message_head != NULL)
935 while (queue != NULL)
937 // content is never NULL
938 GNUNET_assert (queue->content != NULL);
939 // is session already in queue?
940 if (session == queue->content)
948 // Session is not in the queue
950 queue = GNUNET_malloc (sizeof (struct Sessionqueue));
951 queue->content = session;
954 GNUNET_CONTAINER_DLL_insert_tail (plugin->pending_Sessions_head,
955 plugin->pending_Sessions_tail, queue);
956 plugin->pendingsessions++;
957 GNUNET_STATISTICS_set(plugin->env->stats, _("# wlan pending sessions"), plugin->pendingsessions, GNUNET_NO);
963 * Function to schedule the write task, executed after a delay
964 * @param cls pointer to the plugin struct
965 * @param tc GNUNET_SCHEDULER_TaskContext pointer
968 delay_fragment_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
970 struct Plugin *plugin = cls;
972 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
974 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
977 // GNUNET_TIME_UNIT_FOREVER_REL is needed to clean up old msg
978 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
980 plugin->server_write_task =
981 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
982 plugin->server_stdin_handle,
983 &do_transmit, plugin);
988 * Function to calculate the time of the next periodic "hello-beacon"
989 * @param plugin pointer to the plugin struct
993 set_next_beacon_time (struct Plugin *const plugin)
995 //under 10 known peers: once a second
996 if (plugin->mac_count < 10)
998 plugin->beacon_time =
999 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1000 GNUNET_TIME_relative_multiply
1001 (GNUNET_TIME_UNIT_SECONDS,
1002 HALLO_BEACON_SCALING_FACTOR));
1004 //under 30 known peers: every 10 seconds
1005 else if (plugin->mac_count < 30)
1007 plugin->beacon_time =
1008 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1009 GNUNET_TIME_relative_multiply
1010 (GNUNET_TIME_UNIT_SECONDS,
1011 10 * HALLO_BEACON_SCALING_FACTOR));
1013 //over 30 known peers: once a minute
1016 plugin->beacon_time =
1017 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1018 GNUNET_TIME_relative_multiply
1019 (GNUNET_TIME_UNIT_MINUTES,
1020 HALLO_BEACON_SCALING_FACTOR));
1025 * Function to set the timer for the next timeout of the fragment queue
1026 * @param plugin the handle to the plugin struct
1030 set_next_send (struct Plugin *const plugin)
1032 struct GNUNET_TIME_Relative next_send;
1035 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
1037 GNUNET_SCHEDULER_cancel (plugin->server_write_delay_task);
1038 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
1041 //check if some acks are in the queue
1042 if (plugin->ack_send_queue_head != NULL)
1044 next_send = GNUNET_TIME_UNIT_ZERO;
1047 //check if there are some fragments in the queue
1048 else if (plugin->sending_messages_head != NULL)
1050 next_send = GNUNET_TIME_UNIT_ZERO;
1054 next_send = GNUNET_TIME_absolute_get_remaining (plugin->beacon_time);
1058 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1059 "Next packet is send in: %u\n", next_send.rel_value);
1062 if (next_send.rel_value == GNUNET_TIME_UNIT_ZERO.rel_value)
1064 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
1066 plugin->server_write_task =
1067 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1068 plugin->server_stdin_handle,
1069 &do_transmit, plugin);
1074 if (plugin->server_write_delay_task == GNUNET_SCHEDULER_NO_TASK)
1076 plugin->server_write_delay_task =
1077 GNUNET_SCHEDULER_add_delayed (next_send, &delay_fragment_task,
1084 * Function to get the next queued Session, removes the session from the queue
1085 * @param plugin pointer to the plugin struct
1086 * @return pointer to the session found, returns NULL if there is now session in the queue
1088 static struct Session *
1089 get_next_queue_session (struct Plugin *plugin)
1091 struct Session *session;
1092 struct Sessionqueue *sessionqueue;
1093 struct Sessionqueue *sessionqueue_alt;
1094 struct PendingMessage *pm;
1096 sessionqueue = plugin->pending_Sessions_head;
1098 while (sessionqueue != NULL)
1100 session = sessionqueue->content;
1102 GNUNET_assert (session != NULL);
1103 pm = session->pending_message_head;
1108 GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1109 "pending message is empty, should not happen. session %p\n",
1112 sessionqueue_alt = sessionqueue;
1113 sessionqueue = sessionqueue->next;
1114 plugin->pendingsessions--;
1115 GNUNET_STATISTICS_set(plugin->env->stats, _("# wlan pending sessions"), plugin->pendingsessions, GNUNET_NO);
1116 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1117 plugin->pending_Sessions_tail,
1120 GNUNET_free (sessionqueue_alt);
1125 //check for message timeout
1126 if (GNUNET_TIME_absolute_get_remaining (pm->timeout).rel_value > 0)
1128 //check if session has no message in the fragment queue
1129 if ((session->mac->fragment_messages_out_count <
1130 FRAGMENT_QUEUE_MESSAGES_OUT_PER_MACENDPOINT) &&
1131 (session->fragment_messages_out_count <
1132 FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION))
1134 plugin->pendingsessions--;
1135 GNUNET_STATISTICS_set(plugin->env->stats, _("# wlan pending sessions"), plugin->pendingsessions, GNUNET_NO);
1136 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1137 plugin->pending_Sessions_tail,
1139 GNUNET_free (sessionqueue);
1145 sessionqueue = sessionqueue->next;
1150 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
1151 session->pending_message_tail, pm);
1153 //call the cont func that it did not work
1154 if (pm->transmit_cont != NULL)
1155 pm->transmit_cont (pm->transmit_cont_cls, &(session->target),
1157 GNUNET_free (pm->msg);
1160 if (session->pending_message_head == NULL)
1162 sessionqueue_alt = sessionqueue;
1163 sessionqueue = sessionqueue->next;
1164 plugin->pendingsessions--;
1165 GNUNET_STATISTICS_set(plugin->env->stats, _("# wlan pending sessions"), plugin->pendingsessions, GNUNET_NO);
1166 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1167 plugin->pending_Sessions_tail,
1170 GNUNET_free (sessionqueue_alt);
1179 * frees the space of a message in the fragment queue (send queue)
1180 * @param plugin the plugin struct
1181 * @param fm message to free
1184 free_fragment_message (struct Plugin *plugin, struct FragmentMessage *fm)
1186 struct Session *session = fm->session;
1187 struct MacEndpoint *endpoint = session->mac;
1188 struct FragmentMessage_queue *fmq;
1189 struct FragmentMessage_queue *fmq_next;
1193 fmq = plugin->sending_messages_head;
1196 fmq_next = fmq->next;
1197 if (fmq->content == fm)
1199 GNUNET_CONTAINER_DLL_remove (plugin->sending_messages_head,
1200 plugin->sending_messages_tail, fmq);
1206 (session->mac->fragment_messages_out_count)--;
1207 session->fragment_messages_out_count--;
1208 plugin->pending_Fragment_Messages--;
1209 GNUNET_CONTAINER_DLL_remove (endpoint->sending_messages_head,
1210 endpoint->sending_messages_tail, fm);
1211 GNUNET_FRAGMENT_context_destroy (fm->fragcontext);
1212 if (fm->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1213 GNUNET_SCHEDULER_cancel (fm->timeout_task);
1216 queue_session (plugin, session);
1218 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1219 "Free pending fragment messages %p, session %p\n", fm,
1226 * function to fill the radiotap header
1227 * @param plugin pointer to the plugin struct
1228 * @param endpoint pointer to the endpoint
1229 * @param header pointer to the radiotap header
1230 * @return GNUNET_YES at success
1233 getRadiotapHeader (struct Plugin *plugin, struct MacEndpoint *endpoint,
1234 struct Radiotap_Send *header)
1237 if (endpoint != NULL)
1239 header->rate = endpoint->rate;
1240 header->tx_power = endpoint->tx_power;
1241 header->antenna = endpoint->antenna;
1246 header->tx_power = 0;
1247 header->antenna = 0;
1254 * function to generate the wlan hardware header for one packet
1255 * @param Header address to write the header to
1256 * @param to_mac_addr address of the recipient
1257 * @param plugin pointer to the plugin struct
1258 * @param size size of the whole packet, needed to calculate the time to send the packet
1259 * @return GNUNET_YES if there was no error
1262 getWlanHeader (struct ieee80211_frame *Header,
1263 const struct MacAddress *to_mac_addr, struct Plugin *plugin,
1267 const int rate = 11000000;
1269 Header->i_fc[0] = IEEE80211_FC0_TYPE_DATA;
1270 Header->i_fc[1] = 0x00;
1271 memcpy (&Header->i_addr3, &mac_bssid, sizeof (mac_bssid));
1272 memcpy (&Header->i_addr2, plugin->mac_address.mac,
1273 sizeof (plugin->mac_address));
1274 memcpy (&Header->i_addr1, to_mac_addr, sizeof (struct MacAddress));
1276 tmp16 = (uint16_t *) Header->i_dur;
1277 *tmp16 = (uint16_t) htole16 ((size * 1000000) / rate + 290);
1278 Header->llc[0] = WLAN_LLC_DSAP_FIELD;
1279 Header->llc[1] = WLAN_LLC_SSAP_FIELD;
1281 #if DEBUG_wlan_ip_udp_packets_on_air
1286 Header->ip.ip_dst.s_addr = *((uint32_t *) & to_mac_addr->mac[2]);
1287 Header->ip.ip_src.s_addr = *((uint32_t *) & plugin->mac_address.mac[2]);
1288 Header->ip.ip_v = 4;
1289 Header->ip.ip_hl = 5;
1290 Header->ip.ip_p = 17;
1291 Header->ip.ip_ttl = 1;
1292 Header->ip.ip_len = htons (size + 8);
1293 Header->ip.ip_sum = 0;
1294 x = (uint16_t *) & Header->ip;
1295 count = sizeof (struct iph);
1298 /* This is the inner loop */
1299 crc += (unsigned short) *x++;
1302 /* Add left-over byte, if any */
1304 crc += *(unsigned char *) x;
1305 crc = (crc & 0xffff) + (crc >> 16);
1306 Header->ip.ip_sum = htons (~(unsigned short) crc);
1307 Header->udp.len = htons (size - sizeof (struct ieee80211_frame));
1317 * @param msgbuf pointer tor the data
1318 * @param msgbuf_size size of the data
1320 * @return 32bit crc value
1324 getcrc32 (const char *msgbuf, size_t msgbuf_size)
1327 return GNUNET_CRYPTO_crc32_n (msgbuf, msgbuf_size);;
1333 * @param msgbuf pointer tor the data
1334 * @param msgbuf_size size of the data
1336 * @return 16bit crc value
1340 getcrc16 (const char *msgbuf, size_t msgbuf_size)
1342 //TODO calc some crc
1347 * function to add a fragment of a message to send
1348 * @param cls FragmentMessage this message belongs to
1349 * @param hdr pointer to the start of the message
1353 add_message_for_send (void *cls, const struct GNUNET_MessageHeader *hdr)
1356 struct FragmentMessage *fm = cls;
1357 struct FragmentMessage_queue *fmqueue;
1359 GNUNET_assert (cls != NULL);
1360 GNUNET_assert (fm->frag == NULL);
1361 struct MacEndpoint *endpoint = fm->session->mac;
1362 struct Plugin *plugin = endpoint->plugin;
1363 struct GNUNET_MessageHeader *msgheader;
1364 struct GNUNET_MessageHeader *msgheader2;
1367 #if DEBUG_wlan_retransmission
1368 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1369 "Adding fragment of message %p to send, session %p, endpoint %p, type %u\n",
1370 fm, fm->session, endpoint, hdr->type);
1374 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1375 sizeof (struct ieee80211_frame) + ntohs (hdr->size);
1376 fm->frag = GNUNET_malloc (size);
1379 msgheader = (struct GNUNET_MessageHeader *) fm->frag;
1380 msgheader->size = htons (size);
1381 msgheader->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1383 fm->radioHeader = (struct Radiotap_Send *) &msgheader[1];
1384 fm->ieeewlanheader = (struct ieee80211_frame *) &fm->radioHeader[1];
1385 msgheader2 = (struct GNUNET_MessageHeader *) &fm->ieeewlanheader[1];
1386 memcpy (msgheader2, hdr, ntohs (hdr->size));
1388 fmqueue = GNUNET_malloc (sizeof (struct FragmentMessage_queue));
1389 fmqueue->content = fm;
1391 GNUNET_CONTAINER_DLL_insert_tail (plugin->sending_messages_head,
1392 plugin->sending_messages_tail, fmqueue);
1393 set_next_send (plugin);
1397 * function to send a hallo beacon
1398 * @param plugin pointer to the plugin struct
1401 send_hello_beacon (struct Plugin *plugin)
1405 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1406 "Sending hello beacon\n");
1411 uint16_t hallo_size;
1412 struct GNUNET_MessageHeader *msgheader;
1413 struct ieee80211_frame *ieeewlanheader;
1414 struct Radiotap_Send *radioHeader;
1415 struct GNUNET_MessageHeader *msgheader2;
1416 const struct GNUNET_MessageHeader *hello;
1418 GNUNET_assert (plugin != NULL);
1420 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan hello beacons send"), 1, GNUNET_NO);
1422 hello = plugin->env->get_our_hello ();
1423 hallo_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1424 GNUNET_assert (sizeof (struct WlanHeader) + hallo_size <= WLAN_MTU);
1426 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1427 sizeof (struct ieee80211_frame) + hallo_size;
1429 msgheader = GNUNET_malloc (size);
1430 msgheader->size = htons (size);
1431 msgheader->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1433 radioHeader = (struct Radiotap_Send *) &msgheader[1];
1434 getRadiotapHeader (plugin, NULL, radioHeader);
1435 ieeewlanheader = (struct ieee80211_frame *) &radioHeader[1];
1436 getWlanHeader (ieeewlanheader, &bc_all_mac, plugin, size);
1438 msgheader2 = (struct GNUNET_MessageHeader *) &ieeewlanheader[1];
1439 /*msgheader2->size =
1440 htons (GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello) +
1441 sizeof (struct GNUNET_MessageHeader));
1443 msgheader2->type = htons (GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT);*/
1444 memcpy (msgheader2, hello, hallo_size);
1446 bytes = GNUNET_DISK_file_write (plugin->server_stdin_handle, msgheader, size);
1448 if (bytes == GNUNET_SYSERR)
1450 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1452 ("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1453 errno, strerror (errno));
1456 GNUNET_assert (bytes != GNUNET_SYSERR);
1457 GNUNET_assert (bytes == size);
1458 GNUNET_free (msgheader);
1460 set_next_beacon_time (plugin);
1461 set_next_send (plugin);
1465 * function to add an ack to send it for a received fragment
1466 * @param cls MacEndpoint this ack belongs to
1467 * @param msg_id id of the message
1468 * @param hdr pointer to the hdr where the ack is stored
1473 add_ack_for_send (void *cls, uint32_t msg_id,
1474 const struct GNUNET_MessageHeader *hdr)
1477 struct AckSendQueue *ack;
1479 GNUNET_assert (cls != NULL);
1480 struct MacEndpoint *endpoint = cls;
1481 struct Plugin *plugin = endpoint->plugin;
1482 struct GNUNET_MessageHeader *msgheader;
1483 struct GNUNET_MessageHeader *msgheader2;
1487 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1488 sizeof (struct ieee80211_frame) + ntohs (hdr->size) +
1489 sizeof (struct AckSendQueue);
1491 ack = GNUNET_malloc (size);
1492 ack->message_id = msg_id;
1493 ack->endpoint = endpoint;
1496 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1497 sizeof (struct ieee80211_frame) + ntohs (hdr->size);
1499 msgheader = (struct GNUNET_MessageHeader *) &ack[1];
1500 ack->hdr = (struct GNUNET_MessageHeader *) &ack[1];
1501 msgheader->size = htons (size);
1502 msgheader->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1504 ack->radioHeader = (struct Radiotap_Send *) &msgheader[1];
1505 ack->ieeewlanheader = (struct ieee80211_frame *) &(ack->radioHeader)[1];
1506 msgheader2 = (struct GNUNET_MessageHeader *) &(ack->ieeewlanheader)[1];
1507 memcpy (msgheader2, hdr, ntohs (hdr->size));
1509 GNUNET_CONTAINER_DLL_insert_tail (plugin->ack_send_queue_head,
1510 plugin->ack_send_queue_tail, ack);
1512 #if DEBUG_wlan_retransmission
1513 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1514 "Adding ack with message id %u to send, AckSendQueue %p, endpoint %p\n",
1515 msg_id, ack, endpoint);
1518 set_next_send (plugin);
1522 * Function for the scheduler if a FragmentMessage times out
1523 * @param cls pointer to the FragmentMessage
1524 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
1527 fragmentmessage_timeout (void *cls,
1528 const struct GNUNET_SCHEDULER_TaskContext *tc)
1530 struct FragmentMessage *fm = cls;
1532 GNUNET_assert (fm != NULL);
1533 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1534 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1538 free_fragment_message (fm->session->mac->plugin, fm);
1542 * Function to check if there is some space in the fragment queue
1543 * inserts a message if space is available
1544 * @param plugin the plugin struct
1548 check_fragment_queue (struct Plugin *plugin)
1550 struct Session *session;
1551 struct FragmentMessage *fm;
1552 struct GNUNET_PeerIdentity pid;
1554 struct PendingMessage *pm;
1556 if (plugin->pending_Fragment_Messages < FRAGMENT_QUEUE_SIZE)
1558 session = get_next_queue_session (plugin);
1559 if (session != NULL)
1561 pm = session->pending_message_head;
1562 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
1563 session->pending_message_tail, pm);
1564 session->mac->fragment_messages_out_count++;
1565 session->fragment_messages_out_count++;
1566 plugin->pending_Fragment_Messages++;
1567 GNUNET_assert (pm != NULL);
1569 fm = GNUNET_malloc (sizeof (struct FragmentMessage));
1570 fm->session = session;
1571 fm->timeout.abs_value = pm->timeout.abs_value;
1574 GNUNET_FRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
1576 GNUNET_TIME_UNIT_SECONDS,
1578 &add_message_for_send, fm);
1580 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
1581 (fm->timeout), fragmentmessage_timeout,
1583 GNUNET_CONTAINER_DLL_insert_tail (session->mac->sending_messages_head,
1584 session->mac->sending_messages_tail,
1587 if (pm->transmit_cont != NULL)
1589 pid = session->target;
1590 pm->transmit_cont (pm->transmit_cont_cls, &pid, GNUNET_OK);
1592 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1593 "called pm->transmit_cont for %p\n", session);
1599 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1600 "no pm->transmit_cont for %p\n", session);
1605 if (session->pending_message_head != NULL)
1608 queue_session (plugin, session);
1614 //check if timeout changed
1615 set_next_send (plugin);
1619 * Function to send an ack, does not free the ack
1620 * @param plugin pointer to the plugin
1621 * @param ack pointer to the ack to send
1624 send_ack (struct Plugin *plugin, struct AckSendQueue *ack)
1630 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1631 "Sending ack for message_id %u for mac endpoint %p, size %u\n",
1632 ack->message_id, ack->endpoint,
1633 ntohs (ack->hdr->size) - sizeof (struct Radiotap_Send));
1636 GNUNET_assert (plugin != NULL);
1637 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan acks send"), 1, GNUNET_NO);
1639 getRadiotapHeader (plugin, ack->endpoint, ack->radioHeader);
1640 getWlanHeader (ack->ieeewlanheader, &ack->endpoint->addr, plugin,
1641 ntohs (ack->hdr->size));
1644 GNUNET_DISK_file_write (plugin->server_stdin_handle, ack->hdr,
1645 ntohs (ack->hdr->size));
1646 if (bytes == GNUNET_SYSERR)
1648 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1650 ("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1651 errno, strerror (errno));
1654 GNUNET_assert (bytes != GNUNET_SYSERR);
1655 GNUNET_assert (bytes == ntohs (ack->hdr->size));
1656 set_next_send (plugin);
1660 * function to finish a sending if not all could have been writen befor
1661 * @param cls pointer to the Finish_send struct
1662 * @param tc TaskContext
1665 finish_sending (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1667 struct Finish_send *finish = cls;
1668 struct Plugin *plugin;
1671 plugin = finish->plugin;
1672 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1674 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1676 GNUNET_free (finish->msgstart);
1677 GNUNET_free (finish);
1681 GNUNET_DISK_file_write (plugin->server_stdin_handle, finish->msgheader,
1683 GNUNET_assert (bytes != GNUNET_SYSERR);
1685 if (bytes != finish->size)
1687 finish->msgheader = finish->msgheader + bytes;
1688 finish->size = finish->size - bytes;
1689 plugin->server_write_task =
1690 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1691 plugin->server_stdin_handle,
1692 &finish_sending, finish);
1696 GNUNET_free (finish->msgstart);
1697 GNUNET_free (finish);
1698 set_next_send (plugin);
1703 * Function called when wlan helper is ready to get some data
1705 * @param cls closure
1706 * @param tc GNUNET_SCHEDULER_TaskContext
1709 do_transmit (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1711 struct Plugin *plugin = cls;
1712 GNUNET_assert (plugin != NULL);
1714 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1715 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1718 struct Session *session;
1719 struct FragmentMessage *fm;
1720 struct Finish_send *finish;
1721 struct FragmentMessage_queue *fmq;
1722 struct AckSendQueue *ack;
1725 if (plugin->ack_send_queue_head != NULL)
1727 ack = plugin->ack_send_queue_head;
1728 GNUNET_CONTAINER_DLL_remove (plugin->ack_send_queue_head,
1729 plugin->ack_send_queue_tail, ack);
1730 send_ack (plugin, ack);
1735 //test if a "hello-beacon" has to be send
1736 if (GNUNET_TIME_absolute_get_remaining (plugin->beacon_time).rel_value == 0)
1738 send_hello_beacon (plugin);
1742 if (plugin->sending_messages_head != NULL)
1744 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan fragments send"), 1, GNUNET_NO);
1746 fmq = plugin->sending_messages_head;
1748 GNUNET_CONTAINER_DLL_remove (plugin->sending_messages_head,
1749 plugin->sending_messages_tail, fmq);
1752 session = fm->session;
1753 GNUNET_assert (session != NULL);
1756 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1757 "Sending GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT for fragment message %p, size: %u\n",
1761 getRadiotapHeader (plugin, session->mac, fm->radioHeader);
1762 getWlanHeader (fm->ieeewlanheader, &(fm->session->mac->addr), plugin,
1766 GNUNET_DISK_file_write (plugin->server_stdin_handle, fm->frag,
1768 if (bytes == GNUNET_SYSERR)
1770 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1772 ("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1773 errno, strerror (errno));
1776 GNUNET_assert (bytes != GNUNET_SYSERR);
1778 if (bytes != fm->size)
1780 finish = GNUNET_malloc (sizeof (struct Finish_send));
1781 finish->plugin = plugin;
1782 finish->msgheader = fm->frag + bytes;
1783 finish->size = fm->size - bytes;
1784 finish->msgstart = (struct GNUNET_MessageHeader *) fm->frag;
1786 GNUNET_assert (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK);
1788 plugin->server_write_task =
1789 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1790 plugin->server_stdin_handle,
1791 &finish_sending, finish);
1796 GNUNET_free (fm->frag);
1798 set_next_send (plugin);
1800 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
1804 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
1805 "do_transmit did nothing, should not happen!\n");
1809 * Another peer has suggested an address for this
1810 * peer and transport plugin. Check that this could be a valid
1811 * address. If so, consider adding it to the list
1814 * @param cls closure
1815 * @param addr pointer to the address
1816 * @param addrlen length of addr
1817 * @return GNUNET_OK if this is a plausible address for this peer
1821 wlan_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1823 //struct Plugin *plugin = cls;
1825 /* check if the address is plausible; if so,
1826 * add it to our list! */
1828 GNUNET_assert (cls != NULL);
1829 //FIXME mitm is not checked
1830 //Mac Address has 6 bytes
1833 /* TODO check for bad addresses like multicast, broadcast, etc */
1835 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1836 "wlan_plugin_address_suggested got good address, size %u!\n", addrlen);
1841 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1842 "wlan_plugin_address_suggested got bad address, size %u!\n", addrlen);
1844 return GNUNET_SYSERR;
1848 * Function that can be used by the transport service to transmit
1849 * a message using the plugin.
1851 * @param cls closure
1852 * @param target who should receive this message
1853 * @param priority how important is the message
1854 * @param msgbuf the message to transmit
1855 * @param msgbuf_size number of bytes in 'msgbuf'
1856 * @param timeout when should we time out
1857 * @param session which session must be used (or NULL for "any")
1858 * @param addr the address to use (can be NULL if the plugin
1859 * is "on its own" (i.e. re-use existing TCP connection))
1860 * @param addrlen length of the address in bytes
1861 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1862 * otherwise the plugin may use other addresses or
1863 * existing connections (if available)
1864 * @param cont continuation to call once the message has
1865 * been transmitted (or if the transport is ready
1866 * for the next transmission call; or if the
1867 * peer disconnected...)
1868 * @param cont_cls closure for cont
1869 * @return number of bytes used (on the physical network, with overheads);
1870 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1871 * and does NOT mean that the message was not transmitted (DV)
1874 wlan_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
1875 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
1876 struct GNUNET_TIME_Relative timeout, struct Session *session,
1877 const void *addr, size_t addrlen, int force_address,
1878 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1880 struct Plugin *plugin = cls;
1881 struct PendingMessage *newmsg;
1882 struct WlanHeader *wlanheader;
1883 GNUNET_assert (plugin != NULL);
1884 //check if msglen > 0
1885 GNUNET_assert (msgbuf_size > 0);
1887 //get session if needed
1888 if (session == NULL)
1890 if (wlan_plugin_address_suggested (plugin, addr, addrlen) == GNUNET_OK)
1892 session = get_session (plugin, addr, target);
1896 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1897 _("Wlan Address len %d is wrong\n"), addrlen);
1902 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan messages queued"), 1, GNUNET_NO);
1906 //queue message in session
1907 //test if there is no other message in the "queue"
1908 //FIXME: to many send requests
1909 if (session->pending_message_head != NULL)
1911 newmsg = session->pending_message_head;
1912 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1913 "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",
1914 GNUNET_TIME_absolute_get_remaining (newmsg->
1916 session->mac->fragment_messages_out_count);
1919 newmsg = GNUNET_malloc (sizeof (struct PendingMessage));
1920 newmsg->msg = GNUNET_malloc (msgbuf_size + sizeof (struct WlanHeader));
1921 wlanheader = newmsg->msg;
1922 //copy msg to buffer, not fragmented / segmented yet, but with message header
1923 wlanheader->header.size = htons (msgbuf_size + sizeof (struct WlanHeader));
1924 wlanheader->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA);
1925 memcpy (&(wlanheader->target), target, sizeof (struct GNUNET_PeerIdentity));
1926 memcpy (&(wlanheader->source), plugin->env->my_identity,
1927 sizeof (struct GNUNET_PeerIdentity));
1928 wlanheader->crc = 0;
1929 memcpy (&wlanheader[1], msgbuf, msgbuf_size);
1932 ((char *) wlanheader, msgbuf_size + sizeof (struct WlanHeader)));
1933 //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);
1934 //hexdump(newmsg->msg, msgbuf_size + sizeof(struct WlanHeader));
1936 newmsg->transmit_cont = cont;
1937 newmsg->transmit_cont_cls = cont_cls;
1938 newmsg->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1940 newmsg->timeout.abs_value = newmsg->timeout.abs_value - 500;
1942 newmsg->message_size = msgbuf_size + sizeof (struct WlanHeader);
1944 GNUNET_CONTAINER_DLL_insert_tail (session->pending_message_head,
1945 session->pending_message_tail, newmsg);
1948 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1949 "New message for %p with size (incl wlan header) %u added\n",
1950 session, newmsg->message_size);
1952 #if DEBUG_wlan_msg_dump
1953 hexdump (msgbuf, GNUNET_MIN (msgbuf_size, 256));
1956 queue_session (plugin, session);
1958 check_fragment_queue (plugin);
1959 //FIXME not the correct size
1965 * function to free a mac endpoint
1966 * @param plugin pointer to the plugin struct
1967 * @param endpoint pointer to the MacEndpoint to free
1970 free_macendpoint (struct Plugin *plugin, struct MacEndpoint *endpoint)
1972 struct Sessionqueue *sessions;
1973 struct Sessionqueue *sessions_next;
1975 GNUNET_assert (endpoint != NULL);
1977 sessions = endpoint->sessions_head;
1978 while (sessions != NULL)
1980 sessions_next = sessions->next;
1981 free_session (plugin, sessions, GNUNET_NO);
1982 sessions = sessions_next;
1985 GNUNET_CONTAINER_DLL_remove (plugin->mac_head, plugin->mac_tail, endpoint);
1986 if (endpoint->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1987 GNUNET_SCHEDULER_cancel (endpoint->timeout_task);
1988 GNUNET_free (endpoint);
1993 * function to free a session
1994 * @param plugin pointer to the plugin
1995 * @param queue pointer to the sessionqueue element to free
1996 * @param free_macendpoint if GNUNET_YES and mac endpoint would be empty, free mac endpoint
1999 free_session (struct Plugin *plugin, struct Sessionqueue *queue,
2000 int do_free_macendpoint)
2002 struct Sessionqueue *pendingsession;
2003 struct Sessionqueue *pendingsession_tmp;
2004 struct PendingMessage *pm;
2005 struct MacEndpoint *endpoint;
2006 struct FragmentMessage *fm;
2007 struct FragmentMessage *fmnext;
2010 GNUNET_assert (plugin != NULL);
2011 GNUNET_assert (queue != NULL);
2012 GNUNET_assert (queue->content != NULL);
2015 //is this session pending for send
2016 pendingsession = plugin->pending_Sessions_head;
2017 while (pendingsession != NULL)
2019 pendingsession_tmp = pendingsession;
2020 pendingsession = pendingsession->next;
2021 GNUNET_assert (pendingsession_tmp->content != NULL);
2022 if (pendingsession_tmp->content == queue->content)
2024 plugin->pendingsessions--;
2025 GNUNET_STATISTICS_set(plugin->env->stats, _("# wlan pending sessions"), plugin->pendingsessions, GNUNET_NO);
2026 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
2027 plugin->pending_Sessions_tail,
2028 pendingsession_tmp);
2029 GNUNET_free (pendingsession_tmp);
2031 GNUNET_assert (check == 0);
2036 endpoint = queue->content->mac;
2037 fm = endpoint->sending_messages_head;
2041 if (fm->session == queue->content)
2043 free_fragment_message (plugin, fm);
2048 // remove PendingMessage
2049 pm = queue->content->pending_message_head;
2052 GNUNET_CONTAINER_DLL_remove (queue->content->pending_message_head,
2053 queue->content->pending_message_tail, pm);
2054 GNUNET_free (pm->msg);
2056 pm = queue->content->pending_message_head;
2059 GNUNET_CONTAINER_DLL_remove (endpoint->sessions_head, endpoint->sessions_tail,
2062 if (endpoint->sessions_head == NULL && do_free_macendpoint == GNUNET_YES)
2064 free_macendpoint (plugin, endpoint);
2067 if (queue->content->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2068 GNUNET_SCHEDULER_cancel (queue->content->timeout_task);
2069 GNUNET_free (queue);
2070 check_fragment_queue (plugin);
2074 * Function that can be used to force the plugin to disconnect
2075 * from the given peer and cancel all previous transmissions
2076 * (and their continuation).
2078 * @param cls closure
2079 * @param target peer from which to disconnect
2082 wlan_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
2084 struct Plugin *plugin = cls;
2085 struct Sessionqueue *queue;
2086 struct Sessionqueue *queue_next;
2087 struct MacEndpoint *endpoint = plugin->mac_head;
2088 struct MacEndpoint *endpoint_next;
2090 // just look at all the session for the needed one
2091 while (endpoint != NULL)
2093 queue = endpoint->sessions_head;
2094 endpoint_next = endpoint->next;
2095 while (queue != NULL)
2097 // content is never NULL
2098 GNUNET_assert (queue->content != NULL);
2099 queue_next = queue->next;
2101 (target, &(queue->content->target),
2102 sizeof (struct GNUNET_PeerIdentity)) == 0)
2104 free_session (plugin, queue, GNUNET_YES);
2109 endpoint = endpoint_next;
2114 * Convert the transports address to a nice, human-readable
2117 * @param cls closure
2118 * @param type name of the transport that generated the address
2119 * @param addr one of the addresses of the host, NULL for the last address
2120 * the specific address format depends on the transport
2121 * @param addrlen length of the address
2122 * @param numeric should (IP) addresses be displayed in numeric form?
2123 * @param timeout after how long should we give up?
2124 * @param asc function to call on each string
2125 * @param asc_cls closure for asc
2128 wlan_plugin_address_pretty_printer (void *cls, const char *type,
2129 const void *addr, size_t addrlen,
2131 struct GNUNET_TIME_Relative timeout,
2132 GNUNET_TRANSPORT_AddressStringCallback asc,
2136 const unsigned char *input;
2138 //GNUNET_assert(cls !=NULL);
2139 if (addrlen != sizeof(struct MacAddress))
2141 /* invalid address (MAC addresses have 6 bytes) */
2144 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2145 "Func wlan_plugin_address_pretty_printer got size: %u, worng size!\n",
2151 input = (const unsigned char *) addr;
2152 GNUNET_asprintf (&ret,
2153 "Transport %s: %s Mac-Address %.2X:%.2X:%.2X:%.2X:%.2X:%.2X",type,
2154 PROTOCOL_PREFIX, input[0], input[1], input[2], input[3],
2155 input[4], input[5]);
2157 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2158 "Func wlan_plugin_address_pretty_printer got size: %u, nummeric %u, type %s; made string: %s\n",
2159 addrlen, numeric, type, ret);
2161 asc ( asc_cls, ret);
2162 //only one mac address per plugin
2163 asc ( asc_cls, NULL);
2169 * handels the data after all fragments are put together
2170 * @param cls macendpoint this messages belongs to
2171 * @param hdr pointer to the data
2174 wlan_data_message_handler (void *cls, const struct GNUNET_MessageHeader *hdr)
2176 struct MacEndpoint *endpoint = (struct MacEndpoint *) cls;
2177 struct Plugin *plugin = endpoint->plugin;
2178 struct WlanHeader *wlanheader;
2179 struct Session *session;
2181 const struct GNUNET_MessageHeader *temp_hdr;
2182 struct GNUNET_PeerIdentity tmpsource;
2185 GNUNET_assert (plugin != NULL);
2187 if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_DATA)
2191 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2192 "Func wlan_data_message_handler got GNUNET_MESSAGE_TYPE_WLAN_DATA size: %u\n",
2196 if (ntohs (hdr->size) <
2197 sizeof (struct WlanHeader) + sizeof (struct GNUNET_MessageHeader))
2199 //packet not big enought
2203 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan whole messages received"), 1, GNUNET_NO);
2204 wlanheader = (struct WlanHeader *) hdr;
2206 session = search_session (plugin, endpoint, &wlanheader->source);
2208 temp_hdr = (const struct GNUNET_MessageHeader *) &wlanheader[1];
2209 crc = ntohl (wlanheader->crc);
2210 wlanheader->crc = 0;
2211 if (getcrc32 ((char *) wlanheader, ntohs (wlanheader->header.size)) != crc)
2213 //wrong crc, dispose message
2214 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2215 "Wlan message header crc was wrong: %u != %u\n",
2216 getcrc32 ((char *) wlanheader,
2217 ntohs (wlanheader->header.size)), crc);
2218 hexdump ((void *) hdr, ntohs (hdr->size));
2222 //if not in session list
2223 if (session == NULL)
2226 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2227 "WLAN client not in session list: packet size = %u, inner size = %u, header size = %u\n",
2228 ntohs (wlanheader->header.size), ntohs (temp_hdr->size),
2229 sizeof (struct WlanHeader));
2231 //try if it is a hello message
2232 if (ntohs (wlanheader->header.size) >=
2233 ntohs (temp_hdr->size) + sizeof (struct WlanHeader))
2235 if (ntohs (temp_hdr->type) == GNUNET_MESSAGE_TYPE_HELLO)
2237 if (GNUNET_HELLO_get_id
2238 ((const struct GNUNET_HELLO_Message *) temp_hdr,
2239 &tmpsource) == GNUNET_OK)
2241 session = create_session (plugin, endpoint, &tmpsource);
2245 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2246 "WLAN client not in session list and hello message is not okay\n");
2253 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2254 "WLAN client not in session list and not a hello message\n");
2260 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2261 "WLAN client not in session list and message size in does not fit\npacket size = %u, inner size = %u, header size = %u\n",
2262 ntohs (wlanheader->header.size),
2263 ntohs (temp_hdr->size), sizeof (struct WlanHeader));
2268 //"receive" the message
2271 (&wlanheader->source, &session->target,
2272 sizeof (struct GNUNET_PeerIdentity)) != 0)
2276 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2277 "WLAN peer source id doesn't match packet peer source id: session %p\n",
2284 (&wlanheader->target, plugin->env->my_identity,
2285 sizeof (struct GNUNET_PeerIdentity)) != 0)
2289 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2290 "WLAN peer target id doesn't match our peer id: session %p\n",
2296 GNUNET_SERVER_mst_receive (plugin->data_tokenizer, session,
2297 (const char *) temp_hdr,
2298 ntohs (hdr->size) - sizeof (struct WlanHeader),
2299 GNUNET_YES, GNUNET_NO);
2305 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2306 "wlan_data_message_handler got wrong message type: %u\n",
2313 * function to process the a message, give it to the higher layer
2314 * @param cls pointer to the plugin
2315 * @param client pointer to the session this message belongs to
2316 * @param hdr start of the message
2318 //TODO ATS informations
2320 process_data (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
2323 GNUNET_assert (client != NULL);
2324 GNUNET_assert (cls != NULL);
2325 struct Session *session = (struct Session *) client;
2326 struct Plugin *plugin = (struct Plugin *) cls;
2328 struct GNUNET_TRANSPORT_ATS_Information distance[2];
2330 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2331 distance[0].value = htonl (1);
2332 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2333 distance[1].value = htonl (0);
2336 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2337 "Calling plugin->env->receive for session %p; %s; size: %u\n",
2338 session, wlan_plugin_address_to_string (NULL,
2344 plugin->env->receive (plugin->env->cls, &(session->target), hdr,
2345 (const struct GNUNET_TRANSPORT_ATS_Information *)
2346 &distance, 2, session,
2347 (const char *) &session->mac->addr,
2348 sizeof (session->mac->addr));
2352 * Function used for to process the data received from the wlan interface
2354 * @param cls the plugin handle
2355 * @param session_light pointer to the struct holding known informations
2356 * @param hdr hdr of the GNUNET_MessageHeader
2357 * @param rxinfo pointer to the radiotap informations got with this packet
2360 wlan_data_helper (void *cls, struct Session_light *session_light,
2361 const struct GNUNET_MessageHeader *hdr,
2362 const struct Radiotap_rx *rxinfo)
2364 struct Plugin *plugin = cls;
2365 struct FragmentMessage *fm;
2366 struct FragmentMessage *fm2;
2367 struct GNUNET_PeerIdentity tmpsource;
2369 GNUNET_assert(plugin != NULL);
2372 if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_HELLO)
2375 //TODO better DOS protection, error handling
2376 //TODO test first than create session
2377 GNUNET_assert (session_light != NULL);
2380 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2381 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_HELLO size: %u; %s\n",
2382 ntohs (hdr->size), wlan_plugin_address_to_string (NULL,
2383 session_light->addr.
2387 if (session_light->macendpoint == NULL)
2389 session_light->macendpoint =
2390 get_macendpoint (plugin, &session_light->addr, GNUNET_YES);
2394 if (GNUNET_HELLO_get_id
2395 ((const struct GNUNET_HELLO_Message *) hdr,
2396 &tmpsource) == GNUNET_OK)
2398 session_light->session = search_session (plugin, session_light->macendpoint, &tmpsource);
2399 if (session_light->session == NULL)
2401 session_light->session = create_session (plugin, session_light->macendpoint, &tmpsource);
2403 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan hallo messages received"), 1, GNUNET_NO);
2404 plugin->env->receive(plugin->env->cls,&session_light->session->target,hdr, NULL, 0, session_light->session,
2405 (const char *) &session_light->session->mac->addr,
2406 sizeof (session_light->session->mac->addr));
2410 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2411 "WLAN client not in session list and hello message is not okay\n");
2418 else if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_FRAGMENT)
2421 GNUNET_assert (session_light != NULL);
2422 if (session_light->macendpoint == NULL)
2424 session_light->macendpoint =
2425 get_macendpoint (plugin, &session_light->addr, GNUNET_YES);
2429 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2430 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_FRAGMENT with size: %u; mac endpoint %p: %s\n",
2431 ntohs (hdr->size), session_light->macendpoint,
2432 wlan_plugin_address_to_string (NULL,
2433 session_light->addr.mac,
2437 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan fragments received"), 1, GNUNET_NO);
2439 GNUNET_DEFRAGMENT_process_fragment (session_light->macendpoint->defrag,
2442 if (ret == GNUNET_NO)
2444 session_light->macendpoint->dups++;
2446 else if (ret == GNUNET_OK)
2448 session_light->macendpoint->fragc++;
2450 set_next_send (plugin);
2456 else if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_FRAGMENT_ACK)
2458 GNUNET_assert (session_light != NULL);
2459 if (session_light->macendpoint == NULL)
2461 session_light->macendpoint =
2462 get_macendpoint (plugin, &session_light->addr, GNUNET_NO);
2465 if (session_light->macendpoint == NULL)
2468 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2469 "Macendpoint does not exist for this GNUNET_MESSAGE_TYPE_FRAGMENT_ACK size: %u; %s\n",
2470 ntohs (hdr->size), wlan_plugin_address_to_string (NULL,
2471 session_light->addr.mac,
2478 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2479 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_FRAGMENT_ACK size: %u; mac endpoint: %p; %s\n",
2480 ntohs (hdr->size), session_light->macendpoint,
2481 wlan_plugin_address_to_string (NULL,
2482 session_light->addr.mac,
2485 fm = session_light->macendpoint->sending_messages_head;
2489 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan acks received"), 1, GNUNET_NO);
2490 int ret = GNUNET_FRAGMENT_process_ack (fm->fragcontext, hdr);
2492 if (ret == GNUNET_OK)
2494 #if DEBUG_wlan_retransmission
2495 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2496 "Got last ack, finished fragment message %p\n", fm);
2498 session_light->macendpoint->acks++;
2499 fm->session->last_activity = GNUNET_TIME_absolute_get ();
2500 session_light->macendpoint->last_activity = fm->session->last_activity;
2501 free_fragment_message (plugin, fm);
2502 check_fragment_queue (plugin);
2505 if (ret == GNUNET_NO)
2507 #if DEBUG_wlan_retransmission
2508 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2509 "Got ack for: %p\n", fm);
2511 session_light->macendpoint->acks++;
2514 if (ret == GNUNET_SYSERR)
2522 #if DEBUG_wlan_retransmission
2523 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2524 "WLAN fragment not in fragment list\n");
2532 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2533 "WLAN packet inside the WLAN helper packet has not the right type: %u size: %u\n",
2534 ntohs (hdr->type), ntohs (hdr->size));
2540 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2541 "Helper finished\n");
2547 * Function to print mac addresses nice *
2548 * @param pointer to 6 byte with the mac address
2549 * @return pointer to the chars which hold the print out
2552 macprinter (const u_int8_t * mac)
2554 static char macstr[20];
2556 GNUNET_snprintf (macstr, sizeof (macstr), "%X:%X:%X:%X:%X:%X", mac[0], mac[1],
2557 mac[2], mac[3], mac[4], mac[5]);
2562 * Function for the scheduler if a mac endpoint times out
2563 * @param cls pointer to the MacEndpoint
2564 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
2567 macendpoint_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2569 struct MacEndpoint *endpoint = cls;
2571 GNUNET_assert (endpoint != NULL);
2572 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2573 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2577 if (GNUNET_TIME_absolute_get_remaining
2578 (GNUNET_TIME_absolute_add
2579 (endpoint->last_activity, MACENDPOINT_TIMEOUT)).rel_value == 0)
2581 GNUNET_assert(endpoint->plugin != NULL);
2582 GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# wlan mac endpoints timeouts"), 1, GNUNET_NO);
2583 free_macendpoint (endpoint->plugin, endpoint);
2587 endpoint->timeout_task =
2588 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
2594 * function to create an macendpoint
2595 * @param plugin pointer to the plugin struct
2596 * @param addr pointer to the macaddress
2597 * @return returns a macendpoint
2599 static struct MacEndpoint *
2600 create_macendpoint (struct Plugin *plugin, const struct MacAddress *addr)
2602 struct MacEndpoint *newend = GNUNET_malloc (sizeof (struct MacEndpoint));
2604 GNUNET_assert(plugin != NULL);
2605 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan mac endpoints created"), 1, GNUNET_NO);
2606 newend->addr = *addr;
2607 newend->plugin = plugin;
2608 newend->addr = *addr;
2609 newend->fragment_messages_out_count = 0;
2611 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
2612 MESSAGES_IN_DEFRAG_QUEUE_PER_MAC,
2613 newend, &wlan_data_message_handler,
2615 newend->last_activity = GNUNET_TIME_absolute_get ();
2616 newend->timeout_task =
2617 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
2620 plugin->mac_count++;
2621 GNUNET_CONTAINER_DLL_insert_tail (plugin->mac_head, plugin->mac_tail, newend);
2623 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2624 "New Mac Endpoint %p: %s\n", newend,
2625 wlan_plugin_address_to_string (NULL, newend->addr.mac, 6));
2631 * Function used for to process the data from the suid process
2633 * @param cls the plugin handle
2634 * @param client client that send the data (not used)
2635 * @param hdr header of the GNUNET_MessageHeader
2638 wlan_process_helper (void *cls, void *client,
2639 const struct GNUNET_MessageHeader *hdr)
2641 struct Plugin *plugin = cls;
2642 struct ieee80211_frame *wlanIeeeHeader = NULL;
2643 struct Session_light *session_light = NULL;
2644 struct Radiotap_rx *rxinfo;
2645 const struct GNUNET_MessageHeader *temp_hdr = NULL;
2650 GNUNET_assert(plugin != NULL);
2651 switch (ntohs (hdr->type))
2653 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA:
2655 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2656 "Func wlan_process_helper got GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA size: %u\n",
2660 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan WLAN_HELPER_DATA received"), 1, GNUNET_NO);
2661 //call wlan_process_helper with the message inside, later with wlan: analyze signal
2662 if (ntohs (hdr->size) <
2663 sizeof (struct ieee80211_frame) + 2*sizeof (struct GNUNET_MessageHeader) +
2664 sizeof (struct Radiotap_rx))
2667 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2668 "Size of packet is too small; size: %u min size: %u\n",
2670 sizeof (struct ieee80211_frame) +
2671 sizeof (struct GNUNET_MessageHeader));
2674 /* FIXME: restart SUID process */
2678 rxinfo = (struct Radiotap_rx *) &hdr[1];
2679 wlanIeeeHeader = (struct ieee80211_frame *) &rxinfo[1];
2681 //process only if it is an broadcast or for this computer both with the gnunet bssid
2685 (&(wlanIeeeHeader->i_addr3), &mac_bssid,
2686 sizeof (struct MacAddress)) == 0)
2688 //check for broadcast or mac
2690 (&(wlanIeeeHeader->i_addr1), &bc_all_mac,
2691 sizeof (struct MacAddress)) == 0) ||
2692 (memcmp (&(wlanIeeeHeader->i_addr1), &(plugin->mac_address),
2693 sizeof (struct MacAddress)) == 0))
2695 //if packet is from us return
2696 if ((memcmp (&(wlanIeeeHeader->i_addr2), &(plugin->mac_address),
2697 sizeof (struct MacAddress)) == 0)){
2700 // process the inner data
2704 ntohs (hdr->size) - sizeof (struct ieee80211_frame) -
2705 sizeof (struct GNUNET_MessageHeader) - sizeof (struct Radiotap_rx);
2707 session_light = GNUNET_malloc (sizeof (struct Session_light));
2708 memcpy (&session_light->addr, &(wlanIeeeHeader->i_addr2),
2709 sizeof (struct MacAddress));
2710 //session_light->session = search_session(plugin,session_light->addr);
2711 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan messaged for this client received"), 1, GNUNET_NO);
2714 while (pos < datasize)
2716 temp_hdr = (struct GNUNET_MessageHeader *) &wlanIeeeHeader[1] + pos;
2717 if (ntohs(temp_hdr->size) <= datasize + pos)
2719 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan messaged inside WLAN_HELPER_DATA received"), 1, GNUNET_NO);
2720 wlan_data_helper (plugin, session_light, temp_hdr, rxinfo);
2725 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2726 "Size of packet is too small; size: %u > size of packet: %u\n",
2727 ntohs(temp_hdr->size),datasize + pos);
2730 pos += ntohs (temp_hdr->size);
2735 GNUNET_free (session_light);
2740 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2741 "Func wlan_process_helper got wrong MAC: %s\n",
2742 macprinter (wlanIeeeHeader->i_addr1));
2749 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2750 "Func wlan_process_helper got wrong BSSID: %s\n",
2751 macprinter (wlanIeeeHeader->i_addr2));
2755 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
2756 //TODO more control messages
2757 if (ntohs (hdr->size) != sizeof (struct Wlan_Helper_Control_Message))
2760 /* FIXME: restart SUID process */
2763 memcpy (&plugin->mac_address, &hdr[1], sizeof (struct MacAddress));
2765 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2766 "Received WLAN_HELPER_CONTROL message with transport of address %s\n",
2767 wlan_plugin_address_to_string (cls, &plugin->mac_address,
2771 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2772 &plugin->mac_address,
2773 sizeof (struct MacAddress));
2777 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2778 "Func wlan_process_helper got unknown message with number %u, size %u\n",
2779 ntohs (hdr->type), ntohs (hdr->size));
2782 #if DEBUG_wlan_msg_dump
2783 hexdump (hdr, GNUNET_MIN (ntohs (hdr->size), 256));
2791 * We have been notified that wlan-helper has written something to stdout.
2792 * Handle the output, then reschedule this function to be called again once
2793 * more is available.
2795 * @param cls the plugin handle
2796 * @param tc the scheduling context
2799 wlan_plugin_helper_read (void *cls,
2800 const struct GNUNET_SCHEDULER_TaskContext *tc)
2802 struct Plugin *plugin = cls;
2804 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2806 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2809 char mybuf[WLAN_MTU + sizeof (struct GNUNET_MessageHeader)];
2813 GNUNET_DISK_file_read (plugin->server_stdout_handle, mybuf,
2818 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2820 ("Finished reading from wlan-helper stdout with code: %d\n"),
2825 GNUNET_SERVER_mst_receive (plugin->suid_tokenizer, NULL, mybuf, bytes,
2826 GNUNET_NO, GNUNET_NO);
2828 GNUNET_assert (plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2829 plugin->server_read_task =
2830 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2831 plugin->server_stdout_handle,
2832 &wlan_plugin_helper_read, plugin);
2836 * Start the gnunet-wlan-helper process.
2838 * @param plugin the transport plugin
2839 * @param testmode should we use the dummy driver for testing?
2840 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
2843 wlan_transport_start_wlan_helper (struct Plugin *plugin, int testmode)
2845 const char *filenamehw = "gnunet-transport-wlan-helper";
2846 const char *filenameloopback = "gnunet-transport-wlan-helper-dummy";
2848 plugin->server_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
2849 if (plugin->server_stdout == NULL)
2850 return GNUNET_SYSERR;
2852 plugin->server_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);
2853 if (plugin->server_stdin == NULL)
2854 return GNUNET_SYSERR;
2856 /* Start the server process */
2862 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2863 "Starting gnunet-wlan-helper process cmd: %s %s %i\n",
2864 filenamehw, plugin->interface, testmode);
2867 if (GNUNET_OS_check_helper_binary (filenamehw) == GNUNET_YES)
2869 plugin->server_proc =
2870 GNUNET_OS_start_process (plugin->server_stdin, plugin->server_stdout,
2871 filenamehw, filenamehw, plugin->interface,
2874 else if (GNUNET_OS_check_helper_binary (filenamehw) == GNUNET_NO)
2876 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
2877 "gnunet-transport-wlan-helper is not suid, please change it or look at the doku\n");
2882 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
2883 "gnunet-transport-wlan-helper not found, please look if it exists and is the $PATH variable!\n");
2888 else if (testmode == 1)
2892 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2893 "Starting gnunet-wlan-helper loopback 1 process cmd: %s %s %i\n",
2894 filenameloopback, plugin->interface, testmode);
2897 if (GNUNET_OS_check_helper_binary (filenameloopback) != GNUNET_SYSERR)
2899 plugin->server_proc =
2900 GNUNET_OS_start_process (plugin->server_stdin, plugin->server_stdout,
2901 filenameloopback, filenameloopback, "1",
2906 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
2907 "gnunet-transport-wlan-helper-dummy not found, please look if it exists and is the $PATH variable!\n");
2911 else if (testmode == 2)
2914 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2915 "Starting gnunet-wlan-helper loopback 2 process cmd: %s %s %i\n",
2916 filenameloopback, plugin->interface, testmode);
2918 if (GNUNET_OS_check_helper_binary (filenameloopback) != GNUNET_SYSERR)
2920 plugin->server_proc =
2921 GNUNET_OS_start_process (plugin->server_stdin, plugin->server_stdout,
2922 filenameloopback, filenameloopback, "2",
2927 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
2928 "gnunet-transport-wlan-helper-dummy not found, please look if it exists and is in the $PATH variable!\n");
2932 if (plugin->server_proc == NULL)
2935 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2936 "Failed to start gnunet-wlan-helper process\n");
2938 return GNUNET_SYSERR;
2941 /* Close the write end of the read pipe */
2942 GNUNET_DISK_pipe_close_end (plugin->server_stdout,
2943 GNUNET_DISK_PIPE_END_WRITE);
2945 /* Close the read end of the write pipe */
2946 GNUNET_DISK_pipe_close_end (plugin->server_stdin, GNUNET_DISK_PIPE_END_READ);
2948 plugin->server_stdout_handle =
2949 GNUNET_DISK_pipe_handle (plugin->server_stdout,
2950 GNUNET_DISK_PIPE_END_READ);
2951 plugin->server_stdin_handle =
2952 GNUNET_DISK_pipe_handle (plugin->server_stdin,
2953 GNUNET_DISK_PIPE_END_WRITE);
2955 GNUNET_assert (plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2958 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2959 "Adding server_read_task for the wlan-helper\n");
2962 plugin->server_read_task =
2963 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2964 plugin->server_stdout_handle,
2965 &wlan_plugin_helper_read, plugin);
2971 * Exit point from the plugin.
2972 * @param cls pointer to the api struct
2977 libgnunet_plugin_transport_wlan_done (void *cls)
2979 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2980 struct Plugin *plugin = api->cls;
2981 struct MacEndpoint *endpoint = plugin->mac_head;
2982 struct MacEndpoint *endpoint_next;
2985 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2986 "libgnunet_plugin_transport_wlan_done started\n");
2989 GNUNET_DISK_pipe_close (plugin->server_stdout);
2990 GNUNET_DISK_pipe_close (plugin->server_stdin);
2991 GNUNET_OS_process_kill (plugin->server_proc, 9);
2992 GNUNET_OS_process_close (plugin->server_proc);
2994 GNUNET_assert (cls != NULL);
2996 while (endpoint != NULL)
2998 endpoint_next = endpoint->next;
2999 free_macendpoint (plugin, endpoint);
3000 endpoint = endpoint_next;
3003 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
3005 GNUNET_SCHEDULER_cancel (plugin->server_write_delay_task);
3006 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
3008 if (plugin->server_write_task != GNUNET_SCHEDULER_NO_TASK)
3010 GNUNET_SCHEDULER_cancel (plugin->server_write_task);
3011 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
3013 if (plugin->server_read_task != GNUNET_SCHEDULER_NO_TASK)
3015 GNUNET_SCHEDULER_cancel (plugin->server_read_task);
3016 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
3019 if (plugin->suid_tokenizer != NULL)
3020 GNUNET_SERVER_mst_destroy (plugin->suid_tokenizer);
3022 if (plugin->data_tokenizer != NULL)
3023 GNUNET_SERVER_mst_destroy (plugin->data_tokenizer);
3025 GNUNET_free_non_null (plugin->interface);
3026 GNUNET_free (plugin);
3032 * Entry point for the plugin.
3034 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
3035 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
3038 libgnunet_plugin_transport_wlan_init (void *cls)
3040 //struct GNUNET_SERVICE_Context *service;
3041 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3042 struct GNUNET_TRANSPORT_PluginFunctions *api;
3043 struct Plugin *plugin;
3044 static unsigned long long testmode = 0;
3046 GNUNET_assert (cls != NULL);
3048 plugin = GNUNET_malloc (sizeof (struct Plugin));
3050 plugin->pendingsessions = 0;
3051 GNUNET_STATISTICS_set(plugin->env->stats, _("# wlan pending sessions"), plugin->pendingsessions, GNUNET_NO);
3052 plugin->mac_count = 0;
3053 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
3054 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
3055 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
3056 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
3057 GNUNET_BANDWIDTH_value_init (100 * 1024 *
3060 plugin->suid_tokenizer =
3061 GNUNET_SERVER_mst_create (&wlan_process_helper, plugin);
3063 plugin->data_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
3065 //plugin->sessions = GNUNET_malloc (sizeof (struct Sessionqueue));
3066 //plugin->pending_Sessions_head = GNUNET_malloc (sizeof (struct Sessionqueue));
3068 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3070 api->send = &wlan_plugin_send;
3071 api->disconnect = &wlan_plugin_disconnect;
3072 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
3073 api->check_address = &wlan_plugin_address_suggested;
3074 api->address_to_string = &wlan_plugin_address_to_string;
3078 if (GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "TESTMODE"))
3080 if (GNUNET_SYSERR ==
3081 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-wlan",
3082 "TESTMODE", &testmode))
3083 testmode = 0; //default value
3086 if (GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "INTERFACE"))
3088 if (GNUNET_CONFIGURATION_get_value_string
3089 (env->cfg, "transport-wlan", "INTERFACE",
3090 &(plugin->interface)) != GNUNET_YES)
3092 libgnunet_plugin_transport_wlan_done (api);
3098 wlan_transport_start_wlan_helper (plugin, testmode);
3099 set_next_beacon_time (plugin);
3100 set_next_send(plugin);
3102 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
3103 "wlan init finished\n");
3109 /* end of plugin_transport_wlan.c */