jl777 8 years ago
parent
commit
7f599df546
  1. 2
      basilisk/basilisk.c
  2. 186
      basilisk/basilisk_MSG.c

2
basilisk/basilisk.c

@ -635,7 +635,6 @@ void basilisk_msgprocess(struct supernet_info *myinfo,void *_addr,uint32_t sende
{ (void *)"VOT", &basilisk_respond_VOT }, // VOTE handler for something
//{ (void *)"PIN", &basilisk_respond_PIN },
// encrypted data for jumblr
{ (void *)"HOP", &basilisk_respond_forward }, // message forwarding
{ (void *)"BOX", &basilisk_respond_mailbox }, // create/send/check mailbox pubkey
@ -748,6 +747,7 @@ void basilisk_msgprocess(struct supernet_info *myinfo,void *_addr,uint32_t sende
{
if ( (coin != 0 && coin->FULLNODE != 0) || myinfo->NOTARY.RELAYID >= 0 ) // iguana node
{
printf("\n call %s from_basilisk.%d (%s)\n",addr->ipaddr,from_basilisk,type);
if ( (retstr= (*basilisk_services[i][1])(myinfo,type,addr,remoteaddr,basilisktag,valsobj,data,datalen,hash,from_basilisk)) != 0 )
{
printf("%s from_basilisk.%d ret.(%s)\n",addr->ipaddr,from_basilisk,retstr);

186
basilisk/basilisk_MSG.c

@ -15,46 +15,24 @@
// included from basilisk.c
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)
int32_t basilisk_messagekeyread(uint8_t *key,uint32_t *channelp,uint32_t *msgidp,bits256 *srchashp,bits256 *desthashp)
{
struct basilisk_message *msg; int32_t i; bits256 desthash;
if ( keylen != BASILISK_KEYSIZE )
return(0);
portable_mutex_lock(&myinfo->messagemutex);
HASH_FIND(hh,myinfo->messagetable,key,keylen,msg);
if ( msg == 0 || msg->datalen != datalen )
{
if ( msg != 0 )
{
printf("overwrite delete of msg.[%d]\n",msg->datalen);
HASH_DELETE(hh,myinfo->messagetable,msg);
QUEUEITEMS--;
free(msg);
}
msg = calloc(1,sizeof(*msg) + datalen);
}
if ( duration == 0 )
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;
msg->duration = duration;
msg->expiration = (uint32_t)time(NULL) + duration;
msg->keylen = keylen;
memcpy(msg->key,key,keylen);
msg->datalen = datalen;
memcpy(msg->data,data,datalen);
HASH_ADD_KEYPTR(hh,myinfo->messagetable,msg->key,msg->keylen,msg);
for (i=0; i<BASILISK_KEYSIZE; i++)
printf("%02x",key[i]);
printf(" <- ADDMSG.[%d] exp %u\n",QUEUEITEMS,msg->expiration);
QUEUEITEMS++;
if ( sendping != 0 )
queue_enqueue("basilisk_message",&myinfo->msgQ,&msg->DL,0);
portable_mutex_unlock(&myinfo->messagemutex);
return(clonestr("{\"result\":\"message added to hashtable\"}"));
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;
keylen += iguana_rwnum(1,&key[keylen],sizeof(uint32_t),&channel);
keylen += iguana_rwnum(1,&key[keylen],sizeof(uint32_t),&msgid);
keylen += iguana_rwbignum(1,&key[keylen],sizeof(srchash),srchash.bytes);
keylen += iguana_rwbignum(1,&key[keylen],sizeof(desthash),desthash.bytes);
return(keylen);
}
cJSON *basilisk_msgjson(struct basilisk_message *msg,uint8_t *key,int32_t keylen)
@ -77,57 +55,15 @@ cJSON *basilisk_msgjson(struct basilisk_message *msg,uint8_t *key,int32_t keylen
return(msgjson);
}
cJSON *basilisk_respond_getmessage(struct supernet_info *myinfo,uint8_t *key,int32_t keylen)
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;
keylen += iguana_rwnum(1,&key[keylen],sizeof(uint32_t),&channel);
keylen += iguana_rwnum(1,&key[keylen],sizeof(uint32_t),&msgid);
keylen += iguana_rwbignum(1,&key[keylen],sizeof(srchash),srchash.bytes);
keylen += iguana_rwbignum(1,&key[keylen],sizeof(desthash),desthash.bytes);
return(keylen);
}
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 desthash,senderhash; char *retstr;
senderhash = jbits256(valsobj,"srchash");
desthash = jbits256(valsobj,"desthash");
duration = juint(valsobj,"duration");
keylen = basilisk_messagekey(key,juint(valsobj,"channel"),juint(valsobj,"msgid"),senderhash,desthash);
if ( bits256_nonz(hash) == 0 )
{
if ( duration > BASILISK_MSGDURATION )
duration = BASILISK_MSGDURATION;
}
retstr = basilisk_respond_addmessage(myinfo,key,keylen,data,datalen,1,duration);
// 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(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;
@ -157,9 +93,13 @@ char *basilisk_iterate_MSG(struct supernet_info *myinfo,uint32_t channel,uint32_
width = 1;
allflag = (bits256_nonz(srchash) == 0 && bits256_nonz(desthash) == 0);
array = cJSON_CreateArray();
fprintf(stderr,"[");
portable_mutex_lock(&myinfo->messagemutex);
HASH_ITER(hh,myinfo->messagetable,msg,tmpmsg)
{
if ( allflag != 0 || (msg->broadcast != 0 && basilisk_msgcmp(msg,origwidth,channel,msgid,zero,zero) == 0) )
if ( (msgjson= basilisk_msgjson(msg,msg->key,msg->keylen)) != 0 )
jaddi(array,msgjson);
if ( now > msg->expiration )
{
printf("delete expired message.%p QUEUEITEMS.%d\n",msg,QUEUEITEMS);
@ -167,51 +107,49 @@ char *basilisk_iterate_MSG(struct supernet_info *myinfo,uint32_t channel,uint32_
QUEUEITEMS--;
free(msg);
}
if ( allflag != 0 || (msg->broadcast != 0 && basilisk_msgcmp(msg,origwidth,channel,msgid,zero,zero) == 0) )
if ( (msgjson= basilisk_msgjson(msg,msg->key,msg->keylen)) != 0 )
jaddi(array,msgjson);
}
portable_mutex_unlock(&myinfo->messagemutex);
//printf("iterate_MSG allflag.%d width.%d channel.%d msgid.%d src.%llx -> %llx\n",allflag,origwidth,channel,msgid,(long long)srchash.txid,(long long)desthash.txid);
for (i=0; i<width; i++)
{
if ( allflag != 0 )
break;
keylen = basilisk_messagekey(key,channel,msgid,srchash,desthash);
if ( (item= basilisk_respond_getmessage(myinfo,key,keylen)) != 0 )
if ( (item= _basilisk_respond_getmessage(myinfo,key,keylen)) != 0 )
jaddi(array,item);//, printf("gotmsg0.(%s)\n",jprint(item,0));
keylen = basilisk_messagekey(key,channel,msgid,desthash,srchash);
if ( (item= basilisk_respond_getmessage(myinfo,key,keylen)) != 0 )
if ( (item= _basilisk_respond_getmessage(myinfo,key,keylen)) != 0 )
jaddi(array,item);//, printf("gotmsg0.(%s)\n",jprint(item,0));
if ( origwidth > 0 )
{
if ( bits256_nonz(srchash) != 0 )
{
keylen = basilisk_messagekey(key,channel,msgid,zero,desthash);
if ( (item= basilisk_respond_getmessage(myinfo,key,keylen)) != 0 )
if ( (item= _basilisk_respond_getmessage(myinfo,key,keylen)) != 0 )
jaddi(array,item);//, printf("gotmsg1.(%s)\n",jprint(item,0));
keylen = basilisk_messagekey(key,channel,msgid,desthash,zero);
if ( (item= basilisk_respond_getmessage(myinfo,key,keylen)) != 0 )
if ( (item= _basilisk_respond_getmessage(myinfo,key,keylen)) != 0 )
jaddi(array,item);//, printf("gotmsg1.(%s)\n",jprint(item,0));
}
if ( bits256_nonz(desthash) != 0 )
{
keylen = basilisk_messagekey(key,channel,msgid,srchash,zero);
if ( (item= basilisk_respond_getmessage(myinfo,key,keylen)) != 0 )
if ( (item= _basilisk_respond_getmessage(myinfo,key,keylen)) != 0 )
jaddi(array,item);//, printf("gotmsg2.(%s)\n",jprint(item,0));
keylen = basilisk_messagekey(key,channel,msgid,zero,srchash);
if ( (item= basilisk_respond_getmessage(myinfo,key,keylen)) != 0 )
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 )
{
keylen = basilisk_messagekey(key,channel,msgid,zero,zero);
if ( (item= basilisk_respond_getmessage(myinfo,key,keylen)) != 0 )
if ( (item= _basilisk_respond_getmessage(myinfo,key,keylen)) != 0 )
jaddi(array,item);//, printf("gotmsg3.(%s)\n",jprint(item,0));
}
}
msgid--;
}
portable_mutex_unlock(&myinfo->messagemutex);
fprintf(stderr,"]");
if ( cJSON_GetArraySize(array) > 0 )
{
retjson = cJSON_CreateObject();
@ -222,6 +160,68 @@ char *basilisk_iterate_MSG(struct supernet_info *myinfo,uint32_t channel,uint32_
} else return(clonestr("{\"error\":\"no messages\"}"));
}
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; bits256 desthash;
if ( keylen != BASILISK_KEYSIZE )
return(0);
portable_mutex_lock(&myinfo->messagemutex);
HASH_FIND(hh,myinfo->messagetable,key,keylen,msg);
if ( msg == 0 || msg->datalen != datalen )
{
if ( msg != 0 )
{
printf("overwrite delete of msg.[%d]\n",msg->datalen);
HASH_DELETE(hh,myinfo->messagetable,msg);
QUEUEITEMS--;
free(msg);
}
msg = calloc(1,sizeof(*msg) + datalen);
}
if ( duration == 0 )
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;
msg->duration = duration;
msg->expiration = (uint32_t)time(NULL) + duration;
msg->keylen = keylen;
memcpy(msg->key,key,keylen);
msg->datalen = datalen;
memcpy(msg->data,data,datalen);
HASH_ADD_KEYPTR(hh,myinfo->messagetable,msg->key,msg->keylen,msg);
for (i=0; i<BASILISK_KEYSIZE; i++)
printf("%02x",key[i]);
printf(" <- ADDMSG.[%d] exp %u\n",QUEUEITEMS,msg->expiration);
QUEUEITEMS++;
if ( sendping != 0 )
queue_enqueue("basilisk_message",&myinfo->msgQ,&msg->DL,0);
portable_mutex_unlock(&myinfo->messagemutex);
return(clonestr("{\"result\":\"message added to hashtable\"}"));
}
// respond to incoming OUT, MSG
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 desthash,senderhash; char *retstr;
senderhash = jbits256(valsobj,"srchash");
desthash = jbits256(valsobj,"desthash");
duration = juint(valsobj,"duration");
keylen = basilisk_messagekey(key,juint(valsobj,"channel"),juint(valsobj,"msgid"),senderhash,desthash);
if ( bits256_nonz(hash) == 0 )
{
if ( duration > BASILISK_MSGDURATION )
duration = BASILISK_MSGDURATION;
}
retstr = basilisk_respond_addmessage(myinfo,key,keylen,data,datalen,1,duration);
// 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(retstr);
}
char *basilisk_respond_MSG(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 width; uint32_t msgid,channel; char *retstr;

Loading…
Cancel
Save