2 This file is part of GNUnet
3 (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file transport/plugin_transport_wlan.c
23 * @brief transport plugin for wlan
24 * @author David Brodski
27 //TODO split rx and tx structures for better handling
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_util_lib.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_transport_service.h"
35 #include "gnunet_transport_plugin.h"
36 #include "plugin_transport_wlan.h"
37 #include "gnunet_common.h"
38 #include "gnunet_crypto_lib.h"
39 #include "gnunet_fragmentation_lib.h"
40 //#include "wlan/ieee80211.h"
41 //#include <netinet/ip.h>
45 #define PROTOCOL_PREFIX "wlan"
47 #define PLUGIN_LOG_NAME "wlan-plugin"
55 * time out of a session
57 #define SESSION_TIMEOUT GNUNET_TIME_UNIT_MINUTES
60 * time out of a mac endpoint
62 #define MACENDPOINT_TIMEOUT GNUNET_TIME_UNIT_MINUTES
65 * scaling factor for hello beacon
67 #define HALLO_BEACON_SCALING_FACTOR 1
70 * max size of fragment queue
72 #define FRAGMENT_QUEUE_SIZE 10
74 * max messages in fragment queue per session/client
76 #define FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION 1
79 * max messages in fragment queue per MAC
81 #define FRAGMENT_QUEUE_MESSAGES_OUT_PER_MACENDPOINT 1
84 * max messages in in queue
86 #define MESSAGES_IN_QUEUE_SIZE 10
88 * max messages in in queue per session/client
90 #define MESSAGES_IN_DEFRAG_QUEUE_PER_MAC 1
93 * LLC fields for better compatibility
95 #define WLAN_LLC_DSAP_FIELD 0xf
96 #define WLAN_LLC_SSAP_FIELD 0xf
102 #define DEBUG_wlan GNUNET_YES
103 #define DEBUG_wlan_retransmission GNUNET_NO
104 #define DEBUG_wlan_ip_udp_packets_on_air GNUNET_NO
105 #define DEBUG_wlan_msg_dump GNUNET_NO
108 #define IEEE80211_ADDR_LEN 6 /* size of 802.11 address */
110 #define IEEE80211_FC0_VERSION_MASK 0x03
111 #define IEEE80211_FC0_VERSION_SHIFT 0
112 #define IEEE80211_FC0_VERSION_0 0x00
113 #define IEEE80211_FC0_TYPE_MASK 0x0c
114 #define IEEE80211_FC0_TYPE_SHIFT 2
115 #define IEEE80211_FC0_TYPE_MGT 0x00
116 #define IEEE80211_FC0_TYPE_CTL 0x04
117 #define IEEE80211_FC0_TYPE_DATA 0x08
120 * Structure of an internet header, naked of options.
124 #if __BYTE_ORDER == __LITTLE_ENDIAN
125 unsigned int ip_hl:4; /* header length */
126 unsigned int ip_v:4; /* version */
128 #if __BYTE_ORDER == __BIG_ENDIAN
129 unsigned int ip_v:4; /* version */
130 unsigned int ip_hl:4; /* header length */
132 u_int8_t ip_tos; /* type of service */
133 u_short ip_len; /* total length */
134 u_short ip_id; /* identification */
135 u_short ip_off; /* fragment offset field */
136 #define IP_RF 0x8000 /* reserved fragment flag */
137 #define IP_DF 0x4000 /* dont fragment flag */
138 #define IP_MF 0x2000 /* more fragments flag */
139 #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
140 u_int8_t ip_ttl; /* time to live */
141 u_int8_t ip_p; /* protocol */
142 u_short ip_sum; /* checksum */
143 struct in_addr ip_src, ip_dst; /* source and dest address */
155 * generic definitions for IEEE 802.11 frames
157 struct ieee80211_frame
161 u_int8_t i_addr1[IEEE80211_ADDR_LEN];
162 u_int8_t i_addr2[IEEE80211_ADDR_LEN];
163 u_int8_t i_addr3[IEEE80211_ADDR_LEN];
166 #if DEBUG_wlan_ip_udp_packets_on_air
173 * Encapsulation of all of the state of the plugin.
180 struct GNUNET_TRANSPORT_PluginEnvironment *env;
183 * List of open connections. head
185 struct MacEndpoint *mac_head;
188 * List of open connections. tail
190 struct MacEndpoint *mac_tail;
193 * Number of connections
195 unsigned int mac_count;
198 * encapsulation of data from the local wlan helper program
200 struct GNUNET_SERVER_MessageStreamTokenizer *suid_tokenizer;
203 * encapsulation of packets received from the wlan helper
205 struct GNUNET_SERVER_MessageStreamTokenizer *data_tokenizer;
208 * stdout pipe handle for the gnunet-wlan-helper process
210 struct GNUNET_DISK_PipeHandle *server_stdout;
213 * stdout file handle for the gnunet-wlan-helper process
215 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
218 * stdin pipe handle for the gnunet-wlan-helper process
220 struct GNUNET_DISK_PipeHandle *server_stdin;
223 * stdin file handle for the gnunet-wlan-helper process
225 const struct GNUNET_DISK_FileHandle *server_stdin_handle;
228 * ID of the gnunet-wlan-server std read task
230 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
233 * ID of the gnunet-wlan-server std read task
235 GNUNET_SCHEDULER_TaskIdentifier server_write_task;
238 * ID of the delay task for writing
240 GNUNET_SCHEDULER_TaskIdentifier server_write_delay_task;
243 * The process id of the wlan process
245 struct GNUNET_OS_Process *server_proc;
248 * The interface of the wlan card given to us by the user.
253 * The mac_address of the wlan card given to us by the helper.
255 struct MacAddress mac_address;
258 * Sessions currently pending for transmission
261 struct Sessionqueue *pending_Sessions_head;
264 * Sessions currently pending for transmission
265 * to a peer (tail), if any.
267 struct Sessionqueue *pending_Sessions_tail;
270 * number of pending sessions
272 unsigned int pendingsessions;
275 * Messages in the sending queues
277 int pending_Fragment_Messages;
280 * messages ready for send, head
282 struct FragmentMessage_queue *sending_messages_head;
284 * messages ready for send, tail
286 struct FragmentMessage_queue *sending_messages_tail;
288 * time of the next "hello-beacon"
290 struct GNUNET_TIME_Absolute beacon_time;
293 * queue to send acks for received fragments (head)
295 struct AckSendQueue *ack_send_queue_head;
298 * queue to send acks for received fragments (tail)
300 struct AckSendQueue *ack_send_queue_tail;
303 * Tracker for bandwidth limit
305 struct GNUNET_BANDWIDTH_Tracker tracker;
309 * Struct to store data if file write did not accept the whole packet
313 struct Plugin *plugin;
315 struct GNUNET_MessageHeader *msgstart;
320 * Queue of sessions, for the general session queue and the pending session queue
325 struct Sessionqueue *next;
326 struct Sessionqueue *prev;
327 struct Session *content;
331 * Queue of fragmented messages, for the sending queue of the plugin
334 struct FragmentMessage_queue
336 struct FragmentMessage_queue *next;
337 struct FragmentMessage_queue *prev;
338 struct FragmentMessage *content;
342 * Queue for the fragments received
345 struct Receive_Fragment_Queue
347 struct Receive_Fragment_Queue *next;
348 struct Receive_Fragment_Queue *prev;
352 struct Radiotap_rx rxinfo;
356 struct MacEndpoint_id_fragment_triple
358 struct MacEndpoint *endpoint;
360 struct FragmentMessage *fm;
364 struct Plugin_Session_pair
366 struct Plugin *plugin;
367 struct Session *session;
371 * Information kept for each message that is yet to
374 struct PendingMessage
379 struct PendingMessage *next;
383 struct PendingMessage *prev;
386 * The pending message
388 struct WlanHeader *msg;
391 * Size of the message
396 * Continuation function to call once the message
397 * has been sent. Can be NULL if there is no
398 * continuation to call.
400 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
403 * Cls for transmit_cont
405 void *transmit_cont_cls;
408 * Timeout value for the pending message.
410 struct GNUNET_TIME_Absolute timeout;
415 * Queue for acks to send for fragments recived
421 * next ack in the ack send queue
423 struct AckSendQueue *next;
425 * previous ack in the ack send queue
427 struct AckSendQueue *prev;
429 * pointer to the session this ack belongs to
431 struct MacEndpoint *endpoint;
433 * ID of message, to distinguish between the messages, picked randomly.
440 struct GNUNET_MessageHeader *hdr;
442 * pointer to the ieee wlan header
444 struct ieee80211_frame *ieeewlanheader;
446 * pointer to the radiotap header
448 struct Radiotap_Send *radioHeader;
452 * Session infos gathered from a messages
458 * the session this message belongs to
460 struct Session *session;
464 struct MacAddress addr;
469 struct MacEndpoint *macendpoint;
473 * Session handle for connections.
481 struct SessionHeader header;
484 * Message currently pending for transmission
485 * to this peer, if any. head
487 struct PendingMessage *pending_message_head;
490 * Message currently pending for transmission
491 * to this peer, if any. tail
493 struct PendingMessage *pending_message_tail;
496 * To whom are we talking to (set to our identity
497 * if we are still waiting for the welcome message)
499 struct GNUNET_PeerIdentity target;
502 * Address of the other peer (either based on our 'connect'
503 * call or on our 'accept' call).
508 * Last activity on this connection. Used to select preferred
509 * connection and timeout
511 struct GNUNET_TIME_Absolute last_activity;
516 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
521 struct MacEndpoint *mac;
524 * count of messages in the fragment out queue for this session
527 int fragment_messages_out_count;
532 * Struct to represent one network card connection
537 * Pointer to the global plugin struct.
539 struct Plugin *plugin;
541 * Struct to hold the session reachable over this mac; head
543 struct Sessionqueue *sessions_head;
545 * Struct to hold the session reachable over this mac; tail
547 struct Sessionqueue *sessions_tail;
549 * Messages currently sending
552 struct FragmentMessage *sending_messages_head;
555 * Messages currently sending
556 * to a peer (tail), if any.
558 struct FragmentMessage *sending_messages_tail;
562 struct MacEndpoint *next;
566 struct MacEndpoint *prev;
571 struct MacAddress addr;
574 * Defrag context for this mac endpoint
576 struct GNUNET_DEFRAGMENT_Context *defrag;
579 * count of messages in the fragment out queue for this mac endpoint
582 int fragment_messages_out_count;
590 * Duplicates received
605 * Last activity on this endpoint. Used to select preferred
608 struct GNUNET_TIME_Absolute last_activity;
613 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
617 * Struct for Messages in the fragment queue
620 struct FragmentMessage
624 * Session this message belongs to
627 struct Session *session;
630 * This is a doubly-linked list.
632 struct FragmentMessage *next;
635 * This is a doubly-linked list.
637 struct FragmentMessage *prev;
640 * Fragmentation context
642 struct GNUNET_FRAGMENT_Context *fragcontext;
645 * Timeout value for the message.
647 struct GNUNET_TIME_Absolute timeout;
652 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
665 * pointer to the ieee wlan header
667 struct ieee80211_frame *ieeewlanheader;
669 * pointer to the radiotap header
671 struct Radiotap_Send *radioHeader;
675 do_transmit (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
677 free_session (struct Plugin *plugin, struct Sessionqueue *queue,
678 int do_free_macendpoint);
679 static struct MacEndpoint *
680 create_macendpoint (struct Plugin *plugin, const struct MacAddress *addr);
683 * Generates a nice hexdump of a memory area.
685 * \param mem pointer to memory to dump
686 * \param length how many bytes to dump
689 hexdump (const void *mem, unsigned length)
692 char *src = (char *) mem;
694 printf ("dumping %u bytes from %p\r\n"
695 " 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF\r\n",
700 for (i = 0; i < length; i += 16, src += 16)
704 t += sprintf (t, "%04x: ", i);
705 for (j = 0; j < 16; j++)
708 t += sprintf (t, "%02X", src[j] & 0xff);
710 t += sprintf (t, " ");
711 t += sprintf (t, j % 2 ? " " : "-");
714 t += sprintf (t, " ");
715 for (j = 0; j < 16; j++)
719 if (isprint ((unsigned char) src[j]))
720 t += sprintf (t, "%c", src[j]);
722 t += sprintf (t, ".");
726 t += sprintf (t, " ");
730 t += sprintf (t, "\r\n");
736 * Function to find a MacEndpoint with a specific mac addr
737 * @param plugin pointer to the plugin struct
738 * @param addr pointer to the mac address
739 * @param create_new GNUNET_YES if a new end point should be created
742 static struct MacEndpoint *
743 get_macendpoint (struct Plugin *plugin, const struct MacAddress *addr,
746 struct MacEndpoint *queue = plugin->mac_head;
748 while (queue != NULL)
750 GNUNET_assert (queue->sessions_head != NULL);
751 if (memcmp (addr, &queue->addr, sizeof (struct MacAddress)) == 0)
752 return queue; /* session found */
756 if (create_new == GNUNET_YES)
758 return create_macendpoint (plugin, addr);
768 * search for a session with the macendpoint and peer id
770 * @param plugin pointer to the plugin struct
771 * @param endpoint pointer to the mac endpoint of the peer
772 * @param peer pointer to the peerid
773 * @return returns the session
775 static struct Session *
776 search_session (struct Plugin *plugin, const struct MacEndpoint *endpoint,
777 const struct GNUNET_PeerIdentity *peer)
779 GNUNET_assert (endpoint != NULL);
780 struct Sessionqueue *queue = endpoint->sessions_head;
782 while (queue != NULL)
784 GNUNET_assert (queue->content != NULL);
786 (peer, &queue->content->target,
787 sizeof (struct GNUNET_PeerIdentity)) == 0)
788 return queue->content; /* session found */
795 * Function called for a quick conversion of the binary address to
796 * a numeric address. Note that the caller must not free the
797 * address and that the next call to this function is allowed
798 * to override the address again.
801 * @param addr binary address
802 * @param addrlen length of the address
803 * @return string representing the same address
806 wlan_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
809 const struct MacAddress *mac;
811 if (addrlen != sizeof (struct MacAddress))
817 GNUNET_snprintf (ret, sizeof (ret), "%s Mac-Address %X:%X:%X:%X:%X:%X",
818 PROTOCOL_PREFIX, mac->mac[0], mac->mac[1], mac->mac[2],
819 mac->mac[3], mac->mac[4], mac->mac[5]);
825 * Function for the scheduler if a session times out
826 * @param cls pointer to the Sessionqueue
827 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
830 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
832 struct Sessionqueue *queue = cls;
834 GNUNET_assert (queue != NULL);
835 queue->content->timeout_task = GNUNET_SCHEDULER_NO_TASK;
836 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
840 if (GNUNET_TIME_absolute_get_remaining
841 (GNUNET_TIME_absolute_add
842 (queue->content->last_activity, SESSION_TIMEOUT)).rel_value == 0)
844 GNUNET_STATISTICS_update (queue->content->mac->plugin->env->stats, _("# wlan session timeouts"), 1, GNUNET_NO);
845 free_session (queue->content->mac->plugin, queue, GNUNET_YES);
849 queue->content->timeout_task =
850 GNUNET_SCHEDULER_add_delayed (SESSION_TIMEOUT, &session_timeout, queue);
855 * create a new session
857 * @param plugin pointer to the plugin struct
858 * @param endpoint pointer to the mac endpoint of the peer
859 * @param peer peer identity to use for this session
860 * @return returns the session
863 static struct Session *
864 create_session (struct Plugin *plugin, struct MacEndpoint *endpoint,
865 const struct GNUNET_PeerIdentity *peer)
867 GNUNET_assert (endpoint != NULL);
868 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan session created"), 1, GNUNET_NO);
869 struct Sessionqueue *queue =
870 GNUNET_malloc (sizeof (struct Sessionqueue) + sizeof (struct Session));
872 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
873 endpoint->sessions_tail, queue);
875 queue->content = (struct Session *) &queue[1];
876 queue->content->mac = endpoint;
877 memcpy (&(queue->content->target), peer, sizeof (struct GNUNET_PeerIdentity));
878 queue->content->last_activity = GNUNET_TIME_absolute_get ();
879 queue->content->timeout_task =
880 GNUNET_SCHEDULER_add_delayed (SESSION_TIMEOUT, &session_timeout, queue);
883 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
884 "New session %p with endpoint %p: %s\n", queue->content,
885 endpoint, wlan_plugin_address_to_string (NULL,
890 return queue->content;
894 * Get session from address, create if no session exists
896 * @param plugin pointer to the plugin struct
897 * @param addr pointer to the mac address of the peer
898 * @param peer pointer to the peerid
899 * @return returns the session
901 static struct Session *
902 get_session (struct Plugin *plugin, const struct MacAddress *addr,
903 const struct GNUNET_PeerIdentity *peer)
905 struct MacEndpoint *mac;
907 mac = get_macendpoint (plugin, addr, GNUNET_YES);
908 struct Session *session = search_session (plugin, mac, peer);
912 return create_session (plugin, mac, peer);
916 * Queue the session to send data
917 * checks if there is a message pending
918 * checks if this session is not allready in the queue
919 * @param plugin pointer to the plugin
920 * @param session pointer to the session to add
923 queue_session (struct Plugin *plugin, struct Session *session)
925 struct Sessionqueue *queue = plugin->pending_Sessions_head;
927 if (session->pending_message_head != NULL)
929 while (queue != NULL)
931 // content is never NULL
932 GNUNET_assert (queue->content != NULL);
933 // is session already in queue?
934 if (session == queue->content)
942 // Session is not in the queue
944 queue = GNUNET_malloc (sizeof (struct Sessionqueue));
945 queue->content = session;
948 GNUNET_CONTAINER_DLL_insert_tail (plugin->pending_Sessions_head,
949 plugin->pending_Sessions_tail, queue);
950 plugin->pendingsessions++;
956 * Function to schedule the write task, executed after a delay
957 * @param cls pointer to the plugin struct
958 * @param tc GNUNET_SCHEDULER_TaskContext pointer
961 delay_fragment_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
963 struct Plugin *plugin = cls;
965 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
967 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
970 // GNUNET_TIME_UNIT_FOREVER_REL is needed to clean up old msg
971 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
973 plugin->server_write_task =
974 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
975 plugin->server_stdin_handle,
976 &do_transmit, plugin);
981 * Function to calculate the time of the next periodic "hello-beacon"
982 * @param plugin pointer to the plugin struct
986 set_next_beacon_time (struct Plugin *const plugin)
988 //under 10 known peers: once a second
989 if (plugin->mac_count < 10)
991 plugin->beacon_time =
992 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
993 GNUNET_TIME_relative_multiply
994 (GNUNET_TIME_UNIT_SECONDS,
995 HALLO_BEACON_SCALING_FACTOR));
997 //under 30 known peers: every 10 seconds
998 else if (plugin->mac_count < 30)
1000 plugin->beacon_time =
1001 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1002 GNUNET_TIME_relative_multiply
1003 (GNUNET_TIME_UNIT_SECONDS,
1004 10 * HALLO_BEACON_SCALING_FACTOR));
1006 //over 30 known peers: once a minute
1009 plugin->beacon_time =
1010 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1011 GNUNET_TIME_relative_multiply
1012 (GNUNET_TIME_UNIT_MINUTES,
1013 HALLO_BEACON_SCALING_FACTOR));
1018 * Function to set the timer for the next timeout of the fragment queue
1019 * @param plugin the handle to the plugin struct
1023 set_next_send (struct Plugin *const plugin)
1025 struct GNUNET_TIME_Relative next_send;
1028 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
1030 GNUNET_SCHEDULER_cancel (plugin->server_write_delay_task);
1031 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
1034 //check if some acks are in the queue
1035 if (plugin->ack_send_queue_head != NULL)
1037 next_send = GNUNET_TIME_UNIT_ZERO;
1040 //check if there are some fragments in the queue
1041 else if (plugin->sending_messages_head != NULL)
1043 next_send = GNUNET_TIME_UNIT_ZERO;
1047 next_send = GNUNET_TIME_absolute_get_remaining (plugin->beacon_time);
1051 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1052 "Next packet is send in: %u\n", next_send.rel_value);
1055 if (next_send.rel_value == GNUNET_TIME_UNIT_ZERO.rel_value)
1057 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
1059 plugin->server_write_task =
1060 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1061 plugin->server_stdin_handle,
1062 &do_transmit, plugin);
1067 if (plugin->server_write_delay_task == GNUNET_SCHEDULER_NO_TASK)
1069 plugin->server_write_delay_task =
1070 GNUNET_SCHEDULER_add_delayed (next_send, &delay_fragment_task,
1077 * Function to get the next queued Session, removes the session from the queue
1078 * @param plugin pointer to the plugin struct
1079 * @return pointer to the session found, returns NULL if there is now session in the queue
1081 static struct Session *
1082 get_next_queue_session (struct Plugin *plugin)
1084 struct Session *session;
1085 struct Sessionqueue *sessionqueue;
1086 struct Sessionqueue *sessionqueue_alt;
1087 struct PendingMessage *pm;
1089 sessionqueue = plugin->pending_Sessions_head;
1091 while (sessionqueue != NULL)
1093 session = sessionqueue->content;
1095 GNUNET_assert (session != NULL);
1096 pm = session->pending_message_head;
1101 GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1102 "pending message is empty, should not happen. session %p\n",
1105 sessionqueue_alt = sessionqueue;
1106 sessionqueue = sessionqueue->next;
1107 plugin->pendingsessions--;
1108 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1109 plugin->pending_Sessions_tail,
1112 GNUNET_free (sessionqueue_alt);
1117 //check for message timeout
1118 if (GNUNET_TIME_absolute_get_remaining (pm->timeout).rel_value > 0)
1120 //check if session has no message in the fragment queue
1121 if ((session->mac->fragment_messages_out_count <
1122 FRAGMENT_QUEUE_MESSAGES_OUT_PER_MACENDPOINT) &&
1123 (session->fragment_messages_out_count <
1124 FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION))
1126 plugin->pendingsessions--;
1127 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1128 plugin->pending_Sessions_tail,
1130 GNUNET_free (sessionqueue);
1136 sessionqueue = sessionqueue->next;
1141 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
1142 session->pending_message_tail, pm);
1144 //call the cont func that it did not work
1145 if (pm->transmit_cont != NULL)
1146 pm->transmit_cont (pm->transmit_cont_cls, &(session->target),
1148 GNUNET_free (pm->msg);
1151 if (session->pending_message_head == NULL)
1153 sessionqueue_alt = sessionqueue;
1154 sessionqueue = sessionqueue->next;
1155 plugin->pendingsessions--;
1156 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1157 plugin->pending_Sessions_tail,
1160 GNUNET_free (sessionqueue_alt);
1169 * frees the space of a message in the fragment queue (send queue)
1170 * @param plugin the plugin struct
1171 * @param fm message to free
1174 free_fragment_message (struct Plugin *plugin, struct FragmentMessage *fm)
1176 struct Session *session = fm->session;
1177 struct MacEndpoint *endpoint = session->mac;
1178 struct FragmentMessage_queue *fmq;
1179 struct FragmentMessage_queue *fmq_next;
1183 fmq = plugin->sending_messages_head;
1186 fmq_next = fmq->next;
1187 if (fmq->content == fm)
1189 GNUNET_CONTAINER_DLL_remove (plugin->sending_messages_head,
1190 plugin->sending_messages_tail, fmq);
1196 (session->mac->fragment_messages_out_count)--;
1197 session->fragment_messages_out_count--;
1198 plugin->pending_Fragment_Messages--;
1199 GNUNET_CONTAINER_DLL_remove (endpoint->sending_messages_head,
1200 endpoint->sending_messages_tail, fm);
1201 GNUNET_FRAGMENT_context_destroy (fm->fragcontext);
1202 if (fm->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1203 GNUNET_SCHEDULER_cancel (fm->timeout_task);
1206 queue_session (plugin, session);
1208 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1209 "Free pending fragment messages %p, session %p\n", fm,
1216 * function to fill the radiotap header
1217 * @param plugin pointer to the plugin struct
1218 * @param endpoint pointer to the endpoint
1219 * @param header pointer to the radiotap header
1220 * @return GNUNET_YES at success
1223 getRadiotapHeader (struct Plugin *plugin, struct MacEndpoint *endpoint,
1224 struct Radiotap_Send *header)
1227 if (endpoint != NULL)
1229 header->rate = endpoint->rate;
1230 header->tx_power = endpoint->tx_power;
1231 header->antenna = endpoint->antenna;
1236 header->tx_power = 0;
1237 header->antenna = 0;
1244 * function to generate the wlan hardware header for one packet
1245 * @param Header address to write the header to
1246 * @param to_mac_addr address of the recipient
1247 * @param plugin pointer to the plugin struct
1248 * @param size size of the whole packet, needed to calculate the time to send the packet
1249 * @return GNUNET_YES if there was no error
1252 getWlanHeader (struct ieee80211_frame *Header,
1253 const struct MacAddress *to_mac_addr, struct Plugin *plugin,
1257 const int rate = 11000000;
1259 Header->i_fc[0] = IEEE80211_FC0_TYPE_DATA;
1260 Header->i_fc[1] = 0x00;
1261 memcpy (&Header->i_addr3, &mac_bssid, sizeof (mac_bssid));
1262 memcpy (&Header->i_addr2, plugin->mac_address.mac,
1263 sizeof (plugin->mac_address));
1264 memcpy (&Header->i_addr1, to_mac_addr, sizeof (struct MacAddress));
1266 tmp16 = (uint16_t *) Header->i_dur;
1267 *tmp16 = (uint16_t) htole16 ((size * 1000000) / rate + 290);
1268 Header->llc[0] = WLAN_LLC_DSAP_FIELD;
1269 Header->llc[1] = WLAN_LLC_SSAP_FIELD;
1271 #if DEBUG_wlan_ip_udp_packets_on_air
1276 Header->ip.ip_dst.s_addr = *((uint32_t *) & to_mac_addr->mac[2]);
1277 Header->ip.ip_src.s_addr = *((uint32_t *) & plugin->mac_address.mac[2]);
1278 Header->ip.ip_v = 4;
1279 Header->ip.ip_hl = 5;
1280 Header->ip.ip_p = 17;
1281 Header->ip.ip_ttl = 1;
1282 Header->ip.ip_len = htons (size + 8);
1283 Header->ip.ip_sum = 0;
1284 x = (uint16_t *) & Header->ip;
1285 count = sizeof (struct iph);
1288 /* This is the inner loop */
1289 crc += (unsigned short) *x++;
1292 /* Add left-over byte, if any */
1294 crc += *(unsigned char *) x;
1295 crc = (crc & 0xffff) + (crc >> 16);
1296 Header->ip.ip_sum = htons (~(unsigned short) crc);
1297 Header->udp.len = htons (size - sizeof (struct ieee80211_frame));
1307 * @param msgbuf pointer tor the data
1308 * @param msgbuf_size size of the data
1310 * @return 32bit crc value
1314 getcrc32 (const char *msgbuf, size_t msgbuf_size)
1317 return GNUNET_CRYPTO_crc32_n (msgbuf, msgbuf_size);;
1323 * @param msgbuf pointer tor the data
1324 * @param msgbuf_size size of the data
1326 * @return 16bit crc value
1330 getcrc16 (const char *msgbuf, size_t msgbuf_size)
1332 //TODO calc some crc
1337 * function to add a fragment of a message to send
1338 * @param cls FragmentMessage this message belongs to
1339 * @param hdr pointer to the start of the message
1343 add_message_for_send (void *cls, const struct GNUNET_MessageHeader *hdr)
1346 struct FragmentMessage *fm = cls;
1347 struct FragmentMessage_queue *fmqueue;
1349 GNUNET_assert (cls != NULL);
1350 GNUNET_assert (fm->frag == NULL);
1351 struct MacEndpoint *endpoint = fm->session->mac;
1352 struct Plugin *plugin = endpoint->plugin;
1353 struct GNUNET_MessageHeader *msgheader;
1354 struct GNUNET_MessageHeader *msgheader2;
1357 #if DEBUG_wlan_retransmission
1358 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1359 "Adding fragment of message %p to send, session %p, endpoint %p, type %u\n",
1360 fm, fm->session, endpoint, hdr->type);
1364 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1365 sizeof (struct ieee80211_frame) + ntohs (hdr->size);
1366 fm->frag = GNUNET_malloc (size);
1369 msgheader = (struct GNUNET_MessageHeader *) fm->frag;
1370 msgheader->size = htons (size);
1371 msgheader->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1373 fm->radioHeader = (struct Radiotap_Send *) &msgheader[1];
1374 fm->ieeewlanheader = (struct ieee80211_frame *) &fm->radioHeader[1];
1375 msgheader2 = (struct GNUNET_MessageHeader *) &fm->ieeewlanheader[1];
1376 memcpy (msgheader2, hdr, ntohs (hdr->size));
1378 fmqueue = GNUNET_malloc (sizeof (struct FragmentMessage_queue));
1379 fmqueue->content = fm;
1381 GNUNET_CONTAINER_DLL_insert_tail (plugin->sending_messages_head,
1382 plugin->sending_messages_tail, fmqueue);
1383 set_next_send (plugin);
1387 * function to send a hallo beacon
1388 * @param plugin pointer to the plugin struct
1391 send_hello_beacon (struct Plugin *plugin)
1395 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1396 "Sending hello beacon\n");
1401 uint16_t hallo_size;
1402 struct GNUNET_MessageHeader *msgheader;
1403 struct ieee80211_frame *ieeewlanheader;
1404 struct Radiotap_Send *radioHeader;
1405 struct GNUNET_MessageHeader *msgheader2;
1406 const struct GNUNET_MessageHeader *hello;
1408 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan hello beacons send"), 1, GNUNET_NO);
1410 hello = plugin->env->get_our_hello ();
1411 hallo_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1412 GNUNET_assert (sizeof (struct WlanHeader) + hallo_size <= WLAN_MTU);
1414 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1415 sizeof (struct ieee80211_frame) + hallo_size;
1417 msgheader = GNUNET_malloc (size);
1418 msgheader->size = htons (size);
1419 msgheader->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1421 radioHeader = (struct Radiotap_Send *) &msgheader[1];
1422 getRadiotapHeader (plugin, NULL, radioHeader);
1423 ieeewlanheader = (struct ieee80211_frame *) &radioHeader[1];
1424 getWlanHeader (ieeewlanheader, &bc_all_mac, plugin, size);
1426 msgheader2 = (struct GNUNET_MessageHeader *) &ieeewlanheader[1];
1427 /*msgheader2->size =
1428 htons (GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello) +
1429 sizeof (struct GNUNET_MessageHeader));
1431 msgheader2->type = htons (GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT);*/
1432 memcpy (msgheader2, hello, hallo_size);
1434 bytes = GNUNET_DISK_file_write (plugin->server_stdin_handle, msgheader, size);
1436 if (bytes == GNUNET_SYSERR)
1438 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1440 ("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1441 errno, strerror (errno));
1444 GNUNET_assert (bytes != GNUNET_SYSERR);
1445 GNUNET_assert (bytes == size);
1446 GNUNET_free (msgheader);
1448 set_next_beacon_time (plugin);
1449 set_next_send (plugin);
1453 * function to add an ack to send it for a received fragment
1454 * @param cls MacEndpoint this ack belongs to
1455 * @param msg_id id of the message
1456 * @param hdr pointer to the hdr where the ack is stored
1461 add_ack_for_send (void *cls, uint32_t msg_id,
1462 const struct GNUNET_MessageHeader *hdr)
1465 struct AckSendQueue *ack;
1467 GNUNET_assert (cls != NULL);
1468 struct MacEndpoint *endpoint = cls;
1469 struct Plugin *plugin = endpoint->plugin;
1470 struct GNUNET_MessageHeader *msgheader;
1471 struct GNUNET_MessageHeader *msgheader2;
1475 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1476 sizeof (struct ieee80211_frame) + ntohs (hdr->size) +
1477 sizeof (struct AckSendQueue);
1479 ack = GNUNET_malloc (size);
1480 ack->message_id = msg_id;
1481 ack->endpoint = endpoint;
1484 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1485 sizeof (struct ieee80211_frame) + ntohs (hdr->size);
1487 msgheader = (struct GNUNET_MessageHeader *) &ack[1];
1488 ack->hdr = (struct GNUNET_MessageHeader *) &ack[1];
1489 msgheader->size = htons (size);
1490 msgheader->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1492 ack->radioHeader = (struct Radiotap_Send *) &msgheader[1];
1493 ack->ieeewlanheader = (struct ieee80211_frame *) &(ack->radioHeader)[1];
1494 msgheader2 = (struct GNUNET_MessageHeader *) &(ack->ieeewlanheader)[1];
1495 memcpy (msgheader2, hdr, ntohs (hdr->size));
1497 GNUNET_CONTAINER_DLL_insert_tail (plugin->ack_send_queue_head,
1498 plugin->ack_send_queue_tail, ack);
1500 #if DEBUG_wlan_retransmission
1501 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1502 "Adding ack with message id %u to send, AckSendQueue %p, endpoint %p\n",
1503 msg_id, ack, endpoint);
1506 set_next_send (plugin);
1510 * Function for the scheduler if a FragmentMessage times out
1511 * @param cls pointer to the FragmentMessage
1512 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
1515 fragmentmessage_timeout (void *cls,
1516 const struct GNUNET_SCHEDULER_TaskContext *tc)
1518 struct FragmentMessage *fm = cls;
1520 GNUNET_assert (fm != NULL);
1521 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1522 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1526 free_fragment_message (fm->session->mac->plugin, fm);
1530 * Function to check if there is some space in the fragment queue
1531 * inserts a message if space is available
1532 * @param plugin the plugin struct
1536 check_fragment_queue (struct Plugin *plugin)
1538 struct Session *session;
1539 struct FragmentMessage *fm;
1540 struct GNUNET_PeerIdentity pid;
1542 struct PendingMessage *pm;
1544 if (plugin->pending_Fragment_Messages < FRAGMENT_QUEUE_SIZE)
1546 session = get_next_queue_session (plugin);
1547 if (session != NULL)
1549 pm = session->pending_message_head;
1550 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
1551 session->pending_message_tail, pm);
1552 session->mac->fragment_messages_out_count++;
1553 session->fragment_messages_out_count++;
1554 plugin->pending_Fragment_Messages++;
1555 GNUNET_assert (pm != NULL);
1557 fm = GNUNET_malloc (sizeof (struct FragmentMessage));
1558 fm->session = session;
1559 fm->timeout.abs_value = pm->timeout.abs_value;
1562 GNUNET_FRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
1564 GNUNET_TIME_UNIT_SECONDS,
1566 &add_message_for_send, fm);
1568 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
1569 (fm->timeout), fragmentmessage_timeout,
1571 GNUNET_CONTAINER_DLL_insert_tail (session->mac->sending_messages_head,
1572 session->mac->sending_messages_tail,
1575 if (pm->transmit_cont != NULL)
1577 pid = session->target;
1578 pm->transmit_cont (pm->transmit_cont_cls, &pid, GNUNET_OK);
1580 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1581 "called pm->transmit_cont for %p\n", session);
1587 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1588 "no pm->transmit_cont for %p\n", session);
1593 if (session->pending_message_head != NULL)
1596 queue_session (plugin, session);
1602 //check if timeout changed
1603 set_next_send (plugin);
1607 * Function to send an ack, does not free the ack
1608 * @param plugin pointer to the plugin
1609 * @param ack pointer to the ack to send
1612 send_ack (struct Plugin *plugin, struct AckSendQueue *ack)
1618 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1619 "Sending ack for message_id %u for mac endpoint %p, size %u\n",
1620 ack->message_id, ack->endpoint,
1621 ntohs (ack->hdr->size) - sizeof (struct Radiotap_Send));
1624 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan acks send"), 1, GNUNET_NO);
1626 getRadiotapHeader (plugin, ack->endpoint, ack->radioHeader);
1627 getWlanHeader (ack->ieeewlanheader, &ack->endpoint->addr, plugin,
1628 ntohs (ack->hdr->size));
1631 GNUNET_DISK_file_write (plugin->server_stdin_handle, ack->hdr,
1632 ntohs (ack->hdr->size));
1633 if (bytes == GNUNET_SYSERR)
1635 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1637 ("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1638 errno, strerror (errno));
1641 GNUNET_assert (bytes != GNUNET_SYSERR);
1642 GNUNET_assert (bytes == ntohs (ack->hdr->size));
1643 set_next_send (plugin);
1647 * function to finish a sending if not all could have been writen befor
1648 * @param cls pointer to the Finish_send struct
1649 * @param tc TaskContext
1652 finish_sending (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1654 struct Finish_send *finish = cls;
1655 struct Plugin *plugin;
1658 plugin = finish->plugin;
1659 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1661 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1663 GNUNET_free (finish->msgstart);
1664 GNUNET_free (finish);
1668 GNUNET_DISK_file_write (plugin->server_stdin_handle, finish->msgheader,
1670 GNUNET_assert (bytes != GNUNET_SYSERR);
1672 if (bytes != finish->size)
1674 finish->msgheader = finish->msgheader + bytes;
1675 finish->size = finish->size - bytes;
1676 plugin->server_write_task =
1677 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1678 plugin->server_stdin_handle,
1679 &finish_sending, finish);
1683 GNUNET_free (finish->msgstart);
1684 GNUNET_free (finish);
1685 set_next_send (plugin);
1690 * Function called when wlan helper is ready to get some data
1692 * @param cls closure
1693 * @param tc GNUNET_SCHEDULER_TaskContext
1696 do_transmit (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1698 struct Plugin *plugin = cls;
1700 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1701 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1704 struct Session *session;
1705 struct FragmentMessage *fm;
1706 struct Finish_send *finish;
1707 struct FragmentMessage_queue *fmq;
1708 struct AckSendQueue *ack;
1711 if (plugin->ack_send_queue_head != NULL)
1713 ack = plugin->ack_send_queue_head;
1714 GNUNET_CONTAINER_DLL_remove (plugin->ack_send_queue_head,
1715 plugin->ack_send_queue_tail, ack);
1716 send_ack (plugin, ack);
1721 //test if a "hello-beacon" has to be send
1722 if (GNUNET_TIME_absolute_get_remaining (plugin->beacon_time).rel_value == 0)
1724 send_hello_beacon (plugin);
1728 if (plugin->sending_messages_head != NULL)
1730 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan fragments send"), 1, GNUNET_NO);
1732 fmq = plugin->sending_messages_head;
1734 GNUNET_CONTAINER_DLL_remove (plugin->sending_messages_head,
1735 plugin->sending_messages_tail, fmq);
1738 session = fm->session;
1739 GNUNET_assert (session != NULL);
1742 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1743 "Sending GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT for fragment message %p, size: %u\n",
1747 getRadiotapHeader (plugin, session->mac, fm->radioHeader);
1748 getWlanHeader (fm->ieeewlanheader, &(fm->session->mac->addr), plugin,
1752 GNUNET_DISK_file_write (plugin->server_stdin_handle, fm->frag,
1754 if (bytes == GNUNET_SYSERR)
1756 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1758 ("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1759 errno, strerror (errno));
1762 GNUNET_assert (bytes != GNUNET_SYSERR);
1764 if (bytes != fm->size)
1766 finish = GNUNET_malloc (sizeof (struct Finish_send));
1767 finish->plugin = plugin;
1768 finish->msgheader = fm->frag + bytes;
1769 finish->size = fm->size - bytes;
1770 finish->msgstart = (struct GNUNET_MessageHeader *) fm->frag;
1772 GNUNET_assert (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK);
1774 plugin->server_write_task =
1775 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1776 plugin->server_stdin_handle,
1777 &finish_sending, finish);
1782 GNUNET_free (fm->frag);
1784 set_next_send (plugin);
1786 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
1790 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
1791 "do_transmit did nothing, should not happen!\n");
1795 * Another peer has suggested an address for this
1796 * peer and transport plugin. Check that this could be a valid
1797 * address. If so, consider adding it to the list
1800 * @param cls closure
1801 * @param addr pointer to the address
1802 * @param addrlen length of addr
1803 * @return GNUNET_OK if this is a plausible address for this peer
1807 wlan_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1809 //struct Plugin *plugin = cls;
1811 /* check if the address is plausible; if so,
1812 * add it to our list! */
1814 GNUNET_assert (cls != NULL);
1815 //FIXME mitm is not checked
1816 //Mac Address has 6 bytes
1819 /* TODO check for bad addresses like multicast, broadcast, etc */
1821 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1822 "wlan_plugin_address_suggested got good address, size %u!\n", addrlen);
1827 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1828 "wlan_plugin_address_suggested got bad address, size %u!\n", addrlen);
1830 return GNUNET_SYSERR;
1834 * Function that can be used by the transport service to transmit
1835 * a message using the plugin.
1837 * @param cls closure
1838 * @param target who should receive this message
1839 * @param priority how important is the message
1840 * @param msgbuf the message to transmit
1841 * @param msgbuf_size number of bytes in 'msgbuf'
1842 * @param timeout when should we time out
1843 * @param session which session must be used (or NULL for "any")
1844 * @param addr the address to use (can be NULL if the plugin
1845 * is "on its own" (i.e. re-use existing TCP connection))
1846 * @param addrlen length of the address in bytes
1847 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1848 * otherwise the plugin may use other addresses or
1849 * existing connections (if available)
1850 * @param cont continuation to call once the message has
1851 * been transmitted (or if the transport is ready
1852 * for the next transmission call; or if the
1853 * peer disconnected...)
1854 * @param cont_cls closure for cont
1855 * @return number of bytes used (on the physical network, with overheads);
1856 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1857 * and does NOT mean that the message was not transmitted (DV)
1860 wlan_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
1861 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
1862 struct GNUNET_TIME_Relative timeout, struct Session *session,
1863 const void *addr, size_t addrlen, int force_address,
1864 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1866 struct Plugin *plugin = cls;
1867 struct PendingMessage *newmsg;
1868 struct WlanHeader *wlanheader;
1870 //check if msglen > 0
1871 GNUNET_assert (msgbuf_size > 0);
1873 //get session if needed
1874 if (session == NULL)
1876 if (wlan_plugin_address_suggested (plugin, addr, addrlen) == GNUNET_OK)
1878 session = get_session (plugin, addr, target);
1882 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1883 _("Wlan Address len %d is wrong\n"), addrlen);
1888 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan messages queued"), 1, GNUNET_NO);
1892 //queue message in session
1893 //test if there is no other message in the "queue"
1894 //FIXME: to many send requests
1895 if (session->pending_message_head != NULL)
1897 newmsg = session->pending_message_head;
1898 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1899 "wlan_plugin_send: a pending message is already in the queue for this client\n remaining time to send this message is %u, queued fragment messages for this mac connection %u\n",
1900 GNUNET_TIME_absolute_get_remaining (newmsg->
1902 session->mac->fragment_messages_out_count);
1905 newmsg = GNUNET_malloc (sizeof (struct PendingMessage));
1906 newmsg->msg = GNUNET_malloc (msgbuf_size + sizeof (struct WlanHeader));
1907 wlanheader = newmsg->msg;
1908 //copy msg to buffer, not fragmented / segmented yet, but with message header
1909 wlanheader->header.size = htons (msgbuf_size + sizeof (struct WlanHeader));
1910 wlanheader->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA);
1911 memcpy (&(wlanheader->target), target, sizeof (struct GNUNET_PeerIdentity));
1912 memcpy (&(wlanheader->source), plugin->env->my_identity,
1913 sizeof (struct GNUNET_PeerIdentity));
1914 wlanheader->crc = 0;
1915 memcpy (&wlanheader[1], msgbuf, msgbuf_size);
1918 ((char *) wlanheader, msgbuf_size + sizeof (struct WlanHeader)));
1919 //GNUNET_log_from(GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME, "Wlan message Header crc: %u, %u\n",getcrc32((char*) wlanheader, msgbuf_size + sizeof(struct WlanHeader)), wlanheader->crc);
1920 //hexdump(newmsg->msg, msgbuf_size + sizeof(struct WlanHeader));
1922 newmsg->transmit_cont = cont;
1923 newmsg->transmit_cont_cls = cont_cls;
1924 newmsg->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1926 newmsg->timeout.abs_value = newmsg->timeout.abs_value - 500;
1928 newmsg->message_size = msgbuf_size + sizeof (struct WlanHeader);
1930 GNUNET_CONTAINER_DLL_insert_tail (session->pending_message_head,
1931 session->pending_message_tail, newmsg);
1934 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1935 "New message for %p with size (incl wlan header) %u added\n",
1936 session, newmsg->message_size);
1938 #if DEBUG_wlan_msg_dump
1939 hexdump (msgbuf, GNUNET_MIN (msgbuf_size, 256));
1942 queue_session (plugin, session);
1944 check_fragment_queue (plugin);
1945 //FIXME not the correct size
1951 * function to free a mac endpoint
1952 * @param plugin pointer to the plugin struct
1953 * @param endpoint pointer to the MacEndpoint to free
1956 free_macendpoint (struct Plugin *plugin, struct MacEndpoint *endpoint)
1958 struct Sessionqueue *sessions;
1959 struct Sessionqueue *sessions_next;
1961 GNUNET_assert (endpoint != NULL);
1963 sessions = endpoint->sessions_head;
1964 while (sessions != NULL)
1966 sessions_next = sessions->next;
1967 free_session (plugin, sessions, GNUNET_NO);
1968 sessions = sessions_next;
1971 GNUNET_CONTAINER_DLL_remove (plugin->mac_head, plugin->mac_tail, endpoint);
1972 if (endpoint->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1973 GNUNET_SCHEDULER_cancel (endpoint->timeout_task);
1974 GNUNET_free (endpoint);
1979 * function to free a session
1980 * @param plugin pointer to the plugin
1981 * @param queue pointer to the sessionqueue element to free
1982 * @param free_macendpoint if GNUNET_YES and mac endpoint would be empty, free mac endpoint
1985 free_session (struct Plugin *plugin, struct Sessionqueue *queue,
1986 int do_free_macendpoint)
1988 struct Sessionqueue *pendingsession;
1989 struct Sessionqueue *pendingsession_tmp;
1990 struct PendingMessage *pm;
1991 struct MacEndpoint *endpoint;
1992 struct FragmentMessage *fm;
1993 struct FragmentMessage *fmnext;
1996 GNUNET_assert (queue != NULL);
1997 GNUNET_assert (queue->content != NULL);
2000 //is this session pending for send
2001 pendingsession = plugin->pending_Sessions_head;
2002 while (pendingsession != NULL)
2004 pendingsession_tmp = pendingsession->next;
2005 if (pendingsession->content == queue->content)
2007 plugin->pendingsessions--;
2008 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
2009 plugin->pending_Sessions_tail,
2011 GNUNET_free (pendingsession);
2013 GNUNET_assert (check == 0);
2016 pendingsession = pendingsession_tmp;
2019 endpoint = queue->content->mac;
2020 fm = endpoint->sending_messages_head;
2024 if (fm->session == queue->content)
2026 free_fragment_message (plugin, fm);
2031 // remove PendingMessage
2032 pm = queue->content->pending_message_head;
2035 GNUNET_CONTAINER_DLL_remove (queue->content->pending_message_head,
2036 queue->content->pending_message_tail, pm);
2037 GNUNET_free (pm->msg);
2039 pm = queue->content->pending_message_head;
2042 GNUNET_CONTAINER_DLL_remove (endpoint->sessions_head, endpoint->sessions_tail,
2045 if (endpoint->sessions_head == NULL && do_free_macendpoint == GNUNET_YES)
2047 free_macendpoint (plugin, endpoint);
2050 if (queue->content->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2051 GNUNET_SCHEDULER_cancel (queue->content->timeout_task);
2052 GNUNET_free (queue);
2053 check_fragment_queue (plugin);
2057 * Function that can be used to force the plugin to disconnect
2058 * from the given peer and cancel all previous transmissions
2059 * (and their continuation).
2061 * @param cls closure
2062 * @param target peer from which to disconnect
2065 wlan_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
2067 struct Plugin *plugin = cls;
2068 struct Sessionqueue *queue;
2069 struct Sessionqueue *queue_next;
2070 struct MacEndpoint *endpoint = plugin->mac_head;
2071 struct MacEndpoint *endpoint_next;
2073 // just look at all the session for the needed one
2074 while (endpoint != NULL)
2076 queue = endpoint->sessions_head;
2077 endpoint_next = endpoint->next;
2078 while (queue != NULL)
2080 // content is never NULL
2081 GNUNET_assert (queue->content != NULL);
2082 queue_next = queue->next;
2084 (target, &(queue->content->target),
2085 sizeof (struct GNUNET_PeerIdentity)) == 0)
2087 free_session (plugin, queue, GNUNET_YES);
2092 endpoint = endpoint_next;
2097 * Convert the transports address to a nice, human-readable
2100 * @param cls closure
2101 * @param type name of the transport that generated the address
2102 * @param addr one of the addresses of the host, NULL for the last address
2103 * the specific address format depends on the transport
2104 * @param addrlen length of the address
2105 * @param numeric should (IP) addresses be displayed in numeric form?
2106 * @param timeout after how long should we give up?
2107 * @param asc function to call on each string
2108 * @param asc_cls closure for asc
2111 wlan_plugin_address_pretty_printer (void *cls, const char *type,
2112 const void *addr, size_t addrlen,
2114 struct GNUNET_TIME_Relative timeout,
2115 GNUNET_TRANSPORT_AddressStringCallback asc,
2119 const unsigned char *input;
2121 //GNUNET_assert(cls !=NULL);
2122 if (addrlen != sizeof(struct MacAddress))
2124 /* invalid address (MAC addresses have 6 bytes) */
2127 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2128 "Func wlan_plugin_address_pretty_printer got size: %u, worng size!\n",
2134 input = (const unsigned char *) addr;
2135 GNUNET_asprintf (&ret,
2136 "Transport %s: %s Mac-Address %.2X:%.2X:%.2X:%.2X:%.2X:%.2X",type,
2137 PROTOCOL_PREFIX, input[0], input[1], input[2], input[3],
2138 input[4], input[5]);
2140 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2141 "Func wlan_plugin_address_pretty_printer got size: %u, nummeric %u, type %s; made string: %s\n",
2142 addrlen, numeric, type, ret);
2144 asc ( asc_cls, ret);
2145 //only one mac address per plugin
2146 asc ( asc_cls, NULL);
2152 * handels the data after all fragments are put together
2153 * @param cls macendpoint this messages belongs to
2154 * @param hdr pointer to the data
2157 wlan_data_message_handler (void *cls, const struct GNUNET_MessageHeader *hdr)
2159 struct MacEndpoint *endpoint = (struct MacEndpoint *) cls;
2160 struct Plugin *plugin = endpoint->plugin;
2161 struct WlanHeader *wlanheader;
2162 struct Session *session;
2164 const struct GNUNET_MessageHeader *temp_hdr;
2165 struct GNUNET_PeerIdentity tmpsource;
2168 if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_DATA)
2172 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2173 "Func wlan_data_message_handler got GNUNET_MESSAGE_TYPE_WLAN_DATA size: %u\n",
2177 if (ntohs (hdr->size) <
2178 sizeof (struct WlanHeader) + sizeof (struct GNUNET_MessageHeader))
2180 //packet not big enought
2184 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan whole messages received"), 1, GNUNET_NO);
2185 wlanheader = (struct WlanHeader *) hdr;
2187 session = search_session (plugin, endpoint, &wlanheader->source);
2189 temp_hdr = (const struct GNUNET_MessageHeader *) &wlanheader[1];
2190 crc = ntohl (wlanheader->crc);
2191 wlanheader->crc = 0;
2192 if (getcrc32 ((char *) wlanheader, ntohs (wlanheader->header.size)) != crc)
2194 //wrong crc, dispose message
2195 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2196 "Wlan message header crc was wrong: %u != %u\n",
2197 getcrc32 ((char *) wlanheader,
2198 ntohs (wlanheader->header.size)), crc);
2199 hexdump ((void *) hdr, ntohs (hdr->size));
2203 //if not in session list
2204 if (session == NULL)
2207 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2208 "WLAN client not in session list: packet size = %u, inner size = %u, header size = %u\n",
2209 ntohs (wlanheader->header.size), ntohs (temp_hdr->size),
2210 sizeof (struct WlanHeader));
2212 //try if it is a hello message
2213 if (ntohs (wlanheader->header.size) >=
2214 ntohs (temp_hdr->size) + sizeof (struct WlanHeader))
2216 if (ntohs (temp_hdr->type) == GNUNET_MESSAGE_TYPE_HELLO)
2218 if (GNUNET_HELLO_get_id
2219 ((const struct GNUNET_HELLO_Message *) temp_hdr,
2220 &tmpsource) == GNUNET_OK)
2222 session = create_session (plugin, endpoint, &tmpsource);
2226 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2227 "WLAN client not in session list and hello message is not okay\n");
2234 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2235 "WLAN client not in session list and not a hello message\n");
2241 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2242 "WLAN client not in session list and message size in does not fit\npacket size = %u, inner size = %u, header size = %u\n",
2243 ntohs (wlanheader->header.size),
2244 ntohs (temp_hdr->size), sizeof (struct WlanHeader));
2249 //"receive" the message
2252 (&wlanheader->source, &session->target,
2253 sizeof (struct GNUNET_PeerIdentity)) != 0)
2257 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2258 "WLAN peer source id doesn't match packet peer source id: session %p\n",
2265 (&wlanheader->target, plugin->env->my_identity,
2266 sizeof (struct GNUNET_PeerIdentity)) != 0)
2270 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2271 "WLAN peer target id doesn't match our peer id: session %p\n",
2277 GNUNET_SERVER_mst_receive (plugin->data_tokenizer, session,
2278 (const char *) temp_hdr,
2279 ntohs (hdr->size) - sizeof (struct WlanHeader),
2280 GNUNET_YES, GNUNET_NO);
2286 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2287 "wlan_data_message_handler got wrong message type: %u\n",
2294 * function to process the a message, give it to the higher layer
2295 * @param cls pointer to the plugin
2296 * @param client pointer to the session this message belongs to
2297 * @param hdr start of the message
2299 //TODO ATS informations
2301 process_data (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
2304 GNUNET_assert (client != NULL);
2305 GNUNET_assert (cls != NULL);
2306 struct Session *session = (struct Session *) client;
2307 struct Plugin *plugin = (struct Plugin *) cls;
2309 struct GNUNET_TRANSPORT_ATS_Information distance[2];
2311 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2312 distance[0].value = htonl (1);
2313 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2314 distance[1].value = htonl (0);
2317 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2318 "Calling plugin->env->receive for session %p; %s; size: %u\n",
2319 session, wlan_plugin_address_to_string (NULL,
2325 plugin->env->receive (plugin->env->cls, &(session->target), hdr,
2326 (const struct GNUNET_TRANSPORT_ATS_Information *)
2327 &distance, 2, session,
2328 (const char *) &session->mac->addr,
2329 sizeof (session->mac->addr));
2333 * Function used for to process the data received from the wlan interface
2335 * @param cls the plugin handle
2336 * @param session_light pointer to the struct holding known informations
2337 * @param hdr hdr of the GNUNET_MessageHeader
2338 * @param rxinfo pointer to the radiotap informations got with this packet
2341 wlan_data_helper (void *cls, struct Session_light *session_light,
2342 const struct GNUNET_MessageHeader *hdr,
2343 const struct Radiotap_rx *rxinfo)
2345 struct Plugin *plugin = cls;
2346 struct FragmentMessage *fm;
2347 struct FragmentMessage *fm2;
2348 struct GNUNET_PeerIdentity tmpsource;
2351 if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_HELLO)
2354 //TODO better DOS protection, error handling
2355 //TODO test first than create session
2356 GNUNET_assert (session_light != NULL);
2359 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2360 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_HELLO size: %u; %s\n",
2361 ntohs (hdr->size), wlan_plugin_address_to_string (NULL,
2362 session_light->addr.
2366 if (session_light->macendpoint == NULL)
2368 session_light->macendpoint =
2369 get_macendpoint (plugin, &session_light->addr, GNUNET_YES);
2373 if (GNUNET_HELLO_get_id
2374 ((const struct GNUNET_HELLO_Message *) hdr,
2375 &tmpsource) == GNUNET_OK)
2377 session_light->session = search_session (plugin, session_light->macendpoint, &tmpsource);
2378 if (session_light->session == NULL)
2380 session_light->session = create_session (plugin, session_light->macendpoint, &tmpsource);
2382 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan hallo messages received"), 1, GNUNET_NO);
2383 plugin->env->receive(plugin->env->cls,&session_light->session->target,hdr, NULL, 0, session_light->session,
2384 (const char *) &session_light->session->mac->addr,
2385 sizeof (session_light->session->mac->addr));
2389 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2390 "WLAN client not in session list and hello message is not okay\n");
2397 else if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_FRAGMENT)
2400 GNUNET_assert (session_light != NULL);
2401 if (session_light->macendpoint == NULL)
2403 session_light->macendpoint =
2404 get_macendpoint (plugin, &session_light->addr, GNUNET_YES);
2408 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2409 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_FRAGMENT with size: %u; mac endpoint %p: %s\n",
2410 ntohs (hdr->size), session_light->macendpoint,
2411 wlan_plugin_address_to_string (NULL,
2412 session_light->addr.mac,
2415 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan fragments received"), 1, GNUNET_NO);
2417 GNUNET_DEFRAGMENT_process_fragment (session_light->macendpoint->defrag,
2420 if (ret == GNUNET_NO)
2422 session_light->macendpoint->dups++;
2424 else if (ret == GNUNET_OK)
2426 session_light->macendpoint->fragc++;
2428 set_next_send (plugin);
2434 else if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_FRAGMENT_ACK)
2436 GNUNET_assert (session_light != NULL);
2437 if (session_light->macendpoint == NULL)
2439 session_light->macendpoint =
2440 get_macendpoint (plugin, &session_light->addr, GNUNET_NO);
2443 if (session_light->macendpoint == NULL)
2446 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2447 "Macendpoint does not exist for this GNUNET_MESSAGE_TYPE_FRAGMENT_ACK size: %u; %s\n",
2448 ntohs (hdr->size), wlan_plugin_address_to_string (NULL,
2449 session_light->addr.mac,
2456 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2457 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_FRAGMENT_ACK size: %u; mac endpoint: %p; %s\n",
2458 ntohs (hdr->size), session_light->macendpoint,
2459 wlan_plugin_address_to_string (NULL,
2460 session_light->addr.mac,
2463 fm = session_light->macendpoint->sending_messages_head;
2467 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan acks received"), 1, GNUNET_NO);
2468 int ret = GNUNET_FRAGMENT_process_ack (fm->fragcontext, hdr);
2470 if (ret == GNUNET_OK)
2472 #if DEBUG_wlan_retransmission
2473 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2474 "Got last ack, finished fragment message %p\n", fm);
2476 session_light->macendpoint->acks++;
2477 fm->session->last_activity = GNUNET_TIME_absolute_get ();
2478 session_light->macendpoint->last_activity = fm->session->last_activity;
2479 free_fragment_message (plugin, fm);
2480 check_fragment_queue (plugin);
2483 if (ret == GNUNET_NO)
2485 #if DEBUG_wlan_retransmission
2486 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2487 "Got ack for: %p\n", fm);
2489 session_light->macendpoint->acks++;
2492 if (ret == GNUNET_SYSERR)
2500 #if DEBUG_wlan_retransmission
2501 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2502 "WLAN fragment not in fragment list\n");
2510 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2511 "WLAN packet inside the WLAN helper packet has not the right type: %u size: %u\n",
2512 ntohs (hdr->type), ntohs (hdr->size));
2518 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2519 "Helper finished\n");
2525 * Function to print mac addresses nice *
2526 * @param pointer to 6 byte with the mac address
2527 * @return pointer to the chars which hold the print out
2530 macprinter (const u_int8_t * mac)
2532 static char macstr[20];
2534 GNUNET_snprintf (macstr, sizeof (macstr), "%X:%X:%X:%X:%X:%X", mac[0], mac[1],
2535 mac[2], mac[3], mac[4], mac[5]);
2540 * Function for the scheduler if a mac endpoint times out
2541 * @param cls pointer to the MacEndpoint
2542 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
2545 macendpoint_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2547 struct MacEndpoint *endpoint = cls;
2549 GNUNET_assert (endpoint != NULL);
2550 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2551 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2555 if (GNUNET_TIME_absolute_get_remaining
2556 (GNUNET_TIME_absolute_add
2557 (endpoint->last_activity, MACENDPOINT_TIMEOUT)).rel_value == 0)
2559 GNUNET_STATISTICS_update (endpoint->plugin->env->stats, _("# wlan mac endpoints timeouts"), 1, GNUNET_NO);
2560 free_macendpoint (endpoint->plugin, endpoint);
2564 endpoint->timeout_task =
2565 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
2571 * function to create an macendpoint
2572 * @param plugin pointer to the plugin struct
2573 * @param addr pointer to the macaddress
2574 * @return returns a macendpoint
2576 static struct MacEndpoint *
2577 create_macendpoint (struct Plugin *plugin, const struct MacAddress *addr)
2579 struct MacEndpoint *newend = GNUNET_malloc (sizeof (struct MacEndpoint));
2581 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan mac endpoints created"), 1, GNUNET_NO);
2582 newend->addr = *addr;
2583 newend->plugin = plugin;
2584 newend->addr = *addr;
2585 newend->fragment_messages_out_count = 0;
2587 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
2588 MESSAGES_IN_DEFRAG_QUEUE_PER_MAC,
2589 newend, &wlan_data_message_handler,
2591 newend->last_activity = GNUNET_TIME_absolute_get ();
2592 newend->timeout_task =
2593 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
2596 plugin->mac_count++;
2597 GNUNET_CONTAINER_DLL_insert_tail (plugin->mac_head, plugin->mac_tail, newend);
2599 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2600 "New Mac Endpoint %p: %s\n", newend,
2601 wlan_plugin_address_to_string (NULL, newend->addr.mac, 6));
2607 * Function used for to process the data from the suid process
2609 * @param cls the plugin handle
2610 * @param client client that send the data (not used)
2611 * @param hdr header of the GNUNET_MessageHeader
2614 wlan_process_helper (void *cls, void *client,
2615 const struct GNUNET_MessageHeader *hdr)
2617 struct Plugin *plugin = cls;
2618 struct ieee80211_frame *wlanIeeeHeader = NULL;
2619 struct Session_light *session_light = NULL;
2620 struct Radiotap_rx *rxinfo;
2621 const struct GNUNET_MessageHeader *temp_hdr = NULL;
2626 switch (ntohs (hdr->type))
2628 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA:
2630 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2631 "Func wlan_process_helper got GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA size: %u\n",
2634 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan WLAN_HELPER_DATA received"), 1, GNUNET_NO);
2635 //call wlan_process_helper with the message inside, later with wlan: analyze signal
2636 if (ntohs (hdr->size) <
2637 sizeof (struct ieee80211_frame) + 2*sizeof (struct GNUNET_MessageHeader) +
2638 sizeof (struct Radiotap_rx))
2641 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2642 "Size of packet is too small; size: %u min size: %u\n",
2644 sizeof (struct ieee80211_frame) +
2645 sizeof (struct GNUNET_MessageHeader));
2648 /* FIXME: restart SUID process */
2652 rxinfo = (struct Radiotap_rx *) &hdr[1];
2653 wlanIeeeHeader = (struct ieee80211_frame *) &rxinfo[1];
2655 //process only if it is an broadcast or for this computer both with the gnunet bssid
2659 (&(wlanIeeeHeader->i_addr3), &mac_bssid,
2660 sizeof (struct MacAddress)) == 0)
2662 //check for broadcast or mac
2664 (&(wlanIeeeHeader->i_addr1), &bc_all_mac,
2665 sizeof (struct MacAddress)) == 0) ||
2666 (memcmp (&(wlanIeeeHeader->i_addr1), &(plugin->mac_address),
2667 sizeof (struct MacAddress)) == 0))
2669 //if packet is from us return
2670 if ((memcmp (&(wlanIeeeHeader->i_addr2), &(plugin->mac_address),
2671 sizeof (struct MacAddress)) == 0)){
2674 // process the inner data
2678 ntohs (hdr->size) - sizeof (struct ieee80211_frame) -
2679 sizeof (struct GNUNET_MessageHeader) - sizeof (struct Radiotap_rx);
2681 session_light = GNUNET_malloc (sizeof (struct Session_light));
2682 memcpy (&session_light->addr, &(wlanIeeeHeader->i_addr2),
2683 sizeof (struct MacAddress));
2684 //session_light->session = search_session(plugin,session_light->addr);
2685 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan messaged for this client received"), 1, GNUNET_NO);
2688 while (pos < datasize)
2690 temp_hdr = (struct GNUNET_MessageHeader *) &wlanIeeeHeader[1] + pos;
2691 if (ntohs(temp_hdr->size) <= datasize + pos)
2693 GNUNET_STATISTICS_update (plugin->env->stats, _("# wlan messaged in WLAN_HELPER_DATA received"), 1, GNUNET_NO);
2694 wlan_data_helper (plugin, session_light, temp_hdr, rxinfo);
2699 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2700 "Size of packet is too small; size: %u > size of packet: %u\n",
2701 ntohs(temp_hdr->size),datasize + pos);
2704 pos += ntohs (temp_hdr->size);
2709 GNUNET_free (session_light);
2714 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2715 "Func wlan_process_helper got wrong MAC: %s\n",
2716 macprinter (wlanIeeeHeader->i_addr1));
2723 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2724 "Func wlan_process_helper got wrong BSSID: %s\n",
2725 macprinter (wlanIeeeHeader->i_addr2));
2729 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
2730 //TODO more control messages
2731 if (ntohs (hdr->size) != sizeof (struct Wlan_Helper_Control_Message))
2734 /* FIXME: restart SUID process */
2737 memcpy (&plugin->mac_address, &hdr[1], sizeof (struct MacAddress));
2739 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2740 "Received WLAN_HELPER_CONTROL message with transport of address %s\n",
2741 wlan_plugin_address_to_string (cls, &plugin->mac_address,
2745 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2746 &plugin->mac_address,
2747 sizeof (struct MacAddress));
2751 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2752 "Func wlan_process_helper got unknown message with number %u, size %u\n",
2753 ntohs (hdr->type), ntohs (hdr->size));
2756 #if DEBUG_wlan_msg_dump
2757 hexdump (hdr, GNUNET_MIN (ntohs (hdr->size), 256));
2765 * We have been notified that wlan-helper has written something to stdout.
2766 * Handle the output, then reschedule this function to be called again once
2767 * more is available.
2769 * @param cls the plugin handle
2770 * @param tc the scheduling context
2773 wlan_plugin_helper_read (void *cls,
2774 const struct GNUNET_SCHEDULER_TaskContext *tc)
2776 struct Plugin *plugin = cls;
2778 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2780 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2783 char mybuf[WLAN_MTU + sizeof (struct GNUNET_MessageHeader)];
2787 GNUNET_DISK_file_read (plugin->server_stdout_handle, mybuf,
2792 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2794 ("Finished reading from wlan-helper stdout with code: %d\n"),
2799 GNUNET_SERVER_mst_receive (plugin->suid_tokenizer, NULL, mybuf, bytes,
2800 GNUNET_NO, GNUNET_NO);
2802 GNUNET_assert (plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2803 plugin->server_read_task =
2804 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2805 plugin->server_stdout_handle,
2806 &wlan_plugin_helper_read, plugin);
2810 * Start the gnunet-wlan-helper process.
2812 * @param plugin the transport plugin
2813 * @param testmode should we use the dummy driver for testing?
2814 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
2817 wlan_transport_start_wlan_helper (struct Plugin *plugin, int testmode)
2819 const char *filenamehw = "gnunet-transport-wlan-helper";
2820 const char *filenameloopback = "gnunet-transport-wlan-helper-dummy";
2822 plugin->server_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
2823 if (plugin->server_stdout == NULL)
2824 return GNUNET_SYSERR;
2826 plugin->server_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);
2827 if (plugin->server_stdin == NULL)
2828 return GNUNET_SYSERR;
2830 /* Start the server process */
2836 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2837 "Starting gnunet-wlan-helper process cmd: %s %s %i\n",
2838 filenamehw, plugin->interface, testmode);
2841 if (GNUNET_OS_check_helper_binary (filenamehw) == GNUNET_YES)
2843 plugin->server_proc =
2844 GNUNET_OS_start_process (plugin->server_stdin, plugin->server_stdout,
2845 filenamehw, filenamehw, plugin->interface,
2848 else if (GNUNET_OS_check_helper_binary (filenamehw) == GNUNET_NO)
2850 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
2851 "gnunet-transport-wlan-helper is not suid, please change it or look at the doku\n");
2856 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
2857 "gnunet-transport-wlan-helper not found, please look if it exists and is the $PATH variable!\n");
2862 else if (testmode == 1)
2866 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2867 "Starting gnunet-wlan-helper loopback 1 process cmd: %s %s %i\n",
2868 filenameloopback, plugin->interface, testmode);
2871 if (GNUNET_OS_check_helper_binary (filenameloopback) != GNUNET_SYSERR)
2873 plugin->server_proc =
2874 GNUNET_OS_start_process (plugin->server_stdin, plugin->server_stdout,
2875 filenameloopback, filenameloopback, "1",
2880 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
2881 "gnunet-transport-wlan-helper-dummy not found, please look if it exists and is the $PATH variable!\n");
2885 else if (testmode == 2)
2888 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2889 "Starting gnunet-wlan-helper loopback 2 process cmd: %s %s %i\n",
2890 filenameloopback, plugin->interface, testmode);
2892 if (GNUNET_OS_check_helper_binary (filenameloopback) != GNUNET_SYSERR)
2894 plugin->server_proc =
2895 GNUNET_OS_start_process (plugin->server_stdin, plugin->server_stdout,
2896 filenameloopback, filenameloopback, "2",
2901 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
2902 "gnunet-transport-wlan-helper-dummy not found, please look if it exists and is in the $PATH variable!\n");
2906 if (plugin->server_proc == NULL)
2909 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2910 "Failed to start gnunet-wlan-helper process\n");
2912 return GNUNET_SYSERR;
2915 /* Close the write end of the read pipe */
2916 GNUNET_DISK_pipe_close_end (plugin->server_stdout,
2917 GNUNET_DISK_PIPE_END_WRITE);
2919 /* Close the read end of the write pipe */
2920 GNUNET_DISK_pipe_close_end (plugin->server_stdin, GNUNET_DISK_PIPE_END_READ);
2922 plugin->server_stdout_handle =
2923 GNUNET_DISK_pipe_handle (plugin->server_stdout,
2924 GNUNET_DISK_PIPE_END_READ);
2925 plugin->server_stdin_handle =
2926 GNUNET_DISK_pipe_handle (plugin->server_stdin,
2927 GNUNET_DISK_PIPE_END_WRITE);
2929 GNUNET_assert (plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2932 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2933 "Adding server_read_task for the wlan-helper\n");
2936 plugin->server_read_task =
2937 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2938 plugin->server_stdout_handle,
2939 &wlan_plugin_helper_read, plugin);
2945 * Exit point from the plugin.
2946 * @param cls pointer to the api struct
2951 libgnunet_plugin_transport_wlan_done (void *cls)
2953 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2954 struct Plugin *plugin = api->cls;
2955 struct MacEndpoint *endpoint = plugin->mac_head;
2956 struct MacEndpoint *endpoint_next;
2959 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2960 "libgnunet_plugin_transport_wlan_done started\n");
2963 GNUNET_DISK_pipe_close (plugin->server_stdout);
2964 GNUNET_DISK_pipe_close (plugin->server_stdin);
2965 GNUNET_OS_process_kill (plugin->server_proc, 9);
2966 GNUNET_OS_process_close (plugin->server_proc);
2968 GNUNET_assert (cls != NULL);
2970 while (endpoint != NULL)
2972 endpoint_next = endpoint->next;
2973 free_macendpoint (plugin, endpoint);
2974 endpoint = endpoint_next;
2977 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
2979 GNUNET_SCHEDULER_cancel (plugin->server_write_delay_task);
2980 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
2982 if (plugin->server_write_task != GNUNET_SCHEDULER_NO_TASK)
2984 GNUNET_SCHEDULER_cancel (plugin->server_write_task);
2985 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
2987 if (plugin->server_read_task != GNUNET_SCHEDULER_NO_TASK)
2989 GNUNET_SCHEDULER_cancel (plugin->server_read_task);
2990 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2993 if (plugin->suid_tokenizer != NULL)
2994 GNUNET_SERVER_mst_destroy (plugin->suid_tokenizer);
2996 if (plugin->data_tokenizer != NULL)
2997 GNUNET_SERVER_mst_destroy (plugin->data_tokenizer);
2999 GNUNET_free_non_null (plugin->interface);
3000 GNUNET_free (plugin);
3006 * Entry point for the plugin.
3008 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
3009 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
3012 libgnunet_plugin_transport_wlan_init (void *cls)
3014 //struct GNUNET_SERVICE_Context *service;
3015 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3016 struct GNUNET_TRANSPORT_PluginFunctions *api;
3017 struct Plugin *plugin;
3018 static unsigned long long testmode = 0;
3020 GNUNET_assert (cls != NULL);
3022 plugin = GNUNET_malloc (sizeof (struct Plugin));
3024 plugin->pendingsessions = 0;
3025 plugin->mac_count = 0;
3026 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
3027 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
3028 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
3029 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
3030 GNUNET_BANDWIDTH_value_init (100 * 1024 *
3033 plugin->suid_tokenizer =
3034 GNUNET_SERVER_mst_create (&wlan_process_helper, plugin);
3036 plugin->data_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
3038 //plugin->sessions = GNUNET_malloc (sizeof (struct Sessionqueue));
3039 //plugin->pending_Sessions_head = GNUNET_malloc (sizeof (struct Sessionqueue));
3041 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3043 api->send = &wlan_plugin_send;
3044 api->disconnect = &wlan_plugin_disconnect;
3045 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
3046 api->check_address = &wlan_plugin_address_suggested;
3047 api->address_to_string = &wlan_plugin_address_to_string;
3050 if (GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "TESTMODE"))
3052 if (GNUNET_SYSERR ==
3053 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-wlan",
3054 "TESTMODE", &testmode))
3055 testmode = 0; //default value
3058 if (GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "INTERFACE"))
3060 if (GNUNET_CONFIGURATION_get_value_string
3061 (env->cfg, "transport-wlan", "INTERFACE",
3062 &(plugin->interface)) != GNUNET_YES)
3064 libgnunet_plugin_transport_wlan_done (api);
3070 wlan_transport_start_wlan_helper (plugin, testmode);
3071 set_next_beacon_time (plugin);
3072 set_next_send(plugin);
3074 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
3075 "wlan init finished\n");
3081 /* end of plugin_transport_wlan.c */