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