diff --git a/iguana/SuperNET.c b/iguana/SuperNET.c index 9aa80ff7f..6b625fca9 100644 --- a/iguana/SuperNET.c +++ b/iguana/SuperNET.c @@ -214,21 +214,40 @@ int32_t SuperNET_copybits(int32_t reverse,uint8_t *dest,uint8_t *src,int32_t len return(len >> 3); } -uint16_t SuperNET_checkc(bits256 privkey,bits256 otherpub,uint64_t tag) +uint16_t SuperNET_checkc(bits256 privkey,bits256 otherpub,uint32_t x) { uint8_t buf[40]; bits256 check,seed,seed2; seed = curve25519_shared(privkey,otherpub); vcalc_sha256(0,seed2.bytes,seed.bytes,sizeof(seed)); memcpy(buf,seed2.bytes,sizeof(seed)); - iguana_rwnum(1,&buf[sizeof(seed)],sizeof(tag),&tag); + iguana_rwnum(1,&buf[sizeof(seed)],sizeof(x),&x); vcalc_sha256(0,check.bytes,buf,sizeof(buf)); return(check.ushorts[0]); } +int32_t pathstr2bits(uint8_t *path,char *pathstr,uint32_t ipbits) +{ + int32_t pathlen = 0; + if ( pathstr != 0 ) + { + pathlen = ((int32_t)strlen(pathstr) >> 1) / sizeof(uint32_t); + if ( pathlen < SUPERNET_MAXHOPS-1 ) + { + decode_hex(path,(int32_t)(pathlen*sizeof(uint32_t)*2),pathstr); + if ( ipbits != 0 ) + iguana_rwnum(1,&path[pathlen*sizeof(uint32_t)],sizeof(ipbits),&ipbits), pathlen++; + return(pathlen); + } + } + return(0); +} + int32_t SuperNET_json2bits(char *myipaddr,bits256 privkey,bits256 mypubkey,uint8_t *serialized,int32_t maxsize,char *destip,cJSON *json,bits256 destpub,int16_t _othervalid) { - uint16_t apinum,checkc; uint32_t tmp,ipbits,crc; char *agent,*method; uint64_t tag; - char *hexmsg; uint8_t broadcastflag; int8_t othervalid; int32_t n,len = sizeof(uint32_t); + uint16_t apinum,checkc; uint32_t tmp,myipbits,ipbits,crc; char *agent,*method,*hexmsg; + uint8_t broadcastflag; int8_t othervalid; int32_t n,len; + uint8_t pathlen,path[0x100 * sizeof(uint32_t)]; + len = sizeof(crc); if ( _othervalid > 100 ) othervalid = 100; else if ( _othervalid < -100 ) @@ -242,8 +261,12 @@ int32_t SuperNET_json2bits(char *myipaddr,bits256 privkey,bits256 mypubkey,uint8 broadcastflag |= 0x40; if ( juint(json,"plaintext") != 0 ) broadcastflag |= 0x80; - if ( (tag= j64bits(json,"tag")) == 0 ) - OS_randombytes((uint8_t *)&tag,sizeof(tag)); + myipbits = (uint32_t)calc_ipbits(myipaddr); + if ( jstr(json,"path") != 0 ) + pathlen = pathstr2bits(path,jstr(json,"path"),myipbits); + else pathlen = pathstr2bits(path,0,myipbits); + //if ( (tag= j64bits(json,"tag")) == 0 ) + // OS_randombytes((uint8_t *)&tag,sizeof(tag)); agent = jstr(json,"agent"), method = jstr(json,"method"); if ( agent != 0 && method != 0 && strcmp(agent,"SuperNET") == 0 && strcmp(method,"json2bits") == 0 ) { @@ -255,19 +278,28 @@ int32_t SuperNET_json2bits(char *myipaddr,bits256 privkey,bits256 mypubkey,uint8 printf("agent.(%s) method.(%s) is not found\n",agent,method); return(-1); } - checkc = SuperNET_checkc(privkey,destpub,tag); ipbits = (uint32_t)calc_ipbits(destip); + checkc = SuperNET_checkc(privkey,destpub,ipbits); len += iguana_rwnum(1,&serialized[len],sizeof(uint32_t),&ipbits); - ipbits = (uint32_t)calc_ipbits(myipaddr); - len += iguana_rwnum(1,&serialized[len],sizeof(uint32_t),&ipbits); + len += iguana_rwnum(1,&serialized[len],sizeof(uint32_t),&myipbits); len += iguana_rwnum(1,&serialized[len],sizeof(checkc),&checkc); len += iguana_rwnum(1,&serialized[len],sizeof(apinum),&apinum); - len += iguana_rwnum(1,&serialized[len],sizeof(tag),&tag); + //len += iguana_rwnum(1,&serialized[len],sizeof(tag),&tag); len += iguana_rwbignum(1,&serialized[len],sizeof(mypubkey),mypubkey.bytes); len += iguana_rwnum(1,&serialized[len],sizeof(othervalid),&othervalid); len += iguana_rwnum(1,&serialized[len],sizeof(broadcastflag),&broadcastflag); if ( (broadcastflag & 0x40) != 0 ) len += iguana_rwbignum(1,&serialized[len],sizeof(bits256),destpub.bytes); + if ( pathlen > 0 && pathlen < SUPERNET_MAXHOPS ) + { + len += iguana_rwnum(1,&serialized[len],sizeof(pathlen),&pathlen); + memcpy(&serialized[len],path,pathlen*sizeof(uint32_t)), len += pathlen*sizeof(uint32_t); + } + else + { + pathlen = 0; + len += iguana_rwnum(1,&serialized[len],sizeof(pathlen),&pathlen); + } if ( (hexmsg= jstr(json,"hexmsg")) != 0 ) { n = (int32_t)strlen(hexmsg); @@ -289,21 +321,29 @@ int32_t SuperNET_json2bits(char *myipaddr,bits256 privkey,bits256 mypubkey,uint8 cJSON *SuperNET_bits2json(struct iguana_peer *addr,uint8_t *serialized,int32_t datalen) { - char destip[64],method[64],checkstr[5],agent[64],myipaddr[64],str[65],*hexmsg; uint64_t tag; + char destip[64],method[64],checkstr[5],agent[64],myipaddr[64],str[65],*hexmsg; uint16_t apinum,checkc; int8_t othervalid; uint32_t destipbits,myipbits; bits256 destpub,senderpub; cJSON *json = cJSON_CreateObject(); int32_t len = 0; uint32_t crc; uint8_t broadcastflag,plaintext; + uint8_t pathlen,path[0x100 * sizeof(uint32_t)]; char pathstr[0x100 * sizeof(uint32_t) * 2 + 1]; len += iguana_rwnum(0,&serialized[len],sizeof(uint32_t),&crc); len += iguana_rwnum(0,&serialized[len],sizeof(uint32_t),&destipbits); len += iguana_rwnum(0,&serialized[len],sizeof(uint32_t),&myipbits); len += iguana_rwnum(0,&serialized[len],sizeof(checkc),&checkc); len += iguana_rwnum(0,&serialized[len],sizeof(apinum),&apinum); - len += iguana_rwnum(0,&serialized[len],sizeof(tag),&tag); + //len += iguana_rwnum(0,&serialized[len],sizeof(tag),&tag); len += iguana_rwbignum(0,&serialized[len],sizeof(bits256),senderpub.bytes); len += iguana_rwnum(0,&serialized[len],sizeof(othervalid),&othervalid); len += iguana_rwnum(0,&serialized[len],sizeof(broadcastflag),&broadcastflag); if ( (broadcastflag & 0x40) != 0 ) len += iguana_rwbignum(0,&serialized[len],sizeof(bits256),destpub.bytes); + len += iguana_rwnum(0,&serialized[len],sizeof(pathlen),&pathlen); + if ( pathlen > 0 && pathlen < SUPERNET_MAXHOPS ) + { + memcpy(path,&serialized[len],pathlen*sizeof(uint32_t)); + init_hexbytes_noT(pathstr,path,pathlen*sizeof(uint32_t)); + len += pathlen*sizeof(uint32_t); + } else pathstr[0] = 0; plaintext = (broadcastflag & 0x80) != 0; broadcastflag &= 0x3f; if ( broadcastflag > SUPERNET_MAXHOPS ) @@ -320,7 +360,9 @@ cJSON *SuperNET_bits2json(struct iguana_peer *addr,uint8_t *serialized,int32_t d jaddbits256(json,"destpub",GENESIS_PUBKEY); else if ( (broadcastflag & 0x40) != 0 ) jaddbits256(json,"destpub",destpub); - jadd64bits(json,"tag",tag); + if ( pathstr[0] != 0 ) + jaddstr(json,"path",pathstr); + //jadd64bits(json,"tag",tag); init_hexbytes_noT(checkstr,(void *)&checkc,sizeof(checkc)); jaddstr(json,"check",checkstr); jaddnum(json,"ov",othervalid); @@ -330,10 +372,10 @@ cJSON *SuperNET_bits2json(struct iguana_peer *addr,uint8_t *serialized,int32_t d jaddnum(json,"broadcast",broadcastflag); if ( len < datalen ) { - printf("len %d vs %d datalen\n",len,datalen); + //printf("len %d vs %d datalen\n",len,datalen); hexmsg = malloc(((datalen - len)<<1) + 1); init_hexbytes_noT(hexmsg,&serialized[len],datalen - len); - printf("hex.(%s)\n",hexmsg); + //printf("hex.(%s)\n",hexmsg); jaddstr(json,"hexmsg",hexmsg); free(hexmsg); } @@ -382,7 +424,7 @@ int32_t iguana_send_supernet(struct iguana_info *coin,struct iguana_peer *addr,c iguana_setkeys(myinfo,addr,&privkey,&pubkey,&destpub,&nextprivkey,&nextpubkey,&nextdestpub); if ( juint(json,"plaintext") == 0 && juint(json,"broadcast") == 0 && memcmp(destpub.bytes,GENESIS_PUBKEY.bytes,sizeof(pubkey)) == 0 ) { - printf("reject broadcasting non-plaintext! (%s)\n",jsonstr); getchar(); + printf("reject broadcasting non-plaintext! (%s)\n",jsonstr); //getchar(); free_json(json); return(-1); } @@ -499,8 +541,9 @@ char *SuperNET_DHTencode(struct supernet_info *myinfo,char *destip,bits256 destp destipbits = (uint32_t)calc_ipbits(destip); vcalc_sha256(0,routehash.bytes,(uint8_t *)&destipbits,sizeof(destipbits)); } - retstr = SuperNET_DHTsend(myinfo,routehash,hexmsg,maxdelay,broadcastflag,plaintext); - return(retstr); + if ( (retstr = SuperNET_DHTsend(myinfo,routehash,hexmsg,maxdelay,broadcastflag,plaintext)) != 0 ) + free(retstr); + return(clonestr("{\"result\":\"DHT sent\"}")); } char *SuperNET_forward(struct supernet_info *myinfo,char *hexmsg,uint32_t destipbits,bits256 destpub,int32_t maxdelay,int32_t broadcastflag,int32_t plaintext) @@ -545,9 +588,14 @@ int32_t SuperNET_destination(struct supernet_info *myinfo,uint32_t *destipbitsp, char *SuperNET_JSON(struct supernet_info *myinfo,cJSON *json,char *remoteaddr) { int32_t destflag,maxdelay,flag=0; bits256 destpub; uint32_t destipbits; cJSON *retjson; - char *forwardstr=0,*retstr=0,*agent=0,*method=0,*message,*hexmsg,*jsonstr=0; + char *forwardstr=0,*retstr=0,*agent=0,*method=0,*message,*hexmsg,*jsonstr=0; uint64_t tag; if ( remoteaddr != 0 && strcmp(remoteaddr,"127.0.0.1") == 0 ) remoteaddr = 0; + if ( (tag= j64bits(json,"tag")) == 0 ) + { + OS_randombytes((uint8_t *)&tag,sizeof(tag)); + jadd64bits(json,"tag",tag); + } //printf("SuperNET_JSON.(%s) remote.(%s)\n",jprint(json,0),remoteaddr!=0?remoteaddr:""); destflag = SuperNET_destination(myinfo,&destipbits,&destpub,&maxdelay,json,remoteaddr); //printf("destflag.%d\n",destflag); @@ -562,10 +610,10 @@ char *SuperNET_JSON(struct supernet_info *myinfo,cJSON *json,char *remoteaddr) } if ( hexmsg != 0 ) { - printf("check.(%s)\n",hexmsg); + //printf("check.(%s)\n",hexmsg); if ( SuperNET_hexmsguniq(myinfo,destpub,hexmsg,0) >= 0 ) { - SuperNET_hexmsgadd(myinfo,destpub,hexmsg); + SuperNET_hexmsgadd(myinfo,destpub,hexmsg,tai_now()); forwardstr = SuperNET_forward(myinfo,hexmsg,destipbits,destpub,maxdelay,juint(json,"broadcast"),juint(json,"plaintext")!=0); } if ( flag != 0 ) @@ -637,7 +685,7 @@ char *SuperNET_p2p(struct iguana_info *coin,struct iguana_peer *addr,int32_t *de if ( (checkstr= jstr(json,"check")) != 0 ) { decode_hex((uint8_t *)&othercheckc,sizeof(othercheckc),checkstr); - checkc = SuperNET_checkc(myinfo->privkey,senderpub,j64bits(json,"tag")); + checkc = SuperNET_checkc(myinfo->privkey,senderpub,juint(json,"myip")); if ( checkc == othercheckc ) addr->validpub++; else if ( addr->validpub > 0 ) @@ -674,7 +722,7 @@ char *SuperNET_p2p(struct iguana_info *coin,struct iguana_peer *addr,int32_t *de { if ( jstr(retjson,"error") != 0 || (jstr(retjson,"result") != 0 && jstr(retjson,"method") == 0) ) { - printf("filter.(%s) no need to send back\n",retstr); + //printf("filter.(%s) no need to send back\n",retstr); free(retstr); retstr = 0; } diff --git a/iguana/SuperNET.h b/iguana/SuperNET.h index 3d91fe857..c2bd54aa2 100644 --- a/iguana/SuperNET.h +++ b/iguana/SuperNET.h @@ -126,7 +126,7 @@ int32_t SuperNET_decrypt(bits256 *senderpubp,uint64_t *senderbitsp,uint32_t *tim int32_t SuperNET_str2hex(uint8_t *hex,char *str); void SuperNET_hex2str(char *str,uint8_t *hex,int32_t len); -void SuperNET_hexmsgadd(struct supernet_info *myinfo,bits256 destpub,char *hexmsg); +void SuperNET_hexmsgadd(struct supernet_info *myinfo,bits256 destpub,char *hexmsg,struct tai now); int32_t SuperNET_hexmsguniq(struct supernet_info *myinfo,bits256 dest,char *hexmsg,int32_t addflag); #endif diff --git a/iguana/SuperNET_hexmsg.c b/iguana/SuperNET_hexmsg.c index fb258bd7d..6ed1c014c 100644 --- a/iguana/SuperNET_hexmsg.c +++ b/iguana/SuperNET_hexmsg.c @@ -55,7 +55,7 @@ int32_t SuperNET_hexmsguniq(struct supernet_info *myinfo,bits256 dest,char *hexm return(i); } -void SuperNET_hexmsgadd(struct supernet_info *myinfo,bits256 destpub,char *hexmsg) +void SuperNET_hexmsgadd(struct supernet_info *myinfo,bits256 destpub,char *hexmsg,struct tai now) { char str[65]; if ( memcmp(destpub.bytes,GENESIS_PUBKEY.bytes,sizeof(destpub)) == 0 )