forked from stealth/sshttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.cc
285 lines (241 loc) · 6.26 KB
/
socket.cc
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* Copyright (C) 2001-2014 Sebastian Krahmer.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Sebastian Krahmer.
* 4. The name Sebastian Krahmer may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <string.h>
#include <string>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <stdint.h>
#include "socket.h"
#include "config.h"
namespace NS_Socket {
#ifndef IP_TRANSPARENT
const int IP_TRANSPARENT = 19;
#endif
#ifndef IPV6_TRANSPARENT
const int IPV6_TRANSPARENT = 75;
#endif
using namespace std;
string error;
const char *why()
{
return error.c_str();
}
int nodelay(int sock)
{
int one = 1;
socklen_t len = sizeof(one);
if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &one, len) < 0) {
error = "NS_Socket::nodelay::setsockopt: ";
error += strerror(errno);
return -1;
}
return 0;
}
int transparent(int af, int sock)
{
int one = 1, level = SOL_IP, op = IP_TRANSPARENT;
if (af == AF_INET6) {
level = SOL_IPV6;
op = IPV6_TRANSPARENT;
}
if (setsockopt(sock, level, op, &one, sizeof(one)) < 0) {
error = "NS_Socket::transparent::setsockopt:";
error += strerror(errno);
return -1;
}
return 0;
}
int reuse(int sock)
{
int one = 1;
socklen_t len = sizeof(one);
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, len) < 0) {
error = "NS_Socket::reuse::setsockopt: ";
error += strerror(errno);
return -1;
}
return 0;
}
#ifdef FREEBSD
#define LINUX22
#endif
int dstaddr(int sock, sockaddr *dst, socklen_t dlen)
{
if (!dst) {
error = "NS_Socket::dstaddr: dst == NULL";
return -1;
}
#ifdef LINUX22
if (getsockname(sock, dst, &dlen) < 0) {
error = "NS_Socket::dstaddr::getsockname: ";
error += strerror(errno);
return -1;
}
#elif defined(LINUX24) || defined(LINUX26)
#include <linux/netfilter_ipv4.h>
if (dlen == sizeof(sockaddr_in)) {
if (getsockopt(sock, SOL_IP, SO_ORIGINAL_DST, dst, &dlen) < 0) {
error = "NS_Socket::dstaddr::getsockopt:";
error += strerror(errno);
return -1;
}
} else {
#include <linux/netfilter_ipv6.h>
#ifndef IP6T_SO_ORIGINAL_DST
#define IP6T_SO_ORIGINAL_DST 80
#endif
if (getsockopt(sock, SOL_IPV6, IP6T_SO_ORIGINAL_DST, dst, &dlen) < 0) {
error = "NS_Socket::dstaddr::getsockopt:";
error += strerror(errno);
return -1;
}
}
#else
#error "Not supported on this OS yet."
#endif
return 0;
}
int bind_local(int sock, const struct sockaddr *s, socklen_t slen, bool do_listen)
{
if (reuse(sock) < 0)
return -1;
if (bind(sock, s, slen) < 0) {
error = "NS_Socket::bind_local::bind:";
error += strerror(errno);
return -1;
}
if (do_listen) {
if (listen(sock, 10000) < 0) {
if (listen(sock, SOMAXCONN) < 0) {
error = "NS_Socket::bind_local::listen:";
error += strerror(errno);
return -1;
}
}
}
return 0;
}
int tcp_connect_nb(const struct sockaddr *to, socklen_t tolen, const struct sockaddr *from,
socklen_t flen, bool make_transparent)
{
int af = AF_INET;
if (tolen == sizeof(sockaddr_in6))
af = AF_INET6;
int sock = socket(af, SOCK_STREAM, 0);
if (sock < 0) {
error = "NS_Socket::tcp_connect_nb::socket:";
error += strerror(errno);
return -1;
}
if (fcntl(sock, F_SETFL, O_RDWR|O_NONBLOCK) < 0) {
error = "NS_Socket::tcp_connect_nb::fcntl:";
error += strerror(errno);
close(sock);
return -1;
}
if (af == AF_INET) {
if (((sockaddr_in *)from)->sin_port != 0) {
if (make_transparent && transparent(af, sock) < 0) {
error = NS_Socket::why();
close(sock);
return -1;
}
if (bind_local(sock, from, flen, 0) < 0) {
close(sock);
return -1;
}
}
} else if (((sockaddr_in6 *)from)->sin6_port != 0) {
if (make_transparent && transparent(af, sock) < 0) {
error = NS_Socket::why();
close(sock);
return -1;
}
if (bind_local(sock, from, flen, 0) < 0) {
close(sock);
return -1;
}
}
if (connect(sock, to, tolen) < 0 && errno != EINPROGRESS) {
close(sock);
error = "NS_Socket::tcp_connect_nb::connect:";
error += strerror(errno);
return -1;
}
return sock;
}
int finish_connecting(int fd)
{
int e = 0;
socklen_t len = sizeof(error);
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &e, &len) < 0 || e < 0) {
error = "NS_Socket::finish_connecting::getsockopt:";
error += strerror(errno);
return -1;
}
return nodelay(fd);
}
int readn(int fd, void *buf, size_t len)
{
int o = 0, n;
char *ptr = (char*)buf;
while (len > 0) {
if ((n = read(fd, ptr+o, len)) <= 0)
return n;
len -= n;
o += n;
}
return o;
}
int writen(int fd, const void *buf, size_t len)
{
int o = 0, n;
char *ptr = (char*)buf;
while (len > 0) {
if ((n = write(fd, ptr + o, len)) <= 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK)
return o;
return n;
}
len -= n;
o += n;
}
return o;
}
} // namespace