Changeset 1601 for pjproject/trunk/pjlib/src/pj/addr_resolv_sock.c
- Timestamp:
- Dec 1, 2007 8:52:57 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjlib/src/pj/addr_resolv_sock.c
r1599 r1601 56 56 } 57 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 97 /* Resolve the IP address of local machine */98 PJ_DEF(pj_status_t) pj_gethostip(pj_in_addr *addr)99 {100 const pj_str_t *hostname = pj_gethostname();101 struct pj_hostent he;102 pj_status_t status;103 104 105 #ifdef _MSC_VER106 /* Get rid of "uninitialized he variable" with MS compilers */107 pj_bzero(&he, sizeof(he));108 #endif109 110 /* Try with resolving local hostname first */111 status = pj_gethostbyname(hostname, &he);112 if (status == PJ_SUCCESS) {113 *addr = *(pj_in_addr*)he.h_addr;114 }115 116 117 /* If we end up with 127.x.x.x, resolve the IP by getting the default118 * interface to connect to some public host.119 */120 if (status != PJ_SUCCESS || (pj_ntohl(addr->s_addr) >> 24)==127 ||121 addr->s_addr == 0)122 {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 }139 }140 }141 142 return status;143 }144 145 146 58 /* Resolve IPv4/IPv6 address */ 147 PJ_DEF(pj_status_t) pj_getaddrinfo( const pj_str_t *nodename, int af,59 PJ_DEF(pj_status_t) pj_getaddrinfo(int af, const pj_str_t *nodename, 148 60 unsigned *count, pj_addrinfo ai[]) 149 61 { … … 156 68 PJ_ASSERT_RETURN(nodename && count && *count && ai, PJ_EINVAL); 157 69 PJ_ASSERT_RETURN(nodename->ptr && nodename->slen, PJ_EINVAL); 158 PJ_ASSERT_RETURN(af==PJ_AF_INET || af==PJ_AF_INET6, PJ_EINVAL); 70 PJ_ASSERT_RETURN(af==PJ_AF_INET || af==PJ_AF_INET6 || 71 af==PJ_AF_UNSPEC, PJ_EINVAL); 159 72 160 73 /* Copy node name to null terminated string. */ … … 180 93 continue; 181 94 182 /* Ignore name that's too long */ 183 len = pj_ansi_strlen(res->ai_canonname); 184 if (len >= PJ_MAX_HOSTNAME) 185 continue; 186 187 /* Store canonical name */ 188 pj_ansi_strcpy(ai[i].ai_canonname, res->ai_canonname); 95 /* Store canonical name (possibly truncating the name) */ 96 pj_ansi_strncpy(ai[i].ai_canonname, res->ai_canonname, 97 sizeof(ai[i].ai_canonname)); 98 ai[i].ai_canonname[sizeof(ai[i].ai_canonname)-1] = '\0'; 189 99 190 100 /* Store address */ … … 202 112 203 113 #else /* PJ_SOCK_HAS_GETADDRINFO */ 204 /* IPv6 is not supported */205 PJ_UNUSED_ARG(nodename);206 PJ_UNUSED_ARG(af);207 PJ_UNUSED_ARG(ai);208 114 209 115 PJ_ASSERT_RETURN(count, PJ_EINVAL); 210 *count = 0;211 116 212 return PJ_EIPV6NOTSUP; 117 if (af == PJ_AF_INET || af == PJ_AF_UNSPEC) { 118 pj_hostent he; 119 unsigned i, max_count; 120 pj_status_t status; 121 122 status = pj_gethostbyname(nodename, &he); 123 if (status != PJ_SUCCESS) 124 return status; 125 126 max_count = *count; 127 *count = 0; 128 129 pj_bzero(ai, max_count * sizeof(pj_addrinfo)); 130 131 for (i=0; he.h_addr_list[i] && *count<max_count; ++i) { 132 pj_ansi_strncpy(ai[*count].ai_canonname, he.h_name, 133 sizeof(ai[*count].ai_canonname)); 134 ai[*count].ai_canonname[sizeof(ai[*count].ai_canonname)-1] = '\0'; 135 136 ai[*count].ai_addr.ipv4.sin_family = PJ_AF_INET; 137 pj_memcpy(&ai[*count].ai_addr.ipv4.sin_addr, 138 he.h_addr_list[i], he.h_length); 139 140 (*count)++; 141 } 142 143 return PJ_SUCCESS; 144 145 } else { 146 /* IPv6 is not supported */ 147 *count = 0; 148 149 return PJ_EIPV6NOTSUP; 150 } 213 151 #endif /* PJ_SOCK_HAS_GETADDRINFO */ 214 152 }
Note: See TracChangeset
for help on using the changeset viewer.