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