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
28 #include "gnunet_hello_lib.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_statistics_service.h"
32 #include "gnunet_transport_service.h"
33 #include "gnunet_transport_plugin.h"
34 #include "plugin_transport_wlan.h"
35 #include "gnunet_common.h"
36 #include "gnunet_crypto_lib.h"
37 #include "gnunet_fragmentation_lib.h"
38 //#include "wlan/ieee80211.h"
39 //#include <netinet/ip.h>
43 #define PROTOCOL_PREFIX "wlan"
45 #define PLUGIN_LOG_NAME "wlan-plugin"
53 * time out of a session
55 #define SESSION_TIMEOUT GNUNET_TIME_UNIT_MINUTES
58 * time out of a mac endpoint
60 #define MACENDPOINT_TIMEOUT GNUNET_TIME_UNIT_MINUTES
63 * scaling factor for hello beacon
65 #define HALLO_BEACON_SCALING_FACTOR 900
68 * max size of fragment queue
70 #define FRAGMENT_QUEUE_SIZE 10
72 * max messages in fragment queue per session/client
74 #define FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION 1
77 * max messages in fragment queue per MAC
79 #define FRAGMENT_QUEUE_MESSAGES_OUT_PER_MACENDPOINT 1
82 * max messages in in queue
84 #define MESSAGES_IN_QUEUE_SIZE 10
86 * max messages in in queue per session/client
88 #define MESSAGES_IN_DEFRAG_QUEUE_PER_MAC 1
94 #define DEBUG_wlan GNUNET_NO
95 #define DEBUG_wlan_retransmission GNUNET_NO
96 #define DEBUG_wlan_ip_udp_packets_on_air GNUNET_NO
99 #define IEEE80211_ADDR_LEN 6 /* size of 802.11 address */
101 #define IEEE80211_FC0_VERSION_MASK 0x03
102 #define IEEE80211_FC0_VERSION_SHIFT 0
103 #define IEEE80211_FC0_VERSION_0 0x00
104 #define IEEE80211_FC0_TYPE_MASK 0x0c
105 #define IEEE80211_FC0_TYPE_SHIFT 2
106 #define IEEE80211_FC0_TYPE_MGT 0x00
107 #define IEEE80211_FC0_TYPE_CTL 0x04
108 #define IEEE80211_FC0_TYPE_DATA 0x08
111 * Structure of an internet header, naked of options.
115 #if __BYTE_ORDER == __LITTLE_ENDIAN
116 unsigned int ip_hl :4; /* header length */
117 unsigned int ip_v :4; /* version */
119 #if __BYTE_ORDER == __BIG_ENDIAN
120 unsigned int ip_v:4; /* version */
121 unsigned int ip_hl:4; /* header length */
123 u_int8_t ip_tos; /* type of service */
124 u_short ip_len; /* total length */
125 u_short ip_id; /* identification */
126 u_short ip_off; /* fragment offset field */
127 #define IP_RF 0x8000 /* reserved fragment flag */
128 #define IP_DF 0x4000 /* dont fragment flag */
129 #define IP_MF 0x2000 /* more fragments flag */
130 #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
131 u_int8_t ip_ttl; /* time to live */
132 u_int8_t ip_p; /* protocol */
133 u_short ip_sum; /* checksum */
134 struct in_addr ip_src, ip_dst; /* source and dest address */
146 * generic definitions for IEEE 802.11 frames
148 struct ieee80211_frame
152 u_int8_t i_addr1[IEEE80211_ADDR_LEN];
153 u_int8_t i_addr2[IEEE80211_ADDR_LEN];
154 u_int8_t i_addr3[IEEE80211_ADDR_LEN];
156 #if DEBUG_wlan_ip_udp_packets_on_air
164 * Encapsulation of all of the state of the plugin.
171 struct GNUNET_TRANSPORT_PluginEnvironment *env;
174 * List of open connections. head
176 struct MacEndpoint * mac_head;
179 * List of open connections. tail
181 struct MacEndpoint * mac_tail;
184 * Number of connections
186 unsigned int mac_count;
189 * encapsulation of data from the local wlan helper program
191 struct GNUNET_SERVER_MessageStreamTokenizer * suid_tokenizer;
194 * encapsulation of packets received from the wlan helper
196 struct GNUNET_SERVER_MessageStreamTokenizer * data_tokenizer;
199 * stdout pipe handle for the gnunet-wlan-helper process
201 struct GNUNET_DISK_PipeHandle *server_stdout;
204 * stdout file handle for the gnunet-wlan-helper process
206 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
209 * stdin pipe handle for the gnunet-wlan-helper process
211 struct GNUNET_DISK_PipeHandle *server_stdin;
214 * stdin file handle for the gnunet-wlan-helper process
216 const struct GNUNET_DISK_FileHandle *server_stdin_handle;
219 * ID of the gnunet-wlan-server std read task
221 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
224 * ID of the gnunet-wlan-server std read task
226 GNUNET_SCHEDULER_TaskIdentifier server_write_task;
229 * ID of the delay task for writing
231 GNUNET_SCHEDULER_TaskIdentifier server_write_delay_task;
234 * The process id of the wlan process
236 struct GNUNET_OS_Process *server_proc;
239 * The interface of the wlan card given to us by the user.
244 * The mac_address of the wlan card given to us by the helper.
246 struct MacAddress mac_address;
249 * Sessions currently pending for transmission
252 struct Sessionqueue * pending_Sessions_head;
255 * Sessions currently pending for transmission
256 * to a peer (tail), if any.
258 struct Sessionqueue * pending_Sessions_tail;
261 * number of pending sessions
263 unsigned int pendingsessions;
266 * Messages in the sending queues
268 int pending_Fragment_Messages;
271 * messages ready for send, head
273 struct FragmentMessage_queue * sending_messages_head;
275 * messages ready for send, tail
277 struct FragmentMessage_queue * sending_messages_tail;
279 * time of the next "hello-beacon"
281 struct GNUNET_TIME_Absolute beacon_time;
284 * queue to send acks for received fragments (head)
286 struct AckSendQueue * ack_send_queue_head;
289 * queue to send acks for received fragments (tail)
291 struct AckSendQueue * ack_send_queue_tail;
294 * Tracker for bandwidth limit
296 struct GNUNET_BANDWIDTH_Tracker tracker;
300 * Struct to store data if file write did not accept the whole packet
304 struct Plugin * plugin;
306 struct GNUNET_MessageHeader * msgstart;
311 * Queue of sessions, for the general session queue and the pending session queue
316 struct Sessionqueue * next;
317 struct Sessionqueue * prev;
318 struct Session * content;
322 * Queue of fragmented messages, for the sending queue of the plugin
325 struct FragmentMessage_queue
327 struct FragmentMessage_queue * next;
328 struct FragmentMessage_queue * prev;
329 struct FragmentMessage * content;
333 * Queue for the fragments received
336 struct Receive_Fragment_Queue
338 struct Receive_Fragment_Queue * next;
339 struct Receive_Fragment_Queue * prev;
343 struct Radiotap_rx rxinfo;
347 struct MacEndpoint_id_fragment_triple
349 struct MacEndpoint * endpoint;
351 struct FragmentMessage * fm;
355 struct Plugin_Session_pair
357 struct Plugin * plugin;
358 struct Session * session;
362 * Information kept for each message that is yet to
365 struct PendingMessage
370 struct PendingMessage * next;
374 struct PendingMessage * prev;
377 * The pending message
379 struct WlanHeader *msg;
382 * Size of the message
387 * Continuation function to call once the message
388 * has been sent. Can be NULL if there is no
389 * continuation to call.
391 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
394 * Cls for transmit_cont
396 void * transmit_cont_cls;
399 * Timeout value for the pending message.
401 struct GNUNET_TIME_Absolute timeout;
406 * Queue for acks to send for fragments recived
412 * next ack in the ack send queue
414 struct AckSendQueue * next;
416 * previous ack in the ack send queue
418 struct AckSendQueue * prev;
420 * pointer to the session this ack belongs to
422 struct MacEndpoint * endpoint;
424 * ID of message, to distinguish between the messages, picked randomly.
431 struct GNUNET_MessageHeader * hdr;
433 * pointer to the ieee wlan header
435 struct ieee80211_frame * ieeewlanheader;
437 * pointer to the radiotap header
439 struct Radiotap_Send * radioHeader;
443 * Session infos gathered from a messages
449 * the session this message belongs to
451 struct Session * session;
455 struct MacAddress addr;
460 struct MacEndpoint * macendpoint;
464 * Session handle for connections.
472 struct SessionHeader header;
475 * Message currently pending for transmission
476 * to this peer, if any. head
478 struct PendingMessage * pending_message_head;
481 * Message currently pending for transmission
482 * to this peer, if any. tail
484 struct PendingMessage * pending_message_tail;
487 * To whom are we talking to (set to our identity
488 * if we are still waiting for the welcome message)
490 struct GNUNET_PeerIdentity target;
493 * Address of the other peer (either based on our 'connect'
494 * call or on our 'accept' call).
499 * Last activity on this connection. Used to select preferred
500 * connection and timeout
502 struct GNUNET_TIME_Absolute last_activity;
507 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
512 struct MacEndpoint * mac;
515 * count of messages in the fragment out queue for this session
518 int fragment_messages_out_count;
523 * Struct to represent one network card connection
528 * Pointer to the global plugin struct.
530 struct Plugin *plugin;
532 * Struct to hold the session reachable over this mac; head
534 struct Sessionqueue * sessions_head;
536 * Struct to hold the session reachable over this mac; tail
538 struct Sessionqueue * sessions_tail;
540 * Messages currently sending
543 struct FragmentMessage * sending_messages_head;
546 * Messages currently sending
547 * to a peer (tail), if any.
549 struct FragmentMessage * sending_messages_tail;
553 struct MacEndpoint *next;
557 struct MacEndpoint *prev;
562 struct MacAddress addr;
565 * Defrag context for this mac endpoint
567 struct GNUNET_DEFRAGMENT_Context * defrag;
570 * count of messages in the fragment out queue for this mac endpoint
573 int fragment_messages_out_count;
581 * Duplicates received
596 * Last activity on this endpoint. Used to select preferred
599 struct GNUNET_TIME_Absolute last_activity;
604 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
608 * Struct for Messages in the fragment queue
611 struct FragmentMessage
615 * Session this message belongs to
618 struct Session *session;
621 * This is a doubly-linked list.
623 struct FragmentMessage *next;
626 * This is a doubly-linked list.
628 struct FragmentMessage *prev;
631 * Fragmentation context
633 struct GNUNET_FRAGMENT_Context * fragcontext;
636 * Timeout value for the message.
638 struct GNUNET_TIME_Absolute timeout;
643 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
656 * pointer to the ieee wlan header
658 struct ieee80211_frame * ieeewlanheader;
660 * pointer to the radiotap header
662 struct Radiotap_Send * radioHeader;
666 do_transmit(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
668 free_session(struct Plugin * plugin, struct Sessionqueue * queue,
669 int do_free_macendpoint);
670 static struct MacEndpoint *
671 create_macendpoint(struct Plugin *plugin, const struct MacAddress *addr);
674 * Generates a nice hexdump of a memory area.
676 * \param mem pointer to memory to dump
677 * \param length how many bytes to dump
680 hexdump(void *mem, unsigned length)
683 char *src = (char*) mem;
686 "dumping %u bytes from %p\r\n"
687 " 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF\r\n",
692 for (i = 0; i < length; i += 16, src += 16)
696 t += sprintf(t, "%04x: ", i);
697 for (j = 0; j < 16; j++)
700 t += sprintf(t, "%02X", src[j] & 0xff);
702 t += sprintf(t, " ");
703 t += sprintf(t, j % 2 ? " " : "-");
706 t += sprintf(t, " ");
707 for (j = 0; j < 16; j++)
711 if (isprint((unsigned char)src[j]))
712 t += sprintf(t, "%c", src[j]);
714 t += sprintf(t, ".");
718 t += sprintf(t, " ");
722 t += sprintf(t, "\r\n");
728 * Function to find a MacEndpoint with a specific mac addr
729 * @param plugin pointer to the plugin struct
730 * @param addr pointer to the mac address
731 * @param create_new GNUNET_YES if a new end point should be created
734 static struct MacEndpoint *
735 get_macendpoint(struct Plugin *plugin, const struct MacAddress *addr,
738 struct MacEndpoint * queue = plugin->mac_head;
739 while (queue != NULL)
741 GNUNET_assert (queue->sessions_head != NULL);
742 if (memcmp(addr, &queue->addr, sizeof(struct MacAddress)) == 0)
743 return queue; /* session found */
747 if (create_new == GNUNET_YES)
749 return create_macendpoint(plugin, addr);
758 * search for a session with the addr and peer id
760 * @param plugin pointer to the plugin struct
761 * @param addr pointer to the mac address of the peer
762 * @param peer pointer to the peerid
763 * @return returns the session
765 static struct Session *
766 search_session(struct Plugin *plugin, const struct MacEndpoint * endpoint,
767 const struct GNUNET_PeerIdentity * peer)
769 GNUNET_assert(endpoint != NULL);
770 struct Sessionqueue * queue = endpoint->sessions_head;
772 while (queue != NULL)
774 GNUNET_assert (queue->content != NULL);
775 if (memcmp(peer, &queue->content->target,
776 sizeof(struct GNUNET_PeerIdentity)) == 0)
777 return queue->content; /* session found */
784 * Function called for a quick conversion of the binary address to
785 * a numeric address. Note that the caller must not free the
786 * address and that the next call to this function is allowed
787 * to override the address again.
790 * @param addr binary address
791 * @param addrlen length of the address
792 * @return string representing the same address
795 wlan_plugin_address_to_string(void *cls, const void *addr, size_t addrlen)
798 const struct MacAddress *mac;
800 if (addrlen != sizeof(struct MacAddress))
806 GNUNET_snprintf(ret, sizeof(ret), "%s Mac-Address %X:%X:%X:%X:%X:%X",
807 PROTOCOL_PREFIX, mac->mac[0], mac->mac[1], mac->mac[2], mac->mac[3],
808 mac->mac[4], mac->mac[5]);
813 * Function for the scheduler if a session times out
814 * @param cls pointer to the Sessionqueue
815 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
818 session_timeout (void *cls,
819 const struct GNUNET_SCHEDULER_TaskContext *tc){
820 struct Sessionqueue * queue = cls;
821 GNUNET_assert(queue != NULL);
822 queue->content->timeout_task = GNUNET_SCHEDULER_NO_TASK;
823 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN){
826 if (GNUNET_TIME_absolute_get_remaining(
827 GNUNET_TIME_absolute_add(queue->content->last_activity, SESSION_TIMEOUT)).rel_value
830 free_session(queue->content->mac->plugin,queue,GNUNET_YES);
834 queue->content->timeout_task = GNUNET_SCHEDULER_add_delayed(SESSION_TIMEOUT,
835 &session_timeout, queue);
840 * create a new session
842 * @param plugin pointer to the plugin struct
843 * @param addr pointer to the mac endpoint of the peer
844 * @return returns the session
847 static struct Session *
848 create_session(struct Plugin *plugin, struct MacEndpoint * endpoint,
849 const struct GNUNET_PeerIdentity * peer)
851 GNUNET_assert(endpoint != NULL);
852 struct Sessionqueue * queue = GNUNET_malloc (sizeof (struct Sessionqueue) + sizeof (struct Session));
854 GNUNET_CONTAINER_DLL_insert_tail(endpoint->sessions_head, endpoint->sessions_tail, queue);
856 queue->content = (struct Session *) &queue[1];
857 queue->content->mac = endpoint;
858 memcpy(&(queue->content->target), peer, sizeof(struct GNUNET_PeerIdentity));
859 queue->content->last_activity = GNUNET_TIME_absolute_get();
860 queue->content->timeout_task = GNUNET_SCHEDULER_add_delayed(SESSION_TIMEOUT,&session_timeout, queue);
863 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME, "New session %p with endpoint %p: %s\n",
864 queue->content, endpoint,
865 wlan_plugin_address_to_string(NULL, endpoint->addr.mac, 6));
868 return queue->content;
872 * Get session from address, create if no session exists
874 * @param plugin pointer to the plugin struct
875 * @param addr pointer to the mac address of the peer
876 * @param peer pointer to the peerid
877 * @return returns the session
879 static struct Session *
880 get_session(struct Plugin *plugin, const struct MacAddress *addr,
881 const struct GNUNET_PeerIdentity * peer)
883 struct MacEndpoint * mac;
884 mac = get_macendpoint(plugin, addr, GNUNET_YES);
885 struct Session * session = search_session(plugin, mac, peer);
889 return create_session(plugin, mac, peer);
893 * Queue the session to send data
894 * checks if there is a message pending
895 * checks if this session is not allready in the queue
896 * @param plugin pointer to the plugin
897 * @param session pointer to the session to add
900 queue_session(struct Plugin *plugin, struct Session * session)
902 struct Sessionqueue * queue = plugin->pending_Sessions_head;
904 if (session->pending_message_head != NULL)
906 while (queue != NULL)
908 // content is never NULL
909 GNUNET_assert (queue->content != NULL);
910 // is session already in queue?
911 if (session == queue->content)
919 // Session is not in the queue
921 queue = GNUNET_malloc (sizeof (struct Sessionqueue));
922 queue->content = session;
925 GNUNET_CONTAINER_DLL_insert_tail(plugin->pending_Sessions_head,
926 plugin->pending_Sessions_tail, queue);
927 plugin->pendingsessions++;
933 * Function to schedule the write task, executed after a delay
934 * @param cls pointer to the plugin struct
935 * @param tc GNUNET_SCHEDULER_TaskContext pointer
938 delay_fragment_task(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
940 struct Plugin * plugin = cls;
941 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
943 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
946 // GNUNET_TIME_UNIT_FOREVER_REL is needed to clean up old msg
947 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
949 plugin->server_write_task = GNUNET_SCHEDULER_add_write_file(
950 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdin_handle,
951 &do_transmit, plugin);
956 * Function to calculate the time of the next periodic "hello-beacon"
957 * @param plugin pointer to the plugin struct
961 set_next_beacon_time(struct Plugin * const plugin)
963 //under 10 known peers: once a second
964 if (plugin->mac_count < 10)
966 plugin->beacon_time = GNUNET_TIME_absolute_add(
967 GNUNET_TIME_absolute_get(),
968 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
969 HALLO_BEACON_SCALING_FACTOR));
971 //under 30 known peers: every 10 seconds
972 else if (plugin->mac_count < 30)
974 plugin->beacon_time = GNUNET_TIME_absolute_add(
975 GNUNET_TIME_absolute_get(),
976 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
977 10 * HALLO_BEACON_SCALING_FACTOR));
979 //over 30 known peers: once a minute
982 plugin->beacon_time = GNUNET_TIME_absolute_add(
983 GNUNET_TIME_absolute_get(),
984 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES,
985 HALLO_BEACON_SCALING_FACTOR));
990 * Function to set the timer for the next timeout of the fragment queue
991 * @param plugin the handle to the plugin struct
995 set_next_send(struct Plugin * const plugin)
997 struct GNUNET_TIME_Relative next_send;
1000 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
1002 GNUNET_SCHEDULER_cancel(plugin->server_write_delay_task);
1003 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
1006 //check if some acks are in the queue
1007 if (plugin->ack_send_queue_head != NULL)
1009 next_send = GNUNET_TIME_UNIT_ZERO;
1012 //check if there are some fragments in the queue
1013 else if (plugin->sending_messages_head != NULL)
1015 next_send = GNUNET_TIME_UNIT_ZERO;
1019 next_send = GNUNET_TIME_absolute_get_remaining(plugin->beacon_time);
1023 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME, "Next packet is send in: %u\n",
1024 next_send.rel_value);
1027 if (next_send.rel_value == GNUNET_TIME_UNIT_ZERO.rel_value)
1029 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
1031 plugin->server_write_task = GNUNET_SCHEDULER_add_write_file(
1032 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdin_handle,
1033 &do_transmit, plugin);
1038 if (plugin->server_write_delay_task == GNUNET_SCHEDULER_NO_TASK)
1040 plugin->server_write_delay_task = GNUNET_SCHEDULER_add_delayed(
1041 next_send, &delay_fragment_task, plugin);
1047 * Function to get the next queued Session, removes the session from the queue
1048 * @param plugin pointer to the plugin struct
1049 * @return pointer to the session found, returns NULL if there is now session in the queue
1051 static struct Session *
1052 get_next_queue_session(struct Plugin * plugin)
1054 struct Session * session;
1055 struct Sessionqueue * sessionqueue;
1056 struct Sessionqueue * sessionqueue_alt;
1057 struct PendingMessage * pm;
1058 sessionqueue = plugin->pending_Sessions_head;
1060 while (sessionqueue != NULL)
1062 session = sessionqueue->content;
1064 GNUNET_assert(session != NULL);
1065 pm = session->pending_message_head;
1070 GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1071 "pending message is empty, should not happen. session %p\n",
1076 GNUNET_assert(pm != NULL);
1078 //check for message timeout
1079 if (GNUNET_TIME_absolute_get_remaining(pm->timeout).rel_value > 0)
1081 //check if session has no message in the fragment queue
1082 if ((session->mac->fragment_messages_out_count
1083 < FRAGMENT_QUEUE_MESSAGES_OUT_PER_MACENDPOINT)
1084 && (session->fragment_messages_out_count
1085 < FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION))
1087 plugin->pendingsessions--;
1088 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1089 plugin->pending_Sessions_tail, sessionqueue);
1090 GNUNET_free(sessionqueue);
1096 sessionqueue = sessionqueue->next;
1101 GNUNET_CONTAINER_DLL_remove(session->pending_message_head, session->pending_message_tail, pm);
1103 //call the cont func that it did not work
1104 if (pm->transmit_cont != NULL)
1105 pm->transmit_cont(pm->transmit_cont_cls, &(session->target),
1107 GNUNET_free(pm->msg);
1110 if (session->pending_message_head == NULL)
1112 sessionqueue_alt = sessionqueue;
1113 sessionqueue = sessionqueue->next;
1114 plugin->pendingsessions--;
1115 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1116 plugin->pending_Sessions_tail, sessionqueue_alt);
1118 GNUNET_free(sessionqueue_alt);
1127 * frees the space of a message in the fragment queue (send queue)
1128 * @param plugin the plugin struct
1129 * @param fm message to free
1132 free_fragment_message(struct Plugin * plugin, struct FragmentMessage * fm)
1134 struct Session * session = fm->session;
1135 struct MacEndpoint * endpoint = session->mac;
1136 struct FragmentMessage_queue * fmq;
1137 struct FragmentMessage_queue * fmq_next;
1141 fmq = plugin->sending_messages_head;
1144 fmq_next = fmq->next;
1145 if (fmq->content == fm)
1147 GNUNET_CONTAINER_DLL_remove(plugin->sending_messages_head,plugin->sending_messages_tail, fmq);
1153 (session->mac->fragment_messages_out_count)--;
1154 session->fragment_messages_out_count--;
1155 plugin->pending_Fragment_Messages--;
1156 GNUNET_CONTAINER_DLL_remove(endpoint->sending_messages_head,endpoint->sending_messages_tail, fm);
1157 GNUNET_FRAGMENT_context_destroy(fm->fragcontext);
1158 if (fm->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1159 GNUNET_SCHEDULER_cancel(fm->timeout_task);
1162 queue_session(plugin, session);
1164 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1165 "Free pending fragment messages %p, session %p\n", fm, session);
1171 * function to fill the radiotap header
1172 * @param plugin pointer to the plugin struct
1173 * @param endpoint pointer to the endpoint
1174 * @param header pointer to the radiotap header
1175 * @return GNUNET_YES at success
1178 getRadiotapHeader(struct Plugin * plugin, struct MacEndpoint * endpoint,
1179 struct Radiotap_Send * header)
1182 if (endpoint != NULL)
1184 header->rate = endpoint->rate;
1185 header->tx_power = endpoint->tx_power;
1186 header->antenna = endpoint->antenna;
1191 header->tx_power = 0;
1192 header->antenna = 0;
1199 * function to generate the wlan hardware header for one packet
1200 * @param Header address to write the header to
1201 * @param to_mac_addr address of the recipient
1202 * @param plugin pointer to the plugin struct
1203 * @return GNUNET_YES if there was no error
1206 getWlanHeader(struct ieee80211_frame * Header,
1207 const struct MacAddress * to_mac_addr, struct Plugin * plugin,
1211 const int rate = 11000000;
1213 Header->i_fc[0] = IEEE80211_FC0_TYPE_DATA;
1214 Header->i_fc[1] = 0x00;
1215 memcpy(&Header->i_addr3, &mac_bssid, sizeof(mac_bssid));
1216 memcpy(&Header->i_addr2, plugin->mac_address.mac, sizeof(plugin->mac_address));
1217 memcpy(&Header->i_addr1, to_mac_addr, sizeof(struct MacAddress));
1219 tmp16 = (uint16_t*) Header->i_dur;
1220 *tmp16 = (uint16_t) htole16((size * 1000000) / rate + 290);
1222 #if DEBUG_wlan_ip_udp_packets_on_air
1226 Header->ip.ip_dst.s_addr = *((uint32_t*) &to_mac_addr->mac[2]);
1227 Header->ip.ip_src.s_addr = *((uint32_t*) &plugin->mac_address.mac[2]);
1228 Header->ip.ip_v = 4;
1229 Header->ip.ip_hl = 5;
1230 Header->ip.ip_p = 17;
1231 Header->ip.ip_ttl = 1;
1232 Header->ip.ip_len = htons(size + 8);
1233 Header->ip.ip_sum = 0;
1234 x =(uint16_t *) &Header->ip;
1235 count = sizeof(struct iph);
1238 /* This is the inner loop */
1239 crc += (unsigned short) * x++;
1242 /* Add left-over byte, if any */
1244 crc += * (unsigned char *) x;
1245 crc = (crc & 0xffff) + (crc >> 16);
1246 Header->ip.ip_sum = htons(~ (unsigned short) crc);
1249 Header->udp.len = htons(size - sizeof(struct ieee80211_frame));
1259 * @param msgbuf pointer tor the data
1260 * @param msgbuf_size size of the data
1262 * @return 32bit crc value
1266 getcrc32(const char *msgbuf, size_t msgbuf_size)
1269 return GNUNET_CRYPTO_crc32_n(msgbuf, msgbuf_size);;
1275 * @param msgbuf pointer tor the data
1276 * @param msgbuf_size size of the data
1278 * @return 16bit crc value
1282 getcrc16(const char *msgbuf, size_t msgbuf_size)
1284 //TODO calc some crc
1289 * function to add a fragment of a message to send
1290 * @param cls FragmentMessage this message belongs to
1291 * @param hdr pointer to the start of the message
1295 add_message_for_send(void *cls, const struct GNUNET_MessageHeader *hdr)
1298 struct FragmentMessage * fm = cls;
1299 struct FragmentMessage_queue * fmqueue;
1301 GNUNET_assert(cls != NULL);
1302 GNUNET_assert(fm->frag == NULL);
1303 struct MacEndpoint * endpoint = fm->session->mac;
1304 struct Plugin * plugin = endpoint->plugin;
1305 struct GNUNET_MessageHeader * msgheader;
1306 struct GNUNET_MessageHeader * msgheader2;
1309 #if DEBUG_wlan_retransmission
1310 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1311 "Adding fragment of message %p to send, session %p, endpoint %p\n", fm,
1312 fm->session, endpoint);
1315 size = sizeof(struct GNUNET_MessageHeader) + sizeof(struct Radiotap_Send)
1316 + sizeof(struct ieee80211_frame) + ntohs(hdr->size);
1317 fm->frag = GNUNET_malloc(size);
1320 msgheader = (struct GNUNET_MessageHeader *) fm->frag;
1321 msgheader->size = htons(size);
1322 msgheader->type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1324 fm->radioHeader = (struct Radiotap_Send*) &msgheader[1];
1325 fm->ieeewlanheader = (struct ieee80211_frame*) &fm->radioHeader[1];
1326 msgheader2 = (struct GNUNET_MessageHeader*) &fm->ieeewlanheader[1];
1327 memcpy(msgheader2, hdr, ntohs(hdr->size));
1329 fmqueue = GNUNET_malloc(sizeof(struct FragmentMessage_queue));
1330 fmqueue->content = fm;
1332 GNUNET_CONTAINER_DLL_insert_tail(plugin->sending_messages_head,
1333 plugin->sending_messages_tail, fmqueue);
1334 set_next_send(plugin);
1338 * function to send a hallo beacon
1339 * @param plugin pointer to the plugin struct
1342 send_hello_beacon(struct Plugin * plugin)
1346 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME, "Sending hello beacon\n");
1351 uint16_t hallo_size;
1352 struct GNUNET_MessageHeader * msgheader;
1353 struct ieee80211_frame * ieeewlanheader;
1354 struct Radiotap_Send * radioHeader;
1355 struct GNUNET_MessageHeader * msgheader2;
1357 hallo_size = GNUNET_HELLO_size(*(plugin->env->our_hello));
1358 GNUNET_assert(sizeof(struct WlanHeader) + hallo_size <= WLAN_MTU);
1359 size = sizeof(struct GNUNET_MessageHeader) + sizeof(struct Radiotap_Send)
1360 + sizeof(struct ieee80211_frame) + sizeof(struct GNUNET_MessageHeader)
1363 msgheader = GNUNET_malloc(size);
1364 msgheader->size = htons(size);
1365 msgheader->type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1367 radioHeader = (struct Radiotap_Send*) &msgheader[1];
1368 getRadiotapHeader(plugin, NULL, radioHeader);
1369 ieeewlanheader = (struct ieee80211_frame*) &radioHeader[1];
1370 getWlanHeader(ieeewlanheader, &bc_all_mac, plugin, size);
1372 msgheader2 = (struct GNUNET_MessageHeader*) &ieeewlanheader[1];
1373 msgheader2->size = htons(
1374 GNUNET_HELLO_size(*(plugin->env->our_hello))
1375 + sizeof(struct GNUNET_MessageHeader));
1377 msgheader2->type = htons(GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT);
1378 memcpy(&msgheader2[1], *plugin->env->our_hello, hallo_size);
1380 bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle, msgheader, size);
1382 if (bytes == GNUNET_SYSERR)
1384 GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1385 _("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1386 errno, strerror(errno));
1389 GNUNET_assert(bytes != GNUNET_SYSERR);
1390 GNUNET_assert(bytes == size);
1391 GNUNET_free(msgheader);
1393 set_next_beacon_time(plugin);
1394 set_next_send(plugin);
1398 * function to add an ack to send it for a received fragment
1399 * @param cls MacEndpoint this ack belongs to
1400 * @param msg_id id of the message
1401 * @param hdr pointer to the hdr where the ack is stored
1406 add_ack_for_send(void *cls, uint32_t msg_id,
1407 const struct GNUNET_MessageHeader *hdr)
1410 struct AckSendQueue * ack;
1412 GNUNET_assert(cls != NULL);
1413 struct MacEndpoint * endpoint = cls;
1414 struct Plugin * plugin = endpoint->plugin;
1415 struct GNUNET_MessageHeader * msgheader;
1416 struct GNUNET_MessageHeader * msgheader2;
1419 size = sizeof(struct GNUNET_MessageHeader) + sizeof(struct Radiotap_Send)
1420 + sizeof(struct ieee80211_frame) + ntohs(hdr->size)
1421 + sizeof(struct AckSendQueue);
1423 ack = GNUNET_malloc(size);
1424 ack->message_id = msg_id;
1425 ack->endpoint = endpoint;
1427 size = sizeof(struct GNUNET_MessageHeader) + sizeof(struct Radiotap_Send)
1428 + sizeof(struct ieee80211_frame) + ntohs(hdr->size);
1430 msgheader = (struct GNUNET_MessageHeader *) &ack[1];
1431 ack->hdr = (struct GNUNET_MessageHeader *) &ack[1];
1432 msgheader->size = htons(size);
1433 msgheader->type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1435 ack->radioHeader = (struct Radiotap_Send*) &msgheader[1];
1436 ack->ieeewlanheader = (struct ieee80211_frame*) &(ack->radioHeader)[1];
1437 msgheader2 = (struct GNUNET_MessageHeader*) &(ack->ieeewlanheader)[1];
1438 memcpy(msgheader2, hdr, ntohs(hdr->size));
1440 GNUNET_CONTAINER_DLL_insert_tail(plugin->ack_send_queue_head,
1441 plugin->ack_send_queue_tail, ack);
1443 #if DEBUG_wlan_retransmission
1444 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1445 "Adding ack with message id %u to send, AckSendQueue %p, endpoint %p\n",
1446 msg_id, ack, endpoint);
1449 set_next_send(plugin);
1453 * Function for the scheduler if a FragmentMessage times out
1454 * @param cls pointer to the FragmentMessage
1455 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
1458 fragmentmessage_timeout (void *cls,
1459 const struct GNUNET_SCHEDULER_TaskContext *tc){
1460 struct FragmentMessage * fm = cls;
1461 GNUNET_assert(fm != NULL);
1462 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1463 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN){
1466 free_fragment_message(fm->session->mac->plugin,fm);
1470 * Function to check if there is some space in the fragment queue
1471 * inserts a message if space is available
1472 * @param plugin the plugin struct
1476 check_fragment_queue(struct Plugin * plugin)
1478 struct Session * session;
1479 struct FragmentMessage * fm;
1480 struct GNUNET_PeerIdentity pid;
1482 struct PendingMessage * pm;
1484 if (plugin->pending_Fragment_Messages < FRAGMENT_QUEUE_SIZE)
1486 session = get_next_queue_session(plugin);
1487 if (session != NULL)
1489 pm = session->pending_message_head;
1490 GNUNET_CONTAINER_DLL_remove(session->pending_message_head, session->pending_message_tail, pm);
1491 session->mac->fragment_messages_out_count++;
1492 session->fragment_messages_out_count++;
1493 plugin->pending_Fragment_Messages++;
1494 GNUNET_assert(pm != NULL);
1496 fm = GNUNET_malloc(sizeof(struct FragmentMessage));
1497 fm->session = session;
1498 fm->timeout.abs_value = pm->timeout.abs_value;
1500 fm->fragcontext = GNUNET_FRAGMENT_context_create(plugin->env->stats,
1501 WLAN_MTU, &plugin->tracker, GNUNET_TIME_UNIT_SECONDS,
1502 &(pm->msg->header), &add_message_for_send,
1504 fm->timeout_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_absolute_get_remaining(fm->timeout),fragmentmessage_timeout,fm);
1505 GNUNET_CONTAINER_DLL_insert_tail(session->mac->sending_messages_head,session->mac->sending_messages_tail,fm);
1507 if (pm->transmit_cont != NULL)
1509 pid = session->target;
1510 pm->transmit_cont(pm->transmit_cont_cls, &pid, GNUNET_OK);
1512 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1513 "called pm->transmit_cont for %p\n", session);
1519 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1520 "no pm->transmit_cont for %p\n", session);
1525 if (session->pending_message_head != NULL)
1528 queue_session(plugin, session);
1534 //check if timeout changed
1535 set_next_send(plugin);
1539 * Function to send an ack, does not free the ack
1540 * @param plugin pointer to the plugin
1541 * @param ack pointer to the ack to send
1544 send_ack(struct Plugin * plugin, struct AckSendQueue * ack)
1550 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1551 "Sending ack for message_id %u for mac endpoint %p, size %u\n",
1552 ack->message_id, ack->endpoint,
1553 ntohs(ack->hdr->size) - sizeof(struct Radiotap_Send));
1556 getRadiotapHeader(plugin, ack->endpoint, ack->radioHeader);
1557 getWlanHeader(ack->ieeewlanheader, &ack->endpoint->addr, plugin,
1558 ntohs(ack->hdr->size));
1560 bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle, ack->hdr,
1561 ntohs(ack->hdr->size));
1562 if (bytes == GNUNET_SYSERR)
1564 GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1565 _("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1566 errno, strerror(errno));
1569 GNUNET_assert(bytes != GNUNET_SYSERR);
1570 GNUNET_assert(bytes == ntohs(ack->hdr->size));
1571 set_next_send(plugin);
1575 * function to finish a sending if not all could have been writen befor
1576 * @param cls pointer to the Finish_send struct
1577 * @param tc TaskContext
1580 finish_sending(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1582 struct Finish_send * finish = cls;
1583 struct Plugin * plugin;
1586 plugin = finish->plugin;
1587 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1589 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1591 GNUNET_free (finish->msgstart);
1592 GNUNET_free (finish);
1595 bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle,
1596 finish->msgheader, finish->size);
1597 GNUNET_assert (bytes != GNUNET_SYSERR);
1599 if (bytes != finish->size)
1601 finish->msgheader = finish->msgheader + bytes;
1602 finish->size = finish->size - bytes;
1603 plugin->server_write_task = GNUNET_SCHEDULER_add_write_file(
1604 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdin_handle,
1605 &finish_sending, finish);
1609 GNUNET_free(finish->msgstart);
1610 GNUNET_free(finish);
1611 set_next_send(plugin);
1616 * Function called when wlan helper is ready to get some data
1618 * @param cls closure
1619 * @param tc GNUNET_SCHEDULER_TaskContext
1622 do_transmit(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1624 struct Plugin * plugin = cls;
1626 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1627 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1630 struct Session * session;
1631 struct FragmentMessage * fm;
1632 struct Finish_send * finish;
1633 struct FragmentMessage_queue * fmq;
1634 struct AckSendQueue * ack;
1637 if (plugin->ack_send_queue_head != NULL)
1639 ack = plugin->ack_send_queue_head;
1640 GNUNET_CONTAINER_DLL_remove(plugin->ack_send_queue_head,
1641 plugin->ack_send_queue_tail, ack);
1642 send_ack(plugin, ack);
1647 //test if a "hello-beacon" has to be send
1648 if (GNUNET_TIME_absolute_get_remaining(plugin->beacon_time).rel_value == 0)
1650 send_hello_beacon(plugin);
1654 if (plugin->sending_messages_head != NULL)
1656 fmq = plugin->sending_messages_head;
1658 GNUNET_CONTAINER_DLL_remove(plugin->sending_messages_head,plugin->sending_messages_tail,fmq);
1661 session = fm->session;
1662 GNUNET_assert(session != NULL);
1665 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG,PLUGIN_LOG_NAME,
1666 "Sending GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT for fragment message %p, size: %u\n",
1670 getRadiotapHeader(plugin, session->mac, fm->radioHeader);
1671 getWlanHeader(fm->ieeewlanheader, &(fm->session->mac->addr), plugin,
1674 bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle, fm->frag,
1676 if (bytes == GNUNET_SYSERR)
1678 GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1679 _("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1680 errno, strerror(errno));
1683 GNUNET_assert(bytes != GNUNET_SYSERR);
1685 if (bytes != fm->size)
1687 finish = GNUNET_malloc(sizeof( struct Finish_send));
1688 finish->plugin = plugin;
1689 finish->msgheader = fm->frag + bytes;
1690 finish->size = fm->size - bytes;
1691 finish->msgstart = (struct GNUNET_MessageHeader *) fm->frag;
1693 GNUNET_assert(plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK);
1695 plugin->server_write_task = GNUNET_SCHEDULER_add_write_file(
1696 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdin_handle,
1697 &finish_sending, finish);
1702 GNUNET_free(fm->frag);
1704 set_next_send(plugin);
1706 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
1710 GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
1711 "do_transmit did nothing, should not happen!\n");
1715 * Another peer has suggested an address for this
1716 * peer and transport plugin. Check that this could be a valid
1717 * address. If so, consider adding it to the list
1720 * @param cls closure
1721 * @param addr pointer to the address
1722 * @param addrlen length of addr
1723 * @return GNUNET_OK if this is a plausible address for this peer
1727 wlan_plugin_address_suggested(void *cls, const void *addr, size_t addrlen)
1729 //struct Plugin *plugin = cls;
1731 /* check if the address is plausible; if so,
1732 add it to our list! */
1734 GNUNET_assert(cls !=NULL);
1735 //FIXME mitm is not checked
1736 //Mac Address has 6 bytes
1739 /* TODO check for bad addresses like multicast, broadcast, etc */
1743 return GNUNET_SYSERR;
1747 * Function that can be used by the transport service to transmit
1748 * a message using the plugin.
1750 * @param cls closure
1751 * @param target who should receive this message
1752 * @param priority how important is the message
1753 * @param msgbuf the message to transmit
1754 * @param msgbuf_size number of bytes in 'msgbuf'
1755 * @param timeout when should we time out
1756 * @param session which session must be used (or NULL for "any")
1757 * @param addr the address to use (can be NULL if the plugin
1758 * is "on its own" (i.e. re-use existing TCP connection))
1759 * @param addrlen length of the address in bytes
1760 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1761 * otherwise the plugin may use other addresses or
1762 * existing connections (if available)
1763 * @param cont continuation to call once the message has
1764 * been transmitted (or if the transport is ready
1765 * for the next transmission call; or if the
1766 * peer disconnected...)
1767 * @param cont_cls closure for cont
1768 * @return number of bytes used (on the physical network, with overheads);
1769 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1770 * and does NOT mean that the message was not transmitted (DV)
1773 wlan_plugin_send(void *cls, const struct GNUNET_PeerIdentity * target,
1774 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
1775 struct GNUNET_TIME_Relative timeout, struct Session *session,
1776 const void *addr, size_t addrlen, int force_address,
1777 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1779 struct Plugin * plugin = cls;
1780 struct PendingMessage * newmsg;
1781 struct WlanHeader * wlanheader;
1783 //check if msglen > 0
1784 GNUNET_assert(msgbuf_size > 0);
1786 //get session if needed
1787 if (session == NULL)
1789 if (wlan_plugin_address_suggested(plugin, addr, addrlen) == GNUNET_OK)
1791 session = get_session(plugin, addr, target);
1795 GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1796 _("Wlan Address len %d is wrong\n"), addrlen);
1803 //queue message in session
1804 //test if there is no other message in the "queue"
1805 //FIXME: to many send requests
1806 if (session->pending_message_head != NULL)
1808 newmsg = session->pending_message_head;
1809 GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1810 "wlan_plugin_send: a pending message is already in the queue for this client\n remaining time to send this message is %u, queued fragment messages for this mac connection %u\n",
1811 GNUNET_TIME_absolute_get_remaining(newmsg->timeout).rel_value,
1812 session->mac->fragment_messages_out_count);
1815 newmsg = GNUNET_malloc(sizeof(struct PendingMessage));
1816 newmsg->msg = GNUNET_malloc(msgbuf_size + sizeof(struct WlanHeader));
1817 wlanheader = newmsg->msg;
1818 //copy msg to buffer, not fragmented / segmented yet, but with message header
1819 wlanheader->header.size = htons(msgbuf_size + sizeof(struct WlanHeader));
1820 wlanheader->header.type = htons(GNUNET_MESSAGE_TYPE_WLAN_DATA);
1821 memcpy(&(wlanheader->target), target, sizeof(struct GNUNET_PeerIdentity));
1822 memcpy(&(wlanheader->source), plugin->env->my_identity,
1823 sizeof(struct GNUNET_PeerIdentity));
1824 wlanheader->crc = 0;
1825 memcpy(&wlanheader[1], msgbuf, msgbuf_size);
1826 wlanheader->crc = htonl(
1827 getcrc32((char*) wlanheader, msgbuf_size + sizeof(struct WlanHeader)));
1828 //GNUNET_log_from(GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME, "Wlan message Header crc: %u, %u\n",getcrc32((char*) wlanheader, msgbuf_size + sizeof(struct WlanHeader)), wlanheader->crc);
1829 //hexdump(newmsg->msg, msgbuf_size + sizeof(struct WlanHeader));
1831 newmsg->transmit_cont = cont;
1832 newmsg->transmit_cont_cls = cont_cls;
1833 newmsg->timeout = GNUNET_TIME_relative_to_absolute(timeout);
1835 newmsg->timeout.abs_value = newmsg->timeout.abs_value - 500;
1837 newmsg->message_size = msgbuf_size + sizeof(struct WlanHeader);
1839 GNUNET_CONTAINER_DLL_insert_tail(session->pending_message_head, session->pending_message_tail, newmsg);
1842 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1843 "New message for %p with size (incl wlan header) %u added\n", session,
1844 newmsg->message_size);
1848 queue_session(plugin, session);
1850 check_fragment_queue(plugin);
1851 //FIXME not the correct size
1857 * function to free a mac endpoint
1858 * @param plugin pointer to the plugin struct
1859 * @param endpoin pointer to the MacEndpoint to free
1862 free_macendpoint(struct Plugin * plugin, struct MacEndpoint * endpoint)
1864 struct Sessionqueue * sessions;
1865 struct Sessionqueue * sessions_next;
1866 GNUNET_assert(endpoint != NULL);
1868 sessions = endpoint->sessions_head;
1869 while (sessions != NULL)
1871 sessions_next = sessions->next;
1872 free_session(plugin, sessions, GNUNET_NO);
1873 sessions = sessions_next;
1876 GNUNET_CONTAINER_DLL_remove(plugin->mac_head,plugin->mac_tail,endpoint);
1877 if (endpoint->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1878 GNUNET_SCHEDULER_cancel(endpoint->timeout_task);
1879 GNUNET_free(endpoint);
1884 * function to free a session
1885 * @param plugin pointer to the plugin
1886 * @param queue pointer to the sessionqueue element to free
1887 * @param free_macendpoint if GNUNET_YES and mac endpoint would be empty, free mac endpoint
1890 free_session(struct Plugin * plugin, struct Sessionqueue * queue,
1891 int do_free_macendpoint)
1893 struct Sessionqueue * pendingsession;
1894 struct Sessionqueue * pendingsession_tmp;
1895 struct PendingMessage * pm;
1896 struct MacEndpoint * endpoint;
1897 struct FragmentMessage * fm;
1898 struct FragmentMessage * fmnext;
1901 GNUNET_assert(queue != NULL);
1902 GNUNET_assert(queue->content != NULL);
1905 //is this session pending for send
1906 pendingsession = plugin->pending_Sessions_head;
1907 while (pendingsession != NULL)
1909 pendingsession_tmp = pendingsession->next;
1910 if (pendingsession->content == queue->content)
1912 plugin->pendingsessions--;
1913 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1914 plugin->pending_Sessions_tail,
1916 GNUNET_free(pendingsession);
1918 GNUNET_assert (check == 0);
1921 pendingsession = pendingsession_tmp;
1924 endpoint = queue->content->mac;
1925 fm = endpoint->sending_messages_head;
1929 if (fm->session == queue->content)
1931 free_fragment_message(plugin, fm);
1936 // remove PendingMessage
1937 pm = queue->content->pending_message_head;
1940 GNUNET_CONTAINER_DLL_remove(queue->content->pending_message_head,queue->content->pending_message_tail,pm);
1941 GNUNET_free(pm->msg);
1943 pm = queue->content->pending_message_head;
1946 GNUNET_CONTAINER_DLL_remove(endpoint->sessions_head ,
1947 endpoint->sessions_tail,
1950 if (endpoint->sessions_head == NULL && do_free_macendpoint == GNUNET_YES)
1952 free_macendpoint(plugin, endpoint);
1955 if (queue->content->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1956 GNUNET_SCHEDULER_cancel(queue->content->timeout_task);
1958 check_fragment_queue(plugin);
1962 * Function that can be used to force the plugin to disconnect
1963 * from the given peer and cancel all previous transmissions
1964 * (and their continuation).
1966 * @param cls closure
1967 * @param target peer from which to disconnect
1970 wlan_plugin_disconnect(void *cls, const struct GNUNET_PeerIdentity *target)
1972 struct Plugin *plugin = cls;
1973 struct Sessionqueue * queue;
1974 struct Sessionqueue * queue_next;
1975 struct MacEndpoint * endpoint = plugin->mac_head;
1976 struct MacEndpoint * endpoint_next;
1977 // just look at all the session for the needed one
1978 while (endpoint != NULL)
1980 queue = endpoint->sessions_head;
1981 endpoint_next = endpoint->next;
1982 while (queue != NULL)
1984 // content is never NULL
1985 GNUNET_assert (queue->content != NULL);
1986 queue_next = queue->next;
1987 if (memcmp(target, &(queue->content->target),
1988 sizeof(struct GNUNET_PeerIdentity)) == 0)
1990 free_session(plugin, queue, GNUNET_YES);
1995 endpoint = endpoint_next;
2000 * Convert the transports address to a nice, human-readable
2003 * @param cls closure
2004 * @param type name of the transport that generated the address
2005 * @param addr one of the addresses of the host, NULL for the last address
2006 * the specific address format depends on the transport
2007 * @param addrlen length of the address
2008 * @param numeric should (IP) addresses be displayed in numeric form?
2009 * @param timeout after how long should we give up?
2010 * @param asc function to call on each string
2011 * @param asc_cls closure for asc
2014 wlan_plugin_address_pretty_printer(void *cls, const char *type,
2015 const void *addr, size_t addrlen, int numeric,
2016 struct GNUNET_TIME_Relative timeout,
2017 GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
2020 const unsigned char * input;
2022 //GNUNET_assert(cls !=NULL);
2025 /* invalid address (MAC addresses have 6 bytes) */GNUNET_break (0);
2029 input = (const unsigned char*) addr;
2030 GNUNET_snprintf(ret, sizeof(ret),
2031 "%s Mac-Address %.2X:%.2X:%.2X:%.2X:%.2X:%.2X", PROTOCOL_PREFIX,
2032 input[0], input[1], input[2], input[3], input[4], input[5]);
2037 * handels the data after all fragments are put together
2039 * @param session_light
2040 * @param hdr pointer to the data
2043 wlan_data_message_handler(void *cls, const struct GNUNET_MessageHeader *hdr)
2045 struct MacEndpoint * endpoint = (struct MacEndpoint *) cls;
2046 struct Plugin * plugin = endpoint->plugin;
2047 struct WlanHeader * wlanheader;
2048 struct Session * session;
2049 //const char * tempmsg;
2050 const struct GNUNET_MessageHeader * temp_hdr;
2051 struct GNUNET_PeerIdentity tmpsource;
2054 if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_DATA)
2058 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2059 "Func wlan_data_message_handler got GNUNET_MESSAGE_TYPE_WLAN_DATA size: %u\n",
2063 if (ntohs(hdr->size) < sizeof(struct WlanHeader)
2064 + sizeof(struct GNUNET_MessageHeader))
2066 //packet not big enought
2070 wlanheader = (struct WlanHeader *) hdr;
2072 session = search_session(plugin, endpoint, &wlanheader->source);
2074 //tempmsg = (char*) &wlanheader[1];
2075 temp_hdr = (const struct GNUNET_MessageHeader *) &wlanheader[1];
2076 crc = ntohl(wlanheader->crc);
2077 wlanheader->crc = 0;
2078 if (getcrc32((char *) wlanheader, ntohs(wlanheader->header.size)) != crc)
2080 //wrong crc, dispose message
2081 GNUNET_log_from(GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2082 "Wlan message header crc was wrong: %u != %u\n",
2083 getcrc32((char *) wlanheader, ntohs(wlanheader->header.size)),
2085 hexdump((void *) hdr, ntohs(hdr->size));
2089 //if not in session list
2090 if (session == NULL)
2093 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2094 "WLAN client not in session list: packet size = %u, inner size = %u, header size = %u\n",
2095 ntohs(wlanheader->header.size), ntohs(temp_hdr->size),
2096 sizeof(struct WlanHeader));
2098 //try if it is a hello message
2099 if (ntohs(wlanheader->header.size) >= ntohs(temp_hdr->size)
2100 + sizeof(struct WlanHeader))
2102 if (ntohs(temp_hdr->type) == GNUNET_MESSAGE_TYPE_HELLO)
2104 if (GNUNET_HELLO_get_id(
2105 (const struct GNUNET_HELLO_Message *) temp_hdr,
2106 &tmpsource) == GNUNET_OK)
2108 session = create_session(plugin, endpoint, &tmpsource);
2112 GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2113 "WLAN client not in session list and hello message is not okay\n");
2120 GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2121 "WLAN client not in session list and not a hello message\n");
2127 GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2128 "WLAN client not in session list and message size in does not fit\npacket size = %u, inner size = %u, header size = %u\n",
2129 ntohs(wlanheader->header.size), ntohs(temp_hdr->size),
2130 sizeof(struct WlanHeader));
2135 //"receive" the message
2137 if (memcmp(&wlanheader->source, &session->target,
2138 sizeof(struct GNUNET_PeerIdentity)) != 0)
2142 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2143 "WLAN peer source id doesn't match packet peer source id: session %p\n",
2149 if (memcmp(&wlanheader->target, plugin->env->my_identity,
2150 sizeof(struct GNUNET_PeerIdentity)) != 0)
2154 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2155 "WLAN peer target id doesn't match our peer id: session %p\n",
2161 GNUNET_SERVER_mst_receive(plugin->data_tokenizer, session,
2162 (const char *) temp_hdr,
2163 ntohs(hdr->size) - sizeof(struct WlanHeader), GNUNET_YES, GNUNET_NO);
2169 GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2170 "wlan_data_message_handler got wrong message type\n");
2176 * function to process the a message, give it to the higher layer
2177 * @param cls pointer to the plugin
2178 * @param client pointer to the session this message belongs to
2179 * @param hdr start of the message
2181 //TODO ATS informations
2183 process_data(void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
2186 GNUNET_assert(client != NULL);
2187 GNUNET_assert(cls != NULL);
2188 struct Session * session = (struct Session *) client;
2189 struct Plugin * plugin = (struct Plugin *) cls;
2191 struct GNUNET_TRANSPORT_ATS_Information distance[2];
2192 distance[0].type = htonl(GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2193 distance[0].value = htonl(1);
2194 distance[1].type = htonl(GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2195 distance[1].value = htonl(0);
2198 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2199 "Calling plugin->env->receive for session %p; %s; size: %u\n", session,
2200 wlan_plugin_address_to_string(NULL, session->mac->addr.mac, 6),
2204 plugin->env->receive(plugin->env->cls, &(session->target), hdr,
2205 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance, 2, session,
2206 (const char*) &session->mac->addr, sizeof(session->mac->addr));
2210 * Function used for to process the data received from the wlan interface
2212 * @param cls the plugin handle
2213 * @param session_light FIXME: document
2214 * @param hdr hdr of the GNUNET_MessageHeader
2217 wlan_data_helper(void *cls, struct Session_light * session_light,
2218 const struct GNUNET_MessageHeader * hdr, const struct Radiotap_rx * rxinfo)
2220 struct Plugin *plugin = cls;
2221 struct FragmentMessage * fm;
2222 struct FragmentMessage * fm2;
2225 if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT)
2228 //TODO better DOS protection, error handling
2229 //TODO test first than create session
2230 GNUNET_assert(session_light != NULL);
2233 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2234 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT size: %u; %s\n",
2236 wlan_plugin_address_to_string(NULL, session_light->addr.mac, 6));
2239 if (session_light->macendpoint == NULL)
2241 session_light->macendpoint = get_macendpoint(plugin,
2242 &session_light->addr, GNUNET_NO);
2244 GNUNET_assert(GNUNET_HELLO_get_id(
2245 (const struct GNUNET_HELLO_Message *) &hdr[1],
2246 &(session_light->session->target) ) != GNUNET_SYSERR);
2252 else if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_FRAGMENT)
2255 GNUNET_assert(session_light != NULL);
2256 if (session_light->macendpoint == NULL)
2258 session_light->macendpoint = get_macendpoint(plugin,
2259 &session_light->addr, GNUNET_YES);
2263 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2264 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_FRAGMENT with size: %u; mac endpoint %p: %s\n",
2265 ntohs(hdr->size), session_light->macendpoint,
2266 wlan_plugin_address_to_string(NULL, session_light->addr.mac, 6));
2269 int ret = GNUNET_DEFRAGMENT_process_fragment(
2270 session_light->macendpoint->defrag, hdr);
2271 if (ret == GNUNET_NO)
2273 session_light->macendpoint->dups++;
2275 else if (ret == GNUNET_OK)
2277 session_light->macendpoint->fragc++;
2279 set_next_send(plugin);
2285 else if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_FRAGMENT_ACK)
2287 GNUNET_assert(session_light != NULL);
2288 if (session_light->macendpoint == NULL)
2290 session_light->macendpoint = get_macendpoint(plugin,
2291 &session_light->addr, GNUNET_NO);
2294 if (session_light->macendpoint == NULL)
2297 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2298 "Macendpoint does not exist for this GNUNET_MESSAGE_TYPE_FRAGMENT_ACK size: %u; %s\n",
2300 wlan_plugin_address_to_string(NULL, session_light->addr.mac, 6));
2306 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2307 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_FRAGMENT_ACK size: %u; mac endpoint: %p; %s\n",
2308 ntohs(hdr->size), session_light->macendpoint,
2309 wlan_plugin_address_to_string(NULL, session_light->addr.mac, 6));
2311 fm = session_light->macendpoint->sending_messages_head;
2315 int ret = GNUNET_FRAGMENT_process_ack(fm->fragcontext, hdr);
2316 if (ret == GNUNET_OK)
2318 #if DEBUG_wlan_retransmission
2319 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2320 "Got last ack, finished fragment message %p\n", fm);
2322 session_light->macendpoint->acks++;
2323 fm->session->last_activity = GNUNET_TIME_absolute_get();
2324 session_light->macendpoint->last_activity = fm->session->last_activity;
2325 free_fragment_message(plugin, fm);
2326 check_fragment_queue(plugin);
2329 if (ret == GNUNET_NO)
2331 #if DEBUG_wlan_retransmission
2332 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME, "Got ack for: %p\n", fm);
2334 session_light->macendpoint->acks++;
2337 if (ret == GNUNET_SYSERR)
2345 #if DEBUG_wlan_retransmission
2346 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2347 "WLAN fragment not in fragment list\n");
2355 GNUNET_log_from(GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2356 "WLAN packet inside the WLAN helper packet has not the right type: %u size: %u\n",
2357 ntohs(hdr->type), ntohs(hdr->size));
2363 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2364 "Helper finished\n");
2371 macprinter(const u_int8_t * mac)
2373 static char macstr[20];
2375 GNUNET_snprintf(macstr, sizeof(macstr), "%X:%X:%X:%X:%X:%X", mac[0], mac[1],
2376 mac[2], mac[3], mac[4], mac[5]);
2381 * Function for the scheduler if a mac endpoint times out
2382 * @param cls pointer to the MacEndpoint
2383 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
2386 macendpoint_timeout (void *cls,
2387 const struct GNUNET_SCHEDULER_TaskContext *tc){
2388 struct MacEndpoint * endpoint = cls;
2389 GNUNET_assert(endpoint != NULL);
2390 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2391 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN){
2394 if (GNUNET_TIME_absolute_get_remaining(
2395 GNUNET_TIME_absolute_add(endpoint->last_activity, MACENDPOINT_TIMEOUT)).rel_value
2398 free_macendpoint(endpoint->plugin,endpoint);
2402 endpoint->timeout_task = GNUNET_SCHEDULER_add_delayed(MACENDPOINT_TIMEOUT,
2403 &macendpoint_timeout, endpoint);
2408 * function to create an macendpoint
2409 * @param plugin pointer to the plugin struct
2410 * @param addr pointer to the macaddress
2411 * @return returns a macendpoint
2413 static struct MacEndpoint *
2414 create_macendpoint(struct Plugin *plugin, const struct MacAddress *addr)
2416 struct MacEndpoint * newend = GNUNET_malloc(sizeof (struct MacEndpoint));
2417 newend->addr = *addr;
2418 newend->plugin = plugin;
2419 newend->addr = *addr;
2420 newend->fragment_messages_out_count = 0;
2421 newend->defrag = GNUNET_DEFRAGMENT_context_create(plugin->env->stats,
2422 WLAN_MTU, MESSAGES_IN_DEFRAG_QUEUE_PER_MAC, newend,
2423 &wlan_data_message_handler, &add_ack_for_send);
2424 newend->last_activity = GNUNET_TIME_absolute_get();
2425 newend->timeout_task = GNUNET_SCHEDULER_add_delayed(MACENDPOINT_TIMEOUT,
2426 &macendpoint_timeout, newend);
2428 plugin->mac_count++;
2429 GNUNET_CONTAINER_DLL_insert_tail(plugin->mac_head, plugin->mac_tail, newend);
2431 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME, "New Mac Endpoint %p: %s\n", newend,
2432 wlan_plugin_address_to_string(NULL, newend->addr.mac, 6));
2438 * Function used for to process the data from the suid process
2440 * @param cls the plugin handle
2441 * @param client client that send the data (not used)
2442 * @param hdr header of the GNUNET_MessageHeader
2445 wlan_process_helper(void *cls, void *client,
2446 const struct GNUNET_MessageHeader *hdr)
2448 struct Plugin *plugin = cls;
2449 struct ieee80211_frame * wlanIeeeHeader = NULL;
2450 struct Session_light * session_light = NULL;
2451 struct Radiotap_rx * rxinfo;
2452 const struct GNUNET_MessageHeader * temp_hdr = NULL;
2457 switch (ntohs(hdr->type))
2459 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA:
2461 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2462 "Func wlan_process_helper got GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA size: %u\n",
2466 //call wlan_process_helper with the message inside, later with wlan: analyze signal
2467 if (ntohs(hdr->size) < sizeof(struct ieee80211_frame)
2468 + sizeof(struct GNUNET_MessageHeader) + sizeof(struct Radiotap_rx))
2471 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2472 "Size of packet is too small; size: %u min size: %u\n",
2474 sizeof(struct ieee80211_frame)
2475 + sizeof(struct GNUNET_MessageHeader));
2478 /* FIXME: restart SUID process */
2481 rxinfo = (struct Radiotap_rx *) &hdr[1];
2482 wlanIeeeHeader = (struct ieee80211_frame *) &rxinfo[1];
2484 //process only if it is an broadcast or for this computer both with the gnunet bssid
2487 if (memcmp(&(wlanIeeeHeader->i_addr3), &mac_bssid,
2488 sizeof(struct MacAddress)) == 0)
2490 //check for broadcast or mac
2491 if (memcmp(&(wlanIeeeHeader->i_addr1), &bc_all_mac,
2492 sizeof(struct MacAddress) == 0) || memcmp(
2493 &(wlanIeeeHeader->i_addr1), &(plugin->mac_address),
2494 sizeof(struct MacAddress)) == 0)
2497 // process the inner data
2500 datasize = ntohs(hdr->size) - sizeof(struct ieee80211_frame)
2501 - sizeof(struct GNUNET_MessageHeader)
2502 - sizeof(struct Radiotap_rx);
2504 session_light = GNUNET_malloc(sizeof(struct Session_light));
2505 memcpy(&session_light->addr, &(wlanIeeeHeader->i_addr2),
2506 sizeof(struct MacAddress));
2507 //session_light->session = search_session(plugin,session_light->addr);
2510 while (pos < datasize)
2512 temp_hdr = (struct GNUNET_MessageHeader *) &wlanIeeeHeader[1]
2515 wlan_data_helper(plugin, session_light, temp_hdr, rxinfo);
2516 pos += ntohs(temp_hdr->size);
2521 GNUNET_free(session_light);
2526 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2527 "Func wlan_process_helper got wrong MAC: %s\n",
2528 macprinter(wlanIeeeHeader->i_addr1));
2535 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2536 "Func wlan_process_helper got wrong BSSID: %s\n",
2537 macprinter(wlanIeeeHeader->i_addr2));
2541 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
2542 //TODO more control messages
2543 //TODO use struct wlan_helper_control
2544 if (ntohs(hdr->size) != sizeof(struct Wlan_Helper_Control_Message))
2547 /* FIXME: restart SUID process */
2550 memcpy(&plugin->mac_address, &hdr[1], sizeof(struct MacAddress));
2552 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2553 "Received WLAN_HELPER_CONTROL message with transport of address %s\n",
2554 wlan_plugin_address_to_string(cls, &plugin->mac_address,
2555 sizeof(struct MacAddress)));
2557 plugin->env->notify_address(plugin->env->cls, GNUNET_YES,
2558 &plugin->mac_address, sizeof(struct MacAddress));
2567 * We have been notified that wlan-helper has written something to stdout.
2568 * Handle the output, then reschedule this function to be called again once
2569 * more is available.
2571 * @param cls the plugin handle
2572 * @param tc the scheduling context
2575 wlan_plugin_helper_read(void *cls,
2576 const struct GNUNET_SCHEDULER_TaskContext *tc)
2578 struct Plugin *plugin = cls;
2579 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2581 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2584 char mybuf[WLAN_MTU + sizeof(struct GNUNET_MessageHeader)];
2587 bytes = GNUNET_DISK_file_read(plugin->server_stdout_handle, mybuf,
2592 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2593 _("Finished reading from wlan-helper stdout with code: %d\n"),
2598 GNUNET_SERVER_mst_receive(plugin->suid_tokenizer, NULL, mybuf, bytes,
2599 GNUNET_NO, GNUNET_NO);
2601 GNUNET_assert(plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2602 plugin->server_read_task = GNUNET_SCHEDULER_add_read_file(
2603 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdout_handle,
2604 &wlan_plugin_helper_read, plugin);
2608 * Start the gnunet-wlan-helper process.
2610 * @param plugin the transport plugin
2611 * @param testmode should we use the dummy driver for testing?
2612 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
2615 wlan_transport_start_wlan_helper(struct Plugin *plugin, int testmode)
2617 const char * filenamehw = "gnunet-transport-wlan-helper";
2618 const char * filenameloopback = "gnunet-transport-wlan-helper-dummy";
2619 plugin->server_stdout = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_NO, GNUNET_YES);
2620 if (plugin->server_stdout == NULL)
2621 return GNUNET_SYSERR;
2623 plugin->server_stdin = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_NO);
2624 if (plugin->server_stdin == NULL)
2625 return GNUNET_SYSERR;
2627 /* Start the server process */
2633 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2634 "Starting gnunet-wlan-helper process cmd: %s %s %i\n", filenamehw,
2635 plugin->interface, testmode);
2638 plugin->server_proc = GNUNET_OS_start_process(plugin->server_stdin,
2639 plugin->server_stdout, filenamehw, filenamehw, plugin->interface,
2642 else if (testmode == 1)
2646 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2647 "Starting gnunet-wlan-helper loopback 1 process cmd: %s %s %i\n",
2648 filenameloopback, plugin->interface, testmode);
2651 plugin->server_proc = GNUNET_OS_start_process(plugin->server_stdin,
2652 plugin->server_stdout, filenameloopback, filenameloopback, "1", NULL);
2654 else if (testmode == 2)
2657 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2658 "Starting gnunet-wlan-helper loopback 2 process cmd: %s %s %i\n",
2659 filenameloopback, plugin->interface, testmode);
2661 plugin->server_proc = GNUNET_OS_start_process(plugin->server_stdin,
2662 plugin->server_stdout, filenameloopback, filenameloopback, "2", NULL);
2664 if (plugin->server_proc == NULL)
2667 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2668 "Failed to start gnunet-wlan-helper process\n");
2670 return GNUNET_SYSERR;
2673 /* Close the write end of the read pipe */
2674 GNUNET_DISK_pipe_close_end(plugin->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
2676 /* Close the read end of the write pipe */
2677 GNUNET_DISK_pipe_close_end(plugin->server_stdin, GNUNET_DISK_PIPE_END_READ);
2679 plugin->server_stdout_handle = GNUNET_DISK_pipe_handle(plugin->server_stdout,
2680 GNUNET_DISK_PIPE_END_READ);
2681 plugin->server_stdin_handle = GNUNET_DISK_pipe_handle(plugin->server_stdin,
2682 GNUNET_DISK_PIPE_END_WRITE);
2684 GNUNET_assert(plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2687 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2688 "Adding server_read_task for the wlan-helper\n");
2691 plugin->server_read_task = GNUNET_SCHEDULER_add_read_file(
2692 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdout_handle,
2693 &wlan_plugin_helper_read, plugin);
2699 * Exit point from the plugin.
2700 * @param cls pointer to the api struct
2705 libgnunet_plugin_transport_wlan_done(void *cls)
2707 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2708 struct Plugin *plugin = api->cls;
2709 struct MacEndpoint * endpoint = plugin->mac_head;
2710 struct MacEndpoint * endpoint_next;
2713 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2714 "libgnunet_plugin_transport_wlan_done started\n");
2717 GNUNET_DISK_pipe_close(plugin->server_stdout);
2718 GNUNET_DISK_pipe_close(plugin->server_stdin);
2719 GNUNET_OS_process_kill(plugin->server_proc, 9);
2720 GNUNET_OS_process_close(plugin->server_proc);
2722 GNUNET_assert (cls !=NULL);
2724 while (endpoint != NULL)
2726 endpoint_next = endpoint->next;
2727 free_macendpoint(plugin, endpoint);
2728 endpoint = endpoint_next;
2731 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
2733 GNUNET_SCHEDULER_cancel(plugin->server_write_delay_task);
2734 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
2736 if (plugin->server_write_task != GNUNET_SCHEDULER_NO_TASK)
2738 GNUNET_SCHEDULER_cancel(plugin->server_write_task);
2739 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
2741 if (plugin->server_read_task != GNUNET_SCHEDULER_NO_TASK)
2743 GNUNET_SCHEDULER_cancel(plugin->server_read_task);
2744 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2747 if (plugin->suid_tokenizer != NULL)
2748 GNUNET_SERVER_mst_destroy(plugin->suid_tokenizer);
2750 if (plugin->data_tokenizer != NULL)
2751 GNUNET_SERVER_mst_destroy(plugin->data_tokenizer);
2753 GNUNET_free_non_null(plugin->interface);
2754 GNUNET_free (plugin);
2760 * Entry point for the plugin.
2762 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2763 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2766 libgnunet_plugin_transport_wlan_init(void *cls)
2768 //struct GNUNET_SERVICE_Context *service;
2769 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2770 struct GNUNET_TRANSPORT_PluginFunctions *api;
2771 struct Plugin *plugin;
2772 static unsigned long long testmode = 0;
2774 GNUNET_assert(cls !=NULL);
2776 plugin = GNUNET_malloc (sizeof (struct Plugin));
2778 plugin->pendingsessions = 0;
2779 plugin->mac_count = 0;
2780 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
2781 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2782 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
2783 GNUNET_BANDWIDTH_tracker_init(&plugin->tracker,
2784 GNUNET_BANDWIDTH_value_init(100 * 1024 * 1024 / 8), 100);
2786 plugin->suid_tokenizer = GNUNET_SERVER_mst_create(&wlan_process_helper,
2789 plugin->data_tokenizer = GNUNET_SERVER_mst_create(&process_data, plugin);
2791 //plugin->sessions = GNUNET_malloc (sizeof (struct Sessionqueue));
2792 //plugin->pending_Sessions_head = GNUNET_malloc (sizeof (struct Sessionqueue));
2794 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2796 api->send = &wlan_plugin_send;
2797 api->disconnect = &wlan_plugin_disconnect;
2798 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2799 api->check_address = &wlan_plugin_address_suggested;
2800 api->address_to_string = &wlan_plugin_address_to_string;
2803 if (GNUNET_CONFIGURATION_have_value(env->cfg, "transport-wlan", "TESTMODE"))
2805 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(env->cfg,
2806 "transport-wlan", "TESTMODE", &testmode))
2807 testmode = 0; //default value
2810 if (GNUNET_CONFIGURATION_have_value(env->cfg, "transport-wlan", "INTERFACE"))
2812 if (GNUNET_CONFIGURATION_get_value_string(env->cfg, "transport-wlan",
2813 "INTERFACE", &(plugin->interface)) != GNUNET_YES)
2815 libgnunet_plugin_transport_wlan_done(api);
2821 set_next_beacon_time(plugin);
2823 wlan_transport_start_wlan_helper(plugin, testmode);
2826 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME, "wlan init finished\n");
2832 /* end of plugin_transport_wlan.c */