X-Git-Url: http://andersk.mit.edu/gitweb/openssh.git/blobdiff_plain/2b87da3b1fb7c5ca907cb65aa048fada4ad29803..eea098a35a7ed7fd460ffb549c241aa9d47c2919:/canohost.c diff --git a/canohost.c b/canohost.c index 87f56054..6e6a0450 100644 --- a/canohost.c +++ b/canohost.c @@ -12,20 +12,21 @@ */ #include "includes.h" -RCSID("$OpenBSD: canohost.c,v 1.20 2001/02/03 10:08:37 markus Exp $"); +RCSID("$OpenBSD: canohost.c,v 1.27 2001/06/23 15:12:17 itojun Exp $"); #include "packet.h" #include "xmalloc.h" #include "log.h" +#include "canohost.h" -void check_ip_options(int socket, char *ipaddr); +static void check_ip_options(int, char *); /* * Return the canonical name of the host at the other end of the socket. The * caller should free the returned string with xfree. */ -char * +static char * get_remote_hostname(int socket, int reverse_mapping_check) { struct sockaddr_storage from; @@ -70,6 +71,7 @@ get_remote_hostname(int socket, int reverse_mapping_check) NULL, 0, NI_NUMERICHOST) != 0) fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed"); + debug3("Trying to reverse map address %.100s.", ntop); /* Map the IP address to a host name. */ if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name), NULL, 0, NI_NAMEREQD) != 0) { @@ -138,13 +140,13 @@ get_remote_hostname(int socket, int reverse_mapping_check) * exit here if we detect any IP options. */ /* IPv4 only */ -void +static void check_ip_options(int socket, char *ipaddr) { - u_char options[200], *ucp; - char text[1024], *cp; + u_char options[200]; + char text[sizeof(options) * 3 + 1]; socklen_t option_size; - int ipproto; + int i, ipproto; struct protoent *ip; if ((ip = getprotobyname("ip")) != NULL) @@ -154,10 +156,10 @@ check_ip_options(int socket, char *ipaddr) option_size = sizeof(options); if (getsockopt(socket, ipproto, IP_OPTIONS, (void *)options, &option_size) >= 0 && option_size != 0) { - cp = text; - /* Note: "text" buffer must be at least 3x as big as options. */ - for (ucp = options; option_size > 0; ucp++, option_size--, cp += 3) - sprintf(cp, " %2.2x", *ucp); + text[0] = '\0'; + for (i = 0; i < option_size; i++) + snprintf(text + i*3, sizeof(text) - i*3, + " %2.2x", options[i]); log("Connection from %.100s with IP options:%.800s", ipaddr, text); packet_disconnect("Connection from %.100s with IP options:%.800s", @@ -200,30 +202,59 @@ get_canonical_hostname(int reverse_mapping_check) * Returns the remote IP-address of socket as a string. The returned * string must be freed. */ - -char * -get_peer_ipaddr(int socket) +static char * +get_socket_address(int socket, int remote, int flags) { - struct sockaddr_storage from; - socklen_t fromlen; + struct sockaddr_storage addr; + socklen_t addrlen; char ntop[NI_MAXHOST]; /* Get IP address of client. */ - fromlen = sizeof(from); - memset(&from, 0, sizeof(from)); - if (getpeername(socket, (struct sockaddr *) & from, &fromlen) < 0) { - debug("get_peer_ipaddr: getpeername failed: %.100s", strerror(errno)); - return NULL; + addrlen = sizeof(addr); + memset(&addr, 0, sizeof(addr)); + + if (remote) { + if (getpeername(socket, (struct sockaddr *)&addr, &addrlen) + < 0) { + debug("get_socket_ipaddr: getpeername failed: %.100s", + strerror(errno)); + return NULL; + } + } else { + if (getsockname(socket, (struct sockaddr *)&addr, &addrlen) + < 0) { + debug("get_socket_ipaddr: getsockname failed: %.100s", + strerror(errno)); + return NULL; + } } - /* Get the IP address in ascii. */ - if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop), - NULL, 0, NI_NUMERICHOST) != 0) { - error("get_peer_ipaddr: getnameinfo NI_NUMERICHOST failed"); + /* Get the address in ascii. */ + if (getnameinfo((struct sockaddr *)&addr, addrlen, ntop, sizeof(ntop), + NULL, 0, flags) != 0) { + error("get_socket_ipaddr: getnameinfo %d failed", flags); return NULL; } return xstrdup(ntop); } +char * +get_peer_ipaddr(int socket) +{ + return get_socket_address(socket, 1, NI_NUMERICHOST); +} + +char * +get_local_ipaddr(int socket) +{ + return get_socket_address(socket, 0, NI_NUMERICHOST); +} + +char * +get_local_name(int socket) +{ + return get_socket_address(socket, 0, NI_NAMEREQD); +} + /* * Returns the IP-address of the remote host as a string. The returned * string must not be freed. @@ -249,9 +280,20 @@ get_remote_ipaddr() return canonical_host_ip; } +const char * +get_remote_name_or_ip(u_int utmp_len, int reverse_mapping_check) +{ + static const char *remote = ""; + if (utmp_len > 0) + remote = get_canonical_hostname(reverse_mapping_check); + if (utmp_len == 0 || strlen(remote) > utmp_len) + remote = get_remote_ipaddr(); + return remote; +} + /* Returns the local/remote port for the socket. */ -int +static int get_sock_port(int sock, int local) { struct sockaddr_storage from; @@ -281,7 +323,7 @@ get_sock_port(int sock, int local) /* Returns remote/local port number for the current connection. */ -int +static int get_port(int local) { /*