Debian Codegraph @

tests/addrpeek.c

raw
1 /* addrpeek - testing service for Inetd: remote address, environment vars
2 Copyright (C) 2010-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /* Written by Mats Erik Andersson. */
20
21 /* Addrpeek is a test client for examining Inetd, primarily TCP traffic.
22 * The client executes all those tasks that were listed as server
23 * arguments in the configuration file for Inetd:
24 *
25 * addr : Reply with "Your address is $IP."
26 * env : Reply with all known environment variables and their values.
27 *
28 * Reasonable entries in `inetf.conf' could be
29 *
30 * # Return numerical address of the calling client.
31 * #
32 * 7890 stream tcp nowait nobody /tmp/addrpeek addrpeek addr
33 * 7890 stream tcp6 nowait nobody /tmp/addrpeek addrpeek addr
34 * #
35 * # Display all environment variables in use, and append
36 * # the client's address at the end.
37 * #
38 * tcpmux stream tcp nowait nobody internal
39 * tcpmux stream tcp6 nowait nobody internal
40 * tcpmux/env stream tcp nowait nobody /tmp/addrpeek addrkeep env addr
41 */
42
43 #include <config.h>
44
45 #include <stdio.h>
46 #include <unistd.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <netdb.h>
53 #include <progname.h>
54
55 #ifndef SEPARATOR
56 # define SEPARATOR "\n"
57 #endif
58
59 static void
60 write_address (int fd)
61 {
62 int type;
63 size_t len;
64 socklen_t sslen;
65 char addr[INET6_ADDRSTRLEN], answer[128];
66 struct sockaddr_storage ss;
67
68 sslen = sizeof (type);
69 getsockopt (fd, SOL_SOCKET, SO_TYPE, &type, &sslen);
70
71 if (type == SOCK_STREAM)
72 {
73 sslen = sizeof (ss);
74 getpeername (fd, (struct sockaddr *) &ss, &sslen);
75 }
76 else if (type == SOCK_DGRAM)
77 {
78 sslen = sizeof (ss);
79 recvfrom (fd, answer, sizeof (answer), 0,
80 (struct sockaddr *) &ss, &sslen);
81 shutdown (fd, SHUT_RD);
82 }
83 else
84 return;
85
86 getnameinfo ((struct sockaddr *) &ss, sslen, addr, sizeof (addr),
87 NULL, 0, NI_NUMERICHOST);
88
89 len = snprintf (answer, sizeof (answer),
90 "Your address is %s." SEPARATOR, addr);
91
92 sendto (fd, answer, len, 0, (struct sockaddr *) &ss, sslen);
93 }
94
95 void
96 write_environment (int fd, char *envp[])
97 {
98 for (; *envp; ++envp)
99 {
100 write (fd, *envp, strlen (*envp));
101 write (fd, SEPARATOR, strlen (SEPARATOR));
102 }
103 }
104
105 int
106 main (int argc, char *argv[])
107 {
108 int j;
109 set_program_name (argv[0]);
110 for (j = 1; j < argc; ++j)
111 {
112 if (strncmp (argv[j], "addr", strlen ("addr")) == 0)
113 {
114 write_address (STDOUT_FILENO);
115 continue;
116 }
117
118 if (strncmp (argv[j], "env", strlen ("env")) == 0)
119 {
120 write_environment (STDOUT_FILENO, environ);
121 continue;
122 }
123 }
124
125 close (STDIN_FILENO);
126 close (STDOUT_FILENO);
127 close (STDERR_FILENO);
128
129 return EXIT_SUCCESS;
130 }