|
|
@ -239,6 +239,106 @@ char *nanomsg_tcpname(struct supernet_info *myinfo,char *str,char *ipaddr,uint16 |
|
|
|
return(str); |
|
|
|
} |
|
|
|
|
|
|
|
void dpow_psockloop(void *_ptr) |
|
|
|
{ |
|
|
|
int32_t i,nonz,size,sentbytes; uint32_t now; struct psock *ptr; void *buf; struct supernet_info *myinfo = _ptr; |
|
|
|
while ( 1 ) |
|
|
|
{ |
|
|
|
now = (uint32_t)time(NULL); |
|
|
|
for (i=nonz=0; i<myinfo->numpsocks; i++) // change to nn_poll!
|
|
|
|
{ |
|
|
|
portable_mutex_lock(&myinfo->psockmutex); |
|
|
|
if ( i < myinfo->numpsocks ) |
|
|
|
{ |
|
|
|
ptr = &myinfo->PSOCKS[i]; |
|
|
|
if ( (size= nn_recv(ptr->pullsock,&buf,NN_MSG,0)) > 0 ) |
|
|
|
{ |
|
|
|
ptr->lasttime = now; |
|
|
|
if ( (sentbytes= nn_send(ptr->pubsock,buf,size,0)) > 0 ) |
|
|
|
{ |
|
|
|
printf("PSOCKS[%d] of %d (%u %u) -> %d/%d bytes\n",i,myinfo->numpsocks,ptr->pushport,ptr->subport,size,sentbytes); |
|
|
|
nonz++; |
|
|
|
} |
|
|
|
} |
|
|
|
else if ( now > ptr->lasttime+PSOCK_IDLETIMEOUT ) |
|
|
|
{ |
|
|
|
printf("PSOCKS[%d] of %d (%u %u) lag.%d IDLETIMEOUT\n",i,myinfo->numpsocks,ptr->pushport,ptr->subport,now - ptr->lasttime); |
|
|
|
nn_close(ptr->pullsock); |
|
|
|
nn_close(ptr->pubsock); |
|
|
|
if ( myinfo->numpsocks > 1 ) |
|
|
|
{ |
|
|
|
myinfo->PSOCKS[i] = myinfo->PSOCKS[--myinfo->numpsocks]; |
|
|
|
memset(&myinfo->PSOCKS[myinfo->numpsocks],0,sizeof(*ptr)); |
|
|
|
} else myinfo->numpsocks = 0; |
|
|
|
} |
|
|
|
if ( buf != 0 ) |
|
|
|
nn_freemsg(buf), buf = 0; |
|
|
|
} |
|
|
|
portable_mutex_unlock(&myinfo->psockmutex); |
|
|
|
} |
|
|
|
if ( nonz == 0 ) |
|
|
|
usleep(100000); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void dpow_psockadd(struct supernet_info *myinfo,int32_t pullsock,uint16_t pushport,int32_t pubsock,uint16_t subport) |
|
|
|
{ |
|
|
|
struct psock *ptr; |
|
|
|
portable_mutex_lock(&myinfo->psockmutex); |
|
|
|
myinfo->PSOCKS = realloc(myinfo->PSOCKS,sizeof(*myinfo->PSOCKS) * (myinfo->numpsocks + 1)); |
|
|
|
ptr = &myinfo->PSOCKS[myinfo->numpsocks++]; |
|
|
|
ptr->pullsock = pullsock; |
|
|
|
ptr->pushport = pushport; |
|
|
|
ptr->pubsock = pubsock; |
|
|
|
ptr->subport = subport; |
|
|
|
ptr->lasttime = (uint32_t)time(NULL); |
|
|
|
portable_mutex_unlock(&myinfo->psockmutex); |
|
|
|
} |
|
|
|
|
|
|
|
cJSON *dpow_psock(struct supernet_info *myinfo,char *jsonstr) |
|
|
|
{ |
|
|
|
char pushaddr[128],subaddr[128]; uint16_t i,pushport,subport; int32_t timeout,maxsize,pullsock=-1,pubsock=-1; cJSON *retjson=0; |
|
|
|
retjson = cJSON_CreateObject(); |
|
|
|
pushport = myinfo->psockport++; |
|
|
|
subport = myinfo->psockport++; |
|
|
|
for (i=0; i<100; i++) |
|
|
|
{ |
|
|
|
nanomsg_tcpname(myinfo,pushaddr,myinfo->ipaddr,pushport), pushport += 2; |
|
|
|
nanomsg_tcpname(myinfo,subaddr,myinfo->ipaddr,subport), subport += 2; |
|
|
|
if ( (pullsock= nn_socket(AF_SP,NN_PULL)) >= 0 && (pubsock= nn_socket(AF_SP,NN_PUB)) >= 0 ) |
|
|
|
{ |
|
|
|
if ( nn_bind(pullsock,pushaddr) >= 0 && nn_bind(pubsock,subaddr) >= 0 ) |
|
|
|
{ |
|
|
|
timeout = 10; |
|
|
|
nn_setsockopt(pubsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout)); |
|
|
|
timeout = 1; |
|
|
|
nn_setsockopt(pullsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout)); |
|
|
|
timeout = 1; |
|
|
|
maxsize = 1024 * 1024; |
|
|
|
nn_setsockopt(pullsock,NN_SOL_SOCKET,NN_RCVBUF,&maxsize,sizeof(maxsize)); |
|
|
|
dpow_psockadd(myinfo,pullsock,pushport,pubsock,subport); |
|
|
|
jaddstr(retjson,"result","success"); |
|
|
|
jaddstr(retjson,"pushaddr",pushaddr); |
|
|
|
jaddstr(retjson,"subaddr",subaddr); |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
if ( pushport < 1000 ) |
|
|
|
pushport = 1001; |
|
|
|
if ( subport < 1000 ) |
|
|
|
subport = 1001; |
|
|
|
} |
|
|
|
if ( i == 100 ) |
|
|
|
{ |
|
|
|
jaddstr(retjson,"error","cant find psock ports"); |
|
|
|
if ( pullsock >= 0 ) |
|
|
|
nn_close(pullsock); |
|
|
|
if ( pubsock >= 0 ) |
|
|
|
nn_close(pubsock); |
|
|
|
} |
|
|
|
return(retjson); |
|
|
|
} |
|
|
|
|
|
|
|
static int _increasing_ipbits(const void *a,const void *b) |
|
|
|
{ |
|
|
|
#define uint32_a (*(uint32_t *)a) |
|
|
@ -254,7 +354,7 @@ static int _increasing_ipbits(const void *a,const void *b) |
|
|
|
|
|
|
|
void dex_packet(struct supernet_info *myinfo,struct dex_nanomsghdr *dexp,int32_t size) |
|
|
|
{ |
|
|
|
char *retstr; int32_t datalen; struct iguana_info *coin; struct dex_request dexreq; |
|
|
|
char *retstr; int32_t datalen; struct dex_request dexreq; //struct iguana_info *coin;
|
|
|
|
//for (i=0; i<size; i++)
|
|
|
|
// printf("%02x",((uint8_t *)dexp)[i]);
|
|
|
|
if ( strcmp(dexp->handler,"DEX") == 0 && dexp->datalen > BASILISK_KEYSIZE ) |
|
|
@ -266,12 +366,12 @@ void dex_packet(struct supernet_info *myinfo,struct dex_nanomsghdr *dexp,int32_t |
|
|
|
else if ( strcmp(dexp->handler,"request") == 0 ) |
|
|
|
{ |
|
|
|
datalen = dex_rwrequest(0,dexp->packet,&dexreq); |
|
|
|
if ( myinfo->IAMNOTARY != 0 && dexreq.func == 'A' && (coin= iguana_coinfind(dexreq.name)) != 0 ) |
|
|
|
/*if ( myinfo->IAMNOTARY != 0 && dexreq.func == 'A' && (coin= iguana_coinfind(dexreq.name)) != 0 )
|
|
|
|
{ |
|
|
|
if ( (retstr= dpow_importaddress(myinfo,coin,(char *)&dexp->packet[datalen])) != 0 ) |
|
|
|
free(retstr); |
|
|
|
printf("process broadcast importaddress.(%s) [%s]\n",(char *)&dexp->packet[datalen],dexreq.name); |
|
|
|
} |
|
|
|
}*/ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -652,6 +752,23 @@ char *dex_response(int32_t *broadcastflagp,struct supernet_info *myinfo,struct d |
|
|
|
retstr = jprint(retjson,1); |
|
|
|
} |
|
|
|
} |
|
|
|
else if ( dexreq.func == 'Z' ) |
|
|
|
{ |
|
|
|
if ( (retjson= dpow_psock(myinfo,(char *)&dexp->packet[datalen])) != 0 ) |
|
|
|
{ |
|
|
|
dpow_randipbits(myinfo,coin,retjson); |
|
|
|
retstr = jprint(retjson,1); |
|
|
|
} |
|
|
|
} |
|
|
|
else if ( 0 && dexreq.func == 'a' ) |
|
|
|
{ |
|
|
|
if ( (retjson= dpow_addmessage(myinfo,(char *)&dexp->packet[datalen])) != 0 ) |
|
|
|
{ |
|
|
|
*broadcastflagp = 1; |
|
|
|
dpow_randipbits(myinfo,coin,retjson); |
|
|
|
retstr = jprint(retjson,1); |
|
|
|
} |
|
|
|
} |
|
|
|
else if ( dexreq.func == 'N' ) |
|
|
|
{ |
|
|
|
uint8_t pubkeys[64][33]; char str[128]; int32_t numnotaries; cJSON *array,*item; |
|
|
@ -1010,6 +1127,24 @@ char *_dex_getmessage(struct supernet_info *myinfo,char *jsonstr) |
|
|
|
return(_dex_sendrequeststr(myinfo,&dexreq,jsonstr,0,1,"")); |
|
|
|
} |
|
|
|
|
|
|
|
char *_dex_sendmessage(struct supernet_info *myinfo,char *jsonstr) |
|
|
|
{ |
|
|
|
struct dex_request dexreq; |
|
|
|
memset(&dexreq,0,sizeof(dexreq)); |
|
|
|
safecopy(dexreq.name,"KMD",sizeof(dexreq.name)); |
|
|
|
dexreq.func = 'a'; |
|
|
|
return(_dex_sendrequeststr(myinfo,&dexreq,jsonstr,0,1,"")); |
|
|
|
} |
|
|
|
|
|
|
|
char *_dex_psock(struct supernet_info *myinfo,char *jsonstr) |
|
|
|
{ |
|
|
|
struct dex_request dexreq; |
|
|
|
memset(&dexreq,0,sizeof(dexreq)); |
|
|
|
safecopy(dexreq.name,"KMD",sizeof(dexreq.name)); |
|
|
|
dexreq.func = 'Z'; |
|
|
|
return(_dex_sendrequeststr(myinfo,&dexreq,jsonstr,0,1,"")); |
|
|
|
} |
|
|
|
|
|
|
|
char *_dex_listunspentarg(struct supernet_info *myinfo,char *symbol,char *address,uint8_t arg) |
|
|
|
{ |
|
|
|
struct dex_request dexreq; char *retstr; |
|
|
@ -1102,7 +1237,7 @@ int32_t dex_crc32find(struct supernet_info *myinfo,uint32_t crc32) |
|
|
|
|
|
|
|
int32_t dex_packetcheck(struct supernet_info *myinfo,struct dex_nanomsghdr *dexp,int32_t size) |
|
|
|
{ |
|
|
|
int32_t firstz=-1; uint32_t crc32; |
|
|
|
uint32_t crc32; //int32_t firstz=-1;
|
|
|
|
if ( dexp->version0 == (DEX_VERSION & 0xff) && dexp->version1 == ((DEX_VERSION >> 8) & 0xff) ) |
|
|
|
{ |
|
|
|
if ( dexp->datalen == (size - sizeof(*dexp)) ) |
|
|
@ -1988,7 +2123,7 @@ int32_t dpow_nanomsg_update(struct supernet_info *myinfo) |
|
|
|
if ( dex_packetcheck(myinfo,dexp,size) == 0 ) |
|
|
|
{ |
|
|
|
//printf("FROM BUS.%08x -> pub\n",dexp->crc32);
|
|
|
|
signed_nn_send(myinfo,myinfo->ctx,myinfo->persistent_priv,myinfo->pubsock,dexp,size); |
|
|
|
//signed_nn_send(myinfo,myinfo->ctx,myinfo->persistent_priv,myinfo->pubsock,dexp,size);
|
|
|
|
dex_packet(myinfo,dexp,size); |
|
|
|
} |
|
|
|
//printf("GOT DEX bus PACKET.%d\n",size);
|
|
|
@ -2012,7 +2147,7 @@ int32_t dpow_nanomsg_update(struct supernet_info *myinfo) |
|
|
|
{ |
|
|
|
printf("BROADCAST dexp request.[%d]\n",size); |
|
|
|
signed_nn_send(myinfo,myinfo->ctx,myinfo->persistent_priv,myinfo->dexsock,dexp,size); |
|
|
|
signed_nn_send(myinfo,myinfo->ctx,myinfo->persistent_priv,myinfo->pubsock,dexp,size); |
|
|
|
//signed_nn_send(myinfo,myinfo->ctx,myinfo->persistent_priv,myinfo->pubsock,dexp,size);
|
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|