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_YES
65 #define MESSAGE_LENGHT_UNKNOWN -1
66 //#define NO_MESSAGE_OR_MESSAGE_FINISHED -2
69 * After how long do we expire an address that we
70 * learned from another peer if it is not reconfirmed
73 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
76 * Initial handshake message for a session.
81 * Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME.
83 struct GNUNET_MessageHeader header;
86 * Identit*mac_y of the node connecting (TCP client)
88 struct GNUNET_PeerIdentity clientIdentity;
93 * Encapsulation of all of the state of the plugin.
100 struct GNUNET_TRANSPORT_PluginEnvironment *env;
103 * List of open sessions. head
105 struct Sessionqueue *sessions;
108 * List of open sessions. tail
110 struct Sessionqueue *sessions_tail;
119 * encapsulation of data from the local wlan helper program
122 struct GNUNET_SERVER_MessageStreamTokenizer * suid_tokenizer;
125 * encapsulation of packets received
128 struct GNUNET_SERVER_MessageStreamTokenizer * data_tokenizer;
131 * stdout pipe handle for the gnunet-wlan-helper process
133 struct GNUNET_DISK_PipeHandle *server_stdout;
136 * stdout file handle for the gnunet-wlan-helper process
138 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
141 * stdin pipe handle for the gnunet-wlan-helper process
143 struct GNUNET_DISK_PipeHandle *server_stdin;
146 * stdin file handle for the gnunet-wlan-helper process
148 const struct GNUNET_DISK_FileHandle *server_stdin_handle;
151 * ID of the gnunet-wlan-server std read task
153 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
156 * ID of the gnunet-wlan-server std read task
158 GNUNET_SCHEDULER_TaskIdentifier server_write_task;
161 * ID of the delay task for writing
163 GNUNET_SCHEDULER_TaskIdentifier server_write_delay_task;
166 * The process id of the wlan process
168 struct GNUNET_OS_Process *server_proc;
171 * The interface of the wlan card given to us by the user.
176 * The mac_address of the wlan card given to us by the helper.
178 struct MacAddress mac_address;
181 * Sessions currently pending for transmission
182 * to this peer, if any.
184 struct Sessionqueue * pending_Sessions;
187 * Sessions currently pending for transmission
188 * to this peer (tail), if any.
190 struct Sessionqueue * pending_Sessions_tail;
193 * number of pending sessions
195 unsigned int pendingsessions;
198 * Messages in the fragmentation queue, head
201 struct FragmentMessage * pending_Fragment_Messages_head;
204 * Messages in the fragmentation queue, tail
207 struct FragmentMessage * pending_Fragment_Messages_tail;
210 * number of pending fragment message
213 unsigned int pending_fragment_messages;
216 * Messages in the in Queue, head
219 struct Receive_Message_Queue * receive_messages_head;
222 * Messages in the in Queue, tail
225 struct Receive_Message_Queue * receive_messages_teil;
228 * number of messages in the in queue
231 unsigned int pending_receive_messages;
234 * time of the next "hello-beacon"
237 struct GNUNET_TIME_Absolute beacon_time;
240 * queue to send acks for received fragments (head)
243 struct AckSendQueue * ack_send_queue_head;
246 * queue to send acks for received fragments (tail)
249 struct AckSendQueue * ack_send_queue_tail;
254 * Struct to store data if file write did not accept the whole packet
258 struct Plugin * plugin;
260 struct GNUNET_MessageHeader * msgstart;
265 * Queue of sessions, for the general session queue and the pending session queue
270 struct Sessionqueue * next;
271 struct Sessionqueue * prev;
272 struct Session * content;
276 * Queue for the fragments received
279 struct Receive_Fragment_Queue
281 struct Receive_Fragment_Queue * next;
282 struct Receive_Fragment_Queue * prev;
286 struct Radiotap_rx rxinfo;
290 * Queue for the fragments received
293 struct Receive_Message_Queue
295 struct Receive_Message_Queue * next;
297 struct Receive_Message_Queue * prev;
300 * current number for message incoming, to distinguish between the messages
302 uint32_t message_id_in;
305 * size of the message received,
306 * MESSAGE_LENGHT_UNKNOWN means that the size is not known,
307 * NO_MESSAGE_OR_MESSAGE_FINISHED means no message received
313 * Sorted queue with the fragments received; head
316 struct Receive_Fragment_Queue * frag_head;
319 * Sorted queue with the fragments received; tail
322 struct Receive_Fragment_Queue * frag_tail;
325 * Session this fragment belongs to
328 struct Session * session;
331 * Timeout value for the pending message.
333 struct GNUNET_TIME_Absolute timeout;
336 * Bitfield of received fragments
339 uint64_t received_fragments;
343 * Information kept for each message that is yet to
346 struct PendingMessage
350 * The pending message
355 * Size of the message
360 * Continuation function to call once the message
361 * has been sent. Can be NULL if there is no
362 * continuation to call.
364 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
367 * Cls for transmit_cont
369 void * transmit_cont_cls;
372 * Timeout value for the pending message.
374 struct GNUNET_TIME_Absolute timeout;
379 * Queue for acks to send for fragments recived
385 struct AckSendQueue * next;
386 struct AckSendQueue * prev;
388 struct Session * session;
390 * ID of message, to distinguish between the messages, picked randomly.
395 * Bit field for received fragments
397 uint64_t fragments_field;
402 * Session infos gathered from a messages
408 * the session this message belongs to
410 struct Session * session;
418 * Session handle for connections.
426 struct SessionHeader header;
429 * Pointer to the global plugin struct.
431 struct Plugin *plugin;
434 * Message currently pending for transmission
435 * to this peer, if any.
437 struct PendingMessage *pending_message;
440 * Message currently pending for transmission
441 * to this peer, if any.
443 struct PendingMessage *pending_message2;
446 * To whom are we talking to (set to our identity
447 * if we are still waiting for the welcome message)
449 struct GNUNET_PeerIdentity target;
457 * Address of the other peer (either based on our 'connect'
458 * call or on our 'accept' call).
463 * Last activity on this connection. Used to select preferred
466 struct GNUNET_TIME_Absolute last_activity;
469 * count of messages in the fragment out queue for this session
472 int fragment_messages_out_count;
475 * count of messages in the fragment in queue for this session
478 int fragment_messages_in_count;
488 * Struct for Messages in the fragment queue
491 struct FragmentMessage
494 * Session this message belongs to
497 struct Session *session;
500 * This is a doubly-linked list.
502 struct FragmentMessage *next;
505 * This is a doubly-linked list.
507 struct FragmentMessage *prev;
510 * The pending message
515 * Timeout value for the pending message.
517 struct GNUNET_TIME_Absolute timeout;
520 * Timeout value for the pending fragments.
521 * Stores the time when the next msg fragment ack has to be received
523 struct GNUNET_TIME_Absolute next_ack;
526 * bitfield with all acks received for this message
528 uint64_t ack_bitfield;
531 * Size of the message
536 * pos / next fragment number in the message, for fragmentation/segmentation,
537 * some acks can be missing but there is still time
539 uint32_t message_pos;
542 * current number for message outgoing, to distinguish between the messages
544 uint32_t message_id_out;
548 * Header for messages which need fragmentation
550 struct FragmentationAckHeader
553 struct GNUNET_MessageHeader header;
556 * ID of message, to distinguish between the messages, picked randomly.
558 uint32_t message_id GNUNET_PACKED;
561 * Offset or number of this fragment, for fragmentation/segmentation (design choice, TBD)
563 uint64_t fragment_field GNUNET_PACKED;
568 getRadiotapHeader(struct Plugin * plugin, struct Session * session,
569 struct Radiotap_Send * Header);
572 getWlanHeader(struct ieee80211_frame * Header, const char * to_mac_addr,
573 struct Plugin * plugin);
576 wlan_plugin_address_suggested(void *cls, const void *addr, size_t addrlen);
579 getcrc16(const char *msgbuf, size_t msgbuf_size);
582 do_transmit(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
585 check_fragment_queue(struct Plugin * plugin);
588 getcrc32(const char *msgbuf, size_t msgbuf_size);
591 free_receive_message(struct Plugin* plugin,
592 struct Receive_Message_Queue * rx_message);
594 struct Receive_Message_Queue *
595 get_receive_message_from_session(struct Plugin * plugin,
596 struct Session * session);
599 wlan_data_helper(void *cls, struct Session_light * session_light,
600 const struct GNUNET_MessageHeader * hdr, const struct Radiotap_rx * rxinfo);
603 wlan_process_helper(void *cls, void *client,
604 const struct GNUNET_MessageHeader *hdr);
607 finish_sending(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
610 wlan_data_massage_handler(struct Plugin * plugin,
611 struct Session_light * session_light,
612 const struct GNUNET_MessageHeader * hdr);
615 wlan_plugin_address_to_string(void *cls, const void *addr, size_t addrlen);
617 struct Receive_Message_Queue *
618 get_receive_message(struct Plugin * plugin, struct Session * session,
619 uint32_t message_id);
622 htonll(uint64_t input)
628 ntohll(uint64_t input)
634 * Sets a bit active in the bitArray. Increment bit-specific
635 * usage counter on disk only if below 4bit max (==15).
637 * @param bitArray memory area to set the bit in
638 * @param bitIdx which bit to set
641 setBit(char *bitArray, unsigned int bitIdx)
644 unsigned int targetBit;
646 arraySlot = bitIdx / 8;
647 targetBit = (1L << (bitIdx % 8));
648 bitArray[arraySlot] |= targetBit;
652 * Clears a bit from bitArray. Bit is cleared from the array
653 * only if the respective usage counter on the disk hits/is zero.
655 * @param bitArray memory area to set the bit in
656 * @param bitIdx which bit to unset
659 clearBit(char *bitArray, unsigned int bitIdx)
662 unsigned int targetBit;
665 targetBit = (1L << (bitIdx % 8));
666 bitArray[slot] = bitArray[slot] & (~targetBit);
670 * Checks if a bit is active in the bitArray
672 * @param bitArray memory area to set the bit in
673 * @param bitIdx which bit to test
674 * @return GNUNET_YES if the bit is set, GNUNET_NO if not.
677 testBit(char *bitArray, unsigned int bitIdx)
680 unsigned int targetBit;
683 targetBit = (1L << (bitIdx % 8));
684 if (bitArray[slot] & targetBit)
691 * get the next message number, at the moment just a random one
692 * @return returns the next valid message-number for sending packets
695 get_next_message_id()
697 return GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
701 * start next message number generator
702 * (not necessary at the moment)
705 start_next_message_id()
707 //GNUNET_CRYPTO_random_init;
711 * search for a session with the addr
713 * @param plugin pointer to the plugin struct
714 * @param addr pointer to the mac address of the peer
715 * @return returns the session
718 static struct Session *
719 search_session(struct Plugin *plugin, const uint8_t * addr)
721 struct Sessionqueue * queue = plugin->sessions;
722 struct Sessionqueue * lastitem = NULL;
724 //just look at all the session for the needed one
725 while (queue != NULL)
727 // content is never NULL
728 GNUNET_assert (queue->content != NULL);
729 char * addr2 = queue->content->addr;
730 if (memcmp(addr, addr2, 6) == 0)
733 return queue->content;
743 * create a new session
745 * @param plugin pointer to the plugin struct
746 * @param addr pointer to the mac address of the peer
747 * @return returns the session
750 static struct Session *
751 create_session(struct Plugin *plugin, const uint8_t * addr)
753 struct Sessionqueue * queue = GNUNET_malloc (sizeof (struct Sessionqueue));
755 GNUNET_CONTAINER_DLL_insert_tail(plugin->sessions, plugin->sessions_tail, queue);
757 queue->content = GNUNET_malloc (sizeof (struct Session));
758 queue->content->plugin = plugin;
759 memcpy(queue->content->addr, addr, 6);
760 queue->content->fragment_messages_out_count = 0;
761 queue->content->fragment_messages_in_count = 0;
763 plugin->session_count++;
766 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "New session %p with %s\n",
767 queue->content, wlan_plugin_address_to_string(NULL, addr, 6));
770 return queue->content;
774 * get Session from address, create if no session exists
776 * @param plugin pointer to the plugin struct
777 * @param addr pointer to the mac address of the peer
778 * @return returns the session
780 //TODO add other possibilities to find the right session (are there other?)
781 static struct Session *
782 get_Session(struct Plugin *plugin, const uint8_t * addr)
784 struct Session * session = search_session(plugin, addr);
790 return create_session(plugin, addr);
792 /* -- not needed, layer above already has it--
793 //queue welcome message for new sessions, not realy needed
794 //struct WelcomeMessage welcome;
795 struct PendingMessage *pm;
796 pm = GNUNET_malloc (sizeof (struct PendingMessage));
797 pm->msg = GNUNET_malloc(GNUNET_HELLO_size(* (plugin->env->our_hello)));
798 pm->message_size = GNUNET_HELLO_size(* (plugin->env->our_hello));
799 //welcome.header.size = htons (GNUNET_HELLO_size(* (plugin->env->our_hello)));
800 //welcome.header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT);
801 //welcome.clientIdentity = *plugin->env->my_identity;
802 memcpy ( (pm->msg), * plugin->env->our_hello, GNUNET_HELLO_size(* (plugin->env->our_hello)));
803 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
804 queue->content->pending_message = pm;
805 plugin->pendingsessions ++;
806 GNUNET_CONTAINER_DLL_insert_tail(plugin->pending_Sessions, plugin->pending_Sessions_tail, queue);
808 check_fragment_queue(plugin);
813 * Queue the session to send data
817 queue_Session(struct Plugin *plugin, struct Session * session)
819 struct Sessionqueue * queue = plugin->pending_Sessions;
820 struct Sessionqueue * lastitem = NULL;
822 while (queue != NULL)
824 // content is never NULL
825 GNUNET_assert (queue->content != NULL);
826 // is session already in queue?
827 if (session == queue->content)
836 // Session is not in the queue
838 queue = GNUNET_malloc (sizeof (struct Sessionqueue));
839 queue->content = session;
842 GNUNET_CONTAINER_DLL_insert_after (plugin->pending_Sessions,
843 plugin->pending_Sessions_tail,
844 plugin->pending_Sessions_tail, queue);
845 plugin->pendingsessions++;
851 * Function to schedule the write task, executed after a delay
854 delay_fragment_task(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
856 struct Plugin * plugin = cls;
857 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
859 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
862 // GNUNET_TIME_UNIT_FOREVER_REL is needed to clean up old msg
863 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
865 plugin->server_write_task = GNUNET_SCHEDULER_add_write_file(
866 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdin_handle,
867 &do_transmit, plugin);
873 * Function to calculate the time of the next periodic "hello-beacon"
876 set_next_beacon_time(struct Plugin * const plugin)
878 //under 10 known peers: once a second
879 if (plugin->session_count < 10)
881 plugin->beacon_time = GNUNET_TIME_absolute_add(
882 GNUNET_TIME_absolute_get(), GNUNET_TIME_relative_multiply(
883 GNUNET_TIME_UNIT_SECONDS, HALLO_BEACON_SCALING_FACTOR));
885 //under 30 known peers: every 10 seconds
886 else if (plugin->session_count < 30)
888 plugin->beacon_time = GNUNET_TIME_absolute_add(
889 GNUNET_TIME_absolute_get(), GNUNET_TIME_relative_multiply(
890 GNUNET_TIME_UNIT_SECONDS, 10 * HALLO_BEACON_SCALING_FACTOR));
892 //over 30 known peers: once a minute
895 plugin->beacon_time = GNUNET_TIME_absolute_add(
896 GNUNET_TIME_absolute_get(), GNUNET_TIME_relative_multiply(
897 GNUNET_TIME_UNIT_MINUTES, HALLO_BEACON_SCALING_FACTOR));
902 struct GNUNET_TIME_Relative
903 get_next_frag_timeout(struct FragmentMessage * fm)
905 return GNUNET_TIME_relative_min(GNUNET_TIME_absolute_get_remaining(
906 fm->next_ack), GNUNET_TIME_absolute_get_remaining(fm->timeout));
911 * Function to get the timeout value for acks for this session
914 struct GNUNET_TIME_Relative
915 get_ack_timeout(struct FragmentMessage * fm)
917 return FRAGMENT_TIMEOUT;
921 * Function to set the timer for the next timeout of the fragment queue
922 * @param plugin the handle to the plugin struct
925 check_next_fragment_timeout(struct Plugin * const plugin)
927 struct FragmentMessage * fm;
928 struct GNUNET_TIME_Relative next_send;
931 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
933 GNUNET_SCHEDULER_cancel(plugin->server_write_delay_task);
934 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
936 fm = plugin->pending_Fragment_Messages_head;
938 GNUNET_assert(plugin->server_write_delay_task == GNUNET_SCHEDULER_NO_TASK);
940 //check if some acks are in the queue
941 if (plugin->ack_send_queue_head != NULL)
943 next_send = GNUNET_TIME_UNIT_ZERO;
945 //check if there are some fragments in the queue
948 next_send = GNUNET_TIME_absolute_get_remaining(plugin->beacon_time);
951 next_send = GNUNET_TIME_relative_min(next_send,
952 get_next_frag_timeout(fm));
955 plugin->server_write_delay_task = GNUNET_SCHEDULER_add_delayed(next_send,
956 &delay_fragment_task, plugin);
961 * Function to get the next queued Session, removes the session from the queue
964 static struct Session *
965 get_next_queue_Session(struct Plugin * plugin)
967 struct Session * session;
968 struct Sessionqueue * sessionqueue;
969 struct Sessionqueue * sessionqueue_alt;
970 struct PendingMessage * pm;
971 sessionqueue = plugin->pending_Sessions;
972 while (sessionqueue != NULL)
974 session = sessionqueue->content;
976 pm = session->pending_message;
980 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
981 "pending message is empty, should not happen. session %p\n",
985 GNUNET_assert(pm != NULL);
987 //check for message timeout
988 if (GNUNET_TIME_absolute_get_remaining(pm->timeout).rel_value > 0)
990 //check if session has no message in the fragment queue
991 if (session->fragment_messages_out_count
992 < FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION)
994 plugin->pendingsessions--;
995 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions,
996 plugin->pending_Sessions_tail, sessionqueue);
997 GNUNET_free(sessionqueue);
1003 sessionqueue = sessionqueue->next;
1009 session->pending_message = session->pending_message2;
1010 session->pending_message2 = NULL;
1012 //call the cont func that it did not work
1013 if (pm->transmit_cont != NULL)
1014 pm->transmit_cont(pm->transmit_cont_cls, &(session->target),
1016 GNUNET_free(pm->msg);
1019 if (session->pending_message == NULL)
1021 sessionqueue_alt = sessionqueue;
1022 sessionqueue = sessionqueue->next;
1023 plugin->pendingsessions--;
1024 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions,
1025 plugin->pending_Sessions_tail, sessionqueue_alt);
1027 GNUNET_free(sessionqueue_alt);
1037 * Function to sort the message into the message fragment queue
1038 * @param plugin the plugin struct
1039 * @param fm message to sort into the queue
1042 sort_fragment_into_queue(struct Plugin * plugin, struct FragmentMessage * fm)
1044 struct FragmentMessage * fm2;
1045 //sort into the list at the right position
1047 fm2 = plugin->pending_Fragment_Messages_head;
1051 if (GNUNET_TIME_absolute_get_difference(fm2->next_ack, fm->next_ack).rel_value
1054 GNUNET_CONTAINER_DLL_insert_before(plugin->pending_Fragment_Messages_head,
1055 plugin->pending_Fragment_Messages_tail,fm2,fm);
1064 GNUNET_CONTAINER_DLL_insert_tail(plugin->pending_Fragment_Messages_head,
1065 plugin->pending_Fragment_Messages_tail,fm);
1069 * frees the space of a message in the fragment queue (send queue)
1070 * @param plugin the plugin struct
1071 * @param fm message to free
1074 free_fragment_message(struct Plugin * plugin, struct FragmentMessage * fm)
1078 (fm->session->fragment_messages_out_count)--;
1079 GNUNET_free_non_null(fm->msg);
1080 GNUNET_CONTAINER_DLL_remove (plugin->pending_Fragment_Messages_head,
1081 plugin->pending_Fragment_Messages_tail, fm);
1083 plugin->pending_fragment_messages--;
1086 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1087 "free pending fragment messages, pending messages remaining %u\n",
1088 plugin->pending_fragment_messages);
1094 * Function to check if there is some space in the fragment queue
1095 * inserts a message if space is available
1096 * @param plugin the plugin struct
1100 check_fragment_queue(struct Plugin * plugin)
1102 struct Session * session;
1103 struct FragmentMessage * fm;
1104 struct GNUNET_PeerIdentity pid;
1106 struct PendingMessage * pm;
1108 if (plugin->pending_fragment_messages < FRAGMENT_QUEUE_SIZE)
1110 session = get_next_queue_Session(plugin);
1111 if (session != NULL)
1113 pm = session->pending_message;
1114 session->pending_message = NULL;
1115 session->fragment_messages_out_count++;
1116 GNUNET_assert(pm != NULL);
1118 fm = GNUNET_malloc(sizeof(struct FragmentMessage));
1119 fm->message_size = pm->message_size;
1121 fm->session = session;
1122 fm->timeout.abs_value = pm->timeout.abs_value;
1123 fm->message_pos = 0;
1124 fm->next_ack = GNUNET_TIME_absolute_get();
1125 fm->message_id_out = get_next_message_id();
1126 fm->ack_bitfield = 0;
1128 sort_fragment_into_queue(plugin, fm);
1129 plugin->pending_fragment_messages++;
1131 if (pm->transmit_cont != NULL)
1133 pid = session->target;
1134 pm->transmit_cont(pm->transmit_cont_cls, &pid, GNUNET_OK);
1136 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1137 "called pm->transmit_cont for %p\n", session);
1143 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1144 "no pm->transmit_cont for %p\n", session);
1149 if (session->pending_message2 != NULL)
1151 session->pending_message = session->pending_message2;
1152 session->pending_message2 = NULL;
1154 queue_Session(plugin, session);
1157 //check if timeout changed
1158 check_next_fragment_timeout(plugin);
1164 * Funktion to check if all fragments where send and the acks received
1165 * frees the space if finished
1166 * @param plugin the plugin struct
1167 * @param fm the message to check
1170 check_finished_fragment(struct Plugin * plugin, struct FragmentMessage * fm)
1172 //maxack = size of message / max packet size, eg 12 / 5 = 2 start at 0 so ack numbers are 0,1,2
1173 unsigned int maxack = 63 - ((fm->message_size - 1) / (WLAN_MTU
1174 - sizeof(struct FragmentationHeader)));
1175 uint64_t tmpfield = UINT64_MAX;
1176 tmpfield = tmpfield >> maxack;
1180 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1181 "Test bitfields %X and %X, maxack is %u, fm size %u\n",
1182 fm->ack_bitfield, tmpfield, maxack, fm->message_size);
1185 if (fm->ack_bitfield == tmpfield)
1188 free_fragment_message(plugin, fm);
1191 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1192 "Finished sending and got all acks for a fragmented message\n");
1195 check_next_fragment_timeout(plugin);
1196 check_fragment_queue(plugin);
1202 * Function to set the next fragment number
1203 * @param fm use this FragmentMessage
1207 set_next_message_fragment_pos(struct FragmentMessage * fm)
1210 //check if retransmit is needed
1211 if (GNUNET_TIME_absolute_get_remaining(fm->next_ack).rel_value == 0)
1214 // be positive and try again later :-D
1215 fm->next_ack = GNUNET_TIME_relative_to_absolute(get_ack_timeout(fm));
1216 // find first missing fragment
1218 fm->message_pos = 0;
1221 //test if ack 0 (or X) was already received
1222 while (testBit((char*) &fm->ack_bitfield, fm->message_pos) == GNUNET_YES)
1230 send_hello_beacon(struct Plugin * plugin)
1234 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending hello beacon\n");
1239 struct GNUNET_MessageHeader * msgheader = NULL;
1240 struct ieee80211_frame * ieeewlanheader = NULL;
1241 struct Radiotap_Send * radioHeader = NULL;
1242 struct GNUNET_MessageHeader * msgheader2 = NULL;
1244 GNUNET_assert(sizeof(struct WlanHeader) + GNUNET_HELLO_size(
1245 *(plugin->env->our_hello)) <= WLAN_MTU);
1246 size = sizeof(struct GNUNET_MessageHeader) + sizeof(struct Radiotap_Send)
1247 + sizeof(struct ieee80211_frame) + sizeof(struct GNUNET_MessageHeader)
1248 + GNUNET_HELLO_size(*(plugin->env->our_hello));
1250 msgheader = GNUNET_malloc(size);
1251 msgheader->size = htons(size);
1252 msgheader->type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1254 radioHeader = (struct Radiotap_Send*) &msgheader[1];
1255 getRadiotapHeader(plugin, NULL, radioHeader);
1256 ieeewlanheader = (struct ieee80211_frame*) &radioHeader[1];
1257 getWlanHeader(ieeewlanheader, bc_all_mac, plugin);
1259 msgheader2 = (struct GNUNET_MessageHeader*) &ieeewlanheader[1];
1260 msgheader2->size = htons(GNUNET_HELLO_size(*(plugin->env->our_hello))
1261 + sizeof(struct GNUNET_MessageHeader));
1263 msgheader2->type = htons(GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT);
1264 memcpy(&msgheader2[1], *plugin->env->our_hello, GNUNET_HELLO_size(
1265 *(plugin->env->our_hello)));
1267 bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle, msgheader, size);
1269 if (bytes == GNUNET_SYSERR)
1271 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1272 _("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1273 errno, strerror(errno));
1276 GNUNET_assert(bytes != GNUNET_SYSERR);
1277 GNUNET_assert(bytes == size);
1278 GNUNET_free(msgheader);
1280 set_next_beacon_time(plugin);
1281 check_next_fragment_timeout(plugin);
1285 send_ack(struct Plugin * plugin, struct AckSendQueue * ack)
1290 struct GNUNET_MessageHeader * msgheader = NULL;
1291 struct ieee80211_frame * ieeewlanheader = NULL;
1292 struct Radiotap_Send * radioHeader = NULL;
1293 struct FragmentationAckHeader * msgheader2 = NULL;
1295 GNUNET_assert(sizeof(struct FragmentationAckHeader) <= WLAN_MTU);
1298 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1299 "Sending ack for message_id %u with fragment field %u\n",
1300 ack->message_id, ack->fragments_field);
1303 size = sizeof(struct GNUNET_MessageHeader) + sizeof(struct Radiotap_Send)
1304 + sizeof(struct ieee80211_frame) + sizeof(struct FragmentationAckHeader);
1305 msgheader = GNUNET_malloc(size);
1306 msgheader->size = htons(size);
1307 msgheader->type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1309 radioHeader = (struct Radiotap_Send*) &msgheader[1];
1310 getRadiotapHeader(plugin, ack->session, radioHeader);
1311 ieeewlanheader = (struct ieee80211_frame*) &radioHeader[1];
1312 getWlanHeader(ieeewlanheader, ack->session->addr, plugin);
1314 msgheader2 = (struct FragmentationAckHeader*) &ieeewlanheader[1];
1315 msgheader2->header.size = htons(sizeof(struct FragmentationAckHeader));
1316 msgheader2->header.type = htons(GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT_ACK);
1317 msgheader2->message_id = htonl(ack->message_id);
1318 msgheader2->fragment_field = htonll(ack->fragments_field);
1320 bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle, msgheader, size);
1321 if (bytes == GNUNET_SYSERR)
1323 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1324 _("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1325 errno, strerror(errno));
1328 GNUNET_assert(bytes != GNUNET_SYSERR);
1329 GNUNET_assert(bytes == size);
1330 GNUNET_free(msgheader);
1331 check_next_fragment_timeout(plugin);
1335 * Function called when wlan helper is ready to get some data
1337 * @param cls closure
1338 * @param tc GNUNET_SCHEDULER_TaskContext
1342 do_transmit(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1345 struct Plugin * plugin = cls;
1346 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1348 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1351 struct Session * session = NULL;
1352 struct FragmentMessage * fm = NULL;
1353 struct ieee80211_frame * ieeewlanheader = NULL;
1354 struct Radiotap_Send * radioHeader = NULL;
1355 struct GNUNET_MessageHeader * msgheader = NULL;
1357 struct FragmentationHeader fragheader;
1358 struct FragmentationHeader * fragheaderptr = NULL;
1359 struct Finish_send * finish = NULL;
1360 struct AckSendQueue * ack;
1363 const char * copystart = NULL;
1364 uint16_t copysize = 0;
1365 uint copyoffset = 0;
1367 if (plugin->ack_send_queue_head != NULL)
1369 ack = plugin->ack_send_queue_head;
1370 GNUNET_CONTAINER_DLL_remove(plugin->ack_send_queue_head,
1371 plugin->ack_send_queue_tail, ack);
1372 send_ack(plugin, ack);
1377 //test if a "hello-beacon" has to be send
1378 if (GNUNET_TIME_absolute_get_remaining(plugin->beacon_time).rel_value == 0)
1380 send_hello_beacon(plugin);
1386 fm = plugin->pending_Fragment_Messages_head;
1389 session = fm->session;
1390 GNUNET_assert(session != NULL);
1392 // test if message timed out
1393 if (GNUNET_TIME_absolute_get_remaining(fm->timeout).rel_value == 0)
1396 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "message timeout\n");
1399 check_fragment_queue(plugin);
1400 free_fragment_message(plugin, fm);
1406 //if (fm->message_size > WLAN_MTU)
1408 size += sizeof(struct FragmentationHeader);
1410 set_next_message_fragment_pos(fm);
1412 copyoffset = (WLAN_MTU - sizeof(struct FragmentationHeader))
1417 GNUNET_ERROR_TYPE_DEBUG,
1418 "Sending GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT with message_id %u with fragment number %i, size: %u, offset %u, time until timeout %u\n",
1419 fm->message_id_out, fm->message_pos, copysize
1420 + sizeof(struct FragmentationHeader), copyoffset,
1421 GNUNET_TIME_absolute_get_remaining(fm->timeout));
1424 fragheader.fragment_off_or_num = htons(fm->message_pos);
1425 fragheader.message_id = htonl(fm->message_id_out);
1426 copystart = fm->msg + copyoffset;
1427 copysize = GNUNET_MIN(fm->message_size - copyoffset,
1428 WLAN_MTU - sizeof(struct FragmentationHeader));
1430 if (copyoffset >= fm->message_size)
1433 GNUNET_ERROR_TYPE_ERROR,
1434 "offset in message for fragment too large, offset %u, size %u, max size %u, copysize %u, message_pos %u,\n",
1435 copyoffset, fm->message_size, WLAN_MTU
1436 - sizeof(struct FragmentationHeader), copysize,
1439 GNUNET_assert(copyoffset < fm->message_size);
1440 //FIXME remove later
1441 GNUNET_assert(copystart < fm->msg + fm->message_size);
1443 fragheader.header.size = htons(copysize
1444 + sizeof(struct FragmentationHeader));
1445 fragheader.header.type = htons(GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT);
1450 // there is no need to split
1451 copystart = fm->msg;
1452 copysize = fm->message_size;
1456 size += sizeof(struct Radiotap_Send) + sizeof(struct ieee80211_frame)
1457 + sizeof(struct GNUNET_MessageHeader);
1458 msgheader = GNUNET_malloc(size);
1459 msgheader->size = htons(size);
1460 msgheader->type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1462 radioHeader = (struct Radiotap_Send*) &msgheader[1];
1463 getRadiotapHeader(plugin, session, radioHeader);
1465 ieeewlanheader = (struct ieee80211_frame *) &radioHeader[1];
1466 getWlanHeader(ieeewlanheader, fm->session->addr, plugin);
1468 //could be faster if content is just send and not copyed before
1469 //fragmentheader is needed
1470 //if (fm->message_size > WLAN_MTU)
1472 fragheader.message_crc = htons(getcrc16(copystart, copysize));
1473 memcpy(&ieeewlanheader[1], &fragheader,
1474 sizeof(struct FragmentationHeader));
1475 fragheaderptr = (struct FragmentationHeader *) &ieeewlanheader[1];
1476 memcpy(&fragheaderptr[1], copystart, copysize);
1480 memcpy(&ieeewlanheader[1], copystart, copysize);
1483 bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle,
1485 if (bytes == GNUNET_SYSERR)
1488 GNUNET_ERROR_TYPE_ERROR,
1489 _("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1490 errno, strerror(errno));
1493 GNUNET_assert(bytes != GNUNET_SYSERR);
1497 finish = GNUNET_malloc(sizeof( struct Finish_send));
1498 finish->plugin = plugin;
1499 finish->msgheader = (char *) msgheader + bytes;
1500 finish->size = size - bytes;
1501 finish->msgstart = msgheader;
1503 GNUNET_assert(plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK);
1505 plugin->server_write_task = GNUNET_SCHEDULER_add_write_file(
1506 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdin_handle,
1507 &finish_sending, finish);
1512 GNUNET_assert(bytes == size);
1514 GNUNET_free(msgheader);
1515 check_next_fragment_timeout(plugin);
1518 //check if this was the last fragment of this message, if true then queue at the end of the list
1519 if (copysize + copyoffset >= fm->message_size)
1521 GNUNET_assert(copysize + copyoffset == fm->message_size);
1523 GNUNET_CONTAINER_DLL_remove (plugin->pending_Fragment_Messages_head,
1524 plugin->pending_Fragment_Messages_tail, fm);
1526 GNUNET_CONTAINER_DLL_insert_tail(plugin->pending_Fragment_Messages_head,
1527 plugin->pending_Fragment_Messages_tail, fm);
1528 // if fragments have opimized timeouts
1529 //sort_fragment_into_queue(plugin,fm);
1536 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1537 "do_transmit did nothing, should not happen!\n");
1541 finish_sending(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1543 struct Finish_send * finish;
1544 struct Plugin * plugin;
1548 plugin = finish->plugin;
1550 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1552 bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle,
1553 finish->msgheader, finish->size);
1554 GNUNET_assert(bytes != GNUNET_SYSERR);
1556 GNUNET_assert(plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK);
1557 if (bytes != finish->size)
1560 finish->msgheader = finish->msgheader + bytes;
1561 finish->size = finish->size - bytes;
1562 plugin->server_write_task = GNUNET_SCHEDULER_add_write_file(
1563 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdin_handle,
1564 &finish_sending, finish);
1568 GNUNET_free(finish->msgstart);
1569 GNUNET_free(finish);
1570 check_next_fragment_timeout(plugin);
1576 getRadiotapHeader(struct Plugin * plugin, struct Session * session,
1577 struct Radiotap_Send * header)
1580 if (session != NULL)
1582 header->rate = session->rate;
1583 header->tx_power = session->tx_power;
1584 header->antenna = session->antenna;
1589 header->tx_power = 0;
1590 header->antenna = 0;
1598 * function to generate the wlan hardware header for one packet
1599 * @param Header address to write the header to
1600 * @param to_mac_addr address of the recipient
1601 * @param plugin pointer to the plugin struct
1602 * @return GNUNET_YES if there was no error
1606 getWlanHeader(struct ieee80211_frame * Header, const char * const to_mac_addr,
1607 struct Plugin * plugin)
1609 Header->i_fc[0] = 0x80;
1610 Header->i_fc[1] = 0x00;
1611 memcpy(&Header->i_addr3, mac_bssid, sizeof(mac_bssid));
1612 memcpy(&Header->i_addr2, plugin->mac_address.mac, sizeof(plugin->mac_address));
1613 memcpy(&Header->i_addr1, to_mac_addr, sizeof(plugin->mac_address));
1620 * @param msgbuf pointer tor the data
1621 * @param msgbuf_size size of the data
1623 * @return 32bit crc value
1627 getcrc32(const char *msgbuf, size_t msgbuf_size)
1629 //TODO calc some crc
1636 * @param msgbuf pointer tor the data
1637 * @param msgbuf_size size of the data
1639 * @return 16bit crc value
1643 getcrc16(const char *msgbuf, size_t msgbuf_size)
1645 //TODO calc some crc
1650 * Function that can be used by the transport service to transmit
1651 * a message using the plugin.
1653 * @param cls closure
1654 * @param target who should receive this message
1655 * @param priority how important is the message
1656 * @param msgbuf the message to transmit
1657 * @param msgbuf_size number of bytes in 'msgbuf'
1658 * @param timeout when should we time out
1659 * @param session which session must be used (or NULL for "any")
1660 * @param addr the address to use (can be NULL if the plugin
1661 * is "on its own" (i.e. re-use existing TCP connection))
1662 * @param addrlen length of the address in bytes
1663 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1664 * otherwise the plugin may use other addresses or
1665 * existing connections (if available)
1666 * @param cont continuation to call once the message has
1667 * been transmitted (or if the transport is ready
1668 * for the next transmission call; or if the
1669 * peer disconnected...)
1670 * @param cont_cls closure for cont
1671 * @return number of bytes used (on the physical network, with overheads);
1672 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1673 * and does NOT mean that the message was not transmitted (DV)
1676 wlan_plugin_send(void *cls, const struct GNUNET_PeerIdentity * target,
1677 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
1678 struct GNUNET_TIME_Relative timeout, struct Session *session,
1679 const void *addr, size_t addrlen, int force_address,
1680 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1682 struct Plugin * plugin = cls;
1683 struct PendingMessage * newmsg = NULL;
1684 struct WlanHeader * wlanheader = NULL;
1686 //check if msglen > 0
1687 GNUNET_assert(msgbuf_size > 0);
1689 //get session if needed
1690 if (session == NULL)
1692 if (wlan_plugin_address_suggested(plugin, addr, addrlen) == GNUNET_OK)
1694 session = get_Session(plugin, addr);
1698 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1699 _("Wlan Address len %d is wrong\n"), addrlen);
1704 //TODO target "problem" not solved
1705 //if (session->target != NULL){
1706 // GNUNET_assert(session->target == *target);
1708 session->target = *target;
1714 //queue message in session
1715 //test if there is no other message in the "queue"
1716 //FIXME: to many send requests
1717 //GNUNET_assert (session->pending_message == NULL);
1718 if (session->pending_message != NULL)
1720 newmsg = session->pending_message;
1722 GNUNET_ERROR_TYPE_ERROR,
1723 "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",
1724 GNUNET_TIME_absolute_get_remaining(newmsg->timeout).rel_value,
1725 session->fragment_messages_out_count);
1726 if (session->pending_message2 != NULL)
1729 GNUNET_ERROR_TYPE_ERROR,
1730 "wlan_plugin_send: two pending messages are already in the queue for this client\n");
1735 newmsg = GNUNET_malloc(sizeof(struct PendingMessage));
1736 (newmsg->msg) = GNUNET_malloc(msgbuf_size + sizeof(struct WlanHeader));
1737 wlanheader = (struct WlanHeader *) newmsg->msg;
1738 //copy msg to buffer, not fragmented / segmented yet, but with message header
1739 wlanheader->header.size = htons(msgbuf_size + sizeof(struct WlanHeader));
1740 wlanheader->header.type = htons(GNUNET_MESSAGE_TYPE_WLAN_DATA);
1741 memcpy(&(wlanheader->target), target, sizeof(struct GNUNET_PeerIdentity));
1742 wlanheader->crc = htonl(getcrc32(msgbuf, msgbuf_size));
1743 memcpy(&wlanheader[1], msgbuf, msgbuf_size);
1744 newmsg->transmit_cont = cont;
1745 newmsg->transmit_cont_cls = cont_cls;
1746 newmsg->timeout = GNUNET_TIME_relative_to_absolute(timeout);
1748 newmsg->timeout.abs_value = newmsg->timeout.abs_value - 500;
1750 newmsg->message_size = msgbuf_size + sizeof(struct WlanHeader);
1752 if (session->pending_message == NULL)
1754 session->pending_message = newmsg;
1758 session->pending_message2 = newmsg;
1762 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1763 "New message for %p with size (incl wlan header) %u added\n", session,
1764 newmsg->message_size);
1768 queue_Session(plugin, session);
1770 check_fragment_queue(plugin);
1771 //FIXME not the correct size
1777 * function to get the first message in the fragement queue (out) of a session
1778 * @param session pointer to the session
1779 * @return pointer to the struct FragmentMessage
1781 static struct FragmentMessage *
1782 get_fragment_message_from_session(struct Session * session)
1784 struct FragmentMessage * fm = session->plugin->pending_Fragment_Messages_head;
1787 if (fm->session == session)
1797 * function to get the message in the fragement queue (out) of a session with a specific id
1798 * @param session pointer to the session
1799 * @param message_id id of the message
1800 * @return pointer to the struct FragmentMessage
1802 static struct FragmentMessage *
1803 get_fragment_message_from_session_and_id(struct Session * session,
1804 uint32_t message_id)
1806 struct FragmentMessage * fm = session->plugin->pending_Fragment_Messages_head;
1809 if ((fm->session == session) && (fm->message_id_out == message_id))
1819 * Function that can be used to force the plugin to disconnect
1820 * from the given peer and cancel all previous transmissions
1821 * (and their continuation).
1823 * @param cls closure
1824 * @param target peer from which to disconnect
1827 wlan_plugin_disconnect(void *cls, const struct GNUNET_PeerIdentity *target)
1829 struct Plugin *plugin = cls;
1830 struct Sessionqueue * queue = plugin->sessions;
1831 struct Sessionqueue * pendingsession = plugin->pending_Sessions;
1832 struct PendingMessage * pm = NULL;
1833 struct FragmentMessage * fm;
1834 struct Receive_Message_Queue * receive_queue;
1836 // just look at all the session for the needed one
1837 while (queue != NULL)
1839 // content is never NULL
1840 GNUNET_assert (queue->content != NULL);
1841 if (memcmp(target, &(queue->content->target),
1842 sizeof(struct GNUNET_PeerIdentity)) == 0)
1845 //is this session pending for send
1846 while (pendingsession != NULL)
1848 if (pendingsession->content == queue->content)
1850 plugin->pendingsessions--;
1851 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions,
1852 plugin->pending_Sessions_tail, pendingsession);
1853 GNUNET_free(pendingsession);
1856 pendingsession = pendingsession->next;
1859 //is something of this session in the fragment queue?
1860 fm = get_fragment_message_from_session(queue->content);
1863 free_fragment_message(plugin, fm);
1864 fm = get_fragment_message_from_session(queue->content);
1866 check_next_fragment_timeout(plugin);
1868 //dispose all received fragments
1869 receive_queue = get_receive_message_from_session(plugin,
1871 while (receive_queue != NULL)
1873 free_receive_message(plugin, receive_queue);
1874 receive_queue = get_receive_message_from_session(plugin,
1878 // remove PendingMessage
1879 pm = queue->content->pending_message;
1882 GNUNET_free_non_null(pm->msg);
1886 GNUNET_free(queue->content);
1887 GNUNET_CONTAINER_DLL_remove(plugin->sessions, plugin->sessions_tail, queue);
1889 plugin->session_count--;
1894 queue = queue->next;
1899 * Convert the transports address to a nice, human-readable
1902 * @param cls closure
1903 * @param type name of the transport that generated the address
1904 * @param addr one of the addresses of the host, NULL for the last address
1905 * the specific address format depends on the transport
1906 * @param addrlen length of the address
1907 * @param numeric should (IP) addresses be displayed in numeric form?
1908 * @param timeout after how long should we give up?
1909 * @param asc function to call on each string
1910 * @param asc_cls closure for asc
1913 wlan_plugin_address_pretty_printer(void *cls, const char *type,
1914 const void *addr, size_t addrlen, int numeric,
1915 struct GNUNET_TIME_Relative timeout,
1916 GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
1919 const unsigned char * input;
1921 //GNUNET_assert(cls !=NULL);
1924 /* invalid address (MAC addresses have 6 bytes) */
1929 input = (const unsigned char*) addr;
1930 GNUNET_snprintf(ret, sizeof(ret),
1931 "%s Mac-Address %.2X:%.2X:%.2X:%.2X:%.2X:%.2X", PROTOCOL_PREFIX,
1932 input[0], input[1], input[2], input[3], input[4], input[5]);
1937 * Another peer has suggested an address for this
1938 * peer and transport plugin. Check that this could be a valid
1939 * address. If so, consider adding it to the list
1942 * @param cls closure
1943 * @param addr pointer to the address
1944 * @param addrlen length of addr
1945 * @return GNUNET_OK if this is a plausible address for this peer
1950 wlan_plugin_address_suggested(void *cls, const void *addr, size_t addrlen)
1952 //struct Plugin *plugin = cls;
1954 /* check if the address is plausible; if so,
1955 add it to our list! */
1957 GNUNET_assert(cls !=NULL);
1958 //FIXME mitm is not checked
1959 //Mac Address has 6 bytes
1962 /* TODO check for bad addresses like multicast, broadcast, etc */
1966 return GNUNET_SYSERR;
1970 * Function called for a quick conversion of the binary address to
1971 * a numeric address. Note that the caller must not free the
1972 * address and that the next call to this function is allowed
1973 * to override the address again.
1975 * @param cls closure
1976 * @param addr binary address
1977 * @param addrlen length of the address
1978 * @return string representing the same address
1981 wlan_plugin_address_to_string(void *cls, const void *addr, size_t addrlen)
1983 static char ret[40];
1984 const struct MacAddress *mac;
1986 if (addrlen != sizeof(struct MacAddress))
1992 GNUNET_snprintf(ret, sizeof(ret), "%s Mac-Address %X:%X:%X:%X:%X:%X",
1993 PROTOCOL_PREFIX, mac->mac[0], mac->mac[1], mac->mac[2], mac->mac[3],
1994 mac->mac[4], mac->mac[5]);
1999 * function to check if bitfield is representation of fragments of the message
2000 * @param rec_message message to check
2004 check_message_fragment_bitfield(struct Receive_Message_Queue * rec_message)
2006 uint64_t checkfragments = 0;
2007 struct Receive_Fragment_Queue * rec_queue = rec_message->frag_head;
2009 while (rec_queue != NULL)
2011 setBit((char*) &checkfragments, rec_queue->num);
2012 rec_queue = rec_queue->next;
2015 GNUNET_assert(checkfragments == rec_message->received_fragments);
2019 * Function to test if fragment number already exists in the fragments received
2021 * @param rec_message message this fragment belongs to
2022 * @param fh Fragmentheader of the fragment
2023 * @return GNUNET_YES if fragment exists already, GNUNET_NO if it does not exists in the queue of the session
2027 is_double_msg(struct Receive_Message_Queue * rec_message,
2028 struct FragmentationHeader * fh)
2030 //test if bitfield is okay
2032 check_message_fragment_bitfield(rec_message);
2035 return testBit((char *) &rec_message->received_fragments, ntohs(
2036 fh->fragment_off_or_num));
2041 * Function to insert a fragment in a queue of a message
2042 * @param session session the fragment belongs to
2043 * @param rec_queue fragment to add
2047 insert_fragment_in_queue(struct Receive_Message_Queue * rec_message,
2048 struct Receive_Fragment_Queue * rec_queue)
2050 GNUNET_assert(rec_message != NULL);
2051 GNUNET_assert(rec_queue != NULL);
2053 struct Receive_Fragment_Queue * rec_queue2 = rec_message->frag_head;
2054 struct WlanHeader * wlanheader = NULL;
2056 //this is the first fragment of the message (fragment id 0)
2057 if (rec_queue->num == 0)
2059 wlanheader = (struct WlanHeader *) rec_queue->msg;
2060 rec_message->rec_size = ntohs(wlanheader->header.size);
2064 while (rec_queue2 != NULL)
2066 if (rec_queue2->num > rec_queue->num)
2068 //next element number is grater than the current num
2069 GNUNET_CONTAINER_DLL_insert_before(rec_message->frag_head, rec_message->frag_tail, rec_queue2, rec_queue);
2070 setBit((char *) &rec_message->received_fragments, rec_queue->num);
2073 rec_queue = rec_queue->next;
2075 //no element has a grater number
2076 GNUNET_CONTAINER_DLL_insert_tail(rec_message->frag_head, rec_message->frag_tail, rec_queue);
2078 setBit((char *) &rec_message->received_fragments, rec_queue->num);
2082 * Function to dispose the fragments received for a message and the message
2083 * @param plugin pointer to the plugin struct
2084 * @param rec_message pointer to the struct holding the message which should be freed
2088 free_receive_message(struct Plugin* plugin,
2089 struct Receive_Message_Queue * rx_message)
2091 GNUNET_assert(rx_message !=NULL);
2092 struct Receive_Fragment_Queue * rec_queue = rx_message->frag_head;
2093 struct Receive_Fragment_Queue * rec_queue2;
2095 while (rec_queue != NULL)
2097 rec_queue2 = rec_queue;
2098 rec_queue = rec_queue->next;
2099 GNUNET_free(rec_queue2);
2102 GNUNET_CONTAINER_DLL_remove(plugin->receive_messages_head,plugin->receive_messages_teil, rx_message);
2104 GNUNET_assert(plugin->pending_receive_messages > 0);
2105 GNUNET_assert(rx_message->session->fragment_messages_in_count > 0);
2107 plugin->pending_receive_messages--;
2108 rx_message->session->fragment_messages_in_count--;
2109 GNUNET_free(rx_message);
2113 * Function to check if all fragments of a message have been received
2114 * @param plugin the plugin handle
2115 * @param session_light information of the message sender
2116 * @param session session the message belongs to
2117 * @param rec_message pointer to the message that should be checked
2121 check_rec_finished_msg(struct Plugin* plugin,
2122 struct Session_light * session_light, struct Session * session,
2123 struct Receive_Message_Queue * rx_message)
2125 GNUNET_assert(rx_message !=NULL);
2127 struct Receive_Fragment_Queue * rx_frag = rx_message->frag_head;
2128 int packetsize = rx_message->rec_size;
2131 uint64_t bitfield = 0;
2134 //check if first fragment is present
2135 if (packetsize == MESSAGE_LENGHT_UNKNOWN)
2139 // test if message has at least the size of the WlanHeader and a GNUNET_MessageHeader
2140 else if (packetsize < sizeof(struct WlanHeader)
2141 + sizeof(struct GNUNET_MessageHeader))
2144 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Message not big enough\n");
2146 free_receive_message(plugin, rx_message);
2151 check_message_fragment_bitfield(rx_message);
2154 if (rx_message->frag_tail->num == ffsl(rx_message->received_fragments))
2156 bitfield = ~bitfield;
2157 bitfield = bitfield << (63 - ffsl(rx_message->received_fragments));
2158 if (rx_message->received_fragments == bitfield)
2161 while (rx_frag != NULL)
2163 sum += rx_frag->size;
2164 rx_frag = rx_frag->next;
2166 //sum should always be smaller or equal of
2167 GNUNET_assert(sum <= packetsize);
2168 if (sum == packetsize)
2172 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2173 "check_rec_finished_msg: A message for %p is complete\n",
2178 //copy fragments together
2179 msg = GNUNET_malloc(packetsize);
2180 rx_frag = rx_message->frag_head;
2182 while (rx_frag != NULL)
2184 //TODO SAVE SOME COPY OPS AND CHECK CRC WITHOUT COPY
2185 memcpy(msg + aktnum, rx_frag->msg, rx_frag->size);
2186 aktnum += rx_frag->size;
2187 rx_frag = rx_frag->next;
2190 free_receive_message(plugin, rx_message);
2191 //call wlan_process_helper to process the message
2192 wlan_data_massage_handler(plugin, session_light,
2193 (struct GNUNET_MessageHeader*) msg);
2194 //wlan_data_helper (plugin, session_light, (struct GNUNET_MessageHeader*) msg);
2203 process_data(void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
2206 GNUNET_assert(client != NULL);
2207 GNUNET_assert(cls != NULL);
2208 struct Session * session = (struct Session *) client;
2209 struct Plugin * plugin = (struct Plugin *) cls;
2211 struct GNUNET_TRANSPORT_ATS_Information distance[2];
2212 distance[0].type = htonl(GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2213 distance[0].value = htonl(1);
2214 distance[1].type = htonl(GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2215 distance[1].value = htonl(0);
2218 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2219 "Calling plugin->env->receive for session %p; %s\n", session,
2220 wlan_plugin_address_to_string(NULL, session->addr, 6));
2223 plugin->env->receive(plugin->env->cls, &(session->target), hdr,
2224 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance, 2, session,
2225 session->addr, sizeof(session->addr));
2229 * handels the data after all fragments are put together
2231 * @param session_light
2232 * @param hdr pointer to the data
2235 wlan_data_massage_handler(struct Plugin * plugin,
2236 struct Session_light * session_light,
2237 const struct GNUNET_MessageHeader * hdr)
2239 struct WlanHeader * wlanheader = NULL;
2240 struct Session * session = NULL;
2241 const char * tempmsg = NULL;
2242 const struct GNUNET_MessageHeader * temp_hdr = NULL;
2243 struct GNUNET_PeerIdentity tmptarget;
2245 if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_DATA)
2250 GNUNET_ERROR_TYPE_DEBUG,
2251 "Func wlan_data_massage_handler got GNUNET_MESSAGE_TYPE_WLAN_DATA size: %u\n",
2255 if (ntohs(hdr->size) < sizeof(struct WlanHeader)
2256 + sizeof(struct GNUNET_MessageHeader))
2258 //packet not big enought
2262 GNUNET_assert(session_light != NULL);
2263 if (session_light->session == NULL)
2265 session_light->session = search_session(plugin, session_light->addr);
2267 session = session_light->session;
2268 wlanheader = (struct WlanHeader *) hdr;
2270 tempmsg = (char*) &wlanheader[1];
2271 temp_hdr = (const struct GNUNET_MessageHeader *) &wlanheader[1];
2273 if (getcrc32(tempmsg, ntohs(wlanheader->header.size)) != ntohl(
2276 //wrong crc, dispose message
2277 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2278 "Wlan message Header crc was wrong\n");
2282 //if not in session list
2283 if (session == NULL)
2287 GNUNET_ERROR_TYPE_DEBUG,
2288 "WLAN client not in session list: packet size = %u, inner size = %u, header size = %u\n",
2289 ntohs(wlanheader->header.size), ntohs(temp_hdr->size),
2290 sizeof(struct WlanHeader));
2292 //try if it is a hello message
2293 if (ntohs(wlanheader->header.size) >= ntohs(temp_hdr->size)
2294 + sizeof(struct WlanHeader))
2296 if (ntohs(temp_hdr->type) == GNUNET_MESSAGE_TYPE_HELLO)
2298 if (GNUNET_HELLO_get_id(
2299 (const struct GNUNET_HELLO_Message *) temp_hdr,
2300 &tmptarget) == GNUNET_OK)
2302 session = create_session(plugin, session_light->addr);
2303 session_light->session = session;
2304 memcpy(&session->target, &tmptarget,
2305 sizeof(struct GNUNET_PeerIdentity));
2309 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2310 "WLAN client not in session list and hello message not okay\n");
2317 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2318 "WLAN client not in session list and not a hello message\n");
2325 GNUNET_ERROR_TYPE_WARNING,
2326 "WLAN client not in session list and message size in does not fit\npacket size = %u, inner size = %u, header size = %u\n",
2327 ntohs(wlanheader->header.size), ntohs(temp_hdr->size),
2328 sizeof(struct WlanHeader));
2333 //"receive" the message
2336 GNUNET_SERVER_mst_receive(plugin->data_tokenizer, session,
2337 (const char *) temp_hdr,
2338 ntohs(hdr->size) - sizeof(struct WlanHeader), GNUNET_YES, GNUNET_NO);
2344 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2345 "wlan_data_massage_handler got wrong message type\n");
2351 * function to add an ack to send it for a received fragment
2352 * @param plugin pointer to the global plugin structure
2353 * @param session pointer to the session this ack belongs to
2354 * @param bitfield bitfield to send
2355 * @param fh pointer to the fragmentation header which we would like to acknolage
2359 add_ack_for_send(struct Plugin * plugin, struct Session * session,
2360 uint64_t bitfield, struct FragmentationHeader * fh)
2362 struct AckSendQueue * ack;
2364 GNUNET_assert(plugin != NULL);
2365 GNUNET_assert(session != NULL);
2366 GNUNET_assert(fh != NULL);
2368 ack = GNUNET_malloc(sizeof(struct AckSendQueue));
2369 ack->fragments_field = bitfield;
2370 ack->message_id = ntohl(fh->message_id);
2371 ack->session = session;
2373 GNUNET_CONTAINER_DLL_insert_tail(plugin->ack_send_queue_head,
2374 plugin->ack_send_queue_tail, ack);
2379 * function to get the receive message from the message id and the session
2380 * @param plugin pointer to the plugin struct
2381 * @param session session this fragment belongs to
2382 * @param message_id id of the message
2385 struct Receive_Message_Queue *
2386 get_receive_message(struct Plugin * plugin, struct Session * session,
2387 uint32_t message_id)
2389 struct Receive_Message_Queue * rec_message = plugin->receive_messages_head;
2390 while (rec_message != NULL)
2392 if ((rec_message->message_id_in == message_id) && (rec_message->session
2397 rec_message = rec_message->next;
2403 * function to get the receive message of a session
2404 * @param plugin pointer to the plugin struct
2405 * @param session session this fragment belongs to
2408 struct Receive_Message_Queue *
2409 get_receive_message_from_session(struct Plugin * plugin,
2410 struct Session * session)
2412 struct Receive_Message_Queue * rec_message = plugin->receive_messages_head;
2413 while (rec_message != NULL)
2415 if (rec_message->session == session)
2419 rec_message = rec_message->next;
2425 * function to insert a received fragment into the right fragment queue of the right message
2426 * @param plugin pointer to the plugin struct
2427 * @param session_light pointer to the session_light struct of this message
2428 * @param session session this fragment belongs to
2429 * @param fh pointer to the header of the fragment
2430 * @return new fragment bitfield for the message
2434 insert_fragment_in_in_message_queue(struct Plugin * plugin,
2435 struct Session_light * session_light, struct Session * session,
2436 struct FragmentationHeader * fh, const struct Radiotap_rx * rxinfo)
2438 struct Receive_Fragment_Queue * rx_frag = NULL;
2439 struct Receive_Message_Queue * rx_message;
2440 const char * tempmsg = (char*) &fh[1];
2441 uint64_t retval = 0;
2443 //TODO fragments do not timeout
2444 //check if message_id is right or it is a new msg
2445 GNUNET_assert(fh != NULL);
2447 rx_message = get_receive_message(plugin, session, ntohs(fh->message_id));
2449 if (rx_message == NULL)
2451 if (session->fragment_messages_in_count < MESSAGES_IN_QUEUE_PER_SESSION)
2454 //new message incoming
2455 rx_message = GNUNET_malloc(sizeof (struct Receive_Message_Queue));
2456 rx_message->message_id_in = ntohs(fh->message_id);
2457 rx_message->rec_size = MESSAGE_LENGHT_UNKNOWN;
2458 rx_message->session = session;
2459 rx_message->timeout = GNUNET_TIME_absolute_add(
2460 GNUNET_TIME_absolute_get(), MESSAGE_IN_TIMEOUT);
2461 rx_message->received_fragments = 0;
2463 GNUNET_CONTAINER_DLL_insert(plugin->receive_messages_head, plugin->receive_messages_teil, rx_message);
2465 session->fragment_messages_in_count++;
2466 plugin->pending_receive_messages++;
2470 GNUNET_ERROR_TYPE_DEBUG,
2471 "New fragmented message started: message id %u, messages in for this session %u, messages in %u\n",
2472 rx_message->message_id_in, session->fragment_messages_in_count,
2473 plugin->pending_receive_messages);
2480 GNUNET_ERROR_TYPE_INFO,
2481 "WLAN fragment message_id and session message_id do not exist, max MESSAGES_IN_QUEUE_PER_SESSION reached\n");
2482 setBit((char *) &retval, ntohs(fh->fragment_off_or_num));
2487 if (is_double_msg(rx_message, fh) != GNUNET_YES)
2491 rx_frag = GNUNET_malloc(sizeof (struct Receive_Fragment_Queue) +
2492 ntohs(fh->header.size) - sizeof(struct FragmentationHeader));
2493 rx_frag->size = ntohs(fh->header.size)
2494 - sizeof(struct FragmentationHeader);
2495 rx_frag->num = ntohs(fh->fragment_off_or_num);
2496 rx_frag->msg = (char*) &(rx_frag[1]);
2497 //copy msg to buffer
2498 memcpy((char *) rx_frag->msg, tempmsg, rx_frag->size);
2499 memcpy((char *) &(rx_frag->rxinfo), rxinfo, sizeof(rxinfo));
2500 insert_fragment_in_queue(rx_message, rx_frag);
2502 retval = rx_message->received_fragments;
2506 GNUNET_ERROR_TYPE_DEBUG,
2507 "New fragment: size %u, fragsize %u, message id %u, bitfield %X, session %u\n",
2508 rx_message->rec_size, rx_frag->size, rx_message->received_fragments,
2509 rx_message->message_id_in, session);
2512 check_rec_finished_msg(plugin, session_light, session, rx_message);
2516 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "WLAN fragment is a clone\n");
2517 retval = rx_message->received_fragments;
2525 * Function used for to process the data received from the wlan interface
2527 * @param cls the plugin handle
2528 * @param session_light FIXME: document
2529 * @param hdr hdr of the GNUNET_MessageHeader
2532 wlan_data_helper(void *cls, struct Session_light * session_light,
2533 const struct GNUNET_MessageHeader * hdr, const struct Radiotap_rx * rxinfo)
2535 struct Plugin *plugin = cls;
2536 struct Session * session = NULL;
2538 struct FragmentationHeader * fh = NULL;
2539 struct FragmentationAckHeader * fah = NULL;
2540 struct FragmentMessage * fm = NULL;
2542 const char * tempmsg = NULL;
2544 uint64_t fragment_bitfield = 0;
2547 if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT)
2550 //TODO better DOS protection, error handling
2551 //TODO test first than create session
2552 GNUNET_assert(session_light != NULL);
2556 GNUNET_ERROR_TYPE_DEBUG,
2557 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT size: %u; %s\n",
2558 ntohs(hdr->size), wlan_plugin_address_to_string(NULL,
2559 session_light->addr, 6));
2562 if (session_light->session == NULL)
2564 session_light->session = get_Session(plugin, session_light->addr);
2566 GNUNET_assert(GNUNET_HELLO_get_id(
2567 (const struct GNUNET_HELLO_Message *) &hdr[1],
2568 &(session_light->session->target) ) != GNUNET_SYSERR);
2573 else if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT)
2576 GNUNET_assert(session_light != NULL);
2577 if (session_light->session == NULL)
2579 session_light->session = search_session(plugin, session_light->addr);
2581 session = session_light->session;
2583 fh = (struct FragmentationHeader *) hdr;
2584 tempmsg = (char*) &fh[1];
2588 GNUNET_ERROR_TYPE_DEBUG,
2589 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT with message_id %u with fragment number %i, size: %u; %s\n",
2590 ntohl(fh->message_id), ntohs(fh->fragment_off_or_num), ntohs(
2591 hdr->size), wlan_plugin_address_to_string(NULL,
2592 session_light->addr, 6));
2595 if (getcrc16(tempmsg, ntohs(fh->header.size)) != ntohs(fh->message_crc))
2597 //wrong crc, dispose message
2598 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "WLAN fragment crc was wrong\n");
2602 //if in the session list
2603 if (session != NULL)
2605 fragment_bitfield = insert_fragment_in_in_message_queue(plugin,
2606 session_light, session, fh, rxinfo);
2612 GNUNET_ERROR_TYPE_INFO,
2613 "WLAN client not in session list, fragment num %u, message id %u\n",
2614 ntohs(fh->fragment_off_or_num), ntohs(fh->message_id));
2615 wlan_data_massage_handler(plugin, session_light,
2616 (struct GNUNET_MessageHeader *) tempmsg);
2617 session = session_light->session;
2618 //test if a session was created
2619 if (session == NULL)
2623 setBit((char *) &fragment_bitfield, ntohs(fh->fragment_off_or_num));
2626 add_ack_for_send(plugin, session, fragment_bitfield, fh);
2627 check_next_fragment_timeout(plugin);
2632 else if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT_ACK)
2637 GNUNET_ERROR_TYPE_DEBUG,
2638 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT_ACK size: %u; %s\n",
2639 ntohs(hdr->size), wlan_plugin_address_to_string(NULL,
2640 session_light->addr, 6));
2643 GNUNET_assert(session_light != NULL);
2644 if (session_light->session == NULL)
2646 session_light->session = search_session(plugin, session_light->addr);
2647 GNUNET_assert(session_light->session != NULL);
2649 session = session_light->session;
2650 fah = (struct FragmentationAckHeader *) hdr;
2651 fm = get_fragment_message_from_session_and_id(session, ntohl(
2656 fm->ack_bitfield = fm->ack_bitfield | ntohll(fah->fragment_field);
2657 check_finished_fragment(plugin, fm);
2661 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2662 "WLAN fragment not in fragment list with id %u of ack\n", ntohl(
2671 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2672 "WLAN packet inside the WLAN helper packet has not the right type\n");
2677 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2678 "Helper finished\n");
2684 macprinter(const u_int8_t * mac)
2686 static char macstr[20];
2688 GNUNET_snprintf(macstr, sizeof(macstr), "%X:%X:%X:%X:%X:%X", mac[0], mac[1],
2689 mac[2], mac[3], mac[4], mac[5]);
2694 * Function used for to process the data from the suid process
2696 * @param cls the plugin handle
2697 * @param client client that send the data (not used)
2698 * @param hdr header of the GNUNET_MessageHeader
2701 wlan_process_helper(void *cls, void *client,
2702 const struct GNUNET_MessageHeader *hdr)
2704 struct Plugin *plugin = cls;
2705 struct ieee80211_frame * wlanIeeeHeader = NULL;
2706 struct Session_light * session_light = NULL;
2707 struct Radiotap_rx * rxinfo;
2708 const struct GNUNET_MessageHeader * temp_hdr = NULL;
2713 switch (ntohs(hdr->type))
2715 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA:
2718 GNUNET_ERROR_TYPE_DEBUG,
2719 "Func wlan_process_helper got GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA size: %u\n",
2723 //call wlan_process_helper with the message inside, later with wlan: analyze signal
2724 if (ntohs(hdr->size) < sizeof(struct ieee80211_frame)
2725 + sizeof(struct GNUNET_MessageHeader) + sizeof(struct Radiotap_rx))
2728 /* FIXME: restart SUID process */
2731 rxinfo = (struct Radiotap_rx *) &hdr[1];
2732 wlanIeeeHeader = (struct ieee80211_frame *) &rxinfo[1];
2734 //process only if it is an broadcast or for this computer both with the gnunet bssid
2737 if (memcmp(&(wlanIeeeHeader->i_addr3), mac_bssid, sizeof(struct MacAddress))
2740 //check for broadcast or mac
2741 if (memcmp(&(wlanIeeeHeader->i_addr1), bc_all_mac,
2742 sizeof(struct MacAddress) == 0) || memcmp(
2743 &(wlanIeeeHeader->i_addr1), &(plugin->mac_address),
2744 sizeof(struct MacAddress)) == 0)
2747 // process the inner data
2750 datasize = ntohs(hdr->size) - sizeof(struct ieee80211_frame)
2751 - sizeof(struct GNUNET_MessageHeader);
2753 session_light = GNUNET_malloc(sizeof(struct Session_light));
2754 memcpy(session_light->addr, &(wlanIeeeHeader->i_addr2),
2755 sizeof(struct MacAddress));
2756 //session_light->session = search_session(plugin,session_light->addr);
2759 temp_hdr = (struct GNUNET_MessageHeader *) &wlanIeeeHeader[1];
2760 while (pos < datasize)
2762 temp_hdr = (struct GNUNET_MessageHeader *) &wlanIeeeHeader[1]
2765 wlan_data_helper(plugin, session_light, temp_hdr, rxinfo);
2766 pos += ntohs(temp_hdr->size);
2771 GNUNET_free(session_light);
2776 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2777 "Func wlan_process_helper got wrong MAC: %s\n", macprinter(
2778 wlanIeeeHeader->i_addr1));
2785 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2786 "Func wlan_process_helper got wrong BSSID: %s\n", macprinter(
2787 wlanIeeeHeader->i_addr2));
2791 case GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL:
2792 //TODO more control messages
2793 //TODO use struct wlan_helper_control
2794 if (ntohs(hdr->size) != sizeof(struct Wlan_Helper_Control_Message))
2797 /* FIXME: restart SUID process */
2800 memcpy(&plugin->mac_address, &hdr[1], sizeof(struct MacAddress));
2802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2803 "Received WLAN_HELPER_CONTROL message with transport of address %s\n",
2804 wlan_plugin_address_to_string (cls,
2805 &plugin->mac_address,
2806 sizeof (struct MacAddress)));
2808 plugin->env->notify_address(plugin->env->cls, "wlan", &plugin->mac_address,
2809 sizeof(struct MacAddress), GNUNET_TIME_UNIT_FOREVER_REL);
2818 * We have been notified that wlan-helper has written something to stdout.
2819 * Handle the output, then reschedule this function to be called again once
2820 * more is available.
2822 * @param cls the plugin handle
2823 * @param tc the scheduling context
2826 wlan_plugin_helper_read(void *cls,
2827 const struct GNUNET_SCHEDULER_TaskContext *tc)
2829 struct Plugin *plugin = cls;
2830 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2835 "Start reading from STDIN\n");
2838 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2841 char mybuf[WLAN_MTU + sizeof(struct GNUNET_MessageHeader)];
2844 bytes = GNUNET_DISK_file_read(plugin->server_stdout_handle, mybuf,
2849 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2850 _("Finished reading from wlan-helper stdout with code: %d\n"),
2855 GNUNET_SERVER_mst_receive(plugin->suid_tokenizer, NULL, mybuf, bytes,
2856 GNUNET_NO, GNUNET_NO);
2858 GNUNET_assert(plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2859 plugin->server_read_task = GNUNET_SCHEDULER_add_read_file(
2860 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdout_handle,
2861 &wlan_plugin_helper_read, plugin);
2865 * Start the gnunet-wlan-helper process.
2867 * @param plugin the transport plugin
2868 * @param testmode should we use the dummy driver for testing?
2869 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
2872 wlan_transport_start_wlan_helper(struct Plugin *plugin, int testmode)
2874 const char * filename = "gnunet-transport-wlan-helper";
2875 plugin->server_stdout = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_NO, GNUNET_YES);
2876 if (plugin->server_stdout == NULL)
2877 return GNUNET_SYSERR;
2879 plugin->server_stdin = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_NO);
2880 if (plugin->server_stdin == NULL)
2881 return GNUNET_SYSERR;
2884 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2885 "Starting gnunet-wlan-helper process cmd: %s %s %i\n", filename,
2886 plugin->interface, testmode);
2888 /* Start the server process */
2890 plugin->server_proc = GNUNET_OS_start_process(plugin->server_stdin,
2891 plugin->server_stdout, filename, filename, plugin->interface, ((testmode
2892 == 1) ? "1" : (testmode == 2) ? "2" : "0"), NULL);
2893 if (plugin->server_proc == NULL)
2896 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2897 "Failed to start gnunet-wlan-helper process\n");
2899 return GNUNET_SYSERR;
2902 /* Close the write end of the read pipe */
2903 GNUNET_DISK_pipe_close_end(plugin->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
2905 /* Close the read end of the write pipe */
2906 GNUNET_DISK_pipe_close_end(plugin->server_stdin, GNUNET_DISK_PIPE_END_READ);
2908 plugin->server_stdout_handle = GNUNET_DISK_pipe_handle(plugin->server_stdout,
2909 GNUNET_DISK_PIPE_END_READ);
2910 plugin->server_stdin_handle = GNUNET_DISK_pipe_handle(plugin->server_stdin,
2911 GNUNET_DISK_PIPE_END_WRITE);
2913 GNUNET_assert(plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2916 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2917 "Adding server_read_task for the wlan-helper\n");
2922 plugin->server_read_task = GNUNET_SCHEDULER_add_read_file(
2923 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdout_handle,
2924 &wlan_plugin_helper_read, plugin);
2930 * Exit point from the plugin.
2931 * @param cls pointer to the api struct
2936 libgnunet_plugin_transport_wlan_done(void *cls)
2938 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2939 struct Plugin *plugin = api->cls;
2942 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2943 "libgnunet_plugin_transport_wlan_done started\n");
2946 GNUNET_assert(cls !=NULL);
2948 if (plugin->suid_tokenizer != NULL)
2949 GNUNET_SERVER_mst_destroy(plugin->suid_tokenizer);
2951 if (plugin->data_tokenizer != NULL)
2952 GNUNET_SERVER_mst_destroy(plugin->data_tokenizer);
2954 GNUNET_free_non_null(plugin->interface);
2955 GNUNET_free (plugin);
2961 * Entry point for the plugin.
2963 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2964 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2967 libgnunet_plugin_transport_wlan_init(void *cls)
2969 //struct GNUNET_SERVICE_Context *service;
2970 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2971 struct GNUNET_TRANSPORT_PluginFunctions *api;
2972 struct Plugin *plugin;
2973 static unsigned long long testmode = 0;
2975 GNUNET_assert(cls !=NULL);
2977 plugin = GNUNET_malloc (sizeof (struct Plugin));
2979 plugin->pendingsessions = 0;
2980 plugin->session_count = 0;
2981 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
2982 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2983 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
2985 plugin->suid_tokenizer = GNUNET_SERVER_mst_create(&wlan_process_helper,
2988 plugin->data_tokenizer = GNUNET_SERVER_mst_create(&process_data, plugin);
2990 //plugin->sessions = GNUNET_malloc (sizeof (struct Sessionqueue));
2991 //plugin->pending_Sessions = GNUNET_malloc (sizeof (struct Sessionqueue));
2993 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2995 api->send = &wlan_plugin_send;
2996 api->disconnect = &wlan_plugin_disconnect;
2997 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2998 api->check_address = &wlan_plugin_address_suggested;
2999 api->address_to_string = &wlan_plugin_address_to_string;
3003 if (GNUNET_CONFIGURATION_have_value(env->cfg, "transport-wlan", "TESTMODE"))
3005 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(env->cfg,
3006 "transport-wlan", "TESTMODE", &testmode))
3007 testmode = 0; //default value
3010 if (GNUNET_CONFIGURATION_have_value(env->cfg, "transport-wlan", "INTERFACE"))
3012 if (GNUNET_CONFIGURATION_get_value_string(env->cfg, "transport-wlan",
3013 "INTERFACE", &(plugin->interface)) != GNUNET_YES)
3015 libgnunet_plugin_transport_wlan_done(api);
3021 set_next_beacon_time(plugin);
3023 wlan_transport_start_wlan_helper(plugin, testmode);
3025 start_next_message_id();
3028 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "wlan init finished\n");
3034 /* end of plugin_transport_wlan.c */