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;
1356 const struct GNUNET_MessageHeader *hello;
1358 hello = plugin->env->get_our_hello ();
1359 hallo_size = GNUNET_HELLO_size((struct GNUNET_HELLO_Message*)hello);
1360 GNUNET_assert(sizeof(struct WlanHeader) + hallo_size <= WLAN_MTU);
1361 size = sizeof(struct GNUNET_MessageHeader) + sizeof(struct Radiotap_Send)
1362 + sizeof(struct ieee80211_frame) + sizeof(struct GNUNET_MessageHeader)
1365 msgheader = GNUNET_malloc(size);
1366 msgheader->size = htons(size);
1367 msgheader->type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1369 radioHeader = (struct Radiotap_Send*) &msgheader[1];
1370 getRadiotapHeader(plugin, NULL, radioHeader);
1371 ieeewlanheader = (struct ieee80211_frame*) &radioHeader[1];
1372 getWlanHeader(ieeewlanheader, &bc_all_mac, plugin, size);
1374 msgheader2 = (struct GNUNET_MessageHeader*) &ieeewlanheader[1];
1375 msgheader2->size = htons(GNUNET_HELLO_size((struct GNUNET_HELLO_Message*)hello)
1376 + sizeof(struct GNUNET_MessageHeader));
1378 msgheader2->type = htons(GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT);
1379 memcpy(&msgheader2[1], hello, hallo_size);
1381 bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle, msgheader, size);
1383 if (bytes == GNUNET_SYSERR)
1385 GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1386 _("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1387 errno, strerror(errno));
1390 GNUNET_assert(bytes != GNUNET_SYSERR);
1391 GNUNET_assert(bytes == size);
1392 GNUNET_free(msgheader);
1394 set_next_beacon_time(plugin);
1395 set_next_send(plugin);
1399 * function to add an ack to send it for a received fragment
1400 * @param cls MacEndpoint this ack belongs to
1401 * @param msg_id id of the message
1402 * @param hdr pointer to the hdr where the ack is stored
1407 add_ack_for_send(void *cls, uint32_t msg_id,
1408 const struct GNUNET_MessageHeader *hdr)
1411 struct AckSendQueue * ack;
1413 GNUNET_assert(cls != NULL);
1414 struct MacEndpoint * endpoint = cls;
1415 struct Plugin * plugin = endpoint->plugin;
1416 struct GNUNET_MessageHeader * msgheader;
1417 struct GNUNET_MessageHeader * msgheader2;
1420 size = sizeof(struct GNUNET_MessageHeader) + sizeof(struct Radiotap_Send)
1421 + sizeof(struct ieee80211_frame) + ntohs(hdr->size)
1422 + sizeof(struct AckSendQueue);
1424 ack = GNUNET_malloc(size);
1425 ack->message_id = msg_id;
1426 ack->endpoint = endpoint;
1428 size = sizeof(struct GNUNET_MessageHeader) + sizeof(struct Radiotap_Send)
1429 + sizeof(struct ieee80211_frame) + ntohs(hdr->size);
1431 msgheader = (struct GNUNET_MessageHeader *) &ack[1];
1432 ack->hdr = (struct GNUNET_MessageHeader *) &ack[1];
1433 msgheader->size = htons(size);
1434 msgheader->type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1436 ack->radioHeader = (struct Radiotap_Send*) &msgheader[1];
1437 ack->ieeewlanheader = (struct ieee80211_frame*) &(ack->radioHeader)[1];
1438 msgheader2 = (struct GNUNET_MessageHeader*) &(ack->ieeewlanheader)[1];
1439 memcpy(msgheader2, hdr, ntohs(hdr->size));
1441 GNUNET_CONTAINER_DLL_insert_tail(plugin->ack_send_queue_head,
1442 plugin->ack_send_queue_tail, ack);
1444 #if DEBUG_wlan_retransmission
1445 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1446 "Adding ack with message id %u to send, AckSendQueue %p, endpoint %p\n",
1447 msg_id, ack, endpoint);
1450 set_next_send(plugin);
1454 * Function for the scheduler if a FragmentMessage times out
1455 * @param cls pointer to the FragmentMessage
1456 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
1459 fragmentmessage_timeout (void *cls,
1460 const struct GNUNET_SCHEDULER_TaskContext *tc){
1461 struct FragmentMessage * fm = cls;
1462 GNUNET_assert(fm != NULL);
1463 fm->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1464 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN){
1467 free_fragment_message(fm->session->mac->plugin,fm);
1471 * Function to check if there is some space in the fragment queue
1472 * inserts a message if space is available
1473 * @param plugin the plugin struct
1477 check_fragment_queue(struct Plugin * plugin)
1479 struct Session * session;
1480 struct FragmentMessage * fm;
1481 struct GNUNET_PeerIdentity pid;
1483 struct PendingMessage * pm;
1485 if (plugin->pending_Fragment_Messages < FRAGMENT_QUEUE_SIZE)
1487 session = get_next_queue_session(plugin);
1488 if (session != NULL)
1490 pm = session->pending_message_head;
1491 GNUNET_CONTAINER_DLL_remove(session->pending_message_head, session->pending_message_tail, pm);
1492 session->mac->fragment_messages_out_count++;
1493 session->fragment_messages_out_count++;
1494 plugin->pending_Fragment_Messages++;
1495 GNUNET_assert(pm != NULL);
1497 fm = GNUNET_malloc(sizeof(struct FragmentMessage));
1498 fm->session = session;
1499 fm->timeout.abs_value = pm->timeout.abs_value;
1501 fm->fragcontext = GNUNET_FRAGMENT_context_create(plugin->env->stats,
1502 WLAN_MTU, &plugin->tracker, GNUNET_TIME_UNIT_SECONDS,
1503 &(pm->msg->header), &add_message_for_send,
1505 fm->timeout_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_absolute_get_remaining(fm->timeout),fragmentmessage_timeout,fm);
1506 GNUNET_CONTAINER_DLL_insert_tail(session->mac->sending_messages_head,session->mac->sending_messages_tail,fm);
1508 if (pm->transmit_cont != NULL)
1510 pid = session->target;
1511 pm->transmit_cont(pm->transmit_cont_cls, &pid, GNUNET_OK);
1513 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1514 "called pm->transmit_cont for %p\n", session);
1520 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1521 "no pm->transmit_cont for %p\n", session);
1526 if (session->pending_message_head != NULL)
1529 queue_session(plugin, session);
1535 //check if timeout changed
1536 set_next_send(plugin);
1540 * Function to send an ack, does not free the ack
1541 * @param plugin pointer to the plugin
1542 * @param ack pointer to the ack to send
1545 send_ack(struct Plugin * plugin, struct AckSendQueue * ack)
1551 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1552 "Sending ack for message_id %u for mac endpoint %p, size %u\n",
1553 ack->message_id, ack->endpoint,
1554 ntohs(ack->hdr->size) - sizeof(struct Radiotap_Send));
1557 getRadiotapHeader(plugin, ack->endpoint, ack->radioHeader);
1558 getWlanHeader(ack->ieeewlanheader, &ack->endpoint->addr, plugin,
1559 ntohs(ack->hdr->size));
1561 bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle, ack->hdr,
1562 ntohs(ack->hdr->size));
1563 if (bytes == GNUNET_SYSERR)
1565 GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1566 _("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1567 errno, strerror(errno));
1570 GNUNET_assert(bytes != GNUNET_SYSERR);
1571 GNUNET_assert(bytes == ntohs(ack->hdr->size));
1572 set_next_send(plugin);
1576 * function to finish a sending if not all could have been writen befor
1577 * @param cls pointer to the Finish_send struct
1578 * @param tc TaskContext
1581 finish_sending(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1583 struct Finish_send * finish = cls;
1584 struct Plugin * plugin;
1587 plugin = finish->plugin;
1588 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1590 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1592 GNUNET_free (finish->msgstart);
1593 GNUNET_free (finish);
1596 bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle,
1597 finish->msgheader, finish->size);
1598 GNUNET_assert (bytes != GNUNET_SYSERR);
1600 if (bytes != finish->size)
1602 finish->msgheader = finish->msgheader + bytes;
1603 finish->size = finish->size - bytes;
1604 plugin->server_write_task = GNUNET_SCHEDULER_add_write_file(
1605 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdin_handle,
1606 &finish_sending, finish);
1610 GNUNET_free(finish->msgstart);
1611 GNUNET_free(finish);
1612 set_next_send(plugin);
1617 * Function called when wlan helper is ready to get some data
1619 * @param cls closure
1620 * @param tc GNUNET_SCHEDULER_TaskContext
1623 do_transmit(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1625 struct Plugin * plugin = cls;
1627 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1628 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1631 struct Session * session;
1632 struct FragmentMessage * fm;
1633 struct Finish_send * finish;
1634 struct FragmentMessage_queue * fmq;
1635 struct AckSendQueue * ack;
1638 if (plugin->ack_send_queue_head != NULL)
1640 ack = plugin->ack_send_queue_head;
1641 GNUNET_CONTAINER_DLL_remove(plugin->ack_send_queue_head,
1642 plugin->ack_send_queue_tail, ack);
1643 send_ack(plugin, ack);
1648 //test if a "hello-beacon" has to be send
1649 if (GNUNET_TIME_absolute_get_remaining(plugin->beacon_time).rel_value == 0)
1651 send_hello_beacon(plugin);
1655 if (plugin->sending_messages_head != NULL)
1657 fmq = plugin->sending_messages_head;
1659 GNUNET_CONTAINER_DLL_remove(plugin->sending_messages_head,plugin->sending_messages_tail,fmq);
1662 session = fm->session;
1663 GNUNET_assert(session != NULL);
1666 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG,PLUGIN_LOG_NAME,
1667 "Sending GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT for fragment message %p, size: %u\n",
1671 getRadiotapHeader(plugin, session->mac, fm->radioHeader);
1672 getWlanHeader(fm->ieeewlanheader, &(fm->session->mac->addr), plugin,
1675 bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle, fm->frag,
1677 if (bytes == GNUNET_SYSERR)
1679 GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1680 _("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1681 errno, strerror(errno));
1684 GNUNET_assert(bytes != GNUNET_SYSERR);
1686 if (bytes != fm->size)
1688 finish = GNUNET_malloc(sizeof( struct Finish_send));
1689 finish->plugin = plugin;
1690 finish->msgheader = fm->frag + bytes;
1691 finish->size = fm->size - bytes;
1692 finish->msgstart = (struct GNUNET_MessageHeader *) fm->frag;
1694 GNUNET_assert(plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK);
1696 plugin->server_write_task = GNUNET_SCHEDULER_add_write_file(
1697 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdin_handle,
1698 &finish_sending, finish);
1703 GNUNET_free(fm->frag);
1705 set_next_send(plugin);
1707 GNUNET_FRAGMENT_context_transmission_done (fm->fragcontext);
1711 GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
1712 "do_transmit did nothing, should not happen!\n");
1716 * Another peer has suggested an address for this
1717 * peer and transport plugin. Check that this could be a valid
1718 * address. If so, consider adding it to the list
1721 * @param cls closure
1722 * @param addr pointer to the address
1723 * @param addrlen length of addr
1724 * @return GNUNET_OK if this is a plausible address for this peer
1728 wlan_plugin_address_suggested(void *cls, const void *addr, size_t addrlen)
1730 //struct Plugin *plugin = cls;
1732 /* check if the address is plausible; if so,
1733 add it to our list! */
1735 GNUNET_assert(cls !=NULL);
1736 //FIXME mitm is not checked
1737 //Mac Address has 6 bytes
1740 /* TODO check for bad addresses like multicast, broadcast, etc */
1744 return GNUNET_SYSERR;
1748 * Function that can be used by the transport service to transmit
1749 * a message using the plugin.
1751 * @param cls closure
1752 * @param target who should receive this message
1753 * @param priority how important is the message
1754 * @param msgbuf the message to transmit
1755 * @param msgbuf_size number of bytes in 'msgbuf'
1756 * @param timeout when should we time out
1757 * @param session which session must be used (or NULL for "any")
1758 * @param addr the address to use (can be NULL if the plugin
1759 * is "on its own" (i.e. re-use existing TCP connection))
1760 * @param addrlen length of the address in bytes
1761 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1762 * otherwise the plugin may use other addresses or
1763 * existing connections (if available)
1764 * @param cont continuation to call once the message has
1765 * been transmitted (or if the transport is ready
1766 * for the next transmission call; or if the
1767 * peer disconnected...)
1768 * @param cont_cls closure for cont
1769 * @return number of bytes used (on the physical network, with overheads);
1770 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1771 * and does NOT mean that the message was not transmitted (DV)
1774 wlan_plugin_send(void *cls, const struct GNUNET_PeerIdentity * target,
1775 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
1776 struct GNUNET_TIME_Relative timeout, struct Session *session,
1777 const void *addr, size_t addrlen, int force_address,
1778 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1780 struct Plugin * plugin = cls;
1781 struct PendingMessage * newmsg;
1782 struct WlanHeader * wlanheader;
1784 //check if msglen > 0
1785 GNUNET_assert(msgbuf_size > 0);
1787 //get session if needed
1788 if (session == NULL)
1790 if (wlan_plugin_address_suggested(plugin, addr, addrlen) == GNUNET_OK)
1792 session = get_session(plugin, addr, target);
1796 GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1797 _("Wlan Address len %d is wrong\n"), addrlen);
1804 //queue message in session
1805 //test if there is no other message in the "queue"
1806 //FIXME: to many send requests
1807 if (session->pending_message_head != NULL)
1809 newmsg = session->pending_message_head;
1810 GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR, PLUGIN_LOG_NAME,
1811 "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",
1812 GNUNET_TIME_absolute_get_remaining(newmsg->timeout).rel_value,
1813 session->mac->fragment_messages_out_count);
1816 newmsg = GNUNET_malloc(sizeof(struct PendingMessage));
1817 newmsg->msg = GNUNET_malloc(msgbuf_size + sizeof(struct WlanHeader));
1818 wlanheader = newmsg->msg;
1819 //copy msg to buffer, not fragmented / segmented yet, but with message header
1820 wlanheader->header.size = htons(msgbuf_size + sizeof(struct WlanHeader));
1821 wlanheader->header.type = htons(GNUNET_MESSAGE_TYPE_WLAN_DATA);
1822 memcpy(&(wlanheader->target), target, sizeof(struct GNUNET_PeerIdentity));
1823 memcpy(&(wlanheader->source), plugin->env->my_identity,
1824 sizeof(struct GNUNET_PeerIdentity));
1825 wlanheader->crc = 0;
1826 memcpy(&wlanheader[1], msgbuf, msgbuf_size);
1827 wlanheader->crc = htonl(
1828 getcrc32((char*) wlanheader, msgbuf_size + sizeof(struct WlanHeader)));
1829 //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);
1830 //hexdump(newmsg->msg, msgbuf_size + sizeof(struct WlanHeader));
1832 newmsg->transmit_cont = cont;
1833 newmsg->transmit_cont_cls = cont_cls;
1834 newmsg->timeout = GNUNET_TIME_relative_to_absolute(timeout);
1836 newmsg->timeout.abs_value = newmsg->timeout.abs_value - 500;
1838 newmsg->message_size = msgbuf_size + sizeof(struct WlanHeader);
1840 GNUNET_CONTAINER_DLL_insert_tail(session->pending_message_head, session->pending_message_tail, newmsg);
1843 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
1844 "New message for %p with size (incl wlan header) %u added\n", session,
1845 newmsg->message_size);
1849 queue_session(plugin, session);
1851 check_fragment_queue(plugin);
1852 //FIXME not the correct size
1858 * function to free a mac endpoint
1859 * @param plugin pointer to the plugin struct
1860 * @param endpoin pointer to the MacEndpoint to free
1863 free_macendpoint(struct Plugin * plugin, struct MacEndpoint * endpoint)
1865 struct Sessionqueue * sessions;
1866 struct Sessionqueue * sessions_next;
1867 GNUNET_assert(endpoint != NULL);
1869 sessions = endpoint->sessions_head;
1870 while (sessions != NULL)
1872 sessions_next = sessions->next;
1873 free_session(plugin, sessions, GNUNET_NO);
1874 sessions = sessions_next;
1877 GNUNET_CONTAINER_DLL_remove(plugin->mac_head,plugin->mac_tail,endpoint);
1878 if (endpoint->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1879 GNUNET_SCHEDULER_cancel(endpoint->timeout_task);
1880 GNUNET_free(endpoint);
1885 * function to free a session
1886 * @param plugin pointer to the plugin
1887 * @param queue pointer to the sessionqueue element to free
1888 * @param free_macendpoint if GNUNET_YES and mac endpoint would be empty, free mac endpoint
1891 free_session(struct Plugin * plugin, struct Sessionqueue * queue,
1892 int do_free_macendpoint)
1894 struct Sessionqueue * pendingsession;
1895 struct Sessionqueue * pendingsession_tmp;
1896 struct PendingMessage * pm;
1897 struct MacEndpoint * endpoint;
1898 struct FragmentMessage * fm;
1899 struct FragmentMessage * fmnext;
1902 GNUNET_assert(queue != NULL);
1903 GNUNET_assert(queue->content != NULL);
1906 //is this session pending for send
1907 pendingsession = plugin->pending_Sessions_head;
1908 while (pendingsession != NULL)
1910 pendingsession_tmp = pendingsession->next;
1911 if (pendingsession->content == queue->content)
1913 plugin->pendingsessions--;
1914 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1915 plugin->pending_Sessions_tail,
1917 GNUNET_free(pendingsession);
1919 GNUNET_assert (check == 0);
1922 pendingsession = pendingsession_tmp;
1925 endpoint = queue->content->mac;
1926 fm = endpoint->sending_messages_head;
1930 if (fm->session == queue->content)
1932 free_fragment_message(plugin, fm);
1937 // remove PendingMessage
1938 pm = queue->content->pending_message_head;
1941 GNUNET_CONTAINER_DLL_remove(queue->content->pending_message_head,queue->content->pending_message_tail,pm);
1942 GNUNET_free(pm->msg);
1944 pm = queue->content->pending_message_head;
1947 GNUNET_CONTAINER_DLL_remove(endpoint->sessions_head ,
1948 endpoint->sessions_tail,
1951 if (endpoint->sessions_head == NULL && do_free_macendpoint == GNUNET_YES)
1953 free_macendpoint(plugin, endpoint);
1956 if (queue->content->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1957 GNUNET_SCHEDULER_cancel(queue->content->timeout_task);
1959 check_fragment_queue(plugin);
1963 * Function that can be used to force the plugin to disconnect
1964 * from the given peer and cancel all previous transmissions
1965 * (and their continuation).
1967 * @param cls closure
1968 * @param target peer from which to disconnect
1971 wlan_plugin_disconnect(void *cls, const struct GNUNET_PeerIdentity *target)
1973 struct Plugin *plugin = cls;
1974 struct Sessionqueue * queue;
1975 struct Sessionqueue * queue_next;
1976 struct MacEndpoint * endpoint = plugin->mac_head;
1977 struct MacEndpoint * endpoint_next;
1978 // just look at all the session for the needed one
1979 while (endpoint != NULL)
1981 queue = endpoint->sessions_head;
1982 endpoint_next = endpoint->next;
1983 while (queue != NULL)
1985 // content is never NULL
1986 GNUNET_assert (queue->content != NULL);
1987 queue_next = queue->next;
1988 if (memcmp(target, &(queue->content->target),
1989 sizeof(struct GNUNET_PeerIdentity)) == 0)
1991 free_session(plugin, queue, GNUNET_YES);
1996 endpoint = endpoint_next;
2001 * Convert the transports address to a nice, human-readable
2004 * @param cls closure
2005 * @param type name of the transport that generated the address
2006 * @param addr one of the addresses of the host, NULL for the last address
2007 * the specific address format depends on the transport
2008 * @param addrlen length of the address
2009 * @param numeric should (IP) addresses be displayed in numeric form?
2010 * @param timeout after how long should we give up?
2011 * @param asc function to call on each string
2012 * @param asc_cls closure for asc
2015 wlan_plugin_address_pretty_printer(void *cls, const char *type,
2016 const void *addr, size_t addrlen, int numeric,
2017 struct GNUNET_TIME_Relative timeout,
2018 GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
2021 const unsigned char * input;
2023 //GNUNET_assert(cls !=NULL);
2026 /* invalid address (MAC addresses have 6 bytes) */GNUNET_break (0);
2030 input = (const unsigned char*) addr;
2031 GNUNET_snprintf(ret, sizeof(ret),
2032 "%s Mac-Address %.2X:%.2X:%.2X:%.2X:%.2X:%.2X", PROTOCOL_PREFIX,
2033 input[0], input[1], input[2], input[3], input[4], input[5]);
2038 * handels the data after all fragments are put together
2040 * @param session_light
2041 * @param hdr pointer to the data
2044 wlan_data_message_handler(void *cls, const struct GNUNET_MessageHeader *hdr)
2046 struct MacEndpoint * endpoint = (struct MacEndpoint *) cls;
2047 struct Plugin * plugin = endpoint->plugin;
2048 struct WlanHeader * wlanheader;
2049 struct Session * session;
2050 //const char * tempmsg;
2051 const struct GNUNET_MessageHeader * temp_hdr;
2052 struct GNUNET_PeerIdentity tmpsource;
2055 if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_DATA)
2059 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2060 "Func wlan_data_message_handler got GNUNET_MESSAGE_TYPE_WLAN_DATA size: %u\n",
2064 if (ntohs(hdr->size) < sizeof(struct WlanHeader)
2065 + sizeof(struct GNUNET_MessageHeader))
2067 //packet not big enought
2071 wlanheader = (struct WlanHeader *) hdr;
2073 session = search_session(plugin, endpoint, &wlanheader->source);
2075 //tempmsg = (char*) &wlanheader[1];
2076 temp_hdr = (const struct GNUNET_MessageHeader *) &wlanheader[1];
2077 crc = ntohl(wlanheader->crc);
2078 wlanheader->crc = 0;
2079 if (getcrc32((char *) wlanheader, ntohs(wlanheader->header.size)) != crc)
2081 //wrong crc, dispose message
2082 GNUNET_log_from(GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2083 "Wlan message header crc was wrong: %u != %u\n",
2084 getcrc32((char *) wlanheader, ntohs(wlanheader->header.size)),
2086 hexdump((void *) hdr, ntohs(hdr->size));
2090 //if not in session list
2091 if (session == NULL)
2094 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2095 "WLAN client not in session list: packet size = %u, inner size = %u, header size = %u\n",
2096 ntohs(wlanheader->header.size), ntohs(temp_hdr->size),
2097 sizeof(struct WlanHeader));
2099 //try if it is a hello message
2100 if (ntohs(wlanheader->header.size) >= ntohs(temp_hdr->size)
2101 + sizeof(struct WlanHeader))
2103 if (ntohs(temp_hdr->type) == GNUNET_MESSAGE_TYPE_HELLO)
2105 if (GNUNET_HELLO_get_id(
2106 (const struct GNUNET_HELLO_Message *) temp_hdr,
2107 &tmpsource) == GNUNET_OK)
2109 session = create_session(plugin, endpoint, &tmpsource);
2113 GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2114 "WLAN client not in session list and hello message is not okay\n");
2121 GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2122 "WLAN client not in session list and not a hello message\n");
2128 GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2129 "WLAN client not in session list and message size in does not fit\npacket size = %u, inner size = %u, header size = %u\n",
2130 ntohs(wlanheader->header.size), ntohs(temp_hdr->size),
2131 sizeof(struct WlanHeader));
2136 //"receive" the message
2138 if (memcmp(&wlanheader->source, &session->target,
2139 sizeof(struct GNUNET_PeerIdentity)) != 0)
2143 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2144 "WLAN peer source id doesn't match packet peer source id: session %p\n",
2150 if (memcmp(&wlanheader->target, plugin->env->my_identity,
2151 sizeof(struct GNUNET_PeerIdentity)) != 0)
2155 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2156 "WLAN peer target id doesn't match our peer id: session %p\n",
2162 GNUNET_SERVER_mst_receive(plugin->data_tokenizer, session,
2163 (const char *) temp_hdr,
2164 ntohs(hdr->size) - sizeof(struct WlanHeader), GNUNET_YES, GNUNET_NO);
2170 GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, PLUGIN_LOG_NAME,
2171 "wlan_data_message_handler got wrong message type\n");
2177 * function to process the a message, give it to the higher layer
2178 * @param cls pointer to the plugin
2179 * @param client pointer to the session this message belongs to
2180 * @param hdr start of the message
2182 //TODO ATS informations
2184 process_data(void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
2187 GNUNET_assert(client != NULL);
2188 GNUNET_assert(cls != NULL);
2189 struct Session * session = (struct Session *) client;
2190 struct Plugin * plugin = (struct Plugin *) cls;
2192 struct GNUNET_TRANSPORT_ATS_Information distance[2];
2193 distance[0].type = htonl(GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2194 distance[0].value = htonl(1);
2195 distance[1].type = htonl(GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2196 distance[1].value = htonl(0);
2199 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2200 "Calling plugin->env->receive for session %p; %s; size: %u\n", session,
2201 wlan_plugin_address_to_string(NULL, session->mac->addr.mac, 6),
2205 plugin->env->receive(plugin->env->cls, &(session->target), hdr,
2206 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance, 2, session,
2207 (const char*) &session->mac->addr, sizeof(session->mac->addr));
2211 * Function used for to process the data received from the wlan interface
2213 * @param cls the plugin handle
2214 * @param session_light FIXME: document
2215 * @param hdr hdr of the GNUNET_MessageHeader
2218 wlan_data_helper(void *cls, struct Session_light * session_light,
2219 const struct GNUNET_MessageHeader * hdr, const struct Radiotap_rx * rxinfo)
2221 struct Plugin *plugin = cls;
2222 struct FragmentMessage * fm;
2223 struct FragmentMessage * fm2;
2226 if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT)
2229 //TODO better DOS protection, error handling
2230 //TODO test first than create session
2231 GNUNET_assert(session_light != NULL);
2234 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2235 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT size: %u; %s\n",
2237 wlan_plugin_address_to_string(NULL, session_light->addr.mac, 6));
2240 if (session_light->macendpoint == NULL)
2242 session_light->macendpoint = get_macendpoint(plugin,
2243 &session_light->addr, GNUNET_NO);
2245 GNUNET_assert(GNUNET_HELLO_get_id(
2246 (const struct GNUNET_HELLO_Message *) &hdr[1],
2247 &(session_light->session->target) ) != GNUNET_SYSERR);
2253 else if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_FRAGMENT)
2256 GNUNET_assert(session_light != NULL);
2257 if (session_light->macendpoint == NULL)
2259 session_light->macendpoint = get_macendpoint(plugin,
2260 &session_light->addr, GNUNET_YES);
2264 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2265 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_FRAGMENT with size: %u; mac endpoint %p: %s\n",
2266 ntohs(hdr->size), session_light->macendpoint,
2267 wlan_plugin_address_to_string(NULL, session_light->addr.mac, 6));
2270 int ret = GNUNET_DEFRAGMENT_process_fragment(
2271 session_light->macendpoint->defrag, hdr);
2272 if (ret == GNUNET_NO)
2274 session_light->macendpoint->dups++;
2276 else if (ret == GNUNET_OK)
2278 session_light->macendpoint->fragc++;
2280 set_next_send(plugin);
2286 else if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_FRAGMENT_ACK)
2288 GNUNET_assert(session_light != NULL);
2289 if (session_light->macendpoint == NULL)
2291 session_light->macendpoint = get_macendpoint(plugin,
2292 &session_light->addr, GNUNET_NO);
2295 if (session_light->macendpoint == NULL)
2298 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2299 "Macendpoint does not exist for this GNUNET_MESSAGE_TYPE_FRAGMENT_ACK size: %u; %s\n",
2301 wlan_plugin_address_to_string(NULL, session_light->addr.mac, 6));
2307 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2308 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_FRAGMENT_ACK size: %u; mac endpoint: %p; %s\n",
2309 ntohs(hdr->size), session_light->macendpoint,
2310 wlan_plugin_address_to_string(NULL, session_light->addr.mac, 6));
2312 fm = session_light->macendpoint->sending_messages_head;
2316 int ret = GNUNET_FRAGMENT_process_ack(fm->fragcontext, hdr);
2317 if (ret == GNUNET_OK)
2319 #if DEBUG_wlan_retransmission
2320 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2321 "Got last ack, finished fragment message %p\n", fm);
2323 session_light->macendpoint->acks++;
2324 fm->session->last_activity = GNUNET_TIME_absolute_get();
2325 session_light->macendpoint->last_activity = fm->session->last_activity;
2326 free_fragment_message(plugin, fm);
2327 check_fragment_queue(plugin);
2330 if (ret == GNUNET_NO)
2332 #if DEBUG_wlan_retransmission
2333 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME, "Got ack for: %p\n", fm);
2335 session_light->macendpoint->acks++;
2338 if (ret == GNUNET_SYSERR)
2346 #if DEBUG_wlan_retransmission
2347 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2348 "WLAN fragment not in fragment list\n");
2356 GNUNET_log_from(GNUNET_ERROR_TYPE_INFO, PLUGIN_LOG_NAME,
2357 "WLAN packet inside the WLAN helper packet has not the right type: %u size: %u\n",
2358 ntohs(hdr->type), ntohs(hdr->size));
2364 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2365 "Helper finished\n");
2372 macprinter(const u_int8_t * mac)
2374 static char macstr[20];
2376 GNUNET_snprintf(macstr, sizeof(macstr), "%X:%X:%X:%X:%X:%X", mac[0], mac[1],
2377 mac[2], mac[3], mac[4], mac[5]);
2382 * Function for the scheduler if a mac endpoint times out
2383 * @param cls pointer to the MacEndpoint
2384 * @param tc pointer to the GNUNET_SCHEDULER_TaskContext
2387 macendpoint_timeout (void *cls,
2388 const struct GNUNET_SCHEDULER_TaskContext *tc){
2389 struct MacEndpoint * endpoint = cls;
2390 GNUNET_assert(endpoint != NULL);
2391 endpoint->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2392 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN){
2395 if (GNUNET_TIME_absolute_get_remaining(
2396 GNUNET_TIME_absolute_add(endpoint->last_activity, MACENDPOINT_TIMEOUT)).rel_value
2399 free_macendpoint(endpoint->plugin,endpoint);
2403 endpoint->timeout_task = GNUNET_SCHEDULER_add_delayed(MACENDPOINT_TIMEOUT,
2404 &macendpoint_timeout, endpoint);
2409 * function to create an macendpoint
2410 * @param plugin pointer to the plugin struct
2411 * @param addr pointer to the macaddress
2412 * @return returns a macendpoint
2414 static struct MacEndpoint *
2415 create_macendpoint(struct Plugin *plugin, const struct MacAddress *addr)
2417 struct MacEndpoint * newend = GNUNET_malloc(sizeof (struct MacEndpoint));
2418 newend->addr = *addr;
2419 newend->plugin = plugin;
2420 newend->addr = *addr;
2421 newend->fragment_messages_out_count = 0;
2422 newend->defrag = GNUNET_DEFRAGMENT_context_create(plugin->env->stats,
2423 WLAN_MTU, MESSAGES_IN_DEFRAG_QUEUE_PER_MAC, newend,
2424 &wlan_data_message_handler, &add_ack_for_send);
2425 newend->last_activity = GNUNET_TIME_absolute_get();
2426 newend->timeout_task = GNUNET_SCHEDULER_add_delayed(MACENDPOINT_TIMEOUT,
2427 &macendpoint_timeout, newend);
2429 plugin->mac_count++;
2430 GNUNET_CONTAINER_DLL_insert_tail(plugin->mac_head, plugin->mac_tail, newend);
2432 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME, "New Mac Endpoint %p: %s\n", newend,
2433 wlan_plugin_address_to_string(NULL, newend->addr.mac, 6));
2439 * Function used for to process the data from the suid process
2441 * @param cls the plugin handle
2442 * @param client client that send the data (not used)
2443 * @param hdr header of the GNUNET_MessageHeader
2446 wlan_process_helper(void *cls, void *client,
2447 const struct GNUNET_MessageHeader *hdr)
2449 struct Plugin *plugin = cls;
2450 struct ieee80211_frame * wlanIeeeHeader = NULL;
2451 struct Session_light * session_light = NULL;
2452 struct Radiotap_rx * rxinfo;
2453 const struct GNUNET_MessageHeader * temp_hdr = NULL;
2458 switch (ntohs(hdr->type))
2460 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA:
2462 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2463 "Func wlan_process_helper got GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA size: %u\n",
2467 //call wlan_process_helper with the message inside, later with wlan: analyze signal
2468 if (ntohs(hdr->size) < sizeof(struct ieee80211_frame)
2469 + sizeof(struct GNUNET_MessageHeader) + sizeof(struct Radiotap_rx))
2472 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2473 "Size of packet is too small; size: %u min size: %u\n",
2475 sizeof(struct ieee80211_frame)
2476 + sizeof(struct GNUNET_MessageHeader));
2479 /* FIXME: restart SUID process */
2482 rxinfo = (struct Radiotap_rx *) &hdr[1];
2483 wlanIeeeHeader = (struct ieee80211_frame *) &rxinfo[1];
2485 //process only if it is an broadcast or for this computer both with the gnunet bssid
2488 if (memcmp(&(wlanIeeeHeader->i_addr3), &mac_bssid,
2489 sizeof(struct MacAddress)) == 0)
2491 //check for broadcast or mac
2492 if (memcmp(&(wlanIeeeHeader->i_addr1), &bc_all_mac,
2493 sizeof(struct MacAddress) == 0) || memcmp(
2494 &(wlanIeeeHeader->i_addr1), &(plugin->mac_address),
2495 sizeof(struct MacAddress)) == 0)
2498 // process the inner data
2501 datasize = ntohs(hdr->size) - sizeof(struct ieee80211_frame)
2502 - sizeof(struct GNUNET_MessageHeader)
2503 - sizeof(struct Radiotap_rx);
2505 session_light = GNUNET_malloc(sizeof(struct Session_light));
2506 memcpy(&session_light->addr, &(wlanIeeeHeader->i_addr2),
2507 sizeof(struct MacAddress));
2508 //session_light->session = search_session(plugin,session_light->addr);
2511 while (pos < datasize)
2513 temp_hdr = (struct GNUNET_MessageHeader *) &wlanIeeeHeader[1]
2516 wlan_data_helper(plugin, session_light, temp_hdr, rxinfo);
2517 pos += ntohs(temp_hdr->size);
2522 GNUNET_free(session_light);
2527 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2528 "Func wlan_process_helper got wrong MAC: %s\n",
2529 macprinter(wlanIeeeHeader->i_addr1));
2536 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2537 "Func wlan_process_helper got wrong BSSID: %s\n",
2538 macprinter(wlanIeeeHeader->i_addr2));
2542 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
2543 //TODO more control messages
2544 //TODO use struct wlan_helper_control
2545 if (ntohs(hdr->size) != sizeof(struct Wlan_Helper_Control_Message))
2548 /* FIXME: restart SUID process */
2551 memcpy(&plugin->mac_address, &hdr[1], sizeof(struct MacAddress));
2553 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2554 "Received WLAN_HELPER_CONTROL message with transport of address %s\n",
2555 wlan_plugin_address_to_string(cls, &plugin->mac_address,
2556 sizeof(struct MacAddress)));
2558 plugin->env->notify_address(plugin->env->cls, GNUNET_YES,
2559 &plugin->mac_address, sizeof(struct MacAddress));
2568 * We have been notified that wlan-helper has written something to stdout.
2569 * Handle the output, then reschedule this function to be called again once
2570 * more is available.
2572 * @param cls the plugin handle
2573 * @param tc the scheduling context
2576 wlan_plugin_helper_read(void *cls,
2577 const struct GNUNET_SCHEDULER_TaskContext *tc)
2579 struct Plugin *plugin = cls;
2580 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2582 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2585 char mybuf[WLAN_MTU + sizeof(struct GNUNET_MessageHeader)];
2588 bytes = GNUNET_DISK_file_read(plugin->server_stdout_handle, mybuf,
2593 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2594 _("Finished reading from wlan-helper stdout with code: %d\n"),
2599 GNUNET_SERVER_mst_receive(plugin->suid_tokenizer, NULL, mybuf, bytes,
2600 GNUNET_NO, GNUNET_NO);
2602 GNUNET_assert(plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2603 plugin->server_read_task = GNUNET_SCHEDULER_add_read_file(
2604 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdout_handle,
2605 &wlan_plugin_helper_read, plugin);
2609 * Start the gnunet-wlan-helper process.
2611 * @param plugin the transport plugin
2612 * @param testmode should we use the dummy driver for testing?
2613 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
2616 wlan_transport_start_wlan_helper(struct Plugin *plugin, int testmode)
2618 const char * filenamehw = "gnunet-transport-wlan-helper";
2619 const char * filenameloopback = "gnunet-transport-wlan-helper-dummy";
2620 plugin->server_stdout = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_NO, GNUNET_YES);
2621 if (plugin->server_stdout == NULL)
2622 return GNUNET_SYSERR;
2624 plugin->server_stdin = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_NO);
2625 if (plugin->server_stdin == NULL)
2626 return GNUNET_SYSERR;
2628 /* Start the server process */
2634 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2635 "Starting gnunet-wlan-helper process cmd: %s %s %i\n", filenamehw,
2636 plugin->interface, testmode);
2639 plugin->server_proc = GNUNET_OS_start_process(plugin->server_stdin,
2640 plugin->server_stdout, filenamehw, filenamehw, plugin->interface,
2643 else if (testmode == 1)
2647 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2648 "Starting gnunet-wlan-helper loopback 1 process cmd: %s %s %i\n",
2649 filenameloopback, plugin->interface, testmode);
2652 plugin->server_proc = GNUNET_OS_start_process(plugin->server_stdin,
2653 plugin->server_stdout, filenameloopback, filenameloopback, "1", NULL);
2655 else if (testmode == 2)
2658 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2659 "Starting gnunet-wlan-helper loopback 2 process cmd: %s %s %i\n",
2660 filenameloopback, plugin->interface, testmode);
2662 plugin->server_proc = GNUNET_OS_start_process(plugin->server_stdin,
2663 plugin->server_stdout, filenameloopback, filenameloopback, "2", NULL);
2665 if (plugin->server_proc == NULL)
2668 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2669 "Failed to start gnunet-wlan-helper process\n");
2671 return GNUNET_SYSERR;
2674 /* Close the write end of the read pipe */
2675 GNUNET_DISK_pipe_close_end(plugin->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
2677 /* Close the read end of the write pipe */
2678 GNUNET_DISK_pipe_close_end(plugin->server_stdin, GNUNET_DISK_PIPE_END_READ);
2680 plugin->server_stdout_handle = GNUNET_DISK_pipe_handle(plugin->server_stdout,
2681 GNUNET_DISK_PIPE_END_READ);
2682 plugin->server_stdin_handle = GNUNET_DISK_pipe_handle(plugin->server_stdin,
2683 GNUNET_DISK_PIPE_END_WRITE);
2685 GNUNET_assert(plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2688 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2689 "Adding server_read_task for the wlan-helper\n");
2692 plugin->server_read_task = GNUNET_SCHEDULER_add_read_file(
2693 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdout_handle,
2694 &wlan_plugin_helper_read, plugin);
2700 * Exit point from the plugin.
2701 * @param cls pointer to the api struct
2706 libgnunet_plugin_transport_wlan_done(void *cls)
2708 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2709 struct Plugin *plugin = api->cls;
2710 struct MacEndpoint * endpoint = plugin->mac_head;
2711 struct MacEndpoint * endpoint_next;
2714 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME,
2715 "libgnunet_plugin_transport_wlan_done started\n");
2718 GNUNET_DISK_pipe_close(plugin->server_stdout);
2719 GNUNET_DISK_pipe_close(plugin->server_stdin);
2720 GNUNET_OS_process_kill(plugin->server_proc, 9);
2721 GNUNET_OS_process_close(plugin->server_proc);
2723 GNUNET_assert (cls !=NULL);
2725 while (endpoint != NULL)
2727 endpoint_next = endpoint->next;
2728 free_macendpoint(plugin, endpoint);
2729 endpoint = endpoint_next;
2732 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
2734 GNUNET_SCHEDULER_cancel(plugin->server_write_delay_task);
2735 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
2737 if (plugin->server_write_task != GNUNET_SCHEDULER_NO_TASK)
2739 GNUNET_SCHEDULER_cancel(plugin->server_write_task);
2740 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
2742 if (plugin->server_read_task != GNUNET_SCHEDULER_NO_TASK)
2744 GNUNET_SCHEDULER_cancel(plugin->server_read_task);
2745 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2748 if (plugin->suid_tokenizer != NULL)
2749 GNUNET_SERVER_mst_destroy(plugin->suid_tokenizer);
2751 if (plugin->data_tokenizer != NULL)
2752 GNUNET_SERVER_mst_destroy(plugin->data_tokenizer);
2754 GNUNET_free_non_null(plugin->interface);
2755 GNUNET_free (plugin);
2761 * Entry point for the plugin.
2763 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2764 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2767 libgnunet_plugin_transport_wlan_init(void *cls)
2769 //struct GNUNET_SERVICE_Context *service;
2770 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2771 struct GNUNET_TRANSPORT_PluginFunctions *api;
2772 struct Plugin *plugin;
2773 static unsigned long long testmode = 0;
2775 GNUNET_assert(cls !=NULL);
2777 plugin = GNUNET_malloc (sizeof (struct Plugin));
2779 plugin->pendingsessions = 0;
2780 plugin->mac_count = 0;
2781 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
2782 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2783 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
2784 GNUNET_BANDWIDTH_tracker_init(&plugin->tracker,
2785 GNUNET_BANDWIDTH_value_init(100 * 1024 * 1024 / 8), 100);
2787 plugin->suid_tokenizer = GNUNET_SERVER_mst_create(&wlan_process_helper,
2790 plugin->data_tokenizer = GNUNET_SERVER_mst_create(&process_data, plugin);
2792 //plugin->sessions = GNUNET_malloc (sizeof (struct Sessionqueue));
2793 //plugin->pending_Sessions_head = GNUNET_malloc (sizeof (struct Sessionqueue));
2795 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2797 api->send = &wlan_plugin_send;
2798 api->disconnect = &wlan_plugin_disconnect;
2799 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2800 api->check_address = &wlan_plugin_address_suggested;
2801 api->address_to_string = &wlan_plugin_address_to_string;
2804 if (GNUNET_CONFIGURATION_have_value(env->cfg, "transport-wlan", "TESTMODE"))
2806 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(env->cfg,
2807 "transport-wlan", "TESTMODE", &testmode))
2808 testmode = 0; //default value
2811 if (GNUNET_CONFIGURATION_have_value(env->cfg, "transport-wlan", "INTERFACE"))
2813 if (GNUNET_CONFIGURATION_get_value_string(env->cfg, "transport-wlan",
2814 "INTERFACE", &(plugin->interface)) != GNUNET_YES)
2816 libgnunet_plugin_transport_wlan_done(api);
2822 set_next_beacon_time(plugin);
2824 wlan_transport_start_wlan_helper(plugin, testmode);
2827 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, PLUGIN_LOG_NAME, "wlan init finished\n");
2833 /* end of plugin_transport_wlan.c */