diff --git a/iguana/SuperNET.c b/iguana/SuperNET.c index ffcf89b8b..8d668475b 100644 --- a/iguana/SuperNET.c +++ b/iguana/SuperNET.c @@ -334,7 +334,7 @@ cJSON *SuperNET_bits2json(struct iguana_peer *addr,uint8_t *serialized,int32_t d hexmsg = malloc(((datalen - len)<<1) + 1); init_hexbytes_noT(hexmsg,&serialized[len],datalen - len); printf("hex.(%s)\n",hexmsg); - jaddstr(json,"message",hexmsg); + jaddstr(json,"hexmsg",hexmsg); free(hexmsg); } //printf("bits2json.(%s)\n",jprint(json,0)); @@ -382,7 +382,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); + printf("reject broadcasting non-plaintext! (%s)\n",jsonstr); getchar(); free_json(json); return(-1); } @@ -452,44 +452,21 @@ struct iguana_peer *iguana_peerfind(struct supernet_info *myinfo,struct iguana_i char *SuperNET_DHTsend(struct supernet_info *myinfo,bits256 routehash,char *hexmsg,int32_t maxdelay,int32_t broadcastflag,int32_t plaintext) { - static int lastpurge; static uint64_t Packetcache[1024]; - bits256 packethash; int32_t i,j,datalen,firstz; char *jsonstr=0; + int32_t i,j; char *jsonstr=0; struct iguana_peer *addr; cJSON *json; struct iguana_info *coin; if ( myinfo == 0 ) return(clonestr("{\"error\":\"no supernet_info\"}")); - datalen = (int32_t)strlen(hexmsg) + 1; json = cJSON_CreateObject(); jaddstr(json,"agent","SuperNET"); jaddstr(json,"method","DHT"); - jaddstr(json,"message",hexmsg); + jaddstr(json,"hexmsg",hexmsg); if ( broadcastflag > 0 ) jaddnum(json,"broadcast",broadcastflag-1); if ( plaintext != 0 ) jaddnum(json,"plaintext",plaintext!=0); jsonstr = jprint(json,1); - vcalc_sha256(0,packethash.bytes,(void *)hexmsg,datalen); - firstz = -1; - for (i=broadcastflag!=0; i= sizeof(Packetcache)/sizeof(*Packetcache) ) - lastpurge = 0; - } + if ( SuperNET_hexmsguniq(myinfo,routehash,hexmsg,1) < 0 ) + return(clonestr("{\"error\":\"duplicate packet rejected\"}")); if ( broadcastflag != 0 ) { for (i=0; i= 0 ) + { + SuperNET_hexmsgadd(myinfo,destpub,hexmsg); + forwardstr = SuperNET_forward(myinfo,hexmsg,destipbits,destpub,maxdelay,juint(json,"broadcast"),juint(json,"plaintext")!=0); + } + if ( flag != 0 ) + free(hexmsg); } - forwardstr = SuperNET_forward(myinfo,message,destipbits,destpub,maxdelay,juint(json,"broadcast"),juint(json,"plaintext")!=0); } if ( (destflag & SUPERNET_ISMINE) && (agent= jstr(json,"agent")) != 0 && (method= jstr(json,"method")) != 0 ) { @@ -611,9 +599,9 @@ char *SuperNET_JSON(struct supernet_info *myinfo,cJSON *json,char *remoteaddr) char *SuperNET_p2p(struct iguana_info *coin,struct iguana_peer *addr,int32_t *delaymillisp,char *ipaddr,uint8_t *data,int32_t datalen,int32_t compressed) { - struct supernet_info *myinfo; cJSON *json; char *myipaddr,*method,*retstr,*checkstr; void *ptr=0; + struct supernet_info *myinfo;char *myipaddr,*method,*retstr,*checkstr; void *ptr=0; bits256 senderpub,privkey,pubkey,nextprivkey,nextpubkey,nextdestpub; uint16_t checkc,othercheckc; - int32_t offset,maxdelay,msglen = datalen; uint8_t space[8192],*msgbits = 0; + cJSON *json,*retjson; int32_t offset,maxdelay,msglen = datalen; uint8_t space[8192],*msgbits = 0; myinfo = SuperNET_MYINFO(0); retstr = 0; *delaymillisp = 0; @@ -657,7 +645,8 @@ char *SuperNET_p2p(struct iguana_info *coin,struct iguana_peer *addr,int32_t *de //printf("validpub.%d: %x vs %x shared.%llx\n",addr->validpub,checkc,othercheckc,(long long)addr->sharedseed.txid); } maxdelay = juint(json,"maxdelay"); - printf("GOT >>>>>>>> SUPERNET P2P.(%s) from.%s %s valid.%d:%d\n",jprint(json,0),coin->symbol,addr->ipaddr,addr->validpub,addr->othervalid); + if ( jstr(json,"method") != 0 && strcmp(jstr(json,"method"),"getpeers") != 0 ) + printf("GOT >>>>>>>> SUPERNET P2P.(%s) from.%s %s valid.%d:%d\n",jprint(json,0),coin->symbol,addr->ipaddr,addr->validpub,addr->othervalid); if ( (myipaddr= jstr(json,"yourip")) != 0 ) SuperNET_myipaddr(SuperNET_MYINFO(0),coin,addr,myipaddr,ipaddr); jaddstr(json,"fromp2p",coin->symbol); @@ -680,7 +669,17 @@ char *SuperNET_p2p(struct iguana_info *coin,struct iguana_peer *addr,int32_t *de addr->othervalid = (int32_t)jdouble(json,"ov"); addr->pubkey = senderpub; free_json(json); - } + if ( (retjson= cJSON_Parse(retstr)) != 0 ) + { + if ( jstr(retjson,"error") != 0 || (jstr(retjson,"result") != 0 && jstr(retjson,"method") == 0) ) + { + printf("filter.(%s) no need to send back\n",retstr); + free(retstr); + retstr = 0; + } + free_json(retjson); + } + } if ( ptr != 0 ) free(ptr); return(retstr); diff --git a/iguana/SuperNET.h b/iguana/SuperNET.h index e838e1a89..3d91fe857 100644 --- a/iguana/SuperNET.h +++ b/iguana/SuperNET.h @@ -126,6 +126,8 @@ 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); +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 new file mode 100644 index 000000000..83f9a1bfd --- /dev/null +++ b/iguana/SuperNET_hexmsg.c @@ -0,0 +1,62 @@ +/****************************************************************************** + * Copyright © 2014-2016 The SuperNET Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * SuperNET software, including this file may be copied, modified, propagated * + * or distributed except according to the terms contained in the LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include "iguana777.h" + +int32_t SuperNET_hexmsguniq(struct supernet_info *myinfo,bits256 dest,char *hexmsg,int32_t addflag) +{ + static int lastpurge; static uint64_t Packetcache[1024]; + bits256 packethash; int32_t i,datalen; + datalen = (int32_t)strlen(hexmsg) + 1; + vcalc_sha256(0,packethash.bytes,(void *)hexmsg,datalen); + packethash = curve25519(dest,packethash); + for (i=0; i slot[%d]\n",(long long)packethash.txid,hexmsg,i); + } + break; + } + else if ( Packetcache[i] == packethash.txid ) + { + printf("SuperNET_DHTsend reject duplicate packet.%llx (%s)\n",(long long)packethash.txid,hexmsg); + return(-1); + } + } + if ( i == sizeof(Packetcache)/sizeof(*Packetcache) ) + { + if ( addflag != 0 ) + { + printf("purge slot[%d]\n",lastpurge); + Packetcache[lastpurge++] = packethash.txid; + if ( lastpurge >= sizeof(Packetcache)/sizeof(*Packetcache) ) + lastpurge = 0; + } + } + return(i); +} + +void SuperNET_hexmsgadd(struct supernet_info *myinfo,bits256 destpub,char *hexmsg) +{ + char str[65]; + if ( memcmp(destpub.bytes,GENESIS_PUBKEY.bytes,sizeof(destpub)) == 0 ) + strcpy(str,"BROADCAST"); + else bits256_str(str,destpub); + printf("HEXMSG.(%s) -> %s\n",hexmsg,str); +} diff --git a/iguana/iguana.sources b/iguana/iguana.sources index 5b4456aa2..ec9a7a6b2 100755 --- a/iguana/iguana.sources +++ b/iguana/iguana.sources @@ -1,3 +1,3 @@ #iguana_html.c -SOURCES := SuperNET.c pangea_api.c pangea_funds.c cards777.c pangea_fsm.c pangea_network.c pangea_init.c poker.c ramchain_api.c iguana_tx.c iguana_wallet.c iguana_pubkeys.c iguana_recv.c iguana_bundles.c iguana_msg.c iguana_rpc.c iguana777.c iguana_chains.c iguana_peers.c iguana_accept.c iguana_bitmap.c iguana_init.c iguana_ramchain.c iguana_blocks.c iguana_json.c main.c +SOURCES := SuperNET.c SuperNET_hexmsg.c pangea_api.c pangea_funds.c cards777.c pangea_fsm.c pangea_network.c pangea_init.c poker.c ramchain_api.c iguana_tx.c iguana_wallet.c iguana_pubkeys.c iguana_recv.c iguana_bundles.c iguana_msg.c iguana_rpc.c iguana777.c iguana_chains.c iguana_peers.c iguana_accept.c iguana_bitmap.c iguana_init.c iguana_ramchain.c iguana_blocks.c iguana_json.c main.c