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
52 #define DEBUG_wlan GNUNET_YES
54 #define MESSAGE_LENGHT_UNKNOWN -1
55 #define NO_MESSAGE_OR_MESSAGE_FINISHED -2
59 * After how long do we expire an address that we
60 * learned from another peer if it is not reconfirmed
63 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
66 * Initial handshake message for a session.
71 * Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME.
73 struct GNUNET_MessageHeader header;
76 * Identit*mac_y of the node connecting (TCP client)
78 struct GNUNET_PeerIdentity clientIdentity;
84 * Encapsulation of all of the state of the plugin.
91 struct GNUNET_TRANSPORT_PluginEnvironment *env;
94 * List of open sessions. head
96 struct Sessionqueue *sessions;
99 * List of open sessions. tail
101 struct Sessionqueue *sessions_tail;
110 * encapsulation to the local wlan server prog
113 struct GNUNET_SERVER_MessageStreamTokenizer * consoltoken;
117 * stdout pipe handle for the gnunet-wlan-helper process
119 struct GNUNET_DISK_PipeHandle *server_stdout;
122 * stdout file handle for the gnunet-wlan-helper process
124 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
127 * stdin pipe handle for the gnunet-wlan-helper process
129 struct GNUNET_DISK_PipeHandle *server_stdin;
132 * stdin file handle for the gnunet-wlan-helper process
134 const struct GNUNET_DISK_FileHandle *server_stdin_handle;
137 * ID of the gnunet-wlan-server std read task
139 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
142 * ID of the gnunet-wlan-server std read task
144 GNUNET_SCHEDULER_TaskIdentifier server_write_task;
147 * ID of the delay task for writing
149 GNUNET_SCHEDULER_TaskIdentifier server_write_delay_task;
152 * The process id of the wlan process
154 struct GNUNET_OS_Process *server_proc;
157 * The interface of the wlan card given to us by the user.
162 * The mac_address of the wlan card given to us by the helper.
164 struct MacAddress mac_address;
167 * Sessions currently pending for transmission
168 * to this peer, if any.
170 struct Sessionqueue * pending_Sessions;
173 * Sessions currently pending for transmission
174 * to this peer (tail), if any.
176 struct Sessionqueue * pending_Sessions_tail;
179 * number of pending sessions
181 unsigned int pendingsessions;
184 * Messages in the fragmentation queue, head
187 struct FragmentMessage * pending_Fragment_Messages_head;
190 * Messages in the fragmentation queue, tail
193 struct FragmentMessage * pending_Fragment_Messages_tail;
196 * number of pending fragment message
199 unsigned int pending_fragment_messages;
202 * time of the next "hello-beacon"
205 struct GNUNET_TIME_Absolute beacon_time;
210 * Queue of sessions, for the general session queue and the pending session queue
215 struct Sessionqueue * next;
216 struct Sessionqueue * prev;
217 struct Session * content;
221 * Queue of ack received for messages send
226 struct AckQueue * next;
227 struct AckQueue * prev;
228 int fragment_num; //TODO change it to offset if better
234 * Queue for the fragments received
239 struct RecQueue * next;
240 struct RecQueue * prev;
247 * Information kept for each message that is yet to
250 struct PendingMessage
254 * The pending message
259 * Continuation function to call once the message
260 * has been sent. Can be NULL if there is no
261 * continuation to call.
263 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
266 * Cls for transmit_cont
268 void * transmit_cont_cls;
271 * Timeout value for the pending message.
273 struct GNUNET_TIME_Absolute timeout;
276 * Size of the message
283 * Session infos gathered from a messages
289 * the session this message belongs to
291 struct Session * session;
299 * Session handle for connections.
307 struct SessionHeader header;
310 * Pointer to the global plugin struct.
312 struct Plugin *plugin;
315 * Message currently pending for transmission
316 * to this peer, if any.
318 struct PendingMessage *pending_message;
321 * To whom are we talking to (set to our identity
322 * if we are still waiting for the welcome message)
324 struct GNUNET_PeerIdentity target;
327 * encapsulation of the receive data
329 //struct GNUNET_SERVER_MessageStreamTokenizer * receive_token;
332 * offset of the next fragment for the receive_token, -1 means last message finished
338 * size of the message received,
339 * MESSAGE_LENGHT_UNKNOWN means that the size is not known,
340 * NO_MESSAGE_OR_MESSAGE_FINISHED means no message received
346 * Sorted queue with the fragments received; head
349 struct RecQueue * frag_head;
352 * Sorted queue with the fragments received; tail
355 struct RecQueue * frag_tail;
363 * Address of the other peer (either based on our 'connect'
364 * call or on our 'accept' call).
369 * Last activity on this connection. Used to select preferred
372 struct GNUNET_TIME_Absolute last_activity;
375 * current number for message incoming, to distinguish between the messages
377 uint32_t message_id_in;
380 * current number for message outgoing, to distinguish between the messages
382 uint32_t message_id_out;
385 * does this session have a message in the fragment queue
395 struct FragmentMessage
398 * Session this message belongs to
401 struct Session *session;
404 * This is a doubly-linked list.
406 struct FragmentMessage *next;
409 * This is a doubly-linked list.
411 struct FragmentMessage *prev;
414 * The pending message
419 * Timeout value for the pending message.
421 struct GNUNET_TIME_Absolute timeout;
424 * Timeout value for the pending fragments.
425 * Stores the time when the next msg fragment ack has to be received
427 struct GNUNET_TIME_Absolute next_ack;
430 * Sorted queue with the acks received for fragments; head
433 struct AckQueue * head;
436 * Sorted queue with the acks received for fragments; tail
439 struct AckQueue * tail;
442 * Size of the message
447 * pos / next fragment number in the message, for fragmentation/segmentation,
448 * some acks can be missing but there is still time
450 uint32_t message_pos;
455 * Header for messages which need fragmentation
460 struct GNUNET_MessageHeader header;
463 * checksum/error correction
465 uint32_t crc GNUNET_PACKED;
468 * To whom are we talking to (set to our identity
469 * if we are still waiting for the welcome message)
471 struct GNUNET_PeerIdentity target;
473 // followed by payload
478 * Header for messages which need fragmentation
480 struct FragmentationHeader
483 struct GNUNET_MessageHeader header;
486 * To whom are we talking to (set to our identity
487 * if we are still waiting for the welcome message)
489 // struct GNUNET_PeerIdentity target GNUNET_PACKED;
492 * ID of message, to distinguish between the messages, picked randomly.
494 uint32_t message_id GNUNET_PACKED;
497 * Offset or number of this fragment, for fragmentation/segmentation (design choice, TBD)
499 uint16_t fragment_off_or_num GNUNET_PACKED;
502 * CRC of fragment (for error checking)
504 uint16_t message_crc GNUNET_PACKED;
508 * // 0x1 ack => Use two different message types in header.type! (FRAG_MESSAGE; FRAG_ACK)
509 * // 0x2 has data (not only ack)
510 * // 0x4 last fragment of message
513 // uint32_t flags GNUNET_PACKED;
516 * checksum/error correction
518 // uint32_t crc GNUNET_PACKED;
520 // followed by payload unless ACK
524 //enum { ACK_FRAGMENT = 1, DATA_FRAGMENT = 2, LAST_FRAGMENT = 4, NEW_MESSAGE = 8 };
527 getRadiotapHeader(struct RadiotapHeader * Header);
530 getWlanHeader(struct IeeeHeader * Header);
533 wlan_plugin_address_suggested(void *cls, const void *addr, size_t addrlen);
536 getcrc16(const char *msgbuf, size_t msgbuf_size);
539 do_transmit(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
542 check_fragment_queue(struct Plugin * plugin);
545 getcrc32(const char *msgbuf, size_t msgbuf_size);
548 free_rec_frag_queue(struct Session * session);
551 wlan_data_helper(void *cls, void * client, const struct GNUNET_MessageHeader * hdr);
554 wlan_process_helper (void *cls,
556 const struct GNUNET_MessageHeader *hdr);
559 * get the next message number, at the moment just a random one
560 * @return returns the next valid message-number for sending packets
563 get_next_message_id()
565 return GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
569 * start next message number generator
570 * (not necessary at the moment)
573 start_next_message_id()
575 //GNUNET_CRYPTO_random_init;
579 * search for a session with the addr
581 * @param plugin pointer to the plugin struct
582 * @param addr pointer to the mac address of the peer
583 * @return returns the session
586 static struct Session *
587 search_session(struct Plugin *plugin, const uint8_t * addr)
589 struct Sessionqueue * queue = plugin->sessions;
590 struct Sessionqueue * lastitem = NULL;
592 //just look at all the session for the needed one
593 while (queue != NULL)
595 // content is never NULL
596 GNUNET_assert (queue->content != NULL);
597 char * addr2 = queue->content->addr;
598 if (memcmp(addr, addr2, 6) == 0)
601 return queue->content;
611 * create a new session
613 * @param plugin pointer to the plugin struct
614 * @param addr pointer to the mac address of the peer
615 * @return returns the session
618 static struct Session *
619 create_session(struct Plugin *plugin,const uint8_t * addr)
621 struct Sessionqueue * queue = GNUNET_malloc (sizeof (struct Sessionqueue));
623 GNUNET_CONTAINER_DLL_insert_tail(plugin->sessions, plugin->sessions_tail, queue);
625 queue->content = GNUNET_malloc (sizeof (struct Session));
626 queue->content->plugin = plugin;
627 memcpy(queue->content->addr, addr, 6);
628 queue->content->message_id_out = get_next_message_id();
629 queue->content->has_fragment = 0;
630 queue->content->rec_size = NO_MESSAGE_OR_MESSAGE_FINISHED;
632 plugin->session_count++;
633 return queue->content;
637 * get Session from address, create if no session exists
639 * @param plugin pointer to the plugin struct
640 * @param addr pointer to the mac address of the peer
641 * @return returns the session
643 //TODO add other possibilities to find the right session (are there other?)
644 static struct Session *
645 get_Session(struct Plugin *plugin, const uint8_t * addr)
647 struct Session * session = search_session(plugin, addr);
653 return create_session(plugin, addr);
655 /* -- not needed, layer above already has it--
656 //queue welcome message for new sessions, not realy needed
657 //struct WelcomeMessage welcome;
658 struct PendingMessage *pm;
659 pm = GNUNET_malloc (sizeof (struct PendingMessage));
660 pm->msg = GNUNET_malloc(GNUNET_HELLO_size(* (plugin->env->our_hello)));
661 pm->message_size = GNUNET_HELLO_size(* (plugin->env->our_hello));
662 //welcome.header.size = htons (GNUNET_HELLO_size(* (plugin->env->our_hello)));
663 //welcome.header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT);
664 //welcome.clientIdentity = *plugin->env->my_identity;
665 memcpy ( (pm->msg), * plugin->env->our_hello, GNUNET_HELLO_size(* (plugin->env->our_hello)));
666 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
667 queue->content->pending_message = pm;
668 plugin->pendingsessions ++;
669 GNUNET_CONTAINER_DLL_insert_tail(plugin->pending_Sessions, plugin->pending_Sessions_tail, queue);
671 check_fragment_queue(plugin);
676 * Queue the session to send data
680 queue_Session (struct Plugin *plugin,
681 struct Session * session)
683 struct Sessionqueue * queue = plugin->pending_Sessions;
684 struct Sessionqueue * lastitem = NULL;
686 while (queue != NULL){
687 // content is never NULL
688 GNUNET_assert (queue->content == NULL);
689 // is session already in queue?
690 if (session == queue->content){
698 // Session is not in the queue
700 queue = GNUNET_malloc (sizeof (struct Sessionqueue));
701 queue->content = session;
704 GNUNET_CONTAINER_DLL_insert_after (plugin->pending_Sessions,
705 plugin->pending_Sessions_tail,
706 plugin->pending_Sessions_tail, queue);
707 plugin->pendingsessions ++;
713 free_acks (struct FragmentMessage * fm){
714 struct AckQueue * fq;
715 while (fm->head != NULL){
717 GNUNET_CONTAINER_DLL_remove(fm->head, fm->tail, fq);
727 * Function to schedule the write task, executed after a delay
730 delay_fragment_task(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
732 struct Plugin * plugin = cls;
733 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
735 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
738 // GNUNET_TIME_UNIT_FOREVER_REL is needed to clean up old msg
739 if (plugin->server_write_task == GNUNET_SCHEDULER_NO_TASK)
741 plugin->server_write_task = GNUNET_SCHEDULER_add_write_file(
742 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdin_handle,
743 &do_transmit, plugin);
749 * Function to calculate the time of the next periodic "hello-beacon"
752 set_next_beacon_time(struct Plugin * const plugin)
754 //under 10 known peers: once a second
755 if (plugin->session_count < 10)
757 plugin->beacon_time = GNUNET_TIME_absolute_add(
758 GNUNET_TIME_absolute_get(), GNUNET_TIME_UNIT_SECONDS);
760 //under 30 known peers: every 10 seconds
761 else if (plugin->session_count < 30)
763 plugin->beacon_time = GNUNET_TIME_absolute_add(
764 GNUNET_TIME_absolute_get(), GNUNET_TIME_relative_multiply(
765 GNUNET_TIME_UNIT_SECONDS, 10));
767 //over 30 known peers: once a minute
770 plugin->beacon_time = GNUNET_TIME_absolute_add(
771 GNUNET_TIME_absolute_get(), GNUNET_TIME_UNIT_MINUTES);
777 struct GNUNET_TIME_Relative
778 get_next_frag_timeout(struct FragmentMessage * fm)
780 return GNUNET_TIME_relative_min(GNUNET_TIME_absolute_get_remaining(
781 fm->next_ack), GNUNET_TIME_absolute_get_remaining(fm->timeout));
786 * Function to get the timeout value for acks for this session
789 struct GNUNET_TIME_Relative
790 get_ack_timeout (struct FragmentMessage * fm){
791 return FRAGMENT_TIMEOUT;
796 * Function to set the timer for the next timeout of the fragment queue
797 * @param plugin the handle to the plugin struct
800 check_next_fragment_timeout(struct Plugin * const plugin)
802 struct FragmentMessage * fm;
803 struct GNUNET_TIME_Relative next_send;
805 next_send = GNUNET_TIME_absolute_get_remaining(plugin->beacon_time);
808 if (plugin->server_write_delay_task != GNUNET_SCHEDULER_NO_TASK)
810 GNUNET_SCHEDULER_cancel(plugin->server_write_delay_task);
812 fm = plugin->pending_Fragment_Messages_head;
814 GNUNET_assert(plugin->server_write_delay_task == GNUNET_SCHEDULER_NO_TASK);
816 //check if there are some fragments in the queue
820 = GNUNET_TIME_relative_min(next_send, get_next_frag_timeout(fm));
822 plugin->server_write_delay_task = GNUNET_SCHEDULER_add_delayed(next_send,
823 &delay_fragment_task, plugin);
830 * Function to get the next queued Session, removes the session from the queue
833 static struct Session *
834 get_next_queue_Session (struct Plugin * plugin){
835 struct Session * session;
836 struct Sessionqueue * sessionqueue;
837 struct Sessionqueue * sessionqueue_alt;
838 struct PendingMessage * pm;
839 sessionqueue = plugin->pending_Sessions;
840 while (sessionqueue != NULL){
841 session = sessionqueue->content;
842 pm = session->pending_message;
844 //check for message timeout
845 if (GNUNET_TIME_absolute_get_remaining(pm->timeout).rel_value > 0){
846 //check if session has no message in the fragment queue
847 if (! session->has_fragment){
848 plugin->pendingsessions --;
849 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions,
850 plugin->pending_Sessions_tail, sessionqueue);
851 GNUNET_free(sessionqueue);
855 sessionqueue = sessionqueue->next;
859 session->pending_message = NULL;
860 //call the cont func that it did not work
861 if (pm->transmit_cont != NULL)
862 pm->transmit_cont (pm->transmit_cont_cls,
863 &(session->target), GNUNET_SYSERR);
864 GNUNET_free(pm->msg);
867 sessionqueue_alt = sessionqueue;
868 sessionqueue = sessionqueue->next;
869 plugin->pendingsessions --;
870 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions,
871 plugin->pending_Sessions_tail, sessionqueue_alt);
873 GNUNET_free(sessionqueue_alt);
884 * Function to sort the message into the message fragment queue
885 * @prarm plugin the plugin struct
886 * @param message to sort into the queue
889 sort_fragment_into_queue (struct Plugin * plugin, struct FragmentMessage * fm){
890 struct FragmentMessage * fm2;
891 //sort into the list at the right position
893 fm2 = plugin->pending_Fragment_Messages_head;
896 if (GNUNET_TIME_absolute_get_difference(fm2->next_ack, fm->next_ack).rel_value == 0){
903 GNUNET_CONTAINER_DLL_insert_after(plugin->pending_Fragment_Messages_head,
904 plugin->pending_Fragment_Messages_tail,fm2,fm);
908 * frees the space of a message in the fragment queue (send queue)
909 * @param plugin the plugin struct
910 * @param fm message to free
913 free_fragment_message(struct Plugin * plugin,struct FragmentMessage * fm)
918 GNUNET_free_non_null(fm->msg);
919 GNUNET_CONTAINER_DLL_remove (plugin->pending_Fragment_Messages_head,
920 plugin->pending_Fragment_Messages_tail, fm);
922 plugin->pending_fragment_messages --;
923 check_fragment_queue(plugin);
928 * Function to check if there is some space in the fragment queue
929 * inserts a message if space is available
930 * @param plugin the plugin struct
934 check_fragment_queue (struct Plugin * plugin){
935 struct Session * session;
936 struct FragmentMessage * fm;
938 struct PendingMessage * pm;
940 if (plugin->pending_fragment_messages < FRAGMENT_QUEUE_SIZE){
941 session = get_next_queue_Session(plugin);
942 if (session != NULL){
943 pm = session->pending_message;
944 session->pending_message = NULL;
945 session->has_fragment = 1;
946 GNUNET_assert(pm != NULL);
948 fm = GNUNET_malloc(sizeof(struct FragmentMessage));
949 fm->message_size = pm->message_size;
951 fm->session = session;
952 fm->timeout.abs_value = pm->timeout.abs_value;
954 fm->next_ack = GNUNET_TIME_absolute_get();
956 if (pm->transmit_cont != NULL)
957 pm->transmit_cont (pm->transmit_cont_cls,
958 &(session->target), GNUNET_OK);
961 sort_fragment_into_queue(plugin,fm);
962 plugin->pending_fragment_messages ++;
964 //generate new message id
965 session->message_id_out = get_next_message_id();
967 //check if timeout changed
968 check_next_fragment_timeout(plugin);
974 * Funktion to check if all fragments where send and the acks received
975 * frees the space if finished
976 * @param plugin the plugin struct
977 * @param fm the message to check
980 check_finished_fragment(struct Plugin * plugin, struct FragmentMessage * fm){
981 struct AckQueue * ack;
984 if (fm->message_size >= (WLAN_MTU - sizeof(struct FragmentationHeader))
985 * fm->tail->fragment_num)
989 //check if all acks are present
992 if (counter == ack->fragment_num)
1001 fm->session->has_fragment = 0;
1002 free_fragment_message(plugin, fm);
1009 * Function called to when wlan helper is ready to get some data
1011 * @param cls closure
1012 * @param tc GNUNET_SCHEDULER_TaskContext
1016 do_transmit (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1019 struct Plugin * plugin = cls;
1020 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
1024 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1027 struct Session * session = NULL;
1028 struct FragmentMessage * fm = NULL;
1029 struct IeeeHeader * ieeewlanheader = NULL;
1030 struct RadiotapHeader * radioHeader = NULL;
1031 struct GNUNET_MessageHeader * msgheader = NULL;
1032 struct GNUNET_MessageHeader * msgheader2 = NULL;
1033 struct FragmentationHeader fragheader;
1034 struct FragmentationHeader * fragheaderptr = NULL;
1036 const char * copystart = NULL;
1037 uint16_t copysize = 0;
1038 uint copyoffset = 0;
1039 struct AckQueue * akt = NULL;
1041 //test if a "hello-beacon" has to be send
1042 if (GNUNET_TIME_absolute_get_remaining(plugin->beacon_time).rel_value == 0)
1044 //check if the message is not to big
1045 GNUNET_assert(sizeof(struct WlanHeader) + GNUNET_HELLO_size(
1046 *(plugin->env->our_hello)) <= WLAN_MTU);
1047 size = sizeof(struct GNUNET_MessageHeader)
1048 + sizeof(struct RadiotapHeader) + sizeof(struct IeeeHeader)
1049 + sizeof(struct GNUNET_MessageHeader) + GNUNET_HELLO_size(
1050 *(plugin->env->our_hello));
1052 msgheader = GNUNET_malloc(size);
1053 msgheader->size = htons(size);
1054 msgheader->type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1056 radioHeader = (struct RadiotapHeader *) &msgheader[1];
1057 getRadiotapHeader(radioHeader);
1059 ieeewlanheader = (struct IeeeHeader *) &radioHeader[1];
1060 getWlanHeader(ieeewlanheader);
1062 msgheader2 = (struct GNUNET_MessageHeader *) &ieeewlanheader[1];
1063 msgheader2->size = htons(GNUNET_HELLO_size(*(plugin->env->our_hello)) + sizeof(struct GNUNET_MessageHeader));
1064 msgheader2->type = htons(GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT);
1066 memcpy(&msgheader2[1], *plugin->env->our_hello, GNUNET_HELLO_size(
1067 *(plugin->env->our_hello)));
1070 bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle, msgheader,
1072 GNUNET_assert(bytes == size);
1074 set_next_beacon_time(plugin);
1075 check_next_fragment_timeout(plugin);
1081 fm = plugin->pending_Fragment_Messages_head;
1082 GNUNET_assert(fm != NULL);
1083 session = fm->session;
1084 GNUNET_assert(session != NULL);
1086 // test if message timed out
1087 if (GNUNET_TIME_absolute_get_remaining(fm->timeout).rel_value == 0){
1089 GNUNET_assert(plugin->pending_fragment_messages > 0);
1090 plugin->pending_fragment_messages --;
1091 GNUNET_CONTAINER_DLL_remove(plugin->pending_Fragment_Messages_head,
1092 plugin->pending_Fragment_Messages_tail, fm);
1094 GNUNET_free(fm->msg);
1097 check_fragment_queue(plugin);
1100 if (fm->message_size > WLAN_MTU) {
1101 size += sizeof(struct FragmentationHeader);
1102 // check/set for retransmission
1103 if (GNUNET_TIME_absolute_get_duration(fm->next_ack).rel_value == 0) {
1105 // be positive and try again later :-D
1106 fm->next_ack = GNUNET_TIME_relative_to_absolute(get_ack_timeout(fm));
1107 // find first missing fragment
1109 fm->message_pos = 0;
1111 //test if ack 0 was already received
1112 while (akt != NULL){
1113 //if fragment is present, take next
1114 if (akt->fragment_num == fm->message_pos) {
1117 //next ack is bigger then the fragment number
1118 //in case there is something like this: (acks) 1, 2, 5, 6, ...
1119 //and we send 3 again, the next number should be 4
1120 else if (akt->fragment_num > fm->message_pos) {
1131 copyoffset = (WLAN_MTU - sizeof(struct FragmentationHeader)) * fm->message_pos;
1132 fragheader.fragment_off_or_num = htons(fm->message_pos);
1133 fragheader.message_id = htonl(session->message_id_out);
1135 // start should be smaller then the packet size
1136 GNUNET_assert(copyoffset < fm->message_size);
1137 copystart = fm->msg + copyoffset;
1139 //size of the fragment is either the MTU - overhead
1140 //or the missing part of the message in case this is the last fragment
1141 copysize = GNUNET_MIN(fm->message_size - copyoffset,
1142 WLAN_MTU - sizeof(struct FragmentationHeader));
1143 fragheader.header.size = htons(copysize + sizeof(struct FragmentationHeader));
1144 fragheader.header.type = htons(GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT);
1147 //get the next missing fragment
1151 //test if ack was already received
1152 while (akt != NULL){
1153 //if fragment is present, take next
1154 if (akt->fragment_num == fm->message_pos) {
1157 //next ack is bigger then the fragment number
1158 //in case there is something like this: (acks) 1, 2, 5, 6, ...
1159 //and we send 3 again, the next number should be 4
1160 else if (akt->fragment_num > fm->message_pos) {
1167 // there is no need to split
1168 copystart = fm->msg;
1169 copysize = fm->message_size;
1173 size += sizeof(struct RadiotapHeader) + sizeof(struct IeeeHeader)
1174 + sizeof(struct GNUNET_MessageHeader);
1175 msgheader = GNUNET_malloc(size);
1176 msgheader->size = htons(size);
1177 msgheader->type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA);
1179 radioHeader = (struct RadiotapHeader*) &msgheader[1];
1180 getRadiotapHeader(radioHeader);
1182 ieeewlanheader = (struct IeeeHeader *) &radioHeader[1];
1183 getWlanHeader(ieeewlanheader);
1186 //could be faster if content is just send and not copyed before
1187 //fragmentheader is needed
1188 if (fm->message_size > WLAN_MTU){
1189 fragheader.message_crc = htons(getcrc16(copystart, copysize));
1190 memcpy(&ieeewlanheader[1],&fragheader, sizeof(struct FragmentationHeader));
1191 fragheaderptr = (struct FragmentationHeader *) &ieeewlanheader[1];
1192 memcpy(&fragheaderptr[1],copystart,copysize);
1194 memcpy(&ieeewlanheader[1],copystart,copysize);
1197 bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle, msgheader, size);
1198 GNUNET_assert(bytes == size);
1200 //check if this was the last fragment of this message, if true then queue at the end of the list
1201 if (copysize + copyoffset >= fm->message_size){
1202 GNUNET_assert(copysize + copyoffset == fm->message_size);
1204 GNUNET_CONTAINER_DLL_remove (plugin->pending_Fragment_Messages_head,
1205 plugin->pending_Fragment_Messages_tail, fm);
1207 GNUNET_CONTAINER_DLL_insert_tail(plugin->pending_Fragment_Messages_head,
1208 plugin->pending_Fragment_Messages_tail, fm);
1209 // if fragments have opimized timeouts
1210 //sort_fragment_into_queue(plugin,fm);
1213 check_next_fragment_timeout(plugin);
1222 * @param msgbuf pointer tor the data
1223 * @param msgbuf_size size of the data
1225 * @return 32bit crc value
1229 getcrc32 (const char *msgbuf,
1230 size_t msgbuf_size){
1231 //TODO calc some crc
1238 * @param msgbuf pointer tor the data
1239 * @param msgbuf_size size of the data
1241 * @return 16bit crc value
1245 getcrc16 (const char *msgbuf,
1246 size_t msgbuf_size){
1247 //TODO calc some crc
1252 * Function that can be used by the transport service to transmit
1253 * a message using the plugin.
1255 * @param cls closure
1256 * @param target who should receive this message
1257 * @param priority how important is the message
1258 * @param msgbuf the message to transmit
1259 * @param msgbuf_size number of bytes in 'msgbuf'
1260 * @param timeout when should we time out
1261 * @param session which session must be used (or NULL for "any")
1262 * @param addr the address to use (can be NULL if the plugin
1263 * is "on its own" (i.e. re-use existing TCP connection))
1264 * @param addrlen length of the address in bytes
1265 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1266 * otherwise the plugin may use other addresses or
1267 * existing connections (if available)
1268 * @param cont continuation to call once the message has
1269 * been transmitted (or if the transport is ready
1270 * for the next transmission call; or if the
1271 * peer disconnected...)
1272 * @param cont_cls closure for cont
1273 * @return number of bytes used (on the physical network, with overheads);
1274 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1275 * and does NOT mean that the message was not transmitted (DV)
1278 wlan_plugin_send (void *cls,
1279 const struct GNUNET_PeerIdentity * target,
1282 unsigned int priority,
1283 struct GNUNET_TIME_Relative timeout,
1284 struct Session *session,
1288 GNUNET_TRANSPORT_TransmitContinuation cont,
1291 struct Plugin * plugin = cls;
1292 struct PendingMessage * newmsg = NULL;
1293 struct WlanHeader * wlanheader = NULL;
1294 //check if msglen > 0
1295 GNUNET_assert(msgbuf_size > 0);
1297 //get session if needed
1298 if (session == NULL)
1300 if (wlan_plugin_address_suggested(plugin, addr, addrlen) == GNUNET_OK)
1302 session = get_Session(plugin, addr);
1306 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1307 _("Wlan Address len %d is wrong\n"), addrlen);
1312 //TODO target "problem" not solved
1313 session->target = *target;
1316 //first queue session
1317 queue_Session(plugin, session);
1319 //queue message in session
1320 //test if there is no other message in the "queue"
1321 GNUNET_assert (session->pending_message == NULL);
1323 newmsg = GNUNET_malloc(sizeof(struct PendingMessage));
1324 (newmsg->msg) = GNUNET_malloc(msgbuf_size + sizeof(struct WlanHeader));
1325 wlanheader = (struct WlanHeader *) newmsg->msg;
1326 //copy msg to buffer, not fragmented / segmented yet, but with message header
1327 wlanheader->header.size = htons(msgbuf_size + sizeof(struct WlanHeader));
1328 wlanheader->header.type = htons(GNUNET_MESSAGE_TYPE_WLAN_DATA);
1329 memcpy(&(wlanheader->target), target, sizeof(struct GNUNET_PeerIdentity));
1330 wlanheader->crc = htonl(getcrc32(msgbuf, msgbuf_size));
1331 memcpy(&wlanheader[1], msgbuf, msgbuf_size);
1332 newmsg->transmit_cont = cont;
1333 newmsg->transmit_cont_cls = cont_cls;
1334 newmsg->timeout = GNUNET_TIME_relative_to_absolute(timeout);
1335 newmsg->message_size = msgbuf_size + sizeof(struct WlanHeader);
1337 session->pending_message = newmsg;
1339 check_fragment_queue(plugin);
1340 //FIXME not the correct size
1346 static struct FragmentMessage *
1347 get_fragment_message_from_session(struct Session * session)
1349 struct FragmentMessage * fm = session->plugin->pending_Fragment_Messages_head;
1352 if (fm->session == session)
1362 * Function that can be used to force the plugin to disconnect
1363 * from the given peer and cancel all previous transmissions
1364 * (and their continuation).
1366 * @param cls closure
1367 * @param target peer from which to disconnect
1370 wlan_plugin_disconnect(void *cls, const struct GNUNET_PeerIdentity *target)
1372 struct Plugin *plugin = cls;
1373 struct Sessionqueue * queue = plugin->sessions;
1374 struct Sessionqueue * pendingsession = plugin->pending_Sessions;
1375 struct PendingMessage * pm = NULL;
1376 struct FragmentMessage * fm;
1378 // just look at all the session for the needed one
1379 while (queue != NULL)
1381 // content is never NULL
1382 GNUNET_assert (queue->content != NULL);
1383 if (memcmp(target, &(queue->content->target),
1384 sizeof(struct GNUNET_PeerIdentity)) == 0)
1387 //is this session pending for send
1388 while (pendingsession != NULL)
1390 if (pendingsession->content == queue->content)
1392 plugin->pendingsessions --;
1393 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions,
1394 plugin->pending_Sessions_tail, pendingsession);
1395 GNUNET_free(pendingsession);
1398 pendingsession = pendingsession->next;
1401 //is something of this session in the fragment queue?
1402 fm = get_fragment_message_from_session(queue->content);
1403 free_fragment_message(plugin,fm);
1405 //dispose all received fragments
1406 free_rec_frag_queue(queue->content);
1408 // remove PendingMessage
1409 pm = queue->content->pending_message;
1410 GNUNET_free(pm->msg);
1413 GNUNET_free(queue->content);
1414 GNUNET_CONTAINER_DLL_remove(plugin->sessions, plugin->sessions_tail, queue);
1416 plugin->session_count --;
1421 queue = queue->next;
1427 * Convert the transports address to a nice, human-readable
1430 * @param cls closure
1431 * @param type name of the transport that generated the address
1432 * @param addr one of the addresses of the host, NULL for the last address
1433 * the specific address format depends on the transport
1434 * @param addrlen length of the address
1435 * @param numeric should (IP) addresses be displayed in numeric form?
1436 * @param timeout after how long should we give up?
1437 * @param asc function to call on each string
1438 * @param asc_cls closure for asc
1441 wlan_plugin_address_pretty_printer (void *cls,
1446 struct GNUNET_TIME_Relative timeout,
1447 GNUNET_TRANSPORT_AddressStringCallback
1451 const unsigned char * input;
1453 GNUNET_assert(cls !=NULL);
1456 /* invalid address (MAC addresses have 6 bytes) */
1458 asc (asc_cls, NULL);
1461 input = (const unsigned char*) addr;
1462 GNUNET_snprintf (ret,
1464 "%s Mac-Adress %X:%X:%X:%X:%X:%X",
1466 input[0], input[1], input[2], input[3], input[4], input[5]);
1473 * Another peer has suggested an address for this
1474 * peer and transport plugin. Check that this could be a valid
1475 * address. If so, consider adding it to the list
1478 * @param cls closure
1479 * @param addr pointer to the address
1480 * @param addrlen length of addr
1481 * @return GNUNET_OK if this is a plausible address for this peer
1487 wlan_plugin_address_suggested (void *cls,
1491 //struct Plugin *plugin = cls;
1493 /* check if the address is plausible; if so,
1494 add it to our list! */
1496 GNUNET_assert(cls !=NULL);
1497 //FIXME mitm is not checked
1498 //Mac Adress has 6 bytes
1500 /* TODO check for bad addresses like milticast, broadcast, etc */
1503 return GNUNET_SYSERR;
1506 return GNUNET_SYSERR;
1511 * Function called for a quick conversion of the binary address to
1512 * a numeric address. Note that the caller must not free the
1513 * address and that the next call to this function is allowed
1514 * to override the address again.
1516 * @param cls closure
1517 * @param addr binary address
1518 * @param addrlen length of the address
1519 * @return string representing the same address
1522 wlan_plugin_address_to_string (void *cls,
1527 const unsigned char * input;
1529 GNUNET_assert(cls !=NULL);
1532 /* invalid address (MAC addresses have 6 bytes) */
1536 input = (const unsigned char*) addr;
1537 GNUNET_snprintf (ret,
1539 "%s Mac-Adress %X:%X:%X:%X:%X:%X",
1541 input[0], input[1], input[2], input[3], input[4], input[5]);
1542 return GNUNET_strdup (ret);
1546 * Function to test if fragment number already exists in the fragments received
1548 * @param session session the fragment belongs to
1549 * @param fh Fragmentheader of the fragment
1550 * @return GNUNET_YES if fragment exists already, GNUNET_NO if it does not exists in the queue of the session
1554 is_double_msg(struct Session * session, struct FragmentationHeader * fh)
1556 struct RecQueue * rec_queue = session->frag_head;
1557 while (rec_queue != NULL)
1559 if (rec_queue->num == fh->fragment_off_or_num)
1563 rec_queue = rec_queue->next;
1570 * Function to insert a fragment in a queue of a session
1571 * @param session session the fragment belongs to
1572 * @param rec_queue fragment to add
1576 insert_fragment_in_queue(struct Session * session, struct RecQueue * rec_queue)
1578 struct RecQueue * rec_queue2 = session->frag_head;
1579 struct WlanHeader * wlanheader = NULL;
1580 //first received fragment of message
1581 if (session->rec_size == NO_MESSAGE_OR_MESSAGE_FINISHED)
1583 session->rec_size = MESSAGE_LENGHT_UNKNOWN;
1585 //this is the first fragment of the message (fragment id 0)
1586 if (rec_queue->num == 0)
1588 wlanheader = (struct WlanHeader *) rec_queue->msg;
1589 session->rec_size = wlanheader->header.size;
1593 while (rec_queue2 != NULL)
1595 if (rec_queue2->num > rec_queue->num)
1597 //next element number is grater than the current num
1598 GNUNET_CONTAINER_DLL_insert_before(session->frag_head, session->frag_tail, rec_queue2, rec_queue);
1601 rec_queue = rec_queue->next;
1603 //no element has a grater number
1604 GNUNET_CONTAINER_DLL_insert_tail(session->frag_head, session->frag_tail, rec_queue);
1608 * Function to dispose the fragments received for a message
1609 * @param session session to free the fragments from
1613 free_rec_frag_queue(struct Session * session)
1615 struct RecQueue * rec_queue = session->frag_head;
1616 struct RecQueue * rec_queue2;
1617 while (rec_queue != NULL)
1619 rec_queue2 = rec_queue;
1620 rec_queue = rec_queue->next;
1621 GNUNET_free(rec_queue2);
1623 session->frag_head = NULL;
1624 session->frag_tail = NULL;
1625 session->rec_size = NO_MESSAGE_OR_MESSAGE_FINISHED;
1629 * Function to check if all fragments of a message have been received
1630 * @param plugin the plugin handle
1631 * @param session_light information of the message sender
1632 * @param session session the message belongs to
1636 check_rec_finished_msg (struct Plugin* plugin, struct Session_light * session_light, struct Session * session){
1637 struct RecQueue * rec_queue = session->frag_head;
1638 int packetsize = session->rec_size;
1642 //some fragment should be received
1643 GNUNET_assert(session->rec_size != NO_MESSAGE_OR_MESSAGE_FINISHED);
1644 //check if first fragment is present
1645 if (session->rec_size == MESSAGE_LENGHT_UNKNOWN){
1648 while (rec_queue != NULL){
1649 sum += rec_queue->size;
1650 //check if all fragment numbers are present
1651 if (rec_queue->num != aktnum){
1655 rec_queue = rec_queue->next;
1657 //sum should always be smaller or equal of
1658 GNUNET_assert(sum <= packetsize);
1659 if(sum == packetsize){
1662 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1663 "check_rec_finished_msg: A message with fragments is complete\n");
1666 //copy fragments together
1667 msg = GNUNET_malloc(packetsize);
1668 rec_queue = session->frag_head;
1670 while (rec_queue != NULL){
1671 memcpy(msg + aktnum, rec_queue->msg, rec_queue->size);
1672 aktnum += rec_queue->size;
1673 rec_queue = rec_queue->next;
1675 free_rec_frag_queue(session);
1676 //call wlan_process_helper to process the message
1677 wlan_data_helper (plugin, session_light, (struct GNUNET_MessageHeader*) msg);
1684 * Function used for to process the data received from the wlan interface
1686 * @param cls the plugin handle
1687 * @param client client which send the data (not used)
1688 * @param hdr hdr of the GNUNET_MessageHeader
1691 wlan_data_helper(void *cls, void * client, const struct GNUNET_MessageHeader * hdr)
1693 struct Plugin *plugin = cls;
1694 struct Session * session = NULL;
1695 struct Session_light * session_light = NULL;
1697 struct WlanHeader * wlanheader = NULL;
1698 struct FragmentationHeader * fh = NULL;
1699 struct FragmentMessage * fm = NULL;
1701 const char * tempmsg = NULL;
1703 struct AckQueue * ack = NULL;
1704 struct AckQueue * ack2 = NULL;
1706 struct RecQueue * rec_queue = NULL;
1707 const struct GNUNET_MessageHeader * temp_hdr = NULL;
1709 if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT)
1714 GNUNET_ERROR_TYPE_DEBUG,
1715 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT size: %i\n",
1719 //TODO better DOS protection, error handling
1720 GNUNET_assert(client != NULL);
1721 session_light = (struct Session_light *) client;
1722 if (session_light->session == NULL)
1724 session_light->session = get_Session(plugin, session_light->addr);
1726 GNUNET_assert(GNUNET_HELLO_get_id(
1727 (const struct GNUNET_HELLO_Message *) &hdr[1],
1728 &(session_light->session->target) ) != GNUNET_SYSERR);
1733 else if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_DATA)
1737 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1738 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_WLAN_DATA size: %i\n",
1742 GNUNET_assert(client != NULL);
1743 session_light = (struct Session_light *) client;
1744 if (session_light->session == NULL)
1746 session_light->session = search_session(plugin, session_light->addr);
1748 session = session_light->session;
1749 wlanheader = (struct WlanHeader *) &hdr[1];
1750 tempmsg = (char*) &wlanheader[1];
1751 temp_hdr = (const struct GNUNET_MessageHeader *) &wlanheader[1];
1753 if (getcrc32(tempmsg, wlanheader->header.size) != wlanheader->crc)
1755 //wrong crc, dispose message
1756 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "WLAN message crc was wrong\n");
1760 //if not in session list
1761 if (session == NULL)
1764 //try if it is a hello message
1765 if (ntohs(temp_hdr->type) == GNUNET_MESSAGE_TYPE_HELLO)
1767 session = create_session(plugin, session_light->addr);
1768 session_light->session = session;
1769 GNUNET_assert(GNUNET_HELLO_get_id(
1770 (const struct GNUNET_HELLO_Message *) temp_hdr,
1771 &session->target ) != GNUNET_SYSERR);
1776 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1777 "WLAN client not in session list and not a hello message\n");
1781 //"receive" the message
1782 struct GNUNET_TRANSPORT_ATS_Information distance[2];
1783 distance[0].type = htonl(GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
1784 distance[0].value = htonl(1);
1785 distance[1].type = htonl(GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1786 distance[1].value = htonl(0);
1787 plugin->env->receive(plugin, &session->target, temp_hdr,
1788 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance, 2,
1789 session, session->addr, sizeof(session->addr));
1792 else if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT)
1797 GNUNET_ERROR_TYPE_DEBUG,
1798 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT size: %i\n",
1802 GNUNET_assert(client != NULL);
1803 session_light = (struct Session_light *) client;
1804 if (session_light->session == NULL)
1806 session_light->session = search_session(plugin, session_light->addr);
1808 session = session_light->session;
1810 fh = (struct FragmentationHeader *) hdr;
1811 tempmsg = (char*) &fh[1];
1813 //if not in session list
1814 if (session != NULL)
1816 if (getcrc16(tempmsg, fh->header.size) != fh->message_crc)
1818 //wrong crc, dispose message
1819 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1820 "WLAN fragment crc was wrong\n");
1825 //todo fragments do not timeout
1826 //check if message_id is rigth or it is a new msg
1827 if ((session->message_id_in == ntohs(fh->message_id))
1828 || (session->rec_size == NO_MESSAGE_OR_MESSAGE_FINISHED))
1830 session->message_id_in = ntohs(fh->message_id);
1831 if (is_double_msg(session, fh) != GNUNET_YES)
1834 = GNUNET_malloc(sizeof (struct RecQueue) +
1835 ntohs(fh->header.size) - sizeof(struct FragmentationHeader));
1836 rec_queue->size = ntohs(fh->header.size
1837 - sizeof(struct FragmentationHeader));
1838 rec_queue->num = ntohs(fh->fragment_off_or_num);
1839 rec_queue->msg = (char*) &rec_queue[1];
1840 //copy msg to buffer
1841 memcpy((char*) rec_queue->msg, tempmsg, rec_queue->size);
1842 insert_fragment_in_queue(session, rec_queue);
1843 check_rec_finished_msg(plugin, session_light, session);
1847 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1848 "WLAN fragment is a clone\n");
1855 GNUNET_ERROR_TYPE_INFO,
1856 "WLAN fragment message_id and session message_id are not the same and a message is already (partly) received\n");
1863 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1864 "WLAN client not in session list and it is a fragment message\n");
1870 else if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT_ACK)
1875 GNUNET_ERROR_TYPE_DEBUG,
1876 "Func wlan_data_helper got GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT_ACK size: %i\n",
1880 GNUNET_assert(client != NULL);
1881 session_light = (struct Session_light *) client;
1882 if (session_light->session == NULL)
1884 session_light->session = search_session(plugin, session_light->addr);
1885 GNUNET_assert(session_light->session != NULL);
1887 session = session_light->session;
1888 fh = (struct FragmentationHeader *) &hdr[1];
1889 if (fh->message_id == session->message_id_out)
1891 fm = get_fragment_message_from_session(session);
1896 while (ack2 != NULL)
1899 if (ack2->fragment_num != fh->fragment_off_or_num)
1901 // check if next ack has bigger number
1902 if (ack2->fragment_num > fh->fragment_off_or_num)
1904 ack = GNUNET_malloc(sizeof(struct AckQueue));
1905 ack->fragment_num = fh->fragment_off_or_num;
1906 GNUNET_CONTAINER_DLL_insert_before(fm->head,fm->tail,ack2,ack);
1908 check_finished_fragment(plugin, fm);
1919 //GNUNET_CONTAINER_DLL_insert_tail(fm->head,fm->tail,ack);
1920 //should never happen but...
1921 //check_finished_fragment(plugin, fm);
1925 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1926 "WLAN fragment not in fragment list but id is right\n");
1936 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1937 "WLAN packet inside the WLAN helper packet has not the right type\n");
1944 * Function used for to process the data from the suid process
1946 * @param cls the plugin handle
1947 * @param client which send the data (not used)
1948 * @param hdr of the GNUNET_MessageHeader
1952 wlan_process_helper (void *cls,
1954 const struct GNUNET_MessageHeader *hdr)
1956 struct Plugin *plugin = cls;
1957 struct IeeeHeader * wlanIeeeHeader = NULL;
1958 struct Session_light * session_light = NULL;
1959 const struct GNUNET_MessageHeader * temp_hdr = NULL;
1964 if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA)
1968 GNUNET_ERROR_TYPE_DEBUG,
1969 "Func wlan_process_helper got GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA size: %i\n",
1973 //call wlan_process_helper with the message inside, later with wlan: analyze signal
1974 GNUNET_assert(ntohs(hdr->size) >= sizeof(struct IeeeHeader));
1975 wlanIeeeHeader = (struct IeeeHeader *) &hdr[1];
1977 session_light = GNUNET_malloc(sizeof(struct Session_light));
1978 memcpy(session_light->addr, &(wlanIeeeHeader->mac3), sizeof(struct MacAddress));
1979 session_light->session = search_session(plugin, session_light->addr);
1981 //process only if it is an broadcast or for this computer both with the gnunet bssid
1983 if (memcmp(&(wlanIeeeHeader->mac2), macbc, sizeof(struct MacAddress)))
1985 //check for broadcast or mac
1986 if (memcmp(&(wlanIeeeHeader->mac1), bc_all_mac, sizeof(struct MacAddress))
1987 || memcmp(&(wlanIeeeHeader->mac1), &(plugin->mac_address),
1988 sizeof(struct MacAddress)))
1990 // process the inner data
1992 temp_hdr = (struct GNUNET_MessageHeader *) &wlanIeeeHeader[1];
1993 while (pos < hdr->size)
1995 wlan_data_helper(plugin, &session_light, temp_hdr);
1996 pos += temp_hdr->size + sizeof(struct GNUNET_MessageHeader);
2002 GNUNET_free(session_light);
2008 else if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL)
2014 GNUNET_ERROR_TYPE_DEBUG,
2015 "Func wlan_process_helper got GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL size: %i\n",
2020 //TODO use struct wlan_helper_control
2021 if (ntohs(hdr->size) == sizeof(struct Wlan_Helper_Control_Message))
2023 //plugin->mac_address = GNUNET_malloc(sizeof(struct MacAddress));
2024 memcpy(&(plugin->mac_address), &hdr[1], sizeof(struct MacAddress));
2026 GNUNET_ERROR_TYPE_DEBUG,
2027 "Notifying transport of address %s\n",
2028 wlan_plugin_address_to_string(cls, &(plugin->mac_address), ntohs(hdr->size) - sizeof(struct GNUNET_MessageHeader)));
2029 plugin->env->notify_address(plugin->env->cls, "wlan",
2030 &plugin->mac_address, sizeof(struct MacAddress),
2031 GNUNET_TIME_UNIT_FOREVER_REL);
2035 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Wrong wlan mac address %s\n",
2036 plugin->mac_address);
2045 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "WLAN helper packet has not the right type\n");
2051 * We have been notified that wlan-helper has written something to stdout.
2052 * Handle the output, then reschedule this function to be called again once
2053 * more is available.
2055 * @param cls the plugin handle
2056 * @param tc the scheduling context
2060 wlan_plugin_helper_read (void *cls,
2061 const struct GNUNET_SCHEDULER_TaskContext *tc)
2063 struct Plugin *plugin = cls;
2064 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2068 "Start reading from STDIN\n");
2071 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2074 char mybuf[WLAN_MTU + sizeof(struct GNUNET_MessageHeader)];
2077 bytes = GNUNET_DISK_file_read (plugin->server_stdout_handle,
2078 mybuf, sizeof(mybuf));
2082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2083 _("Finished reading from wlan-helper stdout with code: %d\n"), bytes);
2087 GNUNET_SERVER_mst_receive(plugin->consoltoken, NULL,
2088 mybuf, bytes, GNUNET_NO, GNUNET_NO);
2090 GNUNET_assert(plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2091 plugin->server_read_task =
2092 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2093 plugin->server_stdout_handle, &wlan_plugin_helper_read, plugin);
2098 * Start the gnunet-wlan-helper process.
2100 * @param plugin the transport plugin
2101 * @param testmode should we use the dummy driver for testing?
2102 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
2105 wlan_transport_start_wlan_helper(struct Plugin *plugin, int testmode)
2107 const char * filename = "gnunet-transport-wlan-helper";
2108 plugin->server_stdout = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_NO, GNUNET_YES);
2109 if (plugin->server_stdout == NULL)
2110 return GNUNET_SYSERR;
2112 plugin->server_stdin = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_NO);
2113 if (plugin->server_stdin == NULL)
2114 return GNUNET_SYSERR;
2117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2118 "Starting gnunet-wlan-helper process cmd: %s %s %i\n", filename, plugin->interface, testmode);
2120 /* Start the server process */
2123 plugin->server_proc = GNUNET_OS_start_process(plugin->server_stdin,
2124 plugin->server_stdout, filename,filename, plugin->interface, ((testmode==1)?"1":(testmode==2)?"2":"0"), NULL);
2125 if (plugin->server_proc == NULL)
2128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2129 "Failed to start gnunet-wlan-helper process\n");
2131 return GNUNET_SYSERR;
2134 /* Close the write end of the read pipe */
2135 GNUNET_DISK_pipe_close_end(plugin->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
2137 /* Close the read end of the write pipe */
2138 GNUNET_DISK_pipe_close_end(plugin->server_stdin, GNUNET_DISK_PIPE_END_READ);
2140 plugin->server_stdout_handle = GNUNET_DISK_pipe_handle(plugin->server_stdout,
2141 GNUNET_DISK_PIPE_END_READ);
2142 plugin->server_stdin_handle = GNUNET_DISK_pipe_handle(plugin->server_stdin,
2143 GNUNET_DISK_PIPE_END_WRITE);
2145 GNUNET_assert(plugin->server_read_task == GNUNET_SCHEDULER_NO_TASK);
2148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2149 "Adding server_read_task for the wlan-helper\n");
2152 plugin->server_read_task = GNUNET_SCHEDULER_add_read_file(
2153 GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdout_handle,
2154 &wlan_plugin_helper_read, plugin);
2161 * Exit point from the plugin.
2166 libgnunet_plugin_transport_wlan_done (void *cls)
2168 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2169 struct Plugin *plugin = api->cls;
2172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2173 "libgnunet_plugin_transport_wlan_done started\n");
2176 GNUNET_assert(cls !=NULL);
2178 if (plugin->consoltoken != NULL)
2179 GNUNET_SERVER_mst_destroy(plugin->consoltoken);
2181 GNUNET_free_non_null(plugin->interface);
2182 GNUNET_free (plugin);
2188 * Entry point for the plugin.
2190 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2191 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2194 libgnunet_plugin_transport_wlan_init (void *cls)
2196 //struct GNUNET_SERVICE_Context *service;
2197 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2198 struct GNUNET_TRANSPORT_PluginFunctions *api;
2199 struct Plugin *plugin;
2200 static unsigned long long testmode =0;
2202 GNUNET_assert(cls !=NULL);
2204 plugin = GNUNET_malloc (sizeof (struct Plugin));
2206 plugin->pendingsessions = 0;
2207 plugin->session_count = 0;
2208 plugin->server_write_task = GNUNET_SCHEDULER_NO_TASK;
2209 plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK;
2210 plugin->server_write_delay_task = GNUNET_SCHEDULER_NO_TASK;
2213 if (GNUNET_CONFIGURATION_have_value(env->cfg, "transport-wlan", "TESTMODE"))
2215 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(env->cfg, "transport-wlan",
2216 "TESTMODE", &testmode))
2220 if (GNUNET_CONFIGURATION_have_value(env->cfg,
2221 "transport-wlan", "INTERFACE"))
2223 if (GNUNET_CONFIGURATION_get_value_string (env->cfg,
2224 "transport-wlan","INTERFACE", &(plugin->interface)) != GNUNET_YES){
2225 libgnunet_plugin_transport_wlan_done(plugin);
2230 wlan_transport_start_wlan_helper(plugin, testmode);
2231 plugin->consoltoken = GNUNET_SERVER_mst_create(&wlan_process_helper,plugin);
2233 //plugin->sessions = GNUNET_malloc (sizeof (struct Sessionqueue));
2234 //plugin->pending_Sessions = GNUNET_malloc (sizeof (struct Sessionqueue));
2236 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2238 api->send = &wlan_plugin_send;
2239 api->disconnect = &wlan_plugin_disconnect;
2240 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
2241 api->check_address = &wlan_plugin_address_suggested;
2242 api->address_to_string = &wlan_plugin_address_to_string;
2245 start_next_message_id();
2248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2249 "wlan init finished\n");
2255 /* end of plugin_transport_wlan.c */