summaryrefslogtreecommitdiff
blob: e1a462e746b7c33259aa26082f09edc3eec4fd83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
Allow compilation on Darwin

diff --git a/netcat.c b/netcat.c
index a0fb51b..bbb5dd1 100644
--- a/netcat.c
+++ b/netcat.c
@@ -51,11 +51,13 @@
 # include <bsd/readpassphrase.h>
 #endif
 
+#ifndef IPTOS_LOWCOST
+# define IPTOS_LOWCOST 0x02
+#endif
 #ifndef IPTOS_LOWDELAY
 # define IPTOS_LOWDELAY 0x10
 # define IPTOS_THROUGHPUT 0x08
 # define IPTOS_RELIABILITY 0x04
-# define IPTOS_LOWCOST 0x02
 # define IPTOS_MINCOST IPTOS_LOWCOST
 #endif /* IPTOS_LOWDELAY */
 
@@ -108,8 +110,9 @@
 # include <tls.h>
 #endif
 #include <unistd.h>
-#include <bsd/stdlib.h>
-#include <bsd/string.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
 
 #include "atomicio.h"
 
@@ -240,6 +243,43 @@ static int connect_with_timeout(int fd, const struct sockaddr *sa,
 
 static void quit();
 
+static char* strtonumerrs[] = {
+	"too large",
+	"too small",
+	"invalid"
+};
+
+static long long
+strtonum(
+		const char *nptr,
+		long long minval,
+		long long maxval,
+		const char **errstr)
+{
+	long long val;
+
+	while (*nptr != '\0' && isspace(*nptr))
+		nptr++;
+	if (*nptr == '\0') {
+		if (errstr != NULL)
+			*errstr = strtonumerrs[2];
+		return 0;
+	}
+	val = atoll(nptr);
+	if (val < minval) {
+		if (errstr != NULL)
+			*errstr = strtonumerrs[1];
+		return 0;
+	}
+	if (val > maxval) {
+		if (errstr != NULL)
+			*errstr = strtonumerrs[0];
+		return 0;
+	}
+	*errstr = NULL;
+	return val;
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -814,9 +814,8 @@
 # endif
 			} else {
 				len = sizeof(cliaddr);
-				connfd = accept4(s, (struct sockaddr *)&cliaddr,
-				    &len, SOCK_NONBLOCK);
-				if (connfd == -1) {
+				connfd = accept(s, (struct sockaddr *)&cliaddr, &len);
+				if (connfd == -1 || fcntl(connfd, F_SETFL, O_NONBLOCK) == -1) {
 					/* For now, all errnos are fatal */
 					err(1, "accept");
 				}
@@ -1092,14 +1091,16 @@
 	int s, save_errno;
 
 	if (uflag) {
-		if ((s = unix_bind(unix_dg_tmp_socket, SOCK_CLOEXEC)) < 0)
+		if ((s = unix_bind(unix_dg_tmp_socket, 0)) < 0)
 			return -1;
 	} else {
-		if ((s = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) < 0) {
+		if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
 			errx(1,"create unix socket failed");
 			return -1;
 		}
 	}
+	if (fcntl(s, F_SETFD, FD_CLOEXEC) == -1)
+		return -1;
 
 	memset(&s_un, 0, sizeof(struct sockaddr_un));
 	s_un.sun_family = AF_UNIX;
@@ -1174,9 +1175,13 @@
 		    port, gai_strerror(error));
 
 	for (res = res0; res; res = res->ai_next) {
-		if ((s = socket(res->ai_family, res->ai_socktype |
-		    SOCK_NONBLOCK, res->ai_protocol)) < 0)
+		if ((s = socket(res->ai_family, res->ai_socktype,
+		    res->ai_protocol)) < 0)
+			continue;
+		if (fcntl(s, F_SETFL, O_NONBLOCK) == -1) {
+			close(s);
 			continue;
+		}
 
 		/* Bind to a local port or source address if specified. */
 		if (sflag || pflag) {
diff --git a/socks.c b/socks.c
index 9068f39..68b68e3 100644
--- a/socks.c
+++ b/socks.c
@@ -38,7 +38,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <resolv.h>
-#include <bsd/readpassphrase.h>
+#include <readpassphrase.h>
 #include "atomicio.h"
 
 #define SOCKS_PORT	"1080"