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
|
/* addrpeek - testing service for Inetd: remote address, environment vars
Copyright (C) 2010-2025 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.
*
* Reasonable entries in `inetf.conf' could be
*
* # Return numerical address of the calling client.
* #
* 7890 stream tcp nowait nobody /tmp/addrpeek addrpeek addr
* 7890 stream tcp6 nowait nobody /tmp/addrpeek addrpeek addr
* #
* # Display all environment variables in use, and append
* # the client's address at the end.
* #
* tcpmux stream tcp nowait nobody internal
* tcpmux stream tcp6 nowait nobody internal
* tcpmux/env stream tcp nowait nobody /tmp/addrpeek addrkeep env addr
*/
#include <config.h>
#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>
#include <progname.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[])
{
int j;
set_program_name (argv[0]);
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, environ);
continue;
}
}
close (STDIN_FILENO);
close (STDOUT_FILENO);
close (STDERR_FILENO);
return EXIT_SUCCESS;
}
|