-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinypot_main.c
More file actions
199 lines (184 loc) · 5.23 KB
/
Copy pathtinypot_main.c
File metadata and controls
199 lines (184 loc) · 5.23 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
static const char* version = "2.3.8";
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/poll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "tinypot_process.h"
int main(int argc, char* argv[])
{
int do_shtup = false;
int socketFD;
int port_num;
int index;
char* listening_address_arg;
int con_num;
int iarg;
int num_ports, num_ports_requested;
int* port_array;
struct pollfd* pds;
printf("Program tinypot version %s\n", version);
fflush(stdout);
listening_address_arg = "";
switch(argc)
{
case 1:
case 2:
fprintf(stderr,
"Usage: tinypot [-shtup] [address|-] portnum portnum ...\n");
exit(EXIT_FAILURE);
break;
default:
break;
}
/* Process option, if any. */
if ((argv[1][0] == '-') && (argv[1][1] != '\0'))
{
if (strcmp("-shtup", argv[1]) != 0)
{
fprintf(stderr,
"Usage: tinypot [-shtup] [address|-] portnum portnum ...\n");
exit(EXIT_FAILURE);
}
do_shtup = true;
++argv;
--argc;
}
listening_address_arg = argv[1];
if (strcmp(listening_address_arg, "-") == 0)
{
listening_address_arg = "*";
}
num_ports_requested = argc - 2;
if ((port_array = (int*)malloc(num_ports_requested * sizeof(int))) == NULL)
{
perror("malloc failed");
exit(EXIT_FAILURE);
}
if ((pds =
(struct pollfd*)malloc(num_ports_requested * sizeof(struct pollfd))) ==
NULL)
{
perror("malloc failed");
exit(EXIT_FAILURE);
}
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
{
perror("signal failed");
exit(EXIT_FAILURE);
}
if (signal(SIGTRAP, SIG_IGN) == SIG_ERR)
{
perror("signal failed");
exit(EXIT_FAILURE);
}
memset(pds, 0, num_ports_requested * sizeof(struct pollfd));
num_ports = 0;
for (iarg = 2 ; iarg < argc ; ++iarg)
{
char* port_arg = argv[iarg];
if (sscanf(port_arg, "%d", &port_num) != 1)
{
fprintf(stderr, "Illegal numeric expression \"%s\"\n", port_arg);
exit(EXIT_FAILURE);
}
struct sockaddr_in sa;
socketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (socketFD == -1)
{
perror("cannot create socket");
exit(EXIT_FAILURE);
}
int reuse = 1;
if (setsockopt(
socketFD, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
perror("cannot set SO_REUSEADDR");
exit(EXIT_FAILURE);
}
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons((uint16_t)port_num);
if (strcmp(listening_address_arg, "*") == 0)
{
sa.sin_addr.s_addr = htonl(INADDR_ANY);
}
else
{
if ((sa.sin_addr.s_addr =
inet_addr(listening_address_arg)) == INADDR_NONE)
{
fprintf(stderr,
"Cannot use \"%s\" with -listen.\n", listening_address_arg);
fprintf(stderr, "Please use numbers, e.g. nn.nn.nn.nn .\n");
exit(EXIT_FAILURE);
}
}
if (bind(socketFD, (struct sockaddr *)(&sa), (socklen_t)sizeof(sa))
== -1)
{
fprintf(stderr, "On port %d, ", port_num);
perror("bind failed");
continue;
}
if (listen(socketFD, 10) == -1)
{
perror("listen failed");
exit(EXIT_FAILURE);
}
port_array[num_ports] = port_num;
pds[num_ports].fd = socketFD;
pds[num_ports].events = POLLIN;
++num_ports;
}
/* Print opening message */
printf("%s Listening on address %s, %d TCP/IP ports:\n",
my_time(), listening_address_arg, num_ports);
printf(" ");
for (index = 0 ; index < num_ports ; ++index)
printf(" %d", port_array[index]);
printf("\n");
fflush(stdout);
con_num = 0;
while (1)
{
int status = poll(pds, num_ports, -1);
if (status < 0)
{
perror("poll failed");
exit(EXIT_FAILURE);
}
if (status == 0)
{
fprintf(stderr, "Unexpected 0 return from poll()\n");
exit(EXIT_FAILURE);
}
for (index = 0 ; index < num_ports ; ++index)
{
if (pds[index].revents == 0) continue;
if (pds[index].revents != POLLIN)
{
fprintf(stderr, "Unexpected revents value %d (1)\n",
pds[index].revents);
exit(EXIT_FAILURE);
}
if (process_connection(do_shtup, ++con_num, port_array[index],
pds[index].fd) != 0)
{
/* Too many open files, probably. */
for ( ; index < num_ports ; ++index)
{
if (pds[index].revents == 0) continue;
close(port_array[index]);
}
sleep(5);
}
}
}
return EXIT_SUCCESS;
}