-less malloc more stack
[oweals/gnunet.git] / src / transport / plugin_transport_wlan.c
1 /*
2   This file is part of GNUnet
3   (C) 2010, 2011, 2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file transport/plugin_transport_wlan.c
23  * @brief transport plugin for wlan
24  * @author David Brodski
25  * @author Christian Grothoff
26  */
27
28 //TODO split rx and tx structures for better handling
29
30 #include "platform.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_util_lib.h"
34 #include "gnunet_statistics_service.h"
35 #include "gnunet_transport_service.h"
36 #include "gnunet_transport_plugin.h"
37 #include "plugin_transport_wlan.h"
38 #include "gnunet_common.h"
39 #include "gnunet_crypto_lib.h"
40 #include "gnunet_fragmentation_lib.h"
41 #include "gnunet_constants.h"
42
43 /**
44  * DEBUG switch
45  */
46 #define DEBUG_WLAN GNUNET_EXTRA_LOGGING
47
48
49 #define PROTOCOL_PREFIX "wlan"
50
51 #define PLUGIN_LOG_NAME "wlan-plugin"
52
53 /**
54  * Max size of packet
55  */
56 #define WLAN_MTU 1430
57
58 /**
59  * time out of a session
60  */
61 #define SESSION_TIMEOUT GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT
62
63 /**
64  * time out of a mac endpoint
65  */
66 #define MACENDPOINT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 2)
67
68 /**
69  * scaling factor for hello beacon
70  */
71 #define HELLO_BEACON_SCALING_FACTOR 30
72
73 /**
74  * scaling factor for restarting the helper
75  */
76 #define HELPER_RESTART_SCALING_FACTOR 2
77
78 /**
79  * max size of fragment queue
80  */
81 #define FRAGMENT_QUEUE_SIZE 10
82 /**
83  * max messages in fragment queue per session/client
84  */
85 #define FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION 1
86
87 /**
88  * max messages in fragment queue per MAC
89  */
90 #define FRAGMENT_QUEUE_MESSAGES_OUT_PER_MACENDPOINT 1
91
92 /**
93  * max messages in in queue
94  */
95 #define MESSAGES_IN_QUEUE_SIZE 10
96
97 /**
98  * max messages in in queue per session/client
99  */
100 #define MESSAGES_IN_DEFRAG_QUEUE_PER_MAC 1
101
102 /**
103  * Link layer control fields for better compatibility
104  * (i.e. GNUnet over WLAN is not IP-over-WLAN).
105  */
106 #define WLAN_LLC_DSAP_FIELD 0x1f
107 #define WLAN_LLC_SSAP_FIELD 0x1f
108
109
110
111 /**
112  * Encapsulation of all of the state of the plugin.
113  */
114 struct Plugin
115 {
116   /**
117    * Our environment.
118    */
119   struct GNUNET_TRANSPORT_PluginEnvironment *env;
120
121   /**
122    * List of open connections. head
123    */
124   struct MacEndpoint *mac_head;
125
126   /**
127    * List of open connections. tail
128    */
129   struct MacEndpoint *mac_tail;
130
131   /**
132    * Number of connections
133    */
134   unsigned int mac_count;
135
136   /**
137    * encapsulation of data from the local wlan helper program
138    */
139   struct GNUNET_SERVER_MessageStreamTokenizer *suid_tokenizer;
140
141   /**
142    * encapsulation of packets received from the wlan helper
143    */
144   struct GNUNET_SERVER_MessageStreamTokenizer *data_tokenizer;
145
146   /**
147    * stdout pipe handle for the gnunet-helper-transport-wlan process
148    */
149   struct GNUNET_DISK_PipeHandle *server_stdout;
150
151   /**
152    * stdout file handle for the gnunet-helper-transport-wlan process
153    */
154   const struct GNUNET_DISK_FileHandle *server_stdout_handle;
155
156   /**
157    * stdin pipe handle for the gnunet-helper-transport-wlan process
158    */
159   struct GNUNET_DISK_PipeHandle *server_stdin;
160
161   /**
162    * stdin file handle for the gnunet-helper-transport-wlan process
163    */
164   const struct GNUNET_DISK_FileHandle *server_stdin_handle;
165
166   /**
167    * ID of the gnunet-wlan-server std read task
168    */
169   GNUNET_SCHEDULER_TaskIdentifier server_read_task;
170
171   /**
172    * ID of the gnunet-wlan-server std read task
173    */
174   GNUNET_SCHEDULER_TaskIdentifier server_write_task;
175
176   /**
177    * ID of the delay task for writing
178    */
179   GNUNET_SCHEDULER_TaskIdentifier server_write_delay_task;
180
181   /**
182    * The process id of the wlan process
183    */
184   struct GNUNET_OS_Process *server_proc;
185
186   /**
187    * The interface of the wlan card given to us by the user.
188    */
189   char *interface;
190
191   /**
192    * Mode of operation for the helper, 0 = normal, 1 = first loopback, 2 = second loopback
193    */
194   long long unsigned int testmode;
195
196   /**
197    * The mac_address of the wlan card given to us by the helper.
198    */
199   struct GNUNET_TRANSPORT_WLAN_MacAddress mac_address;
200
201   /**
202    * Sessions currently pending for transmission
203    * to a peer, if any.
204    */
205   struct Sessionqueue *pending_Sessions_head;
206
207   /**
208    * Sessions currently pending for transmission
209    * to a peer (tail), if any.
210    */
211   struct Sessionqueue *pending_Sessions_tail;
212
213   /**
214    * number of pending sessions
215    */
216   unsigned int pendingsessions;
217
218   /**
219    * Messages in the sending queues
220    */
221   int pending_Fragment_Messages;
222
223   /**
224    * messages ready for send, head
225    */
226   struct FragmentMessage_queue *sending_messages_head;
227
228   /**
229    * messages ready for send, tail
230    */
231   struct FragmentMessage_queue *sending_messages_tail;
232
233   /**
234    * time of the next "hello-beacon"
235    */
236   struct GNUNET_TIME_Absolute beacon_time;
237
238   /**
239    * queue to send acks for received fragments (head)
240    */
241   struct AckSendQueue *ack_send_queue_head;
242
243   /**
244    * queue to send acks for received fragments (tail)
245    */
246   struct AckSendQueue *ack_send_queue_tail;
247
248   /**
249    * Tracker for bandwidth limit
250    */
251   struct GNUNET_BANDWIDTH_Tracker tracker;
252
253   /**
254    * saves the current state of the helper process
255    */
256   int helper_is_running;
257 };
258
259 /**
260  * Struct to store data if file write did not accept the whole packet
261  */
262 struct Finish_send
263 {
264   /**
265    * pointer to the global plugin struct
266    */
267   struct Plugin *plugin;
268
269   /**
270    * head of the next part to send to the helper
271    */
272   const char *head_of_next_write;
273
274   /**
275    * Start of the message to send, needed for free
276    */
277   struct GNUNET_MessageHeader *msgstart;
278
279   /**
280    * rest size to send
281    */
282   ssize_t size;
283 };
284
285 /**
286  * Queue of sessions, for the general session queue and the pending session queue
287  */
288 //TODO DOXYGEN
289 struct Sessionqueue
290 {
291   struct Sessionqueue *next;
292   struct Sessionqueue *prev;
293   struct Session *content;
294 #if !HAVE_UNALIGNED_64_ACCESS
295   void *dummy;                  /* for alignment, see #1909 */
296 #endif
297 };
298
299
300 /**
301  * Queue of fragmented messages, for the sending queue of the plugin
302  */
303 //TODO DOXYGEN
304 struct FragmentMessage_queue
305 {
306   struct FragmentMessage_queue *next;
307   struct FragmentMessage_queue *prev;
308   struct FragmentMessage *content;
309 };
310
311
312 /**
313  * Queue for the fragments received
314  */
315 //TODO DOXYGEN
316 struct Receive_Fragment_Queue
317 {
318   struct Receive_Fragment_Queue *next;
319   struct Receive_Fragment_Queue *prev;
320
321   uint16_t num;
322   // const char *msg;
323   //   uint16_t size;
324   struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rx_msg;
325 };
326
327
328 //TODO DOXYGEN
329 struct MacEndpoint_id_fragment_triple
330 {
331   struct MacEndpoint *endpoint;
332   uint32_t message_id;
333   struct FragmentMessage *fm;
334 };
335
336 //TODO DOXYGEN
337 struct Plugin_Session_pair
338 {
339   struct Plugin *plugin;
340   struct Session *session;
341 };
342
343
344 GNUNET_NETWORK_STRUCT_BEGIN
345
346 /**
347  * Header for messages which need fragmentation
348  */
349 struct WlanHeader
350 {
351
352   /**
353    * Message type is GNUNET_MESSAGE_TYPE_WLAN_DATA.
354    */
355   struct GNUNET_MessageHeader header;
356
357   /**
358    * checksum/error correction
359    */
360   uint32_t crc GNUNET_PACKED;
361
362   /**
363    * To whom are we talking to (set to our identity
364    * if we are still waiting for the welcome message)
365    */
366   struct GNUNET_PeerIdentity target;
367
368   /**
369    *  Where the packet came from
370    */
371   struct GNUNET_PeerIdentity source;
372
373 // followed by payload
374
375 };
376 GNUNET_NETWORK_STRUCT_END
377
378 /**
379  * Information kept for each message that is yet to
380  * be transmitted.
381  */
382 struct PendingMessage
383 {
384   /**
385    * dll next
386    */
387   struct PendingMessage *next;
388
389   /**
390    * dll prev
391    */
392   struct PendingMessage *prev;
393
394   /**
395    * The pending message
396    */
397   struct WlanHeader *msg;
398
399   /**
400    * Size of the message
401    */
402   size_t message_size;
403
404   /**
405    * Continuation function to call once the message
406    * has been sent.  Can be NULL if there is no
407    * continuation to call.
408    */
409   GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
410
411   /**
412    * Cls for transmit_cont
413    */
414   void *transmit_cont_cls;
415
416   /**
417    * Timeout value for the pending message.
418    */
419   struct GNUNET_TIME_Absolute timeout;
420
421 };
422
423
424 /**
425  * Queue for acks to send for fragments recived
426  */
427 struct AckSendQueue
428 {
429
430   /**
431    * next ack in the ack send queue
432    */
433   struct AckSendQueue *next;
434
435   /**
436    * previous ack in the ack send queue
437    */
438   struct AckSendQueue *prev;
439
440   /**
441    * pointer to the session this ack belongs to
442    */
443   struct MacEndpoint *endpoint;
444
445   /**
446    * ID of message, to distinguish between the messages, picked randomly.
447    */
448   uint32_t message_id;
449
450   /**
451    * pointer to the radiotap header with the ACK Message.
452    */
453   const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
454 };
455
456
457 /**
458  * Session handle for connections.
459  */
460 struct Session
461 {
462
463   /**
464    * API requirement.
465    */
466   struct SessionHeader header;
467
468   /**
469    * Message currently pending for transmission
470    * to this peer, if any. head
471    */
472   struct PendingMessage *pending_message_head;
473
474   /**
475    * Message currently pending for transmission
476    * to this peer, if any. tail
477    */
478   struct PendingMessage *pending_message_tail;
479
480   /**
481    * To whom are we talking to (set to our identity
482    * if we are still waiting for the welcome message)
483    */
484   struct GNUNET_PeerIdentity target;
485
486   /**
487    * Address of the other peer (either based on our 'connect'
488    * call or on our 'accept' call).
489    */
490   void *connect_addr;
491
492   /**
493    * Last activity on this connection.  Used to select preferred
494    * connection and timeout
495    */
496   struct GNUNET_TIME_Absolute last_activity;
497
498   /**
499    * Timeout task.
500    */
501   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
502
503   /**
504    * peer connection
505    */
506   struct MacEndpoint *mac;
507
508   /**
509    * count of messages in the fragment out queue for this session
510    */
511
512   int fragment_messages_out_count;
513
514 };
515
516 /**
517  * Struct to represent one network card connection
518  */
519 struct MacEndpoint
520 {
521   /**
522    * Pointer to the global plugin struct.
523    */
524   struct Plugin *plugin;
525
526   /**
527    * Struct to hold the session reachable over this mac; head
528    */
529   struct Sessionqueue *sessions_head;
530
531   /**
532    * Struct to hold the session reachable over this mac; tail
533    */
534   struct Sessionqueue *sessions_tail;
535
536   /**
537    * Messages currently sending
538    * to a peer, if any.
539    */
540   struct FragmentMessage *sending_messages_head;
541
542   /**
543    * Messages currently sending
544    * to a peer (tail), if any.
545    */
546   struct FragmentMessage *sending_messages_tail;
547
548   /**
549    * dll next
550    */
551   struct MacEndpoint *next;
552
553   /**
554    * dll prev
555    */
556   struct MacEndpoint *prev;
557
558   /**
559    * peer mac address
560    */
561   struct GNUNET_TRANSPORT_WLAN_MacAddress addr;
562
563   /**
564    * Defrag context for this mac endpoint
565    */
566   struct GNUNET_DEFRAGMENT_Context *defrag;
567
568   /**
569    * count of messages in the fragment out queue for this mac endpoint
570    */
571   int fragment_messages_out_count;
572
573   //TODO DOXYGEN
574   uint8_t rate;
575   uint16_t tx_power;
576   uint8_t antenna;
577
578   /**
579    * Duplicates received
580    */
581   int dups;
582
583   /**
584    * Fragments received
585    */
586   int fragc;
587
588   /**
589    * Acks received
590    */
591   int acks;
592
593   /**
594    * Last activity on this endpoint.  Used to select preferred
595    * connection.
596    */
597   struct GNUNET_TIME_Absolute last_activity;
598
599   /**
600    * Timeout task.
601    */
602   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
603 };
604
605 /**
606  * Struct for Messages in the fragment queue
607  */
608 struct FragmentMessage
609 {
610
611   /**
612    * Session this message belongs to
613    */
614   struct Session *session;
615
616   /**
617    * This is a doubly-linked list.
618    */
619   struct FragmentMessage *next;
620
621   /**
622    * This is a doubly-linked list.
623    */
624   struct FragmentMessage *prev;
625
626   /**
627    * Fragmentation context
628    */
629   struct GNUNET_FRAGMENT_Context *fragcontext;
630
631   /**
632    * Timeout value for the message.
633    */
634   struct GNUNET_TIME_Absolute timeout;
635
636   /**
637    * Timeout task.
638    */
639   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
640
641   /**
642    * pointer to the radiotap header
643    */
644   struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
645 };
646
647
648 static void
649 do_transmit (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
650
651
652 static void
653 free_session (struct Plugin *plugin, struct Sessionqueue *queue,
654               int do_free_macendpoint);
655
656
657 static struct MacEndpoint *
658 create_macendpoint (struct Plugin *plugin, const struct GNUNET_TRANSPORT_WLAN_MacAddress *addr);
659
660
661 static void
662 finish_sending (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
663
664
665 /**
666  * Generates a nice hexdump of a memory area.
667  *
668  * \param  mem     pointer to memory to dump
669  * \param  length  how many bytes to dump
670  */
671 static void
672 hexdump (const void *mem, unsigned length)
673 {
674   char line[80];
675   char *src = (char *) mem;
676
677   printf ("dumping %u bytes from %p\r\n"
678           "       0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F    0123456789ABCDEF\r\n",
679           length, src);
680   unsigned i;
681   int j;
682
683   for (i = 0; i < length; i += 16, src += 16)
684   {
685     char *t = line;
686
687     t += sprintf (t, "%04x:  ", i);
688     for (j = 0; j < 16; j++)
689     {
690       if (i + j < length)
691         t += sprintf (t, "%02X", src[j] & 0xff);
692       else
693         t += sprintf (t, "  ");
694
695       t += sprintf (t, (j % 2) ? " " : "-");
696     }
697
698     t += sprintf (t, "  ");
699     for (j = 0; j < 16; j++)
700     {
701       if (i + j < length)
702       {
703         if (isprint ((unsigned char) src[j]))
704           t += sprintf (t, "%c", src[j]);
705         else
706           t += sprintf (t, ".");
707       }
708       else
709       {
710         t += sprintf (t, " ");
711       }
712     }
713
714     t += sprintf (t, "\r\n");
715     printf ("%s", line);
716   }
717 }
718
719
720 /**
721  * Function to find a MacEndpoint with a specific mac addr
722  * @param plugin pointer to the plugin struct
723  * @param addr pointer to the mac address
724  * @param create_new GNUNET_YES if a new end point should be created
725  * @return
726  */
727 static struct MacEndpoint *
728 get_macendpoint (struct Plugin *plugin, const struct GNUNET_TRANSPORT_WLAN_MacAddress *addr,
729                  int create_new)
730 {
731   struct MacEndpoint *queue = plugin->mac_head;
732
733   while (queue != NULL)
734   {
735     //GNUNET_assert (queue->sessions_head != NULL);
736     if (memcmp (addr, &queue->addr, sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0)
737       return queue;             /* session found */
738     queue = queue->next;
739   }
740
741   if (create_new == GNUNET_YES)
742   {
743     return create_macendpoint (plugin, addr);
744   }
745   else
746   {
747     return NULL;
748   }
749 }
750
751
752 /**
753  * search for a session with the macendpoint and peer id
754  *
755  * @param plugin pointer to the plugin struct
756  * @param endpoint pointer to the mac endpoint of the peer
757  * @param peer pointer to the peerid
758  * @return returns the session
759  */
760 static struct Session *
761 search_session (struct Plugin *plugin, const struct MacEndpoint *endpoint,
762                 const struct GNUNET_PeerIdentity *peer)
763 {
764   GNUNET_assert (endpoint != NULL);
765   struct Sessionqueue *queue = endpoint->sessions_head;
766
767   while (queue != NULL)
768   {
769     GNUNET_assert (queue->content != NULL);
770     if (memcmp
771         (peer, &queue->content->target,
772          sizeof (struct GNUNET_PeerIdentity)) == 0)
773       return queue->content;    /* session found */
774     queue = queue->next;
775   }
776   return NULL;
777 }
778
779 /**
780  * Function called for a quick conversion of the binary address to
781  * a numeric address.  Note that the caller must not free the
782  * address and that the next call to this function is allowed
783  * to override the address again.
784  *
785  * @param cls closure
786  * @param addr binary address
787  * @param addrlen length of the address
788  * @return string representing the same address
789  */
790 static const char *
791 wlan_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
792 {
793   static char ret[40];
794   const struct GNUNET_TRANSPORT_WLAN_MacAddress *mac;
795
796   if (addrlen != sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))
797   {
798     GNUNET_break (0);
799     return NULL;
800   }
801   mac = addr;
802   GNUNET_snprintf (ret, sizeof (ret), "%s Mac-Address %X:%X:%X:%X:%X:%X",
803                    PROTOCOL_PREFIX, mac->mac[0], mac->mac[1], mac->mac[2],
804                    mac->mac[3], mac->mac[4], mac->mac[5]);
805
806   return ret;
807 }
808
809 /**
810  * Function for the scheduler if a session times out
811  * @param cls pointer to the Sessionqueue
812  * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
813  */
814 static void
815 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
816 {
817   struct Sessionqueue *queue = cls;
818
819   GNUNET_assert (queue != NULL);
820   GNUNET_assert (queue->content != NULL);
821   queue->content->timeout_task = GNUNET_SCHEDULER_NO_TASK;
822   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
823   {
824     return;
825   }
826   if (GNUNET_TIME_absolute_get_remaining
827       (GNUNET_TIME_absolute_add
828        (queue->content->last_activity, SESSION_TIMEOUT)).rel_value == 0)
829   {
830
831     GNUNET_assert (queue->content->mac != NULL);
832     GNUNET_assert (queue->content->mac->plugin != NULL);
833     GNUNET_STATISTICS_update (queue->content->mac->plugin->env->stats,
834                               _("# wlan session timeouts"), 1, GNUNET_NO);
835     free_session (queue->content->mac->plugin, queue, GNUNET_YES);
836   }
837   else
838   {
839     queue->content->timeout_task =
840         GNUNET_SCHEDULER_add_delayed (SESSION_TIMEOUT, &session_timeout, queue);
841   }
842 }
843
844 /**
845  * create a new session
846  *
847  * @param plugin pointer to the plugin struct
848  * @param endpoint pointer to the mac endpoint of the peer
849  * @param peer peer identity to use for this session
850  * @return returns the session
851  */
852 static struct Session *
853 create_session (struct Plugin *plugin, struct MacEndpoint *endpoint,
854                 const struct GNUNET_PeerIdentity *peer)
855 {
856   GNUNET_assert (endpoint != NULL);
857   GNUNET_assert (plugin != NULL);
858   GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan session created"), 1,
859                             GNUNET_NO);
860   struct Sessionqueue *queue =
861       GNUNET_malloc (sizeof (struct Sessionqueue) + sizeof (struct Session));
862
863   GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
864                                     endpoint->sessions_tail, queue);
865
866   queue->content = (struct Session *) &queue[1];
867   queue->content->mac = endpoint;
868   queue->content->target = *peer;
869   queue->content->last_activity = GNUNET_TIME_absolute_get ();
870   queue->content->timeout_task =
871       GNUNET_SCHEDULER_add_delayed (SESSION_TIMEOUT, &session_timeout, queue);
872
873   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
874                    "New session %p with endpoint %p: %s\n", queue->content,
875                    endpoint, wlan_plugin_address_to_string (NULL,
876                                                             endpoint->addr.mac,
877                                                             6));
878   return queue->content;
879 }
880
881 /**
882  * Get session from address, create if no session exists
883  *
884  * @param plugin pointer to the plugin struct
885  * @param addr pointer to the mac address of the peer
886  * @param peer pointer to the peerid
887  * @return returns the session
888  */
889 static struct Session *
890 get_session (struct Plugin *plugin, const struct GNUNET_TRANSPORT_WLAN_MacAddress *addr,
891              const struct GNUNET_PeerIdentity *peer)
892 {
893   struct MacEndpoint *mac;
894
895   mac = get_macendpoint (plugin, addr, GNUNET_YES);
896   struct Session *session = search_session (plugin, mac, peer);
897
898   if (session != NULL)
899     return session;
900   return create_session (plugin, mac, peer);
901 }
902
903 /**
904  * Queue the session to send data
905  * checks if there is a message pending
906  * checks if this session is not allready in the queue
907  * @param plugin pointer to the plugin
908  * @param session pointer to the session to add
909  */
910 static void
911 queue_session (struct Plugin *plugin, struct Session *session)
912 {
913   struct Sessionqueue *queue = plugin->pending_Sessions_head;
914
915   if (session->pending_message_head != NULL)
916   {
917     while (queue != NULL)
918     {
919       // content is never NULL
920       GNUNET_assert (queue->content != NULL);
921       // is session already in queue?
922       if (session == queue->content)
923       {
924         return;
925       }
926       // try next
927       queue = queue->next;
928     }
929
930     // Session is not in the queue
931
932     queue = GNUNET_malloc (sizeof (struct Sessionqueue));
933     queue->content = session;
934
935     //insert at the tail
936     GNUNET_CONTAINER_DLL_insert_tail (plugin->pending_Sessions_head,
937                                       plugin->pending_Sessions_tail, queue);
938     plugin->pendingsessions++;
939     GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
940                            plugin->pendingsessions, GNUNET_NO);
941   }
942
943 }
944
945 /**
946  * Function to schedule the write task, executed after a delay
947  * @param cls pointer to the plugin struct
948  * @param tc GNUNET_SCHEDULER_TaskContext pointer
949  */
950 static void
951 delay_fragment_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
952 {
953   struct Plugin *plugin = cls;
954
955   plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
956
957   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
958     return;
959
960   // GNUNET_TIME_UNIT_FOREVER_REL is needed to clean up old msg
961   if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
962   {
963     plugin->server_write_task =
964         GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
965                                          plugin->server_stdin_handle,
966                                          &do_transmit, plugin);
967   }
968 }
969
970 /**
971  * Function to calculate the time of the next periodic "hello-beacon"
972  * @param plugin pointer to the plugin struct
973  */
974 static void
975 set_next_beacon_time (struct Plugin *const plugin)
976 {
977   //under 10 known peers: once a second
978   if (plugin->mac_count < 10)
979   {
980     plugin->beacon_time =
981         GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
982                                   GNUNET_TIME_relative_multiply
983                                   (GNUNET_TIME_UNIT_SECONDS,
984                                    HELLO_BEACON_SCALING_FACTOR));
985   }
986   //under 30 known peers: every 10 seconds
987   else if (plugin->mac_count < 30)
988   {
989     plugin->beacon_time =
990         GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
991                                   GNUNET_TIME_relative_multiply
992                                   (GNUNET_TIME_UNIT_SECONDS,
993                                    10 * HELLO_BEACON_SCALING_FACTOR));
994   }
995   //over 30 known peers: once a minute
996   else
997   {
998     plugin->beacon_time =
999         GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1000                                   GNUNET_TIME_relative_multiply
1001                                   (GNUNET_TIME_UNIT_MINUTES,
1002                                    HELLO_BEACON_SCALING_FACTOR));
1003   }
1004 }
1005
1006 /**
1007  * Function to set the timer for the next timeout of the fragment queue
1008  * @param plugin the handle to the plugin struct
1009  */
1010 static void
1011 set_next_send (struct Plugin *const plugin)
1012 {
1013   struct GNUNET_TIME_Relative next_send;
1014
1015   //abort if helper is not running
1016   if (plugin->helper_is_running == GNUNET_NO)
1017   {
1018     return;
1019   }
1020
1021   //cancel old task
1022   if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
1023   {
1024     GNUNET_SCHEDULER_cancel (plugin->server_write_delay_task);
1025     plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
1026   }
1027
1028   //check if some acks are in the queue
1029   if (plugin->ack_send_queue_head != NULL)
1030   {
1031     next_send = GNUNET_TIME_UNIT_ZERO;
1032   }
1033
1034   //check if there are some fragments in the queue
1035   else if (plugin->sending_messages_head != NULL)
1036   {
1037     next_send = GNUNET_TIME_UNIT_ZERO;
1038   }
1039   else
1040   {
1041     next_send = GNUNET_TIME_absolute_get_remaining (plugin->beacon_time);
1042   }
1043
1044   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1045                    "Next packet is send in: %u\n", next_send.rel_value);
1046   if (next_send.rel_value == GNUNET_TIME_UNIT_ZERO.rel_value)
1047   {
1048     if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
1049     {
1050       plugin->server_write_task =
1051           GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1052                                            plugin->server_stdin_handle,
1053                                            &do_transmit, plugin);
1054     }
1055   }
1056   else
1057   {
1058     if (plugin->server_write_delay_task == GNUNET_SCHEDULER_NO_TASK)
1059     {
1060       plugin->server_write_delay_task =
1061           GNUNET_SCHEDULER_add_delayed (next_send, &delay_fragment_task,
1062                                         plugin);
1063     }
1064   }
1065 }
1066
1067 /**
1068  * Function to get the next queued Session, removes the session from the queue
1069  * @param plugin pointer to the plugin struct
1070  * @return pointer to the session found, returns NULL if there is now session in the queue
1071  */
1072 static struct Session *
1073 get_next_queue_session (struct Plugin *plugin)
1074 {
1075   struct Session *session;
1076   struct Sessionqueue *sessionqueue;
1077   struct Sessionqueue *sessionqueue_alt;
1078   struct PendingMessage *pm;
1079
1080   sessionqueue = plugin->pending_Sessions_head;
1081
1082   while (sessionqueue != NULL)
1083   {
1084     session = sessionqueue->content;
1085
1086     GNUNET_assert (session != NULL);
1087     pm = session->pending_message_head;
1088
1089     if (pm == NULL)
1090     {
1091       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1092                        "pending message is empty, should not happen. session %p\n",
1093                        session);
1094       sessionqueue_alt = sessionqueue;
1095       sessionqueue = sessionqueue->next;
1096       plugin->pendingsessions--;
1097       GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
1098                              plugin->pendingsessions, GNUNET_NO);
1099       GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1100                                    plugin->pending_Sessions_tail,
1101                                    sessionqueue_alt);
1102
1103       GNUNET_free (sessionqueue_alt);
1104       continue;
1105
1106     }
1107
1108     //check for message timeout
1109     if (GNUNET_TIME_absolute_get_remaining (pm->timeout).rel_value > 0)
1110     {
1111       //check if session has no message in the fragment queue
1112       if ((session->mac->fragment_messages_out_count <
1113            FRAGMENT_QUEUE_MESSAGES_OUT_PER_MACENDPOINT) &&
1114           (session->fragment_messages_out_count <
1115            FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION))
1116       {
1117         plugin->pendingsessions--;
1118         GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
1119                                plugin->pendingsessions, GNUNET_NO);
1120         GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1121                                      plugin->pending_Sessions_tail,
1122                                      sessionqueue);
1123         GNUNET_free (sessionqueue);
1124
1125         return session;
1126       }
1127       else
1128       {
1129         sessionqueue = sessionqueue->next;
1130       }
1131     }
1132     else
1133     {
1134       GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
1135                                    session->pending_message_tail, pm);
1136
1137       //call the cont func that it did not work
1138       if (pm->transmit_cont != NULL)
1139         pm->transmit_cont (pm->transmit_cont_cls, &(session->target),
1140                            GNUNET_SYSERR);
1141       GNUNET_free (pm->msg);
1142       GNUNET_free (pm);
1143
1144       if (session->pending_message_head == NULL)
1145       {
1146         sessionqueue_alt = sessionqueue;
1147         sessionqueue = sessionqueue->next;
1148         plugin->pendingsessions--;
1149         GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
1150                                plugin->pendingsessions, GNUNET_NO);
1151         GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1152                                      plugin->pending_Sessions_tail,
1153                                      sessionqueue_alt);
1154
1155         GNUNET_free (sessionqueue_alt);
1156       }
1157     }
1158
1159   }
1160   return NULL;
1161 }
1162
1163 /**
1164  * frees the space of a message in the fragment queue (send queue)
1165  * @param plugin the plugin struct
1166  * @param fm message to free
1167  */
1168 static void
1169 free_fragment_message (struct Plugin *plugin, struct FragmentMessage *fm)
1170 {
1171   struct Session *session = fm->session;
1172   struct MacEndpoint *endpoint = session->mac;
1173   struct FragmentMessage_queue *fmq;
1174   struct FragmentMessage_queue *fmq_next;
1175
1176   fmq = plugin->sending_messages_head;
1177   while (fmq != NULL)
1178   {
1179     fmq_next = fmq->next;
1180     if (fmq->content == fm)
1181     {
1182       GNUNET_CONTAINER_DLL_remove (plugin->sending_messages_head,
1183                                    plugin->sending_messages_tail, fmq);
1184       GNUNET_free (fmq);
1185     }
1186     fmq = fmq_next;
1187   }
1188
1189   session->mac->fragment_messages_out_count--;
1190   session->fragment_messages_out_count--;
1191   plugin->pending_Fragment_Messages--;
1192   GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending fragments"),
1193                          plugin->pending_Fragment_Messages, GNUNET_NO);
1194   GNUNET_CONTAINER_DLL_remove (endpoint->sending_messages_head,
1195                                endpoint->sending_messages_tail, fm);
1196   GNUNET_FRAGMENT_context_destroy (fm->fragcontext);
1197   if (fm->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1198   {
1199     GNUNET_SCHEDULER_cancel (fm->timeout_task);
1200     fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1201   }
1202   GNUNET_free (fm);
1203   queue_session (plugin, session);
1204   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1205                    "Free pending fragment messages %p, session %p\n", fm,
1206                    session);
1207 }
1208
1209
1210 /**
1211  * function to fill the radiotap header
1212  *
1213  * @param plugin pointer to the plugin struct
1214  * @param endpoint pointer to the endpoint
1215  * @param header pointer to the radiotap header
1216  * @param size total message size
1217  * @return GNUNET_YES at success
1218  */
1219 static int
1220 getRadiotapHeader (struct Plugin *plugin, struct MacEndpoint *endpoint,
1221                    struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *header,
1222                    uint16_t size)
1223 {
1224   header->header.type = ntohs (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1225   header->header.size = ntohs (size);
1226   if (endpoint != NULL)
1227   {
1228     header->rate = endpoint->rate;
1229     header->tx_power = endpoint->tx_power;
1230     header->antenna = endpoint->antenna;
1231   }
1232   else
1233   {
1234     header->rate = 255;
1235     header->tx_power = 0;
1236     header->antenna = 0;
1237   }
1238
1239   return GNUNET_YES;
1240 }
1241
1242
1243 /**
1244  * function to generate the wlan hardware header for one packet
1245  * @param Header address to write the header to
1246  * @param to_mac_addr address of the recipient
1247  * @param plugin pointer to the plugin struct
1248  * @param size size of the whole packet, needed to calculate the time to send the packet
1249  *        FIXME: 'size' is initialized inconsistently throughout the code (sometimes payload, sometimes everything)
1250  * @return GNUNET_YES if there was no error
1251  */
1252 static int
1253 getWlanHeader (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *Header,
1254                const struct GNUNET_TRANSPORT_WLAN_MacAddress *to_mac_addr, struct Plugin *plugin,
1255                unsigned int size)
1256 {
1257   const int rate = 11000000;
1258
1259   Header->frame_control = htons (IEEE80211_FC0_TYPE_DATA);
1260   Header->addr1 = *to_mac_addr;
1261   Header->addr2 = plugin->mac_address;
1262   Header->addr3 = mac_bssid_gnunet;
1263   Header->duration = GNUNET_htole16 ((size * 1000000) / rate + 290);
1264   Header->sequence_control = 0; // FIXME?
1265   Header->llc[0] = WLAN_LLC_DSAP_FIELD;
1266   Header->llc[1] = WLAN_LLC_SSAP_FIELD;
1267   Header->llc[2] = 0;  // FIXME?
1268   Header->llc[3] = 0;  // FIXME?
1269   return GNUNET_YES;
1270 }
1271
1272
1273 /**
1274  * function to add a fragment of a message to send
1275  * @param cls FragmentMessage this message belongs to
1276  * @param hdr pointer to the start of the message
1277  */
1278 static void
1279 add_message_for_send (void *cls, const struct GNUNET_MessageHeader *hdr)
1280 {
1281   struct FragmentMessage *fm = cls;
1282   struct FragmentMessage_queue *fmqueue;
1283   struct MacEndpoint *endpoint;
1284   struct Plugin *plugin;
1285   uint16_t size;
1286
1287   GNUNET_assert (cls != NULL);
1288   endpoint = fm->session->mac;
1289   plugin = endpoint->plugin;
1290
1291   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1292                    "Adding fragment of message %p to send, session %p, endpoint %p, type %u\n",
1293                    fm, fm->session, endpoint, hdr->type);
1294   size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + ntohs (hdr->size);
1295   fm->radioHeader = GNUNET_malloc (size);
1296   getRadiotapHeader (plugin, fm->session->mac, fm->radioHeader, size);
1297   getWlanHeader (&fm->radioHeader->frame, &(fm->session->mac->addr), plugin,
1298                  size);
1299   memcpy (&fm->radioHeader[1], hdr, ntohs (hdr->size));
1300   // FIXME: yucky allocation structure!
1301   fmqueue = GNUNET_malloc (sizeof (struct FragmentMessage_queue));
1302   fmqueue->content = fm;
1303   GNUNET_CONTAINER_DLL_insert_tail (plugin->sending_messages_head,
1304                                     plugin->sending_messages_tail, fmqueue);
1305   set_next_send (plugin);
1306 }
1307
1308
1309 /**
1310  * We have been notified that gnunet-helper-transport-wlan has written something to stdout.
1311  * Handle the output, then reschedule this function to be called again once
1312  * more is available.
1313  *
1314  * @param cls the plugin handle
1315  * @param tc the scheduling context
1316  */
1317 static void
1318 wlan_plugin_helper_read (void *cls,
1319                          const struct GNUNET_SCHEDULER_TaskContext *tc)
1320 {
1321   struct Plugin *plugin = cls;
1322
1323   plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
1324
1325   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1326     return;
1327
1328   char mybuf[WLAN_MTU + sizeof (struct GNUNET_MessageHeader)];
1329   ssize_t bytes;
1330
1331   bytes =
1332       GNUNET_DISK_file_read (plugin->server_stdout_handle, mybuf,
1333                              sizeof (mybuf));
1334   if (bytes <= 0)
1335   {
1336     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1337                      _
1338                      ("Finished reading from gnunet-helper-transport-wlan stdout with code: %d\n"),
1339                      bytes);
1340     return;
1341   }
1342   GNUNET_SERVER_mst_receive (plugin->suid_tokenizer, NULL, mybuf, bytes,
1343                              GNUNET_NO, GNUNET_NO);
1344
1345   GNUNET_assert (plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
1346   plugin->server_read_task =
1347       GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1348                                       plugin->server_stdout_handle,
1349                                       &wlan_plugin_helper_read, plugin);
1350 }
1351
1352 /**
1353  * Start the gnunet-helper-transport-wlan process.
1354  *
1355  * @param plugin the transport plugin
1356  * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
1357  */
1358 static int
1359 wlan_transport_start_wlan_helper (struct Plugin *plugin)
1360 {
1361   const char *filenamehw = "gnunet-helper-transport-wlan";
1362   const char *filenameloopback = "gnunet-helper-transport-wlan-dummy";
1363   char *absolute_filename = NULL;
1364
1365   if (plugin->helper_is_running == GNUNET_YES)
1366   {
1367     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1368                      "wlan_transport_start_wlan_helper not needed, helper already running!");
1369     return GNUNET_YES;
1370   }
1371
1372   plugin->server_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
1373   if (plugin->server_stdout == NULL)
1374     return GNUNET_SYSERR;
1375
1376   plugin->server_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
1377   if (plugin->server_stdin == NULL)
1378     return GNUNET_SYSERR;
1379
1380   if ((plugin->testmode == 1) || (plugin->testmode == 2))
1381   {
1382     if (GNUNET_OS_check_helper_binary (filenameloopback) == GNUNET_YES)
1383     {
1384       absolute_filename = GNUNET_strdup (filenameloopback);
1385     }
1386     else
1387     {
1388       char cwd[FILENAME_MAX];
1389
1390       GNUNET_assert (getcwd (cwd, sizeof (cwd)) != NULL);
1391
1392       GNUNET_asprintf (&absolute_filename, "%s%s%s", cwd, DIR_SEPARATOR_STR,
1393                        filenameloopback);
1394
1395       if (GNUNET_DISK_file_test (filenameloopback) != GNUNET_YES)
1396       {
1397         GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1398                          "Helper `%s' not found! %i\n", absolute_filename);
1399         GNUNET_break (0);
1400       }
1401     }
1402   }
1403
1404   /* Start the server process */
1405
1406   if (plugin->testmode == 0)
1407   {
1408
1409     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1410                      "Starting gnunet-helper-transport-wlan process cmd: %s %s %i\n",
1411                      filenamehw, plugin->interface, plugin->testmode);
1412     if (GNUNET_OS_check_helper_binary (filenamehw) == GNUNET_YES)
1413     {
1414       plugin->server_proc =
1415           GNUNET_OS_start_process (GNUNET_NO, plugin->server_stdin, plugin->server_stdout,
1416                                    filenamehw, filenamehw, plugin->interface,
1417                                    NULL);
1418     }
1419     else if (GNUNET_OS_check_helper_binary (filenamehw) == GNUNET_NO)
1420     {
1421       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1422                        "gnunet-helper-transport-wlan is not suid, please change it or look at the doku\n");
1423       GNUNET_break (0);
1424     }
1425     else
1426     {
1427       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1428                        "gnunet-helper-transport-wlan not found, please look if it exists and is the $PATH variable!\n");
1429       GNUNET_break (0);
1430     }
1431
1432   }
1433   else if (plugin->testmode == 1)
1434   {
1435
1436     GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
1437                      "Starting gnunet-helper-transport-wlan-dummy loopback 1 process cmd: %s %s %i\n",
1438                      absolute_filename, plugin->interface, plugin->testmode);
1439     plugin->server_proc =
1440         GNUNET_OS_start_process (GNUNET_NO, plugin->server_stdin, plugin->server_stdout,
1441                                  absolute_filename, absolute_filename, "1",
1442                                  NULL);
1443     if (plugin->server_proc == NULL)
1444     {
1445       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1446                        "`%s' not found, please look if it exists and is in the $PATH variable!\n",
1447                        absolute_filename);
1448       GNUNET_break (0);
1449     }
1450   }
1451   else if (plugin->testmode == 2)
1452   {
1453     GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
1454                      "Starting gnunet-helper-transport-wlan-dummy loopback 2 process cmd: %s %s %i\n",
1455                      absolute_filename, plugin->interface, plugin->testmode);
1456     plugin->server_proc =
1457         GNUNET_OS_start_process (GNUNET_NO, plugin->server_stdin, plugin->server_stdout,
1458                                  absolute_filename, absolute_filename, "2",
1459                                  NULL);
1460     if (plugin->server_proc == NULL)
1461     {
1462       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1463                        "`%s' not found, please look if it exists and is in the $PATH variable!\n",
1464                        absolute_filename);
1465       GNUNET_break (0);
1466     }
1467   }
1468   if (absolute_filename != NULL)
1469     GNUNET_free (absolute_filename);
1470   if (plugin->server_proc == NULL)
1471   {
1472     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1473                      "Failed to start gnunet-helper-transport-wlan process\n");
1474     return GNUNET_SYSERR;
1475   }
1476
1477
1478
1479   /* Close the write end of the read pipe */
1480   GNUNET_DISK_pipe_close_end (plugin->server_stdout,
1481                               GNUNET_DISK_PIPE_END_WRITE);
1482
1483   /* Close the read end of the write pipe */
1484   GNUNET_DISK_pipe_close_end (plugin->server_stdin, GNUNET_DISK_PIPE_END_READ);
1485
1486   plugin->server_stdout_handle =
1487       GNUNET_DISK_pipe_handle (plugin->server_stdout,
1488                                GNUNET_DISK_PIPE_END_READ);
1489   plugin->server_stdin_handle =
1490       GNUNET_DISK_pipe_handle (plugin->server_stdin,
1491                                GNUNET_DISK_PIPE_END_WRITE);
1492
1493   GNUNET_assert (plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
1494
1495   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1496                    "Adding server_read_task for the gnunet-helper-transport-wlan\n");
1497   plugin->server_read_task =
1498       GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1499                                       plugin->server_stdout_handle,
1500                                       &wlan_plugin_helper_read, plugin);
1501
1502   plugin->helper_is_running = GNUNET_YES;
1503   return GNUNET_YES;
1504 }
1505
1506 /**
1507  * Stops the gnunet-helper-transport-wlan process.
1508  *
1509  * @param plugin the transport plugin
1510  * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
1511  */
1512 static int
1513 wlan_transport_stop_wlan_helper (struct Plugin *plugin)
1514 {
1515   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1516                    "Stoping WLAN helper process\n");
1517
1518   if (plugin->helper_is_running == GNUNET_NO)
1519   {
1520     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1521                      "wlan_transport_stop_wlan_helper not needed, helper already stopped!");
1522     return GNUNET_YES;
1523   }
1524
1525   if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
1526   {
1527     GNUNET_SCHEDULER_cancel (plugin->server_write_delay_task);
1528     plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
1529   }
1530
1531   if (plugin->server_write_task != GNUNET_SCHEDULER_NO_TASK)
1532   {
1533     GNUNET_SCHEDULER_cancel (plugin->server_write_task);
1534     plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1535   }
1536
1537   if (plugin->server_read_task != GNUNET_SCHEDULER_NO_TASK)
1538   {
1539     GNUNET_SCHEDULER_cancel (plugin->server_read_task);
1540     plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
1541   }
1542
1543   GNUNET_DISK_pipe_close (plugin->server_stdout);
1544   GNUNET_DISK_pipe_close (plugin->server_stdin);
1545   GNUNET_OS_process_kill (plugin->server_proc, SIGKILL);
1546   GNUNET_OS_process_wait (plugin->server_proc);
1547   GNUNET_OS_process_close (plugin->server_proc);
1548
1549   plugin->helper_is_running = GNUNET_NO;
1550
1551   return GNUNET_YES;
1552 }
1553
1554 /**
1555  * function for delayed restart of the helper process
1556  * @param cls Finish_send struct if message should be finished
1557  * @param tc TaskContext
1558  */
1559 static void
1560 delay_restart_helper (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1561 {
1562   struct Finish_send *finish = cls;
1563   struct Plugin *plugin;
1564
1565   plugin = finish->plugin;
1566
1567   plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1568   if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1569   {
1570     GNUNET_free_non_null (finish->msgstart);
1571     GNUNET_free (finish);
1572     return;
1573   }
1574
1575   wlan_transport_start_wlan_helper (plugin);
1576
1577   if (finish->size != 0)
1578   {
1579     plugin->server_write_task =
1580         GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1581                                          plugin->server_stdin_handle,
1582                                          &finish_sending, finish);
1583   }
1584   else
1585   {
1586     set_next_send (plugin);
1587     GNUNET_free_non_null (finish->msgstart);
1588     GNUNET_free (finish);
1589   }
1590
1591 }
1592
1593 /**
1594  * Function to restart the helper
1595  * @param plugin pointer to the global plugin struct
1596  * @param finish pointer to the Finish_send struct to finish
1597  */
1598 static void
1599 restart_helper (struct Plugin *plugin, struct Finish_send *finish)
1600 {
1601   static struct GNUNET_TIME_Relative next_try = { 1000 };
1602   GNUNET_assert (finish != NULL);
1603
1604   wlan_transport_stop_wlan_helper (plugin);
1605   plugin->server_write_task =
1606       GNUNET_SCHEDULER_add_delayed (next_try, &delay_restart_helper, finish);
1607   GNUNET_TIME_relative_multiply (next_try, HELPER_RESTART_SCALING_FACTOR);
1608
1609 }
1610
1611 /**
1612  * function to finish a sending if not all could have been writen befor
1613  * @param cls pointer to the Finish_send struct
1614  * @param tc TaskContext
1615  */
1616 static void
1617 finish_sending (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1618 {
1619   struct Finish_send *finish = cls;
1620   struct Plugin *plugin;
1621   ssize_t bytes;
1622
1623   plugin = finish->plugin;
1624   plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1625
1626   if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1627   {
1628     GNUNET_free (finish->msgstart);
1629     GNUNET_free (finish);
1630     return;
1631   }
1632   bytes =
1633       GNUNET_DISK_file_write (plugin->server_stdin_handle,
1634                               finish->head_of_next_write, finish->size);
1635
1636   if (bytes != finish->size)
1637   {
1638     if (bytes != GNUNET_SYSERR)
1639     {
1640       finish->head_of_next_write += bytes;
1641       finish->size -= bytes;
1642       plugin->server_write_task =
1643           GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1644                                            plugin->server_stdin_handle,
1645                                            &finish_sending, finish);
1646     }
1647     else
1648     {
1649       restart_helper (plugin, finish);
1650     }
1651   }
1652   else
1653   {
1654     GNUNET_free (finish->msgstart);
1655     GNUNET_free (finish);
1656     set_next_send (plugin);
1657   }
1658 }
1659
1660
1661 /**
1662  * function to send a hello beacon
1663  * @param plugin pointer to the plugin struct
1664  */
1665 static void
1666 send_hello_beacon (struct Plugin *plugin)
1667 {
1668   uint16_t size;
1669   ssize_t bytes;
1670   uint16_t hello_size;
1671   struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
1672   const struct GNUNET_MessageHeader *hello;
1673   struct Finish_send *finish;
1674
1675   GNUNET_assert (plugin != NULL);
1676   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1677                    "Sending hello beacon\n");
1678   GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan hello beacons send"),
1679                             1, GNUNET_NO);
1680   hello = plugin->env->get_our_hello ();
1681   hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1682   GNUNET_assert (sizeof (struct WlanHeader) + hello_size <= WLAN_MTU);
1683
1684   size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + hello_size;
1685   radioHeader = GNUNET_malloc (size);
1686   getRadiotapHeader (plugin, NULL, radioHeader, size);
1687   getWlanHeader (&radioHeader->frame, &bc_all_mac, plugin, size);
1688   memcpy (&radioHeader[1], hello, hello_size);
1689   bytes = GNUNET_DISK_file_write (plugin->server_stdin_handle, radioHeader, size);
1690   GNUNET_free (radioHeader);
1691   if (bytes == GNUNET_SYSERR)
1692   {
1693     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1694                      _
1695                      ("Error writing to wlan helper. errno == %d, ERROR: %s\n"),
1696                      errno, strerror (errno));
1697     finish = GNUNET_malloc (sizeof (struct Finish_send));
1698     finish->plugin = plugin;
1699     finish->head_of_next_write = NULL;
1700     finish->size = 0;
1701     finish->msgstart = NULL;
1702     restart_helper (plugin, finish);
1703     set_next_beacon_time (plugin);
1704   }
1705   else
1706   {
1707     GNUNET_assert (bytes == size);
1708     set_next_beacon_time (plugin);
1709     set_next_send (plugin);
1710   }
1711 }
1712
1713
1714 /**
1715  * function to add an ack to send it for a received fragment
1716  *
1717  * @param cls MacEndpoint this ack belongs to
1718  * @param msg_id id of the message
1719  * @param hdr pointer to the hdr where the ack is stored
1720  */
1721 static void
1722 add_ack_for_send (void *cls, uint32_t msg_id,
1723                   const struct GNUNET_MessageHeader *hdr)
1724 {
1725   struct MacEndpoint *endpoint = cls;
1726   struct Plugin *plugin = endpoint->plugin;
1727   struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage* radioHeader;
1728   struct AckSendQueue *ack;
1729   struct GNUNET_MessageHeader *msgheader;
1730   uint16_t size;
1731
1732   GNUNET_assert (endpoint != NULL);
1733   size = sizeof (struct AckSendQueue) + sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + ntohs (hdr->size);
1734   ack = GNUNET_malloc (size);
1735   ack->message_id = msg_id;
1736   ack->endpoint = endpoint;
1737   radioHeader = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage*) &ack[1];
1738   ack->radioHeader = radioHeader;
1739   getRadiotapHeader (plugin, ack->endpoint, radioHeader, size - sizeof (struct AckSendQueue));
1740   size = ntohs (hdr->size);
1741   getWlanHeader (&radioHeader->frame, 
1742                  &ack->endpoint->addr, 
1743                  plugin,
1744                  size);
1745   msgheader = (struct GNUNET_MessageHeader *) &radioHeader[1];
1746   memcpy (msgheader, hdr, size);
1747   GNUNET_CONTAINER_DLL_insert_tail (plugin->ack_send_queue_head,
1748                                     plugin->ack_send_queue_tail, ack);
1749   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1750                    "Adding ack with message id %u to send, AckSendQueue %p, endpoint %p\n",
1751                    msg_id, ack, endpoint);
1752   set_next_send (plugin);
1753 }
1754
1755
1756 /**
1757  * Function for the scheduler if a FragmentMessage times out
1758  * @param cls pointer to the FragmentMessage
1759  * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
1760  */
1761 static void
1762 fragmentmessage_timeout (void *cls,
1763                          const struct GNUNET_SCHEDULER_TaskContext *tc)
1764 {
1765   struct FragmentMessage *fm = cls;
1766
1767   GNUNET_assert (fm != NULL);
1768   fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1769   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1770   {
1771     return;
1772   }
1773   free_fragment_message (fm->session->mac->plugin, fm);
1774 }
1775
1776 /**
1777  * Function to check if there is some space in the fragment queue
1778  * inserts a message if space is available
1779  * @param plugin the plugin struct
1780  */
1781
1782 static void
1783 check_fragment_queue (struct Plugin *plugin)
1784 {
1785   struct Session *session;
1786   struct FragmentMessage *fm;
1787   struct GNUNET_PeerIdentity pid;
1788
1789   struct PendingMessage *pm;
1790
1791   if (plugin->pending_Fragment_Messages < FRAGMENT_QUEUE_SIZE)
1792   {
1793     session = get_next_queue_session (plugin);
1794     if (session != NULL)
1795     {
1796       pm = session->pending_message_head;
1797       GNUNET_assert (pm != NULL);
1798       GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
1799                                    session->pending_message_tail, pm);
1800       session->mac->fragment_messages_out_count++;
1801       session->fragment_messages_out_count++;
1802       plugin->pending_Fragment_Messages++;
1803       GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending fragments"),
1804                              plugin->pending_Fragment_Messages, GNUNET_NO);
1805
1806       fm = GNUNET_malloc (sizeof (struct FragmentMessage));
1807       fm->session = session;
1808       fm->timeout.abs_value = pm->timeout.abs_value;
1809       fm->fragcontext =
1810           GNUNET_FRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
1811                                           &plugin->tracker,
1812                                           GNUNET_TIME_UNIT_SECONDS,
1813                                           &(pm->msg->header),
1814                                           &add_message_for_send, fm);
1815       fm->timeout_task =
1816           GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
1817                                         (fm->timeout), fragmentmessage_timeout,
1818                                         fm);
1819       GNUNET_CONTAINER_DLL_insert_tail (session->mac->sending_messages_head,
1820                                         session->mac->sending_messages_tail,
1821                                         fm);
1822
1823       if (pm->transmit_cont != NULL)
1824       {
1825         pid = session->target;
1826         pm->transmit_cont (pm->transmit_cont_cls, &pid, GNUNET_OK);
1827         GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1828                          "called pm->transmit_cont for %p\n", session);
1829       }
1830       else
1831       {
1832         GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1833                          "no pm->transmit_cont for %p\n", session);
1834       }
1835       GNUNET_free (pm);
1836
1837       if (session->pending_message_head != NULL)
1838       {
1839         //requeue session
1840         queue_session (plugin, session);
1841       }
1842
1843     }
1844   }
1845
1846   //check if timeout changed
1847   set_next_send (plugin);
1848 }
1849
1850
1851 /**
1852  * Function to send an ack, does not free the ack
1853  * @param plugin pointer to the plugin
1854  */
1855 static void
1856 send_ack (struct Plugin *plugin)
1857 {
1858   ssize_t bytes;
1859   struct AckSendQueue *ack;
1860   struct Finish_send *finish;
1861
1862   GNUNET_assert (plugin != NULL);
1863   ack = plugin->ack_send_queue_head;
1864   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1865                    "Sending ack for message_id %u for mac endpoint %p, size %u\n",
1866                    ack->message_id, ack->endpoint,
1867                    ntohs (ack->radioHeader->header.size));
1868   GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan acks send"), 1,
1869                             GNUNET_NO);
1870   bytes =
1871       GNUNET_DISK_file_write (plugin->server_stdin_handle, ack->radioHeader,
1872                               ntohs (ack->radioHeader->header.size));
1873   if (bytes == GNUNET_SYSERR)
1874   {
1875     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1876                      _
1877                      ("Error writing to wlan helper. errno == %d, ERROR: %s\n"),
1878                      errno, strerror (errno));
1879     finish = GNUNET_malloc (sizeof (struct Finish_send));
1880     finish->plugin = plugin;
1881     finish->head_of_next_write = NULL;
1882     finish->size = 0;
1883     finish->msgstart = NULL;
1884     restart_helper (plugin, finish);
1885     return;
1886   }
1887   GNUNET_assert (bytes == ntohs (ack->radioHeader->header.size));
1888   GNUNET_CONTAINER_DLL_remove (plugin->ack_send_queue_head,
1889                                plugin->ack_send_queue_tail, ack);
1890   GNUNET_free (ack);
1891   set_next_send (plugin);
1892 }
1893
1894
1895 /**
1896  * Function called when wlan helper is ready to get some data
1897  *
1898  * @param cls closure
1899  * @param tc GNUNET_SCHEDULER_TaskContext
1900  */
1901 static void
1902 do_transmit (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1903 {
1904   struct Plugin *plugin = cls;
1905   struct Session *session;
1906   struct FragmentMessage *fm;
1907   struct Finish_send *finish;
1908   struct FragmentMessage_queue *fmq;
1909   ssize_t bytes;
1910
1911
1912   GNUNET_assert (plugin != NULL);
1913   plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1914   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1915     return;
1916   if (plugin->ack_send_queue_head != NULL)
1917   {
1918     send_ack (plugin);
1919     return;
1920   }
1921   //test if a "hello-beacon" has to be send
1922   if (GNUNET_TIME_absolute_get_remaining (plugin->beacon_time).rel_value == 0)
1923   {
1924     send_hello_beacon (plugin);
1925     return;
1926   }
1927   if (NULL == plugin->sending_messages_head)
1928   {
1929     /* do_transmit did nothing, should not happen */  
1930     GNUNET_break (0);
1931     set_next_send (plugin);
1932     return;
1933   }
1934   GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan fragments send"), 1,
1935                             GNUNET_NO);
1936   
1937   fmq = plugin->sending_messages_head;
1938   fm = fmq->content;
1939   GNUNET_CONTAINER_DLL_remove (plugin->sending_messages_head,
1940                                plugin->sending_messages_tail, fmq);
1941   GNUNET_free (fmq);
1942   
1943   session = fm->session;
1944   GNUNET_assert (session != NULL);
1945   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1946                    "Sending GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT for fragment message %p, size: %u\n",
1947                    fm, ntohs (fm->radioHeader->header.size));
1948   bytes =
1949     GNUNET_DISK_file_write (plugin->server_stdin_handle, 
1950                             fm->radioHeader,
1951                             ntohs (fm->radioHeader->header.size));
1952   if (bytes != ntohs (fm->radioHeader->header.size))
1953   {
1954     finish = GNUNET_malloc (sizeof (struct Finish_send));
1955     finish->plugin = plugin;
1956     finish->msgstart = &fm->radioHeader->header;
1957     GNUNET_assert (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK);
1958     if (bytes == GNUNET_SYSERR)
1959     {
1960       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1961                        _
1962                        ("Error writing to wlan helper. errno == %d, ERROR: %s\n"),
1963                        errno, strerror (errno));     
1964       finish->head_of_next_write = (char*) fm->radioHeader;
1965       finish->size = ntohs (fm->radioHeader->header.size);
1966       restart_helper (plugin, finish);
1967     }
1968     else
1969     {
1970       finish->head_of_next_write = ((char*) fm->radioHeader) + bytes;
1971       finish->size = ntohs (fm->radioHeader->header.size) - bytes;
1972       plugin->server_write_task =
1973         GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1974                                          plugin->server_stdin_handle,
1975                                          &finish_sending, finish);
1976     }    
1977   }
1978   else
1979   {
1980     GNUNET_free (fm->radioHeader);
1981     fm->radioHeader = NULL;
1982     set_next_send (plugin);
1983   }
1984   GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
1985 }
1986
1987
1988 /**
1989  * Another peer has suggested an address for this
1990  * peer and transport plugin.  Check that this could be a valid
1991  * address.  If so, consider adding it to the list
1992  * of addresses.
1993  *
1994  * @param cls closure
1995  * @param addr pointer to the address
1996  * @param addrlen length of addr
1997  * @return GNUNET_OK if this is a plausible address for this peer
1998  *         and transport
1999  */
2000 static int
2001 wlan_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
2002 {
2003   //struct Plugin *plugin = cls;
2004
2005   /* check if the address is plausible; if so,
2006    * add it to our list! */
2007
2008   GNUNET_assert (cls != NULL);
2009   //FIXME mitm is not checked
2010   //Mac Address has 6 bytes
2011   if (addrlen == 6)
2012   {
2013     /* TODO check for bad addresses like multicast, broadcast, etc */
2014     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2015                      "wlan_plugin_address_suggested got good address, size %u!\n",
2016                      addrlen);
2017     return GNUNET_OK;
2018   }
2019   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2020                    "wlan_plugin_address_suggested got bad address, size %u!\n",
2021                    addrlen);
2022   return GNUNET_SYSERR;
2023 }
2024
2025
2026 /**
2027  * Creates a new outbound session the transport service will use to send data to the
2028  * peer
2029  *
2030  * @param cls the plugin
2031  * @param address the address
2032  * @return the session or NULL of max connections exceeded
2033  */
2034
2035 static struct Session *
2036 wlan_plugin_get_session (void *cls,
2037                   const struct GNUNET_HELLO_Address *address)
2038 {
2039   struct Plugin *plugin = cls;
2040   struct Session * s = NULL;
2041
2042   GNUNET_assert (plugin != NULL);
2043   GNUNET_assert (address != NULL);
2044
2045   if (GNUNET_OK == wlan_plugin_address_suggested (plugin,
2046             address->address,
2047             address->address_length))
2048   {
2049     s = get_session (plugin, address->address, &address->peer);
2050   }
2051   else
2052   {
2053     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
2054                      _("Wlan Address len %d is wrong\n"), address->address_length);
2055     return s;
2056   }
2057
2058   return s;
2059 }
2060
2061 /**
2062  * Function that can be used by the transport service to transmit
2063  * a message using the plugin.   Note that in the case of a
2064  * peer disconnecting, the continuation MUST be called
2065  * prior to the disconnect notification itself.  This function
2066  * will be called with this peer's HELLO message to initiate
2067  * a fresh connection to another peer.
2068  *
2069  * @param cls closure
2070  * @param session which session must be used
2071  * @param msgbuf the message to transmit
2072  * @param msgbuf_size number of bytes in 'msgbuf'
2073  * @param priority how important is the message (most plugins will
2074  *                 ignore message priority and just FIFO)
2075  * @param to how long to wait at most for the transmission (does not
2076  *                require plugins to discard the message after the timeout,
2077  *                just advisory for the desired delay; most plugins will ignore
2078  *                this as well)
2079  * @param cont continuation to call once the message has
2080  *        been transmitted (or if the transport is ready
2081  *        for the next transmission call; or if the
2082  *        peer disconnected...); can be NULL
2083  * @param cont_cls closure for cont
2084  * @return number of bytes used (on the physical network, with overheads);
2085  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
2086  *         and does NOT mean that the message was not transmitted (DV)
2087  */
2088 static ssize_t
2089 wlan_plugin_send (void *cls,
2090                   struct Session *session,
2091                   const char *msgbuf, size_t msgbuf_size,
2092                   unsigned int priority,
2093                   struct GNUNET_TIME_Relative to,
2094                   GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
2095 {
2096   struct Plugin *plugin = cls;
2097   struct PendingMessage *newmsg;
2098   struct WlanHeader *wlanheader;
2099
2100   GNUNET_assert (plugin != NULL);
2101   GNUNET_assert (session != NULL);
2102   GNUNET_assert (msgbuf_size > 0);
2103
2104   //queue message:
2105
2106   //queue message in session
2107   //test if there is no other message in the "queue"
2108   //FIXME: to many send requests
2109   if (session->pending_message_head != NULL)
2110   {
2111     newmsg = session->pending_message_head;
2112     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2113                      "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",
2114                      GNUNET_TIME_absolute_get_remaining (newmsg->
2115                                                          timeout).rel_value,
2116                      session->mac->fragment_messages_out_count);
2117   }
2118
2119   newmsg = GNUNET_malloc (sizeof (struct PendingMessage));
2120   newmsg->msg = GNUNET_malloc (msgbuf_size + sizeof (struct WlanHeader));
2121   wlanheader = newmsg->msg;
2122   //copy msg to buffer, not fragmented / segmented yet, but with message header
2123   wlanheader->header.size = htons (msgbuf_size + sizeof (struct WlanHeader));
2124   wlanheader->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA);
2125   memcpy (&(wlanheader->target), &session->target, sizeof (struct GNUNET_PeerIdentity));
2126   memcpy (&(wlanheader->source), plugin->env->my_identity,
2127           sizeof (struct GNUNET_PeerIdentity));
2128   wlanheader->crc = 0;
2129   memcpy (&wlanheader[1], msgbuf, msgbuf_size);
2130   wlanheader->crc =
2131       htonl (GNUNET_CRYPTO_crc32_n
2132              ((char *) wlanheader, msgbuf_size + sizeof (struct WlanHeader)));
2133
2134   newmsg->transmit_cont = cont;
2135   newmsg->transmit_cont_cls = cont_cls;
2136   newmsg->timeout = GNUNET_TIME_relative_to_absolute (to);
2137
2138   newmsg->timeout.abs_value = newmsg->timeout.abs_value - 500;
2139
2140   newmsg->message_size = msgbuf_size + sizeof (struct WlanHeader);
2141
2142   GNUNET_CONTAINER_DLL_insert_tail (session->pending_message_head,
2143                                     session->pending_message_tail, newmsg);
2144
2145   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2146                    "New message for %p with size (incl wlan header) %u added\n",
2147                    session, newmsg->message_size);
2148 #if DEBUG_WLAN > 1
2149   hexdump (msgbuf, GNUNET_MIN (msgbuf_size, 256));
2150 #endif
2151   //queue session
2152   queue_session (plugin, session);
2153
2154   check_fragment_queue (plugin);
2155   //FIXME not the correct size
2156   return msgbuf_size;
2157 }
2158
2159
2160 /**
2161  * function to free a mac endpoint
2162  * @param plugin pointer to the plugin struct
2163  * @param endpoint pointer to the MacEndpoint to free
2164  */
2165 static void
2166 free_macendpoint (struct Plugin *plugin, struct MacEndpoint *endpoint)
2167 {
2168   struct Sessionqueue *sessions;
2169   struct Sessionqueue *sessions_next;
2170
2171   GNUNET_assert (endpoint != NULL);
2172
2173   sessions = endpoint->sessions_head;
2174   while (sessions != NULL)
2175   {
2176     sessions_next = sessions->next;
2177     free_session (plugin, sessions, GNUNET_NO);
2178     sessions = sessions_next;
2179   }
2180
2181   GNUNET_CONTAINER_DLL_remove (plugin->mac_head, plugin->mac_tail, endpoint);
2182   if (endpoint->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2183   {
2184     GNUNET_SCHEDULER_cancel (endpoint->timeout_task);
2185     endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2186   }
2187   plugin->mac_count--;
2188   GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan mac endpoints"),
2189                          plugin->mac_count, GNUNET_NO);
2190   GNUNET_free (endpoint);
2191
2192 }
2193
2194 /**
2195  * function to free a session
2196  * @param plugin pointer to the plugin
2197  * @param queue pointer to the sessionqueue element to free
2198  * @param do_free_macendpoint if GNUNET_YES and mac endpoint would be empty, free mac endpoint
2199  */
2200 static void
2201 free_session (struct Plugin *plugin, struct Sessionqueue *queue,
2202               int do_free_macendpoint)
2203 {
2204   struct Sessionqueue *pendingsession;
2205   struct Sessionqueue *pendingsession_tmp;
2206   struct PendingMessage *pm;
2207   struct MacEndpoint *endpoint;
2208   struct FragmentMessage *fm;
2209   struct FragmentMessage *fmnext;
2210   int check = 0;
2211
2212   GNUNET_assert (plugin != NULL);
2213   GNUNET_assert (queue != NULL);
2214   GNUNET_assert (queue->content != NULL);
2215
2216   //session found
2217   //is this session pending for send
2218   pendingsession = plugin->pending_Sessions_head;
2219   while (pendingsession != NULL)
2220   {
2221     pendingsession_tmp = pendingsession;
2222     pendingsession = pendingsession->next;
2223     GNUNET_assert (pendingsession_tmp->content != NULL);
2224     if (pendingsession_tmp->content == queue->content)
2225     {
2226       plugin->pendingsessions--;
2227       GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
2228                              plugin->pendingsessions, GNUNET_NO);
2229       GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
2230                                    plugin->pending_Sessions_tail,
2231                                    pendingsession_tmp);
2232       GNUNET_free (pendingsession_tmp);
2233
2234       GNUNET_assert (check == 0);
2235       check = 1;
2236     }
2237   }
2238
2239   endpoint = queue->content->mac;
2240   fm = endpoint->sending_messages_head;
2241   while (fm != NULL)
2242   {
2243     fmnext = fm->next;
2244     if (fm->session == queue->content)
2245     {
2246       free_fragment_message (plugin, fm);
2247     }
2248     fm = fmnext;
2249   }
2250
2251   // remove PendingMessage
2252   pm = queue->content->pending_message_head;
2253   while (pm != NULL)
2254   {
2255     GNUNET_CONTAINER_DLL_remove (queue->content->pending_message_head,
2256                                  queue->content->pending_message_tail, pm);
2257     GNUNET_free (pm->msg);
2258     GNUNET_free (pm);
2259     pm = queue->content->pending_message_head;
2260   }
2261
2262   GNUNET_CONTAINER_DLL_remove (endpoint->sessions_head, endpoint->sessions_tail,
2263                                queue);
2264   //Check that no ohter session on this endpoint for this session exits
2265   GNUNET_assert (search_session (plugin, endpoint, &queue->content->target) ==
2266                  NULL);
2267   if (endpoint->sessions_head == NULL && do_free_macendpoint == GNUNET_YES)
2268   {
2269     free_macendpoint (plugin, endpoint);
2270     //check if no endpoint with the same address exists
2271     GNUNET_assert (get_macendpoint (plugin, &endpoint->addr, GNUNET_NO) ==
2272                    NULL);
2273   }
2274
2275   if (queue->content->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2276   {
2277     GNUNET_SCHEDULER_cancel (queue->content->timeout_task);
2278     queue->content->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2279   }
2280   GNUNET_free (queue);
2281
2282   check_fragment_queue (plugin);
2283 }
2284
2285 /**
2286  * Function that can be used to force the plugin to disconnect
2287  * from the given peer and cancel all previous transmissions
2288  * (and their continuation).
2289  *
2290  * @param cls closure
2291  * @param target peer from which to disconnect
2292  */
2293 static void
2294 wlan_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
2295 {
2296   struct Plugin *plugin = cls;
2297   struct Sessionqueue *queue;
2298   struct Sessionqueue *queue_next;
2299   struct MacEndpoint *endpoint = plugin->mac_head;
2300   struct MacEndpoint *endpoint_next;
2301
2302   // just look at all the session for the needed one
2303   while (endpoint != NULL)
2304   {
2305     queue = endpoint->sessions_head;
2306     endpoint_next = endpoint->next;
2307     while (queue != NULL)
2308     {
2309       // content is never NULL
2310       GNUNET_assert (queue->content != NULL);
2311       queue_next = queue->next;
2312       if (memcmp
2313           (target, &(queue->content->target),
2314            sizeof (struct GNUNET_PeerIdentity)) == 0)
2315       {
2316         free_session (plugin, queue, GNUNET_YES);
2317       }
2318       // try next
2319       queue = queue_next;
2320     }
2321     endpoint = endpoint_next;
2322   }
2323 }
2324
2325 /**
2326  * Convert the transports address to a nice, human-readable
2327  * format.
2328  *
2329  * @param cls closure
2330  * @param type name of the transport that generated the address
2331  * @param addr one of the addresses of the host, NULL for the last address
2332  *        the specific address format depends on the transport
2333  * @param addrlen length of the address
2334  * @param numeric should (IP) addresses be displayed in numeric form?
2335  * @param timeout after how long should we give up?
2336  * @param asc function to call on each string
2337  * @param asc_cls closure for asc
2338  */
2339 static void
2340 wlan_plugin_address_pretty_printer (void *cls, const char *type,
2341                                     const void *addr, size_t addrlen,
2342                                     int numeric,
2343                                     struct GNUNET_TIME_Relative timeout,
2344                                     GNUNET_TRANSPORT_AddressStringCallback asc,
2345                                     void *asc_cls)
2346 {
2347   char *ret;
2348   const unsigned char *input;
2349
2350   //GNUNET_assert(cls !=NULL);
2351   if (addrlen != sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))
2352   {
2353     /* invalid address (MAC addresses have 6 bytes) */
2354     //GNUNET_break (0);
2355     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2356                      "Func wlan_plugin_address_pretty_printer got size: %u, worng size!\n",
2357                      addrlen);
2358     asc (asc_cls, NULL);
2359     return;
2360   }
2361   input = (const unsigned char *) addr;
2362   GNUNET_asprintf (&ret,
2363                    "Transport %s: %s Mac-Address %.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
2364                    type, PROTOCOL_PREFIX, input[0], input[1], input[2],
2365                    input[3], input[4], input[5]);
2366   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2367                    "Func wlan_plugin_address_pretty_printer got size: %u, nummeric %u, type %s; made string: %s\n",
2368                    addrlen, numeric, type, ret);
2369   asc (asc_cls, ret);
2370   //only one mac address per plugin
2371   asc (asc_cls, NULL);
2372 }
2373
2374
2375
2376 /**
2377  * handels the data after all fragments are put together
2378  * @param cls macendpoint this messages belongs to
2379  * @param hdr pointer to the data
2380  */
2381 static void
2382 wlan_data_message_handler (void *cls, const struct GNUNET_MessageHeader *hdr)
2383 {
2384   struct MacEndpoint *endpoint = (struct MacEndpoint *) cls;
2385   struct Plugin *plugin = endpoint->plugin;
2386   struct WlanHeader *wlanheader;
2387   struct Session *session;
2388
2389   const struct GNUNET_MessageHeader *temp_hdr;
2390   struct GNUNET_PeerIdentity tmpsource;
2391   int crc;
2392
2393   GNUNET_assert (plugin != NULL);
2394
2395   if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_DATA)
2396   {
2397
2398     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2399                      "Func wlan_data_message_handler got GNUNET_MESSAGE_TYPE_WLAN_DATA size: %u\n",
2400                      ntohs (hdr->size));
2401
2402     if (ntohs (hdr->size) <
2403         sizeof (struct WlanHeader) + sizeof (struct GNUNET_MessageHeader))
2404     {
2405       //packet not big enought
2406       return;
2407     }
2408
2409     GNUNET_STATISTICS_update (plugin->env->stats,
2410                               _("# wlan whole messages received"), 1,
2411                               GNUNET_NO);
2412     wlanheader = (struct WlanHeader *) hdr;
2413
2414     session = search_session (plugin, endpoint, &wlanheader->source);
2415
2416     temp_hdr = (const struct GNUNET_MessageHeader *) &wlanheader[1];
2417     crc = ntohl (wlanheader->crc);
2418     wlanheader->crc = 0;
2419     if (GNUNET_CRYPTO_crc32_n
2420         ((char *) wlanheader, ntohs (wlanheader->header.size)) != crc)
2421     {
2422       //wrong crc, dispose message
2423       GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2424                        "Wlan message header crc was wrong: %u != %u\n",
2425                        GNUNET_CRYPTO_crc32_n ((char *) wlanheader,
2426                                               ntohs (wlanheader->header.size)),
2427                        crc);
2428       hexdump ((void *) hdr, ntohs (hdr->size));
2429       return;
2430     }
2431
2432     //if not in session list
2433     if (session == NULL)
2434     {
2435       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2436                        "WLAN client not in session list: packet size = %u, inner size = %u, header size = %u\n",
2437                        ntohs (wlanheader->header.size), ntohs (temp_hdr->size),
2438                        sizeof (struct WlanHeader));
2439       //try if it is a hello message
2440       if (ntohs (wlanheader->header.size) >=
2441           ntohs (temp_hdr->size) + sizeof (struct WlanHeader))
2442       {
2443         if (ntohs (temp_hdr->type) == GNUNET_MESSAGE_TYPE_HELLO)
2444         {
2445           if (GNUNET_HELLO_get_id
2446               ((const struct GNUNET_HELLO_Message *) temp_hdr,
2447                &tmpsource) == GNUNET_OK)
2448           {
2449             session = create_session (plugin, endpoint, &tmpsource);
2450           }
2451           else
2452           {
2453             GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2454                              "WLAN client not in session list and hello message is not okay\n");
2455             return;
2456           }
2457
2458         }
2459         else
2460         {
2461           GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2462                            "WLAN client not in session list and not a hello message\n");
2463           return;
2464         }
2465       }
2466       else
2467       {
2468         GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2469                          "WLAN client not in session list and message size in does not fit\npacket size = %u, inner size = %u, header size = %u\n",
2470                          ntohs (wlanheader->header.size),
2471                          ntohs (temp_hdr->size), sizeof (struct WlanHeader));
2472         return;
2473       }
2474     }
2475
2476     //"receive" the message
2477
2478     if (memcmp
2479         (&wlanheader->source, &session->target,
2480          sizeof (struct GNUNET_PeerIdentity)) != 0)
2481     {
2482       //wrong peer id
2483       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2484                        "WLAN peer source id doesn't match packet peer source id: session %p\n",
2485                        session);
2486       return;
2487     }
2488
2489     if (memcmp
2490         (&wlanheader->target, plugin->env->my_identity,
2491          sizeof (struct GNUNET_PeerIdentity)) != 0)
2492     {
2493       //wrong peer id
2494       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2495                        "WLAN peer target id doesn't match our peer id: session %p\n",
2496                        session);
2497       return;
2498     }
2499
2500     GNUNET_SERVER_mst_receive (plugin->data_tokenizer, session,
2501                                (const char *) temp_hdr,
2502                                ntohs (hdr->size) - sizeof (struct WlanHeader),
2503                                GNUNET_YES, GNUNET_NO);
2504
2505     return;
2506   }
2507   else
2508   {
2509     GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2510                      "wlan_data_message_handler got wrong message type: %u\n",
2511                      ntohs (hdr->size));
2512     return;
2513   }
2514 }
2515
2516 /**
2517  * function to process the a message, give it to the higher layer
2518  * @param cls pointer to the plugin
2519  * @param client pointer to the session this message belongs to
2520  * @param hdr start of the message
2521  */
2522 //TODO ATS informations
2523 static void
2524 process_data (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
2525 {
2526
2527   GNUNET_assert (client != NULL);
2528   GNUNET_assert (cls != NULL);
2529   struct Session *session = (struct Session *) client;
2530   struct Plugin *plugin = (struct Plugin *) cls;
2531   struct GNUNET_ATS_Information ats[2];
2532
2533   ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
2534   ats[0].value = htonl (1);
2535   ats[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
2536   ats[1].value = htonl (GNUNET_ATS_NET_WLAN);
2537
2538   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2539                    "Calling plugin->env->receive for session %p; %s; size: %u\n",
2540                    session, wlan_plugin_address_to_string (NULL,
2541                                                            session->mac->
2542                                                            addr.mac, 6),
2543                    htons (hdr->size));
2544   plugin->env->receive (plugin->env->cls, &(session->target), hdr,
2545                         (const struct GNUNET_ATS_Information *) &ats, 2,
2546                         session, (const char *) &session->mac->addr,
2547                         sizeof (session->mac->addr));
2548 }
2549
2550 /**
2551  * Function used for to process the data received from the wlan interface
2552  *
2553  * @param cls the plugin handle
2554  * @param hdr hdr of the GNUNET_MessageHeader
2555  * @param rxinfo pointer to the radiotap informations got with this packet FIXME: give ATS info
2556  */
2557 static void
2558 wlan_data_helper (void *cls, 
2559                   const struct GNUNET_MessageHeader *hdr,
2560                   const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rxinfo)
2561 {
2562   struct Plugin *plugin = cls;
2563   struct FragmentMessage *fm;
2564   struct FragmentMessage *fm2;
2565   struct GNUNET_PeerIdentity tmpsource;
2566   struct MacEndpoint *macendpoint;
2567   struct Session *session;
2568
2569   // NOTE: session_light->addr = rxinfo->frame.addr2;
2570   macendpoint = get_macendpoint (plugin, &rxinfo->frame.addr2, GNUNET_YES);
2571
2572   switch (ntohs (hdr->type))
2573   {
2574   case GNUNET_MESSAGE_TYPE_HELLO:
2575     //ADVERTISEMENT
2576     //TODO better DOS protection, error handling
2577     //TODO test first then create session
2578     if (GNUNET_HELLO_get_id
2579         ((const struct GNUNET_HELLO_Message *) hdr, &tmpsource) == GNUNET_OK)
2580     {
2581       session = search_session (plugin, macendpoint, &tmpsource);
2582       if (session == NULL)
2583       {
2584         session = create_session (plugin, macendpoint, &tmpsource);
2585       }
2586       GNUNET_STATISTICS_update (plugin->env->stats,
2587                                 _("# wlan hello messages received"), 1,
2588                                 GNUNET_NO);
2589       plugin->env->receive (plugin->env->cls, &session->target,
2590                             hdr, NULL, 0, session,
2591                             (const char *) &session->mac->addr,
2592                             sizeof (session->mac->addr));
2593     }
2594     else
2595     {
2596       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2597                        "WLAN client not in session list and HELLO message is not okay\n");
2598       return;
2599     }
2600     break;
2601   case GNUNET_MESSAGE_TYPE_FRAGMENT:
2602     macendpoint = get_macendpoint (plugin, &rxinfo->frame.addr2, GNUNET_YES);
2603     GNUNET_STATISTICS_update (plugin->env->stats,
2604                               _("# wlan fragments received"), 1, GNUNET_NO);
2605     int ret =
2606         GNUNET_DEFRAGMENT_process_fragment (macendpoint->defrag,
2607                                             hdr);
2608
2609     if (ret == GNUNET_NO)
2610     {
2611       macendpoint->dups++;
2612     }
2613     else if (ret == GNUNET_OK)
2614     {
2615       macendpoint->fragc++;
2616     }
2617     set_next_send (plugin);
2618     break;
2619   case GNUNET_MESSAGE_TYPE_FRAGMENT_ACK:
2620     if (NULL == macendpoint)
2621     {
2622       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2623                        "Macendpoint does not exist for this GNUNET_MESSAGE_TYPE_FRAGMENT_ACK size: %u; %s\n",
2624                        ntohs (hdr->size), wlan_plugin_address_to_string (NULL,
2625                                                                          &rxinfo->frame.addr2.mac,
2626                                                                          6));
2627       return;
2628     }
2629     fm = macendpoint->sending_messages_head;
2630     while (fm != NULL)
2631     {
2632       fm2 = fm->next;
2633       GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan acks received"),
2634                                 1, GNUNET_NO);
2635       int ret = GNUNET_FRAGMENT_process_ack (fm->fragcontext, hdr);
2636
2637       if (ret == GNUNET_OK)
2638       {
2639         GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2640                          "Got last ack, finished fragment message %p\n", fm);
2641         macendpoint->acks++;
2642         fm->session->last_activity = GNUNET_TIME_absolute_get ();
2643         macendpoint->last_activity = fm->session->last_activity;
2644         free_fragment_message (plugin, fm);
2645         check_fragment_queue (plugin);
2646         return;
2647       }
2648       if (ret == GNUNET_NO)
2649       {
2650         GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2651                          "Got ack for: %p\n", fm);
2652         macendpoint->acks++;
2653         return;
2654       }
2655       fm = fm2;
2656     }
2657     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2658                      "WLAN fragment not in fragment list\n");
2659     return;
2660   default:
2661     // TODO Wrong data?
2662     GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2663                      "WLAN packet inside the WLAN helper packet has not the right type: %u size: %u\n",
2664                      ntohs (hdr->type), ntohs (hdr->size));
2665     GNUNET_break (0);
2666     return;
2667   }
2668   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2669                    "Helper finished\n");
2670 }
2671
2672 /**
2673  * Function to print mac addresses nicely.
2674  *
2675  * @param mac the mac address
2676  * @return string to a static buffer with the human-readable mac, will be overwritten during the next call to this function
2677  */
2678 static const char *
2679 macprinter (const struct GNUNET_TRANSPORT_WLAN_MacAddress * mac)
2680 {
2681   static char macstr[20];
2682
2683   GNUNET_snprintf (macstr, sizeof (macstr), "%2X:%2X:%2X:%2X:%2X:%2X", mac->mac[0], mac->mac[1],
2684                    mac->mac[2], mac->mac[3], mac->mac[4], mac->mac[5]);
2685   return macstr;
2686 }
2687
2688 /**
2689  * Function for the scheduler if a mac endpoint times out
2690  * @param cls pointer to the MacEndpoint
2691  * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
2692  */
2693 static void
2694 macendpoint_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2695 {
2696   struct MacEndpoint *endpoint = cls;
2697
2698   GNUNET_assert (endpoint != NULL);
2699   endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2700   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2701   {
2702     return;
2703   }
2704   if (GNUNET_TIME_absolute_get_remaining
2705       (GNUNET_TIME_absolute_add
2706        (endpoint->last_activity, MACENDPOINT_TIMEOUT)).rel_value == 0)
2707   {
2708     GNUNET_assert (endpoint->plugin != NULL);
2709     GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
2710                               _("# wlan mac endpoints timeouts"), 1, GNUNET_NO);
2711     free_macendpoint (endpoint->plugin, endpoint);
2712   }
2713   else
2714   {
2715     endpoint->timeout_task =
2716         GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
2717                                       endpoint);
2718   }
2719 }
2720
2721 /**
2722  * function to create an macendpoint
2723  * @param plugin pointer to the plugin struct
2724  * @param addr pointer to the macaddress
2725  * @return returns a macendpoint
2726  */
2727 static struct MacEndpoint *
2728 create_macendpoint (struct Plugin *plugin, const struct GNUNET_TRANSPORT_WLAN_MacAddress *addr)
2729 {
2730   struct MacEndpoint *newend = GNUNET_malloc (sizeof (struct MacEndpoint));
2731
2732   GNUNET_assert (plugin != NULL);
2733   GNUNET_STATISTICS_update (plugin->env->stats,
2734                             _("# wlan mac endpoints created"), 1, GNUNET_NO);
2735   newend->addr = *addr;
2736   newend->plugin = plugin;
2737   newend->addr = *addr;
2738   newend->fragment_messages_out_count = 0;
2739   newend->defrag =
2740       GNUNET_DEFRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
2741                                         MESSAGES_IN_DEFRAG_QUEUE_PER_MAC,
2742                                         newend, &wlan_data_message_handler,
2743                                         &add_ack_for_send);
2744   newend->last_activity = GNUNET_TIME_absolute_get ();
2745   newend->timeout_task =
2746       GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
2747                                     newend);
2748
2749   plugin->mac_count++;
2750   GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan mac endpoints"),
2751                          plugin->mac_count, GNUNET_NO);
2752   GNUNET_CONTAINER_DLL_insert_tail (plugin->mac_head, plugin->mac_tail, newend);
2753   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2754                    "New Mac Endpoint %p: %s\n", newend,
2755                    wlan_plugin_address_to_string (NULL, newend->addr.mac, 6));
2756   return newend;
2757 }
2758
2759 /**
2760  * Function used for to process the data from the suid process
2761  *
2762  * @param cls the plugin handle
2763  * @param client client that send the data (not used)
2764  * @param hdr header of the GNUNET_MessageHeader
2765  */
2766 static void
2767 wlan_process_helper (void *cls, void *client,
2768                      const struct GNUNET_MessageHeader *hdr)
2769 {
2770   struct Plugin *plugin = cls;
2771   const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rxinfo;
2772   const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *cm;
2773   const struct GNUNET_MessageHeader *temp_hdr = NULL;
2774   const char *end;
2775   int datasize = 0;
2776   int pos;
2777
2778   GNUNET_assert (plugin != NULL);
2779   switch (ntohs (hdr->type))
2780   {
2781   case GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA:
2782     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2783                      "Func wlan_process_helper got GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA size: %u\n",
2784                      ntohs (hdr->size));
2785     GNUNET_STATISTICS_update (plugin->env->stats,
2786                               _("# wlan WLAN_HELPER_DATA received"), 1,
2787                               GNUNET_NO);
2788     //call wlan_process_helper with the message inside, later with wlan: analyze signal
2789     if (ntohs (hdr->size) < sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage))
2790     {
2791       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2792                        "Size of packet is too small; size: %u\n",
2793                        ntohs (hdr->size));
2794       GNUNET_break (0);
2795       /* FIXME: restart SUID process */
2796       return;
2797     }
2798
2799     rxinfo = (const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) hdr;
2800     //process only if it is an broadcast or for this computer both with the gnunet bssid
2801     //check for bssid
2802     if (memcmp
2803         (&rxinfo->frame.addr3, &mac_bssid_gnunet,
2804          sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0)
2805     {
2806       //check for broadcast or mac
2807       if ((memcmp
2808            (&rxinfo->frame.addr1, &bc_all_mac,
2809             sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0) ||
2810           (memcmp
2811            (&rxinfo->frame.addr1, &plugin->mac_address,
2812             sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0))
2813       {
2814         //if packet is from us return
2815         if ((memcmp
2816              (&rxinfo->frame.addr2, &plugin->mac_address,
2817               sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0))
2818         {
2819           /* not for us */
2820           return;
2821         }
2822         datasize = ntohs (hdr->size) - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage);
2823         GNUNET_STATISTICS_update (plugin->env->stats,
2824                                   _("# wlan messages for this client received"),
2825                                   1, GNUNET_NO);
2826         // FIXME: this is a job for SERVER_mst -- 
2827         // what we are doing here is not good for alignment...
2828         pos = 0;
2829         end = (const char*) &rxinfo[1];
2830         while (pos < datasize - sizeof (struct GNUNET_MessageHeader))
2831         {
2832           temp_hdr = (struct GNUNET_MessageHeader *) &end[pos];
2833           if (ntohs (temp_hdr->size) <= datasize - pos)
2834           {
2835             GNUNET_STATISTICS_update (plugin->env->stats,
2836                                       _
2837                                       ("# wlan messages inside WLAN_HELPER_DATA received"),
2838                                       1, GNUNET_NO);
2839             wlan_data_helper (plugin, temp_hdr, rxinfo);
2840           }
2841           else
2842           {
2843             GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2844                              "Size of packet is too small; size: %u > size of packet: %u\n",
2845                              ntohs (temp_hdr->size), datasize - pos);
2846           }
2847           pos += ntohs (temp_hdr->size);
2848         }
2849       }
2850       else
2851       {
2852         GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2853                          "Func wlan_process_helper got wrong MAC: %s\n",
2854                          macprinter (&rxinfo->frame.addr1));
2855       }
2856     }
2857     else
2858     {
2859       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2860                        "Func wlan_process_helper got wrong BSSID: %s\n",
2861                        macprinter (&rxinfo->frame.addr2));
2862     }
2863     break;
2864   case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
2865     //TODO more control messages
2866     if (ntohs (hdr->size) != sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage))
2867     {
2868       GNUNET_break (0);
2869       /* FIXME: restart SUID process */
2870       return;
2871     }
2872     cm = (const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *) hdr;
2873     plugin->mac_address = cm->mac;
2874     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2875                      "Received WLAN_HELPER_CONTROL message with transport of address %s\n",
2876                      wlan_plugin_address_to_string (cls, &plugin->mac_address,
2877                                                     sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)));
2878     plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2879                                  &plugin->mac_address,
2880                                  sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress));
2881     break;
2882   default:
2883     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2884                      "Func wlan_process_helper got unknown message with number %u, size %u\n",
2885                      ntohs (hdr->type), ntohs (hdr->size));
2886     GNUNET_break (0);
2887     return;
2888   }
2889 }
2890
2891
2892
2893 /**
2894  * Exit point from the plugin.
2895  * @param cls pointer to the api struct
2896  */
2897 //FIXME cleanup
2898 void *
2899 libgnunet_plugin_transport_wlan_done (void *cls)
2900 {
2901   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2902   struct Plugin *plugin = api->cls;
2903   struct MacEndpoint *endpoint;
2904   struct MacEndpoint *endpoint_next;
2905
2906   if (NULL == plugin)
2907   {
2908     GNUNET_free (api);
2909     return NULL;
2910   }
2911   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2912                    "libgnunet_plugin_transport_wlan_done started\n");
2913   wlan_transport_stop_wlan_helper (plugin);
2914
2915   GNUNET_assert (cls != NULL);
2916   //free sessions
2917   endpoint = plugin->mac_head;
2918   while (endpoint != NULL)
2919   {
2920     endpoint_next = endpoint->next;
2921     free_macendpoint (plugin, endpoint);
2922     endpoint = endpoint_next;
2923
2924   }
2925   if (plugin->suid_tokenizer != NULL)
2926     GNUNET_SERVER_mst_destroy (plugin->suid_tokenizer);
2927
2928   if (plugin->data_tokenizer != NULL)
2929     GNUNET_SERVER_mst_destroy (plugin->data_tokenizer);
2930
2931   GNUNET_free_non_null (plugin->interface);
2932   GNUNET_free (plugin);
2933   GNUNET_free (api);
2934   return NULL;
2935 }
2936
2937
2938 /**
2939  * Entry point for the plugin.
2940  *
2941  * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2942  * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2943  */
2944 void *
2945 libgnunet_plugin_transport_wlan_init (void *cls)
2946 {
2947   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2948   struct GNUNET_TRANSPORT_PluginFunctions *api;
2949   struct Plugin *plugin;
2950
2951   if (NULL == env->receive)
2952   {
2953     /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2954        initialze the plugin or the API */
2955     api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2956     api->cls = NULL;
2957     api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2958     api->address_to_string = &wlan_plugin_address_to_string;
2959     api->string_to_address = NULL; // FIXME!
2960     return api;
2961   }
2962
2963   plugin = GNUNET_malloc (sizeof (struct Plugin));
2964   plugin->env = env;
2965   plugin->pendingsessions = 0;
2966   GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
2967                          plugin->pendingsessions, GNUNET_NO);
2968   plugin->mac_count = 0;
2969   GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan mac endpoints"),
2970                          plugin->mac_count, GNUNET_NO);
2971   plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
2972   plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2973   plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
2974   GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
2975                                  GNUNET_BANDWIDTH_value_init (100 * 1024 *
2976                                                               1024 / 8), 100);
2977
2978   plugin->suid_tokenizer =
2979       GNUNET_SERVER_mst_create (&wlan_process_helper, plugin);
2980
2981   plugin->data_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
2982
2983   //plugin->sessions = GNUNET_malloc (sizeof (struct Sessionqueue));
2984   //plugin->pending_Sessions_head = GNUNET_malloc (sizeof (struct Sessionqueue));
2985
2986   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2987   api->cls = plugin;
2988   api->send = &wlan_plugin_send;
2989   api->get_session = &wlan_plugin_get_session;
2990   api->disconnect = &wlan_plugin_disconnect;
2991   api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2992   api->check_address = &wlan_plugin_address_suggested;
2993   api->address_to_string = &wlan_plugin_address_to_string;
2994
2995   //read config
2996
2997   if (GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "TESTMODE"))
2998   {
2999     if (GNUNET_SYSERR ==
3000         GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-wlan",
3001                                                "TESTMODE", &(plugin->testmode)))
3002       plugin->testmode = 0;     //default value
3003   }
3004
3005   if (GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "INTERFACE"))
3006   {
3007     if (GNUNET_CONFIGURATION_get_value_string
3008         (env->cfg, "transport-wlan", "INTERFACE",
3009          &(plugin->interface)) != GNUNET_YES)
3010     {
3011       libgnunet_plugin_transport_wlan_done (api);
3012       return NULL;
3013     }
3014   }
3015
3016   //start the plugin
3017   wlan_transport_start_wlan_helper (plugin);
3018   set_next_beacon_time (plugin);
3019   set_next_send (plugin);
3020   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
3021                    "wlan init finished\n");
3022   return api;
3023 }
3024
3025
3026
3027
3028 /* end of plugin_transport_wlan.c */