From 55df22df3a07e756bd46120172017db8c72dcce6 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sun, 17 Jan 2016 01:24:46 -0300 Subject: [PATCH] DHT send --- confs/BTCD_peers.txt | 1 + iguana/SuperNET.c => deprecated/SuperNET2.c | 0 iguana/SuperNET.h | 2 + iguana/iguana_accept.c | 6 +- iguana/iguana_json.c | 61 +++++++++++++++++++++ iguana/main.c | 28 +++++----- includes/iguana_apideclares.h | 1 + includes/iguana_apidefs.h | 4 ++ includes/iguana_apiundefs.h | 2 + 9 files changed, 88 insertions(+), 17 deletions(-) rename iguana/SuperNET.c => deprecated/SuperNET2.c (100%) diff --git a/confs/BTCD_peers.txt b/confs/BTCD_peers.txt index 39fce24ca..69592171a 100644 --- a/confs/BTCD_peers.txt +++ b/confs/BTCD_peers.txt @@ -1,3 +1,4 @@ +5.9.102.210 89.248.160.237 89.248.160.238 89.248.160.239 diff --git a/iguana/SuperNET.c b/deprecated/SuperNET2.c similarity index 100% rename from iguana/SuperNET.c rename to deprecated/SuperNET2.c diff --git a/iguana/SuperNET.h b/iguana/SuperNET.h index afea890b3..ec0a3057a 100644 --- a/iguana/SuperNET.h +++ b/iguana/SuperNET.h @@ -22,6 +22,7 @@ #define SUPERNET_LBPORT 7770 #define SUPERNET_PUBPORT 7771 +#define SUPERNET_PORTP2P 7770 #define SUPERNET_NETWORKTIMEOUT 10000 #define SUPERNET_POLLTIMEOUT 1 #define SUPERNET_APIUSLEEP (SUPERNET_POLLTIMEOUT * 10000) @@ -104,6 +105,7 @@ char *pangea_parser(struct supernet_info *myinfo,char *method,cJSON *json,char * char *ramchain_parser(struct supernet_info *myinfo,char *method,cJSON *json,char *remoteaddr); char *iguana_parser(struct supernet_info *myinfo,char *method,cJSON *json,char *remoteaddr); char *InstantDEX_parser(struct supernet_info *myinfo,char *method,cJSON *json,char *remoteaddr); +char *SuperNET_DHTsend(struct supernet_info *myinfo,bits256 routehash,void *data,int32_t datalen,int32_t maxdelay); #endif diff --git a/iguana/iguana_accept.c b/iguana/iguana_accept.c index 397e04c7c..887d47f5f 100755 --- a/iguana/iguana_accept.c +++ b/iguana/iguana_accept.c @@ -63,10 +63,10 @@ int32_t iguana_acceptspoll(uint8_t *buf,int32_t bufsize,struct iguana_accept *ac void iguana_acceptloop(void *args) { struct iguana_peer *addr; struct iguana_info *coin = args; - struct pollfd pfd; int32_t sock; struct iguana_accept *ptr; + struct pollfd pfd; int32_t sock; struct iguana_accept *ptr; uint16_t port = coin->chain->portp2p; socklen_t clilen; struct sockaddr_in cli_addr; char ipaddr[64]; uint32_t ipbits; - while ( (coin->bindsock= iguana_socket(1,"0.0.0.0",coin->chain->portp2p)) < 0 ) - sleep(5); + while ( (coin->bindsock= iguana_socket(1,"0.0.0.0",port)) < 0 ) + sleep(5), port = SUPERNET_PORTP2P; printf("iguana_bindloop 127.0.0.1:%d bind sock.%d\n",coin->chain->portp2p,coin->bindsock); while ( coin->bindsock >= 0 ) { diff --git a/iguana/iguana_json.c b/iguana/iguana_json.c index cee9c8e21..651850f5c 100755 --- a/iguana/iguana_json.c +++ b/iguana/iguana_json.c @@ -82,6 +82,8 @@ cJSON *SuperNET_helpjson() #define IGUANA_HELP_S(agent,name,str) array = helpjson(IGUANA_ARGS,#agent,#name,helparray(cJSON_CreateArray(),helpitem(#str,"string"))) #define IGUANA_HELP_SS(agent,name,str,str2) array = helpjson(IGUANA_ARGS,#agent,#name,helparray2(cJSON_CreateArray(),helpitem(#str,"string"),helpitem(#str2,"string"))) #define IGUANA_HELP_SSS(agent,name,str,str2,str3) array = helpjson(IGUANA_ARGS,#agent,#name,helparray3(cJSON_CreateArray(),helpitem(#str,"string"),helpitem(#str2,"string"),helpitem(#str3,"string"))) +#define IGUANA_HELP_SSH(agent,name,str,str2,hash) array = helpjson(IGUANA_ARGS,#agent,#name,helparray3(cJSON_CreateArray(),helpitem(#str,"string"),helpitem(#str2,"string"),helpitem(#hash,"hash"))) +#define IGUANA_HELP_SSHI(agent,name,str,str2,hash,val) array = helpjson(IGUANA_ARGS,#agent,#name,helparray4(cJSON_CreateArray(),helpitem(#str,"string"),helpitem(#str2,"string"),helpitem(#hash,"hash"),helpitem(#val,"int"))) #define IGUANA_HELP_SI(agent,name,str,val) array = helpjson(IGUANA_ARGS,#agent,#name,helparray2(cJSON_CreateArray(),helpitem(#str,"string"),helpitem(#val,"int"))) #define IGUANA_HELP_SII(agent,name,str,val,val2) array = helpjson(IGUANA_ARGS,#agent,#name,helparray3(cJSON_CreateArray(),helpitem(#str,"string"),helpitem(#val,"int"),helpitem(#val2,"int"))) #define IGUANA_HELP_SSI(agent,name,str,str2,val) array = helpjson(IGUANA_ARGS,#agent,#name,helparray3(cJSON_CreateArray(),helpitem(#str,"string"),helpitem(#str2,"string"),helpitem(#val,"int"))) @@ -131,6 +133,8 @@ cJSON *SuperNET_helpjson() #define TWOINTS_AND_ARRAY IGUANA_HELP_IIA #define STRING_AND_THREEINTS IGUANA_HELP_SIII #define TWOSTRINGS_AND_INT IGUANA_HELP_SSI +#define TWOSTRINGS_AND_HASH IGUANA_HELP_SSH +#define TWOSTRINGS_AND_HASH_AND_INT IGUANA_HELP_SSHI #define THREE_INTS IGUANA_HELP_III #include "../includes/iguana_apideclares.h" @@ -379,6 +383,59 @@ void SuperNET_parsepeers(struct supernet_info *myinfo,cJSON *array,int32_t n,int #include "../includes/iguana_apidefs.h" +TWOSTRINGS_AND_HASH_AND_INT(SuperNET,DHT,message,destip,destpub,maxdelay) +{ + bits256 routehash; uint32_t destipbits = 0; long datalen; + char *retstr,*data,*hexstr; cJSON *msgjson; + if ( is_hexstr(message,(int32_t)strlen(message)) <= 0 ) + return(clonestr("{\"error\":\"message must be in hex\"}")); + else hexstr = message; + msgjson = cJSON_CreateObject(); + jaddstr(msgjson,"agent","SuperNET"); + jaddstr(msgjson,"method","DHT"); + if ( destip == 0 || destip[0] == 0 || strncmp(destip,"127.0.0.1",strlen("127.0.0.1")) == 0 ) + { + routehash = destpub; + jaddbits256(msgjson,"destpub",destpub); + } + else + { + destipbits = (uint32_t)calc_ipbits(destip); + vcalc_sha256(0,routehash.bytes,(uint8_t *)&destipbits,sizeof(destipbits)); + jaddstr(msgjson,"destip",destip); + } + jaddstr(msgjson,"message",message); + data = jprint(msgjson,1); + datalen = strlen(data)+1; + retstr = SuperNET_DHTsend(myinfo,routehash,data,(int32_t)datalen,maxdelay); + free(data); + return(retstr); + /*if ( (msgjson= cJSON_Parse(message)) != 0 ) + { + if ( (agent= jstr(msgjson,"agent")) != 0 && strcmp(agent,"SuperNET")) != 0 ) + { + safecopy(agentstr,agent,sizeof(agentstr)-1); + jdelete(msgjson,"agent"); + jaddstr(msgjson,"agent","SuperNET"); + jaddstr(msgjson,"destagent",agentstr); + } + if ( (method= jstr(msgjson,"method")) != 0 && strcmp(agent,"SuperNET")) != 0 ) + { + safecopy(methodstr,method,sizeof(methodstr)-1); + jdelete(msgjson,"method"); + jaddstr(msgjson,"method","DHTsend"); + jaddstr(msgjson,"destmethod",methodstr); + } + msgstr = jprint(msgjson,1); + msglen = (int32_t)strlen(msgstr); + hexstr = calloc(1,msglen*2+1); + flag = 1; + init_hexbytes_noT(hexstr,msgstr,msglen); + } + if ( flag != 0 ) + free(hexstr);*/ +} + ZERO_ARGS(SuperNET,stop) { if ( remoteaddr == 0 || strncmp(remoteaddr,"127.0.0.1",strlen("127.0.0.1")) == 0 ) @@ -667,6 +724,8 @@ char *SuperNET_parser(struct supernet_info *myinfo,char *agent,char *method,cJSO #define IGUANA_DISPATCH_S(agent,name,str) else if ( strcmp(method,#name) == 0 ) return(agent ## _ ## name(IGUANA_ARGS,jstr(json,#str))) #define IGUANA_DISPATCH_SS(agent,name,str,str2) else if ( strcmp(method,#name) == 0 ) return(agent ## _ ## name(IGUANA_ARGS,jstr(json,#str),jstr(json,#str2))) #define IGUANA_DISPATCH_SSS(agent,name,str,str2,str3) else if ( strcmp(method,#name) == 0 ) return(agent ## _ ## name(IGUANA_ARGS,jstr(json,#str),jstr(json,#str2),jstr(json,#str3))) +#define IGUANA_DISPATCH_SSH(agent,name,str,str2,hash) else if ( strcmp(method,#name) == 0 ) return(agent ## _ ## name(IGUANA_ARGS,jstr(json,#str),jstr(json,#str2),jbits256(json,#hash))) +#define IGUANA_DISPATCH_SSHI(agent,name,str,str2,hash,val) else if ( strcmp(method,#name) == 0 ) return(agent ## _ ## name(IGUANA_ARGS,jstr(json,#str),jstr(json,#str2),jbits256(json,#hash),juint(json,#val))) #define IGUANA_DISPATCH_SI(agent,name,str,val) else if ( strcmp(method,#name) == 0 ) return(agent ## _ ## name(IGUANA_ARGS,jstr(json,#str),juint(json,#val))) #define IGUANA_DISPATCH_SII(agent,name,str,val,val2) else if ( strcmp(method,#name) == 0 ) return(agent ## _ ## name(IGUANA_ARGS,jstr(json,#str),juint(json,#val),juint(json,#val2))) #define IGUANA_DISPATCH_SSI(agent,name,str,str2,val) else if ( strcmp(method,#name) == 0 ) return(agent ## _ ## name(IGUANA_ARGS,jstr(json,#str),jstr(json,#str2),juint(json,#val))) @@ -717,6 +776,8 @@ char *SuperNET_parser(struct supernet_info *myinfo,char *agent,char *method,cJSO #define TWOINTS_AND_ARRAY IGUANA_DISPATCH_IIA #define STRING_AND_THREEINTS IGUANA_DISPATCH_SIII #define TWOSTRINGS_AND_INT IGUANA_DISPATCH_SSI +#define TWOSTRINGS_AND_HASH IGUANA_DISPATCH_SSH +#define TWOSTRINGS_AND_HASH_AND_INT IGUANA_DISPATCH_SSHI #define THREE_INTS IGUANA_DISPATCH_III #include "../includes/iguana_apideclares.h" diff --git a/iguana/main.c b/iguana/main.c index 94440860c..3031bd0bf 100644 --- a/iguana/main.c +++ b/iguana/main.c @@ -257,10 +257,12 @@ int32_t iguana_send_supernet(struct iguana_info *coin,struct iguana_peer *addr,v } uint64_t Packetcache[1024]; -int32_t SuperNET_DHTsend(struct supernet_info *myinfo,bits256 routehash,uint8_t *data,int32_t datalen,int32_t maxdelay) +char *SuperNET_DHTsend(struct supernet_info *myinfo,bits256 routehash,void *data,int32_t datalen,int32_t maxdelay) { static int lastpurge; - bits256 packethash; int32_t i,j,firstz,iter,n = 0; struct iguana_peer *addr; + bits256 packethash; char retbuf[512]; int32_t i,j,firstz,iter,n = 0; struct iguana_peer *addr; + if ( myinfo == 0 ) + myinfo = &MYINFO; vcalc_sha256(0,packethash.bytes,data,datalen); firstz = -1; for (i=0; iiphash.bytes,routehash.bytes,sizeof(addr->iphash)) == 0 ) { iguana_send_supernet(Coins[i],addr,data,datalen,maxdelay==0?0:rand()%maxdelay); - return(100); + return(clonestr("{\"result\":\"packet sent directly to destip\"}")); } else if ( iter == 1 ) { @@ -313,7 +315,10 @@ int32_t SuperNET_DHTsend(struct supernet_info *myinfo,bits256 routehash,uint8_t } } } - return(n); + if ( n > 0 ) + sprintf(retbuf,"{\"result\":\"packet forwarded to superDHT\",\"branches\":%d}",n); + else sprintf(retbuf,"{\"error\":\"no nodes to forward packet to\"}"); + return(clonestr(retbuf)); } int32_t SuperNET_mypacket(struct supernet_info *myinfo,uint32_t destipbits,bits256 destpub) @@ -328,8 +333,8 @@ int32_t SuperNET_mypacket(struct supernet_info *myinfo,uint32_t destipbits,bits2 char *SuperNET_p2p(struct iguana_info *coin,struct iguana_peer *addr,int32_t *delaymillisp,char *ipaddr,uint8_t *data,int32_t datalen) { - cJSON *json,*retjson; bits256 destpub,routehash; uint32_t n,destipbits = 0; - char *destip,*agent,*myipaddr,*method,retbuf[512],*retstr = 0; + cJSON *json,*retjson; bits256 destpub,routehash; uint32_t destipbits = 0; + char *destip,*agent,*myipaddr,*method,*retstr = 0; *delaymillisp = 0; if ( (json= cJSON_Parse((char *)data)) != 0 ) { @@ -344,14 +349,9 @@ char *SuperNET_p2p(struct iguana_info *coin,struct iguana_peer *addr,int32_t *de if ( destipbits != 0 ) vcalc_sha256(0,routehash.bytes,(uint8_t *)&destipbits,sizeof(destipbits)); else routehash = destpub; - n = SuperNET_DHTsend(&MYINFO,routehash,data,datalen,juint(json,"delay")); + retstr = SuperNET_DHTsend(&MYINFO,routehash,data,datalen,juint(json,"delay")); free_json(json); - if ( n > 0 ) - { - if ( n == 100 ) - sprintf(retbuf,"{\"result\":\"packet sent to destination\"}"); - else sprintf(retbuf,"{\"result\":\"packet forwarded to superDHT\",\"branches\":%d}",n); - } else return(clonestr("{\"error\":\"no nodes to forward packet to\"}")); + return(retstr); } if ( (agent= jstr(json,"agent")) != 0 && (method= jstr(json,"method")) != 0 ) { diff --git a/includes/iguana_apideclares.h b/includes/iguana_apideclares.h index 8930884bc..d9a9459c3 100755 --- a/includes/iguana_apideclares.h +++ b/includes/iguana_apideclares.h @@ -15,6 +15,7 @@ ZERO_ARGS(SuperNET,help); TWO_STRINGS(SuperNET,html,agentform,htmlfile); +TWOSTRINGS_AND_HASH_AND_INT(SuperNET,DHT,message,destip,destpub,maxdelay); STRING_ARG(SuperNET,bitcoinrpc,setcoin); STRING_ARG(SuperNET,getpeers,activecoin); TWO_ARRAYS(SuperNET,mypeers,supernet,rawpeers); diff --git a/includes/iguana_apidefs.h b/includes/iguana_apidefs.h index 9c75afdd9..b88e6f624 100755 --- a/includes/iguana_apidefs.h +++ b/includes/iguana_apidefs.h @@ -14,6 +14,8 @@ #define IGUANA_CFUNC_IIA(agent,name,val,val2,array) char *agent ## _ ## name(IGUANA_ARGS,int32_t val,int32_t val2,cJSON *array) #define IGUANA_CFUNC_SS(agent,name,str,str2) char *agent ## _ ## name(IGUANA_ARGS,char *str,char *str2) #define IGUANA_CFUNC_SSI(agent,name,str,str2,val) char *agent ## _ ## name(IGUANA_ARGS,char *str,char *str2,int32_t val) +#define IGUANA_CFUNC_SSH(agent,name,str,str2,hash) char *agent ## _ ## name(IGUANA_ARGS,char *str,char *str2,bits256 hash) +#define IGUANA_CFUNC_SSHI(agent,name,str,str2,hash,val) char *agent ## _ ## name(IGUANA_ARGS,char *str,char *str2,bits256 hash,int32_t val) #define IGUANA_CFUNC_SSS(agent,name,str,str2,str3) char *agent ## _ ## name(IGUANA_ARGS,char *str,char *str2,char *str3) #define IGUANA_CFUNC_SI(agent,name,str,val) char *agent ## _ ## name(IGUANA_ARGS,char *str,int32_t val) #define IGUANA_CFUNC_SII(agent,name,str,val,val2) char *agent ## _ ## name(IGUANA_ARGS,char *str,int32_t val,int32_t val2) @@ -49,4 +51,6 @@ #define TWOINTS_AND_ARRAY IGUANA_CFUNC_IIA #define STRING_AND_THREEINTS IGUANA_CFUNC_SIII #define TWOSTRINGS_AND_INT IGUANA_CFUNC_SSI +#define TWOSTRINGS_AND_HASH IGUANA_CFUNC_SSH +#define TWOSTRINGS_AND_HASH_AND_INT IGUANA_CFUNC_SSHI #define THREE_INTS IGUANA_CFUNC_III diff --git a/includes/iguana_apiundefs.h b/includes/iguana_apiundefs.h index f56fa62e8..d07823ba6 100755 --- a/includes/iguana_apiundefs.h +++ b/includes/iguana_apiundefs.h @@ -23,3 +23,5 @@ #undef STRING_AND_THREEINTS #undef TWOSTRINGS_AND_INT #undef THREE_INTS +#undef TWOSTRINGS_AND_HASH +#undef TWOSTRINGS_AND_HASH_AND_INT