|
|
@ -17,7 +17,7 @@ |
|
|
|
|
|
|
|
char *basilisk_respond_addmessage(struct supernet_info *myinfo,uint8_t *key,int32_t keylen,uint8_t *data,int32_t datalen,int32_t sendping,uint32_t duration) |
|
|
|
{ |
|
|
|
struct basilisk_message *msg; int32_t i; |
|
|
|
struct basilisk_message *msg; int32_t i; bits256 desthash; |
|
|
|
HASH_FIND(hh,myinfo->messagetable,key,keylen,msg); |
|
|
|
if ( msg == 0 && keylen == BASILISK_KEYSIZE ) |
|
|
|
{ |
|
|
@ -26,6 +26,11 @@ char *basilisk_respond_addmessage(struct supernet_info *myinfo,uint8_t *key,int3 |
|
|
|
duration = BASILISK_MSGDURATION; |
|
|
|
else if ( duration > INSTANTDEX_LOCKTIME*2 ) |
|
|
|
duration = INSTANTDEX_LOCKTIME*2; |
|
|
|
memcpy(desthash.bytes,&key[BASILISK_KEYSIZE - sizeof(desthash)],sizeof(desthash)); |
|
|
|
if ( bits256_nonz(desthash) == 0 ) |
|
|
|
msg->broadcast = 1; |
|
|
|
if ( bits256_nonz(desthash) == 0 ) |
|
|
|
msg->broadcast = 1; |
|
|
|
msg->duration = duration; |
|
|
|
msg->expiration = (uint32_t)time(NULL) + duration; |
|
|
|
msg->keylen = keylen; |
|
|
@ -47,13 +52,9 @@ char *basilisk_respond_addmessage(struct supernet_info *myinfo,uint8_t *key,int3 |
|
|
|
} else return(0); |
|
|
|
} |
|
|
|
|
|
|
|
cJSON *basilisk_respond_getmessage(struct supernet_info *myinfo,uint8_t *key,int32_t keylen) |
|
|
|
cJSON *basilisk_msgjson(struct basilisk_message *msg,uint8_t *key,int32_t keylen) |
|
|
|
{ |
|
|
|
cJSON *msgjson=0; struct basilisk_message *msg; char *ptr = 0,strbuf[32768],keystr[BASILISK_KEYSIZE*2+1]; |
|
|
|
portable_mutex_lock(&myinfo->messagemutex); |
|
|
|
HASH_FIND(hh,myinfo->messagetable,key,keylen,msg); |
|
|
|
if ( msg != 0 ) |
|
|
|
{ |
|
|
|
cJSON *msgjson=0; char *ptr = 0,strbuf[32768],keystr[BASILISK_KEYSIZE*2+1]; |
|
|
|
msgjson = cJSON_CreateObject(); |
|
|
|
if ( basilisk_addhexstr(&ptr,msgjson,strbuf,sizeof(strbuf),msg->data,msg->datalen) != 0 ) |
|
|
|
{ |
|
|
@ -68,13 +69,32 @@ cJSON *basilisk_respond_getmessage(struct supernet_info *myinfo,uint8_t *key,int |
|
|
|
free_json(msgjson); |
|
|
|
msgjson = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
return(msgjson); |
|
|
|
} |
|
|
|
|
|
|
|
cJSON *basilisk_respond_getmessage(struct supernet_info *myinfo,uint8_t *key,int32_t keylen) |
|
|
|
{ |
|
|
|
cJSON *msgjson = 0; struct basilisk_message *msg; |
|
|
|
portable_mutex_lock(&myinfo->messagemutex); |
|
|
|
HASH_FIND(hh,myinfo->messagetable,key,keylen,msg); |
|
|
|
if ( msg != 0 && msg->broadcast == 0 ) |
|
|
|
msgjson = basilisk_msgjson(msg,key,keylen); |
|
|
|
portable_mutex_unlock(&myinfo->messagemutex); |
|
|
|
return(msgjson); |
|
|
|
} |
|
|
|
|
|
|
|
// respond to incoming OUT, MSG
|
|
|
|
|
|
|
|
int32_t basilisk_messagekeyread(uint8_t *key,uint32_t *channelp,uint32_t *msgidp,bits256 *srchashp,bits256 *desthashp) |
|
|
|
{ |
|
|
|
int32_t keylen = 0; |
|
|
|
keylen += iguana_rwnum(0,&key[keylen],sizeof(uint32_t),channelp); |
|
|
|
keylen += iguana_rwnum(0,&key[keylen],sizeof(uint32_t),msgidp); |
|
|
|
keylen += iguana_rwbignum(0,&key[keylen],sizeof(*srchashp),srchashp->bytes); |
|
|
|
keylen += iguana_rwbignum(0,&key[keylen],sizeof(*desthashp),desthashp->bytes); |
|
|
|
return(keylen); |
|
|
|
} |
|
|
|
|
|
|
|
int32_t basilisk_messagekey(uint8_t *key,uint32_t channel,uint32_t msgid,bits256 srchash,bits256 desthash) |
|
|
|
{ |
|
|
|
int32_t keylen = 0; |
|
|
@ -87,26 +107,56 @@ int32_t basilisk_messagekey(uint8_t *key,uint32_t channel,uint32_t msgid,bits256 |
|
|
|
|
|
|
|
char *basilisk_respond_OUT(struct supernet_info *myinfo,char *CMD,void *addr,char *remoteaddr,uint32_t basilisktag,cJSON *valsobj,uint8_t *data,int32_t datalen,bits256 hash,int32_t from_basilisk) |
|
|
|
{ |
|
|
|
int32_t keylen,duration; uint8_t key[BASILISK_KEYSIZE]; bits256 senderhash; |
|
|
|
int32_t keylen,duration; uint8_t key[BASILISK_KEYSIZE]; bits256 senderhash; char *retstr; |
|
|
|
senderhash = jbits256(valsobj,"sender"); |
|
|
|
duration = juint(valsobj,"duration"); |
|
|
|
keylen = basilisk_messagekey(key,juint(valsobj,"channel"),juint(valsobj,"msgid"),senderhash,hash); |
|
|
|
if( bits256_nonz(senderhash) == 0 && bits256_nonz(hash) == 0 && duration > BASILISK_MSGDURATION ) |
|
|
|
retstr = basilisk_respond_addmessage(myinfo,key,keylen,data,datalen,1,duration); |
|
|
|
if ( bits256_nonz(hash) == 0 ) |
|
|
|
{ |
|
|
|
if ( duration > BASILISK_MSGDURATION ) |
|
|
|
duration = BASILISK_MSGDURATION; |
|
|
|
} |
|
|
|
// printf("OUT keylen.%d datalen.%d\n",keylen,datalen);
|
|
|
|
// char str[65]; printf("add message.[%d] channel.%u msgid.%x %s\n",datalen,juint(valsobj,"channel"),juint(valsobj,"msgid"),bits256_str(str,hash));
|
|
|
|
return(basilisk_respond_addmessage(myinfo,key,keylen,data,datalen,1,duration)); |
|
|
|
return(retstr); |
|
|
|
} |
|
|
|
|
|
|
|
int32_t basilisk_msgcmp(struct basilisk_message *msg,int32_t width,uint32_t channel,uint32_t msgid,bits256 srchash,bits256 desthash) |
|
|
|
{ |
|
|
|
uint32_t keychannel,keymsgid; bits256 keysrc,keydest; |
|
|
|
basilisk_messagekeyread(msg->key,&keychannel,&keymsgid,&keysrc,&keydest); |
|
|
|
if ( bits256_nonz(srchash) == 0 || bits256_cmp(srchash,keysrc) == 0 ) |
|
|
|
{ |
|
|
|
if ( bits256_nonz(desthash) == 0 || bits256_cmp(desthash,keydest) == 0 ) |
|
|
|
{ |
|
|
|
while ( width >= 0 ) |
|
|
|
{ |
|
|
|
if ( msgid == keymsgid && keychannel == channel ) |
|
|
|
return(0); |
|
|
|
msgid--; |
|
|
|
} |
|
|
|
return(-1); |
|
|
|
} else return(-2); |
|
|
|
} else return(-3); |
|
|
|
} |
|
|
|
|
|
|
|
char *basilisk_iterate_MSG(struct supernet_info *myinfo,uint32_t channel,uint32_t msgid,bits256 srchash,bits256 desthash,int32_t origwidth) |
|
|
|
{ |
|
|
|
uint8_t key[BASILISK_KEYSIZE]; int32_t i,keylen,width; cJSON *item,*retjson,*array; bits256 zero; |
|
|
|
struct basilisk_message *msg,*tmpmsg; uint8_t key[BASILISK_KEYSIZE]; int32_t i,keylen,width; cJSON *item,*retjson,*array; bits256 zero; |
|
|
|
memset(zero.bytes,0,sizeof(zero)); |
|
|
|
array = cJSON_CreateArray(); |
|
|
|
if ( (width= origwidth) > 3600 ) |
|
|
|
width = 3600; |
|
|
|
else if ( width < 1 ) |
|
|
|
width = 1; |
|
|
|
portable_mutex_lock(&myinfo->messagemutex); |
|
|
|
HASH_ITER(hh,myinfo->messagetable,msg,tmpmsg) |
|
|
|
{ |
|
|
|
if ( msg->broadcast != 0 && basilisk_msgcmp(msg,origwidth,channel,msgid,zero,zero) == 0 ) |
|
|
|
jaddi(array,basilisk_msgjson(msg,msg->key,msg->keylen)); |
|
|
|
} |
|
|
|
portable_mutex_unlock(&myinfo->messagemutex); |
|
|
|
//printf("iterate_MSG width.%d channel.%d msgid.%d src.%llx -> %llx\n",origwidth,channel,msgid,(long long)srchash.txid,(long long)desthash.txid);
|
|
|
|
for (i=0; i<width; i++) |
|
|
|
{ |
|
|
@ -124,13 +174,10 @@ char *basilisk_iterate_MSG(struct supernet_info *myinfo,uint32_t channel,uint32_ |
|
|
|
if ( bits256_nonz(desthash) != 0 ) |
|
|
|
{ |
|
|
|
keylen = basilisk_messagekey(key,channel,msgid,srchash,zero); |
|
|
|
//int32_t j; for (j=0; j<keylen; j++)
|
|
|
|
// printf("%02x",key[j]);
|
|
|
|
//printf(" <- key\n");
|
|
|
|
if ( (item= basilisk_respond_getmessage(myinfo,key,keylen)) != 0 ) |
|
|
|
jaddi(array,item);//, printf("gotmsg2.(%s)\n",jprint(item,0));
|
|
|
|
} |
|
|
|
if ( bits256_nonz(srchash) != 0 || bits256_nonz(desthash) != 0 ) |
|
|
|
if ( bits256_nonz(srchash) != 0 && bits256_nonz(desthash) != 0 ) |
|
|
|
{ |
|
|
|
keylen = basilisk_messagekey(key,channel,msgid,zero,zero); |
|
|
|
if ( (item= basilisk_respond_getmessage(myinfo,key,keylen)) != 0 ) |
|
|
@ -187,7 +234,9 @@ HASH_ARRAY_STRING(basilisk,sendmessage,hash,vals,hexstr) |
|
|
|
{ |
|
|
|
keylen = basilisk_messagekey(key,juint(vals,"channel"),juint(vals,"msgid"),jbits256(vals,"sender"),hash); |
|
|
|
if ( (data= get_dataptr(BASILISK_HDROFFSET,&ptr,&datalen,space,sizeof(space),hexstr)) != 0 ) |
|
|
|
{ |
|
|
|
retstr = basilisk_respond_addmessage(myinfo,key,keylen,data,datalen,0,juint(vals,"duration")); |
|
|
|
} |
|
|
|
if ( ptr != 0 ) |
|
|
|
free(ptr); |
|
|
|
if ( retstr != 0 ) |
|
|
|