void idle()
{
    char *jsonstr,*str; cJSON *json; int32_t n = 0; uint32_t nonce;
    while ( INSTANTDEX.readyflag == 0 )
        sleep(1);
    while ( 1 )
    {
        if ( n == 0 )
            msleep(1000);
        n = 0;
        if ( (jsonstr= queue_dequeue(&InstantDEXQ,1)) != 0 )
        {
            if ( (json= cJSON_Parse(jsonstr)) != 0 )
            {
                //printf("Dequeued InstantDEX.(%s)\n",jsonstr);
                //fprintf(stderr,"dequeued\n");
                if ( (str= busdata_sync(&nonce,jsonstr,"allnodes",0)) != 0 )
                {
                    //fprintf(stderr,"busdata.(%s)\n",str);
                    free(str);
                }
                free_json(json);
                n++;
            } else printf("error parsing (%s) from InstantDEXQ\n",jsonstr);
            free_queueitem(jsonstr);
        }
    }
}
Пример #2
0
void calc_nonces(char *destpoint)
{
    char buf[8192],*str; int32_t n = 0; double endmilli = milliseconds() + 60000;
    //printf("calc_nonces.(%s)\n",destpoint);
    memset(SUPERNET.nonces,0,sizeof(SUPERNET.nonces));
    SUPERNET.numnonces = 0;
    while ( milliseconds() < endmilli && n < sizeof(SUPERNET.nonces)/sizeof(*SUPERNET.nonces) )
    {
        sprintf(buf,"{\"plugin\":\"relay\",\"counter\":\"%d\",\"destplugin\":\"relay\",\"method\":\"nonce\",\"broadcast\":\"8\",\"lbendpoint\":\"%s\",\"relaypoint\":\"%s\",\"globalpoint\":\"%s\",\"destpoint\":\"%s\",\"NXT\":\"%s\"}",n,SUPERNET.lbendpoint,SUPERNET.relayendpoint,SUPERNET.globalendpoint,destpoint,SUPERNET.NXTADDR);
        if ( (str= busdata_sync(&SUPERNET.nonces[n],buf,"8",0)) != 0 )
        {
            //fprintf(stderr,"send.(%s)\n",buf);
            free(str);
            n++;
        }
    }
    SUPERNET.numnonces = n;
    SUPERNET.noncing = 0;
    printf("finished noncing for (%s)\n",destpoint);
    free(destpoint);
}
Пример #3
0
char *process_user_json(char *plugin,char *method,char *cmdstr,int32_t broadcastflag,int32_t timeout)
{
    struct daemon_info *find_daemoninfo(int32_t *indp,char *name,uint64_t daemonid,uint64_t instanceid);
    uint32_t nonce; int32_t tmp,len; char *retstr;//,tokenized[8192];
    len = (int32_t)strlen(cmdstr) + 1;
//printf("userjson.(%s).%d plugin.(%s) broadcastflag.%d method.(%s)\n",cmdstr,len,plugin,broadcastflag,method);
    if ( broadcastflag != 0 || strcmp(plugin,"relay") == 0 )
    {
        if ( strcmp(method,"busdata") == 0 )
            retstr = busdata_sync(&nonce,cmdstr,broadcastflag==0?0:"allnodes",0);
        else retstr = clonestr("{\"error\":\"direct load balanced calls deprecated, use busdata\"}");
    }
    //else if ( strcmp(plugin,"peers") == 0 )
    //    retstr = nn_allrelays((uint8_t *)cmdstr,len,timeout,0);
    else if ( find_daemoninfo(&tmp,plugin,0,0) != 0 )
    {
        //len = construct_tokenized_req(tokenized,cmdstr,SUPERNET.NXTACCTSECRET,broadcastflag!=0?"allnodes":0);
        //printf("console.(%s)\n",tokenized);
        retstr = plugin_method(-1,0,1,plugin,method,0,0,cmdstr,len,timeout != 0 ? timeout : 0,0);
    }
    else retstr = clonestr("{\"error\":\"invalid command\"}");
    return(retstr);
}
Пример #4
0
int32_t PLUGNAME(_process_json)(char *forwarder,char *sender,int32_t valid,struct plugin_info *plugin,uint64_t tag,char *retbuf,int32_t maxlen,char *origjsonstr,cJSON *origjson,int32_t initflag,char *tokenstr)
{
    char *resultstr,*retstr = 0,*methodstr,*jsonstr,*destplugin,*submethod; struct destbuf tagstr,endpoint;
    cJSON *retjson,*json,*tokenobj; uint32_t nonce;
    struct applicant_info apply;
    retbuf[0] = 0;
    if ( tokenstr == 0 )
        tokenstr = "";
    if ( is_cJSON_Array(origjson) != 0 && cJSON_GetArraySize(origjson) == 2 )
        json = cJSON_GetArrayItem(origjson,0), jsonstr = cJSON_Print(json), _stripwhite(jsonstr,' ');
    else json = origjson, jsonstr = origjsonstr;
    if ( Debuglevel > 2 )
        printf("<<<<<<<<<<<< INSIDE relays PLUGIN! process %s [(%s).(%s)]\n",plugin->name,jsonstr,tokenstr);
    if ( initflag > 0 )
    {
        // configure settings
        RELAYS.readyflag = 1;
        plugin->allowremote = 1;
        strcpy(retbuf,"{\"result\":\"initflag > 0\"}");
    }
    else
    {
        if ( plugin_result(retbuf,json,tag) > 0 )
            return((int32_t)strlen(retbuf));
        resultstr = cJSON_str(cJSON_GetObjectItem(json,"result"));
        methodstr = cJSON_str(cJSON_GetObjectItem(json,"method"));
        destplugin = cJSON_str(cJSON_GetObjectItem(json,"destplugin"));
        submethod = cJSON_str(cJSON_GetObjectItem(json,"submethod"));
        if ( methodstr == 0 || methodstr[0] == 0 )
        {
            printf("(%s) has not method\n",jsonstr);
            return(0);
        }
        //fprintf(stderr,"RELAYS methodstr.(%s) (%s)\n",methodstr,jsonstr);
        if ( resultstr != 0 && strcmp(resultstr,"registered") == 0 )
        {
            plugin->registered = 1;
            strcpy(retbuf,"{\"result\":\"activated\"}");
        }
#ifdef INSIDE_MGW
        else if ( strcmp(methodstr,"msigaddr") == 0 )
        {
            char *devMGW_command(char *jsonstr,cJSON *json);
            if ( SUPERNET.gatewayid >= 0 )
                retstr = devMGW_command(jsonstr,json);
        }
#endif
        else
        {
            strcpy(retbuf,"{\"result\":\"relay command under construction\"}");
            if ( strcmp(methodstr,"list") == 0 )
                retstr = relays_jsonstr(jsonstr,json);
            else if ( strcmp(methodstr,"telepathy") == 0 )
            {
                sprintf(retbuf,"%s",jsonstr);
            }
            else if ( strcmp(methodstr,"busdata") == 0 )
            {
                retstr = busdata_sync(&nonce,jsonstr,cJSON_str(cJSON_GetObjectItem(json,"broadcast")),0);
                // {"plugin":"relay","method":"busdata","destplugin":"relay","submethod":"join","broadcast":"join","endpoint":""}
            }
            else if ( strcmp(methodstr,"allservices") == 0 )
            {
                if ( (retjson= serviceprovider_json()) != 0 )
                {
                    retstr = cJSON_Print(retjson), _stripwhite(retstr,' ');
                    free_json(retjson);
                    //printf("got.(%s)\n",retstr);
                } else printf("null serviceprovider_json()\n");
            }
            else if ( strcmp(methodstr,"protocol") == 0 || strcmp(methodstr,"allprotocols") == 0 )
            {
                if ( strcmp(methodstr,"protocol") == 0 && valid > 0 )
                    protocols_register(sender,jstr(json,"protocol"),jstr(json,"endpoint"),jint(json,"disconnect"));
                if ( (retjson= protocols_json(jstr(json,"protocol"))) != 0 )
                {
                    retstr = cJSON_Print(retjson), _stripwhite(retstr,' ');
                    free_json(retjson);
                } else printf("null protocols_json()\n");
            }
            else if ( strcmp(methodstr,"join") == 0 )
            {
                if ( SUPERNET.noncing == 0 )
                {
                    copy_cJSON(&tagstr,cJSON_GetObjectItem(json,"tag"));
                    copy_cJSON(&endpoint,cJSON_GetObjectItem(json,"endpoint"));
                    SUPERNET.noncing = 1;
                    if ( SUPERNET.iamrelay != 0 )
                    {
                        portable_thread_create((void *)calc_nonces,clonestr(endpoint.buf));
                        sprintf(retbuf,"{\"result\":\"noncing\",\"endpoint\":\"%s\"}",endpoint.buf);
                    }
                    //fprintf(stderr,"join or nonce.(%s)\n",retbuf);
                }
            }
            else if ( strcmp(methodstr,"nonce") == 0 )
            {
                struct destbuf endpoint,sender,destpoint,relaypoint,globalpoint,noncestr;
                memset(&apply,0,sizeof(apply));
                copy_cJSON(&destpoint,cJSON_GetObjectItem(json,"destpoint"));
                copy_cJSON(&endpoint,cJSON_GetObjectItem(json,"lbendpoint"));
                copy_cJSON(&relaypoint,cJSON_GetObjectItem(json,"relaypoint"));
                copy_cJSON(&globalpoint,cJSON_GetObjectItem(json,"globalpoint"));
                copy_cJSON(&sender,cJSON_GetObjectItem(json,"NXT"));
                if ( SUPERNET.noncing != 0 && strcmp(SUPERNET.lbendpoint,destpoint.buf) == 0 )
                {
                    if ( endpoint.buf[0] != 0 && tokenstr[0] != 0 && (tokenobj= cJSON_Parse(tokenstr)) != 0 )
                    {
                        strcpy(apply.lbendpoint,endpoint.buf);
                        strcpy(apply.relayendpoint,relaypoint.buf);
                        strcpy(apply.globalendpoint,globalpoint.buf);
                        apply.senderbits = calc_nxt64bits(sender.buf);
                        copy_cJSON(&noncestr,cJSON_GetObjectItem(tokenobj,"nonce"));
                        if ( noncestr.buf[0] != 0 )
                            apply.nonce = (uint32_t)calc_nxt64bits(noncestr.buf);
                        //printf("tokenobj.(%s) -> nonce.%u\n",tokenstr,apply.nonce);
                        free_json(tokenobj);
                        recv_nonces(&apply);
                    }
                }
            }
        }
    }
    return(plugin_copyretstr(retbuf,maxlen,retstr));
}