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"
38 #define PROTOCOL_PREFIX "wlan"
41 * Max size of packet from helper
46 * Time until retransmission of a fragment in ms
48 #define FRAGMENT_TIMEOUT GNUNET_TIME_UNIT_SECONDS
50 #define FRAGMENT_QUEUE_SIZE 10
51 #define FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION 1
53 #define MESSAGE_IN_TIMEOUT GNUNET_TIME_UNIT_SECONDS
55 #define MESSAGES_IN_QUEUE_SIZE 10
56 #define MESSAGES_IN_QUEUE_PER_SESSION 1
58 #define HALLO_BEACON_SCALING_FACTOR 900
60 #define DEBUG_wlan GNUNET_YES
62 #define MESSAGE_LENGHT_UNKNOWN -1
63 //#define NO_MESSAGE_OR_MESSAGE_FINISHED -2
66 * After how long do we expire an address that we
67 * learned from another peer if it is not reconfirmed
70 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
73 * Initial handshake message for a session.
78 * Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME.
80 struct GNUNET_MessageHeader header;
83 * Identit*mac_y of the node connecting (TCP client)
85 struct GNUNET_PeerIdentity clientIdentity;
90 * Encapsulation of all of the state of the plugin.
97 struct GNUNET_TRANSPORT_PluginEnvironment *env;
100 * List of open sessions. head
102 struct Sessionqueue *sessions;
105 * List of open sessions. tail
107 struct Sessionqueue *sessions_tail;
116 * encapsulation of data from the local wlan helper program
119 struct GNUNET_SERVER_MessageStreamTokenizer * suid_tokenizer;
122 * encapsulation of packets received
125 struct GNUNET_SERVER_MessageStreamTokenizer * data_tokenizer;
128 * stdout pipe handle for the gnunet-wlan-helper process
130 struct GNUNET_DISK_PipeHandle *server_stdout;
133 * stdout file handle for the gnunet-wlan-helper process
135 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
138 * stdin pipe handle for the gnunet-wlan-helper process
140 struct GNUNET_DISK_PipeHandle *server_stdin;
143 * stdin file handle for the gnunet-wlan-helper process
145 const struct GNUNET_DISK_FileHandle *server_stdin_handle;
148 * ID of the gnunet-wlan-server std read task
150 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
153 * ID of the gnunet-wlan-server std read task
155 GNUNET_SCHEDULER_TaskIdentifier server_write_task;
158 * ID of the delay task for writing
160 GNUNET_SCHEDULER_TaskIdentifier server_write_delay_task;
163 * The process id of the wlan process
165 struct GNUNET_OS_Process *server_proc;
168 * The interface of the wlan card given to us by the user.
173 * The mac_address of the wlan card given to us by the helper.
175 struct MacAddress mac_address;
178 * Sessions currently pending for transmission
179 * to this peer, if any.
181 struct Sessionqueue * pending_Sessions;
184 * Sessions currently pending for transmission
185 * to this peer (tail), if any.
187 struct Sessionqueue * pending_Sessions_tail;
190 * number of pending sessions
192 unsigned int pendingsessions;
195 * Messages in the fragmentation queue, head
198 struct FragmentMessage * pending_Fragment_Messages_head;
201 * Messages in the fragmentation queue, tail
204 struct FragmentMessage * pending_Fragment_Messages_tail;
207 * number of pending fragment message
210 unsigned int pending_fragment_messages;
213 * Messages in the in Queue, head
216 struct Receive_Message_Queue * receive_messages_head;
219 * Messages in the in Queue, tail
222 struct Receive_Message_Queue * receive_messages_teil;
225 * number of messages in the in queue
228 unsigned int pending_receive_messages;
231 * time of the next "hello-beacon"
234 struct GNUNET_TIME_Absolute beacon_time;
237 * queue to send acks for received fragments (head)
240 struct AckSendQueue * ack_send_queue_head;
243 * queue to send acks for received fragments (tail)
246 struct AckSendQueue * ack_send_queue_tail;
251 * Struct to store data if file write did not accept the whole packet
255 struct Plugin * plugin;
257 struct GNUNET_MessageHeader * msgstart;
262 * Queue of sessions, for the general session queue and the pending session queue
267 struct Sessionqueue * next;
268 struct Sessionqueue * prev;
269 struct Session * content;
273 * Queue for the fragments received
276 struct Receive_Fragment_Queue
278 struct Receive_Fragment_Queue * next;
279 struct Receive_Fragment_Queue * prev;
286 * Queue for the fragments received
289 struct Receive_Message_Queue
291 struct Receive_Message_Queue * next;
293 struct Receive_Message_Queue * prev;
296 * current number for message incoming, to distinguish between the messages
298 uint32_t message_id_in;
301 * size of the message received,
302 * MESSAGE_LENGHT_UNKNOWN means that the size is not known,
303 * NO_MESSAGE_OR_MESSAGE_FINISHED means no message received
309 * Sorted queue with the fragments received; head
312 struct Receive_Fragment_Queue * frag_head;
315 * Sorted queue with the fragments received; tail
318 struct Receive_Fragment_Queue * frag_tail;
321 * Session this fragment belongs to
324 struct Session * session;
327 * Timeout value for the pending message.
329 struct GNUNET_TIME_Absolute timeout;
332 * Bitfield of received fragments
335 uint64_t received_fragments;
339 * Information kept for each message that is yet to
342 struct PendingMessage
346 * The pending message
351 * Size of the message
356 * Continuation function to call once the message
357 * has been sent. Can be NULL if there is no
358 * continuation to call.
360 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
363 * Cls for transmit_cont
365 void * transmit_cont_cls;
368 * Timeout value for the pending message.
370 struct GNUNET_TIME_Absolute timeout;
375 * Queue for acks to send for fragments recived
381 struct AckSendQueue * next;
382 struct AckSendQueue * prev;
384 struct Session * session;
386 * ID of message, to distinguish between the messages, picked randomly.
391 * Bit field for received fragments
393 uint64_t fragments_field;
398 * Session infos gathered from a messages
404 * the session this message belongs to
406 struct Session * session;
414 * Session handle for connections.
422 struct SessionHeader header;
425 * Pointer to the global plugin struct.
427 struct Plugin *plugin;
430 * Message currently pending for transmission
431 * to this peer, if any.
433 struct PendingMessage *pending_message;
436 * Message currently pending for transmission
437 * to this peer, if any.
439 struct PendingMessage *pending_message2;
442 * To whom are we talking to (set to our identity
443 * if we are still waiting for the welcome message)
445 struct GNUNET_PeerIdentity target;
453 * Address of the other peer (either based on our 'connect'
454 * call or on our 'accept' call).
459 * Last activity on this connection. Used to select preferred
462 struct GNUNET_TIME_Absolute last_activity;
465 * count of messages in the fragment out queue for this session
468 int fragment_messages_out_count;
471 * count of messages in the fragment in queue for this session
474 int fragment_messages_in_count;
479 * Struct for Messages in the fragment queue
482 struct FragmentMessage
485 * Session this message belongs to
488 struct Session *session;
491 * This is a doubly-linked list.
493 struct FragmentMessage *next;
496 * This is a doubly-linked list.
498 struct FragmentMessage *prev;
501 * The pending message
506 * Timeout value for the pending message.
508 struct GNUNET_TIME_Absolute timeout;
511 * Timeout value for the pending fragments.
512 * Stores the time when the next msg fragment ack has to be received
514 struct GNUNET_TIME_Absolute next_ack;
517 * bitfield with all acks received for this message
519 uint64_t ack_bitfield;
522 * Size of the message
527 * pos / next fragment number in the message, for fragmentation/segmentation,
528 * some acks can be missing but there is still time
530 uint32_t message_pos;
533 * current number for message outgoing, to distinguish between the messages
535 uint32_t message_id_out;
539 * Header for messages which need fragmentation
544 struct GNUNET_MessageHeader header;
547 * checksum/error correction
549 uint32_t crc GNUNET_PACKED;
552 * To whom are we talking to (set to our identity
553 * if we are still waiting for the welcome message)
555 struct GNUNET_PeerIdentity target;
557 // followed by payload
562 * Header for messages which need fragmentation
564 struct FragmentationHeader
567 struct GNUNET_MessageHeader header;
570 * ID of message, to distinguish between the messages, picked randomly.
572 uint32_t message_id GNUNET_PACKED;
575 * Offset or number of this fragment, for fragmentation/segmentation (design choice, TBD)
577 uint16_t fragment_off_or_num GNUNET_PACKED;
580 * CRC of fragment (for error checking)
582 uint16_t message_crc GNUNET_PACKED;
584 // followed by payload
589 * Header for messages which need fragmentation
591 struct FragmentationAckHeader
594 struct GNUNET_MessageHeader header;
597 * ID of message, to distinguish between the messages, picked randomly.
599 uint32_t message_id GNUNET_PACKED;
602 * Offset or number of this fragment, for fragmentation/segmentation (design choice, TBD)
604 uint64_t fragment_field GNUNET_PACKED;
609 getRadiotapHeader(struct RadiotapHeader * Header);
612 getWlanHeader(struct IeeeHeader * Header, const char * to_mac_addr,
613 struct Plugin * plugin);
616 wlan_plugin_address_suggested(void *cls, const void *addr, size_t addrlen);
619 getcrc16(const char *msgbuf, size_t msgbuf_size);
622 do_transmit(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
625 check_fragment_queue(struct Plugin * plugin);
628 getcrc32(const char *msgbuf, size_t msgbuf_size);
631 free_receive_message(struct Plugin* plugin,
632 struct Receive_Message_Queue * rec_message);
634 struct Receive_Message_Queue *
635 get_receive_message_from_session(struct Plugin * plugin,
636 struct Session * session);
639 wlan_data_helper(void *cls, struct Session_light * session_light,
640 const struct GNUNET_MessageHeader * hdr);
643 wlan_process_helper(void *cls, void *client,
644 const struct GNUNET_MessageHeader *hdr);
647 finish_sending(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
650 wlan_data_massage_handler(struct Plugin * plugin,
651 struct Session_light * session_light,
652 const struct GNUNET_MessageHeader * hdr);
655 wlan_plugin_address_to_string(void *cls, const void *addr, size_t addrlen);
657 struct Receive_Message_Queue *
658 get_receive_message(struct Plugin * plugin, struct Session * session,
659 uint32_t message_id);
662 htonll(uint64_t input)
668 ntohll(uint64_t input)
674 * Sets a bit active in the bitArray. Increment bit-specific
675 * usage counter on disk only if below 4bit max (==15).
677 * @param bitArray memory area to set the bit in
678 * @param bitIdx which bit to set
681 setBit(char *bitArray, unsigned int bitIdx)
684 unsigned int targetBit;
686 arraySlot = bitIdx / 8;
687 targetBit = (1L << (bitIdx % 8));
688 bitArray[arraySlot] |= targetBit;
692 * Clears a bit from bitArray. Bit is cleared from the array
693 * only if the respective usage counter on the disk hits/is zero.
695 * @param bitArray memory area to set the bit in
696 * @param bitIdx which bit to unset
699 clearBit(char *bitArray, unsigned int bitIdx)
702 unsigned int targetBit;
705 targetBit = (1L << (bitIdx % 8));
706 bitArray[slot] = bitArray[slot] & (~targetBit);
710 * Checks if a bit is active in the bitArray
712 * @param bitArray memory area to set the bit in
713 * @param bitIdx which bit to test
714 * @return GNUNET_YES if the bit is set, GNUNET_NO if not.
717 testBit(char *bitArray, unsigned int bitIdx)
720 unsigned int targetBit;
723 targetBit = (1L << (bitIdx % 8));
724 if (bitArray[slot] & targetBit)
731 * get the next message number, at the moment just a random one
732 * @return returns the next valid message-number for sending packets
735 get_next_message_id()
737 return GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
741 * start next message number generator
742 * (not necessary at the moment)
745 start_next_message_id()
747 //GNUNET_CRYPTO_random_init;
751 * search for a session with the addr
753 * @param plugin pointer to the plugin struct
754 * @param addr pointer to the mac address of the peer
755 * @return returns the session
758 static struct Session *
759 search_session(struct Plugin *plugin, const uint8_t * addr)
761 struct Sessionqueue * queue = plugin->sessions;
762 struct Sessionqueue * lastitem = NULL;
764 //just look at all the session for the needed one
765 while (queue != NULL)
767 // content is never NULL
768 GNUNET_assert (queue->content != NULL);
769 char * addr2 = queue->content->addr;
770 if (memcmp(addr, addr2, 6) == 0)
773 return queue->content;
783 * create a new session
785 * @param plugin pointer to the plugin struct
786 * @param addr pointer to the mac address of the peer
787 * @return returns the session
790 static struct Session *
791 create_session(struct Plugin *plugin, const uint8_t * addr)
793 struct Sessionqueue * queue = GNUNET_malloc (sizeof (struct Sessionqueue));
795 GNUNET_CONTAINER_DLL_insert_tail(plugin->sessions, plugin->sessions_tail, queue);
797 queue->content = GNUNET_malloc (sizeof (struct Session));
798 queue->content->plugin = plugin;
799 memcpy(queue->content->addr, addr, 6);
800 queue->content->fragment_messages_out_count = 0;
801 queue->content->fragment_messages_in_count = 0;
803 plugin->session_count++;
806 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "New session %p with %s\n",
807 queue->content, wlan_plugin_address_to_string(NULL, addr, 6));
810 return queue->content;
814 * get Session from address, create if no session exists
816 * @param plugin pointer to the plugin struct
817 * @param addr pointer to the mac address of the peer
818 * @return returns the session
820 //TODO add other possibilities to find the right session (are there other?)
821 static struct Session *
822 get_Session(struct Plugin *plugin, const uint8_t * addr)
824 struct Session * session = search_session(plugin, addr);
830 return create_session(plugin, addr);
832 /* -- not needed, layer above already has it--
833 //queue welcome message for new sessions, not realy needed
834 //struct WelcomeMessage welcome;
835 struct PendingMessage *pm;
836 pm = GNUNET_malloc (sizeof (struct PendingMessage));
837 pm->msg = GNUNET_malloc(GNUNET_HELLO_size(* (plugin->env->our_hello)));
838 pm->message_size = GNUNET_HELLO_size(* (plugin->env->our_hello));
839 //welcome.header.size = htons (GNUNET_HELLO_size(* (plugin->env->our_hello)));
840 //welcome.header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT);
841 //welcome.clientIdentity = *plugin->env->my_identity;
842 memcpy ( (pm->msg), * plugin->env->our_hello, GNUNET_HELLO_size(* (plugin->env->our_hello)));
843 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
844 queue->content->pending_message = pm;
845 plugin->pendingsessions ++;
846 GNUNET_CONTAINER_DLL_insert_tail(plugin->pending_Sessions, plugin->pending_Sessions_tail, queue);
848 check_fragment_queue(plugin);
853 * Queue the session to send data
857 queue_Session(struct Plugin *plugin, struct Session * session)
859 struct Sessionqueue * queue = plugin->pending_Sessions;
860 struct Sessionqueue * lastitem = NULL;
862 while (queue != NULL)
864 // content is never NULL
865 GNUNET_assert (queue->content != NULL);
866 // is session already in queue?
867 if (session == queue->content)
876 // Session is not in the queue
878 queue = GNUNET_malloc (sizeof (struct Sessionqueue));
879 queue->content = session;
882 GNUNET_CONTAINER_DLL_insert_after (plugin->pending_Sessions,
883 plugin->pending_Sessions_tail,
884 plugin->pending_Sessions_tail, queue);
885 plugin->pendingsessions++;
891 * Function to schedule the write task, executed after a delay
894 delay_fragment_task(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
896 struct Plugin * plugin = cls;
897 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
899 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
902 // GNUNET_TIME_UNIT_FOREVER_REL is needed to clean up old msg
903 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
905 plugin->server_write_task = GNUNET_SCHEDULER_add_write_file(
906 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdin_handle,
907 &do_transmit, plugin);
913 * Function to calculate the time of the next periodic "hello-beacon"
916 set_next_beacon_time(struct Plugin * const plugin)
918 //under 10 known peers: once a second
919 if (plugin->session_count < 10)
921 plugin->beacon_time = GNUNET_TIME_absolute_add(
922 GNUNET_TIME_absolute_get(), GNUNET_TIME_relative_multiply(
923 GNUNET_TIME_UNIT_SECONDS, HALLO_BEACON_SCALING_FACTOR));
925 //under 30 known peers: every 10 seconds
926 else if (plugin->session_count < 30)
928 plugin->beacon_time = GNUNET_TIME_absolute_add(
929 GNUNET_TIME_absolute_get(), GNUNET_TIME_relative_multiply(
930 GNUNET_TIME_UNIT_SECONDS, 10 * HALLO_BEACON_SCALING_FACTOR));
932 //over 30 known peers: once a minute
935 plugin->beacon_time = GNUNET_TIME_absolute_add(
936 GNUNET_TIME_absolute_get(), GNUNET_TIME_relative_multiply(
937 GNUNET_TIME_UNIT_MINUTES, HALLO_BEACON_SCALING_FACTOR));
942 struct GNUNET_TIME_Relative
943 get_next_frag_timeout(struct FragmentMessage * fm)
945 return GNUNET_TIME_relative_min(GNUNET_TIME_absolute_get_remaining(
946 fm->next_ack), GNUNET_TIME_absolute_get_remaining(fm->timeout));
951 * Function to get the timeout value for acks for this session
954 struct GNUNET_TIME_Relative
955 get_ack_timeout(struct FragmentMessage * fm)
957 return FRAGMENT_TIMEOUT;
961 * Function to set the timer for the next timeout of the fragment queue
962 * @param plugin the handle to the plugin struct
965 check_next_fragment_timeout(struct Plugin * const plugin)
967 struct FragmentMessage * fm;
968 struct GNUNET_TIME_Relative next_send;
970 next_send = GNUNET_TIME_absolute_get_remaining(plugin->beacon_time);
973 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
975 GNUNET_SCHEDULER_cancel(plugin->server_write_delay_task);
976 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
978 fm = plugin->pending_Fragment_Messages_head;
980 GNUNET_assert(plugin->server_write_delay_task == GNUNET_SCHEDULER_NO_TASK);
982 //check if some acks are in the queue
983 if (plugin->ack_send_queue_head != NULL)
985 next_send = GNUNET_TIME_relative_get_zero();
987 //check if there are some fragments in the queue
991 = GNUNET_TIME_relative_min(next_send, get_next_frag_timeout(fm));
993 plugin->server_write_delay_task = GNUNET_SCHEDULER_add_delayed(next_send,
994 &delay_fragment_task, plugin);
999 * Function to get the next queued Session, removes the session from the queue
1002 static struct Session *
1003 get_next_queue_Session(struct Plugin * plugin)
1005 struct Session * session;
1006 struct Sessionqueue * sessionqueue;
1007 struct Sessionqueue * sessionqueue_alt;
1008 struct PendingMessage * pm;
1009 sessionqueue = plugin->pending_Sessions;
1010 while (sessionqueue != NULL)
1012 session = sessionqueue->content;
1014 pm = session->pending_message;
1017 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1018 "pending message is empty, should not happen. session %p\n", session);
1021 GNUNET_assert(pm != NULL);
1023 //check for message timeout
1024 if (GNUNET_TIME_absolute_get_remaining(pm->timeout).rel_value > 0)
1026 //check if session has no message in the fragment queue
1027 if (session->fragment_messages_out_count
1028 < FRAGMENT_QUEUE_MESSAGES_OUT_PER_SESSION)
1030 plugin->pendingsessions--;
1031 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions,
1032 plugin->pending_Sessions_tail, sessionqueue);
1033 GNUNET_free(sessionqueue);
1039 sessionqueue = sessionqueue->next;
1045 session->pending_message = session->pending_message2;
1046 session->pending_message2 = NULL;
1048 //call the cont func that it did not work
1049 if (pm->transmit_cont != NULL)
1050 pm->transmit_cont(pm->transmit_cont_cls, &(session->target),
1052 GNUNET_free(pm->msg);
1055 if (session->pending_message == NULL)
1057 sessionqueue_alt = sessionqueue;
1058 sessionqueue = sessionqueue->next;
1059 plugin->pendingsessions--;
1060 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions,
1061 plugin->pending_Sessions_tail, sessionqueue_alt);
1063 GNUNET_free(sessionqueue_alt);
1073 * Function to sort the message into the message fragment queue
1074 * @param plugin the plugin struct
1075 * @param fm message to sort into the queue
1078 sort_fragment_into_queue(struct Plugin * plugin, struct FragmentMessage * fm)
1080 struct FragmentMessage * fm2;
1081 //sort into the list at the right position
1083 fm2 = plugin->pending_Fragment_Messages_head;
1087 if (GNUNET_TIME_absolute_get_difference(fm2->next_ack, fm->next_ack).rel_value
1098 GNUNET_CONTAINER_DLL_insert_after(plugin->pending_Fragment_Messages_head,
1099 plugin->pending_Fragment_Messages_tail,fm2,fm);
1103 * frees the space of a message in the fragment queue (send queue)
1104 * @param plugin the plugin struct
1105 * @param fm message to free
1108 free_fragment_message(struct Plugin * plugin, struct FragmentMessage * fm)
1112 (fm->session->fragment_messages_out_count)--;
1113 GNUNET_free_non_null(fm->msg);
1114 GNUNET_CONTAINER_DLL_remove (plugin->pending_Fragment_Messages_head,
1115 plugin->pending_Fragment_Messages_tail, fm);
1117 plugin->pending_fragment_messages--;
1120 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1121 "free pending fragment messages, pending messages remaining %u\n",
1122 plugin->pending_fragment_messages);
1128 * Function to check if there is some space in the fragment queue
1129 * inserts a message if space is available
1130 * @param plugin the plugin struct
1134 check_fragment_queue(struct Plugin * plugin)
1136 struct Session * session;
1137 struct FragmentMessage * fm;
1138 struct GNUNET_PeerIdentity pid;
1140 struct PendingMessage * pm;
1142 if (plugin->pending_fragment_messages < FRAGMENT_QUEUE_SIZE)
1144 session = get_next_queue_Session(plugin);
1145 if (session != NULL)
1147 pm = session->pending_message;
1148 session->pending_message = NULL;
1149 session->fragment_messages_out_count++;
1150 GNUNET_assert(pm != NULL);
1152 fm = GNUNET_malloc(sizeof(struct FragmentMessage));
1153 fm->message_size = pm->message_size;
1155 fm->session = session;
1156 fm->timeout.abs_value = pm->timeout.abs_value;
1157 fm->message_pos = 0;
1158 fm->next_ack = GNUNET_TIME_absolute_get();
1159 fm->message_id_out = get_next_message_id();
1160 fm->ack_bitfield = 0;
1162 sort_fragment_into_queue(plugin, fm);
1163 plugin->pending_fragment_messages++;
1165 if (pm->transmit_cont != NULL)
1167 pid = session->target;
1168 pm->transmit_cont(pm->transmit_cont_cls, &pid, GNUNET_OK);
1170 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1171 "called pm->transmit_cont for %p\n", session);
1177 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1178 "no pm->transmit_cont for %p\n", session);
1183 if (session->pending_message2 != NULL){
1184 session->pending_message = session->pending_message2;
1185 session->pending_message2 = NULL;
1187 queue_Session(plugin, session);
1190 //check if timeout changed
1191 check_next_fragment_timeout(plugin);
1197 * Funktion to check if all fragments where send and the acks received
1198 * frees the space if finished
1199 * @param plugin the plugin struct
1200 * @param fm the message to check
1203 check_finished_fragment(struct Plugin * plugin, struct FragmentMessage * fm)
1205 //maxack = size of message / max packet size, eg 12 / 5 = 2 start at 0 so ack numbers are 0,1,2
1206 unsigned int maxack = 63 - ((fm->message_size - 1) / (WLAN_MTU
1207 - sizeof(struct FragmentationHeader)));
1208 uint64_t tmpfield = 0xFFFFFFFFFFFFFFFF;
1209 tmpfield = tmpfield >> maxack;
1213 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1214 "Test bitfields %X and %X, maxack is %u, fm size %u\n",
1215 fm->ack_bitfield, tmpfield, maxack, fm->message_size);
1218 if (fm->ack_bitfield == tmpfield)
1221 free_fragment_message(plugin, fm);
1224 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1225 "Finished sending and got all acks for a fragmented message\n");
1228 check_next_fragment_timeout(plugin);
1229 check_fragment_queue(plugin);
1235 * Function to set the next fragment number
1236 * @param fm use this FragmentMessage
1240 set_next_message_fragment_pos(struct FragmentMessage * fm)
1243 //check if retransmit is needed
1244 if (GNUNET_TIME_absolute_get_remaining(fm->next_ack).rel_value == 0)
1247 // be positive and try again later :-D
1248 fm->next_ack = GNUNET_TIME_relative_to_absolute(get_ack_timeout(fm));
1249 // find first missing fragment
1251 fm->message_pos = 0;
1254 //test if ack 0 (or X) was already received
1255 while (testBit((char*) &fm->ack_bitfield, fm->message_pos) == GNUNET_YES)
1263 send_hello_beacon(struct Plugin * plugin)
1267 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending hello beacon\n");
1272 struct GNUNET_MessageHeader * msgheader = NULL;
1273 struct IeeeHeader * ieeewlanheader = NULL;
1274 struct RadiotapHeader * radioHeader = NULL;
1275 struct GNUNET_MessageHeader * msgheader2 = NULL;
1277 GNUNET_assert(sizeof(struct WlanHeader) + GNUNET_HELLO_size(
1278 *(plugin->env->our_hello)) <= WLAN_MTU);
1279 size = sizeof(struct GNUNET_MessageHeader) + sizeof(struct RadiotapHeader)
1280 + sizeof(struct IeeeHeader) + sizeof(struct GNUNET_MessageHeader)
1281 + GNUNET_HELLO_size(*(plugin->env->our_hello));
1283 msgheader = GNUNET_malloc(size);
1284 msgheader->size = htons(size);
1285 msgheader->type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1287 radioHeader = (struct RadiotapHeader*) &msgheader[1];
1288 getRadiotapHeader(radioHeader);
1289 ieeewlanheader = (struct IeeeHeader*) &radioHeader[1];
1290 getWlanHeader(ieeewlanheader, bc_all_mac, plugin);
1292 msgheader2 = (struct GNUNET_MessageHeader*) &ieeewlanheader[1];
1293 msgheader2->size = htons(GNUNET_HELLO_size(*(plugin->env->our_hello))
1294 + sizeof(struct GNUNET_MessageHeader));
1296 msgheader2->type = htons(GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT);
1297 memcpy(&msgheader2[1], *plugin->env->our_hello, GNUNET_HELLO_size(
1298 *(plugin->env->our_hello)));
1300 bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle, msgheader, size);
1302 if (bytes == GNUNET_SYSERR)
1304 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1305 _("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1306 errno, strerror(errno));
1309 GNUNET_assert(bytes != GNUNET_SYSERR);
1310 GNUNET_assert(bytes == size);
1311 GNUNET_free(msgheader);
1313 set_next_beacon_time(plugin);
1314 check_next_fragment_timeout(plugin);
1318 send_ack(struct Plugin * plugin, struct AckSendQueue * ack)
1323 struct GNUNET_MessageHeader * msgheader = NULL;
1324 struct IeeeHeader * ieeewlanheader = NULL;
1325 struct RadiotapHeader * radioHeader = NULL;
1326 struct FragmentationAckHeader * msgheader2 = NULL;
1328 GNUNET_assert(sizeof(struct FragmentationAckHeader) <= WLAN_MTU);
1331 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1332 "Sending ack for message_id %u with fragment field %u\n",
1333 ack->message_id, ack->fragments_field);
1336 size = sizeof(struct GNUNET_MessageHeader) + sizeof(struct RadiotapHeader)
1337 + sizeof(struct IeeeHeader) + sizeof(struct FragmentationAckHeader);
1338 msgheader = GNUNET_malloc(size);
1339 msgheader->size = htons(size);
1340 msgheader->type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1342 radioHeader = (struct RadiotapHeader*) &msgheader[1];
1343 getRadiotapHeader(radioHeader);
1344 ieeewlanheader = (struct IeeeHeader*) &radioHeader[1];
1345 getWlanHeader(ieeewlanheader, ack->session->addr, plugin);
1347 msgheader2 = (struct FragmentationAckHeader*) &ieeewlanheader[1];
1348 msgheader2->header.size = htons(sizeof(struct FragmentationAckHeader));
1349 msgheader2->header.type = htons(GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT_ACK);
1350 msgheader2->message_id = htonl(ack->message_id);
1351 msgheader2->fragment_field = htonll(ack->fragments_field);
1353 bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle, msgheader, size);
1354 if (bytes == GNUNET_SYSERR)
1356 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1357 _("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1358 errno, strerror(errno));
1361 GNUNET_assert(bytes != GNUNET_SYSERR);
1362 GNUNET_assert(bytes == size);
1363 GNUNET_free(msgheader);
1364 check_next_fragment_timeout(plugin);
1368 * Function called when wlan helper is ready to get some data
1370 * @param cls closure
1371 * @param tc GNUNET_SCHEDULER_TaskContext
1375 do_transmit(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1378 struct Plugin * plugin = cls;
1379 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1381 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1384 struct Session * session = NULL;
1385 struct FragmentMessage * fm = NULL;
1386 struct IeeeHeader * ieeewlanheader = NULL;
1387 struct RadiotapHeader * radioHeader = NULL;
1388 struct GNUNET_MessageHeader * msgheader = NULL;
1390 struct FragmentationHeader fragheader;
1391 struct FragmentationHeader * fragheaderptr = NULL;
1392 struct Finish_send * finish = NULL;
1393 struct AckSendQueue * ack;
1396 const char * copystart = NULL;
1397 uint16_t copysize = 0;
1398 uint copyoffset = 0;
1400 if (plugin->ack_send_queue_head != NULL)
1402 ack = plugin->ack_send_queue_head;
1403 GNUNET_CONTAINER_DLL_remove(plugin->ack_send_queue_head,
1404 plugin->ack_send_queue_tail, ack);
1405 send_ack(plugin, ack);
1410 //test if a "hello-beacon" has to be send
1411 if (GNUNET_TIME_absolute_get_remaining(plugin->beacon_time).rel_value == 0)
1413 send_hello_beacon(plugin);
1419 fm = plugin->pending_Fragment_Messages_head;
1422 session = fm->session;
1423 GNUNET_assert(session != NULL);
1425 // test if message timed out
1426 if (GNUNET_TIME_absolute_get_remaining(fm->timeout).rel_value == 0)
1429 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "message timeout\n");
1432 check_fragment_queue(plugin);
1433 free_fragment_message(plugin, fm);
1439 //if (fm->message_size > WLAN_MTU)
1441 size += sizeof(struct FragmentationHeader);
1443 set_next_message_fragment_pos(fm);
1445 copyoffset = (WLAN_MTU - sizeof(struct FragmentationHeader))
1450 GNUNET_ERROR_TYPE_DEBUG,
1451 "Sending GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT with message_id %u with fragment number %i, size: %u, offset %u, time until timeout %u\n",
1452 fm->message_id_out, fm->message_pos, copysize
1453 + sizeof(struct FragmentationHeader), copyoffset,
1454 GNUNET_TIME_absolute_get_remaining(fm->timeout));
1457 fragheader.fragment_off_or_num = htons(fm->message_pos);
1458 fragheader.message_id = htonl(fm->message_id_out);
1459 copystart = fm->msg + copyoffset;
1460 copysize = GNUNET_MIN(fm->message_size - copyoffset,
1461 WLAN_MTU - sizeof(struct FragmentationHeader));
1463 if (copyoffset >= fm->message_size)
1466 GNUNET_ERROR_TYPE_ERROR,
1467 "offset in message for fragment too large, offset %u, size %u, max size %u, copysize %u, message_pos %u,\n",
1468 copyoffset, fm->message_size, WLAN_MTU
1469 - sizeof(struct FragmentationHeader), copysize,
1472 GNUNET_assert(copyoffset < fm->message_size);
1473 //FIXME remove later
1474 GNUNET_assert(copystart < fm->msg + fm->message_size);
1476 fragheader.header.size = htons(copysize
1477 + sizeof(struct FragmentationHeader));
1478 fragheader.header.type = htons(GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT);
1483 // there is no need to split
1484 copystart = fm->msg;
1485 copysize = fm->message_size;
1489 size += sizeof(struct RadiotapHeader) + sizeof(struct IeeeHeader)
1490 + sizeof(struct GNUNET_MessageHeader);
1491 msgheader = GNUNET_malloc(size);
1492 msgheader->size = htons(size);
1493 msgheader->type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1495 radioHeader = (struct RadiotapHeader*) &msgheader[1];
1496 getRadiotapHeader(radioHeader);
1498 ieeewlanheader = (struct IeeeHeader *) &radioHeader[1];
1499 getWlanHeader(ieeewlanheader, fm->session->addr, plugin);
1501 //could be faster if content is just send and not copyed before
1502 //fragmentheader is needed
1503 //if (fm->message_size > WLAN_MTU)
1505 fragheader.message_crc = htons(getcrc16(copystart, copysize));
1506 memcpy(&ieeewlanheader[1], &fragheader,
1507 sizeof(struct FragmentationHeader));
1508 fragheaderptr = (struct FragmentationHeader *) &ieeewlanheader[1];
1509 memcpy(&fragheaderptr[1], copystart, copysize);
1513 memcpy(&ieeewlanheader[1], copystart, copysize);
1516 bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle,
1518 if (bytes == GNUNET_SYSERR)
1521 GNUNET_ERROR_TYPE_ERROR,
1522 _("Error writing to wlan healper. errno == %d, ERROR: %s\n"),
1523 errno, strerror(errno));
1526 GNUNET_assert(bytes != GNUNET_SYSERR);
1530 finish = GNUNET_malloc(sizeof( struct Finish_send));
1531 finish->plugin = plugin;
1532 finish->msgheader = (char *) msgheader + bytes;
1533 finish->size = size - bytes;
1534 finish->msgstart = msgheader;
1536 GNUNET_assert(plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK);
1538 plugin->server_write_task = GNUNET_SCHEDULER_add_write_file(
1539 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdin_handle,
1540 &finish_sending, finish);
1545 GNUNET_assert(bytes == size);
1547 GNUNET_free(msgheader);
1548 check_next_fragment_timeout(plugin);
1551 //check if this was the last fragment of this message, if true then queue at the end of the list
1552 if (copysize + copyoffset >= fm->message_size)
1554 GNUNET_assert(copysize + copyoffset == fm->message_size);
1556 GNUNET_CONTAINER_DLL_remove (plugin->pending_Fragment_Messages_head,
1557 plugin->pending_Fragment_Messages_tail, fm);
1559 GNUNET_CONTAINER_DLL_insert_tail(plugin->pending_Fragment_Messages_head,
1560 plugin->pending_Fragment_Messages_tail, fm);
1561 // if fragments have opimized timeouts
1562 //sort_fragment_into_queue(plugin,fm);
1569 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1570 "do_transmit did nothing, should not happen!\n");
1574 finish_sending(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1576 struct Finish_send * finish;
1577 struct Plugin * plugin;
1581 plugin = finish->plugin;
1583 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1585 bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle,
1586 finish->msgheader, finish->size);
1587 GNUNET_assert(bytes != GNUNET_SYSERR);
1589 GNUNET_assert(plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK);
1590 if (bytes != finish->size)
1593 finish->plugin = plugin;
1594 finish->msgheader = finish->msgheader + bytes;
1595 finish->size = finish->size - bytes;
1596 plugin->server_write_task = GNUNET_SCHEDULER_add_write_file(
1597 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdin_handle,
1598 &finish_sending, finish);
1602 GNUNET_free(finish->msgstart);
1603 GNUNET_free(finish);
1604 check_next_fragment_timeout(plugin);
1610 getRadiotapHeader(struct RadiotapHeader * Header)
1617 * function to generate the wlan hardware header for one packet
1618 * @param Header address to write the header to
1619 * @param to_mac_addr address of the recipient
1620 * @param plugin pointer to the plugin struct
1621 * @return GNUNET_YES if there was no error
1625 getWlanHeader(struct IeeeHeader * Header, const char * const to_mac_addr,
1626 struct Plugin * plugin)
1628 memcpy(&Header->mac3, mac_bssid, sizeof(mac_bssid));
1629 memcpy(&Header->mac2, plugin->mac_address.mac, sizeof(plugin->mac_address));
1630 memcpy(&Header->mac1, to_mac_addr, sizeof(plugin->mac_address));
1637 * @param msgbuf pointer tor the data
1638 * @param msgbuf_size size of the data
1640 * @return 32bit crc value
1644 getcrc32(const char *msgbuf, size_t msgbuf_size)
1646 //TODO calc some crc
1653 * @param msgbuf pointer tor the data
1654 * @param msgbuf_size size of the data
1656 * @return 16bit crc value
1660 getcrc16(const char *msgbuf, size_t msgbuf_size)
1662 //TODO calc some crc
1667 * Function that can be used by the transport service to transmit
1668 * a message using the plugin.
1670 * @param cls closure
1671 * @param target who should receive this message
1672 * @param priority how important is the message
1673 * @param msgbuf the message to transmit
1674 * @param msgbuf_size number of bytes in 'msgbuf'
1675 * @param timeout when should we time out
1676 * @param session which session must be used (or NULL for "any")
1677 * @param addr the address to use (can be NULL if the plugin
1678 * is "on its own" (i.e. re-use existing TCP connection))
1679 * @param addrlen length of the address in bytes
1680 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1681 * otherwise the plugin may use other addresses or
1682 * existing connections (if available)
1683 * @param cont continuation to call once the message has
1684 * been transmitted (or if the transport is ready
1685 * for the next transmission call; or if the
1686 * peer disconnected...)
1687 * @param cont_cls closure for cont
1688 * @return number of bytes used (on the physical network, with overheads);
1689 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1690 * and does NOT mean that the message was not transmitted (DV)
1693 wlan_plugin_send(void *cls, const struct GNUNET_PeerIdentity * target,
1694 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
1695 struct GNUNET_TIME_Relative timeout, struct Session *session,
1696 const void *addr, size_t addrlen, int force_address,
1697 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1699 struct Plugin * plugin = cls;
1700 struct PendingMessage * newmsg = NULL;
1701 struct WlanHeader * wlanheader = NULL;
1703 //check if msglen > 0
1704 GNUNET_assert(msgbuf_size > 0);
1706 //get session if needed
1707 if (session == NULL)
1709 if (wlan_plugin_address_suggested(plugin, addr, addrlen) == GNUNET_OK)
1711 session = get_Session(plugin, addr);
1715 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1716 _("Wlan Address len %d is wrong\n"), addrlen);
1721 //TODO target "problem" not solved
1722 //if (session->target != NULL){
1723 // GNUNET_assert(session->target == *target);
1725 session->target = *target;
1731 //queue message in session
1732 //test if there is no other message in the "queue"
1733 //FIXME: to many send requests
1734 //GNUNET_assert (session->pending_message == NULL);
1735 if (session->pending_message != NULL)
1737 newmsg = session->pending_message;
1739 GNUNET_ERROR_TYPE_ERROR,
1740 "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",
1741 GNUNET_TIME_absolute_get_remaining(newmsg->timeout).rel_value,
1742 session->fragment_messages_out_count);
1743 if (session->pending_message2 != NULL)
1746 GNUNET_ERROR_TYPE_ERROR,
1747 "wlan_plugin_send: two pending messages are already in the queue for this client\n");
1752 newmsg = GNUNET_malloc(sizeof(struct PendingMessage));
1753 (newmsg->msg) = GNUNET_malloc(msgbuf_size + sizeof(struct WlanHeader));
1754 wlanheader = (struct WlanHeader *) newmsg->msg;
1755 //copy msg to buffer, not fragmented / segmented yet, but with message header
1756 wlanheader->header.size = htons(msgbuf_size + sizeof(struct WlanHeader));
1757 wlanheader->header.type = htons(GNUNET_MESSAGE_TYPE_WLAN_DATA);
1758 memcpy(&(wlanheader->target), target, sizeof(struct GNUNET_PeerIdentity));
1759 wlanheader->crc = htonl(getcrc32(msgbuf, msgbuf_size));
1760 memcpy(&wlanheader[1], msgbuf, msgbuf_size);
1761 newmsg->transmit_cont = cont;
1762 newmsg->transmit_cont_cls = cont_cls;
1763 newmsg->timeout = GNUNET_TIME_relative_to_absolute(timeout);
1765 newmsg->timeout.abs_value = newmsg->timeout.abs_value - 500;
1767 newmsg->message_size = msgbuf_size + sizeof(struct WlanHeader);
1769 if (session->pending_message == NULL)
1771 session->pending_message = newmsg;
1775 session->pending_message2 = newmsg;
1779 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1780 "New message for %p with size (incl wlan header) %u added\n", session,
1781 newmsg->message_size);
1786 queue_Session(plugin, session);
1788 check_fragment_queue(plugin);
1789 //FIXME not the correct size
1795 * function to get the first message in the fragement queue (out) of a session
1796 * @param session pointer to the session
1797 * @return pointer to the struct FragmentMessage
1799 static struct FragmentMessage *
1800 get_fragment_message_from_session(struct Session * session)
1802 struct FragmentMessage * fm = session->plugin->pending_Fragment_Messages_head;
1805 if (fm->session == session)
1815 * function to get the message in the fragement queue (out) of a session with a specific id
1816 * @param session pointer to the session
1817 * @param message_id id of the message
1818 * @return pointer to the struct FragmentMessage
1820 static struct FragmentMessage *
1821 get_fragment_message_from_session_and_id(struct Session * session,
1822 uint32_t message_id)
1824 struct FragmentMessage * fm = session->plugin->pending_Fragment_Messages_head;
1827 if ((fm->session == session) && (fm->message_id_out == message_id))
1837 * Function that can be used to force the plugin to disconnect
1838 * from the given peer and cancel all previous transmissions
1839 * (and their continuation).
1841 * @param cls closure
1842 * @param target peer from which to disconnect
1845 wlan_plugin_disconnect(void *cls, const struct GNUNET_PeerIdentity *target)
1847 struct Plugin *plugin = cls;
1848 struct Sessionqueue * queue = plugin->sessions;
1849 struct Sessionqueue * pendingsession = plugin->pending_Sessions;
1850 struct PendingMessage * pm = NULL;
1851 struct FragmentMessage * fm;
1852 struct Receive_Message_Queue * receive_queue;
1854 // just look at all the session for the needed one
1855 while (queue != NULL)
1857 // content is never NULL
1858 GNUNET_assert (queue->content != NULL);
1859 if (memcmp(target, &(queue->content->target),
1860 sizeof(struct GNUNET_PeerIdentity)) == 0)
1863 //is this session pending for send
1864 while (pendingsession != NULL)
1866 if (pendingsession->content == queue->content)
1868 plugin->pendingsessions--;
1869 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions,
1870 plugin->pending_Sessions_tail, pendingsession);
1871 GNUNET_free(pendingsession);
1874 pendingsession = pendingsession->next;
1877 //is something of this session in the fragment queue?
1878 fm = get_fragment_message_from_session(queue->content);
1881 free_fragment_message(plugin, fm);
1882 fm = get_fragment_message_from_session(queue->content);
1884 check_next_fragment_timeout(plugin);
1886 //dispose all received fragments
1887 receive_queue = get_receive_message_from_session(plugin,
1889 while (receive_queue != NULL)
1891 free_receive_message(plugin, receive_queue);
1892 receive_queue = get_receive_message_from_session(plugin,
1896 // remove PendingMessage
1897 pm = queue->content->pending_message;
1900 GNUNET_free_non_null(pm->msg);
1904 GNUNET_free(queue->content);
1905 GNUNET_CONTAINER_DLL_remove(plugin->sessions, plugin->sessions_tail, queue);
1907 plugin->session_count--;
1912 queue = queue->next;
1917 * Convert the transports address to a nice, human-readable
1920 * @param cls closure
1921 * @param type name of the transport that generated the address
1922 * @param addr one of the addresses of the host, NULL for the last address
1923 * the specific address format depends on the transport
1924 * @param addrlen length of the address
1925 * @param numeric should (IP) addresses be displayed in numeric form?
1926 * @param timeout after how long should we give up?
1927 * @param asc function to call on each string
1928 * @param asc_cls closure for asc
1931 wlan_plugin_address_pretty_printer(void *cls, const char *type,
1932 const void *addr, size_t addrlen, int numeric,
1933 struct GNUNET_TIME_Relative timeout,
1934 GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
1937 const unsigned char * input;
1939 //GNUNET_assert(cls !=NULL);
1942 /* invalid address (MAC addresses have 6 bytes) */
1947 input = (const unsigned char*) addr;
1948 GNUNET_snprintf(ret, sizeof(ret),
1949 "%s Mac-Address %.2X:%.2X:%.2X:%.2X:%.2X:%.2X", PROTOCOL_PREFIX,
1950 input[0], input[1], input[2], input[3], input[4], input[5]);
1955 * Another peer has suggested an address for this
1956 * peer and transport plugin. Check that this could be a valid
1957 * address. If so, consider adding it to the list
1960 * @param cls closure
1961 * @param addr pointer to the address
1962 * @param addrlen length of addr
1963 * @return GNUNET_OK if this is a plausible address for this peer
1968 wlan_plugin_address_suggested(void *cls, const void *addr, size_t addrlen)
1970 //struct Plugin *plugin = cls;
1972 /* check if the address is plausible; if so,
1973 add it to our list! */
1975 GNUNET_assert(cls !=NULL);
1976 //FIXME mitm is not checked
1977 //Mac Address has 6 bytes
1980 /* TODO check for bad addresses like multicast, broadcast, etc */
1985 return GNUNET_SYSERR;
1988 return GNUNET_SYSERR;
1992 * Function called for a quick conversion of the binary address to
1993 * a numeric address. Note that the caller must not free the
1994 * address and that the next call to this function is allowed
1995 * to override the address again.
1997 * @param cls closure
1998 * @param addr binary address
1999 * @param addrlen length of the address
2000 * @return string representing the same address
2003 wlan_plugin_address_to_string(void *cls, const void *addr, size_t addrlen)
2005 static char ret[40];
2006 const unsigned char * input;
2008 //GNUNET_assert(cls !=NULL);
2011 /* invalid address (MAC addresses have 6 bytes) */
2015 input = (const unsigned char*) addr;
2016 GNUNET_snprintf(ret, sizeof(ret),
2017 "%s Mac-Address %02X:%02X:%02X:%02X:%02X:%02X", PROTOCOL_PREFIX,
2018 input[0], input[1], input[2], input[3], input[4], input[5]);
2023 * function to check if bitfield is representation of fragments of the message
2024 * @param rec_message message to check
2028 check_message_fragment_bitfield(struct Receive_Message_Queue * rec_message)
2030 uint64_t checkfragments = 0;
2031 struct Receive_Fragment_Queue * rec_queue = rec_message->frag_head;
2033 while (rec_queue != NULL)
2035 setBit((char*) &checkfragments, rec_queue->num);
2036 rec_queue = rec_queue->next;
2039 GNUNET_assert(checkfragments == rec_message->received_fragments);
2043 * Function to test if fragment number already exists in the fragments received
2045 * @param rec_message message this fragment belongs to
2046 * @param fh Fragmentheader of the fragment
2047 * @return GNUNET_YES if fragment exists already, GNUNET_NO if it does not exists in the queue of the session
2051 is_double_msg(struct Receive_Message_Queue * rec_message,
2052 struct FragmentationHeader * fh)
2054 //test if bitfield is okay
2056 check_message_fragment_bitfield(rec_message);
2059 return testBit((char *) &rec_message->received_fragments, ntohs(
2060 fh->fragment_off_or_num));
2065 * Function to insert a fragment in a queue of a message
2066 * @param session session the fragment belongs to
2067 * @param rec_queue fragment to add
2071 insert_fragment_in_queue(struct Receive_Message_Queue * rec_message,
2072 struct Receive_Fragment_Queue * rec_queue)
2074 struct Receive_Fragment_Queue * rec_queue2 = rec_message->frag_head;
2075 struct WlanHeader * wlanheader = NULL;
2077 GNUNET_assert(rec_message != NULL);
2078 GNUNET_assert(rec_queue != NULL);
2080 //this is the first fragment of the message (fragment id 0)
2081 if (rec_queue->num == 0)
2083 wlanheader = (struct WlanHeader *) rec_queue->msg;
2084 rec_message->rec_size = ntohs(wlanheader->header.size);
2088 while (rec_queue2 != NULL)
2090 if (rec_queue2->num > rec_queue->num)
2092 //next element number is grater than the current num
2093 GNUNET_CONTAINER_DLL_insert_before(rec_message->frag_head, rec_message->frag_tail, rec_queue2, rec_queue);
2094 setBit((char *) &rec_message->received_fragments, rec_queue->num);
2097 rec_queue = rec_queue->next;
2099 //no element has a grater number
2100 GNUNET_CONTAINER_DLL_insert_tail(rec_message->frag_head, rec_message->frag_tail, rec_queue);
2102 setBit((char *) &rec_message->received_fragments, rec_queue->num);
2106 * Function to dispose the fragments received for a message and the message
2107 * @param plugin pointer to the plugin struct
2108 * @param rec_message pointer to the struct holding the message which should be freed
2112 free_receive_message(struct Plugin* plugin,
2113 struct Receive_Message_Queue * rec_message)
2115 GNUNET_assert(rec_message !=NULL);
2116 struct Receive_Fragment_Queue * rec_queue = rec_message->frag_head;
2117 struct Receive_Fragment_Queue * rec_queue2;
2119 while (rec_queue != NULL)
2121 rec_queue2 = rec_queue;
2122 rec_queue = rec_queue->next;
2123 GNUNET_free(rec_queue2);
2126 GNUNET_CONTAINER_DLL_remove(plugin->receive_messages_head,plugin->receive_messages_teil, rec_message);
2128 GNUNET_assert(plugin->pending_receive_messages > 0);
2129 GNUNET_assert(rec_message->session->fragment_messages_in_count > 0);
2131 plugin->pending_receive_messages--;
2132 rec_message->session->fragment_messages_in_count--;
2133 GNUNET_free(rec_message);
2137 * Function to check if all fragments of a message have been received
2138 * @param plugin the plugin handle
2139 * @param session_light information of the message sender
2140 * @param session session the message belongs to
2141 * @param rec_message pointer to the message that should be checked
2145 check_rec_finished_msg(struct Plugin* plugin,
2146 struct Session_light * session_light, struct Session * session,
2147 struct Receive_Message_Queue * rec_message)
2149 struct Receive_Fragment_Queue * rec_queue = rec_message->frag_head;
2150 int packetsize = rec_message->rec_size;
2153 uint64_t bitfield = 0;
2156 GNUNET_assert(rec_message !=NULL);
2157 //check if first fragment is present
2158 if (packetsize == MESSAGE_LENGHT_UNKNOWN)
2163 check_message_fragment_bitfield(rec_message);
2166 bitfield = ~bitfield;
2167 bitfield = bitfield >> (63 - rec_message->frag_tail->num);
2168 if (rec_message->received_fragments == bitfield)
2171 while (rec_queue != NULL)
2173 sum += rec_queue->size;
2174 rec_queue = rec_queue->next;
2176 //sum should always be smaller or equal of
2177 GNUNET_assert(sum <= packetsize);
2178 if (sum == packetsize)
2182 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2183 "check_rec_finished_msg: A message for %p is complete\n", session);
2187 //copy fragments together
2188 msg = GNUNET_malloc(packetsize);
2189 rec_queue = rec_message->frag_head;
2191 while (rec_queue != NULL)
2193 //TODO SAVE SOME COPY OPS AND CHECK CRC WITHOUT COPY
2194 memcpy(msg + aktnum, rec_queue->msg, rec_queue->size);
2195 aktnum += rec_queue->size;
2196 rec_queue = rec_queue->next;
2198 free_receive_message(plugin, rec_message);
2199 //call wlan_process_helper to process the message
2200 wlan_data_massage_handler(plugin, session_light,
2201 (struct GNUNET_MessageHeader*) msg);
2202 //wlan_data_helper (plugin, session_light, (struct GNUNET_MessageHeader*) msg);
2210 process_data(void *cls, void *client, const struct GNUNET_MessageHeader *hdr)
2213 GNUNET_assert(client != NULL);
2214 GNUNET_assert(cls != NULL);
2215 struct Session * session = (struct Session *) client;
2216 struct Plugin * plugin = (struct Plugin *) cls;
2218 struct GNUNET_TRANSPORT_ATS_Information distance[2];
2219 distance[0].type = htonl(GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2220 distance[0].value = htonl(1);
2221 distance[1].type = htonl(GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2222 distance[1].value = htonl(0);
2225 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2226 "Calling plugin->env->receive for session %p; %s\n", session,
2227 wlan_plugin_address_to_string(NULL, session->addr, 6));
2230 plugin->env->receive(plugin->env->cls, &(session->target), hdr,
2231 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance, 2, session,
2232 session->addr, sizeof(session->addr));
2236 * handels the data after all fragments are put together
2238 * @param session_light
2239 * @param hdr pointer to the data
2242 wlan_data_massage_handler(struct Plugin * plugin,
2243 struct Session_light * session_light,
2244 const struct GNUNET_MessageHeader * hdr)
2246 struct WlanHeader * wlanheader = NULL;
2247 struct Session * session = NULL;
2248 const char * tempmsg = NULL;
2249 const struct GNUNET_MessageHeader * temp_hdr = NULL;
2250 struct GNUNET_PeerIdentity tmptarget;
2252 if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_DATA)
2257 GNUNET_ERROR_TYPE_DEBUG,
2258 "Func wlan_data_massage_handler got GNUNET_MESSAGE_TYPE_WLAN_DATA size: %u\n",
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;
2269 tempmsg = (char*) &wlanheader[1];
2270 temp_hdr = (const struct GNUNET_MessageHeader *) &wlanheader[1];
2272 if (getcrc32(tempmsg, ntohs(wlanheader->header.size)) != ntohl(
2275 //wrong crc, dispose message
2276 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2277 "Wlan message Header crc was wrong\n");
2281 //if not in session list
2282 if (session == NULL)
2286 GNUNET_ERROR_TYPE_DEBUG,
2287 "WLAN client not in session list: packet size = %u, inner size = %u, header size = %u\n",
2288 ntohs(wlanheader->header.size), ntohs(temp_hdr->size),
2289 sizeof(struct WlanHeader));
2291 //try if it is a hello message
2292 if (ntohs(wlanheader->header.size) >= ntohs(temp_hdr->size)
2293 + sizeof(struct WlanHeader))
2295 if (ntohs(temp_hdr->type) == GNUNET_MESSAGE_TYPE_HELLO)
2297 if (GNUNET_HELLO_get_id(
2298 (const struct GNUNET_HELLO_Message *) temp_hdr,
2299 &tmptarget) == GNUNET_OK)
2301 session = create_session(plugin, session_light->addr);
2302 session_light->session = session;
2303 memcpy(&session->target, &tmptarget,
2304 sizeof(struct GNUNET_PeerIdentity));
2308 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2309 "WLAN client not in session list and hello message not okay\n");
2316 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2317 "WLAN client not in session list and not a hello message\n");
2324 GNUNET_ERROR_TYPE_WARNING,
2325 "WLAN client not in session list and message size in does not fit\npacket size = %u, inner size = %u, header size = %u\n",
2326 ntohs(wlanheader->header.size), ntohs(temp_hdr->size),
2327 sizeof(struct WlanHeader));
2332 //"receive" the message
2335 GNUNET_SERVER_mst_receive(plugin->data_tokenizer, session,
2336 (const char *) temp_hdr,
2337 ntohs(hdr->size) - sizeof(struct WlanHeader), GNUNET_YES, GNUNET_NO);
2343 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2344 "wlan_data_massage_handler got wrong message type\n");
2350 * function to add an ack to send it for a received fragment
2351 * @param plugin pointer to the global plugin structure
2352 * @param session pointer to the session this ack belongs to
2353 * @param bitfield bitfield to send
2354 * @param fh pointer to the fragmentation header which we would like to acknolage
2358 add_ack_for_send(struct Plugin * plugin, struct Session * session,
2359 uint64_t bitfield, struct FragmentationHeader * fh)
2361 struct AckSendQueue * ack;
2363 GNUNET_assert(plugin != NULL);
2364 GNUNET_assert(session != NULL);
2365 GNUNET_assert(fh != NULL);
2367 ack = GNUNET_malloc(sizeof(struct AckSendQueue));
2368 ack->fragments_field = bitfield;
2369 ack->message_id = ntohl(fh->message_id);
2370 ack->session = session;
2372 GNUNET_CONTAINER_DLL_insert_tail(plugin->ack_send_queue_head,
2373 plugin->ack_send_queue_tail, ack);
2378 * function to get the receive message from the message id and the session
2379 * @param plugin pointer to the plugin struct
2380 * @param session session this fragment belongs to
2381 * @param message_id id of the message
2384 struct Receive_Message_Queue *
2385 get_receive_message(struct Plugin * plugin, struct Session * session,
2386 uint32_t message_id)
2388 struct Receive_Message_Queue * rec_message = plugin->receive_messages_head;
2389 while (rec_message != NULL)
2391 if ((rec_message->message_id_in == message_id) && (rec_message->session
2396 rec_message = rec_message->next;
2402 * function to get the receive message of a session
2403 * @param plugin pointer to the plugin struct
2404 * @param session session this fragment belongs to
2407 struct Receive_Message_Queue *
2408 get_receive_message_from_session(struct Plugin * plugin,
2409 struct Session * session)
2411 struct Receive_Message_Queue * rec_message = plugin->receive_messages_head;
2412 while (rec_message != NULL)
2414 if (rec_message->session == session)
2418 rec_message = rec_message->next;
2424 * function to insert a received fragment into the right fragment queue of the right message
2425 * @param plugin pointer to the plugin struct
2426 * @param session_light pointer to the session_light struct of this message
2427 * @param session session this fragment belongs to
2428 * @param fh pointer to the header of the fragment
2429 * @return new fragment bitfield for the message
2433 insert_fragment_in_in_message_queue(struct Plugin * plugin,
2434 struct Session_light * session_light, struct Session * session,
2435 struct FragmentationHeader * fh)
2437 struct Receive_Fragment_Queue * rec_queue = NULL;
2438 struct Receive_Message_Queue * rec_message;
2439 const char * tempmsg = (char*) &fh[1];
2440 uint64_t retval = 0;
2442 //TODO fragments do not timeout
2443 //check if message_id is right or it is a new msg
2444 GNUNET_assert(fh != NULL);
2446 rec_message = get_receive_message(plugin, session, ntohs(fh->message_id));
2448 if (rec_message == NULL)
2450 if (session->fragment_messages_in_count < MESSAGES_IN_QUEUE_PER_SESSION)
2453 //new message incoming
2454 rec_message = GNUNET_malloc(sizeof (struct Receive_Message_Queue));
2455 rec_message->message_id_in = ntohs(fh->message_id);
2456 rec_message->rec_size = MESSAGE_LENGHT_UNKNOWN;
2457 rec_message->session = session;
2458 rec_message->timeout = GNUNET_TIME_absolute_add(
2459 GNUNET_TIME_absolute_get(), MESSAGE_IN_TIMEOUT);
2460 rec_message->received_fragments = 0;
2462 GNUNET_CONTAINER_DLL_insert(plugin->receive_messages_head, plugin->receive_messages_teil, rec_message);
2464 session->fragment_messages_in_count++;
2465 plugin->pending_receive_messages++;
2469 GNUNET_ERROR_TYPE_DEBUG,
2470 "New fragmented message started: message id %u, messages in for this session %u, messages in %u\n",
2471 rec_message->message_id_in, session->fragment_messages_in_count,
2472 plugin->pending_receive_messages);
2479 GNUNET_ERROR_TYPE_INFO,
2480 "WLAN fragment message_id and session message_id do not exist, max MESSAGES_IN_QUEUE_PER_SESSION reached\n");
2481 setBit((char *) &retval, ntohs(fh->fragment_off_or_num));
2486 if (is_double_msg(rec_message, fh) != GNUNET_YES)
2490 rec_queue = GNUNET_malloc(sizeof (struct Receive_Fragment_Queue) +
2491 ntohs(fh->header.size) - sizeof(struct FragmentationHeader));
2492 rec_queue->size = ntohs(fh->header.size)
2493 - sizeof(struct FragmentationHeader);
2494 rec_queue->num = ntohs(fh->fragment_off_or_num);
2495 rec_queue->msg = (char*) &(rec_queue[1]);
2496 //copy msg to buffer
2497 memcpy((char *)rec_queue->msg, tempmsg, rec_queue->size);
2498 insert_fragment_in_queue(rec_message, rec_queue);
2500 retval = rec_message->received_fragments;
2504 GNUNET_ERROR_TYPE_DEBUG,
2505 "New fragment: size %u, fragsize %u, message id %u, bitfield %X, session %u\n",
2506 rec_message->rec_size, rec_queue->size,
2507 rec_message->received_fragments, rec_message->message_id_in, session);
2510 check_rec_finished_msg(plugin, session_light, session, rec_message);
2514 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "WLAN fragment is a clone\n");
2515 retval = rec_message->received_fragments;
2523 * Function used for to process the data received from the wlan interface
2525 * @param cls the plugin handle
2526 * @param session_light FIXME: document
2527 * @param hdr hdr of the GNUNET_MessageHeader
2530 wlan_data_helper(void *cls, struct Session_light * session_light,
2531 const struct GNUNET_MessageHeader * hdr)
2533 struct Plugin *plugin = cls;
2534 struct Session * session = NULL;
2536 struct FragmentationHeader * fh = NULL;
2537 struct FragmentationAckHeader * fah = NULL;
2538 struct FragmentMessage * fm = NULL;
2540 const char * tempmsg = NULL;
2542 uint64_t fragment_bitfield = 0;
2545 if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT)
2548 //TODO better DOS protection, error handling
2549 //TODO test first than create session
2550 GNUNET_assert(session_light != NULL);
2554 GNUNET_ERROR_TYPE_DEBUG,
2555 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT size: %u; %s\n",
2556 ntohs(hdr->size), wlan_plugin_address_to_string(NULL,
2557 session_light->addr, 6));
2560 if (session_light->session == NULL)
2562 session_light->session = get_Session(plugin, session_light->addr);
2564 GNUNET_assert(GNUNET_HELLO_get_id(
2565 (const struct GNUNET_HELLO_Message *) &hdr[1],
2566 &(session_light->session->target) ) != GNUNET_SYSERR);
2571 else if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT)
2574 GNUNET_assert(session_light != NULL);
2575 if (session_light->session == NULL)
2577 session_light->session = search_session(plugin, session_light->addr);
2579 session = session_light->session;
2581 fh = (struct FragmentationHeader *) hdr;
2582 tempmsg = (char*) &fh[1];
2586 GNUNET_ERROR_TYPE_DEBUG,
2587 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT with message_id %u with fragment number %i, size: %u; %s\n",
2588 ntohl(fh->message_id), ntohs(fh->fragment_off_or_num), ntohs(
2589 hdr->size), wlan_plugin_address_to_string(NULL,
2590 session_light->addr, 6));
2593 if (getcrc16(tempmsg, ntohs(fh->header.size)) != ntohs(fh->message_crc))
2595 //wrong crc, dispose message
2596 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "WLAN fragment crc was wrong\n");
2600 //if in the session list
2601 if (session != NULL)
2603 fragment_bitfield = insert_fragment_in_in_message_queue(plugin,
2604 session_light, session, fh);
2610 GNUNET_ERROR_TYPE_INFO,
2611 "WLAN client not in session list, fragment num %u, message id %u\n",
2612 ntohs(fh->fragment_off_or_num), ntohs(fh->message_id));
2613 wlan_data_massage_handler(plugin, session_light,
2614 (struct GNUNET_MessageHeader *) tempmsg);
2615 session = session_light->session;
2616 //test if a session was created
2617 if (session == NULL)
2621 setBit((char *) &fragment_bitfield, ntohs(fh->fragment_off_or_num));
2624 add_ack_for_send(plugin, session, fragment_bitfield, fh);
2625 check_next_fragment_timeout(plugin);
2630 else if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT_ACK)
2635 GNUNET_ERROR_TYPE_DEBUG,
2636 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT_ACK size: %u; %s\n",
2637 ntohs(hdr->size), wlan_plugin_address_to_string(NULL,
2638 session_light->addr, 6));
2641 GNUNET_assert(session_light != NULL);
2642 if (session_light->session == NULL)
2644 session_light->session = search_session(plugin, session_light->addr);
2645 GNUNET_assert(session_light->session != NULL);
2647 session = session_light->session;
2648 fah = (struct FragmentationAckHeader *) hdr;
2649 fm = get_fragment_message_from_session_and_id(session, ntohl(
2654 fm->ack_bitfield = fm->ack_bitfield | ntohll(fah->fragment_field);
2655 check_finished_fragment(plugin, fm);
2659 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
2660 "WLAN fragment not in fragment list with id %u of ack\n", ntohl(
2669 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2670 "WLAN packet inside the WLAN helper packet has not the right type\n");
2675 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2676 "Helper finished\n");
2682 macprinter(struct MacAddress macx)
2684 static char macstr[20];
2685 char * mac = macx.mac;
2686 sprintf(macstr, "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X", mac[0], mac[1], mac[2],
2687 mac[3], mac[4], mac[5]);
2692 * Function used for to process the data from the suid process
2694 * @param cls the plugin handle
2695 * @param client client that send the data (not used)
2696 * @param hdr header of the GNUNET_MessageHeader
2700 wlan_process_helper(void *cls, void *client,
2701 const struct GNUNET_MessageHeader *hdr)
2703 struct Plugin *plugin = cls;
2704 struct IeeeHeader * wlanIeeeHeader = NULL;
2705 struct Session_light * session_light = NULL;
2706 const struct GNUNET_MessageHeader * temp_hdr = NULL;
2711 if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA)
2715 GNUNET_ERROR_TYPE_DEBUG,
2716 "Func wlan_process_helper got GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA size: %u\n",
2720 //call wlan_process_helper with the message inside, later with wlan: analyze signal
2721 GNUNET_assert(ntohs(hdr->size) >= sizeof(struct IeeeHeader) + sizeof(struct GNUNET_MessageHeader));
2722 wlanIeeeHeader = (struct IeeeHeader *) &hdr[1];
2724 //process only if it is an broadcast or for this computer both with the gnunet bssid
2727 if (memcmp(&(wlanIeeeHeader->mac3), mac_bssid, sizeof(struct MacAddress))
2730 //check for broadcast or mac
2731 if (memcmp(&(wlanIeeeHeader->mac1), bc_all_mac,
2732 sizeof(struct MacAddress) == 0) || memcmp(
2733 &(wlanIeeeHeader->mac1), &(plugin->mac_address),
2734 sizeof(struct MacAddress)) == 0)
2737 // process the inner data
2740 datasize = ntohs(hdr->size) - sizeof(struct IeeeHeader)
2741 - sizeof(struct GNUNET_MessageHeader);
2743 session_light = GNUNET_malloc(sizeof(struct Session_light));
2744 memcpy(session_light->addr, &(wlanIeeeHeader->mac2),
2745 sizeof(struct MacAddress));
2746 //session_light->session = search_session(plugin,session_light->addr);
2749 temp_hdr = (struct GNUNET_MessageHeader *) &wlanIeeeHeader[1];
2750 while (pos < datasize)
2752 temp_hdr = (struct GNUNET_MessageHeader *) &wlanIeeeHeader[1]
2755 wlan_data_helper(plugin, session_light, temp_hdr);
2756 pos += ntohs(temp_hdr->size);
2761 GNUNET_free(session_light);
2766 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2767 "Func wlan_process_helper got wrong MAC: %s\n", macprinter(
2768 wlanIeeeHeader->mac1));
2775 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2776 "Func wlan_process_helper got wrong BSSID: %s\n", macprinter(
2777 wlanIeeeHeader->mac2));
2783 else if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL)
2788 GNUNET_ERROR_TYPE_DEBUG,
2789 "Func wlan_process_helper got GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL size: %u\n",
2793 //TODO more control messages
2794 //TODO use struct wlan_helper_control
2795 if (ntohs(hdr->size) == sizeof(struct Wlan_Helper_Control_Message))
2797 //plugin->mac_address = GNUNET_malloc(sizeof(struct MacAddress));
2798 memcpy(&(plugin->mac_address), &hdr[1], sizeof(struct MacAddress));
2799 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2800 "Notifying transport of address %s\n",
2801 wlan_plugin_address_to_string(cls, &(plugin->mac_address), ntohs(
2802 hdr->size) - sizeof(struct GNUNET_MessageHeader)));
2803 plugin->env->notify_address(plugin->env->cls, "wlan",
2804 &plugin->mac_address, sizeof(struct MacAddress),
2805 GNUNET_TIME_UNIT_FOREVER_REL);
2809 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Wrong wlan mac address %s\n",
2810 macprinter(plugin->mac_address));
2818 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2819 "WLAN helper packet has not the right type\n");
2825 * We have been notified that wlan-helper has written something to stdout.
2826 * Handle the output, then reschedule this function to be called again once
2827 * more is available.
2829 * @param cls the plugin handle
2830 * @param tc the scheduling context
2834 wlan_plugin_helper_read(void *cls,
2835 const struct GNUNET_SCHEDULER_TaskContext *tc)
2837 struct Plugin *plugin = cls;
2838 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2843 "Start reading from STDIN\n");
2846 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2849 char mybuf[WLAN_MTU + sizeof(struct GNUNET_MessageHeader)];
2852 bytes = GNUNET_DISK_file_read(plugin->server_stdout_handle, mybuf,
2857 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2858 _("Finished reading from wlan-helper stdout with code: %d\n"),
2863 GNUNET_SERVER_mst_receive(plugin->suid_tokenizer, NULL, mybuf, bytes,
2864 GNUNET_NO, GNUNET_NO);
2866 GNUNET_assert(plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2867 plugin->server_read_task = GNUNET_SCHEDULER_add_read_file(
2868 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdout_handle,
2869 &wlan_plugin_helper_read, plugin);
2873 * Start the gnunet-wlan-helper process.
2875 * @param plugin the transport plugin
2876 * @param testmode should we use the dummy driver for testing?
2877 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
2880 wlan_transport_start_wlan_helper(struct Plugin *plugin, int testmode)
2882 const char * filename = "gnunet-transport-wlan-helper";
2883 plugin->server_stdout = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_NO, GNUNET_YES);
2884 if (plugin->server_stdout == NULL)
2885 return GNUNET_SYSERR;
2887 plugin->server_stdin = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_NO);
2888 if (plugin->server_stdin == NULL)
2889 return GNUNET_SYSERR;
2892 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2893 "Starting gnunet-wlan-helper process cmd: %s %s %i\n", filename,
2894 plugin->interface, testmode);
2896 /* Start the server process */
2898 plugin->server_proc = GNUNET_OS_start_process(plugin->server_stdin,
2899 plugin->server_stdout, filename, filename, plugin->interface, ((testmode
2900 == 1) ? "1" : (testmode == 2) ? "2" : "0"), NULL);
2901 if (plugin->server_proc == NULL)
2904 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2905 "Failed to start gnunet-wlan-helper process\n");
2907 return GNUNET_SYSERR;
2910 /* Close the write end of the read pipe */
2911 GNUNET_DISK_pipe_close_end(plugin->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
2913 /* Close the read end of the write pipe */
2914 GNUNET_DISK_pipe_close_end(plugin->server_stdin, GNUNET_DISK_PIPE_END_READ);
2916 plugin->server_stdout_handle = GNUNET_DISK_pipe_handle(plugin->server_stdout,
2917 GNUNET_DISK_PIPE_END_READ);
2918 plugin->server_stdin_handle = GNUNET_DISK_pipe_handle(plugin->server_stdin,
2919 GNUNET_DISK_PIPE_END_WRITE);
2921 GNUNET_assert(plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2924 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2925 "Adding server_read_task for the wlan-helper\n");
2930 plugin->server_read_task = GNUNET_SCHEDULER_add_read_file(
2931 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdout_handle,
2932 &wlan_plugin_helper_read, plugin);
2938 * Exit point from the plugin.
2939 * @param cls pointer to the api struct
2944 libgnunet_plugin_transport_wlan_done(void *cls)
2946 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2947 struct Plugin *plugin = api->cls;
2950 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2951 "libgnunet_plugin_transport_wlan_done started\n");
2954 GNUNET_assert(cls !=NULL);
2956 if (plugin->suid_tokenizer != NULL)
2957 GNUNET_SERVER_mst_destroy(plugin->suid_tokenizer);
2959 if (plugin->data_tokenizer != NULL)
2960 GNUNET_SERVER_mst_destroy(plugin->data_tokenizer);
2962 GNUNET_free_non_null(plugin->interface);
2963 GNUNET_free (plugin);
2969 * Entry point for the plugin.
2971 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2972 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2975 libgnunet_plugin_transport_wlan_init(void *cls)
2977 //struct GNUNET_SERVICE_Context *service;
2978 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2979 struct GNUNET_TRANSPORT_PluginFunctions *api;
2980 struct Plugin *plugin;
2981 static unsigned long long testmode = 0;
2983 GNUNET_assert(cls !=NULL);
2985 plugin = GNUNET_malloc (sizeof (struct Plugin));
2987 plugin->pendingsessions = 0;
2988 plugin->session_count = 0;
2989 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
2990 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2991 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
2993 set_next_beacon_time(plugin);
2995 if (GNUNET_CONFIGURATION_have_value(env->cfg, "transport-wlan", "TESTMODE"))
2997 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(env->cfg,
2998 "transport-wlan", "TESTMODE", &testmode))
3002 if (GNUNET_CONFIGURATION_have_value(env->cfg, "transport-wlan", "INTERFACE"))
3004 if (GNUNET_CONFIGURATION_get_value_string(env->cfg, "transport-wlan",
3005 "INTERFACE", &(plugin->interface)) != GNUNET_YES)
3007 libgnunet_plugin_transport_wlan_done(plugin);
3012 wlan_transport_start_wlan_helper(plugin, testmode);
3013 plugin->suid_tokenizer = GNUNET_SERVER_mst_create(&wlan_process_helper,
3016 plugin->data_tokenizer = GNUNET_SERVER_mst_create(&process_data, plugin);
3018 //plugin->sessions = GNUNET_malloc (sizeof (struct Sessionqueue));
3019 //plugin->pending_Sessions = GNUNET_malloc (sizeof (struct Sessionqueue));
3021 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3023 api->send = &wlan_plugin_send;
3024 api->disconnect = &wlan_plugin_disconnect;
3025 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
3026 api->check_address = &wlan_plugin_address_suggested;
3027 api->address_to_string = &wlan_plugin_address_to_string;
3029 start_next_message_id();
3032 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "wlan init finished\n");
3038 /* end of plugin_transport_wlan.c */