Implement is_valid_ip_addr() with getaddrinfo()
This commit is contained in:
parent
b3494dcfc1
commit
aea56f54c4
@ -124,52 +124,22 @@ is_valid_encoded_msg_len(const int len)
|
|||||||
int
|
int
|
||||||
is_valid_ip_addr(const char * const ip_str, const int len, const int family)
|
is_valid_ip_addr(const char * const ip_str, const int len, const int family)
|
||||||
{
|
{
|
||||||
const char *ndx = ip_str;
|
struct addrinfo * result, hints;
|
||||||
char tmp_ip_str[MAX_IPV4_STR_LEN + 1] = {0};
|
int error;
|
||||||
int dot_ctr = 0, char_ctr = 0;
|
char * p;
|
||||||
int res = 1;
|
|
||||||
#if HAVE_SYS_SOCKET_H
|
|
||||||
struct in_addr in;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if(ip_str == NULL)
|
if((p = strndup(ip_str, len)) == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
|
memset(&hints, 0, sizeof(hints));
|
||||||
if((len > MAX_IPV4_STR_LEN) || (len < MIN_IPV4_STR_LEN))
|
hints.ai_family = family;
|
||||||
return 0;
|
hints.ai_flags = AI_NUMERICHOST;
|
||||||
|
error = getaddrinfo(p, NULL, &hints, &result);
|
||||||
while(char_ctr < len)
|
free(p);
|
||||||
{
|
if (error) {
|
||||||
/* If we've hit a null within the given length, then not valid regardless */
|
return 0;
|
||||||
if(*ndx == '\0')
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
char_ctr++;
|
|
||||||
|
|
||||||
if(*ndx == '.')
|
|
||||||
dot_ctr++;
|
|
||||||
else if(isdigit((int)(unsigned char)*ndx) == 0)
|
|
||||||
{
|
|
||||||
res = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
ndx++;
|
|
||||||
}
|
}
|
||||||
|
freeaddrinfo(result);
|
||||||
if((res == 1) && (dot_ctr != 3))
|
return 1;
|
||||||
res = 0;
|
|
||||||
|
|
||||||
#if HAVE_SYS_SOCKET_H
|
|
||||||
/* Stronger IP validation now that we have a candidate that looks
|
|
||||||
* close enough
|
|
||||||
*/
|
|
||||||
if(res == 1) {
|
|
||||||
strncpy(tmp_ip_str, ip_str, len);
|
|
||||||
if (inet_aton(tmp_ip_str, &in) == 0)
|
|
||||||
res = 0;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
return(res);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Validate a hostname
|
/* Validate a hostname
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user