Ignore:
Timestamp:
Sep 14, 2006 6:51:01 PM (18 years ago)
Author:
bennylp
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib/src/pj/addr_resolv_sock.c

    r65 r721  
    2020#include <pj/assert.h> 
    2121#include <pj/string.h> 
     22#include <pj/errno.h> 
    2223#include <pj/compat/socket.h> 
    23 #include <pj/errno.h> 
    2424 
    2525 
     
    5050} 
    5151 
     52/* Resolve the IP address of local machine */ 
     53pj_status_t pj_gethostip(pj_in_addr *addr) 
     54{ 
     55    const pj_str_t *hostname = pj_gethostname(); 
     56    struct pj_hostent he; 
     57    pj_str_t cp; 
     58    pj_in_addr loopip; 
     59    pj_status_t status; 
     60 
     61    cp = pj_str("127.0.0.1"); 
     62    loopip = pj_inet_addr(&cp); 
     63 
     64    /* Try with resolving local hostname first */ 
     65    status = pj_gethostbyname(hostname, &he); 
     66    if (status == PJ_SUCCESS) { 
     67        *addr = *(pj_in_addr*)he.h_addr; 
     68    } 
     69 
     70 
     71    /* If we end up with 127.0.0.1, resolve the IP by getting the default 
     72     * interface to connect to some public host. 
     73     */ 
     74    if (status != PJ_SUCCESS || addr->s_addr == loopip.s_addr) { 
     75        pj_sock_t fd; 
     76        pj_sockaddr_in a; 
     77        int len; 
     78 
     79        status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &fd); 
     80        if (status != PJ_SUCCESS) { 
     81            return status; 
     82        } 
     83 
     84        cp = pj_str("1.1.1.1"); 
     85        pj_sockaddr_in_init(&a, &cp, 53); 
     86 
     87        status = pj_sock_connect(fd, &a, sizeof(a)); 
     88        if (status != PJ_SUCCESS) { 
     89            pj_sock_close(fd); 
     90            return status; 
     91        } 
     92 
     93        len = sizeof(a); 
     94        status = pj_sock_getsockname(fd, &a, &len); 
     95        if (status != PJ_SUCCESS) { 
     96            pj_sock_close(fd); 
     97            return status; 
     98        } 
     99 
     100        pj_sock_close(fd); 
     101 
     102        *addr = a.sin_addr; 
     103    } 
     104 
     105    return status; 
     106} 
     107 
     108 
Note: See TracChangeset for help on using the changeset viewer.