void iguana_acceptloop(void *args) { struct iguana_peer *addr; struct iguana_info *coin = args; struct pollfd pfd; int32_t sock; struct iguana_accept *ptr; uint16_t port = coin->chain->portp2p; socklen_t clilen; struct sockaddr_in cli_addr; char ipaddr[64]; uint32_t i,ipbits; while ( (coin->bindsock= iguana_socket(1,"0.0.0.0",port)) < 0 ) sleep(5); printf(">>>>>>>>>>>>>>>> iguana_bindloop 127.0.0.1:%d bind sock.%d\n",port,coin->bindsock); printf("START ACCEPTING\n"); while ( coin->bindsock >= 0 ) { memset(&pfd,0,sizeof(pfd)); pfd.fd = coin->bindsock; pfd.events = POLLIN; if ( poll(&pfd,1,100) <= 0 ) continue; clilen = sizeof(cli_addr); printf("ACCEPT (%s:%d) on sock.%d\n","127.0.0.1",coin->chain->portp2p,coin->bindsock); sock = accept(coin->bindsock,(struct sockaddr *)&cli_addr,&clilen); if ( sock < 0 ) { printf("ERROR on accept bindsock.%d errno.%d (%s)\n",coin->bindsock,errno,strerror(errno)); continue; } memcpy(&ipbits,&cli_addr.sin_addr.s_addr,sizeof(ipbits)); expand_ipbits(ipaddr,ipbits); printf("NEWSOCK.%d for %x (%s)\n",sock,ipbits,ipaddr); for (i=0; i<IGUANA_MAXPEERS; i++) { if ( coin->peers.active[i].ipbits == (uint32_t)ipbits && coin->peers.active[i].usock >= 0 ) { printf("found existing peer.(%s) in slot[%d]\n",ipaddr,i); iguana_iAkill(coin,&coin->peers.active[i],0); sleep(1); } } /*if ( (uint32_t)ipbits == myinfo->myaddr.myipbits ) { }*/ if ( (addr= iguana_peerslot(coin,ipbits,0)) == 0 ) { ptr = mycalloc('a',1,sizeof(*ptr)); strcpy(ptr->ipaddr,ipaddr); ptr->ipbits = ipbits; ptr->sock = sock; ptr->port = coin->chain->portp2p; printf("queue PENDING ACCEPTS\n"); queue_enqueue("acceptQ",&coin->acceptQ,&ptr->DL,0); } else { printf("LAUNCH DEDICATED THREAD for %s\n",ipaddr); addr->usock = sock; strcpy(addr->symbol,coin->symbol); iguana_launch(coin,"accept",iguana_dedicatedglue,addr,IGUANA_CONNTHREAD); //iguana_dedicatedloop(coin,addr); } } }
int32_t iguana_pollrecv(struct iguana_info *coin,struct iguana_peer *addr,uint8_t *buf,int32_t bufsize) { #ifndef IGUANA_DEDICATED_THREADS strcpy(addr->symbol,coin->symbol); if ( addr != coin->peers.localaddr ) { addr->startrecv = (uint32_t)time(NULL); iguana_launch("processmsg",iguana_processmsg,addr,IGUANA_RECVTHREAD); } else #endif _iguana_processmsg(coin,addr->usock,addr,buf,bufsize); return(1); }
TWO_STRINGS(iguana,addnode,activecoin,ipaddr) { struct iguana_peer *addr; int32_t i,n; if ( coin == 0 ) coin = iguana_coinfind(activecoin); if ( coin != 0 && strcmp(coin->symbol,"RELAY") == 0 ) basilisk_addrelay_info(myinfo,0,(uint32_t)calc_ipbits(ipaddr),GENESIS_PUBKEY); printf("coin.%p.[%s] addnode.%s -> %s\n",coin,coin!=0?coin->symbol:"",activecoin,ipaddr); if ( coin != 0 && coin->peers != 0 && ipaddr != 0 && is_ipaddr(ipaddr) != 0 ) { //iguana_possible_peer(coin,ipaddr); if ( (addr= iguana_peerslot(coin,(uint32_t)calc_ipbits(ipaddr),1)) != 0 ) { addr->supernet = 1; if ( addr->usock >= 0 ) { if ( (n= coin->peers->numranked) != 0 ) { for (i=0; i<n; i++) { if ( addr == coin->peers->ranked[i] ) break; } if ( i == n ) { if ( i == IGUANA_MAXPEERS ) i--; else coin->peers->numranked = n+1; coin->peers->ranked[i] = addr; addr->recvblocks = coin->peers->ranked[0]->recvblocks + 100; addr->recvtotal = coin->peers->ranked[0]->recvtotal*1.1 + 100; printf("set (%s) -> slot.%d numranked.%d\n",ipaddr,i,coin->peers->numranked); } else printf("(%s) is already peer.%d\n",ipaddr,i); } return(clonestr("{\"result\":\"peer was already connected\"}")); } if ( addr->pending == 0 ) { addr->pending = (uint32_t)time(NULL); iguana_launch(coin,"connection",iguana_startconnection,addr,IGUANA_CONNTHREAD); return(clonestr("{\"result\":\"addnode submitted\"}")); } else return(clonestr("{\"result\":\"addnode connection was already pending\"}")); } else return(clonestr("{\"result\":\"addnode cant find peer slot\"}")); } else if ( coin == 0 ) return(clonestr("{\"error\":\"addnode needs active coin, do an addcoin first\"}")); else return(clonestr("{\"error\":\"addnode needs ipaddr\"}")); }
int32_t iguana_pendingaccept(struct iguana_info *coin) { struct iguana_accept *ptr; char ipaddr[64]; struct iguana_peer *addr; if ( (ptr= queue_dequeue(&coin->acceptQ,0)) != 0 ) { if ( (addr= iguana_peerslot(coin,ptr->ipbits,0)) != 0 ) { expand_ipbits(ipaddr,ptr->ipbits); printf("iguana_pendingaccept LAUNCH DEDICATED THREAD for %s\n",ipaddr); addr->usock = ptr->sock; strcpy(addr->symbol,coin->symbol); iguana_launch(coin,"accept",iguana_dedicatedglue,addr,IGUANA_CONNTHREAD); myfree(ptr,sizeof(*ptr)); return(1); } else queue_enqueue("requeue_acceptQ",&coin->acceptQ,&ptr->DL,0); } return(0); }
void *iguana_kvconnectiterator(struct iguana_info *coin,struct iguanakv *kv,struct iguana_kvitem *item,uint64_t args,void *key,void *value,int32_t valuesize) { struct iguana_iAddr *iA = value; char ipaddr[64]; int32_t i; struct iguana_peer *addr = 0; if ( iA->ipbits != 0 && iguana_numthreads(1 << IGUANA_CONNTHREAD) < IGUANA_MAXCONNTHREADS && iA->status != IGUANA_PEER_READY && iA->status != IGUANA_PEER_CONNECTING ) { //printf("%x\n",iA->ipbits); expand_ipbits(ipaddr,iA->ipbits); portable_mutex_lock(&coin->peers_mutex); for (i=0; i<coin->MAXPEERS; i++) { addr = &coin->peers.active[i]; if ( addr->pending != 0 || addr->ipbits == iA->ipbits || strcmp(ipaddr,addr->ipaddr) == 0 ) { portable_mutex_unlock(&coin->peers_mutex); return(0); } if ( addr->ipbits == 0 ) { iguana_initpeer(coin,addr,iA->ipbits); break; } } portable_mutex_unlock(&coin->peers_mutex); if ( addr != 0 ) { //printf("status.%d addr.%p possible peer.(%s) (%s).%x %u threads %d %d %d %d\n",iA->status,addr,ipaddr,addr->ipaddr,addr->ipbits,addr->pending,iguana_numthreads(0),iguana_numthreads(1),iguana_numthreads(2),iguana_numthreads(3)); iA->status = IGUANA_PEER_CONNECTING; if ( iguana_rwiAddrind(coin,1,iA,item->hh.itemind) > 0 ) { //printf("iguana_startconnection.(%s) status.%d\n",ipaddr,IGUANA_PEER_CONNECTING); iguana_launch("connection",iguana_startconnection,addr,IGUANA_CONNTHREAD); } } else printf("no open peer slots left\n"); } return(0); }
int maingen(int argc, char** argv) { uint8_t logs[256]; uint8_t exps[255]; unsigned int x; unsigned int i; x = 1; for( i = 0; i < 255; ++i ) { exps[i] = x; logs[x] = i; x <<= 1; if( x & 0x100 ) x ^= 0x11d; /* Unset the 8th bit and mix in 0x1d */ } logs[0] = 0; /* can't log(0) so just set it neatly to 0 */ /* The above generation algorithm clearly demonstrates that * logs[exps[i]] == i for 0 <= i <= 254 * exps[logs[i]] == i for 1 <= i <= 255 */ /* Spew out the tables */ fprintf(stdout, "\ /*\n\ * This file is autogenerated by gfshare_maketable.\n\ */\n\ \n\ static uint8_t logs[256] = {\n "); for( i = 0; i < 256; ++i ) { fprintf(stdout, "0x%02x", logs[i]); if( i == 255 ) fprintf(stdout, " };\n"); else if( (i % 8) == 7 ) fprintf(stdout, ",\n "); else fprintf(stdout, ", "); } /* The exp table we output from 0 to 509 because that way when we * do the lagrange interpolation we don't have to be quite so strict * with staying inside the field which makes it quicker */ fprintf(stdout, "\ \n\ static uint8_t exps[510] = {\n "); for( i = 0; i < 510; ++i ) { fprintf(stdout, "0x%02x", exps[i % 255]); /* exps[255]==exps[0] */ if( i == 509 ) fprintf(stdout, " };\n"); else if( (i % 8) == 7) fprintf(stdout, ",\n "); else fprintf(stdout, ", "); } return 0; } void iguana_main(void *arg) { int32_t usessl = 0, ismainnet = 1; struct supernet_info *myinfo; char *tmpstr,*helperargs,*coinargs,helperstr[512]; int32_t i; mycalloc(0,0,0); myinfo = SuperNET_MYINFO(0); FILE *fp; int32_t iter; void ztest(); ztest(); strcpy(myinfo->NXTAPIURL,"http://127.0.0.1:7876/nxt"); for (iter=0; iter<2; iter++) { if ( (fp= fopen(iter == 0 ? "nxtpasswords" : "fimpasswords","rb")) != 0 ) { char line[4096],NXTaddr[64]; int32_t j; uint8_t pubkey[32]; while ( fgets(line,sizeof(line),fp) > 0 ) { j = (int32_t)strlen(line) - 1; line[j] = 0; calc_NXTaddr(NXTaddr,pubkey,(uint8_t *)line,j); printf("FORGING %s (%s)\n",NXTaddr,issue_startForging(myinfo,line)); } fclose(fp); } strcpy(myinfo->NXTAPIURL,"http://127.0.0.1:7886/nxt"); } if ( usessl == 0 ) strcpy(myinfo->NXTAPIURL,"http://127.0.0.1:"); else strcpy(myinfo->NXTAPIURL,"https://127.0.0.1:"); if ( ismainnet != 0 ) strcat(myinfo->NXTAPIURL,"7876/nxt"); else strcat(myinfo->NXTAPIURL,"6876/nxt"); if ( 0 ) { #ifndef _WIN32 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); #endif } //iguana_chaingenesis(1,1403138561,0x1e0fffff,8359109,bits256_conv("fd1751cc6963d88feca94c0d01da8883852647a37a0a67ce254d62dd8c9d5b2b")); // BTCD //iguana_chaingenesis(1,1409839200,0x1e0fffff,64881664,bits256_conv("698a93a1cacd495a7a4fb3864ad8d06ed4421dedbc57f9aaad733ea53b1b5828")); // VPN iguana_chaingenesis(1,1317972665,0x1e0ffff0,2084524493,bits256_conv("97ddfbbae6be97fd6cdf3e7ca13232a3afff2353e29badfab7f73011edd4ced9")); // LTC iguana_initQ(&helperQ,"helperQ"); OS_ensure_directory("help"); OS_ensure_directory("confs"); OS_ensure_directory("DB"), OS_ensure_directory("DB/ECB"); OS_ensure_directory("tmp"); if ( (tmpstr= SuperNET_JSON(myinfo,cJSON_Parse("{\"agent\":\"SuperNET\",\"method\":\"help\"}"),0)) != 0 ) { if ( (API_json= cJSON_Parse(tmpstr)) != 0 && (API_json= jobj(API_json,"result")) != 0 ) API_json = jobj(API_json,"API"); free(tmpstr); } if ( IGUANA_NUMHELPERS == 0 ) IGUANA_NUMHELPERS = 1; for (i=0; i<IGUANA_NUMHELPERS; i++) { sprintf(helperstr,"{\"name\":\"helper.%d\"}",i); helperargs = clonestr(helperstr); iguana_launch(iguana_coinadd("BTCD"),"iguana_helper",iguana_helper,helperargs,IGUANA_PERMTHREAD); } iguana_launch(iguana_coinadd("BTCD"),"rpcloop",iguana_rpcloop,SuperNET_MYINFO(0),IGUANA_PERMTHREAD); category_init(&MYINFO); if ( (coinargs= SuperNET_keysinit(&MYINFO,arg)) != 0 ) iguana_launch(iguana_coinadd("BTCD"),"iguana_coins",iguana_coins,coinargs,IGUANA_PERMTHREAD); else if ( 1 ) { #ifdef __APPLE__ sleep(1); char *str; strcpy(MYINFO.rpcsymbol,"BTC"); iguana_launchcoin(MYINFO.rpcsymbol,cJSON_Parse("{}")); if ( 1 && (str= SuperNET_JSON(&MYINFO,cJSON_Parse("{\"agent\":\"iguana\",\"method\":\"addcoin\",\"services\":128,\"maxpeers\":3,\"newcoin\":\"BTC\",\"active\":0}"),0)) != 0 ) { free(str); if ( 1 && (str= SuperNET_JSON(&MYINFO,cJSON_Parse("{\"agent\":\"iguana\",\"method\":\"addcoin\",\"services\":128,\"maxpeers\":3,\"newcoin\":\"BTCD\",\"active\":0}"),0)) != 0 ) { free(str); if ( 0 && (str= SuperNET_JSON(&MYINFO,cJSON_Parse("{\"agent\":\"SuperNET\",\"method\":\"login\",\"handle\":\"alice\",\"password\":\"alice\",\"passphrase\":\"alice\"}"),0)) != 0 ) { free(str); if ( (str= SuperNET_JSON(&MYINFO,cJSON_Parse("{\"agent\":\"SuperNET\",\"method\":\"login\",\"handle\":\"bob\",\"password\":\"bob\",\"passphrase\":\"bob\"}"),0)) != 0 ) free(str); } } } sleep(1); #endif } if ( arg != 0 ) SuperNET_JSON(&MYINFO,cJSON_Parse(arg),0); mainloop(&MYINFO); }
void iguana_acceptloop(void *args) { struct iguana_peer *addr; struct iguana_info *coin = args; struct pollfd pfd; int32_t sock; struct iguana_accept *ptr; uint16_t port = coin->chain->portp2p; socklen_t clilen; struct sockaddr_in cli_addr; char ipaddr[64]; uint32_t i,ipbits,flag; if ( coin->peers == 0 ) return; while ( (coin->bindsock= iguana_socket(1,"0.0.0.0",port)) < 0 ) { if ( coin->peers->localaddr != 0 ) { printf("another daemon running, no need to have iguana accept connections\n"); return; } //if ( port != IGUANA_RPCPORT ) // return; sleep(5); } printf(">>>>>>>>>>>>>>>> iguana_bindloop 127.0.0.1:%d bind sock.%d\n",port,coin->bindsock); printf("START ACCEPTING\n"); while ( coin->bindsock >= 0 ) { memset(&pfd,0,sizeof(pfd)); pfd.fd = coin->bindsock; pfd.events = POLLIN; if ( poll(&pfd,1,100) <= 0 ) continue; clilen = sizeof(cli_addr); //printf("ACCEPT (%s:%d) on sock.%d\n","127.0.0.1",coin->chain->portp2p,coin->bindsock); sock = accept(coin->bindsock,(struct sockaddr *)&cli_addr,&clilen); if ( sock < 0 ) { printf("ERROR on accept bindsock.%d errno.%d (%s)\n",coin->bindsock,errno,strerror(errno)); continue; } memcpy(&ipbits,&cli_addr.sin_addr.s_addr,sizeof(ipbits)); expand_ipbits(ipaddr,ipbits); printf("incoming (%s:%u)\n",ipaddr,cli_addr.sin_port); for (i=flag=0; i<IGUANA_MAXPEERS; i++) { if ( coin->peers->active[i].ipbits == (uint32_t)ipbits && coin->peers->active[i].usock >= 0 ) { printf("found existing peer.(%s) in slot[%d]\n",ipaddr,i); close(coin->peers->active[i].usock); coin->peers->active[i].dead = 0; coin->peers->active[i].usock = sock; coin->peers->active[i].A.port = cli_addr.sin_port; coin->peers->active[i].ready = (uint32_t)time(NULL); flag = 1; //instantdex_peerhas_clear(coin,&coin->peers->active[i]); //iguana_iAkill(coin,&coin->peers->active[i],0); //sleep(1); break; } } if ( flag != 0 ) continue; printf("NEWSOCK.%d for %x (%s)\n",sock,ipbits,ipaddr); /*if ( (uint32_t)ipbits == myinfo->myaddr.myipbits ) { }*/ if ( (addr= iguana_peerslot(coin,ipbits,1)) == 0 ) { ptr = mycalloc('a',1,sizeof(*ptr)); strcpy(ptr->ipaddr,ipaddr); ptr->ipbits = ipbits; ptr->sock = sock; ptr->port = cli_addr.sin_port; printf("queue PENDING ACCEPTS\n"); queue_enqueue("acceptQ",&coin->acceptQ,&ptr->DL,0); } else { printf("LAUNCH DEDICATED THREAD for %s:%u\n",ipaddr,cli_addr.sin_port); addr->usock = sock; addr->dead = 0; addr->A.port = cli_addr.sin_port; strcpy(addr->symbol,coin->symbol); iguana_launch(coin,"accept",iguana_dedicatedglue,addr,IGUANA_CONNTHREAD); //iguana_dedicatedloop(coin,addr); } } }