1 /* $Cambridge: exim/test/src/server.c,v 1.1 2006/02/06 16:24:05 ph10 Exp $ */
3 /* A little hacked up program that listens on a given port and allows a script
4 to play the part of a remote MTA for testing purposes. This scripted version is
5 hacked from my original interactive version. A further hack allows it to listen
6 on a Unix domain socket as an alternative to a TCP/IP port.
8 In an IPv6 world, listening happens on both an IPv6 and an IPv4 socket, always
9 on all interfaces, unless the option -noipv6 is given. */
11 /* ANSI C standard includes */
26 #include <sys/types.h>
28 #include <netinet/in_systm.h>
29 #include <netinet/in.h>
30 #include <netinet/ip.h>
32 #ifdef HAVE_NETINET_IP_VAR_H
33 #include <netinet/ip_var.h>
37 #include <arpa/inet.h>
39 #include <sys/resource.h>
40 #include <sys/socket.h>
52 #define S_ADDR_TYPE u_long
62 /*************************************************
63 * SIGALRM handler - crash out *
64 *************************************************/
67 sigalrm_handler(int sig)
69 sig = sig; /* Keep picky compilers happy */
70 printf("\nServer timed out\n");
75 /*************************************************
76 * Get textual IP address *
77 *************************************************/
79 /* This function is copied from Exim */
82 host_ntoa(const void *arg, char *buffer)
86 /* The new world. It is annoying that we have to fish out the address from
87 different places in the block, depending on what kind of address it is. It
88 is also a pain that inet_ntop() returns a const char *, whereas the IPv4
89 function inet_ntoa() returns just char *, and some picky compilers insist
90 on warning if one assigns a const char * to a char *. Hence the casts. */
94 int family = ((struct sockaddr *)arg)->sa_family;
95 if (family == AF_INET6)
97 struct sockaddr_in6 *sk = (struct sockaddr_in6 *)arg;
98 yield = (char *)inet_ntop(family, &(sk->sin6_addr), addr_buffer,
103 struct sockaddr_in *sk = (struct sockaddr_in *)arg;
104 yield = (char *)inet_ntop(family, &(sk->sin_addr), addr_buffer,
105 sizeof(addr_buffer));
108 /* If the result is a mapped IPv4 address, show it in V4 format. */
110 if (strncmp(yield, "::ffff:", 7) == 0) yield += 7;
112 #else /* HAVE_IPV6 */
116 yield = inet_ntoa(((struct sockaddr_in *)arg)->sin_addr);
119 strcpy(buffer, yield);
124 /*************************************************
126 *************************************************/
128 #define v6n 0 /* IPv6 socket number */
129 #define v4n 1 /* IPv4 socket number */
130 #define udn 2 /* Unix domain socket number */
131 #define skn 2 /* Potential number of sockets */
133 int main(int argc, char **argv)
137 int listen_socket[3] = { -1, -1, -1 };
139 int dup_accept_socket;
140 int connection_count = 1;
153 char *sockname = NULL;
154 unsigned char buffer[10240];
156 struct sockaddr_un sockun; /* don't use "sun" */
157 struct sockaddr_un sockun_accepted;
158 int sockun_len = sizeof(sockun_accepted);
161 struct sockaddr_in6 sin6;
162 struct sockaddr_in6 accepted;
163 struct in6_addr anyaddr6 = IN6ADDR_ANY_INIT ;
165 struct sockaddr_in accepted;
168 /* Always need an IPv4 structure */
170 struct sockaddr_in sin4;
172 int len = sizeof(accepted);
175 /* Sort out the arguments */
177 while (na < argc && argv[na][0] == '-')
179 if (strcmp(argv[na], "-d") == 0) debug = 1;
180 else if (strcmp(argv[na], "-t") == 0) timeout = atoi(argv[++na]);
181 else if (strcmp(argv[na], "-noipv4") == 0) use_ipv4 = 0;
182 else if (strcmp(argv[na], "-noipv6") == 0) use_ipv6 = 0;
185 printf("server: unknown option %s\n", argv[na]);
191 if (!use_ipv4 && !use_ipv6)
193 printf("server: -noipv4 and -noipv6 cannot both be given\n");
199 printf("server: no port number or socket name given\n");
203 if (argv[na][0] == '/')
206 unlink(sockname); /* in case left lying around */
208 else port = atoi(argv[na]);
211 if (na < argc) connection_count = atoi(argv[na]);
216 if (port == 0) /* Unix domain */
218 if (debug) printf("Creating Unix domain socket\n");
219 listen_socket[udn] = socket(PF_UNIX, SOCK_STREAM, 0);
220 if (listen_socket[udn] < 0)
222 printf("Unix domain socket creation failed: %s\n", strerror(errno));
231 if (debug) printf("Creating IPv6 socket\n");
232 listen_socket[v6n] = socket(AF_INET6, SOCK_STREAM, 0);
233 if (listen_socket[v6n] < 0)
235 printf("IPv6 socket creation failed: %s\n", strerror(errno));
239 /* If this is an IPv6 wildcard socket, set IPV6_V6ONLY if that option is
243 if (setsockopt(listen_socket[v6n], IPPROTO_IPV6, IPV6_V6ONLY, (char *)(&on),
245 printf("Setting IPV6_V6ONLY on IPv6 wildcard "
246 "socket failed (%s): carrying on without it\n", strerror(errno));
247 #endif /* IPV6_V6ONLY */
249 #endif /* HAVE_IPV6 */
251 /* Create an IPv4 socket if required */
255 if (debug) printf("Creating IPv4 socket\n");
256 listen_socket[v4n] = socket(AF_INET, SOCK_STREAM, 0);
257 if (listen_socket[v4n] < 0)
259 printf("IPv4 socket creation failed: %s\n", strerror(errno));
266 /* Set SO_REUSEADDR on the IP sockets so that the program can be restarted
267 while a connection is being handled - this can happen as old connections lie
268 around for a bit while crashed processes are tidied away. Without this, a
269 connection will prevent reuse of the smtp port for listening. */
271 for (i = v6n; i <= v4n; i++)
273 if (listen_socket[i] >= 0 &&
274 setsockopt(listen_socket[i], SOL_SOCKET, SO_REUSEADDR, (char *)(&on),
277 printf("setting SO_REUSEADDR on socket failed: %s\n", strerror(errno));
283 /* Now bind the sockets to the required port or path. If a path, ensure
284 anyone can write to it. */
289 sockun.sun_family = AF_UNIX;
290 if (debug) printf("Binding Unix domain socket\n");
291 sprintf(sockun.sun_path, "%.*s", (int)(sizeof(sockun.sun_path)-1), sockname);
292 if (bind(listen_socket[udn], (struct sockaddr *)&sockun, sizeof(sockun)) < 0)
294 printf("Unix domain socket bind() failed: %s\n", strerror(errno));
297 (void)stat(sockname, &statbuf);
298 if (debug) printf("Setting Unix domain socket mode: %0x\n",
299 statbuf.st_mode | 0777);
300 if (chmod(sockname, statbuf.st_mode | 0777) < 0)
302 printf("Unix domain socket chmod() failed: %s\n", strerror(errno));
309 for (i = 0; i < skn; i++)
311 if (listen_socket[i] < 0) continue;
313 /* For an IPv6 listen, use an IPv6 socket */
318 memset(&sin6, 0, sizeof(sin6));
319 sin6.sin6_family = AF_INET6;
320 sin6.sin6_port = htons(port);
321 sin6.sin6_addr = anyaddr6;
322 if (bind(listen_socket[i], (struct sockaddr *)&sin6, sizeof(sin6)) < 0)
324 printf("IPv6 socket bind() failed: %s\n", strerror(errno));
331 /* For an IPv4 bind, use an IPv4 socket, even in an IPv6 world. If an IPv4
332 bind fails EADDRINUSE after IPv6 success, carry on, because it means the
333 IPv6 socket will handle IPv4 connections. */
336 memset(&sin4, 0, sizeof(sin4));
337 sin4.sin_family = AF_INET;
338 sin4.sin_addr.s_addr = (S_ADDR_TYPE)INADDR_ANY;
339 sin4.sin_port = htons(port);
340 if (bind(listen_socket[i], (struct sockaddr *)&sin4, sizeof(sin4)) < 0)
342 if (listen_socket[v6n] < 0 || errno != EADDRINUSE)
344 printf("IPv4 socket bind() failed: %s\n", strerror(errno));
349 close(listen_socket[i]);
350 listen_socket[i] = -1;
358 /* Start listening. If IPv4 fails EADDRINUSE after IPv6 succeeds, ignore the
359 error because it means that the IPv6 socket will handle IPv4 connections. Don't
360 output anything, because it will mess up the test output, which will be
361 different for systems that do this and those that don't. */
363 for (i = 0; i <= skn; i++)
365 if (listen_socket[i] >= 0 && listen(listen_socket[i], 5) < 0)
367 if (i != v4n || listen_socket[v6n] < 0 || errno != EADDRINUSE)
369 printf("listen() failed: %s\n", strerror(errno));
376 /* This program handles only a fixed number of connections, in sequence. Before
377 waiting for the first connection, read the standard input, which contains the
378 script of things to do. A line containing "++++" is treated as end of file.
379 This is so that the Perl driving script doesn't have to close the pipe -
380 because that would cause it to wait for this process, which it doesn't yet want
381 to do. The driving script adds the "++++" automatically - it doesn't actually
382 appear in the test script. */
384 while (fgets(buffer, sizeof(buffer), stdin) != NULL)
387 int n = (int)strlen(buffer);
388 while (n > 0 && isspace(buffer[n-1])) n--;
390 if (strcmp(buffer, "++++") == 0) break;
391 next = malloc(sizeof(line) + n);
393 strcpy(next->line, buffer);
394 if (last == NULL) script = last = next;
395 else last->next = next;
401 /* SIGALRM handler crashes out */
403 signal(SIGALRM, sigalrm_handler);
405 /* s points to the current place in the script */
409 for (count = 0; count < connection_count; count++)
414 printf("Listening on %s ... ", sockname);
416 accept_socket = accept(listen_socket[udn],
417 (struct sockaddr *)&sockun_accepted, &sockun_len);
424 fd_set select_listen;
426 printf("Listening on port %d ... ", port);
429 FD_ZERO(&select_listen);
430 for (i = 0; i < skn; i++)
432 if (listen_socket[i] >= 0) FD_SET(listen_socket[i], &select_listen);
433 if (listen_socket[i] > max_socket) max_socket = listen_socket[i];
436 lcount = select(max_socket + 1, &select_listen, NULL, NULL, NULL);
439 printf("Select failed\n");
445 for (i = 0; i < skn; i++)
447 if (listen_socket[i] > 0 && FD_ISSET(listen_socket[i], &select_listen))
449 accept_socket = accept(listen_socket[i],
450 (struct sockaddr *)&accepted, &len);
451 FD_CLR(listen_socket[i], &select_listen);
458 if (accept_socket < 0)
460 printf("accept() failed: %s\n", strerror(errno));
464 out = fdopen(accept_socket, "w");
466 dup_accept_socket = dup(accept_socket);
469 printf("\nConnection request from [%s]\n", host_ntoa(&accepted, buffer));
472 printf("\nConnection request\n");
474 /* Linux supports a feature for acquiring the peer's credentials, but it
475 appears to be Linux-specific. This code is untested and unused, just
476 saved here for reference. */
478 /**********--------------------
482 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &cl)==0) {
483 printf("Peer's pid=%d, uid=%d, gid=%d\n",
484 cr.pid, cr.uid, cr.gid);
485 --------------*****************/
488 if (dup_accept_socket < 0)
490 printf("Couldn't dup socket descriptor\n");
491 printf("421 Connection refused: %s\n", strerror(errno));
492 fprintf(out, "421 Connection refused: %s\r\n", strerror(errno));
497 in = fdopen(dup_accept_socket, "r");
499 /* Loop for handling the conversation(s). For use in SMTP sessions, there are
500 default rules for determining input and output lines: the latter start with
501 digits. This means that the input looks like SMTP dialog. However, this
502 doesn't work for other tests (e.g. ident tests) so we have explicit '<' and
503 '>' flags for input and output as well as the defaults. */
505 for (; s != NULL; s = s->next)
509 /* Output lines either start with '>' or a digit. In the '>' case we can
510 fudge the sending of \r\n as required. Default is \r\n, ">>" send nothing,
511 ">CR>" sends \r only, and ">LF>" sends \n only. We can also force a
512 connection closedown by ">*eof". */
517 printf("%s\n", ss++);
519 if (strncmp(ss, "*eof", 4) == 0)
527 else if (strncmp(ss, "CR>", 3) == 0)
528 { end = "\r"; ss += 3; }
529 else if (strncmp(ss, "LF>", 3) == 0)
530 { end = "\n"; ss += 3; }
532 fprintf(out, "%s%s", ss, end);
535 else if (isdigit((unsigned char)ss[0]))
538 fprintf(out, "%s\r\n", ss);
541 /* If the script line starts with "*sleep" we just sleep for a while
542 before continuing. */
544 else if (strncmp(ss, "*sleep ", 7) == 0)
546 int sleepfor = atoi(ss+7);
552 /* Otherwise the script line is the start of an input line we are expecting
553 from the client, or "*eof" indicating we expect the client to close the
554 connection. Read command line or data lines; the latter are indicated
555 by the expected line being just ".". If the line starts with '<', that
556 doesn't form part of the expected input. (This allows for incoming data
557 starting with a digit.) */
562 int data = strcmp(ss, ".") == 0;
577 if (fgets(buffer+offset, sizeof(buffer)-offset, in) == NULL)
579 printf("%sxpected EOF read from client\n",
580 (strncmp(ss, "*eof", 4) == 0)? "E" : "Une");
585 n = (int)strlen(buffer);
586 while (n > 0 && isspace(buffer[n-1])) n--;
588 printf("%s\n", buffer);
589 if (!data || strcmp(buffer, ".") == 0) break;
592 if (strncmp(ss, buffer, (int)strlen(ss)) != 0)
594 printf("Comparison failed - bailing out\n");
595 printf("Expected: %s\n", ss);
606 if (s == NULL) printf("End of script\n");
608 if (sockname != NULL) unlink(sockname);
612 /* End of server.c */