-fix use of possibly wrong or uninitialized session
[oweals/gnunet.git] / src / transport / plugin_transport_wlan.c
1 /*
2   This file is part of GNUnet
3   (C) 2010-2014 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 and/or bluetooth
24  * @author David Brodski
25  * @author Christian Grothoff
26  *
27  * BUILD_WLAN or BUILD_BLUETOOTH must be defined such that the respective
28  * variant of this code is compiled.
29  */
30 #include "platform.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_hello_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_statistics_service.h"
35 #include "gnunet_transport_service.h"
36 #include "gnunet_transport_plugin.h"
37 #include "plugin_transport_wlan.h"
38 #include "gnunet_fragmentation_lib.h"
39 #include "gnunet_constants.h"
40
41 #if BUILD_WLAN
42 /* begin case wlan */
43 #define PLUGIN_NAME "wlan"
44 #define CONFIG_NAME "transport-wlan"
45 #define HELPER_NAME "gnunet-helper-transport-wlan"
46 #define DUMMY_HELPER_NAME "gnunet-helper-transport-wlan-dummy"
47 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_wlan_init
48 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_wlan_done
49 #define LOG(kind,...) GNUNET_log_from (kind, "transport-wlan",__VA_ARGS__)
50
51 /**
52  * time out of a mac endpoint
53  */
54 #define MACENDPOINT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 2)
55
56 /**
57  * We reduce the frequence of HELLO beacons in relation to
58  * the number of MAC addresses currently visible to us.
59  * This is the multiplication factor.
60  */
61 #define HELLO_BEACON_SCALING_FACTOR GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
62
63
64 /* end case wlan */
65 #elif BUILD_BLUETOOTH
66 /* begin case bluetooth */
67
68 #define PLUGIN_NAME "bluetooth"
69 #define CONFIG_NAME "transport-bluetooth"
70 #define HELPER_NAME "gnunet-helper-transport-bluetooth"
71 /* yes, this is correct, we use the same dummy driver as 'wlan' */
72 #define DUMMY_HELPER_NAME "gnunet-helper-transport-wlan-dummy"
73 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_bluetooth_init
74 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_bluetooth_done
75 #define LOG(kind,...) GNUNET_log_from (kind, "transport-bluetooth",__VA_ARGS__)
76
77 /**
78  * time out of a mac endpoint
79  */
80 #define MACENDPOINT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 60)
81
82
83 /**
84  * We reduce the frequence of HELLO beacons in relation to
85  * the number of MAC addresses currently visible to us.
86  * This is the multiplication factor.
87  */
88 #define HELLO_BEACON_SCALING_FACTOR GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
89
90 /* end case bluetooth */
91 #else
92 #error need to build wlan or bluetooth
93 #endif
94
95 /**
96  * Max size of packet (that we give to the WLAN driver for transmission)
97  */
98 #define WLAN_MTU 1430
99
100
101
102 /**
103  * Maximum number of messages in defragmentation queue per MAC
104  */
105 #define MESSAGES_IN_DEFRAG_QUEUE_PER_MAC 2
106
107 /**
108  * Link layer control fields for better compatibility
109  * (i.e. GNUnet over WLAN is not IP-over-WLAN).
110  */
111 #define WLAN_LLC_DSAP_FIELD 0x1f
112 #define WLAN_LLC_SSAP_FIELD 0x1f
113
114
115 GNUNET_NETWORK_STRUCT_BEGIN
116 /**
117  * Header for messages which need fragmentation.  This is the format of
118  * a message we obtain AFTER defragmentation.  We then need to check
119  * the CRC and then tokenize the payload and pass it to the
120  * 'receive' callback.
121  */
122 struct WlanHeader
123 {
124
125   /**
126    * Message type is #GNUNET_MESSAGE_TYPE_WLAN_DATA.
127    */
128   struct GNUNET_MessageHeader header;
129
130   /**
131    * CRC32 checksum (only over the payload), in NBO.
132    */
133   uint32_t crc GNUNET_PACKED;
134
135   /**
136    * Sender of the message.
137    */
138   struct GNUNET_PeerIdentity sender;
139
140   /**
141    * Target of the message.
142    */
143   struct GNUNET_PeerIdentity target;
144
145   /* followed by payload, possibly including
146      multiple messages! */
147
148 };
149
150
151 /**
152  * Address format for WLAN.
153  */
154 struct WlanAddress
155 {
156   /**
157    * Options set for the WLAN, in NBO.
158    */
159   uint32_t options GNUNET_PACKED;
160
161   /**
162    * WLAN addresses using MACs.
163    */
164   struct GNUNET_TRANSPORT_WLAN_MacAddress mac;
165 };
166
167
168 GNUNET_NETWORK_STRUCT_END
169
170
171 /**
172  * Information kept for each message that is yet to be fragmented and
173  * transmitted.
174  */
175 struct PendingMessage
176 {
177   /**
178    * next entry in the DLL
179    */
180   struct PendingMessage *next;
181
182   /**
183    * previous entry in the DLL
184    */
185   struct PendingMessage *prev;
186
187   /**
188    * The pending message
189    */
190   struct WlanHeader *msg;
191
192   /**
193    * Continuation function to call once the message
194    * has been sent.  Can be NULL if there is no
195    * continuation to call.
196    */
197   GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
198
199   /**
200    * Cls for @e transmit_cont
201    */
202   void *transmit_cont_cls;
203
204   /**
205    * Timeout task (for this message).
206    */
207   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
208
209 };
210
211
212 /**
213  * Session handle for connections with other peers.
214  */
215 struct Session
216 {
217   /**
218    * To whom are we talking to (set to our identity
219    * if we are still waiting for the welcome message)
220    */
221   struct GNUNET_PeerIdentity target;
222
223   /**
224    * We keep all sessions in a DLL at their respective
225    * `struct MACEndpoint *`.
226    */
227   struct Session *next;
228
229   /**
230    * We keep all sessions in a DLL at their respective
231    * `struct MACEndpoint *`.
232    */
233   struct Session *prev;
234
235   /**
236    * MAC endpoint with the address of this peer.
237    */
238   struct MacEndpoint *mac;
239
240   /**
241    * Address associated with this session and MAC endpoint
242    */
243   struct GNUNET_HELLO_Address *address;
244
245   /**
246    * When should this session time out?
247    */
248   struct GNUNET_TIME_Absolute timeout;
249
250   /**
251    * Timeout task (for the session).
252    */
253   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
254
255 };
256
257
258 /**
259  * Struct for messages that are being fragmented in a MAC's transmission queue.
260  */
261 struct FragmentMessage
262 {
263
264   /**
265    * This is a doubly-linked list.
266    */
267   struct FragmentMessage *next;
268
269   /**
270    * This is a doubly-linked list.
271    */
272   struct FragmentMessage *prev;
273
274   /**
275    * MAC endpoint this message belongs to
276    */
277   struct MacEndpoint *macendpoint;
278
279   /**
280    * Fragmentation context
281    */
282   struct GNUNET_FRAGMENT_Context *fragcontext;
283
284   /**
285    * Transmission handle to helper (to cancel if the frag context
286    * is destroyed early for some reason).
287    */
288   struct GNUNET_HELPER_SendHandle *sh;
289
290   /**
291    * Intended recipient.
292    */
293   struct GNUNET_PeerIdentity target;
294
295   /**
296    * Timeout value for the message.
297    */
298   struct GNUNET_TIME_Absolute timeout;
299
300   /**
301    * Timeout task.
302    */
303   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
304
305   /**
306    * Continuation to call when we're done with this message.
307    */
308   GNUNET_TRANSPORT_TransmitContinuation cont;
309
310   /**
311    * Closure for @e cont
312    */
313   void *cont_cls;
314
315   /**
316    * Size of original message
317    */
318   size_t size_payload;
319
320   /**
321    * Number of bytes used to transmit message
322    */
323   size_t size_on_wire;
324
325 };
326
327
328 /**
329  * Struct to represent one network card connection
330  */
331 struct MacEndpoint
332 {
333
334   /**
335    * We keep all MACs in a DLL in the plugin.
336    */
337   struct MacEndpoint *next;
338
339   /**
340    * We keep all MACs in a DLL in the plugin.
341    */
342   struct MacEndpoint *prev;
343
344   /**
345    * Pointer to the global plugin struct.
346    */
347   struct Plugin *plugin;
348
349   /**
350    * Head of sessions that use this MAC.
351    */
352   struct Session *sessions_head;
353
354   /**
355    * Tail of sessions that use this MAC.
356    */
357   struct Session *sessions_tail;
358
359   /**
360    * Head of messages we are currently sending to this MAC.
361    */
362   struct FragmentMessage *sending_messages_head;
363
364   /**
365    * Tail of messages we are currently sending to this MAC.
366    */
367   struct FragmentMessage *sending_messages_tail;
368
369   /**
370    * Defrag context for this MAC
371    */
372   struct GNUNET_DEFRAGMENT_Context *defrag;
373
374   /**
375    * When should this endpoint time out?
376    */
377   struct GNUNET_TIME_Absolute timeout;
378
379   /**
380    * Timeout task.
381    */
382   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
383
384   /**
385    * count of messages in the fragment out queue for this mac endpoint
386    */
387   unsigned int fragment_messages_out_count;
388
389   /**
390    * peer MAC address
391    */
392   struct WlanAddress wlan_addr;
393
394   /**
395    * Message delay for fragmentation context
396    */
397   struct GNUNET_TIME_Relative msg_delay;
398
399   /**
400    * ACK delay for fragmentation context
401    */
402   struct GNUNET_TIME_Relative ack_delay;
403
404   /**
405    * Desired transmission power for this MAC
406    */
407   uint16_t tx_power;
408
409   /**
410    * Desired transmission rate for this MAC
411    */
412   uint8_t rate;
413
414   /**
415    * Antenna we should use for this MAC
416    */
417   uint8_t antenna;
418
419 };
420
421
422 /**
423  * Encapsulation of all of the state of the plugin.
424  */
425 struct Plugin
426 {
427   /**
428    * Our environment.
429    */
430   struct GNUNET_TRANSPORT_PluginEnvironment *env;
431
432   /**
433    * Handle to helper process for priviledged operations.
434    */
435   struct GNUNET_HELPER_Handle *suid_helper;
436
437   /**
438    * Function to call about session status changes.
439    */
440   GNUNET_TRANSPORT_SessionInfoCallback sic;
441
442   /**
443    * Closure for @e sic.
444    */
445   void *sic_cls;
446
447   /**
448    * ARGV-vector for the helper (all helpers take only the binary
449    * name, one actual argument, plus the NULL terminator for 'argv').
450    */
451   char *helper_argv[3];
452
453   /**
454    * The interface of the wlan card given to us by the user.
455    */
456   char *wlan_interface;
457
458   /**
459    * Tokenizer for demultiplexing of data packets resulting from
460    * defragmentation.
461    */
462   struct GNUNET_SERVER_MessageStreamTokenizer *fragment_data_tokenizer;
463
464   /**
465    * Tokenizer for demultiplexing of data packets received from the suid helper
466    */
467   struct GNUNET_SERVER_MessageStreamTokenizer *helper_payload_tokenizer;
468
469   /**
470    * Tokenizer for demultiplexing of data packets that follow the WLAN Header
471    */
472   struct GNUNET_SERVER_MessageStreamTokenizer *wlan_header_payload_tokenizer;
473
474   /**
475    * Head of list of open connections.
476    */
477   struct MacEndpoint *mac_head;
478
479   /**
480    * Tail of list of open connections.
481    */
482   struct MacEndpoint *mac_tail;
483
484   /**
485    * Task that periodically sends a HELLO beacon via the helper.
486    */
487   GNUNET_SCHEDULER_TaskIdentifier beacon_task;
488
489   /**
490    * Tracker for bandwidth limit
491    */
492   struct GNUNET_BANDWIDTH_Tracker tracker;
493
494   /**
495    * The mac_address of the wlan card given to us by the helper.
496    */
497   struct GNUNET_TRANSPORT_WLAN_MacAddress mac_address;
498
499   /**
500    * Have we received a control message with our MAC address yet?
501    */
502   int have_mac;
503
504   /**
505    * Number of connections
506    */
507   unsigned int mac_count;
508
509   /**
510    * Options for addresses
511    */
512   uint32_t options;
513
514 };
515
516
517 /**
518  * Information associated with a message.  Can contain
519  * the session or the MAC endpoint associated with the
520  * message (or both).
521  */
522 struct MacAndSession
523 {
524   /**
525    * NULL if the identity of the other peer is not known.
526    */
527   struct Session *session;
528
529   /**
530    * MAC address of the other peer, NULL if not known.
531    */
532   struct MacEndpoint *endpoint;
533 };
534
535
536 /**
537  * Print MAC addresses nicely.
538  *
539  * @param mac the mac address
540  * @return string to a static buffer with
541  * the human-readable mac, will be overwritten during the next call to
542  * this function
543  */
544 static const char *
545 mac_to_string (const struct GNUNET_TRANSPORT_WLAN_MacAddress * mac)
546 {
547   static char macstr[20];
548
549   GNUNET_snprintf (macstr,
550                    sizeof (macstr),
551                    "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
552                    mac->mac[0], mac->mac[1],
553                    mac->mac[2], mac->mac[3],
554                    mac->mac[4], mac->mac[5]);
555   return macstr;
556 }
557
558
559 /**
560  * Function called for a quick conversion of the binary address to
561  * a numeric address.  Note that the caller must not free the
562  * address and that the next call to this function is allowed
563  * to override the address again.
564  *
565  * @param cls closure
566  * @param addr binary address
567  * @param addrlen length of the address
568  * @return string representing the same address
569  */
570 static const char *
571 wlan_plugin_address_to_string (void *cls,
572                                const void *addr,
573                                size_t addrlen)
574 {
575   const struct GNUNET_TRANSPORT_WLAN_MacAddress *mac;
576   static char macstr[36];
577
578   if (sizeof (struct WlanAddress) != addrlen)
579   {
580     GNUNET_break (0);
581     return NULL;
582   }
583   mac = &((struct WlanAddress *) addr)->mac;
584   GNUNET_snprintf (macstr,
585                    sizeof (macstr),
586                    "%s.%u.%s",
587                    PLUGIN_NAME,
588                    ntohl (((struct WlanAddress *) addr)->options),
589                    mac_to_string (mac));
590   return macstr;
591 }
592
593
594 /**
595  * If a session monitor is attached, notify it about the new
596  * session state.
597  *
598  * @param plugin our plugin
599  * @param session session that changed state
600  * @param state new state of the session
601  */
602 static void
603 notify_session_monitor (struct Plugin *plugin,
604                         struct Session *session,
605                         enum GNUNET_TRANSPORT_SessionState state)
606 {
607   struct GNUNET_TRANSPORT_SessionInfo info;
608
609   if (NULL == plugin->sic)
610     return;
611   memset (&info, 0, sizeof (info));
612   info.state = state;
613   info.is_inbound = GNUNET_SYSERR; /* hard to say */
614   info.num_msg_pending = 0; /* we queue per MAC, not per peer */
615   info.num_bytes_pending = 0; /* we queue per MAC, not per peer */
616   info.receive_delay = GNUNET_TIME_UNIT_ZERO_ABS; /* not supported by WLAN */
617   info.session_timeout = session->timeout;
618   info.address = session->address;
619   plugin->sic (plugin->sic_cls,
620                session,
621                &info);
622 }
623
624
625 /**
626  * Fill the radiotap header
627  *
628  * @param endpoint pointer to the endpoint, can be NULL
629  * @param header pointer to the radiotap header
630  * @param size total message size
631  */
632 static void
633 get_radiotap_header (struct MacEndpoint *endpoint,
634                      struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *header,
635                      uint16_t size)
636 {
637   header->header.type = ntohs (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER);
638   header->header.size = ntohs (size);
639   if (NULL != endpoint)
640   {
641     header->rate = endpoint->rate;
642     header->tx_power = endpoint->tx_power;
643     header->antenna = endpoint->antenna;
644   }
645   else
646   {
647     header->rate = 255;
648     header->tx_power = 0;
649     header->antenna = 0;
650   }
651 }
652
653
654 /**
655  * Generate the WLAN hardware header for one packet
656  *
657  * @param plugin the plugin handle
658  * @param header address to write the header to
659  * @param to_mac_addr address of the recipient
660  * @param size size of the whole packet, needed to calculate the time to send the packet
661  */
662 static void
663 get_wlan_header (struct Plugin *plugin,
664                  struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *header,
665                  const struct GNUNET_TRANSPORT_WLAN_MacAddress *to_mac_addr,
666                  unsigned int size)
667 {
668   const int rate = 11000000;
669
670   header->frame_control = htons (IEEE80211_FC0_TYPE_DATA);
671   header->addr1 = *to_mac_addr;
672   header->addr2 = plugin->mac_address;
673   header->addr3 = mac_bssid_gnunet;
674   header->duration = GNUNET_htole16 ((size * 1000000) / rate + 290);
675   header->sequence_control = 0; // FIXME?
676   header->llc[0] = WLAN_LLC_DSAP_FIELD;
677   header->llc[1] = WLAN_LLC_SSAP_FIELD;
678   header->llc[2] = 0;  // FIXME?
679   header->llc[3] = 0;  // FIXME?
680 }
681
682
683 /**
684  * Send an ACK for a fragment we received.
685  *
686  * @param cls the `struct MacEndpoint *` the ACK must be sent to
687  * @param msg_id id of the message
688  * @param hdr pointer to the hdr where the ack is stored
689  */
690 static void
691 send_ack (void *cls, uint32_t msg_id,
692           const struct GNUNET_MessageHeader *hdr)
693 {
694   struct MacEndpoint *endpoint = cls;
695   struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage* radio_header;
696   uint16_t msize = ntohs (hdr->size);
697   size_t size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + msize;
698   char buf[size];
699
700   if (NULL == endpoint)
701   {
702     GNUNET_break (0);
703     return;
704   }
705   if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
706   {
707     GNUNET_break (0);
708     return;
709   }
710   LOG (GNUNET_ERROR_TYPE_DEBUG,
711        "Sending ACK to helper\n");
712   radio_header = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) buf;
713   get_radiotap_header (endpoint, radio_header, size);
714   get_wlan_header (endpoint->plugin,
715                    &radio_header->frame,
716                    &endpoint->wlan_addr.mac,
717                    sizeof (endpoint->wlan_addr.mac));
718   memcpy (&radio_header[1], hdr, msize);
719   if (NULL !=
720       GNUNET_HELPER_send (endpoint->plugin->suid_helper,
721                           &radio_header->header,
722                           GNUNET_NO /* dropping ACKs is bad */,
723                           NULL, NULL))
724     GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
725                               _("# ACKs sent"),
726                               1, GNUNET_NO);
727 }
728
729
730 /**
731  * Handles the data after all fragments are put together
732  *
733  * @param cls macendpoint this messages belongs to
734  * @param hdr pointer to the data
735  */
736 static void
737 wlan_data_message_handler (void *cls,
738                            const struct GNUNET_MessageHeader *hdr)
739 {
740   struct MacEndpoint *endpoint = cls;
741   struct Plugin *plugin = endpoint->plugin;
742   struct MacAndSession mas;
743
744   GNUNET_STATISTICS_update (plugin->env->stats,
745                             _("# Messages defragmented"),
746                             1,
747                             GNUNET_NO);
748   mas.session = NULL;
749   mas.endpoint = endpoint;
750   (void) GNUNET_SERVER_mst_receive (plugin->fragment_data_tokenizer,
751                                     &mas,
752                                     (const char *) hdr,
753                                     ntohs (hdr->size),
754                                     GNUNET_YES, GNUNET_NO);
755 }
756
757
758 /**
759  * Free a session
760  *
761  * @param cls our `struct Plugin`.
762  * @param session the session free
763  */
764 static int
765 wlan_plugin_disconnect_session (void *cls,
766                                 struct Session *session)
767 {
768   struct MacEndpoint *endpoint = session->mac;
769   struct Plugin *plugin = endpoint->plugin;
770
771   plugin->env->session_end (plugin->env->cls,
772                             session->address,
773                             session);
774   notify_session_monitor (plugin,
775                           session,
776                           GNUNET_TRANSPORT_SS_DONE);
777   GNUNET_CONTAINER_DLL_remove (endpoint->sessions_head,
778                                endpoint->sessions_tail,
779                                session);
780   if (session->timeout_task != GNUNET_SCHEDULER_NO_TASK)
781   {
782     GNUNET_SCHEDULER_cancel (session->timeout_task);
783     session->timeout_task = GNUNET_SCHEDULER_NO_TASK;
784   }
785   GNUNET_STATISTICS_update (plugin->env->stats,
786                             _("# Sessions allocated"),
787                             -1,
788                             GNUNET_NO);
789   GNUNET_HELLO_address_free (session->address);
790   GNUNET_free (session);
791   return GNUNET_OK;
792 }
793
794
795 /**
796  * Function that is called to get the keepalive factor.
797  * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
798  * calculate the interval between keepalive packets.
799  *
800  * @param cls closure with the `struct Plugin`
801  * @return keepalive factor
802  */
803 static unsigned int
804 wlan_plugin_query_keepalive_factor (void *cls)
805 {
806   return 3;
807 }
808
809
810 /**
811  * A session is timing out.  Clean up.
812  *
813  * @param cls pointer to the Session
814  * @param tc unused
815  */
816 static void
817 session_timeout (void *cls,
818                  const struct GNUNET_SCHEDULER_TaskContext *tc)
819 {
820   struct Session *session = cls;
821   struct GNUNET_TIME_Relative left;
822
823   session->timeout_task = GNUNET_SCHEDULER_NO_TASK;
824   left = GNUNET_TIME_absolute_get_remaining (session->timeout);
825   if (0 != left.rel_value_us)
826   {
827     session->timeout_task =
828       GNUNET_SCHEDULER_add_delayed (left,
829                                     &session_timeout,
830                                     session);
831     return;
832   }
833   wlan_plugin_disconnect_session (session->mac->plugin,
834                                   session);
835 }
836
837
838
839 /**
840  * Lookup a new session
841  *
842  * @param endpoint pointer to the mac endpoint of the peer
843  * @param peer peer identity to use for this session
844  * @return returns the session or NULL
845  */
846 static struct Session *
847 lookup_session (struct MacEndpoint *endpoint,
848                 const struct GNUNET_PeerIdentity *peer)
849 {
850   struct Session *session;
851
852   for (session = endpoint->sessions_head; NULL != session; session = session->next)
853     if (0 == memcmp (peer, &session->target, sizeof (struct GNUNET_PeerIdentity)))
854       return session;
855   return NULL;
856 }
857
858
859 /**
860  * Create a new session
861  *
862  * @param endpoint pointer to the mac endpoint of the peer
863  * @param peer peer identity to use for this session
864  * @return returns the session or NULL
865  */
866 static struct Session *
867 create_session (struct MacEndpoint *endpoint,
868                 const struct GNUNET_PeerIdentity *peer)
869 {
870   struct Session *session;
871
872   GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
873                             _("# Sessions allocated"),
874                             1,
875                             GNUNET_NO);
876   session = GNUNET_new (struct Session);
877   GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
878                                     endpoint->sessions_tail,
879                                     session);
880   session->address = GNUNET_HELLO_address_allocate (peer,
881                                                     PLUGIN_NAME,
882                                                     &endpoint->wlan_addr,
883                                                     sizeof (endpoint->wlan_addr),
884                                                     GNUNET_HELLO_ADDRESS_INFO_NONE);
885   session->mac = endpoint;
886   session->target = *peer;
887   session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
888   session->timeout_task =
889       GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &session_timeout,
890                                     session);
891   notify_session_monitor (endpoint->plugin,
892                           session,
893                           GNUNET_TRANSPORT_SS_INIT);
894   notify_session_monitor (endpoint->plugin,
895                           session,
896                           GNUNET_TRANSPORT_SS_UP);
897   LOG (GNUNET_ERROR_TYPE_DEBUG,
898        "Created new session %p for peer `%s' with endpoint %s\n",
899        session,
900        GNUNET_i2s (peer),
901        mac_to_string (&endpoint->wlan_addr.mac));
902
903   return session;
904 }
905
906
907 /**
908  * Look up a session for a peer and create a new session if none is found
909  *
910  * @param endpoint pointer to the mac endpoint of the peer
911  * @param peer peer identity to use for this session
912  * @return returns the session
913  */
914 static struct Session *
915 get_session (struct MacEndpoint *endpoint,
916              const struct GNUNET_PeerIdentity *peer)
917 {
918   struct Session *session;
919
920   if (NULL != (session = lookup_session (endpoint, peer)))
921     return session;
922   return create_session (endpoint, peer);
923 }
924
925
926 /**
927  * Function called once we have successfully given the fragment
928  * message to the SUID helper process and we are thus ready for
929  * the next fragment.
930  *
931  * @param cls the `struct FragmentMessage *`
932  * @param result result of the operation (#GNUNET_OK on success,
933  *        #GNUNET_NO if the helper died, #GNUNET_SYSERR
934  *        if the helper was stopped)
935  */
936 static void
937 fragment_transmission_done (void *cls,
938                             int result)
939 {
940   struct FragmentMessage *fm = cls;
941
942
943   fm->sh = NULL;
944   GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
945 }
946
947
948 /**
949  * Transmit a fragment of a message.
950  *
951  * @param cls `struct FragmentMessage *` this fragment message belongs to
952  * @param hdr pointer to the start of the fragment message
953  */
954 static void
955 transmit_fragment (void *cls,
956                    const struct GNUNET_MessageHeader *hdr)
957 {
958   struct FragmentMessage *fm = cls;
959   struct MacEndpoint *endpoint = fm->macendpoint;
960   size_t size;
961   uint16_t msize;
962
963   if (NULL == endpoint)
964   {
965     GNUNET_break (0);
966     return;
967   }
968   msize = ntohs (hdr->size);
969   size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + msize;
970   {
971     char buf[size];
972     struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radio_header;
973
974     radio_header = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) buf;
975     get_radiotap_header (endpoint, radio_header, size);
976     get_wlan_header (endpoint->plugin,
977                      &radio_header->frame,
978                      &endpoint->wlan_addr.mac,
979                      sizeof (endpoint->wlan_addr.mac));
980     memcpy (&radio_header[1], hdr, msize);
981     GNUNET_assert (NULL == fm->sh);
982     fm->sh = GNUNET_HELPER_send (endpoint->plugin->suid_helper,
983                                  &radio_header->header,
984                                  GNUNET_NO,
985                                  &fragment_transmission_done, fm);
986     fm->size_on_wire += size;
987     if (NULL != fm->sh)
988       GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
989                                 _("# message fragments sent"),
990                                 1,
991                                 GNUNET_NO);
992     else
993       GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
994     GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
995                               "# bytes currently in buffers",
996                               -msize, GNUNET_NO);
997     GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
998                               "# bytes transmitted",
999                               msize, GNUNET_NO);
1000   }
1001 }
1002
1003
1004 /**
1005  * Frees the space of a message in the fragment queue (send queue)
1006  *
1007  * @param fm message to free
1008  */
1009 static void
1010 free_fragment_message (struct FragmentMessage *fm)
1011 {
1012   struct MacEndpoint *endpoint = fm->macendpoint;
1013
1014   GNUNET_STATISTICS_update (endpoint->plugin->env->stats,
1015                             _("# messages pending (with fragmentation)"),
1016                             -1, GNUNET_NO);
1017   GNUNET_CONTAINER_DLL_remove (endpoint->sending_messages_head,
1018                                endpoint->sending_messages_tail,
1019                                fm);
1020   if (NULL != fm->sh)
1021   {
1022     GNUNET_HELPER_send_cancel (fm->sh);
1023     fm->sh = NULL;
1024   }
1025   GNUNET_FRAGMENT_context_destroy (fm->fragcontext,
1026                                    &endpoint->msg_delay,
1027                                    &endpoint->ack_delay);
1028   if (fm->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1029   {
1030     GNUNET_SCHEDULER_cancel (fm->timeout_task);
1031     fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1032   }
1033   GNUNET_free (fm);
1034 }
1035
1036
1037 /**
1038  * A FragmentMessage has timed out.  Remove it.
1039  *
1040  * @param cls pointer to the 'struct FragmentMessage'
1041  * @param tc unused
1042  */
1043 static void
1044 fragmentmessage_timeout (void *cls,
1045                          const struct GNUNET_SCHEDULER_TaskContext *tc)
1046 {
1047   struct FragmentMessage *fm = cls;
1048
1049   fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1050   if (NULL != fm->cont)
1051   {
1052     fm->cont (fm->cont_cls,
1053               &fm->target,
1054               GNUNET_SYSERR,
1055               fm->size_payload,
1056               fm->size_on_wire);
1057     fm->cont = NULL;
1058   }
1059   free_fragment_message (fm);
1060 }
1061
1062
1063 /**
1064  * Transmit a message to the given destination with fragmentation.
1065  *
1066  * @param endpoint desired destination
1067  * @param timeout how long can the message wait?
1068  * @param target peer that should receive the message
1069  * @param msg message to transmit
1070  * @param payload_size bytes of payload
1071  * @param cont continuation to call once the message has
1072  *        been transmitted (or if the transport is ready
1073  *        for the next transmission call; or if the
1074  *        peer disconnected...); can be NULL
1075  * @param cont_cls closure for @a cont
1076  */
1077 static void
1078 send_with_fragmentation (struct MacEndpoint *endpoint,
1079                          struct GNUNET_TIME_Relative timeout,
1080                          const struct GNUNET_PeerIdentity *target,
1081                          const struct GNUNET_MessageHeader *msg,
1082                          size_t payload_size,
1083                          GNUNET_TRANSPORT_TransmitContinuation cont,
1084                          void *cont_cls)
1085
1086 {
1087   struct FragmentMessage *fm;
1088   struct Plugin *plugin;
1089
1090   plugin = endpoint->plugin;
1091   fm = GNUNET_new (struct FragmentMessage);
1092   fm->macendpoint = endpoint;
1093   fm->target = *target;
1094   fm->size_payload = payload_size;
1095   fm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1096   fm->cont = cont;
1097   fm->cont_cls = cont_cls;
1098   /* 1 MBit/s typical data rate, 1430 byte fragments => ~100 ms per message */
1099   fm->fragcontext =
1100     GNUNET_FRAGMENT_context_create (plugin->env->stats,
1101                                     WLAN_MTU,
1102                                     &plugin->tracker,
1103                                     endpoint->msg_delay,
1104                                     endpoint->ack_delay,
1105                                     msg,
1106                                     &transmit_fragment, fm);
1107   fm->timeout_task =
1108     GNUNET_SCHEDULER_add_delayed (timeout,
1109                                   &fragmentmessage_timeout, fm);
1110   GNUNET_CONTAINER_DLL_insert_tail (endpoint->sending_messages_head,
1111                                     endpoint->sending_messages_tail,
1112                                     fm);
1113 }
1114
1115
1116 /**
1117  * Free a MAC endpoint.
1118  *
1119  * @param endpoint pointer to the MacEndpoint to free
1120  */
1121 static void
1122 free_macendpoint (struct MacEndpoint *endpoint)
1123 {
1124   struct Plugin *plugin = endpoint->plugin;
1125   struct FragmentMessage *fm;
1126   struct Session *session;
1127
1128   GNUNET_STATISTICS_update (plugin->env->stats,
1129                             _("# MAC endpoints allocated"),
1130                             -1,
1131                             GNUNET_NO);
1132   while (NULL != (session = endpoint->sessions_head))
1133     wlan_plugin_disconnect_session (plugin,
1134                                     session);
1135   while (NULL != (fm = endpoint->sending_messages_head))
1136     free_fragment_message (fm);
1137   GNUNET_CONTAINER_DLL_remove (plugin->mac_head,
1138                                plugin->mac_tail,
1139                                endpoint);
1140
1141   if (NULL != endpoint->defrag)
1142   {
1143     GNUNET_DEFRAGMENT_context_destroy(endpoint->defrag);
1144     endpoint->defrag = NULL;
1145   }
1146
1147   plugin->mac_count--;
1148   if (GNUNET_SCHEDULER_NO_TASK != endpoint->timeout_task)
1149   {
1150     GNUNET_SCHEDULER_cancel (endpoint->timeout_task);
1151     endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1152   }
1153   GNUNET_free (endpoint);
1154 }
1155
1156
1157 /**
1158  * A MAC endpoint is timing out.  Clean up.
1159  *
1160  * @param cls pointer to the `struct MacEndpoint *`
1161  * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
1162  */
1163 static void
1164 macendpoint_timeout (void *cls,
1165                      const struct GNUNET_SCHEDULER_TaskContext *tc)
1166 {
1167   struct MacEndpoint *endpoint = cls;
1168   struct GNUNET_TIME_Relative timeout;
1169
1170   endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1171   timeout = GNUNET_TIME_absolute_get_remaining (endpoint->timeout);
1172   if (0 == timeout.rel_value_us)
1173   {
1174     free_macendpoint (endpoint);
1175     return;
1176   }
1177   endpoint->timeout_task =
1178     GNUNET_SCHEDULER_add_delayed (timeout,
1179                                   &macendpoint_timeout,
1180                                   endpoint);
1181 }
1182
1183
1184 /**
1185  * Find (or create) a MacEndpoint with a specific MAC address
1186  *
1187  * @param plugin pointer to the plugin struct
1188  * @param mac the MAC address of the endpoint
1189  * @return handle to our data structure for this MAC
1190  */
1191 static struct MacEndpoint *
1192 create_macendpoint (struct Plugin *plugin,
1193                     struct WlanAddress *mac)
1194 {
1195   struct MacEndpoint *pos;
1196
1197   for (pos = plugin->mac_head; NULL != pos; pos = pos->next)
1198     if (0 == memcmp (mac, &pos->wlan_addr, sizeof (pos->wlan_addr)))
1199       return pos;
1200   pos = GNUNET_new (struct MacEndpoint);
1201   pos->wlan_addr = (*mac);
1202   pos->plugin = plugin;
1203   pos->defrag =
1204     GNUNET_DEFRAGMENT_context_create (plugin->env->stats,
1205                                       WLAN_MTU,
1206                                       MESSAGES_IN_DEFRAG_QUEUE_PER_MAC,
1207                                       pos,
1208                                       &wlan_data_message_handler,
1209                                       &send_ack);
1210
1211   pos->msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1212   pos->ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
1213   pos->timeout = GNUNET_TIME_relative_to_absolute (MACENDPOINT_TIMEOUT);
1214   pos->timeout_task =
1215       GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
1216                                     pos);
1217   GNUNET_CONTAINER_DLL_insert (plugin->mac_head,
1218                                plugin->mac_tail,
1219                                pos);
1220   plugin->mac_count++;
1221   GNUNET_STATISTICS_update (plugin->env->stats,
1222                             _("# MAC endpoints allocated"),
1223                             1, GNUNET_NO);
1224   LOG (GNUNET_ERROR_TYPE_DEBUG,
1225        "New MAC endpoint `%s'\n",
1226        wlan_plugin_address_to_string (NULL,
1227                                       &pos->wlan_addr,
1228                                       sizeof (struct WlanAddress)));
1229   return pos;
1230 }
1231
1232
1233 /**
1234  * Function obtain the network type for a session
1235  *
1236  * @param cls closure (`struct Plugin*`)
1237  * @param session the session
1238  * @return the network type in HBO or #GNUNET_SYSERR
1239  */
1240 static enum GNUNET_ATS_Network_Type
1241 wlan_plugin_get_network (void *cls,
1242                          struct Session *session)
1243 {
1244 #if BUILD_WLAN
1245   return GNUNET_ATS_NET_WLAN;
1246 #else
1247   return GNUNET_ATS_NET_BT;
1248 #endif
1249 }
1250
1251
1252 /**
1253  * Creates a new outbound session the transport service will use to
1254  * send data to the peer
1255  *
1256  * @param cls the `struct Plugin *`
1257  * @param address the address
1258  * @return the session or NULL of max connections exceeded
1259  */
1260 static struct Session *
1261 wlan_plugin_get_session (void *cls,
1262                          const struct GNUNET_HELLO_Address *address)
1263 {
1264   struct Plugin *plugin = cls;
1265   struct MacEndpoint *endpoint;
1266
1267   if (NULL == address)
1268     return NULL;
1269   if (sizeof (struct WlanAddress) != address->address_length)
1270   {
1271     GNUNET_break (0);
1272     return NULL;
1273   }
1274   LOG (GNUNET_ERROR_TYPE_DEBUG,
1275        "Service asked to create session for peer `%s' with MAC `%s'\n",
1276        GNUNET_i2s (&address->peer),
1277        wlan_plugin_address_to_string (NULL,
1278                                       address->address,
1279                                       address->address_length));
1280   endpoint = create_macendpoint (plugin,
1281                                  (struct WlanAddress *) address->address);
1282   return get_session (endpoint, &address->peer);
1283 }
1284
1285
1286 /**
1287  * Function that can be used to force the plugin to disconnect
1288  * from the given peer and cancel all previous transmissions
1289  * (and their continuation).
1290  *
1291  * @param cls closure
1292  * @param target peer from which to disconnect
1293  */
1294 static void
1295 wlan_plugin_disconnect_peer (void *cls,
1296                              const struct GNUNET_PeerIdentity *target)
1297 {
1298   struct Plugin *plugin = cls;
1299   struct Session *session;
1300   struct MacEndpoint *endpoint;
1301
1302   for (endpoint = plugin->mac_head; NULL != endpoint; endpoint = endpoint->next)
1303     for (session = endpoint->sessions_head; NULL != session; session = session->next)
1304       if (0 == memcmp (target, &session->target,
1305                        sizeof (struct GNUNET_PeerIdentity)))
1306       {
1307         wlan_plugin_disconnect_session (plugin, session);
1308         break; /* inner-loop only (in case peer has another MAC as well!) */
1309       }
1310 }
1311
1312
1313 /**
1314  * Function that can be used by the transport service to transmit
1315  * a message using the plugin.   Note that in the case of a
1316  * peer disconnecting, the continuation MUST be called
1317  * prior to the disconnect notification itself.  This function
1318  * will be called with this peer's HELLO message to initiate
1319  * a fresh connection to another peer.
1320  *
1321  * @param cls closure
1322  * @param session which session must be used
1323  * @param msgbuf the message to transmit
1324  * @param msgbuf_size number of bytes in @a msgbuf
1325  * @param priority how important is the message (most plugins will
1326  *                 ignore message priority and just FIFO)
1327  * @param to how long to wait at most for the transmission (does not
1328  *                require plugins to discard the message after the timeout,
1329  *                just advisory for the desired delay; most plugins will ignore
1330  *                this as well)
1331  * @param cont continuation to call once the message has
1332  *        been transmitted (or if the transport is ready
1333  *        for the next transmission call; or if the
1334  *        peer disconnected...); can be NULL
1335  * @param cont_cls closure for @a cont
1336  * @return number of bytes used (on the physical network, with overheads);
1337  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
1338  *         and does NOT mean that the message was not transmitted (DV)
1339  */
1340 static ssize_t
1341 wlan_plugin_send (void *cls,
1342                   struct Session *session,
1343                   const char *msgbuf, size_t msgbuf_size,
1344                   unsigned int priority,
1345                   struct GNUNET_TIME_Relative to,
1346                   GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1347 {
1348   struct Plugin *plugin = cls;
1349   struct WlanHeader *wlanheader;
1350   size_t size = msgbuf_size + sizeof (struct WlanHeader);
1351   char buf[size] GNUNET_ALIGN;
1352
1353   LOG (GNUNET_ERROR_TYPE_DEBUG,
1354        "Transmitting %u bytes of payload to peer `%s' (starting with %u byte message of type %u)\n",
1355        msgbuf_size,
1356        GNUNET_i2s (&session->target),
1357        (unsigned int) ntohs (((struct GNUNET_MessageHeader*)msgbuf)->size),
1358        (unsigned int) ntohs (((struct GNUNET_MessageHeader*)msgbuf)->type));
1359   wlanheader = (struct WlanHeader *) buf;
1360   wlanheader->header.size = htons (msgbuf_size + sizeof (struct WlanHeader));
1361   wlanheader->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA);
1362   wlanheader->sender = *plugin->env->my_identity;
1363   wlanheader->target = session->target;
1364   wlanheader->crc = htonl (GNUNET_CRYPTO_crc32_n (msgbuf, msgbuf_size));
1365   memcpy (&wlanheader[1], msgbuf, msgbuf_size);
1366
1367   GNUNET_STATISTICS_update (plugin->env->stats,
1368                             "# bytes currently in buffers",
1369                             msgbuf_size,
1370                             GNUNET_NO);
1371
1372   send_with_fragmentation (session->mac,
1373                            to,
1374                            &session->target,
1375                            &wlanheader->header,
1376                            msgbuf_size,
1377                            cont, cont_cls);
1378   return size;
1379 }
1380
1381
1382 /**
1383  * We have received data from the WLAN via some session.  Process depending
1384  * on the message type (HELLO, DATA, FRAGMENTATION or FRAGMENTATION-ACK).
1385  *
1386  * @param cls pointer to the plugin
1387  * @param client pointer to the session this message belongs to
1388  * @param hdr start of the message
1389  */
1390 static int
1391 process_data (void *cls,
1392               void *client,
1393               const struct GNUNET_MessageHeader *hdr)
1394 {
1395   struct Plugin *plugin = cls;
1396   struct GNUNET_HELLO_Address *address;
1397   struct MacAndSession *mas = client;
1398   struct MacAndSession xmas;
1399   struct GNUNET_ATS_Information ats;
1400   struct FragmentMessage *fm;
1401   struct GNUNET_PeerIdentity tmpsource;
1402   const struct WlanHeader *wlanheader;
1403   int ret;
1404   uint16_t msize;
1405
1406   ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1407 #if BUILD_WLAN
1408   ats.value = htonl (GNUNET_ATS_NET_WLAN);
1409 #else
1410   ats.value = htonl (GNUNET_ATS_NET_BT);
1411 #endif
1412   msize = ntohs (hdr->size);
1413
1414   GNUNET_STATISTICS_update (plugin->env->stats,
1415                             "# bytes received",
1416                             msize, GNUNET_NO);
1417
1418   switch (ntohs (hdr->type))
1419   {
1420   case GNUNET_MESSAGE_TYPE_HELLO:
1421
1422     if (GNUNET_OK !=
1423         GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hdr,
1424                              &tmpsource))
1425     {
1426       GNUNET_break_op (0);
1427       break;
1428     }
1429     if (NULL == mas->endpoint)
1430     {
1431       GNUNET_break (0);
1432       break;
1433     }
1434
1435     LOG (GNUNET_ERROR_TYPE_DEBUG,
1436          "Processing %u bytes of HELLO from peer `%s' at MAC %s\n",
1437          (unsigned int) msize,
1438          GNUNET_i2s (&tmpsource),
1439          wlan_plugin_address_to_string (NULL,
1440                                         &mas->endpoint->wlan_addr,
1441                                         sizeof (mas->endpoint->wlan_addr)));
1442
1443     GNUNET_STATISTICS_update (plugin->env->stats,
1444                               _("# HELLO messages received"), 1,
1445                               GNUNET_NO);
1446     address = GNUNET_HELLO_address_allocate (&tmpsource,
1447                                              PLUGIN_NAME,
1448                                              &mas->endpoint->wlan_addr,
1449                                              sizeof (mas->endpoint->wlan_addr),
1450                                              GNUNET_HELLO_ADDRESS_INFO_INBOUND);
1451     plugin->env->receive (plugin->env->cls,
1452                           address,
1453                           mas->session,
1454                           hdr);
1455     plugin->env->update_address_metrics (plugin->env->cls,
1456                                          address,
1457                                          mas->session,
1458                                          &ats, 1);
1459     GNUNET_HELLO_address_free (address);
1460     break;
1461   case GNUNET_MESSAGE_TYPE_FRAGMENT:
1462     if (NULL == mas->endpoint)
1463     {
1464       GNUNET_break (0);
1465       break;
1466     }
1467     LOG (GNUNET_ERROR_TYPE_DEBUG,
1468          "Processing %u bytes of FRAGMENT from MAC %s\n",
1469          (unsigned int) msize,
1470          wlan_plugin_address_to_string (NULL,
1471                                         &mas->endpoint->wlan_addr,
1472                                         sizeof (mas->endpoint->wlan_addr)));
1473     GNUNET_STATISTICS_update (plugin->env->stats,
1474                               _("# fragments received"),
1475                               1,
1476                               GNUNET_NO);
1477     (void) GNUNET_DEFRAGMENT_process_fragment (mas->endpoint->defrag,
1478                                               hdr);
1479     break;
1480   case GNUNET_MESSAGE_TYPE_FRAGMENT_ACK:
1481     if (NULL == mas->endpoint)
1482     {
1483       GNUNET_break (0);
1484       break;
1485     }
1486     GNUNET_STATISTICS_update (plugin->env->stats,
1487                               _("# ACKs received"),
1488                               1, GNUNET_NO);
1489     for (fm = mas->endpoint->sending_messages_head; NULL != fm; fm = fm->next)
1490     {
1491       ret = GNUNET_FRAGMENT_process_ack (fm->fragcontext, hdr);
1492       if (GNUNET_OK == ret)
1493       {
1494         LOG (GNUNET_ERROR_TYPE_DEBUG,
1495              "Got last ACK, finished message transmission to `%s' (%p)\n",
1496              wlan_plugin_address_to_string (NULL,
1497                                             &mas->endpoint->wlan_addr,
1498                                             sizeof (mas->endpoint->wlan_addr)),
1499              fm);
1500         mas->endpoint->timeout = GNUNET_TIME_relative_to_absolute (MACENDPOINT_TIMEOUT);
1501         if (NULL != fm->cont)
1502         {
1503           fm->cont (fm->cont_cls,
1504                     &fm->target,
1505                     GNUNET_OK,
1506                     fm->size_payload,
1507                     fm->size_on_wire);
1508           fm->cont = NULL;
1509         }
1510         free_fragment_message (fm);
1511         break;
1512       }
1513       if (GNUNET_NO == ret)
1514       {
1515         LOG (GNUNET_ERROR_TYPE_DEBUG,
1516              "Got an ACK, message transmission to `%s' not yet finished\n",
1517              wlan_plugin_address_to_string (NULL,
1518                                             &mas->endpoint->wlan_addr,
1519                                             sizeof (mas->endpoint->wlan_addr)));
1520         break;
1521       }
1522     }
1523     LOG (GNUNET_ERROR_TYPE_DEBUG,
1524          "ACK not matched against any active fragmentation with MAC `%s'\n",
1525          wlan_plugin_address_to_string (NULL,
1526                                         &mas->endpoint->wlan_addr,
1527                                         sizeof (mas->endpoint->wlan_addr)));
1528     break;
1529   case GNUNET_MESSAGE_TYPE_WLAN_DATA:
1530     if (NULL == mas->endpoint)
1531     {
1532       GNUNET_break (0);
1533       break;
1534     }
1535     if (msize < sizeof (struct WlanHeader))
1536     {
1537       GNUNET_break (0);
1538       break;
1539     }
1540     wlanheader = (const struct WlanHeader *) hdr;
1541     if (0 != memcmp (&wlanheader->target,
1542                      plugin->env->my_identity,
1543                      sizeof (struct GNUNET_PeerIdentity)))
1544     {
1545       LOG (GNUNET_ERROR_TYPE_DEBUG,
1546            "Data for `%s', not for me, ignoring\n",
1547            GNUNET_i2s (&wlanheader->target));
1548       break;
1549     }
1550     if (ntohl (wlanheader->crc) !=
1551         GNUNET_CRYPTO_crc32_n (&wlanheader[1],
1552                                msize - sizeof (struct WlanHeader)))
1553     {
1554       GNUNET_STATISTICS_update (plugin->env->stats,
1555                                 _("# DATA messages discarded due to CRC32 error"),
1556                                 1,
1557                                 GNUNET_NO);
1558       break;
1559     }
1560     xmas.endpoint = mas->endpoint;
1561     if (NULL == (xmas.session = lookup_session (mas->endpoint,
1562                                                 &wlanheader->sender)))
1563     {
1564       xmas.session = create_session (mas->endpoint,
1565                                      &wlanheader->sender);
1566       address = GNUNET_HELLO_address_allocate (&wlanheader->sender,
1567                                                PLUGIN_NAME,
1568                                                &mas->endpoint->wlan_addr,
1569                                                sizeof (struct WlanAddress),
1570                                                GNUNET_HELLO_ADDRESS_INFO_NONE);
1571       plugin->env->session_start (NULL,
1572                                   address,
1573                                   xmas.session,
1574                                   NULL, 0);
1575       LOG (GNUNET_ERROR_TYPE_DEBUG,
1576           "Notifying transport about peer `%s''s new session %p \n",
1577            GNUNET_i2s (&wlanheader->sender),
1578            xmas.session);
1579       GNUNET_HELLO_address_free (address);
1580     }
1581     LOG (GNUNET_ERROR_TYPE_DEBUG,
1582          "Processing %u bytes of DATA from peer `%s'\n",
1583          (unsigned int) msize,
1584          GNUNET_i2s (&wlanheader->sender));
1585     xmas.session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1586     (void) GNUNET_SERVER_mst_receive (plugin->wlan_header_payload_tokenizer,
1587                                       &xmas,
1588                                       (const char *) &wlanheader[1],
1589                                       msize - sizeof (struct WlanHeader),
1590                                       GNUNET_YES,
1591                                       GNUNET_NO);
1592     break;
1593   default:
1594     if (NULL == mas->endpoint)
1595     {
1596       GNUNET_break (0);
1597       break;
1598     }
1599     if (NULL == mas->session)
1600     {
1601       GNUNET_break (0);
1602       break;
1603     }
1604     LOG (GNUNET_ERROR_TYPE_DEBUG,
1605          "Received packet with %u bytes of type %u from peer %s\n",
1606          (unsigned int) msize,
1607          (unsigned int) ntohs (hdr->type),
1608          GNUNET_i2s (&mas->session->target));
1609     plugin->env->receive (plugin->env->cls,
1610                           mas->session->address,
1611                           mas->session,
1612                           hdr);
1613     plugin->env->update_address_metrics (plugin->env->cls,
1614                                          mas->session->address,
1615                                          mas->session,
1616                                          &ats, 1);
1617     break;
1618   }
1619   return GNUNET_OK;
1620 }
1621
1622
1623 /**
1624  * Function used for to process the data from the suid process
1625  *
1626  * @param cls the plugin handle
1627  * @param client client that send the data (not used)
1628  * @param hdr header of the GNUNET_MessageHeader
1629  */
1630 static int
1631 handle_helper_message (void *cls, void *client,
1632                        const struct GNUNET_MessageHeader *hdr)
1633 {
1634   struct Plugin *plugin = cls;
1635   struct GNUNET_HELLO_Address *my_address;
1636   const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rxinfo;
1637   const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *cm;
1638   struct WlanAddress wa;
1639   struct MacAndSession mas;
1640   uint16_t msize;
1641
1642   msize = ntohs (hdr->size);
1643   switch (ntohs (hdr->type))
1644   {
1645   case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
1646     if (msize != sizeof (struct GNUNET_TRANSPORT_WLAN_HelperControlMessage))
1647     {
1648       GNUNET_break (0);
1649       break;
1650     }
1651     cm = (const struct GNUNET_TRANSPORT_WLAN_HelperControlMessage *) hdr;
1652     if (GNUNET_YES == plugin->have_mac)
1653     {
1654       if (0 == memcmp (&plugin->mac_address,
1655                        &cm->mac,
1656                        sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1657         break; /* no change */
1658       /* remove old address */
1659       memset (&wa, 0, sizeof (struct WlanAddress));
1660       wa.mac = plugin->mac_address;
1661       wa.options = htonl(plugin->options);
1662       my_address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1663                                                   PLUGIN_NAME,
1664                                                   &wa, sizeof (wa),
1665                                                   GNUNET_HELLO_ADDRESS_INFO_NONE);
1666       plugin->env->notify_address (plugin->env->cls,
1667                                    GNUNET_NO,
1668                                    my_address);
1669       GNUNET_HELLO_address_free (my_address);
1670     }
1671     plugin->mac_address = cm->mac;
1672     plugin->have_mac = GNUNET_YES;
1673
1674     memset (&wa, 0, sizeof (struct WlanAddress));
1675     wa.mac = plugin->mac_address;
1676     wa.options = htonl(plugin->options);
1677     my_address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1678                                                 PLUGIN_NAME,
1679                                                 &wa, sizeof (wa),
1680                                                 GNUNET_HELLO_ADDRESS_INFO_NONE);
1681
1682     LOG (GNUNET_ERROR_TYPE_DEBUG,
1683          "Received WLAN_HELPER_CONTROL message with MAC address `%s' for peer `%s'\n",
1684          mac_to_string (&cm->mac),
1685          GNUNET_i2s (plugin->env->my_identity));
1686     plugin->env->notify_address (plugin->env->cls,
1687                                  GNUNET_YES,
1688                                  my_address);
1689     GNUNET_HELLO_address_free (my_address);
1690     break;
1691   case GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER:
1692     LOG (GNUNET_ERROR_TYPE_DEBUG,
1693          "Got data message from helper with %u bytes\n",
1694          msize);
1695     GNUNET_STATISTICS_update (plugin->env->stats,
1696                               _("# DATA messages received"), 1,
1697                               GNUNET_NO);
1698     if (msize < sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage))
1699     {
1700       LOG (GNUNET_ERROR_TYPE_DEBUG,
1701            "Size of packet is too small (%u bytes < %u)\n",
1702            msize,  sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage));
1703       break;
1704     }
1705     rxinfo = (const struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) hdr;
1706
1707     /* check if message is actually for us */
1708     if (0 != memcmp (&rxinfo->frame.addr3, &mac_bssid_gnunet,
1709                      sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1710     {
1711       /* Not the GNUnet BSSID */
1712       break;
1713     }
1714     if ( (0 != memcmp (&rxinfo->frame.addr1, &bc_all_mac,
1715                        sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))) &&
1716          (0 != memcmp (&rxinfo->frame.addr1, &plugin->mac_address,
1717                        sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress))) )
1718     {
1719       /* Neither broadcast nor specifically for us */
1720       break;
1721     }
1722     if (0 == memcmp (&rxinfo->frame.addr2, &plugin->mac_address,
1723                      sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)))
1724     {
1725       /* packet is FROM us, thus not FOR us */
1726       break;
1727     }
1728
1729     GNUNET_STATISTICS_update (plugin->env->stats,
1730                               _("# DATA messages processed"),
1731                               1, GNUNET_NO);
1732     LOG (GNUNET_ERROR_TYPE_DEBUG,
1733          "Receiving %u bytes of data from MAC `%s'\n",
1734          (unsigned int) (msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)),
1735          mac_to_string (&rxinfo->frame.addr2));
1736     wa.mac = rxinfo->frame.addr2;
1737     wa.options = htonl (0);
1738     mas.endpoint = create_macendpoint (plugin, &wa);
1739     mas.session = NULL;
1740     (void) GNUNET_SERVER_mst_receive (plugin->helper_payload_tokenizer,
1741                                       &mas,
1742                                       (const char*) &rxinfo[1],
1743                                       msize - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage),
1744                                       GNUNET_YES, GNUNET_NO);
1745     break;
1746   default:
1747     GNUNET_break (0);
1748     LOG (GNUNET_ERROR_TYPE_DEBUG,
1749          "Unexpected message of type %u (%u bytes)",
1750          ntohs (hdr->type), ntohs (hdr->size));
1751     break;
1752   }
1753   return GNUNET_OK;
1754 }
1755
1756
1757 /**
1758  * Task to (periodically) send a HELLO beacon
1759  *
1760  * @param cls pointer to the plugin struct
1761  * @param tc scheduler context
1762  */
1763 static void
1764 send_hello_beacon (void *cls,
1765                    const struct GNUNET_SCHEDULER_TaskContext *tc)
1766 {
1767   struct Plugin *plugin = cls;
1768   uint16_t size;
1769   uint16_t hello_size;
1770   struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *radioHeader;
1771   const struct GNUNET_MessageHeader *hello;
1772
1773   hello = plugin->env->get_our_hello ();
1774   hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1775   GNUNET_assert (sizeof (struct WlanHeader) + hello_size <= WLAN_MTU);
1776   size = sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) + hello_size;
1777   {
1778     char buf[size] GNUNET_ALIGN;
1779
1780     LOG (GNUNET_ERROR_TYPE_DEBUG,
1781          "Sending %u byte HELLO beacon\n",
1782          (unsigned int) size);
1783     radioHeader = (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage*) buf;
1784     get_radiotap_header (NULL, radioHeader, size);
1785     get_wlan_header (plugin, &radioHeader->frame, &bc_all_mac, size);
1786     memcpy (&radioHeader[1], hello, hello_size);
1787     if (NULL !=
1788         GNUNET_HELPER_send (plugin->suid_helper,
1789                             &radioHeader->header,
1790                             GNUNET_YES /* can drop */,
1791                             NULL, NULL))
1792       GNUNET_STATISTICS_update (plugin->env->stats,
1793                                 _("# HELLO beacons sent"),
1794                                 1, GNUNET_NO);
1795   }
1796   plugin->beacon_task =
1797     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1798                                   (HELLO_BEACON_SCALING_FACTOR,
1799                                    plugin->mac_count + 1),
1800                                   &send_hello_beacon,
1801                                   plugin);
1802
1803 }
1804
1805
1806 /**
1807  * Another peer has suggested an address for this
1808  * peer and transport plugin.  Check that this could be a valid
1809  * address.  If so, consider adding it to the list
1810  * of addresses.
1811  *
1812  * @param cls closure
1813  * @param addr pointer to the address
1814  * @param addrlen length of @a addr
1815  * @return #GNUNET_OK if this is a plausible address for this peer
1816  *         and transport
1817  */
1818 static int
1819 wlan_plugin_address_suggested (void *cls,
1820                                const void *addr,
1821                                size_t addrlen)
1822 {
1823   struct Plugin *plugin = cls;
1824   struct WlanAddress *wa = (struct WlanAddress *) addr;
1825
1826   if (addrlen != sizeof (struct WlanAddress))
1827   {
1828     GNUNET_break_op (0);
1829     return GNUNET_SYSERR;
1830   }
1831   if (GNUNET_YES != plugin->have_mac)
1832   {
1833     LOG (GNUNET_ERROR_TYPE_DEBUG,
1834          "Rejecting MAC `%s': I don't know my MAC!\n",
1835          mac_to_string (addr));
1836     return GNUNET_NO; /* don't know my MAC */
1837   }
1838   if (0 != memcmp (&wa->mac,
1839                    &plugin->mac_address,
1840                    sizeof (wa->mac)))
1841   {
1842     LOG (GNUNET_ERROR_TYPE_DEBUG,
1843          "Rejecting MAC `%s': not my MAC!\n",
1844          mac_to_string (addr));
1845     return GNUNET_NO; /* not my MAC */
1846   }
1847   return GNUNET_OK;
1848 }
1849
1850
1851 /**
1852  * Convert the transports address to a nice, human-readable format.
1853  *
1854  * @param cls closure
1855  * @param type name of the transport that generated the address
1856  * @param addr one of the addresses of the host, NULL for the last address
1857  *        the specific address format depends on the transport
1858  * @param addrlen length of the address
1859  * @param numeric should (IP) addresses be displayed in numeric form?
1860  * @param timeout after how long should we give up?
1861  * @param asc function to call on each string
1862  * @param asc_cls closure for @a asc
1863  */
1864 static void
1865 wlan_plugin_address_pretty_printer (void *cls,
1866                                     const char *type,
1867                                     const void *addr,
1868                                     size_t addrlen,
1869                                     int numeric,
1870                                     struct GNUNET_TIME_Relative timeout,
1871                                     GNUNET_TRANSPORT_AddressStringCallback asc,
1872                                     void *asc_cls)
1873 {
1874   const char *ret;
1875
1876   if (sizeof (struct WlanAddress) == addrlen)
1877     ret = wlan_plugin_address_to_string (NULL,
1878                                          addr,
1879                                          addrlen);
1880   else
1881     ret = NULL;
1882   asc (asc_cls,
1883        ret,
1884        (NULL == ret) ? GNUNET_SYSERR : GNUNET_OK);
1885   asc (asc_cls, NULL, GNUNET_OK);
1886 }
1887
1888
1889 /**
1890  * Exit point from the plugin.
1891  *
1892  * @param cls pointer to the api struct
1893  */
1894 void *
1895 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
1896 {
1897   struct WlanAddress wa;
1898   struct GNUNET_HELLO_Address *address;
1899   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1900   struct Plugin *plugin = api->cls;
1901   struct MacEndpoint *endpoint;
1902   struct MacEndpoint *endpoint_next;
1903
1904   if (NULL == plugin)
1905   {
1906     GNUNET_free (api);
1907     return NULL;
1908   }
1909   if (GNUNET_YES == plugin->have_mac)
1910   {
1911     memset (&wa, 0, sizeof(wa));
1912     wa.options = htonl (plugin->options);
1913     wa.mac = plugin->mac_address;
1914     address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1915                                              PLUGIN_NAME,
1916                                              &wa, sizeof (struct WlanAddress),
1917                                              GNUNET_HELLO_ADDRESS_INFO_NONE);
1918
1919     plugin->env->notify_address (plugin->env->cls,
1920                                  GNUNET_NO,
1921                                  address);
1922     plugin->have_mac = GNUNET_NO;
1923     GNUNET_HELLO_address_free (address);
1924   }
1925
1926   if (GNUNET_SCHEDULER_NO_TASK != plugin->beacon_task)
1927   {
1928     GNUNET_SCHEDULER_cancel (plugin->beacon_task);
1929     plugin->beacon_task = GNUNET_SCHEDULER_NO_TASK;
1930   }
1931   if (NULL != plugin->suid_helper)
1932   {
1933     GNUNET_HELPER_stop (plugin->suid_helper,
1934                         GNUNET_NO);
1935     plugin->suid_helper = NULL;
1936   }
1937   endpoint_next = plugin->mac_head;
1938   while (NULL != (endpoint = endpoint_next))
1939   {
1940     endpoint_next = endpoint->next;
1941     free_macendpoint (endpoint);
1942   }
1943   if (NULL != plugin->fragment_data_tokenizer)
1944   {
1945     GNUNET_SERVER_mst_destroy (plugin->fragment_data_tokenizer);
1946     plugin->fragment_data_tokenizer = NULL;
1947   }
1948   if (NULL != plugin->wlan_header_payload_tokenizer)
1949   {
1950     GNUNET_SERVER_mst_destroy (plugin->wlan_header_payload_tokenizer);
1951     plugin->wlan_header_payload_tokenizer = NULL;
1952   }
1953   if (NULL != plugin->helper_payload_tokenizer)
1954   {
1955     GNUNET_SERVER_mst_destroy (plugin->helper_payload_tokenizer);
1956     plugin->helper_payload_tokenizer = NULL;
1957   }
1958   GNUNET_free_non_null (plugin->wlan_interface);
1959   GNUNET_free (plugin);
1960   GNUNET_free (api);
1961   return NULL;
1962 }
1963
1964
1965 /**
1966  * Function called to convert a string address to
1967  * a binary address.
1968  *
1969  * @param cls closure (`struct Plugin *`)
1970  * @param addr string address
1971  * @param addrlen length of the address
1972  * @param buf location to store the buffer
1973  * @param added location to store the number of bytes in the buffer.
1974  *        If the function returns #GNUNET_SYSERR, its contents are undefined.
1975  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1976  */
1977 static int
1978 wlan_plugin_string_to_address (void *cls,
1979                                const char *addr,
1980                                uint16_t addrlen,
1981                                void **buf,
1982                                size_t *added)
1983 {
1984   struct WlanAddress *wa;
1985   unsigned int a[6];
1986   unsigned int i;
1987   char plugin[5];
1988   uint32_t options;
1989
1990   if ((NULL == addr) || (0 == addrlen))
1991   {
1992     GNUNET_break (0);
1993     return GNUNET_SYSERR;
1994   }
1995   if ('\0' != addr[addrlen - 1])
1996   {
1997     GNUNET_break (0);
1998     return GNUNET_SYSERR;
1999   }
2000   if (strlen (addr) != addrlen - 1)
2001   {
2002     GNUNET_break (0);
2003     return GNUNET_SYSERR;
2004   }
2005
2006   if (8 != SSCANF (addr,
2007                    "%4s.%u.%X:%X:%X:%X:%X:%X",
2008                    plugin, &options,
2009                    &a[0], &a[1], &a[2],
2010                    &a[3], &a[4], &a[5]))
2011   {
2012     GNUNET_break (0);
2013     return GNUNET_SYSERR;
2014   }
2015   wa = GNUNET_new (struct WlanAddress);
2016   for (i=0;i<6;i++)
2017     wa->mac.mac[i] = a[i];
2018   wa->options = htonl (0);
2019   *buf = wa;
2020   *added = sizeof (struct WlanAddress);
2021   return GNUNET_OK;
2022 }
2023
2024
2025 /**
2026  * Begin monitoring sessions of a plugin.  There can only
2027  * be one active monitor per plugin (i.e. if there are
2028  * multiple monitors, the transport service needs to
2029  * multiplex the generated events over all of them).
2030  *
2031  * @param cls closure of the plugin
2032  * @param sic callback to invoke, NULL to disable monitor;
2033  *            plugin will being by iterating over all active
2034  *            sessions immediately and then enter monitor mode
2035  * @param sic_cls closure for @a sic
2036  */
2037 static void
2038 wlan_plugin_setup_monitor (void *cls,
2039                            GNUNET_TRANSPORT_SessionInfoCallback sic,
2040                            void *sic_cls)
2041 {
2042   struct Plugin *plugin = cls;
2043   struct MacEndpoint *mac;
2044   struct Session *session;
2045
2046   plugin->sic = sic;
2047   plugin->sic_cls = sic_cls;
2048   if (NULL != sic)
2049   {
2050     for (mac = plugin->mac_head; NULL != mac; mac = mac->next)
2051       for (session = mac->sessions_head; NULL != session; session = session->next)
2052       {
2053         notify_session_monitor (plugin,
2054                                 session,
2055                                 GNUNET_TRANSPORT_SS_INIT);
2056         notify_session_monitor (plugin,
2057                                 session,
2058                                 GNUNET_TRANSPORT_SS_UP);
2059       }
2060     sic (sic_cls, NULL, NULL);
2061   }
2062 }
2063
2064
2065
2066 /**
2067  * Function that will be called whenever the transport service wants to
2068  * notify the plugin that a session is still active and in use and
2069  * therefore the session timeout for this session has to be updated
2070  *
2071  * @param cls closure
2072  * @param peer which peer was the session for
2073  * @param session which session is being updated
2074  */
2075 static void
2076 wlan_plugin_update_session_timeout (void *cls,
2077                                     const struct GNUNET_PeerIdentity *peer,
2078                                     struct Session *session)
2079 {
2080   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != session->timeout_task);
2081   session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2082 }
2083
2084
2085 /**
2086  * Function that will be called whenever the transport service wants to
2087  * notify the plugin that the inbound quota changed and that the plugin
2088  * should update it's delay for the next receive value
2089  *
2090  * @param cls closure
2091  * @param peer which peer was the session for
2092  * @param session which session is being updated
2093  * @param delay new delay to use for receiving
2094  */
2095 static void
2096 wlan_plugin_update_inbound_delay (void *cls,
2097                                   const struct GNUNET_PeerIdentity *peer,
2098                                   struct Session *session,
2099                                   struct GNUNET_TIME_Relative delay)
2100 {
2101   /* does nothing, as inbound delay is not supported by WLAN */
2102 }
2103
2104
2105 /**
2106  * Entry point for the plugin.
2107  *
2108  * @param cls closure, the `struct GNUNET_TRANSPORT_PluginEnvironment *`
2109  * @return the `struct GNUNET_TRANSPORT_PluginFunctions *` or NULL on error
2110  */
2111 void *
2112 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2113 {
2114   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2115   struct GNUNET_TRANSPORT_PluginFunctions *api;
2116   struct Plugin *plugin;
2117   char *wlan_interface;
2118   unsigned long long testmode;
2119   char *binary;
2120
2121   /* check for 'special' mode */
2122   if (NULL == env->receive)
2123   {
2124     /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2125        initialze the plugin or the API */
2126     api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2127     api->cls = NULL;
2128     api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2129     api->address_to_string = &wlan_plugin_address_to_string;
2130     api->string_to_address = &wlan_plugin_string_to_address;
2131     return api;
2132   }
2133
2134   testmode = 0;
2135   /* check configuration */
2136   if ( (GNUNET_YES ==
2137         GNUNET_CONFIGURATION_have_value (env->cfg,
2138                                          CONFIG_NAME,
2139                                          "TESTMODE")) &&
2140        ( (GNUNET_SYSERR ==
2141           GNUNET_CONFIGURATION_get_value_number (env->cfg,
2142                                                  CONFIG_NAME,
2143                                                  "TESTMODE",
2144                                                  &testmode)) ||
2145          (testmode > 2) ) )
2146   {
2147     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2148                                CONFIG_NAME,
2149                                "TESTMODE");
2150     return NULL;
2151   }
2152   binary = GNUNET_OS_get_libexec_binary_path (HELPER_NAME);
2153   if ( (0 == testmode) &&
2154        (GNUNET_YES !=
2155         GNUNET_OS_check_helper_binary (binary,
2156                                        GNUNET_YES,
2157                                        NULL)) )
2158   {
2159     LOG (GNUNET_ERROR_TYPE_ERROR,
2160          _("Helper binary `%s' not SUID, cannot run WLAN transport\n"),
2161          HELPER_NAME);
2162     GNUNET_free (binary);
2163     return NULL;
2164   }
2165     GNUNET_free (binary);
2166   if (GNUNET_YES !=
2167       GNUNET_CONFIGURATION_get_value_string (env->cfg,
2168                                              CONFIG_NAME,
2169                                              "INTERFACE",
2170                                              &wlan_interface))
2171   {
2172     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2173                                CONFIG_NAME,
2174                                "INTERFACE");
2175     return NULL;
2176   }
2177
2178   plugin = GNUNET_new (struct Plugin);
2179   plugin->wlan_interface = wlan_interface;
2180   plugin->env = env;
2181   GNUNET_STATISTICS_set (plugin->env->stats,
2182                          _("# sessions allocated"),
2183                          0, GNUNET_NO);
2184   GNUNET_STATISTICS_set (plugin->env->stats,
2185                          _("# MAC endpoints allocated"),
2186                          0, 0);
2187   GNUNET_BANDWIDTH_tracker_init (&plugin->tracker, NULL, NULL,
2188                                  GNUNET_BANDWIDTH_value_init (100 * 1024 *
2189                                                               1024 / 8),
2190                                  100);
2191   plugin->fragment_data_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
2192   plugin->wlan_header_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
2193   plugin->helper_payload_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
2194   plugin->beacon_task = GNUNET_SCHEDULER_add_now (&send_hello_beacon,
2195                                                   plugin);
2196
2197   plugin->options = 0;
2198
2199   /* some compilers do not like switch on 'long long'... */
2200   switch ((unsigned int) testmode)
2201   {
2202   case 0: /* normal */
2203     plugin->helper_argv[0] = (char *) HELPER_NAME;
2204     plugin->helper_argv[1] = wlan_interface;
2205     plugin->helper_argv[2] = NULL;
2206     plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
2207                                                HELPER_NAME,
2208                                                plugin->helper_argv,
2209                                                &handle_helper_message,
2210                                                NULL,
2211                                                plugin);
2212     break;
2213   case 1: /* testmode, peer 1 */
2214     plugin->helper_argv[0] = (char *) DUMMY_HELPER_NAME;
2215     plugin->helper_argv[1] = (char *) "1";
2216     plugin->helper_argv[2] = NULL;
2217     plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
2218                                                DUMMY_HELPER_NAME,
2219                                                plugin->helper_argv,
2220                                                &handle_helper_message,
2221                                                NULL,
2222                                                plugin);
2223     break;
2224   case 2: /* testmode, peer 2 */
2225     plugin->helper_argv[0] = (char *) DUMMY_HELPER_NAME;
2226     plugin->helper_argv[1] = (char *) "2";
2227     plugin->helper_argv[2] = NULL;
2228     plugin->suid_helper = GNUNET_HELPER_start (GNUNET_NO,
2229                                                DUMMY_HELPER_NAME,
2230                                                plugin->helper_argv,
2231                                                &handle_helper_message,
2232                                                NULL,
2233                                                plugin);
2234     break;
2235   default:
2236     GNUNET_assert (0);
2237   }
2238
2239   api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2240   api->cls = plugin;
2241   api->send = &wlan_plugin_send;
2242   api->get_session = &wlan_plugin_get_session;
2243   api->disconnect_peer = &wlan_plugin_disconnect_peer;
2244   api->disconnect_session = &wlan_plugin_disconnect_session;
2245   api->query_keepalive_factor = &wlan_plugin_query_keepalive_factor;
2246   api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2247   api->check_address = &wlan_plugin_address_suggested;
2248   api->address_to_string = &wlan_plugin_address_to_string;
2249   api->string_to_address = &wlan_plugin_string_to_address;
2250   api->get_network = &wlan_plugin_get_network;
2251   api->update_session_timeout = &wlan_plugin_update_session_timeout;
2252   api->update_inbound_delay = &wlan_plugin_update_inbound_delay;
2253   api->setup_monitor = &wlan_plugin_setup_monitor;
2254   return api;
2255 }
2256
2257
2258 /* end of plugin_transport_wlan.c */