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_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 
Note: See TracChangeset for help on using the changeset viewer.