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
28 #include "gnunet_hello_lib.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_statistics_service.h"
32 #include "gnunet_transport_service.h"
33 #include "gnunet_transport_plugin.h"
34 #include "plugin_transport_wlan.h"
35 #include "gnunet_common.h"
36 #include "gnunet_crypto_lib.h"
37 #include "gnunet_fragmentation_lib.h"
38 //#include "wlan/ieee80211.h"
39 //#include <netinet/ip.h>
43 #define PROTOCOL_PREFIX "wlan"
45 #define PLUGIN_LOG_NAME "wlan-plugin"
53 * time out of a session
55 #define SESSION_TIMEOUT GNUNET_TIME_UNIT_MINUTES
58 * time out of a mac endpoint
60 #define MACENDPOINT_TIMEOUT GNUNET_TIME_UNIT_MINUTES
63 * scaling factor for hello beacon
65 #define HALLO_BEACON_SCALING_FACTOR 900
68 * max size of fragment queue
70 #define FRAGMENT_QUEUE_SIZE 10
72 * max messages in fragment queue per session/client
74 #define FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION 1
77 * max messages in fragment queue per MAC
79 #define FRAGMENT_QUEUE_MESSAGES_OUT_PER_MACENDPOINT 1
82 * max messages in in queue
84 #define MESSAGES_IN_QUEUE_SIZE 10
86 * max messages in in queue per session/client
88 #define MESSAGES_IN_DEFRAG_QUEUE_PER_MAC 1
94 #define DEBUG_wlan GNUNET_NO
95 #define DEBUG_wlan_retransmission GNUNET_NO
96 #define DEBUG_wlan_ip_udp_packets_on_air GNUNET_NO
99 #define IEEE80211_ADDR_LEN 6 /* size of 802.11 address */
101 #define IEEE80211_FC0_VERSION_MASK 0x03
102 #define IEEE80211_FC0_VERSION_SHIFT 0
103 #define IEEE80211_FC0_VERSION_0 0x00
104 #define IEEE80211_FC0_TYPE_MASK 0x0c
105 #define IEEE80211_FC0_TYPE_SHIFT 2
106 #define IEEE80211_FC0_TYPE_MGT 0x00
107 #define IEEE80211_FC0_TYPE_CTL 0x04
108 #define IEEE80211_FC0_TYPE_DATA 0x08
111 * Structure of an internet header, naked of options.
115 #if __BYTE_ORDER == __LITTLE_ENDIAN
116 unsigned int ip_hl:4; /* header length */
117 unsigned int ip_v:4; /* version */
119 #if __BYTE_ORDER == __BIG_ENDIAN
120 unsigned int ip_v:4; /* version */
121 unsigned int ip_hl:4; /* header length */
123 u_int8_t ip_tos; /* type of service */
124 u_short ip_len; /* total length */
125 u_short ip_id; /* identification */
126 u_short ip_off; /* fragment offset field */
127 #define IP_RF 0x8000 /* reserved fragment flag */
128 #define IP_DF 0x4000 /* dont fragment flag */
129 #define IP_MF 0x2000 /* more fragments flag */
130 #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
131 u_int8_t ip_ttl; /* time to live */
132 u_int8_t ip_p; /* protocol */
133 u_short ip_sum; /* checksum */
134 struct in_addr ip_src, ip_dst; /* source and dest address */
146 * generic definitions for IEEE 802.11 frames
148 struct ieee80211_frame
152 u_int8_t i_addr1[IEEE80211_ADDR_LEN];
153 u_int8_t i_addr2[IEEE80211_ADDR_LEN];
154 u_int8_t i_addr3[IEEE80211_ADDR_LEN];
156 #if DEBUG_wlan_ip_udp_packets_on_air
164 * Encapsulation of all of the state of the plugin.
171 struct GNUNET_TRANSPORT_PluginEnvironment *env;
174 * List of open connections. head
176 struct MacEndpoint *mac_head;
179 * List of open connections. tail
181 struct MacEndpoint *mac_tail;
184 * Number of connections
186 unsigned int mac_count;
189 * encapsulation of data from the local wlan helper program
191 struct GNUNET_SERVER_MessageStreamTokenizer *suid_tokenizer;
194 * encapsulation of packets received from the wlan helper
196 struct GNUNET_SERVER_MessageStreamTokenizer *data_tokenizer;
199 * stdout pipe handle for the gnunet-wlan-helper process
201 struct GNUNET_DISK_PipeHandle *server_stdout;
204 * stdout file handle for the gnunet-wlan-helper process
206 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
209 * stdin pipe handle for the gnunet-wlan-helper process
211 struct GNUNET_DISK_PipeHandle *server_stdin;
214 * stdin file handle for the gnunet-wlan-helper process
216 const struct GNUNET_DISK_FileHandle *server_stdin_handle;
219 * ID of the gnunet-wlan-server std read task
221 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
224 * ID of the gnunet-wlan-server std read task
226 GNUNET_SCHEDULER_TaskIdentifier server_write_task;
229 * ID of the delay task for writing
231 GNUNET_SCHEDULER_TaskIdentifier server_write_delay_task;
234 * The process id of the wlan process
236 struct GNUNET_OS_Process *server_proc;
239 * The interface of the wlan card given to us by the user.
244 * The mac_address of the wlan card given to us by the helper.
246 struct MacAddress mac_address;
249 * Sessions currently pending for transmission
252 struct Sessionqueue *pending_Sessions_head;
255 * Sessions currently pending for transmission
256 * to a peer (tail), if any.
258 struct Sessionqueue *pending_Sessions_tail;
261 * number of pending sessions
263 unsigned int pendingsessions;
266 * Messages in the sending queues
268 int pending_Fragment_Messages;
271 * messages ready for send, head
273 struct FragmentMessage_queue *sending_messages_head;
275 * messages ready for send, tail
277 struct FragmentMessage_queue *sending_messages_tail;
279 * time of the next "hello-beacon"
281 struct GNUNET_TIME_Absolute beacon_time;
284 * queue to send acks for received fragments (head)
286 struct AckSendQueue *ack_send_queue_head;
289 * queue to send acks for received fragments (tail)
291 struct AckSendQueue *ack_send_queue_tail;
294 * Tracker for bandwidth limit
296 struct GNUNET_BANDWIDTH_Tracker tracker;
300 * Struct to store data if file write did not accept the whole packet
304 struct Plugin *plugin;
306 struct GNUNET_MessageHeader *msgstart;
311 * Queue of sessions, for the general session queue and the pending session queue
316 struct Sessionqueue *next;
317 struct Sessionqueue *prev;
318 struct Session *content;
322 * Queue of fragmented messages, for the sending queue of the plugin
325 struct FragmentMessage_queue
327 struct FragmentMessage_queue *next;
328 struct FragmentMessage_queue *prev;
329 struct FragmentMessage *content;
333 * Queue for the fragments received
336 struct Receive_Fragment_Queue
338 struct Receive_Fragment_Queue *next;
339 struct Receive_Fragment_Queue *prev;
343 struct Radiotap_rx rxinfo;
347 struct MacEndpoint_id_fragment_triple
349 struct MacEndpoint *endpoint;
351 struct FragmentMessage *fm;
355 struct Plugin_Session_pair
357 struct Plugin *plugin;
358 struct Session *session;
362 * Information kept for each message that is yet to
365 struct PendingMessage
370 struct PendingMessage *next;
374 struct PendingMessage *prev;
377 * The pending message
379 struct WlanHeader *msg;
382 * Size of the message
387 * Continuation function to call once the message
388 * has been sent. Can be NULL if there is no
389 * continuation to call.
391 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
394 * Cls for transmit_cont
396 void *transmit_cont_cls;
399 * Timeout value for the pending message.
401 struct GNUNET_TIME_Absolute timeout;
406 * Queue for acks to send for fragments recived
412 * next ack in the ack send queue
414 struct AckSendQueue *next;
416 * previous ack in the ack send queue
418 struct AckSendQueue *prev;
420 * pointer to the session this ack belongs to
422 struct MacEndpoint *endpoint;
424 * ID of message, to distinguish between the messages, picked randomly.
431 struct GNUNET_MessageHeader *hdr;
433 * pointer to the ieee wlan header
435 struct ieee80211_frame *ieeewlanheader;
437 * pointer to the radiotap header
439 struct Radiotap_Send *radioHeader;
443 * Session infos gathered from a messages
449 * the session this message belongs to
451 struct Session *session;
455 struct MacAddress addr;
460 struct MacEndpoint *macendpoint;
464 * Session handle for connections.
472 struct SessionHeader header;
475 * Message currently pending for transmission
476 * to this peer, if any. head
478 struct PendingMessage *pending_message_head;
481 * Message currently pending for transmission
482 * to this peer, if any. tail
484 struct PendingMessage *pending_message_tail;
487 * To whom are we talking to (set to our identity
488 * if we are still waiting for the welcome message)
490 struct GNUNET_PeerIdentity target;
493 * Address of the other peer (either based on our 'connect'
494 * call or on our 'accept' call).
499 * Last activity on this connection. Used to select preferred
500 * connection and timeout
502 struct GNUNET_TIME_Absolute last_activity;
507 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
512 struct MacEndpoint *mac;
515 * count of messages in the fragment out queue for this session
518 int fragment_messages_out_count;
523 * Struct to represent one network card connection
528 * Pointer to the global plugin struct.
530 struct Plugin *plugin;
532 * Struct to hold the session reachable over this mac; head
534 struct Sessionqueue *sessions_head;
536 * Struct to hold the session reachable over this mac; tail
538 struct Sessionqueue *sessions_tail;
540 * Messages currently sending
543 struct FragmentMessage *sending_messages_head;
546 * Messages currently sending
547 * to a peer (tail), if any.
549 struct FragmentMessage *sending_messages_tail;
553 struct MacEndpoint *next;
557 struct MacEndpoint *prev;
562 struct MacAddress addr;
565 * Defrag context for this mac endpoint
567 struct GNUNET_DEFRAGMENT_Context *defrag;
570 * count of messages in the fragment out queue for this mac endpoint
573 int fragment_messages_out_count;
581 * Duplicates received
596 * Last activity on this endpoint. Used to select preferred
599 struct GNUNET_TIME_Absolute last_activity;
604 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
608 * Struct for Messages in the fragment queue
611 struct FragmentMessage
615 * Session this message belongs to
618 struct Session *session;
621 * This is a doubly-linked list.
623 struct FragmentMessage *next;
626 * This is a doubly-linked list.
628 struct FragmentMessage *prev;
631 * Fragmentation context
633 struct GNUNET_FRAGMENT_Context *fragcontext;
636 * Timeout value for the message.
638 struct GNUNET_TIME_Absolute timeout;
643 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
656 * pointer to the ieee wlan header
658 struct ieee80211_frame *ieeewlanheader;
660 * pointer to the radiotap header
662 struct Radiotap_Send *radioHeader;
665 static void do_transmit (void *cls,
666 const struct GNUNET_SCHEDULER_TaskContext *tc);
667 static void free_session (struct Plugin *plugin, struct Sessionqueue *queue,
668 int do_free_macendpoint);
669 static struct MacEndpoint *create_macendpoint (struct Plugin *plugin,
670 const struct MacAddress *addr);
673 * Generates a nice hexdump of a memory area.
675 * \param mem pointer to memory to dump
676 * \param length how many bytes to dump
679 hexdump (void *mem, unsigned length)
682 char *src = (char *) mem;
684 printf ("dumping %u bytes from %p\r\n"
685 " 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF\r\n",
690 for (i = 0; i < length; i += 16, src += 16)
694 t += sprintf (t, "%04x: ", i);
695 for (j = 0; j < 16; j++)
698 t += sprintf (t, "%02X", src[j] & 0xff);
700 t += sprintf (t, " ");
701 t += sprintf (t, j % 2 ? " " : "-");
704 t += sprintf (t, " ");
705 for (j = 0; j < 16; j++)
709 if (isprint ((unsigned char) src[j]))
710 t += sprintf (t, "%c", src[j]);
712 t += sprintf (t, ".");
716 t += sprintf (t, " ");
720 t += sprintf (t, "\r\n");
726 * Function to find a MacEndpoint with a specific mac addr
727 * @param plugin pointer to the plugin struct
728 * @param addr pointer to the mac address
729 * @param create_new GNUNET_YES if a new end point should be created
732 static struct MacEndpoint *
733 get_macendpoint (struct Plugin *plugin, const struct MacAddress *addr,
736 struct MacEndpoint *queue = plugin->mac_head;
738 while (queue != NULL)
740 GNUNET_assert (queue->sessions_head != NULL);
741 if (memcmp (addr, &queue->addr, sizeof (struct MacAddress)) == 0)
742 return queue; /* session found */
746 if (create_new == GNUNET_YES)
748 return create_macendpoint (plugin, addr);
758 * search for a session with the addr and peer id
760 * @param plugin pointer to the plugin struct
761 * @param addr pointer to the mac address of the peer
762 * @param peer pointer to the peerid
763 * @return returns the session
765 static struct Session *
766 search_session (struct Plugin *plugin, const struct MacEndpoint *endpoint,
767 const struct GNUNET_PeerIdentity *peer)
769 GNUNET_assert (endpoint != NULL);
770 struct Sessionqueue *queue = endpoint->sessions_head;
772 while (queue != NULL)
774 GNUNET_assert (queue->content != NULL);
776 (peer, &queue->content->target,
777 sizeof (struct GNUNET_PeerIdentity)) == 0)
778 return queue->content; /* session found */
785 * Function called for a quick conversion of the binary address to
786 * a numeric address. Note that the caller must not free the
787 * address and that the next call to this function is allowed
788 * to override the address again.
791 * @param addr binary address
792 * @param addrlen length of the address
793 * @return string representing the same address
796 wlan_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
799 const struct MacAddress *mac;
801 if (addrlen != sizeof (struct MacAddress))
807 GNUNET_snprintf (ret, sizeof (ret), "%s Mac-Address %X:%X:%X:%X:%X:%X",
808 PROTOCOL_PREFIX, mac->mac[0], mac->mac[1], mac->mac[2],
809 mac->mac[3], mac->mac[4], mac->mac[5]);
814 * Function for the scheduler if a session times out
815 * @param cls pointer to the Sessionqueue
816 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
819 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
821 struct Sessionqueue *queue = cls;
823 GNUNET_assert (queue != NULL);
824 queue->content->timeout_task = GNUNET_SCHEDULER_NO_TASK;
825 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
829 if (GNUNET_TIME_absolute_get_remaining
830 (GNUNET_TIME_absolute_add
831 (queue->content->last_activity, SESSION_TIMEOUT)).rel_value == 0)
833 free_session (queue->content->mac->plugin, queue, GNUNET_YES);
837 queue->content->timeout_task =
838 GNUNET_SCHEDULER_add_delayed (SESSION_TIMEOUT, &session_timeout, queue);
843 * create a new session
845 * @param plugin pointer to the plugin struct
846 * @param addr pointer to the mac endpoint of the peer
847 * @return returns the session
850 static struct Session *
851 create_session (struct Plugin *plugin, struct MacEndpoint *endpoint,
852 const struct GNUNET_PeerIdentity *peer)
854 GNUNET_assert (endpoint != NULL);
855 struct Sessionqueue *queue =
856 GNUNET_malloc (sizeof (struct Sessionqueue) + sizeof (struct Session));
858 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
859 endpoint->sessions_tail, queue);
861 queue->content = (struct Session *) &queue[1];
862 queue->content->mac = endpoint;
863 memcpy (&(queue->content->target), peer, sizeof (struct GNUNET_PeerIdentity));
864 queue->content->last_activity = GNUNET_TIME_absolute_get ();
865 queue->content->timeout_task =
866 GNUNET_SCHEDULER_add_delayed (SESSION_TIMEOUT, &session_timeout, queue);
869 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
870 "New session %p with endpoint %p: %s\n", queue->content,
871 endpoint, wlan_plugin_address_to_string (NULL,
876 return queue->content;
880 * Get session from address, create if no session exists
882 * @param plugin pointer to the plugin struct
883 * @param addr pointer to the mac address of the peer
884 * @param peer pointer to the peerid
885 * @return returns the session
887 static struct Session *
888 get_session (struct Plugin *plugin, const struct MacAddress *addr,
889 const struct GNUNET_PeerIdentity *peer)
891 struct MacEndpoint *mac;
893 mac = get_macendpoint (plugin, addr, GNUNET_YES);
894 struct Session *session = search_session (plugin, mac, peer);
898 return create_session (plugin, mac, peer);
902 * Queue the session to send data
903 * checks if there is a message pending
904 * checks if this session is not allready in the queue
905 * @param plugin pointer to the plugin
906 * @param session pointer to the session to add
909 queue_session (struct Plugin *plugin, struct Session *session)
911 struct Sessionqueue *queue = plugin->pending_Sessions_head;
913 if (session->pending_message_head != NULL)
915 while (queue != NULL)
917 // content is never NULL
918 GNUNET_assert (queue->content != NULL);
919 // is session already in queue?
920 if (session == queue->content)
928 // Session is not in the queue
930 queue = GNUNET_malloc (sizeof (struct Sessionqueue));
931 queue->content = session;
934 GNUNET_CONTAINER_DLL_insert_tail (plugin->pending_Sessions_head,
935 plugin->pending_Sessions_tail, queue);
936 plugin->pendingsessions++;
942 * Function to schedule the write task, executed after a delay
943 * @param cls pointer to the plugin struct
944 * @param tc GNUNET_SCHEDULER_TaskContext pointer
947 delay_fragment_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
949 struct Plugin *plugin = cls;
951 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
953 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
956 // GNUNET_TIME_UNIT_FOREVER_REL is needed to clean up old msg
957 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
959 plugin->server_write_task =
960 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
961 plugin->server_stdin_handle,
962 &do_transmit, plugin);
967 * Function to calculate the time of the next periodic "hello-beacon"
968 * @param plugin pointer to the plugin struct
972 set_next_beacon_time (struct Plugin *const plugin)
974 //under 10 known peers: once a second
975 if (plugin->mac_count < 10)
977 plugin->beacon_time =
978 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
979 GNUNET_TIME_relative_multiply
980 (GNUNET_TIME_UNIT_SECONDS,
981 HALLO_BEACON_SCALING_FACTOR));
983 //under 30 known peers: every 10 seconds
984 else if (plugin->mac_count < 30)
986 plugin->beacon_time =
987 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
988 GNUNET_TIME_relative_multiply
989 (GNUNET_TIME_UNIT_SECONDS,
990 10 * HALLO_BEACON_SCALING_FACTOR));
992 //over 30 known peers: once a minute
995 plugin->beacon_time =
996 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
997 GNUNET_TIME_relative_multiply
998 (GNUNET_TIME_UNIT_MINUTES,
999 HALLO_BEACON_SCALING_FACTOR));
1004 * Function to set the timer for the next timeout of the fragment queue
1005 * @param plugin the handle to the plugin struct
1009 set_next_send (struct Plugin *const plugin)
1011 struct GNUNET_TIME_Relative next_send;
1014 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
1016 GNUNET_SCHEDULER_cancel (plugin->server_write_delay_task);
1017 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
1020 //check if some acks are in the queue
1021 if (plugin->ack_send_queue_head != NULL)
1023 next_send = GNUNET_TIME_UNIT_ZERO;
1026 //check if there are some fragments in the queue
1027 else if (plugin->sending_messages_head != NULL)
1029 next_send = GNUNET_TIME_UNIT_ZERO;
1033 next_send = GNUNET_TIME_absolute_get_remaining (plugin->beacon_time);
1037 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1038 "Next packet is send in: %u\n", next_send.rel_value);
1041 if (next_send.rel_value == GNUNET_TIME_UNIT_ZERO.rel_value)
1043 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
1045 plugin->server_write_task =
1046 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1047 plugin->server_stdin_handle,
1048 &do_transmit, plugin);
1053 if (plugin->server_write_delay_task == GNUNET_SCHEDULER_NO_TASK)
1055 plugin->server_write_delay_task =
1056 GNUNET_SCHEDULER_add_delayed (next_send, &delay_fragment_task,
1063 * Function to get the next queued Session, removes the session from the queue
1064 * @param plugin pointer to the plugin struct
1065 * @return pointer to the session found, returns NULL if there is now session in the queue
1067 static struct Session *
1068 get_next_queue_session (struct Plugin *plugin)
1070 struct Session *session;
1071 struct Sessionqueue *sessionqueue;
1072 struct Sessionqueue *sessionqueue_alt;
1073 struct PendingMessage *pm;
1075 sessionqueue = plugin->pending_Sessions_head;
1077 while (sessionqueue != NULL)
1079 session = sessionqueue->content;
1081 GNUNET_assert (session != NULL);
1082 pm = session->pending_message_head;
1087 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1088 "pending message is empty, should not happen. session %p\n",
1093 GNUNET_assert (pm != NULL);
1095 //check for message timeout
1096 if (GNUNET_TIME_absolute_get_remaining (pm->timeout).rel_value > 0)
1098 //check if session has no message in the fragment queue
1099 if ((session->mac->fragment_messages_out_count <
1100 FRAGMENT_QUEUE_MESSAGES_OUT_PER_MACENDPOINT) &&
1101 (session->fragment_messages_out_count <
1102 FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION))
1104 plugin->pendingsessions--;
1105 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1106 plugin->pending_Sessions_tail,
1108 GNUNET_free (sessionqueue);
1114 sessionqueue = sessionqueue->next;
1119 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
1120 session->pending_message_tail, pm);
1122 //call the cont func that it did not work
1123 if (pm->transmit_cont != NULL)
1124 pm->transmit_cont (pm->transmit_cont_cls, &(session->target),
1126 GNUNET_free (pm->msg);
1129 if (session->pending_message_head == NULL)
1131 sessionqueue_alt = sessionqueue;
1132 sessionqueue = sessionqueue->next;
1133 plugin->pendingsessions--;
1134 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1135 plugin->pending_Sessions_tail,
1138 GNUNET_free (sessionqueue_alt);
1147 * frees the space of a message in the fragment queue (send queue)
1148 * @param plugin the plugin struct
1149 * @param fm message to free
1152 free_fragment_message (struct Plugin *plugin, struct FragmentMessage *fm)
1154 struct Session *session = fm->session;
1155 struct MacEndpoint *endpoint = session->mac;
1156 struct FragmentMessage_queue *fmq;
1157 struct FragmentMessage_queue *fmq_next;
1161 fmq = plugin->sending_messages_head;
1164 fmq_next = fmq->next;
1165 if (fmq->content == fm)
1167 GNUNET_CONTAINER_DLL_remove (plugin->sending_messages_head,
1168 plugin->sending_messages_tail, fmq);
1174 (session->mac->fragment_messages_out_count)--;
1175 session->fragment_messages_out_count--;
1176 plugin->pending_Fragment_Messages--;
1177 GNUNET_CONTAINER_DLL_remove (endpoint->sending_messages_head,
1178 endpoint->sending_messages_tail, fm);
1179 GNUNET_FRAGMENT_context_destroy (fm->fragcontext);
1180 if (fm->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1181 GNUNET_SCHEDULER_cancel (fm->timeout_task);
1184 queue_session (plugin, session);
1186 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1187 "Free pending fragment messages %p, session %p\n", fm,
1194 * function to fill the radiotap header
1195 * @param plugin pointer to the plugin struct
1196 * @param endpoint pointer to the endpoint
1197 * @param header pointer to the radiotap header
1198 * @return GNUNET_YES at success
1201 getRadiotapHeader (struct Plugin *plugin, struct MacEndpoint *endpoint,
1202 struct Radiotap_Send *header)
1205 if (endpoint != NULL)
1207 header->rate = endpoint->rate;
1208 header->tx_power = endpoint->tx_power;
1209 header->antenna = endpoint->antenna;
1214 header->tx_power = 0;
1215 header->antenna = 0;
1222 * function to generate the wlan hardware header for one packet
1223 * @param Header address to write the header to
1224 * @param to_mac_addr address of the recipient
1225 * @param plugin pointer to the plugin struct
1226 * @return GNUNET_YES if there was no error
1229 getWlanHeader (struct ieee80211_frame *Header,
1230 const struct MacAddress *to_mac_addr, struct Plugin *plugin,
1234 const int rate = 11000000;
1236 Header->i_fc[0] = IEEE80211_FC0_TYPE_DATA;
1237 Header->i_fc[1] = 0x00;
1238 memcpy (&Header->i_addr3, &mac_bssid, sizeof (mac_bssid));
1239 memcpy (&Header->i_addr2, plugin->mac_address.mac,
1240 sizeof (plugin->mac_address));
1241 memcpy (&Header->i_addr1, to_mac_addr, sizeof (struct MacAddress));
1243 tmp16 = (uint16_t *) Header->i_dur;
1244 *tmp16 = (uint16_t) htole16 ((size * 1000000) / rate + 290);
1246 #if DEBUG_wlan_ip_udp_packets_on_air
1251 Header->ip.ip_dst.s_addr = *((uint32_t *) & to_mac_addr->mac[2]);
1252 Header->ip.ip_src.s_addr = *((uint32_t *) & plugin->mac_address.mac[2]);
1253 Header->ip.ip_v = 4;
1254 Header->ip.ip_hl = 5;
1255 Header->ip.ip_p = 17;
1256 Header->ip.ip_ttl = 1;
1257 Header->ip.ip_len = htons (size + 8);
1258 Header->ip.ip_sum = 0;
1259 x = (uint16_t *) & Header->ip;
1260 count = sizeof (struct iph);
1263 /* This is the inner loop */
1264 crc += (unsigned short) *x++;
1267 /* Add left-over byte, if any */
1269 crc += *(unsigned char *) x;
1270 crc = (crc & 0xffff) + (crc >> 16);
1271 Header->ip.ip_sum = htons (~(unsigned short) crc);
1274 Header->udp.len = htons (size - sizeof (struct ieee80211_frame));
1284 * @param msgbuf pointer tor the data
1285 * @param msgbuf_size size of the data
1287 * @return 32bit crc value
1291 getcrc32 (const char *msgbuf, size_t msgbuf_size)
1294 return GNUNET_CRYPTO_crc32_n (msgbuf, msgbuf_size);;
1300 * @param msgbuf pointer tor the data
1301 * @param msgbuf_size size of the data
1303 * @return 16bit crc value
1307 getcrc16 (const char *msgbuf, size_t msgbuf_size)
1309 //TODO calc some crc
1314 * function to add a fragment of a message to send
1315 * @param cls FragmentMessage this message belongs to
1316 * @param hdr pointer to the start of the message
1320 add_message_for_send (void *cls, const struct GNUNET_MessageHeader *hdr)
1323 struct FragmentMessage *fm = cls;
1324 struct FragmentMessage_queue *fmqueue;
1326 GNUNET_assert (cls != NULL);
1327 GNUNET_assert (fm->frag == NULL);
1328 struct MacEndpoint *endpoint = fm->session->mac;
1329 struct Plugin *plugin = endpoint->plugin;
1330 struct GNUNET_MessageHeader *msgheader;
1331 struct GNUNET_MessageHeader *msgheader2;
1334 #if DEBUG_wlan_retransmission
1335 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1336 "Adding fragment of message %p to send, session %p, endpoint %p\n",
1337 fm, fm->session, endpoint);
1341 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1342 sizeof (struct ieee80211_frame) + ntohs (hdr->size);
1343 fm->frag = GNUNET_malloc (size);
1346 msgheader = (struct GNUNET_MessageHeader *) fm->frag;
1347 msgheader->size = htons (size);
1348 msgheader->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1350 fm->radioHeader = (struct Radiotap_Send *) &msgheader[1];
1351 fm->ieeewlanheader = (struct ieee80211_frame *) &fm->radioHeader[1];
1352 msgheader2 = (struct GNUNET_MessageHeader *) &fm->ieeewlanheader[1];
1353 memcpy (msgheader2, hdr, ntohs (hdr->size));
1355 fmqueue = GNUNET_malloc (sizeof (struct FragmentMessage_queue));
1356 fmqueue->content = fm;
1358 GNUNET_CONTAINER_DLL_insert_tail (plugin->sending_messages_head,
1359 plugin->sending_messages_tail, fmqueue);
1360 set_next_send (plugin);
1364 * function to send a hallo beacon
1365 * @param plugin pointer to the plugin struct
1368 send_hello_beacon (struct Plugin *plugin)
1372 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1373 "Sending hello beacon\n");
1378 uint16_t hallo_size;
1379 struct GNUNET_MessageHeader *msgheader;
1380 struct ieee80211_frame *ieeewlanheader;
1381 struct Radiotap_Send *radioHeader;
1382 struct GNUNET_MessageHeader *msgheader2;
1383 const struct GNUNET_MessageHeader *hello;
1385 hello = plugin->env->get_our_hello ();
1386 hallo_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1387 GNUNET_assert (sizeof (struct WlanHeader) + hallo_size <= WLAN_MTU);
1389 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1390 sizeof (struct ieee80211_frame) + sizeof (struct GNUNET_MessageHeader) +
1393 msgheader = GNUNET_malloc (size);
1394 msgheader->size = htons (size);
1395 msgheader->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1397 radioHeader = (struct Radiotap_Send *) &msgheader[1];
1398 getRadiotapHeader (plugin, NULL, radioHeader);
1399 ieeewlanheader = (struct ieee80211_frame *) &radioHeader[1];
1400 getWlanHeader (ieeewlanheader, &bc_all_mac, plugin, size);
1402 msgheader2 = (struct GNUNET_MessageHeader *) &ieeewlanheader[1];
1404 htons (GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello) +
1405 sizeof (struct GNUNET_MessageHeader));
1407 msgheader2->type = htons (GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT);
1408 memcpy (&msgheader2[1], hello, hallo_size);
1410 bytes = GNUNET_DISK_file_write (plugin->server_stdin_handle, msgheader, size);
1412 if (bytes == GNUNET_SYSERR)
1414 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1416 ("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1417 errno, strerror (errno));
1420 GNUNET_assert (bytes != GNUNET_SYSERR);
1421 GNUNET_assert (bytes == size);
1422 GNUNET_free (msgheader);
1424 set_next_beacon_time (plugin);
1425 set_next_send (plugin);
1429 * function to add an ack to send it for a received fragment
1430 * @param cls MacEndpoint this ack belongs to
1431 * @param msg_id id of the message
1432 * @param hdr pointer to the hdr where the ack is stored
1437 add_ack_for_send (void *cls, uint32_t msg_id,
1438 const struct GNUNET_MessageHeader *hdr)
1441 struct AckSendQueue *ack;
1443 GNUNET_assert (cls != NULL);
1444 struct MacEndpoint *endpoint = cls;
1445 struct Plugin *plugin = endpoint->plugin;
1446 struct GNUNET_MessageHeader *msgheader;
1447 struct GNUNET_MessageHeader *msgheader2;
1451 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1452 sizeof (struct ieee80211_frame) + ntohs (hdr->size) +
1453 sizeof (struct AckSendQueue);
1455 ack = GNUNET_malloc (size);
1456 ack->message_id = msg_id;
1457 ack->endpoint = endpoint;
1460 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1461 sizeof (struct ieee80211_frame) + ntohs (hdr->size);
1463 msgheader = (struct GNUNET_MessageHeader *) &ack[1];
1464 ack->hdr = (struct GNUNET_MessageHeader *) &ack[1];
1465 msgheader->size = htons (size);
1466 msgheader->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1468 ack->radioHeader = (struct Radiotap_Send *) &msgheader[1];
1469 ack->ieeewlanheader = (struct ieee80211_frame *) &(ack->radioHeader)[1];
1470 msgheader2 = (struct GNUNET_MessageHeader *) &(ack->ieeewlanheader)[1];
1471 memcpy (msgheader2, hdr, ntohs (hdr->size));
1473 GNUNET_CONTAINER_DLL_insert_tail (plugin->ack_send_queue_head,
1474 plugin->ack_send_queue_tail, ack);
1476 #if DEBUG_wlan_retransmission
1477 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1478 "Adding ack with message id %u to send, AckSendQueue %p, endpoint %p\n",
1479 msg_id, ack, endpoint);
1482 set_next_send (plugin);
1486 * Function for the scheduler if a FragmentMessage times out
1487 * @param cls pointer to the FragmentMessage
1488 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
1491 fragmentmessage_timeout (void *cls,
1492 const struct GNUNET_SCHEDULER_TaskContext *tc)
1494 struct FragmentMessage *fm = cls;
1496 GNUNET_assert (fm != NULL);
1497 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1498 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1502 free_fragment_message (fm->session->mac->plugin, fm);
1506 * Function to check if there is some space in the fragment queue
1507 * inserts a message if space is available
1508 * @param plugin the plugin struct
1512 check_fragment_queue (struct Plugin *plugin)
1514 struct Session *session;
1515 struct FragmentMessage *fm;
1516 struct GNUNET_PeerIdentity pid;
1518 struct PendingMessage *pm;
1520 if (plugin->pending_Fragment_Messages < FRAGMENT_QUEUE_SIZE)
1522 session = get_next_queue_session (plugin);
1523 if (session != NULL)
1525 pm = session->pending_message_head;
1526 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
1527 session->pending_message_tail, pm);
1528 session->mac->fragment_messages_out_count++;
1529 session->fragment_messages_out_count++;
1530 plugin->pending_Fragment_Messages++;
1531 GNUNET_assert (pm != NULL);
1533 fm = GNUNET_malloc (sizeof (struct FragmentMessage));
1534 fm->session = session;
1535 fm->timeout.abs_value = pm->timeout.abs_value;
1538 GNUNET_FRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
1540 GNUNET_TIME_UNIT_SECONDS,
1542 &add_message_for_send, fm);
1544 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
1545 (fm->timeout), fragmentmessage_timeout,
1547 GNUNET_CONTAINER_DLL_insert_tail (session->mac->sending_messages_head,
1548 session->mac->sending_messages_tail,
1551 if (pm->transmit_cont != NULL)
1553 pid = session->target;
1554 pm->transmit_cont (pm->transmit_cont_cls, &pid, GNUNET_OK);
1556 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1557 "called pm->transmit_cont for %p\n", session);
1563 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1564 "no pm->transmit_cont for %p\n", session);
1569 if (session->pending_message_head != NULL)
1572 queue_session (plugin, session);
1578 //check if timeout changed
1579 set_next_send (plugin);
1583 * Function to send an ack, does not free the ack
1584 * @param plugin pointer to the plugin
1585 * @param ack pointer to the ack to send
1588 send_ack (struct Plugin *plugin, struct AckSendQueue *ack)
1594 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1595 "Sending ack for message_id %u for mac endpoint %p, size %u\n",
1596 ack->message_id, ack->endpoint,
1597 ntohs (ack->hdr->size) - sizeof (struct Radiotap_Send));
1600 getRadiotapHeader (plugin, ack->endpoint, ack->radioHeader);
1601 getWlanHeader (ack->ieeewlanheader, &ack->endpoint->addr, plugin,
1602 ntohs (ack->hdr->size));
1605 GNUNET_DISK_file_write (plugin->server_stdin_handle, ack->hdr,
1606 ntohs (ack->hdr->size));
1607 if (bytes == GNUNET_SYSERR)
1609 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1611 ("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1612 errno, strerror (errno));
1615 GNUNET_assert (bytes != GNUNET_SYSERR);
1616 GNUNET_assert (bytes == ntohs (ack->hdr->size));
1617 set_next_send (plugin);
1621 * function to finish a sending if not all could have been writen befor
1622 * @param cls pointer to the Finish_send struct
1623 * @param tc TaskContext
1626 finish_sending (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1628 struct Finish_send *finish = cls;
1629 struct Plugin *plugin;
1632 plugin = finish->plugin;
1633 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1635 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1637 GNUNET_free (finish->msgstart);
1638 GNUNET_free (finish);
1642 GNUNET_DISK_file_write (plugin->server_stdin_handle, finish->msgheader,
1644 GNUNET_assert (bytes != GNUNET_SYSERR);
1646 if (bytes != finish->size)
1648 finish->msgheader = finish->msgheader + bytes;
1649 finish->size = finish->size - bytes;
1650 plugin->server_write_task =
1651 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1652 plugin->server_stdin_handle,
1653 &finish_sending, finish);
1657 GNUNET_free (finish->msgstart);
1658 GNUNET_free (finish);
1659 set_next_send (plugin);
1664 * Function called when wlan helper is ready to get some data
1666 * @param cls closure
1667 * @param tc GNUNET_SCHEDULER_TaskContext
1670 do_transmit (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1672 struct Plugin *plugin = cls;
1674 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1675 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1678 struct Session *session;
1679 struct FragmentMessage *fm;
1680 struct Finish_send *finish;
1681 struct FragmentMessage_queue *fmq;
1682 struct AckSendQueue *ack;
1685 if (plugin->ack_send_queue_head != NULL)
1687 ack = plugin->ack_send_queue_head;
1688 GNUNET_CONTAINER_DLL_remove (plugin->ack_send_queue_head,
1689 plugin->ack_send_queue_tail, ack);
1690 send_ack (plugin, ack);
1695 //test if a "hello-beacon" has to be send
1696 if (GNUNET_TIME_absolute_get_remaining (plugin->beacon_time).rel_value == 0)
1698 send_hello_beacon (plugin);
1702 if (plugin->sending_messages_head != NULL)
1704 fmq = plugin->sending_messages_head;
1706 GNUNET_CONTAINER_DLL_remove (plugin->sending_messages_head,
1707 plugin->sending_messages_tail, fmq);
1710 session = fm->session;
1711 GNUNET_assert (session != NULL);
1714 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1715 "Sending GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT for fragment message %p, size: %u\n",
1719 getRadiotapHeader (plugin, session->mac, fm->radioHeader);
1720 getWlanHeader (fm->ieeewlanheader, &(fm->session->mac->addr), plugin,
1724 GNUNET_DISK_file_write (plugin->server_stdin_handle, fm->frag,
1726 if (bytes == GNUNET_SYSERR)
1728 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1730 ("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1731 errno, strerror (errno));
1734 GNUNET_assert (bytes != GNUNET_SYSERR);
1736 if (bytes != fm->size)
1738 finish = GNUNET_malloc (sizeof (struct Finish_send));
1739 finish->plugin = plugin;
1740 finish->msgheader = fm->frag + bytes;
1741 finish->size = fm->size - bytes;
1742 finish->msgstart = (struct GNUNET_MessageHeader *) fm->frag;
1744 GNUNET_assert (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK);
1746 plugin->server_write_task =
1747 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1748 plugin->server_stdin_handle,
1749 &finish_sending, finish);
1754 GNUNET_free (fm->frag);
1756 set_next_send (plugin);
1758 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
1762 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
1763 "do_transmit did nothing, should not happen!\n");
1767 * Another peer has suggested an address for this
1768 * peer and transport plugin. Check that this could be a valid
1769 * address. If so, consider adding it to the list
1772 * @param cls closure
1773 * @param addr pointer to the address
1774 * @param addrlen length of addr
1775 * @return GNUNET_OK if this is a plausible address for this peer
1779 wlan_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1781 //struct Plugin *plugin = cls;
1783 /* check if the address is plausible; if so,
1784 * add it to our list! */
1786 GNUNET_assert (cls != NULL);
1787 //FIXME mitm is not checked
1788 //Mac Address has 6 bytes
1791 /* TODO check for bad addresses like multicast, broadcast, etc */
1795 return GNUNET_SYSERR;
1799 * Function that can be used by the transport service to transmit
1800 * a message using the plugin.
1802 * @param cls closure
1803 * @param target who should receive this message
1804 * @param priority how important is the message
1805 * @param msgbuf the message to transmit
1806 * @param msgbuf_size number of bytes in 'msgbuf'
1807 * @param timeout when should we time out
1808 * @param session which session must be used (or NULL for "any")
1809 * @param addr the address to use (can be NULL if the plugin
1810 * is "on its own" (i.e. re-use existing TCP connection))
1811 * @param addrlen length of the address in bytes
1812 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1813 * otherwise the plugin may use other addresses or
1814 * existing connections (if available)
1815 * @param cont continuation to call once the message has
1816 * been transmitted (or if the transport is ready
1817 * for the next transmission call; or if the
1818 * peer disconnected...)
1819 * @param cont_cls closure for cont
1820 * @return number of bytes used (on the physical network, with overheads);
1821 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1822 * and does NOT mean that the message was not transmitted (DV)
1825 wlan_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
1826 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
1827 struct GNUNET_TIME_Relative timeout, struct Session *session,
1828 const void *addr, size_t addrlen, int force_address,
1829 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1831 struct Plugin *plugin = cls;
1832 struct PendingMessage *newmsg;
1833 struct WlanHeader *wlanheader;
1835 //check if msglen > 0
1836 GNUNET_assert (msgbuf_size > 0);
1838 //get session if needed
1839 if (session == NULL)
1841 if (wlan_plugin_address_suggested (plugin, addr, addrlen) == GNUNET_OK)
1843 session = get_session (plugin, addr, target);
1847 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1848 _("Wlan Address len %d is wrong\n"), addrlen);
1855 //queue message in session
1856 //test if there is no other message in the "queue"
1857 //FIXME: to many send requests
1858 if (session->pending_message_head != NULL)
1860 newmsg = session->pending_message_head;
1861 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1862 "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",
1863 GNUNET_TIME_absolute_get_remaining (newmsg->timeout).
1864 rel_value, session->mac->fragment_messages_out_count);
1867 newmsg = GNUNET_malloc (sizeof (struct PendingMessage));
1868 newmsg->msg = GNUNET_malloc (msgbuf_size + sizeof (struct WlanHeader));
1869 wlanheader = newmsg->msg;
1870 //copy msg to buffer, not fragmented / segmented yet, but with message header
1871 wlanheader->header.size = htons (msgbuf_size + sizeof (struct WlanHeader));
1872 wlanheader->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA);
1873 memcpy (&(wlanheader->target), target, sizeof (struct GNUNET_PeerIdentity));
1874 memcpy (&(wlanheader->source), plugin->env->my_identity,
1875 sizeof (struct GNUNET_PeerIdentity));
1876 wlanheader->crc = 0;
1877 memcpy (&wlanheader[1], msgbuf, msgbuf_size);
1880 ((char *) wlanheader, msgbuf_size + sizeof (struct WlanHeader)));
1881 //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);
1882 //hexdump(newmsg->msg, msgbuf_size + sizeof(struct WlanHeader));
1884 newmsg->transmit_cont = cont;
1885 newmsg->transmit_cont_cls = cont_cls;
1886 newmsg->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1888 newmsg->timeout.abs_value = newmsg->timeout.abs_value - 500;
1890 newmsg->message_size = msgbuf_size + sizeof (struct WlanHeader);
1892 GNUNET_CONTAINER_DLL_insert_tail (session->pending_message_head,
1893 session->pending_message_tail, newmsg);
1896 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1897 "New message for %p with size (incl wlan header) %u added\n",
1898 session, newmsg->message_size);
1902 queue_session (plugin, session);
1904 check_fragment_queue (plugin);
1905 //FIXME not the correct size
1911 * function to free a mac endpoint
1912 * @param plugin pointer to the plugin struct
1913 * @param endpoin pointer to the MacEndpoint to free
1916 free_macendpoint (struct Plugin *plugin, struct MacEndpoint *endpoint)
1918 struct Sessionqueue *sessions;
1919 struct Sessionqueue *sessions_next;
1921 GNUNET_assert (endpoint != NULL);
1923 sessions = endpoint->sessions_head;
1924 while (sessions != NULL)
1926 sessions_next = sessions->next;
1927 free_session (plugin, sessions, GNUNET_NO);
1928 sessions = sessions_next;
1931 GNUNET_CONTAINER_DLL_remove (plugin->mac_head, plugin->mac_tail, endpoint);
1932 if (endpoint->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1933 GNUNET_SCHEDULER_cancel (endpoint->timeout_task);
1934 GNUNET_free (endpoint);
1939 * function to free a session
1940 * @param plugin pointer to the plugin
1941 * @param queue pointer to the sessionqueue element to free
1942 * @param free_macendpoint if GNUNET_YES and mac endpoint would be empty, free mac endpoint
1945 free_session (struct Plugin *plugin, struct Sessionqueue *queue,
1946 int do_free_macendpoint)
1948 struct Sessionqueue *pendingsession;
1949 struct Sessionqueue *pendingsession_tmp;
1950 struct PendingMessage *pm;
1951 struct MacEndpoint *endpoint;
1952 struct FragmentMessage *fm;
1953 struct FragmentMessage *fmnext;
1956 GNUNET_assert (queue != NULL);
1957 GNUNET_assert (queue->content != NULL);
1960 //is this session pending for send
1961 pendingsession = plugin->pending_Sessions_head;
1962 while (pendingsession != NULL)
1964 pendingsession_tmp = pendingsession->next;
1965 if (pendingsession->content == queue->content)
1967 plugin->pendingsessions--;
1968 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1969 plugin->pending_Sessions_tail,
1971 GNUNET_free (pendingsession);
1973 GNUNET_assert (check == 0);
1976 pendingsession = pendingsession_tmp;
1979 endpoint = queue->content->mac;
1980 fm = endpoint->sending_messages_head;
1984 if (fm->session == queue->content)
1986 free_fragment_message (plugin, fm);
1991 // remove PendingMessage
1992 pm = queue->content->pending_message_head;
1995 GNUNET_CONTAINER_DLL_remove (queue->content->pending_message_head,
1996 queue->content->pending_message_tail, pm);
1997 GNUNET_free (pm->msg);
1999 pm = queue->content->pending_message_head;
2002 GNUNET_CONTAINER_DLL_remove (endpoint->sessions_head, endpoint->sessions_tail,
2005 if (endpoint->sessions_head == NULL && do_free_macendpoint == GNUNET_YES)
2007 free_macendpoint (plugin, endpoint);
2010 if (queue->content->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2011 GNUNET_SCHEDULER_cancel (queue->content->timeout_task);
2012 GNUNET_free (queue);
2013 check_fragment_queue (plugin);
2017 * Function that can be used to force the plugin to disconnect
2018 * from the given peer and cancel all previous transmissions
2019 * (and their continuation).
2021 * @param cls closure
2022 * @param target peer from which to disconnect
2025 wlan_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
2027 struct Plugin *plugin = cls;
2028 struct Sessionqueue *queue;
2029 struct Sessionqueue *queue_next;
2030 struct MacEndpoint *endpoint = plugin->mac_head;
2031 struct MacEndpoint *endpoint_next;
2033 // just look at all the session for the needed one
2034 while (endpoint != NULL)
2036 queue = endpoint->sessions_head;
2037 endpoint_next = endpoint->next;
2038 while (queue != NULL)
2040 // content is never NULL
2041 GNUNET_assert (queue->content != NULL);
2042 queue_next = queue->next;
2044 (target, &(queue->content->target),
2045 sizeof (struct GNUNET_PeerIdentity)) == 0)
2047 free_session (plugin, queue, GNUNET_YES);
2052 endpoint = endpoint_next;
2057 * Convert the transports address to a nice, human-readable
2060 * @param cls closure
2061 * @param type name of the transport that generated the address
2062 * @param addr one of the addresses of the host, NULL for the last address
2063 * the specific address format depends on the transport
2064 * @param addrlen length of the address
2065 * @param numeric should (IP) addresses be displayed in numeric form?
2066 * @param timeout after how long should we give up?
2067 * @param asc function to call on each string
2068 * @param asc_cls closure for asc
2071 wlan_plugin_address_pretty_printer (void *cls, const char *type,
2072 const void *addr, size_t addrlen,
2074 struct GNUNET_TIME_Relative timeout,
2075 GNUNET_TRANSPORT_AddressStringCallback asc,
2079 const unsigned char *input;
2081 //GNUNET_assert(cls !=NULL);
2084 /* invalid address (MAC addresses have 6 bytes) */ GNUNET_break (0);
2085 asc (asc_cls, NULL);
2088 input = (const unsigned char *) addr;
2089 GNUNET_snprintf (ret, sizeof (ret),
2090 "%s Mac-Address %.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
2091 PROTOCOL_PREFIX, input[0], input[1], input[2], input[3],
2092 input[4], input[5]);
2097 * handels the data after all fragments are put together
2099 * @param session_light
2100 * @param hdr pointer to the data
2103 wlan_data_message_handler (void *cls, const struct GNUNET_MessageHeader *hdr)
2105 struct MacEndpoint *endpoint = (struct MacEndpoint *) cls;
2106 struct Plugin *plugin = endpoint->plugin;
2107 struct WlanHeader *wlanheader;
2108 struct Session *session;
2110 //const char * tempmsg;
2111 const struct GNUNET_MessageHeader *temp_hdr;
2112 struct GNUNET_PeerIdentity tmpsource;
2115 if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_DATA)
2119 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2120 "Func wlan_data_message_handler got GNUNET_MESSAGE_TYPE_WLAN_DATA size: %u\n",
2124 if (ntohs (hdr->size) <
2125 sizeof (struct WlanHeader) + sizeof (struct GNUNET_MessageHeader))
2127 //packet not big enought
2131 wlanheader = (struct WlanHeader *) hdr;
2133 session = search_session (plugin, endpoint, &wlanheader->source);
2135 //tempmsg = (char*) &wlanheader[1];
2136 temp_hdr = (const struct GNUNET_MessageHeader *) &wlanheader[1];
2137 crc = ntohl (wlanheader->crc);
2138 wlanheader->crc = 0;
2139 if (getcrc32 ((char *) wlanheader, ntohs (wlanheader->header.size)) != crc)
2141 //wrong crc, dispose message
2142 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2143 "Wlan message header crc was wrong: %u != %u\n",
2144 getcrc32 ((char *) wlanheader,
2145 ntohs (wlanheader->header.size)), crc);
2146 hexdump ((void *) hdr, ntohs (hdr->size));
2150 //if not in session list
2151 if (session == NULL)
2154 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2155 "WLAN client not in session list: packet size = %u, inner size = %u, header size = %u\n",
2156 ntohs (wlanheader->header.size), ntohs (temp_hdr->size),
2157 sizeof (struct WlanHeader));
2159 //try if it is a hello message
2160 if (ntohs (wlanheader->header.size) >=
2161 ntohs (temp_hdr->size) + sizeof (struct WlanHeader))
2163 if (ntohs (temp_hdr->type) == GNUNET_MESSAGE_TYPE_HELLO)
2165 if (GNUNET_HELLO_get_id
2166 ((const struct GNUNET_HELLO_Message *) temp_hdr,
2167 &tmpsource) == GNUNET_OK)
2169 session = create_session (plugin, endpoint, &tmpsource);
2173 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2174 "WLAN client not in session list and hello message is not okay\n");
2181 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2182 "WLAN client not in session list and not a hello message\n");
2188 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2189 "WLAN client not in session list and message size in does not fit\npacket size = %u, inner size = %u, header size = %u\n",
2190 ntohs (wlanheader->header.size),
2191 ntohs (temp_hdr->size), sizeof (struct WlanHeader));
2196 //"receive" the message
2199 (&wlanheader->source, &session->target,
2200 sizeof (struct GNUNET_PeerIdentity)) != 0)
2204 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2205 "WLAN peer source id doesn't match packet peer source id: session %p\n",
2212 (&wlanheader->target, plugin->env->my_identity,
2213 sizeof (struct GNUNET_PeerIdentity)) != 0)
2217 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2218 "WLAN peer target id doesn't match our peer id: session %p\n",
2224 GNUNET_SERVER_mst_receive (plugin->data_tokenizer, session,
2225 (const char *) temp_hdr,
2226 ntohs (hdr->size) - sizeof (struct WlanHeader),
2227 GNUNET_YES, GNUNET_NO);
2233 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2234 "wlan_data_message_handler got wrong message type\n");
2240 * function to process the a message, give it to the higher layer
2241 * @param cls pointer to the plugin
2242 * @param client pointer to the session this message belongs to
2243 * @param hdr start of the message
2245 //TODO ATS informations
2247 process_data (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
2250 GNUNET_assert (client != NULL);
2251 GNUNET_assert (cls != NULL);
2252 struct Session *session = (struct Session *) client;
2253 struct Plugin *plugin = (struct Plugin *) cls;
2255 struct GNUNET_TRANSPORT_ATS_Information distance[2];
2257 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2258 distance[0].value = htonl (1);
2259 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2260 distance[1].value = htonl (0);
2263 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2264 "Calling plugin->env->receive for session %p; %s; size: %u\n",
2265 session, wlan_plugin_address_to_string (NULL,
2271 plugin->env->receive (plugin->env->cls, &(session->target), hdr,
2272 (const struct GNUNET_TRANSPORT_ATS_Information *)
2273 &distance, 2, session,
2274 (const char *) &session->mac->addr,
2275 sizeof (session->mac->addr));
2279 * Function used for to process the data received from the wlan interface
2281 * @param cls the plugin handle
2282 * @param session_light FIXME: document
2283 * @param hdr hdr of the GNUNET_MessageHeader
2286 wlan_data_helper (void *cls, struct Session_light *session_light,
2287 const struct GNUNET_MessageHeader *hdr,
2288 const struct Radiotap_rx *rxinfo)
2290 struct Plugin *plugin = cls;
2291 struct FragmentMessage *fm;
2292 struct FragmentMessage *fm2;
2295 if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT)
2298 //TODO better DOS protection, error handling
2299 //TODO test first than create session
2300 GNUNET_assert (session_light != NULL);
2303 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2304 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT size: %u; %s\n",
2305 ntohs (hdr->size), wlan_plugin_address_to_string (NULL,
2311 if (session_light->macendpoint == NULL)
2313 session_light->macendpoint =
2314 get_macendpoint (plugin, &session_light->addr, GNUNET_NO);
2316 GNUNET_assert (GNUNET_HELLO_get_id
2317 ((const struct GNUNET_HELLO_Message *) &hdr[1],
2318 &(session_light->session->target)) != GNUNET_SYSERR);
2324 else if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_FRAGMENT)
2327 GNUNET_assert (session_light != NULL);
2328 if (session_light->macendpoint == NULL)
2330 session_light->macendpoint =
2331 get_macendpoint (plugin, &session_light->addr, GNUNET_YES);
2335 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2336 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_FRAGMENT with size: %u; mac endpoint %p: %s\n",
2337 ntohs (hdr->size), session_light->macendpoint,
2338 wlan_plugin_address_to_string (NULL,
2339 session_light->addr.mac,
2344 GNUNET_DEFRAGMENT_process_fragment (session_light->macendpoint->defrag,
2347 if (ret == GNUNET_NO)
2349 session_light->macendpoint->dups++;
2351 else if (ret == GNUNET_OK)
2353 session_light->macendpoint->fragc++;
2355 set_next_send (plugin);
2361 else if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_FRAGMENT_ACK)
2363 GNUNET_assert (session_light != NULL);
2364 if (session_light->macendpoint == NULL)
2366 session_light->macendpoint =
2367 get_macendpoint (plugin, &session_light->addr, GNUNET_NO);
2370 if (session_light->macendpoint == NULL)
2373 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2374 "Macendpoint does not exist for this GNUNET_MESSAGE_TYPE_FRAGMENT_ACK size: %u; %s\n",
2375 ntohs (hdr->size), wlan_plugin_address_to_string (NULL,
2385 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2386 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_FRAGMENT_ACK size: %u; mac endpoint: %p; %s\n",
2387 ntohs (hdr->size), session_light->macendpoint,
2388 wlan_plugin_address_to_string (NULL,
2389 session_light->addr.mac,
2392 fm = session_light->macendpoint->sending_messages_head;
2396 int ret = GNUNET_FRAGMENT_process_ack (fm->fragcontext, hdr);
2398 if (ret == GNUNET_OK)
2400 #if DEBUG_wlan_retransmission
2401 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2402 "Got last ack, finished fragment message %p\n", fm);
2404 session_light->macendpoint->acks++;
2405 fm->session->last_activity = GNUNET_TIME_absolute_get ();
2406 session_light->macendpoint->last_activity = fm->session->last_activity;
2407 free_fragment_message (plugin, fm);
2408 check_fragment_queue (plugin);
2411 if (ret == GNUNET_NO)
2413 #if DEBUG_wlan_retransmission
2414 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2415 "Got ack for: %p\n", fm);
2417 session_light->macendpoint->acks++;
2420 if (ret == GNUNET_SYSERR)
2428 #if DEBUG_wlan_retransmission
2429 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2430 "WLAN fragment not in fragment list\n");
2438 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2439 "WLAN packet inside the WLAN helper packet has not the right type: %u size: %u\n",
2440 ntohs (hdr->type), ntohs (hdr->size));
2446 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2447 "Helper finished\n");
2454 macprinter (const u_int8_t * mac)
2456 static char macstr[20];
2458 GNUNET_snprintf (macstr, sizeof (macstr), "%X:%X:%X:%X:%X:%X", mac[0], mac[1],
2459 mac[2], mac[3], mac[4], mac[5]);
2464 * Function for the scheduler if a mac endpoint times out
2465 * @param cls pointer to the MacEndpoint
2466 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
2469 macendpoint_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2471 struct MacEndpoint *endpoint = cls;
2473 GNUNET_assert (endpoint != NULL);
2474 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2475 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2479 if (GNUNET_TIME_absolute_get_remaining
2480 (GNUNET_TIME_absolute_add (endpoint->last_activity, MACENDPOINT_TIMEOUT)).
2483 free_macendpoint (endpoint->plugin, endpoint);
2487 endpoint->timeout_task =
2488 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
2494 * function to create an macendpoint
2495 * @param plugin pointer to the plugin struct
2496 * @param addr pointer to the macaddress
2497 * @return returns a macendpoint
2499 static struct MacEndpoint *
2500 create_macendpoint (struct Plugin *plugin, const struct MacAddress *addr)
2502 struct MacEndpoint *newend = GNUNET_malloc (sizeof (struct MacEndpoint));
2504 newend->addr = *addr;
2505 newend->plugin = plugin;
2506 newend->addr = *addr;
2507 newend->fragment_messages_out_count = 0;
2509 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
2510 MESSAGES_IN_DEFRAG_QUEUE_PER_MAC,
2511 newend, &wlan_data_message_handler,
2513 newend->last_activity = GNUNET_TIME_absolute_get ();
2514 newend->timeout_task =
2515 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
2518 plugin->mac_count++;
2519 GNUNET_CONTAINER_DLL_insert_tail (plugin->mac_head, plugin->mac_tail, newend);
2521 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2522 "New Mac Endpoint %p: %s\n", newend,
2523 wlan_plugin_address_to_string (NULL, newend->addr.mac, 6));
2529 * Function used for to process the data from the suid process
2531 * @param cls the plugin handle
2532 * @param client client that send the data (not used)
2533 * @param hdr header of the GNUNET_MessageHeader
2536 wlan_process_helper (void *cls, void *client,
2537 const struct GNUNET_MessageHeader *hdr)
2539 struct Plugin *plugin = cls;
2540 struct ieee80211_frame *wlanIeeeHeader = NULL;
2541 struct Session_light *session_light = NULL;
2542 struct Radiotap_rx *rxinfo;
2543 const struct GNUNET_MessageHeader *temp_hdr = NULL;
2548 switch (ntohs (hdr->type))
2550 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA:
2552 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2553 "Func wlan_process_helper got GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA size: %u\n",
2557 //call wlan_process_helper with the message inside, later with wlan: analyze signal
2558 if (ntohs (hdr->size) <
2559 sizeof (struct ieee80211_frame) + sizeof (struct GNUNET_MessageHeader) +
2560 sizeof (struct Radiotap_rx))
2563 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2564 "Size of packet is too small; size: %u min size: %u\n",
2566 sizeof (struct ieee80211_frame) +
2567 sizeof (struct GNUNET_MessageHeader));
2570 /* FIXME: restart SUID process */
2573 rxinfo = (struct Radiotap_rx *) &hdr[1];
2574 wlanIeeeHeader = (struct ieee80211_frame *) &rxinfo[1];
2576 //process only if it is an broadcast or for this computer both with the gnunet bssid
2580 (&(wlanIeeeHeader->i_addr3), &mac_bssid,
2581 sizeof (struct MacAddress)) == 0)
2583 //check for broadcast or mac
2585 (&(wlanIeeeHeader->i_addr1), &bc_all_mac,
2586 sizeof (struct MacAddress) == 0) ||
2587 memcmp (&(wlanIeeeHeader->i_addr1), &(plugin->mac_address),
2588 sizeof (struct MacAddress)) == 0)
2591 // process the inner data
2595 ntohs (hdr->size) - sizeof (struct ieee80211_frame) -
2596 sizeof (struct GNUNET_MessageHeader) - sizeof (struct Radiotap_rx);
2598 session_light = GNUNET_malloc (sizeof (struct Session_light));
2599 memcpy (&session_light->addr, &(wlanIeeeHeader->i_addr2),
2600 sizeof (struct MacAddress));
2601 //session_light->session = search_session(plugin,session_light->addr);
2604 while (pos < datasize)
2606 temp_hdr = (struct GNUNET_MessageHeader *) &wlanIeeeHeader[1] + pos;
2608 wlan_data_helper (plugin, session_light, temp_hdr, rxinfo);
2609 pos += ntohs (temp_hdr->size);
2614 GNUNET_free (session_light);
2619 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2620 "Func wlan_process_helper got wrong MAC: %s\n",
2621 macprinter (wlanIeeeHeader->i_addr1));
2628 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2629 "Func wlan_process_helper got wrong BSSID: %s\n",
2630 macprinter (wlanIeeeHeader->i_addr2));
2634 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
2635 //TODO more control messages
2636 //TODO use struct wlan_helper_control
2637 if (ntohs (hdr->size) != sizeof (struct Wlan_Helper_Control_Message))
2640 /* FIXME: restart SUID process */
2643 memcpy (&plugin->mac_address, &hdr[1], sizeof (struct MacAddress));
2645 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2646 "Received WLAN_HELPER_CONTROL message with transport of address %s\n",
2647 wlan_plugin_address_to_string (cls, &plugin->mac_address,
2651 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2652 &plugin->mac_address,
2653 sizeof (struct MacAddress));
2662 * We have been notified that wlan-helper has written something to stdout.
2663 * Handle the output, then reschedule this function to be called again once
2664 * more is available.
2666 * @param cls the plugin handle
2667 * @param tc the scheduling context
2670 wlan_plugin_helper_read (void *cls,
2671 const struct GNUNET_SCHEDULER_TaskContext *tc)
2673 struct Plugin *plugin = cls;
2675 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2677 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2680 char mybuf[WLAN_MTU + sizeof (struct GNUNET_MessageHeader)];
2684 GNUNET_DISK_file_read (plugin->server_stdout_handle, mybuf,
2689 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2691 ("Finished reading from wlan-helper stdout with code: %d\n"),
2696 GNUNET_SERVER_mst_receive (plugin->suid_tokenizer, NULL, mybuf, bytes,
2697 GNUNET_NO, GNUNET_NO);
2699 GNUNET_assert (plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2700 plugin->server_read_task =
2701 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2702 plugin->server_stdout_handle,
2703 &wlan_plugin_helper_read, plugin);
2707 * Start the gnunet-wlan-helper process.
2709 * @param plugin the transport plugin
2710 * @param testmode should we use the dummy driver for testing?
2711 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
2714 wlan_transport_start_wlan_helper (struct Plugin *plugin, int testmode)
2716 const char *filenamehw = "gnunet-transport-wlan-helper";
2717 const char *filenameloopback = "gnunet-transport-wlan-helper-dummy";
2719 plugin->server_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
2720 if (plugin->server_stdout == NULL)
2721 return GNUNET_SYSERR;
2723 plugin->server_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);
2724 if (plugin->server_stdin == NULL)
2725 return GNUNET_SYSERR;
2727 /* Start the server process */
2733 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2734 "Starting gnunet-wlan-helper process cmd: %s %s %i\n",
2735 filenamehw, plugin->interface, testmode);
2738 plugin->server_proc =
2739 GNUNET_OS_start_process (plugin->server_stdin, plugin->server_stdout,
2740 filenamehw, filenamehw, plugin->interface,
2743 else if (testmode == 1)
2747 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2748 "Starting gnunet-wlan-helper loopback 1 process cmd: %s %s %i\n",
2749 filenameloopback, plugin->interface, testmode);
2752 plugin->server_proc =
2753 GNUNET_OS_start_process (plugin->server_stdin, plugin->server_stdout,
2754 filenameloopback, filenameloopback, "1", NULL);
2756 else if (testmode == 2)
2759 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2760 "Starting gnunet-wlan-helper loopback 2 process cmd: %s %s %i\n",
2761 filenameloopback, plugin->interface, testmode);
2763 plugin->server_proc =
2764 GNUNET_OS_start_process (plugin->server_stdin, plugin->server_stdout,
2765 filenameloopback, filenameloopback, "2", NULL);
2767 if (plugin->server_proc == NULL)
2770 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2771 "Failed to start gnunet-wlan-helper process\n");
2773 return GNUNET_SYSERR;
2776 /* Close the write end of the read pipe */
2777 GNUNET_DISK_pipe_close_end (plugin->server_stdout,
2778 GNUNET_DISK_PIPE_END_WRITE);
2780 /* Close the read end of the write pipe */
2781 GNUNET_DISK_pipe_close_end (plugin->server_stdin, GNUNET_DISK_PIPE_END_READ);
2783 plugin->server_stdout_handle =
2784 GNUNET_DISK_pipe_handle (plugin->server_stdout,
2785 GNUNET_DISK_PIPE_END_READ);
2786 plugin->server_stdin_handle =
2787 GNUNET_DISK_pipe_handle (plugin->server_stdin,
2788 GNUNET_DISK_PIPE_END_WRITE);
2790 GNUNET_assert (plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2793 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2794 "Adding server_read_task for the wlan-helper\n");
2797 plugin->server_read_task =
2798 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2799 plugin->server_stdout_handle,
2800 &wlan_plugin_helper_read, plugin);
2806 * Exit point from the plugin.
2807 * @param cls pointer to the api struct
2812 libgnunet_plugin_transport_wlan_done (void *cls)
2814 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2815 struct Plugin *plugin = api->cls;
2816 struct MacEndpoint *endpoint = plugin->mac_head;
2817 struct MacEndpoint *endpoint_next;
2820 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2821 "libgnunet_plugin_transport_wlan_done started\n");
2824 GNUNET_DISK_pipe_close (plugin->server_stdout);
2825 GNUNET_DISK_pipe_close (plugin->server_stdin);
2826 GNUNET_OS_process_kill (plugin->server_proc, 9);
2827 GNUNET_OS_process_close (plugin->server_proc);
2829 GNUNET_assert (cls != NULL);
2831 while (endpoint != NULL)
2833 endpoint_next = endpoint->next;
2834 free_macendpoint (plugin, endpoint);
2835 endpoint = endpoint_next;
2838 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
2840 GNUNET_SCHEDULER_cancel (plugin->server_write_delay_task);
2841 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
2843 if (plugin->server_write_task != GNUNET_SCHEDULER_NO_TASK)
2845 GNUNET_SCHEDULER_cancel (plugin->server_write_task);
2846 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
2848 if (plugin->server_read_task != GNUNET_SCHEDULER_NO_TASK)
2850 GNUNET_SCHEDULER_cancel (plugin->server_read_task);
2851 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2854 if (plugin->suid_tokenizer != NULL)
2855 GNUNET_SERVER_mst_destroy (plugin->suid_tokenizer);
2857 if (plugin->data_tokenizer != NULL)
2858 GNUNET_SERVER_mst_destroy (plugin->data_tokenizer);
2860 GNUNET_free_non_null (plugin->interface);
2861 GNUNET_free (plugin);
2867 * Entry point for the plugin.
2869 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2870 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2873 libgnunet_plugin_transport_wlan_init (void *cls)
2875 //struct GNUNET_SERVICE_Context *service;
2876 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2877 struct GNUNET_TRANSPORT_PluginFunctions *api;
2878 struct Plugin *plugin;
2879 static unsigned long long testmode = 0;
2881 GNUNET_assert (cls != NULL);
2883 plugin = GNUNET_malloc (sizeof (struct Plugin));
2885 plugin->pendingsessions = 0;
2886 plugin->mac_count = 0;
2887 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
2888 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2889 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
2890 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
2891 GNUNET_BANDWIDTH_value_init (100 * 1024 *
2894 plugin->suid_tokenizer =
2895 GNUNET_SERVER_mst_create (&wlan_process_helper, plugin);
2897 plugin->data_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
2899 //plugin->sessions = GNUNET_malloc (sizeof (struct Sessionqueue));
2900 //plugin->pending_Sessions_head = GNUNET_malloc (sizeof (struct Sessionqueue));
2902 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2904 api->send = &wlan_plugin_send;
2905 api->disconnect = &wlan_plugin_disconnect;
2906 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2907 api->check_address = &wlan_plugin_address_suggested;
2908 api->address_to_string = &wlan_plugin_address_to_string;
2911 if (GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "TESTMODE"))
2913 if (GNUNET_SYSERR ==
2914 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-wlan",
2915 "TESTMODE", &testmode))
2916 testmode = 0; //default value
2919 if (GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "INTERFACE"))
2921 if (GNUNET_CONFIGURATION_get_value_string
2922 (env->cfg, "transport-wlan", "INTERFACE",
2923 &(plugin->interface)) != GNUNET_YES)
2925 libgnunet_plugin_transport_wlan_done (api);
2931 set_next_beacon_time (plugin);
2933 wlan_transport_start_wlan_helper (plugin, testmode);
2936 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2937 "wlan init finished\n");
2943 /* end of plugin_transport_wlan.c */