diff options
| author | job <> | 2019-10-24 12:48:54 +0000 |
|---|---|---|
| committer | job <> | 2019-10-24 12:48:54 +0000 |
| commit | d25f6a6259f082732af4c3730fbd47e3b0d53453 (patch) | |
| tree | 300ae009b740a77b851c677e2102a9a06c920693 /src | |
| parent | 80cee2ce711fd650b97832fed814e79435613568 (diff) | |
| download | openbsd-d25f6a6259f082732af4c3730fbd47e3b0d53453.tar.gz openbsd-d25f6a6259f082732af4c3730fbd47e3b0d53453.tar.bz2 openbsd-d25f6a6259f082732af4c3730fbd47e3b0d53453.zip | |
Print IP address in verbose mode
OK kn@
Diffstat (limited to 'src')
| -rw-r--r-- | src/usr.bin/nc/netcat.c | 46 |
1 files changed, 34 insertions, 12 deletions
diff --git a/src/usr.bin/nc/netcat.c b/src/usr.bin/nc/netcat.c index 711a49d247..503e9aa2b5 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.208 2019/10/23 13:49:24 beck Exp $ */ | 1 | /* $OpenBSD: netcat.c,v 1.209 2019/10/24 12:48:54 job 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. |
| @@ -125,7 +125,7 @@ void help(void) __attribute__((noreturn)); | |||
| 125 | int local_listen(const char *, const char *, struct addrinfo); | 125 | int local_listen(const char *, const char *, struct addrinfo); |
| 126 | void readwrite(int, struct tls *); | 126 | void readwrite(int, struct tls *); |
| 127 | void fdpass(int nfd) __attribute__((noreturn)); | 127 | void fdpass(int nfd) __attribute__((noreturn)); |
| 128 | int remote_connect(const char *, const char *, struct addrinfo); | 128 | int remote_connect(const char *, const char *, struct addrinfo, char *); |
| 129 | int timeout_tls(int, struct tls *, int (*)(struct tls *)); | 129 | int timeout_tls(int, struct tls *, int (*)(struct tls *)); |
| 130 | int timeout_connect(int, const struct sockaddr *, socklen_t); | 130 | int timeout_connect(int, const struct sockaddr *, socklen_t); |
| 131 | int socks_connect(const char *, const char *, struct addrinfo, | 131 | int socks_connect(const char *, const char *, struct addrinfo, |
| @@ -151,6 +151,7 @@ main(int argc, char *argv[]) | |||
| 151 | { | 151 | { |
| 152 | int ch, s = -1, ret, socksv; | 152 | int ch, s = -1, ret, socksv; |
| 153 | char *host, *uport; | 153 | char *host, *uport; |
| 154 | char ipaddr[NI_MAXHOST]; | ||
| 154 | struct addrinfo hints; | 155 | struct addrinfo hints; |
| 155 | struct servent *sv; | 156 | struct servent *sv; |
| 156 | socklen_t len; | 157 | socklen_t len; |
| @@ -677,7 +678,8 @@ main(int argc, char *argv[]) | |||
| 677 | proxy, proxyport, proxyhints, socksv, | 678 | proxy, proxyport, proxyhints, socksv, |
| 678 | Pflag); | 679 | Pflag); |
| 679 | else | 680 | else |
| 680 | s = remote_connect(host, portlist[i], hints); | 681 | s = remote_connect(host, portlist[i], hints, |
| 682 | ipaddr); | ||
| 681 | 683 | ||
| 682 | if (s == -1) | 684 | if (s == -1) |
| 683 | continue; | 685 | continue; |
| @@ -701,10 +703,14 @@ main(int argc, char *argv[]) | |||
| 701 | uflag ? "udp" : "tcp"); | 703 | uflag ? "udp" : "tcp"); |
| 702 | } | 704 | } |
| 703 | 705 | ||
| 704 | fprintf(stderr, | 706 | fprintf(stderr, "Connection to %s", host); |
| 705 | "Connection to %s %s port [%s/%s] " | 707 | |
| 706 | "succeeded!\n", host, portlist[i], | 708 | /* if there is something to report, print IP */ |
| 707 | uflag ? "udp" : "tcp", | 709 | if (!nflag && (strcmp(host, ipaddr) != 0)) |
| 710 | fprintf(stderr, " (%s)", ipaddr); | ||
| 711 | |||
| 712 | fprintf(stderr, " %s port [%s/%s] succeeded!\n", | ||
| 713 | portlist[i], uflag ? "udp" : "tcp", | ||
| 708 | sv ? sv->s_name : "*"); | 714 | sv ? sv->s_name : "*"); |
| 709 | } | 715 | } |
| 710 | if (Fflag) | 716 | if (Fflag) |
| @@ -916,10 +922,11 @@ unix_listen(char *path) | |||
| 916 | * port or source address if needed. Returns -1 on failure. | 922 | * port or source address if needed. Returns -1 on failure. |
| 917 | */ | 923 | */ |
| 918 | int | 924 | int |
| 919 | remote_connect(const char *host, const char *port, struct addrinfo hints) | 925 | remote_connect(const char *host, const char *port, struct addrinfo hints, |
| 926 | char *ipaddr) | ||
| 920 | { | 927 | { |
| 921 | struct addrinfo *res, *res0; | 928 | struct addrinfo *res, *res0; |
| 922 | int s = -1, error, on = 1, save_errno; | 929 | int s = -1, error, herr, on = 1, save_errno; |
| 923 | 930 | ||
| 924 | if ((error = getaddrinfo(host, port, &hints, &res0))) | 931 | if ((error = getaddrinfo(host, port, &hints, &res0))) |
| 925 | errx(1, "getaddrinfo for host \"%s\" port %s: %s", host, | 932 | errx(1, "getaddrinfo for host \"%s\" port %s: %s", host, |
| @@ -952,11 +959,26 @@ remote_connect(const char *host, const char *port, struct addrinfo hints) | |||
| 952 | 959 | ||
| 953 | set_common_sockopts(s, res->ai_family); | 960 | set_common_sockopts(s, res->ai_family); |
| 954 | 961 | ||
| 962 | if ((herr = getnameinfo(res->ai_addr, res->ai_addrlen, ipaddr, | ||
| 963 | NI_MAXHOST, NULL, 0, NI_NUMERICHOST)) != 0) { | ||
| 964 | if (herr == EAI_SYSTEM) | ||
| 965 | err(1, "getnameinfo"); | ||
| 966 | else | ||
| 967 | errx(1, "getnameinfo: %s", gai_strerror(herr)); | ||
| 968 | } | ||
| 969 | |||
| 955 | if (timeout_connect(s, res->ai_addr, res->ai_addrlen) == 0) | 970 | if (timeout_connect(s, res->ai_addr, res->ai_addrlen) == 0) |
| 956 | break; | 971 | break; |
| 957 | if (vflag) | 972 | |
| 958 | warn("connect to %s port %s (%s) failed", host, port, | 973 | if (vflag) { |
| 959 | uflag ? "udp" : "tcp"); | 974 | /* only print IP if there is something to report */ |
| 975 | if (nflag || (strncmp(host, ipaddr, NI_MAXHOST) == 0)) | ||
| 976 | warn("connect to %s port %s (%s) failed", host, | ||
| 977 | port, uflag ? "udp" : "tcp"); | ||
| 978 | else | ||
| 979 | warn("connect to %s (%s) port %s (%s) failed", | ||
| 980 | host, ipaddr, port, uflag ? "udp" : "tcp"); | ||
| 981 | } | ||
| 960 | 982 | ||
| 961 | save_errno = errno; | 983 | save_errno = errno; |
| 962 | close(s); | 984 | close(s); |
