int32_t ismyaddress(char *server) { char ipaddr[64]; uint32_t ipbits; if ( strncmp(server,"tcp://",6) == 0 ) server += 6; else if ( strncmp(server,"ws://",5) == 0 ) server += 5; if ( (ipbits= is_ipaddr(server)) != 0 ) { if ( strcmp(server,SUPERNET.myipaddr) == 0 || calc_ipbits(SUPERNET.myipaddr) == ipbits ) { printf("(%s) MATCHES me (%s)\n",server,SUPERNET.myipaddr); return(1); } } else { if ( SUPERNET.hostname[0] != 0 && strcmp(SUPERNET.hostname,server) == 0 ) return(1); if ( conv_domainname(ipaddr,server) == 0 && (strcmp(SUPERNET.myipaddr,ipaddr) == 0 || strcmp(SUPERNET.hostname,ipaddr) == 0) ) return(1); } //printf("(%s) is not me (%s)\n",server,SUPERNET.myipaddr); return(0); }
unsigned long safe_gethostbyname(const char *host) { struct hostent *hp, hp_a, *hp_b; char buf[1024]; int hp_errno; u_long ip; if (is_ipaddr(host)) return inet_addr(host); #if HAVE_GETHOSTBYNAME_R #if defined(linux) if (!gethostbyname_r(host, &hp_a, buf, sizeof(buf), &hp_b, &hp_errno)) hp = &hp_a; else hp = NULL; #else hp = gethostbyname_r(host, &hp_a, buf, sizeof(buf), &hp_errno); #endif #else pthread_mutex_lock(&nss_lock); hp = gethostbyname(host); #endif if (!hp) { _pam_log(LOG_ERR, "safe_gethostbyname: hostname '%s' not found", host); #ifndef HAVE_GETHOSTBYNAME_R pthread_mutex_unlock(&nss_lock); #endif return 0; } #ifndef HAVE_GETHOSTBYNAME_R pthread_mutex_unlock(&nss_lock); #endif return *((unsigned int *) hp->h_addr); }
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); }
void TestExports::testResolving() { // need to avoid duplicate entries in exports paths const char *correct_exports[] = { "/tmp localhost", // parsed as username localhost "/bin @localhost", // parsed as network 127.0.0.1/32, ::1/128 }; const size_t correct_count = sizeof(correct_exports) / sizeof(correct_exports[0]); struct rfs_list *exports = NULL; std::ofstream stream; stream.open(exportsfile, std::ios_base::out | std::ios_base::trunc); CPPUNIT_ASSERT(stream.is_open()); for (size_t i = 0; i < correct_count; ++i) { stream << correct_exports[i] << std::endl; } stream.close(); CPPUNIT_ASSERT(!stream.fail()); CPPUNIT_ASSERT(parse_exports(exportsfile, &exports, 0) == 0); size_t count = list_length(exports); CPPUNIT_ASSERT(count == correct_count); struct rfs_list *export_item = exports; while (export_item != NULL) { struct rfs_export *rec = (struct rfs_export *)export_item->data; CPPUNIT_ASSERT(rec->users != 0); struct user_rec *user = (struct user_rec *)rec->users->data; CPPUNIT_ASSERT(export_item == exports ? user->network == NULL : user->network != NULL && is_ipaddr(user->network) != 0); if (user->network != NULL) { CPPUNIT_ASSERT(user->prefix_len > 0); } export_item = export_item->next; } release_exports(&exports); CPPUNIT_ASSERT(exports == NULL); }
void process_userinput(char *_line) { static char *line,*line2,*match = "./BitcoinDarkd SuperNET '"; char plugin[512],ipaddr[1024],method[512],*cmdstr,*retstr; cJSON *json; int len,timeout,broadcastflag = 0; len = (int32_t)strlen(match); if ( _line[strlen(_line)-1] == '\'' && strncmp(_line,match,len) == 0 ) { _line[strlen(_line)-1] = 0; _line += len; } if ( (json= cJSON_Parse(_line)) != 0 ) { char *process_nn_message(int32_t sock,char *jsonstr); free_json(json); char *SuperNET_JSON(char *jsonstr); retstr = SuperNET_JSON(_line); //retstr = process_nn_message(-1,line); //retstr = nn_loadbalanced((uint8_t *)line,(int32_t)strlen(line)+1); fprintf(stderr,"console.(%s) -> (%s)\n",_line,retstr); return; } else printf("cant parse.(%s)\n",line); printf("[%s]\n",_line); if ( line == 0 ) line = calloc(1,65536), line2 = calloc(1,65536); expand_aliases(line,line2,65536,_line); if ( (line= localcommand(line)) == 0 ) return; if ( line[0] == '!' ) broadcastflag = 1, line++; settoken(ipaddr,line); printf("expands to: %s [%s] %s\n",broadcastflag != 0 ? "broadcast": "",line,ipaddr); if ( is_ipaddr(ipaddr) != 0 ) { line += strlen(ipaddr) + 1; if ( (cmdstr = parse_expandedline(plugin,sizeof(plugin),method,&timeout,line,broadcastflag)) != 0 ) { printf("ipaddr.(%s) (%s)\n",ipaddr,line); //retstr = nn_direct(ipaddr,(uint8_t *)line,(int32_t)strlen(line)+1); printf("deprecated (%s) -> (%s)\n",line,cmdstr); free(cmdstr); } return; } if ( (cmdstr= parse_expandedline(plugin,sizeof(plugin),method,&timeout,line,broadcastflag)) != 0 ) { retstr = process_user_json(plugin,method,cmdstr,broadcastflag,timeout != 0 ? timeout : SUPERNET.PLUGINTIMEOUT); printf("CONSOLE (%s) -> (%s) -> (%s)\n",line,cmdstr,retstr); free(cmdstr); } }
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\"}")); }
static bool test_one_octet_wildcard() { emit_test("Is it identified as an IP address with one octet and a wildcard?"); char* input = strdup( "66.*" ); emit_input_header(); emit_param("IP", input); int result = is_ipaddr( input, NULL ); free( input ); emit_output_expected_header(); emit_retval("%s", tfstr(TRUE)); emit_output_actual_header(); emit_retval("%s", tfstr(result)); if(result != TRUE) { FAIL; } PASS; }
static bool test_start_wildcard() { emit_test("Does it fail correctly with a wildcard and then some octets?"); char* input = strdup( "*.0.42.1" ); emit_input_header(); emit_param("IP", input); int result = is_ipaddr( input, NULL ); free( input ); emit_output_expected_header(); emit_retval("%s", tfstr(FALSE)); emit_output_actual_header(); emit_retval("%s", tfstr(result)); if(result != FALSE) { FAIL; } PASS; }
static bool test_only_wildcard() { emit_test("Does it work with nothing but a single wildcard?"); char* input = strdup( "*" ); emit_input_header(); emit_param("IP", input); int result = is_ipaddr( input, NULL ); free( input ); emit_output_expected_header(); emit_retval("%s", tfstr(TRUE)); emit_output_actual_header(); emit_retval("%s", tfstr(result)); if(result != TRUE) { FAIL; } PASS; }
static bool test_lower_bound() { emit_test("Does it work with one octet <0?"); char* input = strdup( "-2.71.82.81" ); emit_input_header(); emit_param("IP", input); int result = is_ipaddr( input, NULL ); free( input ); emit_output_expected_header(); emit_retval("%s", tfstr(FALSE)); emit_output_actual_header(); emit_retval("%s", tfstr(result)); if(result != FALSE) { FAIL; } PASS; }
void process_userinput(char *_line) { static char *line,*line2; char plugin[512],ipaddr[1024],method[512],*cmdstr,*retstr; cJSON *json; int timeout,broadcastflag = 0; printf("[%s]\n",_line); if ( line == 0 ) line = calloc(1,65536), line2 = calloc(1,65536); expand_aliases(line,line2,65536,_line); if ( (line= localcommand(line)) == 0 ) return; if ( line[0] == '!' ) broadcastflag = 1, line++; if ( (json= cJSON_Parse(line)) != 0 ) { char *process_nn_message(int32_t sock,char *jsonstr); free_json(json); retstr = process_nn_message(-1,line); //retstr = nn_loadbalanced((uint8_t *)line,(int32_t)strlen(line)+1); printf("console.(%s) -> (%s)\n",line,retstr); return; } settoken(ipaddr,line); printf("expands to: %s [%s] %s\n",broadcastflag != 0 ? "broadcast": "",line,ipaddr); if ( is_ipaddr(ipaddr) != 0 ) { line += strlen(ipaddr) + 1; if ( (cmdstr = parse_expandedline(plugin,method,&timeout,line,broadcastflag)) != 0 ) { printf("ipaddr.(%s) (%s)\n",ipaddr,line); //retstr = nn_direct(ipaddr,(uint8_t *)line,(int32_t)strlen(line)+1); printf("deprecated (%s) -> (%s)\n",line,cmdstr); free(cmdstr); } return; } if ( (cmdstr= parse_expandedline(plugin,method,&timeout,line,broadcastflag)) != 0 ) { retstr = process_user_json(plugin,method,cmdstr,broadcastflag,timeout != 0 ? timeout : SUPERNET.PLUGINTIMEOUT); printf("CONSOLE (%s) -> (%s) -> (%s)\n",line,cmdstr,retstr); free(cmdstr); } }
static bool test_normal_case() { emit_test("Is normal input identified correctly?"); char* input = strdup( "66.184.142.51" ); emit_input_header(); emit_param("IP", input); struct in_addr ip; unsigned char* byte = (unsigned char*) &ip; int result = is_ipaddr( input, &ip ); free( input ); emit_output_expected_header(); emit_param("IP", "%d.%d.%d.%d", 66, 184, 142, 51); emit_retval("%s", tfstr(TRUE)); emit_output_actual_header(); emit_param("IP", "%d.%d.%d.%d", *byte, *(byte+1), *(byte+2), *(byte+3)); emit_retval("%s", tfstr(result)); if(result != TRUE || *byte != 66 || *(byte+1) != 184 || *(byte+2) != 142 || *(byte+3) != 51) { FAIL; } PASS; }
/*关于链路维护,只维护一个,多链路由上层处理*/ static void rt_thread_gsm_socket( void* parameter ) { char buf[128]; rt_err_t err; int i; AT_CMD_RESP at_cmd_resp; /*挂断连接*/ if( curr_socket.state == SOCKET_CLOSE ) { return; } /*建立连接*/ if( curr_socket.state != SOCKET_START ) { return; } if( is_ipaddr( curr_socket.ipstr ) ) /*是IP地址*/ { strcpy( curr_socket.ip_addr, curr_socket.ipstr ); curr_socket.state = SOCKET_CONNECT; }else { curr_socket.state = SOCKET_DNS; } if( curr_socket.state == SOCKET_DNS ) { sprintf( buf, "AT%%DNSR=\"%s\"\r\n", curr_socket.ipstr ); err = gsm_send( buf, resp_DNSR, RT_NULL, RESP_TYPE_FUNC_WITHOK, RT_TICK_PER_SECOND * 10, 1 ); if( err != RT_EOK ) { curr_socket.state = SOCKET_DNS_ERR; goto lbl_gsm_socket_end; } curr_socket.state = SOCKET_CONNECT; } if( curr_socket.state == SOCKET_CONNECT ) { if( curr_socket.type == 'u' ) { sprintf( buf, "AT%%IPOPENX=%d,\"UDP\",\"%s\",%d\r\n", curr_socket.linkno, curr_socket.ip_addr, curr_socket.port ); }else { sprintf( buf, "AT%%IPOPENX=%d,\"TCP\",\"%s\",%d\r\n", curr_socket.linkno, curr_socket.ip_addr, curr_socket.port ); } err = gsm_send( buf, RT_NULL, "CONNECT", RESP_TYPE_STR, RT_TICK_PER_SECOND * 10, 1 ); if( err != RT_EOK ) { curr_socket.state = SOCKET_CONNECT_ERR; goto lbl_gsm_socket_end; } } curr_socket.state = SOCKET_READY; lbl_gsm_socket_end: gsm_state = GSM_TCPIP; /*socket过程处理完成,结果在state中*/ rt_kprintf( "%08d gsm_socket>end socket.state=%d\r\n", rt_tick_get( ), curr_socket.state ); }
int main(int argc, char* argv[]) { int i = 0, res = 0, index = 0, datalen = 0; char** data = NULL; nc_t nyan = { 1, 0, 0, NULL, }; struct option opts[] = { {"send", no_argument, NULL, 's'}, {"port", required_argument, NULL, 'p'}, {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'v'}, {0, 0, 0, 0}, }; while ((res = getopt_long(argc, argv, "sp:hv", opts, &index)) != -1) { switch (res) { case 's': nyan.sflag = 1; break; case 'p': for (i = 0; i < strlen(optarg); i++) { if (!isdigit(*(optarg + i))) { fprintf(stderr, "%s: %s: invalid port number\n", PROGNAME, optarg); return -1; } } nyan.net_port = atoi(optarg); nyan.pflag = 1; break; case 'h': print_usage(); case 'v': print_version(); case '?': return -1; } } /* check required argument */ if (optind != (argc - 1)) { fprintf(stderr, "%s: invalid values\n", PROGNAME); return 1; } /* set default port */ if (nyan.pflag == 0) nyan.net_port = PORT; /* set hostname */ if (is_ipaddr(argv[optind]) == 0) { nyan.net_ipv4_addr = nslookup(argv[optind]); } else { if ((nyan.net_ipv4_addr = (char*)malloc(sizeof(char) * (strlen(argv[optind])) + 1)) == NULL) fprintf(stderr, "%s: malloc() failure\n", PROGNAME); else strcpy(nyan.net_ipv4_addr, argv[optind]); } /* reading stdin */ if (nyan.sflag == 1) { if ((data = p_read_file_char(512, 512, stdin)) == NULL) { fprintf(stderr, "%s: p_read_file_char() failure\n", PROGNAME); return 3; } datalen = p_count_file_lines(data); if (sender(&nyan, data, datalen) != 0) { fprintf(stderr, "%s: sender failure\n", PROGNAME); release(&nyan); return 4; } } release(&nyan); return 0; }