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.

1429 lines
55 KiB

9 years ago
/******************************************************************************
9 years ago
* Copyright © 2014-2016 The SuperNET Developers. *
9 years ago
* *
* 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"
#define _iguana_hashfind(coin,ipbits) _iguana_hashset(coin,ipbits,-1)
9 years ago
struct iguana_iAddr *iguana_iAddrhashfind(struct iguana_info *coin,uint64_t ipbits,int32_t createflag);
9 years ago
9 years ago
int32_t iguana_validatehdr(char *symbol,struct iguana_msghdr *H)
{
9 years ago
int32_t i = 0,valid=0,len = -1; char cmdstr[16];
memcpy(cmdstr,H->command,sizeof(H->command));
9 years ago
cmdstr[0] = toupper((int32_t)cmdstr[0]);
9 years ago
cmdstr[5] = toupper((int32_t)cmdstr[5]);
9 years ago
cmdstr[6] = toupper((int32_t)cmdstr[6]);
cmdstr[7] = toupper((int32_t)cmdstr[7]);
9 years ago
if ( strcmp(symbol,"VPN") == 0 || strncmp("SuperNET",cmdstr,strlen("SuperNET")) == 0 )
9 years ago
valid = 1;
else
9 years ago
{
for (i=0; Iguana_validcommands[i]!=0&&Iguana_validcommands[i][0]!=0; i++)
if ( strcmp(H->command,Iguana_validcommands[i]) == 0 )
9 years ago
{
valid = 1;
9 years ago
break;
9 years ago
}
9 years ago
}
9 years ago
if ( valid != 0 )
9 years ago
{
iguana_rwnum(0,H->serdatalen,sizeof(H->serdatalen),(uint32_t *)&len);
if ( len > IGUANA_MAXPACKETSIZE )
return(-1);
9 years ago
} else return(-1);
9 years ago
return(len);
}
9 years ago
struct iguana_peer *iguana_peeralive(struct iguana_peer *addr,int32_t needalive)
{
if ( needalive == 0 || (addr->usock >= 0 && addr->dead == 0) )
return(addr);
else return(0);
}
struct iguana_peer *iguana_peerfindipaddr(struct iguana_info *coin,char *ipaddr,int32_t needalive)
{
int32_t i;
for (i=0; i<IGUANA_MAXPEERS; i++)
if ( strcmp(ipaddr,coin->peers->active[i].ipaddr) == 0 )
return(iguana_peeralive(&coin->peers->active[i],needalive));
return(0);
}
struct iguana_peer *iguana_peerfindipbits(struct iguana_info *coin,uint32_t ipbits,int32_t needalive)
{
int32_t i;
for (i=0; i<IGUANA_MAXPEERS; i++)
if ( ipbits == coin->peers->active[i].ipbits )
return(iguana_peeralive(&coin->peers->active[i],needalive));
return(0);
}
9 years ago
struct iguana_iAddr *_iguana_hashset(struct iguana_info *coin,uint32_t ipbits,int32_t itemind)
9 years ago
{
struct iguana_iAddr *ptr = 0; int32_t allocsize; char str[65]; struct OS_memspace *mem = 0;
9 years ago
if ( coin->virtualchain != 0 )
return(0);
9 years ago
expand_ipbits(str,ipbits);
HASH_FIND(hh,coin->iAddrs,&ipbits,sizeof(ipbits),ptr);
//printf("%p hashset.(%s) -> ptr.%p itemind.%d keylen.%ld %x\n",coin->iAddrs,str,ptr,itemind,sizeof(ipbits),ipbits);
if ( itemind >= 0 )
{
if ( ptr == 0 )
{
allocsize = (int32_t)(sizeof(*ptr));
if ( mem != 0 )
ptr = iguana_memalloc(mem,allocsize,1);
else ptr = mycalloc('t',1,allocsize);
if ( ptr == 0 )
8 years ago
{
printf("fatal alloc errorA in hashset\n");
iguana_exit(0,0);
8 years ago
}
9 years ago
//printf("ptr.%p allocsize.%d key.%p keylen.%d itemind.%d\n",ptr,allocsize,key,keylen,itemind);
ptr->hh.itemind = itemind;
ptr->ipbits = ipbits;
HASH_ADD(hh,coin->iAddrs,ipbits,sizeof(ipbits),ptr);
{
struct iguana_iAddr *tmp;
HASH_FIND(hh,coin->iAddrs,&ipbits,sizeof(ipbits),tmp);
if ( tmp != ptr )
printf("%s itemind.%d search error %p != %p\n",str,itemind,ptr,tmp);
//else printf("%p added.(%s) ind.%d:%d %p tmp.%p %x\n",coin->iAddrs,str,itemind,ptr->hh.itemind,ptr,tmp,ipbits);
}
}
else ptr->hh.itemind = itemind;
}
return(ptr);
}
struct iguana_iAddr *iguana_iAddrhashset(struct iguana_info *coin,struct iguana_iAddr *iA,int32_t ind)
{
struct iguana_iAddr *tmp,*item;
9 years ago
if ( coin->virtualchain != 0 )
return(0);
9 years ago
if ( iA == 0 || iA->ipbits == 0 )
{
9 years ago
printf("null iA.%p or ipbits.%llx ind.%d status.%d\n",iA,iA!=0?(long long)iA->ipbits:0,iA!=0?iA->hh.itemind:0,iA!=0?iA->status:0);
iguana_exit(0,0);
9 years ago
return(0);
}
portable_mutex_lock(&coin->peers_mutex);
9 years ago
if ( (item= _iguana_hashfind(coin,(uint32_t)iA->ipbits)) == 0 )
9 years ago
{
8 years ago
tmp = mycalloc('p',1,sizeof(*iA));
9 years ago
*tmp = *iA;
iA = tmp;
if ( ind <= 0 )
ind = coin->numiAddrs + 1;
9 years ago
printf("coin->iAddrs.%p call set.(%x) ind.%d\n",coin->iAddrs,(uint32_t)iA->ipbits,ind);
9 years ago
if ( (item= _iguana_hashset(coin,(uint32_t)iA->ipbits,ind)) != 0 && item->hh.itemind == coin->numiAddrs+1 )
9 years ago
{
*item = *iA;
iA = item;
coin->numiAddrs++;
} else printf("iguana_hashset error numiAddrs.%d ind.%d\n",coin->numiAddrs,iA->hh.itemind);
}
else
{
*item = *iA;
iA = item;
iA->hh.itemind = ind;
}
portable_mutex_unlock(&coin->peers_mutex);
//printf("return iA.%p ind.%d %x\n",iA,iA->hh.itemind,iA->ipbits);
return(iA);
}
9 years ago
struct iguana_iAddr *iguana_iAddrhashfind(struct iguana_info *coin,uint64_t ipbits,int32_t createflag)
9 years ago
{
int32_t ind; struct iguana_iAddr *item = 0;
9 years ago
if ( coin->virtualchain != 0 )
return(0);
9 years ago
portable_mutex_lock(&coin->peers_mutex);
if ( ipbits != 0 )
{
9 years ago
if ( (item= _iguana_hashfind(coin,(uint32_t)ipbits)) == 0 && createflag != 0 )
9 years ago
{
ind = coin->numiAddrs + 1;
9 years ago
_iguana_hashset(coin,(uint32_t)ipbits,ind);
if ( (item= _iguana_hashfind(coin,(uint32_t)ipbits)) != 0 )
9 years ago
coin->numiAddrs++;
}
}
portable_mutex_unlock(&coin->peers_mutex);
return(item);
}
uint32_t iguana_rwiAddrind(struct iguana_info *coin,int32_t rwflag,struct iguana_iAddr *iA,uint32_t ind)
{
9 years ago
FILE *fp; char fname[512],hexstr[65],ipaddr[64]; uint32_t ipbits; int32_t i,n,m,retval = 0; struct iguana_iAddr tmp,*ptr;
9 years ago
if ( coin->virtualchain != 0 )
return(0);
9 years ago
sprintf(fname,"%s/%s_peers.dat",GLOBAL_DBDIR,coin->symbol), OS_compatible_path(fname);
9 years ago
if ( rwflag < 0 || iA == 0 )
{
coin->numiAddrs = 0;
if ( (fp= fopen(fname,"rb+")) != 0 )
{
fseek(fp,0,SEEK_END);
n = (int32_t)(ftell(fp) / sizeof(*iA));
9 years ago
for (i=m=1; i<n; i++)
9 years ago
{
fseek(fp,i * sizeof(tmp),SEEK_SET);
if ( ftell(fp) == i*sizeof(tmp) && fread(&tmp,1,sizeof(tmp),fp) == sizeof(tmp) && tmp.ipbits != 0 )
{
portable_mutex_lock(&coin->peers_mutex);
9 years ago
ipbits = (uint32_t)tmp.ipbits;
HASH_FIND(hh,coin->iAddrs,&ipbits,sizeof(ipbits),ptr);
9 years ago
if ( ptr == 0 )
{
ptr = mycalloc('t',1,sizeof(*ptr));
if ( ptr == 0 )
8 years ago
{
printf("fatal alloc errorB in hashset\n");
iguana_exit(0,0);
8 years ago
}
9 years ago
ptr->hh.itemind = m;
ptr->ipbits = tmp.ipbits;
9 years ago
HASH_ADD(hh,coin->iAddrs,ipbits,sizeof(ipbits),ptr);
9 years ago
tmp.hh.itemind = m;
9 years ago
tmp.status = 0;
9 years ago
fseek(fp,m*sizeof(tmp),SEEK_SET);
fwrite(&tmp,1,sizeof(tmp),fp);
expand_ipbits(hexstr,ipbits);
9 years ago
//printf("create rwiAddrind m.%-4d %08x %s status.%d\n",m,(uint32_t)tmp.ipbits,hexstr,tmp.status);
9 years ago
m++;
coin->numiAddrs = m;
iguana_possible_peer(coin,hexstr);
}
9 years ago
else
9 years ago
{
9 years ago
expand_ipbits(hexstr,ipbits);
9 years ago
//printf("peer.%d status.%d ipbits.%x\n",i,tmp.status,(uint32_t)ipbits);
9 years ago
tmp.status = 0;
fseek(fp,i * sizeof(tmp),SEEK_SET);
if ( fwrite(&tmp,1,sizeof(tmp),fp) != sizeof(tmp) )
printf("error writing peer.%d\n",i);
9 years ago
}
9 years ago
portable_mutex_unlock(&coin->peers_mutex);
9 years ago
}
9 years ago
}
fclose(fp);
printf("i.%d m.%d numiAddrs.%d\n",i,m,coin->numiAddrs);
}
return(coin->numiAddrs);
}
if ( rwflag == 0 )
{
memset(iA,0,sizeof(*iA));
if ( (fp= fopen(fname,"rb")) != 0 )
{
fseek(fp,ind * sizeof(*iA),SEEK_SET);
if ( ftell(fp) == ind * sizeof(*iA) )
{
if ( fread(iA,1,sizeof(*iA),fp) != sizeof(*iA) )
printf("iAddr: error loading.[%d]\n",ind);
else
{
9 years ago
iA->status = 0;
9 years ago
if ( (iA= iguana_iAddrhashset(coin,iA,ind)) != 0 )
{
retval = iA->hh.itemind+1;
9 years ago
//printf("r %p status.%d ipbits.%x ind.%d saved iA->ind.%d retval.%d\n",iA,iA->status,(uint32_t)iA->ipbits,ind,iA->hh.itemind,retval);
9 years ago
}
}
9 years ago
} else printf("iAddr: error seeking.[%d] %ld vs %d\n",ind,ftell(fp),(int32_t)(ind * sizeof(*iA)));
9 years ago
fclose(fp);
}
}
else
{
if ( (fp= fopen(fname,"rb+")) == 0 )
9 years ago
{
if ( (fp= fopen(fname,"wb")) == 0 )
{
printf("iguana_rwAddrind: couldnt create.(%s)\n",fname);
return(-1);
}
}
9 years ago
if ( fp != 0 )
{
9 years ago
ipbits = (uint32_t)iA->ipbits;
HASH_FIND(hh,coin->iAddrs,&ipbits,sizeof(ipbits),ptr);
if ( ptr != 0 && ptr->hh.itemind != ind )
{
printf("mismatch iAddr ind.%d != %d\n",ptr->hh.itemind,ind);
ind = ptr->hh.itemind;
}
9 years ago
if ( ind <= 0 )
ind = coin->numiAddrs++;
9 years ago
fseek(fp,ind * sizeof(*iA),SEEK_SET);
if ( ftell(fp) == ind * sizeof(*iA) )
{
iA->hh.itemind = ind;
if ( fwrite(iA,1,sizeof(*iA),fp) != sizeof(*iA) )
printf("iAddr: error saving.[%d]\n",ind);
else
{
if ( (iA= iguana_iAddrhashset(coin,iA,ind)) != 0 )
{
retval = iA->hh.itemind+1;
9 years ago
expand_ipbits(ipaddr,iA->ipbits);
9 years ago
//printf("%s status.%d lastkilled.%u ipbits.%x ind.%d saved iA->ind.%d retval.%d numiAddrs.%d\n",ipaddr,iA->status,iA->lastkilled,(uint32_t)ipbits,ind,iA->hh.itemind,retval,coin->numiAddrs);
9 years ago
}
}
9 years ago
} else printf("iAddr: error seeking.[%d] %ld vs %d\n",ind,ftell(fp),(int32_t)(ind * sizeof(*iA)));
9 years ago
fclose(fp);
9 years ago
}
else
{
printf("iguana_rwAddrind: couldnt create.(%s)\n",fname);
return(-1);
}
9 years ago
}
return(retval);
}
void iguana_iAconnected(struct iguana_info *coin,struct iguana_peer *addr)
{
struct iguana_iAddr *iA;
9 years ago
if ( coin->virtualchain != 0 )
return;
9 years ago
if ( (iA= iguana_iAddrhashfind(coin,addr->ipbits,1)) != 0 )
{
iA->status = IGUANA_PEER_READY;
if ( addr->height > iA->height )
iA->height = addr->height;
iA->numconnects++;
iA->lastconnect = (uint32_t)time(NULL);
if ( iguana_rwiAddrind(coin,1,iA,iA->hh.itemind) == 0 )
printf("iguana_iAconnected (%s) save error iA->ind.%d\n",addr->ipaddr,iA->hh.itemind);
//else printf("iguana_iAconnected.(%s)\n",addr->ipaddr);
} else printf("iguana_iAconnected error getting iA\n");
}
void iguana_iAkill(struct iguana_info *coin,struct iguana_peer *addr,int32_t markflag)
{
struct iguana_iAddr *iA; int32_t rank; char ipaddr[64];
9 years ago
if ( coin->virtualchain != 0 )
return;
9 years ago
if ( addr->ipbits == 0 )
{
printf("cant iAkill null ipbits\n");
return;
}
rank = addr->rank;
strcpy(ipaddr,addr->ipaddr);
if ( addr->usock >= 0 )
closesocket(addr->usock), addr->usock = -1;
9 years ago
if ( coin->peers != 0 && addr == coin->peers->localaddr )
9 years ago
coin->peers->localaddr = 0;
9 years ago
//printf("iAkill.(%s)\n",addr->ipaddr);
if ( (iA= iguana_iAddrhashfind(coin,addr->ipbits,1)) != 0 )
{
iA->status = IGUANA_PEER_KILLED;
if ( addr->height > iA->height )
iA->height = addr->height;
if ( markflag != 0 )
{
iA->numkilled++;
iA->lastkilled = (uint32_t)time(NULL);
if ( iguana_rwiAddrind(coin,1,iA,iA->hh.itemind) == 0 )
printf("killconnection (%s) save error\n",addr->ipaddr);
}
} else printf("killconnection cant get ind for ipaddr.%s\n",addr->ipaddr);
9 years ago
memset(addr,0,sizeof(*addr));
9 years ago
addr->usock = -1;
9 years ago
//if ( rank > 0 )
// iguana_possible_peer(coin,ipaddr);
9 years ago
}
int32_t iguana_socket(int32_t bindflag,char *hostname,uint16_t port)
{
9 years ago
int32_t opt,sock,result; char ipaddr[64],checkipaddr[64]; struct timeval timeout;
9 years ago
struct sockaddr_in saddr; socklen_t addrlen,slen;
9 years ago
addrlen = sizeof(saddr);
9 years ago
struct hostent *hostent;
9 years ago
if ( parse_ipaddr(ipaddr,hostname) != 0 )
port = parse_ipaddr(ipaddr,hostname);
9 years ago
hostent = gethostbyname(ipaddr);
9 years ago
if ( hostent == NULL )
{
9 years ago
printf("gethostbyname(%s) returned error: %d port.%d ipaddr.(%s)\n",hostname,errno,port,ipaddr);
9 years ago
return(-1);
}
saddr.sin_family = AF_INET;
saddr.sin_port = htons(port);
//#ifdef WIN32
// saddr.sin_addr.s_addr = (uint32_t)calc_ipbits("127.0.0.1");
//#else
9 years ago
memcpy(&saddr.sin_addr.s_addr,hostent->h_addr_list[0],hostent->h_length);
9 years ago
expand_ipbits(checkipaddr,saddr.sin_addr.s_addr);
if ( strcmp(ipaddr,checkipaddr) != 0 )
printf("bindflag.%d iguana_socket mismatch (%s) -> (%s)?\n",bindflag,checkipaddr,ipaddr);
//#endif
9 years ago
if ( (sock= socket(AF_INET,SOCK_STREAM,0)) < 0 )
{
if ( errno != ETIMEDOUT )
printf("socket() failed: %s errno.%d", strerror(errno),errno);
return(-1);
}
9 years ago
opt = 1;
9 years ago
slen = sizeof(opt);
9 years ago
//printf("set keepalive.%d\n",setsockopt(sock,SOL_SOCKET,SO_KEEPALIVE,(void *)&opt,slen));
#ifndef WIN32
9 years ago
if ( 1 )//&& bindflag != 0 )
9 years ago
{
9 years ago
opt = 0;
getsockopt(sock,SOL_SOCKET,SO_KEEPALIVE,(void *)&opt,&slen);
9 years ago
opt = 1;
9 years ago
//printf("keepalive.%d\n",opt);
8 years ago
}
setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void *)&opt,sizeof(opt));
8 years ago
#ifdef __APPLE__
9 years ago
setsockopt(sock,SOL_SOCKET,SO_NOSIGPIPE,&opt,sizeof(opt));
#endif
9 years ago
#endif
9 years ago
if ( bindflag == 0 )
9 years ago
{
8 years ago
timeout.tv_sec = 10;
timeout.tv_usec = 0;
setsockopt(sock,SOL_SOCKET,SO_RCVTIMEO,(void *)&timeout,sizeof(timeout));
9 years ago
result = connect(sock,(struct sockaddr *)&saddr,addrlen);
if ( result != 0 )
{
if ( errno != ECONNRESET && errno != ENOTCONN && errno != ECONNREFUSED && errno != ETIMEDOUT && errno != EHOSTUNREACH )
9 years ago
{
//printf("%s(%s) port.%d failed: %s sock.%d. errno.%d\n",bindflag!=0?"bind":"connect",hostname,port,strerror(errno),sock,errno);
}
9 years ago
if ( sock >= 0 )
closesocket(sock);
return(-1);
}
9 years ago
}
9 years ago
else
9 years ago
{
9 years ago
while ( (result= bind(sock,(struct sockaddr*)&saddr,addrlen)) != 0 )
{
if ( errno == EADDRINUSE )
9 years ago
{
9 years ago
sleep(1);
9 years ago
printf("ERROR BINDING PORT.%d. this is normal tcp timeout, unless another process is using port\n",port);
9 years ago
sleep(3);
9 years ago
printf("%s(%s) port.%d try again: %s sock.%d. errno.%d\n",bindflag!=0?"bind":"connect",hostname,port,strerror(errno),sock,errno);
9 years ago
if ( bindflag == 1 )
9 years ago
{
closesocket(sock);
9 years ago
return(-1);
9 years ago
}
9 years ago
sleep(13);
9 years ago
//continue;
}
9 years ago
if ( errno != ECONNRESET && errno != ENOTCONN && errno != ECONNREFUSED && errno != ETIMEDOUT && errno != EHOSTUNREACH )
{
printf("%s(%s) port.%d failed: %s sock.%d. errno.%d\n",bindflag!=0?"bind":"connect",hostname,port,strerror(errno),sock,errno);
closesocket(sock);
return(-1);
}
}
if ( listen(sock,64) != 0 )
{
printf("listen(%s) port.%d failed: %s sock.%d. errno.%d\n",hostname,port,strerror(errno),sock,errno);
if ( sock >= 0 )
closesocket(sock);
return(-1);
}
9 years ago
}
8 years ago
#ifdef __APPLE__
8 years ago
timeout.tv_sec = 0;
8 years ago
timeout.tv_usec = 30000;
8 years ago
setsockopt(sock,SOL_SOCKET,SO_RCVTIMEO,(void *)&timeout,sizeof(timeout));
8 years ago
timeout.tv_sec = 0;
8 years ago
timeout.tv_usec = 10000;
8 years ago
setsockopt(sock,SOL_SOCKET,SO_SNDTIMEO,(void *)&timeout,sizeof(timeout));
8 years ago
#endif
9 years ago
return(sock);
}
int32_t iguana_send(struct iguana_info *coin,struct iguana_peer *addr,uint8_t *serialized,int32_t len)
{
9 years ago
int32_t numsent,remains,usock,r,i; char *cmdstr = (char *)&serialized[4];
9 years ago
if ( addr == 0 && coin->peers != 0 )
{
r = rand();
for (i=0; i<IGUANA_MAXPEERS; i++)
{
9 years ago
addr = &coin->peers->active[(i + r) % IGUANA_MAXPEERS];
if ( addr->usock >= 0 && addr->msgcounts.verack > 0 )
break;
}
}
9 years ago
if ( addr == 0 )
return(-1);
usock = addr->usock;
9 years ago
if ( usock < 0 || addr->dead != 0 )
9 years ago
{
9 years ago
return(-1);
9 years ago
}
9 years ago
remains = len;
9 years ago
if ( strncmp(cmdstr+1,"uperNET",strlen("uperNET")) == 0 || strncmp(cmdstr+1,"uperNet",strlen("uperNet")) == 0 )
9 years ago
{
9 years ago
//for (i=0; i<len-sizeof(struct iguana_msghdr); i++)
// printf("%02x",serialized[i+sizeof(struct iguana_msghdr)]);
//printf(" >>>>>>> send.(%s) crc.%x %d bytes to %s:%u supernet.%d\n",cmdstr,calc_crc32(0,serialized,len),len,addr->ipaddr,addr->A.port,addr->supernet);
9 years ago
}
9 years ago
else if ( addr->msgcounts.verack == 0 && (strcmp(cmdstr,"version") != 0 && strcmp(cmdstr,"ConnectTo") != 0 && strcmp(cmdstr,"verack") != 0) != 0 )
9 years ago
{
8 years ago
//printf("skip.(%s) since no verack yet\n",cmdstr);
9 years ago
return(-1);
9 years ago
}
8 years ago
//printf("%s -> %s\n",cmdstr,addr->ipaddr);
9 years ago
if ( strcmp(cmdstr,"ping") == 0 )
9 years ago
addr->sendmillis = OS_milliseconds();
if ( len > IGUANA_MAXPACKETSIZE )
printf("sending too big! %d\n",len);
while ( remains > 0 )
{
9 years ago
if ( coin->peers != 0 && coin->peers->shuttingdown != 0 )
9 years ago
return(-1);
9 years ago
#ifdef _WIN32
if ( (numsent= (int32_t)send(usock,serialized,remains,0)) < 0 )
#else
if ( (numsent= (int32_t)send(usock,serialized,remains,MSG_NOSIGNAL)) < 0 )
9 years ago
#endif
9 years ago
{
8 years ago
if ( errno == EAGAIN || errno == EWOULDBLOCK )
{
8 years ago
//addr->persistent_peer = 1;
sleep(1);
continue;
}
//if ( errno != EAGAIN && errno != EWOULDBLOCK )
{
printf("%s: %s numsent.%d vs remains.%d len.%d errno.%d (%s) usock.%d\n",serialized+4,addr->ipaddr,numsent,remains,len,errno,strerror(errno),addr->usock);
printf("bad errno.%d %s zombify.%p\n",errno,strerror(errno),&addr->dead);
addr->dead = (uint32_t)time(NULL);
return(-errno);
}
}
else if ( remains > 0 )
{
remains -= numsent;
serialized += numsent;
if ( remains > 0 )
printf("%s iguana sent.%d remains.%d of len.%d\n",addr->ipaddr,numsent,remains,len);
}
9 years ago
}
addr->totalsent += len;
8 years ago
if ( 0 && addr->basilisk != 0 )
8 years ago
printf("verack.%d (%s) sent.%d bytes to %s\n",addr->msgcounts.verack,cmdstr,len,addr->ipaddr);
9 years ago
return(len);
}
9 years ago
int32_t iguana_queue_send(struct iguana_peer *addr,int32_t delay,uint8_t *serialized,char *cmd,int32_t len)
9 years ago
{
struct iguana_packet *packet; int32_t datalen;
if ( addr == 0 )
{
printf("iguana_queue_send null addr\n");
iguana_exit(0,0);
9 years ago
return(-1);
}
9 years ago
if ( (datalen= iguana_sethdr((void *)serialized,addr->netmagic,cmd,&serialized[sizeof(struct iguana_msghdr)],len)) < 0 )
9 years ago
return(-1);
9 years ago
if ( strcmp("getaddr",cmd) == 0 && time(NULL) < addr->lastgotaddr+300 )
return(0);
packet = mycalloc('S',1,sizeof(struct iguana_packet) + datalen);
packet->datalen = datalen;
packet->addr = addr;
9 years ago
if ( delay != 0 )
{
if ( delay > IGUANA_MAXDELAY_MILLIS )
delay = IGUANA_MAXDELAY_MILLIS;
packet->embargo = tai_now();
packet->embargo.millis += delay;
9 years ago
}
9 years ago
memcpy(packet->serialized,serialized,datalen);
8 years ago
if ( 0 && addr->supernet != 0 )
8 years ago
printf("%p queue send.(%s) %d to (%s)\n",packet,serialized+4,datalen,addr->ipaddr);
9 years ago
queue_enqueue("sendQ",&addr->sendQ,&packet->DL,0);
return(datalen);
}
9 years ago
int32_t iguana_recv(char *ipaddr,int32_t usock,uint8_t *recvbuf,int32_t len)
9 years ago
{
int32_t recvlen,remains = len;
while ( remains > 0 )
{
if ( (recvlen= (int32_t)recv(usock,recvbuf,remains,0)) < 0 )
{
if ( errno == EAGAIN )
{
9 years ago
//printf("%s recv errno.%d %s len.%d remains.%d\n",ipaddr,errno,strerror(errno),len,remains);
9 years ago
//printf("EAGAIN for len %d, remains.%d\n",len,remains);
9 years ago
sleep(1);
9 years ago
} else return(-errno);
9 years ago
}
else
{
if ( recvlen > 0 )
{
remains -= recvlen;
9 years ago
//int32_t i; for (i=0; i<recvlen; i++)
// printf("%02x",recvbuf[i]);
8 years ago
//printf("got %d remains.%d of total.%d from (%s)\n",recvlen,remains,len,ipaddr);
9 years ago
recvbuf = &recvbuf[recvlen];
} else usleep(10000);
}
}
return(len);
}
8 years ago
void iguana_peer_meminit(struct iguana_info *coin,struct iguana_peer *addr)
{
if ( addr->RAWMEM.ptr == 0 )
iguana_meminit(&addr->RAWMEM,addr->ipaddr,0,IGUANA_MAXPACKETSIZE * 2,0);
if ( addr->TXDATA.ptr == 0 )
iguana_meminit(&addr->TXDATA,"txdata",0,IGUANA_MAXPACKETSIZE * 2,0);
if ( addr->HASHMEM.ptr == 0 )
iguana_meminit(&addr->HASHMEM,"HASHPTRS",0,256,0);//IGUANA_MAXPACKETSIZE*16,0);
//printf("Init %s memory %p %p %p\n",addr->ipaddr,addr->RAWMEM.ptr,addr->TXDATA.ptr,addr->HASHMEM.ptr);
iguana_memreset(&addr->RAWMEM);
iguana_memreset(&addr->TXDATA);
iguana_memreset(&addr->HASHMEM);
}
8 years ago
void iguana_parsebuf(struct supernet_info *myinfo,struct iguana_info *coin,struct iguana_peer *addr,struct iguana_msghdr *H,uint8_t *buf,int32_t len,int32_t fromcache)
9 years ago
{
struct iguana_msghdr checkH;
memset(&checkH,0,sizeof(checkH));
9 years ago
if ( iguana_sethdr(&checkH,coin->chain->netmagic,H->command,buf,len) > 0 && memcmp(&checkH,H,sizeof(checkH)) == 0 )
9 years ago
{
9 years ago
if ( strcmp(H->command,"block") == 0 || strcmp(H->command,"getblocks") == 0 || strcmp(H->command,"tx") == 0 )
8 years ago
iguana_peer_meminit(coin,addr);
8 years ago
if ( iguana_msgparser(myinfo,coin,addr,&addr->RAWMEM,&addr->TXDATA,&addr->HASHMEM,H,buf,len,fromcache) < 0 || addr->dead != 0 )
9 years ago
{
printf("%p addr->dead.%d or parser break at %u\n",&addr->dead,addr->dead,(uint32_t)time(NULL));
addr->dead = (uint32_t)time(NULL);
}
else
{
addr->numpackets++;
addr->totalrecv += len;
coin->totalrecv += len, coin->totalpackets++;
//printf("next iter.(%s) numreferrals.%d numpings.%d\n",addr->ipaddr,addr->numreferrals,addr->numpings);
}
9 years ago
}
else
{
int z;
for (z=0; z<sizeof(*H); z++)
printf("%02x",((uint8_t *)H)[z]);
printf(" header error from %s len %d crc.%08x\n",addr->ipaddr,len,calc_crc32(0,buf,len));
}
9 years ago
}
8 years ago
void _iguana_processmsg(struct supernet_info *myinfo,struct iguana_info *coin,int32_t usock,struct iguana_peer *addr,uint8_t *_buf,int32_t maxlen)
9 years ago
{
int32_t len,recvlen; void *buf = _buf; struct iguana_msghdr H;
9 years ago
if ( (coin->peers != 0 && coin->peers->shuttingdown != 0) || addr->dead != 0 )
9 years ago
return;
memset(&H,0,sizeof(H));
9 years ago
if ( (recvlen= (int32_t)iguana_recv(addr->ipaddr,usock,(uint8_t *)&H,sizeof(H))) == sizeof(H) )
9 years ago
{
9 years ago
if ( (coin->peers != 0 && coin->peers->shuttingdown != 0) || addr->dead != 0 )
9 years ago
return;
9 years ago
{
iguana_rwnum(0,H.serdatalen,sizeof(H.serdatalen),(uint32_t *)&len);
9 years ago
//printf("%08x got.(%s) recvlen.%d from %s | usock.%d ready.%u dead.%u len.%d\n",(uint32_t)addr->ipbits,H.command,recvlen,addr->ipaddr,addr->usock,addr->ready,addr->dead,len);
9 years ago
}
9 years ago
if ( (len= iguana_validatehdr(coin->symbol,&H)) >= 0 )
9 years ago
{
if ( len > 0 )
{
if ( len > IGUANA_MAXPACKETSIZE )
{
printf("buffer %d too small for %d\n",IGUANA_MAXPACKETSIZE,len);
return;
}
if ( len > maxlen )
buf = mycalloc('p',1,len);
9 years ago
if ( (recvlen= iguana_recv(addr->ipaddr,usock,buf,len)) < 0 )
9 years ago
{
8 years ago
printf("%s recv error on (%s) len.%d errno.%d (%s)\n",addr->ipaddr,H.command,len,-recvlen,strerror(-recvlen));
9 years ago
if ( buf != _buf )
myfree(buf,len);
8 years ago
if ( addr->numrecverrs++ > 10 )
{
addr->dead = (uint32_t)time(NULL);
8 years ago
addr->numrecverrs = 0;
8 years ago
}
9 years ago
return;
}
}
8 years ago
iguana_parsebuf(myinfo,coin,addr,&H,buf,len,0);
9 years ago
if ( buf != _buf )
myfree(buf,len);
return;
}
9 years ago
static uint32_t counter;
if ( counter++ < 10 )
{
int32_t i; for (i=0; i<sizeof(H); i++)
printf("%02x",((uint8_t *)&H)[i]);
8 years ago
printf(" %s invalid header.%s received from (%s) len.%d\n",coin->symbol,H.command,addr->ipaddr,len);
9 years ago
}
9 years ago
//addr->dead = 1;
9 years ago
}
// printf("%s recv error on hdr errno.%d (%s) -> zombify\n",addr->ipaddr,-recvlen,strerror(-recvlen));
9 years ago
#ifndef IGUANA_DEDICATED_THREADS
addr->dead = 1;
#endif
}
void iguana_gotdata(struct iguana_info *coin,struct iguana_peer *addr,int32_t height)
{
struct iguana_iAddr *iA;
if ( addr != 0 && height > addr->height && height < coin->longestchain )
{
if ( (iA= iguana_iAddrhashfind(coin,addr->ipbits,0)) != 0 && iA->height < height )
iA->height = height;
//iguana_set_iAddrheight(coin,addr->ipbits,height);
addr->height = height;
}
9 years ago
if ( height > 0 && height > coin->longestchain )
coin->longestchain = (height + coin->longestchain + 1) >> 1;
9 years ago
}
9 years ago
int32_t iguana_iAddrheight(struct iguana_info *coin,uint64_t ipbits)
9 years ago
{
struct iguana_iAddr *iA;
9 years ago
if ( coin->virtualchain != 0 )
return(0);
9 years ago
if ( (iA= iguana_iAddrhashfind(coin,ipbits,0)) != 0 )
return(iA->height);
return(0);
}
void iguana_startconnection(void *arg)
{
9 years ago
int32_t i,n; uint16_t port; char ipaddr[64]; struct iguana_peer *addr = arg; struct iguana_info *coin = 0;
9 years ago
if ( addr == 0 || (coin= iguana_coinfind(addr->symbol)) == 0 )
{
8 years ago
printf("iguana_startconnection nullptrs addr.%p (%s) coin.%p\n",addr,addr!=0?addr->symbol:"",coin);
return;
9 years ago
}
9 years ago
if ( coin->virtualchain != 0 || coin->peers == 0 )
9 years ago
return;
9 years ago
addr->addrind = (int32_t)(((long)addr - (long)&coin->peers->active[0]) / sizeof(*addr));
9 years ago
if ( addr->usock >= 0 )
{
8 years ago
//printf("%s usock.%d skip connection\n",addr->ipaddr,addr->usock);
9 years ago
return;
}
if ( strcmp(coin->name,addr->coinname) != 0 )
9 years ago
{
printf("iguana_startconnection.%s:%04x mismatched coin.%p (%s) vs (%s)\n",addr->ipaddr,coin->chain->portp2p,coin,coin->symbol,addr->coinname);
9 years ago
return;
}
8 years ago
//printf("%s iguana_startconnection.%s:%04x\n",coin->symbol,addr->ipaddr,coin->chain->portp2p);
9 years ago
if ( strcmp("127.0.0.1",addr->ipaddr) == 0 )//&& (coin->myservices & NODE_NETWORK) != 0 )
9 years ago
{
9 years ago
iguana_iAkill(coin,addr,0);
9 years ago
printf("avoid self-loopback\n");
return;
}
8 years ago
//printf(">>>>>>>> %s startconnection.(%s) pending.%u usock.%d addrind.%d\n",coin->symbol,addr->ipaddr,addr->pending,addr->usock,addr->addrind);
9 years ago
addr->pending = (uint32_t)time(NULL);
9 years ago
if ( (port= (uint16_t)(addr->ipbits >> 32)) == 0 )
port = coin->chain->portp2p;
9 years ago
if ( addr->usock < 0 )
9 years ago
addr->usock = iguana_socket(0,addr->ipaddr,port);
9 years ago
if ( addr->usock < 0 || (coin->peers != 0 && coin->peers->shuttingdown != 0) )
9 years ago
{
strcpy(ipaddr,addr->ipaddr);
8 years ago
//printf("%s refused PEER KILLED. slot.%d for %s:%d usock.%d\n",coin->symbol,addr->addrind,ipaddr,coin->chain->portp2p,addr->usock);
9 years ago
iguana_iAkill(coin,addr,1);
}
else
{
addr->ready = (uint32_t)time(NULL);
9 years ago
addr->ipbits = calc_ipbits(addr->ipaddr);
9 years ago
addr->dead = 0;
addr->pending = 0;
addr->height = iguana_iAddrheight(coin,addr->ipbits);
strcpy(addr->symbol,coin->symbol);
strcpy(addr->coinname,coin->name);
9 years ago
if ( coin->peers != 0 )
{
coin->peers->lastpeer = (uint32_t)time(NULL);
9 years ago
for (i=n=0; i<IGUANA_MAXPEERS; i++)
9 years ago
if ( coin->peers->active[i].usock > 0 )
n++;
coin->peers->numconnected++;
if ( strcmp("127.0.0.1",addr->ipaddr) == 0 )
coin->peers->localaddr = addr;
else if ( coin->peers->numranked == 0 )
coin->peers->ranked[0] = addr;
} else n = 0;
9 years ago
iguana_iAconnected(coin,addr);
9 years ago
printf("%s.PEER CONNECTED.%d:%d of max.%d! %s:%d usock.%d\n",coin->symbol,coin->peers->numconnected,n,coin->MAXPEERS,addr->ipaddr,coin->chain->portp2p,addr->usock);
9 years ago
#ifdef IGUANA_DEDICATED_THREADS
//iguana_launch("recv",iguana_dedicatedrecv,addr,IGUANA_RECVTHREAD);
iguana_dedicatedloop(SuperNET_MYINFO(0),coin,addr);
9 years ago
#endif
}
}
9 years ago
void iguana_peerkill(struct iguana_info *coin)
{
struct iguana_peer *addr;
9 years ago
if ( coin->virtualchain != 0 || coin->peers == 0 )
9 years ago
return;
9 years ago
if ( coin->peers->numranked > 0 && (addr= coin->peers->ranked[coin->peers->numranked-1]) != 0 )
9 years ago
{
9 years ago
printf("mark rank.%d as dead.(%s)\n",coin->peers->numranked,addr->ipaddr);
9 years ago
addr->dead = (uint32_t)time(NULL);
}
}
struct iguana_peer *iguana_peerslot(struct iguana_info *coin,uint64_t ipbits,int32_t forceflag)
9 years ago
{
int32_t i; struct iguana_peer *addr; char ipaddr[64];
9 years ago
if ( coin->virtualchain != 0 || coin->peers == 0 )
9 years ago
return(0);
9 years ago
for (i=0; i<IGUANA_MAXPEERS; i++)
9 years ago
if ( (uint32_t)ipbits == (uint32_t)coin->peers->active[i].ipbits )
9 years ago
return(forceflag!=0 ? &coin->peers->active[i] : 0);
9 years ago
expand_ipbits(ipaddr,ipbits);
#ifdef IGUANA_DISABLEPEERS
if ( strcmp("127.0.0.1",ipaddr) != 0 )
return(0);
#endif
//portable_mutex_lock(&coin->peers_mutex);
9 years ago
for (i=0; i<IGUANA_MAXPEERS; i++)
9 years ago
{
9 years ago
if ( i < coin->MAXPEERS || forceflag != 0 )
9 years ago
{
9 years ago
addr = &coin->peers->active[i];
9 years ago
addr->addrind = i;
if ( addr->usock >= 0 || addr->pending != 0 || addr->ipbits == ipbits || strcmp(ipaddr,addr->ipaddr) == 0 )
{
8 years ago
//printf("%s i.%d skip.(%s) usock.%d pending.%d ipbits.%llx lag.%ld\n",coin->symbol,i,addr->ipaddr,addr->usock,addr->pending,(long long)addr->ipbits,time(NULL)-addr->pending);
9 years ago
continue;
}
portable_mutex_lock(&coin->peers_mutex);
if ( addr->ipbits == 0 )
{
iguana_initpeer(coin,addr,ipbits);
//addr->pending = (uint32_t)time(NULL);
portable_mutex_unlock(&coin->peers_mutex);
return(addr);
}
9 years ago
portable_mutex_unlock(&coin->peers_mutex);
}
}
return(0);
}
8 years ago
void iguana_launchpeer(struct iguana_info *coin,char *ipaddr,int32_t forceflag)
9 years ago
{
struct iguana_peer *addr; uint32_t ipbits = (uint32_t)calc_ipbits(ipaddr);
if ( coin == 0 )
{
printf("iguana_launchpeer: null coin?\n");
8 years ago
return;
}
9 years ago
if ( coin->virtualchain != 0 )
return;
8 years ago
if ( (addr= iguana_peerslot(coin,ipbits,forceflag)) != 0 )
9 years ago
iguana_launch(coin,"connection",iguana_startconnection,addr,IGUANA_CONNTHREAD);
8 years ago
else printf("iguana_launchpeer skip %s\n",ipaddr);
9 years ago
}
void *iguana_iAddriterator(struct iguana_info *coin,struct iguana_iAddr *iA,struct iguana_peer *addr)
9 years ago
{
9 years ago
if ( coin->virtualchain != 0 )
return(0);
9 years ago
if ( iA != 0 && iA->ipbits != 0 && iguana_numthreads(coin,1 << IGUANA_CONNTHREAD) < IGUANA_MAXCONNTHREADS && iA->status == IGUANA_PEER_ELIGIBLE )
{
//printf("%x\n",iA->ipbits);
//portable_mutex_unlock(&coin->peers_mutex);
if ( addr != 0 || (addr= iguana_peerslot(coin,iA->ipbits,0)) != 0 )
9 years ago
{
9 years ago
addr->ipbits = iA->ipbits;
9 years ago
//printf("pend.%d status.%d possible peer.(%s).%x threads %d %d %d %d\n",addr->pending,iA->status,addr->ipaddr,(uint32_t)addr->ipbits,iguana_numthreads(coin,0),iguana_numthreads(coin,1),iguana_numthreads(coin,2),iguana_numthreads(coin,3));
9 years ago
if ( addr->pending == 0 && iA->status != IGUANA_PEER_CONNECTING && (iA->status != IGUANA_PEER_KILLED || time(NULL) > iA->lastkilled+600) )
9 years ago
{
9 years ago
//printf("call rwIaddrInd\n");
9 years ago
if ( iguana_rwiAddrind(coin,1,iA,iA->hh.itemind) > 0 )
{
9 years ago
//printf("iA.%p iguana_startconnection.(%s) status.%d pending.%d\n",iA,addr->ipaddr,iA->status,addr->pending);
9 years ago
iA->status = IGUANA_PEER_CONNECTING;
addr->pending = (uint32_t)time(NULL);
9 years ago
iguana_launch(coin,"connection",iguana_startconnection,addr,IGUANA_CONNTHREAD);
9 years ago
}
else
{
addr->ipbits = 0;
printf("error rwiAddrind.%d\n",iA->hh.itemind);
}
9 years ago
}
9 years ago
} else printf("%s no open peer slots left\n",coin->symbol);
9 years ago
}
//else if ( iA != 0 )
// printf("iA->ipbits %d, %d iguana_numthreads(coin,1 << IGUANA_CONNTHREAD) status.%d\n",iA->ipbits,iguana_numthreads(coin,1 << IGUANA_CONNTHREAD),iA->status);
//else printf("connector null iA\n");
return(0);
}
9 years ago
uint32_t iguana_possible_peer(struct iguana_info *coin,char *ipaddr)
{
char checkaddr[64],_ipaddr[64]; uint64_t ipbits; uint32_t now = (uint32_t)time(NULL); int32_t i,n; struct iguana_iAddr *iA; struct iguana_peer *addr;
9 years ago
if ( coin->virtualchain != 0 || coin->peers == 0 )
9 years ago
return(0);
9 years ago
if ( ipaddr != 0 && ipaddr[0] != 0 && coin->peers != 0 )
9 years ago
{
9 years ago
if ( strcmp(ipaddr,"0.0.0.0") == 0 || strcmp(ipaddr,"127.0.0.1") == 0 )
return(0);
9 years ago
for (i=n=0; i<IGUANA_MAXPEERS; i++)
9 years ago
if ( strcmp(ipaddr,coin->peers->active[i].ipaddr) == 0 )
9 years ago
{
8 years ago
//printf("%s possible peer.(%s) %x already there\n",coin->symbol,ipaddr,(uint32_t)coin->peers->active[i].ipbits);
9 years ago
return(0);
}
8 years ago
//printf("%s Q possible.(%s)\n",coin->symbol,ipaddr);
9 years ago
queue_enqueue("possibleQ",&coin->possibleQ,queueitem(ipaddr),1);
return((uint32_t)time(NULL));
}
else if ( iguana_pendingaccept(coin) != 0 )
return((uint32_t)time(NULL));
9 years ago
else if ( (ipaddr= queue_dequeue(&coin->possibleQ,1)) == 0 )
return((uint32_t)time(NULL));
safecopy(_ipaddr,ipaddr,sizeof(_ipaddr));
free_queueitem(ipaddr);
ipaddr = _ipaddr;
9 years ago
#ifdef IGUANA_DISABLEPEERS
if ( strcmp(ipaddr,"127.0.0.1") != 0 )
return((uint32_t)time(NULL));
#endif
9 years ago
ipbits = (uint32_t)calc_ipbits(ipaddr);
if ( (addr= iguana_peerslot(coin,(uint32_t)ipbits,0)) == 0 )
9 years ago
return((uint32_t)time(NULL));
/*for (i=n=0; i<IGUANA_MAXPEERS; i++)
{
if ( strcmp(ipaddr,coin->peers->active[i].ipaddr) == 0 )
return((uint32_t)time(NULL));
else if ( coin->peers->active[i].ipaddr[0] != 0 )
n++;
}*/
n = coin->peers->numranked;
9 years ago
if ( n >= coin->MAXPEERS-(coin->MAXPEERS>>3)-1 || coin->peers->numranked >= coin->MAXPEERS )
9 years ago
return((uint32_t)time(NULL));
9 years ago
if ( strncmp("0.0.0",ipaddr,5) != 0 && strcmp("0.0.255.255",ipaddr) != 0 && strcmp("1.0.0.0",ipaddr) != 0 )
{
9 years ago
for (i=0; ipaddr[i]!=0; i++)
if ( ipaddr[i] == ':' )
{
ipaddr[i] = 0;
break;
}
//printf("%s check possible peer.(%s)\n",coin->symbol,ipaddr);
9 years ago
if ( (ipbits= calc_ipbits(ipaddr)) != 0 )
9 years ago
{
expand_ipbits(checkaddr,ipbits);
if ( strcmp(checkaddr,ipaddr) == 0 )
{
8 years ago
//printf("%s valid ipaddr.(%s) MAXPEERS.%d\n",coin->symbol,ipaddr,coin->MAXPEERS);
9 years ago
if ( (iA= iguana_iAddrhashfind(coin,ipbits,1)) != 0 )
{
if ( iA->status != IGUANA_PEER_CONNECTING && iA->status != IGUANA_PEER_READY && iA->status != IGUANA_PEER_ELIGIBLE )
{
if ( (iA->lastconnect == 0 || iA->lastkilled == 0) || (iA->numconnects > 0 && iA->lastconnect > (now - IGUANA_RECENTPEER)) || iA->lastkilled < now-600 )
{
iA->status = IGUANA_PEER_ELIGIBLE;
if ( iguana_rwiAddrind(coin,1,iA,iA->hh.itemind) == 0 )
printf("error updating status for (%s) ind.%d\n",ipaddr,iA->hh.itemind);
iguana_iAddriterator(coin,iA,addr);
9 years ago
} else printf("ignore.(%s) lastconnect.%u lastkilled.%u numconnects.%d\n",ipaddr,iA->lastconnect,iA->lastkilled,iA->numconnects);
} else printf("skip.(%s) ind.%d status.%d\n",ipaddr,iA->hh.itemind,iA->status);
9 years ago
} else printf("cant find (%s) which should have been created\n",ipaddr);
8 years ago
} else printf("%s reject ipaddr.(%s) vs checkaddr.(%s)\n",coin->symbol,ipaddr,checkaddr);
9 years ago
}
}
return((uint32_t)time(NULL));
}
void iguana_processmsg(void *ptr)
{
struct iguana_info *coin; uint8_t buf[32768]; struct iguana_peer *addr = ptr;
if ( addr == 0 || (coin= iguana_coinfind(addr->symbol)) == 0 || addr->dead != 0 )
{
printf("iguana_processmsg cant find addr.%p symbol.%s\n",addr,addr!=0?addr->symbol:0);
return;
}
8 years ago
_iguana_processmsg(SuperNET_MYINFO(0),coin,addr->usock,addr,buf,sizeof(buf));
9 years ago
addr->startrecv = 0;
}
int32_t iguana_pollsendQ(struct iguana_info *coin,struct iguana_peer *addr)
{
struct iguana_packet *packet;
if ( (packet= queue_dequeue(&addr->sendQ,0)) != 0 )
{
9 years ago
if ( 0 && (addr->supernet != 0 || strcmp((char *)&packet->serialized[4],"SuperNET") == 0) )
9 years ago
printf("%s: send.(%s).%d usock.%d dead.%u ready.%u supernet.%d\n",addr->ipaddr,packet->serialized+4,packet->datalen,addr->usock,addr->dead,addr->ready,addr->supernet);
9 years ago
if ( strcmp((char *)&packet->serialized[4],"getdata") == 0 )
{
printf("unexpected getdata for %s\n",addr->ipaddr);
myfree(packet,sizeof(*packet) + packet->datalen);
}
9 years ago
else if ( packet->embargo.x == 0 || tai_diff(packet->embargo,tai_now()) >= -SMALLVAL )
9 years ago
{
iguana_send(coin,addr,packet->serialized,packet->datalen);
myfree(packet,sizeof(*packet) + packet->datalen);
return(1);
}
else
{
9 years ago
//printf("embargo.x %llu %f\n",(long long)packet->embargo.x,tai_diff(packet->embargo,tai_now()));
queue_enqueue("embargo",&addr->sendQ,&packet->DL,0);
}
9 years ago
}
return(0);
}
8 years ago
int32_t iguana_pollrecv(struct supernet_info *myinfo,struct iguana_info *coin,struct iguana_peer *addr,uint8_t *buf,int32_t bufsize)
9 years ago
{
#ifndef IGUANA_DEDICATED_THREADS
strcpy(addr->symbol,coin->symbol);
9 years ago
if ( coin->peers != 0 && addr != coin->peers->localaddr )
9 years ago
{
addr->startrecv = (uint32_t)time(NULL);
iguana_launch("processmsg",iguana_processmsg,addr,IGUANA_RECVTHREAD);
}
else
#endif
8 years ago
_iguana_processmsg(myinfo,coin,addr->usock,addr,buf,bufsize);
9 years ago
return(1);
}
#ifdef IGUANA_PEERALLOC
void *iguana_peeralloc(struct iguana_info *coin,struct iguana_peer *addr,int32_t datalen)
{
struct OS_memspace *mem; long i,iter; int32_t j,diff,size,bestfit; void *ptr;
//printf("iguana_peeralloc.%s\n",addr->ipaddr);
while ( 1 )
{
bestfit = -1;
for (iter=0; iter<3; iter++)
{
for (i=0; i<sizeof(addr->SEROUT)/sizeof(*addr->SEROUT); i++)
{
mem = addr->SEROUT[i];
if ( mem->threadsafe != 0 )
portable_mutex_lock(&mem->mutex);
if ( iter < 2 && mem->availptrs > 0 )
{
for (j=0; j<mem->numptrs; j++)
{
if ( mem->allocsizes[j] == 0 )
{
size = mem->maxsizes[j];
if ( size >= datalen )
{
diff = (size - datalen);
if ( diff == 0 || (iter == 1 && diff == bestfit) )
{
mem->allocsizes[j] = datalen;
mem->availptrs--;
//printf("%s availptrs.%d size.%d j.%d diff.%d bestfit.%d %p.%d max.%d\n",mem->name,mem->availptrs,size,j,diff,bestfit,mem->ptrs[j],size,mem->maxsizes[j]);
if ( mem->threadsafe != 0 )
portable_mutex_unlock(&mem->mutex);
return(mem->ptrs[j]);
}
else if ( iter == 0 && diff < (datalen >> 3) && diff < 4096 )
bestfit = diff;
}
}
}
}
else if ( iter == 2 && (ptr= iguana_memalloc(mem,datalen,0)) != 0 )
{
if ( mem->threadsafe != 0 )
portable_mutex_unlock(&mem->mutex);
///printf("alloc iter.2\n");
return(ptr);
}
if ( mem->threadsafe != 0 )
portable_mutex_unlock(&mem->mutex);
//printf("iter.%ld bestfit.%d\n",iter,bestfit);
}
}
printf("iguana_peeralloc: cant find memory. wait and hope...\n");
sleep(5);
}
return(0);
}
int64_t iguana_peerallocated(struct iguana_info *coin,struct iguana_peer *addr)
{
int32_t i; int64_t total = 0;
for (i=0; i<sizeof(addr->SEROUT)/sizeof(*addr->SEROUT); i++)
if ( addr->SEROUT[i] != 0 )
total += iguana_memallocated(addr->SEROUT[i]);
return(total);
}
int64_t iguana_peerfree(struct iguana_info *coin,struct iguana_peer *addr,void *ptr,int32_t datalen)
{
struct OS_memspace *mem; long offset,i; int64_t avail = -1;
//printf("iguana_peerfree.%p %d\n",ptr,datalen);
for (i=0; i<sizeof(addr->SEROUT)/sizeof(*addr->SEROUT); i++)
{
mem = addr->SEROUT[i];
offset = ((long)ptr - (long)mem->ptr);
if ( offset >= 0 && offset+datalen < mem->totalsize )
{
if ( iguana_memfree(mem,ptr,datalen) < 0 || (avail= iguana_peerallocated(coin,addr)) < 0 )
{
printf("iguana_peerfree: corrupted mem avail.%lld ptr.%p %d\n",(long long)avail,ptr,datalen);
8 years ago
iguana_exit(myinfo);
9 years ago
}
return(avail);
}
}
printf("iguana_peerfree: cant find ptr.%p %d\n",ptr,datalen);
return(-1);
}
#else
9 years ago
9 years ago
void *iguana_peeralloc(struct iguana_info *coin,struct iguana_peer *addr,int32_t datalen)
{
addr->allocated += datalen;
return(calloc(1,datalen));
}
int64_t iguana_peerfree(struct iguana_info *coin,struct iguana_peer *addr,void *ptr,int32_t datalen)
{
addr->freed += datalen;
free(ptr);
return(1);
}
int64_t iguana_peerallocated(struct iguana_info *coin,struct iguana_peer *addr)
{
return(addr->allocated - addr->freed);
}
#endif
9 years ago
int32_t iguana_voutsfname(struct iguana_info *coin,int32_t roflag,char *fname,int32_t slotid)
9 years ago
{
9 years ago
sprintf(fname,"%s/%s%s/vouts/%04d.vouts",GLOBAL_DBDIR,roflag!=0?"ro/":"",coin->symbol,slotid);
9 years ago
return((int32_t)strlen(fname));
}
9 years ago
int32_t iguana_vinsfname(struct iguana_info *coin,int32_t roflag,char *fname,int32_t slotid)
9 years ago
{
9 years ago
if ( roflag != 0 )
sprintf(fname,"%s/ro/%s/purgeable/%04d.vins",coin->VALIDATEDIR,coin->symbol,slotid);
else sprintf(fname,"%s/%s/%04d.vins",coin->VALIDATEDIR,coin->symbol,slotid);
9 years ago
return((int32_t)strlen(fname));
}
9 years ago
int32_t iguana_peerslotinit(struct iguana_info *coin,struct iguana_peer *addr,int32_t slotid,uint64_t ipbits)
9 years ago
{
char fname[1024];
9 years ago
memcpy(addr->netmagic,coin->chain->netmagic,4);
9 years ago
addr->ipbits = ipbits;
addr->addrind = slotid;
9 years ago
iguana_voutsfname(coin,0,fname,addr->addrind);
9 years ago
if ( (addr->voutsfp= fopen(fname,"rb+")) != 0 )
fseek(addr->voutsfp,0,SEEK_END);
9 years ago
else if ( (addr->voutsfp= fopen(fname,"wb+")) == 0 )
9 years ago
{
9 years ago
printf("iguana_peerslotinit cant create.(%s)\n",fname);
9 years ago
return(-1);
9 years ago
}
8 years ago
if ( coin->MAXPEERS == 1 || coin->VALIDATENODE != 0 || coin->FULLNODE != 0 )
9 years ago
{
9 years ago
iguana_vinsfname(coin,0,fname,addr->addrind);
9 years ago
if ( (addr->vinsfp= fopen(fname,"rb+")) != 0 )
fseek(addr->vinsfp,0,SEEK_END);
9 years ago
else if ( (addr->vinsfp= fopen(fname,"wb+")) == 0 )
9 years ago
{
9 years ago
printf("iguana_peerslotinit cant create.(%s)\n",fname);
9 years ago
return(-1);
9 years ago
}
9 years ago
}
9 years ago
return(0);
9 years ago
}
void iguana_dedicatedloop(struct supernet_info *myinfo,struct iguana_info *coin,struct iguana_peer *addr)
9 years ago
{
//static uint32_t lastping;
9 years ago
struct pollfd fds; struct iguana_bundlereq *req; uint8_t *buf; uint32_t ipbits;
9 years ago
int32_t bufsize,flag,run,timeout = coin->polltimeout == 0 ? 10 : coin->polltimeout;
9 years ago
if ( coin->virtualchain != 0 || coin->peers == 0 )
9 years ago
return;
9 years ago
if ( iguana_peerslotinit(coin,addr,(int32_t)(((long)addr - (long)&coin->peers->active[0]) / sizeof(*addr)),calc_ipbits(addr->ipaddr)) < 0 )
9 years ago
{
printf("error creating peer's files\n");
return;
}
9 years ago
//instantdex_peerhas_clear(coin,addr);
9 years ago
#ifdef IGUANA_PEERALLOC
int32_t i; int64_t remaining; struct OS_memspace *mem[sizeof(addr->SEROUT)/sizeof(*addr->SEROUT)];
for (i=0; i<sizeof(addr->SEROUT)/sizeof(*addr->SEROUT); i++)
{
mem[i] = mycalloc('s',1,sizeof(*mem[i]));
addr->SEROUT[i] = mem[i];
mem[i]->totalsize = IGUANA_MAXPACKETSIZE;
mem[i]->ptr = mycalloc('P',1,mem[i]->totalsize);
mem[i]->used = 0;
strcpy(mem[i]->name,addr->ipaddr);
//mem[i]->threadsafe = 1;
iguana_memreset(mem[i]);
}
#endif
//addr->pubkey = GENESIS_PUBKEY;
9 years ago
ipbits = (uint32_t)addr->ipbits;
9 years ago
vcalc_sha256(0,addr->iphash.bytes,(uint8_t *)&ipbits,sizeof(ipbits));
9 years ago
//char str[65]; printf("start dedicatedloop.%s addrind.%d %s\n",addr->ipaddr,addr->addrind,bits256_str(str,addr->iphash));
//addr->maxfilehash2 = IGUANA_MAXFILEITEMS;
9 years ago
bufsize = IGUANA_MAXPACKETSIZE;
9 years ago
if ( addr->blockspace == 0 )
addr->blockspace = mycalloc('r',1,bufsize + 8192);
9 years ago
buf = mycalloc('r',1,bufsize);
if ( strcmp(coin->symbol,"VPN") == 0 )
9 years ago
{
addr->msgcounts.verack++;
iguana_send_VPNversion(coin,addr,coin->myservices);
}
else
{
8 years ago
sleep(1 + (rand() % 3));
8 years ago
//printf("greeting send version myservices.%llu to (%s)\n",(long long)coin->myservices,addr->ipaddr);
iguana_send_version(coin,addr,coin->myservices);
}
//sleep(1+(rand()%5));
9 years ago
run = 0;
9 years ago
while ( addr->usock >= 0 && addr->dead == 0 && coin->peers->shuttingdown == 0 )
9 years ago
{
9 years ago
if ( (req= queue_dequeue(&coin->cacheQ,0)) != 0 )
9 years ago
{
if ( req->datalen != 0 )
{
9 years ago
//char str[65]; printf("CACHE.%p parse[%d] %s %s\n",req,req->recvlen,req->H.command,bits256_str(str,req->zblock.RO.hash2));
8 years ago
iguana_parsebuf(myinfo,coin,addr,&req->H,req->serializeddata,req->recvlen,1);
9 years ago
} else printf("CACHE error no datalen\n");
9 years ago
coin->cachefreed++;
myfree(req,req->allocsize);
continue;
}
flag = 0;
memset(&fds,0,sizeof(fds));
fds.fd = addr->usock;
fds.events |= (POLLOUT | POLLIN);
if ( poll(&fds,1,timeout) > 0 && (fds.revents & POLLOUT) != 0 )
9 years ago
{
9 years ago
flag += iguana_pollsendQ(coin,addr);
9 years ago
if ( addr->dead != 0 )
9 years ago
{
printf("%s is dead\n",addr->ipaddr);
9 years ago
break;
9 years ago
}
9 years ago
}
9 years ago
if ( flag == 0 )
{
if ( (fds.revents & POLLIN) != 0 )
9 years ago
{
8 years ago
flag += iguana_pollrecv(myinfo,coin,addr,buf,bufsize);
9 years ago
if ( addr->dead != 0 )
9 years ago
{
printf("%s is dead\n",addr->ipaddr);
9 years ago
break;
9 years ago
}
9 years ago
}
9 years ago
if ( flag == 0 )
{
if ( coin->active != 0 && (fds.revents & POLLOUT) != 0 )
9 years ago
{
9 years ago
if ( iguana_pollQsPT(coin,addr) > 0 )
{
9 years ago
//printf("pollQ!\n");
9 years ago
flag++;
}
9 years ago
if ( addr->dead != 0 )
9 years ago
{
printf("%s is dead\n",addr->ipaddr);
9 years ago
break;
9 years ago
}
9 years ago
}
9 years ago
}
9 years ago
if ( flag == 0 )
{
if ( run++ > 1000 )
{
//printf("sleep\n");
usleep(100000);
}
else if ( addr->rank != 1 )
9 years ago
usleep(coin->polltimeout*5000 + 1*(rand() % (coin->polltimeout*3000)));
9 years ago
else usleep(10000 + coin->backlog);
9 years ago
} else run >>= 2;
9 years ago
}
if ( flag != 0 )
run = 0;
/*else if ( 0 && addr->supernet != 0 && time(NULL) > lastping+SUPERNET_PINGGAP )
{
iguana_send_supernet(addr,SUPERNET_GETPEERSTR,0);
lastping = (uint32_t)time(NULL);
}*/
9 years ago
if ( addr->persistent_peer != 0 )
{
if ( addr->usock < 0 || addr->dead != 0 )
{
if ( addr->usock >= 0 )
closesocket(addr->usock);
addr->usock = -1;
while ( addr->usock < 0 )
{
printf("persistent peer.(%s) disconnected... reconnect\n",addr->ipaddr);
sleep(addr->persistent_peer);
9 years ago
if ( (addr->A.port= (uint16_t)(addr->ipbits >> 32)) == 0 )
addr->A.port = coin->chain->portp2p;
addr->usock = iguana_socket(0,addr->ipaddr,addr->A.port);
9 years ago
}
}
}
9 years ago
else if ( coin->isRT != 0 && addr->rank > coin->MAXPEERS && (rand() % 10) == 0 )
9 years ago
{
9 years ago
//printf("isRT and low rank.%d ",addr->rank);
9 years ago
addr->dead = 1;
}
}
9 years ago
//printf(">>>>>>>>>>>>>> finish %s dedicatedloop.%s\n",coin->symbol,addr->ipaddr);
9 years ago
if ( 0 )
{
if ( addr->vinsfp != 0 )
fclose(addr->vinsfp), addr->vinsfp = 0;
if ( addr->voutsfp != 0 )
fclose(addr->voutsfp), addr->voutsfp = 0;
}
9 years ago
iguana_iAkill(coin,addr,addr->dead != 0);
myfree(buf,bufsize);
//if ( addr->filehash2 != 0 )
// myfree(addr->filehash2,addr->maxfilehash2*sizeof(*addr->filehash2)), addr->filehash2 = 0;
8 years ago
if ( 0 )
{
iguana_mempurge(&addr->RAWMEM);
iguana_mempurge(&addr->TXDATA);
iguana_mempurge(&addr->HASHMEM);
}
9 years ago
#ifdef IGUANA_PEERALLOC
while ( (remaining= iguana_peerallocated(coin,addr)) > 0 )
{
char str[65];
printf("waiting for helperQ to flush peer mem %s\n",mbstr(str,remaining));
sleep(5);
}
for (i=0; i<sizeof(addr->SEROUT)/sizeof(*addr->SEROUT); i++)
{
if ( addr->SEROUT[i] != 0 )
{
if ( addr->SEROUT[i]->ptr != 0 )
myfree(addr->SEROUT[i]->ptr,IGUANA_MAXPACKETSIZE);
myfree(addr->SEROUT[i],sizeof(*addr->SEROUT[i]));
}
}
#endif
9 years ago
coin->peers->numconnected--;
9 years ago
}
9 years ago
void iguana_dedicatedglue(void *arg)
{
struct iguana_info *coin = 0; struct iguana_peer *addr = arg;
if ( addr == 0 || (coin= iguana_coinfind(addr->symbol)) == 0 )
{
printf("iguana_dedicatedglue nullptrs addr.%p coin.%p\n",addr,coin);
return;
}
iguana_dedicatedloop(SuperNET_MYINFO(0),coin,addr);
9 years ago
}
9 years ago
void iguana_peersloop(void *ptr)
{
#ifndef IGUANA_DEDICATED_THREADS
struct pollfd fds[IGUANA_MAXPEERS]; struct iguana_info *coin = ptr;
struct iguana_peer *addr; uint8_t *bufs[IGUANA_MAXPEERS];
int32_t i,j,n,r,nonz,flag,bufsizes[IGUANA_MAXPEERS],timeout=1;
memset(fds,0,sizeof(fds));
memset(bufs,0,sizeof(bufs));
memset(bufsizes,0,sizeof(bufsizes));
9 years ago
while ( coin->peers != 0 )
9 years ago
{
9 years ago
while ( coin->peers->shuttingdown != 0 )
9 years ago
{
printf("peers shuttingdown\n");
sleep(3);
}
flag = 0;
r = (rand() % coin->MAXPEERS);
for (j=n=nonz=0; j<coin->MAXPEERS; j++)
{
i = (j + r) % coin->MAXPEERS;
9 years ago
addr = &coin->peers->active[i];
9 years ago
fds[i].fd = -1;
if ( addr->usock >= 0 && addr->dead == 0 && addr->ready != 0 && (addr->startrecv+addr->startsend) != 0 )
{
fds[i].fd = addr->usock;
fds[i].events = (addr->startrecv != 0) * POLLIN | (addr->startsend != 0) * POLLOUT;
nonz++;
}
}
if ( nonz != 0 && poll(fds,coin->MAXPEERS,timeout) > 0 )
{
for (j=0; j<coin->MAXPEERS; j++)
{
i = (j + r) % coin->MAXPEERS;
9 years ago
addr = &coin->peers->active[i];
9 years ago
if ( addr->usock < 0 || addr->dead != 0 || addr->ready == 0 )
continue;
if ( addr->startrecv == 0 && (fds[i].revents & POLLIN) != 0 && iguana_numthreads(1 << IGUANA_RECVTHREAD) < IGUANA_MAXRECVTHREADS )
{
if ( bufs[i] == 0 )
bufsizes[i] = IGUANA_MAXPACKETSIZE, bufs[i] = mycalloc('r',1,bufsizes[i]);
flag += iguana_pollrecv(coin,addr,bufs[i],bufsizes[i]);
}
if ( addr->startsend == 0 && (fds[i].revents & POLLOUT) != 0 && iguana_numthreads(1 << IGUANA_SENDTHREAD) < IGUANA_MAXSENDTHREADS )
{
if ( iguana_pollsendQ(coin,addr) == 0 )
flag += iguana_poll(coin,addr);
else flag++;
}
}
}
if ( flag == 0 )
9 years ago
{
9 years ago
if ( 0 && time(NULL) > lastping+1 && addr->supernet != 0 )
9 years ago
iguana_send_supernet(coin,addr,SUPERNET_GETPEERSTR,0);
9 years ago
usleep(1000);
9 years ago
}
9 years ago
}
#endif
}