diff options
author | Jukka Rissanen <jukka.rissanen@linux.intel.com> | 2012-05-03 16:15:07 +0300 |
---|---|---|
committer | Patrik Flykt <patrik.flykt@linux.intel.com> | 2012-05-04 12:50:49 +0300 |
commit | 26ace5c59f790bce0f1988b88874c6f2c480fd5a (patch) | |
tree | af54b1a2d1924e6b99c6b84dabb2b6df5bda6dec | |
parent | c1b968984212b46bea1330f5ae029507b9bfded9 (diff) | |
download | connman-26ace5c59f790bce0f1988b88874c6f2c480fd5a.tar.gz connman-26ace5c59f790bce0f1988b88874c6f2c480fd5a.tar.bz2 connman-26ace5c59f790bce0f1988b88874c6f2c480fd5a.zip |
inet: Add function that checks if the hostname is valid
-rw-r--r-- | include/inet.h | 1 | ||||
-rw-r--r-- | src/inet.c | 48 |
2 files changed, 49 insertions, 0 deletions
diff --git a/include/inet.h b/include/inet.h index 2048de1a..aa675737 100644 --- a/include/inet.h +++ b/include/inet.h @@ -80,6 +80,7 @@ int connman_inet_setup_tunnel(char *tunnel, int mtu); int connman_inet_create_tunnel(char **iface); int connman_inet_get_dest_addr(int index, char **dest); int connman_inet_ipv6_get_dest_addr(int index, char **dest); +connman_bool_t connman_inet_check_hostname(const char *ptr, size_t len); #ifdef __cplusplus } @@ -44,6 +44,7 @@ #include <netinet/icmp6.h> #include <fcntl.h> #include <linux/if_tun.h> +#include <ctype.h> #include "connman.h" @@ -2172,3 +2173,50 @@ int __connman_inet_rtnl_addattr32(struct nlmsghdr *n, size_t maxlen, int type, return 0; } + +/* Check routine modified from ics-dhcp 4.2.3-P2 */ +connman_bool_t connman_inet_check_hostname(const char *ptr, size_t len) +{ + const char *p; + + /* + * Not empty or complete length not over 255 characters. + */ + if ((len == 0) || (len > 256)) + return FALSE; + + /* + * Consists of [[:alnum:]-]+ labels separated by [.] + * a [_] is against RFC but seems to be "widely used" + */ + for (p = ptr; (*p != 0) && (len-- > 0); p++) { + + if ((*p == '-') || (*p == '_')) { + /* + * Not allowed at begin or end of a label. + */ + if (((p - ptr) == 0) || (len == 0) || (p[1] == '.')) + return FALSE; + + } else if (*p == '.') { + /* + * Each label has to be 1-63 characters; + * we allow [.] at the end ('foo.bar.') + */ + size_t d = p - ptr; + + if ((d <= 0) || (d >= 64)) + return FALSE; + + ptr = p + 1; /* Jump to the next label */ + + } else if (isalnum((unsigned char)*p) == 0) { + /* + * Also numbers at the begin are fine + */ + return FALSE; + } + } + + return TRUE; +} |