summaryrefslogtreecommitdiff
path: root/pjlib/src
diff options
context:
space:
mode:
Diffstat (limited to 'pjlib/src')
-rw-r--r--pjlib/src/pj/addr_resolv_sock.c70
-rw-r--r--pjlib/src/pj/errno.c1
-rw-r--r--pjlib/src/pj/sock_bsd.c176
-rw-r--r--pjlib/src/pj/sock_common.c5
-rw-r--r--pjlib/src/pjlib-test/sock.c48
5 files changed, 297 insertions, 3 deletions
diff --git a/pjlib/src/pj/addr_resolv_sock.c b/pjlib/src/pj/addr_resolv_sock.c
index 408eae12..8a90e89a 100644
--- a/pjlib/src/pj/addr_resolv_sock.c
+++ b/pjlib/src/pj/addr_resolv_sock.c
@@ -115,3 +115,73 @@ PJ_DEF(pj_status_t) pj_gethostip(pj_in_addr *addr)
}
+/* Resolve IPv4/IPv6 address */
+PJ_DEF(pj_status_t) pj_getaddrinfo(const pj_str_t *nodename, int af,
+ unsigned *count, pj_addrinfo ai[])
+{
+#if defined(PJ_SOCK_HAS_GETADDRINFO) && PJ_SOCK_HAS_GETADDRINFO!=0
+ char nodecopy[PJ_MAX_HOSTNAME];
+ struct addrinfo hint, *res;
+ unsigned i;
+ int rc;
+
+ PJ_ASSERT_RETURN(nodename && count && *count && ai, PJ_EINVAL);
+ PJ_ASSERT_RETURN(nodename->ptr && nodename->slen, PJ_EINVAL);
+ PJ_ASSERT_RETURN(af==PJ_AF_INET || af==PJ_AF_INET6, PJ_EINVAL);
+
+ /* Copy node name to null terminated string. */
+ if (nodename->slen >= PJ_MAX_HOSTNAME)
+ return PJ_ENAMETOOLONG;
+ pj_memcpy(nodecopy, nodename->ptr, nodename->slen);
+ nodecopy[nodename->slen] = '\0';
+
+ /* Call getaddrinfo() */
+ pj_bzero(&hint, sizeof(hint));
+ hint.ai_family = af;
+
+ rc = getaddrinfo(nodecopy, NULL, &hint, &res);
+ if (rc != 0)
+ return PJ_ERESOLVE;
+
+ /* Enumerate each item in the result */
+ for (i=0; i<*count && res; res=res->ai_next) {
+ int len;
+
+ /* Ignore unwanted address families */
+ if (af!=PJ_AF_UNSPEC && res->ai_family != af)
+ continue;
+
+ /* Ignore name that's too long */
+ len = pj_ansi_strlen(res->ai_canonname);
+ if (len >= PJ_MAX_HOSTNAME)
+ continue;
+
+ /* Store canonical name */
+ pj_ansi_strcpy(ai[i].ai_canonname, res->ai_canonname);
+
+ /* Store address */
+ PJ_ASSERT_ON_FAIL(res->ai_addrlen <= sizeof(pj_sockaddr), continue);
+ pj_memcpy(&ai[i].ai_addr, res->ai_addr, res->ai_addrlen);
+
+ /* Next slot */
+ ++i;
+ }
+
+ *count = i;
+
+ /* Done */
+ return PJ_SUCCESS;
+
+#else /* PJ_SOCK_HAS_GETADDRINFO */
+ /* IPv6 is not supported */
+ PJ_UNUSED_ARG(nodename);
+ PJ_UNUSED_ARG(af);
+ PJ_UNUSED_ARG(ai);
+
+ PJ_ASSERT_RETURN(count, PJ_EINVAL);
+ *count = 0;
+
+ return PJ_EIPV6NOTSUP;
+#endif /* PJ_SOCK_HAS_GETADDRINFO */
+}
+
diff --git a/pjlib/src/pj/errno.c b/pjlib/src/pj/errno.c
index 183ab509..6de0c922 100644
--- a/pjlib/src/pj/errno.c
+++ b/pjlib/src/pj/errno.c
@@ -71,6 +71,7 @@ static const struct
PJ_BUILD_ERR(PJ_ERESOLVE, "gethostbyname() has returned error"),
PJ_BUILD_ERR(PJ_ETOOSMALL, "Size is too short"),
PJ_BUILD_ERR(PJ_EIGNORED, "Ignored"),
+ PJ_BUILD_ERR(PJ_EIPV6NOTSUP, "IPv6 is not supported")
};
#endif /* PJ_HAS_ERROR_STRING */
diff --git a/pjlib/src/pj/sock_bsd.c b/pjlib/src/pj/sock_bsd.c
index 9cc4abba..eafc29e3 100644
--- a/pjlib/src/pj/sock_bsd.c
+++ b/pjlib/src/pj/sock_bsd.c
@@ -23,11 +23,13 @@
#include <pj/compat/socket.h>
#include <pj/addr_resolv.h>
#include <pj/errno.h>
+#include <pj/unicode.h>
/*
* Address families conversion.
* The values here are indexed based on pj_addr_family.
*/
+const pj_uint16_t PJ_AF_UNSPEC = AF_UNSPEC;
const pj_uint16_t PJ_AF_UNIX = AF_UNIX;
const pj_uint16_t PJ_AF_INET = AF_INET;
const pj_uint16_t PJ_AF_INET6 = AF_INET6;
@@ -184,7 +186,7 @@ PJ_DEF(char*) pj_inet_ntoa(pj_in_addr inaddr)
*/
PJ_DEF(int) pj_inet_aton(const pj_str_t *cp, struct pj_in_addr *inp)
{
- char tempaddr[16];
+ char tempaddr[PJ_INET_ADDRSTRLEN];
/* Initialize output with PJ_INADDR_NONE.
* Some apps relies on this instead of the return value
@@ -197,7 +199,7 @@ PJ_DEF(int) pj_inet_aton(const pj_str_t *cp, struct pj_in_addr *inp)
* (i.e. when called with hostname to check if it's an IP addr).
*/
PJ_ASSERT_RETURN(cp && cp->slen && inp, 0);
- if (cp->slen >= 16) {
+ if (cp->slen >= PJ_INET_ADDRSTRLEN) {
return 0;
}
@@ -213,6 +215,176 @@ PJ_DEF(int) pj_inet_aton(const pj_str_t *cp, struct pj_in_addr *inp)
}
/*
+ * Convert text to IPv4/IPv6 address.
+ */
+PJ_DEF(pj_status_t) pj_inet_pton(int af, const pj_str_t *src, void *dst)
+{
+ char tempaddr[PJ_INET6_ADDRSTRLEN];
+
+ PJ_ASSERT_RETURN(af==PJ_AF_INET || af==PJ_AF_INET6, PJ_EINVAL);
+ PJ_ASSERT_RETURN(src && src->slen && dst, PJ_EINVAL);
+
+ /* Initialize output with PJ_IN_ADDR_NONE for IPv4 (to be
+ * compatible with pj_inet_aton()
+ */
+ if (af==PJ_AF_INET) {
+ ((pj_in_addr*)dst)->s_addr = PJ_INADDR_NONE;
+ }
+
+ /* Caution:
+ * this function might be called with cp->slen >= 46
+ * (i.e. when called with hostname to check if it's an IP addr).
+ */
+ if (src->slen >= PJ_INET6_ADDRSTRLEN) {
+ return PJ_ENAMETOOLONG;
+ }
+
+ pj_memcpy(tempaddr, src->ptr, src->slen);
+ tempaddr[src->slen] = '\0';
+
+#if defined(PJ_SOCK_HAS_INET_PTON) && PJ_SOCK_HAS_INET_PTON != 0
+ /*
+ * Implementation using inet_pton()
+ */
+ if (inet_pton(af, tempaddr, dst) != 1) {
+ pj_status_t status = pj_get_netos_error();
+ if (status == PJ_SUCCESS)
+ status = PJ_EUNKNOWN;
+
+ return status;
+ }
+
+ return PJ_SUCCESS;
+
+#elif defined(PJ_WIN32) || defined(PJ_WIN32_WINCE)
+ /*
+ * Implementation on Windows, using WSAStringToAddress().
+ * Should also work on Unicode systems.
+ */
+ {
+ PJ_DECL_UNICODE_TEMP_BUF(wtempaddr,PJ_INET6_ADDRSTRLEN)
+ pj_sockaddr sock_addr;
+ int addr_len = sizeof(sock_addr);
+ int rc;
+
+ sock_addr.addr.sa_family = (pj_uint16_t)af;
+ rc = WSAStringToAddress(
+ PJ_STRING_TO_NATIVE(tempaddr,wtempaddr,sizeof(wtempaddr)),
+ af, NULL, (LPSOCKADDR)&sock_addr, &addr_len);
+ if (rc != 0) {
+ pj_status_t status = pj_get_netos_error();
+ if (status == PJ_SUCCESS)
+ status = PJ_EUNKNOWN;
+
+ return status;
+ }
+
+ if (sock_addr.addr.sa_family == PJ_AF_INET) {
+ pj_memcpy(dst, &sock_addr.ipv4.sin_addr, 4);
+ return PJ_SUCCESS;
+ } else if (sock_addr.addr.sa_family == PJ_AF_INET6) {
+ pj_memcpy(dst, &sock_addr.ipv6.sin6_addr, 16);
+ return PJ_SUCCESS;
+ } else {
+ pj_assert(!"Shouldn't happen");
+ return PJ_EBUG;
+ }
+ }
+#elif !defined(PJ_HAS_IPV6) || PJ_HAS_IPV6==0
+ /* IPv6 support is disabled, just return error without raising assertion */
+ return PJ_EIPV6NOTSUP;
+#else
+ pj_assert(!"Not supported");
+ return PJ_EIPV6NOTSUP;
+#endif
+}
+
+/*
+ * Convert IPv4/IPv6 address to text.
+ */
+PJ_DEF(pj_status_t) pj_inet_ntop(int af, const void *src,
+ char *dst, int size)
+
+{
+ PJ_ASSERT_RETURN(src && dst && size, PJ_EINVAL);
+ PJ_ASSERT_RETURN(af==PJ_AF_INET || af==PJ_AF_INET6, PJ_EINVAL);
+
+#if defined(PJ_SOCK_HAS_INET_NTOP) && PJ_SOCK_HAS_INET_NTOP != 0
+ /*
+ * Implementation using inet_ntop()
+ */
+ if (inet_ntop(af, src, dst, size) == NULL) {
+ pj_status_t status = pj_get_netos_error();
+ if (status == PJ_SUCCESS)
+ status = PJ_EUNKNOWN;
+
+ return status;
+ }
+
+ return PJ_SUCCESS;
+
+#elif defined(PJ_WIN32) || defined(PJ_WIN32_WINCE)
+ /*
+ * Implementation on Windows, using WSAAddressToString().
+ * Should also work on Unicode systems.
+ */
+ {
+ PJ_DECL_UNICODE_TEMP_BUF(wtempaddr,PJ_INET6_ADDRSTRLEN)
+ pj_sockaddr sock_addr;
+ DWORD addr_len, addr_str_len;
+ int rc;
+
+ pj_bzero(&sock_addr, sizeof(sock_addr));
+ sock_addr.addr.sa_family = (pj_uint16_t)af;
+ if (af == PJ_AF_INET) {
+ if (size < PJ_INET_ADDRSTRLEN)
+ return PJ_ETOOSMALL;
+ pj_memcpy(&sock_addr.ipv4.sin_addr, src, 4);
+ addr_len = sizeof(pj_sockaddr_in);
+ addr_str_len = PJ_INET_ADDRSTRLEN;
+ } else if (af == PJ_AF_INET6) {
+ if (size < PJ_INET6_ADDRSTRLEN)
+ return PJ_ETOOSMALL;
+ pj_memcpy(&sock_addr.ipv6.sin6_addr, src, 16);
+ addr_len = sizeof(pj_sockaddr_in6);
+ addr_str_len = PJ_INET6_ADDRSTRLEN;
+ } else {
+ pj_assert(!"Unsupported address family");
+ return PJ_EINVAL;
+ }
+
+#if PJ_NATIVE_STRING_IS_UNICODE
+ rc = WSAAddressToString((LPSOCKADDR)&sock_addr, addr_len,
+ NULL, wtempaddr, addr_str_len);
+ if (rc == 0) {
+ pj_unicode_to_ansi(wtempaddr, wcslen(wtempaddr), dst, size);
+ }
+#else
+ rc = WSAAddressToString((LPSOCKADDR)&sock_addr, addr_len,
+ NULL, dst, &addr_str_len);
+#endif
+
+ if (rc != 0) {
+ pj_status_t status = pj_get_netos_error();
+ if (status == PJ_SUCCESS)
+ status = PJ_EUNKNOWN;
+
+ return status;
+ }
+
+ return PJ_SUCCESS;
+ }
+
+#elif !defined(PJ_HAS_IPV6) || PJ_HAS_IPV6==0
+ /* IPv6 support is disabled, just return error without raising assertion */
+ return PJ_EIPV6NOTSUP;
+#else
+ pj_assert(!"Not supported");
+ return PJ_EIPV6NOTSUP;
+#endif
+}
+
+/*
* Convert address string with numbers and dots to binary IP address.
*/
PJ_DEF(pj_in_addr) pj_inet_addr(const pj_str_t *cp)
diff --git a/pjlib/src/pj/sock_common.c b/pjlib/src/pj/sock_common.c
index 4431f593..5b629fc2 100644
--- a/pjlib/src/pj/sock_common.c
+++ b/pjlib/src/pj/sock_common.c
@@ -18,6 +18,11 @@
*/
#include <pj/sock.h>
+PJ_DEF(pj_uint16_t) pj_AF_UNSPEC(void)
+{
+ return PJ_AF_UNSPEC;
+}
+
PJ_DEF(pj_uint16_t) pj_AF_UNIX(void)
{
return PJ_AF_UNIX;
diff --git a/pjlib/src/pjlib-test/sock.c b/pjlib/src/pjlib-test/sock.c
index 5e583e42..b36374b0 100644
--- a/pjlib/src/pjlib-test/sock.c
+++ b/pjlib/src/pjlib-test/sock.c
@@ -40,6 +40,8 @@
* The APIs tested in this test:
* - pj_inet_aton()
* - pj_inet_ntoa()
+ * - pj_inet_pton() (only if IPv6 is enabled)
+ * - pj_inet_ntop() (only if IPv6 is enabled)
* - pj_gethostname()
* - pj_sock_socket()
* - pj_sock_close()
@@ -100,7 +102,51 @@ static int format_test(void)
return -20;
if (pj_strcmp2(&s, (char*)p) != 0)
- return -30;
+ return -22;
+
+#if defined(PJ_HAS_IPV6) && PJ_HAS_IPV6!=0
+ /* pj_inet_pton() */
+ /* pj_inet_ntop() */
+ {
+ const pj_str_t s_ipv4 = pj_str("127.0.0.1");
+ const pj_str_t s_ipv6 = pj_str("fe80::2ff:83ff:fe7c:8b42");
+ char buf_ipv4[PJ_INET_ADDRSTRLEN];
+ char buf_ipv6[PJ_INET6_ADDRSTRLEN];
+ pj_in_addr ipv4;
+ pj_in6_addr ipv6;
+
+ if (pj_inet_pton(pj_AF_INET(), &s_ipv4, &ipv4) != PJ_SUCCESS)
+ return -24;
+
+ p = (unsigned char*)&ipv4;
+ if (p[0]!=A[0] || p[1]!=A[1] || p[2]!=A[2] || p[3]!=A[3]) {
+ return -25;
+ }
+
+ if (pj_inet_pton(pj_AF_INET6(), &s_ipv6, &ipv6) != PJ_SUCCESS)
+ return -26;
+
+ p = (unsigned char*)&ipv6;
+ if (p[0] != 0xfe || p[1] != 0x80 || p[2] != 0 || p[3] != 0 ||
+ p[4] != 0 || p[5] != 0 || p[6] != 0 || p[7] != 0 ||
+ p[8] != 0x02 || p[9] != 0xff || p[10] != 0x83 || p[11] != 0xff ||
+ p[12]!=0xfe || p[13]!=0x7c || p[14] != 0x8b || p[15]!=0x42)
+ {
+ return -27;
+ }
+
+ if (pj_inet_ntop(pj_AF_INET(), &ipv4, buf_ipv4, sizeof(buf_ipv4)) != PJ_SUCCESS)
+ return -28;
+ if (pj_stricmp2(&s_ipv4, buf_ipv4) != 0)
+ return -29;
+
+ if (pj_inet_ntop(pj_AF_INET6(), &ipv6, buf_ipv6, sizeof(buf_ipv6)) != PJ_SUCCESS)
+ return -30;
+ if (pj_stricmp2(&s_ipv6, buf_ipv6) != 0)
+ return -31;
+ }
+
+#endif /* PJ_HAS_IPV6 */
/* Test that pj_sockaddr_in_init() initialize the whole structure,
* including sin_zero.