Example #1
0
int cmd_exec( REPLY *r, int argc, char **argv ) {
	char addrbuf[FULL_ADDSTRLEN+1];
	time_t lifetime;
	int minutes;
	IP addrs[16];
	int port;
	int count;
	static struct value_t *value;
	char *p;
	int rc = 0;

	if( argc == 0 ) {

		/* Print usage */
		r_printf( r, cmd_usage );
		if( r->allow_debug ) {
			r_printf( r, cmd_usage_debug );
		}
		rc = 1;

	} else if( match( argv[0], "import" ) && argc == 2 ) {

		rc = cmd_import( r, argv[1] );
#if 0
	} else if( match( argv[0], "lookup_node" ) && argc == 2 ) {

		/* Check searches for node */
		rc = kad_lookup_node( argv[1], &addrs[0] );
		if( rc == 0 ) {
			r_printf( r, "%s\n", str_addr( &addrs[0], addrbuf ) );
		} else if( rc == 1 ) {
			r_printf( r ,"No search found.\n" );
			rc = 1;
		} else if( rc == 2 ) {
			r_printf( r ,"Invalid id format. 20 digit hex string expected.\n" );
			rc = 1;
		} else {
			rc = 1;
		}
#endif
	} else if( match( argv[0], "lookup" ) && argc == 2 ) {

		size_t num = N_ELEMS(addrs);
		size_t i;

		/* Check searches for node */
		rc = kad_lookup_value( argv[1], addrs, &num );

		if( rc >= 0 && num > 0 ) {
			for( i = 0; i < num; ++i ) {
				r_printf( r, "%s\n", str_addr( &addrs[i], addrbuf ) );
			}
		} else if( rc < 0 ) {
			r_printf( r ,"Some error occured.\n" );
			rc = 1;
		} else if( rc == 0 ) {
			r_printf( r ,"Search in progress.\n" );
			rc = 1;
		} else {
			r_printf( r ,"Search started.\n" );
			rc = 1;
		}
	} else if( match( argv[0], "status" ) && argc == 1 ) {

		/* Print node id and statistics */
		cmd_print_status( r );

	} else if( match( argv[0], "announce" ) && (argc == 1 || argc == 2 || argc == 3) ) {

		if( argc == 1 ) {
			/* Announce all values; does not update value.refreshed */
			count = 0;
			value = values_get();
			while( value ) {
				kad_announce_once( value->id, value->port );
				count++;
				value = value->next;
			}
			r_printf( r ,"%d announcements started.\n", count );
			rc = 0;
			goto end;
		} else if( argc == 2 ) {
			minutes = 0;
			lifetime = 0;
		} else if( argc == 3 ) {
			minutes = atoi( argv[2] );
			if( minutes < 0 ) {
				minutes = 0;
				lifetime = LONG_MAX;
			} else {
				/* Round up to multiple of 30 minutes */
				minutes = (30 * (minutes/30 + 1));
				lifetime = (time_now_sec() + (minutes * 60));
			}
		} else {
			/* Make compilers happy */
			exit( 1 );
		}

		int is_random_port = 0;

		/* Find <id>:<port> delimiter */
		p = strchr( argv[1], ':' );

		if( p ) {
			*p = '\0';
			port = port_parse( p + 1, -1 );
		} else {
			/* A valid port will be choosen inside kad_announce() */
			port = 0;
			is_random_port = 1;
		}

		if( kad_announce( argv[1], port, lifetime ) >= 0 ) {
#ifdef FWD
			if( !is_random_port ) {
				forwardings_add( port, lifetime);
			}
#endif
			if( lifetime == 0 ) {
				r_printf( r ,"Start single announcement now.\n" );
			} else if( lifetime == LONG_MAX ) {
				r_printf( r ,"Start regular announcements for the entire run time (%sport %d).\n", (is_random_port ? "random " : ""), port );
			} else {
				r_printf( r ,"Start regular announcements for %d minutes (%sport %d).\n", minutes, (is_random_port ? "random " : ""), port );
			}
		} else {
			r_printf( r ,"Invalid port or query too long.\n" );
			rc = 1;
		}

	} else if( match( argv[0], "blacklist" ) && argc == 2 ) {

		rc = cmd_blacklist( r, argv[1] );

	} else if( match( argv[0], "export" ) && argc == 1 ) {

		rc = cmd_export( r );

	} else if( match( argv[0], "list" ) && argc == 2 && r->allow_debug ) {

		if( gconf->is_daemon == 1 ) {
			r_printf( r ,"The 'list' command is not available while KadNode runs as daemon.\n" );
			rc = 1;
			goto end;
		} else if( match( argv[1], "blacklist" ) ) {
			kad_debug_blacklist( STDOUT_FILENO );
			rc = 0;
		} else if( match( argv[1], "buckets" ) ) {
			kad_debug_buckets( STDOUT_FILENO );
			rc = 0;
		} else if( match( argv[1], "constants" ) ) {
			kad_debug_constants( STDOUT_FILENO );
			rc = 0;
#ifdef FWD
		} else if( match( argv[1], "forwardings" ) ) {
			forwardings_debug( STDOUT_FILENO );
			rc = 0;
#endif
#ifdef AUTH
		} else if( match( argv[1], "pkeys" ) ) {
			auth_debug_pkeys( STDOUT_FILENO );
			rc = 0;
		} else if( match( argv[1], "skeys" ) ) {
			auth_debug_skeys( STDOUT_FILENO );
			rc = 0;
#endif
		} else if( match( argv[1], "results" ) ) {
			results_debug( STDOUT_FILENO );
			rc = 0;
		} else if( match( argv[1], "searches" ) ) {
			kad_debug_searches( STDOUT_FILENO );
			rc = 0;
		} else if( match( argv[1], "storage" ) ) {
			kad_debug_storage( STDOUT_FILENO );
			rc = 0;
		} else if( match( argv[1], "values" ) ) {
			values_debug( STDOUT_FILENO );
			rc = 0;
		} else {
			dprintf( STDERR_FILENO, "Unknown argument.\n" );
			rc = 1;
		}
		r_printf( r ,"\nOutput send to console.\n" );

	} else {
		/* print usage */
		r_printf( r, cmd_usage );
		if( r->allow_debug ) {
			r_printf( r, cmd_usage_debug );
		}
		rc = 1;
	}

end:
	;
	return rc;
}
Example #2
0
static int ip4_parse(char *ipstr, int network_order, int *not_flag, unsigned *host,
                                                unsigned *mask, PORTSET *portset)
{
    char *saved, *s_copy, *maskptr, *endp, *portptr = NULL;
    struct in_addr addrstuff;
    
    if(!ipstr || !not_flag || !host || !mask) 
        return -1;


    if(*ipstr == '\0')
        return -3;

    saved = s_copy = strdup(ipstr);
    
    if(!s_copy)
    {
        return -2;
    }
    else
    {
        while(isspace((int)*s_copy))
            s_copy++;

        if(*s_copy == '\0')
        {
            free(saved);
            return -3;
        }

        if(*s_copy == '!')
        {
            *not_flag = 1;
            s_copy++;

            if(*s_copy == '\0')
            {
                free(saved);
                return -3;
            }
        }
        else
        {
            *not_flag = 0;
        }
        
        if( (endp = strstr(s_copy, "]")) ) 
        {
            /* Removing trailing ']' */
            *endp = 0;
        }
        if( (endp = strstr(s_copy, ",")) ) 
        {
            /* Removing trailing ',' */
            *endp = 0;
        }

        portptr = strstr(s_copy, ":");

        maskptr = strstr(s_copy, "/");

      
        if(!maskptr)
        {
            /* assume this is a host */
            *mask = 0xFFFFFFFF;
        }
        else
        {
            *maskptr = '\0';
            maskptr++;
        }

        if(!portptr)
        {
            /* no port */
        }
        else
        {
            *portptr = '\0';
            portptr++;
        }

        if(!strncmp(s_copy, "0", 1) || !strncmp(s_copy, "0.0.0.0", 7))
        {
            *host = 0;
        }
        else if((addrstuff.s_addr = inet_addr(s_copy)) == -1)
        {
            if(!strncmp(s_copy, "255.255.255.255", 15))
            {
                addrstuff.s_addr = INADDR_BROADCAST;
            }
            else
            {
                /* invalid ip address! */
                free(saved);
                return -3;
            }
        }
        else
        {
            *host = ntohl(addrstuff.s_addr);
        }            
        
        if(maskptr)
        {
            if(*maskptr == '\0')
            {
                /* Nothing beyond the / -- no bits in CIDR */
                free(saved);
                return -3;
            }

            if(strstr(maskptr, "."))
            {
                if(!strncmp(maskptr, "0", 1) || !strncmp(maskptr, "0.0.0.0", 7))
                {
                    *mask = 0;
                }
                else if((addrstuff.s_addr = inet_addr(maskptr)) == -1)
                {
                    if(!strncmp(maskptr, "255.255.255.255", 15))
                    {
                        addrstuff.s_addr = INADDR_BROADCAST;
                    }
                    else
                    {
                        /* invalid ip address! */
                        free(saved);
                        return -3;
                    }
                }
                else
                {
                    memcpy(mask, &addrstuff.s_addr, sizeof(unsigned));
                }           
            }
            else
            {
                int blocksize = atoi(maskptr);
                int i;

                if(blocksize == 0)
                {
                    *mask = 0;
                }
                else if(blocksize < 1 || blocksize > 32)
                {
                    free(saved);
                    return -4;
                }
                else
                {
                    *mask = 0;
                    for(i=0;i<blocksize;i++)
                    {
                        (*mask) |= (1 << 31) >> i;
                    }
                }
            }
        }
        if(portptr)
        {            
            port_parse(portptr, portset);
        }
        else
        {
            /* Make sure we have at least one port range in list, but an invalid port range */
            portset_add(portset, 0, 0);
        }
    }

    /* convert the arguments by default */
    if(network_order)
    {
        *mask = htonl(*mask);
        *host = htonl(*host);	
    }
    
    free(saved);
    return 0;
}
Example #3
0
static int ip4_parse(char *ipstr, int network_order, int *not_flag, unsigned *host,
                     unsigned *mask, PORTSET *portset)
{
    char *saved, *s_copy, *maskptr, *endp, *portptr = NULL, *addrend;
    struct in_addr addrstuff;

    if(!ipstr || !not_flag || !host || !mask) 
        return -1;


    if(*ipstr == '\0')
        return -3;

    saved = s_copy = strdup(ipstr);

    if(!s_copy)
    {
        return -2;
    }
    else
    {
        while(isspace((int)*s_copy))
            s_copy++;

        if(*s_copy == '\0')
        {
            free(saved);
            return -3;
        }

        if(*s_copy == '!')
        {
            *not_flag = 1;
            s_copy++;

            if(*s_copy == '\0')
            {
                free(saved);
                return -3;
            }
        }
        else
        {
            *not_flag = 0;
        }

        endp = strstr(s_copy, "]");
        if (endp)
        {
            /* Removing trailing ']' */
            *endp = 0;
        }

        endp = strstr(s_copy, ",");
        if (endp)
        {
            /* Removing trailing ',' */
            *endp = 0;
        }

        maskptr = strstr(s_copy, "/");

        portptr = strstr(s_copy, ":");

        if(!maskptr)
        {
            /* assume this is a host */
            *mask = 0xFFFFFFFF;
        }
        else
        {
            *maskptr = '\0';
            maskptr++;
        }

        if(!portptr)
        {
            /* no port */
        }
        else
        {
            *portptr = '\0';
            portptr++;
        }

        /* this will just be the address part as *maskptr and *portptr == '\0'
         * we shouldn't find anything after moving past trailing whitespace
         */
        addrend = strpbrk(s_copy, " \t");
        if (addrend != NULL)
        {
            while (isspace((int)(*addrend)))
                addrend++;

            if (*addrend != '\0')
            {
                free(saved);
                return -1;
            }
        }

        if(strncmp(s_copy, "0", 1) == 0 || strncmp(s_copy, "0.0.0.0", 7) == 0)
        {
            *host = 0;
        }
        else if((addrstuff.s_addr = inet_addr(s_copy)) == -1)
        {
            if(!strncmp(s_copy, "255.255.255.255", 15))
            {
                addrstuff.s_addr = INADDR_BROADCAST;
            }
            else
            {
                /* invalid ip address! */
                free(saved);
                return -3;
            }
        }
        else
        {
            *host = ntohl(addrstuff.s_addr);
        }            

        if(maskptr)
        {
            char *maskend;

            while (isspace((int)(*maskptr)))
                maskptr++;

            if(*maskptr == '\0')
            {
                /* Nothing beyond the / -- no bits in CIDR */
                free(saved);
                return -3;
            }

            /* make sure if there are spaces at the end that we don't find
             * any more stuff, like another address that wasn't separated
             * with a comma
             */
            maskend = strpbrk(maskptr, " \t");
            if (maskend != NULL)
            {
                while (isspace((int)(*maskend)))
                    maskend++;

                if (*maskend != '\0')
                {
                    free(saved);
                    return -1;
                }
            }

            if(strstr(maskptr, "."))
            {
                if(strncmp(maskptr, "0", 1) == 0 || strncmp(maskptr, "0.0.0.0", 7) == 0)
                {
                    *mask = 0;
                }
                else if((addrstuff.s_addr = inet_addr(maskptr)) == -1)
                {
                    if(strncmp(maskptr, "255.255.255.255", 15) == 0)
                    {
                        addrstuff.s_addr = INADDR_BROADCAST;
                    }
                    else
                    {
                        /* invalid ip address! */
                        free(saved);
                        return -3;
                    }
                }
                else
                {
                    *mask = ntohl(addrstuff.s_addr);
                }           
            }
            else
            {
                int blocksize = atoi(maskptr);
                int i;

                if(blocksize == 0)
                {
                    *mask = 0;
                }
                else if(blocksize < 1 || blocksize > 32)
                {
                    free(saved);
                    return -4;
                }
                else
                {
                    *mask = 0;
                    for(i=0;i<blocksize;i++)
                    {
                        (*mask) |= (1 << 31) >> i;
                    }
                }
            }
        }

        if(portptr)
        {            
            while (isspace((int)(*portptr)))
                portptr++;

            if (*portptr == '\0')
            {
                free(saved);
                return -5;
            }

            if (port_parse(portptr, portset) != 0)
            {
                free(saved);
                return -6;
            }
        }
        else
        {
            /* Make sure we have at least one port range in list, but an invalid port range */
            portset_add(portset, 0, 0);
        }
    }

    /* convert the arguments by default */
    if(network_order)
    {
        *mask = htonl(*mask);
        *host = htonl(*host);	
    }

    free(saved);
    return 0;
}
Example #4
0
File: ipobj.c Project: sdnnfv/snort
static int ip_parse(char *ipstr, sfip_t *ip, char *not_flag, PORTSET *portset, char **endIP)
{
    char *port_str;
    char *comma;
    char *end_bracket;

    if (*ipstr == '!')
    {
        ipstr++;
        *not_flag = 1;
    }
    else
    {
        *not_flag = 0;
    }

    comma = strchr(ipstr, ',');
    end_bracket = strrchr(ipstr, ']');

    if (comma)
    {
        *comma = '\0';
    }
    else if (end_bracket)
    {
        *end_bracket = '\0';
    }

    if (sfip_pton(ipstr, ip) != SFIP_SUCCESS)
        return -1;

    /* Just to get the IP string out of the way */
    port_str = strtok(ipstr, " \t");
    /* Is either the port after the 1st space, or NULL */
    port_str = strtok(NULL, " \t");

    while (port_str)
    {
        if (!comma)
        {
            comma = strchr(port_str, ',');
            if (comma)
                *comma = '\0';
        }

        if (!end_bracket)
        {
            end_bracket = strrchr(port_str, ']');
            if (end_bracket)
                *end_bracket = '\0';
        }

        port_parse(port_str, portset);
        port_str = strtok(NULL, " \t");
    }

    if (portset->port_list.count == 0)
    {
        /* Make sure we have at least one port range in list, but
         * an invalid port range to convey all is good.  */
        portset_add(portset, 0, 0);
    }

    if (comma)
    {
        *endIP = comma;
        *comma = ',';
    }
    else if (end_bracket)
    {
        *end_bracket = ']';
        *endIP = end_bracket;
    }
    else
    {
        /* Didn't see the comma or end bracket, so set endIP now */
        *endIP = port_str;
    }

    return 0;
}