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 "wlan/ieee80211.h"
41 #define PROTOCOL_PREFIX "wlan"
44 * Max size of packet from helper
49 * Time until retransmission of a fragment in ms
51 #define FRAGMENT_TIMEOUT GNUNET_TIME_UNIT_SECONDS
53 #define FRAGMENT_QUEUE_SIZE 10
54 #define FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION 1
56 #define MESSAGE_IN_TIMEOUT GNUNET_TIME_UNIT_SECONDS
58 #define MESSAGES_IN_QUEUE_SIZE 10
59 #define MESSAGES_IN_QUEUE_PER_SESSION 1
61 #define HALLO_BEACON_SCALING_FACTOR 900
63 #define DEBUG_wlan GNUNET_NO
64 #define DEBUG_wlan_retransmission GNUNET_NO
66 #define MESSAGE_LENGHT_UNKNOWN -1
67 //#define NO_MESSAGE_OR_MESSAGE_FINISHED -2
70 * After how long do we expire an address that we
71 * learned from another peer if it is not reconfirmed
74 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
77 * Initial handshake message for a session.
82 * Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME.
84 struct GNUNET_MessageHeader header;
87 * Identit*mac_y of the node connecting (TCP client)
89 struct GNUNET_PeerIdentity clientIdentity;
94 * Encapsulation of all of the state of the plugin.
101 struct GNUNET_TRANSPORT_PluginEnvironment *env;
104 * List of open sessions. head
106 struct Sessionqueue *sessions;
109 * List of open sessions. tail
111 struct Sessionqueue *sessions_tail;
116 unsigned int session_count;
119 * encapsulation of data from the local wlan helper program
121 struct GNUNET_SERVER_MessageStreamTokenizer * suid_tokenizer;
124 * encapsulation of packets received from the wlan helper
126 struct GNUNET_SERVER_MessageStreamTokenizer * data_tokenizer;
128 * encapsulation of packets received
130 struct GNUNET_SERVER_MessageStreamTokenizer * fragment_tokenizer;
133 * stdout pipe handle for the gnunet-wlan-helper process
135 struct GNUNET_DISK_PipeHandle *server_stdout;
138 * stdout file handle for the gnunet-wlan-helper process
140 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
143 * stdin pipe handle for the gnunet-wlan-helper process
145 struct GNUNET_DISK_PipeHandle *server_stdin;
148 * stdin file handle for the gnunet-wlan-helper process
150 const struct GNUNET_DISK_FileHandle *server_stdin_handle;
153 * ID of the gnunet-wlan-server std read task
155 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
158 * ID of the gnunet-wlan-server std read task
160 GNUNET_SCHEDULER_TaskIdentifier server_write_task;
163 * ID of the delay task for writing
165 GNUNET_SCHEDULER_TaskIdentifier server_write_delay_task;
168 * The process id of the wlan process
170 struct GNUNET_OS_Process *server_proc;
173 * The interface of the wlan card given to us by the user.
178 * The mac_address of the wlan card given to us by the helper.
180 struct MacAddress mac_address;
183 * Sessions currently pending for transmission
184 * to this peer, if any.
186 struct Sessionqueue * pending_Sessions_head;
189 * Sessions currently pending for transmission
190 * to this peer (tail), if any.
192 struct Sessionqueue * pending_Sessions_tail;
195 * number of pending sessions
197 unsigned int pendingsessions;
200 * Messages in the fragmentation queue, head
202 struct GNUNET_CONTAINER_Heap * pending_Fragment_Messages;
205 * Messages in the in Queue, head
207 struct Receive_Message_Queue * receive_messages_head;
210 * Messages in the in Queue, tail
212 struct Receive_Message_Queue * receive_messages_teil;
215 * number of messages in the in queue
217 unsigned int pending_receive_messages;
220 * time of the next "hello-beacon"
222 struct GNUNET_TIME_Absolute beacon_time;
225 * queue to send acks for received fragments (head)
227 struct AckSendQueue * ack_send_queue_head;
230 * queue to send acks for received fragments (tail)
232 struct AckSendQueue * ack_send_queue_tail;
237 * Struct to store data if file write did not accept the whole packet
241 struct Plugin * plugin;
243 struct GNUNET_MessageHeader * msgstart;
245 struct GNUNET_TIME_Absolute next_send;
249 * Queue of sessions, for the general session queue and the pending session queue
254 struct Sessionqueue * next;
255 struct Sessionqueue * prev;
256 struct Session * content;
260 * Queue for the fragments received
263 struct Receive_Fragment_Queue
265 struct Receive_Fragment_Queue * next;
266 struct Receive_Fragment_Queue * prev;
270 struct Radiotap_rx rxinfo;
274 struct Session_id_fragment_triple
276 struct Session * session;
278 struct FragmentMessage * fm;
282 struct Plugin_Session_pair
284 struct Plugin * plugin;
285 struct Session * session;
289 * Queue for the fragments received
291 struct Receive_Message_Queue
293 struct Receive_Message_Queue * next;
295 struct Receive_Message_Queue * prev;
298 * current number for message incoming, to distinguish between the messages
300 uint32_t message_id_in;
303 * size of the message received,
304 * MESSAGE_LENGHT_UNKNOWN means that the size is not known,
305 * NO_MESSAGE_OR_MESSAGE_FINISHED means no message received
310 * Sorted queue with the fragments received; head
313 struct Receive_Fragment_Queue * frag_head;
316 * Sorted queue with the fragments received; tail
319 struct Receive_Fragment_Queue * frag_tail;
322 * Session this fragment belongs to
325 struct Session * session;
328 * Timeout value for the pending message.
330 struct GNUNET_TIME_Absolute timeout;
333 * Bitfield of received fragments
336 uint64_t received_fragments;
340 * Information kept for each message that is yet to
343 struct PendingMessage
347 * The pending message
352 * Size of the message
357 * Continuation function to call once the message
358 * has been sent. Can be NULL if there is no
359 * continuation to call.
361 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
364 * Cls for transmit_cont
366 void * transmit_cont_cls;
369 * Timeout value for the pending message.
371 struct GNUNET_TIME_Absolute timeout;
376 * Queue for acks to send for fragments recived
382 * next ack in the ack send queue
384 struct AckSendQueue * next;
386 * previous ack in the ack send queue
388 struct AckSendQueue * prev;
390 * pointer to the session this ack belongs to
392 struct Session * session;
394 * ID of message, to distinguish between the messages, picked randomly.
399 * Bit field for received fragments
401 uint64_t fragments_field;
406 * Session infos gathered from a messages
412 * the session this message belongs to
414 struct Session * session;
418 struct MacAddress addr;
422 * Session handle for connections.
430 struct SessionHeader header;
433 * Pointer to the global plugin struct.
435 struct Plugin *plugin;
438 * Message currently pending for transmission
439 * to this peer, if any.
441 struct PendingMessage *pending_message;
444 * Message currently pending for transmission
445 * to this peer, if any.
447 struct PendingMessage *pending_message2;
450 * To whom are we talking to (set to our identity
451 * if we are still waiting for the welcome message)
453 struct GNUNET_PeerIdentity target;
458 struct MacAddress addr;
461 * Address of the other peer (either based on our 'connect'
462 * call or on our 'accept' call).
467 * Last activity on this connection. Used to select preferred
470 struct GNUNET_TIME_Absolute last_activity;
473 * count of messages in the fragment out queue for this session
476 int fragment_messages_out_count;
479 * count of messages in the fragment in queue for this session
482 int fragment_messages_in_count;
492 * Struct for Messages in the fragment queue
495 struct FragmentMessage
498 * heap pointer of this message
500 struct GNUNET_CONTAINER_HeapNode * node;
503 * Session this message belongs to
506 struct Session *session;
509 * This is a doubly-linked list.
511 struct FragmentMessage *next;
514 * This is a doubly-linked list.
516 struct FragmentMessage *prev;
519 * The pending message
524 * 0 if not in ack queue
531 * Timeout value for the pending message.
533 struct GNUNET_TIME_Absolute timeout;
536 * Timeout value for the pending fragments.
537 * Stores the time when the next msg fragment ack has to be received
539 struct GNUNET_TIME_Absolute next_ack;
542 * bitfield with all acks received for this message
544 uint64_t ack_bitfield;
547 * Size of the message
552 * pos / next fragment number in the message, for fragmentation/segmentation,
553 * some acks can be missing but there is still time
555 uint32_t message_pos;
558 * current number for message outgoing, to distinguish between the messages
560 uint32_t message_id_out;
564 * Header for messages which need fragmentation
566 struct FragmentationAckHeader
569 struct GNUNET_MessageHeader header;
572 * ID of message, to distinguish between the messages, picked randomly.
574 uint32_t message_id GNUNET_PACKED;
577 * Offset or number of this fragment, for fragmentation/segmentation (design choice, TBD)
579 uint64_t fragment_field GNUNET_PACKED;
584 do_transmit(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
587 * Sets a bit active in the bitArray. Increment bit-specific
588 * usage counter on disk only if below 4bit max (==15).
590 * @param bitArray memory area to set the bit in
591 * @param bitIdx which bit to set
594 setBit(char *bitArray, unsigned int bitIdx)
597 unsigned int targetBit;
599 arraySlot = bitIdx / 8;
600 targetBit = (1L << (bitIdx % 8));
601 bitArray[arraySlot] |= targetBit;
605 * Checks if a bit is active in the bitArray
607 * @param bitArray memory area to set the bit in
608 * @param bitIdx which bit to test
609 * @return GNUNET_YES if the bit is set, GNUNET_NO if not.
612 testBit(char *bitArray, unsigned int bitIdx)
615 unsigned int targetBit;
618 targetBit = (1L << (bitIdx % 8));
619 if (bitArray[slot] & targetBit)
625 * get the next message number, at the moment just a random one
626 * @return returns the next valid message-number for sending packets
629 get_next_message_id()
631 return GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
635 * search for a session with the addr
637 * @param plugin pointer to the plugin struct
638 * @param addr pointer to the mac address of the peer
639 * @return returns the session
641 static struct Session *
642 search_session(struct Plugin *plugin, const struct MacAddress *addr)
644 struct Sessionqueue * queue = plugin->sessions;
646 while (queue != NULL)
648 GNUNET_assert (queue->content != NULL);
649 if (memcmp(addr, &queue->content->addr, sizeof(struct MacAddress)) == 0)
650 return queue->content; /* session found */
657 * Function called for a quick conversion of the binary address to
658 * a numeric address. Note that the caller must not free the
659 * address and that the next call to this function is allowed
660 * to override the address again.
663 * @param addr binary address
664 * @param addrlen length of the address
665 * @return string representing the same address
668 wlan_plugin_address_to_string(void *cls, const void *addr, size_t addrlen)
671 const struct MacAddress *mac;
673 if (addrlen != sizeof(struct MacAddress))
679 GNUNET_snprintf(ret, sizeof(ret), "%s Mac-Address %X:%X:%X:%X:%X:%X",
680 PROTOCOL_PREFIX, mac->mac[0], mac->mac[1], mac->mac[2], mac->mac[3],
681 mac->mac[4], mac->mac[5]);
686 * create a new session
688 * @param plugin pointer to the plugin struct
689 * @param addr pointer to the mac address of the peer
690 * @return returns the session
693 static struct Session *
694 create_session(struct Plugin *plugin, const struct MacAddress * addr)
696 struct Sessionqueue * queue = GNUNET_malloc (sizeof (struct Sessionqueue));
698 GNUNET_CONTAINER_DLL_insert_tail(plugin->sessions, plugin->sessions_tail, queue);
700 queue->content = GNUNET_malloc (sizeof (struct Session));
701 queue->content->plugin = plugin;
702 queue->content->addr = *addr;
703 queue->content->fragment_messages_out_count = 0;
704 queue->content->fragment_messages_in_count = 0;
706 plugin->session_count++;
709 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "New session %p with %s\n",
710 queue->content, wlan_plugin_address_to_string(NULL, addr, 6));
713 return queue->content;
717 * Get session from address, create if no session exists
719 * @param plugin pointer to the plugin struct
720 * @param addr pointer to the mac address of the peer
721 * @return returns the session
723 static struct Session *
724 get_session(struct Plugin *plugin, const struct MacAddress *addr)
726 struct Session * session = search_session(plugin, addr);
729 return create_session(plugin, addr);
733 * Queue the session to send data
734 * checks if there is a message pending
735 * checks if this session is not allready in the queue
736 * @param plugin pointer to the plugin
737 * @param session pointer to the session to add
740 queue_session(struct Plugin *plugin, struct Session * session)
742 struct Sessionqueue * queue = plugin->pending_Sessions_head;
744 if (session->pending_message != NULL)
746 while (queue != NULL)
748 // content is never NULL
749 GNUNET_assert (queue->content != NULL);
750 // is session already in queue?
751 if (session == queue->content)
759 // Session is not in the queue
761 queue = GNUNET_malloc (sizeof (struct Sessionqueue));
762 queue->content = session;
765 GNUNET_CONTAINER_DLL_insert_tail(plugin->pending_Sessions_head,
766 plugin->pending_Sessions_tail, queue);
767 plugin->pendingsessions++;
773 * Function to schedule the write task, executed after a delay
774 * @param cls pointer to the plugin struct
775 * @param tc GNUNET_SCHEDULER_TaskContext pointer
778 delay_fragment_task(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
780 struct Plugin * plugin = cls;
781 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
783 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
786 // GNUNET_TIME_UNIT_FOREVER_REL is needed to clean up old msg
787 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
789 plugin->server_write_task = GNUNET_SCHEDULER_add_write_file(
790 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdin_handle,
791 &do_transmit, plugin);
796 * Function to calculate the time of the next periodic "hello-beacon"
797 * @param plugin pointer to the plugin struct
801 set_next_beacon_time(struct Plugin * const plugin)
803 //under 10 known peers: once a second
804 if (plugin->session_count < 10)
806 plugin->beacon_time = GNUNET_TIME_absolute_add(
807 GNUNET_TIME_absolute_get(), GNUNET_TIME_relative_multiply(
808 GNUNET_TIME_UNIT_SECONDS, HALLO_BEACON_SCALING_FACTOR));
810 //under 30 known peers: every 10 seconds
811 else if (plugin->session_count < 30)
813 plugin->beacon_time = GNUNET_TIME_absolute_add(
814 GNUNET_TIME_absolute_get(), GNUNET_TIME_relative_multiply(
815 GNUNET_TIME_UNIT_SECONDS, 10 * HALLO_BEACON_SCALING_FACTOR));
817 //over 30 known peers: once a minute
820 plugin->beacon_time = GNUNET_TIME_absolute_add(
821 GNUNET_TIME_absolute_get(), GNUNET_TIME_relative_multiply(
822 GNUNET_TIME_UNIT_MINUTES, HALLO_BEACON_SCALING_FACTOR));
827 * Function to get the timeout value for acks for this session
828 * @param fm pointer to the FragmentMessage to get the next timeout
829 * @return time until the next ack should be received, in GNUNET_TIME_Relative
831 static struct GNUNET_TIME_Relative
832 get_ack_timeout(struct FragmentMessage * fm)
834 return FRAGMENT_TIMEOUT;
838 * Function to set the timer for the next timeout of the fragment queue
839 * @param plugin the handle to the plugin struct
843 set_next_send(struct Plugin * const plugin)
845 struct FragmentMessage * fm;
846 struct GNUNET_TIME_Relative next_send;
847 struct GNUNET_TIME_Absolute next_send_tmp;
850 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
852 GNUNET_SCHEDULER_cancel(plugin->server_write_delay_task);
853 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
856 fm = GNUNET_CONTAINER_heap_peek(plugin->pending_Fragment_Messages);
858 //check if some acks are in the queue
859 if (plugin->ack_send_queue_head != NULL)
861 next_send = GNUNET_TIME_UNIT_ZERO;
864 //check if there are some fragments in the queue
867 next_send = GNUNET_TIME_absolute_get_remaining(plugin->beacon_time);
870 next_send_tmp.abs_value = GNUNET_CONTAINER_heap_node_get_cost(
872 next_send = GNUNET_TIME_relative_min(next_send,
873 GNUNET_TIME_absolute_get_remaining(next_send_tmp));
880 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Next packet is send in: %u\n",
881 next_send.rel_value);
885 if (next_send.rel_value == GNUNET_TIME_UNIT_ZERO.rel_value)
887 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
889 plugin->server_write_task = GNUNET_SCHEDULER_add_write_file(
890 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdin_handle,
891 &do_transmit, plugin);
896 plugin->server_write_delay_task = GNUNET_SCHEDULER_add_delayed(next_send,
897 &delay_fragment_task, plugin);
902 * Function to get the next queued Session, removes the session from the queue
903 * @param plugin pointer to the plugin struct
904 * @return pointer to the session found, returns NULL if there is now session in the queue
906 static struct Session *
907 get_next_queue_session(struct Plugin * plugin)
909 struct Session * session;
910 struct Sessionqueue * sessionqueue;
911 struct Sessionqueue * sessionqueue_alt;
912 struct PendingMessage * pm;
913 sessionqueue = plugin->pending_Sessions_head;
915 while (sessionqueue != NULL)
917 session = sessionqueue->content;
919 GNUNET_assert(session != NULL);
920 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
921 "Found session %p from pending session queue\n", session
923 pm = session->pending_message;
929 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
930 "pending message is empty, should not happen. session %p\n",
935 GNUNET_assert(pm != NULL);
937 //check for message timeout
938 if (GNUNET_TIME_absolute_get_remaining(pm->timeout).rel_value > 0)
940 //check if session has no message in the fragment queue
941 if (session->fragment_messages_out_count
942 < FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION)
944 plugin->pendingsessions--;
945 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
946 plugin->pending_Sessions_tail, sessionqueue);
947 GNUNET_free(sessionqueue);
953 sessionqueue = sessionqueue->next;
959 session->pending_message = session->pending_message2;
960 session->pending_message2 = NULL;
962 //call the cont func that it did not work
963 if (pm->transmit_cont != NULL)
964 pm->transmit_cont(pm->transmit_cont_cls, &(session->target),
966 GNUNET_free(pm->msg);
969 if (session->pending_message == NULL)
971 sessionqueue_alt = sessionqueue;
972 sessionqueue = sessionqueue->next;
973 plugin->pendingsessions--;
974 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
975 plugin->pending_Sessions_tail, sessionqueue_alt);
977 GNUNET_free(sessionqueue_alt);
987 * frees the space of a message in the fragment queue (send queue)
988 * @param plugin the plugin struct
989 * @param fm message to free
992 free_fragment_message(struct Plugin * plugin, struct FragmentMessage * fm)
994 struct Session * session = fm->session;
998 (session->fragment_messages_out_count)--;
999 GNUNET_free_non_null(fm->msg);
1000 GNUNET_CONTAINER_heap_remove_node(fm->node);
1003 queue_session(plugin, session);
1005 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1006 "free pending fragment messages, pending messages remaining %u\n",
1007 GNUNET_CONTAINER_heap_get_size(plugin->pending_Fragment_Messages));
1013 * Function to check if there is some space in the fragment queue
1014 * inserts a message if space is available
1015 * @param plugin the plugin struct
1019 check_fragment_queue(struct Plugin * plugin)
1021 struct Session * session;
1022 struct FragmentMessage * fm;
1023 struct GNUNET_PeerIdentity pid;
1025 struct PendingMessage * pm;
1027 if (GNUNET_CONTAINER_heap_get_size(plugin->pending_Fragment_Messages)
1028 < FRAGMENT_QUEUE_SIZE)
1030 session = get_next_queue_session(plugin);
1031 if (session != NULL)
1033 pm = session->pending_message;
1034 session->pending_message = NULL;
1035 session->fragment_messages_out_count++;
1036 GNUNET_assert(pm != NULL);
1038 fm = GNUNET_malloc(sizeof(struct FragmentMessage));
1039 fm->message_size = pm->message_size;
1041 fm->session = session;
1042 fm->timeout.abs_value = pm->timeout.abs_value;
1043 fm->message_pos = 0;
1044 fm->next_ack = GNUNET_TIME_absolute_get();
1045 fm->message_id_out = get_next_message_id();
1046 fm->ack_bitfield = 0;
1047 fm->node = GNUNET_CONTAINER_heap_insert(
1048 plugin->pending_Fragment_Messages, fm,
1049 GNUNET_TIME_absolute_get().abs_value);
1051 GNUNET_assert(session !=NULL);
1053 if (pm->transmit_cont != NULL)
1055 pid = session->target;
1056 pm->transmit_cont(pm->transmit_cont_cls, &pid, GNUNET_OK);
1058 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1059 "called pm->transmit_cont for %p\n", session);
1065 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1066 "no pm->transmit_cont for %p\n", session);
1071 if (session->pending_message2 != NULL)
1073 session->pending_message = session->pending_message2;
1074 session->pending_message2 = NULL;
1076 queue_session(plugin, session);
1082 //check if timeout changed
1083 set_next_send(plugin);
1087 * Funktion to check if all fragments where send and the acks received
1088 * frees the space if finished
1089 * @param plugin the plugin struct
1090 * @param fm the message to check
1093 check_finished_fragment(struct Plugin * plugin, struct FragmentMessage * fm)
1095 //maxack = size of message / max packet size, eg 12 / 5 = 2 start at 0 so ack numbers are 0,1,2
1096 unsigned int maxack = 63 - ((fm->message_size - 1) / (WLAN_MTU
1097 - sizeof(struct FragmentationHeader)));
1098 uint64_t tmpfield = UINT64_MAX;
1099 tmpfield = tmpfield >> maxack;
1103 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1104 "Test bitfields %X and %X, maxack is %u, fm size %u\n",
1105 fm->ack_bitfield, tmpfield, maxack, fm->message_size);
1108 if (fm->ack_bitfield == tmpfield)
1111 #if DEBUG_wlan_retransmission
1112 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1113 "Finished sending and got all acks; message_id %u\n",
1114 fm->message_id_out);
1117 free_fragment_message(plugin, fm);
1119 check_fragment_queue(plugin);
1125 * Function to set the next fragment number
1126 * @param fm use this FragmentMessage
1130 set_next_message_fragment_pos(struct Plugin * plugin,
1131 struct FragmentMessage * fm)
1136 //check if retransmit is needed
1137 if (GNUNET_TIME_absolute_get_remaining(fm->next_ack).rel_value == 0)
1140 // be positive and try again later :-D
1141 fm->next_ack = GNUNET_TIME_relative_to_absolute(get_ack_timeout(fm));
1142 // find first missing fragment
1144 fm->message_pos = 0;
1146 GNUNET_CONTAINER_heap_update_cost(plugin->pending_Fragment_Messages,
1149 #if DEBUG_wlan_retransmission
1150 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1151 "Retransmit; message_id %u; fragment number %i, size: %u\n",
1152 fm->message_id_out, fm->message_pos, fm->message_size);
1156 //test if ack 0 (or X) was already received
1157 while (testBit((char*) &fm->ack_bitfield, fm->message_pos) == GNUNET_YES)
1166 getRadiotapHeader(struct Plugin * plugin, struct Session * session,
1167 struct Radiotap_Send * header)
1170 if (session != NULL)
1172 header->rate = session->rate;
1173 header->tx_power = session->tx_power;
1174 header->antenna = session->antenna;
1179 header->tx_power = 0;
1180 header->antenna = 0;
1187 * function to generate the wlan hardware header for one packet
1188 * @param Header address to write the header to
1189 * @param to_mac_addr address of the recipient
1190 * @param plugin pointer to the plugin struct
1191 * @return GNUNET_YES if there was no error
1194 getWlanHeader(struct ieee80211_frame * Header,
1195 const struct MacAddress * to_mac_addr, struct Plugin * plugin,
1199 const int rate = 11000000;
1201 Header->i_fc[0] = 0x08;
1202 Header->i_fc[1] = 0x00;
1203 memcpy(&Header->i_addr3, &mac_bssid, sizeof(mac_bssid));
1204 memcpy(&Header->i_addr2, plugin->mac_address.mac, sizeof(plugin->mac_address));
1205 memcpy(&Header->i_addr1, to_mac_addr, sizeof(struct MacAddress));
1207 tmp16 = (uint16_t*) Header->i_dur;
1208 *tmp16 = (uint16_t) htole16((size * 1000000) / rate + 290);
1216 * @param msgbuf pointer tor the data
1217 * @param msgbuf_size size of the data
1219 * @return 32bit crc value
1223 getcrc32(const char *msgbuf, size_t msgbuf_size)
1225 //TODO calc some crc
1232 * @param msgbuf pointer tor the data
1233 * @param msgbuf_size size of the data
1235 * @return 16bit crc value
1239 getcrc16(const char *msgbuf, size_t msgbuf_size)
1241 //TODO calc some crc
1247 send_hello_beacon(struct Plugin * plugin)
1251 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending hello beacon\n");
1256 uint16_t hallo_size;
1257 struct GNUNET_MessageHeader * msgheader;
1258 struct ieee80211_frame * ieeewlanheader;
1259 struct Radiotap_Send * radioHeader;
1260 struct GNUNET_MessageHeader * msgheader2;
1262 hallo_size = GNUNET_HELLO_size(*(plugin->env->our_hello));
1263 GNUNET_assert(sizeof(struct WlanHeader) + hallo_size <= WLAN_MTU);
1264 size = sizeof(struct GNUNET_MessageHeader) + sizeof(struct Radiotap_Send)
1265 + sizeof(struct ieee80211_frame) + sizeof(struct GNUNET_MessageHeader)
1268 msgheader = GNUNET_malloc(size);
1269 msgheader->size = htons(size);
1270 msgheader->type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1272 radioHeader = (struct Radiotap_Send*) &msgheader[1];
1273 getRadiotapHeader(plugin, NULL, radioHeader);
1274 ieeewlanheader = (struct ieee80211_frame*) &radioHeader[1];
1275 getWlanHeader(ieeewlanheader, &bc_all_mac, plugin, size);
1277 msgheader2 = (struct GNUNET_MessageHeader*) &ieeewlanheader[1];
1278 msgheader2->size = htons(GNUNET_HELLO_size(*(plugin->env->our_hello))
1279 + sizeof(struct GNUNET_MessageHeader));
1281 msgheader2->type = htons(GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT);
1282 memcpy(&msgheader2[1], *plugin->env->our_hello, hallo_size);
1284 bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle, msgheader, size);
1286 if (bytes == GNUNET_SYSERR)
1288 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1289 _("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1290 errno, strerror(errno));
1293 GNUNET_assert(bytes != GNUNET_SYSERR);
1294 GNUNET_assert(bytes == size);
1295 GNUNET_free(msgheader);
1297 set_next_beacon_time(plugin);
1298 set_next_send(plugin);
1303 send_ack(struct Plugin * plugin, struct AckSendQueue * ack)
1308 struct GNUNET_MessageHeader * msgheader;
1309 struct ieee80211_frame * ieeewlanheader;
1310 struct Radiotap_Send * radioHeader;
1311 struct FragmentationAckHeader * msgheader2;
1313 GNUNET_assert(sizeof(struct FragmentationAckHeader) <= WLAN_MTU);
1315 size = sizeof(struct GNUNET_MessageHeader) + sizeof(struct Radiotap_Send)
1316 + sizeof(struct ieee80211_frame) + sizeof(struct FragmentationAckHeader);
1319 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1320 "Sending ack for message_id %u with fragment field %u, size %u\n",
1321 ack->message_id, ack->fragments_field, size
1322 - sizeof(struct Radiotap_Send));
1325 msgheader = GNUNET_malloc(size);
1326 msgheader->size = htons(size);
1327 msgheader->type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1329 radioHeader = (struct Radiotap_Send*) &msgheader[1];
1330 getRadiotapHeader(plugin, ack->session, radioHeader);
1331 ieeewlanheader = (struct ieee80211_frame*) &radioHeader[1];
1332 getWlanHeader(ieeewlanheader, &ack->session->addr, plugin, size);
1334 msgheader2 = (struct FragmentationAckHeader*) &ieeewlanheader[1];
1335 msgheader2->header.size = htons(sizeof(struct FragmentationAckHeader));
1336 msgheader2->header.type = htons(GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT_ACK);
1337 msgheader2->message_id = htonl(ack->message_id);
1338 msgheader2->fragment_field = GNUNET_htonll(ack->fragments_field);
1340 bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle, msgheader, size);
1341 if (bytes == GNUNET_SYSERR)
1343 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1344 _("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1345 errno, strerror(errno));
1348 GNUNET_assert(bytes != GNUNET_SYSERR);
1349 GNUNET_assert(bytes == size);
1350 GNUNET_free(msgheader);
1351 set_next_send(plugin);
1356 finish_sending(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1358 struct Finish_send * finish;
1359 struct Plugin * plugin;
1363 plugin = finish->plugin;
1365 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1367 bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle,
1368 finish->msgheader, finish->size);
1369 GNUNET_assert(bytes != GNUNET_SYSERR);
1371 if (bytes != finish->size)
1374 finish->msgheader = finish->msgheader + bytes;
1375 finish->size = finish->size - bytes;
1376 plugin->server_write_task = GNUNET_SCHEDULER_add_write_file(
1377 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdin_handle,
1378 &finish_sending, finish);
1382 GNUNET_free(finish->msgstart);
1383 GNUNET_free(finish);
1385 set_next_send(plugin);
1392 * Function called when wlan helper is ready to get some data
1394 * @param cls closure
1395 * @param tc GNUNET_SCHEDULER_TaskContext
1398 do_transmit(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1401 struct Plugin * plugin = cls;
1402 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1404 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1407 struct Session * session;
1408 struct FragmentMessage * fm;
1409 struct ieee80211_frame * ieeewlanheader;
1410 struct Radiotap_Send * radioHeader;
1411 struct GNUNET_MessageHeader * msgheader;
1413 struct FragmentationHeader fragheader;
1414 struct FragmentationHeader * fragheaderptr;
1415 struct Finish_send * finish;
1416 struct AckSendQueue * ack;
1419 const char * copystart;
1422 struct GNUNET_TIME_Absolute next_send;
1424 if (plugin->ack_send_queue_head != NULL)
1426 ack = plugin->ack_send_queue_head;
1427 GNUNET_CONTAINER_DLL_remove(plugin->ack_send_queue_head,
1428 plugin->ack_send_queue_tail, ack);
1429 send_ack(plugin, ack);
1434 //test if a "hello-beacon" has to be send
1435 if (GNUNET_TIME_absolute_get_remaining(plugin->beacon_time).rel_value == 0)
1437 send_hello_beacon(plugin);
1443 fm = GNUNET_CONTAINER_heap_peek(plugin->pending_Fragment_Messages);
1447 session = fm->session;
1448 GNUNET_assert(session != NULL);
1450 // test if message timed out
1451 if (GNUNET_TIME_absolute_get_remaining(fm->timeout).rel_value == 0)
1454 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "message timeout\n");
1457 free_fragment_message(plugin, fm);
1458 check_fragment_queue(plugin);
1464 //if (fm->message_size > WLAN_MTU)
1466 size = sizeof(struct FragmentationHeader);
1468 set_next_message_fragment_pos(plugin, fm);
1470 copyoffset = (WLAN_MTU - sizeof(struct FragmentationHeader))
1473 fragheader.fragment_off_or_num = htons(fm->message_pos);
1474 fragheader.message_id = htonl(fm->message_id_out);
1475 copystart = fm->msg + copyoffset;
1476 copysize = GNUNET_MIN(fm->message_size - copyoffset,
1477 WLAN_MTU - sizeof(struct FragmentationHeader));
1481 GNUNET_ERROR_TYPE_DEBUG,
1482 "Sending GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT with message_id %u with fragment number %i, size: %u, offset %u, time until timeout %u\n",
1483 fm->message_id_out, fm->message_pos, copysize
1484 + sizeof(struct FragmentationHeader), copyoffset,
1485 GNUNET_TIME_absolute_get_remaining(fm->timeout));
1488 if (copyoffset >= fm->message_size)
1491 GNUNET_ERROR_TYPE_ERROR,
1492 "offset in message for fragment too large, offset %u, size %u, max size %u, copysize %u, message_pos %u,\n",
1493 copyoffset, fm->message_size, WLAN_MTU
1494 - sizeof(struct FragmentationHeader), copysize,
1497 GNUNET_assert(copyoffset < fm->message_size);
1499 fragheader.header.size = htons(copysize
1500 + sizeof(struct FragmentationHeader));
1501 fragheader.header.type = htons(GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT);
1504 size += sizeof(struct Radiotap_Send) + sizeof(struct ieee80211_frame)
1505 + sizeof(struct GNUNET_MessageHeader);
1506 msgheader = GNUNET_malloc(size);
1507 msgheader->size = htons(size);
1508 msgheader->type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1510 radioHeader = (struct Radiotap_Send*) &msgheader[1];
1511 getRadiotapHeader(plugin, session, radioHeader);
1513 ieeewlanheader = (struct ieee80211_frame *) &radioHeader[1];
1514 getWlanHeader(ieeewlanheader, &(fm->session->addr), plugin, size);
1516 //could be faster if content is just send and not copyed before
1517 //fragmentheader is needed
1518 fragheader.message_crc = htons(getcrc16(copystart, copysize));
1519 memcpy(&ieeewlanheader[1], &fragheader,
1520 sizeof(struct FragmentationHeader));
1521 fragheaderptr = (struct FragmentationHeader *) &ieeewlanheader[1];
1522 memcpy(&fragheaderptr[1], copystart, copysize);
1524 bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle,
1526 if (bytes == GNUNET_SYSERR)
1529 GNUNET_ERROR_TYPE_ERROR,
1530 _("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1531 errno, strerror(errno));
1534 GNUNET_assert(bytes != GNUNET_SYSERR);
1536 //check if this was the last fragment of this message, if true then queue at the end of the list
1537 if (copysize + copyoffset >= fm->message_size)
1539 GNUNET_assert(copysize + copyoffset == fm->message_size);
1541 GNUNET_CONTAINER_heap_update_cost(
1542 plugin->pending_Fragment_Messages, fm->node, MIN(
1543 fm->timeout.abs_value, fm->next_ack.abs_value));
1544 // if fragments have opimized timeouts
1545 //sort_fragment_into_queue(plugin,fm);
1548 #if DEBUG_wlan_retransmission
1550 GNUNET_ERROR_TYPE_DEBUG,
1551 "Finished sending all fragments waiting for acks; message_id %u; message_id %u; fragment number %i, size: %u, time until timeout %u\n",
1552 fm->message_id_out, fm->message_id_out, fm->message_pos,
1553 fm->message_size, GNUNET_TIME_absolute_get_remaining(
1559 GNUNET_CONTAINER_heap_update_cost(
1560 plugin->pending_Fragment_Messages, fm->node,
1561 GNUNET_TIME_absolute_get().abs_value);
1566 finish = GNUNET_malloc(sizeof( struct Finish_send));
1567 finish->plugin = plugin;
1568 finish->msgheader = (char *) msgheader + bytes;
1569 finish->size = size - bytes;
1570 finish->msgstart = msgheader;
1571 finish ->next_send = next_send;
1573 GNUNET_assert(plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK);
1575 plugin->server_write_task = GNUNET_SCHEDULER_add_write_file(
1576 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdin_handle,
1577 &finish_sending, finish);
1582 GNUNET_free(msgheader);
1583 set_next_send(plugin);
1590 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1591 "do_transmit did nothing, should not happen!\n");
1595 * Another peer has suggested an address for this
1596 * peer and transport plugin. Check that this could be a valid
1597 * address. If so, consider adding it to the list
1600 * @param cls closure
1601 * @param addr pointer to the address
1602 * @param addrlen length of addr
1603 * @return GNUNET_OK if this is a plausible address for this peer
1607 wlan_plugin_address_suggested(void *cls, const void *addr, size_t addrlen)
1609 //struct Plugin *plugin = cls;
1611 /* check if the address is plausible; if so,
1612 add it to our list! */
1614 GNUNET_assert(cls !=NULL);
1615 //FIXME mitm is not checked
1616 //Mac Address has 6 bytes
1619 /* TODO check for bad addresses like multicast, broadcast, etc */
1623 return GNUNET_SYSERR;
1627 * Function that can be used by the transport service to transmit
1628 * a message using the plugin.
1630 * @param cls closure
1631 * @param target who should receive this message
1632 * @param priority how important is the message
1633 * @param msgbuf the message to transmit
1634 * @param msgbuf_size number of bytes in 'msgbuf'
1635 * @param timeout when should we time out
1636 * @param session which session must be used (or NULL for "any")
1637 * @param addr the address to use (can be NULL if the plugin
1638 * is "on its own" (i.e. re-use existing TCP connection))
1639 * @param addrlen length of the address in bytes
1640 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1641 * otherwise the plugin may use other addresses or
1642 * existing connections (if available)
1643 * @param cont continuation to call once the message has
1644 * been transmitted (or if the transport is ready
1645 * for the next transmission call; or if the
1646 * peer disconnected...)
1647 * @param cont_cls closure for cont
1648 * @return number of bytes used (on the physical network, with overheads);
1649 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1650 * and does NOT mean that the message was not transmitted (DV)
1653 wlan_plugin_send(void *cls, const struct GNUNET_PeerIdentity * target,
1654 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
1655 struct GNUNET_TIME_Relative timeout, struct Session *session,
1656 const void *addr, size_t addrlen, int force_address,
1657 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1659 struct Plugin * plugin = cls;
1660 struct PendingMessage * newmsg;
1661 struct WlanHeader * wlanheader;
1663 //check if msglen > 0
1664 GNUNET_assert(msgbuf_size > 0);
1666 //get session if needed
1667 if (session == NULL)
1669 if (wlan_plugin_address_suggested(plugin, addr, addrlen) == GNUNET_OK)
1671 session = get_session(plugin, addr);
1675 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1676 _("Wlan Address len %d is wrong\n"), addrlen);
1681 //TODO target "problem" not solved
1682 //if (session->target != NULL){
1683 // GNUNET_assert(session->target == *target);
1685 session->target = *target;
1691 //queue message in session
1692 //test if there is no other message in the "queue"
1693 //FIXME: to many send requests
1694 //GNUNET_assert (session->pending_message == NULL);
1695 if (session->pending_message != NULL)
1697 newmsg = session->pending_message;
1699 GNUNET_ERROR_TYPE_ERROR,
1700 "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 %u\n",
1701 GNUNET_TIME_absolute_get_remaining(newmsg->timeout).rel_value,
1702 session->fragment_messages_out_count);
1703 if (session->pending_message2 != NULL)
1706 GNUNET_ERROR_TYPE_ERROR,
1707 "wlan_plugin_send: two pending messages are already in the queue for this client\n");
1712 newmsg = GNUNET_malloc(sizeof(struct PendingMessage));
1713 (newmsg->msg) = GNUNET_malloc(msgbuf_size + sizeof(struct WlanHeader));
1714 wlanheader = (struct WlanHeader *) newmsg->msg;
1715 //copy msg to buffer, not fragmented / segmented yet, but with message header
1716 wlanheader->header.size = htons(msgbuf_size + sizeof(struct WlanHeader));
1717 wlanheader->header.type = htons(GNUNET_MESSAGE_TYPE_WLAN_DATA);
1718 memcpy(&(wlanheader->target), target, sizeof(struct GNUNET_PeerIdentity));
1719 wlanheader->crc = htonl(getcrc32(msgbuf, msgbuf_size));
1720 memcpy(&wlanheader[1], msgbuf, msgbuf_size);
1721 newmsg->transmit_cont = cont;
1722 newmsg->transmit_cont_cls = cont_cls;
1723 newmsg->timeout = GNUNET_TIME_relative_to_absolute(timeout);
1725 newmsg->timeout.abs_value = newmsg->timeout.abs_value - 500;
1727 newmsg->message_size = msgbuf_size + sizeof(struct WlanHeader);
1729 if (session->pending_message == NULL)
1731 session->pending_message = newmsg;
1735 session->pending_message2 = newmsg;
1739 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1740 "New message for %p with size (incl wlan header) %u added\n", session,
1741 newmsg->message_size);
1745 queue_session(plugin, session);
1747 check_fragment_queue(plugin);
1748 //FIXME not the correct size
1754 * Iterate over the fragment messages of the given session.
1756 * @param cls argument to give to iterator
1757 * @param node node to iterate over
1758 * @param element value stored at the node
1759 * @param cost cost associated with the node
1760 * @return GNUNET_YES if we should continue to iterate,
1764 free_fragment_message_from_session(void *cls,
1765 struct GNUNET_CONTAINER_HeapNode *node, void *element,
1766 GNUNET_CONTAINER_HeapCostType cost)
1768 struct Plugin_Session_pair * pair = (struct Plugin_Session_pair *) cls;
1769 struct FragmentMessage * fm = (struct FragmentMessage*) element;
1771 if (fm->session == pair->session)
1774 free_fragment_message(pair->plugin, fm);
1781 * Search for fragment message with given id and session
1783 * @param cls argument to give to iterator
1784 * @param node node to iterate over
1785 * @param element value stored at the node
1786 * @param cost cost associated with the node
1787 * @return GNUNET_YES if we should continue to iterate,
1791 search_fragment_message_from_session_and_id(void *cls,
1792 struct GNUNET_CONTAINER_HeapNode *node, void *element,
1793 GNUNET_CONTAINER_HeapCostType cost)
1795 struct Session_id_fragment_triple * triple =
1796 (struct Session_id_fragment_triple *) cls;
1797 struct FragmentMessage * fm = (struct FragmentMessage*) element;
1799 if ((fm->session == triple->session) && (fm->message_id_out
1800 == triple->message_id))
1810 * function to get the message in the fragement queue (out) of a session with a specific id
1811 * @param session pointer to the session
1812 * @param message_id id of the message
1813 * @return pointer to the struct FragmentMessage
1815 static struct FragmentMessage *
1816 get_fragment_message_from_session_and_id(struct Plugin * plugin,
1817 struct Session * session, uint32_t message_id)
1819 struct Session_id_fragment_triple triple;
1820 triple.session = session;
1821 triple.message_id = message_id;
1822 GNUNET_CONTAINER_heap_iterate(plugin->pending_Fragment_Messages,
1823 &search_fragment_message_from_session_and_id, &triple);
1828 * function to get the receive message of a session
1829 * @param plugin pointer to the plugin struct
1830 * @param session session this fragment belongs to
1832 static struct Receive_Message_Queue *
1833 get_receive_message_from_session(struct Plugin * plugin,
1834 struct Session * session)
1836 struct Receive_Message_Queue * rec_message = plugin->receive_messages_head;
1837 while (rec_message != NULL)
1839 if (rec_message->session == session)
1843 rec_message = rec_message->next;
1849 * Function to dispose the fragments received for a message and the message
1850 * @param plugin pointer to the plugin struct
1851 * @param rec_message pointer to the struct holding the message which should be freed
1854 free_receive_message(struct Plugin* plugin,
1855 struct Receive_Message_Queue * rx_message)
1857 GNUNET_assert(rx_message !=NULL);
1858 struct Receive_Fragment_Queue * rec_queue = rx_message->frag_head;
1859 struct Receive_Fragment_Queue * rec_queue2;
1861 while (rec_queue != NULL)
1863 rec_queue2 = rec_queue;
1864 rec_queue = rec_queue->next;
1865 GNUNET_free(rec_queue2);
1868 GNUNET_CONTAINER_DLL_remove(plugin->receive_messages_head,plugin->receive_messages_teil, rx_message);
1870 GNUNET_assert(plugin->pending_receive_messages > 0);
1871 GNUNET_assert(rx_message->session->fragment_messages_in_count > 0);
1873 plugin->pending_receive_messages--;
1874 rx_message->session->fragment_messages_in_count--;
1875 GNUNET_free(rx_message);
1879 * function to get the receive message of a session
1880 * @param plugin pointer to the plugin struct
1881 * @param session session this fragment belongs to
1884 check_receive_message_timeouts(struct Plugin * plugin, struct Session * session)
1886 struct Receive_Message_Queue * rec_message = plugin->receive_messages_head;
1887 while (rec_message != NULL)
1889 if (GNUNET_TIME_absolute_get_remaining(rec_message->timeout).rel_value
1892 free_receive_message(plugin, rec_message);
1894 rec_message = rec_message->next;
1901 free_session(struct Plugin * plugin, struct Sessionqueue * queue)
1903 struct Sessionqueue * pendingsession;
1904 struct Sessionqueue * pendingsession_tmp;
1905 struct PendingMessage * pm;
1906 struct Receive_Message_Queue * receive_queue;
1907 struct Plugin_Session_pair pair;
1910 GNUNET_assert(queue != NULL);
1911 GNUNET_assert(queue->content != NULL);
1914 //is this session pending for send
1915 pendingsession = plugin->pending_Sessions_head;
1916 while (pendingsession != NULL)
1918 pendingsession_tmp = pendingsession->next;
1919 if (pendingsession->content == queue->content)
1921 plugin->pendingsessions--;
1922 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head,
1923 plugin->pending_Sessions_tail, pendingsession);
1924 GNUNET_free(pendingsession);
1926 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1927 "Remove session %p from pending session queue\n", queue->content
1933 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1934 "Session is more then once in pending session\n");
1939 pendingsession = pendingsession_tmp;
1942 //is something of this session in the fragment queue?
1943 pair.plugin = plugin;
1944 pair.session = queue->content;
1945 GNUNET_CONTAINER_heap_iterate(plugin->pending_Fragment_Messages,
1946 &free_fragment_message_from_session, &pair);
1948 //dispose all received fragments
1949 receive_queue = get_receive_message_from_session(plugin, queue->content);
1950 while (receive_queue != NULL)
1952 free_receive_message(plugin, receive_queue);
1953 receive_queue = get_receive_message_from_session(plugin, queue->content);
1956 // remove PendingMessage
1957 pm = queue->content->pending_message;
1960 GNUNET_free_non_null(pm->msg);
1964 // remove PendingMessage
1965 pm = queue->content->pending_message2;
1968 GNUNET_free_non_null(pm->msg);
1972 GNUNET_CONTAINER_DLL_remove(plugin->sessions, plugin->sessions_tail, queue);
1973 GNUNET_free(queue->content);
1975 plugin->session_count--;
1977 check_fragment_queue(plugin);
1982 * Function that can be used to force the plugin to disconnect
1983 * from the given peer and cancel all previous transmissions
1984 * (and their continuation).
1986 * @param cls closure
1987 * @param target peer from which to disconnect
1990 wlan_plugin_disconnect(void *cls, const struct GNUNET_PeerIdentity *target)
1992 struct Plugin *plugin = cls;
1993 struct Sessionqueue * queue = plugin->sessions;
1995 // just look at all the session for the needed one
1996 while (queue != NULL)
1998 // content is never NULL
1999 GNUNET_assert (queue->content != NULL);
2000 if (memcmp(target, &(queue->content->target),
2001 sizeof(struct GNUNET_PeerIdentity)) == 0)
2003 free_session(plugin, queue);
2007 queue = queue->next;
2012 * Convert the transports address to a nice, human-readable
2015 * @param cls closure
2016 * @param type name of the transport that generated the address
2017 * @param addr one of the addresses of the host, NULL for the last address
2018 * the specific address format depends on the transport
2019 * @param addrlen length of the address
2020 * @param numeric should (IP) addresses be displayed in numeric form?
2021 * @param timeout after how long should we give up?
2022 * @param asc function to call on each string
2023 * @param asc_cls closure for asc
2026 wlan_plugin_address_pretty_printer(void *cls, const char *type,
2027 const void *addr, size_t addrlen, int numeric,
2028 struct GNUNET_TIME_Relative timeout,
2029 GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
2032 const unsigned char * input;
2034 //GNUNET_assert(cls !=NULL);
2037 /* invalid address (MAC addresses have 6 bytes) */
2042 input = (const unsigned char*) addr;
2043 GNUNET_snprintf(ret, sizeof(ret),
2044 "%s Mac-Address %.2X:%.2X:%.2X:%.2X:%.2X:%.2X", PROTOCOL_PREFIX,
2045 input[0], input[1], input[2], input[3], input[4], input[5]);
2050 * function to check if bitfield is representation of fragments of the message
2051 * @param rec_message message to check
2055 check_message_fragment_bitfield(struct Receive_Message_Queue * rx_msg)
2057 uint64_t checkfragments = 0;
2058 struct Receive_Fragment_Queue * rx_frag = rx_msg->frag_head;
2060 while (rx_frag != NULL)
2062 setBit((char*) &checkfragments, rx_frag->num);
2063 rx_frag = rx_frag->next;
2066 GNUNET_assert(checkfragments == rx_msg->received_fragments);
2070 * Function to test if fragment number already exists in the fragments received
2072 * @param rec_message message this fragment belongs to
2073 * @param fh Fragmentheader of the fragment
2074 * @return GNUNET_YES if fragment exists already, GNUNET_NO if it does not exists in the queue of the session
2078 is_double_msg(struct Receive_Message_Queue * rx_msg,
2079 struct FragmentationHeader * fh)
2081 //test if bitfield is okay
2083 check_message_fragment_bitfield(rx_msg);
2086 return testBit((char *) &rx_msg->received_fragments, ntohs(
2087 fh->fragment_off_or_num));
2092 * Function to insert a fragment in a queue of a message
2093 * @param session session the fragment belongs to
2094 * @param rec_queue fragment to add
2098 insert_fragment_in_queue(struct Receive_Message_Queue * rx_message,
2099 struct Receive_Fragment_Queue * rx_frag)
2101 GNUNET_assert(rx_message != NULL);
2102 GNUNET_assert(rx_frag != NULL);
2104 struct Receive_Fragment_Queue * rx_frag2 = rx_message->frag_head;
2105 struct WlanHeader * wlanheader;
2107 //this is the first fragment of the message (fragment id 0)
2108 if (rx_frag->num == 0)
2110 wlanheader = (struct WlanHeader *) rx_frag->msg;
2111 rx_message->rec_size = ntohs(wlanheader->header.size);
2115 while (rx_frag2 != NULL)
2117 if (rx_frag2->num > rx_frag->num)
2119 //next element number is grater than the current num
2120 GNUNET_CONTAINER_DLL_insert_before(rx_message->frag_head, rx_message->frag_tail, rx_frag2, rx_frag);
2121 setBit((char *) &rx_message->received_fragments, rx_frag->num);
2124 rx_frag2 = rx_frag2->next;
2127 //no element has a grater number
2128 GNUNET_CONTAINER_DLL_insert_tail(rx_message->frag_head, rx_message->frag_tail, rx_frag);
2130 setBit((char *) &rx_message->received_fragments, rx_frag->num);
2134 * handels the data after all fragments are put together
2136 * @param session_light
2137 * @param hdr pointer to the data
2140 wlan_data_message_handler(void *cls, void *client,
2141 const struct GNUNET_MessageHeader *hdr)
2143 struct Plugin * plugin = (struct Plugin*) cls;
2144 struct Session_light * session_light = (struct Session_light *) client;
2145 struct WlanHeader * wlanheader;
2146 struct Session * session;
2147 const char * tempmsg;
2148 const struct GNUNET_MessageHeader * temp_hdr;
2149 struct GNUNET_PeerIdentity tmptarget;
2151 if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_DATA)
2156 GNUNET_ERROR_TYPE_DEBUG,
2157 "Func wlan_data_message_handler got GNUNET_MESSAGE_TYPE_WLAN_DATA size: %u\n",
2161 if (ntohs(hdr->size) < sizeof(struct WlanHeader)
2162 + sizeof(struct GNUNET_MessageHeader))
2164 //packet not big enought
2168 GNUNET_assert(session_light != NULL);
2169 if (session_light->session == NULL)
2171 session_light->session = search_session(plugin, &session_light->addr);
2173 session = session_light->session;
2174 wlanheader = (struct WlanHeader *) hdr;
2176 tempmsg = (char*) &wlanheader[1];
2177 temp_hdr = (const struct GNUNET_MessageHeader *) &wlanheader[1];
2179 if (getcrc32(tempmsg, ntohs(wlanheader->header.size)) != ntohl(
2182 //wrong crc, dispose message
2183 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2184 "Wlan message Header crc was wrong\n");
2188 //if not in session list
2189 if (session == NULL)
2193 GNUNET_ERROR_TYPE_DEBUG,
2194 "WLAN client not in session list: packet size = %u, inner size = %u, header size = %u\n",
2195 ntohs(wlanheader->header.size), ntohs(temp_hdr->size),
2196 sizeof(struct WlanHeader));
2198 //try if it is a hello message
2199 if (ntohs(wlanheader->header.size) >= ntohs(temp_hdr->size)
2200 + sizeof(struct WlanHeader))
2202 if (ntohs(temp_hdr->type) == GNUNET_MESSAGE_TYPE_HELLO)
2204 if (GNUNET_HELLO_get_id(
2205 (const struct GNUNET_HELLO_Message *) temp_hdr,
2206 &tmptarget) == GNUNET_OK)
2208 session = create_session(plugin, &session_light->addr);
2209 session_light->session = session;
2210 memcpy(&session->target, &tmptarget,
2211 sizeof(struct GNUNET_PeerIdentity));
2215 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2216 "WLAN client not in session list and hello message not okay\n");
2223 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2224 "WLAN client not in session list and not a hello message\n");
2231 GNUNET_ERROR_TYPE_WARNING,
2232 "WLAN client not in session list and message size in does not fit\npacket size = %u, inner size = %u, header size = %u\n",
2233 ntohs(wlanheader->header.size), ntohs(temp_hdr->size),
2234 sizeof(struct WlanHeader));
2239 //"receive" the message
2242 GNUNET_SERVER_mst_receive(plugin->data_tokenizer, session,
2243 (const char *) temp_hdr,
2244 ntohs(hdr->size) - sizeof(struct WlanHeader), GNUNET_YES, GNUNET_NO);
2250 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2251 "wlan_data_message_handler got wrong message type\n");
2257 * Function to check if all fragments of a message have been received
2258 * @param plugin the plugin handle
2259 * @param session_light information of the message sender
2260 * @param session session the message belongs to
2261 * @param rec_message pointer to the message that should be checked
2265 check_rx_finished_msg(struct Plugin* plugin,
2266 struct Session_light * session_light, struct Session * session,
2267 struct Receive_Message_Queue * rx_message)
2269 GNUNET_assert(rx_message !=NULL);
2271 struct Receive_Fragment_Queue * rx_frag = rx_message->frag_head;
2272 int packetsize = rx_message->rec_size;
2276 uint64_t bitfield = 0;
2279 //check if first fragment is present
2280 if (packetsize == MESSAGE_LENGHT_UNKNOWN)
2284 // test if message has at least the size of the WlanHeader and a GNUNET_MessageHeader
2286 else if (packetsize < sizeof(struct WlanHeader)
2287 + sizeof(struct GNUNET_MessageHeader))
2290 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Message not big enough\n");
2292 free_receive_message(plugin, rx_message);
2297 check_message_fragment_bitfield(rx_message);
2300 // if (rx_message->frag_tail->num == ffsl(rx_message->received_fragments))
2302 bitfield = ~bitfield;
2303 bitfield = bitfield >> (63 - rx_message->frag_tail->num);
2304 if (rx_message->received_fragments == bitfield)
2307 while (rx_frag != NULL)
2309 sum += rx_frag->size;
2310 rx_frag = rx_frag->next;
2312 //sum should always be smaller or equal of
2313 GNUNET_assert(sum <= packetsize);
2314 if (sum == packetsize)
2318 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2319 "check_rec_finished_msg: A message for %p is complete\n", session);
2324 //copy fragments together
2325 //msg = GNUNET_malloc(packetsize);
2326 rx_frag = rx_message->frag_head;
2328 /*while (rx_frag != NULL)
2330 //TODO SAVE SOME COPY OPS AND CHECK CRC WITHOUT COPY
2331 memcpy(msg + aktnum, rx_frag->msg, rx_frag->size);
2332 aktnum += rx_frag->size;
2333 rx_frag = rx_frag->next;
2336 while (rx_frag != NULL)
2338 if (rx_frag->next != NULL)
2340 GNUNET_SERVER_mst_receive(plugin->fragment_tokenizer,
2341 session_light, rx_frag->msg, rx_frag->size, GNUNET_NO,
2346 //if it is the last fragment just kill all leftover
2347 GNUNET_SERVER_mst_receive(plugin->fragment_tokenizer,
2348 session_light, rx_frag->msg, rx_frag->size, GNUNET_YES,
2351 rx_frag = rx_frag->next;
2353 free_receive_message(plugin, rx_message);
2354 //call wlan_process_helper to process the message
2355 //wlan_data_message_handler(plugin, session_light,
2356 // (struct GNUNET_MessageHeader*) msg);
2357 //wlan_data_helper (plugin, session_light, (struct GNUNET_MessageHeader*) msg);
2367 process_data(void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
2370 GNUNET_assert(client != NULL);
2371 GNUNET_assert(cls != NULL);
2372 struct Session * session = (struct Session *) client;
2373 struct Plugin * plugin = (struct Plugin *) cls;
2375 struct GNUNET_TRANSPORT_ATS_Information distance[2];
2376 distance[0].type = htonl(GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2377 distance[0].value = htonl(1);
2378 distance[1].type = htonl(GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2379 distance[1].value = htonl(0);
2382 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2383 "Calling plugin->env->receive for session %p; %s; size: %u\n", session,
2384 wlan_plugin_address_to_string(NULL, session->addr.mac, 6), htons(
2388 plugin->env->receive(plugin->env->cls, &(session->target), hdr,
2389 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance, 2, session,
2390 (const char*) &session->addr, sizeof(session->addr));
2394 * function to add an ack to send it for a received fragment
2395 * @param plugin pointer to the global plugin structure
2396 * @param session pointer to the session this ack belongs to
2397 * @param bitfield bitfield to send
2398 * @param fh pointer to the fragmentation header which we would like to acknolage
2402 add_ack_for_send(struct Plugin * plugin, struct Session * session,
2403 uint64_t bitfield, struct FragmentationHeader * fh)
2405 struct AckSendQueue * ack;
2407 GNUNET_assert(plugin != NULL);
2408 GNUNET_assert(session != NULL);
2409 GNUNET_assert(fh != NULL);
2411 ack = GNUNET_malloc(sizeof(struct AckSendQueue));
2412 ack->fragments_field = bitfield;
2413 ack->message_id = ntohl(fh->message_id);
2414 ack->session = session;
2416 GNUNET_CONTAINER_DLL_insert_tail(plugin->ack_send_queue_head,
2417 plugin->ack_send_queue_tail, ack);
2422 * function to get the receive message from the message id and the session
2423 * @param plugin pointer to the plugin struct
2424 * @param session session this fragment belongs to
2425 * @param message_id id of the message
2428 struct Receive_Message_Queue *
2429 get_receive_message(struct Plugin * plugin, struct Session * session,
2430 uint32_t message_id)
2432 struct Receive_Message_Queue * rec_message = plugin->receive_messages_head;
2433 while (rec_message != NULL)
2435 if ((rec_message->message_id_in == message_id) && (rec_message->session
2440 rec_message = rec_message->next;
2446 * function to insert a received fragment into the right fragment queue of the right message
2447 * @param plugin pointer to the plugin struct
2448 * @param session_light pointer to the session_light struct of this message
2449 * @param session session this fragment belongs to
2450 * @param fh pointer to the header of the fragment
2451 * @return new fragment bitfield for the message
2455 insert_fragment_in_in_message_queue(struct Plugin * plugin,
2456 struct Session_light * session_light, struct Session * session,
2457 struct FragmentationHeader * fh, const struct Radiotap_rx * rxinfo)
2459 struct Receive_Fragment_Queue * rx_frag = NULL;
2460 struct Receive_Message_Queue * rx_message;
2461 const char * tempmsg = (char*) &fh[1];
2462 uint64_t retval = 0;
2464 //TODO fragments do not timeout
2465 //check if message_id is right or it is a new msg
2466 GNUNET_assert(fh != NULL);
2468 rx_message = get_receive_message(plugin, session, ntohl(fh->message_id));
2470 if (rx_message == NULL)
2472 if (session->fragment_messages_in_count < MESSAGES_IN_QUEUE_PER_SESSION)
2474 check_receive_message_timeouts(plugin, session);
2477 if (session->fragment_messages_in_count < MESSAGES_IN_QUEUE_PER_SESSION)
2480 //new message incoming
2481 rx_message = GNUNET_malloc(sizeof (struct Receive_Message_Queue));
2482 rx_message->message_id_in = ntohl(fh->message_id);
2483 rx_message->rec_size = MESSAGE_LENGHT_UNKNOWN;
2484 rx_message->session = session;
2485 rx_message->received_fragments = 0;
2487 GNUNET_CONTAINER_DLL_insert(plugin->receive_messages_head, plugin->receive_messages_teil, rx_message);
2489 session->fragment_messages_in_count++;
2490 plugin->pending_receive_messages++;
2494 GNUNET_ERROR_TYPE_DEBUG,
2495 "New fragmented message started: message id %u, messages in for this session %u, messages in %u\n",
2496 rx_message->message_id_in, session->fragment_messages_in_count,
2497 plugin->pending_receive_messages);
2504 GNUNET_ERROR_TYPE_INFO,
2505 "WLAN fragment message_id and session message_id do not exist, max MESSAGES_IN_QUEUE_PER_SESSION reached, akt in message_id %u\n",
2506 get_receive_message_from_session(plugin, session)->message_id_in);
2507 setBit((char *) &retval, ntohs(fh->fragment_off_or_num));
2513 rx_message->timeout = GNUNET_TIME_absolute_add(
2514 GNUNET_TIME_absolute_get(),
2515 MESSAGE_IN_TIMEOUT);
2517 if (is_double_msg(rx_message, fh) != GNUNET_YES)
2521 rx_frag = GNUNET_malloc(sizeof (struct Receive_Fragment_Queue) +
2522 ntohs(fh->header.size) - sizeof(struct FragmentationHeader));
2523 rx_frag->size = ntohs(fh->header.size)
2524 - sizeof(struct FragmentationHeader);
2525 rx_frag->num = ntohs(fh->fragment_off_or_num);
2526 rx_frag->msg = (char*) &(rx_frag[1]);
2527 //copy msg to buffer
2528 memcpy((char *) rx_frag->msg, tempmsg, rx_frag->size);
2529 memcpy((char *) &(rx_frag->rxinfo), rxinfo, sizeof(rxinfo));
2530 insert_fragment_in_queue(rx_message, rx_frag);
2532 retval = rx_message->received_fragments;
2536 GNUNET_ERROR_TYPE_DEBUG,
2537 "New fragment: size %u, fragsize %u, message id %u, bitfield %X, session %u\n",
2538 rx_message->rec_size, rx_frag->size, rx_message->message_id_in,
2539 rx_message->received_fragments, session);
2542 check_rx_finished_msg(plugin, session_light, session, rx_message);
2546 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "WLAN fragment is a clone\n");
2547 retval = rx_message->received_fragments;
2555 * Function used for to process the data received from the wlan interface
2557 * @param cls the plugin handle
2558 * @param session_light FIXME: document
2559 * @param hdr hdr of the GNUNET_MessageHeader
2562 wlan_data_helper(void *cls, struct Session_light * session_light,
2563 const struct GNUNET_MessageHeader * hdr, const struct Radiotap_rx * rxinfo)
2565 struct Plugin *plugin = cls;
2566 struct Session * session;
2568 struct FragmentationHeader * fh;
2569 struct FragmentationAckHeader * fah;
2570 struct FragmentMessage * fm;
2572 const char * tempmsg;
2574 uint64_t fragment_bitfield = 0;
2577 if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT)
2580 //TODO better DOS protection, error handling
2581 //TODO test first than create session
2582 GNUNET_assert(session_light != NULL);
2586 GNUNET_ERROR_TYPE_DEBUG,
2587 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT size: %u; %s\n",
2588 ntohs(hdr->size), wlan_plugin_address_to_string(NULL,
2589 session_light->addr.mac, 6));
2592 if (session_light->session == NULL)
2594 session_light->session = get_session(plugin, &session_light->addr);
2596 GNUNET_assert(GNUNET_HELLO_get_id(
2597 (const struct GNUNET_HELLO_Message *) &hdr[1],
2598 &(session_light->session->target) ) != GNUNET_SYSERR);
2604 else if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT)
2607 GNUNET_assert(session_light != NULL);
2608 if (session_light->session == NULL)
2610 session_light->session = search_session(plugin, &session_light->addr);
2612 session = session_light->session;
2614 fh = (struct FragmentationHeader *) hdr;
2615 tempmsg = (char*) &fh[1];
2619 GNUNET_ERROR_TYPE_DEBUG,
2620 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT with message_id %u with fragment number %i, size: %u; %s\n",
2621 ntohl(fh->message_id), ntohs(fh->fragment_off_or_num), ntohs(
2622 hdr->size), wlan_plugin_address_to_string(NULL,
2623 session_light->addr.mac, 6));
2626 if (getcrc16(tempmsg, ntohs(fh->header.size)) != ntohs(fh->message_crc))
2628 //wrong crc, dispose message
2629 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "WLAN fragment crc was wrong\n");
2633 //if in the session list
2634 if (session != NULL)
2636 fragment_bitfield = insert_fragment_in_in_message_queue(plugin,
2637 session_light, session, fh, rxinfo);
2643 GNUNET_ERROR_TYPE_INFO,
2644 "WLAN client not in session list, fragment num %u, message id %u\n",
2645 ntohs(fh->fragment_off_or_num), ntohl(fh->message_id));
2647 GNUNET_SERVER_mst_receive(plugin->fragment_tokenizer, session_light,
2648 tempmsg, ntohs(hdr->size) - sizeof(struct FragmentationHeader),
2649 GNUNET_YES, GNUNET_NO);
2650 //wlan_data_message_handler(plugin, session_light,
2651 // (struct GNUNET_MessageHeader *) tempmsg);
2652 session = session_light->session;
2653 //test if a session was created
2654 if (session == NULL)
2658 setBit((char *) &fragment_bitfield, ntohs(fh->fragment_off_or_num));
2661 add_ack_for_send(plugin, session, fragment_bitfield, fh);
2662 set_next_send(plugin);
2668 else if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT_ACK)
2673 GNUNET_ERROR_TYPE_DEBUG,
2674 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT_ACK size: %u; %s\n",
2675 ntohs(hdr->size), wlan_plugin_address_to_string(NULL,
2676 session_light->addr.mac, 6));
2679 GNUNET_assert(session_light != NULL);
2680 if (session_light->session == NULL)
2682 session_light->session = search_session(plugin, &session_light->addr);
2683 GNUNET_assert(session_light->session != NULL);
2685 session = session_light->session;
2686 fah = (struct FragmentationAckHeader *) hdr;
2687 fm = get_fragment_message_from_session_and_id(plugin, session, ntohl(
2693 fm->ack_bitfield = fm->ack_bitfield | GNUNET_ntohll(
2694 fah->fragment_field);
2695 fm->next_ack = GNUNET_TIME_relative_to_absolute(get_ack_timeout(fm));
2697 #if DEBUG_wlan_retransmission
2698 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got ack for: %u; %u\n",
2699 fm->message_id_out, fm->ack_bitfield);
2701 check_finished_fragment(plugin, fm);
2702 set_next_send(plugin);
2707 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2708 "WLAN fragment not in fragment list with id %u of ack\n", ntohl(
2718 GNUNET_ERROR_TYPE_INFO,
2719 "WLAN packet inside the WLAN helper packet has not the right type: %u size: %u\n",
2720 ntohs(hdr->type), ntohs(hdr->size));
2726 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2727 "Helper finished\n");
2734 macprinter(const u_int8_t * mac)
2736 static char macstr[20];
2738 GNUNET_snprintf(macstr, sizeof(macstr), "%X:%X:%X:%X:%X:%X", mac[0], mac[1],
2739 mac[2], mac[3], mac[4], mac[5]);
2744 * Function used for to process the data from the suid process
2746 * @param cls the plugin handle
2747 * @param client client that send the data (not used)
2748 * @param hdr header of the GNUNET_MessageHeader
2751 wlan_process_helper(void *cls, void *client,
2752 const struct GNUNET_MessageHeader *hdr)
2754 struct Plugin *plugin = cls;
2755 struct ieee80211_frame * wlanIeeeHeader = NULL;
2756 struct Session_light * session_light = NULL;
2757 struct Radiotap_rx * rxinfo;
2758 const struct GNUNET_MessageHeader * temp_hdr = NULL;
2763 switch (ntohs(hdr->type))
2765 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA:
2768 GNUNET_ERROR_TYPE_DEBUG,
2769 "Func wlan_process_helper got GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA size: %u\n",
2773 //call wlan_process_helper with the message inside, later with wlan: analyze signal
2774 if (ntohs(hdr->size) < sizeof(struct ieee80211_frame)
2775 + sizeof(struct GNUNET_MessageHeader) + sizeof(struct Radiotap_rx))
2778 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2779 "Size of packet is too small; size: %u min size: %u\n", ntohs(
2780 hdr->size), sizeof(struct ieee80211_frame)
2781 + sizeof(struct GNUNET_MessageHeader));
2784 /* FIXME: restart SUID process */
2787 rxinfo = (struct Radiotap_rx *) &hdr[1];
2788 wlanIeeeHeader = (struct ieee80211_frame *) &rxinfo[1];
2790 //process only if it is an broadcast or for this computer both with the gnunet bssid
2793 if (memcmp(&(wlanIeeeHeader->i_addr3), &mac_bssid,
2794 sizeof(struct MacAddress)) == 0)
2796 //check for broadcast or mac
2797 if (memcmp(&(wlanIeeeHeader->i_addr1), &bc_all_mac,
2798 sizeof(struct MacAddress) == 0) || memcmp(
2799 &(wlanIeeeHeader->i_addr1), &(plugin->mac_address),
2800 sizeof(struct MacAddress)) == 0)
2803 // process the inner data
2806 datasize = ntohs(hdr->size) - sizeof(struct ieee80211_frame)
2807 - sizeof(struct GNUNET_MessageHeader)
2808 - sizeof(struct Radiotap_rx);
2810 session_light = GNUNET_malloc(sizeof(struct Session_light));
2811 memcpy(&session_light->addr, &(wlanIeeeHeader->i_addr2),
2812 sizeof(struct MacAddress));
2813 //session_light->session = search_session(plugin,session_light->addr);
2816 while (pos < datasize)
2818 temp_hdr = (struct GNUNET_MessageHeader *) &wlanIeeeHeader[1]
2821 wlan_data_helper(plugin, session_light, temp_hdr, rxinfo);
2822 pos += ntohs(temp_hdr->size);
2827 GNUNET_free(session_light);
2832 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2833 "Func wlan_process_helper got wrong MAC: %s\n", macprinter(
2834 wlanIeeeHeader->i_addr1));
2841 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2842 "Func wlan_process_helper got wrong BSSID: %s\n", macprinter(
2843 wlanIeeeHeader->i_addr2));
2847 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
2848 //TODO more control messages
2849 //TODO use struct wlan_helper_control
2850 if (ntohs(hdr->size) != sizeof(struct Wlan_Helper_Control_Message))
2853 /* FIXME: restart SUID process */
2856 memcpy(&plugin->mac_address, &hdr[1], sizeof(struct MacAddress));
2858 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2859 "Received WLAN_HELPER_CONTROL message with transport of address %s\n",
2860 wlan_plugin_address_to_string(cls, &plugin->mac_address,
2861 sizeof(struct MacAddress)));
2863 plugin->env->notify_address(plugin->env->cls, "wlan", &plugin->mac_address,
2864 sizeof(struct MacAddress), GNUNET_TIME_UNIT_FOREVER_REL);
2873 * We have been notified that wlan-helper has written something to stdout.
2874 * Handle the output, then reschedule this function to be called again once
2875 * more is available.
2877 * @param cls the plugin handle
2878 * @param tc the scheduling context
2881 wlan_plugin_helper_read(void *cls,
2882 const struct GNUNET_SCHEDULER_TaskContext *tc)
2884 struct Plugin *plugin = cls;
2885 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2887 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2890 char mybuf[WLAN_MTU + sizeof(struct GNUNET_MessageHeader)];
2893 bytes = GNUNET_DISK_file_read(plugin->server_stdout_handle, mybuf,
2898 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2899 _("Finished reading from wlan-helper stdout with code: %d\n"),
2904 GNUNET_SERVER_mst_receive(plugin->suid_tokenizer, NULL, mybuf, bytes,
2905 GNUNET_NO, GNUNET_NO);
2907 GNUNET_assert(plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2908 plugin->server_read_task = GNUNET_SCHEDULER_add_read_file(
2909 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdout_handle,
2910 &wlan_plugin_helper_read, plugin);
2914 * Start the gnunet-wlan-helper process.
2916 * @param plugin the transport plugin
2917 * @param testmode should we use the dummy driver for testing?
2918 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
2921 wlan_transport_start_wlan_helper(struct Plugin *plugin, int testmode)
2923 const char * filename = "gnunet-transport-wlan-helper";
2924 plugin->server_stdout = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_NO, GNUNET_YES);
2925 if (plugin->server_stdout == NULL)
2926 return GNUNET_SYSERR;
2928 plugin->server_stdin = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_NO);
2929 if (plugin->server_stdin == NULL)
2930 return GNUNET_SYSERR;
2933 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2934 "Starting gnunet-wlan-helper process cmd: %s %s %i\n", filename,
2935 plugin->interface, testmode);
2937 /* Start the server process */
2939 plugin->server_proc = GNUNET_OS_start_process(plugin->server_stdin,
2940 plugin->server_stdout, filename, filename, plugin->interface, ((testmode
2941 == 1) ? "1" : (testmode == 2) ? "2" : "0"), NULL);
2942 if (plugin->server_proc == NULL)
2945 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2946 "Failed to start gnunet-wlan-helper process\n");
2948 return GNUNET_SYSERR;
2951 /* Close the write end of the read pipe */
2952 GNUNET_DISK_pipe_close_end(plugin->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
2954 /* Close the read end of the write pipe */
2955 GNUNET_DISK_pipe_close_end(plugin->server_stdin, GNUNET_DISK_PIPE_END_READ);
2957 plugin->server_stdout_handle = GNUNET_DISK_pipe_handle(plugin->server_stdout,
2958 GNUNET_DISK_PIPE_END_READ);
2959 plugin->server_stdin_handle = GNUNET_DISK_pipe_handle(plugin->server_stdin,
2960 GNUNET_DISK_PIPE_END_WRITE);
2962 GNUNET_assert(plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2965 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2966 "Adding server_read_task for the wlan-helper\n");
2969 plugin->server_read_task = GNUNET_SCHEDULER_add_read_file(
2970 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdout_handle,
2971 &wlan_plugin_helper_read, plugin);
2977 * Exit point from the plugin.
2978 * @param cls pointer to the api struct
2983 libgnunet_plugin_transport_wlan_done(void *cls)
2985 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2986 struct Plugin *plugin = api->cls;
2987 struct Sessionqueue * queue = plugin->sessions;
2988 struct Sessionqueue * queue_next;
2989 struct FragmentMessage * fm;
2992 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2993 "libgnunet_plugin_transport_wlan_done started\n");
2996 GNUNET_assert(cls !=NULL);
2998 if (plugin->suid_tokenizer != NULL)
2999 GNUNET_SERVER_mst_destroy(plugin->suid_tokenizer);
3001 if (plugin->data_tokenizer != NULL)
3002 GNUNET_SERVER_mst_destroy(plugin->data_tokenizer);
3004 if (plugin->fragment_tokenizer != NULL)
3005 GNUNET_SERVER_mst_destroy(plugin->fragment_tokenizer);
3007 fm = (struct FragmentMessage *) GNUNET_CONTAINER_heap_peek(
3008 plugin->pending_Fragment_Messages);
3012 free_fragment_message(plugin, fm);
3013 fm = (struct FragmentMessage *) GNUNET_CONTAINER_heap_peek(
3014 plugin->pending_Fragment_Messages);
3018 while (queue != NULL)
3020 queue_next = queue->next;
3021 free_session(plugin, queue);
3026 GNUNET_free_non_null(plugin->interface);
3027 GNUNET_CONTAINER_heap_destroy(plugin->pending_Fragment_Messages);
3028 GNUNET_free (plugin);
3034 * Entry point for the plugin.
3036 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
3037 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
3040 libgnunet_plugin_transport_wlan_init(void *cls)
3042 //struct GNUNET_SERVICE_Context *service;
3043 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3044 struct GNUNET_TRANSPORT_PluginFunctions *api;
3045 struct Plugin *plugin;
3046 static unsigned long long testmode = 0;
3048 GNUNET_assert(cls !=NULL);
3050 plugin = GNUNET_malloc (sizeof (struct Plugin));
3052 plugin->pendingsessions = 0;
3053 plugin->session_count = 0;
3054 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
3055 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
3056 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
3057 plugin->pending_Fragment_Messages = GNUNET_CONTAINER_heap_create(
3058 GNUNET_CONTAINER_HEAP_ORDER_MIN);
3060 plugin->suid_tokenizer = GNUNET_SERVER_mst_create(&wlan_process_helper,
3063 plugin->data_tokenizer = GNUNET_SERVER_mst_create(&process_data, plugin);
3064 plugin->fragment_tokenizer = GNUNET_SERVER_mst_create(
3065 &wlan_data_message_handler, plugin);
3067 //plugin->sessions = GNUNET_malloc (sizeof (struct Sessionqueue));
3068 //plugin->pending_Sessions_head = GNUNET_malloc (sizeof (struct Sessionqueue));
3070 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3072 api->send = &wlan_plugin_send;
3073 api->disconnect = &wlan_plugin_disconnect;
3074 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
3075 api->check_address = &wlan_plugin_address_suggested;
3076 api->address_to_string = &wlan_plugin_address_to_string;
3080 if (GNUNET_CONFIGURATION_have_value(env->cfg, "transport-wlan", "TESTMODE"))
3082 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(env->cfg,
3083 "transport-wlan", "TESTMODE", &testmode))
3084 testmode = 0; //default value
3087 if (GNUNET_CONFIGURATION_have_value(env->cfg, "transport-wlan", "INTERFACE"))
3089 if (GNUNET_CONFIGURATION_get_value_string(env->cfg, "transport-wlan",
3090 "INTERFACE", &(plugin->interface)) != GNUNET_YES)
3092 libgnunet_plugin_transport_wlan_done(api);
3098 set_next_beacon_time(plugin);
3100 wlan_transport_start_wlan_helper(plugin, testmode);
3103 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "wlan init finished\n");
3109 /* end of plugin_transport_wlan.c */