├── ChangeLog ├── debian ├── compat ├── rules ├── changelog ├── control └── copyright ├── NEWS ├── Makefile.am ├── man ├── Makefile.am ├── mkman.sh ├── ipaddrcheck.x └── ipaddrcheck.1 ├── README ├── AUTHORS ├── src ├── Makefile.am ├── ipaddrcheck_functions.h ├── ipaddrcheck_functions.c └── ipaddrcheck.c ├── tests ├── Makefile.am ├── assert.sh ├── integration_tests.sh └── check_ipaddrcheck.c ├── configure.ac ├── README.md ├── INSTALL ├── COPYING └── COPYING-LGPL /ChangeLog: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /debian/compat: -------------------------------------------------------------------------------- 1 | 9 2 | -------------------------------------------------------------------------------- /NEWS: -------------------------------------------------------------------------------- 1 | None at this time. 2 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS = src . tests man 2 | -------------------------------------------------------------------------------- /man/Makefile.am: -------------------------------------------------------------------------------- 1 | man1_MANS = ipaddrcheck.1 2 | 3 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | iptest -- IPv4 and IPv6 address validator for use in scripts. 2 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | VyOS maintainers and contributors, including: 2 | 3 | Daniil Baturin 4 | -------------------------------------------------------------------------------- /debian/rules: -------------------------------------------------------------------------------- 1 | #!/usr/bin/make -f 2 | 3 | %: 4 | autoreconf -i 5 | dh $@ 6 | 7 | -------------------------------------------------------------------------------- /man/mkman.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | help2man --no-info --include ipaddrcheck.x -o ipaddrcheck.1 ../src/ipaddrcheck 4 | -------------------------------------------------------------------------------- /src/Makefile.am: -------------------------------------------------------------------------------- 1 | AM_CFLAGS = --pedantic -Wall -Werror -std=c99 -O2 2 | AM_LDFLAGS = 3 | 4 | ipaddrcheck_SOURCES = ipaddrcheck.c ipaddrcheck_functions.c 5 | ipaddrcheck_LDFLAGS = -lcidr -lpcre 6 | 7 | bin_PROGRAMS = ipaddrcheck 8 | -------------------------------------------------------------------------------- /tests/Makefile.am: -------------------------------------------------------------------------------- 1 | TESTS = check_ipaddrcheck integration_tests.sh 2 | 3 | TESTS_ENVIRONMENT = top_srcdir=$(top_srcdir) PATH=.:$(top_srcdir)/src:$$PATH 4 | 5 | check_PROGRAMS = check_ipaddrcheck 6 | check_ipaddrcheck_SOURCES = check_ipaddrcheck.c ../src/ipaddrcheck_functions.c 7 | check_ipaddrcheck_CFLAGS = @CHECK_CFLAGS@ 8 | check_ipaddrcheck_LDADD = -lcidr -lpcre @CHECK_LIBS@ 9 | -------------------------------------------------------------------------------- /debian/changelog: -------------------------------------------------------------------------------- 1 | ipaddrcheck (1.1) unstable; urgency=medium 2 | 3 | * Add a --verbose option and diagnostic messages for common mistakes 4 | 5 | -- Daniil Baturin Tue, 13 Nov 2018 19:51:04 +0100 6 | 7 | ipaddrcheck (1.0) unstable; urgency=medium 8 | 9 | * Initial import and debian packaging 10 | 11 | -- Daniil Baturin Sat, 12 May 2018 00:17:05 +0200 12 | -------------------------------------------------------------------------------- /debian/control: -------------------------------------------------------------------------------- 1 | Source: ipaddrcheck 2 | Section: contrib/net 3 | Priority: extra 4 | Maintainer: VyOS Package Maintainers 5 | Build-Depends: autoconf, debhelper (>= 9), libpcre3-dev, libcidr-dev 6 | Standards-Version: 3.9.6 7 | 8 | Package: ipaddrcheck 9 | Architecture: any 10 | Depends: libpcre3, libcidr0, ${shlibs:Depends}, ${misc:Depends} 11 | Description: IPv4 and IPv6 address validation utility 12 | A validation utility for IPv4 and IPv6 addresses. 13 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | AC_INIT([ipaddrcheck], [1.0], [maintainers@vyos.net], [ipaddrcheck], [https://github.com/vyos/ipaddrcheck]) 2 | AC_COPYRIGHT([Copyright (c) 2018 VyOS maintainers and contributors.]) 3 | 4 | #AC_PROG_CC 5 | AM_PROG_CC_C_O 6 | 7 | AC_CHECK_HEADER([pcre.h], [], [AC_MSG_FAILURE([pcre.h is not found.])]) 8 | AC_CHECK_HEADER([libcidr.h], [], [AC_MSG_FAILURE([libcidr.h is not found.])]) 9 | 10 | AM_INIT_AUTOMAKE([gnu no-dist-gzip dist-bzip2 subdir-objects]) 11 | AC_PREFIX_DEFAULT([/usr]) 12 | 13 | AC_CONFIG_FILES([Makefile src/Makefile tests/Makefile man/Makefile]) 14 | AC_CONFIG_HEADERS([src/config.h]) 15 | 16 | PKG_CHECK_MODULES([CHECK], [check >= 0.9.4]) 17 | 18 | AC_OUTPUT 19 | -------------------------------------------------------------------------------- /debian/copyright: -------------------------------------------------------------------------------- 1 | This package was debianized by Daniil Baturin on 2 | Sat, 12 May 2018 00:17:05 +0200 3 | 4 | It's original content from the GIT repository 5 | 6 | Upstream Author: 7 | 8 | 9 | 10 | Copyright: 11 | 12 | Copyright (C) 2017 VyO8 maintainers and contributors 13 | All Rights Reserved. 14 | 15 | License: 16 | 17 | This program is free software; you can redistribute it and/or modify 18 | it under the terms of the GNU General Public License as published by 19 | the Free Software Foundation; either version 2, or (at your option) 20 | any later version. 21 | 22 | This program is distributed in the hope that it will be useful, but 23 | WITHOUT ANY WARRANTY; without even the implied warranty of 24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 25 | General Public License for more details. 26 | 27 | A copy of the GNU General Public License is available as 28 | `/usr/share/common-licenses/GPL' in the Debian GNU/Linux distribution 29 | or on the World Wide Web at `http://www.gnu.org/copyleft/gpl.html'. 30 | You can also obtain it by writing to the Free Software Foundation, 31 | Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 32 | MA 02110-1301, USA. 33 | 34 | The Debian packaging is (C) 2018, Daniil Baturin and 35 | is licensed under the GPL, see above. 36 | -------------------------------------------------------------------------------- /man/ipaddrcheck.x: -------------------------------------------------------------------------------- 1 | [NAME] 2 | ipaddrcheck \- an IPv4 and IPv6 validator suitable for use in scripts 3 | 4 | [DESCRIPTION] 5 | ipaddrcheck is capable of checking various facts about IPv4 and IPv6 addresses, 6 | from checking whether the address is valid to checking whether it is an IPv4 7 | multicast address, host or network address and many more. 8 | 9 | [OPTIONS] 10 | If more than one option is given, they work as logical AND (i.e. if one of them 11 | fails, overall check result is fail). Logical consistency of the options is 12 | left up to the user. The program does not detect whether given options are 13 | compatible, so a mutually exclusive combination like "--is-ipv4 --is-ipv6" 14 | will be accepted and simply return fail for any argument. 15 | 16 | [DEFINITIONS] 17 | Broadcast address: the last address of an IPv4 subnet. 18 | 19 | Interface address: address passes --is-valid-intf-address check if 20 | prefix length is specified, it is not a broadcast address, not a multicast 21 | address, not unspecified address, not from IPv4 THIS network, not a network address 22 | and not the IPv4 limited broadcast address. 23 | 24 | [EXAMPLES] 25 | Check if the argument is a valid IPv4 host address: 26 | ipaddrcheck --is-ipv4-host 192.0.2.78/24 27 | 28 | Check if the argument is an address that can be assigned to a loopback interface: 29 | ipaddrcheck --allow-loopback --is-valid-intf-address 127.0.0.7/8 30 | 31 | [AUTHOR] 32 | Written by SO3Group. 33 | -------------------------------------------------------------------------------- /src/ipaddrcheck_functions.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ipaddrcheck_functions.h: macros and prototypes for ipaddrcheck 3 | * 4 | * Copyright (C) 2013 Daniil Baturin 5 | * Copyright (C) 2018 VyOS maintainers and contributors 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public 18 | * License along with this library; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 20 | * 21 | */ 22 | 23 | #ifndef IPADDRCHECK_FUNCTIONS_H 24 | #define IPADDRCHECK_FUNCTIONS_H 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | #define INVALID_PROTO -1 34 | 35 | #define RESULT_SUCCESS 1 36 | #define RESULT_FAILURE 0 37 | #define RESULT_INT_ERROR 2 38 | 39 | #define IPV4_MULTICAST "224.0.0.0/4" 40 | #define IPV4_LOOPBACK "127.0.0.0/8" 41 | #define IPV4_LINKLOCAL "169.254.0.0/16" 42 | #define IPV4_UNSPECIFIED "0.0.0.0/0" 43 | #define IPV4_THIS "0.0.0.0/8" 44 | #define IPV4_RFC1918_A "10.0.0.0/8" 45 | #define IPV4_RFC1918_B "172.16.0.0/12" 46 | #define IPV4_RFC1918_C "192.168.0.0/16" 47 | #define IPV4_LIMITED_BROADCAST "255.255.255.255/32" 48 | #define IPV6_MULTICAST "ff00::/8" 49 | #define IPV6_LINKLOCAL "fe80::/64" 50 | #define IPV6_LOOPBACK "::1/128" 51 | 52 | #define NO_LOOPBACK 0 53 | #define LOOPBACK_ALLOWED 1 54 | 55 | int duplicate_double_semicolons(char* address_str); 56 | int is_ipv4_cidr(char* address_str); 57 | int is_ipv4_single(char* address_str); 58 | int is_ipv6_cidr(char* address_str); 59 | int is_ipv6_single(char* address_str); 60 | int is_any_cidr(char* address_str); 61 | int is_any_single(char* address_str); 62 | int is_valid_address(CIDR *address); 63 | int is_ipv4(CIDR *address); 64 | int is_ipv4_host(CIDR *address); 65 | int is_ipv4_net(CIDR *address); 66 | int is_ipv4_broadcast(CIDR *address); 67 | int is_ipv4_multicast(CIDR *address); 68 | int is_ipv4_loopback(CIDR *address); 69 | int is_ipv4_link_local(CIDR *address); 70 | int is_ipv4_rfc1918(CIDR *address); 71 | int is_ipv6(CIDR *address); 72 | int is_ipv6_host(CIDR *address); 73 | int is_ipv6_net(CIDR *address); 74 | int is_ipv6_multicast(CIDR *address); 75 | int is_ipv6_link_local(CIDR *address); 76 | int is_valid_intf_address(CIDR *address, char* address_str, int allow_loopback); 77 | int is_any_host(CIDR *address); 78 | int is_any_net(CIDR *address); 79 | 80 | #endif /* IPADDRCHECK_FUNCTIONS_H */ 81 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ipaddrcheck 2 | =========== 3 | 4 | ## Using 5 | 6 | An IPv4 and IPv6 validation utility for use in scripts 7 | 8 | Depends on libcidr by Matthew Fuller (http://www.over-yonder.net/~fullermd/projects/libcidr) and libpcre. 9 | 10 | Usage: ./ipaddrcheck [STRING] 11 | Options: 12 | --is-valid Check if STRING is a valid IPv4 or IPv6 address 13 | with or without prefix length 14 | --is-any-cidr Check if STRING is a valid IPv4 or IPv6 address 15 | with prefix length 16 | --is-any-single Check if STRING is a valid single IPv4 or IPv6 address 17 | --is-any-host Check if STRING is a valid IPv4 or IPv6 host address 18 | --is-any-net Check if STRING is a valid IPv4 or IPv6 network address 19 | --is-ipv4 Check if STRING is a valid IPv4 address with mask 20 | --is-ipv4-cidr Check if STRING is a valid CIDR-formatted address 21 | --is-ipv4-single Check if STRING is a valid single address 22 | (i.e. with no mask) 23 | --is-ipv4-host Check if STRING is a host address 24 | --is-ipv4-net Check if STRING is a network address 25 | --is-ipv4-broadcast Check if STRING is a broadcast address 26 | --is-ipv4-multicast Check if STRING is a multicast address 27 | --is-ipv4-loopback Check if STRING is a loopback address 28 | --is-ipv4-link-local Check if STRING is a link-local address 29 | --is-ipv4-rfc1918 Check if STRING is a private (RFC1918) address 30 | --is-ipv6 Check if STRING is a valid IPv6 address 31 | --is-ipv6-cidr Check if STRING is a CIDR-formatted IPv6 address 32 | --is-ipv6-single Check if STRING is an IPv6 address with no mask 33 | --is-ipv6-host Check if STRING is an IPv6 host address 34 | --is-ipv6-net Check if STRING is an IPv6 network address 35 | --is-ipv6-multicast Check if STRING is an IPv6 multicast address 36 | --is-ipv6-link-local Check if STRING is an IPv6 link-local address 37 | --is-valid-intf-address Check if STRING is an IPv4 or IPv6 address that 38 | can be assigned to a network interface 39 | --allow-loopback When used with --is-valid-intf-address, 40 | makes IPv4 loopback addresses pass the check 41 | --version Print version information and exit 42 | --help Print help message and exit 43 | 44 | Exit codes: 45 | 0 if check passed, 46 | 1 if check failed, 47 | 2 if a problem occured (wrong option, internal error etc.) 48 | 49 | ## Building 50 | 51 | Building from source: 52 | 53 | ``` 54 | autoreconf -i 55 | ./configure 56 | make && make install 57 | ``` 58 | 59 | Running unit tests: 60 | 61 | ``` 62 | make check 63 | ``` 64 | -------------------------------------------------------------------------------- /man/ipaddrcheck.1: -------------------------------------------------------------------------------- 1 | .\" DO NOT MODIFY THIS FILE! It was generated by help2man 1.38.2. 2 | .TH IPADDRCHECK "1" "June 2013" "ipaddrcheck 0.1" "User Commands" 3 | .SH NAME 4 | ipaddrcheck \- an IPv4 and IPv6 validator suitable for use in scripts 5 | .SH SYNOPSIS 6 | .B ipaddrcheck 7 | \fI \fR[\fISTRING\fR] 8 | .SH DESCRIPTION 9 | ipaddrcheck is capable of checking various facts about IPv4 and IPv6 addresses, 10 | from checking whether the address is valid to checking whether it is an IPv4 11 | multicast address, host or network address and many more. 12 | .SH OPTIONS 13 | If more than one option is given, they work as logical AND (i.e. if one of them 14 | fails, overall check result is fail). Logical consistency of the options is 15 | left up to the user. The program does not detect whether given options are 16 | compatible, so a mutually exclusive combination like "--is-ipv4 --is-ipv6" 17 | will be accepted and simply return fail for any argument. 18 | .TP 19 | \fB\-\-is\-valid\fR 20 | Check if STRING is a valid IPv4 or IPv6 address 21 | with or without prefix length 22 | .TP 23 | \fB\-\-is\-any\-cidr\fR 24 | Check if STRING is a valid IPv4 or IPv6 address 25 | with prefix length 26 | .TP 27 | \fB\-\-is\-any\-single\fR 28 | Check if STRING is a valid single IPv4 or IPv6 address 29 | .TP 30 | \fB\-\-is\-any\-host\fR 31 | Check if STRING is a valid IPv4 or IPv6 host address 32 | .TP 33 | \fB\-\-is\-any\-net\fR 34 | Check if STRING is a valid IPv4 or IPv6 network address 35 | .TP 36 | \fB\-\-is\-ipv4\fR 37 | Check if STRING is a valid IPv4 address with mask 38 | .TP 39 | \fB\-\-is\-ipv4\-cidr\fR 40 | Check if STRING is a valid CIDR\-formatted address 41 | .TP 42 | \fB\-\-is\-ipv4\-single\fR 43 | Check if STRING is a valid single address 44 | (i.e. with no mask) 45 | .TP 46 | \fB\-\-is\-ipv4\-host\fR 47 | Check if STRING is a host address 48 | .TP 49 | \fB\-\-is\-ipv4\-net\fR 50 | Check if STRING is a network address 51 | .TP 52 | \fB\-\-is\-ipv4\-broadcast\fR 53 | Check if STRING is a broadcast address 54 | .TP 55 | \fB\-\-is\-ipv4\-multicast\fR 56 | Check if STRING is a multicast address 57 | .TP 58 | \fB\-\-is\-ipv4\-loopback\fR 59 | Check if STRING is a loopback address 60 | .TP 61 | \fB\-\-is\-ipv4\-link\-local\fR 62 | Check if STRING is a link\-local address 63 | .TP 64 | \fB\-\-is\-ipv4\-rfc1918\fR 65 | Check if STRING is a private (RFC1918) address 66 | .TP 67 | \fB\-\-is\-ipv6\fR 68 | Check if STRING is a valid IPv6 address 69 | .TP 70 | \fB\-\-is\-ipv6\-cidr\fR 71 | Check if STRING is a CIDR\-formatted IPv6 address 72 | .TP 73 | \fB\-\-is\-ipv6\-single\fR 74 | Check if STRING is an IPv6 address with no mask 75 | .TP 76 | \fB\-\-is\-ipv6\-host\fR 77 | Check if STRING is an IPv6 host address 78 | .TP 79 | \fB\-\-is\-ipv6\-net\fR 80 | Check if STRING is an IPv6 network address 81 | .TP 82 | \fB\-\-is\-ipv6\-multicast\fR 83 | Check if STRING is an IPv6 multicast address 84 | .TP 85 | \fB\-\-is\-ipv6\-link\-local\fR 86 | Check if STRING is an IPv6 link\-local address 87 | .TP 88 | \fB\-\-is\-valid\-intf\-address\fR 89 | Check if STRING is an IPv4 or IPv6 address that 90 | can be assigned to a network interface 91 | .TP 92 | \fB\-\-allow\-loopback\fR 93 | When used with \fB\-\-is\-valid\-intf\-address\fR, 94 | makes IPv4 loopback addresses pass the check 95 | .TP 96 | \fB\-\-version\fR 97 | Print version information and exit 98 | .TP 99 | \fB\-\-help\fR 100 | Print help message and exit 101 | .SS "Exit codes:" 102 | .TP 103 | 0 104 | if check passed, 105 | .TP 106 | 1 107 | if check failed, 108 | .TP 109 | 2 110 | if a problem occured (wrong option, internal error etc.) 111 | .SH EXAMPLES 112 | Check if the argument is a valid IPv4 host address: 113 | ipaddrcheck --is-ipv4-host 192.0.2.78/24 114 | 115 | Check if the argument is an address that can be assigned to a loopback interface: 116 | ipaddrcheck --allow-loopback --is-valid-intf-address 127.0.0.7/8 117 | .SH DEFINITIONS 118 | Broadcast address: the last address of an IPv4 subnet. 119 | 120 | Interface address: address passes --is-valid-intf-address check if 121 | prefix length is specified, it is not a broadcast address, not a multicast 122 | address, not unspecified address, not from IPv4 THIS network, not a network address 123 | and not the IPv4 limited broadcast address. 124 | .SH AUTHOR 125 | Written by SO3Group. 126 | .SH COPYRIGHT 127 | Copyright \(co SO3Group 2013. 128 | License GPLv2+: GNU GPL version 2 or later 129 | .br 130 | This is free software: you are free to change and redistribute it. 131 | There is NO WARRANTY, to the extent permitted by law. 132 | -------------------------------------------------------------------------------- /tests/assert.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # assert.sh 1.0 - bash unit testing framework 3 | # Copyright (C) 2009, 2010, 2011, 2012 Robert Lehmann 4 | # 5 | # http://github.com/lehmannro/assert.sh 6 | # 7 | # This program is free software: you can redistribute it and/or modify 8 | # it under the terms of the GNU Lesser General Public License as published 9 | # by the Free Software Foundation, either version 3 of the License, or 10 | # (at your option) any later version. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU Lesser General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU Lesser General Public License 18 | # along with this program. If not, see . 19 | 20 | export DISCOVERONLY=${DISCOVERONLY:-} 21 | export DEBUG=${DEBUG:-} 22 | export STOP=${STOP:-} 23 | export INVARIANT=${INVARIANT:-} 24 | 25 | args="$(getopt -n "$0" -l verbose,help,stop,discover,invariant vhxdi $*)" \ 26 | || exit -1 27 | for arg in $args; do 28 | case "$arg" in 29 | -h) 30 | echo "$0 [-vxid] [--verbose] [--stop] [--invariant] [--discover]" 31 | echo "`sed 's/./ /g' <<< "$0"` [-h] [--help]" 32 | exit 0;; 33 | --help) 34 | cat < [stdin] 91 | (( tests_ran++ )) 92 | [[ -n "$DISCOVERONLY" ]] && return 93 | # printf required for formatting 94 | printf -v expected "x${2:-}" # x required to overwrite older results 95 | result="$(eval 2>/dev/null $1 <<< ${3:-})" 96 | # Note: $expected is already decorated 97 | if [[ "x$result" == "$expected" ]]; then 98 | [[ -n "$DEBUG" ]] && echo -n . 99 | return 100 | fi 101 | [[ -n "$DEBUG" ]] && echo -n X 102 | result="$(sed -e :a -e '$!N;s/\n/\\n/;ta' <<< "$result")" 103 | [[ -z "$result" ]] && result="nothing" || result="\"$result\"" 104 | [[ -z "$2" ]] && expected="nothing" || expected="\"$2\"" 105 | failure="expected $expected${_indent}got $result" 106 | report="test #$tests_ran \"$1${3:+ <<< $3}\" failed:${_indent}$failure" 107 | tests_errors[$tests_failed]="$report" 108 | (( tests_failed++ )) 109 | if [[ -n "$STOP" ]]; then 110 | [[ -n "$DEBUG" ]] && echo 111 | echo "$report" 112 | exit 1 113 | fi 114 | } 115 | 116 | assert_raises() { 117 | # assert_raises [stdin] 118 | (( tests_ran++ )) 119 | [[ -n "$DISCOVERONLY" ]] && return 120 | (eval $1 <<< ${3:-}) > /dev/null 2>&1 121 | status=$? 122 | expected=${2:-0} 123 | if [[ "$status" -eq "$expected" ]]; then 124 | [[ -n "$DEBUG" ]] && echo -n . 125 | return 126 | fi 127 | [[ -n "$DEBUG" ]] && echo -n X 128 | failure="program terminated with code $status instead of $expected" 129 | report="test #$tests_ran \"$1${3:+ <<< $3}\" failed:${_indent}$failure" 130 | tests_errors[$tests_failed]="$report" 131 | (( tests_failed++ )) 132 | if [[ -n "$STOP" ]]; then 133 | [[ -n "$DEBUG" ]] && echo 134 | echo "$report" 135 | exit 1 136 | fi 137 | } 138 | 139 | _assert_reset 140 | 141 | -------------------------------------------------------------------------------- /tests/integration_tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # integration_tests.sh: ipaddrcheck integration tests 4 | # 5 | # Copyright (C) 2013 Daniil Baturin 6 | # Copyright (C) 2018 VyOS maintainers and contributors 7 | # 8 | # This program is free software; you can redistribute it and/or modify 9 | # it under the terms of the GNU General Public License version 2 or later as 10 | # published by the Free Software Foundation. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with this program. If not, see . 19 | # 20 | 21 | . ./assert.sh 22 | 23 | IPADDRCHECK=../src/ipaddrcheck 24 | 25 | # test data 26 | ipv4_single_positive=( 27 | 192.0.2.1 28 | 192.0.2.0 29 | 0.0.0.0 30 | 0.0.0.1 31 | 255.255.255.255 32 | ) 33 | 34 | ipv4_single_negative=( 35 | 192.0.2.666 36 | 500.0.2.1 37 | ) 38 | 39 | ipv4_cidr_positive=( 40 | 192.0.2.1/0 41 | 192.0.2.1/32 42 | 192.0.2.1/24 43 | 192.0.2.0/26 44 | ) 45 | 46 | ipv4_cidr_negative=( 47 | 192.0.2.1/33 48 | 192.0.2.666/32 49 | ) 50 | 51 | ipv6_single_positive=( 52 | 2001:0db8:0000:0000:0000:ff00:0042:8329 53 | 2001:db8:0:0:0:ff00:42:8329 54 | 2001:db8::1 55 | ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 56 | 0000:: 57 | ::1 58 | :: 59 | ) 60 | 61 | ipv6_single_negative=( 62 | gggg::ffff 63 | 2001:db8::bad::f00d 64 | ) 65 | 66 | ipv6_cidr_positive=( 67 | 2001:db8::/0 68 | 2001:db8::/128 69 | 2001:db8::/56 70 | ::/0 71 | ) 72 | 73 | ipv6_cidr_negative=( 74 | 2001:db8::/129 75 | ) 76 | 77 | string="garbage" 78 | 79 | # begin ipaddrcheck_integration 80 | 81 | # --is-valid 82 | for address in \ 83 | ${ipv4_single_positive[*]} \ 84 | ${ipv4_cidr_positive[*]} \ 85 | ${ipv6_single_positive[*]} \ 86 | ${ipv6_cidr_positive[*]} 87 | do 88 | assert_raises "$IPADDRCHECK --is-valid $address" 0 89 | done 90 | 91 | for address in \ 92 | ${ipv4_single_negative[*]} \ 93 | ${ipv4_cidr_negative[*]} \ 94 | ${ipv6_single_negative[*]} \ 95 | ${ipv6_cidr_negative[*]} \ 96 | $string 97 | do 98 | assert_raises "$IPADDRCHECK --is-valid $address" 1 99 | done 100 | 101 | # --is-any-cidr 102 | for address in \ 103 | ${ipv4_cidr_positive[*]} \ 104 | ${ipv6_cidr_positive[*]} 105 | do 106 | assert_raises "$IPADDRCHECK --is-any-cidr $address" 0 107 | done 108 | 109 | for address in \ 110 | ${ipv4_single_positive[0]} \ 111 | ${ipv4_cidr_negative[*]} \ 112 | ${ipv6_single_positive[0]} \ 113 | ${ipv6_cidr_negative[*]} \ 114 | $string 115 | do 116 | assert_raises "$IPADDRCHECK --is-any-cidr $address" 1 117 | done 118 | 119 | 120 | # --is-any-single 121 | for address in \ 122 | ${ipv4_single_positive[*]} \ 123 | ${ipv6_single_positive[*]} 124 | do 125 | assert_raises "$IPADDRCHECK --is-any-single $address" 0 126 | done 127 | 128 | for address in \ 129 | ${ipv4_single_negative[*]} \ 130 | ${ipv4_cidr_postive[0]} \ 131 | ${ipv6_single_negative[*]} \ 132 | ${ipv6_cidr_postitive[0]} \ 133 | $string 134 | do 135 | assert_raises "$IPADDRCHECK --is-any-single $address" 1 136 | done 137 | 138 | 139 | # --is-ipv4 140 | for address in \ 141 | ${ipv4_single_positive[*]} \ 142 | ${ipv4_cidr_positive[*]} 143 | do 144 | assert_raises "$IPADDRCHECK --is-ipv4 $address" 0 145 | done 146 | 147 | for address in \ 148 | ${ipv4_single_negative[*]} \ 149 | ${ipv4_cidr_negative[*]} \ 150 | ${ipv6_single_positive[0]} \ 151 | ${ipv6_cidr_positive[0]} \ 152 | $string 153 | do 154 | assert_raises "$IPADDRCHECK --is-ipv4 $address" 1 155 | done 156 | 157 | # --is-ipv4-cidr 158 | for address in \ 159 | ${ipv4_cidr_positive[*]} 160 | do 161 | assert_raises "$IPADDRCHECK --is-ipv4-cidr $address" 0 162 | done 163 | 164 | for address in \ 165 | ${ipv4_single_positive[0]} \ 166 | ${ipv4_cidr_negative[*]} \ 167 | ${ipv6_single_positive[0]} \ 168 | ${ipv6_cidr_positive[0]} \ 169 | $string 170 | do 171 | assert_raises "$IPADDRCHECK --is-ipv4-cidr $address" 1 172 | done 173 | 174 | # --is-ipv4-single 175 | for address in \ 176 | ${ipv4_single_positive[*]} 177 | do 178 | assert_raises "$IPADDRCHECK --is-ipv4-single $address" 0 179 | done 180 | 181 | for address in \ 182 | ${ipv4_single_negative[*]} \ 183 | ${ipv4_cidr_postive[0]} \ 184 | ${ipv6_single_positive[0]} \ 185 | ${ipv6_cidr_positive[0]} \ 186 | $string 187 | do 188 | assert_raises "$IPADDRCHECK --is-ipv4-single $address" 1 189 | done 190 | 191 | # --is-any-host 192 | # --is-any-net 193 | # --is-ipv4-host 194 | # --is-ipv4-net 195 | # --is-ipv4-broadcast 196 | # --is-ipv4-multicast 197 | # --is-ipv4-loopback 198 | # --is-ipv4-link-local 199 | # --is-ipv4-rfc1918 200 | # --is-ipv6 201 | # --is-ipv6-cidr 202 | # --is-ipv6-single 203 | # --is-ipv6-host 204 | # --is-ipv6-net 205 | # --is-ipv6-multicast 206 | # --is-ipv6-link-local 207 | # --is-valid-intf-address 208 | 209 | assert_end ipaddrcheck_integration 210 | -------------------------------------------------------------------------------- /src/ipaddrcheck_functions.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ipaddrcheck_functions.c: IPv4/IPv6 validation functions for ipaddrcheck 3 | * 4 | * Copyright (C) 2013 Daniil Baturin 5 | * Copyright (C) 2018 VyOS maintainers and contributors 6 | * 7 | * This library is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU Lesser General Public 9 | * License as published by the Free Software Foundation; either 10 | * version 2.1 of the License, or (at your option) any later version. 11 | * 12 | * This library is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | * Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public 18 | * License along with this library; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 20 | * 21 | */ 22 | 23 | #include "ipaddrcheck_functions.h" 24 | 25 | /* 26 | * Address string functions 27 | * 28 | * Note that they perform format check only 29 | * and must not be used to deermine if it's 30 | * a valid address, only what type of address 31 | * format it is. 32 | * 33 | * The only reason they exist is that libcidr 34 | * is very liberal on its input format and 35 | * doesn't provide any information on what 36 | * the format was. 37 | */ 38 | 39 | 40 | /* Does it contain double semicolons? This is not allowed in IPv6 addresses */ 41 | int duplicate_double_semicolons(char* address_str) { 42 | int offsets[1]; 43 | pcre *re; 44 | int rc; 45 | const char *error; 46 | int erroffset; 47 | 48 | re = pcre_compile(".*(::).*\\1", 0, &error, &erroffset, NULL); 49 | rc = pcre_exec(re, NULL, address_str, strlen(address_str), 0, 0, offsets, 1); 50 | 51 | if( rc >= 0) 52 | { 53 | return(1); 54 | } 55 | else 56 | { 57 | return(0); 58 | } 59 | } 60 | 61 | /* Does it look like IPv4 CIDR (e.g. 192.0.2.1/24)? */ 62 | int is_ipv4_cidr(char* address_str) 63 | { 64 | int result; 65 | 66 | int offsets[1]; 67 | pcre *re; 68 | int rc; 69 | const char *error; 70 | int erroffset; 71 | 72 | re = pcre_compile("^((([1-9]\\d{0,2}|0)\\.){3}([1-9]\\d{0,2}|0)\\/([1-9]\\d*|0))$", 73 | 0, &error, &erroffset, NULL); 74 | rc = pcre_exec(re, NULL, address_str, strlen(address_str), 0, 0, offsets, 1); 75 | 76 | if( rc < 0 ) 77 | { 78 | result = RESULT_FAILURE; 79 | } 80 | else 81 | { 82 | result = RESULT_SUCCESS; 83 | } 84 | 85 | return(result); 86 | } 87 | 88 | /* Is it a single dotted decimal address? */ 89 | int is_ipv4_single(char* address_str) 90 | { 91 | int result; 92 | 93 | int offsets[1]; 94 | pcre *re; 95 | int rc; 96 | const char *error; 97 | int erroffset; 98 | 99 | re = pcre_compile("^((([1-9]\\d{0,2}|0)\\.){3}([1-9]\\d{0,2}|0))$", 100 | 0, &error, &erroffset, NULL); 101 | rc = pcre_exec(re, NULL, address_str, strlen(address_str), 0, 0, offsets, 1); 102 | 103 | if( rc < 0 ) 104 | { 105 | result = RESULT_FAILURE; 106 | } 107 | else 108 | { 109 | result = RESULT_SUCCESS; 110 | } 111 | 112 | return(result); 113 | } 114 | 115 | /* Is it an IPv6 address with prefix length? */ 116 | int is_ipv6_cidr(char* address_str) 117 | { 118 | int result; 119 | 120 | int offsets[1]; 121 | pcre *re; 122 | int rc; 123 | const char *error; 124 | int erroffset; 125 | 126 | re = pcre_compile("^((([0-9a-fA-F\\:])+)(\\/\\d{1,3}))$", 127 | 0, &error, &erroffset, NULL); 128 | rc = pcre_exec(re, NULL, address_str, strlen(address_str), 0, 0, offsets, 1); 129 | 130 | if( rc < 0 ) 131 | { 132 | result = RESULT_FAILURE; 133 | } 134 | else 135 | { 136 | result = RESULT_SUCCESS; 137 | } 138 | 139 | return(result); 140 | } 141 | 142 | /* Is it a single IPv6 address? */ 143 | int is_ipv6_single(char* address_str) 144 | { 145 | int result; 146 | 147 | int offsets[1]; 148 | pcre *re; 149 | int rc; 150 | const char *error; 151 | int erroffset; 152 | 153 | re = pcre_compile("^(([0-9a-fA-F\\:])+)$", 154 | 0, &error, &erroffset, NULL); 155 | rc = pcre_exec(re, NULL, address_str, strlen(address_str), 0, 0, offsets, 1); 156 | 157 | if( rc < 0 ) 158 | { 159 | result = RESULT_FAILURE; 160 | } 161 | else 162 | { 163 | result = RESULT_SUCCESS; 164 | } 165 | 166 | return(result); 167 | } 168 | 169 | /* Is it a CIDR-formatted IPv4 or IPv6 address? */ 170 | int is_any_cidr(char* address_str) 171 | { 172 | int result; 173 | 174 | if( (is_ipv4_cidr(address_str) == RESULT_SUCCESS) || 175 | (is_ipv6_cidr(address_str) == RESULT_SUCCESS) ) 176 | { 177 | result = RESULT_SUCCESS; 178 | } 179 | else 180 | { 181 | result = RESULT_FAILURE; 182 | } 183 | 184 | return(result); 185 | } 186 | 187 | /* Is it a single IPv4 or IPv6 address? */ 188 | int is_any_single(char* address_str) 189 | { 190 | int result; 191 | 192 | if( (is_ipv4_single(address_str) == RESULT_SUCCESS) || 193 | (is_ipv6_single(address_str) == RESULT_SUCCESS) ) 194 | { 195 | result = RESULT_SUCCESS; 196 | } 197 | else 198 | { 199 | result = RESULT_FAILURE; 200 | } 201 | 202 | return(result); 203 | } 204 | 205 | /* 206 | * Address checking functions that rely on libcidr 207 | */ 208 | 209 | /* Does it look like a valid address of any protocol? */ 210 | int is_valid_address(CIDR *address) 211 | { 212 | int result; 213 | 214 | if( cidr_get_proto(address) != INVALID_PROTO ) 215 | { 216 | result = RESULT_SUCCESS; 217 | } 218 | else 219 | { 220 | result = RESULT_FAILURE; 221 | } 222 | 223 | return(result); 224 | } 225 | 226 | /* Is it a correct IPv4 host or subnet address 227 | with or without net mask */ 228 | int is_ipv4(CIDR *address) 229 | { 230 | int result; 231 | 232 | if( cidr_get_proto(address) == CIDR_IPV4 ) 233 | { 234 | result = RESULT_SUCCESS; 235 | } 236 | else 237 | { 238 | result = RESULT_FAILURE; 239 | } 240 | 241 | return(result); 242 | } 243 | 244 | /* Is it a correct IPv4 host (i.e. not network) address? */ 245 | int is_ipv4_host(CIDR *address) 246 | { 247 | int result; 248 | 249 | if( (cidr_get_proto(address) == CIDR_IPV4) && 250 | ((cidr_equals(address, cidr_addr_network(address)) < 0) || 251 | (cidr_get_pflen(address) == 32)) ) 252 | { 253 | result = RESULT_SUCCESS; 254 | } 255 | else 256 | { 257 | result = RESULT_FAILURE; 258 | } 259 | 260 | return(result); 261 | } 262 | 263 | /* Is it a correct IPv4 network address? */ 264 | int is_ipv4_net(CIDR *address) 265 | { 266 | int result; 267 | 268 | if( (cidr_get_proto(address) == CIDR_IPV4) && 269 | (cidr_equals(address, cidr_addr_network(address)) == 0) ) 270 | { 271 | result = RESULT_SUCCESS; 272 | } 273 | else 274 | { 275 | result = RESULT_FAILURE; 276 | } 277 | 278 | return(result); 279 | } 280 | 281 | /* Is it an IPv4 broadcast address? */ 282 | int is_ipv4_broadcast(CIDR *address) 283 | { 284 | int result; 285 | 286 | /* The very concept of broadcast address doesn't apply to 287 | IPv6 and point-to-point or /32 IPv4 */ 288 | if( (cidr_get_proto(address) == CIDR_IPV4) && 289 | (cidr_equals(address, cidr_addr_broadcast(address)) == 0 ) && 290 | (cidr_get_pflen(address) < 31) ) 291 | { 292 | result = RESULT_SUCCESS; 293 | } 294 | else 295 | { 296 | result = RESULT_FAILURE; 297 | } 298 | 299 | return(result); 300 | } 301 | 302 | /* Is it an IPv4 multicast address? */ 303 | int is_ipv4_multicast(CIDR *address) 304 | { 305 | int result; 306 | 307 | if( (cidr_get_proto(address) == CIDR_IPV4) && 308 | (cidr_contains(cidr_from_str(IPV4_MULTICAST), address) == 0) ) 309 | { 310 | result = RESULT_SUCCESS; 311 | } 312 | else 313 | { 314 | result = RESULT_FAILURE; 315 | } 316 | 317 | return(result); 318 | } 319 | 320 | /* Is it an IPv4 loopback address? */ 321 | int is_ipv4_loopback(CIDR *address) 322 | { 323 | int result; 324 | 325 | if( (cidr_get_proto(address) == CIDR_IPV4) && 326 | (cidr_contains(cidr_from_str(IPV4_LOOPBACK), address) == 0) ) 327 | { 328 | result = RESULT_SUCCESS; 329 | } 330 | else 331 | { 332 | result = RESULT_FAILURE; 333 | } 334 | 335 | return(result); 336 | } 337 | 338 | /* Is it an IPv4 link-local address? */ 339 | int is_ipv4_link_local(CIDR *address) 340 | { 341 | int result; 342 | 343 | if( (cidr_get_proto(address) == CIDR_IPV4) && 344 | (cidr_contains(cidr_from_str(IPV4_LINKLOCAL), address) == 0) ) 345 | { 346 | result = RESULT_SUCCESS; 347 | } 348 | else 349 | { 350 | result = RESULT_FAILURE; 351 | } 352 | 353 | return(result); 354 | } 355 | 356 | /* Is it an IPv4 RFC1918 address? */ 357 | int is_ipv4_rfc1918(CIDR *address) 358 | { 359 | int result; 360 | 361 | if( (cidr_get_proto(address) == CIDR_IPV4) && 362 | ( (cidr_contains(cidr_from_str(IPV4_RFC1918_A), address) == 0) || 363 | (cidr_contains(cidr_from_str(IPV4_RFC1918_B), address) == 0) || 364 | (cidr_contains(cidr_from_str(IPV4_RFC1918_C), address) == 0) ) ) 365 | { 366 | result = RESULT_SUCCESS; 367 | } 368 | else 369 | { 370 | result = RESULT_FAILURE; 371 | } 372 | 373 | return(result); 374 | } 375 | 376 | /* is it a correct IPv6 host or subnet address, 377 | with or withour mask */ 378 | int is_ipv6(CIDR *address) 379 | { 380 | int result; 381 | 382 | if( cidr_get_proto(address) == CIDR_IPV6 ) 383 | { 384 | result = RESULT_SUCCESS; 385 | } 386 | else 387 | { 388 | result = RESULT_FAILURE; 389 | } 390 | 391 | return(result); 392 | } 393 | 394 | /* Is it a correct IPv6 host address? */ 395 | int is_ipv6_host(CIDR *address) 396 | { 397 | int result; 398 | 399 | if( (cidr_get_proto(address) == CIDR_IPV6) && 400 | ((cidr_equals(address, cidr_addr_network(address)) < 0) || 401 | (cidr_get_pflen(address) == 128)) ) 402 | { 403 | result = RESULT_SUCCESS; 404 | } 405 | else 406 | { 407 | result = RESULT_FAILURE; 408 | } 409 | 410 | return(result); 411 | } 412 | 413 | /* Is it a correct IPv6 network address? */ 414 | int is_ipv6_net(CIDR *address) 415 | { 416 | int result; 417 | 418 | if( (cidr_get_proto(address) == CIDR_IPV6) && 419 | (cidr_equals(address, cidr_addr_network(address)) == 0) ) 420 | { 421 | result = RESULT_SUCCESS; 422 | } 423 | else 424 | { 425 | result = RESULT_FAILURE; 426 | } 427 | 428 | return(result); 429 | } 430 | 431 | /* Is it an IPv6 multicast address? */ 432 | int is_ipv6_multicast(CIDR *address) 433 | { 434 | int result; 435 | 436 | if( (cidr_get_proto(address) == CIDR_IPV6) && 437 | (cidr_contains(cidr_from_str(IPV6_MULTICAST), address) == 0) ) 438 | { 439 | result = RESULT_SUCCESS; 440 | } 441 | else 442 | { 443 | result = RESULT_FAILURE; 444 | } 445 | 446 | return(result); 447 | } 448 | 449 | /* Is it an IPv6 link-local address? */ 450 | int is_ipv6_link_local(CIDR *address) 451 | { 452 | int result; 453 | 454 | if( (cidr_get_proto(address) == CIDR_IPV6) && 455 | (cidr_contains(cidr_from_str(IPV6_LINKLOCAL), address) == 0) ) 456 | { 457 | result = RESULT_SUCCESS; 458 | } 459 | else 460 | { 461 | result = RESULT_FAILURE; 462 | } 463 | 464 | return(result); 465 | } 466 | 467 | /* Is it an address that can belong an interface? */ 468 | int is_valid_intf_address(CIDR *address, char* address_str, int allow_loopback) 469 | { 470 | int result; 471 | 472 | if( (is_ipv4_broadcast(address) == RESULT_FAILURE) && 473 | (is_ipv4_multicast(address) == RESULT_FAILURE) && 474 | (is_ipv6_multicast(address) == RESULT_FAILURE) && 475 | ((is_ipv4_loopback(address) == RESULT_FAILURE) || (allow_loopback == LOOPBACK_ALLOWED)) && 476 | (cidr_equals(address, cidr_from_str(IPV6_LOOPBACK)) != 0) && 477 | (cidr_equals(address, cidr_from_str(IPV4_UNSPECIFIED)) != 0) && 478 | (cidr_contains(cidr_from_str(IPV4_THIS), address) != 0) && 479 | (cidr_equals(address, cidr_from_str(IPV4_LIMITED_BROADCAST)) != 0) && 480 | (is_any_host(address) == RESULT_SUCCESS) && 481 | (is_any_cidr(address_str) == RESULT_SUCCESS) ) 482 | { 483 | result = RESULT_SUCCESS; 484 | } 485 | else 486 | { 487 | result = RESULT_FAILURE; 488 | } 489 | 490 | return(result); 491 | } 492 | 493 | /* Is it an IPv4 or IPv6 host address? */ 494 | int is_any_host(CIDR *address) 495 | { 496 | int result; 497 | 498 | if( (is_ipv4_host(address) == RESULT_SUCCESS) || 499 | (is_ipv6_host(address) == RESULT_SUCCESS) ) 500 | { 501 | result = RESULT_SUCCESS; 502 | } 503 | else 504 | { 505 | result = RESULT_FAILURE; 506 | } 507 | 508 | return(result); 509 | } 510 | 511 | /* Is it an IPv4 or IPv6 network address? */ 512 | int is_any_net(CIDR *address) 513 | { 514 | int result; 515 | 516 | if( (is_ipv4_net(address) == RESULT_SUCCESS) || 517 | (is_ipv6_net(address) == RESULT_SUCCESS) ) 518 | { 519 | result = RESULT_SUCCESS; 520 | } 521 | else 522 | { 523 | result = RESULT_FAILURE; 524 | } 525 | 526 | return(result); 527 | } 528 | 529 | 530 | -------------------------------------------------------------------------------- /INSTALL: -------------------------------------------------------------------------------- 1 | Installation Instructions 2 | ************************* 3 | 4 | Copyright (C) 1994, 1995, 1996, 1999, 2000, 2001, 2002, 2004, 2005, 5 | 2006, 2007, 2008, 2009 Free Software Foundation, Inc. 6 | 7 | Copying and distribution of this file, with or without modification, 8 | are permitted in any medium without royalty provided the copyright 9 | notice and this notice are preserved. This file is offered as-is, 10 | without warranty of any kind. 11 | 12 | Basic Installation 13 | ================== 14 | 15 | Briefly, the shell commands `./configure; make; make install' should 16 | configure, build, and install this package. The following 17 | more-detailed instructions are generic; see the `README' file for 18 | instructions specific to this package. Some packages provide this 19 | `INSTALL' file but do not implement all of the features documented 20 | below. The lack of an optional feature in a given package is not 21 | necessarily a bug. More recommendations for GNU packages can be found 22 | in *note Makefile Conventions: (standards)Makefile Conventions. 23 | 24 | The `configure' shell script attempts to guess correct values for 25 | various system-dependent variables used during compilation. It uses 26 | those values to create a `Makefile' in each directory of the package. 27 | It may also create one or more `.h' files containing system-dependent 28 | definitions. Finally, it creates a shell script `config.status' that 29 | you can run in the future to recreate the current configuration, and a 30 | file `config.log' containing compiler output (useful mainly for 31 | debugging `configure'). 32 | 33 | It can also use an optional file (typically called `config.cache' 34 | and enabled with `--cache-file=config.cache' or simply `-C') that saves 35 | the results of its tests to speed up reconfiguring. Caching is 36 | disabled by default to prevent problems with accidental use of stale 37 | cache files. 38 | 39 | If you need to do unusual things to compile the package, please try 40 | to figure out how `configure' could check whether to do them, and mail 41 | diffs or instructions to the address given in the `README' so they can 42 | be considered for the next release. If you are using the cache, and at 43 | some point `config.cache' contains results you don't want to keep, you 44 | may remove or edit it. 45 | 46 | The file `configure.ac' (or `configure.in') is used to create 47 | `configure' by a program called `autoconf'. You need `configure.ac' if 48 | you want to change it or regenerate `configure' using a newer version 49 | of `autoconf'. 50 | 51 | The simplest way to compile this package is: 52 | 53 | 1. `cd' to the directory containing the package's source code and type 54 | `./configure' to configure the package for your system. 55 | 56 | Running `configure' might take a while. While running, it prints 57 | some messages telling which features it is checking for. 58 | 59 | 2. Type `make' to compile the package. 60 | 61 | 3. Optionally, type `make check' to run any self-tests that come with 62 | the package, generally using the just-built uninstalled binaries. 63 | 64 | 4. Type `make install' to install the programs and any data files and 65 | documentation. When installing into a prefix owned by root, it is 66 | recommended that the package be configured and built as a regular 67 | user, and only the `make install' phase executed with root 68 | privileges. 69 | 70 | 5. Optionally, type `make installcheck' to repeat any self-tests, but 71 | this time using the binaries in their final installed location. 72 | This target does not install anything. Running this target as a 73 | regular user, particularly if the prior `make install' required 74 | root privileges, verifies that the installation completed 75 | correctly. 76 | 77 | 6. You can remove the program binaries and object files from the 78 | source code directory by typing `make clean'. To also remove the 79 | files that `configure' created (so you can compile the package for 80 | a different kind of computer), type `make distclean'. There is 81 | also a `make maintainer-clean' target, but that is intended mainly 82 | for the package's developers. If you use it, you may have to get 83 | all sorts of other programs in order to regenerate files that came 84 | with the distribution. 85 | 86 | 7. Often, you can also type `make uninstall' to remove the installed 87 | files again. In practice, not all packages have tested that 88 | uninstallation works correctly, even though it is required by the 89 | GNU Coding Standards. 90 | 91 | 8. Some packages, particularly those that use Automake, provide `make 92 | distcheck', which can by used by developers to test that all other 93 | targets like `make install' and `make uninstall' work correctly. 94 | This target is generally not run by end users. 95 | 96 | Compilers and Options 97 | ===================== 98 | 99 | Some systems require unusual options for compilation or linking that 100 | the `configure' script does not know about. Run `./configure --help' 101 | for details on some of the pertinent environment variables. 102 | 103 | You can give `configure' initial values for configuration parameters 104 | by setting variables in the command line or in the environment. Here 105 | is an example: 106 | 107 | ./configure CC=c99 CFLAGS=-g LIBS=-lposix 108 | 109 | *Note Defining Variables::, for more details. 110 | 111 | Compiling For Multiple Architectures 112 | ==================================== 113 | 114 | You can compile the package for more than one kind of computer at the 115 | same time, by placing the object files for each architecture in their 116 | own directory. To do this, you can use GNU `make'. `cd' to the 117 | directory where you want the object files and executables to go and run 118 | the `configure' script. `configure' automatically checks for the 119 | source code in the directory that `configure' is in and in `..'. This 120 | is known as a "VPATH" build. 121 | 122 | With a non-GNU `make', it is safer to compile the package for one 123 | architecture at a time in the source code directory. After you have 124 | installed the package for one architecture, use `make distclean' before 125 | reconfiguring for another architecture. 126 | 127 | On MacOS X 10.5 and later systems, you can create libraries and 128 | executables that work on multiple system types--known as "fat" or 129 | "universal" binaries--by specifying multiple `-arch' options to the 130 | compiler but only a single `-arch' option to the preprocessor. Like 131 | this: 132 | 133 | ./configure CC="gcc -arch i386 -arch x86_64 -arch ppc -arch ppc64" \ 134 | CXX="g++ -arch i386 -arch x86_64 -arch ppc -arch ppc64" \ 135 | CPP="gcc -E" CXXCPP="g++ -E" 136 | 137 | This is not guaranteed to produce working output in all cases, you 138 | may have to build one architecture at a time and combine the results 139 | using the `lipo' tool if you have problems. 140 | 141 | Installation Names 142 | ================== 143 | 144 | By default, `make install' installs the package's commands under 145 | `/usr/local/bin', include files under `/usr/local/include', etc. You 146 | can specify an installation prefix other than `/usr/local' by giving 147 | `configure' the option `--prefix=PREFIX', where PREFIX must be an 148 | absolute file name. 149 | 150 | You can specify separate installation prefixes for 151 | architecture-specific files and architecture-independent files. If you 152 | pass the option `--exec-prefix=PREFIX' to `configure', the package uses 153 | PREFIX as the prefix for installing programs and libraries. 154 | Documentation and other data files still use the regular prefix. 155 | 156 | In addition, if you use an unusual directory layout you can give 157 | options like `--bindir=DIR' to specify different values for particular 158 | kinds of files. Run `configure --help' for a list of the directories 159 | you can set and what kinds of files go in them. In general, the 160 | default for these options is expressed in terms of `${prefix}', so that 161 | specifying just `--prefix' will affect all of the other directory 162 | specifications that were not explicitly provided. 163 | 164 | The most portable way to affect installation locations is to pass the 165 | correct locations to `configure'; however, many packages provide one or 166 | both of the following shortcuts of passing variable assignments to the 167 | `make install' command line to change installation locations without 168 | having to reconfigure or recompile. 169 | 170 | The first method involves providing an override variable for each 171 | affected directory. For example, `make install 172 | prefix=/alternate/directory' will choose an alternate location for all 173 | directory configuration variables that were expressed in terms of 174 | `${prefix}'. Any directories that were specified during `configure', 175 | but not in terms of `${prefix}', must each be overridden at install 176 | time for the entire installation to be relocated. The approach of 177 | makefile variable overrides for each directory variable is required by 178 | the GNU Coding Standards, and ideally causes no recompilation. 179 | However, some platforms have known limitations with the semantics of 180 | shared libraries that end up requiring recompilation when using this 181 | method, particularly noticeable in packages that use GNU Libtool. 182 | 183 | The second method involves providing the `DESTDIR' variable. For 184 | example, `make install DESTDIR=/alternate/directory' will prepend 185 | `/alternate/directory' before all installation names. The approach of 186 | `DESTDIR' overrides is not required by the GNU Coding Standards, and 187 | does not work on platforms that have drive letters. On the other hand, 188 | it does better at avoiding recompilation issues, and works well even 189 | when some directory options were not specified in terms of `${prefix}' 190 | at `configure' time. 191 | 192 | Optional Features 193 | ================= 194 | 195 | If the package supports it, you can cause programs to be installed 196 | with an extra prefix or suffix on their names by giving `configure' the 197 | option `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'. 198 | 199 | Some packages pay attention to `--enable-FEATURE' options to 200 | `configure', where FEATURE indicates an optional part of the package. 201 | They may also pay attention to `--with-PACKAGE' options, where PACKAGE 202 | is something like `gnu-as' or `x' (for the X Window System). The 203 | `README' should mention any `--enable-' and `--with-' options that the 204 | package recognizes. 205 | 206 | For packages that use the X Window System, `configure' can usually 207 | find the X include and library files automatically, but if it doesn't, 208 | you can use the `configure' options `--x-includes=DIR' and 209 | `--x-libraries=DIR' to specify their locations. 210 | 211 | Some packages offer the ability to configure how verbose the 212 | execution of `make' will be. For these packages, running `./configure 213 | --enable-silent-rules' sets the default to minimal output, which can be 214 | overridden with `make V=1'; while running `./configure 215 | --disable-silent-rules' sets the default to verbose, which can be 216 | overridden with `make V=0'. 217 | 218 | Particular systems 219 | ================== 220 | 221 | On HP-UX, the default C compiler is not ANSI C compatible. If GNU 222 | CC is not installed, it is recommended to use the following options in 223 | order to use an ANSI C compiler: 224 | 225 | ./configure CC="cc -Ae -D_XOPEN_SOURCE=500" 226 | 227 | and if that doesn't work, install pre-built binaries of GCC for HP-UX. 228 | 229 | On OSF/1 a.k.a. Tru64, some versions of the default C compiler cannot 230 | parse its `' header file. The option `-nodtk' can be used as 231 | a workaround. If GNU CC is not installed, it is therefore recommended 232 | to try 233 | 234 | ./configure CC="cc" 235 | 236 | and if that doesn't work, try 237 | 238 | ./configure CC="cc -nodtk" 239 | 240 | On Solaris, don't put `/usr/ucb' early in your `PATH'. This 241 | directory contains several dysfunctional programs; working variants of 242 | these programs are available in `/usr/bin'. So, if you need `/usr/ucb' 243 | in your `PATH', put it _after_ `/usr/bin'. 244 | 245 | On Haiku, software installed for all users goes in `/boot/common', 246 | not `/usr/local'. It is recommended to use the following options: 247 | 248 | ./configure --prefix=/boot/common 249 | 250 | Specifying the System Type 251 | ========================== 252 | 253 | There may be some features `configure' cannot figure out 254 | automatically, but needs to determine by the type of machine the package 255 | will run on. Usually, assuming the package is built to be run on the 256 | _same_ architectures, `configure' can figure that out, but if it prints 257 | a message saying it cannot guess the machine type, give it the 258 | `--build=TYPE' option. TYPE can either be a short name for the system 259 | type, such as `sun4', or a canonical name which has the form: 260 | 261 | CPU-COMPANY-SYSTEM 262 | 263 | where SYSTEM can have one of these forms: 264 | 265 | OS 266 | KERNEL-OS 267 | 268 | See the file `config.sub' for the possible values of each field. If 269 | `config.sub' isn't included in this package, then this package doesn't 270 | need to know the machine type. 271 | 272 | If you are _building_ compiler tools for cross-compiling, you should 273 | use the option `--target=TYPE' to select the type of system they will 274 | produce code for. 275 | 276 | If you want to _use_ a cross compiler, that generates code for a 277 | platform different from the build platform, you should specify the 278 | "host" platform (i.e., that on which the generated programs will 279 | eventually be run) with `--host=TYPE'. 280 | 281 | Sharing Defaults 282 | ================ 283 | 284 | If you want to set default values for `configure' scripts to share, 285 | you can create a site shell script called `config.site' that gives 286 | default values for variables like `CC', `cache_file', and `prefix'. 287 | `configure' looks for `PREFIX/share/config.site' if it exists, then 288 | `PREFIX/etc/config.site' if it exists. Or, you can set the 289 | `CONFIG_SITE' environment variable to the location of the site script. 290 | A warning: not all `configure' scripts look for a site script. 291 | 292 | Defining Variables 293 | ================== 294 | 295 | Variables not defined in a site shell script can be set in the 296 | environment passed to `configure'. However, some packages may run 297 | configure again during the build, and the customized values of these 298 | variables may be lost. In order to avoid this problem, you should set 299 | them in the `configure' command line, using `VAR=value'. For example: 300 | 301 | ./configure CC=/usr/local2/bin/gcc 302 | 303 | causes the specified `gcc' to be used as the C compiler (unless it is 304 | overridden in the site shell script). 305 | 306 | Unfortunately, this technique does not work for `CONFIG_SHELL' due to 307 | an Autoconf bug. Until the bug is fixed you can use this workaround: 308 | 309 | CONFIG_SHELL=/bin/bash /bin/bash ./configure CONFIG_SHELL=/bin/bash 310 | 311 | `configure' Invocation 312 | ====================== 313 | 314 | `configure' recognizes the following options to control how it 315 | operates. 316 | 317 | `--help' 318 | `-h' 319 | Print a summary of all of the options to `configure', and exit. 320 | 321 | `--help=short' 322 | `--help=recursive' 323 | Print a summary of the options unique to this package's 324 | `configure', and exit. The `short' variant lists options used 325 | only in the top level, while the `recursive' variant lists options 326 | also present in any nested packages. 327 | 328 | `--version' 329 | `-V' 330 | Print the version of Autoconf used to generate the `configure' 331 | script, and exit. 332 | 333 | `--cache-file=FILE' 334 | Enable the cache: use and save the results of the tests in FILE, 335 | traditionally `config.cache'. FILE defaults to `/dev/null' to 336 | disable caching. 337 | 338 | `--config-cache' 339 | `-C' 340 | Alias for `--cache-file=config.cache'. 341 | 342 | `--quiet' 343 | `--silent' 344 | `-q' 345 | Do not print messages saying which checks are being made. To 346 | suppress all normal output, redirect it to `/dev/null' (any error 347 | messages will still be shown). 348 | 349 | `--srcdir=DIR' 350 | Look for the package's source code in directory DIR. Usually 351 | `configure' can determine that directory automatically. 352 | 353 | `--prefix=DIR' 354 | Use DIR as the installation prefix. *note Installation Names:: 355 | for more details, including other options available for fine-tuning 356 | the installation locations. 357 | 358 | `--no-create' 359 | `-n' 360 | Run the configure checks, but stop before creating any output 361 | files. 362 | 363 | `configure' also accepts some other, not widely useful, options. Run 364 | `configure --help' for more details. 365 | 366 | -------------------------------------------------------------------------------- /tests/check_ipaddrcheck.c: -------------------------------------------------------------------------------- 1 | /* 2 | * check_ipaddrcheck.c: ipaddrcheck unit tests 3 | * 4 | * Copyright (C) 2013 Daniil Baturin 5 | * Copyright (C) 2018 VyOS maintainers and contributors 6 | * 7 | * This program is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License version 2 or later as 9 | * published by the Free Software Foundation. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | * 19 | */ 20 | 21 | #include 22 | #include "../src/ipaddrcheck_functions.h" 23 | 24 | START_TEST (test_is_valid_address) 25 | { 26 | char* good_v4_address_str = "192.0.2.1"; 27 | CIDR* good_v4_address = cidr_from_str(good_v4_address_str); 28 | ck_assert_int_eq(is_valid_address(good_v4_address), RESULT_SUCCESS); 29 | cidr_free(good_v4_address); 30 | 31 | char* good_v6_address_str = "2001:db8:dead::1/56"; 32 | CIDR* good_v6_address = cidr_from_str(good_v6_address_str); 33 | ck_assert_int_eq(is_valid_address(good_v6_address), RESULT_SUCCESS); 34 | cidr_free(good_v6_address); 35 | 36 | char* bad_address_str = "192.0.299.563"; 37 | CIDR* bad_address = cidr_from_str(bad_address_str); 38 | ck_assert_int_eq(is_valid_address(bad_address), RESULT_FAILURE); 39 | cidr_free(bad_address); 40 | } 41 | END_TEST 42 | 43 | START_TEST (test_is_ipv4_cidr) 44 | { 45 | char* good_address_str_1 = "192.0.2.1/8"; 46 | ck_assert_int_eq(is_ipv4_cidr(good_address_str_1), RESULT_SUCCESS); 47 | 48 | char* good_address_str_2 = "192.0.2.1/21"; 49 | ck_assert_int_eq(is_ipv4_cidr(good_address_str_2), RESULT_SUCCESS); 50 | 51 | char* address_str_no_mask = "192.0.2.1"; 52 | ck_assert_int_eq(is_ipv4_cidr(address_str_no_mask), RESULT_FAILURE); 53 | 54 | /* libcidr allows it, but we don't want to support it */ 55 | char* address_str_decimal_mask = "192.0.2.1/255.255.255.0"; 56 | ck_assert_int_eq(is_ipv4_cidr(address_str_decimal_mask), RESULT_FAILURE); 57 | } 58 | END_TEST 59 | 60 | START_TEST (test_is_ipv4_single) 61 | { 62 | char* good_address_str = "192.0.2.1"; 63 | ck_assert_int_eq(is_ipv4_single(good_address_str), RESULT_SUCCESS); 64 | 65 | char* bad_address_str = "192.0.2.1/25"; 66 | ck_assert_int_eq(is_ipv4_single(bad_address_str), RESULT_FAILURE); 67 | } 68 | END_TEST 69 | 70 | START_TEST (test_is_ipv6_cidr) 71 | { 72 | char* good_address_str = "2001:db8:abcd::/64"; 73 | ck_assert_int_eq(is_ipv6_cidr(good_address_str), RESULT_SUCCESS); 74 | 75 | char* address_str_no_mask = "2001:db8::1"; 76 | ck_assert_int_eq(is_ipv6_cidr(address_str_no_mask), RESULT_FAILURE); 77 | 78 | /* libcidr allows fully spellt hex masks, but we don't want to support it */ 79 | char* address_str_decimal_mask = "::/0:0:0:0:0:0:0:0"; 80 | ck_assert_int_eq(is_ipv6_cidr(address_str_decimal_mask), RESULT_FAILURE); 81 | } 82 | END_TEST 83 | 84 | START_TEST (test_is_ipv6_single) 85 | { 86 | char* good_address_str = "2001:db8::10"; 87 | ck_assert_int_eq(is_ipv6_single(good_address_str), RESULT_SUCCESS); 88 | 89 | char* bad_address_str = "2001:db8::/32"; 90 | ck_assert_int_eq(is_ipv6_single(bad_address_str), RESULT_FAILURE); 91 | } 92 | END_TEST 93 | 94 | START_TEST (test_is_any_cidr) 95 | { 96 | char* good_address_str_v4 = "192.0.2.1/21"; 97 | ck_assert_int_eq(is_any_cidr(good_address_str_v4), RESULT_SUCCESS); 98 | 99 | char* address_str_no_mask_v4 = "192.0.2.1"; 100 | ck_assert_int_eq(is_any_cidr(address_str_no_mask_v4), RESULT_FAILURE); 101 | 102 | char* good_address_str_v6 = "2001:db8::a/56"; 103 | ck_assert_int_eq(is_any_cidr(good_address_str_v6), RESULT_SUCCESS); 104 | 105 | char* address_str_no_mask_v6 = "2001:db8:a:b::c"; 106 | ck_assert_int_eq(is_any_cidr(address_str_no_mask_v6), RESULT_FAILURE); 107 | } 108 | END_TEST 109 | 110 | START_TEST (test_is_any_single) 111 | { 112 | char* good_address_str_v4 = "192.0.2.1"; 113 | ck_assert_int_eq(is_any_single(good_address_str_v4), RESULT_SUCCESS); 114 | 115 | char* bad_address_str_v4 = "192.0.2.1/25"; 116 | ck_assert_int_eq(is_any_single(bad_address_str_v4), RESULT_FAILURE); 117 | 118 | char* good_address_str_v6 = "2001:db8::10"; 119 | ck_assert_int_eq(is_any_single(good_address_str_v6), RESULT_SUCCESS); 120 | 121 | char* bad_address_str_v6 = "2001:db8::/32"; 122 | ck_assert_int_eq(is_any_single(bad_address_str_v6), RESULT_FAILURE); 123 | } 124 | END_TEST 125 | 126 | START_TEST (test_is_ipv4) 127 | { 128 | char* good_address_str = "192.0.2.1"; 129 | CIDR* good_address = cidr_from_str(good_address_str); 130 | ck_assert_int_eq(is_ipv4(good_address), RESULT_SUCCESS); 131 | cidr_free(good_address); 132 | 133 | char* bad_address_str = "2001:db8::1/64"; 134 | CIDR* bad_address = cidr_from_str(bad_address_str); 135 | ck_assert_int_eq(is_ipv4(bad_address), RESULT_FAILURE); 136 | cidr_free(bad_address); 137 | 138 | } 139 | END_TEST 140 | 141 | START_TEST (test_is_ipv4_host) 142 | { 143 | char* good_address_str_no_mask = "192.0.2.1"; 144 | CIDR* good_address = cidr_from_str(good_address_str_no_mask); 145 | ck_assert_int_eq(is_ipv4_host(good_address), RESULT_SUCCESS); 146 | cidr_free(good_address); 147 | 148 | char* good_address_str_cidr = "192.0.2.55/24"; 149 | CIDR* good_address_cidr = cidr_from_str(good_address_str_cidr); 150 | ck_assert_int_eq(is_ipv4_host(good_address_cidr), RESULT_SUCCESS); 151 | cidr_free(good_address_cidr); 152 | 153 | char* bad_address_str = "192.0.2.0/24"; 154 | CIDR* bad_address = cidr_from_str(bad_address_str); 155 | ck_assert_int_eq(is_ipv4_host(bad_address), RESULT_FAILURE); 156 | cidr_free(bad_address); 157 | } 158 | END_TEST 159 | 160 | START_TEST (test_is_ipv4_net) 161 | { 162 | char* good_address_str = "192.0.2.0/25"; 163 | CIDR* good_address = cidr_from_str(good_address_str); 164 | ck_assert_int_eq(is_ipv4_net(good_address), RESULT_SUCCESS); 165 | cidr_free(good_address); 166 | 167 | char* bad_address_str = "192.0.2.55/24"; 168 | CIDR* bad_address = cidr_from_str(bad_address_str); 169 | ck_assert_int_eq(is_ipv4_net(bad_address), RESULT_FAILURE); 170 | cidr_free(bad_address); 171 | } 172 | END_TEST 173 | 174 | START_TEST (test_is_ipv4_broadcast) 175 | { 176 | char* good_address_str = "192.0.2.255/24"; 177 | CIDR* good_address = cidr_from_str(good_address_str); 178 | ck_assert_int_eq(is_ipv4_broadcast(good_address), RESULT_SUCCESS); 179 | cidr_free(good_address); 180 | 181 | char* bad_address_str = "192.0.2.55/24"; 182 | CIDR* bad_address = cidr_from_str(bad_address_str); 183 | ck_assert_int_eq(is_ipv4_broadcast(bad_address), RESULT_FAILURE); 184 | cidr_free(bad_address); 185 | 186 | char* bad_address_str_ptp = "192.0.2.1/31"; 187 | CIDR* bad_address_ptp = cidr_from_str(bad_address_str_ptp); 188 | ck_assert_int_eq(is_ipv4_broadcast(bad_address_ptp), RESULT_FAILURE); 189 | cidr_free(bad_address_ptp); 190 | 191 | char* bad_address_str_v6 = "2001:0db8:ffff:ffff:ffff:ffff:ffff:ffff/32"; 192 | CIDR* bad_address_v6 = cidr_from_str(bad_address_str_v6); 193 | ck_assert_int_eq(is_ipv4_broadcast(bad_address_v6), RESULT_FAILURE); 194 | cidr_free(bad_address_v6); 195 | } 196 | END_TEST 197 | 198 | START_TEST (test_is_ipv4_multicast) 199 | { 200 | char* good_address_str = "224.0.0.5"; 201 | CIDR* good_address = cidr_from_str(good_address_str); 202 | ck_assert_int_eq(is_ipv4_multicast(good_address), RESULT_SUCCESS); 203 | cidr_free(good_address); 204 | 205 | char* bad_address_str = "192.0.2.55"; 206 | CIDR* bad_address = cidr_from_str(bad_address_str); 207 | ck_assert_int_eq(is_ipv4_multicast(bad_address), RESULT_FAILURE); 208 | cidr_free(bad_address); 209 | } 210 | END_TEST 211 | 212 | START_TEST (test_is_ipv4_loopback) 213 | { 214 | char* good_address_str = "127.0.0.90"; 215 | CIDR* good_address = cidr_from_str(good_address_str); 216 | ck_assert_int_eq(is_ipv4_loopback(good_address), RESULT_SUCCESS); 217 | cidr_free(good_address); 218 | 219 | char* bad_address_str = "192.0.2.55"; 220 | CIDR* bad_address = cidr_from_str(bad_address_str); 221 | ck_assert_int_eq(is_ipv4_loopback(bad_address), RESULT_FAILURE); 222 | cidr_free(bad_address); 223 | } 224 | END_TEST 225 | 226 | START_TEST (test_is_ipv4_link_local) 227 | { 228 | CIDR* address = NULL; 229 | 230 | char* good_address_str = "169.254.23.32"; 231 | address = cidr_from_str(good_address_str); 232 | ck_assert_int_eq(is_ipv4_link_local(address), RESULT_SUCCESS); 233 | cidr_free(address); 234 | 235 | char* bad_address_str = "192.0.2.55"; 236 | address = cidr_from_str(bad_address_str); 237 | ck_assert_int_eq(is_ipv4_link_local(address), RESULT_FAILURE); 238 | cidr_free(address); 239 | } 240 | END_TEST 241 | 242 | START_TEST (test_is_ipv4_rfc1918) 243 | { 244 | char* good_address_str_a = "10.0.0.1"; 245 | CIDR* good_address_a = cidr_from_str(good_address_str_a); 246 | ck_assert_int_eq(is_ipv4_rfc1918(good_address_a), RESULT_SUCCESS); 247 | cidr_free(good_address_a); 248 | 249 | char* good_address_str_b = "172.16.25.100"; 250 | CIDR* good_address_b = cidr_from_str(good_address_str_b); 251 | ck_assert_int_eq(is_ipv4_rfc1918(good_address_b), RESULT_SUCCESS); 252 | cidr_free(good_address_b); 253 | 254 | char* good_address_str_c = "192.168.1.67"; 255 | CIDR* good_address_c = cidr_from_str(good_address_str_c); 256 | ck_assert_int_eq(is_ipv4_rfc1918(good_address_c), RESULT_SUCCESS); 257 | cidr_free(good_address_c); 258 | 259 | char* bad_address_str = "192.0.2.55"; 260 | CIDR* bad_address = cidr_from_str(bad_address_str); 261 | ck_assert_int_eq(is_ipv4_link_local(bad_address), RESULT_FAILURE); 262 | cidr_free(bad_address); 263 | } 264 | END_TEST 265 | 266 | START_TEST (test_is_ipv6) 267 | { 268 | char* good_address_str = "2001:db8:1fe::49"; 269 | CIDR* good_address = cidr_from_str(good_address_str); 270 | ck_assert_int_eq(is_ipv6(good_address), RESULT_SUCCESS); 271 | cidr_free(good_address); 272 | 273 | char* bad_address_str = "192.0.2.44"; 274 | CIDR* bad_address = cidr_from_str(bad_address_str); 275 | ck_assert_int_eq(is_ipv6(bad_address), RESULT_FAILURE); 276 | cidr_free(bad_address); 277 | 278 | } 279 | END_TEST 280 | 281 | START_TEST (test_is_ipv6_host) 282 | { 283 | char* good_address_str_no_mask = "2001:db8:a::1"; 284 | CIDR* good_address = cidr_from_str(good_address_str_no_mask); 285 | ck_assert_int_eq(is_ipv6_host(good_address), RESULT_SUCCESS); 286 | cidr_free(good_address); 287 | 288 | char* good_address_str_cidr = "2001:db8:b::100/64"; 289 | CIDR* good_address_cidr = cidr_from_str(good_address_str_cidr); 290 | ck_assert_int_eq(is_ipv6_host(good_address_cidr), RESULT_SUCCESS); 291 | cidr_free(good_address_cidr); 292 | 293 | char* bad_address_str = "2001:db8:f::/48"; 294 | CIDR* bad_address = cidr_from_str(bad_address_str); 295 | ck_assert_int_eq(is_ipv6_host(bad_address), RESULT_FAILURE); 296 | cidr_free(bad_address); 297 | } 298 | END_TEST 299 | 300 | START_TEST (test_is_ipv6_net) 301 | { 302 | char* good_address_str = "2001:db8::/32"; 303 | CIDR* good_address = cidr_from_str(good_address_str); 304 | ck_assert_int_eq(is_ipv6_net(good_address), RESULT_SUCCESS); 305 | cidr_free(good_address); 306 | 307 | char* bad_address_str = "2001:db8:34::1/64"; 308 | CIDR* bad_address = cidr_from_str(bad_address_str); 309 | ck_assert_int_eq(is_ipv6_net(bad_address), RESULT_FAILURE); 310 | cidr_free(bad_address); 311 | } 312 | END_TEST 313 | 314 | START_TEST (test_is_ipv6_multicast) 315 | { 316 | char* good_address_str = "ff02::6"; 317 | CIDR* good_address = cidr_from_str(good_address_str); 318 | ck_assert_int_eq(is_ipv6_multicast(good_address), RESULT_SUCCESS); 319 | cidr_free(good_address); 320 | 321 | char* bad_address_str = "2001:db8::1"; 322 | CIDR* bad_address = cidr_from_str(bad_address_str); 323 | ck_assert_int_eq(is_ipv6_multicast(bad_address), RESULT_FAILURE); 324 | cidr_free(bad_address); 325 | } 326 | END_TEST 327 | 328 | START_TEST (test_is_ipv6_link_local) 329 | { 330 | CIDR* address = NULL; 331 | 332 | char* good_address_str = "fe80::5ab0:35ff:fef2:9365"; 333 | address = cidr_from_str(good_address_str); 334 | ck_assert_int_eq(is_ipv6_link_local(address), RESULT_SUCCESS); 335 | cidr_free(address); 336 | 337 | char* bad_address_str = "2001:db8::2"; 338 | address = cidr_from_str(bad_address_str); 339 | ck_assert_int_eq(is_ipv6_link_local(address), RESULT_FAILURE); 340 | cidr_free(address); 341 | } 342 | END_TEST 343 | 344 | START_TEST (test_is_valid_intf_address) 345 | { 346 | char* good_address_str_v4 = "192.0.2.5/24"; 347 | CIDR* good_address_v4 = cidr_from_str(good_address_str_v4); 348 | ck_assert_int_eq(is_valid_intf_address(good_address_v4, good_address_str_v4, NO_LOOPBACK), RESULT_SUCCESS); 349 | cidr_free(good_address_v4); 350 | 351 | char* good_address_str_v6 = "2001:db8:a:b::14/64"; 352 | CIDR* good_address_v6 = cidr_from_str(good_address_str_v6); 353 | ck_assert_int_eq(is_valid_intf_address(good_address_v6, good_address_str_v6, NO_LOOPBACK), RESULT_SUCCESS); 354 | cidr_free(good_address_v6); 355 | } 356 | END_TEST 357 | 358 | START_TEST (test_is_any_host) 359 | { 360 | char* good_address_str_v4 = "192.0.2.1/25"; 361 | CIDR* good_address_v4 = cidr_from_str(good_address_str_v4); 362 | ck_assert_int_eq(is_any_host(good_address_v4), RESULT_SUCCESS); 363 | cidr_free(good_address_v4); 364 | 365 | char* good_address_str_v6 = "2001:db8:aff::1/64"; 366 | CIDR* good_address_v6 = cidr_from_str(good_address_str_v6); 367 | ck_assert_int_eq(is_any_host(good_address_v6), RESULT_SUCCESS); 368 | cidr_free(good_address_v6); 369 | 370 | char* bad_address_str_v4 = "192.0.2.0/24"; 371 | CIDR* bad_address_v4 = cidr_from_str(bad_address_str_v4); 372 | ck_assert_int_eq(is_any_host(bad_address_v4), RESULT_FAILURE); 373 | cidr_free(bad_address_v4); 374 | 375 | char* bad_address_str_v6 = "2001:db8::/32"; 376 | CIDR* bad_address_v6 = cidr_from_str(bad_address_str_v6); 377 | ck_assert_int_eq(is_any_host(bad_address_v6), RESULT_FAILURE); 378 | cidr_free(bad_address_v6); 379 | } 380 | END_TEST 381 | 382 | START_TEST (test_is_any_net) 383 | { 384 | char* good_address_str_v4 = "192.0.2.0/25"; 385 | CIDR* good_address_v4 = cidr_from_str(good_address_str_v4); 386 | ck_assert_int_eq(is_any_net(good_address_v4), RESULT_SUCCESS); 387 | cidr_free(good_address_v4); 388 | 389 | char* good_address_str_v6 = "2001:db8:aff::/64"; 390 | CIDR* good_address_v6 = cidr_from_str(good_address_str_v6); 391 | ck_assert_int_eq(is_any_net(good_address_v6), RESULT_SUCCESS); 392 | cidr_free(good_address_v6); 393 | 394 | char* bad_address_str_v4 = "192.0.2.33/24"; 395 | CIDR* bad_address_v4 = cidr_from_str(bad_address_str_v4); 396 | ck_assert_int_eq(is_any_net(bad_address_v4), RESULT_FAILURE); 397 | cidr_free(bad_address_v4); 398 | 399 | char* bad_address_str_v6 = "2001:db8::1/32"; 400 | CIDR* bad_address_v6 = cidr_from_str(bad_address_str_v6); 401 | ck_assert_int_eq(is_any_net(bad_address_v6), RESULT_FAILURE); 402 | cidr_free(bad_address_v6); 403 | } 404 | END_TEST 405 | 406 | 407 | Suite *ipaddrcheck_suite(void) 408 | { 409 | Suite *s = suite_create("ipaddrcheck"); 410 | 411 | /* Core test case */ 412 | TCase *tc_core = tcase_create("Core"); 413 | tcase_add_test(tc_core, test_is_valid_address); 414 | tcase_add_test(tc_core, test_is_ipv4_cidr); 415 | tcase_add_test(tc_core, test_is_ipv4_single); 416 | tcase_add_test(tc_core, test_is_ipv6_cidr); 417 | tcase_add_test(tc_core, test_is_ipv6_single); 418 | tcase_add_test(tc_core, test_is_any_cidr); 419 | tcase_add_test(tc_core, test_is_any_single); 420 | tcase_add_test(tc_core, test_is_ipv4); 421 | tcase_add_test(tc_core, test_is_ipv4_host); 422 | tcase_add_test(tc_core, test_is_ipv4_net); 423 | tcase_add_test(tc_core, test_is_ipv4_broadcast); 424 | tcase_add_test(tc_core, test_is_ipv4_multicast); 425 | tcase_add_test(tc_core, test_is_ipv4_loopback); 426 | tcase_add_test(tc_core, test_is_ipv4_link_local); 427 | tcase_add_test(tc_core, test_is_ipv4_rfc1918); 428 | tcase_add_test(tc_core, test_is_ipv6); 429 | tcase_add_test(tc_core, test_is_ipv6_host); 430 | tcase_add_test(tc_core, test_is_ipv6_net); 431 | tcase_add_test(tc_core, test_is_ipv6_multicast); 432 | tcase_add_test(tc_core, test_is_ipv6_link_local); 433 | tcase_add_test(tc_core, test_is_valid_intf_address); 434 | tcase_add_test(tc_core, test_is_any_host); 435 | tcase_add_test(tc_core, test_is_any_net); 436 | 437 | suite_add_tcase(s, tc_core); 438 | 439 | return(s); 440 | } 441 | 442 | int main (void) 443 | { 444 | int number_failed; 445 | Suite *s = ipaddrcheck_suite(); 446 | SRunner *sr = srunner_create(s); 447 | srunner_run_all(sr, CK_NORMAL); 448 | number_failed = srunner_ntests_failed(sr); 449 | srunner_free (sr); 450 | return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; 451 | } 452 | 453 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /src/ipaddrcheck.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ipaddrcheck.c: an IPv4/IPv6 validator 3 | * 4 | * Copyright (C) 2013 Daniil Baturin 5 | * Copyright (C) 2018 VyOS maintainers and contributors 6 | * 7 | * This program is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License version 2 or later as 9 | * published by the Free Software Foundation. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | * 19 | */ 20 | 21 | #include 22 | #include "config.h" 23 | #include "ipaddrcheck_functions.h" 24 | 25 | /* Option codes */ 26 | #define IS_VALID 10 27 | #define IS_IPV4 20 28 | #define IS_IPV4_CIDR 30 29 | #define IS_IPV4_SINGLE 40 30 | #define IS_IPV4_HOST 50 31 | #define IS_IPV4_NET 60 32 | #define IS_IPV4_BROADCAST 70 33 | #define IS_IPV4_UNICAST 80 34 | #define IS_IPV4_MULTICAST 90 35 | #define IS_IPV4_RFC1918 100 36 | #define IS_IPV4_LOOPBACK 110 37 | #define IS_IPV4_LINKLOCAL 120 38 | #define IS_IPV6 130 39 | #define IS_IPV6_CIDR 140 40 | #define IS_IPV6_SINGLE 150 41 | #define IS_IPV6_HOST 160 42 | #define IS_IPV6_NET 170 43 | #define IS_IPV6_UNICAST 180 44 | #define IS_IPV6_MULTICAST 190 45 | #define IS_IPV6_LINKLOCAL 200 46 | #define IS_VALID_INTF_ADDR 220 47 | #define IS_ANY_CIDR 230 48 | #define IS_ANY_SINGLE 240 49 | #define ALLOW_LOOPBACK 250 50 | #define IS_ANY_HOST 260 51 | #define IS_ANY_NET 270 52 | #define NO_ACTION 500 53 | 54 | static const struct option options[] = 55 | { 56 | { "is-valid", no_argument, NULL, 'a' }, 57 | { "is-ipv4", no_argument, NULL, 'c' }, 58 | { "is-ipv4-cidr", no_argument, NULL, 'd' }, 59 | { "is-ipv4-single", no_argument, NULL, 'e' }, 60 | { "is-any-cidr", no_argument, NULL, 'A' }, 61 | { "is-any-single", no_argument, NULL, 'B' }, 62 | { "is-ipv4-host", no_argument, NULL, 'f' }, 63 | { "is-ipv4-net", no_argument, NULL, 'g' }, 64 | { "is-ipv4-broadcast", no_argument, NULL, 'h' }, 65 | { "is-ipv4-multicast", no_argument, NULL, 'i' }, 66 | { "is-ipv4-loopback", no_argument, NULL, 'j' }, 67 | { "is-ipv4-link-local", no_argument, NULL, 'k' }, 68 | { "is-ipv4-rfc1918", no_argument, NULL, 'l' }, 69 | { "is-ipv6", no_argument, NULL, 'm' }, 70 | { "is-ipv6-cidr", no_argument, NULL, 'n' }, 71 | { "is-ipv6-single", no_argument, NULL, 'o' }, 72 | { "is-ipv6-host", no_argument, NULL, 'p' }, 73 | { "is-ipv6-net", no_argument, NULL, 'r' }, 74 | { "is-ipv6-multicast", no_argument, NULL, 's' }, 75 | { "is-ipv6-link-local", no_argument, NULL, 't' }, 76 | { "is-valid-intf-address", no_argument, NULL, 'u' }, 77 | { "allow-loopback", no_argument, NULL, 'C' }, 78 | { "is-any-host", no_argument, NULL, 'D' }, 79 | { "is-any-net", no_argument, NULL, 'E' }, 80 | { "version", no_argument, NULL, 'z' }, 81 | { "help", no_argument, NULL, '?' }, 82 | { "verbose", no_argument, NULL, 'V' }, 83 | { NULL, no_argument, NULL, 0 } 84 | }; 85 | 86 | /* Auxiliary functions */ 87 | static void print_help(const char* program_name); 88 | static void print_version(void); 89 | 90 | int main(int argc, char* argv[]) 91 | { 92 | char *address_str = ""; /* IP address string obtained from arguments */ 93 | int action = 0; /* Action associated with given check option */ 94 | int* actions; /* Array of all given actions */ 95 | int action_count = 0; /* Actions array size */ 96 | 97 | int option_index = 0; /* Number of the current option for getopt call */ 98 | int optc; /* Option character for getopt call */ 99 | 100 | int allow_loopback = NO_LOOPBACK; /* Allow IPv4 loopback in --is-valid-intf-address */ 101 | 102 | int no_action = 0; /* Indicates the option modifies program behaviour 103 | but doesn't have its own action */ 104 | 105 | int verbose = 0; 106 | 107 | const char* program_name = argv[0]; /* Program name for use in messages */ 108 | 109 | 110 | /* Parse options, convert to action codes, store in array */ 111 | 112 | /* Try to allocate memory for the actions array, abort if fail */ 113 | actions = (int*)calloc(argc, sizeof(int)); 114 | if( errno == ENOMEM ) 115 | { 116 | fprintf(stderr, "Error: could not allocate memory!\n"); 117 | return(RESULT_INT_ERROR); 118 | } 119 | 120 | while( (optc = getopt_long(argc, argv, "acdefghijklmnoprstuzABCDEV?", options, &option_index)) != -1 ) 121 | { 122 | switch(optc) 123 | { 124 | case 'a': 125 | action = IS_VALID; 126 | break; 127 | case 'b': 128 | break; 129 | case 'c': 130 | action = IS_IPV4; 131 | break; 132 | case 'd': 133 | action = IS_IPV4_CIDR; 134 | break; 135 | case 'e': 136 | action = IS_IPV4_SINGLE; 137 | break; 138 | case 'f': 139 | action = IS_IPV4_HOST; 140 | break; 141 | case 'g': 142 | action = IS_IPV4_NET; 143 | break; 144 | case 'h': 145 | action = IS_IPV4_BROADCAST; 146 | break; 147 | case 'i': 148 | action = IS_IPV4_MULTICAST; 149 | break; 150 | case 'j': 151 | action = IS_IPV4_LOOPBACK; 152 | break; 153 | case 'k': 154 | action = IS_IPV4_LINKLOCAL; 155 | break; 156 | case 'l': 157 | action = IS_IPV4_RFC1918; 158 | break; 159 | case 'm': 160 | action = IS_IPV6; 161 | break; 162 | case 'n': 163 | action = IS_IPV6_CIDR; 164 | break; 165 | case 'o': 166 | action = IS_IPV6_SINGLE; 167 | break; 168 | case 'p': 169 | action = IS_IPV6_HOST; 170 | break; 171 | case 'r': 172 | action = IS_IPV6_NET; 173 | break; 174 | case 's': 175 | action = IS_IPV6_MULTICAST; 176 | break; 177 | case 't': 178 | action = IS_IPV6_LINKLOCAL; 179 | break; 180 | case 'u': 181 | action = IS_VALID_INTF_ADDR; 182 | break; 183 | case 'A': 184 | action = IS_ANY_CIDR; 185 | break; 186 | case 'B': 187 | action = IS_ANY_SINGLE; 188 | break; 189 | case 'C': 190 | allow_loopback = LOOPBACK_ALLOWED; 191 | no_action = NO_ACTION; 192 | break; 193 | case 'D': 194 | action = IS_ANY_HOST; 195 | break; 196 | case 'E': 197 | action = IS_ANY_NET; 198 | break; 199 | case 'V': 200 | verbose = 1; 201 | break; 202 | case '?': 203 | print_help(program_name); 204 | return(EXIT_SUCCESS); 205 | case 'z': 206 | print_version(); 207 | return(EXIT_SUCCESS); 208 | default: 209 | fprintf(stderr, "Error: invalid option\n"); 210 | print_help(program_name); 211 | return(RESULT_INT_ERROR); 212 | } 213 | 214 | if( no_action != NO_ACTION ) 215 | { 216 | action_count = optind-2; 217 | actions[action_count] = action; 218 | } 219 | else 220 | { 221 | no_action = 0; /* Reset no_action */ 222 | } 223 | } 224 | 225 | /* Exit if no option given */ 226 | if( optind < 2 ) 227 | { 228 | fprintf(stderr, "Error: at least one option expected!\n"); 229 | print_help(program_name); 230 | return(RESULT_INT_ERROR); 231 | } 232 | 233 | /* Get non-option arguments */ 234 | if( (argc - optind) == 1 ) 235 | { 236 | address_str = argv[optind]; 237 | } 238 | else 239 | { 240 | fprintf(stderr, "Error: wrong number of arguments, one argument required!\n"); 241 | print_help(program_name); 242 | return(RESULT_INT_ERROR); 243 | } 244 | 245 | CIDR *address; 246 | address = cidr_from_str(address_str); 247 | int result = RESULT_SUCCESS; 248 | 249 | /* Check if the address is valid and well-formatted at all, 250 | if not there is no point in going further */ 251 | if( !( (is_valid_address(address) == RESULT_SUCCESS) && 252 | ((is_any_cidr(address_str) == RESULT_SUCCESS) || (is_any_single(address_str) == RESULT_SUCCESS)) ) ) 253 | { 254 | if( verbose ) 255 | { 256 | printf("Malformed address %s\n", address_str); 257 | } 258 | return(EXIT_FAILURE); 259 | } 260 | 261 | /* FIXUP: libcidr allows more than one double semicolon, but the RFC does not! */ 262 | if( duplicate_double_semicolons(address_str) ) { 263 | if( verbose ) 264 | { 265 | printf("More than one \"::\" is not allowed in IPv6 addresses\n"); 266 | } 267 | return(EXIT_FAILURE); 268 | } 269 | /* no else needed, the rest is one big else */ 270 | 271 | while( (action_count >= 0) && (result == RESULT_SUCCESS) ) 272 | { 273 | switch(actions[action_count]) 274 | { 275 | case IS_VALID: 276 | result = is_valid_address(address); 277 | break; 278 | case IS_IPV4: 279 | result = is_ipv4(address); 280 | break; 281 | case IS_IPV4_CIDR: 282 | result = is_ipv4_cidr(address_str); 283 | break; 284 | case IS_IPV4_SINGLE: 285 | result = is_ipv4_single(address_str); 286 | break; 287 | case IS_IPV4_HOST: 288 | /* Host vs. network address check only makes sense 289 | if prefix length is given */ 290 | if( !(cidr_get_proto(address) == CIDR_IPV4) ) 291 | { 292 | if( verbose ) 293 | { 294 | printf("%s is not a valid IPv4 address\n", address_str); 295 | } 296 | result = RESULT_FAILURE; 297 | break; 298 | } 299 | 300 | if( !is_ipv4_cidr(address_str) ) 301 | { 302 | if( verbose ) 303 | { 304 | printf("Cannot check if %s is a valid host address: missing prefix length\n", address_str); 305 | } 306 | result = RESULT_FAILURE; 307 | } 308 | else 309 | { 310 | result = is_ipv4_host(address); 311 | if( (result == RESULT_FAILURE) && verbose ) 312 | { 313 | if( ((cidr_equals(address, cidr_addr_network(address)) >= 0) && 314 | (cidr_get_pflen(address) != 32)) ) 315 | { 316 | printf("%s is an IPv4 network address, not a host address\n", address_str); 317 | } 318 | } 319 | } 320 | break; 321 | case IS_IPV4_NET: 322 | /* Host vs. network address check only makes sense 323 | if prefix length is given */ 324 | if( !(cidr_get_proto(address) == CIDR_IPV4) ) { 325 | if( verbose ) 326 | { 327 | printf("%s is not a valid IPv4 address\n", address_str); 328 | } 329 | result = RESULT_FAILURE; 330 | break; 331 | } 332 | 333 | if( !is_ipv4_cidr(address_str) ) 334 | { 335 | if( verbose ) 336 | { 337 | printf("Cannot check if %s is a valid network address: missing prefix length\n", address_str); 338 | } 339 | result = RESULT_FAILURE; 340 | } 341 | else 342 | { 343 | result = is_ipv4_net(address); 344 | if( (result == RESULT_FAILURE) && verbose ) 345 | { 346 | if( ((cidr_equals(address, cidr_addr_network(address)) < 0) && 347 | (cidr_get_pflen(address) != 32)) ) 348 | { 349 | char* network_addr = cidr_to_str(cidr_addr_network(address), 0); 350 | printf("%s is an IPv4 host address, not a network address. Did you mean %s?\n", address_str, network_addr); 351 | } 352 | } 353 | } 354 | break; 355 | case IS_IPV4_BROADCAST: 356 | /* Broadcast address check only makes sense 357 | if prefix length is given */ 358 | if( !is_ipv4_cidr(address_str) ) 359 | { 360 | if( verbose ) 361 | { 362 | printf("Cannot check if %s is a broadcast address: missing prefix length\n", address_str); 363 | } 364 | result = RESULT_FAILURE; 365 | } 366 | else 367 | { 368 | result = is_ipv4_broadcast(address); 369 | } 370 | break; 371 | case IS_IPV4_MULTICAST: 372 | result = is_ipv4_multicast(address); 373 | break; 374 | case IS_IPV4_LOOPBACK: 375 | result = is_ipv4_loopback(address); 376 | break; 377 | case IS_IPV4_LINKLOCAL: 378 | result = is_ipv4_link_local(address); 379 | break; 380 | case IS_IPV4_RFC1918: 381 | result = is_ipv4_rfc1918(address); 382 | break; 383 | case IS_IPV6: 384 | result = is_ipv6(address); 385 | break; 386 | case IS_IPV6_CIDR: 387 | result = is_ipv6_cidr(address_str); 388 | break; 389 | case IS_IPV6_SINGLE: 390 | result = is_ipv6_single(address_str); 391 | break; 392 | case IS_IPV6_HOST: 393 | /* Host vs. network address check only makes sense 394 | if prefix length is given */ 395 | if( !(cidr_get_proto(address) == CIDR_IPV6) ) { 396 | if( verbose ) 397 | { 398 | printf("%s is not a valid IPv6 address\n", address_str); 399 | } 400 | result = RESULT_FAILURE; 401 | break; 402 | } 403 | 404 | if( !is_ipv6_cidr(address_str) ) 405 | { 406 | if( verbose ) 407 | { 408 | printf("Cannot check if %s is a valid IPv6 host address: missing prefix length\n", address_str); 409 | } 410 | result = RESULT_FAILURE; 411 | } 412 | else 413 | { 414 | result = is_ipv6_host(address); 415 | if( (result == RESULT_FAILURE) && verbose ) 416 | { 417 | if( ((cidr_equals(address, cidr_addr_network(address)) >= 0) && (cidr_get_pflen(address) != 128)) ) 418 | { 419 | printf("%s is an IPv6 network address, not a host address\n", address_str); 420 | } 421 | } 422 | } 423 | break; 424 | case IS_IPV6_NET: 425 | /* Host vs. network address check only makes sense 426 | if prefix length is given */ 427 | if( !(cidr_get_proto(address) == CIDR_IPV6) ) { 428 | if( verbose ) 429 | { 430 | printf("%s is not a valid IPv6 address\n", address_str); 431 | } 432 | result = RESULT_FAILURE; 433 | break; 434 | } 435 | 436 | if( !is_ipv6_cidr(address_str) ) 437 | { 438 | if( verbose ) 439 | { 440 | printf("Cannot check if %s is a valid IPv6 network address: missing prefix length\n", address_str); 441 | } 442 | result = RESULT_FAILURE; 443 | } 444 | else 445 | { 446 | result = is_ipv6_net(address); 447 | if( (result == RESULT_FAILURE) && verbose ) 448 | { 449 | if( ((cidr_equals(address, cidr_addr_network(address)) < 0) && (cidr_get_pflen(address) != 128)) ) { 450 | char* network_addr = cidr_to_str(cidr_addr_network(address), 0); 451 | printf("%s is an IPv6 host address, not a network address. Did you mean %s?\n", address_str, network_addr); 452 | } 453 | } 454 | } 455 | break; 456 | case IS_IPV6_MULTICAST: 457 | result = is_ipv6_multicast(address); 458 | break; 459 | case IS_IPV6_LINKLOCAL: 460 | result = is_ipv6_link_local(address); 461 | break; 462 | case IS_ANY_CIDR: 463 | result = is_any_cidr(address_str); 464 | break; 465 | case IS_ANY_SINGLE: 466 | result = is_any_single(address_str); 467 | break; 468 | case IS_VALID_INTF_ADDR: 469 | result = is_valid_intf_address(address, address_str, allow_loopback); 470 | break; 471 | case NO_ACTION: 472 | break; 473 | case IS_ANY_HOST: 474 | /* Host vs. network address check only makes sense 475 | if prefix length is given */ 476 | if( !is_any_cidr(address_str) ) 477 | { 478 | if( verbose ) 479 | { 480 | printf("Cannot check if %s is a valid host address: missing prefix length\n", address_str); 481 | } 482 | result = RESULT_FAILURE; 483 | } 484 | else 485 | { 486 | result = is_any_host(address); 487 | if( (result == RESULT_FAILURE) && verbose ) { 488 | if( ((cidr_equals(address, cidr_addr_network(address)) >= 0) && 489 | (cidr_get_pflen(address) != 32) && 490 | (cidr_get_pflen(address) != 128)) ) 491 | { 492 | printf("%s is a network address, not a host address\n", address_str); 493 | } 494 | } 495 | } 496 | break; 497 | case IS_ANY_NET: 498 | /* Host vs. network address check only makes sense 499 | if prefix length is given */ 500 | if( !is_any_cidr(address_str) ) 501 | { 502 | if( verbose ) 503 | { 504 | printf("Cannot check if %s is a valid network address: missing prefix length\n", address_str); 505 | } 506 | result = RESULT_FAILURE; 507 | } 508 | else 509 | { 510 | result = is_any_net(address); 511 | if( (result == RESULT_FAILURE) && verbose ) 512 | { 513 | if( ((cidr_equals(address, cidr_addr_network(address)) < 0) && 514 | (cidr_get_pflen(address) != 128) && 515 | (cidr_get_pflen(address) != 32)) ) 516 | { 517 | char* network_addr = cidr_to_str(cidr_addr_network(address), 0); 518 | printf("%s is a host address, not a network address. Did you mean %s?\n", address_str, network_addr); 519 | } 520 | } 521 | } 522 | break; 523 | default: 524 | break; 525 | } 526 | action_count--; 527 | } 528 | 529 | /* Clean up */ 530 | free(actions); 531 | cidr_free(address); 532 | 533 | if( result == RESULT_SUCCESS ) 534 | { 535 | return(EXIT_SUCCESS); 536 | } 537 | else 538 | { 539 | return(EXIT_FAILURE); 540 | } 541 | } 542 | 543 | /* 544 | * Print help, no other side effects 545 | */ 546 | void print_help(const char* program_name) 547 | { 548 | printf("Usage: %s [STRING]\n", program_name); 549 | printf("\ 550 | Options:\n\ 551 | --is-valid Check if STRING is a valid IPv4 or IPv6 address\n\ 552 | with or without prefix length\n\ 553 | --is-any-cidr Check if STRING is a valid IPv4 or IPv6 address\n\ 554 | with prefix length\n\ 555 | --is-any-single Check if STRING is a valid single IPv4 or IPv6 address\n\ 556 | --is-any-host Check if STRING is a valid IPv4 or IPv6 host address\n\ 557 | --is-any-net Check if STRING is a valid IPv4 or IPv6 network address\n\ 558 | --is-ipv4 Check if STRING is a valid IPv4 address with mask \n\ 559 | --is-ipv4-cidr Check if STRING is a valid CIDR-formatted address \n\ 560 | --is-ipv4-single Check if STRING is a valid single address\n\ 561 | (i.e. with no mask)\n\ 562 | --is-ipv4-host Check if STRING is a host address \n\ 563 | --is-ipv4-net Check if STRING is a network address \n\ 564 | --is-ipv4-broadcast Check if STRING is a broadcast address \n\ 565 | --is-ipv4-multicast Check if STRING is a multicast address \n\ 566 | --is-ipv4-loopback Check if STRING is a loopback address \n\ 567 | --is-ipv4-link-local Check if STRING is a link-local address \n\ 568 | --is-ipv4-rfc1918 Check if STRING is a private (RFC1918) address \n\ 569 | --is-ipv6 Check if STRING is a valid IPv6 address \n\ 570 | --is-ipv6-cidr Check if STRING is a CIDR-formatted IPv6 address \n\ 571 | --is-ipv6-single Check if STRING is an IPv6 address with no mask \n\ 572 | --is-ipv6-host Check if STRING is an IPv6 host address \n\ 573 | --is-ipv6-net Check if STRING is an IPv6 network address \n\ 574 | --is-ipv6-multicast Check if STRING is an IPv6 multicast address \n\ 575 | --is-ipv6-link-local Check if STRING is an IPv6 link-local address \n\ 576 | --is-valid-intf-address Check if STRING is an IPv4 or IPv6 address that \n\ 577 | can be assigned to a network interface \n\ 578 | --allow-loopback When used with --is-valid-intf-address,\n\ 579 | makes IPv4 loopback addresses pass the check\n\ 580 | --version Print version information and exit \n\ 581 | --help Print help message and exit\n\ 582 | \n\ 583 | Exit codes:\n\ 584 | 0 if check passed,\n\ 585 | 1 if check failed,\n\ 586 | 2 if a problem occured (wrong option, internal error etc.)\n"); 587 | } 588 | 589 | /* 590 | * Print version information, no other side effects 591 | */ 592 | void print_version(void) 593 | { 594 | printf("%s %s\n\n", PACKAGE_NAME, PACKAGE_VERSION); 595 | printf("Copyright (C) VyOS maintainers and contributors 2018.\n\ 596 | License GPLv2+: GNU GPL version 2 or later \n\ 597 | This is free software: you are free to change and redistribute it.\n\ 598 | There is NO WARRANTY, to the extent permitted by law.\n"); 599 | } 600 | -------------------------------------------------------------------------------- /COPYING-LGPL: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | [This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.] 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS 459 | 460 | How to Apply These Terms to Your New Libraries 461 | 462 | If you develop a new library, and you want it to be of the greatest 463 | possible use to the public, we recommend making it free software that 464 | everyone can redistribute and change. You can do so by permitting 465 | redistribution under these terms (or, alternatively, under the terms of the 466 | ordinary General Public License). 467 | 468 | To apply these terms, attach the following notices to the library. It is 469 | safest to attach them to the start of each source file to most effectively 470 | convey the exclusion of warranty; and each file should have at least the 471 | "copyright" line and a pointer to where the full notice is found. 472 | 473 | 474 | Copyright (C) 475 | 476 | This library is free software; you can redistribute it and/or 477 | modify it under the terms of the GNU Lesser General Public 478 | License as published by the Free Software Foundation; either 479 | version 2.1 of the License, or (at your option) any later version. 480 | 481 | This library is distributed in the hope that it will be useful, 482 | but WITHOUT ANY WARRANTY; without even the implied warranty of 483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 484 | Lesser General Public License for more details. 485 | 486 | You should have received a copy of the GNU Lesser General Public 487 | License along with this library; if not, write to the Free Software 488 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 489 | 490 | Also add information on how to contact you by electronic and paper mail. 491 | 492 | You should also get your employer (if you work as a programmer) or your 493 | school, if any, to sign a "copyright disclaimer" for the library, if 494 | necessary. Here is a sample; alter the names: 495 | 496 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 497 | library `Frob' (a library for tweaking knobs) written by James Random Hacker. 498 | 499 | , 1 April 1990 500 | Ty Coon, President of Vice 501 | 502 | That's all there is to it! 503 | --------------------------------------------------------------------------------