summaryrefslogtreecommitdiff
path: root/pjlib
diff options
context:
space:
mode:
authorBenny Prijono <bennylp@teluu.com>2007-11-18 14:53:47 +0000
committerBenny Prijono <bennylp@teluu.com>2007-11-18 14:53:47 +0000
commitfe61446fbe7fecfa0946812215e42396da6e0b9b (patch)
treec7e35c1578c50d7c8f97f4b27ff64787cd33932a /pjlib
parent54f99f73d8f744c4558645906c99cbab480f5b8c (diff)
Ticket #415: implement IPv6 support in PJLIB
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@1585 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjlib')
-rw-r--r--pjlib/include/pj/addr_resolv.h31
-rw-r--r--pjlib/include/pj/compat/os_auto.h.in3
-rw-r--r--pjlib/include/pj/compat/socket.h39
-rw-r--r--pjlib/include/pj/config.h11
-rw-r--r--pjlib/include/pj/errno.h5
-rw-r--r--pjlib/include/pj/sock.h85
-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
11 files changed, 455 insertions, 19 deletions
diff --git a/pjlib/include/pj/addr_resolv.h b/pjlib/include/pj/addr_resolv.h
index 39645cb6..e69abcf8 100644
--- a/pjlib/include/pj/addr_resolv.h
+++ b/pjlib/include/pj/addr_resolv.h
@@ -73,6 +73,16 @@ typedef struct pj_hostent
/** Shortcut to h_addr_list[0] */
#define h_addr h_addr_list[0]
+/**
+ * This structure describes address information pj_getaddrinfo().
+ */
+typedef struct pj_addrinfo
+{
+ char ai_canonname[PJ_MAX_HOSTNAME]; /**< Canonical name for host*/
+ pj_sockaddr ai_addr; /**< Binary address. */
+} pj_addrinfo;
+
+
/**
* This function fills the structure of type pj_hostent for a given host name.
*
@@ -95,6 +105,27 @@ PJ_DECL(pj_status_t) pj_gethostbyname(const pj_str_t *name, pj_hostent *he);
PJ_DECL(pj_status_t) pj_gethostip(pj_in_addr *ip_addr);
+/**
+ * This function translates the name of a service location (for example,
+ * a host name) and returns a set of addresses and associated information
+ * to be used in creating a socket with which to address the specified
+ * service.
+ *
+ * @param name Descriptive name or an address string, such as host
+ * name.
+ * @param af The desired address family to query.
+ * @param count On input, it specifies the number of elements in
+ * \a ai array. On output, this will be set with the
+ * number of address informations found for the
+ * specified name.
+ *
+ * @return PJ_SUCCESS on success, or the appropriate error code.
+ */
+PJ_DECL(pj_status_t) pj_getaddrinfo(const pj_str_t *nodename, int af,
+ unsigned *count, pj_addrinfo ai[]);
+
+
+
/** @} */
PJ_END_DECL
diff --git a/pjlib/include/pj/compat/os_auto.h.in b/pjlib/include/pj/compat/os_auto.h.in
index 58bf8690..336be7d5 100644
--- a/pjlib/include/pj/compat/os_auto.h.in
+++ b/pjlib/include/pj/compat/os_auto.h.in
@@ -72,6 +72,9 @@
#undef PJ_HAS_WINSOCK2_H
#undef PJ_SOCK_HAS_INET_ATON
+#undef PJ_SOCK_HAS_INET_PTON
+#undef PJ_SOCK_HAS_INET_NTOP
+#undef PJ_SOCK_HAS_GETADDRINFO
/* Set 1 if native sockaddr_in has sin_len member.
* Default: 0
diff --git a/pjlib/include/pj/compat/socket.h b/pjlib/include/pj/compat/socket.h
index 7cc0d654..f44a623c 100644
--- a/pjlib/include/pj/compat/socket.h
+++ b/pjlib/include/pj/compat/socket.h
@@ -32,6 +32,45 @@
# include <winsock.h>
#endif
+/*
+ * IPv6 for Visual Studio's
+ *
+ * = Visual Studio 6 =
+ *
+ * Visual Studio 6 does not ship with IPv6 support, so you MUST
+ * download and install IPv6 Tehnology Preview (IPv6Kit) from:
+ * http://msdn.microsoft.com/downloads/sdks/platform/tpipv6/ReadMe.asp
+ * Then put IPv6Kit\inc in your Visual Studio include path.
+ *
+ * In addition, by default IPv6Kit does not want to install on
+ * Windows 2000 SP4. Please see:
+ * http://msdn.microsoft.com/downloads/sdks/platform/tpipv6/faq.asp
+ * on how to install IPv6Kit on Win2K SP4.
+ *
+ *
+ * = Visual Studio 2003, 2005 (including Express) =
+ *
+ * These VS uses Microsoft Platform SDK for Windows Server 2003 SP1, and
+ * it has built-in IPv6 support.
+ */
+#if defined(_MSC_VER) && defined(PJ_HAS_IPV6) && PJ_HAS_IPV6!=0
+# ifndef s_addr
+# define s_addr S_un.S_addr
+# endif
+
+# include <ws2tcpip.h>
+
+# ifndef IPPROTO_IPV6
+ /* Need to download and install IPv6Kit for this platform.
+ * Please see the comments above about Visual Studio 6.
+ */
+# include <tpipv6.h>
+# endif
+
+# undef s_addr
+#endif /* _MSC_VER */
+
+
#if defined(PJ_HAS_SYS_TYPES_H) && PJ_HAS_SYS_TYPES_H != 0
# include <sys/types.h>
#endif
diff --git a/pjlib/include/pj/config.h b/pjlib/include/pj/config.h
index 2f05c0f7..b1287101 100644
--- a/pjlib/include/pj/config.h
+++ b/pjlib/include/pj/config.h
@@ -449,6 +449,17 @@
#endif
/**
+ * \def PJ_HAS_IPV6
+ * Support IPv6 in the library. If this support is disabled, some IPv6
+ * related functions will return PJ_EIPV6NOTSUP.
+ *
+ * Default: 0 (disabled, for now)
+ */
+#ifndef PJ_HAS_IPV6
+# define PJ_HAS_IPV6 0
+#endif
+
+ /**
* Maximum hostname length.
* Libraries sometimes needs to make copy of an address to stack buffer;
* the value here affects the stack usage.
diff --git a/pjlib/include/pj/errno.h b/pjlib/include/pj/errno.h
index 10b15fe5..507bd7b8 100644
--- a/pjlib/include/pj/errno.h
+++ b/pjlib/include/pj/errno.h
@@ -309,6 +309,11 @@ PJ_DECL(pj_status_t) pj_register_strerror(pj_status_t start_code,
* Ignored
*/
#define PJ_EIGNORED (PJ_ERRNO_START_STATUS + 20)/* 70020 */
+/**
+ * @hideinitializer
+ * IPv6 is not supported
+ */
+#define PJ_EIPV6NOTSUP (PJ_ERRNO_START_STATUS + 21)/* 70021 */
/** @} */ /* pj_errnum */
diff --git a/pjlib/include/pj/sock.h b/pjlib/include/pj/sock.h
index 12b771c9..d90c91e8 100644
--- a/pjlib/include/pj/sock.h
+++ b/pjlib/include/pj/sock.h
@@ -60,6 +60,9 @@ PJ_BEGIN_DECL
* THE LIBRARY WILL DO TRANSLATION TO THE NATIVE VALUE.
*/
+/** Address family is unspecified. @see pj_AF_UNSPEC() */
+extern const pj_uint16_t PJ_AF_UNSPEC;
+
/** Unix domain socket. @see pj_AF_UNIX() */
extern const pj_uint16_t PJ_AF_UNIX;
@@ -84,6 +87,8 @@ extern const pj_uint16_t PJ_AF_IRDA;
* global variables from a DLL.
*/
+/** Get #PJ_AF_UNSPEC value */
+PJ_DECL(pj_uint16_t) pj_AF_UNSPEC(void);
/** Get #PJ_AF_UNIX value. */
PJ_DECL(pj_uint16_t) pj_AF_UNIX(void);
/** Get #PJ_AF_INET value. */
@@ -313,6 +318,17 @@ typedef struct pj_in_addr
/**
+ * Maximum length of text representation of an IPv4 address.
+ */
+#define PJ_INET_ADDRSTRLEN 16
+
+/**
+ * Maximum length of text representation of an IPv6 address.
+ */
+#define PJ_INET6_ADDRSTRLEN 46
+
+
+/**
* This structure describes Internet socket address.
* If PJ_SOCKADDR_HAS_LEN is not zero, then sin_zero_len member is added
* to this struct. As far the application is concerned, the value of
@@ -333,27 +349,26 @@ struct pj_sockaddr_in
char sin_zero[8]; /**< Padding. */
};
+
#undef s6_addr
/**
* This structure describes IPv6 address.
*/
-typedef struct pj_in6_addr
+typedef union pj_in6_addr
{
- /** Union of address formats. */
- union {
- pj_uint8_t u6_addr8[16]; /**< u6_addr8 */
- pj_uint16_t u6_addr16[8]; /**< u6_addr16 */
- pj_uint32_t u6_addr32[4]; /**< u6_addr32 */
- } in6_u;
-/** Shortcut to access in6_u.u6_addr8. */
-#define s6_addr in6_u.u6_addr8
-/** Shortcut to access in6_u.u6_addr16. */
-#define s6_addr16 in6_u.u6_addr16
-/** Shortcut to access in6_u.u6_addr32. */
-#define s6_addr32 in6_u.u6_addr32
+ /* This is the main entry */
+ pj_uint8_t s6_addr[16]; /**< 8-bit array */
+
+ /* While these are used for proper alignment */
+ pj_uint32_t u6_addr32[4];
+#if defined(PJ_HAS_INT64) && PJ_HAS_INT64!=0
+ pj_int64_t u6_addr64[2];
+#endif
+
} pj_in6_addr;
+
/** Initializer value for pj_in6_addr. */
#define PJ_IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
@@ -371,15 +386,15 @@ typedef struct pj_in6_addr
typedef struct pj_sockaddr_in6
{
#if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
- pj_uint8_t sin_zero_len; /**< Just ignore this. */
- pj_uint8_t sin_family; /**< Address family. */
+ pj_uint8_t sin6_zero_len; /**< Just ignore this. */
+ pj_uint8_t sin6_family; /**< Address family. */
#else
pj_uint16_t sin6_family; /**< Address family */
#endif
pj_uint16_t sin6_port; /**< Transport layer port number. */
pj_uint32_t sin6_flowinfo; /**< IPv6 flow information */
pj_in6_addr sin6_addr; /**< IPv6 address. */
- pj_uint32_t sin6_scope_id; /**< IPv6 scope-id */
+ pj_uint32_t sin6_scope_id; /**< Set of interfaces for a scope */
} pj_sockaddr_in6;
@@ -474,6 +489,44 @@ PJ_DECL(char*) pj_inet_ntoa(pj_in_addr inaddr);
PJ_DECL(int) pj_inet_aton(const pj_str_t *cp, struct pj_in_addr *inp);
/**
+ * This function converts an address in its standard text presentation form
+ * into its numeric binary form. It supports both IPv4 and IPv6 address
+ * conversion.
+ *
+ * @param af Specify the family of the address. The PJ_AF_INET and
+ * PJ_AF_INET6 address families shall be supported.
+ * @param src Points to the string being passed in.
+ * @param dst Points to a buffer into which the function stores the
+ * numeric address; this shall be large enough to hold the
+ * numeric address (32 bits for PJ_AF_INET, 128 bits for
+ * PJ_AF_INET6).
+ *
+ * @return PJ_SUCCESS if conversion was successful.
+ */
+PJ_DECL(pj_status_t) pj_inet_pton(int af, const pj_str_t *src, void *dst);
+
+/**
+ * This function converts a numeric address into a text string suitable
+ * for presentation. It supports both IPv4 and IPv6 address
+ * conversion.
+ *
+ * @param af Specify the family of the address. This can be PJ_AF_INET
+ * or PJ_AF_INET6.
+ * @param src Points to a buffer holding an IPv4 address if the af argument
+ * is PJ_AF_INET, or an IPv6 address if the af argument is
+ * PJ_AF_INET6; the address must be in network byte order.
+ * @param dst Points to a buffer where the function stores the resulting
+ * text string; it shall not be NULL.
+ * @param size Specifies the size of this buffer, which shall be large
+ * enough to hold the text string (PJ_INET_ADDRSTRLEN characters
+ * for IPv4, PJ_INET6_ADDRSTRLEN characters for IPv6).
+ *
+ * @return PJ_SUCCESS if conversion was successful..
+ */
+PJ_DECL(pj_status_t) pj_inet_ntop(int af, const void *src,
+ char *dst, int size);
+
+/**
* Convert address string with numbers and dots to binary IP address.
*
* @param cp The IP address in numbers and dots notation.
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.