diff --git a/whois.c b/whois.c index c4afc09..60d2971 100644 --- a/whois.c +++ b/whois.c @@ -1049,6 +1049,32 @@ int domcmp(const char *dom, const char *tld) return 0; } +/* check if dom is a subdomain of tld */ +int in_domain(const char *dom, const char *tld) +{ + size_t dom_len, tld_len; + const char *p = NULL; + + if ((dom_len = strlen(dom)) == 0) + return 0; + + if ((tld_len = strlen(tld)) == 0) + return 0; + + /* dom cannot be shorter than what we are looking for */ + /* -1 to ignore dom containing just a dot and tld */ + if (tld_len >= dom_len - 1) + return 0; + + p = dom + dom_len - tld_len; + + /* fail if the character before tld is not a dot */ + if (*(p - 1) != '.') + return 0; + + return strcaseeq(p, tld); +} + const char *is_new_gtld(const char *s) { int i; diff --git a/whois.h b/whois.h index 3fd49bb..a4b8a3e 100644 --- a/whois.h +++ b/whois.h @@ -29,6 +29,7 @@ unsigned long myinet_aton(const char *); unsigned long asn32_to_long(const char *); int isasciidigit(const char); int domcmp(const char *, const char *); +int in_domain(const char *, const char *); const char *is_new_gtld(const char *); int domfind(const char *, const char *[]); char *normalize_domain(const char *);