blob: 3624ec442b44ee8b4ec1cb72f749a78c8c703e2f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
/*
* iptest.h: macros and functions for iptest IPv4/IPv6 validator
*
* Maintainer: Daniil Baturin <daniil at baturin dot org>
*
* Copyright (C) 2013 SO3Group
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <libcidr.h>
#define INVALID_PROTO -1
/* Option codes */
#define IS_VALID 10
#define IS_IPV4 20
#define IS_IPV4_HOST 30
#define IS_IPV4_NET 40
#define IS_IPV4_BROADCAST 50
#define IS_IPV4_UNICAST 60
#define IS_IPV4_MULTICAST 70
#define IS_IPV4_RFC1918 80
#define IS_IPV4_LOOPBACK 85
#define IS_IPV6 90
#define IS_IPV6_HOST 100
#define IS_IPV6_NET 110
#define IS_IPV6_UNICAST 120
#define IS_IPV6_MULTICAST 130
#define IS_IPV6_LINKLOCAL 140
/* Does it look like a valid address of any protocol? */
int is_valid_address(CIDR *address)
{
int result;
if( cidr_get_proto(address) != INVALID_PROTO )
{
result = EXIT_SUCCESS;
}
else
{
result = EXIT_FAILURE;
}
return(result);
}
/* Is it a correct IPv4 host or subnet address
with or without net mask */
int is_ipv4(CIDR *address)
{
int result;
if( cidr_get_proto(address) == CIDR_IPV4 )
{
result = EXIT_SUCCESS;
}
else
{
result = EXIT_FAILURE;
}
return(result);
}
/* Is it a correct IPv4 host (i.e. not network) address? */
int is_ipv4_host(CIDR *address)
{
int result;
if( (cidr_get_proto(address) == CIDR_IPV4) &&
cidr_equals(address, cidr_addr_network(address)) )
{
result = EXIT_SUCCESS;
}
else
{
result = EXIT_FAILURE;
}
return(result);
}
/* Is it a correct IPv4 network address? */
int is_ipv4_net(CIDR *address)
{
/* TODO: Don't try to validate is mask is not present */
int result;
if( (cidr_get_proto(address) == CIDR_IPV4) &&
(cidr_equals(address, cidr_addr_network(address)) == 0) )
{
result = EXIT_SUCCESS;
}
else
{
result = EXIT_FAILURE;
}
return(result);
}
|