-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibsocks.c
More file actions
403 lines (304 loc) · 9.34 KB
/
libsocks.c
File metadata and controls
403 lines (304 loc) · 9.34 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/un.h>
#include <unistd.h>
#include "eintr_wrappers.h"
#include "libsocks.h"
/*----------------------------------------------------------------------------*/
/** @brief Serializes a uint16_t into a little-endian 2-char array. Used to
* ensure predictable serialization across platforms.
* @param[in] input Value to serialize
* @param[out] output Destination for serialized data */
static void serialize_uint16(uint16_t input, char output[2])
{
output[0] = (char)((input >> 0) & 0xFF);
output[1] = (char)((input >> 8) & 0xFF);
}
/** @brief Deserializes a little-endian 2-char array and returns the result.
* Used to ensure predictable deserialization across platforms.
* @param[in] input Serialized data
* @return Deserialized uint16 */
static uint16_t deserialize_uint16(const char input[2])
{
int result = 0;
result += (input[0]) << 0;
result += (input[1]) << 8;
return (uint16_t) (result & 0xFFFF);
}
/** @brief Reads a specified number of bytes from a file-descriptor. Retries
* until enough bytes are received (or until the read command fails).
* @param[in] filedes File descriptor used to read data
* @param[out] buf Pointer to target data buffer
* @param[in] nbyte Number of bytes to read
* @return Number of bytes retrieved, or -1 in the event of an error.
* @retval <0 A read error occured, and errno was set accordingly.
* @retval >=0 Number of bytes retrieved. */
static ssize_t read_count(int filedes, char *buf, size_t nbyte)
{
size_t total = 0;
while (total < nbyte) {
ssize_t result = read_noeintr(filedes, buf, (nbyte - total));
if (result < 0) {
return result;
}
total += (size_t) result;
}
return (ssize_t) nbyte;
}
/** @brief Writes a specified number of bytes to a file-descriptor. Retries
* until enough bytes are written (or until the write command fails).
* @param[in] filedes File descriptor used to write data
* @param[in] buf Pointer to input data buffer
* @param[in] nbyte Number of bytes to write
* @return Number of bytes written, or -1 in the event of an error.
* @retval <0 A write error occured, and errno was set accordingly.
* @retval >=0 Number of bytes written. */
static ssize_t write_count(int filedes, const char *buf, size_t nbyte)
{
size_t remaining = nbyte;
while (remaining != 0) {
ssize_t result = write_noeintr(filedes, buf, remaining);
if (result < 0) {
return result;
}
remaining -= (size_t) result;
buf += result;
}
return (ssize_t) nbyte;
}
static int fd_socket_setflag(int fd)
{
return fcntl_setown_noeintr(fd, getpid());
}
static int fd_socket_clearflag(int fd)
{
return fcntl_setown_noeintr(fd, 0);
}
static int fd_socket_checkflag(int fd)
{
pid_t result = fcntl_getown_noeintr(fd);
if (result != -1) {
return (result != 0);
}
return -1;
}
/*----------------------------------------------------------------------------*/
#define get_size(type, field) sizeof(((type *)0)->field)
enum {
sun_path_size = get_size(struct sockaddr_un, sun_path) - 1,
address_maxlen = (sun_path_size > PATH_MAX) ? sun_path_size : PATH_MAX,
backlog_target = 16,
backlog_size = (backlog_target < SOMAXCONN) ? backlog_target : SOMAXCONN
};
static int socks_address_make(const char *filename, struct sockaddr_un *result)
{
size_t length = strnlen(filename, PATH_MAX + 1);
if (length > address_maxlen) {
char buffer[address_maxlen + 1];
memcpy(buffer, filename, address_maxlen);
buffer[address_maxlen] = '\x00';
fprintf(stderr, "error: pathname too long [%s]\n", buffer);
return -1;
}
memcpy(result->sun_path, filename, length);
result->sun_path[length] = '\x00';
result->sun_family = AF_UNIX;
return 0;
}
static ssize_t socks_recv(int fd, void *buf, size_t bufsize)
{
char header[2];
uint16_t msgsize;
ssize_t result;
result = read_count(fd, header, 2);
if (result < 0) {
return result;
}
msgsize = deserialize_uint16(header);
if (msgsize > bufsize) {
errno = EMSGSIZE;
return -1;
}
return read_count(fd, (char *) buf, msgsize);
}
static ssize_t socks_send(int fd, const void *buf, uint16_t nbyte)
{
char header[2];
ssize_t result;
serialize_uint16(nbyte, header);
result = write_count(fd, header, 2);
if (result < 0) {
return result;
}
return write_count(fd, (const char *) buf, nbyte);
}
static int socks_process_request(int connection_fd, socks_callback_t callback,
uint16_t input_size)
{
int result;
char buffer[input_size + 1];
int callback_result;
buffer[input_size] = '\x00';
result = (int) read_count(connection_fd, buffer, input_size);
if (result < 0) {
return result;
}
result = fd_socket_clearflag(connection_fd);
if (result < 0) {
fprintf(stderr, "couldn't clear flag\n");
return result;
}
callback_result = callback(connection_fd, buffer, input_size);
switch (fd_socket_checkflag(connection_fd)) {
case 0:
result = (int) socks_server_respond(connection_fd, "", 0);
break;
case 1:
break;
default:
fprintf(stderr, "couldn't check flag\n");
break;
}
if (result == 0) {
return callback_result;
}
return result;
}
static int socks_server_select(int socket_fd, struct timeval *restrict timeout)
{
fd_set read_fds;
fd_set error_fds;
FD_ZERO(&read_fds);
FD_ZERO(&error_fds);
FD_SET(socket_fd, &read_fds);
FD_SET(socket_fd, &error_fds);
int result = select_noeintr(socket_fd + 1, &read_fds, NULL, &error_fds,
timeout);
/* We might normally use FD_ISSET here, but this isn't necessary
* because we're only listening for one item (the socket). */
if (result > 0) {
return 1;
}
return result;
}
/*----------------------------------------------------------------------------*/
ssize_t socks_server_respond(int response_fd, const void *buf, uint16_t nbyte)
{
if (fd_socket_setflag(response_fd) != 0) {
fprintf(stderr, "setflag failed!\n");
}
return socks_send(response_fd, buf, nbyte);
}
int socks_server_open(const char *filename, mode_t mode)
{
int result;
int socket_fd;
struct sockaddr_un address;
result = socks_address_make(filename, &address);
if (result < 0) {
return result;
}
socket_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
if (socket_fd < 0) {
return socket_fd;
}
if (access(filename, F_OK) == 0) {
unlink(filename);
} else if (errno == ENOENT) {
errno = 0;
}
result = bind(socket_fd, (struct sockaddr *) &address, sizeof(address));
if (result != 0) {
return -1;
}
result = chmod_noeintr(filename, mode);
if (result != 0) {
return -1;
}
result = listen(socket_fd, backlog_size);
if (result != 0) {
return -1;
}
return socket_fd;
}
int socks_server_process(int socket_fd, socks_callback_t callback)
{
int connection_fd;
ssize_t result;
char header[2];
uint16_t msgsize;
connection_fd = accept_noeintr(socket_fd, NULL, NULL);
if (connection_fd < 0) {
return connection_fd;
}
result = read_count(connection_fd, header, 2);
if (result < 0) {
return (int) result;
}
msgsize = deserialize_uint16(header);
result = socks_process_request(connection_fd, callback, msgsize);
close_noeintr(connection_fd);
return (int) result;
}
int socks_server_poll(int socket_fd)
{
struct timeval nodelay = {.tv_sec = 0, .tv_usec = 0};
int result = socks_server_select(socket_fd, &nodelay);
if (result > 0) {
return 1;
}
return result;
}
int socks_server_wait(int socket_fd)
{
int result = socks_server_select(socket_fd, NULL);
if (result > 0) {
return 0;
}
if (result == 0) {
return -1;
}
return result;
}
int socks_server_close(int socket_fd)
{
return close_noeintr(socket_fd);
}
/*----------------------------------------------------------------------------*/
ssize_t socks_client_process(const char *filename, const char *input,
uint16_t nbyte, char *output, uint16_t maxlen)
{
ssize_t result;
int socket_fd;
struct sockaddr_un address;
result = socks_address_make(filename, &address);
if (result < 0) {
return result;
}
socket_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
if (socket_fd < 0) {
fprintf(stderr, "Couldn't open socket [%s]\n", filename);
return socket_fd;
}
result = connect_noeintr(socket_fd, (struct sockaddr *) &address,
sizeof(address));
if (result != 0) {
fprintf(stderr, "Couldn't connect to socket [%s]\n", filename);
close_noeintr(socket_fd);
return result;
}
result = socks_send(socket_fd, input, nbyte);
if (result < 0) {
close_noeintr(socket_fd);
return result;
}
result = socks_recv(socket_fd, output, maxlen);
close_noeintr(socket_fd);
return result;
}