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: https://svn.pjsip.org/repos/pjproject/trunk@721 74dad513-b988-da41-8d7b-12977e46ad98
This commit is contained in:
Benny Prijono 2006-09-14 18:51:01 +00:00
parent 2461a14b2e
commit 594e4c5f29
9 changed files with 98 additions and 42 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -127,7 +127,8 @@
*ppe=0.0;
pp=buf+LPC_FILTERORDER+lMem-lTarget;
for (j=0; j<lTarget; j++) {
*ppe+=(*pp)*(*pp++);
*ppe+=(*pp)*(*pp);
++pp;
}
if (*ppe>0.0) {
@ -322,7 +323,8 @@
pp=cbvectors+lMem-lTarget;
for (j=0; j<lTarget; j++) {
*ppe+=(*pp)*(*pp++);
*ppe+=(*pp)*(*pp);
++pp;
}
ppi = cbvectors + lMem - 1 - lTarget;

View File

@ -245,13 +245,13 @@ PJ_DEF(pj_status_t) pjmedia_transport_udp_attach( pjmedia_endpt *endpt,
/* If address is 0.0.0.0, use host's IP address */
if (tp->rtp_addr_name.sin_addr.s_addr == 0) {
pj_hostent he;
const pj_str_t *hostname = pj_gethostname();
status = pj_gethostbyname(hostname, &he);
if (status != PJ_SUCCESS) {
pj_in_addr hostip;
status = pj_gethostip(&hostip);
if (status != PJ_SUCCESS)
goto on_error;
}
tp->rtp_addr_name.sin_addr = *(pj_in_addr*)he.h_addr;
tp->rtp_addr_name.sin_addr = hostip;
}
/* Same with RTCP */

View File

@ -78,7 +78,9 @@ static struct app_config
static pjsua_call_id current_call;
static pj_str_t uri_arg;
#ifdef STEREO_DEMO
static void stereo_demo();
#endif
/*****************************************************************************
* Configuration manipulation

View File

@ -249,15 +249,13 @@ PJ_DEF(pj_status_t) pjsip_tcp_transport_start( pjsip_endpoint *endpt,
* as the transport's address.
*/
if (listener_addr->sin_addr.s_addr == 0) {
const pj_str_t *hostname;
struct pj_hostent he;
pj_in_addr hostip;
hostname = pj_gethostname();
status = pj_gethostbyname(hostname, &he);
status = pj_gethostip(&hostip);
if (status != PJ_SUCCESS)
goto on_error;
listener_addr->sin_addr = *(pj_in_addr*)he.h_addr;
listener_addr->sin_addr = hostip;
}
pj_ansi_snprintf(listener->obj_name, sizeof(listener->obj_name),

View File

@ -652,15 +652,13 @@ PJ_DEF(pj_status_t) pjsip_udp_transport_start( pjsip_endpoint *endpt,
* of local hostname.
*/
if (tmp_addr.sin_addr.s_addr == PJ_INADDR_ANY) {
pj_hostent he;
const pj_str_t *hostname = pj_gethostname();
status = pj_gethostbyname(hostname, &he);
if (status != PJ_SUCCESS) {
pj_sock_close(sock);
pj_in_addr hostip;
status = pj_gethostip(&hostip);
if (status != PJ_SUCCESS)
return status;
}
pj_strcpy2(&bound_name.host,
pj_inet_ntoa(*(pj_in_addr*)he.h_addr));
pj_strcpy2(&bound_name.host, pj_inet_ntoa(hostip));
} else {
/* Otherwise use bound address. */
pj_strcpy2(&bound_name.host, pj_inet_ntoa(tmp_addr.sin_addr));

View File

@ -812,20 +812,16 @@ static pj_status_t create_sip_udp_sock(pj_in_addr bound_addr,
} else {
const pj_str_t *hostname = pj_gethostname();
struct pj_hostent he;
pj_bzero(p_pub_addr, sizeof(pj_sockaddr_in));
status = pj_gethostbyname(hostname, &he);
status = pj_gethostip(&p_pub_addr->sin_addr);
if (status != PJ_SUCCESS) {
pjsua_perror(THIS_FILE, "Unable to resolve local host", status);
pj_sock_close(sock);
return status;
}
pj_bzero(p_pub_addr, sizeof(pj_sockaddr_in));
p_pub_addr->sin_family = PJ_AF_INET;
p_pub_addr->sin_port = pj_htons((pj_uint16_t)port);
p_pub_addr->sin_addr = *(pj_in_addr*)he.h_addr;
}
*p_sock = sock;

View File

@ -286,23 +286,15 @@ static pj_status_t create_rtp_rtcp_sock(const pjsua_transport_config *cfg,
sock[1] = PJ_INVALID_SOCKET;
} else {
const pj_str_t *hostname;
pj_sockaddr_in addr;
pj_in_addr addr;
/* Get local IP address. */
hostname = pj_gethostname();
pj_bzero( &addr, sizeof(addr));
addr.sin_family = PJ_AF_INET;
status = pj_sockaddr_in_set_str_addr( &addr, hostname);
if (status != PJ_SUCCESS) {
pjsua_perror(THIS_FILE, "Unresolvable local hostname",
status);
status = pj_gethostip(&addr);
if (status != PJ_SUCCESS)
goto on_error;
}
for (i=0; i<2; ++i)
pj_memcpy(&mapped_addr[i], &addr, sizeof(addr));
mapped_addr[i].sin_addr = addr;
mapped_addr[0].sin_port=pj_htons((pj_uint16_t)rtp_port);
mapped_addr[1].sin_port=pj_htons((pj_uint16_t)(rtp_port+1));