Committer: Rene Hummen <rene.hummen@xxxxxxxxxxxxxxxxx> Date: Thu Feb 11 00:43:06 2010 +0100 Revision: 3590 Revision-id: rene.hummen@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Branch nick: trunk Log: uncrustified test Modified: M test/HipClient.java M test/HipServer.java M test/HipUserkeyClient.java M test/auth_performance.c M test/certteststub.c M test/conntest-client-hip.c M test/conntest-client-native-user-key.c M test/conntest-client-native.c M test/conntest-client-opp.c M test/conntest-server-native.c M test/conntest-server.c M test/conntest.c M test/conntest.h M test/cookietest.c M test/dh_performance.c M test/dhtteststub.c M test/first_test.c M test/hadb_deprecated.c M test/hadb_old.c M test/hashtest.c M test/hc_performance.c M test/hipsetup.c M test/keygentest.c M test/listtest.c M test/misc_install.c M test/sqliteteststub.c === modified file 'test/HipClient.java' --- test/HipClient.java 2009-12-11 22:49:11 +0000 +++ test/HipClient.java 2010-02-10 23:43:06 +0000 @@ -11,51 +11,50 @@ import jip.HipSocketFactory; public class HipClient { - - public static void main (String[] args) { - try { - // begin GLOB - //Socket.setSocketImplFactory(new HipSocketImplFactory()); - // end GLOB - // begin PC - SocketFactory hipFactory = new HipSocketFactory(); - // end PC - if (args.length != 3) { - System.err.println("Usage: HipClient <host> <port> " - + "<localport>"); - System.exit(1); - } - String host = args[0]; - int port = Integer.parseInt(args[1]); - int localport = Integer.parseInt(args[2]); - // begin GLOB - //Socket s = new Socket(); - // end GLOB - // begin PC - Socket s = hipFactory.createSocket(); - // end PC - s.bind(new InetSocketAddress(localport)); - s.connect(new InetSocketAddress(host, port)); - System.out.println(s.toString()); - InputStream is = s.getInputStream(); - System.out.println(is.toString()); - BufferedReader in = - new BufferedReader(new InputStreamReader(System.in)); - BufferedReader sin = new BufferedReader(new InputStreamReader(is)); - OutputStream os = s.getOutputStream(); - System.out.println(os.toString()); - PrintWriter sout = new PrintWriter(os); - String line; - System.out.println("Type your input, line by line"); - while ((line = in.readLine()) != null) { - sout.println(line); - sout.flush(); - line = sin.readLine(); - System.out.println("Received: " + line); - } - } catch (Exception ex) { - ex.printStackTrace(); - } +public static void main(String[] args) +{ + try { + // begin GLOB + //Socket.setSocketImplFactory(new HipSocketImplFactory()); + // end GLOB + // begin PC + SocketFactory hipFactory = new HipSocketFactory(); + // end PC + if (args.length != 3) { + System.err.println("Usage: HipClient <host> <port> " + + "<localport>"); + System.exit(1); + } + String host = args[0]; + int port = Integer.parseInt(args[1]); + int localport = Integer.parseInt(args[2]); + // begin GLOB + //Socket s = new Socket(); + // end GLOB + // begin PC + Socket s = hipFactory.createSocket(); + // end PC + s.bind(new InetSocketAddress(localport)); + s.connect(new InetSocketAddress(host, port)); + System.out.println(s.toString()); + InputStream is = s.getInputStream(); + System.out.println(is.toString()); + BufferedReader in = + new BufferedReader(new InputStreamReader(System.in)); + BufferedReader sin = new BufferedReader(new InputStreamReader(is)); + OutputStream os = s.getOutputStream(); + System.out.println(os.toString()); + PrintWriter sout = new PrintWriter(os); + String line; + System.out.println("Type your input, line by line"); + while ((line = in.readLine()) != null) { + sout.println(line); + sout.flush(); + line = sin.readLine(); + System.out.println("Received: " + line); + } + } catch (Exception ex) { + ex.printStackTrace(); } - +} } === modified file 'test/HipServer.java' --- test/HipServer.java 2009-12-11 22:49:11 +0000 +++ test/HipServer.java 2010-02-10 23:43:06 +0000 @@ -11,49 +11,49 @@ import jip.HipServerSocketFactory; public class HipServer { - - public static void main (String[] args) { - try { - // begin GLOB - //ServerSocket.setSocketFactory(new HipSocketImplFactory()); - //Socket.setSocketImplFactory(new HipSocketImplFactory()); - // end GLOB - // begin PC - ServerSocketFactory hipFactory = new HipServerSocketFactory(); - // end PC - if (args.length != 1 && args.length != 2) { - System.err.println("Usage: HipServer <port> [<local_addr>]"); - System.exit(1); - } - int port = Integer.parseInt(args[0]); - // begin GLOB - //ServerSocket ss = new ServerSocket(); - // end GLOB - // begin PC - ServerSocket ss = hipFactory.createServerSocket(); - // end PC - if (args.length == 1) - ss.bind(new InetSocketAddress(port)); - else - ss.bind(new InetSocketAddress(args[1], port)); - System.out.println(ss.toString()); - Socket s = ss.accept(); - System.out.println(s.toString()); - InputStream is = s.getInputStream(); - System.out.println(is.toString()); - BufferedReader in = new BufferedReader(new InputStreamReader(is)); - OutputStream os = s.getOutputStream(); - System.out.println(os.toString()); - PrintWriter out = new PrintWriter(os); - String line; - while ((line = in.readLine()) != null) { - System.out.println("Received: " + line); - out.println(line); - out.flush(); - } - } catch (Exception ex) { - ex.printStackTrace(); - } +public static void main(String[] args) +{ + try { + // begin GLOB + //ServerSocket.setSocketFactory(new HipSocketImplFactory()); + //Socket.setSocketImplFactory(new HipSocketImplFactory()); + // end GLOB + // begin PC + ServerSocketFactory hipFactory = new HipServerSocketFactory(); + // end PC + if (args.length != 1 && args.length != 2) { + System.err.println("Usage: HipServer <port> [<local_addr>]"); + System.exit(1); + } + int port = Integer.parseInt(args[0]); + // begin GLOB + //ServerSocket ss = new ServerSocket(); + // end GLOB + // begin PC + ServerSocket ss = hipFactory.createServerSocket(); + // end PC + if (args.length == 1) { + ss.bind(new InetSocketAddress(port)); + } else { + ss.bind(new InetSocketAddress(args[1], port)); + } + System.out.println(ss.toString()); + Socket s = ss.accept(); + System.out.println(s.toString()); + InputStream is = s.getInputStream(); + System.out.println(is.toString()); + BufferedReader in = new BufferedReader(new InputStreamReader(is)); + OutputStream os = s.getOutputStream(); + System.out.println(os.toString()); + PrintWriter out = new PrintWriter(os); + String line; + while ((line = in.readLine()) != null) { + System.out.println("Received: " + line); + out.println(line); + out.flush(); + } + } catch (Exception ex) { + ex.printStackTrace(); } - +} } === modified file 'test/HipUserkeyClient.java' --- test/HipUserkeyClient.java 2009-12-11 22:49:11 +0000 +++ test/HipUserkeyClient.java 2010-02-10 23:43:06 +0000 @@ -13,56 +13,55 @@ import jip.HipSocketFactory; public class HipUserkeyClient { - - public static void main (String[] args) { - try { - // begin GLOB - //Socket.setSocketImplFactory(new HipSocketImplFactory()); - // end GLOB - // begin PC - SocketFactory hipFactory = new HipSocketFactory(); - // end PC - String file = "/etc/hip/hip_host_dsa_key"; - if (args.length < 3 || args.length > 4) { - System.err.println("Usage: HipClient <host> <port> " - + "<localport> [<keyfile>]"); - System.exit(1); - } - String host = args[0]; - int port = Integer.parseInt(args[1]); - int localport = Integer.parseInt(args[2]); - if (args.length == 4) { - file = args[3]; - } - // begin GLOB - //Socket s = new Socket(); - // end GLOB - // begin PC - HipSocket s = (HipSocket) hipFactory.createSocket(); - // end PC - HipAddress addr = HipAddress.getFromFile(file); - s.bind(addr, localport); - s.connect(new InetSocketAddress(host, port)); - System.out.println(s.toString()); - InputStream is = s.getInputStream(); - System.out.println(is.toString()); - BufferedReader in = - new BufferedReader(new InputStreamReader(System.in)); - BufferedReader sin = new BufferedReader(new InputStreamReader(is)); - OutputStream os = s.getOutputStream(); - System.out.println(os.toString()); - PrintWriter sout = new PrintWriter(os); - String line; - System.out.println("Type your input, line by line"); - while ((line = in.readLine()) != null) { - sout.println(line); - sout.flush(); - line = sin.readLine(); - System.out.println("Received: " + line); - } - } catch (Exception ex) { - ex.printStackTrace(); - } +public static void main(String[] args) +{ + try { + // begin GLOB + //Socket.setSocketImplFactory(new HipSocketImplFactory()); + // end GLOB + // begin PC + SocketFactory hipFactory = new HipSocketFactory(); + // end PC + String file = "/etc/hip/hip_host_dsa_key"; + if (args.length < 3 || args.length > 4) { + System.err.println("Usage: HipClient <host> <port> " + + "<localport> [<keyfile>]"); + System.exit(1); + } + String host = args[0]; + int port = Integer.parseInt(args[1]); + int localport = Integer.parseInt(args[2]); + if (args.length == 4) { + file = args[3]; + } + // begin GLOB + //Socket s = new Socket(); + // end GLOB + // begin PC + HipSocket s = (HipSocket) hipFactory.createSocket(); + // end PC + HipAddress addr = HipAddress.getFromFile(file); + s.bind(addr, localport); + s.connect(new InetSocketAddress(host, port)); + System.out.println(s.toString()); + InputStream is = s.getInputStream(); + System.out.println(is.toString()); + BufferedReader in = + new BufferedReader(new InputStreamReader(System.in)); + BufferedReader sin = new BufferedReader(new InputStreamReader(is)); + OutputStream os = s.getOutputStream(); + System.out.println(os.toString()); + PrintWriter sout = new PrintWriter(os); + String line; + System.out.println("Type your input, line by line"); + while ((line = in.readLine()) != null) { + sout.println(line); + sout.flush(); + line = sin.readLine(); + System.out.println("Received: " + line); + } + } catch (Exception ex) { + ex.printStackTrace(); } - +} } === modified file 'test/auth_performance.c' --- test/auth_performance.c 2010-01-19 11:26:32 +0000 +++ test/auth_performance.c 2010-02-10 23:43:06 +0000 @@ -1,5 +1,5 @@ -#include <stdio.h> /* printf & co */ -#include <stdlib.h> /* exit & co */ +#include <stdio.h> /* printf & co */ +#include <stdlib.h> /* exit & co */ #include <unistd.h> #ifdef HAVE_CONFIG_H #include "config.h" @@ -14,14 +14,13 @@ #define PACKET_LENGTH 1280 int num_measurements = 100; -int key_pool_size = 5; - -int rsa_key_len = 1024; -int dsa_key_len = 1024; +int key_pool_size = 5; +int rsa_key_len = 1024; +int dsa_key_len = 1024; #define ECDSA_CURVE NID_sect163r1 /*! - * \brief Determine and print the gettimeofday time resolution. + * \brief Determine and print the gettimeofday time resolution. * * \author Tobias Heer * @@ -29,499 +28,488 @@ * * \return void */ -void print_timeres(void){ - - struct timeval tv1, tv2; - int i; - printf( "-------------------------------\n" - "Determine gettimeofday resolution:\n"); - - - for(i = 0; i < 10; i++){ - gettimeofday(&tv1, NULL); - do { - gettimeofday(&tv2, NULL); - } while (tv1.tv_usec == tv2.tv_usec); - - printf("Resolution: %ld us\n", tv2.tv_usec - tv1.tv_usec + - 1000000 * (tv2.tv_sec - tv1.tv_sec)); - } - - printf( "-------------------------------\n\n"); +void print_timeres(void) +{ + struct timeval tv1, tv2; + int i; + printf( "-------------------------------\n" + "Determine gettimeofday resolution:\n"); + + + for (i = 0; i < 10; i++) { + gettimeofday(&tv1, NULL); + do { + gettimeofday(&tv2, NULL); + } while (tv1.tv_usec == tv2.tv_usec); + + printf("Resolution: %ld us\n", tv2.tv_usec - tv1.tv_usec + + 1000000 * (tv2.tv_sec - tv1.tv_sec)); + } + + printf( "-------------------------------\n\n"); } -int main(int argc, char ** argv) +int main(int argc, char **argv) { - int i; - int err = 0; - struct timeval start_time; - struct timeval stop_time; - uint64_t timediff = 0; -#if 0 - statistics_data_t creation_stats; - statistics_data_t verify_stats; - uint32_t num_items = 0; - double min = 0.0, max = 0.0, avg = 0.0; - double std_dev = 0.0; -#endif - - unsigned int sig_len = 0; - unsigned char data[PACKET_LENGTH * num_measurements]; - unsigned char hashed_data[SHA_DIGEST_LENGTH * num_measurements]; - - unsigned char key[HIP_MAX_KEY_LEN]; - unsigned int hashed_data_len = 0; - - AES_KEY *aes_enc_key = NULL; - AES_KEY *aes_dec_key = NULL; - unsigned char cbc_iv[AES_BLOCK_SIZE]; - unsigned char enc_data[num_measurements * PACKET_LENGTH]; - unsigned char dec_data[num_measurements * PACKET_LENGTH]; - - RSA * rsa_key_pool[key_pool_size]; - unsigned char * rsa_sig_pool[num_measurements]; - - DSA * dsa_key_pool[key_pool_size]; - DSA_SIG * dsa_sig_pool[num_measurements]; - -#ifdef CONFIG_HIP_ECDSA - EC_KEY * ecdsa_key_pool[key_pool_size]; - ECDSA_SIG * ecdsa_sig_pool[num_measurements]; -#endif /* CONFIG_HIP_ECDSA */ - - hip_set_logdebug(LOGDEBUG_NONE); - -#if 0 - memset(&creation_stats, 0, sizeof(statistics_data_t)); - memset(&verify_stats, 0, sizeof(statistics_data_t)); -#endif - - print_timeres(); - - // data to be signed - printf("generating payload data for %i packets (packet length %i bytes)...\n\n", - num_measurements, PACKET_LENGTH); - RAND_bytes(data, PACKET_LENGTH * num_measurements); - - printf("-------------------------------\n" - "SHA1 performance test (20 byte input)\n" - "-------------------------------\n"); - - printf("Calculating hashes over %d inputs...\n", num_measurements); - - for(i = 0; i < num_measurements; i++) - { - gettimeofday(&start_time, NULL); - - // SHA1 on data - SHA1(&data[i * 20], 20, &hashed_data[i * SHA_DIGEST_LENGTH]); - - gettimeofday(&stop_time, NULL); - - timediff = calc_timeval_diff(&start_time, &stop_time); - //add_statistics_item(&creation_stats, timediff); - printf("%i. sha1-20: %.3f ms\n", i + 1, timediff / 1000.0); - } - - printf("-------------------------------\n" - "SHA1 performance test (40 byte input)\n" - "-------------------------------\n"); - - printf("Calculating hashes over %d inputs...\n", num_measurements); - - for(i = 0; i < num_measurements; i++) - { - gettimeofday(&start_time, NULL); - - // SHA1 on data - SHA1(&data[i * 40], 40, &hashed_data[i * SHA_DIGEST_LENGTH]); - - gettimeofday(&stop_time, NULL); - - timediff = calc_timeval_diff(&start_time, &stop_time); - //add_statistics_item(&creation_stats, timediff); - printf("%i. sha1-40: %.3f ms\n", i + 1, timediff / 1000.0); - } - - printf("-------------------------------\n" - "SHA1 performance test (1280 byte input)\n" - "-------------------------------\n"); - - printf("Calculating hashes over %d packets...\n", num_measurements); - - for(i = 0; i < num_measurements; i++) - { - gettimeofday(&start_time, NULL); - - // SHA1 on data - SHA1(&data[i * PACKET_LENGTH], PACKET_LENGTH, &hashed_data[i * SHA_DIGEST_LENGTH]); - - gettimeofday(&stop_time, NULL); - - timediff = calc_timeval_diff(&start_time, &stop_time); - //add_statistics_item(&creation_stats, timediff); - printf("%i. sha1-1280: %.3f ms\n", i + 1, timediff / 1000.0); - } - - - printf("-------------------------------\n" - "SHA1-HMAC performance test\n" - "-------------------------------\n"); - - printf("Calculating hashes over %d packets...\n", num_measurements); - - RAND_bytes(key, 20); - - for(i = 0; i < num_measurements; i++) - { - gettimeofday(&start_time, NULL); - - // HMAC on data - HMAC(EVP_sha1(), key, 20, &data[i * PACKET_LENGTH], PACKET_LENGTH, - &hashed_data[i * SHA_DIGEST_LENGTH], &hashed_data_len); - - gettimeofday(&stop_time, NULL); - - timediff = calc_timeval_diff(&start_time, &stop_time); - //add_statistics_item(&creation_stats, timediff); - printf("%i. sha1-hmac: %.3f ms\n", i + 1, timediff / 1000.0); - } - -#if 0 - calc_statistics(&creation_stats, &num_items, &min, &max, &avg, &std_dev, - STATS_IN_MSECS); - printf("generation statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n", - num_items, min, max, avg, std_dev); -#endif - - - printf("\n-------------------------------\n" - "AES performance test\n" - "-------------------------------\n"); - - // create a key pool - aes_enc_key = malloc(sizeof(AES_KEY)); - aes_dec_key = malloc(sizeof(AES_KEY)); - AES_set_encrypt_key(key, 8 * hip_enc_key_length(HIP_ESP_AES_SHA1), aes_enc_key); - AES_set_decrypt_key(key, 8 * hip_enc_key_length(HIP_ESP_AES_SHA1), aes_dec_key); - RAND_bytes(cbc_iv, AES_BLOCK_SIZE); - - printf("\nCalculating %d AES encryption\n", num_measurements); - for(i = 0; i < num_measurements; i++) - { - gettimeofday(&start_time, NULL); - - AES_cbc_encrypt(&data[i * PACKET_LENGTH], &enc_data[i * PACKET_LENGTH], - PACKET_LENGTH, aes_enc_key, cbc_iv, AES_ENCRYPT); - - gettimeofday(&stop_time, NULL); - - timediff = calc_timeval_diff(&start_time, &stop_time); - //add_statistics_item(&creation_stats, timediff); - - printf("%i. AES encrypt: %.3f ms\n", i + 1, timediff / 1000.0); - - } - - printf("\nCalculating %d AES decryption\n", num_measurements); - for(i = 0; i < num_measurements; i++) - { - gettimeofday(&start_time, NULL); - - AES_cbc_encrypt(&enc_data[i * PACKET_LENGTH], &dec_data[i * PACKET_LENGTH], - PACKET_LENGTH, aes_dec_key, cbc_iv, AES_DECRYPT); - - gettimeofday(&stop_time, NULL); - - timediff = calc_timeval_diff(&start_time, &stop_time); - //add_statistics_item(&creation_stats, timediff); - - - printf("%i. AES decrypt: %.3f ms\n", i + 1, timediff / 1000.0); - } - - // reinitialize statistics -#if 0 - memset(&creation_stats, 0, sizeof(statistics_data_t)); - memset(&verify_stats, 0, sizeof(statistics_data_t)); -#endif - - - - printf("\n-------------------------------\n" - "RSA performance test\n" - "-------------------------------\n"); - - // create a key pool - printf("Creating key pool of %d keys of length %d.\n", key_pool_size, rsa_key_len); - for(i = 0; i < key_pool_size; i++) - { - rsa_key_pool[i] = create_rsa_key(rsa_key_len); - } - - printf("\nCalculating %d RSA signatures\n", num_measurements); - for(i = 0; i < num_measurements; i++) - { - sig_len = RSA_size(rsa_key_pool[i % key_pool_size]); - - rsa_sig_pool[i] = malloc(sig_len); - - gettimeofday(&start_time, NULL); - - // SHA1 on data - SHA1(&data[i * PACKET_LENGTH], PACKET_LENGTH, &hashed_data[i * SHA_DIGEST_LENGTH]); - - // sign - err = RSA_sign(NID_sha1, &hashed_data[i * SHA_DIGEST_LENGTH], SHA_DIGEST_LENGTH, - rsa_sig_pool[i], &sig_len, rsa_key_pool[i % key_pool_size]); - - gettimeofday(&stop_time, NULL); - - timediff = calc_timeval_diff(&start_time, &stop_time); - //add_statistics_item(&creation_stats, timediff); - - if(err <= 0) - { - printf("RSA signature unsuccessful\n"); - } - else - { - printf("%i. rsa signature: %.3f ms\n", i + 1, timediff / 1000.0); - } - } -#if 0 - calc_statistics(&creation_stats, &num_items, &min, &max, &avg, &std_dev, - STATS_IN_MSECS); - printf("generation statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n", - num_items, min, max, avg, std_dev); -#endif - -#if 0 - printf("\n"); - printf("Signature generation took %.3f sec (%.5f sec per key)\n", - bench_secs, bench_secs / sw_bench_loops); - printf("%4.2f signatures per sec, %4.2f signatures per min\n\n", - sw_bench_loops/bench_secs, sw_bench_loops/bench_secs*60); -#endif - - - printf("\nVerifying %d RSA signatures\n", num_measurements); - for(i = 0; i < num_measurements; i++) - { - gettimeofday(&start_time, NULL); - - SHA1(&data[i * PACKET_LENGTH], PACKET_LENGTH, &hashed_data[i * SHA_DIGEST_LENGTH]); - - err = RSA_verify(NID_sha1, &hashed_data[i * SHA_DIGEST_LENGTH], SHA_DIGEST_LENGTH, - rsa_sig_pool[i], RSA_size(rsa_key_pool[i % key_pool_size]), - rsa_key_pool[i % key_pool_size]); - - gettimeofday(&stop_time, NULL); - - timediff = calc_timeval_diff(&start_time, &stop_time); - //add_statistics_item(&verify_stats, timediff); - - if(err <= 0) - { - printf("Verification failed\n"); - } - else - { - printf("%i. rsa verification: %.3f ms\n", i + 1, timediff / 1000.0); - } - } - -#if 0 - calc_statistics(&verify_stats, &num_items, &min, &max, &avg, &std_dev, - STATS_IN_MSECS); - printf("verification statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n", - num_items, min, max, avg, std_dev); -#endif - - - // reinitialize statistics -#if 0 - memset(&creation_stats, 0, sizeof(statistics_data_t)); - memset(&verify_stats, 0, sizeof(statistics_data_t)); -#endif - - - - printf("\n-------------------------------\n" - "DSA performance test\n" - "-------------------------------\n"); - - printf("Creating key pool of %d keys of length %d...\n", key_pool_size, dsa_key_len); - for(i = 0; i < key_pool_size; i++) - { - dsa_key_pool[i] = create_dsa_key(dsa_key_len); - } - - printf("\nCalculating %d DSA signatures\n", num_measurements); - for(i = 0; i < num_measurements; i++) - { - sig_len = sizeof(DSA_SIG *); - - dsa_sig_pool[i] = malloc(sig_len); - - gettimeofday(&start_time, NULL); - - // SHA1 on data - SHA1(&data[i * PACKET_LENGTH], PACKET_LENGTH, &hashed_data[i * SHA_DIGEST_LENGTH]); - - // sign - dsa_sig_pool[i] = DSA_do_sign(&hashed_data[i * SHA_DIGEST_LENGTH], SHA_DIGEST_LENGTH, - dsa_key_pool[i % key_pool_size]); - - gettimeofday(&stop_time, NULL); - - timediff = calc_timeval_diff(&start_time, &stop_time); - //add_statistics_item(&creation_stats, timediff); - - if(!dsa_sig_pool[i]){ - printf("DSA signature not successful\n"); - } - else - { - printf("%i. dsa signature: %.3f ms\n", i + 1, timediff / 1000.0); - } - } -#if 0 - calc_statistics(&creation_stats, &num_items, &min, &max, &avg, &std_dev, - STATS_IN_MSECS); - printf("generation statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n", - num_items, min, max, avg, std_dev); -#endif - - printf("\nVerifying %d DSA signatures\n", num_measurements); - for(i = 0; i < num_measurements; i++) - { - gettimeofday(&start_time, NULL); - - SHA1(&data[i * PACKET_LENGTH], PACKET_LENGTH, &hashed_data[i * SHA_DIGEST_LENGTH]); - - err = DSA_do_verify(&hashed_data[i * SHA_DIGEST_LENGTH], SHA_DIGEST_LENGTH, - dsa_sig_pool[i], dsa_key_pool[i % key_pool_size]); - - gettimeofday(&stop_time, NULL); - - timediff = calc_timeval_diff(&start_time, &stop_time); - //add_statistics_item(&verify_stats, timediff); - - if(err <= 0) - { - printf("Verification failed\n"); - } - else - { - printf("%i. dsa verification: %.3f ms\n", i + 1, timediff / 1000.0); - } - } -#if 0 - calc_statistics(&verify_stats, &num_items, &min, &max, &avg, &std_dev, - STATS_IN_MSECS); - printf("verification statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n", - num_items, min, max, avg, std_dev); -#endif - - - - // reinitialize statistics -#if 0 - memset(&creation_stats, 0, sizeof(statistics_data_t)); - memset(&verify_stats, 0, sizeof(statistics_data_t)); -#endif - - -#ifdef CONFIG_HIP_ECDSA - printf("\n-------------------------------\n" - "ECDSA performance test\n" - "-------------------------------\n"); - - printf("Creating key pool of %d keys for curve ECDSA_CURVE...\n", key_pool_size); - for(i = 0; i < key_pool_size; i++) - { - ecdsa_key_pool[i] = EC_KEY_new_by_curve_name(ECDSA_CURVE); - if (!ecdsa_key_pool[i]) - { - printf("ec key setup failed!\n"); - } - - if (!EC_KEY_generate_key(ecdsa_key_pool[i])) - { - printf("ec key generation failed!\n"); - } - } - - printf("\nCalculating %d ECDSA signatures\n", num_measurements); - for(i = 0; i < num_measurements; i++) - { - sig_len = ECDSA_size(ecdsa_key_pool[i % key_pool_size]); - - ecdsa_sig_pool[i] = malloc(sig_len); - - gettimeofday(&start_time, NULL); - - // SHA1 on data - SHA1(&data[i * PACKET_LENGTH], PACKET_LENGTH, &hashed_data[i * SHA_DIGEST_LENGTH]); - - // sign - ecdsa_sig_pool[i] = ECDSA_do_sign(&hashed_data[i * SHA_DIGEST_LENGTH], - SHA_DIGEST_LENGTH, ecdsa_key_pool[i % key_pool_size]); - - gettimeofday(&stop_time, NULL); - - timediff = calc_timeval_diff(&start_time, &stop_time); - //add_statistics_item(&creation_stats, timediff); - - if(!ecdsa_sig_pool[i]) - { - printf("ECDSA signature not successful\n"); - } - else - { - printf("%i. ecdsa signature: %.3f ms\n", i + 1, timediff / 1000.0); - } - } -#endif /* CONFIG_HIP_ECDSA */ -#if 0 - calc_statistics(&creation_stats, &num_items, &min, &max, &avg, &std_dev, - STATS_IN_MSECS); - printf("generation statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n", - num_items, min, max, avg, std_dev); - - printf("\nVerifying %d ECDSA signatures\n", num_measurements); - for(i = 0; i < num_measurements; i++) - { - gettimeofday(&start_time, NULL); - - SHA1(&data[i * PACKET_LENGTH], PACKET_LENGTH, &hashed_data[i * SHA_DIGEST_LENGTH]); - - err = ECDSA_do_verify(&hashed_data[i * SHA_DIGEST_LENGTH], SHA_DIGEST_LENGTH, - ecdsa_sig_pool[i], ecdsa_key_pool[i % key_pool_size]); - - gettimeofday(&stop_time, NULL); - - timediff = calc_timeval_diff(&start_time, &stop_time); - //add_statistics_item(&verify_stats, timediff); - - if(err <= 0) - { - printf("Verification failed\n"); - } - else - { - printf("%i. ecdsa verification: %.3f ms\n", i + 1, timediff / 1000.0); - } - } -#endif - -#if 0 - calc_statistics(&verify_stats, &num_items, &min, &max, &avg, &std_dev, - STATS_IN_MSECS); - printf("verification statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n", - num_items, min, max, avg, std_dev); -#endif - - return err; + int i; + int err = 0; + struct timeval start_time; + struct timeval stop_time; + uint64_t timediff = 0; +#if 0 + statistics_data_t creation_stats; + statistics_data_t verify_stats; + uint32_t num_items = 0; + double min = 0.0, max = 0.0, avg = 0.0; + double std_dev = 0.0; +#endif + + unsigned int sig_len = 0; + unsigned char data[PACKET_LENGTH * num_measurements]; + unsigned char hashed_data[SHA_DIGEST_LENGTH * num_measurements]; + unsigned char key[HIP_MAX_KEY_LEN]; + unsigned int hashed_data_len = 0; + + AES_KEY *aes_enc_key = NULL; + AES_KEY *aes_dec_key = NULL; + unsigned char cbc_iv[AES_BLOCK_SIZE]; + unsigned char enc_data[num_measurements * PACKET_LENGTH]; + unsigned char dec_data[num_measurements * PACKET_LENGTH]; + + RSA *rsa_key_pool[key_pool_size]; + unsigned char *rsa_sig_pool[num_measurements]; + + DSA *dsa_key_pool[key_pool_size]; + DSA_SIG *dsa_sig_pool[num_measurements]; + +#ifdef CONFIG_HIP_ECDSA + EC_KEY *ecdsa_key_pool[key_pool_size]; + ECDSA_SIG *ecdsa_sig_pool[num_measurements]; +#endif /* CONFIG_HIP_ECDSA */ + + hip_set_logdebug(LOGDEBUG_NONE); + +#if 0 + memset(&creation_stats, 0, sizeof(statistics_data_t)); + memset(&verify_stats, 0, sizeof(statistics_data_t)); +#endif + + print_timeres(); + + // data to be signed + printf("generating payload data for %i packets (packet length %i bytes)...\n\n", + num_measurements, PACKET_LENGTH); + RAND_bytes(data, PACKET_LENGTH * num_measurements); + + printf("-------------------------------\n" + "SHA1 performance test (20 byte input)\n" + "-------------------------------\n"); + + printf("Calculating hashes over %d inputs...\n", num_measurements); + + for (i = 0; i < num_measurements; i++) { + gettimeofday(&start_time, NULL); + + // SHA1 on data + SHA1(&data[i * 20], 20, &hashed_data[i * SHA_DIGEST_LENGTH]); + + gettimeofday(&stop_time, NULL); + + timediff = calc_timeval_diff(&start_time, &stop_time); + //add_statistics_item(&creation_stats, timediff); + printf("%i. sha1-20: %.3f ms\n", i + 1, timediff / 1000.0); + } + + printf("-------------------------------\n" + "SHA1 performance test (40 byte input)\n" + "-------------------------------\n"); + + printf("Calculating hashes over %d inputs...\n", num_measurements); + + for (i = 0; i < num_measurements; i++) { + gettimeofday(&start_time, NULL); + + // SHA1 on data + SHA1(&data[i * 40], 40, &hashed_data[i * SHA_DIGEST_LENGTH]); + + gettimeofday(&stop_time, NULL); + + timediff = calc_timeval_diff(&start_time, &stop_time); + //add_statistics_item(&creation_stats, timediff); + printf("%i. sha1-40: %.3f ms\n", i + 1, timediff / 1000.0); + } + + printf("-------------------------------\n" + "SHA1 performance test (1280 byte input)\n" + "-------------------------------\n"); + + printf("Calculating hashes over %d packets...\n", num_measurements); + + for (i = 0; i < num_measurements; i++) { + gettimeofday(&start_time, NULL); + + // SHA1 on data + SHA1(&data[i * PACKET_LENGTH], PACKET_LENGTH, &hashed_data[i * SHA_DIGEST_LENGTH]); + + gettimeofday(&stop_time, NULL); + + timediff = calc_timeval_diff(&start_time, &stop_time); + //add_statistics_item(&creation_stats, timediff); + printf("%i. sha1-1280: %.3f ms\n", i + 1, timediff / 1000.0); + } + + + printf("-------------------------------\n" + "SHA1-HMAC performance test\n" + "-------------------------------\n"); + + printf("Calculating hashes over %d packets...\n", num_measurements); + + RAND_bytes(key, 20); + + for (i = 0; i < num_measurements; i++) { + gettimeofday(&start_time, NULL); + + // HMAC on data + HMAC(EVP_sha1(), key, 20, &data[i * PACKET_LENGTH], PACKET_LENGTH, + &hashed_data[i * SHA_DIGEST_LENGTH], &hashed_data_len); + + gettimeofday(&stop_time, NULL); + + timediff = calc_timeval_diff(&start_time, &stop_time); + //add_statistics_item(&creation_stats, timediff); + printf("%i. sha1-hmac: %.3f ms\n", i + 1, timediff / 1000.0); + } + +#if 0 + calc_statistics(&creation_stats, &num_items, &min, &max, &avg, &std_dev, + STATS_IN_MSECS); + printf("generation statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n", + num_items, min, max, avg, std_dev); +#endif + + + printf("\n-------------------------------\n" + "AES performance test\n" + "-------------------------------\n"); + + // create a key pool + aes_enc_key = malloc(sizeof(AES_KEY)); + aes_dec_key = malloc(sizeof(AES_KEY)); + AES_set_encrypt_key(key, 8 * hip_enc_key_length(HIP_ESP_AES_SHA1), + aes_enc_key); + AES_set_decrypt_key(key, 8 * hip_enc_key_length(HIP_ESP_AES_SHA1), + aes_dec_key); + RAND_bytes(cbc_iv, AES_BLOCK_SIZE); + + printf("\nCalculating %d AES encryption\n", num_measurements); + for (i = 0; i < num_measurements; i++) { + gettimeofday(&start_time, NULL); + + AES_cbc_encrypt(&data[i * PACKET_LENGTH], &enc_data[i * PACKET_LENGTH], + PACKET_LENGTH, aes_enc_key, cbc_iv, AES_ENCRYPT); + + gettimeofday(&stop_time, NULL); + + timediff = calc_timeval_diff(&start_time, &stop_time); + //add_statistics_item(&creation_stats, timediff); + + printf("%i. AES encrypt: %.3f ms\n", i + 1, timediff / 1000.0); + } + + printf("\nCalculating %d AES decryption\n", num_measurements); + for (i = 0; i < num_measurements; i++) { + gettimeofday(&start_time, NULL); + + AES_cbc_encrypt(&enc_data[i * PACKET_LENGTH], + &dec_data[i * PACKET_LENGTH], + PACKET_LENGTH, aes_dec_key, + cbc_iv, + AES_DECRYPT); + + gettimeofday(&stop_time, NULL); + + timediff = calc_timeval_diff(&start_time, &stop_time); + //add_statistics_item(&creation_stats, timediff); + + + printf("%i. AES decrypt: %.3f ms\n", i + 1, timediff / 1000.0); + } + + // reinitialize statistics +#if 0 + memset(&creation_stats, 0, sizeof(statistics_data_t)); + memset(&verify_stats, 0, sizeof(statistics_data_t)); +#endif + + + + printf("\n-------------------------------\n" + "RSA performance test\n" + "-------------------------------\n"); + + // create a key pool + printf("Creating key pool of %d keys of length %d.\n", key_pool_size, + rsa_key_len); + for (i = 0; i < key_pool_size; i++) { + rsa_key_pool[i] = create_rsa_key(rsa_key_len); + } + + printf("\nCalculating %d RSA signatures\n", num_measurements); + for (i = 0; i < num_measurements; i++) { + sig_len = RSA_size(rsa_key_pool[i % key_pool_size]); + + rsa_sig_pool[i] = malloc(sig_len); + + gettimeofday(&start_time, NULL); + + // SHA1 on data + SHA1(&data[i * PACKET_LENGTH], PACKET_LENGTH, + &hashed_data[i * SHA_DIGEST_LENGTH]); + + // sign + err = RSA_sign(NID_sha1, &hashed_data[i * SHA_DIGEST_LENGTH], + SHA_DIGEST_LENGTH, + rsa_sig_pool[i], &sig_len, + rsa_key_pool[i % key_pool_size]); + + gettimeofday(&stop_time, NULL); + + timediff = calc_timeval_diff(&start_time, &stop_time); + //add_statistics_item(&creation_stats, timediff); + + if (err <= 0) { + printf("RSA signature unsuccessful\n"); + } else { + printf("%i. rsa signature: %.3f ms\n", i + 1, timediff / 1000.0); + } + } +#if 0 + calc_statistics(&creation_stats, &num_items, &min, &max, &avg, &std_dev, + STATS_IN_MSECS); + printf("generation statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n", + num_items, min, max, avg, std_dev); +#endif + +#if 0 + printf("\n"); + printf("Signature generation took %.3f sec (%.5f sec per key)\n", + bench_secs, bench_secs / sw_bench_loops); + printf("%4.2f signatures per sec, %4.2f signatures per min\n\n", + sw_bench_loops / bench_secs, sw_bench_loops / bench_secs * 60); +#endif + + + printf("\nVerifying %d RSA signatures\n", num_measurements); + for (i = 0; i < num_measurements; i++) { + gettimeofday(&start_time, NULL); + + SHA1(&data[i * PACKET_LENGTH], PACKET_LENGTH, + &hashed_data[i * SHA_DIGEST_LENGTH]); + + err = RSA_verify(NID_sha1, &hashed_data[i * SHA_DIGEST_LENGTH], + SHA_DIGEST_LENGTH, + rsa_sig_pool[i], + RSA_size(rsa_key_pool[i % key_pool_size]), + rsa_key_pool[i % key_pool_size]); + + gettimeofday(&stop_time, NULL); + + timediff = calc_timeval_diff(&start_time, &stop_time); + //add_statistics_item(&verify_stats, timediff); + + if (err <= 0) { + printf("Verification failed\n"); + } else { + printf("%i. rsa verification: %.3f ms\n", i + 1, timediff / 1000.0); + } + } + +#if 0 + calc_statistics(&verify_stats, &num_items, &min, &max, &avg, &std_dev, + STATS_IN_MSECS); + printf("verification statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n", + num_items, min, max, avg, std_dev); +#endif + + + // reinitialize statistics +#if 0 + memset(&creation_stats, 0, sizeof(statistics_data_t)); + memset(&verify_stats, 0, sizeof(statistics_data_t)); +#endif + + + + printf("\n-------------------------------\n" + "DSA performance test\n" + "-------------------------------\n"); + + printf("Creating key pool of %d keys of length %d...\n", key_pool_size, + dsa_key_len); + for (i = 0; i < key_pool_size; i++) { + dsa_key_pool[i] = create_dsa_key(dsa_key_len); + } + + printf("\nCalculating %d DSA signatures\n", num_measurements); + for (i = 0; i < num_measurements; i++) { + sig_len = sizeof(DSA_SIG *); + + dsa_sig_pool[i] = malloc(sig_len); + + gettimeofday(&start_time, NULL); + + // SHA1 on data + SHA1(&data[i * PACKET_LENGTH], PACKET_LENGTH, + &hashed_data[i * SHA_DIGEST_LENGTH]); + + // sign + dsa_sig_pool[i] = DSA_do_sign(&hashed_data[i * SHA_DIGEST_LENGTH], + SHA_DIGEST_LENGTH, + dsa_key_pool[i % key_pool_size]); + + gettimeofday(&stop_time, NULL); + + timediff = calc_timeval_diff(&start_time, &stop_time); + //add_statistics_item(&creation_stats, timediff); + + if (!dsa_sig_pool[i]) { + printf("DSA signature not successful\n"); + } else { + printf("%i. dsa signature: %.3f ms\n", i + 1, timediff / 1000.0); + } + } +#if 0 + calc_statistics(&creation_stats, &num_items, &min, &max, &avg, &std_dev, + STATS_IN_MSECS); + printf("generation statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n", + num_items, min, max, avg, std_dev); +#endif + + printf("\nVerifying %d DSA signatures\n", num_measurements); + for (i = 0; i < num_measurements; i++) { + gettimeofday(&start_time, NULL); + + SHA1(&data[i * PACKET_LENGTH], PACKET_LENGTH, + &hashed_data[i * SHA_DIGEST_LENGTH]); + + err = DSA_do_verify(&hashed_data[i * SHA_DIGEST_LENGTH], + SHA_DIGEST_LENGTH, + dsa_sig_pool[i], + dsa_key_pool[i % key_pool_size]); + + gettimeofday(&stop_time, NULL); + + timediff = calc_timeval_diff(&start_time, &stop_time); + //add_statistics_item(&verify_stats, timediff); + + if (err <= 0) { + printf("Verification failed\n"); + } else { + printf("%i. dsa verification: %.3f ms\n", i + 1, timediff / 1000.0); + } + } +#if 0 + calc_statistics(&verify_stats, &num_items, &min, &max, &avg, &std_dev, + STATS_IN_MSECS); + printf("verification statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n", + num_items, min, max, avg, std_dev); +#endif + + + + // reinitialize statistics +#if 0 + memset(&creation_stats, 0, sizeof(statistics_data_t)); + memset(&verify_stats, 0, sizeof(statistics_data_t)); +#endif + + +#ifdef CONFIG_HIP_ECDSA + printf("\n-------------------------------\n" + "ECDSA performance test\n" + "-------------------------------\n"); + + printf("Creating key pool of %d keys for curve ECDSA_CURVE...\n", + key_pool_size); + for (i = 0; i < key_pool_size; i++) { + ecdsa_key_pool[i] = EC_KEY_new_by_curve_name(ECDSA_CURVE); + if (!ecdsa_key_pool[i]) { + printf("ec key setup failed!\n"); + } + + if (!EC_KEY_generate_key(ecdsa_key_pool[i])) { + printf("ec key generation failed!\n"); + } + } + + printf("\nCalculating %d ECDSA signatures\n", num_measurements); + for (i = 0; i < num_measurements; i++) { + sig_len = ECDSA_size(ecdsa_key_pool[i % key_pool_size]); + + ecdsa_sig_pool[i] = malloc(sig_len); + + gettimeofday(&start_time, NULL); + + // SHA1 on data + SHA1(&data[i * PACKET_LENGTH], PACKET_LENGTH, + &hashed_data[i * SHA_DIGEST_LENGTH]); + + // sign + ecdsa_sig_pool[i] = ECDSA_do_sign(&hashed_data[i * SHA_DIGEST_LENGTH], + SHA_DIGEST_LENGTH, + ecdsa_key_pool[i % key_pool_size]); + + gettimeofday(&stop_time, NULL); + + timediff = calc_timeval_diff(&start_time, &stop_time); + //add_statistics_item(&creation_stats, timediff); + + if (!ecdsa_sig_pool[i]) { + printf("ECDSA signature not successful\n"); + } else { + printf("%i. ecdsa signature: %.3f ms\n", i + 1, timediff / 1000.0); + } + } +#endif /* CONFIG_HIP_ECDSA */ +#if 0 + calc_statistics(&creation_stats, &num_items, &min, &max, &avg, &std_dev, + STATS_IN_MSECS); + printf("generation statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n", + num_items, min, max, avg, std_dev); + + printf("\nVerifying %d ECDSA signatures\n", num_measurements); + for (i = 0; i < num_measurements; i++) { + gettimeofday(&start_time, NULL); + + SHA1(&data[i * PACKET_LENGTH], PACKET_LENGTH, + &hashed_data[i * SHA_DIGEST_LENGTH]); + + err = ECDSA_do_verify(&hashed_data[i * SHA_DIGEST_LENGTH], + SHA_DIGEST_LENGTH, + ecdsa_sig_pool[i], + ecdsa_key_pool[i % key_pool_size]); + + gettimeofday(&stop_time, NULL); + + timediff = calc_timeval_diff(&start_time, &stop_time); + //add_statistics_item(&verify_stats, timediff); + + if (err <= 0) { + printf("Verification failed\n"); + } else { + printf("%i. ecdsa verification: %.3f ms\n", i + 1, + timediff / 1000.0); + } + } +#endif + +#if 0 + calc_statistics(&verify_stats, &num_items, &min, &max, &avg, &std_dev, + STATS_IN_MSECS); + printf("verification statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n", + num_items, min, max, avg, std_dev); +#endif + + return err; } === modified file 'test/certteststub.c' --- test/certteststub.c 2010-01-19 09:28:42 +0000 +++ test/certteststub.c 2010-02-10 23:43:06 +0000 @@ -19,223 +19,245 @@ #include "lib/core/certtools.h" /** - * compression_test - Function that takes len bytes of data in orig pointer and then - * compresses (using Zlib) it and uncompresses it and compares it to the original. The - * lengths of the original and compressed data are printed + * compression_test - Function that takes len bytes of data in orig pointer and + * then compresses (using Zlib) it and uncompresses it and compares it to the + * original. The lengths of the original and compressed data are printed * * @param orig is a pointer to the char table containing the data to be compressed * @param len is the length of the data to be compressed * * @return void - * + * * @note this is just a test function for the certteststub and not very usefull for - * anything else unless modified + * anything else unless modified **/ -void compression_test(unsigned char * orig, int len) { - unsigned char original[1024]; - unsigned char compressed[1024]; - unsigned char uncompressed[1024]; - int return_value = 0; - uLongf compressed_buf_length = 0; - uLongf uncompressed_buf_length = 0; - - HIP_DEBUG("Testing Zlib compression on the data\n"); - memset(&compressed, '\0', sizeof(compressed)); - memset(&uncompressed, '0', sizeof(uncompressed)); - memcpy(original, orig, len); - - compressed_buf_length = sizeof(compressed); - - return_value = compress2((Bytef *)compressed , &compressed_buf_length, - (Bytef *)&original, (uLong)len, - Z_BEST_COMPRESSION); - - if (return_value == Z_OK) HIP_DEBUG("Compression was succesfull\n"); - - if (return_value == Z_BUF_ERROR) - HIP_DEBUG("Compression was NOT succesfull (given buffer is too small)\n"); - if (return_value == Z_MEM_ERROR) - HIP_DEBUG("Compression was NOT succesfull (not enough memory)\n"); - - uncompressed_buf_length = sizeof(uncompressed); - - /* compressed_buf_length contains used buffer length after compress */ - HIP_DEBUG("Uncompressed data length: %d\n" - "Compressed data length: %d\n", - len, compressed_buf_length); - - return_value = uncompress((Bytef *)uncompressed, &uncompressed_buf_length, - (Bytef *)compressed, (uLong)compressed_buf_length); - - if (return_value == Z_OK) HIP_DEBUG("Uncompression was succesfull\n"); - - if (return_value == Z_BUF_ERROR) - HIP_DEBUG("Uncompression was NOT succesfull (given buffer is too small)\n"); - if (return_value == Z_MEM_ERROR) - HIP_DEBUG("Uncompression was NOT succesfull (not enough memory)\n"); - - if (memcmp(original, uncompressed, len) == 0) - HIP_DEBUG("Uncompressed data did match the original\n\n"); - else - HIP_DEBUG("Uncompressed data did NOT match the original\n\n"); - - return; +void compression_test(unsigned char *orig, int len) +{ + unsigned char original[1024]; + unsigned char compressed[1024]; + unsigned char uncompressed[1024]; + int return_value = 0; + uLongf compressed_buf_length = 0; + uLongf uncompressed_buf_length = 0; + + HIP_DEBUG("Testing Zlib compression on the data\n"); + memset(&compressed, '\0', sizeof(compressed)); + memset(&uncompressed, '0', sizeof(uncompressed)); + memcpy(original, orig, len); + + compressed_buf_length = sizeof(compressed); + + return_value = compress2((Bytef *) compressed,&compressed_buf_length, + (Bytef *) &original, (uLong) len, + Z_BEST_COMPRESSION); + + if (return_value == Z_OK) { + HIP_DEBUG("Compression was succesfull\n"); + } + + if (return_value == Z_BUF_ERROR) { + HIP_DEBUG("Compression was NOT succesfull (given buffer is too small)\n"); + } + if (return_value == Z_MEM_ERROR) { + HIP_DEBUG("Compression was NOT succesfull (not enough memory)\n"); + } + + uncompressed_buf_length = sizeof(uncompressed); + + /* compressed_buf_length contains used buffer length after compress */ + HIP_DEBUG("Uncompressed data length: %d\n" + "Compressed data length: %d\n", + len, compressed_buf_length); + + return_value = uncompress((Bytef *) uncompressed, &uncompressed_buf_length, + (Bytef *) compressed, + (uLong) compressed_buf_length); + + if (return_value == Z_OK) { + HIP_DEBUG("Uncompression was succesfull\n"); + } + + if (return_value == Z_BUF_ERROR) { + HIP_DEBUG("Uncompression was NOT succesfull (given buffer is too small)\n"); + } + if (return_value == Z_MEM_ERROR) { + HIP_DEBUG("Uncompression was NOT succesfull (not enough memory)\n"); + } + + if (memcmp(original, uncompressed, len) == 0) { + HIP_DEBUG("Uncompressed data did match the original\n\n"); + } else { + HIP_DEBUG("Uncompressed data did NOT match the original\n\n"); + } + + return; } - + int main(int argc, char *argv[]) { - int err = 0, i = 0, len; - struct hip_cert_spki_info * cert = NULL; - struct hip_cert_spki_info * to_verification = NULL; - time_t not_before = 0, not_after = 0; - struct hip_common *msg; - struct in6_addr *defhit; - char certificate[1024]; - unsigned char der_cert[1024]; - CONF * conf; - CONF_VALUE *item; - STACK_OF(CONF_VALUE) * sec = NULL; - STACK_OF(CONF_VALUE) * sec_name = NULL; - - if (argc != 2) { - printf("Usage: %s spki|x509\n", argv[0]); - exit(EXIT_SUCCESS); - } - - HIP_DEBUG("- This test tool has to be run as root otherwise this will fail!\n") ; - HIP_DEBUG("- Hipd has to run otherwise this will hang!\n"); - - HIP_IFEL(!(msg = malloc(HIP_MAX_PACKET)), -1, - "Malloc for msg failed\n"); - defhit = malloc(sizeof(struct in6_addr)); - if (!defhit) goto out_err; - - if (strcmp(argv[1], "spki")) goto skip_spki; - - HIP_DEBUG("Starting to test SPKI certficate tools\n"); - - cert = malloc(sizeof(struct hip_cert_spki_info)); - if (!cert) goto out_err; - - to_verification = malloc(sizeof(struct hip_cert_spki_info)); - if (!to_verification) goto out_err; - - time(¬_before); - time(¬_after); - HIP_DEBUG("Reading configuration file (%s)\n", HIP_CERT_CONF_PATH); - conf = hip_cert_open_conf(); - sec = hip_cert_read_conf_section("hip_spki", conf); - - for (i = 0; i < sk_CONF_VALUE_num(sec); i++) { - item = (void*)sk_CONF_VALUE_value(sec, i); - _HIP_DEBUG("Sec: %s, Key; %s, Val %s\n", - item->section, item->name, item->value); - if (!strcmp(item->name, "issuerhit")) { - err = inet_pton(AF_INET6, item->value, defhit); - if (err < 1) { - err = -1; - goto out_err; - } - } - if (!strcmp(item->name, "days")) { - _HIP_DEBUG("Days in sec = %d\n", HIP_CERT_DAY * atoi(item->value)); - not_after += HIP_CERT_DAY * atoi(item->value); - } - } - hip_cert_free_conf(conf); - - hip_cert_spki_create_cert(cert, - "hit", defhit, - "hit", defhit, - ¬_before, - ¬_after); - - _HIP_DEBUG("\n\nPublic-key sequence contents after all is done:\n\n" - "%s\n\n", cert->public_key); - - _HIP_DEBUG("Cert sequence contents after all is done:\n\n" - "%s\n\n", cert->cert); - - _HIP_DEBUG("Signature sequence contents after all is done:\n\n" - "%s\n\n", cert->signature); - /* - Concatenate everything together as if we would have gotten - it from someone else and we would be starting to verify. - - So the process would be take the cert blob and take out - public-key sequence, cert sequence and signature sequence - and create a hip_cert_spki_info and send it to the daemon - for verification. - */ - memset(&certificate, '\0', sizeof(certificate)); - sprintf((char *) &certificate,"(sequence %s%s%s)", - cert->public_key, - cert->cert, - cert->signature); - HIP_DEBUG("\n\nCertificate gotten back from daemon:\n\n" - "%s\n\nCertificate len %d\n\n", - certificate, - strlen(certificate)); - - compression_test((unsigned char *)certificate, strlen(certificate)); - - HIP_IFEL(hip_cert_spki_char2certinfo(certificate, to_verification), -1, - "Failed to construct the hip_cert_spki_info from certificate\n"); - - /* - below, commented out, is the daemons version of the verification - and below that is the lib version of the verification - */ - /* - HIP_DEBUG("Sending the certificate to daemon for verification\n"); - - HIP_IFEL(hip_cert_spki_send_to_verification(to_verification), -1, - "Failed in sending to verification\n"); - HIP_IFEL(to_verification->success, -1, - "Verification was not successfull\n"); - HIP_DEBUG("Verification was successfull (return value %d)\n", - to_verification->success); - */ - /* Lets do the verification in library */ - HIP_IFEL(hip_cert_spki_lib_verify(to_verification), -1, - "Verification was not succesfull\n"); - HIP_DEBUG("Verification was successfull (return value %d)\n", - to_verification->success); - - goto out_err; - + int err = 0, i = 0, len; + struct hip_cert_spki_info *cert = NULL; + struct hip_cert_spki_info *to_verification = NULL; + time_t not_before = 0, not_after = 0; + struct hip_common *msg; + struct in6_addr *defhit; + char certificate[1024]; + unsigned char der_cert[1024]; + CONF *conf; + CONF_VALUE *item; + STACK_OF(CONF_VALUE) * sec = NULL; + STACK_OF(CONF_VALUE) * sec_name = NULL; + + if (argc != 2) { + printf("Usage: %s spki|x509\n", argv[0]); + exit(EXIT_SUCCESS); + } + + HIP_DEBUG("- This test tool has to be run as root otherwise this will fail!\n"); + HIP_DEBUG("- Hipd has to run otherwise this will hang!\n"); + + HIP_IFEL(!(msg = malloc(HIP_MAX_PACKET)), -1, + "Malloc for msg failed\n"); + defhit = malloc(sizeof(struct in6_addr)); + if (!defhit) { + goto out_err; + } + + if (strcmp(argv[1], "spki")) { + goto skip_spki; + } + + HIP_DEBUG("Starting to test SPKI certficate tools\n"); + + cert = malloc(sizeof(struct hip_cert_spki_info)); + if (!cert) { + goto out_err; + } + + to_verification = malloc(sizeof(struct hip_cert_spki_info)); + if (!to_verification) { + goto out_err; + } + + time(¬_before); + time(¬_after); + HIP_DEBUG("Reading configuration file (%s)\n", HIP_CERT_CONF_PATH); + conf = hip_cert_open_conf(); + sec = hip_cert_read_conf_section("hip_spki", conf); + + for (i = 0; i < sk_CONF_VALUE_num(sec); i++) { + item = (void *) sk_CONF_VALUE_value(sec, i); + _HIP_DEBUG("Sec: %s, Key; %s, Val %s\n", + item->section, item->name, item->value); + if (!strcmp(item->name, "issuerhit")) { + err = inet_pton(AF_INET6, item->value, defhit); + if (err < 1) { + err = -1; + goto out_err; + } + } + if (!strcmp(item->name, "days")) { + _HIP_DEBUG("Days in sec = %d\n", HIP_CERT_DAY * atoi(item->value)); + not_after += HIP_CERT_DAY * atoi(item->value); + } + } + hip_cert_free_conf(conf); + + hip_cert_spki_create_cert(cert, + "hit", defhit, + "hit", defhit, + ¬_before, + ¬_after); + + _HIP_DEBUG("\n\nPublic-key sequence contents after all is done:\n\n" + "%s\n\n", cert->public_key); + + _HIP_DEBUG("Cert sequence contents after all is done:\n\n" + "%s\n\n", cert->cert); + + _HIP_DEBUG("Signature sequence contents after all is done:\n\n" + "%s\n\n", cert->signature); + /* + * Concatenate everything together as if we would have gotten + * it from someone else and we would be starting to verify. + * + * So the process would be take the cert blob and take out + * public-key sequence, cert sequence and signature sequence + * and create a hip_cert_spki_info and send it to the daemon + * for verification. + */ + memset(&certificate, '\0', sizeof(certificate)); + sprintf((char *) &certificate, "(sequence %s%s%s)", + cert->public_key, + cert->cert, + cert->signature); + HIP_DEBUG("\n\nCertificate gotten back from daemon:\n\n" + "%s\n\nCertificate len %d\n\n", + certificate, + strlen(certificate)); + + compression_test((unsigned char *) certificate, strlen(certificate)); + + HIP_IFEL(hip_cert_spki_char2certinfo(certificate, to_verification), -1, + "Failed to construct the hip_cert_spki_info from certificate\n"); + + /* + * below, commented out, is the daemons version of the verification + * and below that is the lib version of the verification + */ + /* + * HIP_DEBUG("Sending the certificate to daemon for verification\n"); + * + * HIP_IFEL(hip_cert_spki_send_to_verification(to_verification), -1, + * "Failed in sending to verification\n"); + * HIP_IFEL(to_verification->success, -1, + * "Verification was not successfull\n"); + * HIP_DEBUG("Verification was successfull (return value %d)\n", + * to_verification->success); + */ + /* Lets do the verification in library */ + HIP_IFEL(hip_cert_spki_lib_verify(to_verification), -1, + "Verification was not succesfull\n"); + HIP_DEBUG("Verification was successfull (return value %d)\n", + to_verification->success); + + goto out_err; + skip_spki: - HIP_DEBUG("Starting to test x509v3 support\n"); - - conf = hip_cert_open_conf(); - sec_name = hip_cert_read_conf_section("hip_x509v3_name", conf); - - for (i = 0; i < sk_CONF_VALUE_num(sec_name); i++) { - item = (void*)sk_CONF_VALUE_value(sec_name, i); - _HIP_DEBUG("Sec: %s, Key; %s, Val %s\n", - item->section, item->name, item->value); - if (!strcmp(item->name, "issuerhit")) { - err = inet_pton(AF_INET6, item->value, defhit); - if (err < 1) { - err = -1; - goto out_err; - } - } - } - hip_cert_free_conf(conf); - len = hip_cert_x509v3_request_certificate(defhit, der_cert); - - compression_test(der_cert, len); - - /** Now send it back for the verification **/ - HIP_IFEL(((err = hip_cert_x509v3_request_verification(der_cert, len)) < 0), -1, - "Failed to verify a certificate\n"); - - out_err: - HIP_DEBUG("If there was no errors above, \"everything\" is OK\n"); - - if (cert) free(cert); - if (to_verification) free(to_verification); - exit(err); + HIP_DEBUG("Starting to test x509v3 support\n"); + + conf = hip_cert_open_conf(); + sec_name = hip_cert_read_conf_section("hip_x509v3_name", conf); + + for (i = 0; i < sk_CONF_VALUE_num(sec_name); i++) { + item = (void *) sk_CONF_VALUE_value(sec_name, i); + _HIP_DEBUG("Sec: %s, Key; %s, Val %s\n", + item->section, item->name, item->value); + if (!strcmp(item->name, "issuerhit")) { + err = inet_pton(AF_INET6, item->value, defhit); + if (err < 1) { + err = -1; + goto out_err; + } + } + } + hip_cert_free_conf(conf); + len = hip_cert_x509v3_request_certificate(defhit, der_cert); + + compression_test(der_cert, len); + + /** Now send it back for the verification **/ + HIP_IFEL(((err = hip_cert_x509v3_request_verification(der_cert, len)) < 0), + -1, "Failed to verify a certificate\n"); + +out_err: + HIP_DEBUG("If there was no errors above, \"everything\" is OK\n"); + + if (cert) { + free(cert); + } + if (to_verification) { + free(to_verification); + } + exit(err); } - === modified file 'test/conntest-client-hip.c' --- test/conntest-client-hip.c 2010-01-19 09:28:42 +0000 +++ test/conntest-client-hip.c 2010-02-10 23:43:06 +0000 @@ -9,8 +9,8 @@ #include <config.h> #endif -#include <stdio.h> -#include <stdlib.h> +#include <stdio.h> +#include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> @@ -30,136 +30,135 @@ /** * Main function. - * + * * @param argc command line argument count. * @param argv command line arguments. * @return EXIT_FAILURE on failure, EXIT_SUCCESS on success. */ -int main(int argc, char *argv[]) { - - int socktype = -1, err = 0; - const char *cfile = "default"; - char usage[100]; - char ping_help[512]; - in_port_t port = 0; - - sprintf(usage, "Usage: %s <host> tcp|udp <port>", argv[0]); - - hip_set_logtype(LOGTYPE_STDERR); - hip_set_logfmt(LOGFMT_SHORT); - hip_set_logdebug(LOGDEBUG_MEDIUM); - - HIP_IFEL(hip_set_auto_logdebug(cfile), -1, - "Error: Cannot set the debugging parameter.\n"); - - if(argc < 4) { - HIP_INFO("Not enough arguments.\n%s\n", usage); - return EXIT_FAILURE; - }else if(argc > 4) { - HIP_INFO("Too many arguments.\n%s\n", usage); - return EXIT_FAILURE; - } - - if (strcmp(argv[2], "tcp") == 0) { - socktype = SOCK_STREAM; - } else if (strcmp(argv[2], "udp") == 0) { - socktype = SOCK_DGRAM; - } else { - HIP_INFO("Invalid protocol: '%s'\n%s\n", argv[2], usage); - return EXIT_FAILURE; - } - - port = atoi(argv[3]); - - /* Disabled since this comparison is always true with the current - port number boundaries. - if(port < MINPORTNUM || port > MAXPORTNUM){ - HIP_INFO("Invalid port number, allowed port numbers are "\ - "from %d to %d.\n%s\n", MINPORTNUM, MAXPORTNUM, - usage); - return EXIT_FAILURE; - } - */ - - HIP_INFO("=== Testing %s connection to '%s' on port %s ===\n", - (socktype == SOCK_STREAM ? "TCP" : "UDP"), argv[1], - argv[3]); - - /* Call the main function to do the actual logic. */ - err = main_client_gai(socktype, argv[1], argv[3], 0); - - out_err: - if(err == 0) { - HIP_INFO("=== Connection test result: "\ - "\e[92mSUCCESS\e[00m ===\n"); - return EXIT_SUCCESS; - } else { - _HIP_DEBUG("err: %d, errno: %d .\n", err, errno); - - /* Get a help string for pinging etc. */ - sprintf(ping_help, "You can try the 'ping', 'ping6', "\ - "'traceroute' or 'traceroute6' programs to\n"\ - "track down the problem.\n"); - - /* Check our specially tailored 'err' values first. - getaddrinfo() returns an error value as defined in - /usr/include/netdb.h. We have stored that error value in - errno. */ - if(err == -EHADDRINFO) { - HIP_ERROR("Error when retrieving address information "\ - "for the peer.\n"); - if(errno == EAI_NONAME) { - HIP_ERROR("Connection refused.\nDo you have a "\ - "local HIP daemon up and running?\n"); - } else if(errno == EAI_AGAIN) { - HIP_ERROR("Temporary failure in name "\ - "resolution.\n"); - } - } else if(err == -EBADMSG) { - HIP_INFO("Error when communicating with the peer.\n"\ - "The peer is supposed to echo back the sent "\ - "data,\nbut the sent and received data do "\ - "not match.\n"); - } - /* Then move to errno handling. Note that the errno is set - in somewhat randomly in libinet6 functions and therefore - these error messages do not neccessarily hold. Well, better - than nothing... */ - else if(errno == ECONNREFUSED) { - HIP_ERROR("The peer was reached but it refused the "\ - "connection.\nThere is no one listening on "\ - "the remote address.\nIf you are trying to "\ - "establish a HIP connection,\nyou need both "\ - "a HIP daemon and a server running at the "\ - "other end.\nFor an IP connection you only "\ - "need a server running at the other end.\n"); - } else if(errno == ENOTSOCK) { - HIP_ERROR("Socket operation on non-socket.\n"\ - "Is the host you are trying to connect local"); - - } else if(errno == ENETUNREACH) { - HIP_ERROR("Network is unreachable.\n%s", ping_help); - } else if(errno == EBADF) { - HIP_ERROR("Bad file descriptor.\nThe file descriptor "\ - "used when trying to connect to the remote "\ - "host is not a\nvalid index in the "\ - "descriptor table.\n"); - } else if(errno == EAFNOSUPPORT) { - HIP_ERROR("Address family not supported by protocol.\n"\ - "Only IPv4, IPv6 and HIP address families "\ - "are supported.\nAre you trying to "\ - "communicate between processes on the same "\ - "machine?\n"); - } - /* Just to make sure we don't print 'success' when the - connection test has actually failed we check errno != 0. */ - else if (errno != 0) { - HIP_PERROR(""); - } - - HIP_INFO("=== Connection test result: "\ - "\e[91mFAILURE\e[00m ===\n"); - - return EXIT_FAILURE; - } +int main(int argc, char *argv[]) +{ + int socktype = -1, err = 0; + const char *cfile = "default"; + char usage[100]; + char ping_help[512]; + in_port_t port = 0; + + sprintf(usage, "Usage: %s <host> tcp|udp <port>", argv[0]); + + hip_set_logtype(LOGTYPE_STDERR); + hip_set_logfmt(LOGFMT_SHORT); + hip_set_logdebug(LOGDEBUG_MEDIUM); + + HIP_IFEL(hip_set_auto_logdebug(cfile), -1, + "Error: Cannot set the debugging parameter.\n"); + + if (argc < 4) { + HIP_INFO("Not enough arguments.\n%s\n", usage); + return EXIT_FAILURE; + } else if (argc > 4) { + HIP_INFO("Too many arguments.\n%s\n", usage); + return EXIT_FAILURE; + } + + if (strcmp(argv[2], "tcp") == 0) { + socktype = SOCK_STREAM; + } else if (strcmp(argv[2], "udp") == 0) { + socktype = SOCK_DGRAM; + } else { + HIP_INFO("Invalid protocol: '%s'\n%s\n", argv[2], usage); + return EXIT_FAILURE; + } + + port = atoi(argv[3]); + + /* Disabled since this comparison is always true with the current + * port number boundaries. + * if(port < MINPORTNUM || port > MAXPORTNUM){ + * HIP_INFO("Invalid port number, allowed port numbers are "\ + * "from %d to %d.\n%s\n", MINPORTNUM, MAXPORTNUM, + * usage); + * return EXIT_FAILURE; + * } + */ + + HIP_INFO("=== Testing %s connection to '%s' on port %s ===\n", + (socktype == SOCK_STREAM ? "TCP" : "UDP"), argv[1], + argv[3]); + + /* Call the main function to do the actual logic. */ + err = main_client_gai(socktype, argv[1], argv[3], 0); + +out_err: + if (err == 0) { + HIP_INFO("=== Connection test result: " \ + "\e[92mSUCCESS\e[00m ===\n"); + return EXIT_SUCCESS; + } else { + _HIP_DEBUG("err: %d, errno: %d .\n", err, errno); + + /* Get a help string for pinging etc. */ + sprintf(ping_help, "You can try the 'ping', 'ping6', " \ + "'traceroute' or 'traceroute6' programs to\n" \ + "track down the problem.\n"); + + /* Check our specially tailored 'err' values first. + * getaddrinfo() returns an error value as defined in + * /usr/include/netdb.h. We have stored that error value in + * errno. */ + if (err == -EHADDRINFO) { + HIP_ERROR("Error when retrieving address information " \ + "for the peer.\n"); + if (errno == EAI_NONAME) { + HIP_ERROR("Connection refused.\nDo you have a " \ + "local HIP daemon up and running?\n"); + } else if (errno == EAI_AGAIN) { + HIP_ERROR("Temporary failure in name " \ + "resolution.\n"); + } + } else if (err == -EBADMSG) { + HIP_INFO("Error when communicating with the peer.\n" \ + "The peer is supposed to echo back the sent " \ + "data,\nbut the sent and received data do " \ + "not match.\n"); + } + /* Then move to errno handling. Note that the errno is set + * in somewhat randomly in libinet6 functions and therefore + * these error messages do not neccessarily hold. Well, better + * than nothing... */ + else if (errno == ECONNREFUSED) { + HIP_ERROR("The peer was reached but it refused the " \ + "connection.\nThere is no one listening on " \ + "the remote address.\nIf you are trying to " \ + "establish a HIP connection,\nyou need both " \ + "a HIP daemon and a server running at the " \ + "other end.\nFor an IP connection you only " \ + "need a server running at the other end.\n"); + } else if (errno == ENOTSOCK) { + HIP_ERROR("Socket operation on non-socket.\n" \ + "Is the host you are trying to connect local"); + } else if (errno == ENETUNREACH) { + HIP_ERROR("Network is unreachable.\n%s", ping_help); + } else if (errno == EBADF) { + HIP_ERROR("Bad file descriptor.\nThe file descriptor " \ + "used when trying to connect to the remote " \ + "host is not a\nvalid index in the " \ + "descriptor table.\n"); + } else if (errno == EAFNOSUPPORT) { + HIP_ERROR("Address family not supported by protocol.\n" \ + "Only IPv4, IPv6 and HIP address families " \ + "are supported.\nAre you trying to " \ + "communicate between processes on the same " \ + "machine?\n"); + } + /* Just to make sure we don't print 'success' when the + * connection test has actually failed we check errno != 0. */ + else if (errno != 0) { + HIP_PERROR(""); + } + + HIP_INFO("=== Connection test result: " \ + "\e[91mFAILURE\e[00m ===\n"); + + return EXIT_FAILURE; + } } === modified file 'test/conntest-client-native-user-key.c' --- test/conntest-client-native-user-key.c 2010-01-19 09:28:42 +0000 +++ test/conntest-client-native-user-key.c 2010-02-10 23:43:06 +0000 @@ -18,8 +18,8 @@ #include <config.h> #endif -#include <stdio.h> -#include <stdlib.h> +#include <stdio.h> +#include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> @@ -36,170 +36,174 @@ #include "lib/core/ife.h" #include "lib/core/getendpointinfo.h" -int main(int argc,char *argv[]) { - struct addrinfo hints, *epinfo, *res = NULL; - struct sockaddr_eid my_eid; - struct timeval stats_before, stats_after; - unsigned long stats_diff_sec, stats_diff_usec; - char mylovemostdata[IP_MAXPACKET]; - char receiveddata[IP_MAXPACKET]; - char *type_name, *peer_port_name, *peer_name; - int recvnum, sendnum; - int datalen = 0; - int datasent = 0; - int datareceived = 0; - int ch; - int err = 0; - int sockfd = 0, socktype; - se_family_t endpoint_family; - char *user_key_base = "/etc/hip/hip_host_dsa_key"; - struct endpoint *endpoint; - const char *cfile = "default"; - - hip_set_logtype(LOGTYPE_STDERR); - hip_set_logfmt(LOGFMT_SHORT); - HIP_IFEL(hip_set_auto_logdebug(cfile), -1, - "Error: Cannot set the debugging parameter.\n"); - - - if (argc != 4) { - HIP_ERROR("Usage: %s host tcp|udp port\n", argv[0]); - err = 1; - goto out; - } - - peer_name = argv[1]; - type_name = argv[2]; - peer_port_name = argv[3]; - endpoint_family = PF_HIP; - - /* Set transport protocol */ - if (strcmp(type_name, "tcp") == 0) { - socktype = SOCK_STREAM; - } else if (strcmp(type_name, "udp") == 0) { - socktype = SOCK_DGRAM; - } else { - HIP_ERROR("Error: only TCP and UDP supported.\n"); - err = 1; - goto out; - } - - sockfd = socket(endpoint_family, socktype, 0); - if (sockfd == -1) { - HIP_ERROR("creation of socket failed\n"); - err = 1; - goto out; - } - - err = load_hip_endpoint_pem(user_key_base, &endpoint); - if (err) { - HIP_ERROR("Failed to load user HIP key %s\n", user_key_base); - goto out; - } - - err = setmyeid(&my_eid, "", endpoint, NULL); - if (err) { - HIP_ERROR("Failed to set up my EID (%d)\n", err); - err = 1; - goto out; - } - - /* We have to bind to the EID to use it. */ - err = bind(sockfd, (struct sockaddr *) &my_eid, sizeof(struct sockaddr_eid)); - if (err) { - HIP_PERROR("bind failed"); - goto out; - } - - /* set up endpoint lookup information */ - memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_socktype = socktype; - hints.ai_family = endpoint_family; - - /* Lookup endpoint. We do not need to call setpeereid because - getaddrinfo does it automatically. */ - err = getaddrinfo(peer_name, peer_port_name, &hints, &res); - if (err) { - HIP_ERROR("getaddrinfo failed (%d): %s\n", err, gepi_strerror(err)); - goto out; - } - - HIP_DEBUG("family=%d value=%d\n", res->ai_family, - ntohs(((struct sockaddr_eid *) res->ai_addr)->eid_val)); - - // data from stdin to buffer - bzero(receiveddata, IP_MAXPACKET); - bzero(mylovemostdata, IP_MAXPACKET); - - printf("Input some text, press enter and ctrl+d\n"); - - // horrible code - while ((ch = fgetc(stdin)) != EOF && (datalen < IP_MAXPACKET)) { - mylovemostdata[datalen] = (unsigned char) ch; - datalen++; - } - - gettimeofday(&stats_before, NULL); - - epinfo = res; - while(epinfo) { - err = connect(sockfd, epinfo->ai_addr, epinfo->ai_addrlen); - if (err) { - HIP_PERROR("connect"); - goto out; - } - epinfo = epinfo->ai_next; - } - - gettimeofday(&stats_after, NULL); - stats_diff_sec = (stats_after.tv_sec - stats_before.tv_sec) * 1000000; - stats_diff_usec = stats_after.tv_usec - stats_before.tv_usec; - - HIP_DEBUG("connect took %.10f sec\n", - (stats_diff_sec + stats_diff_usec) / 1000000.0); - - /* Send the data read from stdin to the server and read the response. - The server should echo all the data received back to here. */ - while((datasent < datalen) || (datareceived < datalen)) { - - if (datasent < datalen) { - sendnum = send(sockfd, mylovemostdata + datasent, datalen - datasent, 0); - - if (sendnum < 0) { - HIP_PERROR("send"); - err = 1; - goto out; - } - datasent += sendnum; - } - - if (datareceived < datalen) { - recvnum = recv(sockfd, receiveddata + datareceived, - datalen-datareceived, 0); - if (recvnum <= 0) { - HIP_PERROR("recv"); - err = 1; - goto out; - } - datareceived += recvnum; - } - } - - if (memcmp(mylovemostdata, receiveddata, IP_MAXPACKET)) { - HIP_ERROR("Sent and received data did not match\n"); - err = 1; - goto out; - } +int main(int argc, char *argv[]) +{ + struct addrinfo hints, *epinfo, *res = NULL; + struct sockaddr_eid my_eid; + struct timeval stats_before, stats_after; + unsigned long stats_diff_sec, stats_diff_usec; + char mylovemostdata[IP_MAXPACKET]; + char receiveddata[IP_MAXPACKET]; + char *type_name, *peer_port_name, *peer_name; + int recvnum, sendnum; + int datalen = 0; + int datasent = 0; + int datareceived = 0; + int ch; + int err = 0; + int sockfd = 0, socktype; + se_family_t endpoint_family; + char *user_key_base = "/etc/hip/hip_host_dsa_key"; + struct endpoint *endpoint; + const char *cfile = "default"; + + hip_set_logtype(LOGTYPE_STDERR); + hip_set_logfmt(LOGFMT_SHORT); + HIP_IFEL(hip_set_auto_logdebug(cfile), -1, + "Error: Cannot set the debugging parameter.\n"); + + + if (argc != 4) { + HIP_ERROR("Usage: %s host tcp|udp port\n", argv[0]); + err = 1; + goto out; + } + + peer_name = argv[1]; + type_name = argv[2]; + peer_port_name = argv[3]; + endpoint_family = PF_HIP; + + /* Set transport protocol */ + if (strcmp(type_name, "tcp") == 0) { + socktype = SOCK_STREAM; + } else if (strcmp(type_name, "udp") == 0) { + socktype = SOCK_DGRAM; + } else { + HIP_ERROR("Error: only TCP and UDP supported.\n"); + err = 1; + goto out; + } + + sockfd = socket(endpoint_family, socktype, 0); + if (sockfd == -1) { + HIP_ERROR("creation of socket failed\n"); + err = 1; + goto out; + } + + err = load_hip_endpoint_pem(user_key_base, &endpoint); + if (err) { + HIP_ERROR("Failed to load user HIP key %s\n", user_key_base); + goto out; + } + + err = setmyeid(&my_eid, "", endpoint, NULL); + if (err) { + HIP_ERROR("Failed to set up my EID (%d)\n", err); + err = 1; + goto out; + } + + /* We have to bind to the EID to use it. */ + err = bind(sockfd, (struct sockaddr *) &my_eid, + sizeof(struct sockaddr_eid)); + if (err) { + HIP_PERROR("bind failed"); + goto out; + } + + /* set up endpoint lookup information */ + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_socktype = socktype; + hints.ai_family = endpoint_family; + + /* Lookup endpoint. We do not need to call setpeereid because + * getaddrinfo does it automatically. */ + err = getaddrinfo(peer_name, peer_port_name, &hints, &res); + if (err) { + HIP_ERROR("getaddrinfo failed (%d): %s\n", err, gepi_strerror(err)); + goto out; + } + + HIP_DEBUG("family=%d value=%d\n", res->ai_family, + ntohs(((struct sockaddr_eid *) res->ai_addr)->eid_val)); + + // data from stdin to buffer + bzero(receiveddata, IP_MAXPACKET); + bzero(mylovemostdata, IP_MAXPACKET); + + printf("Input some text, press enter and ctrl+d\n"); + + // horrible code + while ((ch = fgetc(stdin)) != EOF && (datalen < IP_MAXPACKET)) { + mylovemostdata[datalen] = (unsigned char) ch; + datalen++; + } + + gettimeofday(&stats_before, NULL); + + epinfo = res; + while (epinfo) { + err = connect(sockfd, epinfo->ai_addr, epinfo->ai_addrlen); + if (err) { + HIP_PERROR("connect"); + goto out; + } + epinfo = epinfo->ai_next; + } + + gettimeofday(&stats_after, NULL); + stats_diff_sec = (stats_after.tv_sec - stats_before.tv_sec) * 1000000; + stats_diff_usec = stats_after.tv_usec - stats_before.tv_usec; + + HIP_DEBUG("connect took %.10f sec\n", + (stats_diff_sec + stats_diff_usec) / 1000000.0); + + /* Send the data read from stdin to the server and read the response. + * The server should echo all the data received back to here. */ + while ((datasent < datalen) || (datareceived < datalen)) { + if (datasent < datalen) { + sendnum = send(sockfd, mylovemostdata + datasent, + datalen - datasent, 0); + + if (sendnum < 0) { + HIP_PERROR("send"); + err = 1; + goto out; + } + datasent += sendnum; + } + + if (datareceived < datalen) { + recvnum = recv(sockfd, receiveddata + datareceived, + datalen - datareceived, 0); + if (recvnum <= 0) { + HIP_PERROR("recv"); + err = 1; + goto out; + } + datareceived += recvnum; + } + } + + if (memcmp(mylovemostdata, receiveddata, IP_MAXPACKET)) { + HIP_ERROR("Sent and received data did not match\n"); + err = 1; + goto out; + } out: - if (sockfd) - close(sockfd); // discard errors - if (res) - free_endpointinfo(res); + if (sockfd) { + close(sockfd); // discard errors + } + if (res) { + free_endpointinfo(res); + } - HIP_INFO("Result of data transfer: %s.\n", (err ? "FAIL" : "OK")); + HIP_INFO("Result of data transfer: %s.\n", (err ? "FAIL" : "OK")); out_err: - return err; + return err; } === modified file 'test/conntest-client-native.c' --- test/conntest-client-native.c 2010-01-19 09:28:42 +0000 +++ test/conntest-client-native.c 2010-02-10 23:43:06 +0000 @@ -19,8 +19,8 @@ #include <config.h> #endif -#include <stdio.h> -#include <stdlib.h> +#include <stdio.h> +#include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> @@ -38,41 +38,40 @@ #include "conntest.h" -int main(int argc,char *argv[]) { - char *type_name, *peer_port_name, *peer_name; - int socktype, err = 0; - const char *cfile = "default"; - - hip_set_logtype(LOGTYPE_STDERR); - //hip_set_logfmt(LOGFMT_SHORT); - HIP_IFEL(hip_set_auto_logdebug(cfile), -1, - "Error: Cannot set the debugging parameter.\n"); - - - if (argc != 4) { - HIP_ERROR("Usage: %s host tcp|udp port\n", argv[0]); - return(1); - } - - peer_name = argv[1]; - type_name = argv[2]; - peer_port_name = argv[3]; - - /* Set transport protocol */ - if (strcmp(type_name, "tcp") == 0) { - socktype = SOCK_STREAM; - } else if (strcmp(type_name, "udp") == 0) { - socktype = SOCK_DGRAM; - } else { - HIP_ERROR("Error: only TCP and UDP supported.\n"); - return(1); - } - - HIP_IFEL(main_client_native(socktype, peer_name, peer_port_name), -2, - "Error: Cannot set the client.\n"); - - out_err: - return err; - - +int main(int argc, char *argv[]) +{ + char *type_name, *peer_port_name, *peer_name; + int socktype, err = 0; + const char *cfile = "default"; + + hip_set_logtype(LOGTYPE_STDERR); + //hip_set_logfmt(LOGFMT_SHORT); + HIP_IFEL(hip_set_auto_logdebug(cfile), -1, + "Error: Cannot set the debugging parameter.\n"); + + + if (argc != 4) { + HIP_ERROR("Usage: %s host tcp|udp port\n", argv[0]); + return 1; + } + + peer_name = argv[1]; + type_name = argv[2]; + peer_port_name = argv[3]; + + /* Set transport protocol */ + if (strcmp(type_name, "tcp") == 0) { + socktype = SOCK_STREAM; + } else if (strcmp(type_name, "udp") == 0) { + socktype = SOCK_DGRAM; + } else { + HIP_ERROR("Error: only TCP and UDP supported.\n"); + return 1; + } + + HIP_IFEL(main_client_native(socktype, peer_name, peer_port_name), -2, + "Error: Cannot set the client.\n"); + +out_err: + return err; } === modified file 'test/conntest-client-opp.c' --- test/conntest-client-opp.c 2010-01-19 09:28:42 +0000 +++ test/conntest-client-opp.c 2010-02-10 23:43:06 +0000 @@ -8,8 +8,8 @@ #include <config.h> #endif -#include <stdio.h> -#include <stdlib.h> +#include <stdio.h> +#include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> @@ -24,45 +24,46 @@ #include "lib/core/ife.h" #include "conntest.h" -int main(int argc,char *argv[]) { - - int socktype, i, err = 0; - char *type_name, *peer_port_name, *peer_name; - const char *cfile = "default"; - - hip_set_logtype(LOGTYPE_STDERR); - hip_set_logfmt(LOGFMT_LONG); - HIP_IFEL(hip_set_auto_logdebug(cfile), -1, - "Error: Cannot set the debugging parameter.\n"); - - argc--; - if (argc % 3 || argc < 3) { - fprintf(stderr, "Usage: %s host tcp|udp port [host tcp|udp port...]\n", argv[0]); - exit(1); - } - - if (argc > 3) printf("Making %d connections...\n", argc / 3); - - for (i = 0; i < argc; i += 3) - { - peer_name = argv[i + 1]; - type_name = argv[i + 2]; - peer_port_name = argv[i + 3]; - - if (strcmp(type_name, "tcp") == 0) { - socktype = SOCK_STREAM; - } else if (strcmp(type_name, "udp") == 0) { - socktype = SOCK_DGRAM; - } else { - fprintf(stderr, "error: proto != tcp|udp\n"); - exit(1); - } - - HIP_IFEL(main_client_gai(socktype, peer_name, peer_port_name, - 0), -2,"Error: Cannot set the client.\n"); - } - - out_err: - return err; - +int main(int argc, char *argv[]) +{ + int socktype, i, err = 0; + char *type_name, *peer_port_name, *peer_name; + const char *cfile = "default"; + + hip_set_logtype(LOGTYPE_STDERR); + hip_set_logfmt(LOGFMT_LONG); + HIP_IFEL(hip_set_auto_logdebug(cfile), -1, + "Error: Cannot set the debugging parameter.\n"); + + argc--; + if (argc % 3 || argc < 3) { + fprintf(stderr, "Usage: %s host tcp|udp port [host tcp|udp port...]\n", + argv[0]); + exit(1); + } + + if (argc > 3) { + printf("Making %d connections...\n", argc / 3); + } + + for (i = 0; i < argc; i += 3) { + peer_name = argv[i + 1]; + type_name = argv[i + 2]; + peer_port_name = argv[i + 3]; + + if (strcmp(type_name, "tcp") == 0) { + socktype = SOCK_STREAM; + } else if (strcmp(type_name, "udp") == 0) { + socktype = SOCK_DGRAM; + } else { + fprintf(stderr, "error: proto != tcp|udp\n"); + exit(1); + } + + HIP_IFEL(main_client_gai(socktype, peer_name, peer_port_name, + 0), -2, "Error: Cannot set the client.\n"); + } + +out_err: + return err; } === modified file 'test/conntest-server-native.c' --- test/conntest-server-native.c 2010-01-19 09:28:42 +0000 +++ test/conntest-server-native.c 2010-02-10 23:43:06 +0000 @@ -2,7 +2,7 @@ * Echo server: get data from client and send it back. Use this with * conntest-client-native. * - * Bugs: + * Bugs: * - xx * * Todo: @@ -16,8 +16,8 @@ #include <config.h> #endif -#include <stdio.h> -#include <stdlib.h> +#include <stdio.h> +#include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> @@ -36,42 +36,43 @@ #include "lib/core/debug.h" #include "conntest.h" -static void sig_handler(int signo) { - if (signo == SIGTERM) { - // close socket - HIP_DIE("Sigterm\n"); - } else { - HIP_DIE("Signal %d\n", signo); - } +static void sig_handler(int signo) +{ + if (signo == SIGTERM) { + // close socket + HIP_DIE("Sigterm\n"); + } else { + HIP_DIE("Signal %d\n", signo); + } } -int main(int argc,char *argv[]) { - int socktype; - - hip_set_logtype(LOGTYPE_STDERR); - - if (signal(SIGTERM, sig_handler) == SIG_ERR) { - return(1); - } - - if (argc < 3 || argc > 4) { - HIP_ERROR("Usage: %s tcp|udp port [local_addr]\n", argv[0]); - return(1); - } - - if (strcmp(argv[1], "tcp") == 0) { - socktype = SOCK_STREAM; - } else if (strcmp(argv[1], "udp") == 0) { - socktype = SOCK_DGRAM; - } else { - HIP_ERROR("error: uknown socket type\n"); - return(1); - } - - if (argc == 3) { - return(main_server_native(socktype, argv[2], NULL)); - } else { - return(main_server_native(socktype, argv[2], argv[3])); - } - +int main(int argc, char *argv[]) +{ + int socktype; + + hip_set_logtype(LOGTYPE_STDERR); + + if (signal(SIGTERM, sig_handler) == SIG_ERR) { + return 1; + } + + if (argc < 3 || argc > 4) { + HIP_ERROR("Usage: %s tcp|udp port [local_addr]\n", argv[0]); + return 1; + } + + if (strcmp(argv[1], "tcp") == 0) { + socktype = SOCK_STREAM; + } else if (strcmp(argv[1], "udp") == 0) { + socktype = SOCK_DGRAM; + } else { + HIP_ERROR("error: uknown socket type\n"); + return 1; + } + + if (argc == 3) { + return main_server_native(socktype, argv[2], NULL); + } else { + return main_server_native(socktype, argv[2], argv[3]); + } } === modified file 'test/conntest-server.c' --- test/conntest-server.c 2009-12-11 22:49:11 +0000 +++ test/conntest-server.c 2010-02-10 23:43:06 +0000 @@ -2,7 +2,7 @@ * Get data from client and send it back (echo server). Use this with * conntest-client. * - * Bugs: + * Bugs: * - this is a kludge * * Todo: @@ -13,8 +13,8 @@ #include <config.h> #endif -#include <stdio.h> -#include <stdlib.h> +#include <stdio.h> +#include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> @@ -30,45 +30,46 @@ #include <signal.h> #include "conntest.h" -static void sig_handler(int signo) { - if (signo == SIGTERM) { - // close socket - HIP_DIE("Sigterm\n"); - } else { - HIP_DIE("Signal %d\n", signo); - } +static void sig_handler(int signo) +{ + if (signo == SIGTERM) { + // close socket + HIP_DIE("Sigterm\n"); + } else { + HIP_DIE("Signal %d\n", signo); + } } -int main(int argc,char *argv[]) { - - int port; - int type; - - if (signal(SIGTERM, sig_handler) == SIG_ERR) { - exit(1); - } - - if (argc != 3) { - fprintf(stderr, "Usage: %s tcp|udp port\n", argv[0]); - exit(1); - } - - if (strcmp(argv[1], "tcp") == 0) { - type = SOCK_STREAM; - } else if (strcmp(argv[1], "udp") == 0) { - type = SOCK_DGRAM; - } else { - fprintf(stderr, "error: protonum != tcp|udp\n"); - exit(1); - } - - port = atoi(argv[2]); - if (port <= 0 || port >= 65535) { - fprintf(stderr, "error: port < 0 || port > 65535\n"); - exit(1); - } - - main_server(type, port); - - return(0); +int main(int argc, char *argv[]) +{ + int port; + int type; + + if (signal(SIGTERM, sig_handler) == SIG_ERR) { + exit(1); + } + + if (argc != 3) { + fprintf(stderr, "Usage: %s tcp|udp port\n", argv[0]); + exit(1); + } + + if (strcmp(argv[1], "tcp") == 0) { + type = SOCK_STREAM; + } else if (strcmp(argv[1], "udp") == 0) { + type = SOCK_DGRAM; + } else { + fprintf(stderr, "error: protonum != tcp|udp\n"); + exit(1); + } + + port = atoi(argv[2]); + if (port <= 0 || port >= 65535) { + fprintf(stderr, "error: port < 0 || port > 65535\n"); + exit(1); + } + + main_server(type, port); + + return 0; } === modified file 'test/conntest.c' --- test/conntest.c 2010-01-19 09:28:42 +0000 +++ test/conntest.c 2010-02-10 23:43:06 +0000 @@ -10,381 +10,390 @@ * @return the socket id, * exits on error. */ -int create_serversocket(int type, in_port_t port) { - int fd = -1, on = 1, err = 0; - struct sockaddr_in6 addr; - - fd = socket(AF_INET6, type, 0); - if (fd < 0) { - perror("socket"); - err = -1; - goto out_err; - } - - /* Receive anchillary data with UDP */ - err = setsockopt(fd, IPPROTO_IPV6, - IPV6_2292PKTINFO, &on, sizeof(on)); - if (err != 0) { - perror("setsockopt IPV6_RECVPKTINFO"); - goto out_err; - } - - err = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); - if (err != 0) { - perror("setsockopt SO_REUSEADDR,"); - goto out_err; - } - - /* UDP cannot bind to both IPv4 and IPv6 */ - if (type == SOCK_DGRAM) { - err = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, - &on, sizeof(on)); - if (err != 0) { - perror("setsockopt IPV6_V6ONLY"); - goto out_err; - } - } - - memset(&addr, 0, sizeof(addr)); - addr.sin6_family = AF_INET6; - addr.sin6_port = htons(port); - addr.sin6_addr = in6addr_any; - addr.sin6_flowinfo = 0; - - if (bind(fd, (struct sockaddr *)&addr, - sizeof(struct sockaddr_in6)) < 0) { - perror("bind"); - err = -1; - goto out_err; - } - - if (type == SOCK_STREAM) { - if (listen(fd, 1) < 0) { - perror("listen"); - err = -1; - goto out_err; - } - } - -out_err: - if (err) { - close(fd); - fd = -1; - } - - return fd; -} - -int main_server_tcp(int serversock) { - int peerfd = 0, err = 0; - socklen_t locallen; - unsigned int peerlen; - struct sockaddr_in6 localaddr, peeraddr; - char mylovemostdata[IP_MAXPACKET]; - int recvnum, sendnum; - - peerlen = sizeof(struct sockaddr_in6); - - peerfd = accept(serversock, (struct sockaddr *)&peeraddr, &peerlen); - - if (peerfd < 0) { - perror("accept"); - err = -1; - goto out_err; - } - - locallen = sizeof(localaddr); - if (!getsockname(serversock, - (struct sockaddr *)&localaddr, - &locallen)) - HIP_DEBUG_HIT("local addr", &localaddr.sin6_addr); - HIP_DEBUG_HIT("peer addr", &peeraddr.sin6_addr); - - while((recvnum = recv(peerfd, mylovemostdata, - sizeof(mylovemostdata), 0)) > 0 ) { - mylovemostdata[recvnum] = '\0'; - printf("Client sends:\n%s", mylovemostdata); - fflush(stdout); - if (recvnum == 0) { - close(peerfd); - err = -1; - break; - } - - /* send reply */ - sendnum = send(peerfd, mylovemostdata, recvnum, 0); - if (sendnum < 0) { - perror("send"); - err = -1; - break; - } - printf("Client has been replied.\n"); - } - if (peerfd) - close(peerfd); - -out_err: - return err; -} - -int create_udp_ipv4_socket(in_port_t local_port) { - int ipv4_sock = -1, err = 0, on = 1; - struct sockaddr_in inaddr_any; - - /* IPv6 "server" sockets support incoming IPv4 packets with - IPv4-in-IPv6 format. However, outgoing packets with IPv4-in-IPv6 - formatted address stop working in some kernel version. Here - we create a socket for sending IPv4 packets. */ - ipv4_sock = socket(AF_INET, SOCK_DGRAM, 0); - if (ipv4_sock < 0) { - printf("ipv4 socket\n"); - err = -1; - goto out_err; - } - - err = setsockopt(ipv4_sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); - if (err != 0) { - perror("setsockopt SO_REUSEADDR"); - goto out_err; - } - - err = setsockopt(ipv4_sock, IPPROTO_IP, - IP_PKTINFO, &on, sizeof(on)); - if (err != 0) { - perror("setsockopt IP_PKTINFO"); - goto out_err; - } - - /* Weird, but we have to set this option to receive - IPv4 addresses for UDP. We don't get them in mapped format. */ - err = setsockopt(ipv4_sock, IPPROTO_IP, - IP_PKTINFO, &on, sizeof(on)); - if (err != 0) { - perror("setsockopt IP_PKTINFO"); - goto out_err; - } - - _HIP_DEBUG("my local port %d\n", local_port); - - inaddr_any.sin_family = AF_INET; - inaddr_any.sin_port = htons(local_port); - inaddr_any.sin_addr.s_addr = htonl(INADDR_ANY); - err = bind(ipv4_sock, (struct sockaddr *) &inaddr_any, - sizeof(inaddr_any)); - if (err) { - perror("bind\n"); - goto out_err; - } - -out_err: - if (err == 0) - return ipv4_sock; - else - return -1; - +int create_serversocket(int type, in_port_t port) +{ + int fd = -1, on = 1, err = 0; + struct sockaddr_in6 addr; + + fd = socket(AF_INET6, type, 0); + if (fd < 0) { + perror("socket"); + err = -1; + goto out_err; + } + + /* Receive anchillary data with UDP */ + err = setsockopt(fd, IPPROTO_IPV6, + IPV6_2292PKTINFO, &on, sizeof(on)); + if (err != 0) { + perror("setsockopt IPV6_RECVPKTINFO"); + goto out_err; + } + + err = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); + if (err != 0) { + perror("setsockopt SO_REUSEADDR,"); + goto out_err; + } + + /* UDP cannot bind to both IPv4 and IPv6 */ + if (type == SOCK_DGRAM) { + err = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, + &on, sizeof(on)); + if (err != 0) { + perror("setsockopt IPV6_V6ONLY"); + goto out_err; + } + } + + memset(&addr, 0, sizeof(addr)); + addr.sin6_family = AF_INET6; + addr.sin6_port = htons(port); + addr.sin6_addr = in6addr_any; + addr.sin6_flowinfo = 0; + + if (bind(fd, (struct sockaddr *) &addr, + sizeof(struct sockaddr_in6)) < 0) { + perror("bind"); + err = -1; + goto out_err; + } + + if (type == SOCK_STREAM) { + if (listen(fd, 1) < 0) { + perror("listen"); + err = -1; + goto out_err; + } + } + +out_err: + if (err) { + close(fd); + fd = -1; + } + + return fd; +} + +int main_server_tcp(int serversock) +{ + int peerfd = 0, err = 0; + socklen_t locallen; + unsigned int peerlen; + struct sockaddr_in6 localaddr, peeraddr; + char mylovemostdata[IP_MAXPACKET]; + int recvnum, sendnum; + + peerlen = sizeof(struct sockaddr_in6); + + peerfd = accept(serversock, (struct sockaddr *) &peeraddr, &peerlen); + + if (peerfd < 0) { + perror("accept"); + err = -1; + goto out_err; + } + + locallen = sizeof(localaddr); + if (!getsockname(serversock, + (struct sockaddr *) &localaddr, + &locallen)) { + HIP_DEBUG_HIT("local addr", &localaddr.sin6_addr); + } + HIP_DEBUG_HIT("peer addr", &peeraddr.sin6_addr); + + while ((recvnum = recv(peerfd, mylovemostdata, + sizeof(mylovemostdata), 0)) > 0) { + mylovemostdata[recvnum] = '\0'; + printf("Client sends:\n%s", mylovemostdata); + fflush(stdout); + if (recvnum == 0) { + close(peerfd); + err = -1; + break; + } + + /* send reply */ + sendnum = send(peerfd, mylovemostdata, recvnum, 0); + if (sendnum < 0) { + perror("send"); + err = -1; + break; + } + printf("Client has been replied.\n"); + } + if (peerfd) { + close(peerfd); + } + +out_err: + return err; +} + +int create_udp_ipv4_socket(in_port_t local_port) +{ + int ipv4_sock = -1, err = 0, on = 1; + struct sockaddr_in inaddr_any; + + /* IPv6 "server" sockets support incoming IPv4 packets with + * IPv4-in-IPv6 format. However, outgoing packets with IPv4-in-IPv6 + * formatted address stop working in some kernel version. Here + * we create a socket for sending IPv4 packets. */ + ipv4_sock = socket(AF_INET, SOCK_DGRAM, 0); + if (ipv4_sock < 0) { + printf("ipv4 socket\n"); + err = -1; + goto out_err; + } + + err = setsockopt(ipv4_sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); + if (err != 0) { + perror("setsockopt SO_REUSEADDR"); + goto out_err; + } + + err = setsockopt(ipv4_sock, IPPROTO_IP, + IP_PKTINFO, &on, sizeof(on)); + if (err != 0) { + perror("setsockopt IP_PKTINFO"); + goto out_err; + } + + /* Weird, but we have to set this option to receive + * IPv4 addresses for UDP. We don't get them in mapped format. */ + err = setsockopt(ipv4_sock, IPPROTO_IP, + IP_PKTINFO, &on, sizeof(on)); + if (err != 0) { + perror("setsockopt IP_PKTINFO"); + goto out_err; + } + + _HIP_DEBUG("my local port %d\n", local_port); + + inaddr_any.sin_family = AF_INET; + inaddr_any.sin_port = htons(local_port); + inaddr_any.sin_addr.s_addr = htonl(INADDR_ANY); + err = bind(ipv4_sock, + (struct sockaddr *) &inaddr_any, + sizeof(inaddr_any)); + if (err) { + perror("bind\n"); + goto out_err; + } + +out_err: + if (err == 0) { + return ipv4_sock; + } else { + return -1; + } } int udp_send_msg(int sock, uint8_t *data, size_t data_len, - struct sockaddr *local_addr, - struct sockaddr *peer_addr) { - int err = 0, sendnum; - int is_ipv4 = ((peer_addr->sa_family == AF_INET) ? 1 : 0); - uint8_t cmsgbuf[CMSG_SPACE(sizeof(struct in_pktinfo))]; - struct cmsghdr *cmsg; // = (struct cmsghdr *) cmsgbuf; - struct msghdr msg; - struct iovec iov; - union { - struct in_pktinfo *in4; - struct inet6_pktinfo *in6; - } pktinfo; - - /* The first memset is mandatory. Results in otherwise weird - EMSGSIZE errors. */ - memset(&msg, 0, sizeof(struct msghdr)); - memset(cmsgbuf, 0, sizeof(cmsgbuf)); - - /* Fill message header */ - - msg.msg_name = peer_addr; - if (is_ipv4) - msg.msg_namelen = sizeof(struct sockaddr_in); - else - msg.msg_namelen = sizeof(struct sockaddr_in6); - msg.msg_iov = &iov; - msg.msg_iovlen = 1; - msg.msg_control = cmsgbuf; - msg.msg_controllen = sizeof(cmsgbuf); - msg.msg_flags = 0; - - iov.iov_base = data; - iov.iov_len = data_len; - - /* Set local address */ - cmsg = CMSG_FIRSTHDR(&msg); - if (is_ipv4) - cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo)); - else - cmsg->cmsg_len = CMSG_LEN(sizeof(struct inet6_pktinfo)); - cmsg->cmsg_level = (is_ipv4) ? IPPROTO_IP : IPPROTO_IPV6; - cmsg->cmsg_type = (is_ipv4) ? IP_PKTINFO : IPV6_2292PKTINFO; - - pktinfo.in6 = (struct inet6_pktinfo *) CMSG_DATA(cmsg); - if (is_ipv4) - pktinfo.in4->ipi_addr.s_addr = - ((struct sockaddr_in *) local_addr)->sin_addr.s_addr; - else - memcpy(&pktinfo.in6->ipi6_addr, - &(((struct sockaddr_in6 *) local_addr)->sin6_addr), - sizeof(struct in6_addr)); - - /* Send reply using the ORIGINAL src/dst address pair */ - sendnum = sendmsg(sock, &msg, 0); - if (sendnum < 0) { - perror("sendmsg"); - err = -1; - goto out_err; - } - - printf("=== Sent string successfully back ===\n"); - printf("=== Server listening IN6ADDR_ANY ===\n"); + struct sockaddr *local_addr, + struct sockaddr *peer_addr) +{ + int err = 0, sendnum; + int is_ipv4 = ((peer_addr->sa_family == AF_INET) ? 1 : 0); + uint8_t cmsgbuf[CMSG_SPACE(sizeof(struct in_pktinfo))]; + struct cmsghdr *cmsg; // = (struct cmsghdr *) cmsgbuf; + struct msghdr msg; + struct iovec iov; + union { + struct in_pktinfo * in4; + struct inet6_pktinfo *in6; + } pktinfo; + + /* The first memset is mandatory. Results in otherwise weird + * EMSGSIZE errors. */ + memset(&msg, 0, sizeof(struct msghdr)); + memset(cmsgbuf, 0, sizeof(cmsgbuf)); + + /* Fill message header */ + + msg.msg_name = peer_addr; + if (is_ipv4) { + msg.msg_namelen = sizeof(struct sockaddr_in); + } else { + msg.msg_namelen = sizeof(struct sockaddr_in6); + } + msg.msg_iov = &iov; + msg.msg_iovlen = 1; + msg.msg_control = cmsgbuf; + msg.msg_controllen = sizeof(cmsgbuf); + msg.msg_flags = 0; + + iov.iov_base = data; + iov.iov_len = data_len; + + /* Set local address */ + cmsg = CMSG_FIRSTHDR(&msg); + if (is_ipv4) { + cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo)); + } else { + cmsg->cmsg_len = CMSG_LEN(sizeof(struct inet6_pktinfo)); + } + cmsg->cmsg_level = (is_ipv4) ? IPPROTO_IP : IPPROTO_IPV6; + cmsg->cmsg_type = (is_ipv4) ? IP_PKTINFO : IPV6_2292PKTINFO; + + pktinfo.in6 = (struct inet6_pktinfo *) CMSG_DATA(cmsg); + if (is_ipv4) { + pktinfo.in4->ipi_addr.s_addr = + ((struct sockaddr_in *) local_addr)->sin_addr.s_addr; + } else { + memcpy(&pktinfo.in6->ipi6_addr, + &(((struct sockaddr_in6 *) local_addr)->sin6_addr), + sizeof(struct in6_addr)); + } + + /* Send reply using the ORIGINAL src/dst address pair */ + sendnum = sendmsg(sock, &msg, 0); + if (sendnum < 0) { + perror("sendmsg"); + err = -1; + goto out_err; + } + + printf("=== Sent string successfully back ===\n"); + printf("=== Server listening IN6ADDR_ANY ===\n"); out_err: - return err; + return err; } -int main_server_udp(int ipv4_sock, int ipv6_sock, in_port_t local_port) { - /* Use recvmsg/sendmsg instead of recvfrom/sendto because - the latter combination may choose a different source - HIT for the server */ - int err = 0, recvnum, is_ipv4 = 0; - int cmsg_level, cmsg_type, highest_descriptor = -1; - fd_set read_fdset; - union { - struct sockaddr_in in4; - struct sockaddr_in6 in6; - } peer_addr, local_addr; - uint8_t cmsgbuf[CMSG_SPACE(sizeof(struct inet6_pktinfo))]; - uint8_t mylovemostdata[IP_MAXPACKET]; - struct iovec iov; - struct cmsghdr *cmsg = (struct cmsghdr *) cmsgbuf; - union { - struct in_pktinfo *in4; - struct inet6_pktinfo *in6; - } pktinfo; - struct msghdr msg; - - FD_ZERO(&read_fdset); - FD_SET(ipv4_sock, &read_fdset); - FD_SET(ipv6_sock, &read_fdset); - highest_descriptor = maxof(2, ipv4_sock, ipv6_sock); - - printf("=== Server listening INADDR_ANY/IN6ADDR_ANY ===\n"); - - while(select((highest_descriptor + 1), &read_fdset, - NULL, NULL, NULL)) { - - /* XX FIXME: receiving two packets at the same time */ - - if (FD_ISSET(ipv4_sock, &read_fdset)) { - is_ipv4 = 1; - //FD_CLR(ipv4_sock, &read_fdset); - } else if (FD_ISSET(ipv6_sock, &read_fdset)) { - is_ipv4 = 0; - //FD_CLR(ipv6_sock, &read_fdset); - } else { - printf("Unhandled select event\n"); - goto reset; - } - - msg.msg_name = &peer_addr.in6; - msg.msg_namelen = sizeof(struct sockaddr_in6); - msg.msg_iov = &iov; - msg.msg_iovlen = 1; - msg.msg_control = cmsgbuf; - msg.msg_controllen = sizeof(cmsgbuf); - msg.msg_flags = 0; - - iov.iov_base = mylovemostdata; - iov.iov_len = sizeof(mylovemostdata); - - memset(mylovemostdata, 0, sizeof(mylovemostdata)); - memset(&peer_addr, 0, sizeof(peer_addr)); - memset(cmsgbuf, 0, sizeof(cmsgbuf)); - - recvnum = recvmsg((is_ipv4 ? ipv4_sock : ipv6_sock), &msg, 0); - if (recvnum < 0) { - perror("recvmsg\n"); - goto reset; - } - printf("Received %d bytes\n", recvnum); - - //is_ipv4 = IN6_IS_ADDR_V4MAPPED(&peer_addr.in6.sin6_addr); - - cmsg_level = (is_ipv4) ? IPPROTO_IP : IPPROTO_IPV6; - cmsg_type = (is_ipv4) ? IP_PKTINFO : IPV6_2292PKTINFO; - - /* Local address comes from ancillary data passed - * with msg due to IPV6_PKTINFO socket option */ - for (cmsg=CMSG_FIRSTHDR(&msg); cmsg; - cmsg=CMSG_NXTHDR(&msg,cmsg)){ - if ((cmsg->cmsg_level == cmsg_level) && - (cmsg->cmsg_type == cmsg_type)) { - /* The structure is a union, so this fills - also the pktinfo_in6 pointer */ - pktinfo.in4 = - (struct in_pktinfo *)CMSG_DATA(cmsg); - break; - } - } - - if (is_ipv4) { - local_addr.in4.sin_family = AF_INET; - local_addr.in4.sin_port = htons(local_port); - //local_addr.in4.sin_port = peer_addr.in6.sin6_port; - local_addr.in4.sin_addr.s_addr = - pktinfo.in4->ipi_addr.s_addr; - HIP_DEBUG_INADDR("local addr", - &local_addr.in4.sin_addr); - HIP_DEBUG("local port %d\n", - ntohs(local_addr.in4.sin_port)); - HIP_DEBUG_INADDR("peer addr", - &peer_addr.in4.sin_addr); - HIP_DEBUG("peer port %d\n", - ntohs(peer_addr.in4.sin_port)); - - } else { - local_addr.in6.sin6_family = AF_INET6; - memcpy(&local_addr.in6.sin6_addr, - &pktinfo.in6->ipi6_addr, - sizeof(struct in6_addr)); - local_addr.in6.sin6_port = htons(local_port); - HIP_DEBUG_IN6ADDR("local addr", - &local_addr.in6.sin6_addr); - HIP_DEBUG("local port %d\n", - ntohs(local_addr.in6.sin6_port)); - HIP_DEBUG_IN6ADDR("peer addr", - &peer_addr.in6.sin6_addr); - HIP_DEBUG("peer port %d\n", - ntohs(peer_addr.in6.sin6_port)); - } - - err = udp_send_msg((is_ipv4 ? ipv4_sock : ipv6_sock), - mylovemostdata, recvnum, - (struct sockaddr *) &local_addr, - (struct sockaddr *) &peer_addr); - if (err) { - printf("Failed to echo data back\n"); - } - - reset: - - FD_ZERO(&read_fdset); - FD_SET(ipv4_sock, &read_fdset); - FD_SET(ipv6_sock, &read_fdset); - } - - return err; +int main_server_udp(int ipv4_sock, int ipv6_sock, in_port_t local_port) +{ + /* Use recvmsg/sendmsg instead of recvfrom/sendto because + * the latter combination may choose a different source + * HIT for the server */ + int err = 0, recvnum, is_ipv4 = 0; + int cmsg_level, cmsg_type, highest_descriptor = -1; + fd_set read_fdset; + union { + struct sockaddr_in in4; + struct sockaddr_in6 in6; + } peer_addr, local_addr; + uint8_t cmsgbuf[CMSG_SPACE(sizeof(struct inet6_pktinfo))]; + uint8_t mylovemostdata[IP_MAXPACKET]; + struct iovec iov; + struct cmsghdr *cmsg = (struct cmsghdr *) cmsgbuf; + union { + struct in_pktinfo * in4; + struct inet6_pktinfo *in6; + } pktinfo; + struct msghdr msg; + + FD_ZERO(&read_fdset); + FD_SET(ipv4_sock, &read_fdset); + FD_SET(ipv6_sock, &read_fdset); + highest_descriptor = maxof(2, ipv4_sock, ipv6_sock); + + printf("=== Server listening INADDR_ANY/IN6ADDR_ANY ===\n"); + + while (select((highest_descriptor + 1), &read_fdset, + NULL, NULL, NULL)) { + /* XX FIXME: receiving two packets at the same time */ + + if (FD_ISSET(ipv4_sock, &read_fdset)) { + is_ipv4 = 1; + //FD_CLR(ipv4_sock, &read_fdset); + } else if (FD_ISSET(ipv6_sock, &read_fdset)) { + is_ipv4 = 0; + //FD_CLR(ipv6_sock, &read_fdset); + } else { + printf("Unhandled select event\n"); + goto reset; + } + + msg.msg_name = &peer_addr.in6; + msg.msg_namelen = sizeof(struct sockaddr_in6); + msg.msg_iov = &iov; + msg.msg_iovlen = 1; + msg.msg_control = cmsgbuf; + msg.msg_controllen = sizeof(cmsgbuf); + msg.msg_flags = 0; + + iov.iov_base = mylovemostdata; + iov.iov_len = sizeof(mylovemostdata); + + memset(mylovemostdata, 0, sizeof(mylovemostdata)); + memset(&peer_addr, 0, sizeof(peer_addr)); + memset(cmsgbuf, 0, sizeof(cmsgbuf)); + + recvnum = recvmsg((is_ipv4 ? ipv4_sock : ipv6_sock), &msg, 0); + if (recvnum < 0) { + perror("recvmsg\n"); + goto reset; + } + printf("Received %d bytes\n", recvnum); + + //is_ipv4 = IN6_IS_ADDR_V4MAPPED(&peer_addr.in6.sin6_addr); + + cmsg_level = (is_ipv4) ? IPPROTO_IP : IPPROTO_IPV6; + cmsg_type = (is_ipv4) ? IP_PKTINFO : IPV6_2292PKTINFO; + + /* Local address comes from ancillary data passed + * with msg due to IPV6_PKTINFO socket option */ + for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; + cmsg = CMSG_NXTHDR(&msg, cmsg)) { + if ((cmsg->cmsg_level == cmsg_level) && + (cmsg->cmsg_type == cmsg_type)) { + /* The structure is a union, so this fills + * also the pktinfo_in6 pointer */ + pktinfo.in4 = + (struct in_pktinfo *) CMSG_DATA(cmsg); + break; + } + } + + if (is_ipv4) { + local_addr.in4.sin_family = AF_INET; + local_addr.in4.sin_port = htons(local_port); + //local_addr.in4.sin_port = peer_addr.in6.sin6_port; + local_addr.in4.sin_addr.s_addr = + pktinfo.in4->ipi_addr.s_addr; + HIP_DEBUG_INADDR("local addr", + &local_addr.in4.sin_addr); + HIP_DEBUG("local port %d\n", + ntohs(local_addr.in4.sin_port)); + HIP_DEBUG_INADDR("peer addr", + &peer_addr.in4.sin_addr); + HIP_DEBUG("peer port %d\n", + ntohs(peer_addr.in4.sin_port)); + } else { + local_addr.in6.sin6_family = AF_INET6; + memcpy(&local_addr.in6.sin6_addr, + &pktinfo.in6->ipi6_addr, + sizeof(struct in6_addr)); + local_addr.in6.sin6_port = htons(local_port); + HIP_DEBUG_IN6ADDR("local addr", + &local_addr.in6.sin6_addr); + HIP_DEBUG("local port %d\n", + ntohs(local_addr.in6.sin6_port)); + HIP_DEBUG_IN6ADDR("peer addr", + &peer_addr.in6.sin6_addr); + HIP_DEBUG("peer port %d\n", + ntohs(peer_addr.in6.sin6_port)); + } + + err = udp_send_msg((is_ipv4 ? ipv4_sock : ipv6_sock), + mylovemostdata, recvnum, + (struct sockaddr *) &local_addr, + (struct sockaddr *) &peer_addr); + if (err) { + printf("Failed to echo data back\n"); + } + +reset: + + FD_ZERO(&read_fdset); + FD_SET(ipv4_sock, &read_fdset); + FD_SET(ipv6_sock, &read_fdset); + } + + return err; } /** @@ -398,39 +407,42 @@ */ int main_server(int type, in_port_t port) { - int ipv6_sock = 0, err = 0, ipv4_sock = -1; - - ipv6_sock = create_serversocket(type, port); - if (ipv6_sock < 0) - err = -1; - - /* Create a separate IPv4 socket for receiving and sending UDP - packets even though a single IPv6 socket could be used - for receiving IPv4 packets, but not sending them. */ - if (type == SOCK_DGRAM) { - ipv4_sock = create_udp_ipv4_socket(port); - if (ipv4_sock < 0) { - printf("Could not create ipv4 socket\n"); - err = -1; - goto out_err; - } - } - - while(err == 0) { - if (type == SOCK_STREAM) { - err = main_server_tcp(ipv6_sock); - } else { - err = main_server_udp(ipv4_sock, ipv6_sock, port); - } - } + int ipv6_sock = 0, err = 0, ipv4_sock = -1; + + ipv6_sock = create_serversocket(type, port); + if (ipv6_sock < 0) { + err = -1; + } + + /* Create a separate IPv4 socket for receiving and sending UDP + * packets even though a single IPv6 socket could be used + * for receiving IPv4 packets, but not sending them. */ + if (type == SOCK_DGRAM) { + ipv4_sock = create_udp_ipv4_socket(port); + if (ipv4_sock < 0) { + printf("Could not create ipv4 socket\n"); + err = -1; + goto out_err; + } + } + + while (err == 0) { + if (type == SOCK_STREAM) { + err = main_server_tcp(ipv6_sock); + } else { + err = main_server_udp(ipv4_sock, ipv6_sock, port); + } + } out_err: - if (ipv6_sock) - close(ipv6_sock); - if (ipv4_sock < 0) - close(ipv4_sock); - return err; + if (ipv6_sock) { + close(ipv6_sock); + } + if (ipv4_sock < 0) { + close(ipv4_sock); + } + return err; } /** @@ -449,125 +461,126 @@ */ int hip_connect_func(struct addrinfo *peer_ai, int *sock) { - int err = 0, connect_err = 0; - unsigned long microseconds = 0; - struct timeval stats_before, stats_after; - char ip_str[INET6_ADDRSTRLEN]; - struct addrinfo *ai = NULL; - struct in_addr *ipv4 = NULL; - struct in6_addr *ipv6 = NULL; - - /* Set the memory allocated from the stack to zeros. */ - memset(&stats_before, 0, sizeof(stats_before)); - memset(&stats_after, 0, sizeof(stats_after)); - memset(ip_str, 0, sizeof(ip_str)); - - /* Loop through every address in the address info. */ - for(ai = peer_ai; ai != NULL; ai = ai->ai_next) { - if (ai->ai_family == AF_INET) - _HIP_DEBUG("AF_INET\n"); - else - _HIP_DEBUG("af_inet6\n"); - } - for(ai = peer_ai; ai != NULL; ai = ai->ai_next) { - ipv4 = &((struct sockaddr_in *)ai->ai_addr)->sin_addr; - ipv6 = &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr; - - /* Check the type of address we are connecting to and print - information about the address to the user. If address is - not supported the move to next address in peer_ai. */ - if (ai->ai_family == AF_INET) { - inet_ntop(AF_INET, ipv4, ip_str, sizeof(ip_str)); - - if(IS_LSI32(ipv4->s_addr)) { - HIP_INFO("Connecting to LSI %s.\n", ip_str); - } else { - HIP_INFO("Connecting to IPv4 address %s.\n", - ip_str); - } - } else if(ai->ai_family == AF_INET6 || - ai->ai_family == AF_HIP) { - inet_ntop(AF_INET6, ipv6, ip_str, sizeof(ip_str)); - - if(ipv6_addr_is_hit(ipv6)){ - HIP_INFO("Connecting to HIT %s.\n", ip_str); - } else if (IN6_IS_ADDR_V4MAPPED(ipv6)) { - HIP_INFO("Connecting to IPv6-mapped IPv4 "\ - "address %s.\n", ip_str); - } else { - HIP_INFO("Connecting to IPv6 address %s.\n", - ip_str); - } - } else { - _HIP_DEBUG("Trying to connect to a non-inet address "\ - "family address. Skipping.\n"); - /* If there are no more address in peer_ai, these err - and errno values are returned. */ - errno = EAFNOSUPPORT; - err = -1; - continue; - } - - err = 0; - errno = 0; - - /* Get a socket for sending. */ - *sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); - - if(*sock < 3) { - HIP_ERROR("Unable to get a socket for sending.\n"); - err = -1; - goto out_err; - } - - gettimeofday(&stats_before, NULL); - connect_err = connect(*sock, ai->ai_addr, ai->ai_addrlen); - - /* If we're unable to connect to the remote address we try next - address in peer_ai. We back off if the closing of the socket - fails. */ - if(connect_err != 0){ - _HIP_ERROR("Unable to connect to the remote address.\n"); - if(close(*sock) != 0) { - HIP_ERROR("Unable to close a socket.\n"); - err = -1; - break; - } - *sock = 0; - err = -1; - continue; - } - - gettimeofday(&stats_after, NULL); - - microseconds = - ((stats_after.tv_sec - stats_before.tv_sec) * 1000000) - + (stats_after.tv_usec - stats_before.tv_usec); - - printf("Connecting socket to remote socket address took "\ - "%.5f seconds.\n", microseconds / 1000000.0 ); - - if (connect_err != 0) { - if(close(*sock) != 0) { - HIP_ERROR("Unable to close a socket.\n"); - err = -1; - break; - } - *sock = 0; - /* Try the next address in peer_ai. */ - continue; - } else { - /* Connect succeeded and data can be sent/received. */ - break; - } - } - - out_err: - return err; + int err = 0, connect_err = 0; + unsigned long microseconds = 0; + struct timeval stats_before, stats_after; + char ip_str[INET6_ADDRSTRLEN]; + struct addrinfo *ai = NULL; + struct in_addr *ipv4 = NULL; + struct in6_addr *ipv6 = NULL; + + /* Set the memory allocated from the stack to zeros. */ + memset(&stats_before, 0, sizeof(stats_before)); + memset(&stats_after, 0, sizeof(stats_after)); + memset(ip_str, 0, sizeof(ip_str)); + + /* Loop through every address in the address info. */ + for (ai = peer_ai; ai != NULL; ai = ai->ai_next) { + if (ai->ai_family == AF_INET) { + _HIP_DEBUG("AF_INET\n"); + } else { + _HIP_DEBUG("af_inet6\n"); + } + } + for (ai = peer_ai; ai != NULL; ai = ai->ai_next) { + ipv4 = &((struct sockaddr_in *) ai->ai_addr)->sin_addr; + ipv6 = &((struct sockaddr_in6 *) ai->ai_addr)->sin6_addr; + + /* Check the type of address we are connecting to and print + * information about the address to the user. If address is + * not supported the move to next address in peer_ai. */ + if (ai->ai_family == AF_INET) { + inet_ntop(AF_INET, ipv4, ip_str, sizeof(ip_str)); + + if (IS_LSI32(ipv4->s_addr)) { + HIP_INFO("Connecting to LSI %s.\n", ip_str); + } else { + HIP_INFO("Connecting to IPv4 address %s.\n", + ip_str); + } + } else if (ai->ai_family == AF_INET6 || + ai->ai_family == AF_HIP) { + inet_ntop(AF_INET6, ipv6, ip_str, sizeof(ip_str)); + + if (ipv6_addr_is_hit(ipv6)) { + HIP_INFO("Connecting to HIT %s.\n", ip_str); + } else if (IN6_IS_ADDR_V4MAPPED(ipv6)) { + HIP_INFO("Connecting to IPv6-mapped IPv4 " \ + "address %s.\n", ip_str); + } else { + HIP_INFO("Connecting to IPv6 address %s.\n", + ip_str); + } + } else { + _HIP_DEBUG("Trying to connect to a non-inet address " \ + "family address. Skipping.\n"); + /* If there are no more address in peer_ai, these err + * and errno values are returned. */ + errno = EAFNOSUPPORT; + err = -1; + continue; + } + + err = 0; + errno = 0; + + /* Get a socket for sending. */ + *sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); + + if (*sock < 3) { + HIP_ERROR("Unable to get a socket for sending.\n"); + err = -1; + goto out_err; + } + + gettimeofday(&stats_before, NULL); + connect_err = connect(*sock, ai->ai_addr, ai->ai_addrlen); + + /* If we're unable to connect to the remote address we try next + * address in peer_ai. We back off if the closing of the socket + * fails. */ + if (connect_err != 0) { + _HIP_ERROR("Unable to connect to the remote address.\n"); + if (close(*sock) != 0) { + HIP_ERROR("Unable to close a socket.\n"); + err = -1; + break; + } + *sock = 0; + err = -1; + continue; + } + + gettimeofday(&stats_after, NULL); + + microseconds = + ((stats_after.tv_sec - stats_before.tv_sec) * 1000000) + + (stats_after.tv_usec - stats_before.tv_usec); + + printf("Connecting socket to remote socket address took " \ + "%.5f seconds.\n", microseconds / 1000000.0 ); + + if (connect_err != 0) { + if (close(*sock) != 0) { + HIP_ERROR("Unable to close a socket.\n"); + err = -1; + break; + } + *sock = 0; + /* Try the next address in peer_ai. */ + continue; + } else { + /* Connect succeeded and data can be sent/received. */ + break; + } + } + +out_err: + return err; } /** - * Does the logic of the "conntest-client-gai" command line utility. + * Does the logic of the "conntest-client-gai" command line utility. * * @param socktype the type of socket (SOCK_STREAM or SOCK_DGRAM) * @param peer_name the host name of the peer as read from the command lien @@ -580,135 +593,135 @@ */ int main_client_gai(int socktype, char *peer_name, char *port_name, int flags) { - int recvnum = 0, sendnum = 0, datalen = 0, bytes_sent = 0; - int bytes_received = 0, c = 0, sock = 0, err = 0; - char sendbuffer[IP_MAXPACKET], receivebuffer[IP_MAXPACKET]; - unsigned long microseconds = 0; - struct addrinfo search_key, *peer_ai = NULL; - struct timeval stats_before, stats_after; - - /* Set the memory allocated from the stack to zeros. */ - memset(&search_key, 0, sizeof(search_key)); - memset(&stats_before, 0, sizeof(stats_before)); - memset(&stats_after, 0, sizeof(stats_after)); - memset(sendbuffer, 0, sizeof(sendbuffer)); - memset(receivebuffer, 0, sizeof(receivebuffer)); - - /* Fill in the socket address structure to host and service name. */ - search_key.ai_flags = flags; - - /* If peer_name is not specified the destination is looked in the - hadb. (?) */ - if (peer_name == NULL) - search_key.ai_flags |= AI_KERNEL_LIST; - - /* Legacy API supports only HIT-in-IPv6 */ - search_key.ai_family = AF_UNSPEC; - search_key.ai_socktype = socktype; - - /* Get the peer's address info. Set a generic -EHADDRINFO for */ - if (getaddrinfo(peer_name, port_name, &search_key, &peer_ai)) { - err = -EHADDRINFO; - printf("Name '%s' or service '%s' is unknown.\n", - peer_name, port_name); - goto out_err; - } - - printf("Please input some text to be sent to '%s'.\n"\ - "Empty row or \"CTRL+d\" sends data.\n", peer_name); - - /* Read user input from the standard input. */ - while((c = getc(stdin)) != EOF && (datalen < IP_MAXPACKET)) - { - datalen++; - if((sendbuffer[datalen-1] = c) == '\n'){ - /* First character is a newlinefeed. */ - if(datalen == 1){ - break; - } - c = getc(stdin); - if(c == '\n' || c == EOF){ - break; - } else { - ungetc(c, stdin); - } - } - } - - if(datalen == 0) { - printf("No input data given.\nRunning plain connection test "\ - "with no payload data exchange.\n"); - } - - /* Get a socket for sending and receiving data. */ - if ( (err = hip_connect_func(peer_ai, &sock)) ) { - printf("Failed to connect.\n"); - goto out_err; - } - - gettimeofday(&stats_before, NULL); - - if(datalen > 0) { - /* Send and receive data from the socket. */ - while((bytes_sent < datalen) || (bytes_received < datalen)) { - /* send() returns the number of bytes sent or negative - on error. */ - if (bytes_sent < datalen) { - HIP_IFEL(((sendnum = - send(sock, sendbuffer + bytes_sent, - datalen - bytes_sent, 0)) < 0), - -ECOMM, "Communication error on send.\n"); - bytes_sent += sendnum; - } - - /* recv() returns the number of bytes sent, negative - on error or zero when the peer has performed an - orderly shutdown. */ - if (bytes_received < datalen) { - recvnum = recv(sock, - receivebuffer + bytes_received, - datalen - bytes_received, 0); - - if (recvnum == 0) { - HIP_INFO("The peer has performed an "\ - "orderly shutdown.\n"); - goto out_err; - } else if(recvnum < 0) { - err = -ENODATA; - HIP_ERROR("Communication error on "\ - "receive.\n"); - } - - bytes_received += recvnum; - } - } - } - - gettimeofday(&stats_after, NULL); - - microseconds = - ((stats_after.tv_sec - stats_before.tv_sec) * 1000000) - + (stats_after.tv_usec - stats_before.tv_usec); - - printf("Data exchange took %.5f seconds.\n", - microseconds / 1000000.0 ); - - printf("Sent/received %d/%d bytes payload data to/from '%s'.\n", - bytes_sent, bytes_received, peer_name); - - if (memcmp(sendbuffer, receivebuffer, IP_MAXPACKET) != 0) { - err = -EBADMSG; - } - - out_err: - if (peer_ai != NULL) { - freeaddrinfo(peer_ai); - } - if (sock > 0) { - close(sock); - } - - return err; + int recvnum = 0, sendnum = 0, datalen = 0, bytes_sent = 0; + int bytes_received = 0, c = 0, sock = 0, err = 0; + char sendbuffer[IP_MAXPACKET], receivebuffer[IP_MAXPACKET]; + unsigned long microseconds = 0; + struct addrinfo search_key, *peer_ai = NULL; + struct timeval stats_before, stats_after; + + /* Set the memory allocated from the stack to zeros. */ + memset(&search_key, 0, sizeof(search_key)); + memset(&stats_before, 0, sizeof(stats_before)); + memset(&stats_after, 0, sizeof(stats_after)); + memset(sendbuffer, 0, sizeof(sendbuffer)); + memset(receivebuffer, 0, sizeof(receivebuffer)); + + /* Fill in the socket address structure to host and service name. */ + search_key.ai_flags = flags; + + /* If peer_name is not specified the destination is looked in the + * hadb. (?) */ + if (peer_name == NULL) { + search_key.ai_flags |= AI_KERNEL_LIST; + } + + /* Legacy API supports only HIT-in-IPv6 */ + search_key.ai_family = AF_UNSPEC; + search_key.ai_socktype = socktype; + + /* Get the peer's address info. Set a generic -EHADDRINFO for */ + if (getaddrinfo(peer_name, port_name, &search_key, &peer_ai)) { + err = -EHADDRINFO; + printf("Name '%s' or service '%s' is unknown.\n", + peer_name, port_name); + goto out_err; + } + + printf("Please input some text to be sent to '%s'.\n" \ + "Empty row or \"CTRL+d\" sends data.\n", peer_name); + + /* Read user input from the standard input. */ + while ((c = getc(stdin)) != EOF && (datalen < IP_MAXPACKET)) { + datalen++; + if ((sendbuffer[datalen - 1] = c) == '\n') { + /* First character is a newlinefeed. */ + if (datalen == 1) { + break; + } + c = getc(stdin); + if (c == '\n' || c == EOF) { + break; + } else { + ungetc(c, stdin); + } + } + } + + if (datalen == 0) { + printf("No input data given.\nRunning plain connection test " \ + "with no payload data exchange.\n"); + } + + /* Get a socket for sending and receiving data. */ + if ((err = hip_connect_func(peer_ai, &sock))) { + printf("Failed to connect.\n"); + goto out_err; + } + + gettimeofday(&stats_before, NULL); + + if (datalen > 0) { + /* Send and receive data from the socket. */ + while ((bytes_sent < datalen) || (bytes_received < datalen)) { + /* send() returns the number of bytes sent or negative + * on error. */ + if (bytes_sent < datalen) { + HIP_IFEL(((sendnum = + send(sock, sendbuffer + bytes_sent, + datalen - bytes_sent, 0)) < 0), + -ECOMM, "Communication error on send.\n"); + bytes_sent += sendnum; + } + + /* recv() returns the number of bytes sent, negative + * on error or zero when the peer has performed an + * orderly shutdown. */ + if (bytes_received < datalen) { + recvnum = recv(sock, + receivebuffer + bytes_received, + datalen - bytes_received, 0); + + if (recvnum == 0) { + HIP_INFO("The peer has performed an " \ + "orderly shutdown.\n"); + goto out_err; + } else if (recvnum < 0) { + err = -ENODATA; + HIP_ERROR("Communication error on " \ + "receive.\n"); + } + + bytes_received += recvnum; + } + } + } + + gettimeofday(&stats_after, NULL); + + microseconds = + ((stats_after.tv_sec - stats_before.tv_sec) * 1000000) + + (stats_after.tv_usec - stats_before.tv_usec); + + printf("Data exchange took %.5f seconds.\n", + microseconds / 1000000.0 ); + + printf("Sent/received %d/%d bytes payload data to/from '%s'.\n", + bytes_sent, bytes_received, peer_name); + + if (memcmp(sendbuffer, receivebuffer, IP_MAXPACKET) != 0) { + err = -EBADMSG; + } + +out_err: + if (peer_ai != NULL) { + freeaddrinfo(peer_ai); + } + if (sock > 0) { + close(sock); + } + + return err; } /** @@ -722,102 +735,101 @@ */ int main_client_native(int socktype, char *peer_name, char *peer_port_name) { - //struct endpointinfo hints, *epinfo = NULL, *res = NULL; - //struct endpointinfo *epinfo; - //struct addrinfo hints, *res = NULL; - struct timeval stats_before, stats_after; - struct sockaddr_hip peer_sock; - unsigned long stats_diff_sec, stats_diff_usec; - char mylovemostdata[IP_MAXPACKET]; - char receiveddata[IP_MAXPACKET]; - int recvnum, sendnum; - int datalen = 0; - int datasent = 0; - int datareceived = 0; - int ch; - int err = 0; - int sockfd = -1; - se_family_t endpoint_family; - - endpoint_family = PF_HIP; - - sockfd = socket(endpoint_family, socktype, 0); - HIP_IFEL(sockfd < 0, 1, "creation of socket failed\n"); - - /* XX TODO: Do a proper getaddrinfo() */ - memset(&peer_sock, 0, sizeof(peer_sock)); - peer_sock.ship_family = PF_HIP; - HIP_IFEL(inet_pton(AF_INET6, peer_name, &peer_sock.ship_hit) != 1, 1, "Failed to parse HIT\n"); - peer_sock.ship_port = htons(atoi(peer_port_name)); - HIP_DEBUG("Connecting to %s port %d\n", peer_name, peer_sock.ship_port); - - // data from stdin to buffer - memset(receiveddata, 0, IP_MAXPACKET); - memset(mylovemostdata, 0, IP_MAXPACKET); - - printf("Input some text, press enter and ctrl+d\n"); - - // horrible code - while ((ch = fgetc(stdin)) != EOF && (datalen < IP_MAXPACKET)) { - mylovemostdata[datalen] = (unsigned char) ch; - datalen++; - } - - gettimeofday(&stats_before, NULL); - - err = connect(sockfd, (struct sockaddr *)&peer_sock, sizeof(peer_sock)); - if (err) { - HIP_PERROR("connect: "); - goto out_err; - } - - gettimeofday(&stats_after, NULL); - stats_diff_sec = (stats_after.tv_sec - stats_before.tv_sec) * 1000000; - stats_diff_usec = stats_after.tv_usec - stats_before.tv_usec; - - HIP_DEBUG("connect took %.10f sec\n", - (stats_diff_sec + stats_diff_usec) / 1000000.0); - - /* Send the data read from stdin to the server and read the response. - The server should echo all the data received back to here. */ - while((datasent < datalen) || (datareceived < datalen)) { - - if (datasent < datalen) { - sendnum = send(sockfd, mylovemostdata + datasent, datalen - datasent, 0); - - if (sendnum < 0) { - HIP_PERROR("send"); - err = 1; - goto out_err; - } - datasent += sendnum; - } - - if (datareceived < datalen) { - recvnum = recv(sockfd, receiveddata + datareceived, - datalen-datareceived, 0); - if (recvnum <= 0) { - HIP_PERROR("recv"); - err = 1; - goto out_err; - } - datareceived += recvnum; - } - } - - HIP_IFEL(memcmp(mylovemostdata, receiveddata, IP_MAXPACKET), - 1, "Sent and received data did not match\n"); + //struct endpointinfo hints, *epinfo = NULL, *res = NULL; + //struct endpointinfo *epinfo; + //struct addrinfo hints, *res = NULL; + struct timeval stats_before, stats_after; + struct sockaddr_hip peer_sock; + unsigned long stats_diff_sec, stats_diff_usec; + char mylovemostdata[IP_MAXPACKET]; + char receiveddata[IP_MAXPACKET]; + int recvnum, sendnum; + int datalen = 0; + int datasent = 0; + int datareceived = 0; + int ch; + int err = 0; + int sockfd = -1; + se_family_t endpoint_family; + + endpoint_family = PF_HIP; + + sockfd = socket(endpoint_family, socktype, 0); + HIP_IFEL(sockfd < 0, 1, "creation of socket failed\n"); + + /* XX TODO: Do a proper getaddrinfo() */ + memset(&peer_sock, 0, sizeof(peer_sock)); + peer_sock.ship_family = PF_HIP; + HIP_IFEL(inet_pton(AF_INET6, peer_name, &peer_sock.ship_hit) != 1, 1, "Failed to parse HIT\n"); + peer_sock.ship_port = htons(atoi(peer_port_name)); + HIP_DEBUG("Connecting to %s port %d\n", peer_name, peer_sock.ship_port); + + // data from stdin to buffer + memset(receiveddata, 0, IP_MAXPACKET); + memset(mylovemostdata, 0, IP_MAXPACKET); + + printf("Input some text, press enter and ctrl+d\n"); + + // horrible code + while ((ch = fgetc(stdin)) != EOF && (datalen < IP_MAXPACKET)) { + mylovemostdata[datalen] = (unsigned char) ch; + datalen++; + } + + gettimeofday(&stats_before, NULL); + + err = connect(sockfd, (struct sockaddr *) &peer_sock, sizeof(peer_sock)); + if (err) { + HIP_PERROR("connect: "); + goto out_err; + } + + gettimeofday(&stats_after, NULL); + stats_diff_sec = (stats_after.tv_sec - stats_before.tv_sec) * 1000000; + stats_diff_usec = stats_after.tv_usec - stats_before.tv_usec; + + HIP_DEBUG("connect took %.10f sec\n", + (stats_diff_sec + stats_diff_usec) / 1000000.0); + + /* Send the data read from stdin to the server and read the response. + * The server should echo all the data received back to here. */ + while ((datasent < datalen) || (datareceived < datalen)) { + if (datasent < datalen) { + sendnum = send(sockfd, mylovemostdata + datasent, datalen - datasent, 0); + + if (sendnum < 0) { + HIP_PERROR("send"); + err = 1; + goto out_err; + } + datasent += sendnum; + } + + if (datareceived < datalen) { + recvnum = recv(sockfd, receiveddata + datareceived, + datalen - datareceived, 0); + if (recvnum <= 0) { + HIP_PERROR("recv"); + err = 1; + goto out_err; + } + datareceived += recvnum; + } + } + + HIP_IFEL(memcmp(mylovemostdata, receiveddata, IP_MAXPACKET), + 1, "Sent and received data did not match\n"); out_err: - /*if (res) - //free_endpointinfo(res); - freeaddrinfo(res);*/ - if (sockfd > -1) - close(sockfd); // discard errors - - HIP_INFO("Result of data transfer: %s.\n", (err ? "FAIL" : "OK")); - - return err; + /*if (res) + * //free_endpointinfo(res); + * freeaddrinfo(res);*/ + if (sockfd > -1) { + close(sockfd); // discard errors + } + HIP_INFO("Result of data transfer: %s.\n", (err ? "FAIL" : "OK")); + + return err; } /** @@ -829,127 +841,132 @@ */ int main_server_native(int socktype, char *port_name, char *name) { - struct addrinfo hints, *res = NULL; - struct sockaddr_hip our_sockaddr, peer_sock; - char mylovemostdata[IP_MAXPACKET]; - int recvnum, sendnum, serversock = 0, sockfd = 0, err = 0, on = 1; - int endpoint_family = PF_HIP; - socklen_t peer_eid_len = sizeof(struct sockaddr_hip); - - /* recvmsg() stuff for UDP multihoming */ - char control[CMSG_SPACE(40)]; - struct cmsghdr *cmsg; - struct inet6_pktinfo *pktinfo = NULL; - struct iovec iov = { &mylovemostdata, sizeof(mylovemostdata) - 1 }; - struct msghdr msg = { &peer_sock, sizeof(peer_sock), &iov, 1, - &control, sizeof(control), 0 }; - - serversock = socket(endpoint_family, socktype, 0); - if (serversock < 0) { - HIP_PERROR("socket: "); - err = 1; - goto out_err; - } - - setsockopt(serversock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); - if (socktype == SOCK_DGRAM) - setsockopt(serversock, IPPROTO_IPV6, IPV6_2292PKTINFO, &on, sizeof(on)); - - memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_family = endpoint_family; - hints.ai_socktype = socktype; - - HIP_DEBUG("Native server calls getendpointinfo\n"); - - err = getendpointinfo(NULL, port_name, &hints, &res); - if (err || !res) { - HIP_ERROR("Resolving of peer identifiers failed (%d)\n", err); - goto out_err; - } - - memset(&our_sockaddr, 0, sizeof(struct sockaddr_hip)); - if (name) { - HIP_IFEL(inet_pton(AF_INET6, name, &our_sockaddr.ship_hit) != 1, - 1, "Failed to parse HIT\n"); - } - our_sockaddr.ship_port = htons(atoi(port_name)); - HIP_DEBUG("Binding to port %d\n", ntohs(our_sockaddr.ship_port)); - our_sockaddr.ship_family = endpoint_family; - - if (bind(serversock, (struct sockaddr *)&our_sockaddr, sizeof(struct sockaddr_hip)) < 0) { - HIP_PERROR("bind: "); - err = 1; - goto out_err; - } - - HIP_DEBUG("Native server calls listen\n"); - - if (socktype == SOCK_STREAM && listen(serversock, 1) < 0) { - HIP_PERROR("listen: "); - err = 1; - goto out_err; - } - - HIP_DEBUG("Native server waits connection request\n"); - - while(1) { - if (socktype == SOCK_STREAM) { - sockfd = accept(serversock, (struct sockaddr *) &peer_sock, - &peer_eid_len); - if (sockfd < 0) { - HIP_PERROR("accept: "); - err = 1; - goto out_err; - } - - while((recvnum = recv(sockfd, mylovemostdata, - sizeof(mylovemostdata), 0)) > 0 ) { - mylovemostdata[recvnum] = '\0'; - printf("%s", mylovemostdata); - fflush(stdout); - - sendnum = send(sockfd, mylovemostdata, recvnum, 0); - if (sendnum < 0) { - HIP_PERROR("send: "); - err = 1; - goto out_err; - } - } - } else { /* UDP */ - sockfd = serversock; - serversock = 0; - while((recvnum = recvmsg(sockfd, &msg, 0)) > 0) { - for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { - if (cmsg->cmsg_level == IPPROTO_IPV6 && - cmsg->cmsg_type == IPV6_2292PKTINFO) { - pktinfo = (struct inet6_pktinfo *)CMSG_DATA(cmsg); - break; - } - } - HIP_DEBUG_HIT("localaddr", &pktinfo->ipi6_addr); - iov.iov_len = strlen(mylovemostdata); - - /* ancillary data contains the src - * and dst addresses */ - sendnum = sendmsg(sockfd, &msg, 0); - if (sendnum < 0) { - HIP_PERROR("sendto: "); - err = 1; - goto out_err; - } - } - } - } + struct addrinfo hints, *res = NULL; + struct sockaddr_hip our_sockaddr, peer_sock; + char mylovemostdata[IP_MAXPACKET]; + int recvnum, sendnum, serversock = 0, sockfd = 0, err = 0, on = 1; + int endpoint_family = PF_HIP; + socklen_t peer_eid_len = sizeof(struct sockaddr_hip); + + /* recvmsg() stuff for UDP multihoming */ + char control[CMSG_SPACE(40)]; + struct cmsghdr *cmsg; + struct inet6_pktinfo *pktinfo = NULL; + struct iovec iov = {&mylovemostdata, + sizeof(mylovemostdata) - 1}; + struct msghdr msg = {&peer_sock, sizeof(peer_sock), &iov, 1, + &control, sizeof(control),0}; + + serversock = socket(endpoint_family, socktype, 0); + if (serversock < 0) { + HIP_PERROR("socket: "); + err = 1; + goto out_err; + } + + setsockopt(serversock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); + if (socktype == SOCK_DGRAM) { + setsockopt(serversock, IPPROTO_IPV6, IPV6_2292PKTINFO, &on, sizeof(on)); + } + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = endpoint_family; + hints.ai_socktype = socktype; + + HIP_DEBUG("Native server calls getendpointinfo\n"); + + err = getendpointinfo(NULL, port_name, &hints, &res); + if (err || !res) { + HIP_ERROR("Resolving of peer identifiers failed (%d)\n", err); + goto out_err; + } + + memset(&our_sockaddr, 0, sizeof(struct sockaddr_hip)); + if (name) { + HIP_IFEL(inet_pton(AF_INET6, name, &our_sockaddr.ship_hit) != 1, + 1, "Failed to parse HIT\n"); + } + our_sockaddr.ship_port = htons(atoi(port_name)); + HIP_DEBUG("Binding to port %d\n", ntohs(our_sockaddr.ship_port)); + our_sockaddr.ship_family = endpoint_family; + + if (bind(serversock, (struct sockaddr *) &our_sockaddr, + sizeof(struct sockaddr_hip)) < 0) { + HIP_PERROR("bind: "); + err = 1; + goto out_err; + } + + HIP_DEBUG("Native server calls listen\n"); + + if (socktype == SOCK_STREAM && listen(serversock, 1) < 0) { + HIP_PERROR("listen: "); + err = 1; + goto out_err; + } + + HIP_DEBUG("Native server waits connection request\n"); + + while (1) { + if (socktype == SOCK_STREAM) { + sockfd = accept(serversock, (struct sockaddr *) &peer_sock, + &peer_eid_len); + if (sockfd < 0) { + HIP_PERROR("accept: "); + err = 1; + goto out_err; + } + + while ((recvnum = recv(sockfd, mylovemostdata, + sizeof(mylovemostdata), 0)) > 0) { + mylovemostdata[recvnum] = '\0'; + printf("%s", mylovemostdata); + fflush(stdout); + + sendnum = send(sockfd, mylovemostdata, recvnum, 0); + if (sendnum < 0) { + HIP_PERROR("send: "); + err = 1; + goto out_err; + } + } + } else { /* UDP */ + sockfd = serversock; + serversock = 0; + while ((recvnum = recvmsg(sockfd, &msg, 0)) > 0) { + for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { + if (cmsg->cmsg_level == IPPROTO_IPV6 && + cmsg->cmsg_type == IPV6_2292PKTINFO) { + pktinfo = (struct inet6_pktinfo *) CMSG_DATA(cmsg); + break; + } + } + HIP_DEBUG_HIT("localaddr", &pktinfo->ipi6_addr); + iov.iov_len = strlen(mylovemostdata); + + /* ancillary data contains the src + * and dst addresses */ + sendnum = sendmsg(sockfd, &msg, 0); + if (sendnum < 0) { + HIP_PERROR("sendto: "); + err = 1; + goto out_err; + } + } + } + } out_err: - if (res) - free_endpointinfo(res); - - if (sockfd) - close(sockfd); // discard errors - if (serversock) - close(serversock); // discard errors - - return err; + if (res) { + free_endpointinfo(res); + } + + if (sockfd) { + close(sockfd); // discard errors + } + if (serversock) { + close(serversock); // discard errors + } + return err; } === modified file 'test/conntest.h' --- test/conntest.h 2010-01-19 09:28:42 +0000 +++ test/conntest.h 2010-02-10 23:43:06 +0000 @@ -1,8 +1,8 @@ #ifndef CONNTEST_H #define CONNTEST_H -#include <stdio.h> -#include <stdlib.h> +#include <stdio.h> +#include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> @@ -17,15 +17,17 @@ #include "lib/core/debug.h" #include "lib/core/ife.h" #include "lib/core/misc.h" - + int create_socket(int proto); int create_serversocket(int type, in_port_t port); int main_server(int type, in_port_t port); int main_server_tcp(int serversock); int main_server_native(int socktype, char *port_name, char *name); -int hip_connect_func(struct addrinfo *res, int *sock);//, const char* filename); -int main_client_gai(int socktype, char *peer_name, char *peer_port_name, int hints); +int hip_connect_func(struct addrinfo *res, int *sock); +int main_client_gai(int socktype, char *peer_name, + char *peer_port_name, + int hints); int main_client_native(int socktype, char *peer_name, char *peer_port_name); #endif /* CONNTEST_H */ === modified file 'test/cookietest.c' --- test/cookietest.c 2010-01-19 09:28:42 +0000 +++ test/cookietest.c 2010-02-10 23:43:06 +0000 @@ -4,87 +4,89 @@ #include <time.h> void hip_create_puzzle(struct hip_puzzle *puzzle, uint8_t val_K, - uint32_t opaque, uint64_t random_i) { - /* only the random_j_k is in host byte order */ - puzzle->K = val_K; - puzzle->lifetime = 0; - puzzle->opaque[0] = opaque & 0xFF; - puzzle->opaque[1] = (opaque & 0xFF00) >> 8; - /* puzzle.opaque[2] = (opaque & 0xFF0000) >> 16; */ - puzzle->I = random_i; + uint32_t opaque, uint64_t random_i) +{ + /* only the random_j_k is in host byte order */ + puzzle->K = val_K; + puzzle->lifetime = 0; + puzzle->opaque[0] = opaque & 0xFF; + puzzle->opaque[1] = (opaque & 0xFF00) >> 8; + /* puzzle.opaque[2] = (opaque & 0xFF0000) >> 16; */ + puzzle->I = random_i; } int hip_verify_puzzle(struct hip_common *hdr, struct hip_puzzle *puzzle, - struct hip_solution *solution) { - int err = 1; /* Not really an error: 1=success, 0=failure */ - - if (solution->K != puzzle->K) { - HIP_INFO("Solution's K (%d) does not match sent K (%d)\n", - solution->K, puzzle->K); - - HIP_IFEL(solution->K != puzzle->K, 0, - "Solution's K did not match any sent Ks.\n"); - HIP_IFEL(solution->I != puzzle->I, 0, - "Solution's I did not match the sent I\n"); - HIP_IFEL(memcmp(solution->opaque, puzzle->opaque, HIP_PUZZLE_OPAQUE_LEN), 0, - "Solution's opaque data does not match sent opaque data\n"); - HIP_DEBUG("Received solution to an old puzzle\n"); - - } else { - HIP_HEXDUMP("solution", solution, sizeof(*solution)); - HIP_HEXDUMP("puzzle", puzzle, sizeof(*puzzle)); - HIP_IFEL(solution->I != puzzle->I, 0, - "Solution's I did not match the sent I\n"); - HIP_IFEL(memcmp(solution->opaque, puzzle->opaque, - HIP_PUZZLE_OPAQUE_LEN), 0, - "Solution's opaque data does not match the opaque data sent\n"); - } - HIP_IFEL(!hip_solve_puzzle(solution, hdr, HIP_VERIFY_PUZZLE), 0, - "Puzzle incorrectly solved\n"); - out_err: - return err; - + struct hip_solution *solution) +{ + int err = 1; /* Not really an error: 1=success, 0=failure */ + + if (solution->K != puzzle->K) { + HIP_INFO("Solution's K (%d) does not match sent K (%d)\n", + solution->K, puzzle->K); + + HIP_IFEL(solution->K != puzzle->K, 0, + "Solution's K did not match any sent Ks.\n"); + HIP_IFEL(solution->I != puzzle->I, 0, + "Solution's I did not match the sent I\n"); + HIP_IFEL(memcmp(solution->opaque, puzzle->opaque, + HIP_PUZZLE_OPAQUE_LEN), 0, + "Solution's opaque data does not match sent opaque data\n"); + HIP_DEBUG("Received solution to an old puzzle\n"); + } else { + HIP_HEXDUMP("solution", solution, sizeof(*solution)); + HIP_HEXDUMP("puzzle", puzzle, sizeof(*puzzle)); + HIP_IFEL(solution->I != puzzle->I, 0, + "Solution's I did not match the sent I\n"); + HIP_IFEL(memcmp(solution->opaque, puzzle->opaque, + HIP_PUZZLE_OPAQUE_LEN), 0, + "Solution's opaque data does not match the opaque data sent\n"); + } + HIP_IFEL(!hip_solve_puzzle(solution, hdr, HIP_VERIFY_PUZZLE), 0, + "Puzzle incorrectly solved\n"); +out_err: + return err; } -int main(int argc, char *argv[]) { - struct hip_puzzle pz; - struct hip_solution sol; - struct hip_common hdr = { 0 }; - struct timeval stats_before, stats_after, stats_res; - uint64_t solved_puzzle; - uint8_t k; - - if (argc != 2) { - printf("usage: cookietest k\n"); - exit(-1); - } - - k = atoi(argv[1]); - HIP_DEBUG("k=%d\n", k); - - hip_create_puzzle(&pz, k, 0, 0); - - gettimeofday(&stats_before, NULL); - - if ((solved_puzzle = - hip_solve_puzzle(&pz, &hdr, HIP_SOLVE_PUZZLE)) == 0) { - HIP_ERROR("Puzzle not solved\n"); - } - - gettimeofday(&stats_after, NULL); - - hip_timeval_diff(&stats_after, &stats_before, &stats_res); - HIP_INFO("puzzle solved in %ld.%06ld secs\n", - stats_res.tv_sec, stats_res.tv_usec); - - memcpy(&sol, &pz, sizeof(pz)); - sol.J = solved_puzzle; - - if (!hip_verify_puzzle(&hdr, &pz, &sol)) { - HIP_ERROR("Verifying of puzzle failed\n"); - } - - HIP_DEBUG("Puzzle solved correctly\n"); - - return 0; +int main(int argc, char *argv[]) +{ + struct hip_puzzle pz; + struct hip_solution sol; + struct hip_common hdr = { 0 }; + struct timeval stats_before, stats_after, stats_res; + uint64_t solved_puzzle; + uint8_t k; + + if (argc != 2) { + printf("usage: cookietest k\n"); + exit(-1); + } + + k = atoi(argv[1]); + HIP_DEBUG("k=%d\n", k); + + hip_create_puzzle(&pz, k, 0, 0); + + gettimeofday(&stats_before, NULL); + + if ((solved_puzzle = + hip_solve_puzzle(&pz, &hdr, HIP_SOLVE_PUZZLE)) == 0) { + HIP_ERROR("Puzzle not solved\n"); + } + + gettimeofday(&stats_after, NULL); + + hip_timeval_diff(&stats_after, &stats_before, &stats_res); + HIP_INFO("puzzle solved in %ld.%06ld secs\n", + stats_res.tv_sec, stats_res.tv_usec); + + memcpy(&sol, &pz, sizeof(pz)); + sol.J = solved_puzzle; + + if (!hip_verify_puzzle(&hdr, &pz, &sol)) { + HIP_ERROR("Verifying of puzzle failed\n"); + } + + HIP_DEBUG("Puzzle solved correctly\n"); + + return 0; } === modified file 'test/dh_performance.c' --- test/dh_performance.c 2010-02-08 15:18:06 +0000 +++ test/dh_performance.c 2010-02-10 23:43:06 +0000 @@ -1,23 +1,23 @@ -/*! +/*! * \file dh_performance.c - * + * * \brief Performance testfile that can be used for benchmarking a system. * \author Tobias Heer - * - * This is file provides an executable that can be run to benchmark a HIP system. - * It provides detailed information about the runtime of certain cryptographic + * + * This is file provides an executable that can be run to benchmark a HIP system. + * It provides detailed information about the runtime of certain cryptographic * operations. - * + * * \note Distributed under <a href="http://www.gnu.org/licenses/gpl.txt";>GNU/GPL</a>. - * - */ + * + */ #include <unistd.h> -#include <stdio.h> /* printf & co */ -#include <stdlib.h> /* exit & co */ -#include <openssl/dh.h> /* open ssl library for DH operations */ -#include <openssl/sha.h> /* open ssl library for SHA operations */ -#include <openssl/dsa.h> /* open ssl library for DSA operations */ +#include <stdio.h> /* printf & co */ +#include <stdlib.h> /* exit & co */ +#include <openssl/dh.h> /* open ssl library for DH operations */ +#include <openssl/sha.h> /* open ssl library for SHA operations */ +#include <openssl/dsa.h> /* open ssl library for DSA operations */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -38,10 +38,10 @@ #define TRUE 1 -#define FALSE 0 +#define FALSE 0 /** @addtogroup notification - * @{ + * @{ */ #define PS_DH_CREATE 0 @@ -62,13 +62,13 @@ /*! \brief Perf set number: Hash chain creation */ #define PS_HC_CREATE 8 /*! \brief Perf set number: Hash computation */ -#define PS_HASH 9 +#define PS_HASH 9 /* \brief Maximum perf set number. Number of file outputs */ -#define PS_MAX 10 +#define PS_MAX 10 /*!@}*/ /*! \brief Input bytes for the hash function */ -#define HASH_LEN 20 +#define HASH_LEN 20 /*! @@ -77,32 +77,33 @@ * Prints all possible command line options. * * \author Tobias Heer - * + * * \param progname The name of the executable * \return void */ -void dhp_usage(char* progname){ - printf( "Usage: %s -c [NUM] -l [NUM]\n" - "-c [NUM] : create [NUM] new dh keys for the benchmark\n" - "-g [NUM] : dh group ID for dh keys (Default is 3 (1536 Oakley_5)\n" - "-d [NUM] : create [NUM] new dsa keys for the benchmark\n" - "-r [NUM] : create [NUM] new rsa keys for the benchmark\n" - "-j [NUM] : rsa key length (default is 1024)\n" - "-k [NUM] : dsa key length (default is %d)\n" - "-l [NUM] : run the benchmark [NUM] times\n" - "-p : print keys (do not use this option for benchmarking!)\n" - "-s [NUM] : shared key length (default is 192)\n" - "-h [NUM] : hash chain length (default is 44 (20 trig, 20 sig, 4 binary)\n" - "-x : write singe time values to files (PS_* files)\n" - "-b : put load onto the cpu by calculating DH exchanges\n" - "-f [NUM] : calculate [NUM] SHA-1 hashes\n" - , progname,DSA_KEY_DEFAULT_BITS); -} +void dhp_usage(char *progname) +{ + printf( "Usage: %s -c [NUM] -l [NUM]\n" + "-c [NUM] : create [NUM] new dh keys for the benchmark\n" + "-g [NUM] : dh group ID for dh keys (Default is 3 (1536 Oakley_5)\n" + "-d [NUM] : create [NUM] new dsa keys for the benchmark\n" + "-r [NUM] : create [NUM] new rsa keys for the benchmark\n" + "-j [NUM] : rsa key length (default is 1024)\n" + "-k [NUM] : dsa key length (default is %d)\n" + "-l [NUM] : run the benchmark [NUM] times\n" + "-p : print keys (do not use this option for benchmarking!)\n" + "-s [NUM] : shared key length (default is 192)\n" + "-h [NUM] : hash chain length (default is 44 (20 trig, 20 sig, 4 binary)\n" + "-x : write singe time values to files (PS_* files)\n" + "-b : put load onto the cpu by calculating DH exchanges\n" + "-f [NUM] : calculate [NUM] SHA-1 hashes\n" + , progname, DSA_KEY_DEFAULT_BITS); +} /*! * \brief Get the option values from the input parameters. * - * Takes the input parameters, parses them and returns the option switches. + * Takes the input parameters, parses them and returns the option switches. * * \author Tobias Heer * @@ -117,194 +118,198 @@ * \param sw_print_keys Print the DH, RSA, and DSA keys (for debug). * \param sw_shared_key_len Length of the shared keys. * \param sw_hash_chain_len Length of the hash chain (elements) - * \param sw_file_output Print data to files or to stdout + * \param sw_file_output Print data to files or to stdout * \param sw_cpuload Don't measure, only load the CPU. * \param sw_hash_loops Number of hash computations. - * - * \note all sw_ paramters are pointers to ouput parameters that are modified by dhp_getopts. - * + * + * \note all sw_ paramters are pointers to ouput parameters that are modified + * by dhp_getopts. + * * \return Returns error code. 0 = Success, 1 = Error. */ -int dhp_getopts(int argc, - char ** argv, - int * sw_create_dh, - int * sw_dh_group_id, - int * sw_create_dsa, - int * sw_create_rsa, - int * sw_rsa_keylen, - int * sw_dsa_keylen, - int * sw_bench_loops, - int * sw_print_keys, - int * sw_shared_key_len, - int * sw_hash_chain_len, - int * sw_file_output, - int * sw_cpuload, - int * sw_hash_loops){ - int c; - opterr = 0; - - - - - while ((c=getopt(argc, argv, "c:l:ps:h:d:xbr:j:g:f:k:")) != -1) - { - switch (c){ - case 'c': - /* create n new dh keys */ - *sw_create_dh = atoi(optarg); - if(*sw_create_dh < 2){ - printf("The value for argument %c needs to be greater than 1\n", optopt); - return 0; - } - break; - case 'd': - *sw_create_dsa = atoi(optarg); - if(*sw_create_dsa < 2){ - printf("The value for argument %c needs to be greater than 1\n", optopt); - return 0; - } - break; - case 'r': - *sw_create_rsa = atoi(optarg); - if(*sw_create_rsa < 2){ - printf("The value for argument %c needs to be greater than 1\n", optopt); - return 0; - } - break; - case 'l': - /* number of benchmark loops */ - *sw_bench_loops = atoi(optarg); - if(*sw_bench_loops < 1){ - printf("The value for argument %c needs to be greater than 0\n", optopt); - return 0; - } - break; - case 'p': - *sw_print_keys = TRUE; - break; - case 'x': - *sw_file_output = TRUE; - break; - case 'b': - *sw_cpuload = TRUE; - break; - - case 's': - *sw_shared_key_len = atoi(optarg); - if(*sw_shared_key_len < 1){ - printf("The value for argument %c needs to be greater than 0\n", optopt); - return 0; - } - break; - case 'j': - *sw_rsa_keylen = atoi(optarg); - if(*sw_rsa_keylen < 1){ -// printf("The value for argument %c needs to be greater than 0\n", optopt); - return 0; - } - break; - case 'k': - *sw_dsa_keylen = atoi(optarg); - if(*sw_dsa_keylen < 1){ -// printf("The value for argument %c needs to be greater than 0\n", optopt); - return 0; - } - break; - case 'h': - *sw_hash_chain_len = atoi(optarg); - if(*sw_shared_key_len < 1){ - printf("The value for argument %c needs to be greater than 0\n", optopt); - return 0; - } - break; - case 'f': - *sw_hash_loops = atoi(optarg); - if(*sw_hash_loops < 1 || *sw_hash_loops % 1000 != 0){ - printf("The value must be a multitude of 1000\n"); - return 0; - } - break; - case 'g': - *sw_dh_group_id = atoi(optarg); - if(*sw_dh_group_id < 1){ - printf("The value for argument %c needs to be greater than 0\n", optopt); - return 0; - } - break; - case ':': - printf("Missing argument %c\n", optopt); - return 0; - - case '?': - printf("Unknown option %c\n", optopt); - return 0; - } - } - return 1; +int dhp_getopts(int argc, + char **argv, + int *sw_create_dh, + int *sw_dh_group_id, + int *sw_create_dsa, + int *sw_create_rsa, + int *sw_rsa_keylen, + int *sw_dsa_keylen, + int *sw_bench_loops, + int *sw_print_keys, + int *sw_shared_key_len, + int *sw_hash_chain_len, + int *sw_file_output, + int *sw_cpuload, + int *sw_hash_loops) +{ + int c; + opterr = 0; + + while ((c = getopt(argc, argv, "c:l:ps:h:d:xbr:j:g:f:k:")) != -1) { + switch (c) { + case 'c': + /* create n new dh keys */ + *sw_create_dh = atoi(optarg); + if (*sw_create_dh < 2) { + printf("The value for argument %c needs to be greater than 1\n", + optopt); + return 0; + } + break; + case 'd': + *sw_create_dsa = atoi(optarg); + if (*sw_create_dsa < 2) { + printf("The value for argument %c needs to be greater than 1\n", + optopt); + return 0; + } + break; + case 'r': + *sw_create_rsa = atoi(optarg); + if (*sw_create_rsa < 2) { + printf("The value for argument %c needs to be greater than 1\n", + optopt); + return 0; + } + break; + case 'l': + /* number of benchmark loops */ + *sw_bench_loops = atoi(optarg); + if (*sw_bench_loops < 1) { + printf("The value for argument %c needs to be greater than 0\n", + optopt); + return 0; + } + break; + case 'p': + *sw_print_keys = TRUE; + break; + case 'x': + *sw_file_output = TRUE; + break; + case 'b': + *sw_cpuload = TRUE; + break; + + case 's': + *sw_shared_key_len = atoi(optarg); + if (*sw_shared_key_len < 1) { + printf("The value for argument %c needs to be greater than 0\n", + optopt); + return 0; + } + break; + case 'j': + *sw_rsa_keylen = atoi(optarg); + if (*sw_rsa_keylen < 1) { + return 0; + } + break; + case 'k': + *sw_dsa_keylen = atoi(optarg); + if (*sw_dsa_keylen < 1) { + return 0; + } + break; + case 'h': + *sw_hash_chain_len = atoi(optarg); + if (*sw_shared_key_len < 1) { + printf("The value for argument %c needs to be greater than 0\n", + optopt); + return 0; + } + break; + case 'f': + *sw_hash_loops = atoi(optarg); + if (*sw_hash_loops < 1 || *sw_hash_loops % 1000 != 0) { + printf("The value must be a multitude of 1000\n"); + return 0; + } + break; + case 'g': + *sw_dh_group_id = atoi(optarg); + if (*sw_dh_group_id < 1) { + printf("The value for argument %c needs to be greater than 0\n", + optopt); + return 0; + } + break; + case ':': + printf("Missing argument %c\n", optopt); + return 0; + + case '?': + printf("Unknown option %c\n", optopt); + return 0; + } + } + return 1; } /*! - * \brief Determine and print the gettimeofday time resolution. - * + * \brief Determine and print the gettimeofday time resolution. + * * \author Tobias Heer - * - * Determine the time resolution of gettimeofday. + * + * Determine the time resolution of gettimeofday. * * \return void */ static void print_timeres(void) { - struct timeval tv1, tv2; - int i; - printf( "-------------------------------\n" - "Determine gettimeofday resolution:\n"); - - - for(i = 0; i < 10; i++){ - gettimeofday(&tv1, NULL); - do { - gettimeofday(&tv2, NULL); - } while (tv1.tv_usec == tv2.tv_usec); - - printf("Resolution: %ld us\n", tv2.tv_usec - tv1.tv_usec + - 1000000 * (tv2.tv_sec - tv1.tv_sec)); - } - - printf( "-------------------------------\n\n\n"); + struct timeval tv1, tv2; + int i; + printf( "-------------------------------\n" + "Determine gettimeofday resolution:\n"); + + + for (i = 0; i < 10; i++) { + gettimeofday(&tv1, NULL); + do { + gettimeofday(&tv2, NULL); + } while (tv1.tv_usec == tv2.tv_usec); + + printf("Resolution: %ld us\n", tv2.tv_usec - tv1.tv_usec + + 1000000 * (tv2.tv_sec - tv1.tv_sec)); + } + + printf( "-------------------------------\n\n\n"); } + /*! * \brief Take time for benchmark. * * Starts a time interval. - * + * * \author Tobias Heer - * + * * \param timeval timeval struct from the OS. * \return void */ -void dhp_start_benchmark(struct timeval * bench_time){ - - gettimeofday(bench_time, NULL); - +void dhp_start_benchmark(struct timeval *bench_time) +{ + gettimeofday(bench_time, NULL); } /*! * \brief Take time for benchmark and return passed time. * * Concludes a time interval and returns the past time. - * + * * \author Tobias Heer * * \param timeval timeval struct from the OS. * \return passed time since beginning of the interval. */ -double dhp_stop_benchmark(struct timeval * bench_time){ - struct timeval now; - gettimeofday(&now, NULL); - return ((now.tv_sec - bench_time->tv_sec) * 1000000 + (now.tv_usec - bench_time->tv_usec)) / 1000000.0; +double dhp_stop_benchmark(struct timeval *bench_time) +{ + struct timeval now; + gettimeofday(&now, NULL); + return ((now.tv_sec - bench_time->tv_sec) + * 1000000 + (now.tv_usec - bench_time->tv_usec)) / 1000000.0; } - /*! * \brief Main function that performs the measurements. * @@ -315,468 +320,495 @@ * . * \return Returns error code. 0 = Success, 1 = Error. */ -int main(int argc, char ** argv){ - - int i; - int err = 0; - - /* switches */ - int sw_create_dh = 5; - int sw_create_dsa = 5; - int sw_create_rsa = 5; - int sw_bench_loops = 0; - int sw_file_output = 0; - int sw_print_keys = FALSE; - int sw_shared_key_len = 192; - int sw_hash_chain_len = 46; - int sw_cpuload = 0; - int sw_rsa_keylen = 1024; - int sw_dsa_keylen = DSA_KEY_DEFAULT_BITS; - int sw_hashloops = 100000; - int sw_dh_group_id = HIP_FIRST_DH_GROUP_ID; - printf("Default settings RSA: key pool of %d keys of length %d.\n", - sw_create_rsa, - sw_rsa_keylen); - RSA ** rsa_key_pool = NULL; - DSA ** dsa_key_pool = NULL; - DH ** dh_key_pool = NULL; - - BN_CTX *ctx; - - float bench_secs = 0.0; - struct timeval bench_time; - - unsigned int sig_len; - - perf_set_t * perf_set = NULL; - - if(!dhp_getopts(argc, argv, - &sw_create_dh, - &sw_dh_group_id, - &sw_create_dsa, - &sw_create_rsa, - &sw_rsa_keylen, - &sw_dsa_keylen, - &sw_bench_loops, - &sw_print_keys, - &sw_shared_key_len, - &sw_hash_chain_len, - &sw_file_output, - &sw_cpuload, - &sw_hashloops)){ - dhp_usage(argv[0]); - exit(0); - } - - if(sw_file_output){ - perf_set = hip_perf_create(PS_MAX); - - check_and_create_dir("results", DEFAULT_CONFIG_DIR_MODE); - - hip_perf_set_name(perf_set, PS_DH_CREATE,"PS_DH_CREATE.csv"); - hip_perf_set_name(perf_set, PS_DH_SHARE,"PS_DH_SHARE.csv"); - hip_perf_set_name(perf_set, PS_RSA_CREATE,"PS_RSA_CREATE.csv"); - hip_perf_set_name(perf_set, PS_RSA_SIGN,"PS_RSA_SIGN.csv"); - hip_perf_set_name(perf_set, PS_RSA_VERIFY,"PS_RSA_VERIFY.csv"); - hip_perf_set_name(perf_set, PS_DSA_CREATE,"PS_DSA_CREATE.csv"); - hip_perf_set_name(perf_set, PS_DSA_SIGN,"PS_DSA_SIGN.csv"); - hip_perf_set_name(perf_set, PS_DSA_VERIFY,"PS_DSA_VERIFY.csv"); - hip_perf_set_name(perf_set, PS_HC_CREATE,"PS_HC_CREATE.csv"); - hip_perf_set_name(perf_set, PS_HASH,"PS_HC_HASHLOOPS_100_PER_ENTRY.csv"); - - - - printf( "-------------------------------\n" - "!!! NOTE: File output option set! The benchmark \n" - " results displayed here are not accurate!\n" - " They contain the time needed to write\n" - " the output to the files. Don't rely on these\n" - " values. Use the values provided in the\n" - " PS_*.csv files.\n\n" - " PS_DH_CREATE: Diffie Hellman key creation\n" - " PS_DH_SHARE: Diffie Hellman shared key creation\n" - " PS_DSA_CREATE: DSA key creation\n" - " PS_DSA_SIGN: DSA signature\n" - " PS_DSA_VERIFY: DSA verification\n" - " PS_HC_CREATE: Hash chain creation\n" - " PS_HC_HASHLOOPS_100_PER_ENTRY: Hash performance. 100 hashes per row!\n" - "-------------------------------\n\n"); - hip_perf_open(perf_set); - } - - - if(!sw_cpuload){ - print_timeres(); - - printf( "-------------------------------\n" - "RSA performance test\n" - "-------------------------------\n\n"); - //impl_dsa_sign(u8 *digest, u8 *private_key, u8 *signature) - - - rsa_key_pool = malloc(sw_create_rsa * sizeof(RSA *)); - printf("Creating key pool of %d keys of length %d.\n", - sw_create_rsa, - sw_rsa_keylen); - dhp_start_benchmark(&bench_time); - /* create new DH keys */ - for( i = 0; i < sw_create_rsa; i++){ - //printf("Create key %d\n", i); - if(sw_file_output) hip_perf_start_benchmark(perf_set, PS_RSA_CREATE); - rsa_key_pool[i] = create_rsa_key(sw_rsa_keylen); - if(!rsa_key_pool[i]){ - printf("RSA key is crap!\n"); - exit(0); - } - if(sw_file_output){ - hip_perf_stop_benchmark(perf_set, PS_RSA_CREATE); - hip_perf_write_benchmark(perf_set, PS_RSA_CREATE); - } - if(sw_print_keys == TRUE){ - printf("\nKey %d\n", i+1); - //printf("pub_key =%s\n", BN_bn2hex(rsa_key_pool[i]->pub_key)); - //printf("priv_key =%s\n", BN_bn2hex(rsa_key_pool[i]->priv_key)); - }else{ - //dhp_load_progress(i, sw_create_dsa, 50); - } - } - - - printf("\n"); - bench_secs = dhp_stop_benchmark(&bench_time); - printf("RSA key generation took %.3f sec (%.5f sec per key)\n", - bench_secs, bench_secs / sw_create_rsa); - printf("%4.2f keys per sec, %4.2f keys per min\n\n", - sw_create_rsa/bench_secs, sw_create_rsa/bench_secs*60); - - - if(sw_bench_loops == 0){ - printf( "Using standard number of loops (%d).\n" - "Use the -p parameter to use more loops\n" - "to get more realistic results.\n\n", - DHP_DEFAULT_LOOPS); - sw_bench_loops = DHP_DEFAULT_LOOPS; - } - - /* if sw_rsa_sig_len == 0 we will use the default lengths as they - occur in hip */ - u8 rsa_data[SHA_DIGEST_LENGTH]; - memset(rsa_data, 22, SHA_DIGEST_LENGTH); - u8 ** rsa_sig_pool; - rsa_sig_pool = malloc(sw_bench_loops* sizeof(u8 *)); - - printf("Calculating %d RSA signatures (len: %d)\n", sw_bench_loops, sw_rsa_keylen); - - dhp_start_benchmark(&bench_time); - for( i = 0; i < sw_bench_loops; i++){ - - - sig_len = RSA_size(rsa_key_pool[i%sw_create_rsa]); - rsa_sig_pool[i] = malloc(sig_len); - memset(rsa_sig_pool[i], 0, sig_len); - ctx = BN_CTX_new(); - rsa_key_pool[i%sw_create_rsa]->iqmp = BN_mod_inverse(NULL, rsa_key_pool[i%sw_create_rsa]->p, rsa_key_pool[i%sw_create_rsa]->q, ctx); - - if(sw_file_output) hip_perf_start_benchmark(perf_set, PS_RSA_SIGN); - err = RSA_sign(NID_sha1, rsa_data, SHA_DIGEST_LENGTH, rsa_sig_pool[i], &sig_len, - //err = RSA_sign(NID_md5_sha1, rsa_data, 36, rsa_sig_pool[i], &sig_len, -// NID_md5_sha1, buf,36 - rsa_key_pool[i%sw_create_rsa]); -// err = RSA_sign(NID_sha1, digest, SHA_DIGEST_LENGTH, signature, -// &sig_len, rsa); - if(sw_file_output){ - hip_perf_stop_benchmark(perf_set, PS_RSA_SIGN); - hip_perf_write_benchmark(perf_set, PS_RSA_SIGN); - } - if(!err){ - printf("RSA signature is crap\n"); - } - - - if(sw_print_keys){ - // HIP_DEBUG("DSAsig.r: %s\n", BN_bn2hex(dsa_sig_pool[i]->r)); - // HIP_DEBUG("DSAsig.s: %s\n", BN_bn2hex(dsa_sig_pool[i]->s)); - } - } - bench_secs = dhp_stop_benchmark(&bench_time); - printf("\n"); - printf("Signature generation took %.3f sec (%.5f sec per key)\n", - bench_secs, bench_secs / sw_bench_loops); - printf("%4.2f signatures per sec, %4.2f signatures per min\n\n", - sw_bench_loops/bench_secs, sw_bench_loops/bench_secs*60); - - - printf("Verifying %d RSA signatures\n", sw_bench_loops); - dhp_start_benchmark(&bench_time); - for( i = 0; i < sw_bench_loops; i++){ - if(sw_file_output) hip_perf_start_benchmark(perf_set, PS_RSA_VERIFY); - err = RSA_verify(NID_sha1, rsa_data, SHA_DIGEST_LENGTH, rsa_sig_pool[i],RSA_size(rsa_key_pool[i%sw_create_rsa]) , rsa_key_pool[i%sw_create_rsa]); - //printf("verify %d rsa\n", RSA_size(rsa_key_pool[i%sw_create_rsa])); - //err = RSA_verify(NID_md5_sha1, rsa_data, 36, rsa_sig_pool[i],RSA_size(rsa_key_pool[i%sw_create_rsa]) , rsa_key_pool[i%sw_create_rsa]); - - if(!err){ - printf("Verification failed\n"); - } - if(sw_file_output){ - hip_perf_stop_benchmark(perf_set, PS_RSA_VERIFY); - hip_perf_write_benchmark(perf_set, PS_RSA_VERIFY); - } - if(sw_print_keys){ - //HIP_DEBUG("DSAsig.r: %s\n", BN_bn2hex(dsa_sig_pool[i]->r)); - //HIP_DEBUG("DSAsig.s: %s\n", BN_bn2hex(dsa_sig_pool[i]->s)); - } - } - bench_secs = dhp_stop_benchmark(&bench_time); - printf("\n"); - printf("Signature verification took %.3f sec (%.5f sec per key)\n", - bench_secs, bench_secs / sw_bench_loops); - printf("%4.2f signatures per sec, %4.2f signatures per min\n\n", - sw_bench_loops/bench_secs, sw_bench_loops/bench_secs*60); - - printf( "-------------------------------\n" - "DSA performance test\n" - "-------------------------------\n\n"); - //impl_dsa_sign(u8 *digest, u8 *private_key, u8 *signature) - - dsa_key_pool = malloc(sw_create_dsa * sizeof(DSA *)); - printf("Creating key pool of %d keys of length %d.\n", - sw_create_dsa, - sw_dsa_keylen); - dhp_start_benchmark(&bench_time); - /* create new DH keys */ - for( i = 0; i < sw_create_dsa; i++){ - if(sw_file_output) hip_perf_start_benchmark(perf_set, PS_DSA_CREATE); - dsa_key_pool[i] = create_dsa_key(sw_dsa_keylen); - if(!dsa_key_pool[i]){ - printf("DSA key is crap!\n"); - exit(0); - } - if(sw_file_output){ - hip_perf_stop_benchmark(perf_set, PS_DSA_CREATE); - hip_perf_write_benchmark(perf_set, PS_DSA_CREATE); - } - if(sw_print_keys == TRUE){ - printf("\nKey %d\n", i+1); - printf("pub_key =%s\n", BN_bn2hex(dsa_key_pool[i]->pub_key)); - printf("priv_key =%s\n", BN_bn2hex(dsa_key_pool[i]->priv_key)); - }else{ - //dhp_load_progress(i, sw_create_dsa, 50); - } - } - - - printf("\n"); - bench_secs = dhp_stop_benchmark(&bench_time); - printf("DSA key generation took %.3f sec (%.5f sec per key)\n", - bench_secs, bench_secs / sw_create_dsa); - printf("%4.2f keys per sec, %4.2f keys per min\n\n", - sw_create_dsa/bench_secs, sw_create_dsa/bench_secs*60); - - - if(sw_bench_loops == 0){ - printf( "Using standard number of loops (%d).\n" - "Use the -p parameter to use more loops\n" - "to get more realistic results.\n\n", - DHP_DEFAULT_LOOPS); - sw_bench_loops = DHP_DEFAULT_LOOPS; - } - - - /* if sw_dsa_sig_len == 0 we will use the default lengths as they - occur in hip */ - u8 dsa_data[SHA_DIGEST_LENGTH]; - memset(dsa_data, 22, SHA_DIGEST_LENGTH); - DSA_SIG ** dsa_sig_pool; - dsa_sig_pool = malloc(sw_bench_loops* sizeof(DSA_SIG *)); - - printf("Calculating %d DSA signatures\n", sw_bench_loops); - dhp_start_benchmark(&bench_time); - for( i = 0; i < sw_bench_loops; i++){ - if(sw_file_output) hip_perf_start_benchmark(perf_set, PS_DSA_SIGN); - dsa_sig_pool[i] = DSA_do_sign(dsa_data, SHA_DIGEST_LENGTH, dsa_key_pool[i%sw_create_dsa]); - - if(!dsa_sig_pool[i]){ - printf("DSA signature is crap\n"); - } - - if(sw_file_output){ - hip_perf_stop_benchmark(perf_set, PS_DSA_SIGN); - hip_perf_write_benchmark(perf_set, PS_DSA_SIGN); - } - if(sw_print_keys){ - HIP_DEBUG("DSAsig.r: %s\n", BN_bn2hex(dsa_sig_pool[i]->r)); - HIP_DEBUG("DSAsig.s: %s\n", BN_bn2hex(dsa_sig_pool[i]->s)); - } - } - bench_secs = dhp_stop_benchmark(&bench_time); - printf("\n"); - printf("Signature generation took %.3f sec (%.5f sec per key)\n", - bench_secs, bench_secs / sw_bench_loops); - printf("%4.2f signatures per sec, %4.2f signatures per min\n\n", - sw_bench_loops/bench_secs, sw_bench_loops/bench_secs*60); - - - printf("Verifying %d DSA signatures\n", sw_bench_loops); - dhp_start_benchmark(&bench_time); - for( i = 0; i < sw_bench_loops; i++){ - if(sw_file_output) hip_perf_start_benchmark(perf_set, PS_DSA_VERIFY); - if(0 == DSA_do_verify(dsa_data, SHA_DIGEST_LENGTH, dsa_sig_pool[i], dsa_key_pool[i%sw_create_dsa])){ - printf("Verification failed\n"); - } - if(sw_file_output){ - hip_perf_stop_benchmark(perf_set, PS_DSA_VERIFY); - hip_perf_write_benchmark(perf_set, PS_DSA_VERIFY); - } - if(sw_print_keys){ - HIP_DEBUG("DSAsig.r: %s\n", BN_bn2hex(dsa_sig_pool[i]->r)); - HIP_DEBUG("DSAsig.s: %s\n", BN_bn2hex(dsa_sig_pool[i]->s)); - } - } - bench_secs = dhp_stop_benchmark(&bench_time); - printf("\n"); - printf("Signature verification took %.3f sec (%.5f sec per key)\n", - bench_secs, bench_secs / sw_bench_loops); - printf("%4.2f signatures per sec, %4.2f signatures per min\n\n", - sw_bench_loops/bench_secs, sw_bench_loops/bench_secs*60); - - - printf( "-------------------------------\n" - "Diffie hellman performance test\n" - "-------------------------------\n\n"); - }else{ - printf( "-------------------------------\n" - "Diffie hellman cpu load\n" - "-------------------------------\n\n"); - } - /* allocate memory for the DH key pool a minimum size for - two keys must be allocated (own and peer key) */ - - if(sw_create_dh == 0){ - printf( "Using 2 DH keys.\n" - "Use the -c parameter to create more dh\n" - "keys and to get more realistic results.\n\n"); - - /* DH_generate_key(dh_key_pool[0]); - DH_generate_key(dh_key_pool[1]); */ - sw_create_dh = 2; - } - dh_key_pool = malloc((sw_create_dh == 0 ? 2 : sw_create_dh) * sizeof(DH *)); - printf("Creating key pool of %d keys (Group %d).\n", (sw_create_dh == 0 ? 2 : sw_create_dh), sw_dh_group_id); - - dhp_start_benchmark(&bench_time); - /* create new DH keys */ - for( i = 0; i < sw_create_dh; i++){ - - - if(sw_file_output) hip_perf_start_benchmark(perf_set, PS_DH_CREATE); - dh_key_pool[i] = hip_generate_dh_key(sw_dh_group_id); - if(sw_file_output){ - hip_perf_stop_benchmark(perf_set, PS_DH_CREATE); - hip_perf_write_benchmark(perf_set, PS_DH_CREATE); - } - if(sw_print_keys == TRUE){ - printf("\nKey %d\n", i+1); - printf("pub_key =%s\n", BN_bn2hex(dh_key_pool[i]->pub_key)); - printf("priv_key =%s\n", BN_bn2hex(dh_key_pool[i]->priv_key)); - }else{ - //dhp_load_progress(i, sw_create_dh, 50); - } - } - printf("\n"); - bench_secs = dhp_stop_benchmark(&bench_time); - printf("DH key generation took %.3f sec (%.5f sec per key)\n", bench_secs, bench_secs / sw_create_dh); - printf("%4.2f keys per sec, %4.2f keys per min\n\n", sw_create_dh/bench_secs, sw_create_dh/bench_secs*60); - - - - dhp_start_benchmark(&bench_time); - int dh_size = hip_get_dh_size(HIP_FIRST_DH_GROUP_ID); - u8 shared_key[sw_shared_key_len]; - uint8_t pub_key[dh_size]; - - - printf("Calculating %d DH shared secrets\n", sw_bench_loops); - int k = 0; - for( i = 0; i < sw_bench_loops; i++){ - if(i%sw_create_dh == 0) k++; - bn2bin_safe(dh_key_pool[(i+k)%sw_create_dh]->pub_key, pub_key, dh_size); - if(sw_file_output) hip_perf_start_benchmark(perf_set, PS_DH_SHARE); - hip_gen_dh_shared_key( dh_key_pool[i%sw_create_dh], - pub_key, - dh_size, - shared_key, - sw_shared_key_len); - if(sw_file_output){ - hip_perf_stop_benchmark(perf_set, PS_DH_SHARE); - hip_perf_write_benchmark(perf_set, PS_DH_SHARE); - } - if(sw_print_keys)HIP_HEXDUMP("Shared key:", shared_key, sw_shared_key_len); - if(sw_cpuload) i = 0; - } - printf("\n"); - bench_secs = dhp_stop_benchmark(&bench_time); - printf("Shared Key generation took %.3f sec (%.5f sec per key)\n", bench_secs, bench_secs / sw_bench_loops); - printf("%4.2f keys per sec, %4.2f keys per min\n\n", sw_bench_loops/bench_secs, sw_bench_loops/bench_secs*60); +int main(int argc, char **argv) +{ + int i; + int err = 0; + /* switches */ + int sw_create_dh = 5; + int sw_create_dsa = 5; + int sw_create_rsa = 5; + int sw_bench_loops = 0; + int sw_file_output = 0; + int sw_print_keys = FALSE; + int sw_shared_key_len = 192; + int sw_hash_chain_len = 46; + int sw_cpuload = 0; + int sw_rsa_keylen = 1024; + int sw_dsa_keylen = DSA_KEY_DEFAULT_BITS; + int sw_hashloops = 100000; + int sw_dh_group_id = HIP_FIRST_DH_GROUP_ID; + RSA **rsa_key_pool = NULL; + DSA **dsa_key_pool = NULL; + DH **dh_key_pool = NULL; + BN_CTX *ctx = NULL; + float bench_secs = 0.0; + struct timeval bench_time; + unsigned int sig_len; + perf_set_t *perf_set = NULL; + + printf("Default settings RSA: key pool of %d keys of length %d.\n", + sw_create_rsa, + sw_rsa_keylen); + + if (!dhp_getopts(argc, argv, + &sw_create_dh, + &sw_dh_group_id, + &sw_create_dsa, + &sw_create_rsa, + &sw_rsa_keylen, + &sw_dsa_keylen, + &sw_bench_loops, + &sw_print_keys, + &sw_shared_key_len, + &sw_hash_chain_len, + &sw_file_output, + &sw_cpuload, + &sw_hashloops)) { + dhp_usage(argv[0]); + exit(0); + } + + if (sw_file_output) { + perf_set = hip_perf_create(PS_MAX); + + check_and_create_dir("results", DEFAULT_CONFIG_DIR_MODE); + + hip_perf_set_name(perf_set, PS_DH_CREATE, "PS_DH_CREATE.csv"); + hip_perf_set_name(perf_set, PS_DH_SHARE, "PS_DH_SHARE.csv"); + hip_perf_set_name(perf_set, PS_RSA_CREATE, "PS_RSA_CREATE.csv"); + hip_perf_set_name(perf_set, PS_RSA_SIGN, "PS_RSA_SIGN.csv"); + hip_perf_set_name(perf_set, PS_RSA_VERIFY, "PS_RSA_VERIFY.csv"); + hip_perf_set_name(perf_set, PS_DSA_CREATE, "PS_DSA_CREATE.csv"); + hip_perf_set_name(perf_set, PS_DSA_SIGN, "PS_DSA_SIGN.csv"); + hip_perf_set_name(perf_set, PS_DSA_VERIFY, "PS_DSA_VERIFY.csv"); + hip_perf_set_name(perf_set, PS_HC_CREATE, "PS_HC_CREATE.csv"); + hip_perf_set_name(perf_set, PS_HASH, + "PS_HC_HASHLOOPS_100_PER_ENTRY.csv"); + + printf( "-------------------------------\n" + "!!! NOTE: File output option set! The benchmark \n" + " results displayed here are not accurate!\n" + " They contain the time needed to write\n" + " the output to the files. Don't rely on these\n" + " values. Use the values provided in the\n" + " PS_*.csv files.\n\n" + " PS_DH_CREATE: Diffie Hellman key creation\n" + " PS_DH_SHARE: Diffie Hellman shared key creation\n" + " PS_DSA_CREATE: DSA key creation\n" + " PS_DSA_SIGN: DSA signature\n" + " PS_DSA_VERIFY: DSA verification\n" + " PS_HC_CREATE: Hash chain creation\n" + " PS_HC_HASHLOOPS_100_PER_ENTRY: Hash performance. 100 hashes per row!\n" + "-------------------------------\n\n"); + hip_perf_open(perf_set); + } + + if (!sw_cpuload) { + print_timeres(); + + printf( "-------------------------------\n" + "RSA performance test\n" + "-------------------------------\n\n"); + //impl_dsa_sign(u8 *digest, u8 *private_key, u8 *signature) + + + rsa_key_pool = malloc(sw_create_rsa * sizeof(RSA *)); + printf("Creating key pool of %d keys of length %d.\n", + sw_create_rsa, + sw_rsa_keylen); + dhp_start_benchmark(&bench_time); + /* create new DH keys */ + for (i = 0; i < sw_create_rsa; i++) { + //printf("Create key %d\n", i); + if (sw_file_output) { + hip_perf_start_benchmark(perf_set, PS_RSA_CREATE); + } + rsa_key_pool[i] = create_rsa_key(sw_rsa_keylen); + if (!rsa_key_pool[i]) { + printf("RSA key is crap!\n"); + exit(0); + } + if (sw_file_output) { + hip_perf_stop_benchmark(perf_set, PS_RSA_CREATE); + hip_perf_write_benchmark(perf_set, PS_RSA_CREATE); + } + if (sw_print_keys == TRUE) { + printf("\nKey %d\n", i + 1); + } else { + //dhp_load_progress(i, sw_create_dsa, 50); + } + } + + + printf("\n"); + bench_secs = dhp_stop_benchmark(&bench_time); + printf("RSA key generation took %.3f sec (%.5f sec per key)\n", + bench_secs, bench_secs / sw_create_rsa); + printf("%4.2f keys per sec, %4.2f keys per min\n\n", + sw_create_rsa / bench_secs, sw_create_rsa / bench_secs * 60); + + + if (sw_bench_loops == 0) { + printf( "Using standard number of loops (%d).\n" + "Use the -p parameter to use more loops\n" + "to get more realistic results.\n\n", + DHP_DEFAULT_LOOPS); + sw_bench_loops = DHP_DEFAULT_LOOPS; + } + + /* if sw_rsa_sig_len == 0 we will use the default lengths as they + * occur in hip */ + u8 rsa_data[SHA_DIGEST_LENGTH]; + memset(rsa_data, 22, SHA_DIGEST_LENGTH); + u8 **rsa_sig_pool; + rsa_sig_pool = malloc(sw_bench_loops * sizeof(u8 *)); + + printf("Calculating %d RSA signatures (len: %d)\n", sw_bench_loops, sw_rsa_keylen); + + dhp_start_benchmark(&bench_time); + for (i = 0; i < sw_bench_loops; i++) { + sig_len = RSA_size(rsa_key_pool[i % sw_create_rsa]); + rsa_sig_pool[i] = malloc(sig_len); + memset(rsa_sig_pool[i], 0, sig_len); + ctx = BN_CTX_new(); + rsa_key_pool[i % sw_create_rsa]->iqmp = BN_mod_inverse(NULL, rsa_key_pool[i % sw_create_rsa]->p, rsa_key_pool[i % sw_create_rsa]->q, ctx); + + if (sw_file_output) { + hip_perf_start_benchmark(perf_set, PS_RSA_SIGN); + } + err = RSA_sign(NID_sha1, rsa_data, SHA_DIGEST_LENGTH, + rsa_sig_pool[i], &sig_len, + rsa_key_pool[i % sw_create_rsa]); + + if (sw_file_output) { + hip_perf_stop_benchmark(perf_set, PS_RSA_SIGN); + hip_perf_write_benchmark(perf_set, PS_RSA_SIGN); + } + if (!err) { + printf("RSA signature is crap\n"); + } + + + if (sw_print_keys) { + // HIP_DEBUG("DSAsig.r: %s\n", BN_bn2hex(dsa_sig_pool[i]->r)); + // HIP_DEBUG("DSAsig.s: %s\n", BN_bn2hex(dsa_sig_pool[i]->s)); + } + } + bench_secs = dhp_stop_benchmark(&bench_time); + printf("\n"); + printf("Signature generation took %.3f sec (%.5f sec per key)\n", + bench_secs, bench_secs / sw_bench_loops); + printf("%4.2f signatures per sec, %4.2f signatures per min\n\n", + sw_bench_loops / bench_secs, sw_bench_loops / bench_secs * 60); + + + printf("Verifying %d RSA signatures\n", sw_bench_loops); + dhp_start_benchmark(&bench_time); + for (i = 0; i < sw_bench_loops; i++) { + if (sw_file_output) { + hip_perf_start_benchmark(perf_set, PS_RSA_VERIFY); + } + err = RSA_verify(NID_sha1, rsa_data, SHA_DIGEST_LENGTH, + rsa_sig_pool[i], + RSA_size(rsa_key_pool[i % sw_create_rsa]), + rsa_key_pool[i % sw_create_rsa]); + + if (!err) { + printf("Verification failed\n"); + } + if (sw_file_output) { + hip_perf_stop_benchmark(perf_set, PS_RSA_VERIFY); + hip_perf_write_benchmark(perf_set, PS_RSA_VERIFY); + } + if (sw_print_keys) { + //HIP_DEBUG("DSAsig.r: %s\n", BN_bn2hex(dsa_sig_pool[i]->r)); + //HIP_DEBUG("DSAsig.s: %s\n", BN_bn2hex(dsa_sig_pool[i]->s)); + } + } + bench_secs = dhp_stop_benchmark(&bench_time); + printf("\n"); + printf("Signature verification took %.3f sec (%.5f sec per key)\n", + bench_secs, bench_secs / sw_bench_loops); + printf("%4.2f signatures per sec, %4.2f signatures per min\n\n", + sw_bench_loops / bench_secs, sw_bench_loops / bench_secs * 60); + + printf( "-------------------------------\n" + "DSA performance test\n" + "-------------------------------\n\n"); + //impl_dsa_sign(u8 *digest, u8 *private_key, u8 *signature) + + dsa_key_pool = malloc(sw_create_dsa * sizeof(DSA *)); + printf("Creating key pool of %d keys of length %d.\n", + sw_create_dsa, + sw_dsa_keylen); + dhp_start_benchmark(&bench_time); + /* create new DH keys */ + for (i = 0; i < sw_create_dsa; i++) { + if (sw_file_output) { + hip_perf_start_benchmark(perf_set, PS_DSA_CREATE); + } + dsa_key_pool[i] = create_dsa_key(sw_dsa_keylen); + if (!dsa_key_pool[i]) { + printf("DSA key is crap!\n"); + exit(0); + } + if (sw_file_output) { + hip_perf_stop_benchmark(perf_set, PS_DSA_CREATE); + hip_perf_write_benchmark(perf_set, PS_DSA_CREATE); + } + if (sw_print_keys == TRUE) { + printf("\nKey %d\n", i + 1); + printf("pub_key =%s\n", BN_bn2hex(dsa_key_pool[i]->pub_key)); + printf("priv_key =%s\n", BN_bn2hex(dsa_key_pool[i]->priv_key)); + } else { + //dhp_load_progress(i, sw_create_dsa, 50); + } + } + + + printf("\n"); + bench_secs = dhp_stop_benchmark(&bench_time); + printf("DSA key generation took %.3f sec (%.5f sec per key)\n", + bench_secs, bench_secs / sw_create_dsa); + printf("%4.2f keys per sec, %4.2f keys per min\n\n", + sw_create_dsa / bench_secs, sw_create_dsa / bench_secs * 60); + + + if (sw_bench_loops == 0) { + printf( "Using standard number of loops (%d).\n" + "Use the -p parameter to use more loops\n" + "to get more realistic results.\n\n", + DHP_DEFAULT_LOOPS); + sw_bench_loops = DHP_DEFAULT_LOOPS; + } + + + /* if sw_dsa_sig_len == 0 we will use the default lengths as they + * occur in hip */ + u8 dsa_data[SHA_DIGEST_LENGTH]; + memset(dsa_data, 22, SHA_DIGEST_LENGTH); + DSA_SIG **dsa_sig_pool; + dsa_sig_pool = malloc(sw_bench_loops * sizeof(DSA_SIG *)); + + printf("Calculating %d DSA signatures\n", sw_bench_loops); + dhp_start_benchmark(&bench_time); + for (i = 0; i < sw_bench_loops; i++) { + if (sw_file_output) { + hip_perf_start_benchmark(perf_set, PS_DSA_SIGN); + } + dsa_sig_pool[i] = DSA_do_sign(dsa_data, SHA_DIGEST_LENGTH, + dsa_key_pool[i % sw_create_dsa]); + + if (!dsa_sig_pool[i]) { + printf("DSA signature is crap\n"); + } + + if (sw_file_output) { + hip_perf_stop_benchmark(perf_set, PS_DSA_SIGN); + hip_perf_write_benchmark(perf_set, PS_DSA_SIGN); + } + if (sw_print_keys) { + HIP_DEBUG("DSAsig.r: %s\n", BN_bn2hex(dsa_sig_pool[i]->r)); + HIP_DEBUG("DSAsig.s: %s\n", BN_bn2hex(dsa_sig_pool[i]->s)); + } + } + bench_secs = dhp_stop_benchmark(&bench_time); + printf("\n"); + printf("Signature generation took %.3f sec (%.5f sec per key)\n", + bench_secs, bench_secs / sw_bench_loops); + printf("%4.2f signatures per sec, %4.2f signatures per min\n\n", + sw_bench_loops / bench_secs, sw_bench_loops / bench_secs * 60); + + + printf("Verifying %d DSA signatures\n", sw_bench_loops); + dhp_start_benchmark(&bench_time); + for (i = 0; i < sw_bench_loops; i++) { + if (sw_file_output) { + hip_perf_start_benchmark(perf_set, PS_DSA_VERIFY); + } + if (0 == DSA_do_verify(dsa_data, SHA_DIGEST_LENGTH, dsa_sig_pool[i], + dsa_key_pool[i % sw_create_dsa])) { + printf("Verification failed\n"); + } + if (sw_file_output) { + hip_perf_stop_benchmark(perf_set, PS_DSA_VERIFY); + hip_perf_write_benchmark(perf_set, PS_DSA_VERIFY); + } + if (sw_print_keys) { + HIP_DEBUG("DSAsig.r: %s\n", BN_bn2hex(dsa_sig_pool[i]->r)); + HIP_DEBUG("DSAsig.s: %s\n", BN_bn2hex(dsa_sig_pool[i]->s)); + } + } + bench_secs = dhp_stop_benchmark(&bench_time); + printf("\n"); + printf("Signature verification took %.3f sec (%.5f sec per key)\n", + bench_secs, bench_secs / sw_bench_loops); + printf("%4.2f signatures per sec, %4.2f signatures per min\n\n", + sw_bench_loops / bench_secs, sw_bench_loops / bench_secs * 60); + + + printf( "-------------------------------\n" + "Diffie hellman performance test\n" + "-------------------------------\n\n"); + } else { + printf( "-------------------------------\n" + "Diffie hellman cpu load\n" + "-------------------------------\n\n"); + } + /* allocate memory for the DH key pool a minimum size for + * two keys must be allocated (own and peer key) */ + + if (sw_create_dh == 0) { + printf( "Using 2 DH keys.\n" + "Use the -c parameter to create more dh\n" + "keys and to get more realistic results.\n\n"); + + /* DH_generate_key(dh_key_pool[0]); + * DH_generate_key(dh_key_pool[1]); */ + sw_create_dh = 2; + } + dh_key_pool = malloc((sw_create_dh == 0 ? 2 : sw_create_dh) * sizeof(DH *)); + printf("Creating key pool of %d keys (Group %d).\n", + (sw_create_dh == 0 ? 2 : sw_create_dh), sw_dh_group_id); + + dhp_start_benchmark(&bench_time); + /* create new DH keys */ + for (i = 0; i < sw_create_dh; i++) { + if (sw_file_output) { + hip_perf_start_benchmark(perf_set, PS_DH_CREATE); + } + dh_key_pool[i] = hip_generate_dh_key(sw_dh_group_id); + if (sw_file_output) { + hip_perf_stop_benchmark(perf_set, PS_DH_CREATE); + hip_perf_write_benchmark(perf_set, PS_DH_CREATE); + } + if (sw_print_keys == TRUE) { + printf("\nKey %d\n", i + 1); + printf("pub_key =%s\n", BN_bn2hex(dh_key_pool[i]->pub_key)); + printf("priv_key =%s\n", BN_bn2hex(dh_key_pool[i]->priv_key)); + } else { + //dhp_load_progress(i, sw_create_dh, 50); + } + } + printf("\n"); + bench_secs = dhp_stop_benchmark(&bench_time); + printf("DH key generation took %.3f sec (%.5f sec per key)\n", + bench_secs, bench_secs / sw_create_dh); + printf("%4.2f keys per sec, %4.2f keys per min\n\n", + sw_create_dh / bench_secs, sw_create_dh / bench_secs * 60); + + + + dhp_start_benchmark(&bench_time); + int dh_size = hip_get_dh_size(HIP_FIRST_DH_GROUP_ID); + u8 shared_key[sw_shared_key_len]; + uint8_t pub_key[dh_size]; + + + printf("Calculating %d DH shared secrets\n", sw_bench_loops); + int k = 0; + for (i = 0; i < sw_bench_loops; i++) { + if (i % sw_create_dh == 0) { + k++; + } + bn2bin_safe(dh_key_pool[(i + k) % sw_create_dh]->pub_key, + pub_key, dh_size); + if (sw_file_output) { + hip_perf_start_benchmark(perf_set, PS_DH_SHARE); + } + hip_gen_dh_shared_key( dh_key_pool[i % sw_create_dh], + pub_key, + dh_size, + shared_key, + sw_shared_key_len); + if (sw_file_output) { + hip_perf_stop_benchmark(perf_set, PS_DH_SHARE); + hip_perf_write_benchmark(perf_set, PS_DH_SHARE); + } + if (sw_print_keys) { + HIP_HEXDUMP("Shared key:", shared_key, sw_shared_key_len); + } + if (sw_cpuload) { + i = 0; + } + } + printf("\n"); + bench_secs = dhp_stop_benchmark(&bench_time); + printf("Shared Key generation took %.3f sec (%.5f sec per key)\n", + bench_secs, bench_secs / sw_bench_loops); + printf("%4.2f keys per sec, %4.2f keys per min\n\n", + sw_bench_loops / bench_secs, sw_bench_loops / bench_secs * 60); #ifdef HASHCHAIN - printf( "-------------------------------\n" - "Hash chain performance test\n" - "-------------------------------\n\n"); - - printf("Creating %d hash chains of length %d\n", sw_bench_loops, sw_hash_chain_len); - hash_chain_t * current_chain; - dhp_start_benchmark(&bench_time); - - for( i = 0; i < sw_bench_loops; i++){ - if(sw_file_output) hip_perf_start_benchmark(perf_set, PS_HC_CREATE); - current_chain = hchain_create(sw_hash_chain_len); - if(sw_file_output){ - hip_perf_stop_benchmark(perf_set, PS_HC_CREATE); - hip_perf_write_benchmark(perf_set, PS_HC_CREATE); - } - if(sw_print_keys) hchain_print(current_chain); - } - printf("\n"); - bench_secs = dhp_stop_benchmark(&bench_time); - printf("Hash chain generation took %.3f sec (%.10f sec per hash chain)\n", bench_secs, bench_secs / sw_bench_loops); - printf("%4.2f hash chains per sec, %4.2f hash chains per min\n", sw_bench_loops/bench_secs, sw_bench_loops/bench_secs*60*1000); - - //if(sw_file_output) hip_perf_close(perf_set); + printf( "-------------------------------\n" + "Hash chain performance test\n" + "-------------------------------\n\n"); + + printf("Creating %d hash chains of length %d\n", sw_bench_loops, sw_hash_chain_len); + hash_chain_t *current_chain; + dhp_start_benchmark(&bench_time); + + for (i = 0; i < sw_bench_loops; i++) { + if (sw_file_output) { + hip_perf_start_benchmark(perf_set, PS_HC_CREATE); + } + current_chain = hchain_create(sw_hash_chain_len); + if (sw_file_output) { + hip_perf_stop_benchmark(perf_set, PS_HC_CREATE); + hip_perf_write_benchmark(perf_set, PS_HC_CREATE); + } + if (sw_print_keys) { + hchain_print(current_chain); + } + } + printf("\n"); + bench_secs = dhp_stop_benchmark(&bench_time); + printf("Hash chain generation took %.3f sec (%.10f sec per hash chain)\n", + bench_secs, bench_secs / sw_bench_loops); + printf("%4.2f hash chains per sec, %4.2f hash chains per min\n", + sw_bench_loops / bench_secs, sw_bench_loops / bench_secs * 60 * 1000); + + //if(sw_file_output) hip_perf_close(perf_set); #endif - - printf( "-------------------------------\n" - "Hash function (SHA-1) performance test\n" - "-------------------------------\n\n"); - - printf("Creating %d hashes\n", sw_hashloops); - u8 buffer1[HASH_LEN]; - u8 buffer2[HASH_LEN]; - memset(buffer1, 22, SHA_DIGEST_LENGTH); - memset(buffer2, 25, SHA_DIGEST_LENGTH); - - dhp_start_benchmark(&bench_time); - - int j; - - for( i = 0; i < sw_hashloops/100; i++){ - if(sw_file_output) hip_perf_start_benchmark(perf_set, PS_HASH); - for(j = 0; j < 100; j++){ - HIP_SHA(buffer1, HASH_LEN, buffer2); - } - if(sw_file_output){ - hip_perf_stop_benchmark(perf_set, PS_HASH); - hip_perf_write_benchmark(perf_set, PS_HASH); - } - } - printf("\n"); - bench_secs = dhp_stop_benchmark(&bench_time); - printf("Hash calculation took %.3f sec (%.10f sec per hash)\n", bench_secs, bench_secs / sw_hashloops); - printf("%4.2f hashes per sec, %4.2f hashes per min\n", sw_bench_loops/bench_secs*1000, sw_hashloops/bench_secs*60*1000); + + printf( "-------------------------------\n" + "Hash function (SHA-1) performance test\n" + "-------------------------------\n\n"); + + printf("Creating %d hashes\n", sw_hashloops); + u8 buffer1[HASH_LEN]; + u8 buffer2[HASH_LEN]; + memset(buffer1, 22, SHA_DIGEST_LENGTH); + memset(buffer2, 25, SHA_DIGEST_LENGTH); + + dhp_start_benchmark(&bench_time); + + int j; + + for (i = 0; i < sw_hashloops / 100; i++) { + if (sw_file_output) { + hip_perf_start_benchmark(perf_set, PS_HASH); + } + for (j = 0; j < 100; j++) { + HIP_SHA(buffer1, HASH_LEN, buffer2); + } + if (sw_file_output) { + hip_perf_stop_benchmark(perf_set, PS_HASH); + hip_perf_write_benchmark(perf_set, PS_HASH); + } + } + printf("\n"); + bench_secs = dhp_stop_benchmark(&bench_time); + printf("Hash calculation took %.3f sec (%.10f sec per hash)\n", + bench_secs, bench_secs / sw_hashloops); + printf("%4.2f hashes per sec, %4.2f hashes per min\n", + sw_bench_loops / bench_secs * 1000, sw_hashloops / bench_secs * 60 * 1000); #ifdef CONFIG_HIP_PERFORMANCE - /* Deallocate memory of perf_set after finishing all of tests */ - hip_perf_destroy(perf_set); + /* Deallocate memory of perf_set after finishing all of tests */ + hip_perf_destroy(perf_set); #endif - return err; + return err; } === modified file 'test/dhtteststub.c' --- test/dhtteststub.c 2010-01-19 13:10:46 +0000 +++ test/dhtteststub.c 2010-02-10 23:43:06 +0000 @@ -1,8 +1,9 @@ /** @file * Teststub for the DHT interface * - * A file to test how the DHT interface works. Compatible with OpenDHT/Bamboo and OpenLookup - * "sudo hipconf 0 1" is probably the one you need. Cases 1 and above are for testing latencies. + * A file to test how the DHT interface works. Compatible with OpenDHT/Bamboo + * and OpenLookup "sudo hipconf 0 1" is probably the one you need. Cases 1 and + * above are for testing latencies. * * @author Samu Varjonen */ @@ -24,38 +25,38 @@ int main(int argc, char *argv[]) { - int s = 0, ret, error; - int ttl = 240; + int s = 0, ret, error; + int ttl = 240; /* - struct in6_addr val_hit_addr; - struct in6_addr val_ip_addr; - */ + * struct in6_addr val_hit_addr; + * struct in6_addr val_ip_addr; + */ char opendht[] = "193.167.187.134"; //char opendht[] = "193.167.187.130"; //char opendht[] = "opendht.nyuld.net"; //char opendht[] = "openlookup.net"; /* both responses were 1024 before */ /* now more because base64 lengthens the message */ - unsigned char dht_response[2048]; + unsigned char dht_response[2048]; unsigned char dht_response2[2048]; - char put_packet[2048]; - /* Test values */ - char val_bogus[] = "BogusKey"; - char val_host[] = "testhostname"; - char val_host_test[] = "hosttestname2"; - char val_something[] = "hi-to-everyone"; - char secret_str[] = "secret_str_is_secret"; - char key_test[] = "Testiavain"; - char key_rand[] = "random_key"; - char val_tenbyte[] = "1234567890"; + char put_packet[2048]; + /* Test values */ + char val_bogus[] = "BogusKey"; + char val_host[] = "testhostname"; + char val_host_test[] = "hosttestname2"; + char val_something[] = "hi-to-everyone"; + char secret_str[] = "secret_str_is_secret"; + char key_test[] = "Testiavain"; + char key_rand[] = "random_key"; + char val_tenbyte[] = "1234567890"; /* smaller than 1K actually because any larger will bounce from DHT */ - char val_onekilo[985]; + char val_onekilo[985]; unsigned char val_hit[] = "2001:001a:3aa1:3a84:5b38:de59:28ff:41ea"; - char val_ip[] = "2001:0708:0140:0220:0213:a9ff:fec0:58f6"; + char val_ip[] = "2001:0708:0140:0220:0213:a9ff:fec0:58f6"; /* TODO change this to something smarter :) */ - char host_addr[] = "127.0.0.1";//"openlookup.net"; - int n = 0, iter = 0; - struct timeval conn_before, conn_after; + char host_addr[] = "127.0.0.1"; //"openlookup.net"; + int n = 0, iter = 0; + struct timeval conn_before, conn_after; struct timeval stat_before, stat_after; struct timeval put_rm_before, put_rm_after; struct timeval put_rm2_before, put_rm2_after; @@ -66,786 +67,767 @@ unsigned long put_rm2_diff_sec, put_rm2_diff_usec; unsigned long rm_diff_sec, rm_diff_usec; iter = atoi(argv[2]); - struct addrinfo * serving_gateway; + struct addrinfo *serving_gateway; int port = 5851; //5851 for opendht 80 for openlookup v1 if (argc != 3) { HIP_DEBUG("Usage: %s num iterations\n", argv[0]); HIP_DEBUG("Usage: %s 0 1 is probably the one you need\n" - "Cases 1 and above are for testing latencies\n", argv[0]); + "Cases 1 and above are for testing latencies\n", argv[0]); HIP_DEBUG("Num = 0 for regular testing of functions " - "(iterations not used just give 1)\n" - "Num = 1 get test times when value not found\n" - "Num = 2 get test times when value is found\n" - "Num = 3 put test times with 10 byte value (same key)\n" - "Num = 4 put test times with 10 byte value, " - "waiting 5 sec in between puts(same key)\n" - "Num = 5 put test times with 10 byte value (random key, short TTL)\n" - "Num = 6 put test times with 10 byte value, waiting 5 sec " - "in between puts(random key, short TTL)\n" - "Num = 7 put test times with consecutive keys and 985 byte values\n" - "Num = 8 put test times with consecutive keys and 985 byte values " - "with 5 sec sleep in between puts\n" - "Num = 9 get test times with consecutive keys (do number 7 or 8 first)\n" - "Num = 'a' remove testing\n" - "Iterations, just as it says\n" - "Connect errors will print 999 999\n"); + "(iterations not used just give 1)\n" + "Num = 1 get test times when value not found\n" + "Num = 2 get test times when value is found\n" + "Num = 3 put test times with 10 byte value (same key)\n" + "Num = 4 put test times with 10 byte value, " + "waiting 5 sec in between puts(same key)\n" + "Num = 5 put test times with 10 byte value (random key, short TTL)\n" + "Num = 6 put test times with 10 byte value, waiting 5 sec " + "in between puts(random key, short TTL)\n" + "Num = 7 put test times with consecutive keys and 985 byte values\n" + "Num = 8 put test times with consecutive keys and 985 byte values " + "with 5 sec sleep in between puts\n" + "Num = 9 get test times with consecutive keys (do number 7 or 8 first)\n" + "Num = 'a' remove testing\n" + "Iterations, just as it says\n" + "Connect errors will print 999 999\n"); exit(EXIT_SUCCESS); } /* resolve the gateway address */ - error = resolve_dht_gateway_info (opendht, &serving_gateway, port, AF_INET); + error = resolve_dht_gateway_info(opendht, &serving_gateway, port, AF_INET); if (error < 0) { HIP_DEBUG("Resolving error\n"); exit(0); } - if (argv[1][0] == '0') - { - HIP_DEBUG("Starting to test the openDHT interface.\n"); - HIP_DEBUG("Using test mapping\n'%s (FQDN) -> %s (HIT) -> %s (IP)'.\n", - val_host, val_hit, val_ip); - - /*!!!! put fqdn->hit !!!!*/ - s = init_dht_gateway_socket_gw(s, serving_gateway); - error = 0; - error = connect_dht_gateway(s, serving_gateway, 1); - if (error < 0) exit(0); - ret = 0; - memset(put_packet, '\0', sizeof(put_packet)); - ret = opendht_put( - (unsigned char *)val_host, - (unsigned char *)val_hit, - (unsigned char *)host_addr,port,ttl,put_packet); - ret = opendht_send (s,put_packet); - if (ret == -1) exit(1); - ret = opendht_read_response(s, dht_response); - if (ret == -1) exit(1); - HIP_DEBUG("Put packet (fqdn->hit) sent and ...\n"); - HIP_DEBUG("Put was success\n"); - close(s); - /*!!!! put hit->ip !!!!*/ - - s = init_dht_gateway_socket_gw(s, serving_gateway); - error = connect_dht_gateway(s, serving_gateway, 1); - if (error < 0) exit(0); - ret = 0; - memset(put_packet, '\0', sizeof(put_packet)); - ret = opendht_put( - (unsigned char *)val_hit, - (unsigned char *)val_ip, - (unsigned char *)host_addr,port,ttl,put_packet); - ret = opendht_send (s,put_packet); - if (ret == -1) exit(1); - ret = opendht_read_response(s, dht_response); - if (ret == -1) exit(1); - HIP_DEBUG("Put packet (hit->ip) sent and ...\n"); - HIP_DEBUG("Put was success\n", dht_response); - close(s); - - /*!!!! get fqdn !!!!*/ - - s = init_dht_gateway_socket_gw(s, serving_gateway); - error = connect_dht_gateway(s, serving_gateway, 1); - if (error < 0) exit(0); - ret = 0; - memset(dht_response, '\0', sizeof(dht_response)); - ret = opendht_get(s, (unsigned char *)val_host, (unsigned char *)host_addr, port); - ret = opendht_read_response(s, dht_response); - ret = handle_hit_value(dht_response, (void *)dht_response2); - // if (ret == -1) exit (1); - HIP_DEBUG("Get packet (fqdn) sent and ...\n"); - if (ret == 0) - { - HIP_DEBUG("Teststub: Value received from DHT: %s\n", dht_response2); - if (!strcmp((char *) dht_response2, (char *) val_hit)) - HIP_DEBUG("Did match the sent value.\n"); - else - HIP_DEBUG("Did NOT match the sent value!\n"); - } - close(s); - - /*!!!! get hit !!!!*/ - - s = init_dht_gateway_socket_gw(s, serving_gateway); - error = connect_dht_gateway(s, serving_gateway, 1); - if (error < 0) exit(0); - ret = 0; - memset(dht_response2, '\0', sizeof(dht_response2)); - ret = opendht_get(s, (unsigned char *)val_hit, (unsigned char *)host_addr, port); - ret = opendht_read_response(s, dht_response2); - memset(dht_response, '\0', sizeof(dht_response)); - hip_in6_ntop((struct in6_addr *)dht_response2, - (char *) dht_response); - HIP_DEBUG("Value: %s\n", (char*)dht_response); - if (ret == -1) exit (1); - HIP_DEBUG("Get packet (hit) sent and ...\n"); - if (ret == 0) - { - HIP_DEBUG("Teststub: Value received from DHT: %s\n",dht_response); - if (!strcmp((char *) dht_response, val_ip)) - HIP_DEBUG("Did match the sent value.\n"); - else - HIP_DEBUG("Did NOT match the sent value!\n"); - } - close(s); - - /* Finally let's try to get a key that doesn't exist */ - - s = init_dht_gateway_socket_gw(s, serving_gateway); - error = connect_dht_gateway(s, serving_gateway, 1); - if (error < 0) exit(0); - ret = 0; - memset(dht_response2, '\0', sizeof(dht_response2)); - ret = opendht_get(s, (unsigned char *)val_bogus, (unsigned char *)host_addr, port); - ret = opendht_read_response(s, dht_response2); - // if (ret == -1) exit (1); - HIP_DEBUG("Get packet (bogus, will not be found (hopefully)) sent and ...\n"); - HIP_DEBUG("Teststub: Value received from DHT: %s\n",dht_response2); - close(s); - - /* put_removable and rm tests */ - - /* put_removable */ - HIP_DEBUG("\n\nPut removable starts\n"); - s = init_dht_gateway_socket_gw(s, serving_gateway); - error = connect_dht_gateway(s, serving_gateway, 1); - if (error < 0) exit(0); - ret = 0; - memset(dht_response2, '\0', sizeof(dht_response2)); - ret = opendht_put_rm(s, - (unsigned char *)val_host_test, - (unsigned char *)val_something, - (unsigned char *)secret_str, - (unsigned char *)host_addr,port,ttl); - ret = opendht_read_response(s, dht_response2); - if (ret == -1) exit(1); - HIP_DEBUG("Put(rm) packet (fqdn->hit) sent and ...\n"); - HIP_DEBUG("Put(rm) was success\n"); - close(s); - /* check that value exists */ - s = init_dht_gateway_socket_gw(s, serving_gateway); - error = connect_dht_gateway(s, serving_gateway, 1); - if (error < 0) exit(0); - ret = 0; - memset(dht_response2, '\0', sizeof(dht_response2)); - ret = opendht_get(s, (unsigned char *)val_host_test, - (unsigned char *)host_addr, port); - ret = opendht_read_response(s, dht_response2); - // if (ret == -1) exit (1); - HIP_DEBUG("Get packet sent and (value should be found, just sent it)...\n"); - HIP_DEBUG("Value received from DHT: %s\n",dht_response2); - close(s); - /* send remove */ - s = init_dht_gateway_socket_gw(s, serving_gateway); - error = connect_dht_gateway(s, serving_gateway, 1); - if (error < 0) exit(0); - ret = 0; - memset(dht_response2, '\0', sizeof(dht_response2)); - ret = opendht_rm(s, - (unsigned char *)val_host_test, - (unsigned char *)val_something, - (unsigned char *)secret_str, - (unsigned char *)host_addr,port,ttl); - ret = opendht_read_response(s, dht_response2); - if (ret == -1) exit(1); - HIP_DEBUG("Rm packet sent and ...\n"); - HIP_DEBUG("Rm was success\n"); - close(s); - /* can you get it anymore */ - - s = init_dht_gateway_socket_gw(s, serving_gateway); - error = connect_dht_gateway(s, serving_gateway, 1); - if (error < 0) exit(0); - ret = 0; - memset(dht_response2, '\0', sizeof(dht_response2)); - ret = opendht_get(s, (unsigned char *)val_host_test, - (unsigned char *)host_addr, port); - ret = opendht_read_response(s, dht_response2); - // if (ret == -1) exit (1); - HIP_DEBUG("Get packet (was removed, will not be found (hopefully)) sent and ...\n"); - HIP_DEBUG("Teststub: Value received from DHT: %s\n",dht_response2); - close(s); - - /* testing a wrapper for blocking dht call */ - memset(dht_response, '\0', sizeof(dht_response)); - ret = 0; - HIP_DEBUG("\n\nTrying out get wrapper\n"); - - ret = hip_opendht_get_key(&handle_ip_value, serving_gateway, - (char *) val_hit, (char*) dht_response, 1); - - if (!ret) - HIP_DEBUG("DHT get succeeded\n"); - else - HIP_DEBUG("DHT get was unsuccesfull\n"); - - /* basic testing done */ - exit(EXIT_SUCCESS); - } - else if (argv[1][0] == '1') - { - HIP_DEBUG("Get test times when value not found\n"); - HIP_DEBUG("Printing \"connection time; get time; DHT answer (should be empty here)\n"); - HIP_DEBUG("Doing %s iterations\n", argv[2]); - - for (n = 0; n < iter; n++) - { - HIP_DEBUG("Iteration no %d\n",n); - s = init_dht_gateway_socket_gw(s, serving_gateway); - gettimeofday(&conn_before, NULL); - error = connect_dht_gateway(s, serving_gateway, 1); - gettimeofday(&conn_after, NULL); - if (error < 0) - { - printf("999 999\n"); - close(s); - } - else - { - ret = 0; - memset(dht_response2, '\0', sizeof(dht_response2)); - gettimeofday(&stat_before, NULL); - ret = opendht_get(s, (unsigned char *)val_bogus, - (unsigned char *)host_addr, 5851); - ret = opendht_read_response(s, dht_response2); - gettimeofday(&stat_after, NULL); - close(s); - /* Print findings*/ - conn_diff_sec = (conn_after.tv_sec - conn_before.tv_sec) *1000000; - conn_diff_usec = conn_after.tv_usec - conn_before.tv_usec; - stat_diff_sec = (stat_after.tv_sec - stat_before.tv_sec) *1000000; - stat_diff_usec = stat_after.tv_usec - stat_before.tv_usec; - printf("%.6f %.6f %s\n", - (conn_diff_sec + conn_diff_usec)/1000000.0, - (stat_diff_sec + stat_diff_usec)/1000000.0, - dht_response2); - } - } - } - else if (argv[1][0] == '2') - { - HIP_DEBUG("Get test times when value is found\n"); - HIP_DEBUG("Printing \"connection time; get time; DHT answer " - "(0 = OK, 1 = error, 2 = retry, or some value)\n"); - HIP_DEBUG("Doing %s iterations\n", argv[2]); - - s = init_dht_gateway_socket_gw(s, serving_gateway); - error = connect_dht_gateway(s, serving_gateway, 1); - if (error < 0) exit(0); - ret = 0; - /* iterations by estimate seconds, so the value is there long enough */ - memset(put_packet, '\0', sizeof(put_packet)); - ret = opendht_put( (unsigned char *)val_hit, - (unsigned char *)val_ip, - (unsigned char *)host_addr,5851,(iter * 3),put_packet); - ret = opendht_send (s,put_packet); - ret = opendht_read_response(s, dht_response); - if (ret == -1) exit(1); - HIP_DEBUG("Put packet (hit->ip) sent and ...\n"); - HIP_DEBUG("Put was success\n", dht_response); - close(s); - - for (n = 0; n < iter; n++) - { - HIP_DEBUG("Iteration no %d\n",n); - s = init_dht_gateway_socket_gw(s, serving_gateway); - gettimeofday(&conn_before, NULL); - error = connect_dht_gateway(s, serving_gateway, 1); - gettimeofday(&conn_after, NULL); - if (error < 0) - { - printf("999 999\n"); - close(s); - } - else - { - ret = 0; - memset(dht_response2, '\0', sizeof(dht_response2)); - gettimeofday(&stat_before, NULL); - ret = opendht_get(s, (unsigned char *)val_hit, - (unsigned char *)host_addr, 5851); - ret = opendht_read_response(s, dht_response2); - gettimeofday(&stat_after, NULL); - close(s); - /* Print findings*/ - conn_diff_sec = (conn_after.tv_sec - conn_before.tv_sec) *1000000; - conn_diff_usec = conn_after.tv_usec - conn_before.tv_usec; - stat_diff_sec = (stat_after.tv_sec - stat_before.tv_sec) *1000000; - stat_diff_usec = stat_after.tv_usec - stat_before.tv_usec; - printf("%.6f %.6f %s\n", - (conn_diff_sec + conn_diff_usec)/1000000.0, - (stat_diff_sec + stat_diff_usec)/1000000.0, - dht_response2); - } - } - } - else if (argv[1][0] == '3') - { - HIP_DEBUG("Put test times with 10 byte value (same key)\n"); - HIP_DEBUG("Printing \"connection time; get time; DHT answer " - "(0 = OK, 1 = error, 2 = retry, or some value)\n"); - HIP_DEBUG("Doing %s iterations\n", argv[2]); - - for (n = 0; n < iter; n++) - { - HIP_DEBUG("Iteration no %d\n",n); - s = init_dht_gateway_socket_gw(s, serving_gateway); - gettimeofday(&conn_before, NULL); - error = connect_dht_gateway(s, serving_gateway, 1); - gettimeofday(&conn_after, NULL); - if (error < 0) - { - printf("999 999\n"); - close(s); - } - else - { - ret = 0; - memset(dht_response2, '\0', sizeof(dht_response2)); - gettimeofday(&stat_before, NULL); - /* TTL just 20 secs */ - memset(put_packet, '\0', sizeof(put_packet)); - ret = opendht_put( (unsigned char *)key_test, - (unsigned char *)val_tenbyte, - (unsigned char *)host_addr,5851,20,put_packet); - ret = opendht_send (s,put_packet); - ret = opendht_read_response(s, dht_response); - gettimeofday(&stat_after, NULL); - close(s); - /* Print findings*/ - conn_diff_sec = (conn_after.tv_sec - conn_before.tv_sec) *1000000; - conn_diff_usec = conn_after.tv_usec - conn_before.tv_usec; - stat_diff_sec = (stat_after.tv_sec - stat_before.tv_sec) *1000000; - stat_diff_usec = stat_after.tv_usec - stat_before.tv_usec; - printf("%.6f %.6f %s\n", - (conn_diff_sec + conn_diff_usec)/1000000.0, - (stat_diff_sec + stat_diff_usec)/1000000.0, - dht_response); - } - } - } - else if (argv[1][0] == '4') - { - HIP_DEBUG("Put test times with 10 byte value, waiting " - "5 sec in between puts (same key)\n"); - HIP_DEBUG("Printing \"connection time; get time; DHT answer " - "(0 = OK, 1 = error, 2 = retry, or some value)\n"); - HIP_DEBUG("Doing %s iterations\n", argv[2]); - - for (n = 0; n < iter; n++) - { - HIP_DEBUG("Iteration no %d\n",n); - s = init_dht_gateway_socket_gw(s, serving_gateway); - gettimeofday(&conn_before, NULL); - error = connect_dht_gateway(s, serving_gateway, 1); - gettimeofday(&conn_after, NULL); - if (error < 0) - { - printf("999 999\n"); - close(s); - } - else - { - ret = 0; - memset(dht_response2, '\0', sizeof(dht_response2)); - gettimeofday(&stat_before, NULL); - /* TTL just 20 secs */ - memset(put_packet, '\0', sizeof(put_packet)); - ret = opendht_put( (unsigned char *)key_test, - (unsigned char *)val_tenbyte, - (unsigned char *)host_addr,5851,20,put_packet); - ret = opendht_send (s,put_packet); - ret = opendht_read_response(s, dht_response); - gettimeofday(&stat_after, NULL); - close(s); - /* Print findings*/ - conn_diff_sec = (conn_after.tv_sec - conn_before.tv_sec) *1000000; - conn_diff_usec = conn_after.tv_usec - conn_before.tv_usec; - stat_diff_sec = (stat_after.tv_sec - stat_before.tv_sec) *1000000; - stat_diff_usec = stat_after.tv_usec - stat_before.tv_usec; - printf("%.6f %.6f %s\n", - (conn_diff_sec + conn_diff_usec)/1000000.0, - (stat_diff_sec + stat_diff_usec)/1000000.0, - dht_response); - sleep(5); - } - } - } - else if (argv[1][0] == '5') - { - HIP_DEBUG("Put test times with 10 byte value (random key, short TTL)\n"); - HIP_DEBUG("Printing \"connection time; get time; DHT answer " - "(0 = OK, 1 = error, 2 = retry, or some value)\n"); - HIP_DEBUG("Doing %s iterations\n", argv[2]); - - srand(time(NULL)); - int ra = 0; - for (n = 0; n < iter; n++) - { - HIP_DEBUG("Iteration no %d\n",n); - ra= rand() % 1000000000; - sprintf(key_rand, "%.d", ra); - HIP_DEBUG("random key %s\n", key_rand); - s = init_dht_gateway_socket_gw(s, serving_gateway); - gettimeofday(&conn_before, NULL); - error = connect_dht_gateway(s, serving_gateway, 1); - gettimeofday(&conn_after, NULL); - if (error < 0) - { - printf("999 999\n"); - close(s); - } - else - { - ret = 0; - memset(dht_response2, '\0', sizeof(dht_response2)); - gettimeofday(&stat_before, NULL); - /* TTL just 20 secs */ - memset(put_packet, '\0', sizeof(put_packet)); - ret = opendht_put((unsigned char *)key_rand, - (unsigned char *)val_tenbyte, - (unsigned char *)host_addr,5851,20,put_packet); - ret = opendht_send (s,put_packet); - ret = opendht_read_response(s, dht_response); - gettimeofday(&stat_after, NULL); - close(s); - /* Print findings*/ - conn_diff_sec = (conn_after.tv_sec - conn_before.tv_sec) *1000000; - conn_diff_usec = conn_after.tv_usec - conn_before.tv_usec; - stat_diff_sec = (stat_after.tv_sec - stat_before.tv_sec) *1000000; - stat_diff_usec = stat_after.tv_usec - stat_before.tv_usec; - printf("%.6f %.6f %s\n", - (conn_diff_sec + conn_diff_usec)/1000000.0, - (stat_diff_sec + stat_diff_usec)/1000000.0, - dht_response); - } - } - } - else if (argv[1][0] == '6') - { - HIP_DEBUG("Put test times with 10 byte value, waiting 5 sec in " - "between puts(random key, short TTL)\n"); - HIP_DEBUG("Printing \"connection time; get time; DHT answer " - "(0 = OK, 1 = error, 2 = retry, or some value)\n"); - HIP_DEBUG("Doing %s iterations\n", argv[2]); - srand(time(NULL)); - int ra = 0; - for (n = 0; n < iter; n++) - { - HIP_DEBUG("Iteration no %d\n",n); - ra= rand() % 1000000000; - sprintf(key_rand, "%.d", ra); - HIP_DEBUG("random key %s\n", key_rand); - s = init_dht_gateway_socket_gw(s, serving_gateway); - gettimeofday(&conn_before, NULL); - error = connect_dht_gateway(s, serving_gateway, 1); - gettimeofday(&conn_after, NULL); - if (error < 0) - { - printf("999 999\n"); - close(s); - } - else - { - ret = 0; - memset(dht_response2, '\0', sizeof(dht_response2)); - gettimeofday(&stat_before, NULL); - /* TTL just 20 secs */ - memset(put_packet, '\0', sizeof(put_packet)); - ret = opendht_put( (unsigned char *)key_rand, - (unsigned char *)val_tenbyte, - (unsigned char *)host_addr,5851,20,put_packet); - ret = opendht_send (s,put_packet); - ret = opendht_read_response(s, dht_response); - gettimeofday(&stat_after, NULL); - close(s); - /* Print findings*/ - conn_diff_sec = (conn_after.tv_sec - conn_before.tv_sec) *1000000; - conn_diff_usec = conn_after.tv_usec - conn_before.tv_usec; - stat_diff_sec = (stat_after.tv_sec - stat_before.tv_sec) *1000000; - stat_diff_usec = stat_after.tv_usec - stat_before.tv_usec; - printf("%.6f %.6f %s\n", - (conn_diff_sec + conn_diff_usec)/1000000.0, - (stat_diff_sec + stat_diff_usec)/1000000.0, - dht_response); - sleep(5); - } - } - } - else if (argv[1][0] == '7') - { - memset(val_onekilo,'a',sizeof(val_onekilo)); - HIP_DEBUG("Put test times with consecutive keys and 985 byte values\n"); - HIP_DEBUG("Printing \"connection time; get time; DHT answer " - "(0 = OK, 1 = error, 2 = retry, or some value)\n"); - HIP_DEBUG("Doing %s iterations\n", argv[2]); - srand(time(NULL)); - int ra = 0; - for (n = 0; n < iter; n++) - { - HIP_DEBUG("Iteration no %d\n",n); - /* consecutive key instead of random as the variable says */ - ra= (n + 1) * 1000000; - sprintf(key_rand, "%.d", ra); - HIP_DEBUG("Consecutive key %s\n", key_rand); - s = init_dht_gateway_socket_gw(s, serving_gateway); - gettimeofday(&conn_before, NULL); - error = connect_dht_gateway(s, serving_gateway, 1); - gettimeofday(&conn_after, NULL); - if (error < 0) - { - printf("999 999\n"); - close(s); - } - else - { - ret = 0; - memset(dht_response2, '\0', sizeof(dht_response2)); - memset(dht_response, '\0', sizeof(dht_response)); - gettimeofday(&stat_before, NULL); - /* TTL just iter * 60 secs so values can be found in get test */ - memset(put_packet, '\0', sizeof(put_packet)); - ret = opendht_put( (unsigned char *)key_rand, - (unsigned char *)val_onekilo, - (unsigned char *)host_addr,5851,(iter* 60),put_packet); - ret = opendht_send (s,put_packet); - ret = opendht_read_response(s, dht_response); - gettimeofday(&stat_after, NULL); - close(s); - /* Print findings*/ - conn_diff_sec = (conn_after.tv_sec - conn_before.tv_sec) *1000000; - conn_diff_usec = conn_after.tv_usec - conn_before.tv_usec; - stat_diff_sec = (stat_after.tv_sec - stat_before.tv_sec) *1000000; - stat_diff_usec = stat_after.tv_usec - stat_before.tv_usec; - printf("%.6f %.6f %s\n", - (conn_diff_sec + conn_diff_usec)/1000000.0, - (stat_diff_sec + stat_diff_usec)/1000000.0, - dht_response); - } - } - } - else if (argv[1][0] == '8') - { - memset(val_onekilo,'a',sizeof(val_onekilo)); - HIP_DEBUG("Put test times with consecutive keys and 985 byte values" - " with 5 sec sleep between puts\n"); - HIP_DEBUG("Printing \"connection time; get time; DHT answer\n"); - HIP_DEBUG("(0 = OK, 1 = error, 2 = retry, or some value)\n"); - HIP_DEBUG("Doing %s iterations\n", argv[2]); - srand(time(NULL)); - int ra = 0; - for (n = 0; n < iter; n++) - { - HIP_DEBUG("Iteration no %d\n",n); - /* consecutive key instead of random as the variable says */ - ra= (n + 1 ) * 1000000; - sprintf(key_rand, "%.d", ra); - HIP_DEBUG("Consecutive key %s\n", key_rand); - s = init_dht_gateway_socket_gw(s, serving_gateway); - gettimeofday(&conn_before, NULL); - error = connect_dht_gateway(s, serving_gateway, 1); - gettimeofday(&conn_after, NULL); - if (error < 0) - { - printf("999 999\n"); - close(s); - } - else - { - ret = 0; - memset(dht_response2, '\0', sizeof(dht_response2)); - memset(dht_response, '\0', sizeof(dht_response)); - gettimeofday(&stat_before, NULL); - /* TTL just iter * 60 secs so values can be found in get test */ - memset(put_packet, '\0', sizeof(put_packet)); - ret = opendht_put((unsigned char *)key_rand, - (unsigned char *)val_onekilo, - (unsigned char *)host_addr,5851,(iter * 60),put_packet); - ret = opendht_send (s,put_packet); - ret = opendht_read_response(s, dht_response); - gettimeofday(&stat_after, NULL); - close(s); - /* Print findings*/ - conn_diff_sec = (conn_after.tv_sec - conn_before.tv_sec) *1000000; - conn_diff_usec = conn_after.tv_usec - conn_before.tv_usec; - stat_diff_sec = (stat_after.tv_sec - stat_before.tv_sec) *1000000; - stat_diff_usec = stat_after.tv_usec - stat_before.tv_usec; - printf("%.6f %.6f %s\n", - (conn_diff_sec + conn_diff_usec)/1000000.0, - (stat_diff_sec + stat_diff_usec)/1000000.0, - dht_response); - sleep(5); - } - } - } - else if (argv[1][0] == '9') - { - HIP_DEBUG("Get test times with consecutive keys (do number 7 or 8 first," - " otherwise it will be num 2)\n"); - HIP_DEBUG("Printing \"connection time; get time; DHT answer\n"); - HIP_DEBUG("(0 = OK, 1 = error, 2 = retry, or some value " - "(printing just first character, its just 985 'a's))\n"); - HIP_DEBUG("Doing %s iterations\n", argv[2]); - srand(time(NULL)); - int ra = 0; - for (n = 0; n < iter; n++) - { - HIP_DEBUG("Iteration no %d\n",n); - /* consecutive key instead of random as the variable says */ - ra= (n + 1) * 1000000; - sprintf(key_rand, "%.d", ra); - HIP_DEBUG("Consecutive key %s\n", key_rand); - s = init_dht_gateway_socket_gw(s, serving_gateway); - gettimeofday(&conn_before, NULL); - error = connect_dht_gateway(s, serving_gateway, 1); - gettimeofday(&conn_after, NULL); - if (error < 0) - { - printf("999 999\n"); - close(s); - } - else - { - ret = 0; - memset(dht_response2, '\0', sizeof(dht_response2)); - memset(dht_response, '\0', sizeof(dht_response)); - gettimeofday(&stat_before, NULL); - ret = opendht_get(s, (unsigned char *)key_rand, - (unsigned char *)host_addr, 5851); - ret = opendht_read_response(s, dht_response); - gettimeofday(&stat_after, NULL); - close(s); - /* Print findings*/ - conn_diff_sec = (conn_after.tv_sec - conn_before.tv_sec) *1000000; - conn_diff_usec = conn_after.tv_usec - conn_before.tv_usec; - stat_diff_sec = (stat_after.tv_sec - stat_before.tv_sec) *1000000; - stat_diff_usec = stat_after.tv_usec - stat_before.tv_usec; - printf("%.6f %.6f %s\n", - (conn_diff_sec + conn_diff_usec)/1000000.0, - (stat_diff_sec + stat_diff_usec)/1000000.0, - dht_response); - } - } - - } - else if (argv[1][0] == 'a') - { - HIP_DEBUG("Rm test times, put_removable, rm, put_removable\n" - "get (check that it is the new one you get)\n" - "sleep for rm ttl again...\n"); - HIP_DEBUG("Printing \"put time; rm time; put time; DHT answer\n"); - HIP_DEBUG("(0 = OK, 1 = error, 2 = retry, or some value)\n"); - HIP_DEBUG("Doing %s iterations\n", argv[2]); - - for (n = 0; n < iter; n++) - { - HIP_DEBUG("Iteration no %d\n",n); - /* first put removabe */ - s = init_dht_gateway_socket_gw(s, serving_gateway); - gettimeofday(&put_rm_before, NULL); - error = connect_dht_gateway(s, serving_gateway, 1); - if (error < 0) - { - printf("9999 999 999\n"); - close(s); - } - else - { - ret = 0; - memset(dht_response2, '\0', sizeof(dht_response2)); - ret = opendht_put_rm(s, - (unsigned char *)val_host_test, - (unsigned char *)val_something, - (unsigned char *)secret_str, - (unsigned char *)host_addr,5851,20); - ret = opendht_read_response(s, dht_response2); - gettimeofday(&put_rm_after, NULL); - if (ret == -1) exit(1); - close(s); - /* removing the value */ - s = init_dht_gateway_socket_gw(s, serving_gateway); - gettimeofday(&rm_before, NULL); - error = connect_dht_gateway(s, serving_gateway, 1); - if (error < 0) - { - printf("999 9999 999\n"); - close(s); - } - else - { - ret = 0; - memset(dht_response2, '\0', sizeof(dht_response2)); - ret = opendht_rm(s, - (unsigned char *)val_host_test, - (unsigned char *)val_something, - (unsigned char *)secret_str, - (unsigned char *)host_addr,5851,20); - ret = opendht_read_response(s, dht_response2); - gettimeofday(&rm_after, NULL); - if (ret == -1) exit(1); - close(s); - /* putting a new value */ - - s = init_dht_gateway_socket_gw(s, serving_gateway); - gettimeofday(&put_rm2_before, NULL); - error = connect_dht_gateway(s, serving_gateway, 1); - if (error < 0) - { - printf("999 999 9999\n"); - close(s); - } - else - { - ret = 0; - memset(dht_response2, '\0', sizeof(dht_response2)); - ret = opendht_put_rm(s, - (unsigned char *)val_host_test, - (unsigned char *)val_something, - (unsigned char *)secret_str, - (unsigned char *)host_addr, - 5851,20); - ret = opendht_read_response(s, dht_response2); - gettimeofday(&put_rm2_after, NULL); - if (ret == -1) exit(1); - close(s); - - /* Print findings*/ - put_rm_diff_sec = (put_rm_after.tv_sec - - put_rm_before.tv_sec) *1000000; - put_rm_diff_usec = (put_rm_after.tv_usec - - put_rm_before.tv_usec); - - rm_diff_sec = (rm_after.tv_sec - - rm_before.tv_sec) *1000000; - rm_diff_usec = (rm_after.tv_usec - - rm_before.tv_usec); - - put_rm2_diff_sec = (put_rm2_after.tv_sec - - put_rm2_before.tv_sec) *1000000; - put_rm2_diff_usec = (put_rm2_after.tv_usec - - put_rm2_before.tv_usec); - - printf("%.6f %.6f %.6f %s\n", - ((put_rm_diff_sec + put_rm_diff_usec) - /1000000.0), - ((rm_diff_sec + rm_diff_usec)/1000000.0), - ((put_rm2_diff_sec + - put_rm2_diff_usec)/1000000.0), - dht_response2); - HIP_DEBUG("sleeping for 30 secs to get rid off " - "old values and removes\n"); - sleep(30); - } - } - } - } - - } - else - { - HIP_DEBUG("Unknown parameter, %s\n", argv[1]); - } + if (argv[1][0] == '0') { + HIP_DEBUG("Starting to test the openDHT interface.\n"); + HIP_DEBUG("Using test mapping\n'%s (FQDN) -> %s (HIT) -> %s (IP)'.\n", + val_host, val_hit, val_ip); + + /*!!!! put fqdn->hit !!!!*/ + s = init_dht_gateway_socket_gw(s, serving_gateway); + error = 0; + error = connect_dht_gateway(s, serving_gateway, 1); + if (error < 0) { + exit(0); + } + ret = 0; + memset(put_packet, '\0', sizeof(put_packet)); + ret = opendht_put( + (unsigned char *) val_host, + (unsigned char *) val_hit, + (unsigned char *) host_addr, port, ttl, put_packet); + ret = opendht_send(s, put_packet); + if (ret == -1) { + exit(1); + } + ret = opendht_read_response(s, dht_response); + if (ret == -1) { + exit(1); + } + HIP_DEBUG("Put packet (fqdn->hit) sent and ...\n"); + HIP_DEBUG("Put was success\n"); + close(s); + /*!!!! put hit->ip !!!!*/ + + s = init_dht_gateway_socket_gw(s, serving_gateway); + error = connect_dht_gateway(s, serving_gateway, 1); + if (error < 0) { + exit(0); + } + ret = 0; + memset(put_packet, '\0', sizeof(put_packet)); + ret = opendht_put( + (unsigned char *) val_hit, + (unsigned char *) val_ip, + (unsigned char *) host_addr, port, ttl, put_packet); + ret = opendht_send(s, put_packet); + if (ret == -1) { + exit(1); + } + ret = opendht_read_response(s, dht_response); + if (ret == -1) { + exit(1); + } + HIP_DEBUG("Put packet (hit->ip) sent and ...\n"); + HIP_DEBUG("Put was success\n", dht_response); + close(s); + + /*!!!! get fqdn !!!!*/ + + s = init_dht_gateway_socket_gw(s, serving_gateway); + error = connect_dht_gateway(s, serving_gateway, 1); + if (error < 0) { + exit(0); + } + ret = 0; + memset(dht_response, '\0', sizeof(dht_response)); + ret = opendht_get(s, (unsigned char *) val_host, + (unsigned char *) host_addr, port); + ret = opendht_read_response(s, dht_response); + ret = handle_hit_value(dht_response, (void *) dht_response2); + // if (ret == -1) exit (1); + HIP_DEBUG("Get packet (fqdn) sent and ...\n"); + if (ret == 0) { + HIP_DEBUG("Teststub: Value received from DHT: %s\n", dht_response2); + if (!strcmp((char *) dht_response2, (char *) val_hit)) { + HIP_DEBUG("Did match the sent value.\n"); + } else { + HIP_DEBUG("Did NOT match the sent value!\n"); + } + } + close(s); + + /*!!!! get hit !!!!*/ + + s = init_dht_gateway_socket_gw(s, serving_gateway); + error = connect_dht_gateway(s, serving_gateway, 1); + if (error < 0) { + exit(0); + } + ret = 0; + memset(dht_response2, '\0', sizeof(dht_response2)); + ret = opendht_get(s, (unsigned char *) val_hit, + (unsigned char *) host_addr, port); + ret = opendht_read_response(s, dht_response2); + memset(dht_response, '\0', sizeof(dht_response)); + hip_in6_ntop((struct in6_addr *) dht_response2, + (char *) dht_response); + HIP_DEBUG("Value: %s\n", (char *) dht_response); + if (ret == -1) { + exit(1); + } + HIP_DEBUG("Get packet (hit) sent and ...\n"); + if (ret == 0) { + HIP_DEBUG("Teststub: Value received from DHT: %s\n", dht_response); + if (!strcmp((char *) dht_response, val_ip)) { + HIP_DEBUG("Did match the sent value.\n"); + } else { + HIP_DEBUG("Did NOT match the sent value!\n"); + } + } + close(s); + + /* Finally let's try to get a key that doesn't exist */ + + s = init_dht_gateway_socket_gw(s, serving_gateway); + error = connect_dht_gateway(s, serving_gateway, 1); + if (error < 0) { + exit(0); + } + ret = 0; + memset(dht_response2, '\0', sizeof(dht_response2)); + ret = opendht_get(s, (unsigned char *) val_bogus, + (unsigned char *) host_addr, port); + ret = opendht_read_response(s, dht_response2); + // if (ret == -1) exit (1); + HIP_DEBUG("Get packet (bogus, will not be found (hopefully)) sent and ...\n"); + HIP_DEBUG("Teststub: Value received from DHT: %s\n", dht_response2); + close(s); + + /* put_removable and rm tests */ + + /* put_removable */ + HIP_DEBUG("\n\nPut removable starts\n"); + s = init_dht_gateway_socket_gw(s, serving_gateway); + error = connect_dht_gateway(s, serving_gateway, 1); + if (error < 0) { + exit(0); + } + ret = 0; + memset(dht_response2, '\0', sizeof(dht_response2)); + ret = opendht_put_rm(s, + (unsigned char *) val_host_test, + (unsigned char *) val_something, + (unsigned char *) secret_str, + (unsigned char *) host_addr, port, ttl); + ret = opendht_read_response(s, dht_response2); + if (ret == -1) { + exit(1); + } + HIP_DEBUG("Put(rm) packet (fqdn->hit) sent and ...\n"); + HIP_DEBUG("Put(rm) was success\n"); + close(s); + /* check that value exists */ + s = init_dht_gateway_socket_gw(s, serving_gateway); + error = connect_dht_gateway(s, serving_gateway, 1); + if (error < 0) { + exit(0); + } + ret = 0; + memset(dht_response2, '\0', sizeof(dht_response2)); + ret = opendht_get(s, (unsigned char *) val_host_test, + (unsigned char *) host_addr, port); + ret = opendht_read_response(s, dht_response2); + // if (ret == -1) exit (1); + HIP_DEBUG("Get packet sent and (value should be found, just sent it)...\n"); + HIP_DEBUG("Value received from DHT: %s\n", dht_response2); + close(s); + /* send remove */ + s = init_dht_gateway_socket_gw(s, serving_gateway); + error = connect_dht_gateway(s, serving_gateway, 1); + if (error < 0) { + exit(0); + } + ret = 0; + memset(dht_response2, '\0', sizeof(dht_response2)); + ret = opendht_rm(s, + (unsigned char *) val_host_test, + (unsigned char *) val_something, + (unsigned char *) secret_str, + (unsigned char *) host_addr, port, ttl); + ret = opendht_read_response(s, dht_response2); + if (ret == -1) { + exit(1); + } + HIP_DEBUG("Rm packet sent and ...\n"); + HIP_DEBUG("Rm was success\n"); + close(s); + /* can you get it anymore */ + + s = init_dht_gateway_socket_gw(s, serving_gateway); + error = connect_dht_gateway(s, serving_gateway, 1); + if (error < 0) { + exit(0); + } + ret = 0; + memset(dht_response2, '\0', sizeof(dht_response2)); + ret = opendht_get(s, (unsigned char *) val_host_test, + (unsigned char *) host_addr, port); + ret = opendht_read_response(s, dht_response2); + // if (ret == -1) exit (1); + HIP_DEBUG("Get packet (was removed, will not be found (hopefully)) sent and ...\n"); + HIP_DEBUG("Teststub: Value received from DHT: %s\n", dht_response2); + close(s); + + /* testing a wrapper for blocking dht call */ + memset(dht_response, '\0', sizeof(dht_response)); + ret = 0; + HIP_DEBUG("\n\nTrying out get wrapper\n"); + + ret = hip_opendht_get_key(&handle_ip_value, serving_gateway, + (char *) val_hit, (char *) dht_response, 1); + + if (!ret) { + HIP_DEBUG("DHT get succeeded\n"); + } else { + HIP_DEBUG("DHT get was unsuccesfull\n"); + } + + /* basic testing done */ + exit(EXIT_SUCCESS); + } else if (argv[1][0] == '1') { + HIP_DEBUG("Get test times when value not found\n"); + HIP_DEBUG("Printing \"connection time; get time; DHT answer (should be empty here)\n"); + HIP_DEBUG("Doing %s iterations\n", argv[2]); + + for (n = 0; n < iter; n++) { + HIP_DEBUG("Iteration no %d\n", n); + s = init_dht_gateway_socket_gw(s, serving_gateway); + gettimeofday(&conn_before, NULL); + error = connect_dht_gateway(s, serving_gateway, 1); + gettimeofday(&conn_after, NULL); + if (error < 0) { + printf("999 999\n"); + close(s); + } else { + ret = 0; + memset(dht_response2, '\0', sizeof(dht_response2)); + gettimeofday(&stat_before, NULL); + ret = opendht_get(s, (unsigned char *) val_bogus, + (unsigned char *) host_addr, 5851); + ret = opendht_read_response(s, dht_response2); + gettimeofday(&stat_after, NULL); + close(s); + /* Print findings*/ + conn_diff_sec = (conn_after.tv_sec - conn_before.tv_sec) * 1000000; + conn_diff_usec = conn_after.tv_usec - conn_before.tv_usec; + stat_diff_sec = (stat_after.tv_sec - stat_before.tv_sec) * 1000000; + stat_diff_usec = stat_after.tv_usec - stat_before.tv_usec; + printf("%.6f %.6f %s\n", + (conn_diff_sec + conn_diff_usec) / 1000000.0, + (stat_diff_sec + stat_diff_usec) / 1000000.0, + dht_response2); + } + } + } else if (argv[1][0] == '2') { + HIP_DEBUG("Get test times when value is found\n"); + HIP_DEBUG("Printing \"connection time; get time; DHT answer " + "(0 = OK, 1 = error, 2 = retry, or some value)\n"); + HIP_DEBUG("Doing %s iterations\n", argv[2]); + + s = init_dht_gateway_socket_gw(s, serving_gateway); + error = connect_dht_gateway(s, serving_gateway, 1); + if (error < 0) { + exit(0); + } + ret = 0; + /* iterations by estimate seconds, so the value is there long enough */ + memset(put_packet, '\0', sizeof(put_packet)); + ret = opendht_put((unsigned char *) val_hit, + (unsigned char *) val_ip, + (unsigned char *) host_addr, + 5851, (iter * 3), put_packet); + ret = opendht_send(s, put_packet); + ret = opendht_read_response(s, dht_response); + if (ret == -1) { + exit(1); + } + HIP_DEBUG("Put packet (hit->ip) sent and ...\n"); + HIP_DEBUG("Put was success\n", dht_response); + close(s); + + for (n = 0; n < iter; n++) { + HIP_DEBUG("Iteration no %d\n", n); + s = init_dht_gateway_socket_gw(s, serving_gateway); + gettimeofday(&conn_before, NULL); + error = connect_dht_gateway(s, serving_gateway, 1); + gettimeofday(&conn_after, NULL); + if (error < 0) { + printf("999 999\n"); + close(s); + } else { + ret = 0; + memset(dht_response2, '\0', sizeof(dht_response2)); + gettimeofday(&stat_before, NULL); + ret = opendht_get(s, (unsigned char *) val_hit, + (unsigned char *) host_addr, 5851); + ret = opendht_read_response(s, dht_response2); + gettimeofday(&stat_after, NULL); + close(s); + /* Print findings*/ + conn_diff_sec = (conn_after.tv_sec - conn_before.tv_sec) * 1000000; + conn_diff_usec = conn_after.tv_usec - conn_before.tv_usec; + stat_diff_sec = (stat_after.tv_sec - stat_before.tv_sec) * 1000000; + stat_diff_usec = stat_after.tv_usec - stat_before.tv_usec; + printf("%.6f %.6f %s\n", + (conn_diff_sec + conn_diff_usec) / 1000000.0, + (stat_diff_sec + stat_diff_usec) / 1000000.0, + dht_response2); + } + } + } else if (argv[1][0] == '3') { + HIP_DEBUG("Put test times with 10 byte value (same key)\n"); + HIP_DEBUG("Printing \"connection time; get time; DHT answer " + "(0 = OK, 1 = error, 2 = retry, or some value)\n"); + HIP_DEBUG("Doing %s iterations\n", argv[2]); + + for (n = 0; n < iter; n++) { + HIP_DEBUG("Iteration no %d\n", n); + s = init_dht_gateway_socket_gw(s, serving_gateway); + gettimeofday(&conn_before, NULL); + error = connect_dht_gateway(s, serving_gateway, 1); + gettimeofday(&conn_after, NULL); + if (error < 0) { + printf("999 999\n"); + close(s); + } else { + ret = 0; + memset(dht_response2, '\0', sizeof(dht_response2)); + gettimeofday(&stat_before, NULL); + /* TTL just 20 secs */ + memset(put_packet, '\0', sizeof(put_packet)); + ret = opendht_put((unsigned char *) key_test, + (unsigned char *) val_tenbyte, + (unsigned char *) host_addr, + 5851, 20, put_packet); + ret = opendht_send(s, put_packet); + ret = opendht_read_response(s, dht_response); + gettimeofday(&stat_after, NULL); + close(s); + /* Print findings*/ + conn_diff_sec = (conn_after.tv_sec - conn_before.tv_sec) * 1000000; + conn_diff_usec = conn_after.tv_usec - conn_before.tv_usec; + stat_diff_sec = (stat_after.tv_sec - stat_before.tv_sec) * 1000000; + stat_diff_usec = stat_after.tv_usec - stat_before.tv_usec; + printf("%.6f %.6f %s\n", + (conn_diff_sec + conn_diff_usec) / 1000000.0, + (stat_diff_sec + stat_diff_usec) / 1000000.0, + dht_response); + } + } + } else if (argv[1][0] == '4') { + HIP_DEBUG("Put test times with 10 byte value, waiting " + "5 sec in between puts (same key)\n"); + HIP_DEBUG("Printing \"connection time; get time; DHT answer " + "(0 = OK, 1 = error, 2 = retry, or some value)\n"); + HIP_DEBUG("Doing %s iterations\n", argv[2]); + + for (n = 0; n < iter; n++) { + HIP_DEBUG("Iteration no %d\n", n); + s = init_dht_gateway_socket_gw(s, serving_gateway); + gettimeofday(&conn_before, NULL); + error = connect_dht_gateway(s, serving_gateway, 1); + gettimeofday(&conn_after, NULL); + if (error < 0) { + printf("999 999\n"); + close(s); + } else { + ret = 0; + memset(dht_response2, '\0', sizeof(dht_response2)); + gettimeofday(&stat_before, NULL); + /* TTL just 20 secs */ + memset(put_packet, '\0', sizeof(put_packet)); + ret = opendht_put((unsigned char *) key_test, + (unsigned char *) val_tenbyte, + (unsigned char *) host_addr, + 5851, 20, put_packet); + ret = opendht_send(s, put_packet); + ret = opendht_read_response(s, dht_response); + gettimeofday(&stat_after, NULL); + close(s); + /* Print findings*/ + conn_diff_sec = (conn_after.tv_sec - conn_before.tv_sec) * 1000000; + conn_diff_usec = conn_after.tv_usec - conn_before.tv_usec; + stat_diff_sec = (stat_after.tv_sec - stat_before.tv_sec) * 1000000; + stat_diff_usec = stat_after.tv_usec - stat_before.tv_usec; + printf("%.6f %.6f %s\n", + (conn_diff_sec + conn_diff_usec) / 1000000.0, + (stat_diff_sec + stat_diff_usec) / 1000000.0, + dht_response); + sleep(5); + } + } + } else if (argv[1][0] == '5') { + HIP_DEBUG("Put test times with 10 byte value (random key, short TTL)\n"); + HIP_DEBUG("Printing \"connection time; get time; DHT answer " + "(0 = OK, 1 = error, 2 = retry, or some value)\n"); + HIP_DEBUG("Doing %s iterations\n", argv[2]); + + srand(time(NULL)); + int ra = 0; + for (n = 0; n < iter; n++) { + HIP_DEBUG("Iteration no %d\n", n); + ra = rand() % 1000000000; + sprintf(key_rand, "%.d", ra); + HIP_DEBUG("random key %s\n", key_rand); + s = init_dht_gateway_socket_gw(s, serving_gateway); + gettimeofday(&conn_before, NULL); + error = connect_dht_gateway(s, serving_gateway, 1); + gettimeofday(&conn_after, NULL); + if (error < 0) { + printf("999 999\n"); + close(s); + } else { + ret = 0; + memset(dht_response2, '\0', sizeof(dht_response2)); + gettimeofday(&stat_before, NULL); + /* TTL just 20 secs */ + memset(put_packet, '\0', sizeof(put_packet)); + ret = opendht_put((unsigned char *) key_rand, + (unsigned char *) val_tenbyte, + (unsigned char *) host_addr, + 5851, 20, put_packet); + ret = opendht_send(s, put_packet); + ret = opendht_read_response(s, dht_response); + gettimeofday(&stat_after, NULL); + close(s); + /* Print findings*/ + conn_diff_sec = (conn_after.tv_sec - conn_before.tv_sec) * 1000000; + conn_diff_usec = conn_after.tv_usec - conn_before.tv_usec; + stat_diff_sec = (stat_after.tv_sec - stat_before.tv_sec) * 1000000; + stat_diff_usec = stat_after.tv_usec - stat_before.tv_usec; + printf("%.6f %.6f %s\n", + (conn_diff_sec + conn_diff_usec) / 1000000.0, + (stat_diff_sec + stat_diff_usec) / 1000000.0, + dht_response); + } + } + } else if (argv[1][0] == '6') { + HIP_DEBUG("Put test times with 10 byte value, waiting 5 sec in " + "between puts(random key, short TTL)\n"); + HIP_DEBUG("Printing \"connection time; get time; DHT answer " + "(0 = OK, 1 = error, 2 = retry, or some value)\n"); + HIP_DEBUG("Doing %s iterations\n", argv[2]); + srand(time(NULL)); + int ra = 0; + for (n = 0; n < iter; n++) { + HIP_DEBUG("Iteration no %d\n", n); + ra = rand() % 1000000000; + sprintf(key_rand, "%.d", ra); + HIP_DEBUG("random key %s\n", key_rand); + s = init_dht_gateway_socket_gw(s, serving_gateway); + gettimeofday(&conn_before, NULL); + error = connect_dht_gateway(s, serving_gateway, 1); + gettimeofday(&conn_after, NULL); + if (error < 0) { + printf("999 999\n"); + close(s); + } else { + ret = 0; + memset(dht_response2, '\0', sizeof(dht_response2)); + gettimeofday(&stat_before, NULL); + /* TTL just 20 secs */ + memset(put_packet, '\0', sizeof(put_packet)); + ret = opendht_put((unsigned char *) key_rand, + (unsigned char *) val_tenbyte, + (unsigned char *) host_addr, 5851, 20, put_packet); + ret = opendht_send(s, put_packet); + ret = opendht_read_response(s, dht_response); + gettimeofday(&stat_after, NULL); + close(s); + /* Print findings*/ + conn_diff_sec = (conn_after.tv_sec - conn_before.tv_sec) * 1000000; + conn_diff_usec = conn_after.tv_usec - conn_before.tv_usec; + stat_diff_sec = (stat_after.tv_sec - stat_before.tv_sec) * 1000000; + stat_diff_usec = stat_after.tv_usec - stat_before.tv_usec; + printf("%.6f %.6f %s\n", + (conn_diff_sec + conn_diff_usec) / 1000000.0, + (stat_diff_sec + stat_diff_usec) / 1000000.0, + dht_response); + sleep(5); + } + } + } else if (argv[1][0] == '7') { + memset(val_onekilo, 'a', sizeof(val_onekilo)); + HIP_DEBUG("Put test times with consecutive keys and 985 byte values\n"); + HIP_DEBUG("Printing \"connection time; get time; DHT answer " + "(0 = OK, 1 = error, 2 = retry, or some value)\n"); + HIP_DEBUG("Doing %s iterations\n", argv[2]); + srand(time(NULL)); + int ra = 0; + for (n = 0; n < iter; n++) { + HIP_DEBUG("Iteration no %d\n", n); + /* consecutive key instead of random as the variable says */ + ra = (n + 1) * 1000000; + sprintf(key_rand, "%.d", ra); + HIP_DEBUG("Consecutive key %s\n", key_rand); + s = init_dht_gateway_socket_gw(s, serving_gateway); + gettimeofday(&conn_before, NULL); + error = connect_dht_gateway(s, serving_gateway, 1); + gettimeofday(&conn_after, NULL); + if (error < 0) { + printf("999 999\n"); + close(s); + } else { + ret = 0; + memset(dht_response2, '\0', sizeof(dht_response2)); + memset(dht_response, '\0', sizeof(dht_response)); + gettimeofday(&stat_before, NULL); + /* TTL just iter * 60 secs so values can be found in get test */ + memset(put_packet, '\0', sizeof(put_packet)); + ret = opendht_put((unsigned char *) key_rand, + (unsigned char *) val_onekilo, + (unsigned char *) host_addr, + 5851, (iter * 60), put_packet); + ret = opendht_send(s, put_packet); + ret = opendht_read_response(s, dht_response); + gettimeofday(&stat_after, NULL); + close(s); + /* Print findings*/ + conn_diff_sec = (conn_after.tv_sec - conn_before.tv_sec) * 1000000; + conn_diff_usec = conn_after.tv_usec - conn_before.tv_usec; + stat_diff_sec = (stat_after.tv_sec - stat_before.tv_sec) * 1000000; + stat_diff_usec = stat_after.tv_usec - stat_before.tv_usec; + printf("%.6f %.6f %s\n", + (conn_diff_sec + conn_diff_usec) / 1000000.0, + (stat_diff_sec + stat_diff_usec) / 1000000.0, + dht_response); + } + } + } else if (argv[1][0] == '8') { + memset(val_onekilo, 'a', sizeof(val_onekilo)); + HIP_DEBUG("Put test times with consecutive keys and 985 byte values" + " with 5 sec sleep between puts\n"); + HIP_DEBUG("Printing \"connection time; get time; DHT answer\n"); + HIP_DEBUG("(0 = OK, 1 = error, 2 = retry, or some value)\n"); + HIP_DEBUG("Doing %s iterations\n", argv[2]); + srand(time(NULL)); + int ra = 0; + for (n = 0; n < iter; n++) { + HIP_DEBUG("Iteration no %d\n", n); + /* consecutive key instead of random as the variable says */ + ra = (n + 1) * 1000000; + sprintf(key_rand, "%.d", ra); + HIP_DEBUG("Consecutive key %s\n", key_rand); + s = init_dht_gateway_socket_gw(s, serving_gateway); + gettimeofday(&conn_before, NULL); + error = connect_dht_gateway(s, serving_gateway, 1); + gettimeofday(&conn_after, NULL); + if (error < 0) { + printf("999 999\n"); + close(s); + } else { + ret = 0; + memset(dht_response2, '\0', sizeof(dht_response2)); + memset(dht_response, '\0', sizeof(dht_response)); + gettimeofday(&stat_before, NULL); + /* TTL just iter * 60 secs so values can be found in get test */ + memset(put_packet, '\0', sizeof(put_packet)); + ret = opendht_put((unsigned char *) key_rand, + (unsigned char *) val_onekilo, + (unsigned char *) host_addr, + 5851, (iter * 60), put_packet); + ret = opendht_send(s, put_packet); + ret = opendht_read_response(s, dht_response); + gettimeofday(&stat_after, NULL); + close(s); + /* Print findings*/ + conn_diff_sec = (conn_after.tv_sec - conn_before.tv_sec) * 1000000; + conn_diff_usec = conn_after.tv_usec - conn_before.tv_usec; + stat_diff_sec = (stat_after.tv_sec - stat_before.tv_sec) * 1000000; + stat_diff_usec = stat_after.tv_usec - stat_before.tv_usec; + printf("%.6f %.6f %s\n", + (conn_diff_sec + conn_diff_usec) / 1000000.0, + (stat_diff_sec + stat_diff_usec) / 1000000.0, + dht_response); + sleep(5); + } + } + } else if (argv[1][0] == '9') { + HIP_DEBUG("Get test times with consecutive keys (do number 7 or 8 first," + " otherwise it will be num 2)\n"); + HIP_DEBUG("Printing \"connection time; get time; DHT answer\n"); + HIP_DEBUG("(0 = OK, 1 = error, 2 = retry, or some value " + "(printing just first character, its just 985 'a's))\n"); + HIP_DEBUG("Doing %s iterations\n", argv[2]); + srand(time(NULL)); + int ra = 0; + for (n = 0; n < iter; n++) { + HIP_DEBUG("Iteration no %d\n", n); + /* consecutive key instead of random as the variable says */ + ra = (n + 1) * 1000000; + sprintf(key_rand, "%.d", ra); + HIP_DEBUG("Consecutive key %s\n", key_rand); + s = init_dht_gateway_socket_gw(s, serving_gateway); + gettimeofday(&conn_before, NULL); + error = connect_dht_gateway(s, serving_gateway, 1); + gettimeofday(&conn_after, NULL); + if (error < 0) { + printf("999 999\n"); + close(s); + } else { + ret = 0; + memset(dht_response2, '\0', sizeof(dht_response2)); + memset(dht_response, '\0', sizeof(dht_response)); + gettimeofday(&stat_before, NULL); + ret = opendht_get(s, (unsigned char *) key_rand, + (unsigned char *) host_addr, 5851); + ret = opendht_read_response(s, dht_response); + gettimeofday(&stat_after, NULL); + close(s); + /* Print findings*/ + conn_diff_sec = (conn_after.tv_sec - conn_before.tv_sec) * 1000000; + conn_diff_usec = conn_after.tv_usec - conn_before.tv_usec; + stat_diff_sec = (stat_after.tv_sec - stat_before.tv_sec) * 1000000; + stat_diff_usec = stat_after.tv_usec - stat_before.tv_usec; + printf("%.6f %.6f %s\n", + (conn_diff_sec + conn_diff_usec) / 1000000.0, + (stat_diff_sec + stat_diff_usec) / 1000000.0, + dht_response); + } + } + } else if (argv[1][0] == 'a') { + HIP_DEBUG("Rm test times, put_removable, rm, put_removable\n" + "get (check that it is the new one you get)\n" + "sleep for rm ttl again...\n"); + HIP_DEBUG("Printing \"put time; rm time; put time; DHT answer\n"); + HIP_DEBUG("(0 = OK, 1 = error, 2 = retry, or some value)\n"); + HIP_DEBUG("Doing %s iterations\n", argv[2]); + + for (n = 0; n < iter; n++) { + HIP_DEBUG("Iteration no %d\n", n); + /* first put removabe */ + s = init_dht_gateway_socket_gw(s, serving_gateway); + gettimeofday(&put_rm_before, NULL); + error = connect_dht_gateway(s, serving_gateway, 1); + if (error < 0) { + printf("9999 999 999\n"); + close(s); + } else { + ret = 0; + memset(dht_response2, '\0', sizeof(dht_response2)); + ret = opendht_put_rm(s, + (unsigned char *) val_host_test, + (unsigned char *) val_something, + (unsigned char *) secret_str, + (unsigned char *) host_addr, 5851, 20); + ret = opendht_read_response(s, dht_response2); + gettimeofday(&put_rm_after, NULL); + if (ret == -1) { + exit(1); + } + close(s); + /* removing the value */ + s = init_dht_gateway_socket_gw(s, serving_gateway); + gettimeofday(&rm_before, NULL); + error = connect_dht_gateway(s, serving_gateway, 1); + if (error < 0) { + printf("999 9999 999\n"); + close(s); + } else { + ret = 0; + memset(dht_response2, '\0', sizeof(dht_response2)); + ret = opendht_rm(s, + (unsigned char *) val_host_test, + (unsigned char *) val_something, + (unsigned char *) secret_str, + (unsigned char *) host_addr, 5851, 20); + ret = opendht_read_response(s, dht_response2); + gettimeofday(&rm_after, NULL); + if (ret == -1) { + exit(1); + } + close(s); + /* putting a new value */ + + s = init_dht_gateway_socket_gw(s, serving_gateway); + gettimeofday(&put_rm2_before, NULL); + error = connect_dht_gateway(s, serving_gateway, 1); + if (error < 0) { + printf("999 999 9999\n"); + close(s); + } else { + ret = 0; + memset(dht_response2, '\0', sizeof(dht_response2)); + ret = opendht_put_rm(s, + (unsigned char *) val_host_test, + (unsigned char *) val_something, + (unsigned char *) secret_str, + (unsigned char *) host_addr, + 5851, 20); + ret = opendht_read_response(s, dht_response2); + gettimeofday(&put_rm2_after, NULL); + if (ret == -1) { + exit(1); + } + close(s); + + /* Print findings*/ + put_rm_diff_sec = (put_rm_after.tv_sec + - put_rm_before.tv_sec) * 1000000; + put_rm_diff_usec = (put_rm_after.tv_usec + - put_rm_before.tv_usec); + + rm_diff_sec = (rm_after.tv_sec + - rm_before.tv_sec) * 1000000; + rm_diff_usec = (rm_after.tv_usec + - rm_before.tv_usec); + + put_rm2_diff_sec = (put_rm2_after.tv_sec + - put_rm2_before.tv_sec) * 1000000; + put_rm2_diff_usec = (put_rm2_after.tv_usec + - put_rm2_before.tv_usec); + + printf("%.6f %.6f %.6f %s\n", + ((put_rm_diff_sec + put_rm_diff_usec) + / 1000000.0), + ((rm_diff_sec + rm_diff_usec) / 1000000.0), + ((put_rm2_diff_sec + + put_rm2_diff_usec) / 1000000.0), + dht_response2); + HIP_DEBUG("sleeping for 30 secs to get rid off " + "old values and removes\n"); + sleep(30); + } + } + } + } + } else { + HIP_DEBUG("Unknown parameter, %s\n", argv[1]); + } return 0; } === modified file 'test/first_test.c' --- test/first_test.c 2010-01-19 09:28:42 +0000 +++ test/first_test.c 2010-02-10 23:43:06 +0000 @@ -21,14 +21,14 @@ #include "lib/core/getendpointinfo.h" const char *usage_str = "first_test -h for help\n" - "first_test to run one base exchange\n" - "first_test -n [NUM] to run NUM times the base exchange\n" - "\n" - ; + "first_test to run one base exchange\n" + "first_test -n [NUM] to run NUM times the base exchange\n" + "\n" +; void usage_f(void) { - printf("Usage:\n%s\n", usage_str); + printf("Usage:\n%s\n", usage_str); } /** @@ -38,38 +38,38 @@ */ int send_bos(void) { - struct hip_common *msg; - int err = 0; - - msg = malloc(HIP_MAX_PACKET); - if (!msg) { - HIP_ERROR("malloc failed\n"); - goto out; - } - hip_msg_init(msg); - - printf("Sending BOS... "); - err = hip_conf_handle_bos(msg, 0, (const char **) NULL, 0, 0); - if (err) { - HIP_ERROR("\nfailed to handle msg\n"); - goto out_err; - } - - if (hip_get_msg_type(msg) == 0) { - err = -1; - goto out_err; - } - - err = hip_send_recv_daemon_info(msg, 0, 0); - if (err) { - HIP_ERROR("\nsending msg failed\n"); - goto out_err; - } - printf(" SENT\n"); + struct hip_common *msg; + int err = 0; + + msg = malloc(HIP_MAX_PACKET); + if (!msg) { + HIP_ERROR("malloc failed\n"); + goto out; + } + hip_msg_init(msg); + + printf("Sending BOS... "); + err = hip_conf_handle_bos(msg, 0, (const char **) NULL, 0, 0); + if (err) { + HIP_ERROR("\nfailed to handle msg\n"); + goto out_err; + } + + if (hip_get_msg_type(msg) == 0) { + err = -1; + goto out_err; + } + + err = hip_send_recv_daemon_info(msg, 0, 0); + if (err) { + HIP_ERROR("\nsending msg failed\n"); + goto out_err; + } + printf(" SENT\n"); out_err: - free(msg); + free(msg); out: - return err; + return err; } /** @@ -79,25 +79,25 @@ */ int u_install_modules(void) { - struct hip_common *msg; - int err = 0; - /* Initializing the msg for installing the modules */ - msg = malloc(HIP_MAX_PACKET); - if (!msg) { - HIP_ERROR("malloc failed\n"); - err = -1; - goto out; - } - hip_msg_init(msg); + struct hip_common *msg; + int err = 0; + /* Initializing the msg for installing the modules */ + msg = malloc(HIP_MAX_PACKET); + if (!msg) { + HIP_ERROR("malloc failed\n"); + err = -1; + goto out; + } + hip_msg_init(msg); - err = main_install(msg); - if (err) - goto out_err; + err = main_install(msg); + if (err) { + goto out_err; + } out_err: - free(msg); + free(msg); out: - return err; - + return err; } /** @@ -111,158 +111,164 @@ */ int handle_single_connection(void) { - int err = 0, sock = 0; - - struct in6_addr my_hit; - struct addrinfo hints, *res = NULL; - char buf[20]; - - if (u_install_modules()) - goto out_err; - printf("\n"); - sleep(5); - - /* Retrieving our HIT */ - memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_flags = AI_HIP | AI_PASSIVE; - hints.ai_family = AF_INET6; /* Legacy API supports only HIT-in-IPv6 */ - hints.ai_socktype = SOCK_STREAM; - sprintf(buf, "%d",DEFAULT_PORT); - err = getaddrinfo(NULL, buf, &hints, &res); - - if (err) { - printf("GAI ERROR %d: %s\n", err, gai_strerror(err)); - return(1); - } - if (res) { - struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) res->ai_addr; - ipv6_addr_copy(&my_hit, &sin6->sin6_addr); - /* Now that I have the HIT I can free the memory allocated for res */ - freeaddrinfo(res); - res = NULL; - } else { - HIP_ERROR("Cannot find my HIT\n"); - goto out_err; - } - - /* By calling getaddrinfo with the flags set to AI_HIP | AI_KERNEL_LIST, the beet database - * is scrolled and if any entry is found, then it means that the BOS packets has been received - */ - hints.ai_flags = AI_HIP | AI_KERNEL_LIST; - - while (!res) { - /* BOS */ - if (send_bos()) - goto out_err; - sleep(5); - - err = getaddrinfo(NULL, buf, &hints, &res); - if (err < 0) { - printf("GAI ERROR %d: %s\n", err, gai_strerror(err)); - return(1); - } else if (err > 0) { - /*The BOS packet has been received because at least one entry has been found*/ - struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) res->ai_addr; - - HIP_HEXDUMP("sin6->sin6_addr is: ", &sin6->sin6_addr, 16); - - if (ipv6_addr_cmp(&my_hit, &sin6->sin6_addr) > 0) { - /* my_hit is greater ---> I am the initiator */ - printf("Initiator mode ...\n"); - - hip_set_logtype(LOGTYPE_STDERR); - hip_set_logfmt(LOGFMT_SHORT); - - hip_connect_func(res, &sock); - if (sock) - close(sock); - - } else { - int peer, serversock; - unsigned int peerlen = sizeof(struct sockaddr_in6); - struct sockaddr_in6 peeraddr; - /* my_hit is smaller ---> I am the responder */ - printf("Responder mode ...\n"); - - serversock = create_serversocket(SOCK_STREAM, DEFAULT_PORT); - /* Base Exchange Responder */ - peer = accept(serversock, (struct sockaddr *)&peeraddr, &peerlen); - if (peer < 0) { - perror("accept"); - if (res) - freeaddrinfo(res); - goto out_err; - } - close(peer); - close(serversock); - } - printf("Connection closed\n"); - } else { - - if (res) - freeaddrinfo(res); - res = NULL; - } - } - - if (res) - freeaddrinfo(res); - + int err = 0, sock = 0; + + struct in6_addr my_hit; + struct addrinfo hints, *res = NULL; + char buf[20]; + + if (u_install_modules()) { + goto out_err; + } + printf("\n"); + sleep(5); + + /* Retrieving our HIT */ + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_flags = AI_HIP | AI_PASSIVE; + hints.ai_family = AF_INET6; /* Legacy API supports only HIT-in-IPv6 */ + hints.ai_socktype = SOCK_STREAM; + sprintf(buf, "%d", DEFAULT_PORT); + err = getaddrinfo(NULL, buf, &hints, &res); + + if (err) { + printf("GAI ERROR %d: %s\n", err, gai_strerror(err)); + return 1; + } + if (res) { + struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) res->ai_addr; + ipv6_addr_copy(&my_hit, &sin6->sin6_addr); + /* Now that I have the HIT I can free the memory allocated for res */ + freeaddrinfo(res); + res = NULL; + } else { + HIP_ERROR("Cannot find my HIT\n"); + goto out_err; + } + + /* By calling getaddrinfo with the flags set to AI_HIP | AI_KERNEL_LIST, the + * beet database is scrolled and if any entry is found, then it means that + * the BOS packets has been received + */ + hints.ai_flags = AI_HIP | AI_KERNEL_LIST; + + while (!res) { + /* BOS */ + if (send_bos()) { + goto out_err; + } + sleep(5); + + err = getaddrinfo(NULL, buf, &hints, &res); + if (err < 0) { + printf("GAI ERROR %d: %s\n", err, gai_strerror(err)); + return 1; + } else if (err > 0) { + /* The BOS packet has been received because at least one entry has + * been found*/ + struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) res->ai_addr; + + HIP_HEXDUMP("sin6->sin6_addr is: ", &sin6->sin6_addr, 16); + + if (ipv6_addr_cmp(&my_hit, &sin6->sin6_addr) > 0) { + /* my_hit is greater ---> I am the initiator */ + printf("Initiator mode ...\n"); + + hip_set_logtype(LOGTYPE_STDERR); + hip_set_logfmt(LOGFMT_SHORT); + + hip_connect_func(res, &sock); + if (sock) { + close(sock); + } + } else { + int peer, serversock; + unsigned int peerlen = sizeof(struct sockaddr_in6); + struct sockaddr_in6 peeraddr; + /* my_hit is smaller ---> I am the responder */ + printf("Responder mode ...\n"); + + serversock = create_serversocket(SOCK_STREAM, DEFAULT_PORT); + /* Base Exchange Responder */ + peer = accept(serversock, (struct sockaddr *) &peeraddr, + &peerlen); + if (peer < 0) { + perror("accept"); + if (res) { + freeaddrinfo(res); + } + goto out_err; + } + close(peer); + close(serversock); + } + printf("Connection closed\n"); + } else { + if (res) { + freeaddrinfo(res); + } + res = NULL; + } + } + + if (res) { + freeaddrinfo(res); + } + out_err: - return err; + return err; } /** * main: if the number of times the connection has to be estalibhed (n option) - * only one base exchange is run. Otherwise the connection is established Ntimes times. - * At the end, if everything goes well, the connection is kept established. - * At the moment, in order to restart the connection, the modules and HIP daemon - * are reset. + * only one base exchange is run. Otherwise the connection is established Ntimes + * times. At the end, if everything goes well, the connection is kept + * established. At the moment, in order to restart the connection, the modules + * and HIP daemon are reset. * * Returns:0 on success, non-zero value on error */ int main(int argc, char *argv[]) { - int c, err = 0, Ntimes = 1, i; - - if(argc > 3){ - printf("Number of args wrong \n"); - usage_f(); - return 0; - } - - while ((c = getopt(argc, argv, ":hn:")) != -1) - { - switch (c){ - case 'h': - usage_f(); - goto out_err; - case 'n': - Ntimes = atoi(optarg); - printf("NTimes = %d\n", Ntimes); - if (Ntimes <= 0) { - HIP_ERROR("The specified number cannot be negative or zero\n"); - goto out_err; - } - break; - case ':': - /*Missing arguments*/ - Ntimes = 1; - break; - case '?': - printf("Unknown option %c\n", optopt); - usage_f(); - goto out_err; - } - } - - for (i = 0; i < Ntimes; i++) { - err = handle_single_connection(); - if (!err) - goto out_err; - } + int c, err = 0, Ntimes = 1, i; + + if (argc > 3) { + printf("Number of args wrong \n"); + usage_f(); + return 0; + } + + while ((c = getopt(argc, argv, ":hn:")) != -1) { + switch (c) { + case 'h': + usage_f(); + goto out_err; + case 'n': + Ntimes = atoi(optarg); + printf("NTimes = %d\n", Ntimes); + if (Ntimes <= 0) { + HIP_ERROR("The specified number cannot be negative or zero\n"); + goto out_err; + } + break; + case ':': + /*Missing arguments*/ + Ntimes = 1; + break; + case '?': + printf("Unknown option %c\n", optopt); + usage_f(); + goto out_err; + } + } + + for (i = 0; i < Ntimes; i++) { + err = handle_single_connection(); + if (!err) { + goto out_err; + } + } out_err: - return err; - + return err; } === modified file 'test/hadb_deprecated.c' --- test/hadb_deprecated.c 2010-02-08 15:18:06 +0000 +++ test/hadb_deprecated.c 2010-02-10 23:43:06 +0000 @@ -1,221 +1,228 @@ /* Set the ifindex of given SPI */ /* assumes locked HA */ -void hip_hadb_set_spi_ifindex_deprecated(hip_ha_t *entry, uint32_t spi, int ifindex) +void hip_hadb_set_spi_ifindex_deprecated(hip_ha_t *entry, + uint32_t spi, + int ifindex) { - struct hip_spi_in_item *spi_item; - hip_list_t *item, *tmp; - int i; + struct hip_spi_in_item *spi_item; + hip_list_t *item, *tmp; + int i; - /* assumes that inbound spi already exists in ha's spis_in_old */ - HIP_DEBUG("SPI=0x%x ifindex=%d\n", spi, ifindex); - list_for_each_safe(item, tmp, entry->spis_in_old, i) - { - spi_item = list_entry(item); - _HIP_DEBUG("test item: ifindex=%d spi=0x%x\n", spi_item->ifindex, spi_item->spi); - if (spi_item->spi == spi) - { - HIP_DEBUG("found updated spi-ifindex mapping\n"); - spi_item->ifindex = ifindex; - return; - } - } - HIP_DEBUG("SPI not found, returning\n"); + /* assumes that inbound spi already exists in ha's spis_in_old */ + HIP_DEBUG("SPI=0x%x ifindex=%d\n", spi, ifindex); + list_for_each_safe(item, tmp, entry->spis_in_old, i) + { + spi_item = list_entry(item); + _HIP_DEBUG("test item: ifindex=%d spi=0x%x\n", + spi_item->ifindex, spi_item->spi); + if (spi_item->spi == spi) { + HIP_DEBUG("found updated spi-ifindex mapping\n"); + spi_item->ifindex = ifindex; + return; + } + } + HIP_DEBUG("SPI not found, returning\n"); } /* Get the ifindex of given SPI, returns 0 if SPI was not found */ int hip_hadb_get_spi_ifindex_deprecated(hip_ha_t *entry, uint32_t spi) { - struct hip_spi_in_item *spi_item; - hip_list_t *item, *tmp; - int i; + struct hip_spi_in_item *spi_item; + hip_list_t *item, *tmp; + int i; - _HIP_DEBUG("spi=0x%x\n", spi); - list_for_each_safe(item, tmp, entry->spis_in_old, i) - { - spi_item = list_entry(item); - _HIP_DEBUG("test item: ifindex=%d spi=0x%x\n", spi_item->ifindex, spi_item->spi); - if (spi_item->spi == spi || spi_item->new_spi == spi) - { - _HIP_DEBUG("found\n"); - return spi_item->ifindex; - } - } - HIP_DEBUG("ifindex not found for the SPI 0x%x\n", spi); - return 0; + _HIP_DEBUG("spi=0x%x\n", spi); + list_for_each_safe(item, tmp, entry->spis_in_old, i) + { + spi_item = list_entry(item); + _HIP_DEBUG("test item: ifindex=%d spi=0x%x\n", + spi_item->ifindex, spi_item->spi); + if (spi_item->spi == spi || spi_item->new_spi == spi) { + _HIP_DEBUG("found\n"); + return spi_item->ifindex; + } + } + HIP_DEBUG("ifindex not found for the SPI 0x%x\n", spi); + return 0; } -uint32_t hip_update_get_prev_spi_in_deprecated_rekeying(hip_ha_t *entry, uint32_t peer_update_id) +uint32_t hip_update_get_prev_spi_in_deprecated_rekeying(hip_ha_t *entry, + uint32_t peer_update_id) { - struct hip_spi_in_item *spi_item; - hip_list_t *item, *tmp; - int i; + struct hip_spi_in_item *spi_item; + hip_list_t *item, *tmp; + int i; - HIP_DEBUG("peer_update_id=%u\n", peer_update_id); - list_for_each_safe(item, tmp, entry->spis_in_old, i) - { - spi_item = list_entry(item); - _HIP_DEBUG("test item: ifindex=%d spi=0x%x nes_spi_out=0x%x seq_id=%u\n", - spi_item->ifindex, spi_item->spi, spi_item->nes_spi_out, spi_item->seq_update_id); - if (spi_item->seq_update_id == peer_update_id) { - HIP_DEBUG("found SPI 0x%x\n", spi_item->spi); - return spi_item->spi; - } - } - HIP_DEBUG("SPI not found\n"); - return 0; + HIP_DEBUG("peer_update_id=%u\n", peer_update_id); + list_for_each_safe(item, tmp, entry->spis_in_old, i) + { + spi_item = list_entry(item); + _HIP_DEBUG("test item: ifindex=%d spi=0x%x nes_spi_out=0x%x seq_id=%u\n", + spi_item->ifindex, spi_item->spi, spi_item->nes_spi_out, + spi_item->seq_update_id); + if (spi_item->seq_update_id == peer_update_id) { + HIP_DEBUG("found SPI 0x%x\n", spi_item->spi); + return spi_item->spi; + } + } + HIP_DEBUG("SPI not found\n"); + return 0; } /* Get the SPI of the SA belonging to the interface through - which we received the UPDATE */ + * which we received the UPDATE */ /* also sets updating flag of SPI to 1 */ -uint32_t hip_get_spi_to_update_in_established_deprecated(hip_ha_t *entry, struct in6_addr *dev_addr) +uint32_t hip_get_spi_to_update_in_established_deprecated(hip_ha_t *entry, + struct in6_addr *dev_addr) { - struct hip_spi_in_item *spi_item; - hip_list_t *item, *tmp; - int i; - int ifindex; - - HIP_DEBUG_HIT("dst dev_addr", dev_addr); - ifindex = hip_devaddr2ifindex(dev_addr); - HIP_DEBUG("ifindex of dst dev=%d\n", ifindex); - if (!ifindex) - return 0; - - list_for_each_safe(item, tmp, entry->spis_in_old, i) - { - spi_item = list_entry(item); - _HIP_DEBUG("test item: ifindex=%d spi=0x%x\n", spi_item->ifindex, spi_item->spi); - if (spi_item->ifindex == ifindex) - { - spi_item->updating = 1; - return spi_item->spi; - } - } - - HIP_DEBUG("SPI not found for ifindex\n"); - return 0; + struct hip_spi_in_item *spi_item; + hip_list_t *item, *tmp; + int i; + int ifindex; + + HIP_DEBUG_HIT("dst dev_addr", dev_addr); + ifindex = hip_devaddr2ifindex(dev_addr); + HIP_DEBUG("ifindex of dst dev=%d\n", ifindex); + if (!ifindex) { + return 0; + } + + list_for_each_safe(item, tmp, entry->spis_in_old, i) + { + spi_item = list_entry(item); + _HIP_DEBUG("test item: ifindex=%d spi=0x%x\n", + spi_item->ifindex, spi_item->spi); + if (spi_item->ifindex == ifindex) { + spi_item->updating = 1; + return spi_item->spi; + } + } + + HIP_DEBUG("SPI not found for ifindex\n"); + return 0; } -void hip_set_spi_update_status_deprecated_rekeying(hip_ha_t *entry, uint32_t spi, int set) +void hip_set_spi_update_status_deprecated_rekeying(hip_ha_t *entry, + uint32_t spi, int set) { - struct hip_spi_in_item *spi_item; - hip_list_t *item, *tmp; - int i; + struct hip_spi_in_item *spi_item; + hip_list_t *item, *tmp; + int i; - HIP_DEBUG("spi=0x%x set=%d\n", spi, set); - list_for_each_safe(item, tmp, entry->spis_in_old, i) - { - spi_item = list_entry(item); - _HIP_DEBUG("test item: ifindex=%d spi=0x%x updating=%d\n", - spi_item->ifindex, spi_item->spi, spi_item->updating); - if (spi_item->spi == spi) - { - HIP_DEBUG("setting updating status to %d\n", set); - spi_item->updating = set; - break; - } - } + HIP_DEBUG("spi=0x%x set=%d\n", spi, set); + list_for_each_safe(item, tmp, entry->spis_in_old, i) + { + spi_item = list_entry(item); + _HIP_DEBUG("test item: ifindex=%d spi=0x%x updating=%d\n", + spi_item->ifindex, spi_item->spi, spi_item->updating); + if (spi_item->spi == spi) { + HIP_DEBUG("setting updating status to %d\n", set); + spi_item->updating = set; + break; + } + } } /* just sets the new_spi field */ -void hip_update_set_new_spi_out_deprecated_rekeying(hip_ha_t *entry, uint32_t spi, uint32_t new_spi) +void hip_update_set_new_spi_out_deprecated_rekeying(hip_ha_t *entry, + uint32_t spi, + uint32_t new_spi) { - struct hip_spi_in_item *spi_item; - hip_list_t *item, *tmp; - int i; + struct hip_spi_in_item *spi_item; + hip_list_t *item, *tmp; + int i; - _HIP_DEBUG("spi=0x%x new_spi=0x%x\n", spi, new_spi); - list_for_each_safe(item, tmp, entry->spis_in_old, i) - { - spi_item = list_entry(item); - _HIP_DEBUG("test item: spi=0x%x new_spi=0x%x\n", - spi_item->spi, spi_item->new_spi); - if (spi_item->spi == spi) - { - _HIP_DEBUG("setting new_spi\n"); - if (spi_item->new_spi) - { - HIP_ERROR("previous new_spi is not zero: 0x%x\n", spi_item->new_spi); - HIP_ERROR("todo: delete previous new_spi\n"); - } - spi_item->new_spi = new_spi; - break; - } - } + _HIP_DEBUG("spi=0x%x new_spi=0x%x\n", spi, new_spi); + list_for_each_safe(item, tmp, entry->spis_in_old, i) + { + spi_item = list_entry(item); + _HIP_DEBUG("test item: spi=0x%x new_spi=0x%x\n", + spi_item->spi, spi_item->new_spi); + if (spi_item->spi == spi) { + _HIP_DEBUG("setting new_spi\n"); + if (spi_item->new_spi) { + HIP_ERROR("previous new_spi is not zero: 0x%x\n", + spi_item->new_spi); + HIP_ERROR("todo: delete previous new_spi\n"); + } + spi_item->new_spi = new_spi; + break; + } + } } - -uint32_t hip_update_get_new_spi_in_deprecated_rekeying(hip_ha_t *entry, uint32_t peer_update_id) +uint32_t hip_update_get_new_spi_in_deprecated_rekeying(hip_ha_t *entry, + uint32_t peer_update_id) { - struct hip_spi_in_item *spi_item; - hip_list_t *item, *tmp; - int i; + struct hip_spi_in_item *spi_item; + hip_list_t *item, *tmp; + int i; - _HIP_DEBUG("peer_update_id=%u\n", peer_update_id); - list_for_each_safe(item, tmp, entry->spis_in_old, i) - { - spi_item = list_entry(item); - _HIP_DEBUG("test item: spi=0x%x new_spi=0x%x\n", - spi_item->spi, spi_item->new_spi); - if (spi_item->seq_update_id == peer_update_id) - { - if (spi_item->new_spi) - return spi_item->new_spi; - return spi_item->spi; - } - } - HIP_DEBUG("New SPI not found\n"); - return 0; + _HIP_DEBUG("peer_update_id=%u\n", peer_update_id); + list_for_each_safe(item, tmp, entry->spis_in_old, i) + { + spi_item = list_entry(item); + _HIP_DEBUG("test item: spi=0x%x new_spi=0x%x\n", + spi_item->spi, spi_item->new_spi); + if (spi_item->seq_update_id == peer_update_id) { + if (spi_item->new_spi) { + return spi_item->new_spi; + } + return spi_item->spi; + } + } + HIP_DEBUG("New SPI not found\n"); + return 0; } /* switch from Old SPI to New SPI (inbound SA) */ /* caller must delete the Old SPI */ -void hip_update_switch_spi_in_deprecated_rekeying(hip_ha_t *entry, uint32_t old_spi) +void hip_update_switch_spi_in_deprecated_rekeying(hip_ha_t *entry, + uint32_t old_spi) { - struct hip_spi_in_item *spi_item; - hip_list_t *item, *tmp; - int i; + struct hip_spi_in_item *spi_item; + hip_list_t *item, *tmp; + int i; - _HIP_DEBUG("old_spi=0x%x\n", old_spi); - list_for_each_safe(item, tmp, entry->spis_in_old, i) - { - spi_item = list_entry(item); - _HIP_DEBUG("test item: ifindex=%d spi=0x%x new_spi=0x%x nes_spi_out=0x%x seq_id=%u\n", - spi_item->ifindex, item->spi, spi_item->new_spi, - spi_item->nes_spi_out, spi_item->seq_update_id); - if (spi_item->spi == old_spi) - { - _HIP_DEBUG("switching\n"); - spi_item->spi = spi_item->new_spi; - spi_item->new_spi = 0; - spi_item->esp_info_spi_out = 0; - break; - } - } + _HIP_DEBUG("old_spi=0x%x\n", old_spi); + list_for_each_safe(item, tmp, entry->spis_in_old, i) + { + spi_item = list_entry(item); + _HIP_DEBUG("test item: ifindex=%d spi=0x%x new_spi=0x%x nes_spi_out=0x%x seq_id=%u\n", + spi_item->ifindex, item->spi, spi_item->new_spi, + spi_item->nes_spi_out, spi_item->seq_update_id); + if (spi_item->spi == old_spi) { + _HIP_DEBUG("switching\n"); + spi_item->spi = spi_item->new_spi; + spi_item->new_spi = 0; + spi_item->esp_info_spi_out = 0; + break; + } + } } /* switch from Old SPI to New SPI (outbound SA) */ /* caller must delete the Old SPI */ -void hip_update_switch_spi_out_deprecated_rekeying(hip_ha_t *entry, uint32_t old_spi) +void hip_update_switch_spi_out_deprecated_rekeying(hip_ha_t *entry, + uint32_t old_spi) { - struct hip_spi_in_item *spi_item; - hip_list_t *item, *tmp; - int i; + struct hip_spi_in_item *spi_item; + hip_list_t *item, *tmp; + int i; - _HIP_DEBUG("old_spi=0x%x\n", old_spi); - list_for_each_safe(item, tmp, entry->spis_in_old, i) - { - spi_item = list_entry(item); - _HIP_DEBUG("test item: spi=0x%x new_spi=0x%x seq_id=%u\n", - spi_item->spi, spi_item->new_spi, spi_item->seq_update_id); - if (spi_item->spi == old_spi) - { - _HIP_DEBUG("switching\n"); - spi_item->spi = spi_item->new_spi; - spi_item->new_spi = 0; - break; - } - } + _HIP_DEBUG("old_spi=0x%x\n", old_spi); + list_for_each_safe(item, tmp, entry->spis_in_old, i) + { + spi_item = list_entry(item); + _HIP_DEBUG("test item: spi=0x%x new_spi=0x%x seq_id=%u\n", + spi_item->spi, spi_item->new_spi, spi_item->seq_update_id); + if (spi_item->spi == old_spi) { + _HIP_DEBUG("switching\n"); + spi_item->spi = spi_item->new_spi; + spi_item->new_spi = 0; + break; + } + } } /** @@ -223,43 +230,42 @@ * @return 1 if given SPI belongs to the SA having direction, else 0. */ int hip_update_exists_spi_deprecated_rekeying(hip_ha_t *entry, uint32_t spi, - int direction, int test_new_spi) + int direction, int test_new_spi) { - hip_list_t *item, *tmp; - struct hip_spi_in_item *spi_item; - int i; - - /* assumes locked entry */ - - _HIP_DEBUG("spi=0x%x direction=%d test_new_spi=%d\n", - spi, direction, test_new_spi); - - if (direction == HIP_SPI_DIRECTION_IN) - { - list_for_each_safe(item, tmp, entry->spis_in_old, i) - { - spi_item = list_entry(item); - _HIP_DEBUG("test item: spi_in=0x%x new_spi=0x%x\n", - spi_item->spi, spi_item->new_spi); - if ( (spi_item->spi == spi && !test_new_spi) || - (spi_item->new_spi == spi && test_new_spi) ) - return 1; - } - } - else - { - list_for_each_safe(item, tmp, entry->spis_out_old, i) - { - spi_item = list_entry(item); - _HIP_DEBUG("test item: spi_out=0x%x new_spi=0x%x\n", - spi_item->spi, spi_item->new_spi); - if ( (spi_item->spi == spi && !test_new_spi) || - (spi_item->new_spi == spi && test_new_spi) ) - return 1; - } - } - HIP_DEBUG("not found\n"); - return 0; + hip_list_t *item, *tmp; + struct hip_spi_in_item *spi_item; + int i; + + /* assumes locked entry */ + + _HIP_DEBUG("spi=0x%x direction=%d test_new_spi=%d\n", + spi, direction, test_new_spi); + + if (direction == HIP_SPI_DIRECTION_IN) { + list_for_each_safe(item, tmp, entry->spis_in_old, i) + { + spi_item = list_entry(item); + _HIP_DEBUG("test item: spi_in=0x%x new_spi=0x%x\n", + spi_item->spi, spi_item->new_spi); + if ((spi_item->spi == spi && !test_new_spi) || + (spi_item->new_spi == spi && test_new_spi)) { + return 1; + } + } + } else { + list_for_each_safe(item, tmp, entry->spis_out_old, i) + { + spi_item = list_entry(item); + _HIP_DEBUG("test item: spi_out=0x%x new_spi=0x%x\n", + spi_item->spi, spi_item->new_spi); + if ((spi_item->spi == spi && !test_new_spi) || + (spi_item->new_spi == spi && test_new_spi)) { + return 1; + } + } + } + HIP_DEBUG("not found\n"); + return 0; } /* if add is non-NULL, set addr as the default address for both @@ -267,61 +273,55 @@ /* if addr is null, select some address from the SPI list */ void hip_hadb_set_default_out_addr_deprecated(hip_ha_t *entry, - struct in6_addr *addr) + struct in6_addr *addr) { #if 0 HIP_DEBUG("\n"); - if (!spi_out) - { - HIP_ERROR("NULL spi_out\n"); - return; - } + if (!spi_out) { + HIP_ERROR("NULL spi_out\n"); + return; + } - if (addr) - { - HIP_DEBUG("testing, setting given address as default out addr\n"); - ipv6_addr_copy(&spi_out->preferred_address, addr); - ipv6_addr_copy(&entry->peer_addr, addr); - } - else - { - /* useless ? */ - struct in6_addr a; - int err = hip_hadb_select_spi_addr(entry, spi_out, &a); - _HIP_DEBUG("setting address as default out addr\n"); - if (!err) - { - ipv6_addr_copy(&spi_out->preferred_address, &a); - ipv6_addr_copy(&entry->peer_addr, &a); - HIP_DEBUG("default out addr\n", - &entry->peer_addr); - } - else HIP_ERROR("couldn't select and set preferred address\n"); - } - HIP_DEBUG("setting default SPI out to 0x%x\n", spi_out->spi); - entry->default_spi_out = spi_out->spi; + if (addr) { + HIP_DEBUG("testing, setting given address as default out addr\n"); + ipv6_addr_copy(&spi_out->preferred_address, addr); + ipv6_addr_copy(&entry->peer_addr, addr); + } else { + /* useless ? */ + struct in6_addr a; + int err = hip_hadb_select_spi_addr(entry, spi_out, &a); + _HIP_DEBUG("setting address as default out addr\n"); + if (!err) { + ipv6_addr_copy(&spi_out->preferred_address, &a); + ipv6_addr_copy(&entry->peer_addr, &a); + HIP_DEBUG("default out addr\n", + &entry->peer_addr); + } else {HIP_ERROR("couldn't select and set preferred address\n"); + } + } + HIP_DEBUG("setting default SPI out to 0x%x\n", spi_out->spi); + entry->default_spi_out = spi_out->spi; #endif } /* works if update contains only one ESP_INFO */ -int hip_update_get_spi_keymat_index_deprecated_rekeying(hip_ha_t *entry, uint32_t peer_update_id) +int hip_update_get_spi_keymat_index_deprecated_rekeying(hip_ha_t *entry, + uint32_t peer_update_id) { - hip_list_t *item, *tmp; - struct hip_spi_in_item *spi_item; - int i; + hip_list_t *item, *tmp; + struct hip_spi_in_item *spi_item; + int i; - _HIP_DEBUG("peer_update_id=%u\n", peer_update_id); - list_for_each_safe(item, tmp, entry->spis_in_old, i) - { - spi_item = list_entry(item); - _HIP_DEBUG("test item: spi_in=0x%x seq_update_id=%u keymat_index=%u\n", - spi_item->spi, item->seq_update_id, item->keymat_index); - if (spi_item->seq_update_id == peer_update_id) - { - return spi_item->keymat_index; - } - } - return 0; + _HIP_DEBUG("peer_update_id=%u\n", peer_update_id); + list_for_each_safe(item, tmp, entry->spis_in_old, i) + { + spi_item = list_entry(item); + _HIP_DEBUG("test item: spi_in=0x%x seq_update_id=%u keymat_index=%u\n", + spi_item->spi, item->seq_update_id, item->keymat_index); + if (spi_item->seq_update_id == peer_update_id) { + return spi_item->keymat_index; + } + } + return 0; } - === modified file 'test/hadb_old.c' --- test/hadb_old.c 2010-02-08 15:18:06 +0000 +++ test/hadb_old.c 2010-02-10 23:43:06 +0000 @@ -5,523 +5,527 @@ #if 0 /* assume already locked entry */ // SYNCH -int hip_hadb_add_outbound_spi_old(hip_ha_t *entry, struct hip_spi_out_item *data) +int hip_hadb_add_outbound_spi_old(hip_ha_t *entry, + struct hip_spi_out_item *data) { - int err = 0, i; - struct hip_spi_out_item *spi_item; - uint32_t spi_out; - hip_list_t *item, *tmp; - - /* assumes locked entry ? */ - spi_out = data->spi; - - _HIP_DEBUG("SPI_out=0x%x\n", spi_out); - list_for_each_safe(item, tmp, entry->spis_out_old, i) - { - spi_item = list_entry(item); - if (spi_item->spi == spi_out) - { - HIP_DEBUG("not adding duplicate SPI 0x%x\n", spi_out); - goto out; - } - } - - spi_item = (struct hip_spi_out_item *)HIP_MALLOC(sizeof(struct hip_spi_out_item), GFP_ATOMIC); - if (!spi_item) - { - HIP_ERROR("item HIP_MALLOC failed\n"); - err = -ENOMEM; - goto out_err; - } - memcpy(spi_item, data, sizeof(struct hip_spi_out_item)); -// INIT_LIST_HEAD(&spi_item->peer_addr_list); - spi_item->peer_addr_list = hip_ht_init(hip_hash_peer_addr, hip_match_peer_addr); - ipv6_addr_copy(&spi_item->preferred_address, &in6addr_any); - list_add(spi_item, entry->spis_out_old); - HIP_DEBUG("added SPI 0x%x to the outbound SPI list\n", spi_out); - - out_err: - out: - return err; + int err = 0, i; + struct hip_spi_out_item *spi_item; + uint32_t spi_out; + hip_list_t *item, *tmp; + + /* assumes locked entry ? */ + spi_out = data->spi; + + _HIP_DEBUG("SPI_out=0x%x\n", spi_out); + list_for_each_safe(item, tmp, entry->spis_out_old, i) + { + spi_item = list_entry(item); + if (spi_item->spi == spi_out) { + HIP_DEBUG("not adding duplicate SPI 0x%x\n", spi_out); + goto out; + } + } + + spi_item = (struct hip_spi_out_item *) + HIP_MALLOC(sizeof(struct hip_spi_out_item), GFP_ATOMIC); + if (!spi_item) { + HIP_ERROR("item HIP_MALLOC failed\n"); + err = -ENOMEM; + goto out_err; + } + memcpy(spi_item, data, sizeof(struct hip_spi_out_item)); +// INIT_LIST_HEAD(&spi_item->peer_addr_list); + spi_item->peer_addr_list = hip_ht_init(hip_hash_peer_addr, + hip_match_peer_addr); + ipv6_addr_copy(&spi_item->preferred_address, &in6addr_any); + list_add(spi_item, entry->spis_out_old); + HIP_DEBUG("added SPI 0x%x to the outbound SPI list\n", spi_out); + +out_err: +out: + return err; } /* assume already locked entry */ int hip_hadb_add_spi_old(hip_ha_t *entry, int direction, void *data) { - int err = -EINVAL; - - if (direction == HIP_SPI_DIRECTION_IN) - err = hip_hadb_add_inbound_spi_old(entry, (struct hip_spi_in_item *) data); - else if (direction == HIP_SPI_DIRECTION_OUT) - err = hip_hadb_add_outbound_spi_old(entry, (struct hip_spi_out_item *) data); - else - HIP_ERROR("bug, invalid direction %d\n", direction); - - return err; + int err = -EINVAL; + + if (direction == HIP_SPI_DIRECTION_IN) { + err = hip_hadb_add_inbound_spi_old(entry, + (struct hip_spi_in_item *) data); + } else if (direction == HIP_SPI_DIRECTION_OUT) { + err = hip_hadb_add_outbound_spi_old(entry, + (struct hip_spi_out_item *) data); + } else { + HIP_ERROR("bug, invalid direction %d\n", direction); + } + + return err; } /* Get the SPI of given ifindex, returns 0 if ifindex was not found */ uint32_t hip_hadb_get_spi_old(hip_ha_t *entry, int ifindex) { - struct hip_spi_in_item *spi_item; - hip_list_t *item, *tmp; - int i; - - HIP_DEBUG("ifindex=%d\n", ifindex); - list_for_each_safe(item, tmp, entry->spis_in_old, i) - { - spi_item = list_entry(item); - _HIP_DEBUG("test item: ifindex=%d spi=0x%x\n", spi_item->ifindex, spi_item->spi); - if (spi_item->ifindex == ifindex || ifindex == -1) - { - HIP_DEBUG("found SPI 0x%x\n", spi_item->spi); - return spi_item->spi; - } - } - - HIP_DEBUG("SPI not found for the ifindex\n"); - return 0; + struct hip_spi_in_item *spi_item; + hip_list_t *item, *tmp; + int i; + + HIP_DEBUG("ifindex=%d\n", ifindex); + list_for_each_safe(item, tmp, entry->spis_in_old, i) + { + spi_item = list_entry(item); + _HIP_DEBUG("test item: ifindex=%d spi=0x%x\n", + spi_item->ifindex, spi_item->spi); + if (spi_item->ifindex == ifindex || ifindex == -1) { + HIP_DEBUG("found SPI 0x%x\n", spi_item->spi); + return spi_item->spi; + } + } + + HIP_DEBUG("SPI not found for the ifindex\n"); + return 0; } /* spi_out is the SPI which was in the received NES Old SPI field */ -void hip_update_set_new_spi_in_old(hip_ha_t *entry, uint32_t spi, uint32_t new_spi, - uint32_t spi_out /* test */) +void hip_update_set_new_spi_in_old(hip_ha_t *entry, + uint32_t spi, uint32_t new_spi, + uint32_t spi_out /* test */) { - struct hip_spi_in_item *spi_item; - hip_list_t *item, *tmp; - int i; - - _HIP_DEBUG("spi=0x%x new_spi=0x%x spi_out=0x%x\n", spi, new_spi, spi_out); - - list_for_each_safe(item, tmp, entry->spis_in_old, i) - { - spi_item = list_entry(item); - _HIP_DEBUG("test item: spi=0x%x new_spi=0x%x\n", - spi_item->spi, spi_item->new_spi); - if (spi_item->spi == spi) - { - HIP_DEBUG("setting new_spi\n"); - if (!spi_item->updating) - { - _HIP_ERROR("SA update not in progress, continuing anyway\n"); - } - if ((spi_item->spi != spi_item->new_spi) && spi_item->new_spi) - { - HIP_ERROR("warning: previous new_spi is not zero: 0x%x\n", - spi_item->new_spi); - } - spi_item->new_spi = new_spi; - spi_item->esp_info_spi_out = spi_out; /* maybe useless */ - break; - } - } + struct hip_spi_in_item *spi_item; + hip_list_t *item, *tmp; + int i; + + _HIP_DEBUG("spi=0x%x new_spi=0x%x spi_out=0x%x\n", spi, new_spi, spi_out); + + list_for_each_safe(item, tmp, entry->spis_in_old, i) + { + spi_item = list_entry(item); + _HIP_DEBUG("test item: spi=0x%x new_spi=0x%x\n", + spi_item->spi, spi_item->new_spi); + if (spi_item->spi == spi) { + HIP_DEBUG("setting new_spi\n"); + if (!spi_item->updating) { + _HIP_ERROR("SA update not in progress, continuing anyway\n"); + } + if ((spi_item->spi != spi_item->new_spi) && spi_item->new_spi) { + HIP_ERROR("warning: previous new_spi is not zero: 0x%x\n", + spi_item->new_spi); + } + spi_item->new_spi = new_spi; + spi_item->esp_info_spi_out = spi_out; /* maybe useless */ + break; + } + } } /* have_esp_info is 1, if there is ESP_INFO in the same packet as the ACK was */ -void hip_update_handle_ack_old(hip_ha_t *entry, struct hip_ack *ack, int have_esp_info) +void hip_update_handle_ack_old(hip_ha_t *entry, + struct hip_ack *ack, int have_esp_info) { - size_t n = 0, i = 0; - uint32_t *peer_update_id = NULL; - - HIP_DEBUG("hip_update_handle_ack() invoked with have_esp_info = %d.\n", - have_esp_info); - - if (ack == NULL) { - HIP_ERROR("Function parameter ack was NULL in "\ - "hip_update_handle_ack().\n"); - goto out_err; - } - - if (hip_get_param_contents_len(ack) % sizeof(uint32_t)) { - HIP_ERROR("ACK parameter length is not divisible by 4 (%u).\n", - hip_get_param_contents_len(ack)); - goto out_err; - } - - n = hip_get_param_contents_len(ack) / sizeof(uint32_t); - - HIP_DEBUG("Number of peer Update IDs in ACK parameter: %d.\n", n); - - peer_update_id = - (uint32_t *) ((void *)ack + sizeof(struct hip_tlv_common)); - - /* Loop through all peer Update IDs in the ACK parameter. */ - for (i = 0; i < n; i++, peer_update_id++) { - hip_list_t *item, *tmp; - struct hip_spi_in_item *in_item; - uint32_t puid = ntohl(*peer_update_id); - int i; - - _HIP_DEBUG("peer Update ID=%u\n", puid); - - /* See if your ESP_INFO is acked and maybe if corresponging - ESP_INFO was received */ - list_for_each_safe(item, tmp, entry->spis_in_old, i) { - in_item = list_entry(item); - _HIP_DEBUG("test item: spi_in=0x%x seq=%u\n", - in_item->spi, in_item->seq_update_id); - if (in_item->seq_update_id == puid) { - _HIP_DEBUG("SEQ and ACK match\n"); - /* Received ACK */ - in_item->update_state_flags |= 0x1; - /* Received also ESP_INFO */ - if (have_esp_info) { - in_item->update_state_flags |= 0x2; - } - } - } - - } - out_err: - return; + size_t n = 0, i = 0; + uint32_t *peer_update_id = NULL; + + HIP_DEBUG("hip_update_handle_ack() invoked with have_esp_info = %d.\n", + have_esp_info); + + if (ack == NULL) { + HIP_ERROR("Function parameter ack was NULL in " \ + "hip_update_handle_ack().\n"); + goto out_err; + } + + if (hip_get_param_contents_len(ack) % sizeof(uint32_t)) { + HIP_ERROR("ACK parameter length is not divisible by 4 (%u).\n", + hip_get_param_contents_len(ack)); + goto out_err; + } + + n = hip_get_param_contents_len(ack) / sizeof(uint32_t); + + HIP_DEBUG("Number of peer Update IDs in ACK parameter: %d.\n", n); + + peer_update_id = + (uint32_t *) ((void *) ack + sizeof(struct hip_tlv_common)); + + /* Loop through all peer Update IDs in the ACK parameter. */ + for (i = 0; i < n; i++, peer_update_id++) { + hip_list_t *item, *tmp; + struct hip_spi_in_item *in_item; + uint32_t puid = ntohl(*peer_update_id); + int i; + + _HIP_DEBUG("peer Update ID=%u\n", puid); + + /* See if your ESP_INFO is acked and maybe if corresponging + * ESP_INFO was received */ + list_for_each_safe(item, tmp, entry->spis_in_old, i) { + in_item = list_entry(item); + _HIP_DEBUG("test item: spi_in=0x%x seq=%u\n", + in_item->spi, in_item->seq_update_id); + if (in_item->seq_update_id == puid) { + _HIP_DEBUG("SEQ and ACK match\n"); + /* Received ACK */ + in_item->update_state_flags |= 0x1; + /* Received also ESP_INFO */ + if (have_esp_info) { + in_item->update_state_flags |= 0x2; + } + } + } + } +out_err: + return; } /* todo: use jiffies instead of timestamp */ uint32_t hip_hadb_get_latest_inbound_spi_old(hip_ha_t *entry) { - hip_list_t *item, *tmp; - struct hip_spi_in_item *spi_item; - uint32_t spi = 0; - unsigned int now = jiffies; - unsigned long t = ULONG_MAX; - int i; - - /* assumes already locked entry */ - - list_for_each_safe(item, tmp, entry->spis_in_old, i) - { - spi_item = list_entry(item); - HIP_DEBUG("spi_in in loop is 0x%x\n", spi_item->spi); - if (now - spi_item->timestamp < t) - { - spi = spi_item->spi; - t = now - spi_item->timestamp; - } - } - - _HIP_DEBUG("newest spi_in is 0x%x\n", spi); - return spi; + hip_list_t *item, *tmp; + struct hip_spi_in_item *spi_item; + uint32_t spi = 0; + unsigned int now = jiffies; + unsigned long t = ULONG_MAX; + int i; + + /* assumes already locked entry */ + + list_for_each_safe(item, tmp, entry->spis_in_old, i) + { + spi_item = list_entry(item); + HIP_DEBUG("spi_in in loop is 0x%x\n", spi_item->spi); + if (now - spi_item->timestamp < t) { + spi = spi_item->spi; + t = now - spi_item->timestamp; + } + } + + _HIP_DEBUG("newest spi_in is 0x%x\n", spi); + return spi; } //add by santtu /* todo: use jiffies instead of timestamp */ uint32_t hip_hadb_get_outbound_spi_old(hip_ha_t *entry) { - hip_list_t *item, *tmp; - struct hip_spi_out_item *spi_item; - uint32_t spi = 0; - unsigned int now = jiffies; - unsigned long t = ULONG_MAX; - int i; - - /* assumes already locked entry */ - - list_for_each_safe(item, tmp, entry->spis_out_old, i) - { - spi_item = list_entry(item); - - spi = spi_item->spi; - - break; - - } - - _HIP_DEBUG("newest spi_in out 0x%x\n", spi); - return spi; + hip_list_t *item, *tmp; + struct hip_spi_out_item *spi_item; + uint32_t spi = 0; + unsigned int now = jiffies; + unsigned long t = ULONG_MAX; + int i; + + /* assumes already locked entry */ + + list_for_each_safe(item, tmp, entry->spis_out_old, i) + { + spi_item = list_entry(item); + + spi = spi_item->spi; + + break; + } + + _HIP_DEBUG("newest spi_in out 0x%x\n", spi); + return spi; } //end add /* get pointer to the outbound SPI list or NULL if the outbound SPI - list does not exist */ -struct hip_spi_out_item *hip_hadb_get_spi_list_old(hip_ha_t *entry, uint32_t spi) + * list does not exist */ +struct hip_spi_out_item *hip_hadb_get_spi_list_old(hip_ha_t *entry, + uint32_t spi) { - struct hip_spi_out_item *spi_item; - hip_list_t *item, *tmp; - int i; - - /* assumes already locked entry */ - - _HIP_DEBUG("Search spi list for SPI=0x%x\n", spi); - list_for_each_safe(item, tmp, entry->spis_out_old, i) - { - spi_item = list_entry(item); - _HIP_DEBUG("search: 0x%x ?= 0x%x\n", spi_item->spi, spi); - if (spi_item->spi == spi) return spi_item; - } - - return NULL; + struct hip_spi_out_item *spi_item; + hip_list_t *item, *tmp; + int i; + + /* assumes already locked entry */ + + _HIP_DEBUG("Search spi list for SPI=0x%x\n", spi); + list_for_each_safe(item, tmp, entry->spis_out_old, i) + { + spi_item = list_entry(item); + _HIP_DEBUG("search: 0x%x ?= 0x%x\n", spi_item->spi, spi); + if (spi_item->spi == spi) { + return spi_item; + } + } + + return NULL; } /* get pointer to the inbound SPI list or NULL if SPI list does not exist */ -struct hip_spi_in_item *hip_hadb_get_spi_in_list_old(hip_ha_t *entry, uint32_t spi) +struct hip_spi_in_item *hip_hadb_get_spi_in_list_old(hip_ha_t *entry, + uint32_t spi) { - struct hip_spi_in_item *spi_item; - hip_list_t *item, *tmp; - int i; - - /* assumes already locked entry */ - - HIP_DEBUG("SPI=0x%x\n", spi); - list_for_each_safe(item, tmp, entry->spis_in_old, i) - { - spi_item = list_entry(item); - if (spi_item->spi == spi) return spi_item; - } - - return NULL; + struct hip_spi_in_item *spi_item; + hip_list_t *item, *tmp; + int i; + + /* assumes already locked entry */ + + HIP_DEBUG("SPI=0x%x\n", spi); + list_for_each_safe(item, tmp, entry->spis_in_old, i) + { + spi_item = list_entry(item); + if (spi_item->spi == spi) { + return spi_item; + } + } + + return NULL; } void hip_hadb_dump_spis_in_old(hip_ha_t *entry) { - struct hip_spi_in_item *spi_item; - hip_list_t *item, *tmp; - int i; + struct hip_spi_in_item *spi_item; + hip_list_t *item, *tmp; + int i; - HIP_DEBUG("start\n"); - HIP_LOCK_HA(entry); - list_for_each_safe(item, tmp, entry->spis_in_old, i) - { - spi_item = list_entry(item); - HIP_DEBUG(" SPI=0x%x new_SPI=0x%x esp_info_SPI_out=0x%x ifindex=%d " - "ts=%lu updating=%d keymat_index=%u upd_flags=0x%x seq_update_id=%u ESP_INFO=old 0x%x,new 0x%x,km %u\n", - spi_item->spi, spi_item->new_spi, spi_item->esp_info_spi_out, spi_item->ifindex, - jiffies - spi_item->timestamp, spi_item->updating, spi_item->keymat_index, - spi_item->update_state_flags, spi_item->seq_update_id, - spi_item->stored_received_esp_info.old_spi, - spi_item->stored_received_esp_info.old_spi, - spi_item->stored_received_esp_info.keymat_index); - } - HIP_UNLOCK_HA(entry); - HIP_DEBUG("end\n"); + HIP_DEBUG("start\n"); + HIP_LOCK_HA(entry); + list_for_each_safe(item, tmp, entry->spis_in_old, i) + { + spi_item = list_entry(item); + HIP_DEBUG(" SPI=0x%x new_SPI=0x%x esp_info_SPI_out=0x%x ifindex=%d " + "ts=%lu updating=%d keymat_index=%u upd_flags=0x%x seq_update_id=%u ESP_INFO=old 0x%x,new 0x%x,km %u\n", + spi_item->spi, spi_item->new_spi, spi_item->esp_info_spi_out, + spi_item->ifindex, + jiffies - spi_item->timestamp, spi_item->updating, + spi_item->keymat_index, + spi_item->update_state_flags, spi_item->seq_update_id, + spi_item->stored_received_esp_info.old_spi, + spi_item->stored_received_esp_info.old_spi, + spi_item->stored_received_esp_info.keymat_index); + } + HIP_UNLOCK_HA(entry); + HIP_DEBUG("end\n"); } void hip_hadb_dump_spis_out_old(hip_ha_t *entry) { - struct hip_spi_out_item *spi_item; - hip_list_t *item, *tmp; - int i; + struct hip_spi_out_item *spi_item; + hip_list_t *item, *tmp; + int i; - HIP_DEBUG("start\n"); - HIP_LOCK_HA(entry); - list_for_each_safe(item, tmp, entry->spis_out_old, i) - { - spi_item = list_entry(item); - HIP_DEBUG(" SPI=0x%x new_SPI=0x%x seq_update_id=%u\n", - spi_item->spi, spi_item->new_spi, spi_item->seq_update_id); - } - HIP_UNLOCK_HA(entry); - HIP_DEBUG("end\n"); + HIP_DEBUG("start\n"); + HIP_LOCK_HA(entry); + list_for_each_safe(item, tmp, entry->spis_out_old, i) + { + spi_item = list_entry(item); + HIP_DEBUG(" SPI=0x%x new_SPI=0x%x seq_update_id=%u\n", + spi_item->spi, spi_item->new_spi, spi_item->seq_update_id); + } + HIP_UNLOCK_HA(entry); + HIP_DEBUG("end\n"); } /* assume already locked entry */ // SYNC int hip_hadb_add_inbound_spi_old(hip_ha_t *entry, struct hip_spi_in_item *data) { - int err = 0, i; - struct hip_spi_in_item *spi_item; - uint32_t spi_in; - hip_list_t *item, *tmp; - spi_in = data->spi; - - /* assumes locked entry */ - _HIP_DEBUG("SPI_in=0x%x\n", spi_in); - list_for_each_safe(item, tmp, entry->spis_in_old, i) - { - spi_item = list_entry(item); - if (spi_item->spi == spi_in) - { - HIP_DEBUG("not adding duplicate SPI 0x%x\n", spi_in); - goto out; - } - } - - spi_item = (struct hip_spi_in_item *)HIP_MALLOC(sizeof(struct hip_spi_in_item), GFP_ATOMIC); - if (!spi_item) - { - HIP_ERROR("item HIP_MALLOC failed\n"); - err = -ENOMEM; - goto out_err; - } - memcpy(spi_item, data, sizeof(struct hip_spi_in_item)); - spi_item->timestamp = jiffies; - list_add(spi_item, entry->spis_in_old); - spi_item->addresses = NULL; - spi_item->addresses_n = 0; - HIP_DEBUG("added SPI 0x%x to the inbound SPI list\n", spi_in); - // hip_hold_ha(entry); ? - - /*_HIP_DEBUG("inserting SPI to HIT-SPI hashtable\n"); - err = hip_hadb_insert_state_spi_list(&entry->hit_peer, &entry->hit_our, spi_in); - if (err == -EEXIST) err = 0;*/ + int err = 0, i; + struct hip_spi_in_item *spi_item; + uint32_t spi_in; + hip_list_t *item, *tmp; + spi_in = data->spi; + + /* assumes locked entry */ + _HIP_DEBUG("SPI_in=0x%x\n", spi_in); + list_for_each_safe(item, tmp, entry->spis_in_old, i) + { + spi_item = list_entry(item); + if (spi_item->spi == spi_in) { + HIP_DEBUG("not adding duplicate SPI 0x%x\n", spi_in); + goto out; + } + } + + spi_item = (struct hip_spi_in_item *) + HIP_MALLOC(sizeof(struct hip_spi_in_item), GFP_ATOMIC); + if (!spi_item) { + HIP_ERROR("item HIP_MALLOC failed\n"); + err = -ENOMEM; + goto out_err; + } + memcpy(spi_item, data, sizeof(struct hip_spi_in_item)); + spi_item->timestamp = jiffies; + list_add(spi_item, entry->spis_in_old); + spi_item->addresses = NULL; + spi_item->addresses_n = 0; + HIP_DEBUG("added SPI 0x%x to the inbound SPI list\n", spi_in); out_err: out: - return err; + return err; } void hip_update_clear_status(hip_ha_t *entry, uint32_t spi) { - struct hip_spi_in_item *spi_item; - hip_list_t *item, *tmp; - int i; + struct hip_spi_in_item *spi_item; + hip_list_t *item, *tmp; + int i; - _HIP_DEBUG("spi=0x%x\n", spi); - list_for_each_safe(item, tmp, entry->spis_in_old, i) - { - spi_item = list_entry(item); - _HIP_DEBUG("test item: spi=0x%x\n", spi_item->spi); - if (spi_item->spi == spi) - { - _HIP_DEBUG("clearing SPI status\n"); - spi_item->update_state_flags = 0; - memset(&spi_item->stored_received_esp_info, 0, - sizeof(struct hip_esp_info)); - break; - } - } + _HIP_DEBUG("spi=0x%x\n", spi); + list_for_each_safe(item, tmp, entry->spis_in_old, i) + { + spi_item = list_entry(item); + _HIP_DEBUG("test item: spi=0x%x\n", spi_item->spi); + if (spi_item->spi == spi) { + _HIP_DEBUG("clearing SPI status\n"); + spi_item->update_state_flags = 0; + memset(&spi_item->stored_received_esp_info, 0, + sizeof(struct hip_esp_info)); + break; + } + } } void hip_update_set_status(hip_ha_t *entry, uint32_t spi, int set_flags, - uint32_t update_id, int update_flags_or, - struct hip_esp_info *esp_info, - uint16_t keymat_index) + uint32_t update_id, int update_flags_or, + struct hip_esp_info *esp_info, + uint16_t keymat_index) { - struct hip_spi_in_item *spi_item; - hip_list_t *item, *tmp; - int i; - - _HIP_DEBUG("spi=0x%x update_id=%u update_flags_or=0x%x keymat_index=%u esp_info=0x%p\n", - spi, update_id, update_flags_or, keymat_index, esp_info); - if (esp_info) - _HIP_DEBUG("esp_info: old_spi=0x%x new_spi=0x%x keymat_index=%u\n", - ntohl(esp_info->old_spi), ntohl(esp_info->new_spi), ntohs(esp_info->keymat_index)); - - list_for_each_safe(item, tmp, entry->spis_in_old, i) - { - spi_item = list_entry(item); - _HIP_DEBUG("test item: spi_in=0x%x new_spi=0x%x\n", spi_item->spi, spi_item->new_spi); - if (spi_item->spi == spi) - { - _HIP_DEBUG("setting new values\n"); - if (set_flags & 0x1) spi_item->seq_update_id = update_id; - if (set_flags & 0x2) spi_item->update_state_flags |= update_flags_or; - if (esp_info && (set_flags & 0x4)) - { - spi_item->stored_received_esp_info.old_spi = esp_info->old_spi; - spi_item->stored_received_esp_info.new_spi = esp_info->new_spi; - spi_item->stored_received_esp_info.keymat_index = esp_info->keymat_index; - } - if (set_flags & 0x8) spi_item->keymat_index = keymat_index; - - return; - } - } - HIP_ERROR("SPI not found\n"); + struct hip_spi_in_item *spi_item; + hip_list_t *item, *tmp; + int i; + + _HIP_DEBUG("spi=0x%x update_id=%u update_flags_or=0x%x keymat_index=%u esp_info=0x%p\n", + spi, update_id, update_flags_or, keymat_index, esp_info); + if (esp_info) { + _HIP_DEBUG("esp_info: old_spi=0x%x new_spi=0x%x keymat_index=%u\n", + ntohl(esp_info->old_spi), ntohl(esp_info->new_spi), + ntohs(esp_info->keymat_index)); + } + + list_for_each_safe(item, tmp, entry->spis_in_old, i) + { + spi_item = list_entry(item); + _HIP_DEBUG("test item: spi_in=0x%x new_spi=0x%x\n", + spi_item->spi, spi_item->new_spi); + if (spi_item->spi == spi) { + _HIP_DEBUG("setting new values\n"); + if (set_flags & 0x1) { + spi_item->seq_update_id = update_id; + } + if (set_flags & 0x2) { + spi_item->update_state_flags |= update_flags_or; + } + if (esp_info && (set_flags & 0x4)) { + spi_item->stored_received_esp_info.old_spi = esp_info->old_spi; + spi_item->stored_received_esp_info.new_spi = esp_info->new_spi; + spi_item->stored_received_esp_info.keymat_index = esp_info->keymat_index; + } + if (set_flags & 0x8) { + spi_item->keymat_index = keymat_index; + } + + return; + } + } + HIP_ERROR("SPI not found\n"); } void hip_update_handle_esp_info(hip_ha_t *entry, uint32_t peer_update_id) { - hip_list_t *item, *tmp; - struct hip_spi_in_item *spi_item; - int i; + hip_list_t *item, *tmp; + struct hip_spi_in_item *spi_item; + int i; - _HIP_DEBUG("peer_update_id=%u\n", peer_update_id); - list_for_each_safe(item, tmp, entry->spis_in_old, i) - { - spi_item = list_entry(item); - _HIP_DEBUG("test item: spi_in=0x%x seq=%u\n", - spi_item->spi, spi_item->seq_update_id); - if (spi_item->seq_update_id == peer_update_id) - { - _HIP_DEBUG("received peer's ESP_INFO\n"); - spi_item->update_state_flags |= 0x2; /* recv'd ESP_INFO */ - } - } + _HIP_DEBUG("peer_update_id=%u\n", peer_update_id); + list_for_each_safe(item, tmp, entry->spis_in_old, i) + { + spi_item = list_entry(item); + _HIP_DEBUG("test item: spi_in=0x%x seq=%u\n", + spi_item->spi, spi_item->seq_update_id); + if (spi_item->seq_update_id == peer_update_id) { + _HIP_DEBUG("received peer's ESP_INFO\n"); + spi_item->update_state_flags |= 0x2; /* recv'd ESP_INFO */ + } + } } /* Delete given inbound SPI, and all if spi == 0 */ void hip_hadb_delete_inbound_spi(hip_ha_t *entry, uint32_t spi) { - struct hip_spi_in_item *spi_item; - hip_list_t *item, *tmp; - int i; - - /* assumes locked entry */ - HIP_DEBUG("SPI=0x%x\n", spi); - int counter = 0; - - /** @todo check that the deletion below actually works (hits and - addresses are used inconsistenly). */ - list_for_each_safe(item, tmp, entry->spis_in_old, i) - { - spi_item = list_entry(item); - if (!spi || spi_item->spi == spi) - { - HIP_DEBUG("deleting SPI_in=0x%x SPI_in_new=0x%x from " - "inbound list, item=0x%p addresses=0x%p\n", - spi_item->spi, spi_item->new_spi, item, spi_item->addresses); - HIP_ERROR("remove SPI from HIT-SPI HT\n"); - //hip_hadb_remove_hs(spi_item->spi); - HIP_DEBUG_IN6ADDR("delete", &entry->our_addr); - default_ipsec_func_set.hip_delete_sa(spi_item->spi, &entry->our_addr, - &entry->hit_our, HIP_SPI_DIRECTION_IN, entry); - //AF_INET6, 0, 0); - // XX FIX: should be deleted like this? - //for(i = 0; i < spi_item->addresses_n; i++) - // default_ipsec_func_set.hip_delete_sa(spi_item->spi, - // &spi_item->addresses->address + i, AF_INET6); - if (spi_item->spi != spi_item->new_spi) - default_ipsec_func_set.hip_delete_sa(spi_item->new_spi, &entry->hit_our, - &entry->our_addr, HIP_SPI_DIRECTION_IN, entry); - if (spi_item->addresses) - { - HIP_DEBUG("deleting stored addrlist 0x%p\n", spi_item->addresses); - HIP_FREE(spi_item->addresses); - spi_item->addresses = NULL; - } - list_del(spi_item, entry->spis_in_old); - HIP_FREE(spi_item); - break; - - } - } + struct hip_spi_in_item *spi_item; + hip_list_t *item, *tmp; + int i; + + /* assumes locked entry */ + HIP_DEBUG("SPI=0x%x\n", spi); + int counter = 0; + + /** @todo check that the deletion below actually works (hits and + * addresses are used inconsistenly). */ + list_for_each_safe(item, tmp, entry->spis_in_old, i) + { + spi_item = list_entry(item); + if (!spi || spi_item->spi == spi) { + HIP_DEBUG("deleting SPI_in=0x%x SPI_in_new=0x%x from " + "inbound list, item=0x%p addresses=0x%p\n", + spi_item->spi, spi_item->new_spi, item, spi_item->addresses); + HIP_ERROR("remove SPI from HIT-SPI HT\n"); + //hip_hadb_remove_hs(spi_item->spi); + HIP_DEBUG_IN6ADDR("delete", &entry->our_addr); + default_ipsec_func_set.hip_delete_sa(spi_item->spi, &entry->our_addr, + &entry->hit_our, HIP_SPI_DIRECTION_IN, entry); + //AF_INET6, 0, 0); + // XX FIX: should be deleted like this? + //for(i = 0; i < spi_item->addresses_n; i++) + // default_ipsec_func_set.hip_delete_sa(spi_item->spi, + // &spi_item->addresses->address + i, AF_INET6); + if (spi_item->spi != spi_item->new_spi) { + default_ipsec_func_set.hip_delete_sa(spi_item->new_spi, &entry->hit_our, + &entry->our_addr, HIP_SPI_DIRECTION_IN, entry); + } + if (spi_item->addresses) { + HIP_DEBUG("deleting stored addrlist 0x%p\n", spi_item->addresses); + HIP_FREE(spi_item->addresses); + spi_item->addresses = NULL; + } + list_del(spi_item, entry->spis_in_old); + HIP_FREE(spi_item); + break; + } + } } /* Delete given outbound SPI, and all if spi == 0 */ void hip_hadb_delete_outbound_spi(hip_ha_t *entry, uint32_t spi) { - struct hip_spi_out_item *spi_item; - hip_list_t *item, *tmp; - int i, ii; - - /* assumes locked entry */ - HIP_DEBUG("entry=0x%p SPI=0x%x\n", entry, spi); - list_for_each_safe(item, tmp, entry->spis_out_old, i) - { - spi_item = list_entry(item); - if ((!spi && spi != -1) || spi_item->spi == spi) - { - struct hip_peer_addr_list_item *addr_item; - hip_list_t *a_item, *a_tmp; - - HIP_DEBUG("deleting SPI_out=0x%x SPI_out_new=0x%x from outbound list, item=0x%p\n", - spi_item->spi, spi_item->new_spi, item); - default_ipsec_func_set.hip_delete_sa(spi_item->spi, &entry->peer_addr, &entry->peer_addr, - HIP_SPI_DIRECTION_OUT, entry); - default_ipsec_func_set.hip_delete_sa(spi_item->new_spi, &entry->peer_addr,&entry->peer_addr, - HIP_SPI_DIRECTION_OUT, entry); - /* delete peer's addresses */ - list_for_each_safe(a_item, a_tmp, spi_item->peer_addr_list, ii) - { - addr_item = list_entry(a_item); - list_del(addr_item, spi_item->peer_addr_list); - HIP_FREE(addr_item); - } - hip_ht_uninit(spi_item->peer_addr_list); - list_del(spi_item, entry->spis_out_old); - HIP_FREE(spi_item); - } - } + struct hip_spi_out_item *spi_item; + hip_list_t *item, *tmp; + int i, ii; + + /* assumes locked entry */ + HIP_DEBUG("entry=0x%p SPI=0x%x\n", entry, spi); + list_for_each_safe(item, tmp, entry->spis_out_old, i) + { + spi_item = list_entry(item); + if ((!spi && spi != -1) || spi_item->spi == spi) { + struct hip_peer_addr_list_item *addr_item; + hip_list_t *a_item, *a_tmp; + + HIP_DEBUG("deleting SPI_out=0x%x SPI_out_new=0x%x from outbound list, item=0x%p\n", + spi_item->spi, spi_item->new_spi, item); + default_ipsec_func_set.hip_delete_sa(spi_item->spi, &entry->peer_addr, &entry->peer_addr, + HIP_SPI_DIRECTION_OUT, entry); + default_ipsec_func_set.hip_delete_sa(spi_item->new_spi, &entry->peer_addr, &entry->peer_addr, + HIP_SPI_DIRECTION_OUT, entry); + /* delete peer's addresses */ + list_for_each_safe(a_item, a_tmp, spi_item->peer_addr_list, ii) + { + addr_item = list_entry(a_item); + list_del(addr_item, spi_item->peer_addr_list); + HIP_FREE(addr_item); + } + hip_ht_uninit(spi_item->peer_addr_list); + list_del(spi_item, entry->spis_out_old); + HIP_FREE(spi_item); + } + } } #endif === modified file 'test/hashtest.c' --- test/hashtest.c 2010-01-19 12:36:19 +0000 +++ test/hashtest.c 2010-02-10 23:43:06 +0000 @@ -1,5 +1,5 @@ -#include <stdio.h> -#include <stdlib.h> +#include <stdio.h> +#include <stdlib.h> #include <string.h> #include <sys/types.h> #include <netdb.h> @@ -10,27 +10,27 @@ #include "hipd/hadb.h" struct hip_opp_socket_entry { - unsigned long hash_key; /* pid XOR old_socket */ - //hip_list_t next_entry; - //spinlock_t lock; - // atomic_t refcnt; - pid_t pid; - int orig_socket; - int translated_socket; - int domain; - int type; - int protocol; - int local_id_is_translated; - int peer_id_is_translated; - int force_orig; - struct sockaddr_storage orig_local_id; - struct sockaddr_storage orig_peer_id; - struct sockaddr_storage translated_local_id; - struct sockaddr_storage translated_peer_id; - socklen_t orig_local_id_len; - socklen_t orig_peer_id_len; - socklen_t translated_local_id_len; - socklen_t translated_peer_id_len; + unsigned long hash_key; /* pid XOR old_socket */ + //hip_list_t next_entry; + //spinlock_t lock; + // atomic_t refcnt; + pid_t pid; + int orig_socket; + int translated_socket; + int domain; + int type; + int protocol; + int local_id_is_translated; + int peer_id_is_translated; + int force_orig; + struct sockaddr_storage orig_local_id; + struct sockaddr_storage orig_peer_id; + struct sockaddr_storage translated_local_id; + struct sockaddr_storage translated_peer_id; + socklen_t orig_local_id_len; + socklen_t orig_peer_id_len; + socklen_t translated_local_id_len; + socklen_t translated_peer_id_len; }; typedef struct hip_opp_socket_entry hip_opp_socket_t; @@ -42,271 +42,276 @@ // inline int, int range removed //miika unsigned long hip_hash_pid_socket(const void *ptr) { - unsigned long key = ((hip_opp_socket_t *)ptr)->hash_key; - _HIP_DEBUG("hip_hash_pid_socket(%p): 0x%x\n", ptr, key); - return key; + unsigned long key = ((hip_opp_socket_t *) ptr)->hash_key; + _HIP_DEBUG("hip_hash_pid_socket(%p): 0x%x\n", ptr, key); + return key; } - // removed: inline int hip_socketdb_match(const void *ptr1, const void *ptr2) { - unsigned long key1, key2; - key1 = ((hip_opp_socket_t *)ptr1)->hash_key; - key2 = ((hip_opp_socket_t *)ptr2)->hash_key; - _HIP_DEBUG("key1=0x%x key2=0x%x\n", key1, key2); - return (key1 != key2); + unsigned long key1, key2; + key1 = ((hip_opp_socket_t *) ptr1)->hash_key; + key2 = ((hip_opp_socket_t *) ptr2)->hash_key; + _HIP_DEBUG("key1=0x%x key2=0x%x\n", key1, key2); + return key1 != key2; } static inline void *hip_socketdb_get_key(void *entry) { - return &(((hip_opp_socket_t *)entry)->hash_key); + return &(((hip_opp_socket_t *) entry)->hash_key); } static inline void hip_xor_pid_socket(unsigned int *key, int pid, int socket) { - *key = pid ^ socket; + *key = pid ^ socket; } + void hip_init_socket_db(void) { #if 0 - memset(&socketdb,0,sizeof(socketdb)); - - socketdb.head = socketdb; - socketdb.hashsize = HIP_SOCKETDB_SIZE; - socketdb.offset = offsetof(hip_opp_socket_t, next_entry); - socketdb.hash = hip_hash_pid_socket; - socketdb.compare = hip_socketdb_match; - socketdb.hold = hip_socketdb_hold_entry; - socketdb.put = hip_socketdb_put_entry; - socketdb.get_key = hip_socketdb_get_key; - - strncpy(socketdb.name,"SOCKETDB_BYPSOC", 15); - socketdb.name[15] = 0; - + memset(&socketdb, 0, sizeof(socketdb)); + + socketdb.head = socketdb; + socketdb.hashsize = HIP_SOCKETDB_SIZE; + socketdb.offset = offsetof(hip_opp_socket_t, next_entry); + socketdb.hash = hip_hash_pid_socket; + socketdb.compare = hip_socketdb_match; + socketdb.hold = hip_socketdb_hold_entry; + socketdb.put = hip_socketdb_put_entry; + socketdb.get_key = hip_socketdb_get_key; + + strncpy(socketdb.name, "SOCKETDB_BYPSOC", 15); + socketdb.name[15] = 0; + #endif - socketdb = (HIP_HASHTABLE *)hip_ht_init(hip_hash_pid_socket, hip_socketdb_match); - if (!socketdb) HIP_ERROR("could not init socketdb!\n"); + socketdb = (HIP_HASHTABLE *) hip_ht_init(hip_hash_pid_socket, + hip_socketdb_match); + if (!socketdb) { + HIP_ERROR("could not init socketdb!\n"); + } } void hip_uninit_socket_db(void) { - int n; - //int i; - //hip_opp_socket_t *item = NULL; - //hip_opp_socket_t *tmp = NULL; - hip_list_t *item, *tmp; - - _HIP_DEBUG("DEBUG: DUMP SOCKETDB LISTS\n"); - //hip_socketdb_dump(); - - _HIP_DEBUG("DELETING\n"); - // hip_ht_uninit(); + int n; + //int i; + //hip_opp_socket_t *item = NULL; + //hip_opp_socket_t *tmp = NULL; + hip_list_t *item, *tmp; + + _HIP_DEBUG("DEBUG: DUMP SOCKETDB LISTS\n"); + //hip_socketdb_dump(); + + _HIP_DEBUG("DELETING\n"); + // hip_ht_uninit(); #if 0 - for(i = 0; i < HIP_SOCKETDB_SIZE; i++) { - list_for_each_entry_safe(item, tmp, - socketdb[i], - socketdb, - next_entry) { + for (i = 0; i < HIP_SOCKETDB_SIZE; i++) { + list_for_each_entry_safe(item, tmp, + socketdb[i], + socketdb, + next_entry) { #endif - list_for_each_safe(item, tmp, socketdb, n) - { + list_for_each_safe(item, tmp, socketdb, n) + { // if (atomic_read(&item->refcnt) > 2) // HIP_ERROR("socketdb: %p, in use while removing it from socketdb\n", item); - //hip_socketdb_put_entry(item); - HIP_FREE((void*)list_entry(item)); - } -// } + //hip_socketdb_put_entry(item); + HIP_FREE((void *) list_entry(item)); + } +// } - hip_ht_uninit(socketdb); + hip_ht_uninit(socketdb); } hip_opp_socket_t *hip_socketdb_find_entry(int pid, int socket) { - unsigned int key = 0; - - hip_xor_pid_socket(&key, pid, socket); - HIP_DEBUG("pid %d socket %d computed key 0x%x\n", pid, socket, key); - - return (hip_opp_socket_t *)hip_ht_find(socketdb, (void *)&key); + unsigned int key = 0; + + hip_xor_pid_socket(&key, pid, socket); + HIP_DEBUG("pid %d socket %d computed key 0x%x\n", pid, socket, key); + + return (hip_opp_socket_t *) hip_ht_find(socketdb, (void *) &key); } int hip_socketdb_add_entry(pid_t pid, int socket) { - int err = 0; - hip_opp_socket_t *new_item = NULL; - - new_item = (hip_opp_socket_t *)malloc(sizeof(hip_opp_socket_t)); - if (!new_item) { - HIP_ERROR("new_item malloc failed\n"); - err = -ENOMEM; - return err; - } - - memset(new_item, 0, sizeof(hip_opp_socket_t)); - - hip_xor_pid_socket((unsigned int *) &new_item->hash_key, pid, socket); - new_item->pid = pid; - new_item->orig_socket = socket; - HIP_DEBUG("added entry %p %d\n", pid, socket); - err = hip_ht_add(socketdb, new_item); - if (err) HIP_ERROR("hip_ht_add() failed!\n"); - //hip_socketdb_dump(); - - return err; + int err = 0; + hip_opp_socket_t *new_item = NULL; + + new_item = (hip_opp_socket_t *) malloc(sizeof(hip_opp_socket_t)); + if (!new_item) { + HIP_ERROR("new_item malloc failed\n"); + err = -ENOMEM; + return err; + } + + memset(new_item, 0, sizeof(hip_opp_socket_t)); + + hip_xor_pid_socket((unsigned int *) &new_item->hash_key, pid, socket); + new_item->pid = pid; + new_item->orig_socket = socket; + HIP_DEBUG("added entry %p %d\n", pid, socket); + err = hip_ht_add(socketdb, new_item); + if (err) { + HIP_ERROR("hip_ht_add() failed!\n"); + } + //hip_socketdb_dump(); + + return err; } void hip_socketdb_dump(void) { - int n; - /*int i; - char src_ip[INET6_ADDRSTRLEN] = "\0"; - char dst_ip[INET6_ADDRSTRLEN] = "\0"; - char src_hit[INET6_ADDRSTRLEN] = "\0"; - char dst_hit[INET6_ADDRSTRLEN] = "\0";*/ - hip_list_t *item, *tmp; - hip_opp_socket_t *data; - - HIP_DEBUG("start socketdb dump\n"); - - //HIP_LOCK_HT(socketdb); + int n; + /*int i; + * char src_ip[INET6_ADDRSTRLEN] = "\0"; + * char dst_ip[INET6_ADDRSTRLEN] = "\0"; + * char src_hit[INET6_ADDRSTRLEN] = "\0"; + * char dst_hit[INET6_ADDRSTRLEN] = "\0";*/ + hip_list_t *item, *tmp; + hip_opp_socket_t *data; + + HIP_DEBUG("start socketdb dump\n"); + + //HIP_LOCK_HT(socketdb); #if 0 - for(i = 0; i < HIP_SOCKETDB_SIZE; i++) - { - //if (!list_empty(socketdb[i])) - { - HIP_DEBUG("HT[%d]\n", i); + for (i = 0; i < HIP_SOCKETDB_SIZE; i++) { + //if (!list_empty(socketdb[i])) + { + HIP_DEBUG("HT[%d]\n", i); #endif - list_for_each_safe(item, tmp, socketdb, n) - { - data = (hip_opp_socket_t *)list_entry(item); - HIP_DEBUG("pid=%d orig_socket=%d new_socket=%d hash_key=%d" - " domain=%d type=%d protocol=%d src_ip=%s dst_ip=%s" - " src_hit=%s dst_hit=%s lock=%d refcnt=%d\n", - data->pid, data->orig_socket); - } + list_for_each_safe(item, tmp, socketdb, n) + { + data = (hip_opp_socket_t *) list_entry(item); + HIP_DEBUG("pid=%d orig_socket=%d new_socket=%d hash_key=%d" + " domain=%d type=%d protocol=%d src_ip=%s dst_ip=%s" + " src_hit=%s dst_hit=%s lock=%d refcnt=%d\n", + data->pid, data->orig_socket); + } // } // } - //HIP_UNLOCK_HT(socketdb); - HIP_DEBUG("end socketdb dump\n"); + //HIP_UNLOCK_HT(socketdb); + HIP_DEBUG("end socketdb dump\n"); } void hip_socketdb_del_entry_by_entry(hip_opp_socket_t *entry) { - _HIP_DEBUG("entry=0x%p pid=%d, orig_socket=%d\n", entry, - entry->pid, entry->orig_socket); - //HIP_LOCK_SOCKET(entry); - hip_ht_delete(socketdb, entry); - //HIP_UNLOCK_SOCKET(entry); - HIP_FREE(entry); + _HIP_DEBUG("entry=0x%p pid=%d, orig_socket=%d\n", entry, + entry->pid, entry->orig_socket); + //HIP_LOCK_SOCKET(entry); + hip_ht_delete(socketdb, entry); + //HIP_UNLOCK_SOCKET(entry); + HIP_FREE(entry); } int hip_socketdb_del_entry(int pid, int socket) { - hip_opp_socket_t *entry = NULL; - - entry = hip_socketdb_find_entry(pid, socket); - if (!entry) { - return -ENOENT; - } - hip_socketdb_del_entry_by_entry(entry); - return 0; + hip_opp_socket_t *entry = NULL; + + entry = hip_socketdb_find_entry(pid, socket); + if (!entry) { + return -ENOENT; + } + hip_socketdb_del_entry_by_entry(entry); + return 0; } // used to test socketdb -void test_db(void){ - pid_t pid = getpid(); - int socket = 1; - int err = 0; - hip_opp_socket_t *entry = NULL; - // struct hip_opp_socket_entry *entry = NULL; - - HIP_DEBUG("testing db\n"); - - HIP_DEBUG("1111 pid=%d, socket=%d\n", pid, socket); - entry = hip_socketdb_find_entry(pid, socket); - HIP_ASSERT(!entry); - err = hip_socketdb_add_entry(pid, socket); - HIP_ASSERT(!err); - entry = hip_socketdb_find_entry(pid, socket); - HIP_ASSERT(entry); - hip_socketdb_dump(); - - // pid++; - socket++; - HIP_DEBUG("2222 pid=%d, socket=%d\n", pid, socket); - entry = NULL; - entry = hip_socketdb_find_entry(pid, socket); - HIP_ASSERT(!entry); - err = hip_socketdb_add_entry(pid, socket); - HIP_ASSERT(!err); - entry = hip_socketdb_find_entry(pid, socket); - HIP_ASSERT(entry); - entry->translated_socket = socket+100; - hip_socketdb_dump(); - - - //pid++; - socket++; - HIP_DEBUG("3333 pid=%d, socket=%d\n", pid, socket); - entry = NULL; - entry = hip_socketdb_find_entry(pid, socket); - HIP_ASSERT(!entry); - err = hip_socketdb_add_entry(pid, socket); - HIP_ASSERT(!err); - entry = NULL; - entry = hip_socketdb_find_entry(pid, socket); - HIP_ASSERT(entry); - hip_socketdb_dump(); - - HIP_DEBUG("3333 testing del entry\n\n"); - HIP_DEBUG("pid=%d, socket=%d\n", pid, socket); - entry = NULL; - entry = hip_socketdb_find_entry(pid, socket); - HIP_ASSERT(entry); - entry = NULL; - err = hip_socketdb_del_entry(pid, socket); - HIP_ASSERT(!err); - entry = NULL; - entry = hip_socketdb_find_entry(pid, socket); - HIP_ASSERT(!entry); - hip_socketdb_dump(); - - - HIP_DEBUG("2222 testing del entry by entry\n\n"); - socket--; - HIP_DEBUG("pid=%d, socket=%d\n", pid, socket); - entry = NULL; - entry = hip_socketdb_find_entry(pid, socket); - HIP_ASSERT(entry); - hip_socketdb_del_entry_by_entry(entry); - entry = NULL; - entry = NULL; - entry = hip_socketdb_find_entry(pid, socket); - HIP_ASSERT(!entry); - hip_socketdb_dump(); - - HIP_DEBUG("1111 testing del entry by entry\n\n"); - socket--; - HIP_DEBUG("pid=%d, socket=%d\n", pid, socket); - entry = NULL; - entry = hip_socketdb_find_entry(pid, socket); - HIP_ASSERT(entry); - hip_socketdb_del_entry_by_entry(entry); - entry = NULL; - entry = hip_socketdb_find_entry(pid, socket); - HIP_ASSERT(!entry); - hip_socketdb_dump(); - HIP_DEBUG("end of testing db\n"); - - HIP_DEBUG("*** success ***\n"); +void test_db(void) +{ + pid_t pid = getpid(); + int socket = 1; + int err = 0; + hip_opp_socket_t *entry = NULL; + // struct hip_opp_socket_entry *entry = NULL; + + HIP_DEBUG("testing db\n"); + + HIP_DEBUG("1111 pid=%d, socket=%d\n", pid, socket); + entry = hip_socketdb_find_entry(pid, socket); + HIP_ASSERT(!entry); + err = hip_socketdb_add_entry(pid, socket); + HIP_ASSERT(!err); + entry = hip_socketdb_find_entry(pid, socket); + HIP_ASSERT(entry); + hip_socketdb_dump(); + + // pid++; + socket++; + HIP_DEBUG("2222 pid=%d, socket=%d\n", pid, socket); + entry = NULL; + entry = hip_socketdb_find_entry(pid, socket); + HIP_ASSERT(!entry); + err = hip_socketdb_add_entry(pid, socket); + HIP_ASSERT(!err); + entry = hip_socketdb_find_entry(pid, socket); + HIP_ASSERT(entry); + entry->translated_socket = socket + 100; + hip_socketdb_dump(); + + + //pid++; + socket++; + HIP_DEBUG("3333 pid=%d, socket=%d\n", pid, socket); + entry = NULL; + entry = hip_socketdb_find_entry(pid, socket); + HIP_ASSERT(!entry); + err = hip_socketdb_add_entry(pid, socket); + HIP_ASSERT(!err); + entry = NULL; + entry = hip_socketdb_find_entry(pid, socket); + HIP_ASSERT(entry); + hip_socketdb_dump(); + + HIP_DEBUG("3333 testing del entry\n\n"); + HIP_DEBUG("pid=%d, socket=%d\n", pid, socket); + entry = NULL; + entry = hip_socketdb_find_entry(pid, socket); + HIP_ASSERT(entry); + entry = NULL; + err = hip_socketdb_del_entry(pid, socket); + HIP_ASSERT(!err); + entry = NULL; + entry = hip_socketdb_find_entry(pid, socket); + HIP_ASSERT(!entry); + hip_socketdb_dump(); + + + HIP_DEBUG("2222 testing del entry by entry\n\n"); + socket--; + HIP_DEBUG("pid=%d, socket=%d\n", pid, socket); + entry = NULL; + entry = hip_socketdb_find_entry(pid, socket); + HIP_ASSERT(entry); + hip_socketdb_del_entry_by_entry(entry); + entry = NULL; + entry = NULL; + entry = hip_socketdb_find_entry(pid, socket); + HIP_ASSERT(!entry); + hip_socketdb_dump(); + + HIP_DEBUG("1111 testing del entry by entry\n\n"); + socket--; + HIP_DEBUG("pid=%d, socket=%d\n", pid, socket); + entry = NULL; + entry = hip_socketdb_find_entry(pid, socket); + HIP_ASSERT(entry); + hip_socketdb_del_entry_by_entry(entry); + entry = NULL; + entry = hip_socketdb_find_entry(pid, socket); + HIP_ASSERT(!entry); + hip_socketdb_dump(); + HIP_DEBUG("end of testing db\n"); + + HIP_DEBUG("*** success ***\n"); } int main(int argc, char **argv) { - hip_init_socket_db(); - test_db(); - hip_uninit_socket_db(); - return 0; + hip_init_socket_db(); + test_db(); + hip_uninit_socket_db(); + return 0; } === modified file 'test/hc_performance.c' --- test/hc_performance.c 2010-01-19 09:28:42 +0000 +++ test/hc_performance.c 2010-02-10 23:43:06 +0000 @@ -1,5 +1,5 @@ -#include <stdio.h> /* printf & co */ -#include <stdlib.h> /* exit & co */ +#include <stdio.h> /* printf & co */ +#include <stdlib.h> /* exit & co */ #include <unistd.h> #include <openssl/md5.h> #include <openssl/sha.h> @@ -9,33 +9,34 @@ #include "lib/core/hashtree.h" #include "lib/core/debug.h" -const hash_function_t hash_functions[2] = {(hash_function_t)SHA1, (hash_function_t)MD5}; +const hash_function_t hash_functions[2] = {(hash_function_t) SHA1, + (hash_function_t) MD5}; -int count = 100; +int count = 100; // this is supported by both md5 and sha1 -int hash_length = 16; +int hash_length = 16; int hchain_length = 100000; int verify_length = 64; hash_function_t hash_function; -int test_hc = 0; -int test_ht = 0; +int test_hc = 0; +int test_ht = 0; void print_usage(void) { - printf( "Usage: hc_performance -c|t -s|m [-lhvn NUM]\n" - "-c = do hash-chain performance tests\n" - "-t = do hash-tree performance tests\n" - "-s = use SHA1 hash-function\n" - "-m = use MD5 hash-function\n" - "-l [NUM] = create hash-chain with length NUM\n" - "-h [NUM] = create hash elements of length NUM\n" - "-v [NUM] = verify NUM elements\n" - "-n [NUM] = do NUM measurements\n"); + printf( "Usage: hc_performance -c|t -s|m [-lhvn NUM]\n" + "-c = do hash-chain performance tests\n" + "-t = do hash-tree performance tests\n" + "-s = use SHA1 hash-function\n" + "-m = use MD5 hash-function\n" + "-l [NUM] = create hash-chain with length NUM\n" + "-h [NUM] = create hash elements of length NUM\n" + "-v [NUM] = verify NUM elements\n" + "-n [NUM] = do NUM measurements\n"); } /*! - * \brief Determine and print the gettimeofday time resolution. + * \brief Determine and print the gettimeofday time resolution. * * \author Tobias Heer * @@ -43,325 +44,306 @@ * * \return void */ -void print_timeres(void){ - - struct timeval tv1, tv2; - int i; - printf( "-------------------------------\n" - "Determine gettimeofday resolution:\n"); - - - for(i = 0; i < 10; i++){ - gettimeofday(&tv1, NULL); - do { - gettimeofday(&tv2, NULL); - } while (tv1.tv_usec == tv2.tv_usec); - - printf("Resolution: %li us\n", tv2.tv_usec - tv1.tv_usec + - 1000000 * (tv2.tv_sec - tv1.tv_sec)); - } - - printf( "-------------------------------\n\n\n"); +void print_timeres(void) +{ + struct timeval tv1, tv2; + int i; + printf( "-------------------------------\n" + "Determine gettimeofday resolution:\n"); + + + for (i = 0; i < 10; i++) { + gettimeofday(&tv1, NULL); + do { + gettimeofday(&tv2, NULL); + } while (tv1.tv_usec == tv2.tv_usec); + + printf("Resolution: %li us\n", tv2.tv_usec - tv1.tv_usec + + 1000000 * (tv2.tv_sec - tv1.tv_sec)); + } + + printf( "-------------------------------\n\n\n"); } -int main(int argc, char ** argv) +int main(int argc, char **argv) { - int i; - int c; - int err = 0; - struct timeval start_time; - struct timeval stop_time; - hash_chain_t *hchain = NULL; - hash_tree_t *htree = NULL; - statistics_data_t creation_stats; - statistics_data_t verify_stats; - uint64_t timediff = 0; - uint32_t num_items = 0; - double min = 0.0, max = 0.0, avg = 0.0; - double std_dev = 0.0; - unsigned char *branch_nodes = NULL; - int branch_length = 0; - const unsigned char *secret = NULL; - int secret_length = 0; - hash_chain_t *hchains[8]; - const unsigned char *data = NULL; - int data_length = 0; - const unsigned char *root = NULL; - int root_length = 0; - - hash_function = NULL; - - - while ((c=getopt(argc, argv, "ctsml:h:v:n:")) != -1) - { - switch (c) - { - case 'c': - test_hc = 1; - break; - case 't': - test_ht = 1; - break; - case 's': - hash_function = hash_functions[0]; - break; - case 'm': - hash_function = hash_functions[1]; - break; - case 'l': - hchain_length = atoi(optarg); - break; - case 'h': - hash_length = atoi(optarg); - break; - case 'v': - verify_length = atoi(optarg); - break; - case 'n': - count = atoi(optarg); - break; - case ':': - printf("Missing argument %c\n", optopt); - print_usage(); - exit(1); - case '?': - printf("Unknown option %c\n", optopt); - print_usage(); - exit(1); - } - } - - if (hash_function == NULL) - { - printf("no hash function selected!\n"); - print_usage(); - exit(1); - } - - hip_set_logdebug(LOGDEBUG_NONE); - - memset(&creation_stats, 0, sizeof(statistics_data_t)); - memset(&verify_stats, 0, sizeof(statistics_data_t)); - - print_timeres(); - - if (test_hc) - { - printf( "-------------------------------\n" - "Hash chain performance test\n" - "-------------------------------\n\n"); - - printf("Creating %d hash chains of length %d with element length %d\n", - count, hchain_length, hash_length); - - for(i = 0; i < count; i++) - { - gettimeofday(&start_time, NULL); - if ( (hchain = hchain_create(hash_function, hash_length, hchain_length, 0, - NULL)) ) - { - gettimeofday(&stop_time, NULL); - timediff = calc_timeval_diff(&start_time, &stop_time); - add_statistics_item(&creation_stats, timediff); - hchain_free(hchain); - } else - { - printf("ERROR creating hchain!\n"); - exit(1); - } - } - - calc_statistics(&creation_stats, &num_items, &min, &max, &avg, &std_dev, - STATS_IN_MSECS); - printf("creation statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n", - num_items, min, max, avg, std_dev); - - printf("\n"); - - printf("Verifying %d hash chains of length %d with element length %d\n", - count, verify_length, hash_length); - - for(i = 0; i < count; i++) - { - if (!(hchain = hchain_create(hash_function, hash_length, verify_length, 0, - NULL))) - { - printf("ERROR creating hchain!"); - exit(1); - } - - gettimeofday(&start_time, NULL); - if(hchain_verify(hchain_get_seed(hchain), hchain_get_anchor(hchain), - hash_function, hash_length, verify_length, NULL, 0)) - { - gettimeofday(&stop_time, NULL); - timediff = calc_timeval_diff(&start_time, &stop_time); - add_statistics_item(&verify_stats, timediff); - hchain_free(hchain); - } else - { - printf("ERROR verifying hchain!\n"); - exit(1); - } - } - - calc_statistics(&verify_stats, &num_items, &min, &max, &avg, &std_dev, - STATS_IN_MSECS); - printf("verification statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n", - num_items, min, max, avg, std_dev); - } - - if (test_ht) - { - printf( "\n-------------------------------\n" - "Hash tree performance test\n" - "-------------------------------\n\n"); - - memset(&creation_stats, 0, sizeof(statistics_data_t)); - memset(&verify_stats, 0, sizeof(statistics_data_t)); - - printf("Creating %d hash trees of length %d with element length %d\n", - count, hchain_length, hash_length); - - for(i = 0; i < count; i++) - { - HIP_DEBUG("number of leaves: %i\n", hchain_length); - HIP_DEBUG("hash_length: %i\n", hash_length); - HIP_DEBUG("data_length: %i\n", hash_length); - - gettimeofday(&start_time, NULL); - htree = htree_init(hchain_length, hash_length, hash_length, 0, NULL, 0); - htree_add_random_data(htree, hchain_length); - htree_calc_nodes(htree, htree_leaf_generator, htree_node_generator, NULL); - gettimeofday(&stop_time, NULL); - timediff = calc_timeval_diff(&start_time, &stop_time); - add_statistics_item(&creation_stats, timediff); - - htree_free(htree); - } - - calc_statistics(&creation_stats, &num_items, &min, &max, &avg, &std_dev, - STATS_IN_MSECS); - printf("creation statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n", - num_items, min, max, avg, std_dev); - - for(i = 0; i < count; i++) - { - htree = htree_init(hchain_length, hash_length, hash_length, hash_length, NULL, 0); - htree_add_random_data(htree, hchain_length); - htree_add_random_secrets(htree); - htree_calc_nodes(htree, htree_leaf_generator, htree_node_generator, NULL); - - root = htree_get_root(htree, &root_length); - branch_nodes = htree_get_branch(htree, i, NULL, &branch_length); - data = htree_get_data(htree, i, &data_length); - secret = htree_get_secret(htree, i, &secret_length); - - gettimeofday(&start_time, NULL); - if (!htree_verify_branch(root, root_length, - branch_nodes, branch_length, - data, data_length, i, - secret, secret_length, - htree_leaf_generator, htree_node_generator, NULL)) - { - gettimeofday(&stop_time, NULL); - timediff = calc_timeval_diff(&start_time, &stop_time); - add_statistics_item(&verify_stats, timediff); - - HIP_DEBUG("branch verified\n"); - - } else - { - printf("ERROR verifying htree!\n"); - exit(1); - } - - htree_free(htree); - } - - calc_statistics(&verify_stats, &num_items, &min, &max, &avg, &std_dev, - STATS_IN_MSECS); - printf("verification statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n", - num_items, min, max, avg, std_dev); - - - - printf("\n\ntrying out hchain linking...\n"); - - // simulate level 0 creation - htree = htree_init(8, hash_length, hash_length, hash_length, NULL, 0); - htree_add_random_secrets(htree); - - for (i = 0; i < 8; i++) - { - hchains[i] = hchain_create(hash_function, hash_length, hchain_length, 0, - NULL); - htree_add_data(htree, hchain_get_anchor(hchains[i]), hash_length); - } - - htree_calc_nodes(htree, htree_leaf_generator, htree_node_generator, NULL); - - // simulate level 1 creation - hchain = hchain_create(hash_function, hash_length, hchain_length, 1, - htree); - - // simulate BEX - // get hchain anchor - root = htree_get_root(htree, &root_length); - - // simulate level 1 hchain verification - if(!hchain_verify(hchain_get_seed(hchain), hchain_get_anchor(hchain), - hash_function, hash_length, verify_length, root, root_length)) - { - printf("hchain level 1 verfied\n"); - - } else - { - printf("ERROR verifying hchain level 1!\n"); - exit(1); - } - - // simulate update - branch_nodes = htree_get_branch(htree, 0, NULL, &branch_length); - secret = htree_get_secret(htree, 0, &secret_length); - data = htree_get_data(htree, 0, &data_length); - - if (!htree_verify_branch(root, root_length, - branch_nodes, branch_length, - data, data_length, i, - secret, secret_length, - htree_leaf_generator, htree_node_generator, NULL)) - { - printf("anchor verified\n"); - - } else - { - printf("ERROR verifying anchor!\n"); - exit(1); - } - - if (!memcmp(data, hchain_get_anchor(hchains[0]), hash_length)) - { - printf("yes, this is the anchor we verified!\n"); - } else - { - printf("ERROR no this is not the anchor we verified!\n"); - exit(1); - } - hchain_free(hchain); - - // simulate level 0 hchain verification - if(!hchain_verify(hchain_get_seed(hchains[0]), data, - hash_function, hash_length, verify_length, NULL, 0)) - { - printf("hchain level 0 verfied\n"); - - } else - { - printf("ERROR verifying hchain level 0!\n"); - exit(1); - } - } - - return err; + int i; + int c; + int err = 0; + struct timeval start_time; + struct timeval stop_time; + hash_chain_t *hchain = NULL; + hash_tree_t *htree = NULL; + statistics_data_t creation_stats; + statistics_data_t verify_stats; + uint64_t timediff = 0; + uint32_t num_items = 0; + double min = 0.0, max = 0.0, avg = 0.0; + double std_dev = 0.0; + unsigned char *branch_nodes = NULL; + int branch_length = 0; + const unsigned char *secret = NULL; + int secret_length = 0; + hash_chain_t *hchains[8]; + const unsigned char *data = NULL; + int data_length = 0; + const unsigned char *root = NULL; + int root_length = 0; + + hash_function = NULL; + + + while ((c = getopt(argc, argv, "ctsml:h:v:n:")) != -1) { + switch (c) { + case 'c': + test_hc = 1; + break; + case 't': + test_ht = 1; + break; + case 's': + hash_function = hash_functions[0]; + break; + case 'm': + hash_function = hash_functions[1]; + break; + case 'l': + hchain_length = atoi(optarg); + break; + case 'h': + hash_length = atoi(optarg); + break; + case 'v': + verify_length = atoi(optarg); + break; + case 'n': + count = atoi(optarg); + break; + case ':': + printf("Missing argument %c\n", optopt); + print_usage(); + exit(1); + case '?': + printf("Unknown option %c\n", optopt); + print_usage(); + exit(1); + } + } + + if (hash_function == NULL) { + printf("no hash function selected!\n"); + print_usage(); + exit(1); + } + + hip_set_logdebug(LOGDEBUG_NONE); + + memset(&creation_stats, 0, sizeof(statistics_data_t)); + memset(&verify_stats, 0, sizeof(statistics_data_t)); + + print_timeres(); + + if (test_hc) { + printf( "-------------------------------\n" + "Hash chain performance test\n" + "-------------------------------\n\n"); + + printf("Creating %d hash chains of length %d with element length %d\n", + count, hchain_length, hash_length); + + for (i = 0; i < count; i++) { + gettimeofday(&start_time, NULL); + if ((hchain = hchain_create(hash_function, hash_length, + hchain_length, 0, NULL))) { + gettimeofday(&stop_time, NULL); + timediff = calc_timeval_diff(&start_time, &stop_time); + add_statistics_item(&creation_stats, timediff); + hchain_free(hchain); + } else { + printf("ERROR creating hchain!\n"); + exit(1); + } + } + + calc_statistics(&creation_stats, &num_items, &min, &max, &avg, &std_dev, + STATS_IN_MSECS); + printf("creation statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n", + num_items, min, max, avg, std_dev); + + printf("\n"); + + printf("Verifying %d hash chains of length %d with element length %d\n", + count, verify_length, hash_length); + + for (i = 0; i < count; i++) { + if (!(hchain = hchain_create(hash_function, hash_length, + verify_length, 0, NULL))) { + printf("ERROR creating hchain!"); + exit(1); + } + + gettimeofday(&start_time, NULL); + if (hchain_verify(hchain_get_seed(hchain), hchain_get_anchor(hchain), + hash_function, hash_length, + verify_length, NULL, 0)) { + gettimeofday(&stop_time, NULL); + timediff = calc_timeval_diff(&start_time, &stop_time); + add_statistics_item(&verify_stats, timediff); + hchain_free(hchain); + } else { + printf("ERROR verifying hchain!\n"); + exit(1); + } + } + + calc_statistics(&verify_stats, &num_items, &min, &max, &avg, &std_dev, + STATS_IN_MSECS); + printf("verification statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n", + num_items, min, max, avg, std_dev); + } + + if (test_ht) { + printf( "\n-------------------------------\n" + "Hash tree performance test\n" + "-------------------------------\n\n"); + + memset(&creation_stats, 0, sizeof(statistics_data_t)); + memset(&verify_stats, 0, sizeof(statistics_data_t)); + + printf("Creating %d hash trees of length %d with element length %d\n", + count, hchain_length, hash_length); + + for (i = 0; i < count; i++) { + HIP_DEBUG("number of leaves: %i\n", hchain_length); + HIP_DEBUG("hash_length: %i\n", hash_length); + HIP_DEBUG("data_length: %i\n", hash_length); + + gettimeofday(&start_time, NULL); + htree = htree_init(hchain_length, hash_length, + hash_length, 0, NULL, 0); + htree_add_random_data(htree, hchain_length); + htree_calc_nodes(htree, htree_leaf_generator, + htree_node_generator, NULL); + gettimeofday(&stop_time, NULL); + timediff = calc_timeval_diff(&start_time, &stop_time); + add_statistics_item(&creation_stats, timediff); + + htree_free(htree); + } + + calc_statistics(&creation_stats, &num_items, &min, &max, &avg, &std_dev, + STATS_IN_MSECS); + printf("creation statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n", + num_items, min, max, avg, std_dev); + + for (i = 0; i < count; i++) { + htree = htree_init(hchain_length, hash_length, hash_length, + hash_length, NULL, 0); + htree_add_random_data(htree, hchain_length); + htree_add_random_secrets(htree); + htree_calc_nodes(htree, htree_leaf_generator, + htree_node_generator, NULL); + + root = htree_get_root(htree, &root_length); + branch_nodes = htree_get_branch(htree, i, NULL, &branch_length); + data = htree_get_data(htree, i, &data_length); + secret = htree_get_secret(htree, i, &secret_length); + + gettimeofday(&start_time, NULL); + if (!htree_verify_branch(root, root_length, + branch_nodes, branch_length, + data, data_length, i, + secret, secret_length, + htree_leaf_generator, + htree_node_generator, NULL)) { + gettimeofday(&stop_time, NULL); + timediff = calc_timeval_diff(&start_time, &stop_time); + add_statistics_item(&verify_stats, timediff); + + HIP_DEBUG("branch verified\n"); + } else { + printf("ERROR verifying htree!\n"); + exit(1); + } + + htree_free(htree); + } + + calc_statistics(&verify_stats, &num_items, &min, &max, &avg, &std_dev, + STATS_IN_MSECS); + printf("verification statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n", + num_items, min, max, avg, std_dev); + + + + printf("\n\ntrying out hchain linking...\n"); + + // simulate level 0 creation + htree = htree_init(8, hash_length, hash_length, hash_length, NULL, 0); + htree_add_random_secrets(htree); + + for (i = 0; i < 8; i++) { + hchains[i] = hchain_create(hash_function, hash_length, + hchain_length, 0, NULL); + htree_add_data(htree, hchain_get_anchor(hchains[i]), hash_length); + } + + htree_calc_nodes(htree, htree_leaf_generator, + htree_node_generator, NULL); + + // simulate level 1 creation + hchain = hchain_create(hash_function, hash_length, hchain_length, 1, + htree); + + // simulate BEX + // get hchain anchor + root = htree_get_root(htree, &root_length); + + // simulate level 1 hchain verification + if (!hchain_verify(hchain_get_seed(hchain), hchain_get_anchor(hchain), + hash_function, hash_length, verify_length, + root, root_length)) { + printf("hchain level 1 verfied\n"); + } else { + printf("ERROR verifying hchain level 1!\n"); + exit(1); + } + + // simulate update + branch_nodes = htree_get_branch(htree, 0, NULL, &branch_length); + secret = htree_get_secret(htree, 0, &secret_length); + data = htree_get_data(htree, 0, &data_length); + + if (!htree_verify_branch(root, root_length, + branch_nodes, branch_length, + data, data_length, i, + secret, secret_length, + htree_leaf_generator, + htree_node_generator, NULL)) { + printf("anchor verified\n"); + } else { + printf("ERROR verifying anchor!\n"); + exit(1); + } + + if (!memcmp(data, hchain_get_anchor(hchains[0]), hash_length)) { + printf("yes, this is the anchor we verified!\n"); + } else { + printf("ERROR no this is not the anchor we verified!\n"); + exit(1); + } + hchain_free(hchain); + + // simulate level 0 hchain verification + if (!hchain_verify(hchain_get_seed(hchains[0]), data, + hash_function, hash_length, + verify_length, NULL, 0)) { + printf("hchain level 0 verfied\n"); + } else { + printf("ERROR verifying hchain level 0!\n"); + exit(1); + } + } + + return err; } === modified file 'test/hipsetup.c' --- test/hipsetup.c 2010-01-19 11:26:32 +0000 +++ test/hipsetup.c 2010-02-10 23:43:06 +0000 @@ -10,126 +10,129 @@ #include "lib/conf/hipconf.h" const char *usage_str = "hipsetup -h for help\n" - "hipsetup -m to install hipmod module\n" - "hipsetup -i peer_name for Base Exchange Initiator\n" - "hipsetup -r for Base Exchange Responder\n" - "hipsetup -s for Base Exchange SSH\n" - "hipsetup -b for BOS (in initiator)\n" - "\n" - ; + "hipsetup -m to install hipmod module\n" + "hipsetup -i peer_name for Base Exchange Initiator\n" + "hipsetup -r for Base Exchange Responder\n" + "hipsetup -s for Base Exchange SSH\n" + "hipsetup -b for BOS (in initiator)\n" + "\n" +; void usage_f() { - printf("Usage:\n%s\n", usage_str); + printf("Usage:\n%s\n", usage_str); } int main(int argc, char *argv[]) { - int c, err = 0; - struct hip_common *msg; - char *peer_name, buf[20]; - - if(argc < 2){ - printf("No args specified \n"); - usage_f(); - return 0; - } - - msg = malloc(HIP_MAX_PACKET); - if (!msg) { - HIP_ERROR("malloc failed\n"); - err = -1; - goto out; - } - hip_msg_init(msg); - - while ((c = getopt(argc, argv, ":hmrsdbi:")) != -1) - { - switch (c){ - case 'h': - usage_f(); - break; - case 'm': - /* Install the modules */ - err = main_install(msg); - if (err) - goto out_err; - -#if 0 - if (hip_get_msg_type(msg) == 0) - goto out_err; - - err = hip_send_daemon_info(msg, 0, 0); - if (err) { - HIP_ERROR("sending msg failed\n"); - goto out_err; - } -#endif - break; - case 'd': - /* HIPL_DIR */ - /* I don't know whether this is needed anymore ...*/ - break; - case 'i': - /* Base Exchange Initiator */ - printf("Initiator mode\n"); - hip_set_logtype(LOGTYPE_STDERR); - hip_set_logfmt(LOGFMT_SHORT); - if (optarg[0] == '0') - peer_name = NULL; - else - peer_name = optarg; - sprintf(buf, "%d",DEFAULT_PORT); - main_client_gai(SOCK_STREAM, peer_name, buf, AI_HIP); - break; - case 'r': - printf("Responder mode\n"); - /* Base Exchange Responder */ - main_server(SOCK_STREAM, DEFAULT_PORT); - break; - case 's': - /* Base Exchange SSH */ - printf("Initiator-responder mode\n"); - break; - case 'b': - /* BOS */ - printf("BOS\n"); -#if 0 - HIP_IFEL(hip_conf_handle_bos(msg, 0, (const char **) NULL, 0), -1, "Failed to handle BOS\n"); - - /* hipconf new hi does not involve any messages to kernel */ - HIP_IFE((hip_get_msg_type(msg)), -1); - - HIP_IFEL(hip_send_daemon_info(msg), -1, "Sending msg failed\n"); -#endif - err = hip_conf_handle_bos(msg, 0, (const char **) NULL, 0, 0); - if (err) { - HIP_ERROR("failed to handle msg\n"); - goto out_err; - } - - if (hip_get_msg_type(msg) == 0) - goto out_err; - - err = hip_send_recv_daemon_info(msg, 0, 0); - if (err) { - HIP_ERROR("sending msg failed\n"); - goto out_err; - } - break; - case ':': - printf("Missing argument %c\n", optopt); - usage_f(); - return(0); - case '?': - printf("Unknown option %c\n", optopt); - usage_f(); - return(0); - } - } + int c, err = 0; + struct hip_common *msg; + char *peer_name, buf[20]; + + if (argc < 2) { + printf("No args specified \n"); + usage_f(); + return 0; + } + + msg = malloc(HIP_MAX_PACKET); + if (!msg) { + HIP_ERROR("malloc failed\n"); + err = -1; + goto out; + } + hip_msg_init(msg); + + while ((c = getopt(argc, argv, ":hmrsdbi:")) != -1) { + switch (c) { + case 'h': + usage_f(); + break; + case 'm': + /* Install the modules */ + err = main_install(msg); + if (err) { + goto out_err; + } + +#if 0 + if (hip_get_msg_type(msg) == 0) { + goto out_err; + } + + err = hip_send_daemon_info(msg, 0, 0); + if (err) { + HIP_ERROR("sending msg failed\n"); + goto out_err; + } +#endif + break; + case 'd': + /* HIPL_DIR */ + /* I don't know whether this is needed anymore ...*/ + break; + case 'i': + /* Base Exchange Initiator */ + printf("Initiator mode\n"); + hip_set_logtype(LOGTYPE_STDERR); + hip_set_logfmt(LOGFMT_SHORT); + if (optarg[0] == '0') { + peer_name = NULL; + } else { + peer_name = optarg; + } + sprintf(buf, "%d", DEFAULT_PORT); + main_client_gai(SOCK_STREAM, peer_name, buf, AI_HIP); + break; + case 'r': + printf("Responder mode\n"); + /* Base Exchange Responder */ + main_server(SOCK_STREAM, DEFAULT_PORT); + break; + case 's': + /* Base Exchange SSH */ + printf("Initiator-responder mode\n"); + break; + case 'b': + /* BOS */ + printf("BOS\n"); +#if 0 + HIP_IFEL(hip_conf_handle_bos(msg, 0, (const char **) NULL, 0), -1, "Failed to handle BOS\n"); + + /* hipconf new hi does not involve any messages to kernel */ + HIP_IFE((hip_get_msg_type(msg)), -1); + + HIP_IFEL(hip_send_daemon_info(msg), -1, "Sending msg failed\n"); +#endif + err = hip_conf_handle_bos(msg, 0, (const char **) NULL, 0, 0); + if (err) { + HIP_ERROR("failed to handle msg\n"); + goto out_err; + } + + if (hip_get_msg_type(msg) == 0) { + goto out_err; + } + + err = hip_send_recv_daemon_info(msg, 0, 0); + if (err) { + HIP_ERROR("sending msg failed\n"); + goto out_err; + } + break; + case ':': + printf("Missing argument %c\n", optopt); + usage_f(); + return 0; + case '?': + printf("Unknown option %c\n", optopt); + usage_f(); + return 0; + } + } out_err: - free(msg); + free(msg); out: - return err; + return err; } === modified file 'test/keygentest.c' --- test/keygentest.c 2010-01-19 09:28:42 +0000 +++ test/keygentest.c 2010-02-10 23:43:06 +0000 @@ -12,45 +12,47 @@ #include "lib/core/debug.h" #include "lib/core/misc.h" -int main(int argc, char *argv[]) { - struct timeval stats_before, stats_after, stats_res; - int err = 0, bits; - DSA *dsa = NULL; - RSA *rsa = NULL; - int use_dsa; - - if (argc != 3) { - printf("usage: keygentest <dsa|rsa> <bits>\n"); - exit(-1); - } - - if (!strcmp(argv[1], "dsa")) { - use_dsa = 1; - } else { - use_dsa = 0; - } - - bits= atoi(argv[2]); - HIP_DEBUG("bits=%d\n", bits); - - ERR_load_crypto_strings(); - - gettimeofday(&stats_before, NULL); - - if (use_dsa) - dsa = (DSA *) create_dsa_key(bits); - else - rsa = (RSA *) create_rsa_key(bits); - - HIP_IFEL(!(rsa || dsa), -1, "key creation failed\n") - - gettimeofday(&stats_after, NULL); - - hip_timeval_diff(&stats_after, &stats_before, &stats_res); - HIP_INFO("%s key created in %ld.%06ld secs\n", - (use_dsa ? "dsa" : "rsa"), stats_res.tv_sec, stats_res.tv_usec); - - out_err: - - return err; +int main(int argc, char *argv[]) +{ + struct timeval stats_before, stats_after, stats_res; + int err = 0, bits; + DSA *dsa = NULL; + RSA *rsa = NULL; + int use_dsa; + + if (argc != 3) { + printf("usage: keygentest <dsa|rsa> <bits>\n"); + exit(-1); + } + + if (!strcmp(argv[1], "dsa")) { + use_dsa = 1; + } else { + use_dsa = 0; + } + + bits = atoi(argv[2]); + HIP_DEBUG("bits=%d\n", bits); + + ERR_load_crypto_strings(); + + gettimeofday(&stats_before, NULL); + + if (use_dsa) { + dsa = (DSA *) create_dsa_key(bits); + } else { + rsa = (RSA *) create_rsa_key(bits); + } + + HIP_IFEL(!(rsa || dsa), -1, "key creation failed\n") + + gettimeofday(&stats_after, NULL); + + hip_timeval_diff(&stats_after, &stats_before, &stats_res); + HIP_INFO("%s key created in %ld.%06ld secs\n", + (use_dsa ? "dsa" : "rsa"), stats_res.tv_sec, stats_res.tv_usec); + +out_err: + + return err; } === modified file 'test/listtest.c' --- test/listtest.c 2009-12-11 22:49:11 +0000 +++ test/listtest.c 2010-02-10 23:43:06 +0000 @@ -2,28 +2,31 @@ #include <sys/queue.h> #include <stdio.h> -int main(int argc, char **argv) { - LIST_HEAD(listhead, entry) head; - struct entry { - LIST_ENTRY(entry) entries; /* List. */ - int myval; - } *n1, *n2, *np; - - LIST_INIT(&head); /* Initialize the list. */ - - n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ - LIST_INSERT_HEAD(&head, n1, entries); - - n2 = malloc(sizeof(struct entry)); /* Insert after. */ - LIST_INSERT_AFTER(n1, n2, entries); - - n2 = malloc(sizeof(struct entry)); /* Insert before. */ - LIST_INSERT_AFTER(n1, n2, entries); - /* Forward traversal. */ - for (np = head.lh_first; np != NULL; np = np->entries.le_next) - printf("entry found\n"); - - while (head.lh_first != NULL) /* Delete. */ - LIST_REMOVE(head.lh_first, entries); - return 0; +int main(int argc, char **argv) +{ + LIST_HEAD(listhead, entry) head; + struct entry { + LIST_ENTRY(entry) entries; /* List. */ + int myval; + } *n1, *n2, *np; + + LIST_INIT(&head); /* Initialize the list. */ + + n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ + LIST_INSERT_HEAD(&head, n1, entries); + + n2 = malloc(sizeof(struct entry)); /* Insert after. */ + LIST_INSERT_AFTER(n1, n2, entries); + + n2 = malloc(sizeof(struct entry)); /* Insert before. */ + LIST_INSERT_AFTER(n1, n2, entries); + /* Forward traversal. */ + for (np = head.lh_first; np != NULL; np = np->entries.le_next) { + printf("entry found\n"); + } + + while (head.lh_first != NULL) { /* Delete. */ + LIST_REMOVE(head.lh_first, entries); + } + return 0; } === modified file 'test/misc_install.c' --- test/misc_install.c 2009-12-29 15:10:11 +0000 +++ test/misc_install.c 2010-02-10 23:43:06 +0000 @@ -2,92 +2,91 @@ int init_daemon() { - int err = 0; - /*************************************** - * Initialization of hip daemon: not yet considered - * This has to be fixed in future, according on how to identify - * the user space is compiled in the kernel - ***************************************/ - if ( system("killall hipd") == -1) { - HIP_ERROR("Could not kill hipd"); - }; - /* - * The path has to be decided. We assume that this is run from test/ directory - * in an unstable and initial version. - * Later on this will changed to the only command, without specifying the - * path, because we will insert it into $PATH - */ - err = system("../hipd/hipd -b"); - if (err == -1) - printf("Please run 'make install' in top directory\n"); - return err; + int err = 0; + /*************************************** + * Initialization of hip daemon: not yet considered + * This has to be fixed in future, according on how to identify + * the user space is compiled in the kernel + ***************************************/ + if (system("killall hipd") == -1) { + HIP_ERROR("Could not kill hipd"); + } + ; + /* + * The path has to be decided. We assume that this is run from + * test/directory in an unstable and initial version. + * Later on this will changed to the only command, without specifying the + * path, because we will insert it into $PATH + */ + err = system("../hipd/hipd -b"); + if (err == -1) { + printf("Please run 'make install' in top directory\n"); + } + return err; } int install_module() { - - int err; - err = system("grep -q hipmod /proc/modules"); - if (!err){ - printf("Removing the hipmod module.\n"); - err = system("rmmod hipmod"); - if(err == -1) { - printf("Some error occured while removing the hipmod module\n"); - return(err); - } - } - - printf("The hipmod module is being installed...\n"); - err = system("/sbin/modprobe -v hipmod"); - - return err; + int err; + err = system("grep -q hipmod /proc/modules"); + if (!err) { + printf("Removing the hipmod module.\n"); + err = system("rmmod hipmod"); + if (err == -1) { + printf("Some error occured while removing the hipmod module\n"); + return err; + } + } + + printf("The hipmod module is being installed...\n"); + err = system("/sbin/modprobe -v hipmod"); + + return err; } int add_hi_default(struct hip_common *msg) -{ - /* - $HIPL_DIR/tools/hipconf add hi default - This function is in hipconftool.c and is handle_hi() - */ - char *opts[1]; - int err; - opts[0] = "default"; - printf("Calling handle_hi...\n"); - err = hip_conf_handle_hi(msg, ACTION_ADD, (const char **) opts, 1, 0); - return err; +{ + /* + * $HIPL_DIR/tools/hipconf add hi default + * This function is in hipconftool.c and is handle_hi() + */ + char *opts[1]; + int err; + opts[0] = "default"; + printf("Calling handle_hi...\n"); + err = hip_conf_handle_hi(msg, ACTION_ADD, (const char **) opts, 1, 0); + return err; } int main_install(struct hip_common *msg) { - int err = 0; - if (!getuid()) { - //err = install_module(msg); - HIP_IFEL(install_module(), -1, "Error in installing modules\n"); - /* - if (err) { - HIP_ERROR("error in installing modules\n"); - goto out_err; - } - */ - printf("Initializing the hipd daemon...\n"); - if (init_daemon() == -1) { - err = -1; - goto out_err; - } - sleep(3); - HIP_IFEL(add_hi_default(msg), -1, "Error in add_hi_default\n"); - //add_hi_default(msg); - - } - else { - HIP_ERROR("Installation must be done as root\n"); - err = -1; - goto out_err; - } + int err = 0; + if (!getuid()) { + //err = install_module(msg); + HIP_IFEL(install_module(), -1, "Error in installing modules\n"); + /* + * if (err) { + * HIP_ERROR("error in installing modules\n"); + * goto out_err; + * } + */ + printf("Initializing the hipd daemon...\n"); + if (init_daemon() == -1) { + err = -1; + goto out_err; + } + sleep(3); + HIP_IFEL(add_hi_default(msg), -1, "Error in add_hi_default\n"); + //add_hi_default(msg); + } else { + HIP_ERROR("Installation must be done as root\n"); + err = -1; + goto out_err; + } - /* hipconf new hi does not involve any messages to kernel */ - HIP_IFE((!hip_get_msg_type(msg)), -1); - HIP_IFEL(hip_send_recv_daemon_info(msg, 0, 0), -1, "sending msg failed\n"); + /* hipconf new hi does not involve any messages to kernel */ + HIP_IFE((!hip_get_msg_type(msg)), -1); + HIP_IFEL(hip_send_recv_daemon_info(msg, 0, 0), -1, "sending msg failed\n"); out_err: - return err; + return err; } === modified file 'test/sqliteteststub.c' --- test/sqliteteststub.c 2010-01-19 09:28:42 +0000 +++ test/sqliteteststub.c 2010-02-10 23:43:06 +0000 @@ -19,44 +19,49 @@ #include "lib/core/utils.h" #include "lib/core/sqlitedbapi.h" -static int hip_sqlite_callback(void *NotUsed, int argc, char **argv, char **azColName) { - int i; - for(i=0; i<argc; i++){ - HIP_DEBUG("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); - } - return 0; +static int hip_sqlite_callback(void *NotUsed, + int argc, + char **argv, + char **azColName) +{ + int i; + for (i = 0; i < argc; i++) { + HIP_DEBUG("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); + } + return 0; } -int main(int argc, char *argv[]) { - int err = 0, i = 0; - sqlite3 * db = NULL; - char dbpath[] = "/tmp/hip_sqltest.db"; - char table_sql[] = "CREATE TABLE test (num INTEGER, value VARCHAR(128));"; - char insert_sql[256]; - char delete_sql[] = "DELETE FROM test WHERE num = 3;"; - char select_sql[] = "SELECT * FROM test;"; - - db = hip_sqlite_open_db(dbpath, table_sql); - HIP_IFEL((db == NULL), -1, "Failed to open/create the database\n"); - for(i = 1; i < 10; i++) { - memset(insert_sql, '\0', sizeof(insert_sql)); - sprintf(insert_sql, "INSERT INTO test VALUES(%d, " - "'Hi to you. #%d times said');", i,i); - HIP_IFEL(hip_sqlite_insert_into_table(db, insert_sql), - -1, "Failed to execute insert into query\n"); - } - HIP_IFEL(hip_sqlite_select(db, select_sql, hip_sqlite_callback), -1, - "Failed to execute select query on the db\n"); - HIP_DEBUG("Removing row where num is 3\n"); - HIP_IFEL(hip_sqlite_delete_from_table(db, delete_sql), - -1, "Failed to execute delete query\n"); - HIP_IFEL(hip_sqlite_select(db, select_sql, hip_sqlite_callback), -1, - "Failed to execute select query on the db\n"); - HIP_DEBUG("Did the num 3 disappear?\n"); - HIP_IFEL(hip_sqlite_close_db(db), -1, "Failed to close the db\n"); - - out_err: - return(err); +int main(int argc, char *argv[]) +{ + int err = 0, i = 0; + sqlite3 *db = NULL; + char dbpath[] = "/tmp/hip_sqltest.db"; + char table_sql[] = "CREATE TABLE test (num INTEGER, value VARCHAR(128));"; + char insert_sql[256]; + char delete_sql[] = "DELETE FROM test WHERE num = 3;"; + char select_sql[] = "SELECT * FROM test;"; + + db = hip_sqlite_open_db(dbpath, table_sql); + HIP_IFEL((db == NULL), -1, "Failed to open/create the database\n"); + for (i = 1; i < 10; i++) { + memset(insert_sql, '\0', sizeof(insert_sql)); + sprintf(insert_sql, "INSERT INTO test VALUES(%d, " + "'Hi to you. #%d times said');", i, i); + HIP_IFEL(hip_sqlite_insert_into_table(db, insert_sql), + -1, "Failed to execute insert into query\n"); + } + HIP_IFEL(hip_sqlite_select(db, select_sql, hip_sqlite_callback), -1, + "Failed to execute select query on the db\n"); + HIP_DEBUG("Removing row where num is 3\n"); + HIP_IFEL(hip_sqlite_delete_from_table(db, delete_sql), + -1, "Failed to execute delete query\n"); + HIP_IFEL(hip_sqlite_select(db, select_sql, hip_sqlite_callback), -1, + "Failed to execute select query on the db\n"); + HIP_DEBUG("Did the num 3 disappear?\n"); + HIP_IFEL(hip_sqlite_close_db(db), -1, "Failed to close the db\n"); + +out_err: + return err; } #else @@ -64,10 +69,10 @@ #include <stdio.h> -int main(int argc, char *argv[]) { - printf("You need to configure HIP with agent support\n"); - return -1; +int main(int argc, char *argv[]) +{ + printf("You need to configure HIP with agent support\n"); + return -1; } #endif /* sqlitedbapi.h */ -