From 6865a250791e6137bbfe053095c34de382c3f7c2 Mon Sep 17 00:00:00 2001 From: jl777 Date: Sat, 16 Jan 2016 14:26:03 -0300 Subject: [PATCH] test --- iguana/iguana.sources | 4 +- iguana/iguana_html.c | 332 +----------------------------------------- iguana/iguana_rpc.c | 325 +++++++++++++++++++++++++++++++++++++++++ iguana/main.c | 10 +- 4 files changed, 340 insertions(+), 331 deletions(-) diff --git a/iguana/iguana.sources b/iguana/iguana.sources index 920d106f1..97aa7c68f 100755 --- a/iguana/iguana.sources +++ b/iguana/iguana.sources @@ -1 +1,3 @@ -SOURCES := ramchain_api.c iguana_tx.c iguana_wallet.c iguana_html.c iguana_pubkeys.c iguana_recv.c iguana_bundles.c iguana_msg.c iguana_rpc.c iguana777.c iguana_chains.c iguana_peers.c iguana_accept.c iguana_bitmap.c iguana_init.c iguana_ramchain.c iguana_blocks.c iguana_json.c main.c +#iguana_html.c + +SOURCES := ramchain_api.c iguana_tx.c iguana_wallet.c iguana_pubkeys.c iguana_recv.c iguana_bundles.c iguana_msg.c iguana_rpc.c iguana777.c iguana_chains.c iguana_peers.c iguana_accept.c iguana_bitmap.c iguana_init.c iguana_ramchain.c iguana_blocks.c iguana_json.c main.c diff --git a/iguana/iguana_html.c b/iguana/iguana_html.c index 6bc304e01..b384bc327 100755 --- a/iguana/iguana_html.c +++ b/iguana/iguana_html.c @@ -328,10 +328,10 @@ char *iguana_htmlresponse(char *retbuf,int32_t bufsize,int32_t *remainsp,int32_t n = iguana_htmlgen(&retbuf[n],bufsize-n,result,error,json,"iguana",Currentjsonstr); free_json(json); /*if ( n == 0 ) - { - n = (int32_t)(strlen(html) + strlen(retstr) + 1); - sprintf(retbuf+strlen(retbuf),html,retstr); - }*/ + { + n = (int32_t)(strlen(html) + strlen(retstr) + 1); + sprintf(retbuf+strlen(retbuf),html,retstr); + }*/ if ( freeflag != 0 ) free(retstr); if ( n > bufsize ) @@ -342,327 +342,3 @@ char *iguana_htmlresponse(char *retbuf,int32_t bufsize,int32_t *remainsp,int32_t *remainsp = n; return(retbuf); } - -int32_t Supernet_lineparse(char *key,int32_t keymax,char *value,int32_t valuemax,char *src) -{ - int32_t a,b,c,n = 0; - key[0] = value[0] = 0; - while ( (c= src[n]) == ' ' || c == '\t' || c == '\n' || c == '\t' ) - n++; - while ( (c= src[n]) != ':' && c != 0 ) - { - *key++ = c; - if ( ++n >= keymax-1 ) - { - *key = 0; - printf("lineparse overflow key.(%s)\n",src); - return(-1); - } - } - *key = 0; - if ( src[n] != ':' ) - return(n); - n++; - while ( (c= src[n]) == ' ' || c == '\t' ) - n++; - while ( (c= src[n]) != 0 && c != '\r' && c != '\n' ) - { - if ( c == '%' && (a= src[n+1]) != 0 && (b= src[n+2]) != 0 ) - c = ((unhex(a) << 4) | unhex(b)), n += 2; - *value++ = c; - n++; - if ( n >= valuemax-1 ) - { - *value = 0; - printf("lineparse overflow.(%s)\n",src); - return(-1); - } - } - *value = 0; - if ( src[n] != 0 ) - { - n++; - while ( (c= src[n]) == '\r' || c == '\n' ) - n++; - } - printf("key.(%s) value.(%s)\n",key,value); - return(n); -} - -cJSON *SuperNET_urlconv(char *value,int32_t bufsize,char *urlstr) -{ - int32_t i,n,totallen,datalen,len = 0; cJSON *json,*array; char key[8192],*data; - json = cJSON_CreateObject(); - array = cJSON_CreateArray(); - totallen = (int32_t)strlen(urlstr); - while ( 1 ) - { - for (i=len; urlstr[i]!=0; i++) - if ( urlstr[i] == '\r' || urlstr[i] == '\n' ) - break; - if ( i == len && (urlstr[len] == '\r' || urlstr[len] == '\n') ) - { - len++; - continue; - } - urlstr[i] = 0; - if ( (n= Supernet_lineparse(key,sizeof(key),value,bufsize,&urlstr[len])) > 0 ) - { - if ( value[0] != 0 ) - jaddstr(json,key,value); - else jaddistr(array,key); - len += (n + 1); - if ( strcmp(key,"Content-Length") == 0 && (datalen= atoi(value)) > 0 ) - { - data = &urlstr[totallen - datalen]; - data[-1] = 0; - //printf("post.(%s) (%c)\n",data,data[0]); - jaddstr(json,"POST",data); - } - } else break; - } - jadd(json,"lines",array); - return(json); -} - -char *SuperNET_rpcparse(struct supernet_info *myinfo,char *retbuf,int32_t bufsize,int32_t *jsonflagp,int32_t *postflagp,char *urlstr,char *remoteaddr) -{ - cJSON *tokens,*argjson,*json = 0; char symbol[16],buf[4096],urlmethod[16],*data,url[1024],*retstr,*token = 0; int32_t i,j,n; - //printf("rpcparse.(%s)\n",urlstr); - for (i=0; i 0 ) - { - jaddstr(argjson,"agent",jstri(tokens,0)); - if ( n > 1 ) - { - if ( jstri(tokens,1) != 0 ) - { - char *key,*value; - strcpy(buf,jstri(tokens,1)); - key = value = 0; - i = 0; - for (; buf[i]!=0; i++) - { - if ( buf[i] == '?' ) - { - buf[i] = 0; - jaddstr(argjson,"method",buf); - i++; - key = &buf[i]; - break; - } - } - while ( buf[i] != 0 ) - { - //printf("iter.[%s]\n",&buf[i]); - if ( buf[i] != 0 && key != 0 ) - { - for (; buf[i]!=0; i++) - { - if ( buf[i] == '=' ) - { - buf[i] = 0; - i++; - //printf("got key.(%s)\n",key); - value = &buf[i]; - break; - } - } - if ( buf[i] != 0 && value != 0 ) - { - for (; buf[i]!=0; i++) - { - if ( buf[i] == '&' ) - { - buf[i] = 0; - jaddstr(argjson,key,value); - i++; - //printf("got value.(%s)\n",value); - value = 0; - key = &buf[i]; - break; - } - } - } - } - } - if ( key != 0 && value != 0 ) - jaddstr(argjson,key,value); - } else jaddstr(argjson,"method",buf); - } - for (i=2; i (%s) postflag.%d (%s)\n",urlstr,cJSON_Print(json),*postflagp,retstr); - return(retstr); - } - return(clonestr("{\"error\":\"couldnt process packet\"}")); -} - -void iguana_rpcloop(void *args) -{ - struct supernet_info *myinfo = args; - int32_t recvlen,bindsock,postflag,sock,remains,numsent,jsonflag,len; socklen_t clilen; - char remoteaddr[64],jsonbuf[8192],*buf,*retstr,*space;//,*retbuf; ,n,i,m - struct sockaddr_in cli_addr; uint32_t ipbits,i,size = IGUANA_WIDTH*IGUANA_HEIGHT*16 + 512; uint16_t port; - port = IGUANA_RPCPORT; - bindsock = iguana_socket(1,"127.0.0.1",port); - printf("iguana_rpcloop 127.0.0.1:%d bind sock.%d\n",port,bindsock); - space = calloc(1,size); - while ( bindsock >= 0 ) - { - clilen = sizeof(cli_addr); - //printf("ACCEPT (%s:%d) on sock.%d\n","127.0.0.1",port,bindsock); - sock = accept(bindsock,(struct sockaddr *)&cli_addr,&clilen); - if ( sock < 0 ) - { - //printf("iguana_rpcloop ERROR on accept usock.%d\n",sock); - continue; - } - memcpy(&ipbits,&cli_addr.sin_addr.s_addr,sizeof(ipbits)); - expand_ipbits(remoteaddr,ipbits); - //printf("RPC.%d for %x (%s)\n",sock,ipbits,ipaddr); - //printf("%p got.(%s) from %s | usock.%d ready.%u dead.%u\n",addr,H.command,addr->ipaddr,addr->usock,addr->ready,addr->dead); - memset(jsonbuf,0,sizeof(jsonbuf)); - remains = (int32_t)(sizeof(jsonbuf) - 1); - buf = jsonbuf; - recvlen = 0; - retstr = 0; - while ( remains > 0 ) - { - if ( (len= (int32_t)recv(sock,buf,remains,0)) < 0 ) - { - if ( errno == EAGAIN ) - { - printf("EAGAIN for len %d, remains.%d\n",len,remains); - usleep(10000); - } - break; - } - else - { - if ( len > 0 ) - { - remains -= len; - recvlen += len; - buf = &buf[len]; - retstr = SuperNET_rpcparse(myinfo,space,size,&jsonflag,&postflag,jsonbuf,remoteaddr); - break; - } else usleep(10000); - //printf("got.(%s) %d remains.%d of total.%d\n",jsonbuf,recvlen,remains,len); - //retstr = iguana_rpcparse(space,size,&postflag,jsonbuf); - break; - } - } - if ( retstr == 0 ) - retstr = iguana_htmlresponse(space,size,&remains,1,retstr,retstr != space); - if ( retstr != 0 ) - { - i = 0; - if ( 0 && postflag == 0 ) - retstr = iguana_htmlresponse(space,size,&remains,1,retstr,retstr != space); - else remains = (int32_t)strlen(retstr); - //printf("POSTFLAG.%d\n",postflag); - //printf("RETBUF.(%s)\n",retstr); - while ( remains > 0 ) - { - if ( (numsent= (int32_t)send(sock,&retstr[i],remains,MSG_NOSIGNAL)) < 0 ) - { - if ( errno != EAGAIN && errno != EWOULDBLOCK ) - { - //printf("%s: %s numsent.%d vs remains.%d len.%d errno.%d (%s) usock.%d\n",retstr,ipaddr,numsent,remains,recvlen,errno,strerror(errno),sock); - break; - } - } - else if ( remains > 0 ) - { - remains -= numsent; - i += numsent; - if ( remains > 0 ) - printf("iguana sent.%d remains.%d of len.%d\n",numsent,remains,recvlen); - } - } - if ( retstr != space) - free(retstr); - } - if ( Currentjsonstr[0] != 0 ) - strcpy(Prevjsonstr,Currentjsonstr); - Currentjsonstr[0] = 0; - //printf("done response sock.%d\n",sock); - closesocket(sock); - } -} \ No newline at end of file diff --git a/iguana/iguana_rpc.c b/iguana/iguana_rpc.c index 72998a573..ee20aa4b8 100755 --- a/iguana/iguana_rpc.c +++ b/iguana/iguana_rpc.c @@ -611,3 +611,328 @@ char *iguana_bitcoinRPC(struct supernet_info *myinfo,char *method,cJSON *json,ch retstr = clonestr("{\"error\":\"cant parse jsonstr\"}"); return(retstr); } + +int32_t Supernet_lineparse(char *key,int32_t keymax,char *value,int32_t valuemax,char *src) +{ + int32_t a,b,c,n = 0; + key[0] = value[0] = 0; + while ( (c= src[n]) == ' ' || c == '\t' || c == '\n' || c == '\t' ) + n++; + while ( (c= src[n]) != ':' && c != 0 ) + { + *key++ = c; + if ( ++n >= keymax-1 ) + { + *key = 0; + printf("lineparse overflow key.(%s)\n",src); + return(-1); + } + } + *key = 0; + if ( src[n] != ':' ) + return(n); + n++; + while ( (c= src[n]) == ' ' || c == '\t' ) + n++; + while ( (c= src[n]) != 0 && c != '\r' && c != '\n' ) + { + if ( c == '%' && (a= src[n+1]) != 0 && (b= src[n+2]) != 0 ) + c = ((unhex(a) << 4) | unhex(b)), n += 2; + *value++ = c; + n++; + if ( n >= valuemax-1 ) + { + *value = 0; + printf("lineparse overflow.(%s)\n",src); + return(-1); + } + } + *value = 0; + if ( src[n] != 0 ) + { + n++; + while ( (c= src[n]) == '\r' || c == '\n' ) + n++; + } + printf("key.(%s) value.(%s)\n",key,value); + return(n); +} + +cJSON *SuperNET_urlconv(char *value,int32_t bufsize,char *urlstr) +{ + int32_t i,n,totallen,datalen,len = 0; cJSON *json,*array; char key[8192],*data; + json = cJSON_CreateObject(); + array = cJSON_CreateArray(); + totallen = (int32_t)strlen(urlstr); + while ( 1 ) + { + for (i=len; urlstr[i]!=0; i++) + if ( urlstr[i] == '\r' || urlstr[i] == '\n' ) + break; + if ( i == len && (urlstr[len] == '\r' || urlstr[len] == '\n') ) + { + len++; + continue; + } + urlstr[i] = 0; + if ( (n= Supernet_lineparse(key,sizeof(key),value,bufsize,&urlstr[len])) > 0 ) + { + if ( value[0] != 0 ) + jaddstr(json,key,value); + else jaddistr(array,key); + len += (n + 1); + if ( strcmp(key,"Content-Length") == 0 && (datalen= atoi(value)) > 0 ) + { + data = &urlstr[totallen - datalen]; + data[-1] = 0; + //printf("post.(%s) (%c)\n",data,data[0]); + jaddstr(json,"POST",data); + } + } else break; + } + jadd(json,"lines",array); + return(json); +} + +char *SuperNET_rpcparse(struct supernet_info *myinfo,char *retbuf,int32_t bufsize,int32_t *jsonflagp,int32_t *postflagp,char *urlstr,char *remoteaddr) +{ + cJSON *tokens,*argjson,*json = 0; char symbol[16],buf[4096],urlmethod[16],*data,url[1024],*retstr,*token = 0; int32_t i,j,n; + //printf("rpcparse.(%s)\n",urlstr); + for (i=0; i 0 ) + { + jaddstr(argjson,"agent",jstri(tokens,0)); + if ( n > 1 ) + { + if ( jstri(tokens,1) != 0 ) + { + char *key,*value; + strcpy(buf,jstri(tokens,1)); + key = value = 0; + i = 0; + for (; buf[i]!=0; i++) + { + if ( buf[i] == '?' ) + { + buf[i] = 0; + jaddstr(argjson,"method",buf); + i++; + key = &buf[i]; + break; + } + } + while ( buf[i] != 0 ) + { + //printf("iter.[%s]\n",&buf[i]); + if ( buf[i] != 0 && key != 0 ) + { + for (; buf[i]!=0; i++) + { + if ( buf[i] == '=' ) + { + buf[i] = 0; + i++; + //printf("got key.(%s)\n",key); + value = &buf[i]; + break; + } + } + if ( buf[i] != 0 && value != 0 ) + { + for (; buf[i]!=0; i++) + { + if ( buf[i] == '&' ) + { + buf[i] = 0; + jaddstr(argjson,key,value); + i++; + //printf("got value.(%s)\n",value); + value = 0; + key = &buf[i]; + break; + } + } + } + } + } + if ( key != 0 && value != 0 ) + jaddstr(argjson,key,value); + } else jaddstr(argjson,"method",buf); + } + for (i=2; i (%s) postflag.%d (%s)\n",urlstr,cJSON_Print(json),*postflagp,retstr); + return(retstr); + } + return(clonestr("{\"error\":\"couldnt process packet\"}")); +} + +void iguana_rpcloop(void *args) +{ + struct supernet_info *myinfo = args; + int32_t recvlen,bindsock,postflag,sock,remains,numsent,jsonflag,len; socklen_t clilen; + char remoteaddr[64],jsonbuf[8192],*buf,*retstr,*space;//,*retbuf; ,n,i,m + struct sockaddr_in cli_addr; uint32_t ipbits,i,size = IGUANA_WIDTH*IGUANA_HEIGHT*16 + 512; uint16_t port; + port = IGUANA_RPCPORT; + bindsock = iguana_socket(1,"127.0.0.1",port); + printf("iguana_rpcloop 127.0.0.1:%d bind sock.%d\n",port,bindsock); + space = calloc(1,size); + while ( bindsock >= 0 ) + { + clilen = sizeof(cli_addr); + //printf("ACCEPT (%s:%d) on sock.%d\n","127.0.0.1",port,bindsock); + sock = accept(bindsock,(struct sockaddr *)&cli_addr,&clilen); + if ( sock < 0 ) + { + //printf("iguana_rpcloop ERROR on accept usock.%d\n",sock); + continue; + } + memcpy(&ipbits,&cli_addr.sin_addr.s_addr,sizeof(ipbits)); + expand_ipbits(remoteaddr,ipbits); + //printf("RPC.%d for %x (%s)\n",sock,ipbits,ipaddr); + //printf("%p got.(%s) from %s | usock.%d ready.%u dead.%u\n",addr,H.command,addr->ipaddr,addr->usock,addr->ready,addr->dead); + memset(jsonbuf,0,sizeof(jsonbuf)); + remains = (int32_t)(sizeof(jsonbuf) - 1); + buf = jsonbuf; + recvlen = 0; + retstr = 0; + while ( remains > 0 ) + { + if ( (len= (int32_t)recv(sock,buf,remains,0)) < 0 ) + { + if ( errno == EAGAIN ) + { + printf("EAGAIN for len %d, remains.%d\n",len,remains); + usleep(10000); + } + break; + } + else + { + if ( len > 0 ) + { + remains -= len; + recvlen += len; + buf = &buf[len]; + retstr = SuperNET_rpcparse(myinfo,space,size,&jsonflag,&postflag,jsonbuf,remoteaddr); + break; + } else usleep(10000); + //printf("got.(%s) %d remains.%d of total.%d\n",jsonbuf,recvlen,remains,len); + //retstr = iguana_rpcparse(space,size,&postflag,jsonbuf); + break; + } + } + //if ( retstr == 0 ) + // retstr = iguana_htmlresponse(space,size,&remains,1,retstr,retstr != space); + if ( retstr != 0 ) + { + i = 0; + //if ( 0 && postflag == 0 ) + // retstr = iguana_htmlresponse(space,size,&remains,1,retstr,retstr != space); + //else + remains = (int32_t)strlen(retstr); + //printf("POSTFLAG.%d\n",postflag); + //printf("RETBUF.(%s)\n",retstr); + while ( remains > 0 ) + { + if ( (numsent= (int32_t)send(sock,&retstr[i],remains,MSG_NOSIGNAL)) < 0 ) + { + if ( errno != EAGAIN && errno != EWOULDBLOCK ) + { + //printf("%s: %s numsent.%d vs remains.%d len.%d errno.%d (%s) usock.%d\n",retstr,ipaddr,numsent,remains,recvlen,errno,strerror(errno),sock); + break; + } + } + else if ( remains > 0 ) + { + remains -= numsent; + i += numsent; + if ( remains > 0 ) + printf("iguana sent.%d remains.%d of len.%d\n",numsent,remains,recvlen); + } + } + if ( retstr != space) + free(retstr); + } + //if ( Currentjsonstr[0] != 0 ) + // strcpy(Prevjsonstr,Currentjsonstr); + //Currentjsonstr[0] = 0; + //printf("done response sock.%d\n",sock); + closesocket(sock); + } +} \ No newline at end of file diff --git a/iguana/main.c b/iguana/main.c index f0c07d050..f32efd9ae 100644 --- a/iguana/main.c +++ b/iguana/main.c @@ -118,11 +118,17 @@ char *SuperNET_JSON(struct supernet_info *myinfo,cJSON *json,char *remoteaddr) if ( json != 0 ) { if ( (tag= j64bits(json,"tag")) == 0 ) + { OS_randombytes((uint8_t *)&tag,sizeof(tag)); + jadd64bits(json,"tag",tag); + } if ( (timeout= juint(json,"timeout")) == 0 ) timeout = IGUANA_JSONTIMEOUT; jsonstr = jprint(json,0); - if ( (retjsonstr= iguana_blockingjsonstr(myinfo,jsonstr,tag,timeout,remoteaddr)) != 0 ) + if ( jstr(json,"immediate") != 0 ) + retjsonstr = SuperNET_jsonstr(myinfo,jsonstr,remoteaddr); + else retjsonstr = iguana_blockingjsonstr(myinfo,jsonstr,tag,timeout,remoteaddr); + if ( retjsonstr != 0 ) { if ( (retjson= cJSON_Parse(retjsonstr)) != 0 ) { @@ -252,7 +258,7 @@ void iguana_main(void *arg) { flag = 0; iguana_jsonQ(); - if ( flag == 0 )//|| addr->rank >= (coin->peers.numranked>>1) ) + if ( flag == 0 ) { struct iguana_helper *ptr; if ( (ptr= queue_dequeue(&bundlesQ,0)) != 0 )