Exemplo n.º 1
0
char *SuperNET_gotpacket(char *msg,int32_t duration,char *ip_port)
{
    static int flood,duplicates;
    cJSON *json;
    uint16_t p2pport;
    struct pserver_info *pserver;
    uint64_t txid;
    struct sockaddr prevaddr;
    int32_t len,createdflag,valid;
    unsigned char packet[2*MAX_JSON_FIELD];
    char ipaddr[64],txidstr[64],retjsonstr[2*MAX_JSON_FIELD],verifiedNXTaddr[64],*cmdstr,*retstr;
    if ( SUPERNET_PORT != _SUPERNET_PORT )
        return(clonestr("{\"error\":private SuperNET}"));
    strcpy(retjsonstr,"{\"result\":null}");
    if ( Debuglevel > 2 )
        printf("gotpacket.(%s) duration.%d from (%s)\n",msg,duration,ip_port);
    if ( Finished_loading == 0 )
    {
        if ( is_hexstr(msg) == 0 )
        {
            //printf("QUEUE.(%s)\n",msg);
            return(block_on_SuperNET(0,msg));
        }
        return(clonestr(retjsonstr));
    }
    p2pport = parse_ipaddr(ipaddr,ip_port);
    uv_ip4_addr(ipaddr,0,(struct sockaddr_in *)&prevaddr);
    pserver = get_pserver(0,ipaddr,0,p2pport);
    len = (int32_t)strlen(msg);
    if ( is_hexstr(msg) != 0 )
    {
        len >>= 1;
        len = decode_hex(packet,len,msg);
        txid = calc_txid(packet,len);//hash,sizeof(hash));
        sprintf(txidstr,"%llu",(long long)txid);
        MTadd_hashtable(&createdflag,&Global_pNXT->msg_txids,txidstr);
        if ( createdflag == 0 )
        {
            duplicates++;
            return(clonestr("{\"error\":\"duplicate msg\"}"));
        }
        if ( (len<<1) == 30 ) // hack against flood
            flood++;
        if ( Debuglevel > 0 )
            printf("gotpacket %d | Finished_loading.%d | flood.%d duplicates.%d\n",duration,Finished_loading,flood,duplicates);
        if ( is_encrypted_packet(packet,len) != 0 )
            process_packet(0,retjsonstr,packet,len,0,&prevaddr,ipaddr,0);
        /*else if ( (obookid= is_orderbook_tx(packet,len)) != 0 )
        {
            if ( update_orderbook_tx(1,obookid,(struct orderbook_tx *)packet,txid) == 0 )
            {
                ((struct orderbook_tx *)packet)->txid = txid;
                sprintf(retjsonstr,"{\"result\":\"SuperNET_gotpacket got obbokid.%llu packet txid.%llu\"}",(long long)obookid,(long long)txid);
            } else sprintf(retjsonstr,"{\"result\":\"SuperNET_gotpacket error updating obookid.%llu\"}",(long long)obookid);
        }*/ else sprintf(retjsonstr,"{\"error\":\"SuperNET_gotpacket cant find obookid\"}");
    }
Exemplo n.º 2
0
char *init_NXTservices(char *JSON_or_fname,char *myipaddr)
{
    static int32_t zero,one = 1;
    struct coin_info *cp;
    struct NXThandler_info *mp = Global_mp;    // seems safest place to have main data structure
    if ( Debuglevel > 0 )
       printf("init_NXTservices.(%s)\n",myipaddr);
    UV_loop = uv_default_loop();
    myipaddr = init_MGWconf(JSON_or_fname,myipaddr);
    mp->udp = start_libuv_udpserver(4,SUPERNET_PORT,on_udprecv);
    if ( (cp= get_coin_info("BTCD")) != 0 && cp->bridgeport != 0 )
        cp->bridgeudp = start_libuv_udpserver(4,cp->bridgeport,on_bridgerecv);
    if ( 0 )
    {
        uint32_t before,after;
        int32_t numinputs;
        char *rawtx = clonestr("0100000074fc77540156a5b19aaada0496780b1fbce72f7647da5f940883da7ee5d5774f673c6703c401000000fdfd0000483045022100f0b26a43136af6c28d381f461a9fcd30309788456cb81784dbc68ee85ae4151d022036fc96c4edd7b87e762bb139d5d34838a88054c37173236236f72940b2e5309801473044022068ae115a397d9a6f462b78416d58582736fa38ecc4eab2c26759e1e58d6326bc02204e148ab84d49b0f1c8aab1033819ad90c536c604ce24dab419013a5914d39d8a014c695221035827b3c432eb5a528a21657d36a1b61dd85078a6ba5f328bed2d928c173a46c421024ae5e013fda966cf8544025534012156f84a40a5672a894c42e144b0664202502102acdb9c782d499de9b98e8b166fc22bd68895e2293cb49e4a2e071f1254d1a7aa53aeffffffff0340420f00000000001976a9148466f34f39c23547abf922d422e3e5322fdf156588ac20cd8800020000001976a914cd073e0a5d4225f2577113400c3abf9ac1ad2cc488ac60c791e50600000017a914194a1499c343beefe3127e041f480ee4aef058408700000000");
        before = extract_sequenceid(&numinputs,get_coin_info("BTCD"),rawtx,0);
        replace_bitcoin_sequenceid(get_coin_info("BTCD"),rawtx,12345678);
        after = extract_sequenceid(&numinputs,get_coin_info("BTCD"),rawtx,0);
        printf("newtx.(%s) before.%u after.%u\n",rawtx,before,after); getchar();
    }
    if ( myipaddr != 0 )
        strcpy(mp->ipaddr,myipaddr);
//#ifndef __APPLE__
//    Coinloop(0);
//#else
    if ( IS_LIBTEST > 1 && portable_thread_create((void *)Coinloop,0) == 0 )
        printf("ERROR hist Coinloop SSL\n");
//#endif
    Finished_loading = 1;
    if ( Debuglevel > 0 )
        printf("run_UVloop\n");
    if ( portable_thread_create((void *)run_UVloop,Global_mp) == 0 )
        printf("ERROR hist process_hashtablequeues\n");
    if ( portable_thread_create((void *)run_libwebsockets,&one) == 0 )
        printf("ERROR hist run_libwebsockets SSL\n");
    while ( SSL_done == 0 )
        usleep(100000);
    if ( portable_thread_create((void *)run_libwebsockets,&zero) == 0 )
        printf("ERROR hist run_libwebsockets\n");
    sleep(3);
    {
        struct coin_info *cp;
        while ( (cp= get_coin_info("BTCD")) == 0 )
            sleep(1);
        parse_ipaddr(cp->myipaddr,myipaddr);
        bind_NXT_ipaddr(cp->srvpubnxtbits,myipaddr);
    }
    return(myipaddr);
}
Exemplo n.º 3
0
static int
parse_ip(struct ipset_session *session,
	 enum ipset_opt opt, const char *str, enum ipaddr_type addrtype)
{
	struct ipset_data *data = ipset_session_data(session);
	uint8_t family = ipset_data_family(data);

	if (family == AF_UNSPEC) {
		family = AF_INET;
		ipset_data_set(data, IPSET_OPT_FAMILY, &family);
	}

	switch (addrtype) {
	case IPADDR_PLAIN:
		if (range_separator(str)
		    || (cidr_separator(str) && !cidr_hostaddr(str, family)))
			return syntax_err("plain IP address must be supplied: %s",
					  str);
		break;
	case IPADDR_NET:
		if (!cidr_separator(str) || range_separator(str))
			return syntax_err("IP/netblock must be supplied: %s",
					  str);
		break;
	case IPADDR_RANGE:
		if (!range_separator(str) || cidr_separator(str))
			return syntax_err("IP-IP range must supplied: %s",
					  str);
		break;
	case IPADDR_ANY:
	default:
		break;
	}

	return parse_ipaddr(session, opt, str, family);
}
Exemplo n.º 4
0
/*! \brief Lease file parser.  The parser can only read ISC DHCPD
 * dhcpd.leases file format.  */
int parse_leases(void)
{
	FILE *dhcpd_leases;
	char *line, *ipstring, macstring[20];
	union ipaddr_t addr;
	struct stat lease_file_stats;
	bool ethernets = false;
	struct leases_t *lease;

	dhcpd_leases = fopen(config.dhcpdlease_file, "r");
	if (dhcpd_leases == NULL) {
		err(EXIT_FAILURE, "parse_leases: %s", config.dhcpdlease_file);
	}
#ifdef HAVE_POSIX_FADVISE
# ifdef POSIX_FADV_WILLNEED
	posix_fadvise(fileno(dhcpd_leases), 0, 0, POSIX_FADV_WILLNEED);
	if (errno) {
		err(EXIT_FAILURE, "parse_leases: fadvise %s",
		    config.dhcpdlease_file);
	}
# endif				/* POSIX_FADV_WILLNEED */
# ifdef POSIX_FADV_SEQUENTIAL
	posix_fadvise(fileno(dhcpd_leases), 0, 0, POSIX_FADV_SEQUENTIAL);
	if (errno) {
		err(EXIT_FAILURE, "parse_leases: fadvise %s",
		    config.dhcpdlease_file);
	}
# endif				/* POSIX_FADV_SEQUENTIAL */
#endif				/* HAVE_POSIX_FADVISE */

	/* I found out that there's one lease address per 300 bytes in
	 * dhcpd.leases file. Malloc is little bit pessimistic and uses 250.
	 * If someone has higher density in lease file I'm interested to
	 * hear about that. */
	if (stat(config.dhcpdlease_file, &lease_file_stats)) {
		err(EXIT_FAILURE, "parse_leases: %s", config.dhcpdlease_file);
	}

	line = xmalloc(sizeof(char) * MAXLEN);
	ipstring = xmalloc(sizeof(char) * MAXLEN);
	if (config.output_format[0] == 'X' || config.output_format[0] == 'J') {
		ethernets = true;
	}

	const char **p = prefixes[config.dhcp_version];
	int *l = prefix_length[config.dhcp_version];

/*! \def HAS_PREFIX(line, type)
 * \brief A macro to match IPv4 and IPv6 lease lines.
 *
 * FIXME: This macro should have better name. The HAS_PREFIX sounds like
 * some sort of prefix length test. */
#define HAS_PREFIX(line, type) xstrstr((line), p[type], l[type])

	while (!feof(dhcpd_leases)) {
		if (!fgets(line, MAXLEN, dhcpd_leases) && ferror(dhcpd_leases)) {
			err(EXIT_FAILURE, "parse_leases: %s",
			    config.dhcpdlease_file);
		}
		/* It's a lease, save IP */
		if (HAS_PREFIX(line, PREFIX_LEASE)) {
			nth_field(ipstring, line + l[PREFIX_LEASE]);
			parse_ipaddr(ipstring, &addr);
			continue;
		}
		if (HAS_PREFIX(line, PREFIX_BINDING_STATE_FREE) ||
		    HAS_PREFIX(line, PREFIX_BINDING_STATE_ABANDONED) ||
		    HAS_PREFIX(line, PREFIX_BINDING_STATE_EXPIRED) ||
		    HAS_PREFIX(line, PREFIX_BINDING_STATE_RELEASED)) {
			/* remove old entry, if exists */
			if ((lease = find_lease(&addr)) != NULL) {
				delete_lease(lease);
			}
			add_lease(&addr, FREE);
			continue;
		}
		/* Copy IP to correct array */
		if (HAS_PREFIX(line, PREFIX_BINDING_STATE_ACTIVE)) {
			/* remove old entry, if exists */
			if ((lease = find_lease(&addr)) != NULL) {
				delete_lease(lease);
			}
			add_lease(&addr, ACTIVE);
			continue;
		}
		if (HAS_PREFIX(line, PREFIX_BINDING_STATE_BACKUP)) {
			/* remove old entry, if exists */
			if ((lease = find_lease(&addr)) != NULL) {
				delete_lease(lease);
			}
			add_lease(&addr, BACKUP);
			config.backups_found = true;
			continue;
		}
		if (ethernets && (xstrstr(line, "  hardware ethernet", 19))) {
			nth_field(macstring, line + 20);
			macstring[17] = '\0';
			if ((lease = find_lease(&addr)) != NULL) {
				lease->ethernet = xstrdup(macstring);
			}
		}
	}
#undef HAS_PREFIX
	free(line);
	free(ipstring);
	fclose(dhcpd_leases);
	return 0;
}