diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/usr.bin/nc/netcat.c | 40 |
1 files changed, 19 insertions, 21 deletions
diff --git a/src/usr.bin/nc/netcat.c b/src/usr.bin/nc/netcat.c index 72c56a60d9..bb74bb6070 100644 --- a/src/usr.bin/nc/netcat.c +++ b/src/usr.bin/nc/netcat.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: netcat.c,v 1.160 2016/07/13 16:35:47 jsing Exp $ */ | 1 | /* $OpenBSD: netcat.c,v 1.161 2016/07/30 22:04:04 halex Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> | 3 | * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> |
| 4 | * Copyright (c) 2015 Bob Beck. All rights reserved. | 4 | * Copyright (c) 2015 Bob Beck. All rights reserved. |
| @@ -834,13 +834,12 @@ remote_connect(const char *host, const char *port, struct addrinfo hints) | |||
| 834 | struct addrinfo *res, *res0; | 834 | struct addrinfo *res, *res0; |
| 835 | int s, error, on = 1, save_errno; | 835 | int s, error, on = 1, save_errno; |
| 836 | 836 | ||
| 837 | if ((error = getaddrinfo(host, port, &hints, &res))) | 837 | if ((error = getaddrinfo(host, port, &hints, &res0))) |
| 838 | errx(1, "getaddrinfo: %s", gai_strerror(error)); | 838 | errx(1, "getaddrinfo: %s", gai_strerror(error)); |
| 839 | 839 | ||
| 840 | res0 = res; | 840 | for (res = res0; res; res = res->ai_next) { |
| 841 | do { | 841 | if ((s = socket(res->ai_family, res->ai_socktype | |
| 842 | if ((s = socket(res0->ai_family, res0->ai_socktype | | 842 | SOCK_NONBLOCK, res->ai_protocol)) < 0) |
| 843 | SOCK_NONBLOCK, res0->ai_protocol)) < 0) | ||
| 844 | continue; | 843 | continue; |
| 845 | 844 | ||
| 846 | /* Bind to a local port or source address if specified. */ | 845 | /* Bind to a local port or source address if specified. */ |
| @@ -850,7 +849,7 @@ remote_connect(const char *host, const char *port, struct addrinfo hints) | |||
| 850 | /* try SO_BINDANY, but don't insist */ | 849 | /* try SO_BINDANY, but don't insist */ |
| 851 | setsockopt(s, SOL_SOCKET, SO_BINDANY, &on, sizeof(on)); | 850 | setsockopt(s, SOL_SOCKET, SO_BINDANY, &on, sizeof(on)); |
| 852 | memset(&ahints, 0, sizeof(struct addrinfo)); | 851 | memset(&ahints, 0, sizeof(struct addrinfo)); |
| 853 | ahints.ai_family = res0->ai_family; | 852 | ahints.ai_family = res->ai_family; |
| 854 | ahints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM; | 853 | ahints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM; |
| 855 | ahints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP; | 854 | ahints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP; |
| 856 | ahints.ai_flags = AI_PASSIVE; | 855 | ahints.ai_flags = AI_PASSIVE; |
| @@ -863,9 +862,9 @@ remote_connect(const char *host, const char *port, struct addrinfo hints) | |||
| 863 | freeaddrinfo(ares); | 862 | freeaddrinfo(ares); |
| 864 | } | 863 | } |
| 865 | 864 | ||
| 866 | set_common_sockopts(s, res0->ai_family); | 865 | set_common_sockopts(s, res->ai_family); |
| 867 | 866 | ||
| 868 | if (timeout_connect(s, res0->ai_addr, res0->ai_addrlen) == 0) | 867 | if (timeout_connect(s, res->ai_addr, res->ai_addrlen) == 0) |
| 869 | break; | 868 | break; |
| 870 | if (vflag) | 869 | if (vflag) |
| 871 | warn("connect to %s port %s (%s) failed", host, port, | 870 | warn("connect to %s port %s (%s) failed", host, port, |
| @@ -875,9 +874,9 @@ remote_connect(const char *host, const char *port, struct addrinfo hints) | |||
| 875 | close(s); | 874 | close(s); |
| 876 | errno = save_errno; | 875 | errno = save_errno; |
| 877 | s = -1; | 876 | s = -1; |
| 878 | } while ((res0 = res0->ai_next) != NULL); | 877 | } |
| 879 | 878 | ||
| 880 | freeaddrinfo(res); | 879 | freeaddrinfo(res0); |
| 881 | 880 | ||
| 882 | return (s); | 881 | return (s); |
| 883 | } | 882 | } |
| @@ -932,37 +931,36 @@ local_listen(char *host, char *port, struct addrinfo hints) | |||
| 932 | if (host == NULL && hints.ai_family == AF_UNSPEC) | 931 | if (host == NULL && hints.ai_family == AF_UNSPEC) |
| 933 | hints.ai_family = AF_INET; | 932 | hints.ai_family = AF_INET; |
| 934 | 933 | ||
| 935 | if ((error = getaddrinfo(host, port, &hints, &res))) | 934 | if ((error = getaddrinfo(host, port, &hints, &res0))) |
| 936 | errx(1, "getaddrinfo: %s", gai_strerror(error)); | 935 | errx(1, "getaddrinfo: %s", gai_strerror(error)); |
| 937 | 936 | ||
| 938 | res0 = res; | 937 | for (res = res0; res; res = res->ai_next) { |
| 939 | do { | 938 | if ((s = socket(res->ai_family, res->ai_socktype, |
| 940 | if ((s = socket(res0->ai_family, res0->ai_socktype, | 939 | res->ai_protocol)) < 0) |
| 941 | res0->ai_protocol)) < 0) | ||
| 942 | continue; | 940 | continue; |
| 943 | 941 | ||
| 944 | ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x)); | 942 | ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x)); |
| 945 | if (ret == -1) | 943 | if (ret == -1) |
| 946 | err(1, NULL); | 944 | err(1, NULL); |
| 947 | 945 | ||
| 948 | set_common_sockopts(s, res0->ai_family); | 946 | set_common_sockopts(s, res->ai_family); |
| 949 | 947 | ||
| 950 | if (bind(s, (struct sockaddr *)res0->ai_addr, | 948 | if (bind(s, (struct sockaddr *)res->ai_addr, |
| 951 | res0->ai_addrlen) == 0) | 949 | res->ai_addrlen) == 0) |
| 952 | break; | 950 | break; |
| 953 | 951 | ||
| 954 | save_errno = errno; | 952 | save_errno = errno; |
| 955 | close(s); | 953 | close(s); |
| 956 | errno = save_errno; | 954 | errno = save_errno; |
| 957 | s = -1; | 955 | s = -1; |
| 958 | } while ((res0 = res0->ai_next) != NULL); | 956 | } |
| 959 | 957 | ||
| 960 | if (!uflag && s != -1) { | 958 | if (!uflag && s != -1) { |
| 961 | if (listen(s, 1) < 0) | 959 | if (listen(s, 1) < 0) |
| 962 | err(1, "listen"); | 960 | err(1, "listen"); |
| 963 | } | 961 | } |
| 964 | 962 | ||
| 965 | freeaddrinfo(res); | 963 | freeaddrinfo(res0); |
| 966 | 964 | ||
| 967 | return (s); | 965 | return (s); |
| 968 | } | 966 | } |
