From 2a470ee8568990ea7276eb347c1bfe1fcd5174e3 Mon Sep 17 00:00:00 2001 From: xethyrion Date: Tue, 21 Feb 2017 20:55:28 +0200 Subject: [PATCH 1/5] Fixed encapsulation Now working properly. --- multiaddr.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/multiaddr.c b/multiaddr.c index ce89a3d..d566144 100644 --- a/multiaddr.c +++ b/multiaddr.c @@ -141,15 +141,34 @@ int multiaddress_encapsulate(struct MultiAddress* result, char* string) free(result->bytes); result->bytes = NULL; result->bsize = 0; + char * exstr; + if(string[0] == '/') + { + exstr = (char *) malloc(strlen(result->string)+1); + } + else + { + exstr = (char *) malloc(strlen(result->string)); + } + strcpy(exstr, result->string); free(result->string); // insert the new values - result->string = malloc(strlen(string) + 1); + result->string = malloc(strlen(string) + strlen(exstr) + 1); if (result->string == NULL) { multiaddress_free(result); return 0; } - strcpy(result->string, string); - if(string_to_bytes(&result->bytes, &result->bsize, result->string, sizeof(result->string)) == 0) + strcpy(result->string, exstr); + free(exstr); + if(string[0] == '/') + { + strcat(result->string, string+1); + } + else + { + strcat(result->string, string); + } + if(string_to_bytes(&result->bytes, &result->bsize, result->string, strlen(result->string)+1) == 0) { multiaddress_free(result); return 0; @@ -209,4 +228,3 @@ int multiaddress_decapsulate(struct MultiAddress * result, char * srci) return 0; } } - From 43a19d7c616c479390cc2828256ec3c6763b24fb Mon Sep 17 00:00:00 2001 From: xethyrion Date: Tue, 21 Feb 2017 21:02:42 +0200 Subject: [PATCH 2/5] Updated to current version --- README.md | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 4063617..e2ddacc 100644 --- a/README.md +++ b/README.md @@ -5,32 +5,30 @@ multiaddr for IPFS in C. # Usage: #### All you need to include is multiaddr.h ## Maddr struct: -* char string[]; // String that contains addresses such as /ip4/192.168.1.1/ +* char * string; // String that contains addresses such as /ip4/192.168.1.1/ * uint8_t bytes; //String that contains the enecoded address -* int bsize[]; //int[1] that contains the real bytes size (Use it whenever using the bytes so you don't input trash!) +* size_t bsize; //int[1] that contains the real bytes size (Use it whenever using the bytes so you don't input trash!) -## New Multi Address From String(new_maddr_fs) - char addrstr[] = "/ip4/192.168.1.1/" - struct maddr a; - a=new_maddr_fs(addrstr); +## New Multi Address From String(multiaddress_new_from_string) +struct MultiAddress* a = multiaddress_new_from_string("/ip4/127.0.0.1/tcp/8080/"); ## Obtaining the byte buffer(.bytes, .bsize[0]): - printf("TEST BYTES: %s\n",Var_To_Hex(a.bsize[0], a.bytes)); + printf("TEST BYTES: %s\n",Var_To_Hex(a->bsize, a->bytes)); Var_To_Hex = Byte Buffer to Hex String Hex_To_Var = Hex String to Byte Buffer ## Encapsulation & Decapsulation(m_encapsulate, m_decapsulate) #### Remember, Decapsulation happens from right to left, never in reverse, if you have /ip4/udp/ipfs/ if you decapsulate "udp" you will also take out ipfs! * Now the string is: /ip4/192.168.1.1/ -* m_encapsulate(&a,"/udp/3333/"); //Adds udp/3333/ to char addrstr +* multiaddress_encapsulate(a,"/udp/3333/"); //Adds udp/3333/ to char addrstr * Now the string is: /ip4/192.168.1.1/udp/3333/ -* m_decapsulate(&a,"udp"); //Removes udp protocol and its address +* multiaddress_decapsulate(a,"udp"); //Removes udp protocol and its address * Now the string is: /ip4/192.168.1.1/ -* m_encapsulate(&a,"/tcp/8080"); +* multiaddress_encapsulate(a,"/tcp/8080"); * Now the string is: /ip4/192.168.1.1/tcp/8080/ # Constructing a multiaddress from bytes: -* struct maddr beta; -* beta=new_maddr_fb(a.bytes,a.bsize[0]); //This will already construct back to the string too! -* printf("B STRING: %s\n",beta.string); //So after encapsulation and decapsulation atm this string would +* struct MultiAddress* beta; +* beta = multiaddress_new_from_bytes(a->bytes,a->bsize); //This will already construct back to the string too! +* printf("B STRING: %s\n",beta->string); //So after encapsulation and decapsulation atm this string would * contain: /ip4/192.168.1.1/tcp/8080/ From 10302dd4b1d9e27cd7f2822a4f7c8f80c9a16946 Mon Sep 17 00:00:00 2001 From: xethyrion Date: Tue, 21 Feb 2017 21:04:06 +0200 Subject: [PATCH 3/5] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e2ddacc..d38ea91 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,8 @@ multiaddr for IPFS in C. #### All you need to include is multiaddr.h ## Maddr struct: * char * string; // String that contains addresses such as /ip4/192.168.1.1/ -* uint8_t bytes; //String that contains the enecoded address -* size_t bsize; //int[1] that contains the real bytes size (Use it whenever using the bytes so you don't input trash!) +* uint8_t * bytes; // uint8_t * that contains the enecoded address +* size_t bsize; //size_t that contains the real bytes size (Use it whenever using the bytes so you don't input trash!) ## New Multi Address From String(multiaddress_new_from_string) struct MultiAddress* a = multiaddress_new_from_string("/ip4/127.0.0.1/tcp/8080/"); @@ -18,7 +18,7 @@ struct MultiAddress* a = multiaddress_new_from_string("/ip4/127.0.0.1/tcp/8080/" ## Encapsulation & Decapsulation(m_encapsulate, m_decapsulate) #### Remember, Decapsulation happens from right to left, never in reverse, if you have /ip4/udp/ipfs/ if you decapsulate "udp" you will also take out ipfs! * Now the string is: /ip4/192.168.1.1/ -* multiaddress_encapsulate(a,"/udp/3333/"); //Adds udp/3333/ to char addrstr +* multiaddress_encapsulate(a,"/udp/3333/"); //Adds udp/3333/ * Now the string is: /ip4/192.168.1.1/udp/3333/ * multiaddress_decapsulate(a,"udp"); //Removes udp protocol and its address * Now the string is: /ip4/192.168.1.1/ From 2dab592164a5769478eca1a8cc806eb88de35024 Mon Sep 17 00:00:00 2001 From: xethyrion Date: Tue, 21 Feb 2017 21:05:20 +0200 Subject: [PATCH 4/5] How it should be used --- testing.c | 127 +++++++++++++++++++++++++----------------------------- 1 file changed, 59 insertions(+), 68 deletions(-) diff --git a/testing.c b/testing.c index 7c7bd48..dbdd725 100644 --- a/testing.c +++ b/testing.c @@ -1,74 +1,65 @@ -#include +#pragma once -#include "test_multiaddr.h" +#include "multiaddr/multiaddr.h" -const char* names[] = { - "test_new_from_string", - "test_full", - "test_hex_to_var", - "test_int_to_hex" -}; - -int (*funcs[])(void) = { - test_new_from_string, - test_full, - test_hex_to_var, - test_int_to_hex -}; - -int testit(const char* name, int (*func)(void)) { - printf("Testing %s...\n", name); - int retVal = func(); - if (retVal) - printf("%s success!\n", name); - else - printf("** Uh oh! %s failed.**\n", name); - return retVal; -} - -int main(int argc, char** argv) { - int counter = 0; - int tests_ran = 0; - char* test_wanted; - int only_one = 0; - if(argc > 1) { - only_one = 1; - if (argv[1][0] == '\'') { // some shells put quotes around arguments - argv[1][strlen(argv[1])-1] = 0; - test_wanted = &(argv[1][1]); - } - else - test_wanted = argv[1]; - } - int array_length = sizeof(funcs) / sizeof(funcs[0]); - int array2_length = sizeof(names) / sizeof(names[0]); - if (array_length != array2_length) { - printf("Test arrays are not of the same length. Funcs: %d, Names: %d\n", array_length, array2_length); - } - for (int i = 0; i < array_length; i++) { - if (only_one) { - const char* currName = names[i]; - if (strcmp(currName, test_wanted) == 0) { - tests_ran++; - counter += testit(names[i], funcs[i]); - } - } - else - if (!only_one) { - tests_ran++; - counter += testit(names[i], funcs[i]); - } - } - - if (tests_ran == 0) - printf("***** No tests found *****\n"); - else { - if (tests_ran - counter > 0) { - printf("***** There were %d failed test(s) (%d successful) *****\n", tests_ran - counter, counter); - } else { - printf("All %d tests passed\n", tests_ran); - } +int test_new_from_string() { + struct MultiAddress* a = multiaddress_new_from_string("/ip4/127.0.0.1/tcp/8080/"); + printf("Number of Bytes: %lu, Bytes: ", a->bsize); + for(int i = 0; i < a->bsize; i++) { + printf("%02x ", a->bytes[i]); } + printf(" End of bytes\n"); + multiaddress_free(a); return 1; } +int test_full() { + char addrstr[100]; + strcpy(addrstr,"/ip4/192.168.1.1/tcp/8080/"); + printf("INITIAL: %s\n",addrstr); + struct MultiAddress* a; + a= multiaddress_new_from_string(addrstr); + printf("TEST BYTES: %s\n",Var_To_Hex(a->bsize, a->bytes)); + + //Remember, Decapsulation happens from right to left, never in reverse! + + printf("A STRING:%s\n",a->string); + multiaddress_encapsulate(a,"/udp/3333/"); + printf("A STRING ENCAPSULATED:%s\n",a->string); + + multiaddress_decapsulate(a,"udp"); + printf("A STRING DECAPSULATED UDP:%s\n",a->string); + + multiaddress_encapsulate(a,"/tcp/8080"); + printf("A STRING ENCAPSULATED TCP:%s\n",a->string); + + struct MultiAddress* beta; + beta = multiaddress_new_from_bytes(a->bytes,a->bsize); + printf("B STRING: %s\n",beta->string); + + multiaddress_free(a); + multiaddress_free(beta); + return 1; +} + +int test_hex_to_var() { + size_t d; + unsigned char* result = Hex_To_Var("04", &d); + if (d != 1) + return 0; + if (result[0] != 4) + return 0; + + if (result != NULL) + free(result); + return 1; +} + +int test_int_to_hex() { + int val = 2555351; + char* result = Int_To_Hex(val); + int retVal = Hex_To_Int(result); + if (retVal != val) + return 0; + return 1; +} From 62749975609d962f989bb3d6e56ef9361b241951 Mon Sep 17 00:00:00 2001 From: xethyrion Date: Wed, 22 Feb 2017 07:17:09 +0200 Subject: [PATCH 5/5] Fixed everything.. as far as I can tell.. --- multiaddr.c | 11 ++++ protoutils.c | 20 ++++---- test_multiaddr.h | 16 +++--- testing.c | 127 +++++++++++++++++++++++++---------------------- 4 files changed, 100 insertions(+), 74 deletions(-) diff --git a/multiaddr.c b/multiaddr.c index d566144..f2d5558 100644 --- a/multiaddr.c +++ b/multiaddr.c @@ -215,6 +215,16 @@ int multiaddress_decapsulate(struct MultiAddress * result, char * srci) procstr[i] = '/'; } } + //Bytes update + if (result->bytes != NULL) + free(result->bytes); + result->bytes = NULL; + result->bsize = 0; + if(string_to_bytes(&result->bytes, &result->bsize, result->string, strlen(result->string)+1) == 0) + { + multiaddress_free(result); + return 0; + } return 1; } else @@ -228,3 +238,4 @@ int multiaddress_decapsulate(struct MultiAddress * result, char * srci) return 0; } } + diff --git a/protoutils.c b/protoutils.c index 33c65dc..52f1751 100644 --- a/protoutils.c +++ b/protoutils.c @@ -314,15 +314,18 @@ int bytes_to_string(char** buffer, const uint8_t* in_bytes, int in_bytes_size) int lastpos = 0; char pid[3]; //Process Hex String + //printf("FULL HEX: %s", hex); NAX: + //printf("REDO!!!!!\n"); //Stage 1 ID: if(lastpos!=0) { - lastpos++; + //lastpos++; } pid[0] = hex[lastpos]; pid[1] = hex[lastpos+1]; pid[2] = '\0'; + //printf("pid: %s\n",pid); if(proto_with_deccode(head, Hex_To_Int(pid))) { //////////Stage 2: Address @@ -347,7 +350,7 @@ int bytes_to_string(char** buffer, const uint8_t* in_bytes, int in_bytes_size) //printf("Protocol: %s\n", PID->name); //printf("Address : %s\n", address); lastpos= lastpos+(PID->size/4); - //printf("lastpos: %d",lastpos); + //printf("lastpos: %d\n",lastpos); //////////Address: //Keeping Valgrind happy @@ -416,7 +419,7 @@ int bytes_to_string(char** buffer, const uint8_t* in_bytes, int in_bytes_size) unsigned char * pointyaddr = NULL; pointyaddr = rezultat; int returnstatus = 0; - returnstatus = multiaddr_encoding_base58_encode(addrbuf, sizeof(addrbuf), &pointyaddr, &rezbuflen); + returnstatus = multiaddr_encoding_base58_encode(addrbuf, sizeof(IPFS_ADDR)/2, &pointyaddr, &rezbuflen); free(addrbuf); if(returnstatus == 0) { @@ -566,16 +569,15 @@ char * address_string_to_bytes(struct Protocol * xx, const char * abc,size_t get } case 42://IPFS - !!! { - - char * x_data = NULL; - x_data = (char*)abc; + x_data = (char*) abc; size_t x_data_length = strlen(x_data); - size_t result_buffer_length = multiaddr_encoding_base58_decode_size((unsigned char*)x_data); + size_t result_buffer_length = multiaddr_encoding_base58_decode_max_size((unsigned char*)x_data); unsigned char result_buffer[result_buffer_length]; unsigned char* ptr_to_result = result_buffer; memset(result_buffer, 0, result_buffer_length); // now get the decoded address + int return_value = multiaddr_encoding_base58_decode(x_data, x_data_length, &ptr_to_result, &result_buffer_length); if (return_value == 0) { @@ -594,7 +596,7 @@ char * address_string_to_bytes(struct Protocol * xx, const char * abc,size_t get unsigned char c = ptr_to_result[i]; char miu[3]; bzero(miu, 3); - miu[3] = '\0'; + miu[2] = '\0'; sprintf(miu,"%02x", c); strcat(ADDR_ENCODED, miu); @@ -602,7 +604,7 @@ char * address_string_to_bytes(struct Protocol * xx, const char * abc,size_t get ilen = strlen(ADDR_ENCODED); char prefixed[3]; strcpy(prefixed,Num_To_HexVar_32(ilen)); - prefixed[3] = '\0'; + prefixed[2] = '\0'; strcat(returning_result, prefixed); strcat(returning_result, ADDR_ENCODED); //printf("ADDRESS: %s\nSIZEADDR: %d\n",ADDR_ENCODED,ilen); diff --git a/test_multiaddr.h b/test_multiaddr.h index 3afea04..c17e70e 100644 --- a/test_multiaddr.h +++ b/test_multiaddr.h @@ -15,7 +15,7 @@ int test_new_from_string() { int test_full() { char addrstr[100]; - strcpy(addrstr,"/ip4/192.168.1.1/tcp/8080/"); + strcpy(addrstr,"/ip4/192.168.1.1/"); printf("INITIAL: %s\n",addrstr); struct MultiAddress* a; a= multiaddress_new_from_string(addrstr); @@ -24,14 +24,18 @@ int test_full() { //Remember, Decapsulation happens from right to left, never in reverse! printf("A STRING:%s\n",a->string); - multiaddress_encapsulate(a,"/ip4/192.131.200.111/udp/3333/"); + multiaddress_encapsulate(a,"/udp/3333/"); printf("A STRING ENCAPSULATED:%s\n",a->string); - + printf("TEST BYTES: %s\n",Var_To_Hex(a->bsize, a->bytes)); multiaddress_decapsulate(a,"udp"); printf("A STRING DECAPSULATED UDP:%s\n",a->string); - - multiaddress_encapsulate(a,"/tcp/8080"); - printf("A STRING ENCAPSULATED TCP:%s\n",a->string); + printf("TEST BYTES: %s\n",Var_To_Hex(a->bsize, a->bytes)); + multiaddress_encapsulate(a,"/udp/3333/"); + printf("A STRING ENCAPSULATED UDP: %s\n",a->string); + multiaddress_encapsulate(a,"/ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG"); + printf("A STRING ENCAPSULATED IPFS:%s\n",a->string); + printf("TEST BYTES: %s\n",Var_To_Hex(a->bsize, a->bytes)); + printf("TEST BYTE SIZE: %u\n",a->bsize); struct MultiAddress* beta; beta = multiaddress_new_from_bytes(a->bytes,a->bsize); diff --git a/testing.c b/testing.c index dbdd725..7c7bd48 100644 --- a/testing.c +++ b/testing.c @@ -1,65 +1,74 @@ -#pragma once +#include -#include "multiaddr/multiaddr.h" +#include "test_multiaddr.h" -int test_new_from_string() { - struct MultiAddress* a = multiaddress_new_from_string("/ip4/127.0.0.1/tcp/8080/"); - printf("Number of Bytes: %lu, Bytes: ", a->bsize); - for(int i = 0; i < a->bsize; i++) { - printf("%02x ", a->bytes[i]); +const char* names[] = { + "test_new_from_string", + "test_full", + "test_hex_to_var", + "test_int_to_hex" +}; + +int (*funcs[])(void) = { + test_new_from_string, + test_full, + test_hex_to_var, + test_int_to_hex +}; + +int testit(const char* name, int (*func)(void)) { + printf("Testing %s...\n", name); + int retVal = func(); + if (retVal) + printf("%s success!\n", name); + else + printf("** Uh oh! %s failed.**\n", name); + return retVal; +} + +int main(int argc, char** argv) { + int counter = 0; + int tests_ran = 0; + char* test_wanted; + int only_one = 0; + if(argc > 1) { + only_one = 1; + if (argv[1][0] == '\'') { // some shells put quotes around arguments + argv[1][strlen(argv[1])-1] = 0; + test_wanted = &(argv[1][1]); + } + else + test_wanted = argv[1]; + } + int array_length = sizeof(funcs) / sizeof(funcs[0]); + int array2_length = sizeof(names) / sizeof(names[0]); + if (array_length != array2_length) { + printf("Test arrays are not of the same length. Funcs: %d, Names: %d\n", array_length, array2_length); + } + for (int i = 0; i < array_length; i++) { + if (only_one) { + const char* currName = names[i]; + if (strcmp(currName, test_wanted) == 0) { + tests_ran++; + counter += testit(names[i], funcs[i]); + } + } + else + if (!only_one) { + tests_ran++; + counter += testit(names[i], funcs[i]); + } + } + + if (tests_ran == 0) + printf("***** No tests found *****\n"); + else { + if (tests_ran - counter > 0) { + printf("***** There were %d failed test(s) (%d successful) *****\n", tests_ran - counter, counter); + } else { + printf("All %d tests passed\n", tests_ran); + } } - printf(" End of bytes\n"); - multiaddress_free(a); return 1; } -int test_full() { - char addrstr[100]; - strcpy(addrstr,"/ip4/192.168.1.1/tcp/8080/"); - printf("INITIAL: %s\n",addrstr); - struct MultiAddress* a; - a= multiaddress_new_from_string(addrstr); - printf("TEST BYTES: %s\n",Var_To_Hex(a->bsize, a->bytes)); - - //Remember, Decapsulation happens from right to left, never in reverse! - - printf("A STRING:%s\n",a->string); - multiaddress_encapsulate(a,"/udp/3333/"); - printf("A STRING ENCAPSULATED:%s\n",a->string); - - multiaddress_decapsulate(a,"udp"); - printf("A STRING DECAPSULATED UDP:%s\n",a->string); - - multiaddress_encapsulate(a,"/tcp/8080"); - printf("A STRING ENCAPSULATED TCP:%s\n",a->string); - - struct MultiAddress* beta; - beta = multiaddress_new_from_bytes(a->bytes,a->bsize); - printf("B STRING: %s\n",beta->string); - - multiaddress_free(a); - multiaddress_free(beta); - return 1; -} - -int test_hex_to_var() { - size_t d; - unsigned char* result = Hex_To_Var("04", &d); - if (d != 1) - return 0; - if (result[0] != 4) - return 0; - - if (result != NULL) - free(result); - return 1; -} - -int test_int_to_hex() { - int val = 2555351; - char* result = Int_To_Hex(val); - int retVal = Hex_To_Int(result); - if (retVal != val) - return 0; - return 1; -}