Changeset 1599


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

Location:
pjproject/trunk/pjlib
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib/include/pj/addr_resolv.h

    r1585 r1599  
    107107 
    108108/** 
     109 * Get the IP address of the default interface. Default interface is the 
     110 * interface of the default route. 
     111 * 
     112 * @param ip_addr   On successful resolution, this will be filled up with 
     113 *                  the IP address, in network byte order. 
     114 * 
     115 * @return          PJ_SUCCESS on success, or the appropriate error code. 
     116 */ 
     117PJ_DECL(pj_status_t) pj_getdefaultipinterface(pj_in_addr *ip_addr); 
     118 
     119 
     120/** 
    109121 * This function translates the name of a service location (for example,  
    110122 * a host name) and returns a set of addresses and associated information 
  • pjproject/trunk/pjlib/src/pj/addr_resolv_sock.c

    r1592 r1599  
    2121#include <pj/string.h> 
    2222#include <pj/errno.h> 
     23#include <pj/ip_helper.h> 
    2324#include <pj/compat/socket.h> 
    2425 
     
    5556} 
    5657 
     58/* Get the default IP interface */ 
     59PJ_DEF(pj_status_t) pj_getdefaultipinterface(pj_in_addr *addr) 
     60{ 
     61    pj_sock_t fd; 
     62    pj_str_t cp; 
     63    pj_sockaddr_in a; 
     64    int len; 
     65    pj_status_t status; 
     66 
     67    status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &fd); 
     68    if (status != PJ_SUCCESS) { 
     69        return status; 
     70    } 
     71 
     72    cp = pj_str("1.1.1.1"); 
     73    pj_sockaddr_in_init(&a, &cp, 53); 
     74 
     75    status = pj_sock_connect(fd, &a, sizeof(a)); 
     76    if (status != PJ_SUCCESS) { 
     77        pj_sock_close(fd); 
     78        return status; 
     79    } 
     80 
     81    len = sizeof(a); 
     82    status = pj_sock_getsockname(fd, &a, &len); 
     83    if (status != PJ_SUCCESS) { 
     84        pj_sock_close(fd); 
     85        return status; 
     86    } 
     87 
     88    pj_sock_close(fd); 
     89 
     90    *addr = a.sin_addr; 
     91 
     92    /* Success */ 
     93    return PJ_SUCCESS; 
     94} 
     95 
     96 
    5797/* Resolve the IP address of local machine */ 
    5898PJ_DEF(pj_status_t) pj_gethostip(pj_in_addr *addr) 
     
    81121        addr->s_addr == 0)  
    82122    { 
    83         pj_sock_t fd; 
    84         pj_str_t cp; 
    85         pj_sockaddr_in a; 
    86         int len; 
    87  
    88         status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &fd); 
    89         if (status != PJ_SUCCESS) { 
    90             return status; 
     123        status = pj_getdefaultipinterface(addr); 
     124    } 
     125 
     126    /* As the last resort, get the first available interface */ 
     127    if (status != PJ_SUCCESS) { 
     128        pj_in_addr addrs[2]; 
     129        unsigned count = PJ_ARRAY_SIZE(addrs); 
     130 
     131        status = pj_enum_ip_interface(&count, addrs); 
     132        if (status == PJ_SUCCESS) { 
     133            if (count != 0) { 
     134                *addr = addrs[0]; 
     135            } else { 
     136                /* Just return 127.0.0.1 */ 
     137                addr->s_addr = pj_htonl (0x7f000001); 
     138            } 
    91139        } 
    92  
    93         cp = pj_str("1.1.1.1"); 
    94         pj_sockaddr_in_init(&a, &cp, 53); 
    95  
    96         status = pj_sock_connect(fd, &a, sizeof(a)); 
    97         if (status != PJ_SUCCESS) { 
    98             pj_sock_close(fd); 
    99             /* Return 127.0.0.1 as the address */ 
    100             return PJ_SUCCESS; 
    101         } 
    102  
    103         len = sizeof(a); 
    104         status = pj_sock_getsockname(fd, &a, &len); 
    105         if (status != PJ_SUCCESS) { 
    106             pj_sock_close(fd); 
    107             /* Return 127.0.0.1 as the address */ 
    108             return PJ_SUCCESS; 
    109         } 
    110  
    111         pj_sock_close(fd); 
    112  
    113         *addr = a.sin_addr; 
    114140    } 
    115141 
  • 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  
  • pjproject/trunk/pjlib/src/pj/ip_helper_generic.c

    r1584 r1599  
    3434 
    3535    /* Just get one default route */ 
    36     status = pj_gethostip(&ifs[0]); 
     36    status = pj_getdefaultipinterface(&ifs[0]); 
    3737    if (status != PJ_SUCCESS) 
    3838        return status; 
     
    112112 
    113113    /* Just get one default route */ 
    114     status = pj_gethostip(&routes[0].ipv4.if_addr); 
     114    status = pj_getdefaultipinterface(&routes[0].ipv4.if_addr); 
    115115    if (status != PJ_SUCCESS) 
    116116        return status; 
Note: See TracChangeset for help on using the changeset viewer.