END_TEST

START_TEST (ip_address_filtering){
	char *ip_address;
	Filter filters[2];
	filters[0].cidr_block = cidr_from_str("71.190.22.0/24");
	filters[1].cidr_block = cidr_from_str("2607:f0d0:1002:51::/64");

	// match
	ip_address = "71.190.22.42";
	int result = match_ip_address(ip_address, filters, 1);
	fail_unless(result);

	// no match
	ip_address = "90.190.22.42";
	result = match_ip_address(ip_address, filters, 1);
	fail_unless(result == 0);

	// match IPv6
	ip_address = "2607:f0d0:1002:51::4";
	result = match_ip_address(ip_address, filters, 1);
	fail_unless(result);

	// no match IPv6
	ip_address = "3607:f0d0:1002:51::4";
	result = match_ip_address(ip_address, filters, 1);
	fail_unless(result == 0);
}
void init_ip_addresses(Filter *filters, char *ipaddress_input, const char delimiter){
	int i=0;

	char *input = strdup(ipaddress_input);
	char *startToken = input;
	char *endToken;

	for (;;) {
		endToken = strchr(startToken, delimiter);
		if (endToken) {
			*endToken = '\0';
		}

		// convert this IP address or CIDR range into a libcidr CIDR object.
		filters[i].cidr_block = cidr_from_str(startToken);

		if (filters[i].cidr_block == NULL) {
            perror("Could not initialize cidr filter");
			exit(EXIT_FAILURE);
		}

		// if we didnt' find an endToken delimeter, then
		// this is the end of the -i filter input.
		// break out of loop now.
		if (!endToken) {
			break;
		}
		i++;
		startToken = endToken + 1;
	}
	free(input);
}
示例#3
0
/* Is it an IPv4 RFC1918 address? */
int is_ipv4_rfc1918(CIDR *address)
{
    int result;

    if( (cidr_get_proto(address) == CIDR_IPV4) &&
        ( (cidr_contains(cidr_from_str(IPV4_RFC1918_A), address) == 0) ||
        (cidr_contains(cidr_from_str(IPV4_RFC1918_B), address) == 0) ||
        (cidr_contains(cidr_from_str(IPV4_RFC1918_C), address) == 0) ) )
    {
        result = RESULT_SUCCESS;
    }
    else
    {
        result = RESULT_FAILURE;
    }

    return(result);
}
示例#4
0
/* Is it an address that can belong an interface? */
int is_valid_intf_address(CIDR *address, char* address_str, int allow_loopback)
{
    int result;

    if( (is_ipv4_broadcast(address) == RESULT_FAILURE) &&
        (is_ipv4_multicast(address) == RESULT_FAILURE) &&
        (is_ipv6_multicast(address) == RESULT_FAILURE) &&
        ((is_ipv4_loopback(address) == RESULT_FAILURE) || (allow_loopback == LOOPBACK_ALLOWED)) &&
        (cidr_equals(address, cidr_from_str(IPV6_LOOPBACK)) != 0) &&
        (cidr_equals(address, cidr_from_str(IPV4_UNSPECIFIED)) != 0) &&
        (cidr_contains(cidr_from_str(IPV4_THIS), address) != 0) &&
        (cidr_equals(address, cidr_from_str(IPV4_LIMITED_BROADCAST)) != 0) &&
        (is_any_host(address) == RESULT_SUCCESS) &&
        (is_any_cidr(address_str) == RESULT_SUCCESS) )
    {
        result = RESULT_SUCCESS;
    }
    else
    {
        result = RESULT_FAILURE;
    }

    return(result);
}
示例#5
0
int
main(int argc, char *argv[])
{
	CIDR *tcidr;
	char *tstr;
	int cflags;

	cflags=CIDR_NOFLAGS;
	pname = *argv++;
	argc--;

	if(argc==0)
		usage();

	/* All the rest of the args are addresses to run */
	while(*argv!=NULL)
	{
		tstr = NULL;
		tcidr = cidr_from_str(*argv);
		if(tcidr==NULL)
			printf("***> ERROR: Can't parse '%s'!\n", *argv);
		else
		{
			/* XXX Sanity */
			if(tcidr->proto==CIDR_IPV4 && cidr_get_pflen(tcidr)<24)
			{
				printf("***> Error: No v4 prefixes shorter than /24\n");
				argv++;
				continue;
			}
			else if(tcidr->proto==CIDR_IPV6 && cidr_get_pflen(tcidr)<120)
			{
				printf("***> Error: No v6 prefixes shorter than /120\n");
				argv++;
				continue;
			}
			tstr = cidr_to_str(tcidr, cflags);
			printf("Subdividing %s:\n", tstr);
			free(tstr);

			showkids(tcidr, 1);
		}

		argv++;
	}

	exit(0);
}
示例#6
0
/* Is it an IPv6 link-local address? */
int is_ipv6_link_local(CIDR *address)
{
    int result;

    if( (cidr_get_proto(address) == CIDR_IPV6) &&
        (cidr_contains(cidr_from_str(IPV6_LINKLOCAL), address) == 0) )
    {
        result = RESULT_SUCCESS;
    }
    else
    {
        result = RESULT_FAILURE;
    }

    return(result);
}
示例#7
0
/* Is it an IPv6 multicast address? */
int is_ipv6_multicast(CIDR *address)
{
    int result;

    if( (cidr_get_proto(address) == CIDR_IPV6) &&
        (cidr_contains(cidr_from_str(IPV6_MULTICAST), address) == 0) )
    {
        result = RESULT_SUCCESS;
    }
    else
    {
        result = RESULT_FAILURE;
    }

    return(result);
}
示例#8
0
/* Is it an IPv4 loopback address? */
int is_ipv4_loopback(CIDR *address)
{
    int result;

    if( (cidr_get_proto(address) == CIDR_IPV4) &&
        (cidr_contains(cidr_from_str(IPV4_LOOPBACK), address) == 0) )
    {
        result = RESULT_SUCCESS;
    }
    else
    {
        result = RESULT_FAILURE;
    }

    return(result);
}