Ignore:
Timestamp:
Nov 26, 2007 5:36:18 AM (16 years ago)
Author:
bennylp
Message:

Fixed pj_gethostip() returns 0.0.0.0. Also how it returns the first interface if else fails

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib/src/pj/addr_resolv_symbian.cpp

    r1426 r1599  
    2020#include <pj/assert.h> 
    2121#include <pj/errno.h> 
     22#include <pj/ip_helper.h> 
     23#include <pj/sock.h> 
    2224#include <pj/string.h> 
    2325#include <pj/unicode.h> 
     
    8587 
    8688 
     89/* Get the default IP interface */ 
     90PJ_DEF(pj_status_t) pj_getdefaultipinterface(pj_in_addr *addr) 
     91{ 
     92    pj_sock_t fd; 
     93    pj_str_t cp; 
     94    pj_sockaddr_in a; 
     95    int len; 
     96    pj_status_t status; 
     97 
     98    status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &fd); 
     99    if (status != PJ_SUCCESS) { 
     100        return status; 
     101    } 
     102 
     103    cp = pj_str("1.1.1.1"); 
     104    pj_sockaddr_in_init(&a, &cp, 53); 
     105 
     106    status = pj_sock_connect(fd, &a, sizeof(a)); 
     107    if (status != PJ_SUCCESS) { 
     108        pj_sock_close(fd); 
     109        return status; 
     110    } 
     111 
     112    len = sizeof(a); 
     113    status = pj_sock_getsockname(fd, &a, &len); 
     114    if (status != PJ_SUCCESS) { 
     115        pj_sock_close(fd); 
     116        return status; 
     117    } 
     118 
     119    pj_sock_close(fd); 
     120 
     121    *addr = a.sin_addr; 
     122 
     123    /* Success */ 
     124    return PJ_SUCCESS; 
     125} 
     126 
     127 
    87128/* Resolve the IP address of local machine */ 
    88129PJ_DEF(pj_status_t) pj_gethostip(pj_in_addr *addr) 
     
    90131    const pj_str_t *hostname = pj_gethostname(); 
    91132    struct pj_hostent he; 
    92     pj_str_t cp; 
    93     pj_in_addr loopip; 
    94133    pj_status_t status; 
    95134 
    96     cp = pj_str("127.0.0.1"); 
    97     loopip = pj_inet_addr(&cp); 
    98135 
    99136    /* Try with resolving local hostname first */ 
     
    104141 
    105142 
    106     /* If we end up with 127.0.0.1 or 0.0.0.0, resolve the IP by getting  
    107      * the default interface to connect to some public host. 
     143    /* If we end up with 127.x.x.x, resolve the IP by getting the default 
     144     * interface to connect to some public host. 
    108145     */ 
    109     if (status!=PJ_SUCCESS || addr->s_addr == loopip.s_addr || !addr->s_addr) { 
    110         pj_sock_t fd; 
    111         pj_sockaddr_in a; 
    112         int len; 
     146    if (status != PJ_SUCCESS || (pj_ntohl(addr->s_addr) >> 24)==127 || 
     147        addr->s_addr == 0)  
     148    { 
     149        status = pj_getdefaultipinterface(addr); 
     150    } 
    113151 
    114         status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &fd); 
    115         if (status != PJ_SUCCESS) { 
    116             return status; 
     152    /* As the last resort, get the first available interface */ 
     153    if (status != PJ_SUCCESS) { 
     154        pj_in_addr addrs[2]; 
     155        unsigned count = PJ_ARRAY_SIZE(addrs); 
     156 
     157        status = pj_enum_ip_interface(&count, addrs); 
     158        if (status == PJ_SUCCESS) { 
     159            if (count != 0) { 
     160                *addr = addrs[0]; 
     161            } else { 
     162                /* Just return 127.0.0.1 */ 
     163                addr->s_addr = pj_htonl(0x7f000001); 
     164            } 
    117165        } 
    118  
    119         cp = pj_str("1.1.1.1"); 
    120         pj_sockaddr_in_init(&a, &cp, 53); 
    121  
    122         status = pj_sock_connect(fd, &a, sizeof(a)); 
    123         if (status != PJ_SUCCESS) { 
    124             pj_sock_close(fd); 
    125             return status; 
    126         } 
    127  
    128         len = sizeof(a); 
    129         status = pj_sock_getsockname(fd, &a, &len); 
    130         if (status != PJ_SUCCESS || a.sin_addr.s_addr==0) { 
    131             pj_sock_close(fd); 
    132             /* May return 127.0.0.1 */ 
    133             return status; 
    134         } 
    135  
    136         pj_sock_close(fd); 
    137          
    138         *addr = a.sin_addr; 
    139  
    140         if (a.sin_addr.s_addr == 0) 
    141             return PJ_ENOTFOUND; 
    142166    } 
    143167 
     
    146170 
    147171 
    148  
Note: See TracChangeset for help on using the changeset viewer.