You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

315 lines
13 KiB

9 years ago
/******************************************************************************
* 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. *
* *
******************************************************************************/
// included from basilisk.c
8 years ago
#ifdef ENABLE_VIRTPING
8 years ago
int32_t basilisk_blocksend(struct supernet_info *myinfo,struct iguana_info *notary,struct iguana_info *virt,struct iguana_peer *addr,int32_t height)
9 years ago
{
9 years ago
int32_t blocklen; bits256 hash2; uint8_t *data = 0; char str[65],strbuf[4096],*blockstr,*allocptr = 0; struct iguana_block *block;
9 years ago
hash2 = iguana_blockhash(virt,height);
if ( (block= iguana_blockfind("bsend",virt,hash2)) != 0 )
{
if ( block->height != height )
{
printf("basilisk_blocksend: height.%d mismatch %d\n",block->height,height);
return(-1);
}
else if ( block->queued != 0 && block->req != 0 )
{
memcpy(&blocklen,block->req,sizeof(blocklen));
data = (uint8_t *)(void *)((long)block->req + sizeof(blocklen));
}
}
if ( data == 0 )
{
if ( (blocklen= iguana_peerblockrequest(virt,virt->blockspace,IGUANA_MAXPACKETSIZE,0,hash2,0)) > 0 )
data = &virt->blockspace[sizeof(struct iguana_msghdr)];
}
if ( data != 0 )
{
blockstr = basilisk_addhexstr(&allocptr,0,strbuf,sizeof(strbuf),data,blocklen);
printf("RELAYID.%d send block.%d %s -> (%s) %s\n",myinfo->RELAYID,height,blockstr,addr->ipaddr,bits256_str(str,hash2));
8 years ago
basilisk_blocksubmit(myinfo,notary,virt,addr,blockstr,hash2,height);
9 years ago
if ( allocptr != 0 )
free(allocptr);
return(0);
} else printf("blocklen.%d for hwm.%d height.%d %s\n",blocklen,virt->blocks.hwmchain.height,height,bits256_str(str,hash2));
return(-1);
}
8 years ago
int32_t basilisk_ping_processvirts(struct supernet_info *myinfo,struct iguana_info *notary,struct iguana_peer *addr,uint8_t *data,int32_t datalen)
9 years ago
{
int32_t diff,i,j,len = 0; struct iguana_info *virt; char symbol[7]; uint32_t numvirts,height;
len += iguana_rwvarint32(0,&data[len],&numvirts);
symbol[6] = 0;
for (i=0; i<numvirts; i++)
{
memcpy(symbol,&data[len],6), len += 6;
len += iguana_rwvarint32(0,&data[len],&height);
9 years ago
//printf("(%s %d).%p ",symbol,height,addr);
8 years ago
if ( NUMRELAYS > 0 && addr != 0 && (virt= iguana_coinfind(symbol)) != 0 )
9 years ago
{
if ( height > virt->longestchain )
virt->longestchain = height;
8 years ago
if ( NUMRELAYS > 0 && virt->blocks.hwmchain.height > height )
9 years ago
{
8 years ago
diff = ((height % NUMRELAYS) - myinfo->RELAYID);
9 years ago
diff *= diff;
diff++;
if ( (rand() % diff) == 0 )
{
for (j=1; height+j<virt->blocks.hwmchain.height && j<3; j++)
8 years ago
basilisk_blocksend(myinfo,notary,virt,addr,height+j);
9 years ago
}
}
}
}
return(len);
}
int32_t basilisk_ping_genvirts(struct supernet_info *myinfo,uint8_t *data,int32_t maxlen)
{
struct iguana_info *virt,*tmpcoin; int32_t iter,datalen = 0; uint32_t n;
for (iter=n=0; iter<2; iter++)
{
HASH_ITER(hh,myinfo->allcoins,virt,tmpcoin)
{
if ( virt != 0 && virt->virtualchain != 0 )
{
if ( iter == 0 )
n++;
else
{
memcpy(&data[datalen],virt->symbol,6), datalen += 6;
datalen += iguana_rwvarint32(1,&data[datalen],(uint32_t *)&virt->blocks.hwmchain.height);
}
}
}
if ( iter == 0 )
datalen += iguana_rwvarint32(1,&data[datalen],&n);
}
return(datalen);
}
8 years ago
#endif
8 years ago
int32_t basilisk_ping_processMSG(struct supernet_info *myinfo,uint32_t senderipbits,uint8_t *data,int32_t datalen)
8 years ago
{
8 years ago
int32_t i,msglen,len=0; uint8_t num,keylen,*message,*key; uint32_t duration;
8 years ago
if ( (num= data[len++]) > 0 )
{
8 years ago
//printf("processMSG num.%d datalen.%d\n",num,datalen);
8 years ago
for (i=0; i<num; i++)
{
keylen = data[len++];
8 years ago
if ( keylen != BASILISK_KEYSIZE )
8 years ago
{
8 years ago
printf("invalid keylen.%d != %d\n",keylen,BASILISK_KEYSIZE);
8 years ago
return(0);
}
key = &data[len], len += keylen;
if ( len+sizeof(msglen) > datalen )
{
printf("processMSG overflow len.%d msglen.%d %d > %d\n",len,msglen,(int32_t)(len+sizeof(msglen)),datalen);
return(0);
}
len += iguana_rwnum(0,&data[len],sizeof(msglen),&msglen);
len += iguana_rwnum(0,&data[len],sizeof(duration),&duration);
8 years ago
message = &data[len], len += msglen;
8 years ago
if ( msglen <= 0 || len > datalen )
{
printf("illegal msglen.%d or len.%d > %d\n",msglen,len,datalen);
return(0);
}
8 years ago
//printf("i.%d: keylen.%d msglen.%d\n",i,keylen,msglen);
8 years ago
basilisk_respond_addmessage(myinfo,key,keylen,message,msglen,0,duration);
8 years ago
}
}
return(len);
}
int32_t basilisk_ping_genMSG(struct supernet_info *myinfo,uint8_t *data,int32_t maxlen)
{
struct basilisk_message *msg; int32_t datalen = 0;
if ( maxlen > sizeof(msg->key) && (msg= queue_dequeue(&myinfo->msgQ,0)) != 0 ) // oneshot ping
{
data[datalen++] = 1;
data[datalen++] = msg->keylen;
memcpy(&data[datalen],msg->key,msg->keylen), datalen += msg->keylen;
datalen += iguana_rwnum(1,&data[datalen],sizeof(msg->datalen),&msg->datalen);
datalen += iguana_rwnum(1,&data[datalen],sizeof(msg->duration),&msg->duration);
if ( maxlen > datalen+msg->datalen )
{
8 years ago
//printf("SEND keylen.%d msglen.%d\n",msg->keylen,msg->datalen);
8 years ago
memcpy(&data[datalen],msg->data,msg->datalen), datalen += msg->datalen;
}
else
{
printf("basilisk_ping_genMSG message doesnt fit %d vs %d\n",maxlen,datalen+msg->datalen);
datalen = 0;
}
//printf("\n-> ");
//int32_t i;
//for (i=0; i<datalen; i++)
// printf("%02x",data[i]);
//printf(" <- genMSG\n");
} else data[datalen++] = 0;
return(datalen);
}
9 years ago
int32_t basilisk_ping_genrelay(struct supernet_info *myinfo,uint8_t *data,int32_t maxlen,struct basilisk_relay *rp)
{
int32_t datalen = 0;
datalen = iguana_rwnum(1,&data[datalen],sizeof(rp->ipbits),&rp->ipbits);
data[datalen++] = rp->direct.pingdelay;
return(datalen);
}
8 years ago
int32_t baslisk_relay_report(struct supernet_info *myinfo,uint8_t *data,int32_t maxlen,struct basilisk_relaystatus *reported,uint8_t pingdelay)
{
if ( reported != 0 )
{
reported->pingdelay = pingdelay;
}
return(0);
}
9 years ago
int32_t basilisk_ping_processrelay(struct supernet_info *myinfo,uint8_t *data,int32_t maxlen,struct basilisk_relay *rp,int32_t i)
{
8 years ago
uint8_t pingdelay; int32_t j,datalen = 0; uint32_t ipbits; char ipaddr[64];
9 years ago
ipbits = rp->ipbits;
if ( maxlen < sizeof(ipbits)+1 )
{
printf("unping error maxlen.%d is too small\n",maxlen);
return(-1);
}
datalen = iguana_rwnum(1,&data[datalen],sizeof(ipbits),&ipbits);
pingdelay = data[datalen++];
if ( (j= basilisk_relayid(myinfo,ipbits)) >= 0 )
{
datalen += baslisk_relay_report(myinfo,&data[datalen],maxlen-datalen,&rp->reported[j],pingdelay);
return(datalen);
}
datalen += baslisk_relay_report(myinfo,&data[datalen],maxlen-datalen,0,pingdelay);
8 years ago
expand_ipbits(ipaddr,ipbits);
printf("notified about unknown relay (%s)\n",ipaddr); // parse it to match bytes sent
basilisk_addrelay_info(myinfo,0,ipbits,GENESIS_PUBKEY);
9 years ago
return(datalen);
}
void basilisk_ping_process(struct supernet_info *myinfo,struct iguana_peer *addr,uint32_t senderipbits,uint8_t *data,int32_t datalen)
{
8 years ago
int32_t diff,i,n,len = 0; struct iguana_info *notary; char ipbuf[64]; struct basilisk_relay *rp; uint8_t numrelays; uint16_t sn; uint32_t now = (uint32_t)time(NULL);
9 years ago
expand_ipbits(ipbuf,senderipbits);
8 years ago
notary = iguana_coinfind("NOTARY");
8 years ago
for (i=0; i<myinfo->NOTARY.NUMRELAYS; i++)
9 years ago
{
8 years ago
rp = &myinfo->NOTARY.RELAYS[i];
9 years ago
rp->direct.pingdelay = 0;
if ( rp->ipbits == senderipbits )
rp->lastping = now;
if ( rp->lastping == now )
rp->direct.pingdelay = 1;
else
{
diff = (now - rp->lastping);
if ( diff < 0xff )
rp->direct.pingdelay = diff;
}
}
numrelays = data[len++];
8 years ago
//len += basilisk_ping_processvirts(myinfo,notary,addr,&data[len],datalen - len);
9 years ago
for (i=0; i<numrelays; i++)
{
8 years ago
rp = &myinfo->NOTARY.RELAYS[i];
9 years ago
if ( len > datalen )
break;
if ( (n= basilisk_ping_processrelay(myinfo,&data[len],datalen-len,rp,i)) < 0 )
break;
len += n;
}
if ( len <= datalen-sizeof(sn) )
9 years ago
{
8 years ago
//len += basilisk_ping_processDEX(myinfo,senderipbits,&data[len],datalen-len);
8 years ago
len += basilisk_ping_processMSG(myinfo,senderipbits,&data[len],datalen-len);
9 years ago
}
8 years ago
//printf("PING got %d, processed.%d from (%s)\n",datalen,len,ipbuf);
9 years ago
//else printf("\n");
8 years ago
//for (i=0; i<datalen; i++)
// printf("%02x",data[i]);
8 years ago
printf("<<<<<<<<<<< input ping from.(%s) rel.%d numrelays.%d datalen.%d relay.%d Q.%d\n",ipbuf,basilisk_relayid(myinfo,(uint32_t)calc_ipbits(ipbuf)),numrelays,datalen,myinfo->NOTARY.RELAYID,QUEUEITEMS);
8 years ago
basilisk_addrelay_info(myinfo,0,(uint32_t)calc_ipbits(ipbuf),GENESIS_PUBKEY);
9 years ago
}
int32_t basilisk_ping_gen(struct supernet_info *myinfo,uint8_t *data,int32_t maxlen)
{
int32_t i,datalen = 0;
8 years ago
data[datalen++] = myinfo->NOTARY.NUMRELAYS;
8 years ago
//datalen += basilisk_ping_genvirts(myinfo,&data[datalen],maxlen - datalen);
8 years ago
for (i=0; i<myinfo->NOTARY.NUMRELAYS; i++)
datalen += basilisk_ping_genrelay(myinfo,&data[datalen],maxlen - datalen,&myinfo->NOTARY.RELAYS[i]);
8 years ago
//datalen += basilisk_ping_genDEX(myinfo,&data[datalen],maxlen - datalen);
9 years ago
datalen += basilisk_ping_genMSG(myinfo,&data[datalen],maxlen - datalen);
8 years ago
for (i=0; i<datalen; i++)
printf("%02x",data[i]);
printf("output ping datalen.%d relay.%d >>>>>>>>>> Q.%d\n",datalen,myinfo->NOTARY.RELAYID,QUEUEITEMS);
9 years ago
return(datalen);
}
8 years ago
// encapsulate other messages inside msgQ for onetime ping
// filter out duplicates
8 years ago
void basilisk_ping_send(struct supernet_info *myinfo,struct iguana_info *notary)
9 years ago
{
8 years ago
struct iguana_peer *addr; char ipaddr[64]; struct basilisk_relay *rp; uint32_t r; int32_t i,j,incr,datalen=0; uint64_t alreadysent;
8 years ago
if ( notary == 0 || myinfo->NOTARY.NUMRELAYS <= 0 || myinfo->IAMNOTARY == 0 )
8 years ago
{
printf("skip ping send %p %d %d\n",notary,myinfo->NOTARY.NUMRELAYS,myinfo->IAMNOTARY);
8 years ago
return;
8 years ago
}
9 years ago
if ( myinfo->pingbuf == 0 )
myinfo->pingbuf = malloc(IGUANA_MAXPACKETSIZE);
datalen = basilisk_ping_gen(myinfo,&myinfo->pingbuf[sizeof(struct iguana_msghdr)],IGUANA_MAXPACKETSIZE-sizeof(struct iguana_msghdr));
8 years ago
incr = sqrt(myinfo->NOTARY.NUMRELAYS) + 1;
8 years ago
for (alreadysent=j=0; j<=incr; j++)
9 years ago
{
8 years ago
OS_randombytes((void *)&r,sizeof(r));
8 years ago
i = (j == 0) ? myinfo->NOTARY.RELAYID : (r % myinfo->NOTARY.NUMRELAYS);
if ( j != 0 && i == myinfo->NOTARY.RELAYID )
i = (myinfo->NOTARY.RELAYID + 1) % myinfo->NOTARY.NUMRELAYS;
8 years ago
if ( (((uint64_t)1 << i) & alreadysent) != 0 )
{
j--;
continue;
}
alreadysent |= ((uint64_t)1 << i);
8 years ago
rp = &myinfo->NOTARY.RELAYS[i];
9 years ago
addr = 0;
8 years ago
expand_ipbits(ipaddr,rp->ipbits);
9 years ago
if ( rp->ipbits == myinfo->myaddr.myipbits )
basilisk_ping_process(myinfo,0,myinfo->myaddr.myipbits,&myinfo->pingbuf[sizeof(struct iguana_msghdr)],datalen);
8 years ago
else if ( (addr= iguana_peerfindipbits(notary,rp->ipbits,1)) != 0 && addr->usock >= 0 )
9 years ago
{
if ( iguana_queue_send(addr,0,myinfo->pingbuf,"SuperNETPIN",datalen) <= 0 )
printf("error sending %d to (%s)\n",datalen,addr->ipaddr);
8 years ago
else if ( datalen > 200 )
8 years ago
fprintf(stderr,"+(%s).%d ",ipaddr,i);
8 years ago
} //else fprintf(stderr,"-(%s).%d ",ipaddr,i);
9 years ago
}
8 years ago
if ( datalen > 200 )
printf("my RELAYID.%d of %d\n",myinfo->NOTARY.RELAYID,myinfo->NOTARY.NUMRELAYS);
9 years ago
}