084fc611c9b12bee9c065ad204343166cc36e066
[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 /**
43  * DEBUG switch
44  */
45 #define DEBUG_WLAN GNUNET_EXTRA_LOGGING
46
47
48 #define PROTOCOL_PREFIX "wlan"
49
50 #define PLUGIN_LOG_NAME "wlan-plugin"
51
52 /**
53  * Max size of packet
54  */
55 #define WLAN_MTU 1430
56
57 /**
58  * time out of a session
59  */
60 #define SESSION_TIMEOUT GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT
61
62 /**
63  * time out of a mac endpoint
64  */
65 #define MACENDPOINT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 2)
66
67 /**
68  * scaling factor for hello beacon
69  */
70 #define HELLO_BEACON_SCALING_FACTOR 30
71
72 /**
73  * scaling factor for restarting the helper
74  */
75 #define HELPER_RESTART_SCALING_FACTOR 2
76
77 /**
78  * max size of fragment queue
79  */
80 #define FRAGMENT_QUEUE_SIZE 10
81 /**
82  * max messages in fragment queue per session/client
83  */
84 #define FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION 1
85
86 /**
87  * max messages in fragment queue per MAC
88  */
89 #define FRAGMENT_QUEUE_MESSAGES_OUT_PER_MACENDPOINT 1
90
91 /**
92  * max messages in in queue
93  */
94 #define MESSAGES_IN_QUEUE_SIZE 10
95 /**
96  * max messages in in queue per session/client
97  */
98 #define MESSAGES_IN_DEFRAG_QUEUE_PER_MAC 1
99
100 /**
101  * LLC fields for better compatibility
102  */
103 #define WLAN_LLC_DSAP_FIELD 0x1f
104 #define WLAN_LLC_SSAP_FIELD 0x1f
105
106
107 #define IEEE80211_ADDR_LEN      6       /* size of 802.11 address */
108
109 #define IEEE80211_FC0_VERSION_MASK              0x03
110 #define IEEE80211_FC0_VERSION_SHIFT             0
111 #define IEEE80211_FC0_VERSION_0                 0x00
112 #define IEEE80211_FC0_TYPE_MASK                 0x0c
113 #define IEEE80211_FC0_TYPE_SHIFT                2
114 #define IEEE80211_FC0_TYPE_MGT                  0x00
115 #define IEEE80211_FC0_TYPE_CTL                  0x04
116 #define IEEE80211_FC0_TYPE_DATA                 0x08
117
118 GNUNET_NETWORK_STRUCT_BEGIN
119
120 /*
121  * generic definitions for IEEE 802.11 frames
122  */
123 struct ieee80211_frame
124 {
125   u_int8_t i_fc[2];
126   u_int8_t i_dur[2];
127   u_int8_t i_addr1[IEEE80211_ADDR_LEN];
128   u_int8_t i_addr2[IEEE80211_ADDR_LEN];
129   u_int8_t i_addr3[IEEE80211_ADDR_LEN];
130   u_int8_t i_seq[2];
131   u_int8_t llc[4];
132 } GNUNET_PACKED;
133 GNUNET_NETWORK_STRUCT_END
134
135 /**
136  * Encapsulation of all of the state of the plugin.
137  */
138 struct Plugin
139 {
140   /**
141    * Our environment.
142    */
143   struct GNUNET_TRANSPORT_PluginEnvironment *env;
144
145   /**
146    * List of open connections. head
147    */
148   struct MacEndpoint *mac_head;
149
150   /**
151    * List of open connections. tail
152    */
153   struct MacEndpoint *mac_tail;
154
155   /**
156    * Number of connections
157    */
158   unsigned int mac_count;
159
160   /**
161    * encapsulation of data from the local wlan helper program
162    */
163   struct GNUNET_SERVER_MessageStreamTokenizer *suid_tokenizer;
164
165   /**
166    * encapsulation of packets received from the wlan helper
167    */
168   struct GNUNET_SERVER_MessageStreamTokenizer *data_tokenizer;
169
170   /**
171    * stdout pipe handle for the gnunet-helper-transport-wlan process
172    */
173   struct GNUNET_DISK_PipeHandle *server_stdout;
174
175   /**
176    * stdout file handle for the gnunet-helper-transport-wlan process
177    */
178   const struct GNUNET_DISK_FileHandle *server_stdout_handle;
179
180   /**
181    * stdin pipe handle for the gnunet-helper-transport-wlan process
182    */
183   struct GNUNET_DISK_PipeHandle *server_stdin;
184
185   /**
186    * stdin file handle for the gnunet-helper-transport-wlan process
187    */
188   const struct GNUNET_DISK_FileHandle *server_stdin_handle;
189
190   /**
191    * ID of the gnunet-wlan-server std read task
192    */
193   GNUNET_SCHEDULER_TaskIdentifier server_read_task;
194
195   /**
196    * ID of the gnunet-wlan-server std read task
197    */
198   GNUNET_SCHEDULER_TaskIdentifier server_write_task;
199
200   /**
201    * ID of the delay task for writing
202    */
203   GNUNET_SCHEDULER_TaskIdentifier server_write_delay_task;
204
205   /**
206    * The process id of the wlan process
207    */
208   struct GNUNET_OS_Process *server_proc;
209
210   /**
211    * The interface of the wlan card given to us by the user.
212    */
213   char *interface;
214
215   /**
216    * Mode of operation for the helper, 0 = normal, 1 = first loopback, 2 = second loopback
217    */
218   long long unsigned int testmode;
219
220   /**
221    * The mac_address of the wlan card given to us by the helper.
222    */
223   struct GNUNET_TRANSPORT_WLAN_MacAddress mac_address;
224
225   /**
226    * Sessions currently pending for transmission
227    * to a peer, if any.
228    */
229   struct Sessionqueue *pending_Sessions_head;
230
231   /**
232    * Sessions currently pending for transmission
233    * to a peer (tail), if any.
234    */
235   struct Sessionqueue *pending_Sessions_tail;
236
237   /**
238    * number of pending sessions
239    */
240   unsigned int pendingsessions;
241
242   /**
243    * Messages in the sending queues
244    */
245   int pending_Fragment_Messages;
246
247   /**
248    * messages ready for send, head
249    */
250   struct FragmentMessage_queue *sending_messages_head;
251   /**
252    * messages ready for send, tail
253    */
254   struct FragmentMessage_queue *sending_messages_tail;
255   /**
256    * time of the next "hello-beacon"
257    */
258   struct GNUNET_TIME_Absolute beacon_time;
259
260   /**
261    * queue to send acks for received fragments (head)
262    */
263   struct AckSendQueue *ack_send_queue_head;
264
265   /**
266    * queue to send acks for received fragments (tail)
267    */
268   struct AckSendQueue *ack_send_queue_tail;
269
270   /**
271    * Tracker for bandwidth limit
272    */
273   struct GNUNET_BANDWIDTH_Tracker tracker;
274
275   /**
276    * saves the current state of the helper process
277    */
278   int helper_is_running;
279 };
280
281 /**
282  * Struct to store data if file write did not accept the whole packet
283  */
284 struct Finish_send
285 {
286   /**
287    * pointer to the global plugin struct
288    */
289   struct Plugin *plugin;
290
291   /**
292    * head of the next part to send to the helper
293    */
294   char *head_of_next_write;
295
296   /**
297    * Start of the message to send, needed for free
298    */
299   struct GNUNET_MessageHeader *msgstart;
300
301   /**
302    * rest size to send
303    */
304   ssize_t size;
305 };
306
307 /**
308  * Queue of sessions, for the general session queue and the pending session queue
309  */
310 //TODO DOXIGEN
311 struct Sessionqueue
312 {
313   struct Sessionqueue *next;
314   struct Sessionqueue *prev;
315   struct Session *content;
316 #if !HAVE_UNALIGNED_64_ACCESS
317   void *dummy;                  /* for alignment, see #1909 */
318 #endif
319 };
320
321 /**
322  * Queue of fragmented messages, for the sending queue of the plugin
323  */
324 //TODO DOXIGEN
325 struct FragmentMessage_queue
326 {
327   struct FragmentMessage_queue *next;
328   struct FragmentMessage_queue *prev;
329   struct FragmentMessage *content;
330 };
331
332 /**
333  * Queue for the fragments received
334  */
335 //TODO DOXIGEN
336 struct Receive_Fragment_Queue
337 {
338   struct Receive_Fragment_Queue *next;
339   struct Receive_Fragment_Queue *prev;
340   uint16_t num;
341   const char *msg;
342   uint16_t size;
343   struct Radiotap_rx rxinfo;
344 };
345
346 //TODO DOXIGEN
347 struct MacEndpoint_id_fragment_triple
348 {
349   struct MacEndpoint *endpoint;
350   uint32_t message_id;
351   struct FragmentMessage *fm;
352 };
353
354 //TODO DOXIGEN
355 struct Plugin_Session_pair
356 {
357   struct Plugin *plugin;
358   struct Session *session;
359 };
360
361
362 GNUNET_NETWORK_STRUCT_BEGIN
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 GNUNET_NETWORK_STRUCT_END
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 GNUNET_TRANSPORT_WLAN_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 GNUNET_TRANSPORT_WLAN_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 GNUNET_TRANSPORT_WLAN_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 GNUNET_TRANSPORT_WLAN_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 GNUNET_TRANSPORT_WLAN_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 GNUNET_TRANSPORT_WLAN_MacAddress *mac;
836
837   if (addrlen != sizeof (struct GNUNET_TRANSPORT_WLAN_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   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
915                    "New session %p with endpoint %p: %s\n", queue->content,
916                    endpoint, wlan_plugin_address_to_string (NULL,
917                                                             endpoint->addr.mac,
918                                                             6));
919   return queue->content;
920 }
921
922 /**
923  * Get session from address, create if no session exists
924  *
925  * @param plugin pointer to the plugin struct
926  * @param addr pointer to the mac address of the peer
927  * @param peer pointer to the peerid
928  * @return returns the session
929  */
930 static struct Session *
931 get_session (struct Plugin *plugin, const struct GNUNET_TRANSPORT_WLAN_MacAddress *addr,
932              const struct GNUNET_PeerIdentity *peer)
933 {
934   struct MacEndpoint *mac;
935
936   mac = get_macendpoint (plugin, addr, GNUNET_YES);
937   struct Session *session = search_session (plugin, mac, peer);
938
939   if (session != NULL)
940     return session;
941   return create_session (plugin, mac, peer);
942 }
943
944 /**
945  * Queue the session to send data
946  * checks if there is a message pending
947  * checks if this session is not allready in the queue
948  * @param plugin pointer to the plugin
949  * @param session pointer to the session to add
950  */
951 static void
952 queue_session (struct Plugin *plugin, struct Session *session)
953 {
954   struct Sessionqueue *queue = plugin->pending_Sessions_head;
955
956   if (session->pending_message_head != NULL)
957   {
958     while (queue != NULL)
959     {
960       // content is never NULL
961       GNUNET_assert (queue->content != NULL);
962       // is session already in queue?
963       if (session == queue->content)
964       {
965         return;
966       }
967       // try next
968       queue = queue->next;
969     }
970
971     // Session is not in the queue
972
973     queue = GNUNET_malloc (sizeof (struct Sessionqueue));
974     queue->content = session;
975
976     //insert at the tail
977     GNUNET_CONTAINER_DLL_insert_tail (plugin->pending_Sessions_head,
978                                       plugin->pending_Sessions_tail, queue);
979     plugin->pendingsessions++;
980     GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
981                            plugin->pendingsessions, GNUNET_NO);
982   }
983
984 }
985
986 /**
987  * Function to schedule the write task, executed after a delay
988  * @param cls pointer to the plugin struct
989  * @param tc GNUNET_SCHEDULER_TaskContext pointer
990  */
991 static void
992 delay_fragment_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
993 {
994   struct Plugin *plugin = cls;
995
996   plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
997
998   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
999     return;
1000
1001   // GNUNET_TIME_UNIT_FOREVER_REL is needed to clean up old msg
1002   if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
1003   {
1004     plugin->server_write_task =
1005         GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1006                                          plugin->server_stdin_handle,
1007                                          &do_transmit, plugin);
1008   }
1009 }
1010
1011 /**
1012  * Function to calculate the time of the next periodic "hello-beacon"
1013  * @param plugin pointer to the plugin struct
1014  */
1015 static void
1016 set_next_beacon_time (struct Plugin *const plugin)
1017 {
1018   //under 10 known peers: once a second
1019   if (plugin->mac_count < 10)
1020   {
1021     plugin->beacon_time =
1022         GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1023                                   GNUNET_TIME_relative_multiply
1024                                   (GNUNET_TIME_UNIT_SECONDS,
1025                                    HELLO_BEACON_SCALING_FACTOR));
1026   }
1027   //under 30 known peers: every 10 seconds
1028   else if (plugin->mac_count < 30)
1029   {
1030     plugin->beacon_time =
1031         GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1032                                   GNUNET_TIME_relative_multiply
1033                                   (GNUNET_TIME_UNIT_SECONDS,
1034                                    10 * HELLO_BEACON_SCALING_FACTOR));
1035   }
1036   //over 30 known peers: once a minute
1037   else
1038   {
1039     plugin->beacon_time =
1040         GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1041                                   GNUNET_TIME_relative_multiply
1042                                   (GNUNET_TIME_UNIT_MINUTES,
1043                                    HELLO_BEACON_SCALING_FACTOR));
1044   }
1045 }
1046
1047 /**
1048  * Function to set the timer for the next timeout of the fragment queue
1049  * @param plugin the handle to the plugin struct
1050  */
1051 static void
1052 set_next_send (struct Plugin *const plugin)
1053 {
1054   struct GNUNET_TIME_Relative next_send;
1055
1056   //abort if helper is not running
1057   if (plugin->helper_is_running == GNUNET_NO)
1058   {
1059     return;
1060   }
1061
1062   //cancel old task
1063   if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
1064   {
1065     GNUNET_SCHEDULER_cancel (plugin->server_write_delay_task);
1066     plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
1067   }
1068
1069   //check if some acks are in the queue
1070   if (plugin->ack_send_queue_head != NULL)
1071   {
1072     next_send = GNUNET_TIME_UNIT_ZERO;
1073   }
1074
1075   //check if there are some fragments in the queue
1076   else if (plugin->sending_messages_head != NULL)
1077   {
1078     next_send = GNUNET_TIME_UNIT_ZERO;
1079   }
1080   else
1081   {
1082     next_send = GNUNET_TIME_absolute_get_remaining (plugin->beacon_time);
1083   }
1084
1085   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1086                    "Next packet is send in: %u\n", next_send.rel_value);
1087   if (next_send.rel_value == GNUNET_TIME_UNIT_ZERO.rel_value)
1088   {
1089     if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
1090     {
1091       plugin->server_write_task =
1092           GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1093                                            plugin->server_stdin_handle,
1094                                            &do_transmit, plugin);
1095     }
1096   }
1097   else
1098   {
1099     if (plugin->server_write_delay_task == GNUNET_SCHEDULER_NO_TASK)
1100     {
1101       plugin->server_write_delay_task =
1102           GNUNET_SCHEDULER_add_delayed (next_send, &delay_fragment_task,
1103                                         plugin);
1104     }
1105   }
1106 }
1107
1108 /**
1109  * Function to get the next queued Session, removes the session from the queue
1110  * @param plugin pointer to the plugin struct
1111  * @return pointer to the session found, returns NULL if there is now session in the queue
1112  */
1113 static struct Session *
1114 get_next_queue_session (struct Plugin *plugin)
1115 {
1116   struct Session *session;
1117   struct Sessionqueue *sessionqueue;
1118   struct Sessionqueue *sessionqueue_alt;
1119   struct PendingMessage *pm;
1120
1121   sessionqueue = plugin->pending_Sessions_head;
1122
1123   while (sessionqueue != NULL)
1124   {
1125     session = sessionqueue->content;
1126
1127     GNUNET_assert (session != NULL);
1128     pm = session->pending_message_head;
1129
1130     if (pm == NULL)
1131     {
1132       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1133                        "pending message is empty, should not happen. session %p\n",
1134                        session);
1135       sessionqueue_alt = sessionqueue;
1136       sessionqueue = sessionqueue->next;
1137       plugin->pendingsessions--;
1138       GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
1139                              plugin->pendingsessions, GNUNET_NO);
1140       GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1141                                    plugin->pending_Sessions_tail,
1142                                    sessionqueue_alt);
1143
1144       GNUNET_free (sessionqueue_alt);
1145       continue;
1146
1147     }
1148
1149     //check for message timeout
1150     if (GNUNET_TIME_absolute_get_remaining (pm->timeout).rel_value > 0)
1151     {
1152       //check if session has no message in the fragment queue
1153       if ((session->mac->fragment_messages_out_count <
1154            FRAGMENT_QUEUE_MESSAGES_OUT_PER_MACENDPOINT) &&
1155           (session->fragment_messages_out_count <
1156            FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION))
1157       {
1158         plugin->pendingsessions--;
1159         GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
1160                                plugin->pendingsessions, GNUNET_NO);
1161         GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1162                                      plugin->pending_Sessions_tail,
1163                                      sessionqueue);
1164         GNUNET_free (sessionqueue);
1165
1166         return session;
1167       }
1168       else
1169       {
1170         sessionqueue = sessionqueue->next;
1171       }
1172     }
1173     else
1174     {
1175       GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
1176                                    session->pending_message_tail, pm);
1177
1178       //call the cont func that it did not work
1179       if (pm->transmit_cont != NULL)
1180         pm->transmit_cont (pm->transmit_cont_cls, &(session->target),
1181                            GNUNET_SYSERR);
1182       GNUNET_free (pm->msg);
1183       GNUNET_free (pm);
1184
1185       if (session->pending_message_head == NULL)
1186       {
1187         sessionqueue_alt = sessionqueue;
1188         sessionqueue = sessionqueue->next;
1189         plugin->pendingsessions--;
1190         GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
1191                                plugin->pendingsessions, GNUNET_NO);
1192         GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1193                                      plugin->pending_Sessions_tail,
1194                                      sessionqueue_alt);
1195
1196         GNUNET_free (sessionqueue_alt);
1197       }
1198     }
1199
1200   }
1201   return NULL;
1202 }
1203
1204 /**
1205  * frees the space of a message in the fragment queue (send queue)
1206  * @param plugin the plugin struct
1207  * @param fm message to free
1208  */
1209 static void
1210 free_fragment_message (struct Plugin *plugin, struct FragmentMessage *fm)
1211 {
1212   struct Session *session = fm->session;
1213   struct MacEndpoint *endpoint = session->mac;
1214   struct FragmentMessage_queue *fmq;
1215   struct FragmentMessage_queue *fmq_next;
1216
1217   fmq = plugin->sending_messages_head;
1218   while (fmq != NULL)
1219   {
1220     fmq_next = fmq->next;
1221     if (fmq->content == fm)
1222     {
1223       GNUNET_CONTAINER_DLL_remove (plugin->sending_messages_head,
1224                                    plugin->sending_messages_tail, fmq);
1225       GNUNET_free (fmq);
1226     }
1227     fmq = fmq_next;
1228   }
1229
1230   (session->mac->fragment_messages_out_count)--;
1231   session->fragment_messages_out_count--;
1232   plugin->pending_Fragment_Messages--;
1233   GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending fragments"),
1234                          plugin->pending_Fragment_Messages, GNUNET_NO);
1235   GNUNET_CONTAINER_DLL_remove (endpoint->sending_messages_head,
1236                                endpoint->sending_messages_tail, fm);
1237   GNUNET_FRAGMENT_context_destroy (fm->fragcontext);
1238   if (fm->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1239   {
1240     GNUNET_SCHEDULER_cancel (fm->timeout_task);
1241     fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1242   }
1243
1244   GNUNET_free (fm);
1245
1246   queue_session (plugin, session);
1247   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1248                    "Free pending fragment messages %p, session %p\n", fm,
1249                    session);
1250 }
1251
1252 /**
1253  * function to fill the radiotap header
1254  * @param plugin pointer to the plugin struct
1255  * @param endpoint pointer to the endpoint
1256  * @param header pointer to the radiotap header
1257  * @return GNUNET_YES at success
1258  */
1259 static int
1260 getRadiotapHeader (struct Plugin *plugin, struct MacEndpoint *endpoint,
1261                    struct Radiotap_Send *header)
1262 {
1263
1264   if (endpoint != NULL)
1265   {
1266     header->rate = endpoint->rate;
1267     header->tx_power = endpoint->tx_power;
1268     header->antenna = endpoint->antenna;
1269   }
1270   else
1271   {
1272     header->rate = 255;
1273     header->tx_power = 0;
1274     header->antenna = 0;
1275   }
1276
1277   return GNUNET_YES;
1278 }
1279
1280 /**
1281  * function to generate the wlan hardware header for one packet
1282  * @param Header address to write the header to
1283  * @param to_mac_addr address of the recipient
1284  * @param plugin pointer to the plugin struct
1285  * @param size size of the whole packet, needed to calculate the time to send the packet
1286  * @return GNUNET_YES if there was no error
1287  */
1288 static int
1289 getWlanHeader (struct ieee80211_frame *Header,
1290                const struct GNUNET_TRANSPORT_WLAN_MacAddress *to_mac_addr, struct Plugin *plugin,
1291                unsigned int size)
1292 {
1293   uint16_t *tmp16;
1294   const int rate = 11000000;
1295
1296   Header->i_fc[0] = IEEE80211_FC0_TYPE_DATA;
1297   Header->i_fc[1] = 0x00;
1298   memcpy (&Header->i_addr3, &mac_bssid_gnunet, sizeof (mac_bssid_gnunet));
1299   memcpy (&Header->i_addr2, plugin->mac_address.mac,
1300           sizeof (plugin->mac_address));
1301   memcpy (&Header->i_addr1, to_mac_addr, sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress));
1302
1303   tmp16 = (uint16_t *) Header->i_dur;
1304   *tmp16 = (uint16_t) GNUNET_htole16 ((size * 1000000) / rate + 290);
1305   Header->llc[0] = WLAN_LLC_DSAP_FIELD;
1306   Header->llc[1] = WLAN_LLC_SSAP_FIELD;
1307
1308   return GNUNET_YES;
1309 }
1310
1311
1312 /**
1313  * function to add a fragment of a message to send
1314  * @param cls FragmentMessage this message belongs to
1315  * @param hdr pointer to the start of the message
1316  */
1317 static void
1318 add_message_for_send (void *cls, const struct GNUNET_MessageHeader *hdr)
1319 {
1320
1321   struct FragmentMessage *fm = cls;
1322   struct FragmentMessage_queue *fmqueue;
1323
1324   GNUNET_assert (cls != NULL);
1325   GNUNET_assert (fm->frag == NULL);
1326   struct MacEndpoint *endpoint = fm->session->mac;
1327   struct Plugin *plugin = endpoint->plugin;
1328   struct GNUNET_MessageHeader *msgheader;
1329   struct GNUNET_MessageHeader *msgheader2;
1330   uint16_t size;
1331
1332   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1333                    "Adding fragment of message %p to send, session %p, endpoint %p, type %u\n",
1334                    fm, fm->session, endpoint, hdr->type);
1335   size =
1336       sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1337       sizeof (struct ieee80211_frame) + ntohs (hdr->size);
1338   fm->frag = GNUNET_malloc (size);
1339   fm->size = size;
1340
1341   msgheader = (struct GNUNET_MessageHeader *) fm->frag;
1342   msgheader->size = htons (size);
1343   msgheader->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1344
1345   fm->radioHeader = (struct Radiotap_Send *) &msgheader[1];
1346   fm->ieeewlanheader = (struct ieee80211_frame *) &fm->radioHeader[1];
1347   msgheader2 = (struct GNUNET_MessageHeader *) &fm->ieeewlanheader[1];
1348   memcpy (msgheader2, hdr, ntohs (hdr->size));
1349
1350   fmqueue = GNUNET_malloc (sizeof (struct FragmentMessage_queue));
1351   fmqueue->content = fm;
1352
1353   GNUNET_CONTAINER_DLL_insert_tail (plugin->sending_messages_head,
1354                                     plugin->sending_messages_tail, fmqueue);
1355   set_next_send (plugin);
1356 }
1357
1358
1359 /**
1360  * We have been notified that gnunet-helper-transport-wlan has written something to stdout.
1361  * Handle the output, then reschedule this function to be called again once
1362  * more is available.
1363  *
1364  * @param cls the plugin handle
1365  * @param tc the scheduling context
1366  */
1367 static void
1368 wlan_plugin_helper_read (void *cls,
1369                          const struct GNUNET_SCHEDULER_TaskContext *tc)
1370 {
1371   struct Plugin *plugin = cls;
1372
1373   plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
1374
1375   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1376     return;
1377
1378   char mybuf[WLAN_MTU + sizeof (struct GNUNET_MessageHeader)];
1379   ssize_t bytes;
1380
1381   bytes =
1382       GNUNET_DISK_file_read (plugin->server_stdout_handle, mybuf,
1383                              sizeof (mybuf));
1384   if (bytes <= 0)
1385   {
1386     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1387                      _
1388                      ("Finished reading from gnunet-helper-transport-wlan stdout with code: %d\n"),
1389                      bytes);
1390     return;
1391   }
1392   GNUNET_SERVER_mst_receive (plugin->suid_tokenizer, NULL, mybuf, bytes,
1393                              GNUNET_NO, GNUNET_NO);
1394
1395   GNUNET_assert (plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
1396   plugin->server_read_task =
1397       GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1398                                       plugin->server_stdout_handle,
1399                                       &wlan_plugin_helper_read, plugin);
1400 }
1401
1402 /**
1403  * Start the gnunet-helper-transport-wlan process.
1404  *
1405  * @param plugin the transport plugin
1406  * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
1407  */
1408 static int
1409 wlan_transport_start_wlan_helper (struct Plugin *plugin)
1410 {
1411   const char *filenamehw = "gnunet-helper-transport-wlan";
1412   const char *filenameloopback = "gnunet-helper-transport-wlan-dummy";
1413   char *absolute_filename = NULL;
1414
1415   if (plugin->helper_is_running == GNUNET_YES)
1416   {
1417     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1418                      "wlan_transport_start_wlan_helper not needed, helper already running!");
1419     return GNUNET_YES;
1420   }
1421
1422   plugin->server_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
1423   if (plugin->server_stdout == NULL)
1424     return GNUNET_SYSERR;
1425
1426   plugin->server_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
1427   if (plugin->server_stdin == NULL)
1428     return GNUNET_SYSERR;
1429
1430   if ((plugin->testmode == 1) || (plugin->testmode == 2))
1431   {
1432     if (GNUNET_OS_check_helper_binary (filenameloopback) == GNUNET_YES)
1433     {
1434       absolute_filename = GNUNET_strdup (filenameloopback);
1435     }
1436     else
1437     {
1438       char cwd[FILENAME_MAX];
1439
1440       GNUNET_assert (getcwd (cwd, sizeof (cwd)) != NULL);
1441
1442       GNUNET_asprintf (&absolute_filename, "%s%s%s", cwd, DIR_SEPARATOR_STR,
1443                        filenameloopback);
1444
1445       if (GNUNET_DISK_file_test (filenameloopback) != GNUNET_YES)
1446       {
1447         GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1448                          "Helper `%s' not found! %i\n", absolute_filename);
1449         GNUNET_break (0);
1450       }
1451     }
1452   }
1453
1454   /* Start the server process */
1455
1456   if (plugin->testmode == 0)
1457   {
1458
1459     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1460                      "Starting gnunet-helper-transport-wlan process cmd: %s %s %i\n",
1461                      filenamehw, plugin->interface, plugin->testmode);
1462     if (GNUNET_OS_check_helper_binary (filenamehw) == GNUNET_YES)
1463     {
1464       plugin->server_proc =
1465           GNUNET_OS_start_process (GNUNET_NO, plugin->server_stdin, plugin->server_stdout,
1466                                    filenamehw, filenamehw, plugin->interface,
1467                                    NULL);
1468     }
1469     else if (GNUNET_OS_check_helper_binary (filenamehw) == GNUNET_NO)
1470     {
1471       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1472                        "gnunet-helper-transport-wlan is not suid, please change it or look at the doku\n");
1473       GNUNET_break (0);
1474     }
1475     else
1476     {
1477       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1478                        "gnunet-helper-transport-wlan not found, please look if it exists and is the $PATH variable!\n");
1479       GNUNET_break (0);
1480     }
1481
1482   }
1483   else if (plugin->testmode == 1)
1484   {
1485
1486     GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
1487                      "Starting gnunet-helper-transport-wlan-dummy loopback 1 process cmd: %s %s %i\n",
1488                      absolute_filename, plugin->interface, plugin->testmode);
1489     plugin->server_proc =
1490         GNUNET_OS_start_process (GNUNET_NO, plugin->server_stdin, plugin->server_stdout,
1491                                  absolute_filename, absolute_filename, "1",
1492                                  NULL);
1493     if (plugin->server_proc == NULL)
1494     {
1495       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1496                        "`%s' not found, please look if it exists and is in the $PATH variable!\n",
1497                        absolute_filename);
1498       GNUNET_break (0);
1499     }
1500   }
1501   else if (plugin->testmode == 2)
1502   {
1503     GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
1504                      "Starting gnunet-helper-transport-wlan-dummy loopback 2 process cmd: %s %s %i\n",
1505                      absolute_filename, plugin->interface, plugin->testmode);
1506     plugin->server_proc =
1507         GNUNET_OS_start_process (GNUNET_NO, plugin->server_stdin, plugin->server_stdout,
1508                                  absolute_filename, absolute_filename, "2",
1509                                  NULL);
1510     if (plugin->server_proc == NULL)
1511     {
1512       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1513                        "`%s' not found, please look if it exists and is in the $PATH variable!\n",
1514                        absolute_filename);
1515       GNUNET_break (0);
1516     }
1517   }
1518   if (absolute_filename != NULL)
1519     GNUNET_free (absolute_filename);
1520   if (plugin->server_proc == NULL)
1521   {
1522     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1523                      "Failed to start gnunet-helper-transport-wlan process\n");
1524     return GNUNET_SYSERR;
1525   }
1526
1527
1528
1529   /* Close the write end of the read pipe */
1530   GNUNET_DISK_pipe_close_end (plugin->server_stdout,
1531                               GNUNET_DISK_PIPE_END_WRITE);
1532
1533   /* Close the read end of the write pipe */
1534   GNUNET_DISK_pipe_close_end (plugin->server_stdin, GNUNET_DISK_PIPE_END_READ);
1535
1536   plugin->server_stdout_handle =
1537       GNUNET_DISK_pipe_handle (plugin->server_stdout,
1538                                GNUNET_DISK_PIPE_END_READ);
1539   plugin->server_stdin_handle =
1540       GNUNET_DISK_pipe_handle (plugin->server_stdin,
1541                                GNUNET_DISK_PIPE_END_WRITE);
1542
1543   GNUNET_assert (plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
1544
1545   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1546                    "Adding server_read_task for the gnunet-helper-transport-wlan\n");
1547   plugin->server_read_task =
1548       GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1549                                       plugin->server_stdout_handle,
1550                                       &wlan_plugin_helper_read, plugin);
1551
1552   plugin->helper_is_running = GNUNET_YES;
1553   return GNUNET_YES;
1554 }
1555
1556 /**
1557  * Stops the gnunet-helper-transport-wlan process.
1558  *
1559  * @param plugin the transport plugin
1560  * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
1561  */
1562 static int
1563 wlan_transport_stop_wlan_helper (struct Plugin *plugin)
1564 {
1565   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1566                    "Stoping WLAN helper process\n");
1567
1568   if (plugin->helper_is_running == GNUNET_NO)
1569   {
1570     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1571                      "wlan_transport_stop_wlan_helper not needed, helper already stopped!");
1572     return GNUNET_YES;
1573   }
1574
1575   if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
1576   {
1577     GNUNET_SCHEDULER_cancel (plugin->server_write_delay_task);
1578     plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
1579   }
1580
1581   if (plugin->server_write_task != GNUNET_SCHEDULER_NO_TASK)
1582   {
1583     GNUNET_SCHEDULER_cancel (plugin->server_write_task);
1584     plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1585   }
1586
1587   if (plugin->server_read_task != GNUNET_SCHEDULER_NO_TASK)
1588   {
1589     GNUNET_SCHEDULER_cancel (plugin->server_read_task);
1590     plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
1591   }
1592
1593   GNUNET_DISK_pipe_close (plugin->server_stdout);
1594   GNUNET_DISK_pipe_close (plugin->server_stdin);
1595   GNUNET_OS_process_kill (plugin->server_proc, SIGKILL);
1596   GNUNET_OS_process_wait (plugin->server_proc);
1597   GNUNET_OS_process_close (plugin->server_proc);
1598
1599   plugin->helper_is_running = GNUNET_NO;
1600
1601   return GNUNET_YES;
1602 }
1603
1604 /**
1605  * function for delayed restart of the helper process
1606  * @param cls Finish_send struct if message should be finished
1607  * @param tc TaskContext
1608  */
1609 static void
1610 delay_restart_helper (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1611 {
1612   struct Finish_send *finish = cls;
1613   struct Plugin *plugin;
1614
1615   plugin = finish->plugin;
1616
1617   plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1618   if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1619   {
1620     GNUNET_free_non_null (finish->msgstart);
1621     GNUNET_free (finish);
1622     return;
1623   }
1624
1625   wlan_transport_start_wlan_helper (plugin);
1626
1627   if (finish->size != 0)
1628   {
1629     plugin->server_write_task =
1630         GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1631                                          plugin->server_stdin_handle,
1632                                          &finish_sending, finish);
1633   }
1634   else
1635   {
1636     set_next_send (plugin);
1637     GNUNET_free_non_null (finish->msgstart);
1638     GNUNET_free (finish);
1639   }
1640
1641 }
1642
1643 /**
1644  * Function to restart the helper
1645  * @param plugin pointer to the global plugin struct
1646  * @param finish pointer to the Finish_send struct to finish
1647  */
1648 static void
1649 restart_helper (struct Plugin *plugin, struct Finish_send *finish)
1650 {
1651   static struct GNUNET_TIME_Relative next_try = { 1000 };
1652   GNUNET_assert (finish != NULL);
1653
1654   wlan_transport_stop_wlan_helper (plugin);
1655   plugin->server_write_task =
1656       GNUNET_SCHEDULER_add_delayed (next_try, &delay_restart_helper, finish);
1657   GNUNET_TIME_relative_multiply (next_try, HELPER_RESTART_SCALING_FACTOR);
1658
1659 }
1660
1661 /**
1662  * function to finish a sending if not all could have been writen befor
1663  * @param cls pointer to the Finish_send struct
1664  * @param tc TaskContext
1665  */
1666 static void
1667 finish_sending (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1668 {
1669   struct Finish_send *finish = cls;
1670   struct Plugin *plugin;
1671   ssize_t bytes;
1672
1673   plugin = finish->plugin;
1674   plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1675
1676   if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1677   {
1678     GNUNET_free (finish->msgstart);
1679     GNUNET_free (finish);
1680     return;
1681   }
1682   bytes =
1683       GNUNET_DISK_file_write (plugin->server_stdin_handle,
1684                               finish->head_of_next_write, finish->size);
1685
1686   if (bytes != finish->size)
1687   {
1688     if (bytes != GNUNET_SYSERR)
1689     {
1690       finish->head_of_next_write += bytes;
1691       finish->size -= bytes;
1692       plugin->server_write_task =
1693           GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1694                                            plugin->server_stdin_handle,
1695                                            &finish_sending, finish);
1696     }
1697     else
1698     {
1699       restart_helper (plugin, finish);
1700     }
1701   }
1702   else
1703   {
1704     GNUNET_free (finish->msgstart);
1705     GNUNET_free (finish);
1706     set_next_send (plugin);
1707   }
1708 }
1709
1710 /**
1711  * function to send a hello beacon
1712  * @param plugin pointer to the plugin struct
1713  */
1714 static void
1715 send_hello_beacon (struct Plugin *plugin)
1716 {
1717   uint16_t size;
1718   ssize_t bytes;
1719   uint16_t hello_size;
1720   struct GNUNET_MessageHeader *msgheader;
1721   struct ieee80211_frame *ieeewlanheader;
1722   struct Radiotap_Send *radioHeader;
1723   struct GNUNET_MessageHeader *msgheader2;
1724   const struct GNUNET_MessageHeader *hello;
1725   struct Finish_send *finish;
1726
1727   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1728                    "Sending hello beacon\n");
1729
1730   GNUNET_assert (plugin != NULL);
1731
1732   GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan hello beacons send"),
1733                             1, GNUNET_NO);
1734
1735   hello = plugin->env->get_our_hello ();
1736   hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1737   GNUNET_assert (sizeof (struct WlanHeader) + hello_size <= WLAN_MTU);
1738   size =
1739       sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1740       sizeof (struct ieee80211_frame) + hello_size;
1741
1742   msgheader = GNUNET_malloc (size);
1743   msgheader->size = htons (size);
1744   msgheader->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1745
1746   radioHeader = (struct Radiotap_Send *) &msgheader[1];
1747   getRadiotapHeader (plugin, NULL, radioHeader);
1748   ieeewlanheader = (struct ieee80211_frame *) &radioHeader[1];
1749   getWlanHeader (ieeewlanheader, &bc_all_mac, plugin, size);
1750
1751   msgheader2 = (struct GNUNET_MessageHeader *) &ieeewlanheader[1];
1752   /*msgheader2->size =
1753    * htons (GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello) +
1754    * sizeof (struct GNUNET_MessageHeader));
1755    *
1756    * msgheader2->type = htons (GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT); */
1757   memcpy (msgheader2, hello, hello_size);
1758
1759   bytes = GNUNET_DISK_file_write (plugin->server_stdin_handle, msgheader, size);
1760
1761   if (bytes == GNUNET_SYSERR)
1762   {
1763     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1764                      _
1765                      ("Error writing to wlan helper. errno == %d, ERROR: %s\n"),
1766                      errno, strerror (errno));
1767     finish = GNUNET_malloc (sizeof (struct Finish_send));
1768     finish->plugin = plugin;
1769     finish->head_of_next_write = NULL;
1770     finish->size = 0;
1771     finish->msgstart = NULL;
1772     restart_helper (plugin, finish);
1773
1774     set_next_beacon_time (plugin);
1775
1776   }
1777   else
1778   {
1779     GNUNET_assert (bytes == size);
1780     set_next_beacon_time (plugin);
1781     set_next_send (plugin);
1782   }
1783   GNUNET_free (msgheader);
1784
1785
1786 }
1787
1788 /**
1789  * function to add an ack to send it for a received fragment
1790  * @param cls MacEndpoint this ack belongs to
1791  * @param msg_id id of the message
1792  * @param hdr pointer to the hdr where the ack is stored
1793  *
1794  */
1795 static void
1796 add_ack_for_send (void *cls, uint32_t msg_id,
1797                   const struct GNUNET_MessageHeader *hdr)
1798 {
1799
1800   struct AckSendQueue *ack;
1801
1802   GNUNET_assert (cls != NULL);
1803   struct MacEndpoint *endpoint = cls;
1804   struct Plugin *plugin = endpoint->plugin;
1805   struct GNUNET_MessageHeader *msgheader;
1806   struct GNUNET_MessageHeader *msgheader2;
1807   uint16_t size;
1808
1809   size =
1810       sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1811       sizeof (struct ieee80211_frame) + ntohs (hdr->size) +
1812       sizeof (struct AckSendQueue);
1813
1814   ack = GNUNET_malloc (size);
1815   ack->message_id = msg_id;
1816   ack->endpoint = endpoint;
1817
1818   size =
1819       sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1820       sizeof (struct ieee80211_frame) + ntohs (hdr->size);
1821
1822   msgheader = (struct GNUNET_MessageHeader *) &ack[1];
1823   ack->hdr = (struct GNUNET_MessageHeader *) &ack[1];
1824   msgheader->size = htons (size);
1825   msgheader->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1826
1827   ack->radioHeader = (struct Radiotap_Send *) &msgheader[1];
1828   ack->ieeewlanheader = (struct ieee80211_frame *) &(ack->radioHeader)[1];
1829   msgheader2 = (struct GNUNET_MessageHeader *) &(ack->ieeewlanheader)[1];
1830   memcpy (msgheader2, hdr, ntohs (hdr->size));
1831
1832   GNUNET_CONTAINER_DLL_insert_tail (plugin->ack_send_queue_head,
1833                                     plugin->ack_send_queue_tail, ack);
1834
1835   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1836                    "Adding ack with message id %u to send, AckSendQueue %p, endpoint %p\n",
1837                    msg_id, ack, endpoint);
1838   set_next_send (plugin);
1839 }
1840
1841 /**
1842  * Function for the scheduler if a FragmentMessage times out
1843  * @param cls pointer to the FragmentMessage
1844  * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
1845  */
1846 static void
1847 fragmentmessage_timeout (void *cls,
1848                          const struct GNUNET_SCHEDULER_TaskContext *tc)
1849 {
1850   struct FragmentMessage *fm = cls;
1851
1852   GNUNET_assert (fm != NULL);
1853   fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1854   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1855   {
1856     return;
1857   }
1858   free_fragment_message (fm->session->mac->plugin, fm);
1859 }
1860
1861 /**
1862  * Function to check if there is some space in the fragment queue
1863  * inserts a message if space is available
1864  * @param plugin the plugin struct
1865  */
1866
1867 static void
1868 check_fragment_queue (struct Plugin *plugin)
1869 {
1870   struct Session *session;
1871   struct FragmentMessage *fm;
1872   struct GNUNET_PeerIdentity pid;
1873
1874   struct PendingMessage *pm;
1875
1876   if (plugin->pending_Fragment_Messages < FRAGMENT_QUEUE_SIZE)
1877   {
1878     session = get_next_queue_session (plugin);
1879     if (session != NULL)
1880     {
1881       pm = session->pending_message_head;
1882       GNUNET_assert (pm != NULL);
1883       GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
1884                                    session->pending_message_tail, pm);
1885       session->mac->fragment_messages_out_count++;
1886       session->fragment_messages_out_count++;
1887       plugin->pending_Fragment_Messages++;
1888       GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending fragments"),
1889                              plugin->pending_Fragment_Messages, GNUNET_NO);
1890
1891       fm = GNUNET_malloc (sizeof (struct FragmentMessage));
1892       fm->session = session;
1893       fm->timeout.abs_value = pm->timeout.abs_value;
1894       fm->frag = NULL;
1895       fm->fragcontext =
1896           GNUNET_FRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
1897                                           &plugin->tracker,
1898                                           GNUNET_TIME_UNIT_SECONDS,
1899                                           &(pm->msg->header),
1900                                           &add_message_for_send, fm);
1901       fm->timeout_task =
1902           GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
1903                                         (fm->timeout), fragmentmessage_timeout,
1904                                         fm);
1905       GNUNET_CONTAINER_DLL_insert_tail (session->mac->sending_messages_head,
1906                                         session->mac->sending_messages_tail,
1907                                         fm);
1908
1909       if (pm->transmit_cont != NULL)
1910       {
1911         pid = session->target;
1912         pm->transmit_cont (pm->transmit_cont_cls, &pid, GNUNET_OK);
1913         GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1914                          "called pm->transmit_cont for %p\n", session);
1915       }
1916       else
1917       {
1918         GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1919                          "no pm->transmit_cont for %p\n", session);
1920       }
1921       GNUNET_free (pm);
1922
1923       if (session->pending_message_head != NULL)
1924       {
1925         //requeue session
1926         queue_session (plugin, session);
1927       }
1928
1929     }
1930   }
1931
1932   //check if timeout changed
1933   set_next_send (plugin);
1934 }
1935
1936 /**
1937  * Function to send an ack, does not free the ack
1938  * @param plugin pointer to the plugin
1939  */
1940 static void
1941 send_ack (struct Plugin *plugin)
1942 {
1943
1944   ssize_t bytes;
1945   struct AckSendQueue *ack;
1946   struct Finish_send *finish;
1947
1948   ack = plugin->ack_send_queue_head;
1949   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1950                    "Sending ack for message_id %u for mac endpoint %p, size %u\n",
1951                    ack->message_id, ack->endpoint,
1952                    ntohs (ack->hdr->size) - sizeof (struct Radiotap_Send));
1953   GNUNET_assert (plugin != NULL);
1954   GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan acks send"), 1,
1955                             GNUNET_NO);
1956
1957   getRadiotapHeader (plugin, ack->endpoint, ack->radioHeader);
1958   getWlanHeader (ack->ieeewlanheader, &ack->endpoint->addr, plugin,
1959                  ntohs (ack->hdr->size));
1960
1961   bytes =
1962       GNUNET_DISK_file_write (plugin->server_stdin_handle, ack->hdr,
1963                               ntohs (ack->hdr->size));
1964   if (bytes == GNUNET_SYSERR)
1965   {
1966     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1967                      _
1968                      ("Error writing to wlan helper. errno == %d, ERROR: %s\n"),
1969                      errno, strerror (errno));
1970     finish = GNUNET_malloc (sizeof (struct Finish_send));
1971     finish->plugin = plugin;
1972     finish->head_of_next_write = NULL;
1973     finish->size = 0;
1974     finish->msgstart = NULL;
1975     restart_helper (plugin, finish);
1976   }
1977   else
1978   {
1979     GNUNET_assert (bytes == ntohs (ack->hdr->size));
1980     GNUNET_CONTAINER_DLL_remove (plugin->ack_send_queue_head,
1981                                  plugin->ack_send_queue_tail, ack);
1982     GNUNET_free (ack);
1983     set_next_send (plugin);
1984   }
1985 }
1986
1987 /**
1988  * Function called when wlan helper is ready to get some data
1989  *
1990  * @param cls closure
1991  * @param tc GNUNET_SCHEDULER_TaskContext
1992  */
1993 static void
1994 do_transmit (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1995 {
1996   struct Plugin *plugin = cls;
1997
1998   GNUNET_assert (plugin != NULL);
1999
2000   plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
2001   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2002     return;
2003
2004   struct Session *session;
2005   struct FragmentMessage *fm;
2006   struct Finish_send *finish;
2007   struct FragmentMessage_queue *fmq;
2008   ssize_t bytes;
2009
2010   if (plugin->ack_send_queue_head != NULL)
2011   {
2012     send_ack (plugin);
2013     return;
2014   }
2015
2016   //test if a "hello-beacon" has to be send
2017   if (GNUNET_TIME_absolute_get_remaining (plugin->beacon_time).rel_value == 0)
2018   {
2019     send_hello_beacon (plugin);
2020     return;
2021   }
2022
2023   if (plugin->sending_messages_head != NULL)
2024   {
2025     GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan fragments send"), 1,
2026                               GNUNET_NO);
2027
2028     fmq = plugin->sending_messages_head;
2029     fm = fmq->content;
2030     GNUNET_CONTAINER_DLL_remove (plugin->sending_messages_head,
2031                                  plugin->sending_messages_tail, fmq);
2032     GNUNET_free (fmq);
2033
2034     session = fm->session;
2035     GNUNET_assert (session != NULL);
2036     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2037                      "Sending GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT for fragment message %p, size: %u\n",
2038                      fm, fm->size);
2039     getRadiotapHeader (plugin, session->mac, fm->radioHeader);
2040     getWlanHeader (fm->ieeewlanheader, &(fm->session->mac->addr), plugin,
2041                    fm->size);
2042
2043     bytes =
2044         GNUNET_DISK_file_write (plugin->server_stdin_handle, fm->frag,
2045                                 fm->size);
2046
2047
2048     if (bytes != fm->size)
2049     {
2050       finish = GNUNET_malloc (sizeof (struct Finish_send));
2051       finish->plugin = plugin;
2052       finish->msgstart = (struct GNUNET_MessageHeader *) fm->frag;
2053       GNUNET_assert (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK);
2054
2055       if (bytes == GNUNET_SYSERR)
2056       {
2057         GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
2058                          _
2059                          ("Error writing to wlan helper. errno == %d, ERROR: %s\n"),
2060                          errno, strerror (errno));
2061
2062         finish->head_of_next_write = fm->frag;
2063         finish->size = fm->size;
2064         restart_helper (plugin, finish);
2065       }
2066       else
2067       {
2068         finish->head_of_next_write = fm->frag + bytes;
2069         finish->size = fm->size - bytes;
2070         plugin->server_write_task =
2071             GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
2072                                              plugin->server_stdin_handle,
2073                                              &finish_sending, finish);
2074       }
2075
2076       fm->frag = NULL;
2077     }
2078     else
2079     {
2080       GNUNET_free (fm->frag);
2081       fm->frag = NULL;
2082       set_next_send (plugin);
2083     }
2084     GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
2085     return;
2086   }
2087
2088 #if 1
2089   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2090                    "do_transmit did nothing, should not happen!\n");
2091 #endif
2092   set_next_send (plugin);
2093 }
2094
2095 /**
2096  * Another peer has suggested an address for this
2097  * peer and transport plugin.  Check that this could be a valid
2098  * address.  If so, consider adding it to the list
2099  * of addresses.
2100  *
2101  * @param cls closure
2102  * @param addr pointer to the address
2103  * @param addrlen length of addr
2104  * @return GNUNET_OK if this is a plausible address for this peer
2105  *         and transport
2106  */
2107 static int
2108 wlan_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
2109 {
2110   //struct Plugin *plugin = cls;
2111
2112   /* check if the address is plausible; if so,
2113    * add it to our list! */
2114
2115   GNUNET_assert (cls != NULL);
2116   //FIXME mitm is not checked
2117   //Mac Address has 6 bytes
2118   if (addrlen == 6)
2119   {
2120     /* TODO check for bad addresses like multicast, broadcast, etc */
2121     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2122                      "wlan_plugin_address_suggested got good address, size %u!\n",
2123                      addrlen);
2124     return GNUNET_OK;
2125   }
2126   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2127                    "wlan_plugin_address_suggested got bad address, size %u!\n",
2128                    addrlen);
2129   return GNUNET_SYSERR;
2130 }
2131
2132
2133 /**
2134  * Creates a new outbound session the transport service will use to send data to the
2135  * peer
2136  *
2137  * @param cls the plugin
2138  * @param address the address
2139  * @return the session or NULL of max connections exceeded
2140  */
2141
2142 static struct Session *
2143 wlan_plugin_get_session (void *cls,
2144                   const struct GNUNET_HELLO_Address *address)
2145 {
2146   struct Plugin *plugin = cls;
2147   struct Session * s = NULL;
2148
2149   GNUNET_assert (plugin != NULL);
2150   GNUNET_assert (address != NULL);
2151
2152   if (GNUNET_OK == wlan_plugin_address_suggested (plugin,
2153             address->address,
2154             address->address_length))
2155   {
2156     s = get_session (plugin, address->address, &address->peer);
2157   }
2158   else
2159   {
2160     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
2161                      _("Wlan Address len %d is wrong\n"), address->address_length);
2162     return s;
2163   }
2164
2165   return s;
2166 }
2167
2168 /**
2169  * Function that can be used by the transport service to transmit
2170  * a message using the plugin.   Note that in the case of a
2171  * peer disconnecting, the continuation MUST be called
2172  * prior to the disconnect notification itself.  This function
2173  * will be called with this peer's HELLO message to initiate
2174  * a fresh connection to another peer.
2175  *
2176  * @param cls closure
2177  * @param session which session must be used
2178  * @param msgbuf the message to transmit
2179  * @param msgbuf_size number of bytes in 'msgbuf'
2180  * @param priority how important is the message (most plugins will
2181  *                 ignore message priority and just FIFO)
2182  * @param to how long to wait at most for the transmission (does not
2183  *                require plugins to discard the message after the timeout,
2184  *                just advisory for the desired delay; most plugins will ignore
2185  *                this as well)
2186  * @param cont continuation to call once the message has
2187  *        been transmitted (or if the transport is ready
2188  *        for the next transmission call; or if the
2189  *        peer disconnected...); can be NULL
2190  * @param cont_cls closure for cont
2191  * @return number of bytes used (on the physical network, with overheads);
2192  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
2193  *         and does NOT mean that the message was not transmitted (DV)
2194  */
2195 static ssize_t
2196 wlan_plugin_send (void *cls,
2197                   struct Session *session,
2198                   const char *msgbuf, size_t msgbuf_size,
2199                   unsigned int priority,
2200                   struct GNUNET_TIME_Relative to,
2201                   GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
2202 {
2203   struct Plugin *plugin = cls;
2204   struct PendingMessage *newmsg;
2205   struct WlanHeader *wlanheader;
2206
2207   GNUNET_assert (plugin != NULL);
2208   GNUNET_assert (session != NULL);
2209   GNUNET_assert (msgbuf_size > 0);
2210
2211   //queue message:
2212
2213   //queue message in session
2214   //test if there is no other message in the "queue"
2215   //FIXME: to many send requests
2216   if (session->pending_message_head != NULL)
2217   {
2218     newmsg = session->pending_message_head;
2219     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2220                      "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",
2221                      GNUNET_TIME_absolute_get_remaining (newmsg->
2222                                                          timeout).rel_value,
2223                      session->mac->fragment_messages_out_count);
2224   }
2225
2226   newmsg = GNUNET_malloc (sizeof (struct PendingMessage));
2227   newmsg->msg = GNUNET_malloc (msgbuf_size + sizeof (struct WlanHeader));
2228   wlanheader = newmsg->msg;
2229   //copy msg to buffer, not fragmented / segmented yet, but with message header
2230   wlanheader->header.size = htons (msgbuf_size + sizeof (struct WlanHeader));
2231   wlanheader->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA);
2232   memcpy (&(wlanheader->target), &session->target, sizeof (struct GNUNET_PeerIdentity));
2233   memcpy (&(wlanheader->source), plugin->env->my_identity,
2234           sizeof (struct GNUNET_PeerIdentity));
2235   wlanheader->crc = 0;
2236   memcpy (&wlanheader[1], msgbuf, msgbuf_size);
2237   wlanheader->crc =
2238       htonl (GNUNET_CRYPTO_crc32_n
2239              ((char *) wlanheader, msgbuf_size + sizeof (struct WlanHeader)));
2240
2241   newmsg->transmit_cont = cont;
2242   newmsg->transmit_cont_cls = cont_cls;
2243   newmsg->timeout = GNUNET_TIME_relative_to_absolute (to);
2244
2245   newmsg->timeout.abs_value = newmsg->timeout.abs_value - 500;
2246
2247   newmsg->message_size = msgbuf_size + sizeof (struct WlanHeader);
2248
2249   GNUNET_CONTAINER_DLL_insert_tail (session->pending_message_head,
2250                                     session->pending_message_tail, newmsg);
2251
2252   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2253                    "New message for %p with size (incl wlan header) %u added\n",
2254                    session, newmsg->message_size);
2255 #if DEBUG_WLAN > 1
2256   hexdump (msgbuf, GNUNET_MIN (msgbuf_size, 256));
2257 #endif
2258   //queue session
2259   queue_session (plugin, session);
2260
2261   check_fragment_queue (plugin);
2262   //FIXME not the correct size
2263   return msgbuf_size;
2264 }
2265
2266
2267 /**
2268  * function to free a mac endpoint
2269  * @param plugin pointer to the plugin struct
2270  * @param endpoint pointer to the MacEndpoint to free
2271  */
2272 static void
2273 free_macendpoint (struct Plugin *plugin, struct MacEndpoint *endpoint)
2274 {
2275   struct Sessionqueue *sessions;
2276   struct Sessionqueue *sessions_next;
2277
2278   GNUNET_assert (endpoint != NULL);
2279
2280   sessions = endpoint->sessions_head;
2281   while (sessions != NULL)
2282   {
2283     sessions_next = sessions->next;
2284     free_session (plugin, sessions, GNUNET_NO);
2285     sessions = sessions_next;
2286   }
2287
2288   GNUNET_CONTAINER_DLL_remove (plugin->mac_head, plugin->mac_tail, endpoint);
2289   if (endpoint->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2290   {
2291     GNUNET_SCHEDULER_cancel (endpoint->timeout_task);
2292     endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2293   }
2294   plugin->mac_count--;
2295   GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan mac endpoints"),
2296                          plugin->mac_count, GNUNET_NO);
2297   GNUNET_free (endpoint);
2298
2299 }
2300
2301 /**
2302  * function to free a session
2303  * @param plugin pointer to the plugin
2304  * @param queue pointer to the sessionqueue element to free
2305  * @param do_free_macendpoint if GNUNET_YES and mac endpoint would be empty, free mac endpoint
2306  */
2307 static void
2308 free_session (struct Plugin *plugin, struct Sessionqueue *queue,
2309               int do_free_macendpoint)
2310 {
2311   struct Sessionqueue *pendingsession;
2312   struct Sessionqueue *pendingsession_tmp;
2313   struct PendingMessage *pm;
2314   struct MacEndpoint *endpoint;
2315   struct FragmentMessage *fm;
2316   struct FragmentMessage *fmnext;
2317   int check = 0;
2318
2319   GNUNET_assert (plugin != NULL);
2320   GNUNET_assert (queue != NULL);
2321   GNUNET_assert (queue->content != NULL);
2322
2323   //session found
2324   //is this session pending for send
2325   pendingsession = plugin->pending_Sessions_head;
2326   while (pendingsession != NULL)
2327   {
2328     pendingsession_tmp = pendingsession;
2329     pendingsession = pendingsession->next;
2330     GNUNET_assert (pendingsession_tmp->content != NULL);
2331     if (pendingsession_tmp->content == queue->content)
2332     {
2333       plugin->pendingsessions--;
2334       GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
2335                              plugin->pendingsessions, GNUNET_NO);
2336       GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
2337                                    plugin->pending_Sessions_tail,
2338                                    pendingsession_tmp);
2339       GNUNET_free (pendingsession_tmp);
2340
2341       GNUNET_assert (check == 0);
2342       check = 1;
2343     }
2344   }
2345
2346   endpoint = queue->content->mac;
2347   fm = endpoint->sending_messages_head;
2348   while (fm != NULL)
2349   {
2350     fmnext = fm->next;
2351     if (fm->session == queue->content)
2352     {
2353       free_fragment_message (plugin, fm);
2354     }
2355     fm = fmnext;
2356   }
2357
2358   // remove PendingMessage
2359   pm = queue->content->pending_message_head;
2360   while (pm != NULL)
2361   {
2362     GNUNET_CONTAINER_DLL_remove (queue->content->pending_message_head,
2363                                  queue->content->pending_message_tail, pm);
2364     GNUNET_free (pm->msg);
2365     GNUNET_free (pm);
2366     pm = queue->content->pending_message_head;
2367   }
2368
2369   GNUNET_CONTAINER_DLL_remove (endpoint->sessions_head, endpoint->sessions_tail,
2370                                queue);
2371   //Check that no ohter session on this endpoint for this session exits
2372   GNUNET_assert (search_session (plugin, endpoint, &queue->content->target) ==
2373                  NULL);
2374   if (endpoint->sessions_head == NULL && do_free_macendpoint == GNUNET_YES)
2375   {
2376     free_macendpoint (plugin, endpoint);
2377     //check if no endpoint with the same address exists
2378     GNUNET_assert (get_macendpoint (plugin, &endpoint->addr, GNUNET_NO) ==
2379                    NULL);
2380   }
2381
2382   if (queue->content->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2383   {
2384     GNUNET_SCHEDULER_cancel (queue->content->timeout_task);
2385     queue->content->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2386   }
2387   GNUNET_free (queue);
2388
2389   check_fragment_queue (plugin);
2390 }
2391
2392 /**
2393  * Function that can be used to force the plugin to disconnect
2394  * from the given peer and cancel all previous transmissions
2395  * (and their continuation).
2396  *
2397  * @param cls closure
2398  * @param target peer from which to disconnect
2399  */
2400 static void
2401 wlan_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
2402 {
2403   struct Plugin *plugin = cls;
2404   struct Sessionqueue *queue;
2405   struct Sessionqueue *queue_next;
2406   struct MacEndpoint *endpoint = plugin->mac_head;
2407   struct MacEndpoint *endpoint_next;
2408
2409   // just look at all the session for the needed one
2410   while (endpoint != NULL)
2411   {
2412     queue = endpoint->sessions_head;
2413     endpoint_next = endpoint->next;
2414     while (queue != NULL)
2415     {
2416       // content is never NULL
2417       GNUNET_assert (queue->content != NULL);
2418       queue_next = queue->next;
2419       if (memcmp
2420           (target, &(queue->content->target),
2421            sizeof (struct GNUNET_PeerIdentity)) == 0)
2422       {
2423         free_session (plugin, queue, GNUNET_YES);
2424       }
2425       // try next
2426       queue = queue_next;
2427     }
2428     endpoint = endpoint_next;
2429   }
2430 }
2431
2432 /**
2433  * Convert the transports address to a nice, human-readable
2434  * format.
2435  *
2436  * @param cls closure
2437  * @param type name of the transport that generated the address
2438  * @param addr one of the addresses of the host, NULL for the last address
2439  *        the specific address format depends on the transport
2440  * @param addrlen length of the address
2441  * @param numeric should (IP) addresses be displayed in numeric form?
2442  * @param timeout after how long should we give up?
2443  * @param asc function to call on each string
2444  * @param asc_cls closure for asc
2445  */
2446 static void
2447 wlan_plugin_address_pretty_printer (void *cls, const char *type,
2448                                     const void *addr, size_t addrlen,
2449                                     int numeric,
2450                                     struct GNUNET_TIME_Relative timeout,
2451                                     GNUNET_TRANSPORT_AddressStringCallback asc,
2452                                     void *asc_cls)
2453 {
2454   char *ret;
2455   const unsigned char *input;
2456
2457   //GNUNET_assert(cls !=NULL);
2458   if (addrlen != sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))
2459   {
2460     /* invalid address (MAC addresses have 6 bytes) */
2461     //GNUNET_break (0);
2462     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2463                      "Func wlan_plugin_address_pretty_printer got size: %u, worng size!\n",
2464                      addrlen);
2465     asc (asc_cls, NULL);
2466     return;
2467   }
2468   input = (const unsigned char *) addr;
2469   GNUNET_asprintf (&ret,
2470                    "Transport %s: %s Mac-Address %.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
2471                    type, PROTOCOL_PREFIX, input[0], input[1], input[2],
2472                    input[3], input[4], input[5]);
2473   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2474                    "Func wlan_plugin_address_pretty_printer got size: %u, nummeric %u, type %s; made string: %s\n",
2475                    addrlen, numeric, type, ret);
2476   asc (asc_cls, ret);
2477   //only one mac address per plugin
2478   asc (asc_cls, NULL);
2479 }
2480
2481
2482
2483 /**
2484  * handels the data after all fragments are put together
2485  * @param cls macendpoint this messages belongs to
2486  * @param hdr pointer to the data
2487  */
2488 static void
2489 wlan_data_message_handler (void *cls, const struct GNUNET_MessageHeader *hdr)
2490 {
2491   struct MacEndpoint *endpoint = (struct MacEndpoint *) cls;
2492   struct Plugin *plugin = endpoint->plugin;
2493   struct WlanHeader *wlanheader;
2494   struct Session *session;
2495
2496   const struct GNUNET_MessageHeader *temp_hdr;
2497   struct GNUNET_PeerIdentity tmpsource;
2498   int crc;
2499
2500   GNUNET_assert (plugin != NULL);
2501
2502   if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_DATA)
2503   {
2504
2505     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2506                      "Func wlan_data_message_handler got GNUNET_MESSAGE_TYPE_WLAN_DATA size: %u\n",
2507                      ntohs (hdr->size));
2508
2509     if (ntohs (hdr->size) <
2510         sizeof (struct WlanHeader) + sizeof (struct GNUNET_MessageHeader))
2511     {
2512       //packet not big enought
2513       return;
2514     }
2515
2516     GNUNET_STATISTICS_update (plugin->env->stats,
2517                               _("# wlan whole messages received"), 1,
2518                               GNUNET_NO);
2519     wlanheader = (struct WlanHeader *) hdr;
2520
2521     session = search_session (plugin, endpoint, &wlanheader->source);
2522
2523     temp_hdr = (const struct GNUNET_MessageHeader *) &wlanheader[1];
2524     crc = ntohl (wlanheader->crc);
2525     wlanheader->crc = 0;
2526     if (GNUNET_CRYPTO_crc32_n
2527         ((char *) wlanheader, ntohs (wlanheader->header.size)) != crc)
2528     {
2529       //wrong crc, dispose message
2530       GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2531                        "Wlan message header crc was wrong: %u != %u\n",
2532                        GNUNET_CRYPTO_crc32_n ((char *) wlanheader,
2533                                               ntohs (wlanheader->header.size)),
2534                        crc);
2535       hexdump ((void *) hdr, ntohs (hdr->size));
2536       return;
2537     }
2538
2539     //if not in session list
2540     if (session == NULL)
2541     {
2542       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2543                        "WLAN client not in session list: packet size = %u, inner size = %u, header size = %u\n",
2544                        ntohs (wlanheader->header.size), ntohs (temp_hdr->size),
2545                        sizeof (struct WlanHeader));
2546       //try if it is a hello message
2547       if (ntohs (wlanheader->header.size) >=
2548           ntohs (temp_hdr->size) + sizeof (struct WlanHeader))
2549       {
2550         if (ntohs (temp_hdr->type) == GNUNET_MESSAGE_TYPE_HELLO)
2551         {
2552           if (GNUNET_HELLO_get_id
2553               ((const struct GNUNET_HELLO_Message *) temp_hdr,
2554                &tmpsource) == GNUNET_OK)
2555           {
2556             session = create_session (plugin, endpoint, &tmpsource);
2557           }
2558           else
2559           {
2560             GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2561                              "WLAN client not in session list and hello message is not okay\n");
2562             return;
2563           }
2564
2565         }
2566         else
2567         {
2568           GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2569                            "WLAN client not in session list and not a hello message\n");
2570           return;
2571         }
2572       }
2573       else
2574       {
2575         GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2576                          "WLAN client not in session list and message size in does not fit\npacket size = %u, inner size = %u, header size = %u\n",
2577                          ntohs (wlanheader->header.size),
2578                          ntohs (temp_hdr->size), sizeof (struct WlanHeader));
2579         return;
2580       }
2581     }
2582
2583     //"receive" the message
2584
2585     if (memcmp
2586         (&wlanheader->source, &session->target,
2587          sizeof (struct GNUNET_PeerIdentity)) != 0)
2588     {
2589       //wrong peer id
2590       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2591                        "WLAN peer source id doesn't match packet peer source id: session %p\n",
2592                        session);
2593       return;
2594     }
2595
2596     if (memcmp
2597         (&wlanheader->target, plugin->env->my_identity,
2598          sizeof (struct GNUNET_PeerIdentity)) != 0)
2599     {
2600       //wrong peer id
2601       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2602                        "WLAN peer target id doesn't match our peer id: session %p\n",
2603                        session);
2604       return;
2605     }
2606
2607     GNUNET_SERVER_mst_receive (plugin->data_tokenizer, session,
2608                                (const char *) temp_hdr,
2609                                ntohs (hdr->size) - sizeof (struct WlanHeader),
2610                                GNUNET_YES, GNUNET_NO);
2611
2612     return;
2613   }
2614   else
2615   {
2616     GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2617                      "wlan_data_message_handler got wrong message type: %u\n",
2618                      ntohs (hdr->size));
2619     return;
2620   }
2621 }
2622
2623 /**
2624  * function to process the a message, give it to the higher layer
2625  * @param cls pointer to the plugin
2626  * @param client pointer to the session this message belongs to
2627  * @param hdr start of the message
2628  */
2629 //TODO ATS informations
2630 static void
2631 process_data (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
2632 {
2633
2634   GNUNET_assert (client != NULL);
2635   GNUNET_assert (cls != NULL);
2636   struct Session *session = (struct Session *) client;
2637   struct Plugin *plugin = (struct Plugin *) cls;
2638   struct GNUNET_ATS_Information ats[2];
2639
2640   ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
2641   ats[0].value = htonl (1);
2642   ats[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
2643   ats[1].value = htonl (GNUNET_ATS_NET_WLAN);
2644
2645   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2646                    "Calling plugin->env->receive for session %p; %s; size: %u\n",
2647                    session, wlan_plugin_address_to_string (NULL,
2648                                                            session->mac->
2649                                                            addr.mac, 6),
2650                    htons (hdr->size));
2651   plugin->env->receive (plugin->env->cls, &(session->target), hdr,
2652                         (const struct GNUNET_ATS_Information *) &ats, 2,
2653                         session, (const char *) &session->mac->addr,
2654                         sizeof (session->mac->addr));
2655 }
2656
2657 /**
2658  * Function used for to process the data received from the wlan interface
2659  *
2660  * @param cls the plugin handle
2661  * @param session_light pointer to the struct holding known informations
2662  * @param hdr hdr of the GNUNET_MessageHeader
2663  * @param rxinfo pointer to the radiotap informations got with this packet FIXME: give ATS for info
2664  */
2665 static void
2666 wlan_data_helper (void *cls, struct Session_light *session_light,
2667                   const struct GNUNET_MessageHeader *hdr,
2668                   const struct Radiotap_rx *rxinfo)
2669 {
2670   struct Plugin *plugin = cls;
2671   struct FragmentMessage *fm;
2672   struct FragmentMessage *fm2;
2673   struct GNUNET_PeerIdentity tmpsource;
2674
2675   GNUNET_assert (plugin != NULL);
2676
2677   //ADVERTISEMENT
2678   if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_HELLO)
2679   {
2680
2681     //TODO better DOS protection, error handling
2682     //TODO test first than create session
2683     GNUNET_assert (session_light != NULL);
2684
2685     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2686                      "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_HELLO size: %u; %s\n",
2687                      ntohs (hdr->size), wlan_plugin_address_to_string (NULL,
2688                                                                        session_light->addr.
2689                                                                        mac, 6));
2690     if (session_light->macendpoint == NULL)
2691     {
2692       session_light->macendpoint =
2693           get_macendpoint (plugin, &session_light->addr, GNUNET_YES);
2694     }
2695
2696
2697     if (GNUNET_HELLO_get_id
2698         ((const struct GNUNET_HELLO_Message *) hdr, &tmpsource) == GNUNET_OK)
2699     {
2700       session_light->session =
2701           search_session (plugin, session_light->macendpoint, &tmpsource);
2702       if (session_light->session == NULL)
2703       {
2704         session_light->session =
2705             create_session (plugin, session_light->macendpoint, &tmpsource);
2706       }
2707       GNUNET_STATISTICS_update (plugin->env->stats,
2708                                 _("# wlan hello messages received"), 1,
2709                                 GNUNET_NO);
2710       plugin->env->receive (plugin->env->cls, &session_light->session->target,
2711                             hdr, NULL, 0, session_light->session,
2712                             (const char *) &session_light->session->mac->addr,
2713                             sizeof (session_light->session->mac->addr));
2714     }
2715     else
2716     {
2717       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2718                        "WLAN client not in session list and hello message is not okay\n");
2719       return;
2720     }
2721   }
2722
2723   //FRAGMENT
2724
2725   else if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_FRAGMENT)
2726   {
2727
2728     GNUNET_assert (session_light != NULL);
2729     if (session_light->macendpoint == NULL)
2730     {
2731       session_light->macendpoint =
2732           get_macendpoint (plugin, &session_light->addr, GNUNET_YES);
2733     }
2734
2735     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2736                      "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_FRAGMENT with size: %u; mac endpoint %p: %s\n",
2737                      ntohs (hdr->size), session_light->macendpoint,
2738                      wlan_plugin_address_to_string (NULL,
2739                                                     session_light->addr.mac,
2740                                                     6));
2741     GNUNET_STATISTICS_update (plugin->env->stats,
2742                               _("# wlan fragments received"), 1, GNUNET_NO);
2743     int ret =
2744         GNUNET_DEFRAGMENT_process_fragment (session_light->macendpoint->defrag,
2745                                             hdr);
2746
2747     if (ret == GNUNET_NO)
2748     {
2749       session_light->macendpoint->dups++;
2750     }
2751     else if (ret == GNUNET_OK)
2752     {
2753       session_light->macendpoint->fragc++;
2754     }
2755     set_next_send (plugin);
2756
2757   }
2758
2759   //ACK
2760
2761   else if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_FRAGMENT_ACK)
2762   {
2763     GNUNET_assert (session_light != NULL);
2764     if (session_light->macendpoint == NULL)
2765     {
2766       session_light->macendpoint =
2767           get_macendpoint (plugin, &session_light->addr, GNUNET_NO);
2768     }
2769
2770     if (session_light->macendpoint == NULL)
2771     {
2772       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2773                        "Macendpoint does not exist for this GNUNET_MESSAGE_TYPE_FRAGMENT_ACK size: %u; %s\n",
2774                        ntohs (hdr->size), wlan_plugin_address_to_string (NULL,
2775                                                                          session_light->addr.mac,
2776                                                                          6));
2777       return;
2778     }
2779
2780     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2781                      "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_FRAGMENT_ACK size: %u; mac endpoint: %p; %s\n",
2782                      ntohs (hdr->size), session_light->macendpoint,
2783                      wlan_plugin_address_to_string (NULL,
2784                                                     session_light->addr.mac,
2785                                                     6));
2786     fm = session_light->macendpoint->sending_messages_head;
2787     while (fm != NULL)
2788     {
2789       fm2 = fm->next;
2790       GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan acks received"),
2791                                 1, GNUNET_NO);
2792       int ret = GNUNET_FRAGMENT_process_ack (fm->fragcontext, hdr);
2793
2794       if (ret == GNUNET_OK)
2795       {
2796         GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2797                          "Got last ack, finished fragment message %p\n", fm);
2798         session_light->macendpoint->acks++;
2799         fm->session->last_activity = GNUNET_TIME_absolute_get ();
2800         session_light->macendpoint->last_activity = fm->session->last_activity;
2801         free_fragment_message (plugin, fm);
2802         check_fragment_queue (plugin);
2803         return;
2804       }
2805       if (ret == GNUNET_NO)
2806       {
2807         GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2808                          "Got ack for: %p\n", fm);
2809         session_light->macendpoint->acks++;
2810         return;
2811       }
2812       if (ret == GNUNET_SYSERR)
2813       {
2814
2815       }
2816
2817       fm = fm2;
2818     }
2819
2820     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2821                      "WLAN fragment not in fragment list\n");
2822     return;
2823
2824   }
2825   else
2826   {
2827     // TODO Wrong data?
2828     GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2829                      "WLAN packet inside the WLAN helper packet has not the right type: %u size: %u\n",
2830                      ntohs (hdr->type), ntohs (hdr->size));
2831     GNUNET_break (0);
2832     return;
2833   }
2834
2835 #if 0
2836   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2837                    "Helper finished\n");
2838 #endif
2839
2840 }
2841
2842 /**
2843  * Function to print mac addresses nicely.
2844  *
2845  * @param mac the mac address
2846  * @return string to a static buffer with the human-readable mac, will be overwritten during the next call to this function
2847  */
2848 static const char *
2849 macprinter (const u_int8_t * mac)
2850 {
2851   static char macstr[20];
2852
2853   GNUNET_snprintf (macstr, sizeof (macstr), "%X:%X:%X:%X:%X:%X", mac[0], mac[1],
2854                    mac[2], mac[3], mac[4], mac[5]);
2855   return macstr;
2856 }
2857
2858 /**
2859  * Function for the scheduler if a mac endpoint times out
2860  * @param cls pointer to the MacEndpoint
2861  * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
2862  */
2863 static void
2864 macendpoint_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2865 {
2866   struct MacEndpoint *endpoint = cls;
2867
2868   GNUNET_assert (endpoint != NULL);
2869   endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2870   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2871   {
2872     return;
2873   }
2874   if (GNUNET_TIME_absolute_get_remaining
2875       (GNUNET_TIME_absolute_add
2876        (endpoint->last_activity, MACENDPOINT_TIMEOUT)).rel_value == 0)
2877   {
2878     GNUNET_assert (endpoint->plugin != NULL);
2879     GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
2880                               _("# wlan mac endpoints timeouts"), 1, GNUNET_NO);
2881     free_macendpoint (endpoint->plugin, endpoint);
2882   }
2883   else
2884   {
2885     endpoint->timeout_task =
2886         GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
2887                                       endpoint);
2888   }
2889 }
2890
2891 /**
2892  * function to create an macendpoint
2893  * @param plugin pointer to the plugin struct
2894  * @param addr pointer to the macaddress
2895  * @return returns a macendpoint
2896  */
2897 static struct MacEndpoint *
2898 create_macendpoint (struct Plugin *plugin, const struct GNUNET_TRANSPORT_WLAN_MacAddress *addr)
2899 {
2900   struct MacEndpoint *newend = GNUNET_malloc (sizeof (struct MacEndpoint));
2901
2902   GNUNET_assert (plugin != NULL);
2903   GNUNET_STATISTICS_update (plugin->env->stats,
2904                             _("# wlan mac endpoints created"), 1, GNUNET_NO);
2905   newend->addr = *addr;
2906   newend->plugin = plugin;
2907   newend->addr = *addr;
2908   newend->fragment_messages_out_count = 0;
2909   newend->defrag =
2910       GNUNET_DEFRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
2911                                         MESSAGES_IN_DEFRAG_QUEUE_PER_MAC,
2912                                         newend, &wlan_data_message_handler,
2913                                         &add_ack_for_send);
2914   newend->last_activity = GNUNET_TIME_absolute_get ();
2915   newend->timeout_task =
2916       GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
2917                                     newend);
2918
2919   plugin->mac_count++;
2920   GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan mac endpoints"),
2921                          plugin->mac_count, GNUNET_NO);
2922   GNUNET_CONTAINER_DLL_insert_tail (plugin->mac_head, plugin->mac_tail, newend);
2923   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2924                    "New Mac Endpoint %p: %s\n", newend,
2925                    wlan_plugin_address_to_string (NULL, newend->addr.mac, 6));
2926   return newend;
2927 }
2928
2929 /**
2930  * Function used for to process the data from the suid process
2931  *
2932  * @param cls the plugin handle
2933  * @param client client that send the data (not used)
2934  * @param hdr header of the GNUNET_MessageHeader
2935  */
2936 static void
2937 wlan_process_helper (void *cls, void *client,
2938                      const struct GNUNET_MessageHeader *hdr)
2939 {
2940   struct Plugin *plugin = cls;
2941   struct ieee80211_frame *wlanIeeeHeader = NULL;
2942   struct Session_light *session_light = NULL;
2943   struct Radiotap_rx *rxinfo;
2944   const struct GNUNET_MessageHeader *temp_hdr = NULL;
2945
2946   int datasize = 0;
2947   int pos;
2948
2949   GNUNET_assert (plugin != NULL);
2950   switch (ntohs (hdr->type))
2951   {
2952   case GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA:
2953     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2954                      "Func wlan_process_helper got GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA size: %u\n",
2955                      ntohs (hdr->size));
2956     GNUNET_STATISTICS_update (plugin->env->stats,
2957                               _("# wlan WLAN_HELPER_DATA received"), 1,
2958                               GNUNET_NO);
2959     //call wlan_process_helper with the message inside, later with wlan: analyze signal
2960     if (ntohs (hdr->size) <
2961         sizeof (struct ieee80211_frame) +
2962         2 * sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_rx))
2963     {
2964       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2965                        "Size of packet is too small; size: %u min size: %u\n",
2966                        ntohs (hdr->size),
2967                        sizeof (struct ieee80211_frame) +
2968                        sizeof (struct GNUNET_MessageHeader));
2969       //GNUNET_break (0);
2970       /* FIXME: restart SUID process */
2971       return;
2972     }
2973
2974     rxinfo = (struct Radiotap_rx *) &hdr[1];
2975     wlanIeeeHeader = (struct ieee80211_frame *) &rxinfo[1];
2976
2977     //process only if it is an broadcast or for this computer both with the gnunet bssid
2978
2979     //check for bssid
2980     if (memcmp
2981         (&(wlanIeeeHeader->i_addr3), &mac_bssid_gnunet,
2982          sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0)
2983     {
2984       //check for broadcast or mac
2985       if ((memcmp
2986            (&(wlanIeeeHeader->i_addr1), &bc_all_mac,
2987             sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0) ||
2988           (memcmp
2989            (&(wlanIeeeHeader->i_addr1), &(plugin->mac_address),
2990             sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0))
2991       {
2992         //if packet is from us return
2993         if ((memcmp
2994              (&(wlanIeeeHeader->i_addr2), &(plugin->mac_address),
2995               sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0))
2996         {
2997           return;
2998         }
2999         // process the inner data
3000
3001
3002         datasize =
3003             ntohs (hdr->size) - sizeof (struct ieee80211_frame) -
3004             sizeof (struct GNUNET_MessageHeader) - sizeof (struct Radiotap_rx);
3005
3006         session_light = GNUNET_malloc (sizeof (struct Session_light));
3007         memcpy (&session_light->addr, &(wlanIeeeHeader->i_addr2),
3008                 sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress));
3009         //session_light->session = search_session(plugin,session_light->addr);
3010         GNUNET_STATISTICS_update (plugin->env->stats,
3011                                   _("# wlan messages for this client received"),
3012                                   1, GNUNET_NO);
3013
3014         pos = 0;
3015         while (pos < datasize)
3016         {
3017           temp_hdr = (struct GNUNET_MessageHeader *) &wlanIeeeHeader[1] + pos;
3018           if (ntohs (temp_hdr->size) <= datasize + pos)
3019           {
3020             GNUNET_STATISTICS_update (plugin->env->stats,
3021                                       _
3022                                       ("# wlan messages inside WLAN_HELPER_DATA received"),
3023                                       1, GNUNET_NO);
3024             wlan_data_helper (plugin, session_light, temp_hdr, rxinfo);
3025           }
3026           else
3027           {
3028             GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
3029                              "Size of packet is too small; size: %u > size of packet: %u\n",
3030                              ntohs (temp_hdr->size), datasize + pos);
3031           }
3032           pos += ntohs (temp_hdr->size);
3033
3034         }
3035
3036         //clean up
3037         GNUNET_free (session_light);
3038       }
3039       else
3040       {
3041         GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
3042                          "Func wlan_process_helper got wrong MAC: %s\n",
3043                          macprinter (wlanIeeeHeader->i_addr1));
3044       }
3045     }
3046     else
3047     {
3048       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
3049                        "Func wlan_process_helper got wrong BSSID: %s\n",
3050                        macprinter (wlanIeeeHeader->i_addr2));
3051     }
3052     break;
3053   case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
3054     //TODO more control messages
3055     if (ntohs (hdr->size) != sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage))
3056     {
3057       GNUNET_break (0);
3058       /* FIXME: restart SUID process */
3059       return;
3060     }
3061     memcpy (&plugin->mac_address, &hdr[1], sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress));
3062     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
3063                      "Received WLAN_HELPER_CONTROL message with transport of address %s\n",
3064                      wlan_plugin_address_to_string (cls, &plugin->mac_address,
3065                                                     sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)));
3066     plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
3067                                  &plugin->mac_address,
3068                                  sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress));
3069     break;
3070   default:
3071     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
3072                      "Func wlan_process_helper got unknown message with number %u, size %u\n",
3073                      ntohs (hdr->type), ntohs (hdr->size));
3074
3075 #if DEBUG_WLAN > 1
3076     hexdump (hdr, GNUNET_MIN (ntohs (hdr->size), 256));
3077 #endif
3078     GNUNET_break (0);
3079     return;
3080   }
3081 }
3082
3083
3084
3085 /**
3086  * Exit point from the plugin.
3087  * @param cls pointer to the api struct
3088  */
3089
3090 //FIXME cleanup
3091 void *
3092 libgnunet_plugin_transport_wlan_done (void *cls)
3093 {
3094   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3095   struct Plugin *plugin = api->cls;
3096   struct MacEndpoint *endpoint;
3097   struct MacEndpoint *endpoint_next;
3098
3099   if (NULL == plugin)
3100   {
3101     GNUNET_free (api);
3102     return NULL;
3103   }
3104   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
3105                    "libgnunet_plugin_transport_wlan_done started\n");
3106   wlan_transport_stop_wlan_helper (plugin);
3107
3108   GNUNET_assert (cls != NULL);
3109   //free sessions
3110   endpoint = plugin->mac_head;
3111   while (endpoint != NULL)
3112   {
3113     endpoint_next = endpoint->next;
3114     free_macendpoint (plugin, endpoint);
3115     endpoint = endpoint_next;
3116
3117   }
3118
3119
3120   if (plugin->suid_tokenizer != NULL)
3121     GNUNET_SERVER_mst_destroy (plugin->suid_tokenizer);
3122
3123   if (plugin->data_tokenizer != NULL)
3124     GNUNET_SERVER_mst_destroy (plugin->data_tokenizer);
3125
3126   GNUNET_free_non_null (plugin->interface);
3127   GNUNET_free (plugin);
3128   GNUNET_free (api);
3129   return NULL;
3130 }
3131
3132
3133 /**
3134  * Entry point for the plugin.
3135  *
3136  * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
3137  * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
3138  */
3139 void *
3140 libgnunet_plugin_transport_wlan_init (void *cls)
3141 {
3142   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3143   struct GNUNET_TRANSPORT_PluginFunctions *api;
3144   struct Plugin *plugin;
3145
3146   if (NULL == env->receive)
3147   {
3148     /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3149        initialze the plugin or the API */
3150     api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3151     api->cls = NULL;
3152     api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
3153     api->address_to_string = &wlan_plugin_address_to_string;
3154     api->string_to_address = NULL; // FIXME!
3155     return api;
3156   }
3157
3158   plugin = GNUNET_malloc (sizeof (struct Plugin));
3159   plugin->env = env;
3160   plugin->pendingsessions = 0;
3161   GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan pending sessions"),
3162                          plugin->pendingsessions, GNUNET_NO);
3163   plugin->mac_count = 0;
3164   GNUNET_STATISTICS_set (plugin->env->stats, _("# wlan mac endpoints"),
3165                          plugin->mac_count, GNUNET_NO);
3166   plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
3167   plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
3168   plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
3169   GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
3170                                  GNUNET_BANDWIDTH_value_init (100 * 1024 *
3171                                                               1024 / 8), 100);
3172
3173   plugin->suid_tokenizer =
3174       GNUNET_SERVER_mst_create (&wlan_process_helper, plugin);
3175
3176   plugin->data_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
3177
3178   //plugin->sessions = GNUNET_malloc (sizeof (struct Sessionqueue));
3179   //plugin->pending_Sessions_head = GNUNET_malloc (sizeof (struct Sessionqueue));
3180
3181   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3182   api->cls = plugin;
3183   api->send = &wlan_plugin_send;
3184   api->get_session = &wlan_plugin_get_session;
3185   api->disconnect = &wlan_plugin_disconnect;
3186   api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
3187   api->check_address = &wlan_plugin_address_suggested;
3188   api->address_to_string = &wlan_plugin_address_to_string;
3189
3190   //read config
3191
3192   if (GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "TESTMODE"))
3193   {
3194     if (GNUNET_SYSERR ==
3195         GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-wlan",
3196                                                "TESTMODE", &(plugin->testmode)))
3197       plugin->testmode = 0;     //default value
3198   }
3199
3200   if (GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "INTERFACE"))
3201   {
3202     if (GNUNET_CONFIGURATION_get_value_string
3203         (env->cfg, "transport-wlan", "INTERFACE",
3204          &(plugin->interface)) != GNUNET_YES)
3205     {
3206       libgnunet_plugin_transport_wlan_done (api);
3207       return NULL;
3208     }
3209   }
3210
3211   //start the plugin
3212   wlan_transport_start_wlan_helper (plugin);
3213   set_next_beacon_time (plugin);
3214   set_next_send (plugin);
3215   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
3216                    "wlan init finished\n");
3217   return api;
3218 }
3219
3220
3221
3222
3223 /* end of plugin_transport_wlan.c */