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 #include +#include +#include +#include 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 *);