예제 #1
0
static int32_t process_json(char *retbuf,int32_t max,struct plugin_info *plugin,char *jsonargs,int32_t initflag)
{
    void *loadfile(uint64_t *allocsizep,char *fname);
    struct destbuf tokenstr,filename;
    char *myipaddr,*jsonstr = 0;
    cJSON *obj=0,*tmp,*json = 0;
    uint64_t allocsize,nxt64bits,tag = 0;
    int32_t retval = 0;
//printf("call process_json.(%s)\n",jsonargs);
    if ( jsonargs != 0 && (json= cJSON_Parse(jsonargs)) != 0 )
    {
        if ( is_cJSON_Array(json) != 0 && cJSON_GetArraySize(json) == 2 )
        {
            obj = cJSON_GetArrayItem(json,0);
            copy_cJSON(&tokenstr,cJSON_GetArrayItem(json,1));
        }
        else obj = json, tokenstr.buf[0] = 0;
        copy_cJSON(&filename,cJSON_GetObjectItem(obj,"filename"));
        if ( filename.buf[0] != 0 && (jsonstr= loadfile(&allocsize,filename.buf)) != 0 )
        {
            if ( (tmp= cJSON_Parse(jsonstr)) != 0 )
                obj = tmp;
            else free(jsonstr), jsonstr = 0;
        }
        if ( jsonstr == 0 )
            jsonstr = cJSON_Print(obj);
        stripwhite2(jsonstr,' ');
    }
    if ( obj != 0 )
    {
//printf("jsonargs.(%s)\n",jsonargs);
        tag = get_API_nxt64bits(cJSON_GetObjectItem(obj,"tag"));
        if ( initflag > 0 )
        {
            if ( (nxt64bits= get_API_nxt64bits(cJSON_GetObjectItem(obj,"NXT"))) != 0 )
            {
                plugin->nxt64bits = nxt64bits;
                expand_nxt64bits(plugin->NXTADDR,plugin->nxt64bits);
            }
            if ( (nxt64bits= get_API_nxt64bits(cJSON_GetObjectItem(obj,"serviceNXT"))) != 0 )
                expand_nxt64bits(plugin->SERVICENXT,nxt64bits);
            myipaddr = cJSON_str(cJSON_GetObjectItem(obj,"ipaddr"));
            if ( is_ipaddr(myipaddr) != 0 )
                strcpy(plugin->ipaddr,myipaddr);
            plugin->port = get_API_int(cJSON_GetObjectItem(obj,"port"),0);
        }
    }
    //fprintf(stderr,"tag.%llu initflag.%d got jsonargs.(%s) [%s] %p\n",(long long)tag,initflag,jsonargs,jsonstr,obj);
    if ( jsonstr != 0 && obj != 0 )
        retval = PLUGNAME(_process_json)(0,0,1,plugin,tag,retbuf,max,jsonstr,obj,initflag,tokenstr.buf);
    else printf("error with JSON.(%s)\n",jsonstr);//, getchar();
    //fprintf(stderr,"done tag.%llu initflag.%d got jsonargs.(%p) %p %p\n",(long long)tag,initflag,jsonargs,jsonstr,obj);
    if ( jsonstr != 0 )
        free(jsonstr);
    if ( json != 0 )
        free_json(json);
    printf("%s\n",retbuf), fflush(stdout);
    return(retval);
}
예제 #2
0
파일: rps777.c 프로젝트: apitests/libjl777
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 *jsonstr,cJSON *json,int32_t initflag,char *tokenstr)
{
    char *resultstr,*methodstr,*depositaddr,*retstr = 0; int32_t i; double pingmillis = 60000;
    retbuf[0] = 0;
    printf("<<<<<<<<<<<< INSIDE PLUGIN! process %s (%s)\n",plugin->name,jsonstr);
    if ( initflag > 0 )
    {
        RPS.readyflag = 1;
        plugin->allowremote = 1;
        copy_cJSON(RPS.name,jobj(json,"name"));
        txnet777_init(&RPS.TXNET,json,"rps","RPS",RPS.name,pingmillis);
        strcpy(plugin->NXTADDR,RPS.TXNET.ACCT.NXTADDR);
        if ( (depositaddr= jstr(json,"depositaddr")) == 0 )
            depositaddr = GATEWAY_ADDRESS;
        printf("fixed.%ld malleable.%ld tx.%ld\n",sizeof(struct txnet777_input),sizeof(struct txnet777_output),sizeof(struct txnet777));
        sprintf(retbuf,"{\"result\":\"initialized RPS\",\"pluginNXT\":\"%s\",\"serviceNXT\":\"%s\",\"depositaddr\":\"%s\"}",plugin->NXTADDR,plugin->SERVICENXT,depositaddr);
    }
    else
    {
        if ( plugin_result(retbuf,json,tag) > 0 )
            return((int32_t)strlen(retbuf));
        methodstr = jstr(json,"method");
        resultstr = jstr(json,"result");
        if ( methodstr == 0 || methodstr[0] == 0 )
        {
            printf("(%s) has not method\n",jsonstr);
            return(0);
        }
        else if ( resultstr != 0 && strcmp(resultstr,"registered") == 0 )
        {
            plugin->registered = 1;
            strcpy(retbuf,"{\"result\":\"activated\"}");
        }
        for (i=0; i<sizeof(PLUGNAME(_methods))/sizeof(*PLUGNAME(_methods)); i++)
            if ( strcmp(PLUGNAME(_methods)[i],methodstr) == 0 )
                retstr = (*PLUGNAME(_functions)[i])(retbuf,maxlen,&RPS.TXNET,json,jsonstr,tokenstr,forwarder,sender,valid);
    }
    return(plugin_copyretstr(retbuf,maxlen,retstr));
}
예제 #3
0
int32_t main
#endif
(int argc,const char *argv[])
{
    char *retbuf,*line,*jsonargs,*transportstr,registerbuf[MAX_JSON_FIELD];
    struct plugin_info *plugin; double startmilli; cJSON *argjson;
    int32_t max,sendflag,sleeptime=1,len = 0;
#ifndef BUNDLED
    portable_OS_init();
#endif
    milliseconds2();
    max = 1000000;
    retbuf = malloc(max+1);
    plugin = calloc(1,sizeof(*plugin));// + PLUGIN_EXTRASIZE);
    //plugin->extrasize = PLUGIN_EXTRASIZE;
    plugin->ppid = OS_getppid();
    strcpy(plugin->name,PLUGINSTR);
    if ( 0 )
    {
        int32_t i;
        for (i=0; i<argc; i++)
            printf("(%s) ",argv[i]);
        printf("argc.%d\n",argc);
    }
    //printf("%s (%s).argc%d parent PID.%d\n",plugin->name,argv[0],argc,plugin->ppid);
    plugin->timeout = 1;
    if ( argc <= 2 )
    {
        jsonargs = (argc > 1) ? stringifyM((char *)argv[1]) : stringifyM("{}");
        configure_plugin(retbuf,max,plugin,jsonargs,-1);
        free(jsonargs);
//fprintf(stderr,"PLUGIN_RETURNS.[%s]\n",retbuf), fflush(stdout);
        return(0);
    }
    randombytes((uint8_t *)&plugin->myid,sizeof(plugin->myid));
    plugin->permanentflag = atoi(argv[1]);
    plugin->daemonid = calc_nxt64bits(argv[2]);
#ifdef BUNDLED
    plugin->bundledflag = 1;
#endif
    transportstr = get_localtransport(plugin->bundledflag);
    sprintf(plugin->connectaddr,"%s://SuperNET.agents",transportstr);
    sprintf(plugin->bindaddr,"%s://%llu",transportstr,(long long)plugin->daemonid);
    jsonargs = (argc >= 3) ? ((char *)argv[3]) : 0;
    configure_plugin(retbuf,max,plugin,jsonargs,1);
    printf("CONFIGURED.(%s) argc.%d: %s myid.%llu daemonid.%llu NXT.%s serviceNXT.%s\n",plugin->name,argc,plugin->permanentflag != 0 ? "PERMANENT" : "WEBSOCKET",(long long)plugin->myid,(long long)plugin->daemonid,plugin->NXTADDR,plugin->SERVICENXT);//,jsonargs!=0?jsonargs:"");
    if ( init_pluginsocks(plugin,plugin->permanentflag,plugin->myid,plugin->daemonid,plugin->timeout) == 0 )
    {
        argjson = cJSON_Parse(jsonargs);
        if ( (len= registerAPI(registerbuf,sizeof(registerbuf)-1,plugin,argjson)) > 0 )
        {
            //fprintf(stderr,">>>>>>>>>>>>>>> plugin.(%s) sends REGISTER SEND.(%s)\n",plugin->name,registerbuf);
            nn_local_broadcast2(plugin->pushsock,0,0,(uint8_t *)registerbuf,(int32_t)strlen(registerbuf)+1), plugin->numsent++;
        } else printf("error register API\n");
        if ( argjson != 0 )
            free_json(argjson);
    } else printf("error init_pluginsocks\n");
    while ( OS_getppid() == plugin->ppid )
    {
        retbuf[0] = 0;
        if ( (len= nn_recv(plugin->pullsock,&line,NN_MSG,0)) > 0 )
        {
            len = (int32_t)strlen(line);
            //printf("(s%d r%d) <<<<<<<<<<<<<< %s.RECEIVED (%s).%d -> bind.(%s) connect.(%s) %s\n",plugin->numsent,plugin->numrecv,plugin->name,line,len,plugin->bindaddr,plugin->connectaddr,plugin->permanentflag != 0 ? "PERMANENT" : "WEBSOCKET"), fflush(stdout);
            if ( (len= process_plugin_json(retbuf,max,&sendflag,plugin,plugin->permanentflag,plugin->daemonid,plugin->myid,line)) > 0 )
            {
                if ( plugin->bundledflag == 0 )
                    printf("%s\n",retbuf), fflush(stdout);
                if ( sendflag != 0 )
                {
                    nn_local_broadcast2(plugin->pushsock,0,0,(uint8_t *)retbuf,(int32_t)strlen(retbuf)+1), plugin->numsent++;
                    //fprintf(stderr,">>>>>>>>>>>>>> returned.(%s)\n",retbuf);
                }
            } //else printf("null return from process_plugin_json\n");
            nn_freemsg(line);
        }
        else
        {
            startmilli = milliseconds2();
            if ( PLUGNAME(_idle)(plugin) == 0 )
            {
                if ( plugin->sleepmillis != 0 )
                {
                    sleeptime = plugin->sleepmillis - (milliseconds2() - startmilli);
                    //printf("%s sleepmillis.%d sleeptime.%d\n",plugin->name,plugin->sleepmillis,sleeptime);
                    if ( sleeptime > 0 )
                        msleep2(sleeptime);
                }
            }
        }
    } fprintf(stderr,"ppid.%d changed to %d\n",plugin->ppid,OS_getppid());
    PLUGNAME(_shutdown)(plugin,len); // rc == 0 -> parent process died
    nn_shutdown(plugin->pushsock,0);
    if ( plugin->pushsock != plugin->pullsock )
        nn_shutdown(plugin->pullsock,0);
    free(plugin);
    return(len);
}
예제 #4
0
static int32_t process_plugin_json(char *retbuf,int32_t max,int32_t *sendflagp,struct plugin_info *plugin,int32_t permanentflag,uint64_t daemonid,uint64_t myid,char *jsonstr)
{
    int32_t valid = -11,len = (int32_t)strlen(jsonstr); cJSON *json,*obj,*tokenobj; uint64_t tag = 0;
    struct destbuf name,destname,forwarder,tokenstr,sender;
    retbuf[0] = *sendflagp = 0;
    //printf("PLUGIN.(%s) process_plugin_json (%s)\n",plugin->name,jsonstr);
    if ( (json= cJSON_Parse(jsonstr)) != 0 )
    {
        if ( is_cJSON_Array(json) != 0 )
        {
            obj = cJSON_GetArrayItem(json,0);
            jsonstr = cJSON_Print(obj), stripwhite2(jsonstr,' ');
            tokenobj = cJSON_GetArrayItem(json,1), stripwhite2(tokenstr.buf,' ');
            copy_cJSON(&tokenstr,tokenobj);
            copy_cJSON(&forwarder,cJSON_GetObjectItem(tokenobj,"forwarder"));
            copy_cJSON(&sender,cJSON_GetObjectItem(tokenobj,"sender"));
            valid = get_API_int(cJSON_GetObjectItem(tokenobj,"valid"),valid);
        }
        else obj = json, tokenstr.buf[0] = forwarder.buf[0] = sender.buf[0] = 0;
        copy_cJSON(&name,cJSON_GetObjectItem(obj,"plugin"));
        if ( name.buf[0] == 0 )
            copy_cJSON(&name,cJSON_GetObjectItem(obj,"agent"));
        copy_cJSON(&destname,cJSON_GetObjectItem(obj,"destplugin"));
        if ( destname.buf[0] == 0 )
            copy_cJSON(&destname,cJSON_GetObjectItem(obj,"destagent"));
        tag = get_API_nxt64bits(cJSON_GetObjectItem(obj,"tag"));
        if ( (strcmp(name.buf,plugin->name) == 0 || strcmp(destname.buf,plugin->name) == 0) && (len= PLUGNAME(_process_json)(forwarder.buf,sender.buf,valid,plugin,tag,retbuf,max,jsonstr,obj,0,tokenstr.buf)) > 0 )
        {
            *sendflagp = 1;
            if ( retbuf[0] == 0 )
                sprintf(retbuf,"{\"result\":\"no response\"}");
            append_stdfields(retbuf,max,plugin,tag,0);
            return((int32_t)strlen(retbuf));
        } //else printf("(%s) -> no return.%d (%s) vs (%s):(%s) len.%d\n",jsonstr,strcmp(name,plugin->name),name,destname,plugin->name,len);
    }
    else
    {
printf("process_plugin_json: couldnt parse.(%s)\n",jsonstr);
        if ( jsonstr[len-1] == '\r' || jsonstr[len-1] == '\n' || jsonstr[len-1] == '\t' || jsonstr[len-1] == ' ' )
            jsonstr[--len] = 0;
        sprintf(retbuf,"{\"result\":\"unparseable\",\"message\":\"%s\"}",jsonstr);
    }
    if ( *sendflagp != 0 && retbuf[0] != 0 )
        append_stdfields(retbuf,max,plugin,tag,0);
    else retbuf[0] = *sendflagp = 0;
    return((int32_t)strlen(retbuf));
}
예제 #5
0
static int32_t registerAPI(char *retbuf,int32_t max,struct plugin_info *plugin,cJSON *argjson)
{
    cJSON *json,*array;
    char *jsonstr;
    int32_t i;
    uint64_t disableflags = 0;
    json = cJSON_CreateObject();
    retbuf[0] = 0;
    disableflags = PLUGNAME(_register)(plugin,(void *)plugin->pluginspace,argjson);
    array = cJSON_CreateArray();
    for (i=0; i<(sizeof(PLUGNAME(_methods))/sizeof(*PLUGNAME(_methods))); i++)
    {
        if ( PLUGNAME(_methods)[i] == 0 || PLUGNAME(_methods)[i][0] == 0 )
            break;
        if ( ((1LL << i) & disableflags) == 0 )
            cJSON_AddItemToArray(array,cJSON_CreateString(PLUGNAME(_methods)[i]));
    }
    cJSON_AddItemToObject(json,"methods",array);
    array = cJSON_CreateArray();
    for (i=0; i<(sizeof(PLUGNAME(_pubmethods))/sizeof(*PLUGNAME(_pubmethods))); i++)
    {
        if ( PLUGNAME(_pubmethods)[i] == 0 || PLUGNAME(_pubmethods)[i][0] == 0 )
            break;
        if ( ((1LL << i) & disableflags) == 0 )
            cJSON_AddItemToArray(array,cJSON_CreateString(PLUGNAME(_pubmethods)[i]));
    }
    cJSON_AddItemToObject(json,"pubmethods",array);
    array = cJSON_CreateArray();
    for (i=0; i<(sizeof(PLUGNAME(_authmethods))/sizeof(*PLUGNAME(_authmethods))); i++)
    {
        if ( PLUGNAME(_authmethods)[i] == 0 || PLUGNAME(_authmethods)[i][0] == 0 )
            break;
        if ( ((1LL << i) & disableflags) == 0 )
            cJSON_AddItemToArray(array,cJSON_CreateString(PLUGNAME(_authmethods)[i]));
    }
    cJSON_AddItemToObject(json,"authmethods",array);
    cJSON_AddItemToObject(json,"pluginrequest",cJSON_CreateString("SuperNET"));
    cJSON_AddItemToObject(json,"requestType",cJSON_CreateString("register"));
    if ( plugin->sleepmillis == 0 )
        plugin->sleepmillis = get_API_int(cJSON_GetObjectItem(json,"sleepmillis"),25);//SUPERNET.APISLEEP);
    cJSON_AddItemToObject(json,"sleepmillis",cJSON_CreateNumber(plugin->sleepmillis));
    if ( cJSON_GetObjectItem(json,"NXT") == 0 )
        cJSON_AddItemToObject(json,"NXT",cJSON_CreateString(plugin->NXTADDR));
    else cJSON_ReplaceItemInObject(json,"NXT",cJSON_CreateString(plugin->NXTADDR));
    if ( cJSON_GetObjectItem(json,"serviceNXT") == 0 )
        cJSON_AddItemToObject(json,"serviceNXT",cJSON_CreateString(plugin->SERVICENXT));
    else cJSON_ReplaceItemInObject(json,"serviceNXT",cJSON_CreateString(plugin->SERVICENXT));
    jsonstr = cJSON_Print(json), free_json(json);
    stripwhite2(jsonstr,' ');
    strcpy(retbuf,jsonstr), free(jsonstr);
    append_stdfields(retbuf,max,plugin,0,1);
   //printf(">>>>>>>>>>> register return.(%s)\n",retbuf);
    return((int32_t)strlen(retbuf) + (retbuf[0] != 0));
}