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
|
/*
getpeername.c, part of
faucet and hose: network pipe utilities
Copyright (C) 1995-98 Robert Forsman
This program 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 2 of the License, or
(at your option) any later version.
This program 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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
static char info[] = "getpeername: a network utility for sockets\nWritten 1995-98 by Robert Forsman <thoth@cis.ufl.edu>\n";
#include <stdio.h>
#include <string.h>
#include <errno.h>
extern int errno; /* I hate the errno header file */
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#ifndef NOUNIXSOCKETS
#include <sys/un.h>
#endif
#include <netinet/in.h>
#include <netdb.h>
#include "common.h"
void usage()
{
fprintf(stderr, "Usage: %s [ -verbose ] [ fd# ]\n", progname);
}
int getpeername();
int getsockname();
int main(argc, argv)
int argc;
char **argv;
{
int i;
int peer_not_sock = 1;
int fd = 0; /* assume socket is attached to stdin.
(if it's attached to stdout, our
output will go over the socket) */
int verbose=0;
set_progname(argv[0]);
if (0==strcmp(progname + strlen(progname) - 11, "getsockname") &&
(strlen(progname)<12 || progname[strlen(progname)-12] == '/'))
peer_not_sock = 0;
for (i=1; i<argc; i++) {
char *name = argv[i];
while( *name && *name=='-' )
++name;
if (0==strncmp(name, "verbose", strlen(name))) {
verbose++;
} else if (0==strncmp(name, "sock", strlen(name))) {
peer_not_sock=0;
} else if (0==strncmp(name, "peer", strlen(name))) {
peer_not_sock=1;
} else {
break;
}
}
if (i<argc) {
fd = atoi(argv[i++]);
}
if (i<argc) {
fprintf(stderr, "Too many arguments\n");
usage();
exit(1);
}
if (verbose>1) {
emit_version("getpeername", 1995);
}
{
union {
struct sockaddr_storage base;
struct sockaddr_in in;
struct sockaddr_in6 in6;
#ifndef NOUNIXSOCKETS
struct sockaddr_un un;
#endif
} saddr;
struct sockaddr_in *sinp = &saddr.in;
struct sockaddr_in6 *sin6p = &saddr.in6;
#ifndef NOUNIXSOCKETS
struct sockaddr_un *sunp = &saddr.un;
#endif
int saddrlen = sizeof(saddr);
int (*f)();
char *name;
if (peer_not_sock) {
f = getpeername;
name = "peer";
} else {
f = getsockname;
name = "sock";
}
memset(&saddr, 0, sizeof(saddr));
if (0!= f(fd, &saddr, &saddrlen)) {
fprintf(stderr, "%s: get%sname failed on descriptor %d: ", progname, name, fd);
perror("");
exit(1);
}
#ifndef NOUNIXSOCKETS
if (saddr.base.ss_family == AF_UNIX) {
if (verbose)
printf("UNIX, path ");
printf("%s\n", (*sunp->sun_path == '\0') ? "(abstract)"
: (*sunp->sun_path == '/') ? sunp->sun_path
: "(unnamed)");
} else
#endif
if (saddr.base.ss_family == AF_INET ||
saddr.base.ss_family == AF_INET6) {
const int is_inet6 = ( saddr.base.ss_family == AF_INET6 );
if (verbose)
printf("Internet, port ");
printf("%d\n", ntohs( is_inet6 ? sin6p->sin6_port
: sinp->sin_port));
if (verbose)
printf("Host ");
{
char ip[INET6_ADDRSTRLEN];
getnameinfo((struct sockaddr *) &saddr.base,
sizeof(struct sockaddr_storage),
ip, sizeof(ip), NULL, 0, NI_NUMERICHOST);
printf("%s%s", ip, verbose ? " " : "\n");
}
if (verbose) {
char host[NI_MAXHOST];
getnameinfo((struct sockaddr *) &saddr.base,
sizeof(struct sockaddr_storage),
host, sizeof(host), NULL, 0, 0);
printf("%s\n", host);
}
} else {
fprintf(stderr, "%s: unknown address family (%d) returned by get%sname\n", progname, saddr.base.ss_family, name);
}
}
return 0;
}
|