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