struct basilisk_request *basilisk_parsejson(struct basilisk_request *rp,cJSON *reqjson) { uint32_t requestid,quoteid; memset(rp,0,sizeof(*rp)); rp->hash = jbits256(reqjson,"hash"); rp->desthash = jbits256(reqjson,"desthash"); rp->srcamount = j64bits(reqjson,"srcamount"); rp->minamount = j64bits(reqjson,"minamount"); rp->destamount = j64bits(reqjson,"destamount"); requestid = juint(reqjson,"requestid"); quoteid = juint(reqjson,"quoteid"); if ( jstr(reqjson,"relay") != 0 ) rp->relaybits = (uint32_t)calc_ipbits(jstr(reqjson,"relay")); rp->timestamp = juint(reqjson,"timestamp"); rp->quotetime = juint(reqjson,"quotetime"); safecopy(rp->src,jstr(reqjson,"src"),sizeof(rp->src)); safecopy(rp->dest,jstr(reqjson,"dest"),sizeof(rp->dest)); if ( quoteid != 0 ) { rp->quoteid = basilisk_quoteid(rp); if ( quoteid != rp->quoteid ) printf("basilisk_parsejson quoteid.%u != %u error\n",quoteid,rp->quoteid); } rp->requestid = basilisk_requestid(rp); if ( requestid != rp->requestid ) printf("basilisk_parsejson requestid.%u != %u error\n",requestid,rp->requestid); return(rp); }
static void print_option(char *line, int type, OPT_UNION *val) { char text[STRLEN]; if(!val) { safecopy(text, " -- "); } else { switch(type) { case TYPE_FLAG: case TYPE_INT: sprintf(text, "%10d", val->i_val); break; case TYPE_LINGER: sprintf(text, "%d:%-8d", val->linger_val.l_onoff, val->linger_val.l_linger); break; case TYPE_TIMEVAL: sprintf(text, "%6d:%-3d", (int)val->timeval_val.tv_sec, (int)val->timeval_val.tv_usec); break; case TYPE_STRING: sprintf(text, "%10s", val->c_val); break; default: safecopy(text, " Ooops? "); /* Internal error? */ } } safeconcat(line, text); }
static void parse_cmdline(LPSTR command_line) { char line[STRLEN], *c, *opt; safecopy(line, command_line); memset(&cmdline, 0, sizeof(cmdline)); c=line; while(*c && (*c=='/' || *c=='-')) { opt=c; while(*c && !isspace(*c)) /* skip non-whitespaces */ c++; while(*c && isspace(*c)) /* replace whitespaces with '\0' */ *c++='\0'; if(!strcasecmp(opt+1, "install")) cmdline.install=1; else if(!strcasecmp(opt+1, "uninstall")) cmdline.uninstall=1; else if(!strcasecmp(opt+1, "start")) cmdline.start=1; else if(!strcasecmp(opt+1, "stop")) cmdline.stop=1; else if(!strcasecmp(opt+1, "service")) cmdline.service=1; else if(!strcasecmp(opt+1, "quiet")) cmdline.quiet=1; else { /* option to be processed in options.c */ safecopy(cmdline.config_file, opt); return; /* no need to parse other options */ } } safecopy(cmdline.config_file, c); }
int main(int argc, char *argv[]) { char from[] = "0123456789"; int from_len = sizeof(from); char to[] = "0123456"; int to_len = sizeof(to); debug("Copying %s: %d to %s: %d.", from, from_len, to, to_len); int rc = safecopy(from_len, from, to_len, to); check(rc > 0, "Failed to safecopy."); check(to[to_len - 1] == '\0', "String not terminated."); debug("Result is: %s: %d.", to, to_len); // rc = safecopy(from_len * -1, from, to_len, to); check(rc == -1, "safecopy should fail #1."); printf("rc: %d\n", rc); check(to[to_len - 1] == '\0', "String not terminated."); rc = safecopy(from_len, from, 0, to); check(rc == -1, "safecopy should failed #2."); check(to[to_len - 1] == '\0', "String not terminated."); return 0; error: return 1; }
STRING_ARG(iguana,addcoin,newcoin) { char *symbol,*seedip; int32_t retval; if ( (symbol= newcoin) == 0 && coin != 0 ) symbol = coin->symbol; if ( symbol != 0 ) { if ( (seedip= jstr(json,"seedipaddr")) != 0 ) safecopy(myinfo->seedipaddr,seedip,sizeof(myinfo->seedipaddr)); printf(">> addcoin.%s seedipaddr.%s\n",symbol,myinfo->seedipaddr); #ifdef __PNACL__ // if ( strcmp(symbol,"BTC") == 0 ) // return(clonestr("{\"result\":\"BTC for chrome app is not yet\"}")); #endif if ( (retval= iguana_launchcoin(myinfo,symbol,json,0)) > 0 ) { if ( myinfo->rpcsymbol[0] == 0 ) safecopy(myinfo->rpcsymbol,symbol,sizeof(myinfo->rpcsymbol)); return(clonestr("{\"result\":\"coin added\"}")); } else if ( retval == 0 ) return(clonestr("{\"result\":\"coin already there\"}")); else return(clonestr("{\"error\":\"error adding coin\"}")); } else return(clonestr("{\"error\":\"addcoin needs newcoin\"}")); }
Image<T> concatArray(const Image<T> arr[], const int nbarr, const int Nx, const int destX, const int destY) { GVX_TRACE(__PRETTY_FUNCTION__); int width, height; // check if we are going to reshape: if (destX <= 0 || destY <= 0) { width = arr[0].getWidth(); height = arr[0].getHeight(); } else { width = destX; height = destY; } int tw = width * Nx; int th = height * (int)ceil(((double)nbarr) / ((double)Nx)); Image<T> result(tw, th, NO_INIT); T zeros[width]; for (int i = 0; i < width; ++i) zeros[i] = T(); int arrnb = 0; for (int j = 0; j < th; j += height) for (int i = 0; i < tw; i += width) { if (destX <= 0 || destY <= 0) // no reshaping { if (arrnb < nbarr) // copy image { ASSERT(arr[arrnb].getWidth() == width && arr[arrnb].getHeight() == height); for (int jj = 0; jj < height; jj ++) safecopy(result.beginw() + (i + (j + jj) * tw), arr[arrnb].begin() + (jj * width), width); } else // put blanks at the end for (int jj = 0; jj < height; jj ++) safecopy(result.beginw() + (i + (j + jj) * tw), zeros, width); } else // reshape first { if (arrnb < nbarr) // copy image { Image<T> tmp = rescaleBilinear(arr[arrnb], width, height); for (int jj = 0; jj < height; jj ++) safecopy(result.beginw() + (i + (j + jj) * tw), tmp.begin() + (jj * width), width); } else // put blanks at the end for (int jj = 0; jj < height; jj ++) safecopy(result.beginw() + (i + (j + jj) * tw), zeros, width); } arrnb ++; } return result; }
void process_pNXT_typematch(struct pNXT_info *dp,struct NXT_protocol_parms *parms) { char NXTaddr[64],*sender,*receiver,*txid; sender = parms->sender; receiver = parms->receiver; txid = parms->txid; safecopy(NXTaddr,sender,sizeof(NXTaddr)); printf("got txid.(%s) type.%d subtype.%d sender.(%s) -> (%s)\n",txid,parms->type,parms->subtype,sender,receiver); }
char *telepod_func(char *sender,int32_t valid,cJSON **objs,int32_t numobjs,char *origargstr) { uint64_t satoshis; struct coin_info *cp; uint32_t crc,ind,height,vout,totalcrc,sharei,M,N; char NXTACCTSECRET[1024],coinstr[512],coinaddr[512],otherpubaddr[512],txid[512],pubkey[512],privkey[2048],privkeyhex[2048],*retstr = 0; copy_cJSON(NXTACCTSECRET,objs[1]); if ( NXTACCTSECRET[0] == 0 && (cp= get_coin_info("BTCD")) != 0 ) safecopy(NXTACCTSECRET,cp->NXTACCTSECRET,sizeof(NXTACCTSECRET)); crc = get_API_uint(objs[2],0); ind = get_API_uint(objs[3],0); height = get_API_uint(objs[4],0); copy_cJSON(coinstr,objs[5]); satoshis = SATOSHIDEN * get_API_float(objs[6]); copy_cJSON(coinaddr,objs[7]); copy_cJSON(txid,objs[8]); vout = get_API_uint(objs[9],0); copy_cJSON(pubkey,objs[10]); copy_cJSON(privkeyhex,objs[11]); decode_hex((unsigned char *)privkey,(int32_t)strlen(privkeyhex)/2,privkeyhex); privkey[strlen(privkeyhex)/2] = 0; totalcrc = get_API_uint(objs[12],0); sharei = get_API_uint(objs[13],0); M = get_API_uint(objs[14],1); N = get_API_uint(objs[15],1); copy_cJSON(otherpubaddr,objs[16]); if ( coinstr[0] != 0 && sender[0] != 0 && valid != 0 ) retstr = telepod_received(sender,NXTACCTSECRET,coinstr,crc,ind,height,satoshis,coinaddr,txid,vout,pubkey,privkey,totalcrc,sharei,M,N,otherpubaddr); else retstr = clonestr("{\"error\":\"invalid telepod received\"}"); return(retstr); }
int32_t PAX_ecbparse(char *date,double *prices,char *url,int32_t basenum) { char *jsonstr,*relstr,*basestr; int32_t count=0,i,relnum; cJSON *json,*ratesobj,*item; struct destbuf tmp; if ( (jsonstr= issue_curl(url)) != 0 ) { if ( Debuglevel > 2 ) printf("(%s)\n",jsonstr); if ( (json= cJSON_Parse(jsonstr)) != 0 ) { copy_cJSON(&tmp,jobj(json,"date")), safecopy(date,tmp.buf,64); if ( (basestr= jstr(json,"base")) != 0 && strcmp(basestr,CURRENCIES[basenum]) == 0 && (ratesobj= jobj(json,"rates")) != 0 && (item= ratesobj->child) != 0 ) { while ( item != 0 ) { if ( (relstr= get_cJSON_fieldname(item)) != 0 && (relnum= PAX_basenum(relstr)) >= 0 ) { i = basenum*MAX_CURRENCIES + relnum; prices[i] = item->valuedouble; //if ( basenum == JPYNUM ) // prices[i] *= 100.; // else if ( relnum == JPYNUM ) // prices[i] /= 100.; count++; if ( Debuglevel > 2 ) printf("(%02d:%02d %f) ",basenum,relnum,prices[i]); } else printf("cant find.(%s)\n",relstr);//, getchar(); item = item->next; } } free_json(json); } free(jsonstr); } return(count); }
uint64_t bitcoin_parseunspent(struct iguana_info *coin,struct bitcoin_unspent *unspent,double minconfirms,char *account,cJSON *item) { char *hexstr,coinaddr[64]; memset(unspent,0,sizeof(*unspent)); if ( jstr(item,"address") != 0 ) { safecopy(coinaddr,jstr(item,"address"),sizeof(coinaddr)); bitcoin_addr2rmd160(&unspent->addrtype,unspent->rmd160,coinaddr); /*sprintf(args,"[\"%s\"]",coinaddr); wifstr = bitcoind_RPC(0,coin->symbol,coin->chain->serverport,coin->chain->userpass,"dumpprivkey",args); if ( wifstr != 0 ) { bitcoin_wif2priv(&addrtype,&unspent->privkeys[0],wifstr); //printf("wifstr.(%s) -> %s\n",wifstr,bits256_str(str,unspent->privkeys[0])); free(wifstr); } else fprintf(stderr,"error (%s) cant find privkey\n",coinaddr);*/ } if ( (account == 0 || jstr(item,"account") == 0 || strcmp(account,jstr(item,"account")) == 0) && (minconfirms <= 0 || juint(item,"confirmations") >= minconfirms-SMALLVAL) ) { if ( (hexstr= jstr(item,"scriptPubKey")) != 0 ) { unspent->spendlen = (int32_t)strlen(hexstr) >> 1; if ( unspent->spendlen < sizeof(unspent->spendscript) ) decode_hex(unspent->spendscript,unspent->spendlen,hexstr); } unspent->txid = jbits256(item,"txid"); unspent->value = SATOSHIDEN * jdouble(item,"amount"); unspent->vout = jint(item,"vout"); //char str[65]; printf("(%s) -> %s %.8f scriptlen.%d\n",jprint(item,0),bits256_str(str,unspent->txid),dstr(unspent->value),unspent->scriptlen); } else printf("skip.(%s) minconfirms.%f\n",jprint(item,0),minconfirms);
static int service_install(LPSTR command_line) { SC_HANDLE scm, service; char exe_file_name[STRLEN], service_path[STRLEN]; scm=OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE); if(!scm) { error_box(TEXT("OpenSCManager")); return 1; } GetModuleFileName(0, exe_file_name, STRLEN); safecopy(service_path, "\""); safeconcat(service_path, exe_file_name); safeconcat(service_path, "\" -service "); safeconcat(service_path, command_line); service=CreateService(scm, options.win32_service, options.win32_service, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, service_path, NULL, NULL, NULL, NULL, NULL); if(!service) { error_box(TEXT("CreateService")); CloseServiceHandle(scm); return 1; } if(!cmdline.quiet) MessageBox(hwnd, TEXT("Service installed"), win32_name, MB_ICONINFORMATION); CloseServiceHandle(service); CloseServiceHandle(scm); return 0; }
char *iguana_blockingjsonstr(struct supernet_info *myinfo,char *jsonstr,uint64_t tag,int32_t maxmillis,char *remoteaddr) { 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 ) { usleep(100); if ( retjsonstr != 0 ) { //printf("got blocking retjsonstr.(%s) delete allocsize.%d:%d\n",retjsonstr,allocsize,ptr->allocsize); queue_delete(&finishedQ,&ptr->DL,ptr->allocsize,1); return(retjsonstr); } usleep(1000); } //printf("(%s) expired\n",jsonstr); ptr->expired = (uint32_t)time(NULL); return(clonestr("{\"error\":\"iguana jsonstr expired\"}")); }
Image<T> decY(const Image<T>& src, const int factor) { GVX_TRACE(__PRETTY_FUNCTION__); ASSERT(factor > 0); if (src.getHeight() <= 1) return src; // do not go smaller than 1 pixel high const int w = src.getWidth(); int h2 = src.getHeight() / factor; if (h2 == 0) h2 = 1; Image<T> result(w, h2, NO_INIT); typename Image<T>::const_iterator sptr = src.begin(); typename Image<T>::iterator dptr = result.beginw(); int skip = w * factor; for (int j = 0; j < h2; ++j) { safecopy(dptr, sptr, w); dptr += w; sptr += skip; } return result; }
Image<T> intXY(const Image<T>& src, const bool dupli) { GVX_TRACE(__PRETTY_FUNCTION__); ASSERT(src.initialized()); // works only if there is an image const int w = src.getWidth(); const int h = src.getHeight(); const int w2 = w * 2; const int h2 = h * 2; Image<T> result(w2, h2, NO_INIT); typename Image<T>::const_iterator sptr = src.begin(); typename Image<T>::iterator dptr = result.beginw(); if (dupli) for (int j = 0; j < h; ++j) { for (int i = 0; i < w; ++i) { *dptr++ = *sptr; // copy one point *dptr++ = *sptr++; // again } // duplicate line we just wrote: safecopy(dptr, dptr - w2, w2); dptr += w2; } else { T zeros[w2]; for (int i = 0; i < w2; ++i) zeros[i] = T(); // needed?? for (int j = 0; j < h; ++j) { for (int i = 0; i < w; ++i) { *dptr++ = *sptr++; // copy one point *dptr++ = zeros[0]; // put a blank } safecopy(dptr, zeros, w2); dptr += w2; } } return result; }
Image<T> intY(const Image<T>& src, const bool dupli) { GVX_TRACE(__PRETTY_FUNCTION__); ASSERT(src.initialized()); const int w = src.getWidth(); const int h = src.getHeight(); const int h2 = h * 2; Image<T> result(w, h2, NO_INIT); typename Image<T>::const_iterator sptr = src.begin(); typename Image<T>::iterator dptr = result.beginw(); if (dupli) for (int j = 0; j < h; ++j) { safecopy(dptr, sptr, w); dptr += w; safecopy(dptr, sptr, w); dptr += w; sptr += w; } else { T zeros[w]; for (int i = 0; i < w; ++i) zeros[i] = T(); // needed?? for (int j = 0; j < h; ++j) { safecopy(dptr, sptr, w); dptr += w; safecopy(dptr, zeros, w); dptr += w; sptr += w; } } return result; }
static LRESULT CALLBACK pass_proc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { char titlebar[STRLEN]; WORD cchPassword; switch (message) { case WM_INITDIALOG: /* Set the default push button to "Cancel." */ SendMessage(hDlg, DM_SETDEFID, (WPARAM) IDCANCEL, (LPARAM) 0); safecopy(titlebar, "Private key: "); safeconcat(titlebar, "options.key"); SetWindowText(hDlg, titlebar); win_log("init"); return TRUE; case WM_COMMAND: /* Set the default push button to "OK" when the user enters text. */ if(HIWORD (wParam) == EN_CHANGE && LOWORD(wParam) == IDE_PASSWORDEDIT) SendMessage(hDlg, DM_SETDEFID, (WPARAM) IDOK, (LPARAM) 0); switch(wParam) { case IDOK: /* Get number of characters. */ cchPassword = (WORD) SendDlgItemMessage(hDlg, IDE_PASSWORDEDIT, EM_LINELENGTH, (WPARAM) 0, (LPARAM) 0); if(cchPassword==0 || cchPassword>=STRLEN) { EndDialog(hDlg, FALSE); return FALSE; } /* Put the number of characters into first word of buffer. */ *((LPWORD)passphrase) = cchPassword; /* Get the characters. */ SendDlgItemMessage(hDlg, IDE_PASSWORDEDIT, EM_GETLINE, (WPARAM) 0, /* line 0 */ (LPARAM) passphrase); passphrase[cchPassword] = 0; /* Null-terminate the string. */ EndDialog(hDlg, TRUE); return TRUE; case IDCANCEL: EndDialog(hDlg, FALSE); return TRUE; } return 0; } return FALSE; UNREFERENCED_PARAMETER(lParam); }
void queue_enqueue(char *name,queue_t *queue,struct queueitem *item) { if ( queue->list == 0 && name != 0 && name[0] != 0 ) safecopy(queue->name,name,sizeof(queue->name)); if ( item == 0 ) { printf("FATAL type error: queueing empty value\n"), getchar(); return; } lock_queue(queue); DL_APPEND(queue->list,item); portable_mutex_unlock(&queue->mutex); //printf("name.(%s) append.%p list.%p\n",name,item,queue->list); }
void ram_parse_MGWpingstr(struct mgw777 *mgw,char *sender,char *pingstr) { void save_MGW_status(char *NXTaddr,char *jsonstr); char name[512],coinstr[MAX_JSON_FIELD],*jsonstr = 0; struct MGWstate S; int32_t gatewayid; cJSON *json,*array; if ( (array= cJSON_Parse(pingstr)) != 0 && is_cJSON_Array(array) != 0 ) { json = cJSON_GetArrayItem(array,0); if ( mgw == 0 ) { copy_cJSON(coinstr,cJSON_GetObjectItem(json,"coin")); if ( coinstr[0] != 0 ) mgw = get_MGW_info(coinstr); } if ( Debuglevel > 2 || (mgw != 0 && mgw->remotemode != 0) ) printf("[%s] parse.(%s)\n",coinstr,pingstr); if ( mgw != 0 ) { cJSON_DeleteItemFromObject(json,"ipaddr"); if ( (gatewayid= (int32_t)get_API_int(cJSON_GetObjectItem(json,"gatewayid"),-1)) >= 0 && gatewayid < mgw->numgateways ) { if ( strcmp(mgw->special_NXTaddrs[gatewayid],sender) == 0 ) ram_parse_MGWstate(&mgw->otherS[gatewayid],json,mgw->coinstr,sender); else printf("ram_parse_MGWpingstr: got wrong address.(%s) for gatewayid.%d expected.(%s)\n",sender,gatewayid,mgw->special_NXTaddrs[gatewayid]); } else { //printf("call parse.(%s)\n",cJSON_Print(json)); ram_parse_MGWstate(&S,json,mgw->coinstr,sender); ram_update_remotesrc(mgw,&S); } jsonstr = cJSON_Print(json); if ( gatewayid >= 0 && gatewayid < 3 && strcmp(mgw->mgwstrs[gatewayid],jsonstr) != 0 ) { safecopy(mgw->mgwstrs[gatewayid],jsonstr,sizeof(mgw->mgwstrs[gatewayid])); //sprintf(name,"%s.%s",mgw->coinstr,Server_ipaddrs[gatewayid]); sprintf(name,"%s.%d",mgw->coinstr,gatewayid); //printf("name is (%s) + (%s) -> (%s)\n",mgw->coinstr,Server_ipaddrs[gatewayid],name); save_MGW_status(name,jsonstr); } } if ( jsonstr != 0 ) free(jsonstr); free_json(array); } //printf("parsed\n"); }
static int set_cwd(void) { char *c, errmsg[STRLEN], exe_file_name[STRLEN]; GetModuleFileName(0, exe_file_name, STRLEN); c=strrchr(exe_file_name, '\\'); /* last backslash */ if(c) /* found */ c[1]='\0'; /* truncate program name */ if(!SetCurrentDirectory(exe_file_name)) { safecopy(errmsg, "Cannot set directory to "); safeconcat(errmsg, exe_file_name); MessageBox(hwnd, errmsg, TEXT("stunnel"), MB_ICONERROR); return 1; } return 0; }
uv_work_t *start_task(tfunc func,char *name,int32_t sleepmicros,void *args,int32_t argsize) { uv_work_t *work; struct task_info *task; task = calloc(1,sizeof(*task) + argsize); memcpy(task->args,args,argsize); //printf("start_tasks copy %p %p\n",*(void **)args,*(void **)task->args); task->func = func; task->argsize = argsize; safecopy(task->name,name,sizeof(task->name)); task->sleepmicros = sleepmicros; work = calloc(1,sizeof(*work)); work->data = task; uv_queue_work(UV_loop,work,Task_handler,aftertask); return(work); }
void fdputline(CLI *c, int fd, char *line) { char tmpline[STRLEN]; const char crlf[]="\r\n"; int len; if(strlen(line)+2>=STRLEN) { /* 2 for crlf */ s_log(LOG_ERR, "Line too long in fdputline"); longjmp(c->err, 1); } safecopy(tmpline, line); safeconcat(tmpline, crlf); len=strlen(tmpline); write_blocking(c, fd, tmpline, len); tmpline[len-2]='\0'; /* remove CRLF */ safestring(tmpline); s_log(LOG_DEBUG, " -> %s", tmpline); }
int name2addrlist(SOCKADDR_LIST *addr_list, char *name, char *default_host) { char tmp[STRLEN], *hostname, *portname; /* set hostname and portname */ safecopy(tmp, name); portname=strrchr(tmp, ':'); if(portname) { hostname=tmp; *portname++='\0'; } else { /* no ':' - use default host IP */ hostname=default_host; portname=tmp; } /* fill addr_list structure */ return hostport2addrlist(addr_list, hostname, portname); }
void fdgetline(CLI *c, int fd, char *line) { char logline[STRLEN]; s_poll_set fds; int ptr; for(ptr=0;;) { s_poll_zero(&fds); s_poll_add(&fds, fd, 1, 0); /* read */ switch(s_poll_wait(&fds, c->opt->timeout_busy)) { case -1: sockerror("fdgetline: s_poll_wait"); longjmp(c->err, 1); /* error */ case 0: s_log(LOG_INFO, "fdgetline: s_poll_wait timeout"); longjmp(c->err, 1); /* timeout */ case 1: break; /* OK */ default: s_log(LOG_ERR, "fdgetline: s_poll_wait unknown result"); longjmp(c->err, 1); /* error */ } switch(readsocket(fd, line+ptr, 1)) { case -1: /* error */ sockerror("readsocket (fdgetline)"); longjmp(c->err, 1); case 0: /* EOF */ s_log(LOG_ERR, "Unexpected socket close (fdgetline)"); longjmp(c->err, 1); } if(line[ptr]=='\r') continue; if(line[ptr]=='\n') break; if(!line[ptr]) break; if(++ptr==STRLEN) { s_log(LOG_ERR, "Input line too long"); longjmp(c->err, 1); } } line[ptr]='\0'; safecopy(logline, line); safestring(logline); s_log(LOG_DEBUG, " <- %s", logline); }
int fdscanf(CLI *c, int fd, const char *format, char *buffer) { char line[STRLEN], lformat[STRLEN]; int ptr, retval; fdgetline(c, fd, line); retval=sscanf(line, format, buffer); if(retval>=0) return retval; s_log(LOG_DEBUG, "fdscanf falling back to lowercase"); safecopy(lformat, format); for(ptr=0; lformat[ptr]; ptr++) lformat[ptr]=tolower(lformat[ptr]); for(ptr=0; line[ptr]; ptr++) line[ptr]=tolower(line[ptr]); return sscanf(line, lformat, buffer); }
char *transporterstatus_func(char *sender,int32_t valid,cJSON **objs,int32_t numobjs,char *origargstr) { uint64_t value; cJSON *array; struct coin_info *cp; int32_t minage; uint8_t sharenrs[255]; uint32_t totalcrc,status,i,sharei,M,N,height,ind,num,n=0,*crcs = 0; char NXTACCTSECRET[1024],sharenrsbuf[1024],otherpubaddr[512],coinstr[512],*retstr = 0; copy_cJSON(NXTACCTSECRET,objs[1]); if ( NXTACCTSECRET[0] == 0 && (cp= get_coin_info("BTCD")) != 0 ) safecopy(NXTACCTSECRET,cp->NXTACCTSECRET,sizeof(NXTACCTSECRET)); status = get_API_int(objs[2],0); copy_cJSON(coinstr,objs[3]); totalcrc = get_API_uint(objs[4],0); value = (SATOSHIDEN * get_API_float(objs[5])); num = get_API_int(objs[6],0); minage = get_API_int(objs[7],0); height = get_API_int(objs[8],0); array = objs[9]; if ( is_cJSON_Array(array) != 0 && (n= cJSON_GetArraySize(array)) > 0 ) { crcs = calloc(n,sizeof(*crcs)); for (i=0; i<n; i++) crcs[i] = get_API_uint(cJSON_GetArrayItem(array,i),0); } sharei = get_API_int(objs[9],0); M = get_API_int(objs[11],0); N = get_API_int(objs[12],0); copy_cJSON(sharenrsbuf,objs[13]); memset(sharenrs,0,sizeof(sharenrs)); if ( M <= N && N < 0xff && M > 0 ) decode_hex(sharenrs,N,sharenrsbuf); else M = N = 1; ind = get_API_int(objs[14],0); copy_cJSON(otherpubaddr,objs[15]); //printf("transporterstatus_func sharei.%d M.%d N.%d other.(%s)\n",sharei,M,N,otherpubaddr); if ( coinstr[0] != 0 && sender[0] != 0 && valid != 0 && num > 0 ) retstr = got_transporter_status(NXTACCTSECRET,sender,coinstr,status,totalcrc,value,num,crcs,ind,minage,height,sharei,M,N,sharenrs,otherpubaddr); else retstr = clonestr("{\"error\":\"invalid incoming transporter status\"}"); if ( crcs != 0 ) free(crcs); return(retstr); }
int32_t extract_cJSON_str(char *dest,int32_t max,cJSON *json,char *field) { int32_t safecopy(char *dest,char *src,long len); char *str; cJSON *obj; int32_t len; long offset; dest[0] = 0; obj = cJSON_GetObjectItem(json,field); if ( obj != 0 ) { str = cJSON_Print(obj); offset = stripquotes(str); len = safecopy(dest,str+offset,max); free(str); return(len); } return(0); }
char *parse_expandedline(char *plugin,int32_t max,char *method,int32_t *timeoutp,char *line,int32_t broadcastflag) { int32_t i,j; char numstr[64],*pubstr,*cmdstr = 0; cJSON *json; uint64_t tag; struct destbuf tmp; for (i=0; i<512&&line[i]!=' '&&line[i]!=0; i++) plugin[i] = line[i]; plugin[i] = 0; *timeoutp = 0; pubstr = line; if ( strcmp(plugin,"pub") == 0 ) strcpy(plugin,"subscriptions"), strcpy(method,"publish"), pubstr += 4; else if ( line[i+1] != 0 ) { for (++i,j=0; i<512&&line[i]!=' '&&line[i]!=0; i++,j++) method[j] = line[i]; method[j] = 0; } else method[0] = 0; if ( (json= cJSON_Parse(line+i+1)) == 0 ) json = cJSON_CreateObject(); if ( json != 0 ) { if ( strcmp("direct",method) == 0 && cJSON_GetObjectItem(json,"myipaddr") == 0 ) cJSON_AddItemToObject(json,"myipaddr",cJSON_CreateString(SUPERNET.myipaddr)); if ( cJSON_GetObjectItem(json,"tag") == 0 ) randombytes((void *)&tag,sizeof(tag)), sprintf(numstr,"%llu",(long long)tag), cJSON_AddItemToObject(json,"tag",cJSON_CreateString(numstr)); //if ( cJSON_GetObjectItem(json,"NXT") == 0 ) // cJSON_AddItemToObject(json,"NXT",cJSON_CreateString(SUPERNET.NXTADDR)); *timeoutp = juint(json,"timeout"); if ( plugin[0] == 0 ) strcpy(plugin,"relay"); if ( cJSON_GetObjectItem(json,"plugin") == 0 ) cJSON_AddItemToObject(json,"plugin",cJSON_CreateString(plugin)); else copy_cJSON(&tmp,cJSON_GetObjectItem(json,"plugin")), safecopy(plugin,tmp.buf,max); if ( method[0] == 0 ) strcpy(method,"help"); cJSON_AddItemToObject(json,"method",cJSON_CreateString(method)); if ( broadcastflag != 0 ) cJSON_AddItemToObject(json,"broadcast",cJSON_CreateString("allrelays")); cmdstr = cJSON_Print(json), _stripwhite(cmdstr,' '); return(cmdstr); } else return(clonestr(pubstr)); }
void *register_NXT_handler(char *name,struct NXThandler_info *mp,int32_t type,int32_t subtype,NXT_handler handler,uint32_t AMsigfilter,int32_t priority,char **assetlist,char **whitelist) { struct NXT_protocol_parms PARMS; struct NXT_protocol *p = calloc(1,sizeof(*p)); safecopy(p->name,name,sizeof(p->name)); p->type = type; p->subtype = subtype; p->AMsigfilter = AMsigfilter; p->priority = priority; p->assetlist = assetlist; p->whitelist = whitelist; p->NXT_handler = handler; memset(&PARMS,0,sizeof(PARMS)); PARMS.mode = NXTPROTOCOL_INIT; if ( Num_NXThandlers < (int32_t)(sizeof(NXThandlers)/sizeof(*NXThandlers)) ) { printf("calling handlerinit.%s size.%d\n",name,Num_NXThandlers); p->handlerdata = (*handler)(mp,&PARMS,0); NXThandlers[Num_NXThandlers++] = p; printf("back handlerinit.%s size.%d\n",name,Num_NXThandlers); } return(p->handlerdata); }
char *stunnel_info(void) { static char retval[STRLEN]; safecopy(retval, "stunnel " VERSION " on " HOST); #ifdef USE_UCONTEXT safeconcat(retval, " UCONTEXT"); #endif #ifdef USE_PTHREAD safeconcat(retval, " PTHREAD"); #endif #ifdef USE_WIN32 safeconcat(retval, " WIN32"); #endif #ifdef USE_FORK safeconcat(retval, " FORK"); #endif #ifdef USE_POLL safeconcat(retval, "+POLL"); #else /* defined(USE_POLL) */ safeconcat(retval, "+SELECT"); #endif /* defined(USE_POLL) */ #ifdef USE_WIN32 if(s_getaddrinfo) safeconcat(retval, "+IPv6"); else safeconcat(retval, "+IPv4"); #else /* defined(USE_WIN32) */ #if defined(USE_IPv6) safeconcat(retval, "+IPv6"); #else /* defined(USE_IPv6) */ safeconcat(retval, "+IPv4"); #endif /* defined(USE_IPv6) */ #endif /* defined(USE_WIN32) */ #ifdef USE_LIBWRAP safeconcat(retval, "+LIBWRAP"); #endif safeconcat(retval, " with "); safeconcat(retval, SSLeay_version(SSLEAY_VERSION)); return retval; }
char *transporter_func(char *sender,int32_t valid,cJSON **objs,int32_t numobjs,char *origargstr) { uint64_t value; struct coin_info *cp; uint8_t sharenrs[255]; uint32_t totalcrc,M,N,minage,height,i,n=0,*crcs = 0; char NXTACCTSECRET[1024],sharenrsbuf[1024],coinstr[512],otherpubaddr[512],*retstr = 0; copy_cJSON(NXTACCTSECRET,objs[1]); if ( NXTACCTSECRET[0] == 0 && (cp= get_coin_info("BTCD")) != 0 ) safecopy(NXTACCTSECRET,cp->NXTACCTSECRET,sizeof(NXTACCTSECRET)); copy_cJSON(coinstr,objs[2]); height = get_API_uint(objs[3],0); minage = get_API_uint(objs[4],0); value = (SATOSHIDEN * get_API_float(objs[5])); totalcrc = get_API_uint(objs[6],0); if ( is_cJSON_Array(objs[7]) != 0 && (n= cJSON_GetArraySize(objs[7])) > 0 ) { crcs = calloc(n,sizeof(*crcs)); for (i=0; i<n; i++) crcs[i] = get_API_uint(cJSON_GetArrayItem(objs[7],i),0); } M = get_API_int(objs[8],0); N = get_API_int(objs[9],0); copy_cJSON(sharenrsbuf,objs[10]); memset(sharenrs,0,sizeof(sharenrs)); if ( M <= N && N < 0xff && M > 0 ) decode_hex(sharenrs,N,sharenrsbuf); else M = N = 1; copy_cJSON(otherpubaddr,objs[11]); printf("transporterstatus_func M.%d N.%d [%s] otherpubaddr.(%s)\n",M,N,sharenrsbuf,otherpubaddr); if ( coinstr[0] != 0 && sender[0] != 0 && valid != 0 && n > 0 ) retstr = transporter_received(sender,NXTACCTSECRET,coinstr,totalcrc,height,value,minage,crcs,n,M,N,sharenrs,otherpubaddr); else retstr = clonestr("{\"error\":\"invalid incoming transporter bundle\"}"); if ( crcs != 0 ) free(crcs); return(retstr); }