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