forked from stealth/sshttp
/
socket.cc
239 lines (204 loc) · 5.46 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
/*
* Copyright (C) 2001-2010 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"
namespace NS_Socket {
#ifndef IP_TRANSPARENT
const int IP_TRANSPARENT = 19;
#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 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_in *dst)
{
if (!dst) {
error = "NS_Socket::dstaddr: dst == NULL";
return -1;
}
#ifdef LINUX22
socklen_t size = sizeof(sockaddr_in);
if (getsockname(sock, (struct sockaddr*)dst, &size) < 0) {
error = "NS_Socket::dstaddr::getsockname: ";
error += strerror(errno);
return -1;
}
#elif defined(LINUX24) || defined(LINUX26)
#include <linux/netfilter_ipv4.h>
socklen_t size = sizeof(sockaddr_in);
if (getsockopt(sock, SOL_IP, SO_ORIGINAL_DST, dst, &size) < 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_in &sin, bool do_listen)
{
if (reuse(sock) < 0)
return -1;
if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) < 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_in &to, const struct sockaddr_in &from, bool transparent)
{
int sock = socket(PF_INET, 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 (from.sin_port != 0) {
int one = 1;
#ifdef LINUX26
if (transparent && setsockopt(sock, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) < 0) {
#else
if (transparent && setsockopt(sock, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) < 0) {
#endif
error = "NS_Socket::tcp_connect_nb::setsockopt:";
error += strerror(errno);
close(sock);
return -1;
}
if (bind_local(sock, from, 0) < 0) {
close(sock);
return -1;
}
}
if (connect(sock, (struct sockaddr *)&to, sizeof(to)) < 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