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.

287 lines
11 KiB

9 years ago
/******************************************************************************
* Copyright © 2014-2015 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. *
* *
******************************************************************************/
#define CHROMEAPP_NAME iguana
#define CHROMEAPP_STR "iguana"
#define CHROMEAPP_CONF "iguana.conf"
#define CHROMEAPP_MAIN iguana_main
#define CHROMEAPP_JSON iguana_JSON
#define CHROMEAPP_HANDLER Handler_iguana
#include "../pnacl_main.h"
#include "iguana777.h"
9 years ago
#include "SuperNET.h"
9 years ago
9 years ago
#define SUPERNET_HELPSTR "SuperNET help text here"
9 years ago
// ALL globals must be here!
9 years ago
struct iguana_info *Coins[IGUANA_MAXCOINS];
9 years ago
int32_t USE_JAY,FIRST_EXTERNAL,IGUANA_disableNXT,Debuglevel;
uint32_t prices777_NXTBLOCK,MAX_DEPTH = 100;
char NXTAPIURL[256],IGUANA_NXTADDR[256],IGUANA_NXTACCTSECRET[256];
uint64_t IGUANA_MY64BITS;
9 years ago
queue_t helperQ,jsonQ,finishedQ,bundlesQ;
9 years ago
static int32_t initflag;
#ifdef __linux__
9 years ago
int32_t IGUANA_NUMHELPERS = 8;
9 years ago
#else
int32_t IGUANA_NUMHELPERS = 1;
9 years ago
#endif
9 years ago
char *SuperNET_jsonstr(struct supernet_info *myinfo,char *jsonstr,char *remoteaddr)
9 years ago
{
9 years ago
cJSON *json; char *agent,*method;
9 years ago
if ( (json= cJSON_Parse(jsonstr)) != 0 )
{
9 years ago
method = jstr(json,"method");
if ( (agent= jstr(json,"agent")) != 0 && method != 0 )
9 years ago
return(SuperNET_parser(myinfo,agent,method,json,remoteaddr));
9 years ago
else if ( method != 0 && is_bitcoinrpc(method) )
return(iguana_bitcoinRPC(myinfo,method,json,remoteaddr));
9 years ago
return(clonestr("{\"error\":\"need both agent and method\"}"));
}
return(clonestr("{\"error\":\"couldnt parse SuperNET_JSON\"}"));
9 years ago
}
9 years ago
struct iguana_jsonitem { struct queueitem DL; struct supernet_info *myinfo; uint32_t fallback,expired,allocsize; char **retjsonstrp; char remoteaddr[64]; char jsonstr[]; };
9 years ago
9 years ago
int32_t iguana_jsonQ()
9 years ago
{
9 years ago
struct iguana_jsonitem *ptr;
if ( (ptr= queue_dequeue(&finishedQ,0)) != 0 )
{
if ( ptr->expired != 0 )
{
*ptr->retjsonstrp = clonestr("{\"error\":\"request timeout\"}");
printf("garbage collection: expired.(%s)\n",ptr->jsonstr);
myfree(ptr,ptr->allocsize);
} else queue_enqueue("finishedQ",&finishedQ,&ptr->DL,0);
}
if ( (ptr= queue_dequeue(&jsonQ,0)) != 0 )
{
9 years ago
printf("process.(%s)\n",ptr->jsonstr);
9 years ago
if ( (*ptr->retjsonstrp= SuperNET_jsonstr(ptr->myinfo,ptr->jsonstr,ptr->remoteaddr)) == 0 )
*ptr->retjsonstrp = clonestr("{\"error\":\"null return from iguana_jsonstr\"}");
9 years ago
printf("finished.(%s) -> (%s)\n",ptr->jsonstr,*ptr->retjsonstrp!=0?*ptr->retjsonstrp:"null return");
9 years ago
queue_enqueue("finishedQ",&finishedQ,&ptr->DL,0);
9 years ago
return(1);
9 years ago
}
9 years ago
return(0);
}
9 years ago
char *iguana_blockingjsonstr(struct supernet_info *myinfo,char *jsonstr,uint64_t tag,int32_t maxmillis,char *remoteaddr)
9 years ago
{
9 years ago
struct iguana_jsonitem *ptr; char *retjsonstr = 0; int32_t len,allocsize; double expiration;
expiration = OS_milliseconds() + maxmillis;
//printf("blocking case.(%s)\n",jsonstr);
len = (int32_t)strlen(jsonstr);
allocsize = sizeof(*ptr) + len + 1;
ptr = mycalloc('J',1,allocsize);
ptr->allocsize = allocsize;
ptr->myinfo = myinfo;
ptr->retjsonstrp = &retjsonstr;
safecopy(ptr->remoteaddr,remoteaddr,sizeof(ptr->remoteaddr));
memcpy(ptr->jsonstr,jsonstr,len+1);
queue_enqueue("jsonQ",&jsonQ,&ptr->DL,0);
while ( OS_milliseconds() < expiration )
9 years ago
{
9 years ago
usleep(100);
if ( retjsonstr != 0 )
{
9 years ago
//printf("blocking retjsonstr.(%s)\n",retjsonstr);
9 years ago
queue_delete(&finishedQ,&ptr->DL,allocsize,1);
return(retjsonstr);
}
usleep(1000);
9 years ago
}
9 years ago
//printf("(%s) expired\n",jsonstr);
ptr->expired = (uint32_t)time(NULL);
return(clonestr("{\"error\":\"iguana jsonstr expired\"}"));
9 years ago
}
9 years ago
char *SuperNET_JSON(struct supernet_info *myinfo,cJSON *json,char *remoteaddr)
9 years ago
{
9 years ago
cJSON *retjson; uint64_t tag; uint32_t timeout; char *jsonstr; char *retjsonstr,*retstr = 0;
9 years ago
printf("SuperNET_JSON.(%s) remoteaddr.(%s)\n",jprint(json,0),remoteaddr!=0?remoteaddr:"");
9 years ago
if ( json != 0 )
9 years ago
{
9 years ago
if ( (tag= j64bits(json,"tag")) == 0 )
9 years ago
{
9 years ago
OS_randombytes((uint8_t *)&tag,sizeof(tag));
9 years ago
jadd64bits(json,"tag",tag);
}
9 years ago
if ( (timeout= juint(json,"timeout")) == 0 )
timeout = IGUANA_JSONTIMEOUT;
jsonstr = jprint(json,0);
9 years ago
if ( remoteaddr == 0 || jstr(json,"immediate") != 0 )
9 years ago
retjsonstr = SuperNET_jsonstr(myinfo,jsonstr,remoteaddr);
else retjsonstr = iguana_blockingjsonstr(myinfo,jsonstr,tag,timeout,remoteaddr);
if ( retjsonstr != 0 )
9 years ago
{
9 years ago
if ( (retjson= cJSON_Parse(retjsonstr)) != 0 )
{
jdelete(retjson,"tag");
jadd64bits(retjson,"tag",tag);
retstr = jprint(retjson,1);
//printf("retstr.(%s) retjsonstr.%p retjson.%p\n",retstr,retjsonstr,retjson);
free(retjsonstr);//,strlen(retjsonstr)+1);
} else retstr = retjsonstr;
9 years ago
}
9 years ago
free(jsonstr);
} else retstr = clonestr("{\"error\":\"cant parse JSON\"}");
if ( retstr == 0 )
retstr = clonestr("{\"error\":\"null return\"}");
return(retstr);
9 years ago
}
9 years ago
char *iguana_JSON(char *jsonstr)
9 years ago
{
9 years ago
char *retstr=0; cJSON *json;
if ( (json= cJSON_Parse(jsonstr)) != 0 )
9 years ago
{
9 years ago
retstr = SuperNET_JSON(0,json,"127.0.0.1");
free_json(json);
9 years ago
}
9 years ago
if ( retstr == 0 )
retstr = clonestr("{\"error\":\"cant parse jsonstr from pnacl\"}");
return(retstr);
9 years ago
}
9 years ago
char *SuperNET_p2p(struct iguana_info *coin,int32_t *delaymillisp,char *ipaddr,uint8_t *data,int32_t datalen)
9 years ago
{
9 years ago
cJSON *json,*retjson; char *agent,*method,*retstr = 0;
9 years ago
*delaymillisp = 0;
9 years ago
if ( (json= cJSON_Parse((char *)data)) != 0 )
9 years ago
{
9 years ago
printf("GOT >>>>>>>> SUPERNET P2P.(%s) from.%s\n",(char *)data,coin->symbol);
if ( (agent= jstr(json,"agent")) != 0 && (method= jstr(json,"method")) != 0 )
{
jaddstr(json,"fromp2p",coin->symbol);
if ( (retstr= SuperNET_JSON(0,json,ipaddr)) != 0 )
{
9 years ago
//printf("retstr.(%s)\n",retstr);
9 years ago
if ( (retjson= cJSON_Parse(retstr)) != 0 )
{
if ( jobj(retjson,"result") != 0 || jobj(retjson,"error") != 0 || jobj(retjson,"method") == 0 )
{
9 years ago
//printf("it is a result, dont return\n");
9 years ago
free(retstr);
retstr = 0;
9 years ago
} else *delaymillisp = (rand() % 1000);
9 years ago
free_json(retjson);
}
9 years ago
} else printf("null retstr from SuperNET_JSON\n");
9 years ago
}
free_json(json);
9 years ago
}
9 years ago
return(retstr);
9 years ago
}
#include <signal.h>
9 years ago
void sigint_func() { printf("SIGINT\n"); exit(0); }
void sigillegal_func() { printf("SIGILL\n"); exit(0); }
void sighangup_func() { printf("SIGHUP\n"); exit(0); }
void sigkill_func() { printf("SIGKILL\n"); exit(0); }
void sigabort_func() { printf("SIGABRT\n"); exit(0); }
void sigquit_func() { printf("SIGQUIT\n"); exit(0); }
void sigchild_func() { printf("SIGCHLD\n"); signal(SIGCHLD,sigchild_func); }
void sigalarm_func() { printf("SIGALRM\n"); signal(SIGALRM,sigalarm_func); }
void sigcontinue_func() { printf("SIGCONT\n"); signal(SIGCONT,sigcontinue_func); }
9 years ago
9 years ago
void iguana_main(void *arg)
9 years ago
{
9 years ago
struct supernet_info MYINFO; char helperstr[64],*helperargs,*coinargs=0,*secret,*jsonstr = arg;
int32_t i,len,flag; cJSON *json; uint8_t secretbuf[512];
9 years ago
signal(SIGINT,sigint_func);
signal(SIGILL,sigillegal_func);
signal(SIGHUP,sighangup_func);
signal(SIGKILL,sigkill_func);
signal(SIGABRT,sigabort_func);
signal(SIGQUIT,sigquit_func);
signal(SIGCHLD,sigchild_func);
signal(SIGALRM,sigalarm_func);
signal(SIGCONT,sigcontinue_func);
9 years ago
mycalloc(0,0,0);
iguana_initQ(&helperQ,"helperQ");
OS_ensure_directory("confs");
OS_ensure_directory("DB");
OS_ensure_directory("tmp");
memset(&MYINFO,0,sizeof(MYINFO));
if ( jsonstr != 0 && (json= cJSON_Parse(jsonstr)) != 0 )
9 years ago
{
9 years ago
if ( jobj(json,"numhelpers") != 0 )
IGUANA_NUMHELPERS = juint(json,"numhelpers");
if ( (secret= jstr(json,"secret")) != 0 )
9 years ago
{
9 years ago
len = (int32_t)strlen(secret);
if ( is_hexstr(secret,0) != 0 && len == 128 )
{
len >>= 1;
decode_hex(secretbuf,len,secret);
} else vcalc_sha256(0,secretbuf,(void *)secret,len), len = sizeof(bits256);
memcpy(MYINFO.privkey.bytes,secretbuf,sizeof(MYINFO.privkey));
9 years ago
}
9 years ago
if ( jobj(json,"coins") != 0 )
coinargs = jsonstr;
9 years ago
}
9 years ago
if ( IGUANA_NUMHELPERS == 0 )
IGUANA_NUMHELPERS = 1;
for (i=0; i<IGUANA_NUMHELPERS; i++)
9 years ago
{
9 years ago
sprintf(helperstr,"{\"name\":\"helper.%d\"}",i);
helperargs = clonestr(helperstr);
iguana_launch(iguana_coinadd("BTCD"),"iguana_helper",iguana_helper,helperargs,IGUANA_PERMTHREAD);
9 years ago
}
9 years ago
iguana_launch(iguana_coinadd("BTCD"),"rpcloop",iguana_rpcloop,iguana_coinadd("BTCD"),IGUANA_PERMTHREAD);
9 years ago
if ( coinargs != 0 )
iguana_launch(iguana_coinadd("BTCD"),"iguana_coins",iguana_coins,coinargs,IGUANA_PERMTHREAD);
9 years ago
else if ( 1 )
9 years ago
{
9 years ago
#ifdef __APPLE__
sleep(1);
char *str;
9 years ago
if ( (str= SuperNET_JSON(&MYINFO,cJSON_Parse("{\"agent\":\"iguana\",\"method\":\"addcoin\",\"services\":0,\"maxpeers\":2,\"activecoin\":\"BTCD\",\"active\":1}"),0)) != 0 )
9 years ago
{
9 years ago
printf("got.(%s)\n",str);
free(str);
}
#endif
9 years ago
}
9 years ago
if ( arg != 0 )
SuperNET_JSON(&MYINFO,cJSON_Parse(arg),0);
9 years ago
//#ifndef MINIGUANA
// iguana_launch(iguana_coinadd("BTCD"),"SuperNET_init",SuperNET_init,&MYINFO,IGUANA_PERMTHREAD);
//#endif
9 years ago
//init_InstantDEX();
9 years ago
while ( 1 )
{
9 years ago
flag = 0;
9 years ago
iguana_jsonQ();
9 years ago
if ( flag == 0 )
9 years ago
{
struct iguana_helper *ptr;
if ( (ptr= queue_dequeue(&bundlesQ,0)) != 0 )
{
if ( ptr->bp != 0 && ptr->coin != 0 )
9 years ago
flag += iguana_bundleiters(ptr->coin,ptr->bp,ptr->timelimit);
9 years ago
myfree(ptr,ptr->allocsize);
}
}
9 years ago
if ( flag == 0 )
9 years ago
sleep(1);
9 years ago
}
}