From: David Brodski Date: Mon, 23 May 2011 23:14:47 +0000 (+0000) Subject: Some bugfixes, udp packets can now be send over wlan, unrel test added, clean up X-Git-Tag: initial-import-from-subversion-38251~18437 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=5cdc9eb4091dfbfd1d65c4aaf1e3dfdd4b5b6316;p=oweals%2Fgnunet.git Some bugfixes, udp packets can now be send over wlan, unrel test added, clean up --- diff --git a/src/transport/Makefile.am b/src/transport/Makefile.am index 4cca96cd3..e4a266871 100644 --- a/src/transport/Makefile.am +++ b/src/transport/Makefile.am @@ -34,10 +34,12 @@ endif if HAVE_PCAP if LINUX - WANBIN = gnunet-transport-wlan-helper + WLAN_BIN = gnunet-transport-wlan-helper + WLAN_BIN_DUMMY = gnunet-transport-wlan-helper-dummy WLAN_PLUGIN_LA = libgnunet_plugin_transport_wlan.la WLAN_API_TEST = test_transport_api_wlan WLAN_REL_TEST = test_transport_api_reliability_wlan + WLAN_UREL_TEST = test_transport_api_unreliability_wlan endif endif @@ -78,8 +80,9 @@ libgnunettransport_la_LDFLAGS = \ bin_PROGRAMS = \ gnunet-transport \ - $(WANBIN) \ - gnunet-service-transport $(NATBIN) + $(WLAN_BIN) \ + gnunet-service-transport $(NATBIN) \ + $(WLAN_BIN_DUMMY) bin_SCRIPTS = \ gnunet-transport-certificate-creation @@ -91,13 +94,21 @@ gnunet_nat_server_SOURCES = \ gnunet_transport_wlan_helper_SOURCES = \ wlan/radiotap-parser.c \ wlan/helper_common.c \ - wlan/loopback_helper.c \ gnunet-transport-wlan-helper.c +gnunet_transport_wlan_helper_dummy_SOURCES = \ + wlan/radiotap-parser.c \ + wlan/helper_common.c \ + test_plugin_transport_wlan_dummy.c + gnunet_transport_wlan_helper_LDADD = \ $(top_builddir)/src/util/libgnunetutil.la \ -lpcap +gnunet_transport_wlan_helper_dummy_LDADD = \ + $(top_builddir)/src/util/libgnunetutil.la \ + -lpcap + gnunet_nat_client_SOURCES = \ $(NATCLIENT) @@ -238,7 +249,8 @@ check_PROGRAMS = \ $(HTTP_QUOTA_TEST) \ $(HTTPS_QUOTA_TEST) \ $(WLAN_API_TEST) \ - $(WLAN_REL_TEST) + $(WLAN_REL_TEST) \ + $(WLAN_UREL_TEST) # TODO: add tests for nat, etc. if !DISABLE_TEST_RUN @@ -268,9 +280,12 @@ TESTS = \ $(UNIX_QUOTA_TEST) \ $(HTTP_QUOTA_TEST) \ $(HTTPS_QUOTA_TEST) \ - $(WLAN_REL_TEST) + $(WLAN_UREL_TEST) endif +# $(WLAN_API_TEST) +# $(WLAN_REL_TEST) + test_transport_ats_SOURCES = \ test_transport_ats.c test_transport_ats_LDADD = -lm \ @@ -322,7 +337,13 @@ test_transport_api_reliability_wlan_LDADD = \ $(top_builddir)/src/transport/libgnunettransport.la \ $(top_builddir)/src/hello/libgnunethello.la \ $(top_builddir)/src/util/libgnunetutil.la - + +test_transport_api_unreliability_wlan_SOURCES = \ + test_transport_api_unreliability.c +test_transport_api_unreliability_wlan_LDADD = \ + $(top_builddir)/src/transport/libgnunettransport.la \ + $(top_builddir)/src/hello/libgnunethello.la \ + $(top_builddir)/src/util/libgnunetutil.la test_transport_api_udp_SOURCES = \ test_transport_api.c diff --git a/src/transport/gnunet-transport-wlan-helper.c b/src/transport/gnunet-transport-wlan-helper.c index 9759db532..4f2d66b13 100644 --- a/src/transport/gnunet-transport-wlan-helper.c +++ b/src/transport/gnunet-transport-wlan-helper.c @@ -378,7 +378,7 @@ openraw (struct Hardware_Infos *dev) if (-1 == ioctl(dev->fd_raw, SIOCGIFINDEX, &ifr)) { fprintf (stderr, - "ioctl(SIOCGIFINDEX) on interface `%.*s' failed: %s\n", + "Line: 381 ioctl(SIOCGIFINDEX) on interface `%.*s' failed: %s\n", IFNAMSIZ, dev->iface, strerror (errno)); @@ -428,10 +428,10 @@ openraw (struct Hardware_Infos *dev) /* Bring interface up*/ ifr.ifr_flags |= IFF_UP | IFF_BROADCAST | IFF_RUNNING; - if (-1 != ioctl(dev->fd_raw, SIOCSIFFLAGS, &ifr)) + if (-1 == ioctl(dev->fd_raw, SIOCSIFFLAGS, &ifr)) { fprintf (stderr, - "ioctl(SIOCSIFFLAGS) on interface `%.*s' failed: %s\n", + "Line: 434 ioctl(SIOCSIFFLAGS) on interface `%.*s' failed: %s\n", IFNAMSIZ, dev->iface, strerror (errno)); @@ -451,10 +451,10 @@ openraw (struct Hardware_Infos *dev) } /* lookup the hardware type */ - if (-1 != ioctl(dev->fd_raw, SIOCGIFHWADDR, &ifr)) + if (-1 == ioctl(dev->fd_raw, SIOCGIFHWADDR, &ifr)) { fprintf (stderr, - "ioctl(SIOCGIFHWADDR) on interface `%.*s' failed: %s\n", + "Line: 457 ioctl(SIOCGIFHWADDR) on interface `%.*s' failed: %s\n", IFNAMSIZ, dev->iface, strerror (errno)); @@ -588,6 +588,13 @@ mac_set (struct ieee80211_frame *u8aIeeeHeader, } +struct RadioTapheader +{ + struct ieee80211_radiotap_header header; + u8 rate; + u8 pad1; + u16 txflags; +}; static void stdin_send_hw (void *cls, @@ -601,14 +608,20 @@ stdin_send_hw (void *cls, size_t sendsize; // struct? // FIXME: make nice... - unsigned char u8aRadiotap[] = - { 0x00, 0x00, // <-- radiotap version - 0x0c, 0x00, // <- radiotap header length - 0x04, 0x80, 0x00, 0x00, // <-- bitmap - 0x00, // <-- rate - 0x00, // <-- padding for natural alignment - 0x18, 0x00, // <-- TX flags - }; + struct RadioTapheader rtheader; + rtheader.header.it_version = 0; + rtheader.header.it_len = htole16(0x0c); + rtheader.header.it_present = htole32(0x00008004); + rtheader.rate = 0x00; + rtheader.txflags = htole16(IEEE80211_RADIOTAP_F_TX_NOACK | IEEE80211_RADIOTAP_F_TX_NOSEQ); + + /* { 0x00, 0x00, <-- radiotap version + 0x0c, 0x00, <- radiotap header length + 0x04, 0x80, 0x00, 0x00, <-- bitmap + 0x00, <-- rate + 0x00, <-- padding for natural alignment + 0x18, 0x00, <-- TX flags + };*/ sendsize = ntohs(hdr->size); if (sendsize < sizeof(struct Radiotap_Send) + sizeof(struct GNUNET_MessageHeader)) @@ -631,15 +644,15 @@ stdin_send_hw (void *cls, exit(1); } - u8aRadiotap[2] = htole16(sizeof(u8aRadiotap)); // WTF? - u8aRadiotap[8] = header->rate; - memcpy(write_pout->buf, u8aRadiotap, sizeof(u8aRadiotap)); - memcpy(write_pout->buf + sizeof(u8aRadiotap), &header[1], sendsize); + rtheader.header.it_len = htole16(sizeof(rtheader)); + rtheader.rate = header->rate; + memcpy(write_pout->buf, &rtheader, sizeof(rtheader)); + memcpy(write_pout->buf + sizeof(rtheader), &header[1], sendsize); /* payload contains MAC address, but we don't trust it, so we'll overwrite it with OUR MAC address again to prevent mischief */ - wlanheader = (struct ieee80211_frame *) (write_pout->buf + sizeof(u8aRadiotap)); + wlanheader = (struct ieee80211_frame *) (write_pout->buf + sizeof(rtheader)); mac_set(wlanheader, dev); - write_pout->size = sendsize + sizeof(u8aRadiotap); + write_pout->size = sendsize + sizeof(rtheader); } #if 0 @@ -757,14 +770,6 @@ hardwaremode (int argc, int stdin_open; struct GNUNET_SERVER_MessageStreamTokenizer * stdin_mst; - if (2 != argc) - { - fprintf (stderr, - "This program must be started with the interface argument.\n"); - fprintf (stderr, - "Usage: interface-name\n"); - return 1; - } if (0 != wlaninit(&dev, argv[1])) return 1; uid = getuid(); @@ -930,19 +935,14 @@ hardwaremode (int argc, int main(int argc, char *argv[]) { - if (3 != argc) + if (2 != argc) { fprintf (stderr, - "This program must be started with the interface and the operating mode as argument.\n"); + "This program must be started with the interface as argument.\n"); fprintf (stderr, - "Usage: interface-name options\n" - "options: 0 = with hardware\n" - "1 = first loopback file\n" - "2 = second loopback file\n" + "Usage: interface-name\n" "\n"); return 1; } - if (strstr(argv[2], "1") || strstr(argv[2], "2")) - return testmode(argc, argv); - return hardwaremode(argc - 1, argv); + return hardwaremode(argc , argv); } diff --git a/src/transport/plugin_transport_wlan.c b/src/transport/plugin_transport_wlan.c index 26eda2b97..25af40b7c 100644 --- a/src/transport/plugin_transport_wlan.c +++ b/src/transport/plugin_transport_wlan.c @@ -34,7 +34,8 @@ #include "plugin_transport_wlan.h" #include "gnunet_common.h" #include "gnunet_crypto_lib.h" -#include "wlan/ieee80211.h" +//#include "wlan/ieee80211.h" +//#include #include @@ -43,7 +44,7 @@ /** * Max size of packet from helper */ -#define WLAN_MTU 3000 +#define WLAN_MTU 2100 /** * Time until retransmission of a fragment in ms @@ -60,8 +61,9 @@ #define HALLO_BEACON_SCALING_FACTOR 900 -#define DEBUG_wlan GNUNET_YES +#define DEBUG_wlan GNUNET_NO #define DEBUG_wlan_retransmission GNUNET_NO +#define DEBUG_wlan_ip_udp_packets_on_air GNUNET_NO #define MESSAGE_LENGHT_UNKNOWN -1 //#define NO_MESSAGE_OR_MESSAGE_FINISHED -2 @@ -73,6 +75,68 @@ */ #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6) +#define IEEE80211_ADDR_LEN 6 /* size of 802.11 address */ + +#define IEEE80211_FC0_VERSION_MASK 0x03 +#define IEEE80211_FC0_VERSION_SHIFT 0 +#define IEEE80211_FC0_VERSION_0 0x00 +#define IEEE80211_FC0_TYPE_MASK 0x0c +#define IEEE80211_FC0_TYPE_SHIFT 2 +#define IEEE80211_FC0_TYPE_MGT 0x00 +#define IEEE80211_FC0_TYPE_CTL 0x04 +#define IEEE80211_FC0_TYPE_DATA 0x08 + +/* + * Structure of an internet header, naked of options. + */ +struct iph + { +#if __BYTE_ORDER == __LITTLE_ENDIAN + unsigned int ip_hl:4; /* header length */ + unsigned int ip_v:4; /* version */ +#endif +#if __BYTE_ORDER == __BIG_ENDIAN + unsigned int ip_v:4; /* version */ + unsigned int ip_hl:4; /* header length */ +#endif + u_int8_t ip_tos; /* type of service */ + u_short ip_len; /* total length */ + u_short ip_id; /* identification */ + u_short ip_off; /* fragment offset field */ +#define IP_RF 0x8000 /* reserved fragment flag */ +#define IP_DF 0x4000 /* dont fragment flag */ +#define IP_MF 0x2000 /* more fragments flag */ +#define IP_OFFMASK 0x1fff /* mask for fragmenting bits */ + u_int8_t ip_ttl; /* time to live */ + u_int8_t ip_p; /* protocol */ + u_short ip_sum; /* checksum */ + struct in_addr ip_src, ip_dst; /* source and dest address */ + }; + +struct udphdr +{ + u_int16_t source; + u_int16_t dest; + u_int16_t len; + u_int16_t check; +}; + +/* + * generic definitions for IEEE 802.11 frames + */ +struct ieee80211_frame { + u_int8_t i_fc[2]; + u_int8_t i_dur[2]; + u_int8_t i_addr1[IEEE80211_ADDR_LEN]; + u_int8_t i_addr2[IEEE80211_ADDR_LEN]; + u_int8_t i_addr3[IEEE80211_ADDR_LEN]; + u_int8_t i_seq[2]; +#if DEBUG_wlan_ip_udp_packets_on_air + u_int8_t llc[4]; + struct iph ip; + struct udphdr udp; +#endif +} GNUNET_PACKED; /** * Initial handshake message for a session. */ @@ -582,6 +646,54 @@ struct FragmentationAckHeader static void do_transmit(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); +/** + * Generates a nice hexdump of a memory area. + * + * \param mem pointer to memory to dump + * \param length how many bytes to dump + */ +void hexdump(void *mem, unsigned length) +{ + char line[80]; + char *src = (char*)mem; + + printf( + "dumping %u bytes from %p\r\n" + " 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF\r\n" + , length, src + ); + unsigned i; + int j; + + for (i=0; iserver_write_delay_task - = GNUNET_SCHEDULER_add_delayed(next_send, - &delay_fragment_task, plugin); + plugin->server_write_delay_task = GNUNET_SCHEDULER_add_delayed(next_send, + &delay_fragment_task, plugin); } } @@ -919,7 +1030,6 @@ get_next_queue_session(struct Plugin * plugin) GNUNET_assert(session != NULL); pm = session->pending_message; - #if DEBUG_wlan if (pm == NULL) { @@ -1195,7 +1305,7 @@ getWlanHeader(struct ieee80211_frame * Header, uint16_t * tmp16; const int rate = 11000000; - Header->i_fc[0] = 0x08; + Header->i_fc[0] = IEEE80211_FC0_TYPE_DATA; Header->i_fc[1] = 0x00; memcpy(&Header->i_addr3, &mac_bssid, sizeof(mac_bssid)); memcpy(&Header->i_addr2, plugin->mac_address.mac, sizeof(plugin->mac_address)); @@ -1204,6 +1314,36 @@ getWlanHeader(struct ieee80211_frame * Header, tmp16 = (uint16_t*) Header->i_dur; *tmp16 = (uint16_t) htole16((size * 1000000) / rate + 290); +#if DEBUG_wlan_ip_udp_packets_on_air + uint crc = 0; + uint16_t * x; + int count; + Header->ip.ip_dst.s_addr = *((uint32_t*) &to_mac_addr->mac[2]); + Header->ip.ip_src.s_addr = *((uint32_t*) &plugin->mac_address.mac[2]); + Header->ip.ip_v = 4; + Header->ip.ip_hl = 5; + Header->ip.ip_p = 17; + Header->ip.ip_ttl = 1; + Header->ip.ip_len = htons(size + 8); + Header->ip.ip_sum = 0; + x =(uint16_t *) &Header->ip; + count = sizeof(struct iph); + while (count > 1) { + /* This is the inner loop */ + crc += (unsigned short) * x++; + count -= 2; + } + /* Add left-over byte, if any */ + if( count > 0 ) + crc += * (unsigned char *) x; + crc = (crc & 0xffff) + (crc >> 16); + Header->ip.ip_sum = htons(~ (unsigned short) crc); + Header->llc[0] = 6; + Header->llc[1] = 6; + Header->udp.len = htons(size - sizeof(struct ieee80211_frame)); + +#endif + return GNUNET_YES; } @@ -1219,8 +1359,8 @@ getWlanHeader(struct ieee80211_frame * Header, uint32_t getcrc32(const char *msgbuf, size_t msgbuf_size) { - //TODO calc some crc - return 0; + + return GNUNET_CRYPTO_crc32_n(msgbuf, msgbuf_size);; } /** @@ -1350,8 +1490,7 @@ send_ack(struct Plugin * plugin, struct AckSendQueue * ack) //TODO DOXIGEN static void -finish_sending(void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) +finish_sending(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { struct Finish_send * finish = cls; struct Plugin * plugin; @@ -1366,19 +1505,17 @@ finish_sending(void *cls, GNUNET_free (finish); return; } - bytes = GNUNET_DISK_file_write (plugin->server_stdin_handle, - finish->msgheader, - finish->size); + bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle, + finish->msgheader, finish->size); GNUNET_assert (bytes != GNUNET_SYSERR); if (bytes != finish->size) { finish->msgheader = finish->msgheader + bytes; finish->size = finish->size - bytes; - plugin->server_write_task - = GNUNET_SCHEDULER_add_write_file(GNUNET_TIME_UNIT_FOREVER_REL, - plugin->server_stdin_handle, - &finish_sending, finish); + plugin->server_write_task = GNUNET_SCHEDULER_add_write_file( + GNUNET_TIME_UNIT_FOREVER_REL, plugin->server_stdin_handle, + &finish_sending, finish); } else { @@ -1508,6 +1645,7 @@ do_transmit(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) ieeewlanheader = (struct ieee80211_frame *) &radioHeader[1]; getWlanHeader(ieeewlanheader, &(fm->session->addr), plugin, size); + //could be faster if content is just send and not copyed before //fragmentheader is needed fragheader.message_crc = htons(getcrc16(copystart, copysize)); @@ -1710,8 +1848,12 @@ wlan_plugin_send(void *cls, const struct GNUNET_PeerIdentity * target, wlanheader->header.size = htons(msgbuf_size + sizeof(struct WlanHeader)); wlanheader->header.type = htons(GNUNET_MESSAGE_TYPE_WLAN_DATA); memcpy(&(wlanheader->target), target, sizeof(struct GNUNET_PeerIdentity)); - wlanheader->crc = htonl(getcrc32(msgbuf, msgbuf_size)); + wlanheader->crc = 0; memcpy(&wlanheader[1], msgbuf, msgbuf_size); + wlanheader->crc = htonl(getcrc32((char*) wlanheader, msgbuf_size + sizeof(struct WlanHeader))); + //GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Wlan message Header crc: %u, %u\n",getcrc32((char*) wlanheader, msgbuf_size + sizeof(struct WlanHeader)), wlanheader->crc); + //hexdump(newmsg->msg, msgbuf_size + sizeof(struct WlanHeader)); + newmsg->transmit_cont = cont; newmsg->transmit_cont_cls = cont_cls; newmsg->timeout = GNUNET_TIME_relative_to_absolute(timeout); @@ -1813,6 +1955,7 @@ get_fragment_message_from_session_and_id(struct Plugin * plugin, struct Session_id_fragment_triple triple; triple.session = session; triple.message_id = message_id; + triple.fm = NULL; GNUNET_CONTAINER_heap_iterate(plugin->pending_Fragment_Messages, &search_fragment_message_from_session_and_id, &triple); return triple.fm; @@ -1914,8 +2057,8 @@ free_session(struct Plugin * plugin, struct Sessionqueue * queue) { plugin->pendingsessions--; GNUNET_CONTAINER_DLL_remove (plugin->pending_Sessions_head, - plugin->pending_Sessions_tail, - pendingsession); + plugin->pending_Sessions_tail, + pendingsession); GNUNET_free(pendingsession); GNUNET_assert (check == 0); @@ -1954,9 +2097,9 @@ free_session(struct Plugin * plugin, struct Sessionqueue * queue) GNUNET_free(pm); } - GNUNET_CONTAINER_DLL_remove(plugin->sessions, - plugin->sessions_tail, - queue); + GNUNET_CONTAINER_DLL_remove(plugin->sessions, + plugin->sessions_tail, + queue); GNUNET_free(queue->content); GNUNET_free(queue); plugin->session_count--; @@ -2031,7 +2174,6 @@ wlan_plugin_address_pretty_printer(void *cls, const char *type, asc(asc_cls, ret); } - /** * Function to test if fragment number already exists in the fragments received * @@ -2044,7 +2186,6 @@ is_double_msg(struct Receive_Message_Queue * rx_msg, struct FragmentationHeader * fh) { - return testBit((char *) &rx_msg->received_fragments, ntohs( fh->fragment_off_or_num)); @@ -2108,6 +2249,7 @@ wlan_data_message_handler(void *cls, void *client, const char * tempmsg; const struct GNUNET_MessageHeader * temp_hdr; struct GNUNET_PeerIdentity tmptarget; + int crc; if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_WLAN_DATA) { @@ -2136,13 +2278,14 @@ wlan_data_message_handler(void *cls, void *client, tempmsg = (char*) &wlanheader[1]; temp_hdr = (const struct GNUNET_MessageHeader *) &wlanheader[1]; - - if (getcrc32(tempmsg, ntohs(wlanheader->header.size)) != ntohl( - wlanheader->crc)) + crc = ntohl(wlanheader->crc); + wlanheader->crc = 0; + if (getcrc32((char *) wlanheader, ntohs(wlanheader->header.size)) != crc) { //wrong crc, dispose message GNUNET_log(GNUNET_ERROR_TYPE_INFO, - "Wlan message Header crc was wrong\n"); + "Wlan message Header crc was wrong: %u != %u\n",getcrc32((char *) wlanheader, ntohs(wlanheader->header.size)), crc); + hexdump((void *)hdr, ntohs(hdr->size)); return; } @@ -2877,7 +3020,8 @@ wlan_plugin_helper_read(void *cls, static int wlan_transport_start_wlan_helper(struct Plugin *plugin, int testmode) { - const char * filename = "gnunet-transport-wlan-helper"; + const char * filenamehw = "gnunet-transport-wlan-helper"; + const char * filenameloopback = "gnunet-transport-wlan-helper-dummy"; plugin->server_stdout = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_NO, GNUNET_YES); if (plugin->server_stdout == NULL) return GNUNET_SYSERR; @@ -2886,16 +3030,43 @@ wlan_transport_start_wlan_helper(struct Plugin *plugin, int testmode) if (plugin->server_stdin == NULL) return GNUNET_SYSERR; + + /* Start the server process */ + + if (testmode == 0) + { + #if DEBUG_wlan GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, - "Starting gnunet-wlan-helper process cmd: %s %s %i\n", filename, + "Starting gnunet-wlan-helper process cmd: %s %s %i\n", filenamehw, plugin->interface, testmode); #endif - /* Start the server process */ - plugin->server_proc = GNUNET_OS_start_process(plugin->server_stdin, - plugin->server_stdout, filename, filename, plugin->interface, ((testmode - == 1) ? "1" : (testmode == 2) ? "2" : "0"), NULL); + plugin->server_proc = GNUNET_OS_start_process(plugin->server_stdin, + plugin->server_stdout, filenamehw, filenamehw, plugin->interface, NULL); + } + else if (testmode == 1) + { + +#if DEBUG_wlan + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "Starting gnunet-wlan-helper loopback 1 process cmd: %s %s %i\n", filenameloopback, + plugin->interface, testmode); +#endif + + plugin->server_proc = GNUNET_OS_start_process(plugin->server_stdin, + plugin->server_stdout, filenameloopback, filenameloopback, "1", NULL); + } + else if (testmode == 2) + { +#if DEBUG_wlan + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "Starting gnunet-wlan-helper loopback 2 process cmd: %s %s %i\n", filenameloopback, + plugin->interface, testmode); +#endif + plugin->server_proc = GNUNET_OS_start_process(plugin->server_stdin, + plugin->server_stdout, filenameloopback, filenameloopback, "2", NULL); + } if (plugin->server_proc == NULL) { #if DEBUG_wlan @@ -2951,9 +3122,11 @@ libgnunet_plugin_transport_wlan_done(void *cls) #endif - GNUNET_OS_process_close(plugin->server_proc); GNUNET_DISK_pipe_close(plugin->server_stdout); GNUNET_DISK_pipe_close(plugin->server_stdin); + GNUNET_OS_process_kill(plugin->server_proc,9); + GNUNET_OS_process_close(plugin->server_proc); + GNUNET_assert (cls !=NULL); //free sessions @@ -2980,7 +3153,6 @@ libgnunet_plugin_transport_wlan_done(void *cls) plugin->server_read_task = GNUNET_SCHEDULER_NO_TASK; } - if (plugin->suid_tokenizer != NULL) GNUNET_SERVER_mst_destroy(plugin->suid_tokenizer); @@ -3051,7 +3223,6 @@ libgnunet_plugin_transport_wlan_init(void *cls) api->address_pretty_printer = &wlan_plugin_address_pretty_printer; api->check_address = &wlan_plugin_address_suggested; api->address_to_string = &wlan_plugin_address_to_string; - //read config if (GNUNET_CONFIGURATION_have_value(env->cfg, "transport-wlan", "TESTMODE")) diff --git a/src/transport/test_plugin_transport_wlan_dummy.c b/src/transport/test_plugin_transport_wlan_dummy.c index 79f87584d..21cc74aa2 100644 --- a/src/transport/test_plugin_transport_wlan_dummy.c +++ b/src/transport/test_plugin_transport_wlan_dummy.c @@ -19,241 +19,447 @@ */ /** * @file transport/test_transport_wlan_dummy.c - * @brief helper for the testcase for plugin_transport_wlan.c + * @brief helper for the testcases for plugin_transport_wlan.c * @author David Brodski */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + #include "platform.h" #include "gnunet_constants.h" #include "gnunet_os_lib.h" #include "gnunet_transport_plugin.h" #include "transport.h" +#include "gnunet_util_lib.h" #include "plugin_transport_wlan.h" #include "gnunet_common.h" #include "gnunet-transport-wlan-helper.h" +#include "gnunet_crypto_lib.h" +#include "wlan/loopback_helper.h" +#include "wlan/helper_common.h" -#include -#include -#include +int first; -#define FIFO_FILE1 "MYFIFOin" -#define FIFO_FILE2 "MYFIFOout" -#define MAXLINE 5000 - -int closeprog = 0; +static void +sigfunc(int sig) +{ + closeprog = 1; + unlink(FIFO_FILE1); + unlink(FIFO_FILE2); +} -void sigfunc(int sig) +static void +stdin_send(void *cls, void *client, const struct GNUNET_MessageHeader *hdr) { + struct sendbuf *write_pout = cls; + int sendsize; + struct GNUNET_MessageHeader newheader; + unsigned char * from_data; + unsigned char * to_data; + //unsigned char * from_radiotap; + unsigned char * to_radiotap; + //unsigned char * from_start; + unsigned char * to_start; + + sendsize = ntohs(hdr->size) - sizeof(struct Radiotap_Send) + + sizeof(struct Radiotap_rx); + + if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs(hdr->type)) + { + fprintf(stderr, "Function stdin_send: wrong packet type\n"); + exit(1); + } + if ((sendsize + write_pout->size) > MAXLINE * 2) + { + fprintf(stderr, "Function stdin_send: Packet too big for buffer\n"); + exit(1); + } - if(sig != SIGINT || sig != SIGTERM || sig != SIGKILL) - return; - else - { - closeprog = 1; - exit(0); - } + newheader.size = htons(sendsize); + newheader.type = htons(GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA); + + to_start = write_pout->buf + write_pout->size; + to_radiotap = to_start + sizeof(struct GNUNET_MessageHeader); + to_data = to_radiotap + sizeof(struct Radiotap_rx); + + from_data = ((unsigned char *) hdr) + sizeof(struct Radiotap_Send) + + sizeof(struct GNUNET_MessageHeader); + + memcpy(to_start, &newheader, sizeof(struct GNUNET_MessageHeader)); + write_pout->size += sizeof(struct GNUNET_MessageHeader); + + write_pout->size += sizeof(struct Radiotap_rx); + + memcpy(to_data, from_data, ntohs(hdr->size) - sizeof(struct Radiotap_Send) + - sizeof(struct GNUNET_MessageHeader)); + write_pout->size += ntohs(hdr->size) - sizeof(struct Radiotap_Send) + - sizeof(struct GNUNET_MessageHeader); } +static void +file_in_send(void *cls, void *client, const struct GNUNET_MessageHeader *hdr) +{ + struct sendbuf * write_std = cls; + uint16_t sendsize; + sendsize = ntohs(hdr->size); + + if (GNUNET_MESSAGE_TYPE_WLAN_HELPER_DATA != ntohs(hdr->type)) + { + fprintf(stderr, "Function file_in_send: wrong packet type\n"); + exit(1); + } + if ((sendsize + write_std->size) > MAXLINE * 2) + { + fprintf(stderr, "Function file_in_send: Packet too big for buffer\n"); + exit(1); + } + + memcpy(write_std->buf + write_std->size, hdr, sendsize); + write_std->size += sendsize; +} + +int closeprog; int -main(int argc, char *argv[]) +testmode(int argc, char *argv[]) { struct stat st; - struct stat st2; int erg; - int first; - FILE *fpin; - FILE *fpout; - pid_t pid; - perror("Test"); + FILE *fpin = NULL; + FILE *fpout = NULL; + int fdpin; + int fdpout; //make the fifos if needed - if (stat(FIFO_FILE1, &st) != 0) + if (0 != stat(FIFO_FILE1, &st)) { - if (stat(FIFO_FILE2, &st2) == 0) + if (0 == stat(FIFO_FILE2, &st)) { - perror("FIFO 2 exists, but FIFO 1 not, blub"); + fprintf(stderr, "FIFO_FILE2 exists, but FIFO_FILE1 not\n"); exit(1); } - first = 1; - perror("First"); + umask(0); - erg = mknod(FIFO_FILE1, S_IFIFO | 0666, 0); + //unlink(FIFO_FILE1); + //unlink(FIFO_FILE2); + // FIXME: use mkfifo! + erg = mknod(FIFO_FILE1, S_IFIFO | 0666, 0); + if (0 != erg) + { + fprintf(stderr, "Error at mknode1 \n"); + //exit(1); + } erg = mknod(FIFO_FILE2, S_IFIFO | 0666, 0); + if (0 != erg) + { + fprintf(stderr, "Error at mknode2 \n"); + //exit(1); + } - if ((fpin = fopen(FIFO_FILE1, "r")) == NULL) + } + else + { + + if (0 != stat(FIFO_FILE2, &st)) { - perror("fopen"); + fprintf(stderr, "FIFO_FILE1 exists, but FIFO_FILE2 not\n"); exit(1); } - if ((fpout = fopen(FIFO_FILE2, "w")) == NULL) + + } + + if (strstr(argv[1], "1")) + { + //fprintf(stderr, "First\n"); + first = 1; + fpin = fopen(FIFO_FILE1, "r"); + if (NULL == fpin) + { + fprintf(stderr, "fopen of read FIFO_FILE1\n"); + goto end; + } + fpout = fopen(FIFO_FILE2, "w"); + if (NULL == fpout) { - perror("fopen"); - exit(1); + fprintf(stderr, "fopen of write FIFO_FILE2\n"); + goto end; } + } else { first = 0; - perror("Second"); - if (stat(FIFO_FILE2, &st2) != 0) + //fprintf(stderr, "Second\n"); + fpout = fopen(FIFO_FILE1, "w"); + if (NULL == fpout) { - perror("FIFO 1 exists, but FIFO 2 not, mäh"); - exit(1); + fprintf(stderr, "fopen of write FIFO_FILE1\n"); + goto end; } - if ((fpout = fopen(FIFO_FILE1, "w")) == NULL) + fpin = fopen(FIFO_FILE2, "r"); + if (NULL == fpin) { - perror("fopen"); - exit(1); - } - if ((fpin = fopen(FIFO_FILE2, "r")) == NULL) - { - perror("fopen"); - exit(1); + fprintf(stderr, "fopen of read FIFO_FILE2\n"); + goto end; } } - // fork + fdpin = fileno(fpin); + GNUNET_assert(fpin >= 0); - if ((pid = fork()) < 0) + if (fdpin >= FD_SETSIZE) { - perror("FORK ERROR"); - - //clean up - if (first == 1) - { - unlink(FIFO_FILE1); - unlink(FIFO_FILE2); - } - fclose(fpin); - fclose(fpout); - return -3; + fprintf(stderr, "File fdpin number too large (%d > %u)\n", fdpin, + (unsigned int) FD_SETSIZE); + goto end; } - else if (pid == 0) // CHILD PROCESS + + fdpout = fileno(fpout); + GNUNET_assert(fdpout >= 0 ); + + if (fdpout >= FD_SETSIZE) { - perror("Child"); - signal(SIGINT, sigfunc); - signal(SIGTERM, sigfunc); - signal(SIGKILL, sigfunc); - int rv = 0; - int readc = 0; - int pos = 0; - char line[MAXLINE]; + fprintf(stderr, "File fdpout number too large (%d > %u)\n", fdpout, + (unsigned int) FD_SETSIZE); + goto end; - fd_set rfds; - fd_set wfds; - struct timeval tv; - int retval; + } + signal(SIGINT, &sigfunc); + signal(SIGTERM, &sigfunc); - tv.tv_sec = 5; - tv.tv_usec = 0; + char readbuf[MAXLINE]; + int readsize = 0; + struct sendbuf write_std; + write_std.size = 0; + write_std.pos = 0; + struct sendbuf write_pout; + write_pout.size = 0; + write_pout.pos = 0; - FD_ZERO(&rfds); - FD_SET(STDIN_FILENO, &rfds); + int ret = 0; + int maxfd = 0; - FD_ZERO(&wfds); - FD_SET(STDOUT_FILENO, &wfds); + fd_set rfds; + fd_set wfds; + struct timeval tv; + int retval; - struct GNUNET_SERVER_MessageStreamTokenizer * stdin_mst; - struct GNUNET_SERVER_MessageStreamTokenizer * file_in_mst; + struct GNUNET_SERVER_MessageStreamTokenizer * stdin_mst; + struct GNUNET_SERVER_MessageStreamTokenizer * file_in_mst; - stdin_mst = GNUNET_SERVER_mst_create(&stdin_send, NULL); - file_in_mst = GNUNET_SERVER_mst_create(&file_in_send, NULL); + stdin_mst = GNUNET_SERVER_mst_create(&stdin_send, &write_pout); + file_in_mst = GNUNET_SERVER_mst_create(&file_in_send, &write_std); - while (closeprog == 0) - { - readc = 0; + //send mac first + struct MacAddress macaddr; - while (readc < sizeof( struct RadiotapHeader) + sizeof(struct GNUNET_MessageHeader)){ - if ((rv = read(STDIN_FILENO, line, MAXLINE)) < 0) - { - perror("READ ERROR FROM STDIN"); - } - readc += rv; - } + //Send random mac address + macaddr.mac[0] = 0x13; + macaddr.mac[1] = 0x22; + macaddr.mac[2] = 0x33; + macaddr.mac[3] = 0x44; + macaddr.mac[4] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 256); + macaddr.mac[5] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, 256); - pos = 0; + write_std.size = send_mac_to_plugin((char *) write_std.buf, macaddr.mac); - //fwrite(&line[pos], 1, sizeof(struct GNUNET_MessageHeader), fpout); + while (0 == closeprog) + { - //pos += sizeof(struct GNUNET_MessageHeader); + maxfd = 0; - //do not send radiotap header - pos += sizeof( struct RadiotapHeader); + //set timeout + tv.tv_sec = 5; + tv.tv_usec = 0; - while (pos < readc) - { - pos += fwrite(&line[pos], 1, readc - pos, fpout); - } + FD_ZERO(&rfds); + // if output queue is empty + if (0 == write_pout.size) + { + FD_SET(STDIN_FILENO, &rfds); + + } + if (0 == write_std.size) + { + FD_SET(fdpin, &rfds); + maxfd = fdpin; + } + FD_ZERO(&wfds); + // if there is something to write + if (0 < write_std.size) + { + FD_SET(STDOUT_FILENO, &wfds); + maxfd = MAX(maxfd, STDOUT_FILENO); } + if (0 < write_pout.size) + { + FD_SET(fdpout, &wfds); + maxfd = MAX(maxfd, fdpout); + } - //clean up - fclose(fpout); - } - else // PARENT PROCESS - { - perror("Parent"); - signal(SIGINT, sigfunc); - signal(SIGTERM, sigfunc); - signal(SIGKILL, sigfunc); - int rv = 0; - ssize_t pos = 0; - char line[MAXLINE]; - struct Wlan_Helper_Control_Message macmsg; - - - //Send random mac address - macmsg.mac.mac[0] = 0x13; - macmsg.mac.mac[1] = 0x22; - macmsg.mac.mac[2] = 0x33; - macmsg.mac.mac[3] = 0x44; - macmsg.mac.mac[4] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 255); - macmsg.mac.mac[5] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 255); - macmsg.hdr.size = sizeof(struct Wlan_Helper_Control_Message); - - pos = 0; - /* - while (pos < sizeof(struct Wlan_Helper_Control_Message)) + retval = select(maxfd + 1, &rfds, &wfds, NULL, &tv); + + if (-1 == retval && EINTR == errno) + { + continue; + } + if (0 > retval) { - pos += write(STDOUT_FILENO, &macmsg + pos, sizeof(struct Wlan_Helper_Control_Message) - pos); + fprintf(stderr, "select failed: %s\n", strerror(errno)); + closeprog = 1; + break; } - */ - while (closeprog == 0) + + if (FD_ISSET(STDOUT_FILENO, &wfds)) { - if ((rv = fread(line, 1, MAXLINE, fpin)) < 0) + ret = write(STDOUT_FILENO, write_std.buf + write_std.pos, + write_std.size - write_std.pos); + + if (0 > ret) + { + closeprog = 1; + fprintf(stderr, "Write ERROR to STDOUT\n"); + break; + } + else { - perror("READ ERROR FROM fpin"); + write_std.pos += ret; + // check if finished + if (write_std.pos == write_std.size) + { + write_std.pos = 0; + write_std.size = 0; + } } + } - pos = 0; - while (pos < rv) + if (FD_ISSET(fdpout, &wfds)) + { + ret = write(fdpout, write_pout.buf + write_pout.pos, write_pout.size + - write_pout.pos); + + if (0 > ret) + { + closeprog = 1; + fprintf(stderr, "Write ERROR to fdpout\n"); + } + else { - pos += write(STDOUT_FILENO, &line[pos], rv - pos); + write_pout.pos += ret; + // check if finished + if (write_pout.pos == write_pout.size) + { + write_pout.pos = 0; + write_pout.size = 0; + } } } + if (FD_ISSET(STDIN_FILENO, &rfds)) + { + readsize = read(STDIN_FILENO, readbuf, sizeof(readbuf)); - //clean up - fclose(fpin); + if (0 > readsize) + { + closeprog = 1; + fprintf(stderr, "Read ERROR to STDIN_FILENO\n"); + } + else if (0 < readsize) + { + GNUNET_SERVER_mst_receive(stdin_mst, NULL, readbuf, readsize, + GNUNET_NO, GNUNET_NO); - if (first == 1) + } + else + { + //eof + closeprog = 1; + } + } + + if (FD_ISSET(fdpin, &rfds)) { - unlink(FIFO_FILE1); - unlink(FIFO_FILE2); + readsize = read(fdpin, readbuf, sizeof(readbuf)); + + if (0 > readsize) + { + closeprog = 1; + fprintf(stderr, "Read ERROR to fdpin: %s\n", strerror(errno)); + break; + } + else if (0 < readsize) + { + GNUNET_SERVER_mst_receive(file_in_mst, NULL, readbuf, readsize, + GNUNET_NO, GNUNET_NO); + + } + else + { + //eof + closeprog = 1; + } } + } - // Write the input to the output + //clean up + + GNUNET_SERVER_mst_destroy(stdin_mst); + GNUNET_SERVER_mst_destroy(file_in_mst); + + end: if (fpout != NULL) + fclose(fpout); + if (fpin != NULL) + fclose(fpin); + + if (1 == first) + { + unlink(FIFO_FILE1); + unlink(FIFO_FILE2); + } return (0); } +int +main(int argc, char *argv[]) +{ + if (2 != argc) + { + fprintf (stderr, + "This program must be started with the operating mode as argument.\n"); + fprintf (stderr, + "Usage: options\n" + "options:\n" + "1 = first loopback file\n" + "2 = second loopback file\n" + "\n"); + return 1; + } + if (strstr(argv[1], "1") || strstr(argv[1], "2")) + return testmode(argc, argv); + return 1; +} diff --git a/src/transport/test_transport_api_unreliability.c b/src/transport/test_transport_api_unreliability.c index b8a766c19..929e88b1c 100644 --- a/src/transport/test_transport_api_unreliability.c +++ b/src/transport/test_transport_api_unreliability.c @@ -88,6 +88,8 @@ static int is_udp; static int is_unix; +static int is_wlan; + static int connected; static unsigned long long total_bytes; @@ -779,6 +781,11 @@ run (void *cls, setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf"); setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf"); } + else if (is_wlan) + { + setup_peer (&p1, "test_transport_api_wlan_peer1.conf"); + setup_peer (&p2, "test_transport_api_wlan_peer2.conf"); + } else GNUNET_assert (0); GNUNET_assert(p1.th != NULL); @@ -904,6 +911,10 @@ main (int argc, char *argv[]) is_unix = GNUNET_YES; GNUNET_asprintf(&test_name, "unix"); } + else if (strstr(argv[0], "wlan") != NULL) + { + is_wlan = GNUNET_YES; + } GNUNET_log_setup ("test-transport-api-reliability", #if VERBOSE "DEBUG",