blob: 538b141b169495f32f6046ece1da47b77e5152b4 [file] [log] [blame]
Damien Millere3b60b52006-07-10 21:08:03 +10001/* $OpenBSD: canohost.c,v 1.55 2006/07/08 21:47:12 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>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100023
24#include "packet.h"
25#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000026#include "log.h"
Ben Lindstrom31ca54a2001-02-09 02:11:24 +000027#include "canohost.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100028
Ben Lindstrombba81212001-06-25 05:01:22 +000029static void check_ip_options(int, char *);
Damien Miller33804262001-02-04 23:20:18 +110030
Damien Miller5428f641999-11-25 11:54:57 +110031/*
32 * Return the canonical name of the host at the other end of the socket. The
33 * caller should free the returned string with xfree.
34 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035
Ben Lindstrombba81212001-06-25 05:01:22 +000036static char *
Darren Tucker3f9fdc72004-06-22 12:56:01 +100037get_remote_hostname(int sock, int use_dns)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038{
Damien Miller34132e52000-01-14 15:45:46 +110039 struct sockaddr_storage from;
40 int i;
41 socklen_t fromlen;
42 struct addrinfo hints, *ai, *aitop;
Damien Miller33804262001-02-04 23:20:18 +110043 char name[NI_MAXHOST], ntop[NI_MAXHOST], ntop2[NI_MAXHOST];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
Damien Miller95def091999-11-25 00:26:21 +110045 /* Get IP address of client. */
46 fromlen = sizeof(from);
47 memset(&from, 0, sizeof(from));
Darren Tucker3f9fdc72004-06-22 12:56:01 +100048 if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
Damien Miller95def091999-11-25 00:26:21 +110049 debug("getpeername failed: %.100s", strerror(errno));
Darren Tucker3e33cec2003-10-02 16:12:36 +100050 cleanup_exit(255);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051 }
Damien Miller7bcb0892000-03-11 20:45:40 +110052
Damien Miller2eaf37d2006-04-18 15:13:16 +100053 if (from.ss_family == AF_INET)
54 check_ip_options(sock, ntop);
55
Damien Miller927f5272003-11-24 12:57:25 +110056 ipv64_normalise_mapped(&from, &fromlen);
Damien Miller7bcb0892000-03-11 20:45:40 +110057
Damien Miller5e4471e2003-01-07 10:51:23 +110058 if (from.ss_family == AF_INET6)
59 fromlen = sizeof(struct sockaddr_in6);
Damien Miller7bcb0892000-03-11 20:45:40 +110060
Damien Miller34132e52000-01-14 15:45:46 +110061 if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop),
Damien Miller9f0f5c62001-12-21 14:45:46 +110062 NULL, 0, NI_NUMERICHOST) != 0)
Damien Miller34132e52000-01-14 15:45:46 +110063 fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed");
Damien Miller95def091999-11-25 00:26:21 +110064
Damien Miller3a961dc2003-06-03 10:25:48 +100065 if (!use_dns)
66 return xstrdup(ntop);
67
Ben Lindstrom121c7852001-04-18 15:32:44 +000068 debug3("Trying to reverse map address %.100s.", ntop);
Damien Miller34132e52000-01-14 15:45:46 +110069 /* Map the IP address to a host name. */
70 if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
Damien Miller9f0f5c62001-12-21 14:45:46 +110071 NULL, 0, NI_NAMEREQD) != 0) {
Damien Miller33804262001-02-04 23:20:18 +110072 /* Host name not found. Use ip address. */
Damien Miller33804262001-02-04 23:20:18 +110073 return xstrdup(ntop);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075
Damien Miller3a961dc2003-06-03 10:25:48 +100076 /*
77 * if reverse lookup result looks like a numeric hostname,
78 * someone is trying to trick us by PTR record like following:
79 * 1.1.1.10.in-addr.arpa. IN PTR 2.3.4.5
80 */
81 memset(&hints, 0, sizeof(hints));
82 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
Darren Tucker0f684862003-06-05 09:52:42 +100083 hints.ai_flags = AI_NUMERICHOST;
Damien Miller3a961dc2003-06-03 10:25:48 +100084 if (getaddrinfo(name, "0", &hints, &ai) == 0) {
85 logit("Nasty PTR record \"%s\" is set up for %s, ignoring",
86 name, ntop);
87 freeaddrinfo(ai);
88 return xstrdup(ntop);
89 }
90
Damien Miller5428f641999-11-25 11:54:57 +110091 /*
Damien Miller33804262001-02-04 23:20:18 +110092 * Convert it to all lowercase (which is expected by the rest
93 * of this software).
Damien Miller5428f641999-11-25 11:54:57 +110094 */
Damien Miller33804262001-02-04 23:20:18 +110095 for (i = 0; name[i]; i++)
96 if (isupper(name[i]))
Damien Miller1d2b6702006-03-26 14:09:54 +110097 name[i] = (char)tolower(name[i]);
Damien Miller33804262001-02-04 23:20:18 +110098 /*
99 * Map it back to an IP address and check that the given
100 * address actually is an address of this host. This is
101 * necessary because anyone with access to a name server can
102 * define arbitrary names for an IP address. Mapping from
103 * name to IP address can be trusted better (but can still be
104 * fooled if the intruder has access to the name server of
105 * the domain).
106 */
107 memset(&hints, 0, sizeof(hints));
108 hints.ai_family = from.ss_family;
109 hints.ai_socktype = SOCK_STREAM;
110 if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000111 logit("reverse mapping checking getaddrinfo for %.700s "
Damien Millerde85a282006-03-15 12:06:41 +1100112 "[%s] failed - POSSIBLE BREAK-IN ATTEMPT!", name, ntop);
Damien Miller33804262001-02-04 23:20:18 +1100113 return xstrdup(ntop);
Damien Miller95def091999-11-25 00:26:21 +1100114 }
Damien Miller33804262001-02-04 23:20:18 +1100115 /* Look for the address from the list of addresses. */
116 for (ai = aitop; ai; ai = ai->ai_next) {
117 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
118 sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
119 (strcmp(ntop, ntop2) == 0))
120 break;
121 }
122 freeaddrinfo(aitop);
123 /* If we reached the end of the list, the address was not there. */
124 if (!ai) {
125 /* Address not found for the host name. */
Damien Miller996acd22003-04-09 20:59:48 +1000126 logit("Address %.100s maps to %.600s, but this does not "
Damien Miller5eb137c2005-12-31 16:19:53 +1100127 "map back to the address - POSSIBLE BREAK-IN ATTEMPT!",
Damien Miller33804262001-02-04 23:20:18 +1100128 ntop, name);
129 return xstrdup(ntop);
130 }
Damien Miller95def091999-11-25 00:26:21 +1100131 return xstrdup(name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132}
133
Damien Miller5428f641999-11-25 11:54:57 +1100134/*
Damien Miller33804262001-02-04 23:20:18 +1100135 * If IP options are supported, make sure there are none (log and
136 * disconnect them if any are found). Basically we are worried about
137 * source routing; it can be used to pretend you are somebody
138 * (ip-address) you are not. That itself may be "almost acceptable"
139 * under certain circumstances, but rhosts autentication is useless
140 * if source routing is accepted. Notice also that if we just dropped
141 * source routing here, the other side could use IP spoofing to do
142 * rest of the interaction and could still bypass security. So we
143 * exit here if we detect any IP options.
144 */
145/* IPv4 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000146static void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000147check_ip_options(int sock, char *ipaddr)
Damien Miller33804262001-02-04 23:20:18 +1100148{
Darren Tucker89f4cf02003-08-07 13:29:04 +1000149#ifdef IP_OPTIONS
Ben Lindstrom075390a2001-02-10 21:34:46 +0000150 u_char options[200];
151 char text[sizeof(options) * 3 + 1];
Damien Miller33804262001-02-04 23:20:18 +1100152 socklen_t option_size;
Damien Millereccb9de2005-06-17 12:59:34 +1000153 u_int i;
154 int ipproto;
Damien Miller33804262001-02-04 23:20:18 +1100155 struct protoent *ip;
156
157 if ((ip = getprotobyname("ip")) != NULL)
158 ipproto = ip->p_proto;
159 else
160 ipproto = IPPROTO_IP;
161 option_size = sizeof(options);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000162 if (getsockopt(sock, ipproto, IP_OPTIONS, options,
Damien Miller33804262001-02-04 23:20:18 +1100163 &option_size) >= 0 && option_size != 0) {
Ben Lindstrom075390a2001-02-10 21:34:46 +0000164 text[0] = '\0';
165 for (i = 0; i < option_size; i++)
166 snprintf(text + i*3, sizeof(text) - i*3,
167 " %2.2x", options[i]);
Damien Miller4d3fd542005-11-05 15:13:24 +1100168 fatal("Connection from %.100s with IP options:%.800s",
Damien Miller33804262001-02-04 23:20:18 +1100169 ipaddr, text);
170 }
Darren Tucker89f4cf02003-08-07 13:29:04 +1000171#endif /* IP_OPTIONS */
Damien Miller33804262001-02-04 23:20:18 +1100172}
173
Darren Tucker2fba9932005-02-02 23:30:24 +1100174void
Damien Miller927f5272003-11-24 12:57:25 +1100175ipv64_normalise_mapped(struct sockaddr_storage *addr, socklen_t *len)
176{
177 struct sockaddr_in6 *a6 = (struct sockaddr_in6 *)addr;
178 struct sockaddr_in *a4 = (struct sockaddr_in *)addr;
179 struct in_addr inaddr;
180 u_int16_t port;
181
Damien Miller94cf4c82005-07-17 17:04:47 +1000182 if (addr->ss_family != AF_INET6 ||
Damien Miller927f5272003-11-24 12:57:25 +1100183 !IN6_IS_ADDR_V4MAPPED(&a6->sin6_addr))
184 return;
185
186 debug3("Normalising mapped IPv4 in IPv6 address");
187
188 memcpy(&inaddr, ((char *)&a6->sin6_addr) + 12, sizeof(inaddr));
189 port = a6->sin6_port;
190
191 memset(addr, 0, sizeof(*a4));
192
193 a4->sin_family = AF_INET;
194 *len = sizeof(*a4);
195 memcpy(&a4->sin_addr, &inaddr, sizeof(inaddr));
196 a4->sin_port = port;
197}
198
Damien Miller33804262001-02-04 23:20:18 +1100199/*
Damien Miller5428f641999-11-25 11:54:57 +1100200 * Return the canonical name of the host in the other side of the current
201 * connection. The host name is cached, so it is efficient to call this
202 * several times.
203 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204
Damien Miller95def091999-11-25 00:26:21 +1100205const char *
Damien Miller3a961dc2003-06-03 10:25:48 +1000206get_canonical_hostname(int use_dns)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000207{
Damien Miller24ecf612005-11-05 15:16:52 +1100208 char *host;
Damien Miller34132e52000-01-14 15:45:46 +1100209 static char *canonical_host_name = NULL;
Damien Miller24ecf612005-11-05 15:16:52 +1100210 static char *remote_ip = NULL;
Damien Miller34132e52000-01-14 15:45:46 +1100211
Damien Miller33804262001-02-04 23:20:18 +1100212 /* Check if we have previously retrieved name with same option. */
Damien Miller24ecf612005-11-05 15:16:52 +1100213 if (use_dns && canonical_host_name != NULL)
214 return canonical_host_name;
215 if (!use_dns && remote_ip != NULL)
216 return remote_ip;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217
Damien Miller95def091999-11-25 00:26:21 +1100218 /* Get the real hostname if socket; otherwise return UNKNOWN. */
Damien Miller34132e52000-01-14 15:45:46 +1100219 if (packet_connection_is_on_socket())
Damien Miller24ecf612005-11-05 15:16:52 +1100220 host = get_remote_hostname(packet_get_connection_in(), use_dns);
Damien Miller95def091999-11-25 00:26:21 +1100221 else
Damien Miller24ecf612005-11-05 15:16:52 +1100222 host = "UNKNOWN";
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223
Damien Miller24ecf612005-11-05 15:16:52 +1100224 if (use_dns)
225 canonical_host_name = host;
226 else
227 remote_ip = host;
228 return host;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000229}
230
Damien Miller5428f641999-11-25 11:54:57 +1100231/*
Ben Lindstromacaac972002-12-23 02:13:37 +0000232 * Returns the local/remote IP-address/hostname of socket as a string.
233 * The returned string must be freed.
Damien Millerd83ff352001-01-30 09:19:34 +1100234 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000235static char *
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000236get_socket_address(int sock, int remote, int flags)
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000237{
238 struct sockaddr_storage addr;
239 socklen_t addrlen;
240 char ntop[NI_MAXHOST];
Damien Miller9b8073e2005-03-01 21:16:18 +1100241 int r;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000242
243 /* Get IP address of client. */
244 addrlen = sizeof(addr);
245 memset(&addr, 0, sizeof(addr));
246
247 if (remote) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000248 if (getpeername(sock, (struct sockaddr *)&addr, &addrlen)
Damien Millerb2f844d2002-09-25 12:19:08 +1000249 < 0)
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000250 return NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000251 } else {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000252 if (getsockname(sock, (struct sockaddr *)&addr, &addrlen)
Damien Millerb2f844d2002-09-25 12:19:08 +1000253 < 0)
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000254 return NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000255 }
Damien Miller5e4471e2003-01-07 10:51:23 +1100256
257 /* Work around Linux IPv6 weirdness */
258 if (addr.ss_family == AF_INET6)
259 addrlen = sizeof(struct sockaddr_in6);
260
Darren Tucker5b115d42005-05-03 19:05:32 +1000261 ipv64_normalise_mapped(&addr, &addrlen);
262
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000263 /* Get the address in ascii. */
Damien Miller9b8073e2005-03-01 21:16:18 +1100264 if ((r = getnameinfo((struct sockaddr *)&addr, addrlen, ntop,
265 sizeof(ntop), NULL, 0, flags)) != 0) {
266 error("get_socket_address: getnameinfo %d failed: %s", flags,
267 r == EAI_SYSTEM ? strerror(errno) : gai_strerror(r));
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000268 return NULL;
269 }
270 return xstrdup(ntop);
271}
Damien Millerd83ff352001-01-30 09:19:34 +1100272
273char *
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000274get_peer_ipaddr(int sock)
Damien Millerd83ff352001-01-30 09:19:34 +1100275{
Damien Millerb2f844d2002-09-25 12:19:08 +1000276 char *p;
277
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000278 if ((p = get_socket_address(sock, 1, NI_NUMERICHOST)) != NULL)
Damien Millerb2f844d2002-09-25 12:19:08 +1000279 return p;
280 return xstrdup("UNKNOWN");
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000281}
Damien Millerd83ff352001-01-30 09:19:34 +1100282
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000283char *
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000284get_local_ipaddr(int sock)
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000285{
Damien Millerb2f844d2002-09-25 12:19:08 +1000286 char *p;
287
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000288 if ((p = get_socket_address(sock, 0, NI_NUMERICHOST)) != NULL)
Damien Millerb2f844d2002-09-25 12:19:08 +1000289 return p;
290 return xstrdup("UNKNOWN");
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000291}
292
293char *
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000294get_local_name(int sock)
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000295{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000296 return get_socket_address(sock, 0, NI_NAMEREQD);
Damien Millerd83ff352001-01-30 09:19:34 +1100297}
298
299/*
Damien Miller5428f641999-11-25 11:54:57 +1100300 * Returns the IP-address of the remote host as a string. The returned
Damien Miller34132e52000-01-14 15:45:46 +1100301 * string must not be freed.
Damien Miller5428f641999-11-25 11:54:57 +1100302 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303
Damien Miller95def091999-11-25 00:26:21 +1100304const char *
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000305get_remote_ipaddr(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306{
Damien Miller34132e52000-01-14 15:45:46 +1100307 static char *canonical_host_ip = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000308
Damien Millerd83ff352001-01-30 09:19:34 +1100309 /* Check whether we have cached the ipaddr. */
310 if (canonical_host_ip == NULL) {
311 if (packet_connection_is_on_socket()) {
312 canonical_host_ip =
313 get_peer_ipaddr(packet_get_connection_in());
314 if (canonical_host_ip == NULL)
Darren Tucker3e33cec2003-10-02 16:12:36 +1000315 cleanup_exit(255);
Damien Millerd83ff352001-01-30 09:19:34 +1100316 } else {
317 /* If not on socket, return UNKNOWN. */
318 canonical_host_ip = xstrdup("UNKNOWN");
319 }
Damien Miller95def091999-11-25 00:26:21 +1100320 }
Damien Miller95def091999-11-25 00:26:21 +1100321 return canonical_host_ip;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322}
323
Ben Lindstromf15a3862001-04-05 23:32:17 +0000324const char *
Damien Miller3a961dc2003-06-03 10:25:48 +1000325get_remote_name_or_ip(u_int utmp_len, int use_dns)
Ben Lindstromf15a3862001-04-05 23:32:17 +0000326{
327 static const char *remote = "";
328 if (utmp_len > 0)
Damien Miller3a961dc2003-06-03 10:25:48 +1000329 remote = get_canonical_hostname(use_dns);
Ben Lindstromf15a3862001-04-05 23:32:17 +0000330 if (utmp_len == 0 || strlen(remote) > utmp_len)
331 remote = get_remote_ipaddr();
332 return remote;
333}
334
Damien Miller34132e52000-01-14 15:45:46 +1100335/* Returns the local/remote port for the socket. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336
Ben Lindstrombba81212001-06-25 05:01:22 +0000337static int
Damien Miller34132e52000-01-14 15:45:46 +1100338get_sock_port(int sock, int local)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339{
Damien Miller34132e52000-01-14 15:45:46 +1100340 struct sockaddr_storage from;
341 socklen_t fromlen;
342 char strport[NI_MAXSERV];
Damien Miller9b8073e2005-03-01 21:16:18 +1100343 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000344
Damien Miller95def091999-11-25 00:26:21 +1100345 /* Get IP address of client. */
346 fromlen = sizeof(from);
347 memset(&from, 0, sizeof(from));
Damien Miller34132e52000-01-14 15:45:46 +1100348 if (local) {
349 if (getsockname(sock, (struct sockaddr *)&from, &fromlen) < 0) {
350 error("getsockname failed: %.100s", strerror(errno));
351 return 0;
352 }
353 } else {
Ben Lindstromacaac972002-12-23 02:13:37 +0000354 if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +1100355 debug("getpeername failed: %.100s", strerror(errno));
Damien Miller677257f2005-06-17 12:55:03 +1000356 return -1;
Damien Miller34132e52000-01-14 15:45:46 +1100357 }
Damien Miller95def091999-11-25 00:26:21 +1100358 }
Damien Miller5e4471e2003-01-07 10:51:23 +1100359
360 /* Work around Linux IPv6 weirdness */
361 if (from.ss_family == AF_INET6)
362 fromlen = sizeof(struct sockaddr_in6);
363
Damien Miller95def091999-11-25 00:26:21 +1100364 /* Return port number. */
Damien Miller9b8073e2005-03-01 21:16:18 +1100365 if ((r = getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0,
366 strport, sizeof(strport), NI_NUMERICSERV)) != 0)
367 fatal("get_sock_port: getnameinfo NI_NUMERICSERV failed: %s",
368 r == EAI_SYSTEM ? strerror(errno) : gai_strerror(r));
Damien Miller34132e52000-01-14 15:45:46 +1100369 return atoi(strport);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000370}
371
Damien Miller34132e52000-01-14 15:45:46 +1100372/* Returns remote/local port number for the current connection. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000373
Ben Lindstrombba81212001-06-25 05:01:22 +0000374static int
Damien Miller34132e52000-01-14 15:45:46 +1100375get_port(int local)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000376{
Damien Miller5428f641999-11-25 11:54:57 +1100377 /*
378 * If the connection is not a socket, return 65535. This is
379 * intentionally chosen to be an unprivileged port number.
380 */
Damien Miller34132e52000-01-14 15:45:46 +1100381 if (!packet_connection_is_on_socket())
Damien Miller95def091999-11-25 00:26:21 +1100382 return 65535;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000383
Damien Miller34132e52000-01-14 15:45:46 +1100384 /* Get socket and return the port number. */
385 return get_sock_port(packet_get_connection_in(), local);
386}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000387
Damien Miller4af51302000-04-16 11:18:38 +1000388int
Damien Miller34132e52000-01-14 15:45:46 +1100389get_peer_port(int sock)
390{
391 return get_sock_port(sock, 0);
392}
393
Damien Miller4af51302000-04-16 11:18:38 +1000394int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000395get_remote_port(void)
Damien Miller34132e52000-01-14 15:45:46 +1100396{
Damien Miller0670c732004-07-21 21:53:34 +1000397 static int port = -1;
398
399 /* Cache to avoid getpeername() on a dead connection */
400 if (port == -1)
401 port = get_port(0);
402
403 return port;
Damien Miller34132e52000-01-14 15:45:46 +1100404}
405
406int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000407get_local_port(void)
Damien Miller34132e52000-01-14 15:45:46 +1100408{
409 return get_port(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410}