]> andersk Git - openssh.git/blobdiff - canohost.c
- jakob@cvs.openbsd.org 2001/07/31 09:28:44
[openssh.git] / canohost.c
index 52921f5b01526f918ce86a04883d1cd32f76ed34..6e6a04505e97c2244342baca4cd8a81aed33975c 100644 (file)
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: canohost.c,v 1.23 2001/02/10 01:33:32 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;
@@ -71,7 +71,7 @@ get_remote_hostname(int socket, int reverse_mapping_check)
             NULL, 0, NI_NUMERICHOST) != 0)
                fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed");
 
-       debug("Trying to reverse map address %.100s.", ntop);
+       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) {
@@ -140,7 +140,7 @@ 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];
@@ -202,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.
@@ -251,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;
@@ -283,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)
 {
        /*
This page took 0.041349 seconds and 4 git commands to generate.