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.

242 lines
10 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
9 years ago
char *basilisk_respond_addmessage(struct supernet_info *myinfo,uint8_t *key,int32_t keylen,uint8_t *data,int32_t datalen,int32_t sendping)
9 years ago
{
struct basilisk_message *msg;
9 years ago
if ( keylen == sizeof(bits256)+sizeof(uint32_t)*2 )
9 years ago
{
9 years ago
msg = calloc(1,sizeof(*msg) + datalen);
msg->expiration = (uint32_t)time(NULL) + INSTANTDEX_LOCKTIME*2;
msg->keylen = keylen;
memcpy(msg->key,key,keylen);
msg->datalen = datalen;
memcpy(msg->data,data,datalen);
portable_mutex_lock(&myinfo->messagemutex);
HASH_ADD_KEYPTR(hh,myinfo->messagetable,msg->key,msg->keylen,msg);
portable_mutex_unlock(&myinfo->messagemutex);
if ( sendping != 0 )
{
queue_enqueue("basilisk_message",&myinfo->msgQ,&msg->DL,0);
return(clonestr("{\"result\":\"message added to hashtable\"}"));
} else return(0);
9 years ago
} else return(0);
}
int32_t basilisk_ping_processMSG(struct supernet_info *myinfo,uint32_t senderipbits,uint8_t *data,int32_t datalen)
{
int32_t i,msglen,len=0; uint8_t num,keylen,*msg,*key;
if ( (num= data[len++]) > 0 )
{
9 years ago
printf("processMSG num.%d datalen.%d\n",num,datalen);
9 years ago
for (i=0; i<num; i++)
{
keylen = data[len++];
9 years ago
if ( keylen != sizeof(bits256)+sizeof(uint32_t)*2 )
return(0);
9 years ago
key = &data[len], len += keylen;
9 years ago
if ( len+sizeof(msglen) > datalen )
return(0);
9 years ago
len += iguana_rwnum(0,&data[len],sizeof(msglen),&msglen);
msg = &data[len], len += msglen;
9 years ago
if ( msglen <= 0 || len > datalen )
return(0);
9 years ago
//printf("i.%d: keylen.%d msglen.%d\n",i,keylen,msglen);
9 years ago
basilisk_respond_addmessage(myinfo,key,keylen,msg,msglen,0);
9 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;
9 years ago
datalen += iguana_rwnum(1,&data[datalen],sizeof(msg->datalen),&msg->datalen);
9 years ago
if ( maxlen > datalen+msg->datalen )
9 years ago
{
9 years ago
//printf("SEND keylen.%d msglen.%d\n",msg->keylen,msg->datalen);
9 years ago
memcpy(&data[datalen],msg->data,msg->datalen), datalen += msg->datalen;
9 years ago
}
9 years ago
else
{
printf("basilisk_ping_genMSG message doesnt fit %d vs %d\n",maxlen,datalen+msg->datalen);
datalen = 0;
}
9 years ago
//printf("\n-> ");
//int32_t i;
//for (i=0; i<datalen; i++)
// printf("%02x",data[i]);
//printf(" <- genMSG\n");
9 years ago
} else data[datalen++] = 0;
9 years ago
return(datalen);
}
char *basilisk_respond_getmessage(struct supernet_info *myinfo,uint8_t *key,int32_t keylen)
{
9 years ago
cJSON *retjson,*msgjson; struct basilisk_message *msg; char *ptr = 0,strbuf[32768];
9 years ago
retjson = cJSON_CreateObject();
portable_mutex_lock(&myinfo->messagemutex);
HASH_FIND(hh,myinfo->messagetable,key,keylen,msg);
if ( msg != 0 )
{
9 years ago
msgjson = cJSON_CreateObject();
if ( basilisk_addhexstr(&ptr,msgjson,strbuf,sizeof(strbuf),msg->data,msg->datalen) != 0 )
9 years ago
{
9 years ago
jadd(retjson,"message",msgjson);
9 years ago
jaddstr(retjson,"result","success");
9 years ago
printf("havemessage len.%d\n",msg->datalen);
9 years ago
} else jaddstr(retjson,"error","couldnt add message");
9 years ago
} else jaddstr(retjson,"error","no message");
portable_mutex_unlock(&myinfo->messagemutex);
return(jprint(retjson,1));
}
// respond to incoming OUT, MSG
int32_t basilisk_messagekey(uint8_t *key,bits256 hash,cJSON *valsobj)
{
uint32_t channel,msgid; int32_t keylen = 0;
channel = juint(valsobj,"channel");
msgid = juint(valsobj,"msgid");
keylen += iguana_rwbignum(1,&key[keylen],sizeof(hash),hash.bytes);
keylen += iguana_rwnum(1,&key[keylen],sizeof(uint32_t),&channel);
keylen += iguana_rwnum(1,&key[keylen],sizeof(uint32_t),&msgid);
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; uint8_t key[64];
keylen = basilisk_messagekey(key,hash,valsobj);
9 years ago
//printf("keylen.%d datalen.%d\n",keylen,datalen);
9 years ago
char str[65]; printf("add message.[%d] channel.%u msgid.%x %s\n",datalen,juint(valsobj,"channel"),juint(valsobj,"msgid"),bits256_str(str,hash));
9 years ago
return(basilisk_respond_addmessage(myinfo,key,keylen,data,datalen,1));
9 years ago
}
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 keylen; uint8_t key[64];
keylen = basilisk_messagekey(key,hash,valsobj);
9 years ago
char str[65]; printf("%s channel.%u msgid.%u datalen.%d\n",bits256_str(str,hash),juint(valsobj,"channel"),juint(valsobj,"msgid"),datalen);
9 years ago
return(basilisk_respond_getmessage(myinfo,key,keylen));
}
#include "../includes/iguana_apidefs.h"
#include "../includes/iguana_apideclares.h"
9 years ago
9 years ago
HASH_ARRAY_STRING(basilisk,getmessage,hash,vals,hexstr)
{
9 years ago
int32_t keylen; uint8_t key[64];
if ( myinfo->RELAYID >= 0 )
{
keylen = basilisk_messagekey(key,hash,vals);
return(basilisk_respond_getmessage(myinfo,key,keylen));
} else return(basilisk_standardservice("MSG",myinfo,0,myinfo->myaddr.persistent,vals,hexstr,1));
9 years ago
}
HASH_ARRAY_STRING(basilisk,sendmessage,hash,vals,hexstr)
{
9 years ago
int32_t keylen,datalen; uint8_t key[64],space[16384],*data,*ptr = 0; char *retstr=0;
if ( myinfo->RELAYID >= 0 )
{
keylen = basilisk_messagekey(key,hash,vals);
if ( (data= get_dataptr(BASILISK_HDROFFSET,&ptr,&datalen,space,sizeof(space),hexstr)) != 0 )
9 years ago
retstr = basilisk_respond_addmessage(myinfo,key,keylen,data,datalen,1);
9 years ago
if ( ptr != 0 )
free(ptr);
9 years ago
if ( retstr != 0 )
free(retstr);
}
9 years ago
if ( vals != 0 )
jaddnum(vals,"fanout",BASILISK_MAXFANOUT);
9 years ago
return(basilisk_standardservice("OUT",myinfo,0,hash,vals,hexstr,1));
9 years ago
}
#include "../includes/iguana_apiundefs.h"
int32_t basilisk_channelsend(struct supernet_info *myinfo,bits256 hash,uint32_t channel,uint32_t msgid,uint8_t *data,int32_t datalen)
{
9 years ago
char *retstr,*hexstr,strbuf[4096],*ptr = 0; int32_t retval = -1; cJSON *valsobj;
9 years ago
if ( (hexstr= basilisk_addhexstr(&ptr,0,strbuf,sizeof(strbuf),data,datalen)) != 0 )
{
valsobj = cJSON_CreateObject();
jaddnum(valsobj,"channel",channel);
jaddnum(valsobj,"msgid",msgid);
9 years ago
char str[65]; printf("sendmessage.[%d] channel.%u msgid.%x -> %s\n",datalen,channel,msgid,bits256_str(str,hash));
9 years ago
if ( (retstr= basilisk_sendmessage(myinfo,0,0,0,hash,valsobj,hexstr)) != 0 )
{
9 years ago
retval = 0;
9 years ago
free(retstr);
}
free_json(valsobj);
if ( ptr != 0 )
free(ptr);
}
return(retval);
}
9 years ago
int32_t basilisk_message_returned(uint8_t *data,int32_t maxlen,cJSON *item)
{
char *hexstr=0; cJSON *msgobj; int32_t datalen=0,retval = -1;
if ( (msgobj= jobj(item,"message")) != 0 )
{
if ( (hexstr= jstr(msgobj,"data")) != 0 && (datalen= is_hexstr(hexstr,0)) > 0 )
{
if ( datalen < maxlen )
{
decode_hex(data,datalen,hexstr);
9 years ago
printf("decoded hexstr.[%d]\n",datalen);
9 years ago
retval = datalen;
} else printf("datalen.%d < maxlen.%d\n",datalen,maxlen);
} else printf("no hexstr.%p or datalen.%d\n",hexstr,datalen);
9 years ago
} else printf("no msgobj\n");
9 years ago
return(retval);
}
9 years ago
int32_t basilisk_channelget(struct supernet_info *myinfo,bits256 hash,uint32_t channel,uint32_t msgid,uint8_t *data,int32_t maxlen)
{
9 years ago
char *retstr; cJSON *valsobj,*retarray,*item; int32_t i,datalen=0;
9 years ago
valsobj = cJSON_CreateObject();
jaddnum(valsobj,"channel",channel);
jaddnum(valsobj,"msgid",msgid);
9 years ago
jaddnum(valsobj,"fanout",1);
9 years ago
if ( (retstr= basilisk_getmessage(myinfo,0,0,0,hash,valsobj,0)) != 0 )
{
9 years ago
printf("gotmessage.(%s)\n",retstr);
9 years ago
if ( (retarray= cJSON_Parse(retstr)) != 0 )
9 years ago
{
9 years ago
if ( is_cJSON_Array(retarray) != 0 )
9 years ago
{
9 years ago
for (i=0; i<cJSON_GetArraySize(retarray); i++)
9 years ago
{
9 years ago
item = jitem(retarray,i);
9 years ago
if ( (datalen= basilisk_message_returned(data,maxlen,item)) > 0 )
9 years ago
break;
}
} else datalen = basilisk_message_returned(data,maxlen,retarray);
9 years ago
free_json(retarray);
9 years ago
} else printf("cant parse message\n");
9 years ago
free(retstr);
9 years ago
} else printf("null getmessage\n");
9 years ago
free_json(valsobj);
9 years ago
return(datalen);
9 years ago
}