Lines 248-284
Link Here
|
248 |
{ |
248 |
{ |
249 |
char hbuf[256], sbuf[8]; |
249 |
char hbuf[256], sbuf[8]; |
250 |
struct addrinfo hints, *res; |
250 |
struct addrinfo hints, *res; |
251 |
const char *sep, *host, *service; |
251 |
const char *hb, *he, *sep; |
|
|
252 |
const char *host, *service; |
252 |
int err, len; |
253 |
int err, len; |
253 |
|
254 |
|
254 |
/* split address if necessary */ |
255 |
/* first, check for a bracketed IPv6 address */ |
255 |
err = EAI_SYSTEM; |
256 |
if (*addr == '[') { |
256 |
if ((sep = strchr(addr, ':')) != NULL) { |
257 |
hb = addr + 1; |
|
|
258 |
if ((sep = strchr(hb, ']')) == NULL) { |
259 |
errno = EINVAL; |
260 |
goto syserr; |
261 |
} |
262 |
he = sep++; |
263 |
} else { |
264 |
hb = addr; |
265 |
sep = strchrnul(hb, ':'); |
266 |
he = sep; |
267 |
} |
268 |
|
269 |
/* see if we need to copy the host name */ |
270 |
if (*he != '\0') { |
257 |
len = snprintf(hbuf, sizeof(hbuf), |
271 |
len = snprintf(hbuf, sizeof(hbuf), |
258 |
"%.*s", (int)(sep - addr), addr); |
272 |
"%.*s", (int)(he - hb), hb); |
259 |
if (len < 0) |
273 |
if (len < 0) |
260 |
return (NULL); |
274 |
goto syserr; |
261 |
if (len >= (int)sizeof(hbuf)) { |
275 |
if (len >= (int)sizeof(hbuf)) { |
262 |
errno = ENAMETOOLONG; |
276 |
errno = ENAMETOOLONG; |
263 |
fetch_syserr(); |
277 |
goto syserr; |
264 |
return (NULL); |
|
|
265 |
} |
278 |
} |
266 |
host = hbuf; |
279 |
host = hbuf; |
267 |
service = sep + 1; |
280 |
} else { |
268 |
} else if (port != 0) { |
281 |
host = hb; |
|
|
282 |
} |
283 |
|
284 |
/* was it followed by a service name? */ |
285 |
if (*sep == '\0' && port != 0) { |
269 |
if (port < 1 || port > 65535) { |
286 |
if (port < 1 || port > 65535) { |
270 |
errno = EINVAL; |
287 |
errno = EINVAL; |
271 |
fetch_syserr(); |
288 |
goto syserr; |
272 |
return (NULL); |
|
|
273 |
} |
289 |
} |
274 |
if (snprintf(sbuf, sizeof(sbuf), "%d", port) < 0) { |
290 |
if (snprintf(sbuf, sizeof(sbuf), "%d", port) < 0) |
275 |
fetch_syserr(); |
291 |
goto syserr; |
276 |
return (NULL); |
|
|
277 |
} |
278 |
host = addr; |
279 |
service = sbuf; |
292 |
service = sbuf; |
|
|
293 |
} else if (*sep != '\0') { |
294 |
service = sep; |
280 |
} else { |
295 |
} else { |
281 |
host = addr; |
|
|
282 |
service = NULL; |
296 |
service = NULL; |
283 |
} |
297 |
} |
284 |
|
298 |
|
Lines 292-297
Link Here
|
292 |
return (NULL); |
306 |
return (NULL); |
293 |
} |
307 |
} |
294 |
return (res); |
308 |
return (res); |
|
|
309 |
syserr: |
310 |
fetch_syserr(); |
311 |
return (NULL); |
295 |
} |
312 |
} |
296 |
|
313 |
|
297 |
|
314 |
|