diff options
author | avsm <> | 2005-05-24 20:13:28 +0000 |
---|---|---|
committer | avsm <> | 2005-05-24 20:13:28 +0000 |
commit | a3374fefe00db04f7c91ec3205b912b87f2cbe18 (patch) | |
tree | fc8ed8403633c4027bd5b04b34482aee974a4dc3 | |
parent | 33ce8b8d9887f1d1e3b1e1b2673b810adf2fa717 (diff) | |
download | openbsd-a3374fefe00db04f7c91ec3205b912b87f2cbe18.tar.gz openbsd-a3374fefe00db04f7c91ec3205b912b87f2cbe18.tar.bz2 openbsd-a3374fefe00db04f7c91ec3205b912b87f2cbe18.zip |
Switch atomicio to a simpler interface which returns size_t and uses
0 to signal errors. should be no functional change in nc apart from
different error messages.
"groovy", said deraadt@
-rw-r--r-- | src/usr.bin/nc/atomicio.c | 29 | ||||
-rw-r--r-- | src/usr.bin/nc/atomicio.h | 33 | ||||
-rw-r--r-- | src/usr.bin/nc/netcat.c | 17 | ||||
-rw-r--r-- | src/usr.bin/nc/socks.c | 54 |
4 files changed, 78 insertions, 55 deletions
diff --git a/src/usr.bin/nc/atomicio.c b/src/usr.bin/nc/atomicio.c index 151dde0cf6..545bbeee94 100644 --- a/src/usr.bin/nc/atomicio.c +++ b/src/usr.bin/nc/atomicio.c | |||
@@ -1,4 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright (c) 2005 Anil Madhavapeddy. All rights served. | ||
2 | * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved. | 3 | * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved. |
3 | * All rights reserved. | 4 | * All rights reserved. |
4 | * | 5 | * |
@@ -21,36 +22,42 @@ | |||
21 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | * $OpenBSD: atomicio.c,v 1.6 2005/05/24 20:13:28 avsm Exp $ | ||
24 | */ | 26 | */ |
25 | 27 | ||
26 | #include <sys/types.h> | 28 | #include <sys/types.h> |
27 | #include <sys/uio.h> | 29 | #include <sys/uio.h> |
28 | |||
29 | #include <errno.h> | 30 | #include <errno.h> |
30 | #include <unistd.h> | 31 | #include <unistd.h> |
31 | 32 | #include "atomicio.h" | |
32 | ssize_t atomicio(ssize_t (*f)(int, void *, size_t), int fd, void *_s, size_t n); | ||
33 | 33 | ||
34 | /* | 34 | /* |
35 | * ensure all of data on socket comes through. f==read || f==write | 35 | * ensure all of data on socket comes through. f==read || f==vwrite |
36 | */ | 36 | */ |
37 | ssize_t | 37 | size_t |
38 | atomicio(ssize_t (*f) (int, void *, size_t), int fd, void *_s, size_t n) | 38 | atomicio(f, fd, _s, n) |
39 | ssize_t (*f) (int, void *, size_t); | ||
40 | int fd; | ||
41 | void *_s; | ||
42 | size_t n; | ||
39 | { | 43 | { |
40 | char *s = _s; | 44 | char *s = _s; |
41 | ssize_t res, pos = 0; | 45 | size_t pos = 0; |
46 | ssize_t res; | ||
42 | 47 | ||
43 | while (n > (size_t)pos) { | 48 | while (n > pos) { |
44 | res = (f) (fd, s + pos, n - pos); | 49 | res = (f) (fd, s + pos, n - pos); |
45 | switch (res) { | 50 | switch (res) { |
46 | case -1: | 51 | case -1: |
47 | if (errno == EINTR || errno == EAGAIN) | 52 | if (errno == EINTR || errno == EAGAIN) |
48 | continue; | 53 | continue; |
54 | return 0; | ||
49 | case 0: | 55 | case 0: |
50 | return (res); | 56 | errno = EPIPE; |
57 | return pos; | ||
51 | default: | 58 | default: |
52 | pos += res; | 59 | pos += (u_int)res; |
53 | } | 60 | } |
54 | } | 61 | } |
55 | return (pos); | 62 | return pos; |
56 | } | 63 | } |
diff --git a/src/usr.bin/nc/atomicio.h b/src/usr.bin/nc/atomicio.h new file mode 100644 index 0000000000..551a0565da --- /dev/null +++ b/src/usr.bin/nc/atomicio.h | |||
@@ -0,0 +1,33 @@ | |||
1 | /* $OpenBSD: atomicio.h,v 1.1 2005/05/24 20:13:28 avsm Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved. | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | /* | ||
29 | * Ensure all of data on socket comes through. f==read || f==vwrite | ||
30 | */ | ||
31 | size_t atomicio(ssize_t (*)(int, void *, size_t), int, void *, size_t); | ||
32 | |||
33 | #define vwrite (ssize_t (*)(int, void *, size_t))write | ||
diff --git a/src/usr.bin/nc/netcat.c b/src/usr.bin/nc/netcat.c index 88dde5290b..36d133a2bb 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.78 2005/04/10 19:43:34 otto Exp $ */ | 1 | /* $OpenBSD: netcat.c,v 1.79 2005/05/24 20:13:28 avsm Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> | 3 | * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> |
4 | * | 4 | * |
@@ -50,6 +50,7 @@ | |||
50 | #include <string.h> | 50 | #include <string.h> |
51 | #include <unistd.h> | 51 | #include <unistd.h> |
52 | #include <fcntl.h> | 52 | #include <fcntl.h> |
53 | #include "atomicio.h" | ||
53 | 54 | ||
54 | #ifndef SUN_LEN | 55 | #ifndef SUN_LEN |
55 | #define SUN_LEN(su) \ | 56 | #define SUN_LEN(su) \ |
@@ -80,7 +81,6 @@ int timeout = -1; | |||
80 | int family = AF_UNSPEC; | 81 | int family = AF_UNSPEC; |
81 | char *portlist[PORT_MAX+1]; | 82 | char *portlist[PORT_MAX+1]; |
82 | 83 | ||
83 | ssize_t atomicio(ssize_t (*)(int, void *, size_t), int, void *, size_t); | ||
84 | void atelnet(int, unsigned char *, unsigned int); | 84 | void atelnet(int, unsigned char *, unsigned int); |
85 | void build_ports(char *); | 85 | void build_ports(char *); |
86 | void help(void); | 86 | void help(void); |
@@ -590,7 +590,7 @@ readwrite(int nfd) | |||
590 | { | 590 | { |
591 | struct pollfd pfd[2]; | 591 | struct pollfd pfd[2]; |
592 | unsigned char buf[BUFSIZ]; | 592 | unsigned char buf[BUFSIZ]; |
593 | int wfd = fileno(stdin), n; | 593 | int n, wfd = fileno(stdin); |
594 | int lfd = fileno(stdout); | 594 | int lfd = fileno(stdout); |
595 | 595 | ||
596 | /* Setup Network FD */ | 596 | /* Setup Network FD */ |
@@ -623,8 +623,7 @@ readwrite(int nfd) | |||
623 | } else { | 623 | } else { |
624 | if (tflag) | 624 | if (tflag) |
625 | atelnet(nfd, buf, n); | 625 | atelnet(nfd, buf, n); |
626 | if (atomicio((ssize_t (*)(int, void *, size_t))write, | 626 | if (atomicio(vwrite, lfd, buf, n) != n) |
627 | lfd, buf, n) != n) | ||
628 | return; | 627 | return; |
629 | } | 628 | } |
630 | } | 629 | } |
@@ -637,8 +636,7 @@ readwrite(int nfd) | |||
637 | pfd[1].fd = -1; | 636 | pfd[1].fd = -1; |
638 | pfd[1].events = 0; | 637 | pfd[1].events = 0; |
639 | } else { | 638 | } else { |
640 | if (atomicio((ssize_t (*)(int, void *, size_t))write, | 639 | if (atomicio(vwrite, nfd, buf, n) != n) |
641 | nfd, buf, n) != n) | ||
642 | return; | 640 | return; |
643 | } | 641 | } |
644 | } | 642 | } |
@@ -669,9 +667,8 @@ atelnet(int nfd, unsigned char *buf, unsigned int size) | |||
669 | p++; | 667 | p++; |
670 | obuf[2] = *p; | 668 | obuf[2] = *p; |
671 | obuf[3] = '\0'; | 669 | obuf[3] = '\0'; |
672 | if (atomicio((ssize_t (*)(int, void *, size_t))write, | 670 | if (atomicio(vwrite, nfd, obuf, 3) != 3) |
673 | nfd, obuf, 3) != 3) | 671 | warn("Write Error!"); |
674 | warnx("Write Error!"); | ||
675 | obuf[0] = '\0'; | 672 | obuf[0] = '\0'; |
676 | } | 673 | } |
677 | } | 674 | } |
diff --git a/src/usr.bin/nc/socks.c b/src/usr.bin/nc/socks.c index 7380b7999d..de61439400 100644 --- a/src/usr.bin/nc/socks.c +++ b/src/usr.bin/nc/socks.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: socks.c,v 1.14 2005/05/20 22:46:08 djm Exp $ */ | 1 | /* $OpenBSD: socks.c,v 1.15 2005/05/24 20:13:28 avsm Exp $ */ |
2 | 2 | ||
3 | /* | 3 | /* |
4 | * Copyright (c) 1999 Niklas Hallqvist. All rights reserved. | 4 | * Copyright (c) 1999 Niklas Hallqvist. All rights reserved. |
@@ -37,6 +37,7 @@ | |||
37 | #include <stdlib.h> | 37 | #include <stdlib.h> |
38 | #include <string.h> | 38 | #include <string.h> |
39 | #include <unistd.h> | 39 | #include <unistd.h> |
40 | #include "atomicio.h" | ||
40 | 41 | ||
41 | #define SOCKS_PORT "1080" | 42 | #define SOCKS_PORT "1080" |
42 | #define HTTP_PROXY_PORT "3128" | 43 | #define HTTP_PROXY_PORT "3128" |
@@ -50,7 +51,6 @@ | |||
50 | #define SOCKS_DOMAIN 3 | 51 | #define SOCKS_DOMAIN 3 |
51 | #define SOCKS_IPV6 4 | 52 | #define SOCKS_IPV6 4 |
52 | 53 | ||
53 | ssize_t atomicio(ssize_t (*)(int, void *, size_t), int, void *, size_t); | ||
54 | int remote_connect(const char *, const char *, struct addrinfo); | 54 | int remote_connect(const char *, const char *, struct addrinfo); |
55 | int socks_connect(const char *host, const char *port, struct addrinfo hints, | 55 | int socks_connect(const char *host, const char *port, struct addrinfo hints, |
56 | const char *proxyhost, const char *proxyport, struct addrinfo proxyhints, | 56 | const char *proxyhost, const char *proxyport, struct addrinfo proxyhints, |
@@ -86,18 +86,15 @@ decode_addrport(const char *h, const char *p, struct sockaddr *addr, | |||
86 | } | 86 | } |
87 | 87 | ||
88 | static int | 88 | static int |
89 | proxy_read_line(int fd, char *buf, int bufsz) | 89 | proxy_read_line(int fd, char *buf, size_t bufsz) |
90 | { | 90 | { |
91 | int r, off; | 91 | size_t off; |
92 | 92 | ||
93 | for(off = 0;;) { | 93 | for(off = 0;;) { |
94 | if (off >= bufsz) | 94 | if (off >= bufsz) |
95 | errx(1, "proxy read too long"); | 95 | errx(1, "proxy read too long"); |
96 | if ((r = read(fd, buf + off, 1)) <= 0) { | 96 | if (atomicio(read, fd, buf + off, 1) != 1) |
97 | if (r == -1 && errno == EINTR) | ||
98 | continue; | ||
99 | err(1, "proxy read"); | 97 | err(1, "proxy read"); |
100 | } | ||
101 | /* Skip CR */ | 98 | /* Skip CR */ |
102 | if (buf[off] == '\r') | 99 | if (buf[off] == '\r') |
103 | continue; | 100 | continue; |
@@ -119,7 +116,7 @@ socks_connect(const char *host, const char *port, | |||
119 | int proxyfd, r; | 116 | int proxyfd, r; |
120 | size_t hlen, wlen; | 117 | size_t hlen, wlen; |
121 | unsigned char buf[1024]; | 118 | unsigned char buf[1024]; |
122 | ssize_t cnt; | 119 | size_t cnt; |
123 | struct sockaddr_storage addr; | 120 | struct sockaddr_storage addr; |
124 | struct sockaddr_in *in4 = (struct sockaddr_in *)&addr; | 121 | struct sockaddr_in *in4 = (struct sockaddr_in *)&addr; |
125 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&addr; | 122 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&addr; |
@@ -148,13 +145,14 @@ socks_connect(const char *host, const char *port, | |||
148 | buf[0] = SOCKS_V5; | 145 | buf[0] = SOCKS_V5; |
149 | buf[1] = 1; | 146 | buf[1] = 1; |
150 | buf[2] = SOCKS_NOAUTH; | 147 | buf[2] = SOCKS_NOAUTH; |
151 | cnt = write(proxyfd, buf, 3); | 148 | cnt = atomicio(vwrite, proxyfd, buf, 3); |
152 | if (cnt == -1) | ||
153 | err(1, "write failed"); | ||
154 | if (cnt != 3) | 149 | if (cnt != 3) |
155 | errx(1, "short write, %d (expected 3)", cnt); | 150 | err(1, "write failed (%d/3)", cnt); |
151 | |||
152 | cnt = atomicio(read, proxyfd, buf, 2); | ||
153 | if (cnt != 2) | ||
154 | err(1, "read failed (%d/3)", cnt); | ||
156 | 155 | ||
157 | read(proxyfd, buf, 2); | ||
158 | if (buf[1] == SOCKS_NOMETHOD) | 156 | if (buf[1] == SOCKS_NOMETHOD) |
159 | errx(1, "authentication method negotiation failed"); | 157 | errx(1, "authentication method negotiation failed"); |
160 | 158 | ||
@@ -200,18 +198,13 @@ socks_connect(const char *host, const char *port, | |||
200 | errx(1, "internal error: silly AF"); | 198 | errx(1, "internal error: silly AF"); |
201 | } | 199 | } |
202 | 200 | ||
203 | cnt = atomicio((ssize_t (*)(int, void *, size_t))write, | 201 | cnt = atomicio(vwrite, proxyfd, buf, wlen); |
204 | proxyfd, buf, wlen); | ||
205 | if (cnt == -1) | ||
206 | err(1, "write failed"); | ||
207 | if (cnt != wlen) | 202 | if (cnt != wlen) |
208 | errx(1, "short write, %d (expected %d)", cnt, wlen); | 203 | err(1, "write failed (%d/%d)", cnt, wlen); |
209 | 204 | ||
210 | cnt = atomicio(read, proxyfd, buf, 10); | 205 | cnt = atomicio(read, proxyfd, buf, 10); |
211 | if (cnt == -1) | ||
212 | err(1, "read failed"); | ||
213 | if (cnt != 10) | 206 | if (cnt != 10) |
214 | errx(1, "unexpected reply size %d (expected 10)", cnt); | 207 | err(1, "read failed (%d/10)", cnt); |
215 | if (buf[1] != 0) | 208 | if (buf[1] != 0) |
216 | errx(1, "connection failed, SOCKS error %d", buf[1]); | 209 | errx(1, "connection failed, SOCKS error %d", buf[1]); |
217 | } else if (socksv == 4) { | 210 | } else if (socksv == 4) { |
@@ -227,17 +220,13 @@ socks_connect(const char *host, const char *port, | |||
227 | buf[8] = 0; /* empty username */ | 220 | buf[8] = 0; /* empty username */ |
228 | wlen = 9; | 221 | wlen = 9; |
229 | 222 | ||
230 | cnt = write(proxyfd, buf, wlen); | 223 | cnt = atomicio(vwrite, proxyfd, buf, wlen); |
231 | if (cnt == -1) | ||
232 | err(1, "write failed"); | ||
233 | if (cnt != wlen) | 224 | if (cnt != wlen) |
234 | errx(1, "short write, %d (expected %d)", cnt, wlen); | 225 | err(1, "write failed (%d/%d)", cnt, wlen); |
235 | 226 | ||
236 | cnt = atomicio(read, proxyfd, buf, 8); | 227 | cnt = atomicio(read, proxyfd, buf, 8); |
237 | if (cnt == -1) | ||
238 | err(1, "read failed"); | ||
239 | if (cnt != 8) | 228 | if (cnt != 8) |
240 | errx(1, "unexpected reply size %d (expected 8)", cnt); | 229 | err(1, "read failed (%d/8)", cnt); |
241 | if (buf[1] != 90) | 230 | if (buf[1] != 90) |
242 | errx(1, "connection failed, SOCKS error %d", buf[1]); | 231 | errx(1, "connection failed, SOCKS error %d", buf[1]); |
243 | } else if (socksv == -1) { | 232 | } else if (socksv == -1) { |
@@ -261,12 +250,9 @@ socks_connect(const char *host, const char *port, | |||
261 | errx(1, "hostname too long"); | 250 | errx(1, "hostname too long"); |
262 | r = strlen(buf); | 251 | r = strlen(buf); |
263 | 252 | ||
264 | cnt = atomicio((ssize_t (*)(int, void *, size_t))write, | 253 | cnt = atomicio(vwrite, proxyfd, buf, r); |
265 | proxyfd, buf, r); | ||
266 | if (cnt == -1) | ||
267 | err(1, "write failed"); | ||
268 | if (cnt != r) | 254 | if (cnt != r) |
269 | errx(1, "short write, %d (expected %d)", cnt, r); | 255 | err(1, "write failed (%d/%d)", cnt, r); |
270 | 256 | ||
271 | /* Read reply */ | 257 | /* Read reply */ |
272 | for (r = 0; r < HTTP_MAXHDRS; r++) { | 258 | for (r = 0; r < HTTP_MAXHDRS; r++) { |