diff options
| author | beck <> | 2017-04-30 05:09:22 +0000 |
|---|---|---|
| committer | beck <> | 2017-04-30 05:09:22 +0000 |
| commit | 11a87330614c2f08fdfae566b604f04dafb84b38 (patch) | |
| tree | 5a94d85be4911fdf33a355de4ace376efa131930 | |
| parent | d6384322b936d181e80c1948d8ee20a647f0408e (diff) | |
| download | openbsd-11a87330614c2f08fdfae566b604f04dafb84b38.tar.gz openbsd-11a87330614c2f08fdfae566b604f04dafb84b38.tar.bz2 openbsd-11a87330614c2f08fdfae566b604f04dafb84b38.zip | |
Rework BIO_accept to be more like modern code.
ok jsing@
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libcrypto/bio/b_sock.c | 73 |
1 files changed, 19 insertions, 54 deletions
diff --git a/src/lib/libcrypto/bio/b_sock.c b/src/lib/libcrypto/bio/b_sock.c index cb5549434f..b8aba39578 100644 --- a/src/lib/libcrypto/bio/b_sock.c +++ b/src/lib/libcrypto/bio/b_sock.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: b_sock.c,v 1.64 2017/04/30 04:18:58 beck Exp $ */ | 1 | /* $OpenBSD: b_sock.c,v 1.65 2017/04/30 05:09:22 beck Exp $ */ |
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * | 4 | * |
| @@ -243,23 +243,15 @@ err: | |||
| 243 | int | 243 | int |
| 244 | BIO_accept(int sock, char **addr) | 244 | BIO_accept(int sock, char **addr) |
| 245 | { | 245 | { |
| 246 | char h[NI_MAXHOST], s[NI_MAXSERV]; | ||
| 247 | struct sockaddr_in sin; | ||
| 248 | socklen_t sin_len = sizeof(sin); | ||
| 246 | int ret = -1; | 249 | int ret = -1; |
| 247 | unsigned long l; | ||
| 248 | unsigned short port; | ||
| 249 | char *p, *tmp; | ||
| 250 | 250 | ||
| 251 | struct { | 251 | if (addr == NULL) |
| 252 | socklen_t len; | 252 | goto end; |
| 253 | union { | ||
| 254 | struct sockaddr sa; | ||
| 255 | struct sockaddr_in sa_in; | ||
| 256 | struct sockaddr_in6 sa_in6; | ||
| 257 | } from; | ||
| 258 | } sa; | ||
| 259 | 253 | ||
| 260 | sa.len = sizeof(sa.from); | 254 | ret = accept(sock, (struct sockaddr *)&sin, &sin_len); |
| 261 | memset(&sa.from, 0, sizeof(sa.from)); | ||
| 262 | ret = accept(sock, &sa.from.sa, &sa.len); | ||
| 263 | if (ret == -1) { | 255 | if (ret == -1) { |
| 264 | if (BIO_sock_should_retry(ret)) | 256 | if (BIO_sock_should_retry(ret)) |
| 265 | return -2; | 257 | return -2; |
| @@ -267,51 +259,24 @@ BIO_accept(int sock, char **addr) | |||
| 267 | BIOerror(BIO_R_ACCEPT_ERROR); | 259 | BIOerror(BIO_R_ACCEPT_ERROR); |
| 268 | goto end; | 260 | goto end; |
| 269 | } | 261 | } |
| 262 | /* XXX Crazy API. Can't be helped */ | ||
| 263 | if (*addr != NULL) { | ||
| 264 | free(*addr); | ||
| 265 | *addr = NULL; | ||
| 266 | } | ||
| 270 | 267 | ||
| 271 | if (addr == NULL) | 268 | if (sin.sin_family != AF_INET) |
| 272 | goto end; | 269 | goto end; |
| 273 | 270 | ||
| 274 | do { | 271 | if (getnameinfo((struct sockaddr *)&sin, sin_len, h, sizeof(h), |
| 275 | char h[NI_MAXHOST], s[NI_MAXSERV]; | 272 | s, sizeof(s), NI_NUMERICHOST|NI_NUMERICSERV) != 0) |
| 276 | size_t nl; | ||
| 277 | |||
| 278 | if (getnameinfo(&sa.from.sa, sa.len, h, sizeof(h), | ||
| 279 | s, sizeof(s), NI_NUMERICHOST|NI_NUMERICSERV)) | ||
| 280 | break; | ||
| 281 | nl = strlen(h) + strlen(s) + 2; | ||
| 282 | p = *addr; | ||
| 283 | if (p) | ||
| 284 | *p = '\0'; | ||
| 285 | if (!(tmp = realloc(p, nl))) { | ||
| 286 | close(ret); | ||
| 287 | ret = -1; | ||
| 288 | free(p); | ||
| 289 | *addr = NULL; | ||
| 290 | BIOerror(ERR_R_MALLOC_FAILURE); | ||
| 291 | goto end; | ||
| 292 | } | ||
| 293 | p = tmp; | ||
| 294 | *addr = p; | ||
| 295 | snprintf(*addr, nl, "%s:%s", h, s); | ||
| 296 | goto end; | 273 | goto end; |
| 297 | } while (0); | 274 | |
| 298 | if (sa.from.sa.sa_family != AF_INET) | 275 | if ((asprintf(addr, "%s:%s", h, s)) == -1) { |
| 276 | BIOerror(ERR_R_MALLOC_FAILURE); | ||
| 277 | *addr = NULL; | ||
| 299 | goto end; | 278 | goto end; |
| 300 | l = ntohl(sa.from.sa_in.sin_addr.s_addr); | ||
| 301 | port = ntohs(sa.from.sa_in.sin_port); | ||
| 302 | if (*addr == NULL) { | ||
| 303 | if ((p = malloc(24)) == NULL) { | ||
| 304 | close(ret); | ||
| 305 | ret = -1; | ||
| 306 | BIOerror(ERR_R_MALLOC_FAILURE); | ||
| 307 | goto end; | ||
| 308 | } | ||
| 309 | *addr = p; | ||
| 310 | } | 279 | } |
| 311 | snprintf(*addr, 24, "%d.%d.%d.%d:%d", | ||
| 312 | (unsigned char)(l >> 24L) & 0xff, (unsigned char)(l >> 16L) & 0xff, | ||
| 313 | (unsigned char)(l >> 8L) & 0xff, (unsigned char)(l) & 0xff, port); | ||
| 314 | |||
| 315 | end: | 280 | end: |
| 316 | return (ret); | 281 | return (ret); |
| 317 | } | 282 | } |
