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 "plugin_transport.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
49 #define FRAGMENT_TIMEOUT 1000
52 #define DEBUG_wlan GNUNET_NO
55 * After how long do we expire an address that we
56 * learned from another peer if it is not reconfirmed
59 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
62 * Initial handshake message for a session.
67 * Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME.
69 struct GNUNET_MessageHeader header;
72 * Identity of the node connecting (TCP client)
74 struct GNUNET_PeerIdentity clientIdentity;
79 * Encapsulation of all of the state of the plugin.
86 struct GNUNET_TRANSPORT_PluginEnvironment *env;
89 * List of open sessions. head
91 struct Sessionqueue *sessions;
94 * List of open sessions. tail
96 struct Sessionqueue *sessions_tail;
99 * encapsulation to the local wlan server prog
102 struct GNUNET_SERVER_MessageStreamTokenizer * consoltoken;
106 * stdout pipe handle for the gnunet-wlan-helper process
108 struct GNUNET_DISK_PipeHandle *server_stdout;
111 * stdout file handle for the gnunet-wlan-helper process
113 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
116 * stdin pipe handle for the gnunet-wlan-helper process
118 struct GNUNET_DISK_PipeHandle *server_stdin;
121 * stdin file handle for the gnunet-wlan-helper process
123 const struct GNUNET_DISK_FileHandle *server_stdin_handle;
126 * ID of select gnunet-nat-server std read task
128 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
131 * ID of select gnunet-nat-server std read task
133 GNUNET_SCHEDULER_TaskIdentifier server_write_task;
136 * The process id of the server process (if behind NAT)
138 struct GNUNET_OS_Process *server_proc;
141 * The interface of the wlan card given to us by the user.
146 * The mac_address of the wlan card given to us by the helper.
151 * Sessions currently pending for transmission
152 * to this peer, if any.
154 struct Sessionqueue * pending_Sessions;
157 * Sessions currently pending for transmission
158 * to this peer (tail), if any.
160 struct Sessionqueue * pending_Sessions_tail;
163 * number of pending sessions
165 uint pendingsessions;
173 struct Sessionqueue * next;
174 struct Sessionqueue * prev;
175 struct Session * content;
182 struct FragmentQueue * next;
183 struct FragmentQueue * prev;
188 * Session handle for connections.
196 struct SessionHeader header;
199 * Pointer to the global plugin struct.
201 struct Plugin *plugin;
204 * Messages currently pending for transmission
205 * to this peer, if any.
207 struct PendingMessage *pending_messages_head;
210 * Messages currently pending for transmission
211 * to this peer, if any.
213 struct PendingMessage *pending_messages_tail;
216 * To whom are we talking to (set to our identity
217 * if we are still waiting for the welcome message)
219 struct GNUNET_PeerIdentity target;
222 * encapsulation of the data
224 //struct GNUNET_SERVER_MessageStreamTokenizer * datatoken;
232 * Address of the other peer (either based on our 'connect'
233 * call or on our 'accept' call).
238 * Last activity on this connection. Used to select preferred
241 struct GNUNET_TIME_Absolute last_activity;
244 * current number for message incoming , to distinguish between the messages
246 uint32_t message_id_in;
249 * current number for message outgoing, to distinguish between the messages
251 uint32_t message_id_out;
257 * Information kept for each message that is yet to
260 struct PendingMessage
264 * This is a doubly-linked list.
266 struct PendingMessage *next;
269 * This is a doubly-linked list.
271 struct PendingMessage *prev;
274 * The pending message
279 * Continuation function to call once the message
280 * has been sent. Can be NULL if there is no
281 * continuation to call.
283 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
286 * Cls for transmit_cont
288 void * transmit_cont_cls;
291 * Timeout value for the pending message.
293 struct GNUNET_TIME_Absolute timeout;
296 * Timeout value for the pending fragments.
297 * Stores the time when the last msg fragment ack was received
299 struct GNUNET_TIME_Absolute last_ack;
302 * Sorted queue with the acks received for fragments; head
305 struct FragmentQueue * head;
308 * Sorted queue with the acks received for fragments; tail
311 struct FragmentQueue * tail;
314 * Size of the message
319 * pos / next fragment number in the message, for fragmentation/segmentation,
320 * some acks can be missing but there is still time
322 uint32_t message_pos;
327 * Header for messages which need fragmentation
332 struct GNUNET_MessageHeader header;
335 * checksum/error correction
337 uint32_t crc GNUNET_PACKED;
340 * To whom are we talking to (set to our identity
341 * if we are still waiting for the welcome message)
343 struct GNUNET_PeerIdentity target;
345 // followed by payload
350 * Header for messages which need fragmentation
352 struct FragmentationHeader
355 struct GNUNET_MessageHeader header;
358 * To whom are we talking to (set to our identity
359 * if we are still waiting for the welcome message)
361 // struct GNUNET_PeerIdentity target GNUNET_PACKED;
364 * ID of message, to distinguish between the messages, picked randomly.
366 uint32_t message_id GNUNET_PACKED;
369 * Offset or number of this fragment, for fragmentation/segmentation (design choice, TBD)
371 uint16_t fragment_off_or_num GNUNET_PACKED;
374 * CRC of fragment (for error checking)
376 uint16_t message_crc GNUNET_PACKED;
380 * // 0x1 ack => Use two different message types in header.type! (FRAG_MESSAGE; FRAG_ACK)
381 * // 0x2 has data (not only ack)
382 * // 0x4 last fragment of message
385 // uint32_t flags GNUNET_PACKED;
388 * checksum/error correction
390 // uint32_t crc GNUNET_PACKED;
392 // followed by payload unless ACK
396 //enum { ACK_FRAGMENT = 1, DATA_FRAGMENT = 2, LAST_FRAGMENT = 4, NEW_MESSAGE = 8 };
398 int getRadiotapHeader (struct RadiotapHeader * Header);
399 int getWlanHeader (struct IeeeHeader * Header);
400 static int wlan_plugin_address_suggested (void *cls,
403 uint16_t getcrc16 (const char *msgbuf, size_t msgbuf_size);
406 * get the next message number, at the moment just a random one
411 get_next_message_id()
413 return GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
417 * start next message number generator
421 start_next_message_id()
423 //GNUNET_CRYPTO_random_init;
428 * get Session from address
432 //TODO add other possibilities to find the right session (are there other?)
433 static struct Session *
434 get_Session (struct Plugin *plugin,
437 struct Sessionqueue * queue = plugin->sessions;
438 struct Sessionqueue * lastitem = NULL;
441 //just look at all the session for the needed one
442 while (queue != NULL){
443 // content is never NULL
444 GNUNET_assert (queue->content == NULL);
445 char * addr2 = queue->content->addr;
446 if (memcmp(addr, addr2, 6) == 0)
449 return queue->content;
456 queue = GNUNET_malloc (sizeof (struct Sessionqueue));
458 GNUNET_CONTAINER_DLL_insert(plugin->sessions, plugin->sessions_tail, queue);
460 queue->content = GNUNET_malloc (sizeof (struct Session));
461 queue->content->plugin = plugin;
462 memcpy(queue->content->addr, addr, 6);
463 queue->content->message_id_out = get_next_message_id();
465 //queue welcome message for new sessions, not realy needed
466 //struct WelcomeMessage welcome;
467 struct PendingMessage *pm;
468 pm = GNUNET_malloc (sizeof (struct PendingMessage) + GNUNET_HELLO_size(* (plugin->env->our_hello)));
469 pm->msg = (const char*) &pm[1];
470 pm->message_size = GNUNET_HELLO_size(* (plugin->env->our_hello));
471 //welcome.header.size = htons (GNUNET_HELLO_size(* (plugin->env->our_hello)));
472 //welcome.header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT);
473 //welcome.clientIdentity = *plugin->env->my_identity;
474 memcpy (&pm[1], * plugin->env->our_hello, GNUNET_HELLO_size(* (plugin->env->our_hello)));
475 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
476 GNUNET_CONTAINER_DLL_insert ((queue->content)->pending_messages_head,
477 (queue->content)->pending_messages_tail,
479 plugin->pendingsessions ++;
480 GNUNET_CONTAINER_DLL_insert_after(plugin->pending_Sessions, plugin->pending_Sessions_tail, plugin->pending_Sessions_tail, queue);
481 return queue->content;
486 * Queue the session to send data
490 queue_Session (struct Plugin *plugin,
491 struct Session * session)
493 struct Sessionqueue * queue = plugin->pending_Sessions;
494 struct Sessionqueue * lastitem = NULL;
496 while (queue != NULL){
497 // content is never NULL
498 GNUNET_assert (queue->content == NULL);
499 // is session already in queue?
500 if (session == queue->content){
508 // Session is not in the queue
510 queue = GNUNET_malloc (sizeof (struct Sessionqueue));
511 queue->content = session;
514 GNUNET_CONTAINER_DLL_insert_after (plugin->pending_Sessions,
515 plugin->pending_Sessions_tail,
516 plugin->pending_Sessions_tail, queue);
517 plugin->pendingsessions ++;
523 free_acks (struct PendingMessage * pm){
524 struct FragmentQueue * fq;
525 while (pm->head != NULL){
527 GNUNET_CONTAINER_DLL_remove(pm->head, pm->tail, fq);
533 * Function called to when wlan helper is ready to get some data
536 * @param GNUNET_SCHEDULER_TaskContext
540 do_transmit (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
542 struct Plugin * plugin = cls;
545 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
548 struct Session * session;
549 struct Sessionqueue * queue;
550 struct PendingMessage * pm;
551 struct IeeeHeader * wlanheader;
552 struct RadiotapHeader * radioHeader;
553 struct GNUNET_MessageHeader * msgheader;
554 struct FragmentationHeader fragheader;
556 const char * copystart = NULL;
557 uint16_t copysize = 0;
559 struct FragmentQueue * akt = NULL;
564 struct GNUNET_TIME_Absolute nextsend;
565 struct GNUNET_TIME_Relative timeout;
566 struct Sessionqueue * nextsession = NULL;
568 timeout.rel_value = FRAGMENT_TIMEOUT;
569 nextsend = GNUNET_TIME_absolute_get_forever();
571 queue = plugin->pending_Sessions;
573 // check if the are some pending sessions/messages ...
574 GNUNET_assert(queue != NULL);
576 session = queue->content;
577 GNUNET_assert(session != NULL);
579 pm = session->pending_messages_head;
580 GNUNET_assert(pm != NULL);
582 // get next valid session
583 // check if this session is only waiting to receive the acks for an already send fragments to finish it
584 // timeout is not reached
585 for (i = 0; i < plugin->pendingsessions; i++){
587 // check if the are some pending sessions/messages ...
588 GNUNET_assert(queue != NULL);
590 session = queue->content;
591 GNUNET_assert(session != NULL);
593 pm = session->pending_messages_head;
594 GNUNET_assert(pm != NULL);
597 nextsession = queue->next;
598 // test if message timed out
599 while (GNUNET_TIME_absolute_get_remaining(pm->timeout).rel_value == 0){
603 //call the cont func that it did not work
604 if (pm->transmit_cont != NULL)
605 pm->transmit_cont (pm->transmit_cont_cls,
606 &(session->target), GNUNET_SYSERR);
608 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
609 session->pending_messages_tail,
613 //test if there are no more messages pending for this session
614 if (session->pending_messages_head == NULL){
616 //test if tail is null too
617 GNUNET_assert(session->pending_messages_tail == NULL);
619 plugin->pendingsessions --;
620 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions, plugin->pending_Sessions_tail, queue);
626 pm = session->pending_messages_head;
630 // restore next session if necessary
634 //there are no more messages in this session
639 // test if retransmit is needed
640 if (GNUNET_TIME_absolute_get_duration(pm->last_ack).rel_value < FRAGMENT_TIMEOUT) {
641 // get last offset for this message
642 copyoffset = pm->message_size /(WLAN_MTU - sizeof(struct FragmentationHeader));
643 // one more is the end
645 // test if it is not the end
646 if (copyoffset > pm->message_pos){
651 nextsend = GNUNET_TIME_absolute_min(GNUNET_TIME_absolute_add(pm->last_ack, timeout), nextsend);
653 GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions, plugin->pending_Sessions_tail, queue);
655 GNUNET_CONTAINER_DLL_insert_after (plugin->pending_Sessions,
656 plugin->pending_Sessions_tail,
657 plugin->pending_Sessions_tail, queue);
659 //get next pending session
670 //test if there is one session to send something
671 if (nextsession != NULL){
674 // check if the are some pending sessions/messages ...
675 GNUNET_assert(queue != NULL);
677 session = queue->content;
678 GNUNET_assert(session != NULL);
680 pm = session->pending_messages_head;
681 GNUNET_assert(pm != NULL);
683 //nothing to send at the moment
684 plugin->server_read_task =
685 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining(nextsend),
686 &do_transmit, plugin);
692 if (pm->message_size > WLAN_MTU) {
693 size += sizeof(struct FragmentationHeader);
694 // check for retransmission
695 if (GNUNET_TIME_absolute_get_duration(pm->last_ack).rel_value > FRAGMENT_TIMEOUT) {
697 // be positive and try again later :-D
698 pm->last_ack = GNUNET_TIME_absolute_get();
699 // find first missing fragment
704 //test if ack 0 was already received
706 //if fragment is present, take next
707 if (akt->fragment_num == pm->message_pos) {
710 //next ack is bigger then the fragment number
711 //in case there is something like this: (acks) 1, 2, 5, 6, ...
712 //and we send 3 again, the next number should be 4
713 if (akt->fragment_num > pm->message_pos) {
724 copyoffset = (WLAN_MTU - sizeof(struct FragmentationHeader)) * pm->message_pos;
725 fragheader.fragment_off_or_num = pm->message_pos;
726 fragheader.message_id = session->message_id_out;
728 // start should be smaller then the packet size
729 //TODO send some other data if everything was send but not all acks are present
730 GNUNET_assert(copyoffset < pm->message_size);
731 copystart = pm->msg + copyoffset;
733 //size of the fragment is either the MTU - overhead
734 //or the missing part of the message in case this is the last fragment
735 copysize = GNUNET_MIN(pm->message_size - copyoffset,
736 WLAN_MTU - sizeof(struct FragmentationHeader));
737 fragheader.header.size = copysize;
738 fragheader.header.type = GNUNET_MESSAGE_TYPE_WLAN_FRAGMENT;
740 //get the next missing fragment
745 //test if ack was already received
747 //if fragment is present, take next
748 if (akt->fragment_num == pm->message_pos) {
751 //next ack is bigger then the fragment number
752 //in case there is something like this: (acks) 1, 2, 5, 6, ...
753 //and we send 3 again, the next number should be 4
754 if (akt->fragment_num > pm->message_pos) {
763 // there is no need to split
765 copysize = pm->message_size;
768 size += sizeof(struct RadiotapHeader) + sizeof(struct IeeeHeader)
769 + sizeof(struct GNUNET_MessageHeader);
770 msgheader = GNUNET_malloc(size);
771 msgheader->size = htons(size - sizeof(struct GNUNET_MessageHeader));
772 msgheader->type = GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA;
774 radioHeader = (struct RadiotapHeader*) &msgheader[1];
775 getRadiotapHeader(radioHeader);
777 wlanheader = (struct IeeeHeader *) &radioHeader[1];
778 getWlanHeader(wlanheader);
781 //could be faster if content is just send and not copyed before
782 //fragmentheader is needed
783 if (pm->message_size > WLAN_MTU){
784 fragheader.message_crc = getcrc16(copystart, copysize);
785 memcpy(&wlanheader[1],&fragheader, sizeof(struct FragmentationHeader));
786 memcpy(&wlanheader[1] + sizeof(struct FragmentationHeader),copystart,copysize);
788 memcpy(&wlanheader[1],copystart,copysize);
791 bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle, msgheader, size);
805 //plugin->server_read_task =
806 //GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
807 // plugin->server_stdout_handle, &wlan_plugin_helper_read, plugin);
814 * If we have pending messages, ask the server to
815 * transmit them (schedule the respective tasks, etc.)
817 * @param Plugin env to get everything needed
820 process_pending_messages (struct Plugin * plugin)
822 struct Sessionqueue * queue;
823 struct Session * session;
825 if (plugin->pending_Sessions == NULL)
828 queue = plugin->pending_Sessions;
829 //contet should not be empty
830 GNUNET_assert(queue->content != NULL);
832 session = queue->content;
833 //pending sessions should have some msg
834 GNUNET_assert(session->pending_messages_head != NULL);
836 // GNUNET_TIME_UNIT_FOREVER_REL is needed to clean up old msg
837 plugin->server_write_task
838 = GNUNET_SCHEDULER_add_write_file(GNUNET_TIME_UNIT_FOREVER_REL,
839 plugin->server_stdin_handle,
848 * @param msgbuf pointer tor the data
849 * @param msgbuf_size size of the data
851 * @return 32bit crc value
855 getcrc32 (const char *msgbuf,
864 * @param msgbuf pointer tor the data
865 * @param msgbuf_size size of the data
867 * @return 16bit crc value
871 getcrc16 (const char *msgbuf,
878 * Function that can be used by the transport service to transmit
879 * a message using the plugin.
882 * @param target who should receive this message
883 * @param priority how important is the message
884 * @param msgbuf the message to transmit
885 * @param msgbuf_size number of bytes in 'msgbuf'
886 * @param timeout when should we time out
887 * @param session which session must be used (or NULL for "any")
888 * @param addr the address to use (can be NULL if the plugin
889 * is "on its own" (i.e. re-use existing TCP connection))
890 * @param addrlen length of the address in bytes
891 * @param force_address GNUNET_YES if the plugin MUST use the given address,
892 * otherwise the plugin may use other addresses or
893 * existing connections (if available)
894 * @param cont continuation to call once the message has
895 * been transmitted (or if the transport is ready
896 * for the next transmission call; or if the
897 * peer disconnected...)
898 * @param cont_cls closure for cont
899 * @return number of bytes used (on the physical network, with overheads);
900 * -1 on hard errors (i.e. address invalid); 0 is a legal value
901 * and does NOT mean that the message was not transmitted (DV)
904 wlan_plugin_send (void *cls,
905 const struct GNUNET_PeerIdentity * target,
908 unsigned int priority,
909 struct GNUNET_TIME_Relative timeout,
910 struct Session *session,
914 GNUNET_TRANSPORT_TransmitContinuation cont,
917 struct Plugin * plugin = cls;
918 struct PendingMessage * newmsg = NULL;
919 struct WlanHeader * wlanheader = NULL;
920 //check if msglen > 0
921 GNUNET_assert(msgbuf_size > 0);
923 //get session if needed
924 if (session == NULL) {
925 if ( wlan_plugin_address_suggested(plugin , addr, addrlen) == GNUNET_OK){
926 session = get_Session(plugin, addr);
928 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
929 _("Wlan Address len %d is wrong\n"),
935 //TODO target "problem" not solved
936 session->target = *target;
939 //first queue session
940 queue_Session(plugin, session);
942 //queue message in session
943 newmsg = GNUNET_malloc(sizeof(struct PendingMessage) + msgbuf_size + sizeof(struct WlanHeader));
944 newmsg->msg = (const char*) &newmsg[1];
945 wlanheader = (struct WlanHeader *) &newmsg[1];
946 //copy msg to buffer, not fragmented / segmented yet, but with message header
947 wlanheader->header.size = msgbuf_size;
948 wlanheader->header.type = GNUNET_MESSAGE_TYPE_WLAN_DATA;
949 wlanheader->target = *target;
950 wlanheader->crc = getcrc32(msgbuf, msgbuf_size);
951 memcpy(&wlanheader[1], msgbuf, msgbuf_size);
952 newmsg->transmit_cont = cont;
953 newmsg->transmit_cont_cls = cont_cls;
954 newmsg->timeout = GNUNET_TIME_relative_to_absolute(timeout);
955 newmsg->message_pos = 0;
956 newmsg->message_size = msgbuf_size + sizeof(struct WlanHeader);
959 //check if queue is empty
960 struct PendingMessage * tailmsg;
961 tailmsg = session->pending_messages_tail;
963 //new tail is the new msg
964 session->pending_messages_tail = newmsg;
965 newmsg->prev = tailmsg;
967 //test if tail was not NULL (queue is empty)
968 if (tailmsg == NULL){
969 // head should be NULL too
970 GNUNET_assert(session->pending_messages_head == NULL);
972 session->pending_messages_head = newmsg;
975 //next at the tail should be NULL
976 GNUNET_assert(tailmsg->next == NULL);
979 tailmsg->next = newmsg;
982 process_pending_messages(plugin);
985 //FIXME not the correct size
993 * Function that can be used to force the plugin to disconnect
994 * from the given peer and cancel all previous transmissions
995 * (and their continuation).
998 * @param target peer from which to disconnect
1001 wlan_plugin_disconnect (void *cls,
1002 const struct GNUNET_PeerIdentity *target)
1004 struct Plugin *plugin = cls;
1005 struct Sessionqueue * queue = plugin->sessions;
1006 struct Sessionqueue * lastitem = NULL;
1007 struct PendingMessage * pm;
1009 // just look at all the session for the needed one
1010 while (queue != NULL){
1011 // content is never NULL
1012 GNUNET_assert (queue->content == NULL);
1013 if (memcmp(target, &(queue->content->target), sizeof(struct GNUNET_PeerIdentity)) == 0)
1016 // remove PendingMessage
1017 while (queue->content->pending_messages_head != NULL){
1018 pm = queue->content->pending_messages_head;
1020 GNUNET_CONTAINER_DLL_remove(queue->content->pending_messages_head,queue->content->pending_messages_tail, pm);
1025 GNUNET_free(queue->content);
1026 GNUNET_CONTAINER_DLL_remove(plugin->sessions, plugin->sessions_tail, queue);
1033 queue = queue->next;
1039 * Convert the transports address to a nice, human-readable
1042 * @param cls closure
1043 * @param type name of the transport that generated the address
1044 * @param addr one of the addresses of the host, NULL for the last address
1045 * the specific address format depends on the transport
1046 * @param addrlen length of the address
1047 * @param numeric should (IP) addresses be displayed in numeric form?
1048 * @param timeout after how long should we give up?
1049 * @param asc function to call on each string
1050 * @param asc_cls closure for asc
1053 wlan_plugin_address_pretty_printer (void *cls,
1058 struct GNUNET_TIME_Relative timeout,
1059 GNUNET_TRANSPORT_AddressStringCallback
1063 const unsigned char * input;
1065 GNUNET_assert(cls !=NULL);
1068 /* invalid address (MAC addresses have 6 bytes) */
1070 asc (asc_cls, NULL);
1073 input = (const unsigned char*) addr;
1074 GNUNET_snprintf (ret,
1076 "%s Mac-Adress %X:%X:%X:%X:%X:%X",
1078 input[0], input[1], input[2], input[3], input[4], input[5]);
1085 * Another peer has suggested an address for this
1086 * peer and transport plugin. Check that this could be a valid
1087 * address. If so, consider adding it to the list
1090 * @param cls closure
1091 * @param addr pointer to the address
1092 * @param addrlen length of addr
1093 * @return GNUNET_OK if this is a plausible address for this peer
1099 wlan_plugin_address_suggested (void *cls,
1103 //struct Plugin *plugin = cls;
1105 /* check if the address is plausible; if so,
1106 add it to our list! */
1108 GNUNET_assert(cls !=NULL);
1109 //FIXME mitm is not checked
1110 //Mac Adress has 6 bytes
1112 /* TODO check for bad addresses like milticast, broadcast, etc */
1115 return GNUNET_SYSERR;
1118 return GNUNET_SYSERR;
1123 * Function called for a quick conversion of the binary address to
1124 * a numeric address. Note that the caller must not free the
1125 * address and that the next call to this function is allowed
1126 * to override the address again.
1128 * @param cls closure
1129 * @param addr binary address
1130 * @param addrlen length of the address
1131 * @return string representing the same address
1134 wlan_plugin_address_to_string (void *cls,
1139 const unsigned char * input;
1141 GNUNET_assert(cls !=NULL);
1144 /* invalid address (MAC addresses have 6 bytes) */
1148 input = (const unsigned char*) addr;
1149 GNUNET_snprintf (ret,
1151 "%s Mac-Adress %X:%X:%X:%X:%X:%X",
1153 input[0], input[1], input[2], input[3], input[4], input[5]);
1154 return GNUNET_strdup (ret);
1160 * Function used for to process the data from the suid process
1165 wlan_process_helper (void *cls,
1167 const struct GNUNET_MessageHeader *hdr)
1169 struct Plugin *plugin = cls;
1170 if (hdr->type == GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA){
1172 } else if (hdr->type == GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT){
1174 } else if (hdr->type == GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL){
1176 if (hdr->size == 6){
1177 plugin->mac_address = GNUNET_malloc(6);
1178 memcpy(plugin->mac_address, &hdr[1],6);
1179 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Notifying transport of address %s\n", wlan_plugin_address_to_string(cls, plugin->mac_address, hdr->size));
1180 plugin->env->notify_address (plugin->env->cls,
1182 &plugin->mac_address, sizeof(plugin->mac_address),
1183 GNUNET_TIME_UNIT_FOREVER_REL);
1185 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Wrong wlan mac address %s\n", plugin->mac_address);
1196 wlan_plugin_helper_read (void *cls,
1197 const struct GNUNET_SCHEDULER_TaskContext *tc)
1199 struct Plugin *plugin = cls;
1200 char mybuf[WLAN_MTU];
1203 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1205 bytes = GNUNET_DISK_file_read (plugin->server_stdout_handle,
1206 mybuf, sizeof(mybuf));
1210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1211 _("Finished reading from wlan-helper stdout with code: %d\n"), bytes);
1215 GNUNET_SERVER_mst_receive(plugin->consoltoken, NULL,
1216 mybuf, bytes, 0, GNUNET_NO);
1222 * Start the gnunet-wlan-helper process.
1224 * @param plugin the transport plugin
1226 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
1229 wlan_transport_start_wlan_helper (struct Plugin *plugin)
1232 plugin->server_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
1233 if (plugin->server_stdout == NULL)
1234 return GNUNET_SYSERR;
1236 plugin->server_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);
1237 if (plugin->server_stdin == NULL)
1238 return GNUNET_SYSERR;
1241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1242 "Starting gnunet-wlan-helper process cmd: %s %s\n", "gnunet-wlan-helper", plugin->interface);
1244 /* Start the server process */
1245 plugin->server_proc = GNUNET_OS_start_process(plugin->server_stdin, plugin->server_stdout, "gnunet-transport-wlan-helper", "gnunet-transport-wlan-helper", plugin->interface, NULL);
1246 if (plugin->server_proc == NULL)
1249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1250 "Failed to start gnunet-wlan-helper process\n");
1252 return GNUNET_SYSERR;
1254 /* Close the write end of the read pipe */
1255 GNUNET_DISK_pipe_close_end(plugin->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
1257 /* Close the read end of the write pipe */
1258 GNUNET_DISK_pipe_close_end(plugin->server_stdin, GNUNET_DISK_PIPE_END_READ);
1260 plugin->server_stdout_handle = GNUNET_DISK_pipe_handle(plugin->server_stdout, GNUNET_DISK_PIPE_END_READ);
1261 plugin->server_stdin_handle = GNUNET_DISK_pipe_handle(plugin->server_stdin, GNUNET_DISK_PIPE_END_WRITE);
1263 plugin->server_read_task =
1264 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1265 plugin->server_stdout_handle, &wlan_plugin_helper_read, plugin);
1272 * Entry point for the plugin.
1274 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
1275 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
1278 gnunet_plugin_transport_wlan_init (void *cls)
1280 struct GNUNET_SERVICE_Context *service;
1281 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1282 struct GNUNET_TRANSPORT_PluginFunctions *api;
1283 struct Plugin *plugin;
1285 GNUNET_assert(cls !=NULL);
1287 service = GNUNET_SERVICE_start ("transport-wlan", env->cfg);
1288 if (service == NULL){
1289 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1290 _("Failed to start service for `%s' transport plugin.\n"),
1295 plugin = GNUNET_malloc (sizeof (struct Plugin));
1297 plugin->pendingsessions = 0;
1299 wlan_transport_start_wlan_helper(plugin);
1300 plugin->consoltoken = GNUNET_SERVER_mst_create(&wlan_process_helper,plugin);
1302 //plugin->sessions = GNUNET_malloc (sizeof (struct Sessionqueue));
1303 //plugin->pending_Sessions = GNUNET_malloc (sizeof (struct Sessionqueue));
1305 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1307 api->send = &wlan_plugin_send;
1308 api->disconnect = &wlan_plugin_disconnect;
1309 api->address_pretty_printer = &wlan_plugin_address_pretty_printer;
1310 api->check_address = &wlan_plugin_address_suggested;
1311 api->address_to_string = &wlan_plugin_address_to_string;
1313 start_next_message_id();
1320 * Exit point from the plugin.
1324 gnunet_plugin_transport_wlan_done (void *cls)
1326 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1327 struct Plugin *plugin = api->cls;
1329 GNUNET_assert(cls !=NULL);
1331 GNUNET_free_non_null(plugin->mac_address);
1332 GNUNET_free (plugin);
1337 /* end of plugin_transport_wlan.c */