summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Withnall <philip.withnall@collabora.co.uk>2014-07-04 11:50:29 +0100
committerOlivier Crête <olivier.crete@collabora.com>2014-07-07 19:39:22 -0400
commit839f9e3f10b992749f29fc548f7e67b5552b6399 (patch)
treeb449bc5a5e39a7526747732444d47f843b245a02
parent9a622300e88f7c439773fc385c98051796ac67cf (diff)
downloadlibnice-839f9e3f10b992749f29fc548f7e67b5552b6399.tar.gz
agent: Use getaddrinfo() to parse addresses from strings
Rather than using inet_pton(), which doesn’t support IPv6 link-local scope IDs, use getaddrinfo(). This is supported on all platforms (POSIX.1-2001, and Windows winsock).
-rw-r--r--agent/address.c31
1 files changed, 19 insertions, 12 deletions
diff --git a/agent/address.c b/agent/address.c
index 0cfe5d1..afb890c 100644
--- a/agent/address.c
+++ b/agent/address.c
@@ -44,6 +44,10 @@
#include <string.h>
+#ifndef G_OS_WIN32
+#include <netdb.h>
+#endif
+
#include "address.h"
#ifdef G_OS_WIN32
@@ -197,18 +201,21 @@ nice_address_get_port (const NiceAddress *addr)
NICEAPI_EXPORT gboolean
nice_address_set_from_string (NiceAddress *addr, const gchar *str)
{
- union
- {
- struct in_addr ipv4;
- struct in6_addr ipv6;
- } a;
-
- if (inet_pton (AF_INET, str, &a.ipv4) > 0)
- nice_address_set_ipv4 (addr, ntohl (a.ipv4.s_addr));
- else if (inet_pton (AF_INET6, str, &a.ipv6) > 0)
- nice_address_set_ipv6 (addr, a.ipv6.s6_addr);
- else
- return FALSE; /* Invalid address */
+ struct addrinfo hints;
+ struct addrinfo *res;
+
+ memset (&hints, 0, sizeof (hints));
+
+ /* AI_NUMERICHOST prevents getaddrinfo() from doing DNS resolution. */
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_flags = AI_NUMERICHOST;
+
+ if (getaddrinfo (str, NULL, &hints, &res) != 0)
+ return FALSE; /* invalid address */
+
+ nice_address_set_from_sockaddr (addr, res->ai_addr);
+
+ freeaddrinfo (res);
return TRUE;
}