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
|
/*
* Copyright (c) 1997-2001 Silicon Graphics, Inc. All Rights Reserved.
* Copyright (c) 2011 Ken McDonell. All Rights Reserved.
*
* Drain server ...
* accepts a connection and then reads until end of input
*
* Based on pdu-server.c
*/
#include <pcp/pmapi.h>
#include <pcp/impl.h>
int
main(int argc, char *argv[])
{
int fd;
int port = 1214;
/* default port assigned to kazaa what ever that is! */
int hang = 0;
int i, sts;
int c;
int newfd;
struct sockaddr_in myAddr;
struct linger noLinger = {1, 0};
char *endnum;
int errflag = 0;
__pmSetProgname(argv[0]);
while ((c = getopt(argc, argv, "D:hp:?")) != EOF) {
switch (c) {
case 'D': /* debug flag */
sts = __pmParseDebug(optarg);
if (sts < 0) {
fprintf(stderr, "%s: unrecognized debug flag specification (%s)\n",
pmProgname, optarg);
errflag++;
}
else
pmDebug |= sts;
break;
case 'h': /* hang after accept */
hang = 1;
break;
case 'p':
port = (int)strtol(optarg, &endnum, 10);
if (*endnum != '\0') {
fprintf(stderr, "%s: port argument must be a numeric internet port number\n", pmProgname);
exit(1);
}
break;
case '?':
default:
errflag++;
break;
}
}
if (errflag || optind != argc) {
fprintf(stderr, "Usage: %s [-D n] [-h] [-p port]\n", pmProgname);
exit(1);
}
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
perror("socket");
exit(1);
}
if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &i,
sizeof(i)) < 0) {
perror("setsockopt(nodelay)");
exit(1);
}
/* Don't linger on close */
if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &noLinger, sizeof(noLinger)) < 0) {
perror("setsockopt(nolinger)");
exit(1);
}
memset(&myAddr, 0, sizeof(myAddr));
myAddr.sin_family = AF_INET;
myAddr.sin_addr.s_addr = htonl(INADDR_ANY);
myAddr.sin_port = htons(port);
sts = bind(fd, (struct sockaddr*)&myAddr, sizeof(myAddr));
if (sts < 0) {
fprintf(stderr, "bind(%d): %s\n", port, strerror(errno));
exit(1);
}
sts = listen(fd, 5); /* Max. of 5 pending connection requests */
if (sts == -1) {
perror("listen");
exit(1);
}
newfd = accept(fd, (struct sockaddr *)0, 0);
if (newfd < 0) {
fprintf(stderr, "%s: accept: %s\n", pmProgname, strerror(errno));
exit(1);
}
if (hang) {
/* wait for a signal ... */
pause();
exit(0);
}
/* drain input */
while ((sts = read(newfd, &c, 1)) == 1)
;
if (sts < 0) {
/*
* ECONNRESET is expected when client exits w/out closing
* socket.
*/
if (errno != ECONNRESET)
fprintf(stderr, "%s: read error: %s\n", pmProgname, pmErrStr(-errno));
}
exit(0);
}
|