inet: Create helper function for IP address checking
authorPatrik Flykt <patrik.flykt@linux.intel.com>
Mon, 4 Jun 2012 10:45:02 +0000 (13:45 +0300)
committerPatrik Flykt <patrik.flykt@linux.intel.com>
Thu, 14 Jun 2012 12:05:55 +0000 (15:05 +0300)
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.

include/inet.h
src/inet.c

index aa67573..8b9b390 100644 (file)
@@ -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
index effa68c..17d58d5 100644 (file)
@@ -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)
 {