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