From 1283a345ea3162a459b24f9d9fd968524e49204c Mon Sep 17 00:00:00 2001 From: DeckerSU Date: Sun, 7 Oct 2018 23:00:46 +0300 Subject: [PATCH 1/5] [test] check iguana nodes connectivity before addnotary if node doesn't respond in 5 sec, we shouldn't add it (!) --- iguana/dpow/dpow_network.c | 61 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/iguana/dpow/dpow_network.c b/iguana/dpow/dpow_network.c index 9c7fd209f..09821c90f 100755 --- a/iguana/dpow/dpow_network.c +++ b/iguana/dpow/dpow_network.c @@ -1303,6 +1303,60 @@ struct dpow_block *dpow_heightfind(struct supernet_info *myinfo,struct dpow_info int32_t dpow_signedtxgen(struct supernet_info *myinfo,struct dpow_info *dp,struct iguana_info *coin,struct dpow_block *bp,int8_t bestk,uint64_t bestmask,int32_t myind,uint32_t deprec,int32_t src_or_dest,int32_t useratified); void dpow_sigscheck(struct supernet_info *myinfo,struct dpow_info *dp,struct dpow_block *bp,int32_t myind,int32_t src_or_dest,int8_t bestk,uint64_t bestmask,uint8_t pubkeys[64][33],int32_t numratified); +int checknode(char *hostname, int portno, int timeout_rw) +{ +#if defined(_linux) || defined(__linux__) + + unsigned char iguana_reply[8]; // buffer for iguana reply + + int sockfd; + struct sockaddr_in serv_addr; + struct hostent *server; + + unsigned char reply_dat[] = { 0x00, 0x53, 0x50, 0x00, 0x00, 0x70, 0x00, 0x00 }; + unsigned int reply_dat_len = 8; + struct timeval timeout; + + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd < 0) return(-1); // error opening socket + server = gethostbyname(hostname); + if (server == NULL) { close(sockfd); return(-2); } // no such host + + bzero((char *) &serv_addr, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + bcopy((char *)server->h_addr, + (char *)&serv_addr.sin_addr.s_addr, + server->h_length); + + serv_addr.sin_port = htons(portno); + + timeout.tv_sec = timeout_rw; + timeout.tv_usec = 0; + + if (setsockopt (sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, + sizeof(timeout)) < 0) + { close(sockfd); return(-3); } // set rcv timeout filed + + if (setsockopt (sockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, + sizeof(timeout)) < 0) + { close(sockfd); return(-4); } // set send timeout filed + + if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) + { close(sockfd); return(-5); } // port is closed + + int recv_count; + if( ( recv_count = recv(sockfd, iguana_reply , sizeof(iguana_reply) , 0)) < 0) + { close(sockfd); return(-6); } // recv is failed + + if ( recv_count != reply_dat_len ) { close(sockfd); return(-7); }// wrong reply size + + if (memcmp(iguana_reply, reply_dat, reply_dat_len) != 0) { close(sockfd); return(-8); } // wrong / unknown reply, possible it's not iguana on remote + + close(sockfd); +#endif // __linux__ + return 0; +} + int32_t dpow_addnotary(struct supernet_info *myinfo,struct dpow_info *dp,char *ipaddr) { char str[512]; uint32_t ipbits,*ptr; int32_t i,iter,n,retval = -1; @@ -1310,6 +1364,13 @@ int32_t dpow_addnotary(struct supernet_info *myinfo,struct dpow_info *dp,char *i return(-1); //if ( strcmp(ipaddr,"88.99.251.101") == 0 || strcmp(ipaddr,"82.202.193.100") == 0 ) // return(-1); + + // [+] Decker, if node doesn't respond in 5 sec, we shouldn't add it + if (checknode(ipaddr, Notaries_port, 5) != 0) { + printf("[Decker] Node " "\033[31m" "%s:%d" "\033[0m" "is dead!\n", ipaddr, Notaries_port); + return(-1); + } + portable_mutex_lock(&myinfo->notarymutex); if ( myinfo->dpowsock >= 0 )//&& myinfo->dexsock >= 0 ) { From 3ebe81fae296580357cbb19df207beb595186706 Mon Sep 17 00:00:00 2001 From: DeckerSU Date: Sun, 7 Oct 2018 23:05:02 +0300 Subject: [PATCH 2/5] + fix printout --- iguana/dpow/dpow_network.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iguana/dpow/dpow_network.c b/iguana/dpow/dpow_network.c index 09821c90f..d2c85e1ad 100755 --- a/iguana/dpow/dpow_network.c +++ b/iguana/dpow/dpow_network.c @@ -1367,7 +1367,7 @@ int32_t dpow_addnotary(struct supernet_info *myinfo,struct dpow_info *dp,char *i // [+] Decker, if node doesn't respond in 5 sec, we shouldn't add it if (checknode(ipaddr, Notaries_port, 5) != 0) { - printf("[Decker] Node " "\033[31m" "%s:%d" "\033[0m" "is dead!\n", ipaddr, Notaries_port); + printf("[Decker] Node " "\033[31m" "%s:%d" "\033[0m" " is dead!\n", ipaddr, Notaries_port); return(-1); } From b0488a4541a5e82bf2af710d4a58c0fec6d86fea Mon Sep 17 00:00:00 2001 From: DeckerSU Date: Mon, 8 Oct 2018 01:25:40 +0300 Subject: [PATCH 3/5] + add static list of dead ips [128] in dpow_addnotary --- iguana/dpow/dpow_network.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/iguana/dpow/dpow_network.c b/iguana/dpow/dpow_network.c index d2c85e1ad..f73a2a6ef 100755 --- a/iguana/dpow/dpow_network.c +++ b/iguana/dpow/dpow_network.c @@ -1360,16 +1360,38 @@ int checknode(char *hostname, int portno, int timeout_rw) int32_t dpow_addnotary(struct supernet_info *myinfo,struct dpow_info *dp,char *ipaddr) { char str[512]; uint32_t ipbits,*ptr; int32_t i,iter,n,retval = -1; + + // -B- [+] Decker --- + static uint32_t dead_ipbits[128]; + static int dead_ipsize; + int in_dead_flag; + uint32_t ip_pattern; + // -E- [+] Decker --- + if ( myinfo->IAMNOTARY == 0 ) return(-1); //if ( strcmp(ipaddr,"88.99.251.101") == 0 || strcmp(ipaddr,"82.202.193.100") == 0 ) // return(-1); - // [+] Decker, if node doesn't respond in 5 sec, we shouldn't add it + // -B- [+] Decker --- + if ((dead_ipsize == 0) || (dead_ipsize > 127)) { + for (int i_dead = 0; i_dead < 128; i_dead++) dead_ipbits[i_dead] = 0; + dead_ipsize = 0; + in_dead_flag = 0; + } else { + in_dead_flag = 0; + ip_pattern = (uint32_t)calc_ipbits(ipaddr); + for (int i_dead = 0; i_dead < dead_ipsize; i_dead++) if (dead_ipbits[i_dead] == ip_pattern) { in_dead_flag = 1; break; } + } + + if (in_dead_flag !=0) return -1; if (checknode(ipaddr, Notaries_port, 5) != 0) { + dead_ipbits[dead_ipsize] = (uint32_t)calc_ipbits(ipaddr); + dead_ipsize++; printf("[Decker] Node " "\033[31m" "%s:%d" "\033[0m" " is dead!\n", ipaddr, Notaries_port); - return(-1); + return -1; } + // -E- [+] Decker --- portable_mutex_lock(&myinfo->notarymutex); if ( myinfo->dpowsock >= 0 )//&& myinfo->dexsock >= 0 ) From 25686f0ebd2545552ad1c283be0153ca86831d67 Mon Sep 17 00:00:00 2001 From: DeckerSU Date: Mon, 8 Oct 2018 02:09:57 +0300 Subject: [PATCH 4/5] fix: every new ip now checks once and goes to dead or alive list --- iguana/dpow/dpow_network.c | 46 ++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/iguana/dpow/dpow_network.c b/iguana/dpow/dpow_network.c index f73a2a6ef..6675cb181 100755 --- a/iguana/dpow/dpow_network.c +++ b/iguana/dpow/dpow_network.c @@ -1362,9 +1362,10 @@ int32_t dpow_addnotary(struct supernet_info *myinfo,struct dpow_info *dp,char *i char str[512]; uint32_t ipbits,*ptr; int32_t i,iter,n,retval = -1; // -B- [+] Decker --- - static uint32_t dead_ipbits[128]; - static int dead_ipsize; - int in_dead_flag; + static uint32_t list_ipbits[128]; + static int dead_or_alive[128]; // 0 - not set, -1 - dead, 1 - alive + static int list_ipsize; + int in_list_flag; uint32_t ip_pattern; // -E- [+] Decker --- @@ -1374,23 +1375,30 @@ int32_t dpow_addnotary(struct supernet_info *myinfo,struct dpow_info *dp,char *i // return(-1); // -B- [+] Decker --- - if ((dead_ipsize == 0) || (dead_ipsize > 127)) { - for (int i_dead = 0; i_dead < 128; i_dead++) dead_ipbits[i_dead] = 0; - dead_ipsize = 0; - in_dead_flag = 0; + // every new ip in BUS topology network goes to dead or white list forever, until iguana restart + ip_pattern = (uint32_t)calc_ipbits(ipaddr); + if ((list_ipsize == 0) || (list_ipsize > 127)) { + for (int i_list = 0; i_list < 128; i_list++) { list_ipbits[i_list] = 0; dead_or_alive[i_list] = 0; } + list_ipsize = 0; + in_list_flag = -1; } else { - in_dead_flag = 0; - ip_pattern = (uint32_t)calc_ipbits(ipaddr); - for (int i_dead = 0; i_dead < dead_ipsize; i_dead++) if (dead_ipbits[i_dead] == ip_pattern) { in_dead_flag = 1; break; } - } - - if (in_dead_flag !=0) return -1; - if (checknode(ipaddr, Notaries_port, 5) != 0) { - dead_ipbits[dead_ipsize] = (uint32_t)calc_ipbits(ipaddr); - dead_ipsize++; - printf("[Decker] Node " "\033[31m" "%s:%d" "\033[0m" " is dead!\n", ipaddr, Notaries_port); - return -1; - } + in_list_flag = -1; + for (int i_list = 0; i_list < list_ipsize; i_list++) if (list_ipbits[i_list] == ip_pattern) { in_list_flag = i_list; break; } + } + + if (in_list_flag == -1) { + list_ipbits[list_ipsize] = ip_pattern; + if (checknode(ipaddr, Notaries_port, 5) != 0) { + dead_or_alive[list_ipsize] = -1; + list_ipsize++; + printf("[Decker] Node " "\033[31m" "%s:%d" "\033[0m" " is dead!\n", ipaddr, Notaries_port); + return -1; + } else { + dead_or_alive[list_ipsize] = 1; + list_ipsize++; + } + } else + if (dead_or_alive[in_list_flag] == -1) return -1; // -E- [+] Decker --- portable_mutex_lock(&myinfo->notarymutex); From ce89f3d3d28d7b95b55760766d0dd32c8e8acf21 Mon Sep 17 00:00:00 2001 From: DeckerSU Date: Mon, 8 Oct 2018 13:50:21 +0300 Subject: [PATCH 5/5] + CHECKNODEIP compilation flag for dead / alive IP lists and checks --- iguana/dpow/dpow_network.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/iguana/dpow/dpow_network.c b/iguana/dpow/dpow_network.c index 6675cb181..c6d3db7d5 100755 --- a/iguana/dpow/dpow_network.c +++ b/iguana/dpow/dpow_network.c @@ -1303,6 +1303,7 @@ struct dpow_block *dpow_heightfind(struct supernet_info *myinfo,struct dpow_info int32_t dpow_signedtxgen(struct supernet_info *myinfo,struct dpow_info *dp,struct iguana_info *coin,struct dpow_block *bp,int8_t bestk,uint64_t bestmask,int32_t myind,uint32_t deprec,int32_t src_or_dest,int32_t useratified); void dpow_sigscheck(struct supernet_info *myinfo,struct dpow_info *dp,struct dpow_block *bp,int32_t myind,int32_t src_or_dest,int8_t bestk,uint64_t bestmask,uint8_t pubkeys[64][33],int32_t numratified); +#ifdef CHECKNODEIP int checknode(char *hostname, int portno, int timeout_rw) { #if defined(_linux) || defined(__linux__) @@ -1356,11 +1357,13 @@ int checknode(char *hostname, int portno, int timeout_rw) #endif // __linux__ return 0; } +#endif int32_t dpow_addnotary(struct supernet_info *myinfo,struct dpow_info *dp,char *ipaddr) { char str[512]; uint32_t ipbits,*ptr; int32_t i,iter,n,retval = -1; + #ifdef CHECKNODEIP // -B- [+] Decker --- static uint32_t list_ipbits[128]; static int dead_or_alive[128]; // 0 - not set, -1 - dead, 1 - alive @@ -1368,12 +1371,14 @@ int32_t dpow_addnotary(struct supernet_info *myinfo,struct dpow_info *dp,char *i int in_list_flag; uint32_t ip_pattern; // -E- [+] Decker --- + #endif if ( myinfo->IAMNOTARY == 0 ) return(-1); //if ( strcmp(ipaddr,"88.99.251.101") == 0 || strcmp(ipaddr,"82.202.193.100") == 0 ) // return(-1); + #ifdef CHECKNODEIP // -B- [+] Decker --- // every new ip in BUS topology network goes to dead or white list forever, until iguana restart ip_pattern = (uint32_t)calc_ipbits(ipaddr); @@ -1400,6 +1405,7 @@ int32_t dpow_addnotary(struct supernet_info *myinfo,struct dpow_info *dp,char *i } else if (dead_or_alive[in_list_flag] == -1) return -1; // -E- [+] Decker --- + #endif portable_mutex_lock(&myinfo->notarymutex); if ( myinfo->dpowsock >= 0 )//&& myinfo->dexsock >= 0 )