echosrv.c
5.14 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2007 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <sys/wait.h>
#include "getopt.h"
#include <mailutils/mailutils.h>
#include <mailutils/server.h>
#define MU_ASSERT(expr) \
do \
{ \
int rc = expr; \
if (rc) \
{ \
mu_error (#expr " failed: %s", mu_strerror (rc)); \
abort (); \
} \
} \
while (0);
mu_server_t server;
int
echo_conn (int fd, struct sockaddr_in *s, void *server_data, void *call_data,
mu_tcp_server_t srv)
{
struct sockaddr_in srv_addr;
pid_t pid;
char *buf = NULL;
size_t size = 0;
FILE *in, *out;
mu_tcp_server_get_sockaddr (srv, &srv_addr);
pid = fork ();
if (pid == -1)
{
mu_error ("fork failed: %s", mu_strerror (errno));
return 0;
}
if (pid)
{
mu_diag_output (MU_DIAG_INFO, "%lu: opened connection %s:%d => %s:%d",
(unsigned long) pid,
inet_ntoa (srv_addr.sin_addr), ntohs (srv_addr.sin_port),
inet_ntoa (s->sin_addr), ntohs (s->sin_port));
return 0;
}
mu_tcp_server_shutdown (srv);
in = fdopen (fd, "r");
out = fdopen (fd, "w");
setvbuf (in, NULL, _IOLBF, 0);
setvbuf (out, NULL, _IOLBF, 0);
pid = getpid ();
while (getline (&buf, &size, in) > 0)
{
int len = strlen (buf);
if (len > 0)
{
buf[--len] = 0;
if (buf[len-1] == '\r')
buf[--len] = 0;
}
fprintf (out, "%lu: you said: \"%s\"\r\n", (unsigned long) pid, buf);
}
free (buf);
exit (0);
}
int
tcp_conn_handler (int fd, void *conn_data, void *server_data)
{
mu_tcp_server_t tcpsrv = (mu_tcp_server_t) conn_data;
int rc = mu_tcp_server_accept (tcpsrv, server_data);
if (rc && rc != EINTR)
{
mu_tcp_server_shutdown (tcpsrv);
return MU_SERVER_CLOSE_CONN;
}
return MU_SERVER_SUCCESS;
}
void
tcp_conn_free (void *conn_data, void *server_data)
{
mu_tcp_server_t tcpsrv = (mu_tcp_server_t) conn_data;
mu_tcp_server_destroy (&tcpsrv);
}
void
create_server (char *arg)
{
char *p, *q;
struct sockaddr_in s;
mu_tcp_server_t tcpsrv;
unsigned n;
p = strchr (arg, ':');
if (!*p)
{
mu_error ("invalid specification: %s\n", arg);
exit (1);
}
*p++ = 0;
s.sin_family = AF_INET;
if (inet_aton (arg, &s.sin_addr) == 0)
{
mu_error ("invalid IP address: %s\n", arg);
exit (1);
}
n = strtoul (p, &q, 0);
if (*q)
{
mu_error ("invalid port number: %s\n", p);
exit (1);
}
s.sin_port = htons (n);
MU_ASSERT (mu_tcp_server_create (&tcpsrv, &s));
MU_ASSERT (mu_tcp_server_open (tcpsrv));
MU_ASSERT (mu_tcp_server_set_conn (tcpsrv, echo_conn));
MU_ASSERT (mu_server_add_connection (server,
mu_tcp_server_get_fd (tcpsrv),
tcpsrv,
tcp_conn_handler, tcp_conn_free));
}
static int cleanup_needed;
RETSIGTYPE
sig_child (int sig)
{
cleanup_needed = 1;
signal (sig, sig_child);
}
int
server_idle (void *server_data)
{
if (cleanup_needed)
{
int status;
pid_t pid;
cleanup_needed = 0;
while ((pid = waitpid (-1, &status, WNOHANG)) > 0)
{
if (WIFEXITED (status))
mu_diag_output (MU_DIAG_INFO, "%lu: finished with code %d",
(unsigned long) pid,
WEXITSTATUS (status));
else if (WIFSIGNALED (status))
mu_diag_output (MU_DIAG_ERR, "%lu: terminated on signal %d",
(unsigned long) pid,
WTERMSIG (status));
else
mu_diag_output (MU_DIAG_ERR, "%lu: terminated (cause unknown)");
}
}
return 0;
}
int
run ()
{
int rc;
signal (SIGCHLD, sig_child);
rc = mu_server_run (server);
if (rc)
mu_error ("%s", mu_strerror (rc));
mu_server_destroy (&server);
return rc ? 1 : 0;
}
int
main (int argc, char **argv)
{
int rc;
mu_set_program_name (argv[0]);
while ((rc = getopt (argc, argv, "Dd:")) != EOF)
{
switch (rc)
{
case 'D':
mu_debug_line_info = 1;
break;
case 'd':
mu_global_debug_from_string (optarg, "command line");
break;
default:
exit (1);
}
}
argc -= optind;
argv += optind;
MU_ASSERT (mu_server_create (&server));
mu_server_set_idle (server, server_idle);
while (argc--)
create_server (*argv++);
return run ();
}