Changeset 1585 for pjproject/trunk/pjlib/src/pj/addr_resolv_sock.c
- Timestamp:
- Nov 18, 2007 2:53:47 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjlib/src/pj/addr_resolv_sock.c
r1405 r1585 116 116 117 117 118 /* Resolve IPv4/IPv6 address */ 119 PJ_DEF(pj_status_t) pj_getaddrinfo(const pj_str_t *nodename, int af, 120 unsigned *count, pj_addrinfo ai[]) 121 { 122 #if defined(PJ_SOCK_HAS_GETADDRINFO) && PJ_SOCK_HAS_GETADDRINFO!=0 123 char nodecopy[PJ_MAX_HOSTNAME]; 124 struct addrinfo hint, *res; 125 unsigned i; 126 int rc; 127 128 PJ_ASSERT_RETURN(nodename && count && *count && ai, PJ_EINVAL); 129 PJ_ASSERT_RETURN(nodename->ptr && nodename->slen, PJ_EINVAL); 130 PJ_ASSERT_RETURN(af==PJ_AF_INET || af==PJ_AF_INET6, PJ_EINVAL); 131 132 /* Copy node name to null terminated string. */ 133 if (nodename->slen >= PJ_MAX_HOSTNAME) 134 return PJ_ENAMETOOLONG; 135 pj_memcpy(nodecopy, nodename->ptr, nodename->slen); 136 nodecopy[nodename->slen] = '\0'; 137 138 /* Call getaddrinfo() */ 139 pj_bzero(&hint, sizeof(hint)); 140 hint.ai_family = af; 141 142 rc = getaddrinfo(nodecopy, NULL, &hint, &res); 143 if (rc != 0) 144 return PJ_ERESOLVE; 145 146 /* Enumerate each item in the result */ 147 for (i=0; i<*count && res; res=res->ai_next) { 148 int len; 149 150 /* Ignore unwanted address families */ 151 if (af!=PJ_AF_UNSPEC && res->ai_family != af) 152 continue; 153 154 /* Ignore name that's too long */ 155 len = pj_ansi_strlen(res->ai_canonname); 156 if (len >= PJ_MAX_HOSTNAME) 157 continue; 158 159 /* Store canonical name */ 160 pj_ansi_strcpy(ai[i].ai_canonname, res->ai_canonname); 161 162 /* Store address */ 163 PJ_ASSERT_ON_FAIL(res->ai_addrlen <= sizeof(pj_sockaddr), continue); 164 pj_memcpy(&ai[i].ai_addr, res->ai_addr, res->ai_addrlen); 165 166 /* Next slot */ 167 ++i; 168 } 169 170 *count = i; 171 172 /* Done */ 173 return PJ_SUCCESS; 174 175 #else /* PJ_SOCK_HAS_GETADDRINFO */ 176 /* IPv6 is not supported */ 177 PJ_UNUSED_ARG(nodename); 178 PJ_UNUSED_ARG(af); 179 PJ_UNUSED_ARG(ai); 180 181 PJ_ASSERT_RETURN(count, PJ_EINVAL); 182 *count = 0; 183 184 return PJ_EIPV6NOTSUP; 185 #endif /* PJ_SOCK_HAS_GETADDRINFO */ 186 } 187
Note: See TracChangeset
for help on using the changeset viewer.