diff --git a/common/fko_util.c b/common/fko_util.c index f3820279..125e89c4 100644 --- a/common/fko_util.c +++ b/common/fko_util.c @@ -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 diff --git a/common/fko_util.h b/common/fko_util.h index ea8ec36e..c5ad8f56 100644 --- a/common/fko_util.h +++ b/common/fko_util.h @@ -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);