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;
666 do_transmit (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
668 free_session (struct Plugin *plugin, struct Sessionqueue *queue,
669 int do_free_macendpoint);
670 static struct MacEndpoint *
671 create_macendpoint (struct Plugin *plugin, const struct MacAddress *addr);
674 * Generates a nice hexdump of a memory area.
676 * \param mem pointer to memory to dump
677 * \param length how many bytes to dump
680 hexdump (void *mem, unsigned length)
683 char *src = (char *) mem;
685 printf ("dumping %u bytes from %p\r\n"
686 " 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF\r\n",
691 for (i = 0; i < length; i += 16, src += 16)
695 t += sprintf (t, "%04x: ", i);
696 for (j = 0; j < 16; j++)
699 t += sprintf (t, "%02X", src[j] & 0xff);
701 t += sprintf (t, " ");
702 t += sprintf (t, j % 2 ? " " : "-");
705 t += sprintf (t, " ");
706 for (j = 0; j < 16; j++)
710 if (isprint ((unsigned char) src[j]))
711 t += sprintf (t, "%c", src[j]);
713 t += sprintf (t, ".");
717 t += sprintf (t, " ");
721 t += sprintf (t, "\r\n");
727 * Function to find a MacEndpoint with a specific mac addr
728 * @param plugin pointer to the plugin struct
729 * @param addr pointer to the mac address
730 * @param create_new GNUNET_YES if a new end point should be created
733 static struct MacEndpoint *
734 get_macendpoint (struct Plugin *plugin, const struct MacAddress *addr,
737 struct MacEndpoint *queue = plugin->mac_head;
739 while (queue != NULL)
741 GNUNET_assert (queue->sessions_head != NULL);
742 if (memcmp (addr, &queue->addr, sizeof (struct MacAddress)) == 0)
743 return queue; /* session found */
747 if (create_new == GNUNET_YES)
749 return create_macendpoint (plugin, addr);
759 * search for a session with the addr and peer id
761 * @param plugin pointer to the plugin struct
762 * @param addr pointer to the mac address of the peer
763 * @param peer pointer to the peerid
764 * @return returns the session
766 static struct Session *
767 search_session (struct Plugin *plugin, const struct MacEndpoint *endpoint,
768 const struct GNUNET_PeerIdentity *peer)
770 GNUNET_assert (endpoint != NULL);
771 struct Sessionqueue *queue = endpoint->sessions_head;
773 while (queue != NULL)
775 GNUNET_assert (queue->content != NULL);
777 (peer, &queue->content->target,
778 sizeof (struct GNUNET_PeerIdentity)) == 0)
779 return queue->content; /* session found */
786 * Function called for a quick conversion of the binary address to
787 * a numeric address. Note that the caller must not free the
788 * address and that the next call to this function is allowed
789 * to override the address again.
792 * @param addr binary address
793 * @param addrlen length of the address
794 * @return string representing the same address
797 wlan_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
800 const struct MacAddress *mac;
802 if (addrlen != sizeof (struct MacAddress))
808 GNUNET_snprintf (ret, sizeof (ret), "%s Mac-Address %X:%X:%X:%X:%X:%X",
809 PROTOCOL_PREFIX, mac->mac[0], mac->mac[1], mac->mac[2],
810 mac->mac[3], mac->mac[4], mac->mac[5]);
815 * Function for the scheduler if a session times out
816 * @param cls pointer to the Sessionqueue
817 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
820 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
822 struct Sessionqueue *queue = cls;
824 GNUNET_assert (queue != NULL);
825 queue->content->timeout_task = GNUNET_SCHEDULER_NO_TASK;
826 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
830 if (GNUNET_TIME_absolute_get_remaining
831 (GNUNET_TIME_absolute_add
832 (queue->content->last_activity, SESSION_TIMEOUT)).rel_value == 0)
834 free_session (queue->content->mac->plugin, queue, GNUNET_YES);
838 queue->content->timeout_task =
839 GNUNET_SCHEDULER_add_delayed (SESSION_TIMEOUT, &session_timeout, queue);
844 * create a new session
846 * @param plugin pointer to the plugin struct
847 * @param addr pointer to the mac endpoint of the peer
848 * @return returns the session
851 static struct Session *
852 create_session (struct Plugin *plugin, struct MacEndpoint *endpoint,
853 const struct GNUNET_PeerIdentity *peer)
855 GNUNET_assert (endpoint != NULL);
856 struct Sessionqueue *queue =
857 GNUNET_malloc (sizeof (struct Sessionqueue) + sizeof (struct Session));
859 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
860 endpoint->sessions_tail, queue);
862 queue->content = (struct Session *) &queue[1];
863 queue->content->mac = endpoint;
864 memcpy (&(queue->content->target), peer, sizeof (struct GNUNET_PeerIdentity));
865 queue->content->last_activity = GNUNET_TIME_absolute_get ();
866 queue->content->timeout_task =
867 GNUNET_SCHEDULER_add_delayed (SESSION_TIMEOUT, &session_timeout, queue);
870 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
871 "New session %p with endpoint %p: %s\n", queue->content,
872 endpoint, wlan_plugin_address_to_string (NULL,
877 return queue->content;
881 * Get session from address, create if no session exists
883 * @param plugin pointer to the plugin struct
884 * @param addr pointer to the mac address of the peer
885 * @param peer pointer to the peerid
886 * @return returns the session
888 static struct Session *
889 get_session (struct Plugin *plugin, const struct MacAddress *addr,
890 const struct GNUNET_PeerIdentity *peer)
892 struct MacEndpoint *mac;
894 mac = get_macendpoint (plugin, addr, GNUNET_YES);
895 struct Session *session = search_session (plugin, mac, peer);
899 return create_session (plugin, mac, peer);
903 * Queue the session to send data
904 * checks if there is a message pending
905 * checks if this session is not allready in the queue
906 * @param plugin pointer to the plugin
907 * @param session pointer to the session to add
910 queue_session (struct Plugin *plugin, struct Session *session)
912 struct Sessionqueue *queue = plugin->pending_Sessions_head;
914 if (session->pending_message_head != NULL)
916 while (queue != NULL)
918 // content is never NULL
919 GNUNET_assert (queue->content != NULL);
920 // is session already in queue?
921 if (session == queue->content)
929 // Session is not in the queue
931 queue = GNUNET_malloc (sizeof (struct Sessionqueue));
932 queue->content = session;
935 GNUNET_CONTAINER_DLL_insert_tail (plugin->pending_Sessions_head,
936 plugin->pending_Sessions_tail, queue);
937 plugin->pendingsessions++;
943 * Function to schedule the write task, executed after a delay
944 * @param cls pointer to the plugin struct
945 * @param tc GNUNET_SCHEDULER_TaskContext pointer
948 delay_fragment_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
950 struct Plugin *plugin = cls;
952 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
954 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
957 // GNUNET_TIME_UNIT_FOREVER_REL is needed to clean up old msg
958 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
960 plugin->server_write_task =
961 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
962 plugin->server_stdin_handle,
963 &do_transmit, plugin);
968 * Function to calculate the time of the next periodic "hello-beacon"
969 * @param plugin pointer to the plugin struct
973 set_next_beacon_time (struct Plugin *const plugin)
975 //under 10 known peers: once a second
976 if (plugin->mac_count < 10)
978 plugin->beacon_time =
979 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
980 GNUNET_TIME_relative_multiply
981 (GNUNET_TIME_UNIT_SECONDS,
982 HALLO_BEACON_SCALING_FACTOR));
984 //under 30 known peers: every 10 seconds
985 else if (plugin->mac_count < 30)
987 plugin->beacon_time =
988 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
989 GNUNET_TIME_relative_multiply
990 (GNUNET_TIME_UNIT_SECONDS,
991 10 * HALLO_BEACON_SCALING_FACTOR));
993 //over 30 known peers: once a minute
996 plugin->beacon_time =
997 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
998 GNUNET_TIME_relative_multiply
999 (GNUNET_TIME_UNIT_MINUTES,
1000 HALLO_BEACON_SCALING_FACTOR));
1005 * Function to set the timer for the next timeout of the fragment queue
1006 * @param plugin the handle to the plugin struct
1010 set_next_send (struct Plugin *const plugin)
1012 struct GNUNET_TIME_Relative next_send;
1015 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
1017 GNUNET_SCHEDULER_cancel (plugin->server_write_delay_task);
1018 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
1021 //check if some acks are in the queue
1022 if (plugin->ack_send_queue_head != NULL)
1024 next_send = GNUNET_TIME_UNIT_ZERO;
1027 //check if there are some fragments in the queue
1028 else if (plugin->sending_messages_head != NULL)
1030 next_send = GNUNET_TIME_UNIT_ZERO;
1034 next_send = GNUNET_TIME_absolute_get_remaining (plugin->beacon_time);
1038 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1039 "Next packet is send in: %u\n", next_send.rel_value);
1042 if (next_send.rel_value == GNUNET_TIME_UNIT_ZERO.rel_value)
1044 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
1046 plugin->server_write_task =
1047 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1048 plugin->server_stdin_handle,
1049 &do_transmit, plugin);
1054 if (plugin->server_write_delay_task == GNUNET_SCHEDULER_NO_TASK)
1056 plugin->server_write_delay_task =
1057 GNUNET_SCHEDULER_add_delayed (next_send, &delay_fragment_task,
1064 * Function to get the next queued Session, removes the session from the queue
1065 * @param plugin pointer to the plugin struct
1066 * @return pointer to the session found, returns NULL if there is now session in the queue
1068 static struct Session *
1069 get_next_queue_session (struct Plugin *plugin)
1071 struct Session *session;
1072 struct Sessionqueue *sessionqueue;
1073 struct Sessionqueue *sessionqueue_alt;
1074 struct PendingMessage *pm;
1076 sessionqueue = plugin->pending_Sessions_head;
1078 while (sessionqueue != NULL)
1080 session = sessionqueue->content;
1082 GNUNET_assert (session != NULL);
1083 pm = session->pending_message_head;
1088 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1089 "pending message is empty, should not happen. session %p\n",
1094 GNUNET_assert (pm != NULL);
1096 //check for message timeout
1097 if (GNUNET_TIME_absolute_get_remaining (pm->timeout).rel_value > 0)
1099 //check if session has no message in the fragment queue
1100 if ((session->mac->fragment_messages_out_count <
1101 FRAGMENT_QUEUE_MESSAGES_OUT_PER_MACENDPOINT) &&
1102 (session->fragment_messages_out_count <
1103 FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION))
1105 plugin->pendingsessions--;
1106 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1107 plugin->pending_Sessions_tail,
1109 GNUNET_free (sessionqueue);
1115 sessionqueue = sessionqueue->next;
1120 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
1121 session->pending_message_tail, pm);
1123 //call the cont func that it did not work
1124 if (pm->transmit_cont != NULL)
1125 pm->transmit_cont (pm->transmit_cont_cls, &(session->target),
1127 GNUNET_free (pm->msg);
1130 if (session->pending_message_head == NULL)
1132 sessionqueue_alt = sessionqueue;
1133 sessionqueue = sessionqueue->next;
1134 plugin->pendingsessions--;
1135 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1136 plugin->pending_Sessions_tail,
1139 GNUNET_free (sessionqueue_alt);
1148 * frees the space of a message in the fragment queue (send queue)
1149 * @param plugin the plugin struct
1150 * @param fm message to free
1153 free_fragment_message (struct Plugin *plugin, struct FragmentMessage *fm)
1155 struct Session *session = fm->session;
1156 struct MacEndpoint *endpoint = session->mac;
1157 struct FragmentMessage_queue *fmq;
1158 struct FragmentMessage_queue *fmq_next;
1162 fmq = plugin->sending_messages_head;
1165 fmq_next = fmq->next;
1166 if (fmq->content == fm)
1168 GNUNET_CONTAINER_DLL_remove (plugin->sending_messages_head,
1169 plugin->sending_messages_tail, fmq);
1175 (session->mac->fragment_messages_out_count)--;
1176 session->fragment_messages_out_count--;
1177 plugin->pending_Fragment_Messages--;
1178 GNUNET_CONTAINER_DLL_remove (endpoint->sending_messages_head,
1179 endpoint->sending_messages_tail, fm);
1180 GNUNET_FRAGMENT_context_destroy (fm->fragcontext);
1181 if (fm->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1182 GNUNET_SCHEDULER_cancel (fm->timeout_task);
1185 queue_session (plugin, session);
1187 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1188 "Free pending fragment messages %p, session %p\n", fm,
1195 * function to fill the radiotap header
1196 * @param plugin pointer to the plugin struct
1197 * @param endpoint pointer to the endpoint
1198 * @param header pointer to the radiotap header
1199 * @return GNUNET_YES at success
1202 getRadiotapHeader (struct Plugin *plugin, struct MacEndpoint *endpoint,
1203 struct Radiotap_Send *header)
1206 if (endpoint != NULL)
1208 header->rate = endpoint->rate;
1209 header->tx_power = endpoint->tx_power;
1210 header->antenna = endpoint->antenna;
1215 header->tx_power = 0;
1216 header->antenna = 0;
1223 * function to generate the wlan hardware header for one packet
1224 * @param Header address to write the header to
1225 * @param to_mac_addr address of the recipient
1226 * @param plugin pointer to the plugin struct
1227 * @return GNUNET_YES if there was no error
1230 getWlanHeader (struct ieee80211_frame *Header,
1231 const struct MacAddress *to_mac_addr, struct Plugin *plugin,
1235 const int rate = 11000000;
1237 Header->i_fc[0] = IEEE80211_FC0_TYPE_DATA;
1238 Header->i_fc[1] = 0x00;
1239 memcpy (&Header->i_addr3, &mac_bssid, sizeof (mac_bssid));
1240 memcpy (&Header->i_addr2, plugin->mac_address.mac,
1241 sizeof (plugin->mac_address));
1242 memcpy (&Header->i_addr1, to_mac_addr, sizeof (struct MacAddress));
1244 tmp16 = (uint16_t *) Header->i_dur;
1245 *tmp16 = (uint16_t) htole16 ((size * 1000000) / rate + 290);
1247 #if DEBUG_wlan_ip_udp_packets_on_air
1252 Header->ip.ip_dst.s_addr = *((uint32_t *) & to_mac_addr->mac[2]);
1253 Header->ip.ip_src.s_addr = *((uint32_t *) & plugin->mac_address.mac[2]);
1254 Header->ip.ip_v = 4;
1255 Header->ip.ip_hl = 5;
1256 Header->ip.ip_p = 17;
1257 Header->ip.ip_ttl = 1;
1258 Header->ip.ip_len = htons (size + 8);
1259 Header->ip.ip_sum = 0;
1260 x = (uint16_t *) & Header->ip;
1261 count = sizeof (struct iph);
1264 /* This is the inner loop */
1265 crc += (unsigned short) *x++;
1268 /* Add left-over byte, if any */
1270 crc += *(unsigned char *) x;
1271 crc = (crc & 0xffff) + (crc >> 16);
1272 Header->ip.ip_sum = htons (~(unsigned short) crc);
1275 Header->udp.len = htons (size - sizeof (struct ieee80211_frame));
1285 * @param msgbuf pointer tor the data
1286 * @param msgbuf_size size of the data
1288 * @return 32bit crc value
1292 getcrc32 (const char *msgbuf, size_t msgbuf_size)
1295 return GNUNET_CRYPTO_crc32_n (msgbuf, msgbuf_size);;
1301 * @param msgbuf pointer tor the data
1302 * @param msgbuf_size size of the data
1304 * @return 16bit crc value
1308 getcrc16 (const char *msgbuf, size_t msgbuf_size)
1310 //TODO calc some crc
1315 * function to add a fragment of a message to send
1316 * @param cls FragmentMessage this message belongs to
1317 * @param hdr pointer to the start of the message
1321 add_message_for_send (void *cls, const struct GNUNET_MessageHeader *hdr)
1324 struct FragmentMessage *fm = cls;
1325 struct FragmentMessage_queue *fmqueue;
1327 GNUNET_assert (cls != NULL);
1328 GNUNET_assert (fm->frag == NULL);
1329 struct MacEndpoint *endpoint = fm->session->mac;
1330 struct Plugin *plugin = endpoint->plugin;
1331 struct GNUNET_MessageHeader *msgheader;
1332 struct GNUNET_MessageHeader *msgheader2;
1335 #if DEBUG_wlan_retransmission
1336 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1337 "Adding fragment of message %p to send, session %p, endpoint %p\n",
1338 fm, fm->session, endpoint);
1342 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1343 sizeof (struct ieee80211_frame) + ntohs (hdr->size);
1344 fm->frag = GNUNET_malloc (size);
1347 msgheader = (struct GNUNET_MessageHeader *) fm->frag;
1348 msgheader->size = htons (size);
1349 msgheader->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1351 fm->radioHeader = (struct Radiotap_Send *) &msgheader[1];
1352 fm->ieeewlanheader = (struct ieee80211_frame *) &fm->radioHeader[1];
1353 msgheader2 = (struct GNUNET_MessageHeader *) &fm->ieeewlanheader[1];
1354 memcpy (msgheader2, hdr, ntohs (hdr->size));
1356 fmqueue = GNUNET_malloc (sizeof (struct FragmentMessage_queue));
1357 fmqueue->content = fm;
1359 GNUNET_CONTAINER_DLL_insert_tail (plugin->sending_messages_head,
1360 plugin->sending_messages_tail, fmqueue);
1361 set_next_send (plugin);
1365 * function to send a hallo beacon
1366 * @param plugin pointer to the plugin struct
1369 send_hello_beacon (struct Plugin *plugin)
1373 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1374 "Sending hello beacon\n");
1379 uint16_t hallo_size;
1380 struct GNUNET_MessageHeader *msgheader;
1381 struct ieee80211_frame *ieeewlanheader;
1382 struct Radiotap_Send *radioHeader;
1383 struct GNUNET_MessageHeader *msgheader2;
1384 const struct GNUNET_MessageHeader *hello;
1386 hello = plugin->env->get_our_hello ();
1387 hallo_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1388 GNUNET_assert (sizeof (struct WlanHeader) + hallo_size <= WLAN_MTU);
1390 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1391 sizeof (struct ieee80211_frame) + sizeof (struct GNUNET_MessageHeader) +
1394 msgheader = GNUNET_malloc (size);
1395 msgheader->size = htons (size);
1396 msgheader->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1398 radioHeader = (struct Radiotap_Send *) &msgheader[1];
1399 getRadiotapHeader (plugin, NULL, radioHeader);
1400 ieeewlanheader = (struct ieee80211_frame *) &radioHeader[1];
1401 getWlanHeader (ieeewlanheader, &bc_all_mac, plugin, size);
1403 msgheader2 = (struct GNUNET_MessageHeader *) &ieeewlanheader[1];
1405 htons (GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello) +
1406 sizeof (struct GNUNET_MessageHeader));
1408 msgheader2->type = htons (GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT);
1409 memcpy (&msgheader2[1], hello, hallo_size);
1411 bytes = GNUNET_DISK_file_write (plugin->server_stdin_handle, msgheader, size);
1413 if (bytes == GNUNET_SYSERR)
1415 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1417 ("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1418 errno, strerror (errno));
1421 GNUNET_assert (bytes != GNUNET_SYSERR);
1422 GNUNET_assert (bytes == size);
1423 GNUNET_free (msgheader);
1425 set_next_beacon_time (plugin);
1426 set_next_send (plugin);
1430 * function to add an ack to send it for a received fragment
1431 * @param cls MacEndpoint this ack belongs to
1432 * @param msg_id id of the message
1433 * @param hdr pointer to the hdr where the ack is stored
1438 add_ack_for_send (void *cls, uint32_t msg_id,
1439 const struct GNUNET_MessageHeader *hdr)
1442 struct AckSendQueue *ack;
1444 GNUNET_assert (cls != NULL);
1445 struct MacEndpoint *endpoint = cls;
1446 struct Plugin *plugin = endpoint->plugin;
1447 struct GNUNET_MessageHeader *msgheader;
1448 struct GNUNET_MessageHeader *msgheader2;
1452 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1453 sizeof (struct ieee80211_frame) + ntohs (hdr->size) +
1454 sizeof (struct AckSendQueue);
1456 ack = GNUNET_malloc (size);
1457 ack->message_id = msg_id;
1458 ack->endpoint = endpoint;
1461 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1462 sizeof (struct ieee80211_frame) + ntohs (hdr->size);
1464 msgheader = (struct GNUNET_MessageHeader *) &ack[1];
1465 ack->hdr = (struct GNUNET_MessageHeader *) &ack[1];
1466 msgheader->size = htons (size);
1467 msgheader->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1469 ack->radioHeader = (struct Radiotap_Send *) &msgheader[1];
1470 ack->ieeewlanheader = (struct ieee80211_frame *) &(ack->radioHeader)[1];
1471 msgheader2 = (struct GNUNET_MessageHeader *) &(ack->ieeewlanheader)[1];
1472 memcpy (msgheader2, hdr, ntohs (hdr->size));
1474 GNUNET_CONTAINER_DLL_insert_tail (plugin->ack_send_queue_head,
1475 plugin->ack_send_queue_tail, ack);
1477 #if DEBUG_wlan_retransmission
1478 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1479 "Adding ack with message id %u to send, AckSendQueue %p, endpoint %p\n",
1480 msg_id, ack, endpoint);
1483 set_next_send (plugin);
1487 * Function for the scheduler if a FragmentMessage times out
1488 * @param cls pointer to the FragmentMessage
1489 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
1492 fragmentmessage_timeout (void *cls,
1493 const struct GNUNET_SCHEDULER_TaskContext *tc)
1495 struct FragmentMessage *fm = cls;
1497 GNUNET_assert (fm != NULL);
1498 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1499 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1503 free_fragment_message (fm->session->mac->plugin, fm);
1507 * Function to check if there is some space in the fragment queue
1508 * inserts a message if space is available
1509 * @param plugin the plugin struct
1513 check_fragment_queue (struct Plugin *plugin)
1515 struct Session *session;
1516 struct FragmentMessage *fm;
1517 struct GNUNET_PeerIdentity pid;
1519 struct PendingMessage *pm;
1521 if (plugin->pending_Fragment_Messages < FRAGMENT_QUEUE_SIZE)
1523 session = get_next_queue_session (plugin);
1524 if (session != NULL)
1526 pm = session->pending_message_head;
1527 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
1528 session->pending_message_tail, pm);
1529 session->mac->fragment_messages_out_count++;
1530 session->fragment_messages_out_count++;
1531 plugin->pending_Fragment_Messages++;
1532 GNUNET_assert (pm != NULL);
1534 fm = GNUNET_malloc (sizeof (struct FragmentMessage));
1535 fm->session = session;
1536 fm->timeout.abs_value = pm->timeout.abs_value;
1539 GNUNET_FRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
1541 GNUNET_TIME_UNIT_SECONDS,
1543 &add_message_for_send, fm);
1545 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
1546 (fm->timeout), fragmentmessage_timeout,
1548 GNUNET_CONTAINER_DLL_insert_tail (session->mac->sending_messages_head,
1549 session->mac->sending_messages_tail,
1552 if (pm->transmit_cont != NULL)
1554 pid = session->target;
1555 pm->transmit_cont (pm->transmit_cont_cls, &pid, GNUNET_OK);
1557 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1558 "called pm->transmit_cont for %p\n", session);
1564 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1565 "no pm->transmit_cont for %p\n", session);
1570 if (session->pending_message_head != NULL)
1573 queue_session (plugin, session);
1579 //check if timeout changed
1580 set_next_send (plugin);
1584 * Function to send an ack, does not free the ack
1585 * @param plugin pointer to the plugin
1586 * @param ack pointer to the ack to send
1589 send_ack (struct Plugin *plugin, struct AckSendQueue *ack)
1595 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1596 "Sending ack for message_id %u for mac endpoint %p, size %u\n",
1597 ack->message_id, ack->endpoint,
1598 ntohs (ack->hdr->size) - sizeof (struct Radiotap_Send));
1601 getRadiotapHeader (plugin, ack->endpoint, ack->radioHeader);
1602 getWlanHeader (ack->ieeewlanheader, &ack->endpoint->addr, plugin,
1603 ntohs (ack->hdr->size));
1606 GNUNET_DISK_file_write (plugin->server_stdin_handle, ack->hdr,
1607 ntohs (ack->hdr->size));
1608 if (bytes == GNUNET_SYSERR)
1610 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1612 ("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1613 errno, strerror (errno));
1616 GNUNET_assert (bytes != GNUNET_SYSERR);
1617 GNUNET_assert (bytes == ntohs (ack->hdr->size));
1618 set_next_send (plugin);
1622 * function to finish a sending if not all could have been writen befor
1623 * @param cls pointer to the Finish_send struct
1624 * @param tc TaskContext
1627 finish_sending (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1629 struct Finish_send *finish = cls;
1630 struct Plugin *plugin;
1633 plugin = finish->plugin;
1634 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1636 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1638 GNUNET_free (finish->msgstart);
1639 GNUNET_free (finish);
1643 GNUNET_DISK_file_write (plugin->server_stdin_handle, finish->msgheader,
1645 GNUNET_assert (bytes != GNUNET_SYSERR);
1647 if (bytes != finish->size)
1649 finish->msgheader = finish->msgheader + bytes;
1650 finish->size = finish->size - bytes;
1651 plugin->server_write_task =
1652 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1653 plugin->server_stdin_handle,
1654 &finish_sending, finish);
1658 GNUNET_free (finish->msgstart);
1659 GNUNET_free (finish);
1660 set_next_send (plugin);
1665 * Function called when wlan helper is ready to get some data
1667 * @param cls closure
1668 * @param tc GNUNET_SCHEDULER_TaskContext
1671 do_transmit (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1673 struct Plugin *plugin = cls;
1675 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1676 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1679 struct Session *session;
1680 struct FragmentMessage *fm;
1681 struct Finish_send *finish;
1682 struct FragmentMessage_queue *fmq;
1683 struct AckSendQueue *ack;
1686 if (plugin->ack_send_queue_head != NULL)
1688 ack = plugin->ack_send_queue_head;
1689 GNUNET_CONTAINER_DLL_remove (plugin->ack_send_queue_head,
1690 plugin->ack_send_queue_tail, ack);
1691 send_ack (plugin, ack);
1696 //test if a "hello-beacon" has to be send
1697 if (GNUNET_TIME_absolute_get_remaining (plugin->beacon_time).rel_value == 0)
1699 send_hello_beacon (plugin);
1703 if (plugin->sending_messages_head != NULL)
1705 fmq = plugin->sending_messages_head;
1707 GNUNET_CONTAINER_DLL_remove (plugin->sending_messages_head,
1708 plugin->sending_messages_tail, fmq);
1711 session = fm->session;
1712 GNUNET_assert (session != NULL);
1715 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1716 "Sending GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT for fragment message %p, size: %u\n",
1720 getRadiotapHeader (plugin, session->mac, fm->radioHeader);
1721 getWlanHeader (fm->ieeewlanheader, &(fm->session->mac->addr), plugin,
1725 GNUNET_DISK_file_write (plugin->server_stdin_handle, fm->frag,
1727 if (bytes == GNUNET_SYSERR)
1729 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1731 ("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1732 errno, strerror (errno));
1735 GNUNET_assert (bytes != GNUNET_SYSERR);
1737 if (bytes != fm->size)
1739 finish = GNUNET_malloc (sizeof (struct Finish_send));
1740 finish->plugin = plugin;
1741 finish->msgheader = fm->frag + bytes;
1742 finish->size = fm->size - bytes;
1743 finish->msgstart = (struct GNUNET_MessageHeader *) fm->frag;
1745 GNUNET_assert (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK);
1747 plugin->server_write_task =
1748 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1749 plugin->server_stdin_handle,
1750 &finish_sending, finish);
1755 GNUNET_free (fm->frag);
1757 set_next_send (plugin);
1759 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
1763 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
1764 "do_transmit did nothing, should not happen!\n");
1768 * Another peer has suggested an address for this
1769 * peer and transport plugin. Check that this could be a valid
1770 * address. If so, consider adding it to the list
1773 * @param cls closure
1774 * @param addr pointer to the address
1775 * @param addrlen length of addr
1776 * @return GNUNET_OK if this is a plausible address for this peer
1780 wlan_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1782 //struct Plugin *plugin = cls;
1784 /* check if the address is plausible; if so,
1785 * add it to our list! */
1787 GNUNET_assert (cls != NULL);
1788 //FIXME mitm is not checked
1789 //Mac Address has 6 bytes
1792 /* TODO check for bad addresses like multicast, broadcast, etc */
1796 return GNUNET_SYSERR;
1800 * Function that can be used by the transport service to transmit
1801 * a message using the plugin.
1803 * @param cls closure
1804 * @param target who should receive this message
1805 * @param priority how important is the message
1806 * @param msgbuf the message to transmit
1807 * @param msgbuf_size number of bytes in 'msgbuf'
1808 * @param timeout when should we time out
1809 * @param session which session must be used (or NULL for "any")
1810 * @param addr the address to use (can be NULL if the plugin
1811 * is "on its own" (i.e. re-use existing TCP connection))
1812 * @param addrlen length of the address in bytes
1813 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1814 * otherwise the plugin may use other addresses or
1815 * existing connections (if available)
1816 * @param cont continuation to call once the message has
1817 * been transmitted (or if the transport is ready
1818 * for the next transmission call; or if the
1819 * peer disconnected...)
1820 * @param cont_cls closure for cont
1821 * @return number of bytes used (on the physical network, with overheads);
1822 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1823 * and does NOT mean that the message was not transmitted (DV)
1826 wlan_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
1827 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
1828 struct GNUNET_TIME_Relative timeout, struct Session *session,
1829 const void *addr, size_t addrlen, int force_address,
1830 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1832 struct Plugin *plugin = cls;
1833 struct PendingMessage *newmsg;
1834 struct WlanHeader *wlanheader;
1836 //check if msglen > 0
1837 GNUNET_assert (msgbuf_size > 0);
1839 //get session if needed
1840 if (session == NULL)
1842 if (wlan_plugin_address_suggested (plugin, addr, addrlen) == GNUNET_OK)
1844 session = get_session (plugin, addr, target);
1848 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1849 _("Wlan Address len %d is wrong\n"), addrlen);
1856 //queue message in session
1857 //test if there is no other message in the "queue"
1858 //FIXME: to many send requests
1859 if (session->pending_message_head != NULL)
1861 newmsg = session->pending_message_head;
1862 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1863 "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",
1864 GNUNET_TIME_absolute_get_remaining (newmsg->
1866 session->mac->fragment_messages_out_count);
1869 newmsg = GNUNET_malloc (sizeof (struct PendingMessage));
1870 newmsg->msg = GNUNET_malloc (msgbuf_size + sizeof (struct WlanHeader));
1871 wlanheader = newmsg->msg;
1872 //copy msg to buffer, not fragmented / segmented yet, but with message header
1873 wlanheader->header.size = htons (msgbuf_size + sizeof (struct WlanHeader));
1874 wlanheader->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA);
1875 memcpy (&(wlanheader->target), target, sizeof (struct GNUNET_PeerIdentity));
1876 memcpy (&(wlanheader->source), plugin->env->my_identity,
1877 sizeof (struct GNUNET_PeerIdentity));
1878 wlanheader->crc = 0;
1879 memcpy (&wlanheader[1], msgbuf, msgbuf_size);
1882 ((char *) wlanheader, msgbuf_size + sizeof (struct WlanHeader)));
1883 //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);
1884 //hexdump(newmsg->msg, msgbuf_size + sizeof(struct WlanHeader));
1886 newmsg->transmit_cont = cont;
1887 newmsg->transmit_cont_cls = cont_cls;
1888 newmsg->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1890 newmsg->timeout.abs_value = newmsg->timeout.abs_value - 500;
1892 newmsg->message_size = msgbuf_size + sizeof (struct WlanHeader);
1894 GNUNET_CONTAINER_DLL_insert_tail (session->pending_message_head,
1895 session->pending_message_tail, newmsg);
1898 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1899 "New message for %p with size (incl wlan header) %u added\n",
1900 session, newmsg->message_size);
1904 queue_session (plugin, session);
1906 check_fragment_queue (plugin);
1907 //FIXME not the correct size
1913 * function to free a mac endpoint
1914 * @param plugin pointer to the plugin struct
1915 * @param endpoin pointer to the MacEndpoint to free
1918 free_macendpoint (struct Plugin *plugin, struct MacEndpoint *endpoint)
1920 struct Sessionqueue *sessions;
1921 struct Sessionqueue *sessions_next;
1923 GNUNET_assert (endpoint != NULL);
1925 sessions = endpoint->sessions_head;
1926 while (sessions != NULL)
1928 sessions_next = sessions->next;
1929 free_session (plugin, sessions, GNUNET_NO);
1930 sessions = sessions_next;
1933 GNUNET_CONTAINER_DLL_remove (plugin->mac_head, plugin->mac_tail, endpoint);
1934 if (endpoint->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1935 GNUNET_SCHEDULER_cancel (endpoint->timeout_task);
1936 GNUNET_free (endpoint);
1941 * function to free a session
1942 * @param plugin pointer to the plugin
1943 * @param queue pointer to the sessionqueue element to free
1944 * @param free_macendpoint if GNUNET_YES and mac endpoint would be empty, free mac endpoint
1947 free_session (struct Plugin *plugin, struct Sessionqueue *queue,
1948 int do_free_macendpoint)
1950 struct Sessionqueue *pendingsession;
1951 struct Sessionqueue *pendingsession_tmp;
1952 struct PendingMessage *pm;
1953 struct MacEndpoint *endpoint;
1954 struct FragmentMessage *fm;
1955 struct FragmentMessage *fmnext;
1958 GNUNET_assert (queue != NULL);
1959 GNUNET_assert (queue->content != NULL);
1962 //is this session pending for send
1963 pendingsession = plugin->pending_Sessions_head;
1964 while (pendingsession != NULL)
1966 pendingsession_tmp = pendingsession->next;
1967 if (pendingsession->content == queue->content)
1969 plugin->pendingsessions--;
1970 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1971 plugin->pending_Sessions_tail,
1973 GNUNET_free (pendingsession);
1975 GNUNET_assert (check == 0);
1978 pendingsession = pendingsession_tmp;
1981 endpoint = queue->content->mac;
1982 fm = endpoint->sending_messages_head;
1986 if (fm->session == queue->content)
1988 free_fragment_message (plugin, fm);
1993 // remove PendingMessage
1994 pm = queue->content->pending_message_head;
1997 GNUNET_CONTAINER_DLL_remove (queue->content->pending_message_head,
1998 queue->content->pending_message_tail, pm);
1999 GNUNET_free (pm->msg);
2001 pm = queue->content->pending_message_head;
2004 GNUNET_CONTAINER_DLL_remove (endpoint->sessions_head, endpoint->sessions_tail,
2007 if (endpoint->sessions_head == NULL && do_free_macendpoint == GNUNET_YES)
2009 free_macendpoint (plugin, endpoint);
2012 if (queue->content->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2013 GNUNET_SCHEDULER_cancel (queue->content->timeout_task);
2014 GNUNET_free (queue);
2015 check_fragment_queue (plugin);
2019 * Function that can be used to force the plugin to disconnect
2020 * from the given peer and cancel all previous transmissions
2021 * (and their continuation).
2023 * @param cls closure
2024 * @param target peer from which to disconnect
2027 wlan_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
2029 struct Plugin *plugin = cls;
2030 struct Sessionqueue *queue;
2031 struct Sessionqueue *queue_next;
2032 struct MacEndpoint *endpoint = plugin->mac_head;
2033 struct MacEndpoint *endpoint_next;
2035 // just look at all the session for the needed one
2036 while (endpoint != NULL)
2038 queue = endpoint->sessions_head;
2039 endpoint_next = endpoint->next;
2040 while (queue != NULL)
2042 // content is never NULL
2043 GNUNET_assert (queue->content != NULL);
2044 queue_next = queue->next;
2046 (target, &(queue->content->target),
2047 sizeof (struct GNUNET_PeerIdentity)) == 0)
2049 free_session (plugin, queue, GNUNET_YES);
2054 endpoint = endpoint_next;
2059 * Convert the transports address to a nice, human-readable
2062 * @param cls closure
2063 * @param type name of the transport that generated the address
2064 * @param addr one of the addresses of the host, NULL for the last address
2065 * the specific address format depends on the transport
2066 * @param addrlen length of the address
2067 * @param numeric should (IP) addresses be displayed in numeric form?
2068 * @param timeout after how long should we give up?
2069 * @param asc function to call on each string
2070 * @param asc_cls closure for asc
2073 wlan_plugin_address_pretty_printer (void *cls, const char *type,
2074 const void *addr, size_t addrlen,
2076 struct GNUNET_TIME_Relative timeout,
2077 GNUNET_TRANSPORT_AddressStringCallback asc,
2081 const unsigned char *input;
2083 //GNUNET_assert(cls !=NULL);
2086 /* invalid address (MAC addresses have 6 bytes) */ GNUNET_break (0);
2087 asc (asc_cls, NULL);
2090 input = (const unsigned char *) addr;
2091 GNUNET_snprintf (ret, sizeof (ret),
2092 "%s Mac-Address %.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
2093 PROTOCOL_PREFIX, input[0], input[1], input[2], input[3],
2094 input[4], input[5]);
2099 * handels the data after all fragments are put together
2101 * @param session_light
2102 * @param hdr pointer to the data
2105 wlan_data_message_handler (void *cls, const struct GNUNET_MessageHeader *hdr)
2107 struct MacEndpoint *endpoint = (struct MacEndpoint *) cls;
2108 struct Plugin *plugin = endpoint->plugin;
2109 struct WlanHeader *wlanheader;
2110 struct Session *session;
2112 //const char * tempmsg;
2113 const struct GNUNET_MessageHeader *temp_hdr;
2114 struct GNUNET_PeerIdentity tmpsource;
2117 if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_DATA)
2121 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2122 "Func wlan_data_message_handler got GNUNET_MESSAGE_TYPE_WLAN_DATA size: %u\n",
2126 if (ntohs (hdr->size) <
2127 sizeof (struct WlanHeader) + sizeof (struct GNUNET_MessageHeader))
2129 //packet not big enought
2133 wlanheader = (struct WlanHeader *) hdr;
2135 session = search_session (plugin, endpoint, &wlanheader->source);
2137 //tempmsg = (char*) &wlanheader[1];
2138 temp_hdr = (const struct GNUNET_MessageHeader *) &wlanheader[1];
2139 crc = ntohl (wlanheader->crc);
2140 wlanheader->crc = 0;
2141 if (getcrc32 ((char *) wlanheader, ntohs (wlanheader->header.size)) != crc)
2143 //wrong crc, dispose message
2144 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2145 "Wlan message header crc was wrong: %u != %u\n",
2146 getcrc32 ((char *) wlanheader,
2147 ntohs (wlanheader->header.size)), crc);
2148 hexdump ((void *) hdr, ntohs (hdr->size));
2152 //if not in session list
2153 if (session == NULL)
2156 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2157 "WLAN client not in session list: packet size = %u, inner size = %u, header size = %u\n",
2158 ntohs (wlanheader->header.size), ntohs (temp_hdr->size),
2159 sizeof (struct WlanHeader));
2161 //try if it is a hello message
2162 if (ntohs (wlanheader->header.size) >=
2163 ntohs (temp_hdr->size) + sizeof (struct WlanHeader))
2165 if (ntohs (temp_hdr->type) == GNUNET_MESSAGE_TYPE_HELLO)
2167 if (GNUNET_HELLO_get_id
2168 ((const struct GNUNET_HELLO_Message *) temp_hdr,
2169 &tmpsource) == GNUNET_OK)
2171 session = create_session (plugin, endpoint, &tmpsource);
2175 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2176 "WLAN client not in session list and hello message is not okay\n");
2183 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2184 "WLAN client not in session list and not a hello message\n");
2190 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2191 "WLAN client not in session list and message size in does not fit\npacket size = %u, inner size = %u, header size = %u\n",
2192 ntohs (wlanheader->header.size),
2193 ntohs (temp_hdr->size), sizeof (struct WlanHeader));
2198 //"receive" the message
2201 (&wlanheader->source, &session->target,
2202 sizeof (struct GNUNET_PeerIdentity)) != 0)
2206 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2207 "WLAN peer source id doesn't match packet peer source id: session %p\n",
2214 (&wlanheader->target, plugin->env->my_identity,
2215 sizeof (struct GNUNET_PeerIdentity)) != 0)
2219 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2220 "WLAN peer target id doesn't match our peer id: session %p\n",
2226 GNUNET_SERVER_mst_receive (plugin->data_tokenizer, session,
2227 (const char *) temp_hdr,
2228 ntohs (hdr->size) - sizeof (struct WlanHeader),
2229 GNUNET_YES, GNUNET_NO);
2235 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2236 "wlan_data_message_handler got wrong message type\n");
2242 * function to process the a message, give it to the higher layer
2243 * @param cls pointer to the plugin
2244 * @param client pointer to the session this message belongs to
2245 * @param hdr start of the message
2247 //TODO ATS informations
2249 process_data (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
2252 GNUNET_assert (client != NULL);
2253 GNUNET_assert (cls != NULL);
2254 struct Session *session = (struct Session *) client;
2255 struct Plugin *plugin = (struct Plugin *) cls;
2257 struct GNUNET_TRANSPORT_ATS_Information distance[2];
2259 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2260 distance[0].value = htonl (1);
2261 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2262 distance[1].value = htonl (0);
2265 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2266 "Calling plugin->env->receive for session %p; %s; size: %u\n",
2267 session, wlan_plugin_address_to_string (NULL,
2273 plugin->env->receive (plugin->env->cls, &(session->target), hdr,
2274 (const struct GNUNET_TRANSPORT_ATS_Information *)
2275 &distance, 2, session,
2276 (const char *) &session->mac->addr,
2277 sizeof (session->mac->addr));
2281 * Function used for to process the data received from the wlan interface
2283 * @param cls the plugin handle
2284 * @param session_light FIXME: document
2285 * @param hdr hdr of the GNUNET_MessageHeader
2288 wlan_data_helper (void *cls, struct Session_light *session_light,
2289 const struct GNUNET_MessageHeader *hdr,
2290 const struct Radiotap_rx *rxinfo)
2292 struct Plugin *plugin = cls;
2293 struct FragmentMessage *fm;
2294 struct FragmentMessage *fm2;
2297 if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT)
2300 //TODO better DOS protection, error handling
2301 //TODO test first than create session
2302 GNUNET_assert (session_light != NULL);
2305 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2306 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT size: %u; %s\n",
2307 ntohs (hdr->size), wlan_plugin_address_to_string (NULL,
2308 session_light->addr.
2312 if (session_light->macendpoint == NULL)
2314 session_light->macendpoint =
2315 get_macendpoint (plugin, &session_light->addr, GNUNET_NO);
2317 GNUNET_assert (GNUNET_HELLO_get_id
2318 ((const struct GNUNET_HELLO_Message *) &hdr[1],
2319 &(session_light->session->target)) != GNUNET_SYSERR);
2325 else if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_FRAGMENT)
2328 GNUNET_assert (session_light != NULL);
2329 if (session_light->macendpoint == NULL)
2331 session_light->macendpoint =
2332 get_macendpoint (plugin, &session_light->addr, GNUNET_YES);
2336 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2337 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_FRAGMENT with size: %u; mac endpoint %p: %s\n",
2338 ntohs (hdr->size), session_light->macendpoint,
2339 wlan_plugin_address_to_string (NULL,
2340 session_light->addr.mac,
2345 GNUNET_DEFRAGMENT_process_fragment (session_light->macendpoint->defrag,
2348 if (ret == GNUNET_NO)
2350 session_light->macendpoint->dups++;
2352 else if (ret == GNUNET_OK)
2354 session_light->macendpoint->fragc++;
2356 set_next_send (plugin);
2362 else if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_FRAGMENT_ACK)
2364 GNUNET_assert (session_light != NULL);
2365 if (session_light->macendpoint == NULL)
2367 session_light->macendpoint =
2368 get_macendpoint (plugin, &session_light->addr, GNUNET_NO);
2371 if (session_light->macendpoint == NULL)
2374 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2375 "Macendpoint does not exist for this GNUNET_MESSAGE_TYPE_FRAGMENT_ACK size: %u; %s\n",
2376 ntohs (hdr->size), wlan_plugin_address_to_string (NULL,
2377 session_light->addr.mac,
2384 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2385 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_FRAGMENT_ACK size: %u; mac endpoint: %p; %s\n",
2386 ntohs (hdr->size), session_light->macendpoint,
2387 wlan_plugin_address_to_string (NULL,
2388 session_light->addr.mac,
2391 fm = session_light->macendpoint->sending_messages_head;
2395 int ret = GNUNET_FRAGMENT_process_ack (fm->fragcontext, hdr);
2397 if (ret == GNUNET_OK)
2399 #if DEBUG_wlan_retransmission
2400 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2401 "Got last ack, finished fragment message %p\n", fm);
2403 session_light->macendpoint->acks++;
2404 fm->session->last_activity = GNUNET_TIME_absolute_get ();
2405 session_light->macendpoint->last_activity = fm->session->last_activity;
2406 free_fragment_message (plugin, fm);
2407 check_fragment_queue (plugin);
2410 if (ret == GNUNET_NO)
2412 #if DEBUG_wlan_retransmission
2413 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2414 "Got ack for: %p\n", fm);
2416 session_light->macendpoint->acks++;
2419 if (ret == GNUNET_SYSERR)
2427 #if DEBUG_wlan_retransmission
2428 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2429 "WLAN fragment not in fragment list\n");
2437 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2438 "WLAN packet inside the WLAN helper packet has not the right type: %u size: %u\n",
2439 ntohs (hdr->type), ntohs (hdr->size));
2445 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2446 "Helper finished\n");
2453 macprinter (const u_int8_t * mac)
2455 static char macstr[20];
2457 GNUNET_snprintf (macstr, sizeof (macstr), "%X:%X:%X:%X:%X:%X", mac[0], mac[1],
2458 mac[2], mac[3], mac[4], mac[5]);
2463 * Function for the scheduler if a mac endpoint times out
2464 * @param cls pointer to the MacEndpoint
2465 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
2468 macendpoint_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2470 struct MacEndpoint *endpoint = cls;
2472 GNUNET_assert (endpoint != NULL);
2473 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2474 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2478 if (GNUNET_TIME_absolute_get_remaining
2479 (GNUNET_TIME_absolute_add
2480 (endpoint->last_activity, MACENDPOINT_TIMEOUT)).rel_value == 0)
2482 free_macendpoint (endpoint->plugin, endpoint);
2486 endpoint->timeout_task =
2487 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
2493 * function to create an macendpoint
2494 * @param plugin pointer to the plugin struct
2495 * @param addr pointer to the macaddress
2496 * @return returns a macendpoint
2498 static struct MacEndpoint *
2499 create_macendpoint (struct Plugin *plugin, const struct MacAddress *addr)
2501 struct MacEndpoint *newend = GNUNET_malloc (sizeof (struct MacEndpoint));
2503 newend->addr = *addr;
2504 newend->plugin = plugin;
2505 newend->addr = *addr;
2506 newend->fragment_messages_out_count = 0;
2508 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
2509 MESSAGES_IN_DEFRAG_QUEUE_PER_MAC,
2510 newend, &wlan_data_message_handler,
2512 newend->last_activity = GNUNET_TIME_absolute_get ();
2513 newend->timeout_task =
2514 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
2517 plugin->mac_count++;
2518 GNUNET_CONTAINER_DLL_insert_tail (plugin->mac_head, plugin->mac_tail, newend);
2520 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2521 "New Mac Endpoint %p: %s\n", newend,
2522 wlan_plugin_address_to_string (NULL, newend->addr.mac, 6));
2528 * Function used for to process the data from the suid process
2530 * @param cls the plugin handle
2531 * @param client client that send the data (not used)
2532 * @param hdr header of the GNUNET_MessageHeader
2535 wlan_process_helper (void *cls, void *client,
2536 const struct GNUNET_MessageHeader *hdr)
2538 struct Plugin *plugin = cls;
2539 struct ieee80211_frame *wlanIeeeHeader = NULL;
2540 struct Session_light *session_light = NULL;
2541 struct Radiotap_rx *rxinfo;
2542 const struct GNUNET_MessageHeader *temp_hdr = NULL;
2547 switch (ntohs (hdr->type))
2549 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA:
2551 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2552 "Func wlan_process_helper got GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA size: %u\n",
2556 //call wlan_process_helper with the message inside, later with wlan: analyze signal
2557 if (ntohs (hdr->size) <
2558 sizeof (struct ieee80211_frame) + sizeof (struct GNUNET_MessageHeader) +
2559 sizeof (struct Radiotap_rx))
2562 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2563 "Size of packet is too small; size: %u min size: %u\n",
2565 sizeof (struct ieee80211_frame) +
2566 sizeof (struct GNUNET_MessageHeader));
2569 /* FIXME: restart SUID process */
2572 rxinfo = (struct Radiotap_rx *) &hdr[1];
2573 wlanIeeeHeader = (struct ieee80211_frame *) &rxinfo[1];
2575 //process only if it is an broadcast or for this computer both with the gnunet bssid
2579 (&(wlanIeeeHeader->i_addr3), &mac_bssid,
2580 sizeof (struct MacAddress)) == 0)
2582 //check for broadcast or mac
2584 (&(wlanIeeeHeader->i_addr1), &bc_all_mac,
2585 sizeof (struct MacAddress) == 0) ||
2586 memcmp (&(wlanIeeeHeader->i_addr1), &(plugin->mac_address),
2587 sizeof (struct MacAddress)) == 0)
2590 // process the inner data
2594 ntohs (hdr->size) - sizeof (struct ieee80211_frame) -
2595 sizeof (struct GNUNET_MessageHeader) - sizeof (struct Radiotap_rx);
2597 session_light = GNUNET_malloc (sizeof (struct Session_light));
2598 memcpy (&session_light->addr, &(wlanIeeeHeader->i_addr2),
2599 sizeof (struct MacAddress));
2600 //session_light->session = search_session(plugin,session_light->addr);
2603 while (pos < datasize)
2605 temp_hdr = (struct GNUNET_MessageHeader *) &wlanIeeeHeader[1] + pos;
2607 wlan_data_helper (plugin, session_light, temp_hdr, rxinfo);
2608 pos += ntohs (temp_hdr->size);
2613 GNUNET_free (session_light);
2618 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2619 "Func wlan_process_helper got wrong MAC: %s\n",
2620 macprinter (wlanIeeeHeader->i_addr1));
2627 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2628 "Func wlan_process_helper got wrong BSSID: %s\n",
2629 macprinter (wlanIeeeHeader->i_addr2));
2633 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
2634 //TODO more control messages
2635 //TODO use struct wlan_helper_control
2636 if (ntohs (hdr->size) != sizeof (struct Wlan_Helper_Control_Message))
2639 /* FIXME: restart SUID process */
2642 memcpy (&plugin->mac_address, &hdr[1], sizeof (struct MacAddress));
2644 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2645 "Received WLAN_HELPER_CONTROL message with transport of address %s\n",
2646 wlan_plugin_address_to_string (cls, &plugin->mac_address,
2650 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2651 &plugin->mac_address,
2652 sizeof (struct MacAddress));
2661 * We have been notified that wlan-helper has written something to stdout.
2662 * Handle the output, then reschedule this function to be called again once
2663 * more is available.
2665 * @param cls the plugin handle
2666 * @param tc the scheduling context
2669 wlan_plugin_helper_read (void *cls,
2670 const struct GNUNET_SCHEDULER_TaskContext *tc)
2672 struct Plugin *plugin = cls;
2674 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2676 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2679 char mybuf[WLAN_MTU + sizeof (struct GNUNET_MessageHeader)];
2683 GNUNET_DISK_file_read (plugin->server_stdout_handle, mybuf,
2688 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2690 ("Finished reading from wlan-helper stdout with code: %d\n"),
2695 GNUNET_SERVER_mst_receive (plugin->suid_tokenizer, NULL, mybuf, bytes,
2696 GNUNET_NO, GNUNET_NO);
2698 GNUNET_assert (plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2699 plugin->server_read_task =
2700 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2701 plugin->server_stdout_handle,
2702 &wlan_plugin_helper_read, plugin);
2706 * Start the gnunet-wlan-helper process.
2708 * @param plugin the transport plugin
2709 * @param testmode should we use the dummy driver for testing?
2710 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
2713 wlan_transport_start_wlan_helper (struct Plugin *plugin, int testmode)
2715 const char *filenamehw = "gnunet-transport-wlan-helper";
2716 const char *filenameloopback = "gnunet-transport-wlan-helper-dummy";
2718 plugin->server_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
2719 if (plugin->server_stdout == NULL)
2720 return GNUNET_SYSERR;
2722 plugin->server_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);
2723 if (plugin->server_stdin == NULL)
2724 return GNUNET_SYSERR;
2726 /* Start the server process */
2732 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2733 "Starting gnunet-wlan-helper process cmd: %s %s %i\n",
2734 filenamehw, plugin->interface, testmode);
2737 plugin->server_proc =
2738 GNUNET_OS_start_process (plugin->server_stdin, plugin->server_stdout,
2739 filenamehw, filenamehw, plugin->interface,
2742 else if (testmode == 1)
2746 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2747 "Starting gnunet-wlan-helper loopback 1 process cmd: %s %s %i\n",
2748 filenameloopback, plugin->interface, testmode);
2751 plugin->server_proc =
2752 GNUNET_OS_start_process (plugin->server_stdin, plugin->server_stdout,
2753 filenameloopback, filenameloopback, "1", NULL);
2755 else if (testmode == 2)
2758 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2759 "Starting gnunet-wlan-helper loopback 2 process cmd: %s %s %i\n",
2760 filenameloopback, plugin->interface, testmode);
2762 plugin->server_proc =
2763 GNUNET_OS_start_process (plugin->server_stdin, plugin->server_stdout,
2764 filenameloopback, filenameloopback, "2", NULL);
2766 if (plugin->server_proc == NULL)
2769 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2770 "Failed to start gnunet-wlan-helper process\n");
2772 return GNUNET_SYSERR;
2775 /* Close the write end of the read pipe */
2776 GNUNET_DISK_pipe_close_end (plugin->server_stdout,
2777 GNUNET_DISK_PIPE_END_WRITE);
2779 /* Close the read end of the write pipe */
2780 GNUNET_DISK_pipe_close_end (plugin->server_stdin, GNUNET_DISK_PIPE_END_READ);
2782 plugin->server_stdout_handle =
2783 GNUNET_DISK_pipe_handle (plugin->server_stdout,
2784 GNUNET_DISK_PIPE_END_READ);
2785 plugin->server_stdin_handle =
2786 GNUNET_DISK_pipe_handle (plugin->server_stdin,
2787 GNUNET_DISK_PIPE_END_WRITE);
2789 GNUNET_assert (plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2792 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2793 "Adding server_read_task for the wlan-helper\n");
2796 plugin->server_read_task =
2797 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2798 plugin->server_stdout_handle,
2799 &wlan_plugin_helper_read, plugin);
2805 * Exit point from the plugin.
2806 * @param cls pointer to the api struct
2811 libgnunet_plugin_transport_wlan_done (void *cls)
2813 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2814 struct Plugin *plugin = api->cls;
2815 struct MacEndpoint *endpoint = plugin->mac_head;
2816 struct MacEndpoint *endpoint_next;
2819 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2820 "libgnunet_plugin_transport_wlan_done started\n");
2823 GNUNET_DISK_pipe_close (plugin->server_stdout);
2824 GNUNET_DISK_pipe_close (plugin->server_stdin);
2825 GNUNET_OS_process_kill (plugin->server_proc, 9);
2826 GNUNET_OS_process_close (plugin->server_proc);
2828 GNUNET_assert (cls != NULL);
2830 while (endpoint != NULL)
2832 endpoint_next = endpoint->next;
2833 free_macendpoint (plugin, endpoint);
2834 endpoint = endpoint_next;
2837 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
2839 GNUNET_SCHEDULER_cancel (plugin->server_write_delay_task);
2840 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
2842 if (plugin->server_write_task != GNUNET_SCHEDULER_NO_TASK)
2844 GNUNET_SCHEDULER_cancel (plugin->server_write_task);
2845 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
2847 if (plugin->server_read_task != GNUNET_SCHEDULER_NO_TASK)
2849 GNUNET_SCHEDULER_cancel (plugin->server_read_task);
2850 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2853 if (plugin->suid_tokenizer != NULL)
2854 GNUNET_SERVER_mst_destroy (plugin->suid_tokenizer);
2856 if (plugin->data_tokenizer != NULL)
2857 GNUNET_SERVER_mst_destroy (plugin->data_tokenizer);
2859 GNUNET_free_non_null (plugin->interface);
2860 GNUNET_free (plugin);
2866 * Entry point for the plugin.
2868 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2869 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2872 libgnunet_plugin_transport_wlan_init (void *cls)
2874 //struct GNUNET_SERVICE_Context *service;
2875 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2876 struct GNUNET_TRANSPORT_PluginFunctions *api;
2877 struct Plugin *plugin;
2878 static unsigned long long testmode = 0;
2880 GNUNET_assert (cls != NULL);
2882 plugin = GNUNET_malloc (sizeof (struct Plugin));
2884 plugin->pendingsessions = 0;
2885 plugin->mac_count = 0;
2886 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
2887 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2888 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
2889 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
2890 GNUNET_BANDWIDTH_value_init (100 * 1024 *
2893 plugin->suid_tokenizer =
2894 GNUNET_SERVER_mst_create (&wlan_process_helper, plugin);
2896 plugin->data_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
2898 //plugin->sessions = GNUNET_malloc (sizeof (struct Sessionqueue));
2899 //plugin->pending_Sessions_head = GNUNET_malloc (sizeof (struct Sessionqueue));
2901 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2903 api->send = &wlan_plugin_send;
2904 api->disconnect = &wlan_plugin_disconnect;
2905 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2906 api->check_address = &wlan_plugin_address_suggested;
2907 api->address_to_string = &wlan_plugin_address_to_string;
2910 if (GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "TESTMODE"))
2912 if (GNUNET_SYSERR ==
2913 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-wlan",
2914 "TESTMODE", &testmode))
2915 testmode = 0; //default value
2918 if (GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "INTERFACE"))
2920 if (GNUNET_CONFIGURATION_get_value_string
2921 (env->cfg, "transport-wlan", "INTERFACE",
2922 &(plugin->interface)) != GNUNET_YES)
2924 libgnunet_plugin_transport_wlan_done (api);
2930 set_next_beacon_time (plugin);
2932 wlan_transport_start_wlan_helper (plugin, testmode);
2935 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2936 "wlan init finished\n");
2942 /* end of plugin_transport_wlan.c */