Add is_valid_hostname()

This commit is contained in:
Jonathan Bennett 2016-05-07 21:56:09 -05:00
parent 5f80a1ae33
commit 65e0c4abd6
2 changed files with 63 additions and 0 deletions

View File

@ -175,6 +175,68 @@ is_valid_ipv4_addr(const char * const ip_str, const int len)
return(res);
}
/* Validate a hostname
*/
int
is_valid_hostname(const char * const hostname_str, const int len)
{
int label_size = 0, total_size = 0;
const char *ndx = hostname_str;
if (hostname_str == NULL)
return 0;
if (len > 254)
return 0;
while(total_size < len)
{
if (*ndx == '\0')
return 0;
if (label_size == 0) //More restrictions on first character of a label
{
if (!isalnum(*ndx))
return 0;
}
else if (!(isalnum(*ndx) | (*ndx == '.') | (*ndx == '-')))
return 0;
if (*ndx == '.')
{
if (label_size > 63)
return 0;
if (!isalnum(*(ndx-1))) //checks that previous character was not a . or -
return 0;
label_size = 0;
}
else
{
label_size++;
}
total_size++;
ndx++; //move to next character
}
/* At this point, we're pointing at the null. Decrement ndx for simplicity
*/
ndx--;
if (*ndx == '-')
return 0;
if (*ndx == '.')
total_size--;
if (label_size > 63)
return 0;
/* By now we've bailed if invalid
*/
return 1;
}
/* Convert a digest_type string to its integer value.
*/
short

View File

@ -41,6 +41,7 @@
int is_valid_encoded_msg_len(const int len);
int is_valid_pt_msg_len(const int len);
int is_valid_ipv4_addr(const char * const ip_str, const int len);
int is_valid_hostname(const char * const hostname_str, const int len);
int is_base64(const unsigned char * const buf, const unsigned short int len);
void hex_dump(const unsigned char *data, const int size);
int enc_mode_strtoint(const char *enc_mode_str);