diff options
author | tb <> | 2022-12-18 12:45:34 +0000 |
---|---|---|
committer | tb <> | 2022-12-18 12:45:34 +0000 |
commit | 30846c3c31588126f4e6da373b8f9427a7298c6a (patch) | |
tree | be1900a0c95bed127df2b72d389535807aa19030 /src/usr.bin/nc/netcat.c | |
parent | 9a8d30c38d1a72a7788bfd177ca9de16f652ccc0 (diff) | |
download | openbsd-30846c3c31588126f4e6da373b8f9427a7298c6a.tar.gz openbsd-30846c3c31588126f4e6da373b8f9427a7298c6a.tar.bz2 openbsd-30846c3c31588126f4e6da373b8f9427a7298c6a.zip |
nc: factor printing of connection info into a function
This simply moves a chunk of code in this spaghetti mess into its own
function with minimal changes.
idea from a diff by mpf
Diffstat (limited to 'src/usr.bin/nc/netcat.c')
-rw-r--r-- | src/usr.bin/nc/netcat.c | 53 |
1 files changed, 28 insertions, 25 deletions
diff --git a/src/usr.bin/nc/netcat.c b/src/usr.bin/nc/netcat.c index 4a26db6aec..e60527199f 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.219 2022/06/08 20:07:31 tb Exp $ */ | 1 | /* $OpenBSD: netcat.c,v 1.220 2022/12/18 12:45:34 tb 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. |
@@ -131,6 +131,7 @@ 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, |
132 | const char *, const char *, struct addrinfo, int, const char *); | 132 | const char *, const char *, struct addrinfo, int, const char *); |
133 | int udptest(int); | 133 | int udptest(int); |
134 | void connection_info(const char *, const char *, const char *); | ||
134 | int unix_bind(char *, int); | 135 | int unix_bind(char *, int); |
135 | int unix_connect(char *); | 136 | int unix_connect(char *); |
136 | int unix_listen(char *); | 137 | int unix_listen(char *); |
@@ -153,7 +154,6 @@ main(int argc, char *argv[]) | |||
153 | char *host, *uport; | 154 | char *host, *uport; |
154 | char ipaddr[NI_MAXHOST]; | 155 | char ipaddr[NI_MAXHOST]; |
155 | struct addrinfo hints; | 156 | struct addrinfo hints; |
156 | struct servent *sv; | ||
157 | socklen_t len; | 157 | socklen_t len; |
158 | struct sockaddr_storage cliaddr; | 158 | struct sockaddr_storage cliaddr; |
159 | char *proxy = NULL, *proxyport = NULL; | 159 | char *proxy = NULL, *proxyport = NULL; |
@@ -168,7 +168,6 @@ main(int argc, char *argv[]) | |||
168 | socksv = 5; | 168 | socksv = 5; |
169 | host = NULL; | 169 | host = NULL; |
170 | uport = NULL; | 170 | uport = NULL; |
171 | sv = NULL; | ||
172 | Rflag = tls_default_ca_cert_file(); | 171 | Rflag = tls_default_ca_cert_file(); |
173 | 172 | ||
174 | signal(SIGPIPE, SIG_IGN); | 173 | signal(SIGPIPE, SIG_IGN); |
@@ -709,28 +708,7 @@ main(int argc, char *argv[]) | |||
709 | } | 708 | } |
710 | } | 709 | } |
711 | 710 | ||
712 | /* Don't look up port if -n. */ | 711 | connection_info(host, portlist[i], ipaddr); |
713 | if (nflag) | ||
714 | sv = NULL; | ||
715 | else { | ||
716 | sv = getservbyport( | ||
717 | ntohs(atoi(portlist[i])), | ||
718 | uflag ? "udp" : "tcp"); | ||
719 | } | ||
720 | |||
721 | fprintf(stderr, "Connection to %s", host); | ||
722 | |||
723 | /* | ||
724 | * if we aren't connecting thru a proxy and | ||
725 | * there is something to report, print IP | ||
726 | */ | ||
727 | if (!nflag && !xflag && | ||
728 | strcmp(host, ipaddr) != 0) | ||
729 | fprintf(stderr, " (%s)", ipaddr); | ||
730 | |||
731 | fprintf(stderr, " %s port [%s/%s] succeeded!\n", | ||
732 | portlist[i], uflag ? "udp" : "tcp", | ||
733 | sv ? sv->s_name : "*"); | ||
734 | } | 712 | } |
735 | if (Fflag) | 713 | if (Fflag) |
736 | fdpass(s); | 714 | fdpass(s); |
@@ -1541,6 +1519,31 @@ udptest(int s) | |||
1541 | } | 1519 | } |
1542 | 1520 | ||
1543 | void | 1521 | void |
1522 | connection_info(const char *host, const char *port, const char *ipaddr) | ||
1523 | { | ||
1524 | struct servent *sv; | ||
1525 | |||
1526 | /* Don't look up port if -n. */ | ||
1527 | if (nflag) | ||
1528 | sv = NULL; | ||
1529 | else { | ||
1530 | sv = getservbyport(ntohs(atoi(port)), uflag ? "udp" : "tcp"); | ||
1531 | } | ||
1532 | |||
1533 | fprintf(stderr, "Connection to %s", host); | ||
1534 | |||
1535 | /* | ||
1536 | * if we aren't connecting thru a proxy and | ||
1537 | * there is something to report, print IP | ||
1538 | */ | ||
1539 | if (!nflag && !xflag && strcmp(host, ipaddr) != 0) | ||
1540 | fprintf(stderr, " (%s)", ipaddr); | ||
1541 | |||
1542 | fprintf(stderr, " %s port [%s/%s] succeeded!\n", | ||
1543 | port, uflag ? "udp" : "tcp", sv ? sv->s_name : "*"); | ||
1544 | } | ||
1545 | |||
1546 | void | ||
1544 | set_common_sockopts(int s, int af) | 1547 | set_common_sockopts(int s, int af) |
1545 | { | 1548 | { |
1546 | int x = 1; | 1549 | int x = 1; |