summaryrefslogtreecommitdiff
path: root/pjlib
diff options
context:
space:
mode:
authorBenny Prijono <bennylp@teluu.com>2006-09-14 18:51:01 +0000
committerBenny Prijono <bennylp@teluu.com>2006-09-14 18:51:01 +0000
commitb6b613a4e59e2177a2e899dc4e21ab660c05de3d (patch)
treeab3821c5de8972a5006c4590ec631ff40ef61e15 /pjlib
parente015ebe064c6f5120ef2af2b73ce52796a06f790 (diff)
Fix the local IP address resolution issue in PJSIP, PJMEDIA, and PJSUA, by adding a new API pj_gethostip() to resolve the default local IP address of local host. This new function will work even when local hostname resolution is not set correctly, by detecting the default IP interface in the system.
Also fix compile warnings in iLBC. git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@721 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjlib')
-rw-r--r--pjlib/include/pj/addr_resolv.h15
-rw-r--r--pjlib/src/pj/addr_resolv_sock.c59
2 files changed, 71 insertions, 3 deletions
diff --git a/pjlib/include/pj/addr_resolv.h b/pjlib/include/pj/addr_resolv.h
index 3d75798c..45194b30 100644
--- a/pjlib/include/pj/addr_resolv.h
+++ b/pjlib/include/pj/addr_resolv.h
@@ -21,10 +21,10 @@
/**
* @file addr_resolv.h
- * @brief Address resolve (pj_gethostbyname()).
+ * @brief IP address resolution.
*/
-#include <pj/types.h>
+#include <pj/sock.h>
PJ_BEGIN_DECL
@@ -84,6 +84,17 @@ typedef struct pj_hostent
PJ_DECL(pj_status_t) pj_gethostbyname(const pj_str_t *name, pj_hostent *he);
+/**
+ * Resolve the primary IP address of local host.
+ *
+ * @param ip_addr On successful resolution, this will be filled up with
+ * the host IP address, in network byte order.
+ *
+ * @return PJ_SUCCESS on success, or the appropriate error code.
+ */
+PJ_DECL(pj_status_t) pj_gethostip(pj_in_addr *ip_addr);
+
+
/** @} */
PJ_END_DECL
diff --git a/pjlib/src/pj/addr_resolv_sock.c b/pjlib/src/pj/addr_resolv_sock.c
index 9b9eb6dc..36c1c574 100644
--- a/pjlib/src/pj/addr_resolv_sock.c
+++ b/pjlib/src/pj/addr_resolv_sock.c
@@ -19,8 +19,8 @@
#include <pj/addr_resolv.h>
#include <pj/assert.h>
#include <pj/string.h>
-#include <pj/compat/socket.h>
#include <pj/errno.h>
+#include <pj/compat/socket.h>
PJ_DEF(pj_status_t) pj_gethostbyname(const pj_str_t *hostname, pj_hostent *phe)
@@ -49,3 +49,60 @@ PJ_DEF(pj_status_t) pj_gethostbyname(const pj_str_t *hostname, pj_hostent *phe)
return PJ_SUCCESS;
}
+/* Resolve the IP address of local machine */
+pj_status_t pj_gethostip(pj_in_addr *addr)
+{
+ const pj_str_t *hostname = pj_gethostname();
+ struct pj_hostent he;
+ pj_str_t cp;
+ pj_in_addr loopip;
+ pj_status_t status;
+
+ cp = pj_str("127.0.0.1");
+ loopip = pj_inet_addr(&cp);
+
+ /* Try with resolving local hostname first */
+ status = pj_gethostbyname(hostname, &he);
+ if (status == PJ_SUCCESS) {
+ *addr = *(pj_in_addr*)he.h_addr;
+ }
+
+
+ /* If we end up with 127.0.0.1, resolve the IP by getting the default
+ * interface to connect to some public host.
+ */
+ if (status != PJ_SUCCESS || addr->s_addr == loopip.s_addr) {
+ pj_sock_t fd;
+ pj_sockaddr_in a;
+ int len;
+
+ status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &fd);
+ if (status != PJ_SUCCESS) {
+ return status;
+ }
+
+ cp = pj_str("1.1.1.1");
+ pj_sockaddr_in_init(&a, &cp, 53);
+
+ status = pj_sock_connect(fd, &a, sizeof(a));
+ if (status != PJ_SUCCESS) {
+ pj_sock_close(fd);
+ return status;
+ }
+
+ len = sizeof(a);
+ status = pj_sock_getsockname(fd, &a, &len);
+ if (status != PJ_SUCCESS) {
+ pj_sock_close(fd);
+ return status;
+ }
+
+ pj_sock_close(fd);
+
+ *addr = a.sin_addr;
+ }
+
+ return status;
+}
+
+