From ba8b2066cba86e67f73ba5554669e7d0563408ac Mon Sep 17 00:00:00 2001 From: Benny Prijono Date: Mon, 26 Nov 2007 05:36:18 +0000 Subject: Fixed pj_gethostip() returns 0.0.0.0. Also how it returns the first interface if else fails git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@1599 74dad513-b988-da41-8d7b-12977e46ad98 --- pjlib/src/pj/addr_resolv_sock.c | 84 +++++++++++++++++++----------- pjlib/src/pj/addr_resolv_symbian.cpp | 99 ++++++++++++++++++++++-------------- pjlib/src/pj/ip_helper_generic.c | 4 +- 3 files changed, 118 insertions(+), 69 deletions(-) (limited to 'pjlib/src/pj') diff --git a/pjlib/src/pj/addr_resolv_sock.c b/pjlib/src/pj/addr_resolv_sock.c index 5e8cde59..a4599ef7 100644 --- a/pjlib/src/pj/addr_resolv_sock.c +++ b/pjlib/src/pj/addr_resolv_sock.c @@ -20,6 +20,7 @@ #include #include #include +#include #include @@ -54,6 +55,45 @@ PJ_DEF(pj_status_t) pj_gethostbyname(const pj_str_t *hostname, pj_hostent *phe) return PJ_SUCCESS; } +/* Get the default IP interface */ +PJ_DEF(pj_status_t) pj_getdefaultipinterface(pj_in_addr *addr) +{ + pj_sock_t fd; + pj_str_t cp; + pj_sockaddr_in a; + int len; + pj_status_t status; + + 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; + + /* Success */ + return PJ_SUCCESS; +} + + /* Resolve the IP address of local machine */ PJ_DEF(pj_status_t) pj_gethostip(pj_in_addr *addr) { @@ -80,37 +120,23 @@ PJ_DEF(pj_status_t) pj_gethostip(pj_in_addr *addr) if (status != PJ_SUCCESS || (pj_ntohl(addr->s_addr) >> 24)==127 || addr->s_addr == 0) { - pj_sock_t fd; - pj_str_t cp; - 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 127.0.0.1 as the address */ - return PJ_SUCCESS; - } + status = pj_getdefaultipinterface(addr); + } - len = sizeof(a); - status = pj_sock_getsockname(fd, &a, &len); - if (status != PJ_SUCCESS) { - pj_sock_close(fd); - /* Return 127.0.0.1 as the address */ - return PJ_SUCCESS; + /* As the last resort, get the first available interface */ + if (status != PJ_SUCCESS) { + pj_in_addr addrs[2]; + unsigned count = PJ_ARRAY_SIZE(addrs); + + status = pj_enum_ip_interface(&count, addrs); + if (status == PJ_SUCCESS) { + if (count != 0) { + *addr = addrs[0]; + } else { + /* Just return 127.0.0.1 */ + addr->s_addr = pj_htonl (0x7f000001); + } } - - pj_sock_close(fd); - - *addr = a.sin_addr; } return status; diff --git a/pjlib/src/pj/addr_resolv_symbian.cpp b/pjlib/src/pj/addr_resolv_symbian.cpp index 7bdc4a6f..496c1f8b 100644 --- a/pjlib/src/pj/addr_resolv_symbian.cpp +++ b/pjlib/src/pj/addr_resolv_symbian.cpp @@ -19,6 +19,8 @@ #include #include #include +#include +#include #include #include @@ -84,17 +86,52 @@ PJ_DEF(pj_status_t) pj_gethostbyname(const pj_str_t *name, pj_hostent *he) } +/* Get the default IP interface */ +PJ_DEF(pj_status_t) pj_getdefaultipinterface(pj_in_addr *addr) +{ + pj_sock_t fd; + pj_str_t cp; + pj_sockaddr_in a; + int len; + pj_status_t status; + + 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; + + /* Success */ + return PJ_SUCCESS; +} + + /* Resolve the IP address of local machine */ PJ_DEF(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); @@ -103,46 +140,32 @@ PJ_DEF(pj_status_t) pj_gethostip(pj_in_addr *addr) } - /* If we end up with 127.0.0.1 or 0.0.0.0, resolve the IP by getting - * the default interface to connect to some public host. + /* If we end up with 127.x.x.x, resolve the IP by getting the default + * interface to connect to some public host. */ - if (status!=PJ_SUCCESS || addr->s_addr == loopip.s_addr || !addr->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; - } + if (status != PJ_SUCCESS || (pj_ntohl(addr->s_addr) >> 24)==127 || + addr->s_addr == 0) + { + status = pj_getdefaultipinterface(addr); + } - len = sizeof(a); - status = pj_sock_getsockname(fd, &a, &len); - if (status != PJ_SUCCESS || a.sin_addr.s_addr==0) { - pj_sock_close(fd); - /* May return 127.0.0.1 */ - return status; + /* As the last resort, get the first available interface */ + if (status != PJ_SUCCESS) { + pj_in_addr addrs[2]; + unsigned count = PJ_ARRAY_SIZE(addrs); + + status = pj_enum_ip_interface(&count, addrs); + if (status == PJ_SUCCESS) { + if (count != 0) { + *addr = addrs[0]; + } else { + /* Just return 127.0.0.1 */ + addr->s_addr = pj_htonl(0x7f000001); + } } - - pj_sock_close(fd); - - *addr = a.sin_addr; - - if (a.sin_addr.s_addr == 0) - return PJ_ENOTFOUND; } return status; } - diff --git a/pjlib/src/pj/ip_helper_generic.c b/pjlib/src/pj/ip_helper_generic.c index 092bb28e..a9284807 100644 --- a/pjlib/src/pj/ip_helper_generic.c +++ b/pjlib/src/pj/ip_helper_generic.c @@ -33,7 +33,7 @@ static pj_status_t dummy_enum_ip_interface(unsigned *p_cnt, pj_bzero(ifs, sizeof(ifs[0]) * (*p_cnt)); /* Just get one default route */ - status = pj_gethostip(&ifs[0]); + status = pj_getdefaultipinterface(&ifs[0]); if (status != PJ_SUCCESS) return status; @@ -111,7 +111,7 @@ PJ_DEF(pj_status_t) pj_enum_ip_route(unsigned *p_cnt, pj_bzero(routes, sizeof(routes[0]) * (*p_cnt)); /* Just get one default route */ - status = pj_gethostip(&routes[0].ipv4.if_addr); + status = pj_getdefaultipinterface(&routes[0].ipv4.if_addr); if (status != PJ_SUCCESS) return status; -- cgit v1.2.3