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
96 #define DEBUG_wlan GNUNET_NO
97 #define DEBUG_wlan_retransmission GNUNET_NO
98 #define DEBUG_wlan_ip_udp_packets_on_air GNUNET_NO
99 #define DEBUG_wlan_msg_dump GNUNET_NO
102 #define IEEE80211_ADDR_LEN 6 /* size of 802.11 address */
104 #define IEEE80211_FC0_VERSION_MASK 0x03
105 #define IEEE80211_FC0_VERSION_SHIFT 0
106 #define IEEE80211_FC0_VERSION_0 0x00
107 #define IEEE80211_FC0_TYPE_MASK 0x0c
108 #define IEEE80211_FC0_TYPE_SHIFT 2
109 #define IEEE80211_FC0_TYPE_MGT 0x00
110 #define IEEE80211_FC0_TYPE_CTL 0x04
111 #define IEEE80211_FC0_TYPE_DATA 0x08
114 * Structure of an internet header, naked of options.
118 #if __BYTE_ORDER == __LITTLE_ENDIAN
119 unsigned int ip_hl:4; /* header length */
120 unsigned int ip_v:4; /* version */
122 #if __BYTE_ORDER == __BIG_ENDIAN
123 unsigned int ip_v:4; /* version */
124 unsigned int ip_hl:4; /* header length */
126 u_int8_t ip_tos; /* type of service */
127 u_short ip_len; /* total length */
128 u_short ip_id; /* identification */
129 u_short ip_off; /* fragment offset field */
130 #define IP_RF 0x8000 /* reserved fragment flag */
131 #define IP_DF 0x4000 /* dont fragment flag */
132 #define IP_MF 0x2000 /* more fragments flag */
133 #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
134 u_int8_t ip_ttl; /* time to live */
135 u_int8_t ip_p; /* protocol */
136 u_short ip_sum; /* checksum */
137 struct in_addr ip_src, ip_dst; /* source and dest address */
149 * generic definitions for IEEE 802.11 frames
151 struct ieee80211_frame
155 u_int8_t i_addr1[IEEE80211_ADDR_LEN];
156 u_int8_t i_addr2[IEEE80211_ADDR_LEN];
157 u_int8_t i_addr3[IEEE80211_ADDR_LEN];
159 #if DEBUG_wlan_ip_udp_packets_on_air
167 * Encapsulation of all of the state of the plugin.
174 struct GNUNET_TRANSPORT_PluginEnvironment *env;
177 * List of open connections. head
179 struct MacEndpoint *mac_head;
182 * List of open connections. tail
184 struct MacEndpoint *mac_tail;
187 * Number of connections
189 unsigned int mac_count;
192 * encapsulation of data from the local wlan helper program
194 struct GNUNET_SERVER_MessageStreamTokenizer *suid_tokenizer;
197 * encapsulation of packets received from the wlan helper
199 struct GNUNET_SERVER_MessageStreamTokenizer *data_tokenizer;
202 * stdout pipe handle for the gnunet-wlan-helper process
204 struct GNUNET_DISK_PipeHandle *server_stdout;
207 * stdout file handle for the gnunet-wlan-helper process
209 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
212 * stdin pipe handle for the gnunet-wlan-helper process
214 struct GNUNET_DISK_PipeHandle *server_stdin;
217 * stdin file handle for the gnunet-wlan-helper process
219 const struct GNUNET_DISK_FileHandle *server_stdin_handle;
222 * ID of the gnunet-wlan-server std read task
224 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
227 * ID of the gnunet-wlan-server std read task
229 GNUNET_SCHEDULER_TaskIdentifier server_write_task;
232 * ID of the delay task for writing
234 GNUNET_SCHEDULER_TaskIdentifier server_write_delay_task;
237 * The process id of the wlan process
239 struct GNUNET_OS_Process *server_proc;
242 * The interface of the wlan card given to us by the user.
247 * The mac_address of the wlan card given to us by the helper.
249 struct MacAddress mac_address;
252 * Sessions currently pending for transmission
255 struct Sessionqueue *pending_Sessions_head;
258 * Sessions currently pending for transmission
259 * to a peer (tail), if any.
261 struct Sessionqueue *pending_Sessions_tail;
264 * number of pending sessions
266 unsigned int pendingsessions;
269 * Messages in the sending queues
271 int pending_Fragment_Messages;
274 * messages ready for send, head
276 struct FragmentMessage_queue *sending_messages_head;
278 * messages ready for send, tail
280 struct FragmentMessage_queue *sending_messages_tail;
282 * time of the next "hello-beacon"
284 struct GNUNET_TIME_Absolute beacon_time;
287 * queue to send acks for received fragments (head)
289 struct AckSendQueue *ack_send_queue_head;
292 * queue to send acks for received fragments (tail)
294 struct AckSendQueue *ack_send_queue_tail;
297 * Tracker for bandwidth limit
299 struct GNUNET_BANDWIDTH_Tracker tracker;
303 * Struct to store data if file write did not accept the whole packet
307 struct Plugin *plugin;
309 struct GNUNET_MessageHeader *msgstart;
314 * Queue of sessions, for the general session queue and the pending session queue
319 struct Sessionqueue *next;
320 struct Sessionqueue *prev;
321 struct Session *content;
325 * Queue of fragmented messages, for the sending queue of the plugin
328 struct FragmentMessage_queue
330 struct FragmentMessage_queue *next;
331 struct FragmentMessage_queue *prev;
332 struct FragmentMessage *content;
336 * Queue for the fragments received
339 struct Receive_Fragment_Queue
341 struct Receive_Fragment_Queue *next;
342 struct Receive_Fragment_Queue *prev;
346 struct Radiotap_rx rxinfo;
350 struct MacEndpoint_id_fragment_triple
352 struct MacEndpoint *endpoint;
354 struct FragmentMessage *fm;
358 struct Plugin_Session_pair
360 struct Plugin *plugin;
361 struct Session *session;
365 * Information kept for each message that is yet to
368 struct PendingMessage
373 struct PendingMessage *next;
377 struct PendingMessage *prev;
380 * The pending message
382 struct WlanHeader *msg;
385 * Size of the message
390 * Continuation function to call once the message
391 * has been sent. Can be NULL if there is no
392 * continuation to call.
394 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
397 * Cls for transmit_cont
399 void *transmit_cont_cls;
402 * Timeout value for the pending message.
404 struct GNUNET_TIME_Absolute timeout;
409 * Queue for acks to send for fragments recived
415 * next ack in the ack send queue
417 struct AckSendQueue *next;
419 * previous ack in the ack send queue
421 struct AckSendQueue *prev;
423 * pointer to the session this ack belongs to
425 struct MacEndpoint *endpoint;
427 * ID of message, to distinguish between the messages, picked randomly.
434 struct GNUNET_MessageHeader *hdr;
436 * pointer to the ieee wlan header
438 struct ieee80211_frame *ieeewlanheader;
440 * pointer to the radiotap header
442 struct Radiotap_Send *radioHeader;
446 * Session infos gathered from a messages
452 * the session this message belongs to
454 struct Session *session;
458 struct MacAddress addr;
463 struct MacEndpoint *macendpoint;
467 * Session handle for connections.
475 struct SessionHeader header;
478 * Message currently pending for transmission
479 * to this peer, if any. head
481 struct PendingMessage *pending_message_head;
484 * Message currently pending for transmission
485 * to this peer, if any. tail
487 struct PendingMessage *pending_message_tail;
490 * To whom are we talking to (set to our identity
491 * if we are still waiting for the welcome message)
493 struct GNUNET_PeerIdentity target;
496 * Address of the other peer (either based on our 'connect'
497 * call or on our 'accept' call).
502 * Last activity on this connection. Used to select preferred
503 * connection and timeout
505 struct GNUNET_TIME_Absolute last_activity;
510 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
515 struct MacEndpoint *mac;
518 * count of messages in the fragment out queue for this session
521 int fragment_messages_out_count;
526 * Struct to represent one network card connection
531 * Pointer to the global plugin struct.
533 struct Plugin *plugin;
535 * Struct to hold the session reachable over this mac; head
537 struct Sessionqueue *sessions_head;
539 * Struct to hold the session reachable over this mac; tail
541 struct Sessionqueue *sessions_tail;
543 * Messages currently sending
546 struct FragmentMessage *sending_messages_head;
549 * Messages currently sending
550 * to a peer (tail), if any.
552 struct FragmentMessage *sending_messages_tail;
556 struct MacEndpoint *next;
560 struct MacEndpoint *prev;
565 struct MacAddress addr;
568 * Defrag context for this mac endpoint
570 struct GNUNET_DEFRAGMENT_Context *defrag;
573 * count of messages in the fragment out queue for this mac endpoint
576 int fragment_messages_out_count;
584 * Duplicates received
599 * Last activity on this endpoint. Used to select preferred
602 struct GNUNET_TIME_Absolute last_activity;
607 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
611 * Struct for Messages in the fragment queue
614 struct FragmentMessage
618 * Session this message belongs to
621 struct Session *session;
624 * This is a doubly-linked list.
626 struct FragmentMessage *next;
629 * This is a doubly-linked list.
631 struct FragmentMessage *prev;
634 * Fragmentation context
636 struct GNUNET_FRAGMENT_Context *fragcontext;
639 * Timeout value for the message.
641 struct GNUNET_TIME_Absolute timeout;
646 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
659 * pointer to the ieee wlan header
661 struct ieee80211_frame *ieeewlanheader;
663 * pointer to the radiotap header
665 struct Radiotap_Send *radioHeader;
669 do_transmit (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
671 free_session (struct Plugin *plugin, struct Sessionqueue *queue,
672 int do_free_macendpoint);
673 static struct MacEndpoint *
674 create_macendpoint (struct Plugin *plugin, const struct MacAddress *addr);
677 * Generates a nice hexdump of a memory area.
679 * \param mem pointer to memory to dump
680 * \param length how many bytes to dump
683 hexdump (const void *mem, unsigned length)
686 char *src = (char *) mem;
688 printf ("dumping %u bytes from %p\r\n"
689 " 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF\r\n",
694 for (i = 0; i < length; i += 16, src += 16)
698 t += sprintf (t, "%04x: ", i);
699 for (j = 0; j < 16; j++)
702 t += sprintf (t, "%02X", src[j] & 0xff);
704 t += sprintf (t, " ");
705 t += sprintf (t, j % 2 ? " " : "-");
708 t += sprintf (t, " ");
709 for (j = 0; j < 16; j++)
713 if (isprint ((unsigned char) src[j]))
714 t += sprintf (t, "%c", src[j]);
716 t += sprintf (t, ".");
720 t += sprintf (t, " ");
724 t += sprintf (t, "\r\n");
730 * Function to find a MacEndpoint with a specific mac addr
731 * @param plugin pointer to the plugin struct
732 * @param addr pointer to the mac address
733 * @param create_new GNUNET_YES if a new end point should be created
736 static struct MacEndpoint *
737 get_macendpoint (struct Plugin *plugin, const struct MacAddress *addr,
740 struct MacEndpoint *queue = plugin->mac_head;
742 while (queue != NULL)
744 GNUNET_assert (queue->sessions_head != NULL);
745 if (memcmp (addr, &queue->addr, sizeof (struct MacAddress)) == 0)
746 return queue; /* session found */
750 if (create_new == GNUNET_YES)
752 return create_macendpoint (plugin, addr);
762 * search for a session with the macendpoint and peer id
764 * @param plugin pointer to the plugin struct
765 * @param endpoint pointer to the mac endpoint of the peer
766 * @param peer pointer to the peerid
767 * @return returns the session
769 static struct Session *
770 search_session (struct Plugin *plugin, const struct MacEndpoint *endpoint,
771 const struct GNUNET_PeerIdentity *peer)
773 GNUNET_assert (endpoint != NULL);
774 struct Sessionqueue *queue = endpoint->sessions_head;
776 while (queue != NULL)
778 GNUNET_assert (queue->content != NULL);
780 (peer, &queue->content->target,
781 sizeof (struct GNUNET_PeerIdentity)) == 0)
782 return queue->content; /* session found */
789 * Function called for a quick conversion of the binary address to
790 * a numeric address. Note that the caller must not free the
791 * address and that the next call to this function is allowed
792 * to override the address again.
795 * @param addr binary address
796 * @param addrlen length of the address
797 * @return string representing the same address
800 wlan_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
803 const struct MacAddress *mac;
805 if (addrlen != sizeof (struct MacAddress))
811 GNUNET_snprintf (ret, sizeof (ret), "%s Mac-Address %X:%X:%X:%X:%X:%X",
812 PROTOCOL_PREFIX, mac->mac[0], mac->mac[1], mac->mac[2],
813 mac->mac[3], mac->mac[4], mac->mac[5]);
818 * Function for the scheduler if a session times out
819 * @param cls pointer to the Sessionqueue
820 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
823 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
825 struct Sessionqueue *queue = cls;
827 GNUNET_assert (queue != NULL);
828 queue->content->timeout_task = GNUNET_SCHEDULER_NO_TASK;
829 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
833 if (GNUNET_TIME_absolute_get_remaining
834 (GNUNET_TIME_absolute_add
835 (queue->content->last_activity, SESSION_TIMEOUT)).rel_value == 0)
837 free_session (queue->content->mac->plugin, queue, GNUNET_YES);
841 queue->content->timeout_task =
842 GNUNET_SCHEDULER_add_delayed (SESSION_TIMEOUT, &session_timeout, queue);
847 * create a new session
849 * @param plugin pointer to the plugin struct
850 * @param endpoint pointer to the mac endpoint of the peer
851 * @param peer peer identity to use for this session
852 * @return returns the session
855 static struct Session *
856 create_session (struct Plugin *plugin, struct MacEndpoint *endpoint,
857 const struct GNUNET_PeerIdentity *peer)
859 GNUNET_assert (endpoint != NULL);
860 struct Sessionqueue *queue =
861 GNUNET_malloc (sizeof (struct Sessionqueue) + sizeof (struct Session));
863 GNUNET_CONTAINER_DLL_insert_tail (endpoint->sessions_head,
864 endpoint->sessions_tail, queue);
866 queue->content = (struct Session *) &queue[1];
867 queue->content->mac = endpoint;
868 memcpy (&(queue->content->target), peer, sizeof (struct GNUNET_PeerIdentity));
869 queue->content->last_activity = GNUNET_TIME_absolute_get ();
870 queue->content->timeout_task =
871 GNUNET_SCHEDULER_add_delayed (SESSION_TIMEOUT, &session_timeout, queue);
874 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
875 "New session %p with endpoint %p: %s\n", queue->content,
876 endpoint, wlan_plugin_address_to_string (NULL,
881 return queue->content;
885 * Get session from address, create if no session exists
887 * @param plugin pointer to the plugin struct
888 * @param addr pointer to the mac address of the peer
889 * @param peer pointer to the peerid
890 * @return returns the session
892 static struct Session *
893 get_session (struct Plugin *plugin, const struct MacAddress *addr,
894 const struct GNUNET_PeerIdentity *peer)
896 struct MacEndpoint *mac;
898 mac = get_macendpoint (plugin, addr, GNUNET_YES);
899 struct Session *session = search_session (plugin, mac, peer);
903 return create_session (plugin, mac, peer);
907 * Queue the session to send data
908 * checks if there is a message pending
909 * checks if this session is not allready in the queue
910 * @param plugin pointer to the plugin
911 * @param session pointer to the session to add
914 queue_session (struct Plugin *plugin, struct Session *session)
916 struct Sessionqueue *queue = plugin->pending_Sessions_head;
918 if (session->pending_message_head != NULL)
920 while (queue != NULL)
922 // content is never NULL
923 GNUNET_assert (queue->content != NULL);
924 // is session already in queue?
925 if (session == queue->content)
933 // Session is not in the queue
935 queue = GNUNET_malloc (sizeof (struct Sessionqueue));
936 queue->content = session;
939 GNUNET_CONTAINER_DLL_insert_tail (plugin->pending_Sessions_head,
940 plugin->pending_Sessions_tail, queue);
941 plugin->pendingsessions++;
947 * Function to schedule the write task, executed after a delay
948 * @param cls pointer to the plugin struct
949 * @param tc GNUNET_SCHEDULER_TaskContext pointer
952 delay_fragment_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
954 struct Plugin *plugin = cls;
956 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
958 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
961 // GNUNET_TIME_UNIT_FOREVER_REL is needed to clean up old msg
962 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
964 plugin->server_write_task =
965 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
966 plugin->server_stdin_handle,
967 &do_transmit, plugin);
972 * Function to calculate the time of the next periodic "hello-beacon"
973 * @param plugin pointer to the plugin struct
977 set_next_beacon_time (struct Plugin *const plugin)
979 //under 10 known peers: once a second
980 if (plugin->mac_count < 10)
982 plugin->beacon_time =
983 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
984 GNUNET_TIME_relative_multiply
985 (GNUNET_TIME_UNIT_SECONDS,
986 HALLO_BEACON_SCALING_FACTOR));
988 //under 30 known peers: every 10 seconds
989 else if (plugin->mac_count < 30)
991 plugin->beacon_time =
992 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
993 GNUNET_TIME_relative_multiply
994 (GNUNET_TIME_UNIT_SECONDS,
995 10 * HALLO_BEACON_SCALING_FACTOR));
997 //over 30 known peers: once a minute
1000 plugin->beacon_time =
1001 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1002 GNUNET_TIME_relative_multiply
1003 (GNUNET_TIME_UNIT_MINUTES,
1004 HALLO_BEACON_SCALING_FACTOR));
1009 * Function to set the timer for the next timeout of the fragment queue
1010 * @param plugin the handle to the plugin struct
1014 set_next_send (struct Plugin *const plugin)
1016 struct GNUNET_TIME_Relative next_send;
1019 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
1021 GNUNET_SCHEDULER_cancel (plugin->server_write_delay_task);
1022 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
1025 //check if some acks are in the queue
1026 if (plugin->ack_send_queue_head != NULL)
1028 next_send = GNUNET_TIME_UNIT_ZERO;
1031 //check if there are some fragments in the queue
1032 else if (plugin->sending_messages_head != NULL)
1034 next_send = GNUNET_TIME_UNIT_ZERO;
1038 next_send = GNUNET_TIME_absolute_get_remaining (plugin->beacon_time);
1042 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1043 "Next packet is send in: %u\n", next_send.rel_value);
1046 if (next_send.rel_value == GNUNET_TIME_UNIT_ZERO.rel_value)
1048 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
1050 plugin->server_write_task =
1051 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1052 plugin->server_stdin_handle,
1053 &do_transmit, plugin);
1058 if (plugin->server_write_delay_task == GNUNET_SCHEDULER_NO_TASK)
1060 plugin->server_write_delay_task =
1061 GNUNET_SCHEDULER_add_delayed (next_send, &delay_fragment_task,
1068 * Function to get the next queued Session, removes the session from the queue
1069 * @param plugin pointer to the plugin struct
1070 * @return pointer to the session found, returns NULL if there is now session in the queue
1072 static struct Session *
1073 get_next_queue_session (struct Plugin *plugin)
1075 struct Session *session;
1076 struct Sessionqueue *sessionqueue;
1077 struct Sessionqueue *sessionqueue_alt;
1078 struct PendingMessage *pm;
1080 sessionqueue = plugin->pending_Sessions_head;
1082 while (sessionqueue != NULL)
1084 session = sessionqueue->content;
1086 GNUNET_assert (session != NULL);
1087 pm = session->pending_message_head;
1092 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1093 "pending message is empty, should not happen. session %p\n",
1098 GNUNET_assert (pm != NULL);
1100 //check for message timeout
1101 if (GNUNET_TIME_absolute_get_remaining (pm->timeout).rel_value > 0)
1103 //check if session has no message in the fragment queue
1104 if ((session->mac->fragment_messages_out_count <
1105 FRAGMENT_QUEUE_MESSAGES_OUT_PER_MACENDPOINT) &&
1106 (session->fragment_messages_out_count <
1107 FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION))
1109 plugin->pendingsessions--;
1110 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1111 plugin->pending_Sessions_tail,
1113 GNUNET_free (sessionqueue);
1119 sessionqueue = sessionqueue->next;
1124 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
1125 session->pending_message_tail, pm);
1127 //call the cont func that it did not work
1128 if (pm->transmit_cont != NULL)
1129 pm->transmit_cont (pm->transmit_cont_cls, &(session->target),
1131 GNUNET_free (pm->msg);
1134 if (session->pending_message_head == NULL)
1136 sessionqueue_alt = sessionqueue;
1137 sessionqueue = sessionqueue->next;
1138 plugin->pendingsessions--;
1139 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1140 plugin->pending_Sessions_tail,
1143 GNUNET_free (sessionqueue_alt);
1152 * frees the space of a message in the fragment queue (send queue)
1153 * @param plugin the plugin struct
1154 * @param fm message to free
1157 free_fragment_message (struct Plugin *plugin, struct FragmentMessage *fm)
1159 struct Session *session = fm->session;
1160 struct MacEndpoint *endpoint = session->mac;
1161 struct FragmentMessage_queue *fmq;
1162 struct FragmentMessage_queue *fmq_next;
1166 fmq = plugin->sending_messages_head;
1169 fmq_next = fmq->next;
1170 if (fmq->content == fm)
1172 GNUNET_CONTAINER_DLL_remove (plugin->sending_messages_head,
1173 plugin->sending_messages_tail, fmq);
1179 (session->mac->fragment_messages_out_count)--;
1180 session->fragment_messages_out_count--;
1181 plugin->pending_Fragment_Messages--;
1182 GNUNET_CONTAINER_DLL_remove (endpoint->sending_messages_head,
1183 endpoint->sending_messages_tail, fm);
1184 GNUNET_FRAGMENT_context_destroy (fm->fragcontext);
1185 if (fm->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1186 GNUNET_SCHEDULER_cancel (fm->timeout_task);
1189 queue_session (plugin, session);
1191 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1192 "Free pending fragment messages %p, session %p\n", fm,
1199 * function to fill the radiotap header
1200 * @param plugin pointer to the plugin struct
1201 * @param endpoint pointer to the endpoint
1202 * @param header pointer to the radiotap header
1203 * @return GNUNET_YES at success
1206 getRadiotapHeader (struct Plugin *plugin, struct MacEndpoint *endpoint,
1207 struct Radiotap_Send *header)
1210 if (endpoint != NULL)
1212 header->rate = endpoint->rate;
1213 header->tx_power = endpoint->tx_power;
1214 header->antenna = endpoint->antenna;
1219 header->tx_power = 0;
1220 header->antenna = 0;
1227 * function to generate the wlan hardware header for one packet
1228 * @param Header address to write the header to
1229 * @param to_mac_addr address of the recipient
1230 * @param plugin pointer to the plugin struct
1231 * @param size size of the whole packet, needed to calculate the time to send the packet
1232 * @return GNUNET_YES if there was no error
1235 getWlanHeader (struct ieee80211_frame *Header,
1236 const struct MacAddress *to_mac_addr, struct Plugin *plugin,
1240 const int rate = 11000000;
1242 Header->i_fc[0] = IEEE80211_FC0_TYPE_DATA;
1243 Header->i_fc[1] = 0x00;
1244 memcpy (&Header->i_addr3, &mac_bssid, sizeof (mac_bssid));
1245 memcpy (&Header->i_addr2, plugin->mac_address.mac,
1246 sizeof (plugin->mac_address));
1247 memcpy (&Header->i_addr1, to_mac_addr, sizeof (struct MacAddress));
1249 tmp16 = (uint16_t *) Header->i_dur;
1250 *tmp16 = (uint16_t) htole16 ((size * 1000000) / rate + 290);
1252 #if DEBUG_wlan_ip_udp_packets_on_air
1257 Header->ip.ip_dst.s_addr = *((uint32_t *) & to_mac_addr->mac[2]);
1258 Header->ip.ip_src.s_addr = *((uint32_t *) & plugin->mac_address.mac[2]);
1259 Header->ip.ip_v = 4;
1260 Header->ip.ip_hl = 5;
1261 Header->ip.ip_p = 17;
1262 Header->ip.ip_ttl = 1;
1263 Header->ip.ip_len = htons (size + 8);
1264 Header->ip.ip_sum = 0;
1265 x = (uint16_t *) & Header->ip;
1266 count = sizeof (struct iph);
1269 /* This is the inner loop */
1270 crc += (unsigned short) *x++;
1273 /* Add left-over byte, if any */
1275 crc += *(unsigned char *) x;
1276 crc = (crc & 0xffff) + (crc >> 16);
1277 Header->ip.ip_sum = htons (~(unsigned short) crc);
1280 Header->udp.len = htons (size - sizeof (struct ieee80211_frame));
1290 * @param msgbuf pointer tor the data
1291 * @param msgbuf_size size of the data
1293 * @return 32bit crc value
1297 getcrc32 (const char *msgbuf, size_t msgbuf_size)
1300 return GNUNET_CRYPTO_crc32_n (msgbuf, msgbuf_size);;
1306 * @param msgbuf pointer tor the data
1307 * @param msgbuf_size size of the data
1309 * @return 16bit crc value
1313 getcrc16 (const char *msgbuf, size_t msgbuf_size)
1315 //TODO calc some crc
1320 * function to add a fragment of a message to send
1321 * @param cls FragmentMessage this message belongs to
1322 * @param hdr pointer to the start of the message
1326 add_message_for_send (void *cls, const struct GNUNET_MessageHeader *hdr)
1329 struct FragmentMessage *fm = cls;
1330 struct FragmentMessage_queue *fmqueue;
1332 GNUNET_assert (cls != NULL);
1333 GNUNET_assert (fm->frag == NULL);
1334 struct MacEndpoint *endpoint = fm->session->mac;
1335 struct Plugin *plugin = endpoint->plugin;
1336 struct GNUNET_MessageHeader *msgheader;
1337 struct GNUNET_MessageHeader *msgheader2;
1340 #if DEBUG_wlan_retransmission
1341 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1342 "Adding fragment of message %p to send, session %p, endpoint %p, type %u\n",
1343 fm, fm->session, endpoint, hdr->type);
1347 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1348 sizeof (struct ieee80211_frame) + ntohs (hdr->size);
1349 fm->frag = GNUNET_malloc (size);
1352 msgheader = (struct GNUNET_MessageHeader *) fm->frag;
1353 msgheader->size = htons (size);
1354 msgheader->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1356 fm->radioHeader = (struct Radiotap_Send *) &msgheader[1];
1357 fm->ieeewlanheader = (struct ieee80211_frame *) &fm->radioHeader[1];
1358 msgheader2 = (struct GNUNET_MessageHeader *) &fm->ieeewlanheader[1];
1359 memcpy (msgheader2, hdr, ntohs (hdr->size));
1361 fmqueue = GNUNET_malloc (sizeof (struct FragmentMessage_queue));
1362 fmqueue->content = fm;
1364 GNUNET_CONTAINER_DLL_insert_tail (plugin->sending_messages_head,
1365 plugin->sending_messages_tail, fmqueue);
1366 set_next_send (plugin);
1370 * function to send a hallo beacon
1371 * @param plugin pointer to the plugin struct
1374 send_hello_beacon (struct Plugin *plugin)
1378 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1379 "Sending hello beacon\n");
1384 uint16_t hallo_size;
1385 struct GNUNET_MessageHeader *msgheader;
1386 struct ieee80211_frame *ieeewlanheader;
1387 struct Radiotap_Send *radioHeader;
1388 struct GNUNET_MessageHeader *msgheader2;
1389 const struct GNUNET_MessageHeader *hello;
1391 hello = plugin->env->get_our_hello ();
1392 hallo_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
1393 GNUNET_assert (sizeof (struct WlanHeader) + hallo_size <= WLAN_MTU);
1395 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1396 sizeof (struct ieee80211_frame) + sizeof (struct GNUNET_MessageHeader) +
1399 msgheader = GNUNET_malloc (size);
1400 msgheader->size = htons (size);
1401 msgheader->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1403 radioHeader = (struct Radiotap_Send *) &msgheader[1];
1404 getRadiotapHeader (plugin, NULL, radioHeader);
1405 ieeewlanheader = (struct ieee80211_frame *) &radioHeader[1];
1406 getWlanHeader (ieeewlanheader, &bc_all_mac, plugin, size);
1408 msgheader2 = (struct GNUNET_MessageHeader *) &ieeewlanheader[1];
1410 htons (GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello) +
1411 sizeof (struct GNUNET_MessageHeader));
1413 msgheader2->type = htons (GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT);
1414 memcpy (&msgheader2[1], hello, hallo_size);
1416 bytes = GNUNET_DISK_file_write (plugin->server_stdin_handle, msgheader, size);
1418 if (bytes == GNUNET_SYSERR)
1420 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1422 ("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1423 errno, strerror (errno));
1426 GNUNET_assert (bytes != GNUNET_SYSERR);
1427 GNUNET_assert (bytes == size);
1428 GNUNET_free (msgheader);
1430 set_next_beacon_time (plugin);
1431 set_next_send (plugin);
1435 * function to add an ack to send it for a received fragment
1436 * @param cls MacEndpoint this ack belongs to
1437 * @param msg_id id of the message
1438 * @param hdr pointer to the hdr where the ack is stored
1443 add_ack_for_send (void *cls, uint32_t msg_id,
1444 const struct GNUNET_MessageHeader *hdr)
1447 struct AckSendQueue *ack;
1449 GNUNET_assert (cls != NULL);
1450 struct MacEndpoint *endpoint = cls;
1451 struct Plugin *plugin = endpoint->plugin;
1452 struct GNUNET_MessageHeader *msgheader;
1453 struct GNUNET_MessageHeader *msgheader2;
1457 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1458 sizeof (struct ieee80211_frame) + ntohs (hdr->size) +
1459 sizeof (struct AckSendQueue);
1461 ack = GNUNET_malloc (size);
1462 ack->message_id = msg_id;
1463 ack->endpoint = endpoint;
1466 sizeof (struct GNUNET_MessageHeader) + sizeof (struct Radiotap_Send) +
1467 sizeof (struct ieee80211_frame) + ntohs (hdr->size);
1469 msgheader = (struct GNUNET_MessageHeader *) &ack[1];
1470 ack->hdr = (struct GNUNET_MessageHeader *) &ack[1];
1471 msgheader->size = htons (size);
1472 msgheader->type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1474 ack->radioHeader = (struct Radiotap_Send *) &msgheader[1];
1475 ack->ieeewlanheader = (struct ieee80211_frame *) &(ack->radioHeader)[1];
1476 msgheader2 = (struct GNUNET_MessageHeader *) &(ack->ieeewlanheader)[1];
1477 memcpy (msgheader2, hdr, ntohs (hdr->size));
1479 GNUNET_CONTAINER_DLL_insert_tail (plugin->ack_send_queue_head,
1480 plugin->ack_send_queue_tail, ack);
1482 #if DEBUG_wlan_retransmission
1483 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1484 "Adding ack with message id %u to send, AckSendQueue %p, endpoint %p\n",
1485 msg_id, ack, endpoint);
1488 set_next_send (plugin);
1492 * Function for the scheduler if a FragmentMessage times out
1493 * @param cls pointer to the FragmentMessage
1494 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
1497 fragmentmessage_timeout (void *cls,
1498 const struct GNUNET_SCHEDULER_TaskContext *tc)
1500 struct FragmentMessage *fm = cls;
1502 GNUNET_assert (fm != NULL);
1503 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1504 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1508 free_fragment_message (fm->session->mac->plugin, fm);
1512 * Function to check if there is some space in the fragment queue
1513 * inserts a message if space is available
1514 * @param plugin the plugin struct
1518 check_fragment_queue (struct Plugin *plugin)
1520 struct Session *session;
1521 struct FragmentMessage *fm;
1522 struct GNUNET_PeerIdentity pid;
1524 struct PendingMessage *pm;
1526 if (plugin->pending_Fragment_Messages < FRAGMENT_QUEUE_SIZE)
1528 session = get_next_queue_session (plugin);
1529 if (session != NULL)
1531 pm = session->pending_message_head;
1532 GNUNET_CONTAINER_DLL_remove (session->pending_message_head,
1533 session->pending_message_tail, pm);
1534 session->mac->fragment_messages_out_count++;
1535 session->fragment_messages_out_count++;
1536 plugin->pending_Fragment_Messages++;
1537 GNUNET_assert (pm != NULL);
1539 fm = GNUNET_malloc (sizeof (struct FragmentMessage));
1540 fm->session = session;
1541 fm->timeout.abs_value = pm->timeout.abs_value;
1544 GNUNET_FRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
1546 GNUNET_TIME_UNIT_SECONDS,
1548 &add_message_for_send, fm);
1550 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
1551 (fm->timeout), fragmentmessage_timeout,
1553 GNUNET_CONTAINER_DLL_insert_tail (session->mac->sending_messages_head,
1554 session->mac->sending_messages_tail,
1557 if (pm->transmit_cont != NULL)
1559 pid = session->target;
1560 pm->transmit_cont (pm->transmit_cont_cls, &pid, GNUNET_OK);
1562 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1563 "called pm->transmit_cont for %p\n", session);
1569 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1570 "no pm->transmit_cont for %p\n", session);
1575 if (session->pending_message_head != NULL)
1578 queue_session (plugin, session);
1584 //check if timeout changed
1585 set_next_send (plugin);
1589 * Function to send an ack, does not free the ack
1590 * @param plugin pointer to the plugin
1591 * @param ack pointer to the ack to send
1594 send_ack (struct Plugin *plugin, struct AckSendQueue *ack)
1600 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1601 "Sending ack for message_id %u for mac endpoint %p, size %u\n",
1602 ack->message_id, ack->endpoint,
1603 ntohs (ack->hdr->size) - sizeof (struct Radiotap_Send));
1606 getRadiotapHeader (plugin, ack->endpoint, ack->radioHeader);
1607 getWlanHeader (ack->ieeewlanheader, &ack->endpoint->addr, plugin,
1608 ntohs (ack->hdr->size));
1611 GNUNET_DISK_file_write (plugin->server_stdin_handle, ack->hdr,
1612 ntohs (ack->hdr->size));
1613 if (bytes == GNUNET_SYSERR)
1615 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1617 ("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1618 errno, strerror (errno));
1621 GNUNET_assert (bytes != GNUNET_SYSERR);
1622 GNUNET_assert (bytes == ntohs (ack->hdr->size));
1623 set_next_send (plugin);
1627 * function to finish a sending if not all could have been writen befor
1628 * @param cls pointer to the Finish_send struct
1629 * @param tc TaskContext
1632 finish_sending (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1634 struct Finish_send *finish = cls;
1635 struct Plugin *plugin;
1638 plugin = finish->plugin;
1639 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1641 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1643 GNUNET_free (finish->msgstart);
1644 GNUNET_free (finish);
1648 GNUNET_DISK_file_write (plugin->server_stdin_handle, finish->msgheader,
1650 GNUNET_assert (bytes != GNUNET_SYSERR);
1652 if (bytes != finish->size)
1654 finish->msgheader = finish->msgheader + bytes;
1655 finish->size = finish->size - bytes;
1656 plugin->server_write_task =
1657 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1658 plugin->server_stdin_handle,
1659 &finish_sending, finish);
1663 GNUNET_free (finish->msgstart);
1664 GNUNET_free (finish);
1665 set_next_send (plugin);
1670 * Function called when wlan helper is ready to get some data
1672 * @param cls closure
1673 * @param tc GNUNET_SCHEDULER_TaskContext
1676 do_transmit (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1678 struct Plugin *plugin = cls;
1680 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1681 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1684 struct Session *session;
1685 struct FragmentMessage *fm;
1686 struct Finish_send *finish;
1687 struct FragmentMessage_queue *fmq;
1688 struct AckSendQueue *ack;
1691 if (plugin->ack_send_queue_head != NULL)
1693 ack = plugin->ack_send_queue_head;
1694 GNUNET_CONTAINER_DLL_remove (plugin->ack_send_queue_head,
1695 plugin->ack_send_queue_tail, ack);
1696 send_ack (plugin, ack);
1701 //test if a "hello-beacon" has to be send
1702 if (GNUNET_TIME_absolute_get_remaining (plugin->beacon_time).rel_value == 0)
1704 send_hello_beacon (plugin);
1708 if (plugin->sending_messages_head != NULL)
1710 fmq = plugin->sending_messages_head;
1712 GNUNET_CONTAINER_DLL_remove (plugin->sending_messages_head,
1713 plugin->sending_messages_tail, fmq);
1716 session = fm->session;
1717 GNUNET_assert (session != NULL);
1720 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1721 "Sending GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT for fragment message %p, size: %u\n",
1725 getRadiotapHeader (plugin, session->mac, fm->radioHeader);
1726 getWlanHeader (fm->ieeewlanheader, &(fm->session->mac->addr), plugin,
1730 GNUNET_DISK_file_write (plugin->server_stdin_handle, fm->frag,
1732 if (bytes == GNUNET_SYSERR)
1734 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1736 ("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1737 errno, strerror (errno));
1740 GNUNET_assert (bytes != GNUNET_SYSERR);
1742 if (bytes != fm->size)
1744 finish = GNUNET_malloc (sizeof (struct Finish_send));
1745 finish->plugin = plugin;
1746 finish->msgheader = fm->frag + bytes;
1747 finish->size = fm->size - bytes;
1748 finish->msgstart = (struct GNUNET_MessageHeader *) fm->frag;
1750 GNUNET_assert (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK);
1752 plugin->server_write_task =
1753 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
1754 plugin->server_stdin_handle,
1755 &finish_sending, finish);
1760 GNUNET_free (fm->frag);
1762 set_next_send (plugin);
1764 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
1768 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
1769 "do_transmit did nothing, should not happen!\n");
1773 * Another peer has suggested an address for this
1774 * peer and transport plugin. Check that this could be a valid
1775 * address. If so, consider adding it to the list
1778 * @param cls closure
1779 * @param addr pointer to the address
1780 * @param addrlen length of addr
1781 * @return GNUNET_OK if this is a plausible address for this peer
1785 wlan_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1787 //struct Plugin *plugin = cls;
1789 /* check if the address is plausible; if so,
1790 * add it to our list! */
1792 GNUNET_assert (cls != NULL);
1793 //FIXME mitm is not checked
1794 //Mac Address has 6 bytes
1797 /* TODO check for bad addresses like multicast, broadcast, etc */
1801 return GNUNET_SYSERR;
1805 * Function that can be used by the transport service to transmit
1806 * a message using the plugin.
1808 * @param cls closure
1809 * @param target who should receive this message
1810 * @param priority how important is the message
1811 * @param msgbuf the message to transmit
1812 * @param msgbuf_size number of bytes in 'msgbuf'
1813 * @param timeout when should we time out
1814 * @param session which session must be used (or NULL for "any")
1815 * @param addr the address to use (can be NULL if the plugin
1816 * is "on its own" (i.e. re-use existing TCP connection))
1817 * @param addrlen length of the address in bytes
1818 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1819 * otherwise the plugin may use other addresses or
1820 * existing connections (if available)
1821 * @param cont continuation to call once the message has
1822 * been transmitted (or if the transport is ready
1823 * for the next transmission call; or if the
1824 * peer disconnected...)
1825 * @param cont_cls closure for cont
1826 * @return number of bytes used (on the physical network, with overheads);
1827 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1828 * and does NOT mean that the message was not transmitted (DV)
1831 wlan_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
1832 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
1833 struct GNUNET_TIME_Relative timeout, struct Session *session,
1834 const void *addr, size_t addrlen, int force_address,
1835 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1837 struct Plugin *plugin = cls;
1838 struct PendingMessage *newmsg;
1839 struct WlanHeader *wlanheader;
1841 //check if msglen > 0
1842 GNUNET_assert (msgbuf_size > 0);
1844 //get session if needed
1845 if (session == NULL)
1847 if (wlan_plugin_address_suggested (plugin, addr, addrlen) == GNUNET_OK)
1849 session = get_session (plugin, addr, target);
1853 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1854 _("Wlan Address len %d is wrong\n"), addrlen);
1861 //queue message in session
1862 //test if there is no other message in the "queue"
1863 //FIXME: to many send requests
1864 if (session->pending_message_head != NULL)
1866 newmsg = session->pending_message_head;
1867 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1868 "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",
1869 GNUNET_TIME_absolute_get_remaining (newmsg->
1871 session->mac->fragment_messages_out_count);
1874 newmsg = GNUNET_malloc (sizeof (struct PendingMessage));
1875 newmsg->msg = GNUNET_malloc (msgbuf_size + sizeof (struct WlanHeader));
1876 wlanheader = newmsg->msg;
1877 //copy msg to buffer, not fragmented / segmented yet, but with message header
1878 wlanheader->header.size = htons (msgbuf_size + sizeof (struct WlanHeader));
1879 wlanheader->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA);
1880 memcpy (&(wlanheader->target), target, sizeof (struct GNUNET_PeerIdentity));
1881 memcpy (&(wlanheader->source), plugin->env->my_identity,
1882 sizeof (struct GNUNET_PeerIdentity));
1883 wlanheader->crc = 0;
1884 memcpy (&wlanheader[1], msgbuf, msgbuf_size);
1887 ((char *) wlanheader, msgbuf_size + sizeof (struct WlanHeader)));
1888 //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);
1889 //hexdump(newmsg->msg, msgbuf_size + sizeof(struct WlanHeader));
1891 newmsg->transmit_cont = cont;
1892 newmsg->transmit_cont_cls = cont_cls;
1893 newmsg->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1895 newmsg->timeout.abs_value = newmsg->timeout.abs_value - 500;
1897 newmsg->message_size = msgbuf_size + sizeof (struct WlanHeader);
1899 GNUNET_CONTAINER_DLL_insert_tail (session->pending_message_head,
1900 session->pending_message_tail, newmsg);
1903 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1904 "New message for %p with size (incl wlan header) %u added\n",
1905 session, newmsg->message_size);
1907 #if DEBUG_wlan_msg_dump
1908 hexdump (msgbuf, GNUNET_MIN (msgbuf_size, 256));
1911 queue_session (plugin, session);
1913 check_fragment_queue (plugin);
1914 //FIXME not the correct size
1920 * function to free a mac endpoint
1921 * @param plugin pointer to the plugin struct
1922 * @param endpoint pointer to the MacEndpoint to free
1925 free_macendpoint (struct Plugin *plugin, struct MacEndpoint *endpoint)
1927 struct Sessionqueue *sessions;
1928 struct Sessionqueue *sessions_next;
1930 GNUNET_assert (endpoint != NULL);
1932 sessions = endpoint->sessions_head;
1933 while (sessions != NULL)
1935 sessions_next = sessions->next;
1936 free_session (plugin, sessions, GNUNET_NO);
1937 sessions = sessions_next;
1940 GNUNET_CONTAINER_DLL_remove (plugin->mac_head, plugin->mac_tail, endpoint);
1941 if (endpoint->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1942 GNUNET_SCHEDULER_cancel (endpoint->timeout_task);
1943 GNUNET_free (endpoint);
1948 * function to free a session
1949 * @param plugin pointer to the plugin
1950 * @param queue pointer to the sessionqueue element to free
1951 * @param free_macendpoint if GNUNET_YES and mac endpoint would be empty, free mac endpoint
1954 free_session (struct Plugin *plugin, struct Sessionqueue *queue,
1955 int do_free_macendpoint)
1957 struct Sessionqueue *pendingsession;
1958 struct Sessionqueue *pendingsession_tmp;
1959 struct PendingMessage *pm;
1960 struct MacEndpoint *endpoint;
1961 struct FragmentMessage *fm;
1962 struct FragmentMessage *fmnext;
1965 GNUNET_assert (queue != NULL);
1966 GNUNET_assert (queue->content != NULL);
1969 //is this session pending for send
1970 pendingsession = plugin->pending_Sessions_head;
1971 while (pendingsession != NULL)
1973 pendingsession_tmp = pendingsession->next;
1974 if (pendingsession->content == queue->content)
1976 plugin->pendingsessions--;
1977 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1978 plugin->pending_Sessions_tail,
1980 GNUNET_free (pendingsession);
1982 GNUNET_assert (check == 0);
1985 pendingsession = pendingsession_tmp;
1988 endpoint = queue->content->mac;
1989 fm = endpoint->sending_messages_head;
1993 if (fm->session == queue->content)
1995 free_fragment_message (plugin, fm);
2000 // remove PendingMessage
2001 pm = queue->content->pending_message_head;
2004 GNUNET_CONTAINER_DLL_remove (queue->content->pending_message_head,
2005 queue->content->pending_message_tail, pm);
2006 GNUNET_free (pm->msg);
2008 pm = queue->content->pending_message_head;
2011 GNUNET_CONTAINER_DLL_remove (endpoint->sessions_head, endpoint->sessions_tail,
2014 if (endpoint->sessions_head == NULL && do_free_macendpoint == GNUNET_YES)
2016 free_macendpoint (plugin, endpoint);
2019 if (queue->content->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2020 GNUNET_SCHEDULER_cancel (queue->content->timeout_task);
2021 GNUNET_free (queue);
2022 check_fragment_queue (plugin);
2026 * Function that can be used to force the plugin to disconnect
2027 * from the given peer and cancel all previous transmissions
2028 * (and their continuation).
2030 * @param cls closure
2031 * @param target peer from which to disconnect
2034 wlan_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
2036 struct Plugin *plugin = cls;
2037 struct Sessionqueue *queue;
2038 struct Sessionqueue *queue_next;
2039 struct MacEndpoint *endpoint = plugin->mac_head;
2040 struct MacEndpoint *endpoint_next;
2042 // just look at all the session for the needed one
2043 while (endpoint != NULL)
2045 queue = endpoint->sessions_head;
2046 endpoint_next = endpoint->next;
2047 while (queue != NULL)
2049 // content is never NULL
2050 GNUNET_assert (queue->content != NULL);
2051 queue_next = queue->next;
2053 (target, &(queue->content->target),
2054 sizeof (struct GNUNET_PeerIdentity)) == 0)
2056 free_session (plugin, queue, GNUNET_YES);
2061 endpoint = endpoint_next;
2066 * Convert the transports address to a nice, human-readable
2069 * @param cls closure
2070 * @param type name of the transport that generated the address
2071 * @param addr one of the addresses of the host, NULL for the last address
2072 * the specific address format depends on the transport
2073 * @param addrlen length of the address
2074 * @param numeric should (IP) addresses be displayed in numeric form?
2075 * @param timeout after how long should we give up?
2076 * @param asc function to call on each string
2077 * @param asc_cls closure for asc
2080 wlan_plugin_address_pretty_printer (void *cls, const char *type,
2081 const void *addr, size_t addrlen,
2083 struct GNUNET_TIME_Relative timeout,
2084 GNUNET_TRANSPORT_AddressStringCallback asc,
2088 const unsigned char *input;
2090 //GNUNET_assert(cls !=NULL);
2093 /* invalid address (MAC addresses have 6 bytes) */ GNUNET_break (0);
2094 asc (asc_cls, NULL);
2097 input = (const unsigned char *) addr;
2098 GNUNET_snprintf (ret, sizeof (ret),
2099 "%s Mac-Address %.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
2100 PROTOCOL_PREFIX, input[0], input[1], input[2], input[3],
2101 input[4], input[5]);
2106 * handels the data after all fragments are put together
2107 * @param cls macendpoint this messages belongs to
2108 * @param hdr pointer to the data
2111 wlan_data_message_handler (void *cls, const struct GNUNET_MessageHeader *hdr)
2113 struct MacEndpoint *endpoint = (struct MacEndpoint *) cls;
2114 struct Plugin *plugin = endpoint->plugin;
2115 struct WlanHeader *wlanheader;
2116 struct Session *session;
2118 //const char * tempmsg;
2119 const struct GNUNET_MessageHeader *temp_hdr;
2120 struct GNUNET_PeerIdentity tmpsource;
2123 if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_DATA)
2127 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2128 "Func wlan_data_message_handler got GNUNET_MESSAGE_TYPE_WLAN_DATA size: %u\n",
2132 if (ntohs (hdr->size) <
2133 sizeof (struct WlanHeader) + sizeof (struct GNUNET_MessageHeader))
2135 //packet not big enought
2139 wlanheader = (struct WlanHeader *) hdr;
2141 session = search_session (plugin, endpoint, &wlanheader->source);
2143 //tempmsg = (char*) &wlanheader[1];
2144 temp_hdr = (const struct GNUNET_MessageHeader *) &wlanheader[1];
2145 crc = ntohl (wlanheader->crc);
2146 wlanheader->crc = 0;
2147 if (getcrc32 ((char *) wlanheader, ntohs (wlanheader->header.size)) != crc)
2149 //wrong crc, dispose message
2150 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2151 "Wlan message header crc was wrong: %u != %u\n",
2152 getcrc32 ((char *) wlanheader,
2153 ntohs (wlanheader->header.size)), crc);
2154 hexdump ((void *) hdr, ntohs (hdr->size));
2158 //if not in session list
2159 if (session == NULL)
2162 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2163 "WLAN client not in session list: packet size = %u, inner size = %u, header size = %u\n",
2164 ntohs (wlanheader->header.size), ntohs (temp_hdr->size),
2165 sizeof (struct WlanHeader));
2167 //try if it is a hello message
2168 if (ntohs (wlanheader->header.size) >=
2169 ntohs (temp_hdr->size) + sizeof (struct WlanHeader))
2171 if (ntohs (temp_hdr->type) == GNUNET_MESSAGE_TYPE_HELLO)
2173 if (GNUNET_HELLO_get_id
2174 ((const struct GNUNET_HELLO_Message *) temp_hdr,
2175 &tmpsource) == GNUNET_OK)
2177 session = create_session (plugin, endpoint, &tmpsource);
2181 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2182 "WLAN client not in session list and hello message is not okay\n");
2189 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2190 "WLAN client not in session list and not a hello message\n");
2196 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2197 "WLAN client not in session list and message size in does not fit\npacket size = %u, inner size = %u, header size = %u\n",
2198 ntohs (wlanheader->header.size),
2199 ntohs (temp_hdr->size), sizeof (struct WlanHeader));
2204 //"receive" the message
2207 (&wlanheader->source, &session->target,
2208 sizeof (struct GNUNET_PeerIdentity)) != 0)
2212 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2213 "WLAN peer source id doesn't match packet peer source id: session %p\n",
2220 (&wlanheader->target, plugin->env->my_identity,
2221 sizeof (struct GNUNET_PeerIdentity)) != 0)
2225 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2226 "WLAN peer target id doesn't match our peer id: session %p\n",
2232 GNUNET_SERVER_mst_receive (plugin->data_tokenizer, session,
2233 (const char *) temp_hdr,
2234 ntohs (hdr->size) - sizeof (struct WlanHeader),
2235 GNUNET_YES, GNUNET_NO);
2241 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2242 "wlan_data_message_handler got wrong message type: %u\n",
2249 * function to process the a message, give it to the higher layer
2250 * @param cls pointer to the plugin
2251 * @param client pointer to the session this message belongs to
2252 * @param hdr start of the message
2254 //TODO ATS informations
2256 process_data (void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
2259 GNUNET_assert (client != NULL);
2260 GNUNET_assert (cls != NULL);
2261 struct Session *session = (struct Session *) client;
2262 struct Plugin *plugin = (struct Plugin *) cls;
2264 struct GNUNET_TRANSPORT_ATS_Information distance[2];
2266 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2267 distance[0].value = htonl (1);
2268 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2269 distance[1].value = htonl (0);
2272 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2273 "Calling plugin->env->receive for session %p; %s; size: %u\n",
2274 session, wlan_plugin_address_to_string (NULL,
2280 plugin->env->receive (plugin->env->cls, &(session->target), hdr,
2281 (const struct GNUNET_TRANSPORT_ATS_Information *)
2282 &distance, 2, session,
2283 (const char *) &session->mac->addr,
2284 sizeof (session->mac->addr));
2288 * Function used for to process the data received from the wlan interface
2290 * @param cls the plugin handle
2291 * @param session_light pointer to the struct holding known informations
2292 * @param hdr hdr of the GNUNET_MessageHeader
2293 * @param rxinfo pointer to the radiotap informations got with this packet
2296 wlan_data_helper (void *cls, struct Session_light *session_light,
2297 const struct GNUNET_MessageHeader *hdr,
2298 const struct Radiotap_rx *rxinfo)
2300 struct Plugin *plugin = cls;
2301 struct FragmentMessage *fm;
2302 struct FragmentMessage *fm2;
2305 if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT)
2308 //TODO better DOS protection, error handling
2309 //TODO test first than create session
2310 GNUNET_assert (session_light != NULL);
2313 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2314 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT size: %u; %s\n",
2315 ntohs (hdr->size), wlan_plugin_address_to_string (NULL,
2316 session_light->addr.
2320 if (session_light->macendpoint == NULL)
2322 session_light->macendpoint =
2323 get_macendpoint (plugin, &session_light->addr, GNUNET_NO);
2325 GNUNET_assert (GNUNET_HELLO_get_id
2326 ((const struct GNUNET_HELLO_Message *) &hdr[1],
2327 &(session_light->session->target)) != GNUNET_SYSERR);
2333 else if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_FRAGMENT)
2336 GNUNET_assert (session_light != NULL);
2337 if (session_light->macendpoint == NULL)
2339 session_light->macendpoint =
2340 get_macendpoint (plugin, &session_light->addr, GNUNET_YES);
2344 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2345 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_FRAGMENT with size: %u; mac endpoint %p: %s\n",
2346 ntohs (hdr->size), session_light->macendpoint,
2347 wlan_plugin_address_to_string (NULL,
2348 session_light->addr.mac,
2353 GNUNET_DEFRAGMENT_process_fragment (session_light->macendpoint->defrag,
2356 if (ret == GNUNET_NO)
2358 session_light->macendpoint->dups++;
2360 else if (ret == GNUNET_OK)
2362 session_light->macendpoint->fragc++;
2364 set_next_send (plugin);
2370 else if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_FRAGMENT_ACK)
2372 GNUNET_assert (session_light != NULL);
2373 if (session_light->macendpoint == NULL)
2375 session_light->macendpoint =
2376 get_macendpoint (plugin, &session_light->addr, GNUNET_NO);
2379 if (session_light->macendpoint == NULL)
2382 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2383 "Macendpoint does not exist for this GNUNET_MESSAGE_TYPE_FRAGMENT_ACK size: %u; %s\n",
2384 ntohs (hdr->size), wlan_plugin_address_to_string (NULL,
2385 session_light->addr.mac,
2392 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2393 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_FRAGMENT_ACK size: %u; mac endpoint: %p; %s\n",
2394 ntohs (hdr->size), session_light->macendpoint,
2395 wlan_plugin_address_to_string (NULL,
2396 session_light->addr.mac,
2399 fm = session_light->macendpoint->sending_messages_head;
2403 int ret = GNUNET_FRAGMENT_process_ack (fm->fragcontext, hdr);
2405 if (ret == GNUNET_OK)
2407 #if DEBUG_wlan_retransmission
2408 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2409 "Got last ack, finished fragment message %p\n", fm);
2411 session_light->macendpoint->acks++;
2412 fm->session->last_activity = GNUNET_TIME_absolute_get ();
2413 session_light->macendpoint->last_activity = fm->session->last_activity;
2414 free_fragment_message (plugin, fm);
2415 check_fragment_queue (plugin);
2418 if (ret == GNUNET_NO)
2420 #if DEBUG_wlan_retransmission
2421 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2422 "Got ack for: %p\n", fm);
2424 session_light->macendpoint->acks++;
2427 if (ret == GNUNET_SYSERR)
2435 #if DEBUG_wlan_retransmission
2436 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2437 "WLAN fragment not in fragment list\n");
2445 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2446 "WLAN packet inside the WLAN helper packet has not the right type: %u size: %u\n",
2447 ntohs (hdr->type), ntohs (hdr->size));
2453 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2454 "Helper finished\n");
2460 * Function to print mac addresses nice *
2461 * @param pointer to 6 byte with the mac address
2462 * @return pointer to the chars which hold the print out
2465 macprinter (const u_int8_t * mac)
2467 static char macstr[20];
2469 GNUNET_snprintf (macstr, sizeof (macstr), "%X:%X:%X:%X:%X:%X", mac[0], mac[1],
2470 mac[2], mac[3], mac[4], mac[5]);
2475 * Function for the scheduler if a mac endpoint times out
2476 * @param cls pointer to the MacEndpoint
2477 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
2480 macendpoint_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2482 struct MacEndpoint *endpoint = cls;
2484 GNUNET_assert (endpoint != NULL);
2485 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2486 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2490 if (GNUNET_TIME_absolute_get_remaining
2491 (GNUNET_TIME_absolute_add
2492 (endpoint->last_activity, MACENDPOINT_TIMEOUT)).rel_value == 0)
2494 free_macendpoint (endpoint->plugin, endpoint);
2498 endpoint->timeout_task =
2499 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
2505 * function to create an macendpoint
2506 * @param plugin pointer to the plugin struct
2507 * @param addr pointer to the macaddress
2508 * @return returns a macendpoint
2510 static struct MacEndpoint *
2511 create_macendpoint (struct Plugin *plugin, const struct MacAddress *addr)
2513 struct MacEndpoint *newend = GNUNET_malloc (sizeof (struct MacEndpoint));
2515 newend->addr = *addr;
2516 newend->plugin = plugin;
2517 newend->addr = *addr;
2518 newend->fragment_messages_out_count = 0;
2520 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, WLAN_MTU,
2521 MESSAGES_IN_DEFRAG_QUEUE_PER_MAC,
2522 newend, &wlan_data_message_handler,
2524 newend->last_activity = GNUNET_TIME_absolute_get ();
2525 newend->timeout_task =
2526 GNUNET_SCHEDULER_add_delayed (MACENDPOINT_TIMEOUT, &macendpoint_timeout,
2529 plugin->mac_count++;
2530 GNUNET_CONTAINER_DLL_insert_tail (plugin->mac_head, plugin->mac_tail, newend);
2532 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2533 "New Mac Endpoint %p: %s\n", newend,
2534 wlan_plugin_address_to_string (NULL, newend->addr.mac, 6));
2540 * Function used for to process the data from the suid process
2542 * @param cls the plugin handle
2543 * @param client client that send the data (not used)
2544 * @param hdr header of the GNUNET_MessageHeader
2547 wlan_process_helper (void *cls, void *client,
2548 const struct GNUNET_MessageHeader *hdr)
2550 struct Plugin *plugin = cls;
2551 struct ieee80211_frame *wlanIeeeHeader = NULL;
2552 struct Session_light *session_light = NULL;
2553 struct Radiotap_rx *rxinfo;
2554 const struct GNUNET_MessageHeader *temp_hdr = NULL;
2559 switch (ntohs (hdr->type))
2561 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA:
2563 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2564 "Func wlan_process_helper got GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA size: %u\n",
2568 //call wlan_process_helper with the message inside, later with wlan: analyze signal
2569 if (ntohs (hdr->size) <
2570 sizeof (struct ieee80211_frame) + sizeof (struct GNUNET_MessageHeader) +
2571 sizeof (struct Radiotap_rx))
2574 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2575 "Size of packet is too small; size: %u min size: %u\n",
2577 sizeof (struct ieee80211_frame) +
2578 sizeof (struct GNUNET_MessageHeader));
2581 /* FIXME: restart SUID process */
2584 rxinfo = (struct Radiotap_rx *) &hdr[1];
2585 wlanIeeeHeader = (struct ieee80211_frame *) &rxinfo[1];
2587 //process only if it is an broadcast or for this computer both with the gnunet bssid
2591 (&(wlanIeeeHeader->i_addr3), &mac_bssid,
2592 sizeof (struct MacAddress)) == 0)
2594 //check for broadcast or mac
2596 (&(wlanIeeeHeader->i_addr1), &bc_all_mac,
2597 sizeof (struct MacAddress) == 0) ||
2598 memcmp (&(wlanIeeeHeader->i_addr1), &(plugin->mac_address),
2599 sizeof (struct MacAddress)) == 0)
2602 // process the inner data
2606 ntohs (hdr->size) - sizeof (struct ieee80211_frame) -
2607 sizeof (struct GNUNET_MessageHeader) - sizeof (struct Radiotap_rx);
2609 session_light = GNUNET_malloc (sizeof (struct Session_light));
2610 memcpy (&session_light->addr, &(wlanIeeeHeader->i_addr2),
2611 sizeof (struct MacAddress));
2612 //session_light->session = search_session(plugin,session_light->addr);
2615 while (pos < datasize)
2617 temp_hdr = (struct GNUNET_MessageHeader *) &wlanIeeeHeader[1] + pos;
2619 wlan_data_helper (plugin, session_light, temp_hdr, rxinfo);
2620 pos += ntohs (temp_hdr->size);
2625 GNUNET_free (session_light);
2630 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2631 "Func wlan_process_helper got wrong MAC: %s\n",
2632 macprinter (wlanIeeeHeader->i_addr1));
2639 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2640 "Func wlan_process_helper got wrong BSSID: %s\n",
2641 macprinter (wlanIeeeHeader->i_addr2));
2645 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
2646 //TODO more control messages
2647 if (ntohs (hdr->size) != sizeof (struct Wlan_Helper_Control_Message))
2650 /* FIXME: restart SUID process */
2653 memcpy (&plugin->mac_address, &hdr[1], sizeof (struct MacAddress));
2655 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2656 "Received WLAN_HELPER_CONTROL message with transport of address %s\n",
2657 wlan_plugin_address_to_string (cls, &plugin->mac_address,
2661 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2662 &plugin->mac_address,
2663 sizeof (struct MacAddress));
2667 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2668 "Func wlan_process_helper got unknown message with number %u, size %u\n",
2669 ntohs (hdr->type), ntohs (hdr->size));
2672 #if DEBUG_wlan_msg_dump
2673 hexdump (hdr, GNUNET_MIN (ntohs (hdr->size), 256));
2681 * We have been notified that wlan-helper has written something to stdout.
2682 * Handle the output, then reschedule this function to be called again once
2683 * more is available.
2685 * @param cls the plugin handle
2686 * @param tc the scheduling context
2689 wlan_plugin_helper_read (void *cls,
2690 const struct GNUNET_SCHEDULER_TaskContext *tc)
2692 struct Plugin *plugin = cls;
2694 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2696 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2699 char mybuf[WLAN_MTU + sizeof (struct GNUNET_MessageHeader)];
2703 GNUNET_DISK_file_read (plugin->server_stdout_handle, mybuf,
2708 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2710 ("Finished reading from wlan-helper stdout with code: %d\n"),
2715 GNUNET_SERVER_mst_receive (plugin->suid_tokenizer, NULL, mybuf, bytes,
2716 GNUNET_NO, GNUNET_NO);
2718 GNUNET_assert (plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2719 plugin->server_read_task =
2720 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2721 plugin->server_stdout_handle,
2722 &wlan_plugin_helper_read, plugin);
2726 * Start the gnunet-wlan-helper process.
2728 * @param plugin the transport plugin
2729 * @param testmode should we use the dummy driver for testing?
2730 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
2733 wlan_transport_start_wlan_helper (struct Plugin *plugin, int testmode)
2735 const char *filenamehw = "gnunet-transport-wlan-helper";
2736 const char *filenameloopback = "gnunet-transport-wlan-helper-dummy";
2738 plugin->server_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
2739 if (plugin->server_stdout == NULL)
2740 return GNUNET_SYSERR;
2742 plugin->server_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);
2743 if (plugin->server_stdin == NULL)
2744 return GNUNET_SYSERR;
2746 /* Start the server process */
2752 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2753 "Starting gnunet-wlan-helper process cmd: %s %s %i\n",
2754 filenamehw, plugin->interface, testmode);
2757 if (GNUNET_OS_check_helper_binary (filenamehw) == GNUNET_YES)
2759 plugin->server_proc =
2760 GNUNET_OS_start_process (plugin->server_stdin, plugin->server_stdout,
2761 filenamehw, filenamehw, plugin->interface,
2764 else if (GNUNET_OS_check_helper_binary (filenamehw) == GNUNET_NO)
2766 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
2767 "gnunet-transport-wlan-helper is not suid, please change it or look at the doku\n");
2772 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
2773 "gnunet-transport-wlan-helper not found, please look if it exists and is the $PATH variable!\n");
2778 else if (testmode == 1)
2782 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2783 "Starting gnunet-wlan-helper loopback 1 process cmd: %s %s %i\n",
2784 filenameloopback, plugin->interface, testmode);
2787 if (GNUNET_OS_check_helper_binary (filenameloopback) != GNUNET_SYSERR)
2789 plugin->server_proc =
2790 GNUNET_OS_start_process (plugin->server_stdin, plugin->server_stdout,
2791 filenameloopback, filenameloopback, "1",
2796 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
2797 "gnunet-transport-wlan-helper-dummy not found, please look if it exists and is the $PATH variable!\n");
2801 else if (testmode == 2)
2804 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2805 "Starting gnunet-wlan-helper loopback 2 process cmd: %s %s %i\n",
2806 filenameloopback, plugin->interface, testmode);
2808 if (GNUNET_OS_check_helper_binary (filenameloopback) != GNUNET_SYSERR)
2810 plugin->server_proc =
2811 GNUNET_OS_start_process (plugin->server_stdin, plugin->server_stdout,
2812 filenameloopback, filenameloopback, "2",
2817 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
2818 "gnunet-transport-wlan-helper-dummy not found, please look if it exists and is in the $PATH variable!\n");
2822 if (plugin->server_proc == NULL)
2825 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2826 "Failed to start gnunet-wlan-helper process\n");
2828 return GNUNET_SYSERR;
2831 /* Close the write end of the read pipe */
2832 GNUNET_DISK_pipe_close_end (plugin->server_stdout,
2833 GNUNET_DISK_PIPE_END_WRITE);
2835 /* Close the read end of the write pipe */
2836 GNUNET_DISK_pipe_close_end (plugin->server_stdin, GNUNET_DISK_PIPE_END_READ);
2838 plugin->server_stdout_handle =
2839 GNUNET_DISK_pipe_handle (plugin->server_stdout,
2840 GNUNET_DISK_PIPE_END_READ);
2841 plugin->server_stdin_handle =
2842 GNUNET_DISK_pipe_handle (plugin->server_stdin,
2843 GNUNET_DISK_PIPE_END_WRITE);
2845 GNUNET_assert (plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2848 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2849 "Adding server_read_task for the wlan-helper\n");
2852 plugin->server_read_task =
2853 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2854 plugin->server_stdout_handle,
2855 &wlan_plugin_helper_read, plugin);
2861 * Exit point from the plugin.
2862 * @param cls pointer to the api struct
2867 libgnunet_plugin_transport_wlan_done (void *cls)
2869 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2870 struct Plugin *plugin = api->cls;
2871 struct MacEndpoint *endpoint = plugin->mac_head;
2872 struct MacEndpoint *endpoint_next;
2875 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2876 "libgnunet_plugin_transport_wlan_done started\n");
2879 GNUNET_DISK_pipe_close (plugin->server_stdout);
2880 GNUNET_DISK_pipe_close (plugin->server_stdin);
2881 GNUNET_OS_process_kill (plugin->server_proc, 9);
2882 GNUNET_OS_process_close (plugin->server_proc);
2884 GNUNET_assert (cls != NULL);
2886 while (endpoint != NULL)
2888 endpoint_next = endpoint->next;
2889 free_macendpoint (plugin, endpoint);
2890 endpoint = endpoint_next;
2893 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
2895 GNUNET_SCHEDULER_cancel (plugin->server_write_delay_task);
2896 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
2898 if (plugin->server_write_task != GNUNET_SCHEDULER_NO_TASK)
2900 GNUNET_SCHEDULER_cancel (plugin->server_write_task);
2901 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
2903 if (plugin->server_read_task != GNUNET_SCHEDULER_NO_TASK)
2905 GNUNET_SCHEDULER_cancel (plugin->server_read_task);
2906 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2909 if (plugin->suid_tokenizer != NULL)
2910 GNUNET_SERVER_mst_destroy (plugin->suid_tokenizer);
2912 if (plugin->data_tokenizer != NULL)
2913 GNUNET_SERVER_mst_destroy (plugin->data_tokenizer);
2915 GNUNET_free_non_null (plugin->interface);
2916 GNUNET_free (plugin);
2922 * Entry point for the plugin.
2924 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2925 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2928 libgnunet_plugin_transport_wlan_init (void *cls)
2930 //struct GNUNET_SERVICE_Context *service;
2931 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2932 struct GNUNET_TRANSPORT_PluginFunctions *api;
2933 struct Plugin *plugin;
2934 static unsigned long long testmode = 0;
2936 GNUNET_assert (cls != NULL);
2938 plugin = GNUNET_malloc (sizeof (struct Plugin));
2940 plugin->pendingsessions = 0;
2941 plugin->mac_count = 0;
2942 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
2943 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2944 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
2945 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
2946 GNUNET_BANDWIDTH_value_init (100 * 1024 *
2949 plugin->suid_tokenizer =
2950 GNUNET_SERVER_mst_create (&wlan_process_helper, plugin);
2952 plugin->data_tokenizer = GNUNET_SERVER_mst_create (&process_data, plugin);
2954 //plugin->sessions = GNUNET_malloc (sizeof (struct Sessionqueue));
2955 //plugin->pending_Sessions_head = GNUNET_malloc (sizeof (struct Sessionqueue));
2957 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2959 api->send = &wlan_plugin_send;
2960 api->disconnect = &wlan_plugin_disconnect;
2961 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2962 api->check_address = &wlan_plugin_address_suggested;
2963 api->address_to_string = &wlan_plugin_address_to_string;
2966 if (GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "TESTMODE"))
2968 if (GNUNET_SYSERR ==
2969 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-wlan",
2970 "TESTMODE", &testmode))
2971 testmode = 0; //default value
2974 if (GNUNET_CONFIGURATION_have_value (env->cfg, "transport-wlan", "INTERFACE"))
2976 if (GNUNET_CONFIGURATION_get_value_string
2977 (env->cfg, "transport-wlan", "INTERFACE",
2978 &(plugin->interface)) != GNUNET_YES)
2980 libgnunet_plugin_transport_wlan_done (api);
2986 set_next_beacon_time (plugin);
2988 wlan_transport_start_wlan_helper (plugin, testmode);
2991 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2992 "wlan init finished\n");
2998 /* end of plugin_transport_wlan.c */