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
|
#define _GNU_SOURCE // for POLLRDHUP
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* for OSes which don't have it */
#ifndef POLLRDHUP
#define POLLRDHUP 0
#endif
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
#ifndef MSG_MORE
#define MSG_MORE 0
#endif
int verbose = 0;
int cmd = 0;
int cmdstep = 0;
int zero = 0;
int one = 1;
int lfd = -1;
int cfd = -1;
int sfd = -1;
struct sockaddr_in saddr, caddr;
socklen_t salen, calen;
void usage(const char *arg0)
{
printf("Usage: %s [ arg [<action>[,...]] ] ...\n"
"args:\n"
" -h display this help\n"
" -v verbose mode (shows ret values)\n"
" -c <actions> perform <action> on client side socket\n"
" -s <actions> perform <action> on server side socket\n"
" -l <actions> perform <action> on listening socket\n"
"\n"
"actions for -c/-s/-l (multiple may be delimited by commas) :\n"
" acc accept on listener, implicit before first -s\n"
" snd send a few bytes of data\n"
" mor send a few bytes of data with MSG_MORE\n"
" rcv receive a few bytes of data\n"
" drn drain: receive till zero\n"
" shr SHUT_RD : shutdown read side\n"
" shw SHUT_WR : shutdown write side\n"
" shb SHUT_RDWR : shutdown both sides\n"
" lin disable lingering on the socket\n"
" clo close the file descriptor\n"
" pol poll() for any event\n"
"\n", arg0);
}
void die(const char *msg)
{
if (msg)
fprintf(stderr, "%s\n", msg);
exit(1);
}
const char *get_errno(int ret)
{
static char errmsg[100];
if (ret >= 0)
return "";
snprintf(errmsg, sizeof(errmsg), " (%s)", strerror(errno));
return errmsg;
}
void do_acc(int fd)
{
int ret;
calen = sizeof(caddr);
ret = accept(lfd, (struct sockaddr*)&caddr, &calen);
if (sfd < 0)
sfd = ret;
if (verbose)
printf("cmd #%d stp #%d: %s(%d): ret=%d%s\n", cmd, cmdstep, __FUNCTION__, fd, ret, get_errno(ret));
}
void do_snd(int fd)
{
int ret;
ret = send(fd, "foo", 3, MSG_NOSIGNAL|MSG_DONTWAIT);
if (verbose)
printf("cmd #%d stp #%d: %s(%d): ret=%d%s\n", cmd, cmdstep, __FUNCTION__, fd, ret, get_errno(ret));
}
void do_mor(int fd)
{
int ret;
ret = send(fd, "foo", 3, MSG_NOSIGNAL|MSG_DONTWAIT|MSG_MORE);
if (verbose)
printf("cmd #%d stp #%d: %s(%d): ret=%d%s\n", cmd, cmdstep, __FUNCTION__, fd, ret, get_errno(ret));
}
void do_rcv(int fd)
{
char buf[10];
int ret;
ret = recv(fd, buf, sizeof(buf), MSG_DONTWAIT);
if (verbose)
printf("cmd #%d stp #%d: %s(%d): ret=%d%s\n", cmd, cmdstep, __FUNCTION__, fd, ret, get_errno(ret));
}
void do_drn(int fd)
{
char buf[16384];
int total = -1;
int ret;
while (1) {
ret = recv(fd, buf, sizeof(buf), 0);
if (ret <= 0)
break;
if (total < 0)
total = 0;
total += ret;
}
if (verbose)
printf("cmd #%d stp #%d: %s(%d): ret=%d%s\n", cmd, cmdstep, __FUNCTION__, fd, total, get_errno(ret));
}
void do_shr(int fd)
{
int ret;
ret = shutdown(fd, SHUT_RD);
if (verbose)
printf("cmd #%d stp #%d: %s(%d): ret=%d%s\n", cmd, cmdstep, __FUNCTION__, fd, ret, get_errno(ret));
}
void do_shw(int fd)
{
int ret;
ret = shutdown(fd, SHUT_WR);
if (verbose)
printf("cmd #%d stp #%d: %s(%d): ret=%d%s\n", cmd, cmdstep, __FUNCTION__, fd, ret, get_errno(ret));
}
void do_shb(int fd)
{
int ret;
ret = shutdown(fd, SHUT_RDWR);
if (verbose)
printf("cmd #%d stp #%d: %s(%d): ret=%d%s\n", cmd, cmdstep, __FUNCTION__, fd, ret, get_errno(ret));
}
void do_lin(int fd)
{
struct linger nolinger = { .l_onoff = 1, .l_linger = 0 };
int ret;
ret = setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(nolinger));
if (verbose)
printf("cmd #%d stp #%d: %s(%d): ret=%d%s\n", cmd, cmdstep, __FUNCTION__, fd, ret, get_errno(ret));
}
void do_clo(int fd)
{
int ret;
ret = close(fd);
if (verbose)
printf("cmd #%d stp #%d: %s(%d): ret=%d%s\n", cmd, cmdstep, __FUNCTION__, fd, ret, get_errno(ret));
}
void do_pol(int fd)
{
struct pollfd fds = { .fd = fd, .events = POLLIN|POLLOUT|POLLRDHUP, .revents=0 };
int ret;
ret = poll(&fds, 1, 0);
if (verbose) {
printf("cmd #%d stp #%d: %s(%d): ret=%d%s ev=%#x ", cmd, cmdstep, __FUNCTION__, fd, ret, get_errno(ret), ret > 0 ? fds.revents : 0);
if (ret > 0 && fds.revents) {
int flags, flag;
putchar('(');
for (flags = fds.revents; flags; flags ^= flag) {
flag = flags ^ (flags & (flags - 1)); // keep lowest bit only
switch (flag) {
case POLLIN: printf("IN"); break;
case POLLOUT: printf("OUT"); break;
case POLLPRI: printf("PRI"); break;
case POLLHUP: printf("HUP"); break;
case POLLERR: printf("ERR"); break;
case POLLNVAL: printf("NVAL"); break;
#if POLLRDHUP
case POLLRDHUP: printf("RDHUP"); break;
#endif
default: printf("???[%#x]", flag); break;
}
if (flags ^ flag)
putchar(' ');
}
putchar(')');
}
putchar('\n');
}
}
int main(int argc, char **argv)
{
const char *arg0;
char *word, *next;
int fd;
/* listener */
lfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (lfd < 0)
die("socket(l)");
setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = htons(0);
salen = sizeof(saddr);
if (bind(lfd, (struct sockaddr *)&saddr, salen) < 0)
die("bind()");
if (listen(lfd, 1000) < 0)
die("listen()");
if (getsockname(lfd, (struct sockaddr *)&saddr, &salen) < 0)
die("getsockname()");
/* client */
cfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (cfd < 0)
die("socket(c)");
if (connect(cfd, (const struct sockaddr*)&saddr, salen) == -1)
die("connect()");
/* connection is pending in accept queue, accept() will either be
* explicit with "-l acc" below, or implicit on "-s <cmd>"
*/
arg0 = argv[0];
if (argc < 2) {
usage(arg0);
exit(1);
}
write(1, "#### BEGIN ####\n", 16); // add a visible delimiter in the traces
while (argc > 1) {
argc--; argv++;
if (**argv != '-') {
usage(arg0);
exit(1);
}
fd = -1;
switch (argv[0][1]) {
case 'h' :
usage(arg0);
exit(0);
break;
case 'v' :
verbose++;
break;
case 'c' :
cmd++; cmdstep = 0;
fd = cfd;
break;
case 's' :
cmd++; cmdstep = 0;
if (sfd < 0)
do_acc(lfd);
if (sfd < 0)
die("accept()");
fd = sfd;
break;
case 'l' :
cmd++; cmdstep = 0;
fd = lfd;
break;
default : usage(arg0); exit(1); break;
}
if (fd >= 0) { /* an action is required */
if (argc < 2) {
usage(arg0);
exit(1);
}
for (word = argv[1]; word && *word; word = next) {
next = strchr(word, ',');
if (next)
*(next++) = 0;
cmdstep++;
if (strcmp(word, "acc") == 0) {
do_acc(fd);
}
else if (strcmp(word, "snd") == 0) {
do_snd(fd);
}
else if (strcmp(word, "mor") == 0) {
do_mor(fd);
}
else if (strcmp(word, "rcv") == 0) {
do_rcv(fd);
}
else if (strcmp(word, "drn") == 0) {
do_drn(fd);
}
else if (strcmp(word, "shb") == 0) {
do_shb(fd);
}
else if (strcmp(word, "shr") == 0) {
do_shr(fd);
}
else if (strcmp(word, "shw") == 0) {
do_shw(fd);
}
else if (strcmp(word, "lin") == 0) {
do_lin(fd);
}
else if (strcmp(word, "clo") == 0) {
do_clo(fd);
}
else if (strcmp(word, "pol") == 0) {
do_pol(fd);
}
else {
printf("Ignoring unknown action '%s' in step #%d of cmd #%d\n", word, cmdstep, cmd);
}
}
argc--; argv++;
}
}
write(1, "#### END ####\n", 14); // add a visible delimiter in the traces
if (!cmd) {
printf("No command was requested!\n");
usage(arg0);
exit(1);
}
return 0;
}
|