- Timestamp:
- Nov 26, 2007 5:36:18 AM (17 years ago)
- Location:
- pjproject/trunk/pjlib
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjlib/include/pj/addr_resolv.h
r1585 r1599 107 107 108 108 /** 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 */ 117 PJ_DECL(pj_status_t) pj_getdefaultipinterface(pj_in_addr *ip_addr); 118 119 120 /** 109 121 * This function translates the name of a service location (for example, 110 122 * a host name) and returns a set of addresses and associated information -
pjproject/trunk/pjlib/src/pj/addr_resolv_sock.c
r1592 r1599 21 21 #include <pj/string.h> 22 22 #include <pj/errno.h> 23 #include <pj/ip_helper.h> 23 24 #include <pj/compat/socket.h> 24 25 … … 55 56 } 56 57 58 /* Get the default IP interface */ 59 PJ_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 57 97 /* Resolve the IP address of local machine */ 58 98 PJ_DEF(pj_status_t) pj_gethostip(pj_in_addr *addr) … … 81 121 addr->s_addr == 0) 82 122 { 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 } 91 139 } 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;114 140 } 115 141 -
pjproject/trunk/pjlib/src/pj/addr_resolv_symbian.cpp
r1426 r1599 20 20 #include <pj/assert.h> 21 21 #include <pj/errno.h> 22 #include <pj/ip_helper.h> 23 #include <pj/sock.h> 22 24 #include <pj/string.h> 23 25 #include <pj/unicode.h> … … 85 87 86 88 89 /* Get the default IP interface */ 90 PJ_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 87 128 /* Resolve the IP address of local machine */ 88 129 PJ_DEF(pj_status_t) pj_gethostip(pj_in_addr *addr) … … 90 131 const pj_str_t *hostname = pj_gethostname(); 91 132 struct pj_hostent he; 92 pj_str_t cp;93 pj_in_addr loopip;94 133 pj_status_t status; 95 134 96 cp = pj_str("127.0.0.1");97 loopip = pj_inet_addr(&cp);98 135 99 136 /* Try with resolving local hostname first */ … … 104 141 105 142 106 /* If we end up with 127. 0.0.1 or 0.0.0.0, resolve the IP by getting107 * the defaultinterface 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. 108 145 */ 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 } 113 151 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 } 117 165 } 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;142 166 } 143 167 … … 146 170 147 171 148 -
pjproject/trunk/pjlib/src/pj/ip_helper_generic.c
r1584 r1599 34 34 35 35 /* Just get one default route */ 36 status = pj_get hostip(&ifs[0]);36 status = pj_getdefaultipinterface(&ifs[0]); 37 37 if (status != PJ_SUCCESS) 38 38 return status; … … 112 112 113 113 /* Just get one default route */ 114 status = pj_get hostip(&routes[0].ipv4.if_addr);114 status = pj_getdefaultipinterface(&routes[0].ipv4.if_addr); 115 115 if (status != PJ_SUCCESS) 116 116 return status;
Note: See TracChangeset
for help on using the changeset viewer.