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