-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.c
More file actions
440 lines (415 loc) · 17.6 KB
/
Copy pathsocket.c
File metadata and controls
440 lines (415 loc) · 17.6 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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#include "builtins.h"
#include "config.h"
#include "shell.h"
#include "common.h"
#include <errno.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "loadables.h"
#define FAIL_EXECUTION(fd, msg, ...) do { \
int err = errno; \
if (msg) builtin_error (msg, __VA_ARGS__); \
close(fd); \
return err == EADDRINUSE ? EADDRINUSE : EXECUTION_FAILURE; \
} while (0)
#define USAGE do { \
builtin_usage(); \
return EX_USAGE; \
} while (0)
#define SA struct sockaddr *
typedef union { struct sockaddr_in in;
struct sockaddr_in6 in6;
struct sockaddr_un un; } saddr;
static const int socket_send(intmax_t, WORD_LIST *);
static const int socket_receive(intmax_t, WORD_LIST *);
static int
socket_builtin(WORD_LIST *list) {
saddr addr, client;
char varbuf[128], *varname = NULL;
intmax_t opt, n, af, queue, sendfd = -1, recvfd = -1, sockfd = -1;
uint16_t port;
memset((char *)&client, 0, sizeof(client));
memset((char *)&addr, 0, sizeof(addr));
/* First check for -h, -s or -r on the command line */
if (list == NULL)
USAGE;
reset_internal_getopt ();
while ((opt = internal_getopt (list, "hr:s:")) != -1) {
if (opt == 's' || opt == 'r') {
if (!valid_number(list_optarg, opt == 'r' ? &recvfd : &sendfd))
FAIL_EXECUTION(-1, "%s: invalid file descriptor", list_optarg);
continue;
}
USAGE;
}
if ((list = loptend) == NULL || no_options(list))
USAGE;
/* Sending or receving data */
if (sendfd != -1 && recvfd != -1)
FAIL_EXECUTION(-1, "cannot send and receive", 0);
if (sendfd != -1)
return socket_send(sendfd, list);
if (recvfd != -1)
return socket_receive(recvfd, list);
/* Result variable name */
if (strcasecmp("AF_INET", list->word->word) != 0 &&
strcasecmp("AF_INET6", list->word->word) != 0 &&
strcasecmp("AF_UNIX", list->word->word) != 0) {
varname = list->word->word;
if (!valid_identifier(varname) && !valid_array_reference(varname, 0))
FAIL_EXECUTION(-1, "%s: invalid variable name", varname);
list = list->next;
} else
varname = "SOCK_FD";
if (list == NULL)
USAGE;
/* Parse socket domain: unix|inet|inet6 */
if (strcasecmp("AF_INET", list->word->word) == 0)
af = AF_INET;
else if (strcasecmp("AF_INET6", list->word->word) == 0)
af = AF_INET6;
else if (strcasecmp("AF_UNIX", list->word->word) == 0)
af = AF_UNIX;
else
USAGE;
if ((list = list->next) == NULL)
USAGE;
int socktype;
/* Parse socket type keyword. */
if (strcasecmp("SOCK_STREAM", list->word->word) == 0)
socktype = SOCK_STREAM;
else if (strcasecmp("SOCK_DGRAM", list->word->word) == 0)
socktype = SOCK_DGRAM;
else
USAGE;
if ((list = list->next) == NULL)
USAGE;
/* 0 = bind + listen, 1 = connect */
int op;
if (strcasecmp("local", list->word->word) == 0)
op = 0;
else if (strcasecmp("peer", list->word->word) == 0)
op = 1;
else
USAGE;
if ((list = list->next) == NULL)
USAGE;
socklen_t addrlen;
switch (af) {
case AF_UNIX:
addr.un.sun_family = AF_UNIX;
/* POSIX standard defines maximum socket name to 108. */
snprintf(addr.un.sun_path, 107, "%s", list->word->word);
if (op == 1 && (list = list->next)) {
client.un.sun_family = AF_UNIX;
snprintf(client.un.sun_path, 107, "%s", list->word->word);
}
addrlen = sizeof(addr.un);
break;
case AF_INET:
addr.in.sin_family = AF_INET;
if (inet_pton(AF_INET, list->word->word, &addr.in.sin_addr) != 1)
FAIL_EXECUTION(-1, "inet_pton: invalid address: %s", list->word->word);
if ((list = list->next) == NULL)
USAGE;
if (sscanf(list->word->word, "%"SCNu16, &port) != 1)
FAIL_EXECUTION(-1, "invalid port: %s", list->word->word);
addr.in.sin_port = htons(port);
addrlen = sizeof(addr.in);
break;
case AF_INET6:
addr.in6.sin6_family = AF_INET6;
if (inet_pton(AF_INET6, list->word->word, &addr.in6.sin6_addr) != 1)
FAIL_EXECUTION(-1, "inet_pton: invalid address: %s", list->word->word);
if ((list = list->next) == NULL)
USAGE;
if (sscanf(list->word->word, "%"SCNu16, &port) != 1)
FAIL_EXECUTION(-1, "invalid port: %s", list->word->word);
addr.in6.sin6_port = htons(port);
addrlen = sizeof(addr.in6);
break;
}
list = list->next;
queue = 5;
if (op == 0) {
/* Listen */
if (list != NULL && (sscanf(list->word->word, "%u", &queue) < 1))
FAIL_EXECUTION(-1, "invalid queue size: %s", list->word->word);
if (list != NULL)
list = list->next;
/* Fail on extraneous arguments. */
if (list != NULL)
USAGE;
sockfd = socket(af, socktype, 0);
if (sockfd == -1)
FAIL_EXECUTION(-1, "%s", strerror(errno));
if (bind(sockfd, (SA)&addr, addrlen) == -1)
FAIL_EXECUTION(sockfd, "bind: %s", strerror(errno));
if (socktype == SOCK_STREAM && listen(sockfd, queue) == -1) {
if (af == AF_UNIX)
unlink(addr.un.sun_path);
FAIL_EXECUTION(sockfd, "listen: %s", strerror(errno));
}
n = snprintf(varbuf, sizeof(varbuf), "%d", sockfd);
if (n < 0 || n >= sizeof(varbuf))
FAIL_EXECUTION(sockfd, "snprintf: %s", strerror(errno));
bind_variable(varname, varbuf, 0);
return EXECUTION_SUCCESS;
} else {
/* Fail on extraneous arguments. */
if (list != NULL)
USAGE;
/* Connect */
if ((sockfd = socket(af, socktype, 0)) == -1)
FAIL_EXECUTION(-1, "%s", strerror(errno));
/* Bind AF_UNIX local socket path so replies can be sent */
if (af == AF_UNIX && client.un.sun_family == AF_UNIX) {
if (bind(sockfd, (SA)&client, addrlen) == -1)
FAIL_EXECUTION(sockfd, "bind: %s", strerror(errno));
}
if (connect(sockfd, (SA)&addr, addrlen) == -1)
FAIL_EXECUTION(sockfd, "connect: %s", strerror(errno));
n = snprintf(varbuf, sizeof(varbuf), "%d", sockfd);
if (n < 0 || n >= sizeof(varbuf))
FAIL_EXECUTION(sockfd, "snprintf: %s", strerror(errno));
bind_variable(varname, varbuf, 0);
return EXECUTION_SUCCESS;
}
}
static const int socket_send(intmax_t fd, WORD_LIST *list) {
saddr client;
intmax_t queue, r, s, n, opt;
char *sizevar, *databuf, varbuf[128];
memset((char *)&client, 0, sizeof(client));
/* Remote address is required */
client.in.sin_family = AF_INET;
if (inet_pton(AF_INET, list->word->word, &client.in.sin_addr) != 1) {
client.in6.sin6_family = AF_INET6;
if (inet_pton(AF_INET6, list->word->word, &client.in6.sin6_addr) != 1) {
client.un.sun_family = AF_UNIX;
snprintf(client.un.sun_path, 107, "%s", list->word->word);
}
}
/* In case of AF_INET(6) we also require a port */
if (client.un.sun_family != AF_UNIX && (list = list->next)) {
if (!valid_number(list->word->word, &n) || n <= 0
|| n > (unsigned short)-1)
FAIL_EXECUTION(-1, "%s: invalid port number", list->word->word);
client.in.sin_port = (unsigned short)htons(n);
list = list->next;
}
/* Parse the rest of the command line for varname and queue size */
for (queue = 0, sizevar = NULL; list; list = list->next) {
if (sizevar != NULL && queue != 0)
break;
if (valid_number(list->word->word, &queue)) {
if (queue < 1)
FAIL_EXECUTION(-1, "must specify send size > 0", 0);
} else if (valid_identifier(list->word->word)
|| valid_array_reference(list->word->word, 0))
sizevar = list->word->word;
else
FAIL_EXECUTION(-1, "%s: not valid vrariable name or size",
list->word->word);
}
/* Fail on extraneous arguments. */
if (list != NULL)
USAGE;
if (sizevar == NULL)
sizevar = "SOCK_SSIZE";
if (queue == 0)
queue = 8192;
unbind_variable(sizevar);
opt = client.un.sun_family == AF_UNIX ? sizeof(struct sockaddr_un) :
client.in.sin_family == AF_INET ? sizeof(struct sockaddr_in)
: sizeof(struct sockaddr_in6);
r = s = 0;
/* Receive from stdin and send to fd, check if sizes match, if they
* don't this is an error and we report the amount of data sent */
if ((databuf = malloc(queue)) == NULL)
FAIL_EXECUTION(-1, "malloc failure", strerror(errno));
while ( (n = read(0, databuf, queue - r)) > 0 && (r += n)
&& (n = sendto(fd, databuf, n, 0, (SA)&client, opt)) > 0
&& (s += n) == r);
free(databuf);
if (s >= 0) { /* Receiving or sending 0 bytes is valid */
if ((opt = snprintf(varbuf, sizeof(varbuf), "%d", s)) < 0
|| opt >= sizeof(varbuf))
FAIL_EXECUTION(-1, "buffer error: %s)", strerror(errno));
builtin_bind_variable(sizevar, varbuf, 0);
}
if (n < 0 || s != r)
FAIL_EXECUTION(-1, "send failure: %s", strerror(errno));
return EXECUTION_SUCCESS;
}
static const int socket_receive(const intmax_t fd, WORD_LIST *list) {
saddr client;
intmax_t queue, s, r, n, opt;
char *databuf, varbuf[128], *p;
char *hostvar = NULL, *portvar = NULL, *sizevar = NULL;
struct timeval timeout = { -1, -1 };
fd_set iofds;
memset((char *)&client, 0, sizeof(client));
/* Parse command line unitl all parameters are set, or no more words. */
for (opt = queue = 0; list; list = list->next) {
if (queue != 0 && timeout.tv_sec != -1 && hostvar && portvar && sizevar)
break;
/* Nrs are not valid variable names */
if (!valid_identifier(list->word->word)
&& !valid_array_reference(list->word->word, 0)) {
if ( (queue == 0 && !valid_number(list->word->word, &opt))
|| (queue != 0 && (uconvert(list->word->word, &timeout.tv_sec,
&timeout.tv_usec, (char **)0) == 0
|| timeout.tv_sec < 0
|| timeout.tv_usec < 0)))
FAIL_EXECUTION(-1, "%s: invalid name, queue or timeout",
list->word->word);
if (queue == 0 && opt > 0)
queue = opt;
} else if (hostvar == NULL)
hostvar = list->word->word;
else if (portvar == NULL)
portvar = list->word->word;
else if (sizevar == NULL)
sizevar = list->word->word;
}
/* Fail on extraneous arguments. */
if (list != NULL)
USAGE;
if (queue <= 0)
FAIL_EXECUTION(-1, "receive size must be > 0", 0);
if (hostvar == NULL)
hostvar = "SOCK_RHOST";
if (portvar == NULL)
portvar = "SOCK_RPORT";
if (sizevar == NULL)
sizevar = "SOCK_RSIZE";
unbind_variable(hostvar);
unbind_variable(portvar);
unbind_variable(sizevar);
/* Check for data with timeout */
if (timeout.tv_sec >= 0 && timeout.tv_usec >= 0) {
FD_ZERO(&iofds);
FD_SET(fd, &iofds);
opt = select (fd + 1, &iofds, NULL, NULL, &timeout);
if (opt < 0)
FAIL_EXECUTION(-1, "select failure: %s", strerror(errno));
if (opt == 0)
return EXECUTION_SUCCESS;
}
/* Receive data from socket and send to stdout check if sizes match
* don't this is an error and we report the amount of data sent */
opt = client.un.sun_family == AF_UNIX ? sizeof(struct sockaddr_un) :
client.in.sin_family == AF_INET ? sizeof(struct sockaddr_in)
: sizeof(struct sockaddr_in6);
s = r = -1;
if ((databuf = malloc(queue)) == NULL)
FAIL_EXECUTION(-1, "malloc failure", strerror(errno));
if ((r = recvfrom(fd, databuf, queue, 0, (SA)&client,
(socklen_t *)&opt)) >= 0)
s = write(1, databuf, r);
opt = errno;
free(databuf);
/* Set variables, rhost and rport when data was received
* and size when data was sent to stdout */
if (s >= 0) { /* Receiving or sending 0 bytes is valid */
if ((n = snprintf(varbuf, sizeof(varbuf), "%d", s)) < 0
|| n >= sizeof(varbuf))
FAIL_EXECUTION(-1, "buffer error: %s)", strerror(errno));
builtin_bind_variable(sizevar, varbuf, 0);
}
if (r >= 0) { /* Receiving or sending 0 bytes is valid */
if (client.in.sin_family == AF_INET)
p = (char *)&client.in.sin_addr;
if (client.in6.sin6_family == AF_INET6)
p = (char *)&client.in6.sin6_addr;
if (client.un.sun_family != AF_UNIX) {
n = sprintf(varbuf, "%d", ntohs(client.in.sin_port));
builtin_bind_variable(portvar, varbuf, 0);
inet_ntop(client.in.sin_family, p, varbuf, sizeof(varbuf));
} else
n = snprintf(varbuf, sizeof(varbuf), "%s", client.un.sun_path);
if (n < 0 || n >= sizeof(varbuf))
FAIL_EXECUTION(-1, "buffer error: %s)", strerror(errno));
builtin_bind_variable(hostvar, varbuf, 0);
}
if (r < 0 || s < 0 || s != r)
FAIL_EXECUTION(-1, "receive failure: %s", strerror(opt));
return EXECUTION_SUCCESS;
}
static char *const socket_doc[] = {
"Provides an interface for creating and using POSIX sockets",
"The result socket handle is written to the variable denoted by varname.",
"The default varname is SOCK_FD. Parameters are case insensitive.",
"AF_UNIX, AF_INET, AF_INET6 (case insensitive) cannot be used as varname.",
"The default queue (backlog) size is 5.",
"",
"socket -s and socket -r.",
"Sends to or receives data from a socket. This only applies to SOCK_DGRAM",
"sockets. read cannot be called on such sockets, as read() calls block.",
"Data is read standard input and written to standard output. It is not",
"checked whether the socket is SOCK_DGRAM.",
"",
"Receiving data requires a queue size to be set. By default no timeout is",
"used. Three variables will be set, SOCK_RHOST, SOCK_RPORT and SOCK_RSIZE.",
"If the socket is AF_UNIX SOCK_RHOST will be set to the remote socket path",
"and SOCK_RPORT will not be set. SOCK_RSIZE is set to the size of the",
"received datagram. Only a queue size is mandatory, the order is flexible,",
"the first number will be the queue size, the second number the timeout,",
"variable names are in order: RHOST, RPORT, RSIZE.",
"On success 0 is returned and the variables will be set. On failure 1 is",
"returned (or the OS value of EADDRINUSE).",
"On timeout success is returned and no variables will be set",
"",
"Sending data requires a remote address and port, or remote socket path",
"in case of an AF_UNIX socket. When sending data to an AF_UNIX socket, if",
"a local path is not specified, the peer socket will not have a remote",
"path and will not be able to reply. Message or FD passing is not",
"implemented, neither are abstract sockets. A maximum [size] of data",
"to be sent can optionally be specified. The size of the data sent to the",
"socket is saved in variable ssizevar (SOCK_SSIZE).",
"",
"Examples:",
" socket SOCKFD AF_UNIX SOCK_STREAM local /tmp/sock",
" Opens a local UNIX STREAM socket in /tmp/sock.",
" SOCKFD will be set to the fd",
" socket SOCKFD af_inet6 sock_dgram local :: 12345",
" Opens a ipv6 udp socket on all ip addresses port 12345.",
" SOCKFD will be set to the fd",
" socket AF_INET sock_stream local 1.2.3.4 80 100",
" Opens ipv4 tcp socket on ip 1.2.3.4 port 80 queue size 100.",
" SOCK_FD will be set to the fd."
" socket AF_INET6 SOCK_STREAM PEER 2a00:400::1 443",
" Connects to ipv6 adress 2a00:400::1 on tcp port 443.",
" SOCK_FD will be set to te fd.",
" socket fd af_unix sock_dgram PEER /tmp/sock",
" Connects to UNIX datagram socket /tmp/sock.",
" fd will be set to the fd",
" socket -s 3 1.2.3.4 1234",
" Sends data from stdin on the socket fd 3 to 1.2.3.4:1234.",
" socket -r 3 1000 10 RHOST RPORT RSIZE",
" Receives max 1000 bytes of data on socket fd 3, RHOST",
" contains the remote ip / path, RPORT the remote port and",
" RSIZE the recieved size. Timeout of 10 seconds.",
" socket -r 3 RHOST 1000 RPORT 10 RSIZE",
" Same as above.a,"
" socket -r 3 RHOST 1000 10.1",
" In this case the default of SOCK_RPORT and SOCK_RSIZE",
" will be used and a timeout of 10.1 seconds.",
NULL
};
struct builtin socket_struct = {
.name = "socket",
.function = socket_builtin,
.flags = BUILTIN_ENABLED,
.long_doc = socket_doc,
.short_doc =
"socket [varname] AF_UNIX|AF_INET|AF_INET6 SOCK_STREAM|SOCK_DGRAM local|peer PATH|ADDRESS [port] [queue]\n"
" socket -s fd addr port / [local path] [ssizevar] [size]\n"
" socket -r fd queue [timeout] [rhostvar] [rportvar] [rsizevar]",
.handle = NULL
};