blob: 4566e2ab15fb7efcf3966dc646cc21befb50042c [file] [log] [blame]
Darren Tucker39972492006-07-12 22:22:46 +10001/* $OpenBSD: canohost.c,v 1.56 2006/07/11 20:07:25 stevesk Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for returning the canonical host name of the remote site.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * 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".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerc7b06362006-03-15 11:53:45 +110016
Damien Millere3b60b52006-07-10 21:08:03 +100017#include <sys/types.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100018#include <sys/socket.h>
19
20#include <netinet/in.h>
21
Damien Millerc7b06362006-03-15 11:53:45 +110022#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100023#include <errno.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100024
25#include "packet.h"
26#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000027#include "log.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000028#include "canohost.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100029
Ben Lindstrombba81212001-06-25 05:01:22 +000030static void check_ip_options(int, char *);
Damien Miller33804262001-02-04 23:20:18 +110031
Damien Miller5428f641999-11-25 11:54:57 +110032/*
33 * Return the canonical name of the host at the other end of the socket. The
34 * caller should free the returned string with xfree.
35 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036
Ben Lindstrombba81212001-06-25 05:01:22 +000037static char *
Darren Tucker3f9fdc72004-06-22 12:56:01 +100038get_remote_hostname(int sock, int use_dns)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039{
Damien Miller34132e52000-01-14 15:45:46 +110040 struct sockaddr_storage from;
41 int i;
42 socklen_t fromlen;
43 struct addrinfo hints, *ai, *aitop;
Damien Miller33804262001-02-04 23:20:18 +110044 char name[NI_MAXHOST], ntop[NI_MAXHOST], ntop2[NI_MAXHOST];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045
Damien Miller95def091999-11-25 00:26:21 +110046 /* Get IP address of client. */
47 fromlen = sizeof(from);
48 memset(&from, 0, sizeof(from));
Darren Tucker3f9fdc72004-06-22 12:56:01 +100049 if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
Damien Miller95def091999-11-25 00:26:21 +110050 debug("getpeername failed: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +100051 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052 }
Damien Miller7bcb0892000-03-11 20:45:40 +110053
Damien Miller2eaf37d2006-04-18 15:13:16 +100054 if (from.ss_family == AF_INET)
55 check_ip_options(sock, ntop);
56
Damien Miller927f5272003-11-24 12:57:25 +110057 ipv64_normalise_mapped(&from, &fromlen);
Damien Miller7bcb0892000-03-11 20:45:40 +110058
Damien Miller5e4471e2003-01-07 10:51:23 +110059 if (from.ss_family == AF_INET6)
60 fromlen = sizeof(struct sockaddr_in6);
Damien Miller7bcb0892000-03-11 20:45:40 +110061
Damien Miller34132e52000-01-14 15:45:46 +110062 if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop),
Damien Miller9f0f5c62001-12-21 14:45:46 +110063 NULL, 0, NI_NUMERICHOST) != 0)
Damien Miller34132e52000-01-14 15:45:46 +110064 fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed");
Damien Miller95def091999-11-25 00:26:21 +110065
Damien Miller3a961dc2003-06-03 10:25:48 +100066 if (!use_dns)
67 return xstrdup(ntop);
68
Ben Lindstrom121c7852001-04-18 15:32:44 +000069 debug3("Trying to reverse map address %.100s.", ntop);
Damien Miller34132e52000-01-14 15:45:46 +110070 /* Map the IP address to a host name. */
71 if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
Damien Miller9f0f5c62001-12-21 14:45:46 +110072 NULL, 0, NI_NAMEREQD) != 0) {
Damien Miller33804262001-02-04 23:20:18 +110073 /* Host name not found. Use ip address. */
Damien Miller33804262001-02-04 23:20:18 +110074 return xstrdup(ntop);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076
Damien Miller3a961dc2003-06-03 10:25:48 +100077 /*
78 * if reverse lookup result looks like a numeric hostname,
79 * someone is trying to trick us by PTR record like following:
80 * 1.1.1.10.in-addr.arpa. IN PTR 2.3.4.5
81 */
82 memset(&hints, 0, sizeof(hints));
83 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
Darren Tucker0f684862003-06-05 09:52:42 +100084 hints.ai_flags = AI_NUMERICHOST;
Damien Miller3a961dc2003-06-03 10:25:48 +100085 if (getaddrinfo(name, "0", &hints, &ai) == 0) {
86 logit("Nasty PTR record \"%s\" is set up for %s, ignoring",
87 name, ntop);
88 freeaddrinfo(ai);
89 return xstrdup(ntop);
90 }
91
Damien Miller5428f641999-11-25 11:54:57 +110092 /*
Damien Miller33804262001-02-04 23:20:18 +110093 * Convert it to all lowercase (which is expected by the rest
94 * of this software).
Damien Miller5428f641999-11-25 11:54:57 +110095 */
Damien Miller33804262001-02-04 23:20:18 +110096 for (i = 0; name[i]; i++)
97 if (isupper(name[i]))
Damien Miller1d2b6702006-03-26 14:09:54 +110098 name[i] = (char)tolower(name[i]);
Damien Miller33804262001-02-04 23:20:18 +110099 /*
100 * Map it back to an IP address and check that the given
101 * address actually is an address of this host. This is
102 * necessary because anyone with access to a name server can
103 * define arbitrary names for an IP address. Mapping from
104 * name to IP address can be trusted better (but can still be
105 * fooled if the intruder has access to the name server of
106 * the domain).
107 */
108 memset(&hints, 0, sizeof(hints));
109 hints.ai_family = from.ss_family;
110 hints.ai_socktype = SOCK_STREAM;
111 if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000112 logit("reverse mapping checking getaddrinfo for %.700s "
Damien Millerde85a282006-03-15 12:06:41 +1100113 "[%s] failed - POSSIBLE BREAK-IN ATTEMPT!", name, ntop);
Damien Miller33804262001-02-04 23:20:18 +1100114 return xstrdup(ntop);
Damien Miller95def091999-11-25 00:26:21 +1100115 }
Damien Miller33804262001-02-04 23:20:18 +1100116 /* Look for the address from the list of addresses. */
117 for (ai = aitop; ai; ai = ai->ai_next) {
118 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
119 sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
120 (strcmp(ntop, ntop2) == 0))
121 break;
122 }
123 freeaddrinfo(aitop);
124 /* If we reached the end of the list, the address was not there. */
125 if (!ai) {
126 /* Address not found for the host name. */
Damien Miller996acd22003-04-09 20:59:48 +1000127 logit("Address %.100s maps to %.600s, but this does not "
Damien Miller5eb137c2005-12-31 16:19:53 +1100128 "map back to the address - POSSIBLE BREAK-IN ATTEMPT!",
Damien Miller33804262001-02-04 23:20:18 +1100129 ntop, name);
130 return xstrdup(ntop);
131 }
Damien Miller95def091999-11-25 00:26:21 +1100132 return xstrdup(name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133}
134
Damien Miller5428f641999-11-25 11:54:57 +1100135/*
Damien Miller33804262001-02-04 23:20:18 +1100136 * If IP options are supported, make sure there are none (log and
137 * disconnect them if any are found). Basically we are worried about
138 * source routing; it can be used to pretend you are somebody
139 * (ip-address) you are not. That itself may be "almost acceptable"
140 * under certain circumstances, but rhosts autentication is useless
141 * if source routing is accepted. Notice also that if we just dropped
142 * source routing here, the other side could use IP spoofing to do
143 * rest of the interaction and could still bypass security. So we
144 * exit here if we detect any IP options.
145 */
146/* IPv4 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000147static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000148check_ip_options(int sock, char *ipaddr)
Damien Miller33804262001-02-04 23:20:18 +1100149{
Darren Tucker89f4cf02003-08-07 13:29:04 +1000150#ifdef IP_OPTIONS
Ben Lindstrom075390a2001-02-10 21:34:46 +0000151 u_char options[200];
152 char text[sizeof(options) * 3 + 1];
Damien Miller33804262001-02-04 23:20:18 +1100153 socklen_t option_size;
Damien Millereccb9de2005-06-17 12:59:34 +1000154 u_int i;
155 int ipproto;
Damien Miller33804262001-02-04 23:20:18 +1100156 struct protoent *ip;
157
158 if ((ip = getprotobyname("ip")) != NULL)
159 ipproto = ip->p_proto;
160 else
161 ipproto = IPPROTO_IP;
162 option_size = sizeof(options);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000163 if (getsockopt(sock, ipproto, IP_OPTIONS, options,
Damien Miller33804262001-02-04 23:20:18 +1100164 &option_size) >= 0 && option_size != 0) {
Ben Lindstrom075390a2001-02-10 21:34:46 +0000165 text[0] = '\0';
166 for (i = 0; i < option_size; i++)
167 snprintf(text + i*3, sizeof(text) - i*3,
168 " %2.2x", options[i]);
Damien Miller4d3fd542005-11-05 15:13:24 +1100169 fatal("Connection from %.100s with IP options:%.800s",
Damien Miller33804262001-02-04 23:20:18 +1100170 ipaddr, text);
171 }
Darren Tucker89f4cf02003-08-07 13:29:04 +1000172#endif /* IP_OPTIONS */
Damien Miller33804262001-02-04 23:20:18 +1100173}
174
Darren Tucker2fba9932005-02-02 23:30:24 +1100175void
Damien Miller927f5272003-11-24 12:57:25 +1100176ipv64_normalise_mapped(struct sockaddr_storage *addr, socklen_t *len)
177{
178 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *)addr;
179 struct sockaddr_in *a4 = (struct sockaddr_in *)addr;
180 struct in_addr inaddr;
181 u_int16_t port;
182
Damien Miller94cf4c82005-07-17 17:04:47 +1000183 if (addr->ss_family != AF_INET6 ||
Damien Miller927f5272003-11-24 12:57:25 +1100184 !IN6_IS_ADDR_V4MAPPED(&a6->sin6_addr))
185 return;
186
187 debug3("Normalising mapped IPv4 in IPv6 address");
188
189 memcpy(&inaddr, ((char *)&a6->sin6_addr) + 12, sizeof(inaddr));
190 port = a6->sin6_port;
191
192 memset(addr, 0, sizeof(*a4));
193
194 a4->sin_family = AF_INET;
195 *len = sizeof(*a4);
196 memcpy(&a4->sin_addr, &inaddr, sizeof(inaddr));
197 a4->sin_port = port;
198}
199
Damien Miller33804262001-02-04 23:20:18 +1100200/*
Damien Miller5428f641999-11-25 11:54:57 +1100201 * Return the canonical name of the host in the other side of the current
202 * connection. The host name is cached, so it is efficient to call this
203 * several times.
204 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000205
Damien Miller95def091999-11-25 00:26:21 +1100206const char *
Damien Miller3a961dc2003-06-03 10:25:48 +1000207get_canonical_hostname(int use_dns)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000208{
Damien Miller24ecf612005-11-05 15:16:52 +1100209 char *host;
Damien Miller34132e52000-01-14 15:45:46 +1100210 static char *canonical_host_name = NULL;
Damien Miller24ecf612005-11-05 15:16:52 +1100211 static char *remote_ip = NULL;
Damien Miller34132e52000-01-14 15:45:46 +1100212
Damien Miller33804262001-02-04 23:20:18 +1100213 /* Check if we have previously retrieved name with same option. */
Damien Miller24ecf612005-11-05 15:16:52 +1100214 if (use_dns && canonical_host_name != NULL)
215 return canonical_host_name;
216 if (!use_dns && remote_ip != NULL)
217 return remote_ip;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000218
Damien Miller95def091999-11-25 00:26:21 +1100219 /* Get the real hostname if socket; otherwise return UNKNOWN. */
Damien Miller34132e52000-01-14 15:45:46 +1100220 if (packet_connection_is_on_socket())
Damien Miller24ecf612005-11-05 15:16:52 +1100221 host = get_remote_hostname(packet_get_connection_in(), use_dns);
Damien Miller95def091999-11-25 00:26:21 +1100222 else
Damien Miller24ecf612005-11-05 15:16:52 +1100223 host = "UNKNOWN";
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224
Damien Miller24ecf612005-11-05 15:16:52 +1100225 if (use_dns)
226 canonical_host_name = host;
227 else
228 remote_ip = host;
229 return host;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230}
231
Damien Miller5428f641999-11-25 11:54:57 +1100232/*
Ben Lindstromacaac972002-12-23 02:13:37 +0000233 * Returns the local/remote IP-address/hostname of socket as a string.
234 * The returned string must be freed.
Damien Millerd83ff352001-01-30 09:19:34 +1100235 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000236static char *
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000237get_socket_address(int sock, int remote, int flags)
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000238{
239 struct sockaddr_storage addr;
240 socklen_t addrlen;
241 char ntop[NI_MAXHOST];
Damien Miller9b8073e2005-03-01 21:16:18 +1100242 int r;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000243
244 /* Get IP address of client. */
245 addrlen = sizeof(addr);
246 memset(&addr, 0, sizeof(addr));
247
248 if (remote) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000249 if (getpeername(sock, (struct sockaddr *)&addr, &addrlen)
Damien Millerb2f844d2002-09-25 12:19:08 +1000250 < 0)
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000251 return NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000252 } else {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000253 if (getsockname(sock, (struct sockaddr *)&addr, &addrlen)
Damien Millerb2f844d2002-09-25 12:19:08 +1000254 < 0)
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000255 return NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000256 }
Damien Miller5e4471e2003-01-07 10:51:23 +1100257
258 /* Work around Linux IPv6 weirdness */
259 if (addr.ss_family == AF_INET6)
260 addrlen = sizeof(struct sockaddr_in6);
261
Darren Tucker5b115d42005-05-03 19:05:32 +1000262 ipv64_normalise_mapped(&addr, &addrlen);
263
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000264 /* Get the address in ascii. */
Damien Miller9b8073e2005-03-01 21:16:18 +1100265 if ((r = getnameinfo((struct sockaddr *)&addr, addrlen, ntop,
266 sizeof(ntop), NULL, 0, flags)) != 0) {
267 error("get_socket_address: getnameinfo %d failed: %s", flags,
268 r == EAI_SYSTEM ? strerror(errno) : gai_strerror(r));
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000269 return NULL;
270 }
271 return xstrdup(ntop);
272}
Damien Millerd83ff352001-01-30 09:19:34 +1100273
274char *
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000275get_peer_ipaddr(int sock)
Damien Millerd83ff352001-01-30 09:19:34 +1100276{
Damien Millerb2f844d2002-09-25 12:19:08 +1000277 char *p;
278
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000279 if ((p = get_socket_address(sock, 1, NI_NUMERICHOST)) != NULL)
Damien Millerb2f844d2002-09-25 12:19:08 +1000280 return p;
281 return xstrdup("UNKNOWN");
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000282}
Damien Millerd83ff352001-01-30 09:19:34 +1100283
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000284char *
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000285get_local_ipaddr(int sock)
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000286{
Damien Millerb2f844d2002-09-25 12:19:08 +1000287 char *p;
288
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000289 if ((p = get_socket_address(sock, 0, NI_NUMERICHOST)) != NULL)
Damien Millerb2f844d2002-09-25 12:19:08 +1000290 return p;
291 return xstrdup("UNKNOWN");
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000292}
293
294char *
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000295get_local_name(int sock)
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000296{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000297 return get_socket_address(sock, 0, NI_NAMEREQD);
Damien Millerd83ff352001-01-30 09:19:34 +1100298}
299
300/*
Damien Miller5428f641999-11-25 11:54:57 +1100301 * Returns the IP-address of the remote host as a string. The returned
Damien Miller34132e52000-01-14 15:45:46 +1100302 * string must not be freed.
Damien Miller5428f641999-11-25 11:54:57 +1100303 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000304
Damien Miller95def091999-11-25 00:26:21 +1100305const char *
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000306get_remote_ipaddr(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000307{
Damien Miller34132e52000-01-14 15:45:46 +1100308 static char *canonical_host_ip = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000309
Damien Millerd83ff352001-01-30 09:19:34 +1100310 /* Check whether we have cached the ipaddr. */
311 if (canonical_host_ip == NULL) {
312 if (packet_connection_is_on_socket()) {
313 canonical_host_ip =
314 get_peer_ipaddr(packet_get_connection_in());
315 if (canonical_host_ip == NULL)
Darren Tucker3e33cec2003-10-02 16:12:36 +1000316 cleanup_exit(255);
Damien Millerd83ff352001-01-30 09:19:34 +1100317 } else {
318 /* If not on socket, return UNKNOWN. */
319 canonical_host_ip = xstrdup("UNKNOWN");
320 }
Damien Miller95def091999-11-25 00:26:21 +1100321 }
Damien Miller95def091999-11-25 00:26:21 +1100322 return canonical_host_ip;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323}
324
Ben Lindstromf15a3862001-04-05 23:32:17 +0000325const char *
Damien Miller3a961dc2003-06-03 10:25:48 +1000326get_remote_name_or_ip(u_int utmp_len, int use_dns)
Ben Lindstromf15a3862001-04-05 23:32:17 +0000327{
328 static const char *remote = "";
329 if (utmp_len > 0)
Damien Miller3a961dc2003-06-03 10:25:48 +1000330 remote = get_canonical_hostname(use_dns);
Ben Lindstromf15a3862001-04-05 23:32:17 +0000331 if (utmp_len == 0 || strlen(remote) > utmp_len)
332 remote = get_remote_ipaddr();
333 return remote;
334}
335
Damien Miller34132e52000-01-14 15:45:46 +1100336/* Returns the local/remote port for the socket. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000337
Ben Lindstrombba81212001-06-25 05:01:22 +0000338static int
Damien Miller34132e52000-01-14 15:45:46 +1100339get_sock_port(int sock, int local)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000340{
Damien Miller34132e52000-01-14 15:45:46 +1100341 struct sockaddr_storage from;
342 socklen_t fromlen;
343 char strport[NI_MAXSERV];
Damien Miller9b8073e2005-03-01 21:16:18 +1100344 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000345
Damien Miller95def091999-11-25 00:26:21 +1100346 /* Get IP address of client. */
347 fromlen = sizeof(from);
348 memset(&from, 0, sizeof(from));
Damien Miller34132e52000-01-14 15:45:46 +1100349 if (local) {
350 if (getsockname(sock, (struct sockaddr *)&from, &fromlen) < 0) {
351 error("getsockname failed: %.100s", strerror(errno));
352 return 0;
353 }
354 } else {
Ben Lindstromacaac972002-12-23 02:13:37 +0000355 if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +1100356 debug("getpeername failed: %.100s", strerror(errno));
Damien Miller677257f2005-06-17 12:55:03 +1000357 return -1;
Damien Miller34132e52000-01-14 15:45:46 +1100358 }
Damien Miller95def091999-11-25 00:26:21 +1100359 }
Damien Miller5e4471e2003-01-07 10:51:23 +1100360
361 /* Work around Linux IPv6 weirdness */
362 if (from.ss_family == AF_INET6)
363 fromlen = sizeof(struct sockaddr_in6);
364
Damien Miller95def091999-11-25 00:26:21 +1100365 /* Return port number. */
Damien Miller9b8073e2005-03-01 21:16:18 +1100366 if ((r = getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0,
367 strport, sizeof(strport), NI_NUMERICSERV)) != 0)
368 fatal("get_sock_port: getnameinfo NI_NUMERICSERV failed: %s",
369 r == EAI_SYSTEM ? strerror(errno) : gai_strerror(r));
Damien Miller34132e52000-01-14 15:45:46 +1100370 return atoi(strport);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000371}
372
Damien Miller34132e52000-01-14 15:45:46 +1100373/* Returns remote/local port number for the current connection. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000374
Ben Lindstrombba81212001-06-25 05:01:22 +0000375static int
Damien Miller34132e52000-01-14 15:45:46 +1100376get_port(int local)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000377{
Damien Miller5428f641999-11-25 11:54:57 +1100378 /*
379 * If the connection is not a socket, return 65535. This is
380 * intentionally chosen to be an unprivileged port number.
381 */
Damien Miller34132e52000-01-14 15:45:46 +1100382 if (!packet_connection_is_on_socket())
Damien Miller95def091999-11-25 00:26:21 +1100383 return 65535;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000384
Damien Miller34132e52000-01-14 15:45:46 +1100385 /* Get socket and return the port number. */
386 return get_sock_port(packet_get_connection_in(), local);
387}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000388
Damien Miller4af51302000-04-16 11:18:38 +1000389int
Damien Miller34132e52000-01-14 15:45:46 +1100390get_peer_port(int sock)
391{
392 return get_sock_port(sock, 0);
393}
394
Damien Miller4af51302000-04-16 11:18:38 +1000395int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000396get_remote_port(void)
Damien Miller34132e52000-01-14 15:45:46 +1100397{
Damien Miller0670c732004-07-21 21:53:34 +1000398 static int port = -1;
399
400 /* Cache to avoid getpeername() on a dead connection */
401 if (port == -1)
402 port = get_port(0);
403
404 return port;
Damien Miller34132e52000-01-14 15:45:46 +1100405}
406
407int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000408get_local_port(void)
Damien Miller34132e52000-01-14 15:45:46 +1100409{
410 return get_port(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411}