1 /* $OpenBSD: canohost.c,v 1.77 2023/03/31 04:42:29 dtucker Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * Functions for returning the canonical host name of the remote site.
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
17 #include <sys/types.h>
18 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
39 ipv64_normalise_mapped(struct sockaddr_storage
*addr
, socklen_t
*len
)
41 struct sockaddr_in6
*a6
= (struct sockaddr_in6
*)addr
;
42 struct sockaddr_in
*a4
= (struct sockaddr_in
*)addr
;
43 struct in_addr inaddr
;
46 if (addr
->ss_family
!= AF_INET6
||
47 !IN6_IS_ADDR_V4MAPPED(&a6
->sin6_addr
))
50 debug3("Normalising mapped IPv4 in IPv6 address");
52 memcpy(&inaddr
, ((char *)&a6
->sin6_addr
) + 12, sizeof(inaddr
));
55 memset(a4
, 0, sizeof(*a4
));
57 a4
->sin_family
= AF_INET
;
59 memcpy(&a4
->sin_addr
, &inaddr
, sizeof(inaddr
));
64 * Returns the local/remote IP-address/hostname of socket as a string.
65 * The returned string must be freed.
68 get_socket_address(int sock
, int remote
, int flags
)
70 struct sockaddr_storage addr
;
72 char ntop
[NI_MAXHOST
];
78 /* Get IP address of client. */
79 addrlen
= sizeof(addr
);
80 memset(&addr
, 0, sizeof(addr
));
83 if (getpeername(sock
, (struct sockaddr
*)&addr
, &addrlen
) != 0)
86 if (getsockname(sock
, (struct sockaddr
*)&addr
, &addrlen
) != 0)
90 /* Work around Linux IPv6 weirdness */
91 if (addr
.ss_family
== AF_INET6
) {
92 addrlen
= sizeof(struct sockaddr_in6
);
93 ipv64_normalise_mapped(&addr
, &addrlen
);
96 switch (addr
.ss_family
) {
99 /* Get the address in ascii. */
100 if ((r
= getnameinfo((struct sockaddr
*)&addr
, addrlen
, ntop
,
101 sizeof(ntop
), NULL
, 0, flags
)) != 0) {
102 error_f("getnameinfo %d failed: %s",
103 flags
, ssh_gai_strerror(r
));
106 return xstrdup(ntop
);
108 /* Get the Unix domain socket path. */
109 return xstrdup(((struct sockaddr_un
*)&addr
)->sun_path
);
111 /* We can't look up remote Unix domain sockets. */
117 get_peer_ipaddr(int sock
)
121 if ((p
= get_socket_address(sock
, 1, NI_NUMERICHOST
)) != NULL
)
123 return xstrdup("UNKNOWN");
127 get_local_ipaddr(int sock
)
131 if ((p
= get_socket_address(sock
, 0, NI_NUMERICHOST
)) != NULL
)
133 return xstrdup("UNKNOWN");
137 get_local_name(int fd
)
139 char *host
, myname
[NI_MAXHOST
];
141 /* Assume we were passed a socket */
142 if ((host
= get_socket_address(fd
, 0, NI_NAMEREQD
)) != NULL
)
145 /* Handle the case where we were passed a pipe */
146 if (gethostname(myname
, sizeof(myname
)) == -1) {
147 verbose_f("gethostname: %s", strerror(errno
));
148 host
= xstrdup("UNKNOWN");
150 host
= xstrdup(myname
);
156 /* Returns the local/remote port for the socket. */
159 get_sock_port(int sock
, int local
)
161 struct sockaddr_storage from
;
163 char strport
[NI_MAXSERV
];
168 /* Get IP address of client. */
169 fromlen
= sizeof(from
);
170 memset(&from
, 0, sizeof(from
));
172 if (getsockname(sock
, (struct sockaddr
*)&from
, &fromlen
) == -1) {
173 error("getsockname failed: %.100s", strerror(errno
));
177 if (getpeername(sock
, (struct sockaddr
*)&from
, &fromlen
) == -1) {
178 debug("getpeername failed: %.100s", strerror(errno
));
183 /* Work around Linux IPv6 weirdness */
184 if (from
.ss_family
== AF_INET6
)
185 fromlen
= sizeof(struct sockaddr_in6
);
187 /* Non-inet sockets don't have a port number. */
188 if (from
.ss_family
!= AF_INET
&& from
.ss_family
!= AF_INET6
)
191 /* Return port number. */
192 if ((r
= getnameinfo((struct sockaddr
*)&from
, fromlen
, NULL
, 0,
193 strport
, sizeof(strport
), NI_NUMERICSERV
)) != 0)
194 fatal_f("getnameinfo NI_NUMERICSERV failed: %s",
195 ssh_gai_strerror(r
));
196 return atoi(strport
);
200 get_peer_port(int sock
)
202 return get_sock_port(sock
, 0);
206 get_local_port(int sock
)
208 return get_sock_port(sock
, 1);