diff options
Diffstat (limited to 'regress/netcat.c')
-rw-r--r-- | regress/netcat.c | 38 |
1 files changed, 25 insertions, 13 deletions
diff --git a/regress/netcat.c b/regress/netcat.c index fe94dd908..d583176f1 100644 --- a/regress/netcat.c +++ b/regress/netcat.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: netcat.c,v 1.126 2014/10/30 16:08:31 tedu Exp $ */ | 1 | /* $OpenBSD: netcat.c,v 1.131 2015/09/03 23:06:28 sobrado Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> | 3 | * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> |
4 | * | 4 | * |
@@ -44,14 +44,15 @@ | |||
44 | #include <netinet/ip.h> | 44 | #include <netinet/ip.h> |
45 | 45 | ||
46 | #include <errno.h> | 46 | #include <errno.h> |
47 | #include <fcntl.h> | ||
48 | #include <limits.h> | ||
47 | #include <netdb.h> | 49 | #include <netdb.h> |
50 | #include <signal.h> | ||
48 | #include <stdarg.h> | 51 | #include <stdarg.h> |
49 | #include <stdio.h> | 52 | #include <stdio.h> |
50 | #include <stdlib.h> | 53 | #include <stdlib.h> |
51 | #include <string.h> | 54 | #include <string.h> |
52 | #include <unistd.h> | 55 | #include <unistd.h> |
53 | #include <fcntl.h> | ||
54 | #include <limits.h> | ||
55 | #include "atomicio.h" | 56 | #include "atomicio.h" |
56 | 57 | ||
57 | #ifdef HAVE_POLL_H | 58 | #ifdef HAVE_POLL_H |
@@ -133,7 +134,7 @@ int udptest(int); | |||
133 | int unix_bind(char *); | 134 | int unix_bind(char *); |
134 | int unix_connect(char *); | 135 | int unix_connect(char *); |
135 | int unix_listen(char *); | 136 | int unix_listen(char *); |
136 | void set_common_sockopts(int); | 137 | void set_common_sockopts(int, int); |
137 | int map_tos(char *, int *); | 138 | int map_tos(char *, int *); |
138 | void report_connect(const struct sockaddr *, socklen_t); | 139 | void report_connect(const struct sockaddr *, socklen_t); |
139 | void usage(int); | 140 | void usage(int); |
@@ -162,6 +163,8 @@ main(int argc, char *argv[]) | |||
162 | uport = NULL; | 163 | uport = NULL; |
163 | sv = NULL; | 164 | sv = NULL; |
164 | 165 | ||
166 | signal(SIGPIPE, SIG_IGN); | ||
167 | |||
165 | while ((ch = getopt(argc, argv, | 168 | while ((ch = getopt(argc, argv, |
166 | "46DdFhI:i:klNnO:P:p:rSs:tT:UuV:vw:X:x:z")) != -1) { | 169 | "46DdFhI:i:klNnO:P:p:rSs:tT:UuV:vw:X:x:z")) != -1) { |
167 | switch (ch) { | 170 | switch (ch) { |
@@ -648,7 +651,7 @@ remote_connect(const char *host, const char *port, struct addrinfo hints) | |||
648 | freeaddrinfo(ares); | 651 | freeaddrinfo(ares); |
649 | } | 652 | } |
650 | 653 | ||
651 | set_common_sockopts(s); | 654 | set_common_sockopts(s, res0->ai_family); |
652 | 655 | ||
653 | if (timeout_connect(s, res0->ai_addr, res0->ai_addrlen) == 0) | 656 | if (timeout_connect(s, res0->ai_addr, res0->ai_addrlen) == 0) |
654 | break; | 657 | break; |
@@ -748,7 +751,7 @@ local_listen(char *host, char *port, struct addrinfo hints) | |||
748 | if (ret == -1) | 751 | if (ret == -1) |
749 | err(1, "setsockopt SO_REUSEADDR"); | 752 | err(1, "setsockopt SO_REUSEADDR"); |
750 | #endif | 753 | #endif |
751 | set_common_sockopts(s); | 754 | set_common_sockopts(s, res0->ai_family); |
752 | 755 | ||
753 | if (bind(s, (struct sockaddr *)res0->ai_addr, | 756 | if (bind(s, (struct sockaddr *)res0->ai_addr, |
754 | res0->ai_addrlen) == 0) | 757 | res0->ai_addrlen) == 0) |
@@ -1034,17 +1037,17 @@ fdpass(int nfd) | |||
1034 | 1037 | ||
1035 | bzero(&pfd, sizeof(pfd)); | 1038 | bzero(&pfd, sizeof(pfd)); |
1036 | pfd.fd = STDOUT_FILENO; | 1039 | pfd.fd = STDOUT_FILENO; |
1040 | pfd.events = POLLOUT; | ||
1037 | for (;;) { | 1041 | for (;;) { |
1038 | r = sendmsg(STDOUT_FILENO, &msg, 0); | 1042 | r = sendmsg(STDOUT_FILENO, &msg, 0); |
1039 | if (r == -1) { | 1043 | if (r == -1) { |
1040 | if (errno == EAGAIN || errno == EINTR) { | 1044 | if (errno == EAGAIN || errno == EINTR) { |
1041 | pfd.events = POLLOUT; | ||
1042 | if (poll(&pfd, 1, -1) == -1) | 1045 | if (poll(&pfd, 1, -1) == -1) |
1043 | err(1, "poll"); | 1046 | err(1, "poll"); |
1044 | continue; | 1047 | continue; |
1045 | } | 1048 | } |
1046 | err(1, "sendmsg"); | 1049 | err(1, "sendmsg"); |
1047 | } else if (r == -1) | 1050 | } else if (r != 1) |
1048 | errx(1, "sendmsg: unexpected return value %zd", r); | 1051 | errx(1, "sendmsg: unexpected return value %zd", r); |
1049 | else | 1052 | else |
1050 | break; | 1053 | break; |
@@ -1168,7 +1171,7 @@ udptest(int s) | |||
1168 | } | 1171 | } |
1169 | 1172 | ||
1170 | void | 1173 | void |
1171 | set_common_sockopts(int s) | 1174 | set_common_sockopts(int s, int af) |
1172 | { | 1175 | { |
1173 | int x = 1; | 1176 | int x = 1; |
1174 | 1177 | ||
@@ -1184,10 +1187,19 @@ set_common_sockopts(int s) | |||
1184 | &x, sizeof(x)) == -1) | 1187 | &x, sizeof(x)) == -1) |
1185 | err(1, "setsockopt"); | 1188 | err(1, "setsockopt"); |
1186 | } | 1189 | } |
1187 | #ifdef IP_TOS | 1190 | #if defined(IP_TOS) && defined(IPV6_TCLASS) |
1188 | if (Tflag != -1) { | 1191 | if (Tflag != -1) { |
1189 | if (setsockopt(s, IPPROTO_IP, IP_TOS, | 1192 | int proto, option; |
1190 | &Tflag, sizeof(Tflag)) == -1) | 1193 | |
1194 | if (af == AF_INET6) { | ||
1195 | proto = IPPROTO_IPV6; | ||
1196 | option = IPV6_TCLASS; | ||
1197 | } else { | ||
1198 | proto = IPPROTO_IP; | ||
1199 | option = IP_TOS; | ||
1200 | } | ||
1201 | |||
1202 | if (setsockopt(s, proto, option, &Tflag, sizeof(Tflag)) == -1) | ||
1191 | err(1, "set IP ToS"); | 1203 | err(1, "set IP ToS"); |
1192 | } | 1204 | } |
1193 | #endif | 1205 | #endif |
@@ -1321,7 +1333,7 @@ usage(int ret) | |||
1321 | { | 1333 | { |
1322 | fprintf(stderr, | 1334 | fprintf(stderr, |
1323 | "usage: nc [-46DdFhklNnrStUuvz] [-I length] [-i interval] [-O length]\n" | 1335 | "usage: nc [-46DdFhklNnrStUuvz] [-I length] [-i interval] [-O length]\n" |
1324 | "\t [-P proxy_username] [-p source_port] [-s source] [-T ToS]\n" | 1336 | "\t [-P proxy_username] [-p source_port] [-s source] [-T toskeyword]\n" |
1325 | "\t [-V rtable] [-w timeout] [-X proxy_protocol]\n" | 1337 | "\t [-V rtable] [-w timeout] [-X proxy_protocol]\n" |
1326 | "\t [-x proxy_address[:port]] [destination] [port]\n"); | 1338 | "\t [-x proxy_address[:port]] [destination] [port]\n"); |
1327 | if (ret) | 1339 | if (ret) |