diff options
author | Daniil Baturin <daniil@baturin.org> | 2013-05-22 16:25:04 -0700 |
---|---|---|
committer | Daniil Baturin <daniil@baturin.org> | 2013-05-22 16:25:04 -0700 |
commit | 256d4d7c8d494093a206cd72e7375079e6cf8b99 (patch) | |
tree | 1bc849f4b177f800d9b8bf43cbffe440f9d11f41 /src | |
parent | 0b218869c22668c4473482070b004b5d04291cec (diff) | |
download | ipaddrcheck-256d4d7c8d494093a206cd72e7375079e6cf8b99.tar.gz ipaddrcheck-256d4d7c8d494093a206cd72e7375079e6cf8b99.zip |
Fix initial commit.
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 6 | ||||
-rw-r--r-- | src/iptest.c | 117 | ||||
-rw-r--r-- | src/iptest.h | 117 |
3 files changed, 240 insertions, 0 deletions
diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..bda48fb --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,6 @@ +AM_CFLAGS = --pedantic -Wall -Werror -std=c99 -O2 -lcidr +AM_LDFLAGS = + +iptest_SOURCES = iptest.c iptest.h + +bin_PROGRAMS = iptest diff --git a/src/iptest.c b/src/iptest.c new file mode 100644 index 0000000..5087736 --- /dev/null +++ b/src/iptest.c @@ -0,0 +1,117 @@ +/* + * iptest.c: iptest IPv4/IPv6 validator + * + * Maintainer: Daniil Baturin <daniil at baturin dot org> + * + * Copyright (C) 2013 SO3Group + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +#include "config.h" +#include "iptest.h" + +static const struct option options[] = +{ + { "is-valid", required_argument, NULL, 'a' }, + { "is-ipv4", required_argument, NULL, 'b' }, + { "is-ipv4-host", required_argument, NULL, 'c'}, + { "is-ipv4-net", required_argument, NULL, 'd'}, + { "is-ipv6", required_argument, NULL, 'g' }, + { "is-ipv6-net", required_argument, NULL, 'G' }, + { NULL, no_argument, NULL, 0 } +}; + +void help(void) +{ + static char *message = \ +"--is-valid STR Check if STR is a valid IPv4 or IPv6 address\n\ + or subnet\n\ +--is-ipv4 STR Check if STR is a valid IPv4 address with mask\n"; + + printf("%s", message); +} + +int main(int argc, char* argv[]) +{ + char *address_str = ""; + int action = 0; + + int option_index = 0; + char c; + int option_count = 0; + + while( (c = getopt_long(argc, argv, "abcd?", options, &option_index)) != -1 ) + { + switch(c) + { + case 'a': + action = IS_VALID; + address_str = optarg; + break; + case 'b': + action = IS_IPV4; + address_str = optarg; + break; + case 'c': + action = IS_IPV4_HOST; + address_str = optarg; + break; + case 'd': + action = IS_IPV4_NET; + address_str = optarg; + break; + case 'I': + action = IS_IPV6; + address_str = optarg; + break; + case '?': + default: + fprintf(stderr, "Invalid option\n"); + break; + } + option_count++; + } + + if( option_count != 1 ) + { + fprintf(stderr, "Wrong options number, exactly one option expected!\n"); + help(); + return(1); + } + + CIDR *address; + address = cidr_from_str(address_str); + int result = EXIT_FAILURE; + + switch(action) + { + case IS_VALID: + result = is_valid_address(address); + break; + case IS_IPV4: + result = is_ipv4(address); + break; + case IS_IPV4_HOST: + result = is_ipv4_host(address); + break; + case IS_IPV4_NET: + result = is_ipv4_net(address); + break; + } + + return(result); +} + + diff --git a/src/iptest.h b/src/iptest.h new file mode 100644 index 0000000..3624ec4 --- /dev/null +++ b/src/iptest.h @@ -0,0 +1,117 @@ +/* + * iptest.h: macros and functions for iptest IPv4/IPv6 validator + * + * Maintainer: Daniil Baturin <daniil at baturin dot org> + * + * Copyright (C) 2013 SO3Group + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + + +#include <stdio.h> +#include <stdlib.h> +#include <getopt.h> +#include <libcidr.h> + +#define INVALID_PROTO -1 + +/* Option codes */ +#define IS_VALID 10 +#define IS_IPV4 20 +#define IS_IPV4_HOST 30 +#define IS_IPV4_NET 40 +#define IS_IPV4_BROADCAST 50 +#define IS_IPV4_UNICAST 60 +#define IS_IPV4_MULTICAST 70 +#define IS_IPV4_RFC1918 80 +#define IS_IPV4_LOOPBACK 85 +#define IS_IPV6 90 +#define IS_IPV6_HOST 100 +#define IS_IPV6_NET 110 +#define IS_IPV6_UNICAST 120 +#define IS_IPV6_MULTICAST 130 +#define IS_IPV6_LINKLOCAL 140 + +/* Does it look like a valid address of any protocol? */ +int is_valid_address(CIDR *address) +{ + int result; + + if( cidr_get_proto(address) != INVALID_PROTO ) + { + result = EXIT_SUCCESS; + } + else + { + result = EXIT_FAILURE; + } + + return(result); +} + +/* Is it a correct IPv4 host or subnet address + with or without net mask */ +int is_ipv4(CIDR *address) +{ + int result; + + if( cidr_get_proto(address) == CIDR_IPV4 ) + { + result = EXIT_SUCCESS; + } + else + { + result = EXIT_FAILURE; + } + + return(result); +} + +/* Is it a correct IPv4 host (i.e. not network) address? */ +int is_ipv4_host(CIDR *address) +{ + int result; + + if( (cidr_get_proto(address) == CIDR_IPV4) && + cidr_equals(address, cidr_addr_network(address)) ) + { + result = EXIT_SUCCESS; + } + else + { + result = EXIT_FAILURE; + } + + return(result); +} + +/* Is it a correct IPv4 network address? */ +int is_ipv4_net(CIDR *address) +{ + /* TODO: Don't try to validate is mask is not present */ + int result; + + if( (cidr_get_proto(address) == CIDR_IPV4) && + (cidr_equals(address, cidr_addr_network(address)) == 0) ) + { + result = EXIT_SUCCESS; + } + else + { + result = EXIT_FAILURE; + } + + return(result); +} |