summaryrefslogtreecommitdiff
path: root/src/usr.bin/nc/socks.c
diff options
context:
space:
mode:
authoravsm <>2005-05-24 20:13:28 +0000
committeravsm <>2005-05-24 20:13:28 +0000
commita3374fefe00db04f7c91ec3205b912b87f2cbe18 (patch)
treefc8ed8403633c4027bd5b04b34482aee974a4dc3 /src/usr.bin/nc/socks.c
parent33ce8b8d9887f1d1e3b1e1b2673b810adf2fa717 (diff)
downloadopenbsd-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@
Diffstat (limited to 'src/usr.bin/nc/socks.c')
-rw-r--r--src/usr.bin/nc/socks.c54
1 files changed, 20 insertions, 34 deletions
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
53ssize_t atomicio(ssize_t (*)(int, void *, size_t), int, void *, size_t);
54int remote_connect(const char *, const char *, struct addrinfo); 54int remote_connect(const char *, const char *, struct addrinfo);
55int socks_connect(const char *host, const char *port, struct addrinfo hints, 55int 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
88static int 88static int
89proxy_read_line(int fd, char *buf, int bufsz) 89proxy_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++) {