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
118
119
120
121
122
123
124
125
126
|
Index: utils.c
===================================================================
RCS file: /cvsroot/nagiosplug/nagiosplug/plugins/utils.c,v
retrieving revision 1.13
diff -u -r1.13 utils.c
--- utils.c 18 Nov 2002 07:22:28 -0000 1.13
+++ utils.c 11 Dec 2002 02:39:17 -0000
@@ -16,6 +16,9 @@
#include "version.h"
#include <stdarg.h>
#include <limits.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <netdb.h>
extern int timeout_interval;
@@ -27,7 +30,9 @@
RETSIGTYPE timeout_alarm_handler (int);
int is_host (char *);
-int is_dotted_quad (char *);
+int resolve_host_or_addr (char *, int);
+int is_ipv4_addr (char *);
+int is_ipv6_addr (char *);
int is_hostname (char *);
int is_integer (char *);
@@ -154,53 +159,49 @@
int
is_host (char *address)
{
- if (is_dotted_quad (address) || is_hostname (address))
+ if (is_ipv4_addr (address) || is_ipv6_addr (address) ||
+ is_hostname (address))
return (TRUE);
+
return (FALSE);
}
int
-is_dotted_quad (char *address)
+resolve_host_or_addr (char *address, int family)
{
- int o1, o2, o3, o4;
- char c[1];
+ struct addrinfo hints;
+ struct addrinfo *res;
+ int retval;
- if (sscanf (address, "%d.%d.%d.%d%c", &o1, &o2, &o3, &o4, c) != 4)
- return FALSE;
- else if (o1 > 255 || o2 > 255 || o3 > 255 || o4 > 255)
- return FALSE;
- else if (o1 < 0 || o2 < 0 || o3 < 0 || o4 < 0)
+ memset (&hints, 0, sizeof (hints));
+ hints.ai_family = family;
+ retval = getaddrinfo (address, NULL, &hints, &res);
+
+ if (retval != 0)
return FALSE;
else
+ {
+ freeaddrinfo (res);
return TRUE;
+ }
}
-/* from RFC-1035
- *
- * The labels must follow the rules for ARPANET host names. They must
- * start with a letter, end with a letter or digit, and have as interior
- * characters only letters, digits, and hyphen. There are also some
- * restrictions on the length. Labels must be 63 characters or less. */
+int
+is_ipv4_addr (char *address)
+{
+ return resolve_host_or_addr (address, AF_INET);
+}
+
+int
+is_ipv6_addr (char *address)
+{
+ return resolve_host_or_addr (address, AF_INET6);
+}
int
is_hostname (char *s1)
{
- if (strlen (s1) > 63)
- return FALSE;
- if (strcspn
- (s1,
- "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUWVXYZ0123456789-.") !=
- 0) return FALSE;
- if (strspn (s1, "0123456789-.") == 1)
- return FALSE;
- while ((s1 = index (s1, '.'))) {
- s1++;
- if (strspn (s1, "0123456789-.") == 1) {
- printf ("%s\n", s1);
- return FALSE;
- }
- }
- return TRUE;
+ return resolve_host_or_addr (s1, AF_UNSPEC);
}
int
Index: utils.h.in
===================================================================
RCS file: /cvsroot/nagiosplug/nagiosplug/plugins/utils.h.in,v
retrieving revision 1.7
diff -u -r1.7 utils.h.in
--- utils.h.in 9 Nov 2002 03:39:35 -0000 1.7
+++ utils.h.in 11 Dec 2002 02:39:17 -0000
@@ -28,7 +28,8 @@
/* Test input types */
int is_host (char *);
-int is_dotted_quad (char *);
+int is_ipv4_addr (char *);
+int is_ipv6_addr (char *);
int is_hostname (char *);
int is_integer (char *);
|