diff options
author | Patrik Flykt <patrik.flykt@linux.intel.com> | 2012-06-04 13:45:02 +0300 |
---|---|---|
committer | Patrik Flykt <patrik.flykt@linux.intel.com> | 2012-06-14 15:05:55 +0300 |
commit | b93f2462a3dc6cc00810ed28b87a5c05516c0921 (patch) | |
tree | 4c79fa22d5976691536906976272f6e948162d65 | |
parent | 23da213b31cac162597b73438885ca756d11bafd (diff) | |
download | connman-b93f2462a3dc6cc00810ed28b87a5c05516c0921.tar.gz connman-b93f2462a3dc6cc00810ed28b87a5c05516c0921.tar.bz2 connman-b93f2462a3dc6cc00810ed28b87a5c05516c0921.zip |
inet: Create helper function for IP address checking
Create a helper function that checks if a given hostname is an IPv4
or IPv6 address. If it is, AF_INET or AF_INET6 is returned. On
error the negative error value from getaddrinfo is returned.
-rw-r--r-- | include/inet.h | 1 | ||||
-rw-r--r-- | src/inet.c | 20 |
2 files changed, 21 insertions, 0 deletions
diff --git a/include/inet.h b/include/inet.h index aa675737..8b9b390a 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); +int connman_inet_check_ipaddress(const char *host); connman_bool_t connman_inet_check_hostname(const char *ptr, size_t len); #ifdef __cplusplus @@ -2174,6 +2174,26 @@ int __connman_inet_rtnl_addattr32(struct nlmsghdr *n, size_t maxlen, int type, return 0; } +int connman_inet_check_ipaddress(const char *host) +{ + struct addrinfo hints; + struct addrinfo *addr; + int result; + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_flags = AI_NUMERICHOST; + addr = NULL; + + result = getaddrinfo(host, NULL, &hints, &addr); + if (result == 0) + result = addr->ai_family; + else + result = -result; + freeaddrinfo(addr); + + return result; +} + /* Check routine modified from ics-dhcp 4.2.3-P2 */ connman_bool_t connman_inet_check_hostname(const char *ptr, size_t len) { |