Hello,

I have found the attached client very useful in verifying
the functionality of Inetd. It is pushed into

    tests/addrpeek.c

and it momentarily can test address of remote host, as well
as listing all environment variables in use.

The source is registered in `tests/Makefile.am' and compiles
well in GNU/Linux, using `make -C tests/ addrpeek'. However,
there is some sort of bug in GNUlib that causes OpenBSD to
abort the corresponding action with a complaint

    CC    addrpeek.o
  In file included from ../lib/unistd.h:103,
                   from addrpeek.c:30:
  ../lib/getopt.h:196: error: redefinition of `struct option'

Since no compilation is performed in "tests/" by the standard
targets (this is a bug) in the present state of IU, I leave the
insertion of Addrpeek in Makefile.am as it is for now. Someone
with knowledge of Autotools should mend this situation.

Observe, though, that "gcc -Wall tests/addrpeek.c" works on
OpenBSD without any complaint, so it is the default linking
against GNUlib that casuses all problems. The new client is
contructed in order not to depend on GNUlib at all.

Best regards,

Mats
From 66389037d1e57097c9373f38aa09bbe6338162fe Mon Sep 17 00:00:00 2001
From: Mats Erik Andersson <[email protected]>
Date: Tue, 9 Nov 2010 12:54:52 +0100
Subject: [PATCH] New test client for Inetd.

---
 ChangeLog         |    6 +++
 tests/Makefile.am |    4 ++
 tests/addrpeek.c  |  112 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 122 insertions(+), 0 deletions(-)
 create mode 100644 tests/addrpeek.c

diff --git a/ChangeLog b/ChangeLog
index fadabfc..cf06cfa 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2010-11-09  Mats Erik Andersson <[email protected]>
+
+	* tests/addrpeek.c: New file.
+	* tests/Makefile.am: Register `addrpeek' using
+	`check_PROGRAMS', but conditioned on `ENABLE_inetd'.
+
 2010-11-08  Mats Erik Andersson <[email protected]>
 
 	* src/inetd.c (prepenv): New prototype:
diff --git a/tests/Makefile.am b/tests/Makefile.am
index cce19ee..1327c5e 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -21,6 +21,10 @@ AM_CPPFLAGS = -I$(top_srcdir)/libinetutils -I$(top_srcdir)/lib
 LDADD = -L../libinetutils -linetutils  ../lib/libgnu.a
 
 check_PROGRAMS = localhost
+if ENABLE_inetd
+check_PROGRAMS += addrpeek
+endif
+
 dist_check_SCRIPTS =
 if ENABLE_ping
 dist_check_SCRIPTS += ping-localhost.sh
diff --git a/tests/addrpeek.c b/tests/addrpeek.c
new file mode 100644
index 0000000..d68545a
--- /dev/null
+++ b/tests/addrpeek.c
@@ -0,0 +1,112 @@
+/*
+  Copyright (C) 2010 Free Software Foundation, Inc.
+
+  This file is part of GNU Inetutils.
+
+  GNU Inetutils is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or (at
+  your option) any later version.
+
+  GNU Inetutils 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
+  General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see `http://www.gnu.org/licenses/'. */
+
+/* Written by Mats Erik Andersson.  */
+
+/* Addrpeek is a test client for examining Inetd, primarily TCP traffic.
+ * The client executes all those tasks that were listed as server
+ * arguments in the configuration file for Inetd:
+ *
+ *    addr : Reply with "Your address is $IP."
+ *    env  : Reply with all known environment variables and their values.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+
+#ifndef SEPARATOR
+# define SEPARATOR "\n"
+#endif
+
+static void
+write_address (int fd)
+{
+  int type;
+  size_t len;
+  socklen_t sslen;
+  char addr[INET6_ADDRSTRLEN], answer[128];
+  struct sockaddr_storage ss;
+
+  sslen = sizeof (type);
+  getsockopt (fd, SOL_SOCKET, SO_TYPE, &type, &sslen);
+
+  if (type == SOCK_STREAM)
+    {
+      sslen = sizeof (ss);
+      getpeername (fd, (struct sockaddr *) &ss, &sslen);
+    }
+  else if (type == SOCK_DGRAM)
+    {
+      sslen = sizeof (ss);
+      recvfrom (fd, answer, sizeof (answer), 0,
+                  (struct sockaddr *) &ss, &sslen);
+      shutdown (fd, SHUT_RD);
+    }
+  else
+      return;
+
+  getnameinfo ((struct sockaddr *) &ss, sslen, addr, sizeof (addr),
+                NULL, 0, NI_NUMERICHOST);
+
+  len = snprintf (answer, sizeof (answer),
+                  "Your address is %s." SEPARATOR, addr);
+
+  sendto (fd, answer, len, 0, (struct sockaddr *) &ss, sslen);
+}
+
+void
+write_environment (int fd, char *envp[])
+{
+  for ( ; *envp; ++envp)
+    {
+      write (fd, *envp, strlen (*envp));
+      write (fd, SEPARATOR, strlen (SEPARATOR));
+    }
+}
+
+int
+main (int argc, char *argv[], char *envp[])
+{
+  int j;
+
+  for (j = 1; j < argc; ++j)
+    {
+      if (strncmp (argv[j], "addr", strlen ("addr")) == 0)
+        {
+          write_address (STDOUT_FILENO);
+          continue;
+        }
+      if (strncmp (argv[j], "env", strlen ("env")) == 0)
+        {
+          write_environment (STDOUT_FILENO, envp);
+          continue;
+        }
+    }
+
+  close (STDIN_FILENO);
+  close (STDOUT_FILENO);
+  close (STDERR_FILENO);
+
+  return EXIT_SUCCESS;
+}
-- 
1.7.1

Attachment: signature.asc
Description: Digital signature

Reply via email to