xref: /onnv-gate/usr/src/cmd/ssh/libssh/common/canohost.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Author: Tatu Ylonen <ylo@cs.hut.fi>
3*0Sstevel@tonic-gate  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4*0Sstevel@tonic-gate  *                    All rights reserved
5*0Sstevel@tonic-gate  * Functions for returning the canonical host name of the remote site.
6*0Sstevel@tonic-gate  *
7*0Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
8*0Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
9*0Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
10*0Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
11*0Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
12*0Sstevel@tonic-gate  */
13*0Sstevel@tonic-gate /*
14*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
15*0Sstevel@tonic-gate  * Use is subject to license terms.
16*0Sstevel@tonic-gate  */
17*0Sstevel@tonic-gate 
18*0Sstevel@tonic-gate #include "includes.h"
19*0Sstevel@tonic-gate RCSID("$OpenBSD: canohost.c,v 1.34 2002/09/23 20:46:27 stevesk Exp $");
20*0Sstevel@tonic-gate 
21*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
22*0Sstevel@tonic-gate 
23*0Sstevel@tonic-gate #include "packet.h"
24*0Sstevel@tonic-gate #include "xmalloc.h"
25*0Sstevel@tonic-gate #include "log.h"
26*0Sstevel@tonic-gate #include "canohost.h"
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate static const char *inet_ntop_native(int af, const void *src,
29*0Sstevel@tonic-gate 	char *dst, size_t size);
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate /*
33*0Sstevel@tonic-gate  * Return the canonical name of the host at the other end of the socket. The
34*0Sstevel@tonic-gate  * caller should free the returned string with xfree.
35*0Sstevel@tonic-gate  */
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate static char *
get_remote_hostname(int socket,int verify_reverse_mapping)38*0Sstevel@tonic-gate get_remote_hostname(int socket, int verify_reverse_mapping)
39*0Sstevel@tonic-gate {
40*0Sstevel@tonic-gate 	struct sockaddr_storage from;
41*0Sstevel@tonic-gate 	int i, res;
42*0Sstevel@tonic-gate 	socklen_t fromlen;
43*0Sstevel@tonic-gate 	struct addrinfo hints, *ai, *aitop;
44*0Sstevel@tonic-gate 	char name[NI_MAXHOST], ntop[NI_MAXHOST], ntop2[NI_MAXHOST];
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate 	/* Get IP address of client. */
47*0Sstevel@tonic-gate 	fromlen = sizeof(from);
48*0Sstevel@tonic-gate 	memset(&from, 0, sizeof(from));
49*0Sstevel@tonic-gate 	if (getpeername(socket, (struct sockaddr *) &from, &fromlen) < 0) {
50*0Sstevel@tonic-gate 		debug("getpeername failed: %.100s", strerror(errno));
51*0Sstevel@tonic-gate 		fatal_cleanup();
52*0Sstevel@tonic-gate 	}
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 	if ((res = getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop),
55*0Sstevel@tonic-gate 	    NULL, 0, NI_NUMERICHOST)) != 0)
56*0Sstevel@tonic-gate 		fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed: %d", res);
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate #ifdef IPV4_IN_IPV6
59*0Sstevel@tonic-gate 	if (from.ss_family == AF_INET6) {
60*0Sstevel@tonic-gate 		struct sockaddr_in6 *from6 = (struct sockaddr_in6 *)&from;
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate 		(void) inet_ntop_native(from.ss_family,
63*0Sstevel@tonic-gate 				from6->sin6_addr.s6_addr,
64*0Sstevel@tonic-gate 				ntop, sizeof(ntop));
65*0Sstevel@tonic-gate 	}
66*0Sstevel@tonic-gate #endif /* IPV4_IN_IPV6 */
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate 	debug3("Trying to reverse map address %.100s.", ntop);
69*0Sstevel@tonic-gate 	/* Map the IP address to a host name. */
70*0Sstevel@tonic-gate 	if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
71*0Sstevel@tonic-gate 	    NULL, 0, NI_NAMEREQD) != 0) {
72*0Sstevel@tonic-gate 		/* Host name not found.  Use ip address. */
73*0Sstevel@tonic-gate #if 0
74*0Sstevel@tonic-gate 		log("Could not reverse map address %.100s.", ntop);
75*0Sstevel@tonic-gate #endif
76*0Sstevel@tonic-gate 		return xstrdup(ntop);
77*0Sstevel@tonic-gate 	}
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 	/* Got host name. */
80*0Sstevel@tonic-gate 	name[sizeof(name) - 1] = '\0';
81*0Sstevel@tonic-gate 	/*
82*0Sstevel@tonic-gate 	 * Convert it to all lowercase (which is expected by the rest
83*0Sstevel@tonic-gate 	 * of this software).
84*0Sstevel@tonic-gate 	 */
85*0Sstevel@tonic-gate 	for (i = 0; name[i]; i++)
86*0Sstevel@tonic-gate 		if (isupper(name[i]))
87*0Sstevel@tonic-gate 			name[i] = tolower(name[i]);
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate 	if (!verify_reverse_mapping)
90*0Sstevel@tonic-gate 		return xstrdup(name);
91*0Sstevel@tonic-gate 	/*
92*0Sstevel@tonic-gate 	 * Map it back to an IP address and check that the given
93*0Sstevel@tonic-gate 	 * address actually is an address of this host.  This is
94*0Sstevel@tonic-gate 	 * necessary because anyone with access to a name server can
95*0Sstevel@tonic-gate 	 * define arbitrary names for an IP address. Mapping from
96*0Sstevel@tonic-gate 	 * name to IP address can be trusted better (but can still be
97*0Sstevel@tonic-gate 	 * fooled if the intruder has access to the name server of
98*0Sstevel@tonic-gate 	 * the domain).
99*0Sstevel@tonic-gate 	 */
100*0Sstevel@tonic-gate 	memset(&hints, 0, sizeof(hints));
101*0Sstevel@tonic-gate 	hints.ai_family = from.ss_family;
102*0Sstevel@tonic-gate 	hints.ai_socktype = SOCK_STREAM;
103*0Sstevel@tonic-gate 	if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
104*0Sstevel@tonic-gate 		log("reverse mapping checking getaddrinfo for %.700s "
105*0Sstevel@tonic-gate 		    "failed - POSSIBLE BREAKIN ATTEMPT!", name);
106*0Sstevel@tonic-gate 		return xstrdup(ntop);
107*0Sstevel@tonic-gate 	}
108*0Sstevel@tonic-gate 	/* Look for the address from the list of addresses. */
109*0Sstevel@tonic-gate 	for (ai = aitop; ai; ai = ai->ai_next) {
110*0Sstevel@tonic-gate 		if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
111*0Sstevel@tonic-gate 		    sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
112*0Sstevel@tonic-gate 		    (strcmp(ntop, ntop2) == 0))
113*0Sstevel@tonic-gate 				break;
114*0Sstevel@tonic-gate 	}
115*0Sstevel@tonic-gate 	freeaddrinfo(aitop);
116*0Sstevel@tonic-gate 	/* If we reached the end of the list, the address was not there. */
117*0Sstevel@tonic-gate 	if (!ai) {
118*0Sstevel@tonic-gate 		/* Address not found for the host name. */
119*0Sstevel@tonic-gate 		log("Address %.100s maps to %.600s, but this does not "
120*0Sstevel@tonic-gate 		    "map back to the address - POSSIBLE BREAKIN ATTEMPT!",
121*0Sstevel@tonic-gate 		    ntop, name);
122*0Sstevel@tonic-gate 		return xstrdup(ntop);
123*0Sstevel@tonic-gate 	}
124*0Sstevel@tonic-gate 	return xstrdup(name);
125*0Sstevel@tonic-gate }
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate /*
128*0Sstevel@tonic-gate  * Return the canonical name of the host in the other side of the current
129*0Sstevel@tonic-gate  * connection.  The host name is cached, so it is efficient to call this
130*0Sstevel@tonic-gate  * several times.
131*0Sstevel@tonic-gate  */
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate const char *
get_canonical_hostname(int verify_reverse_mapping)134*0Sstevel@tonic-gate get_canonical_hostname(int verify_reverse_mapping)
135*0Sstevel@tonic-gate {
136*0Sstevel@tonic-gate 	static char *canonical_host_name = NULL;
137*0Sstevel@tonic-gate 	static int verify_reverse_mapping_done = 0;
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	/* Check if we have previously retrieved name with same option. */
140*0Sstevel@tonic-gate 	if (canonical_host_name != NULL) {
141*0Sstevel@tonic-gate 		if (verify_reverse_mapping_done != verify_reverse_mapping)
142*0Sstevel@tonic-gate 			xfree(canonical_host_name);
143*0Sstevel@tonic-gate 		else
144*0Sstevel@tonic-gate 			return canonical_host_name;
145*0Sstevel@tonic-gate 	}
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	/* Get the real hostname if socket; otherwise return UNKNOWN. */
148*0Sstevel@tonic-gate 	if (packet_connection_is_on_socket())
149*0Sstevel@tonic-gate 		canonical_host_name = get_remote_hostname(
150*0Sstevel@tonic-gate 		    packet_get_connection_in(), verify_reverse_mapping);
151*0Sstevel@tonic-gate 	else
152*0Sstevel@tonic-gate 		canonical_host_name = xstrdup("UNKNOWN");
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	verify_reverse_mapping_done = verify_reverse_mapping;
155*0Sstevel@tonic-gate 	return canonical_host_name;
156*0Sstevel@tonic-gate }
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate /*
159*0Sstevel@tonic-gate  * Returns the remote IP-address of socket as a string.  The returned
160*0Sstevel@tonic-gate  * string must be freed.
161*0Sstevel@tonic-gate  */
162*0Sstevel@tonic-gate char *
get_socket_address(int socket,int remote,int flags)163*0Sstevel@tonic-gate get_socket_address(int socket, int remote, int flags)
164*0Sstevel@tonic-gate {
165*0Sstevel@tonic-gate 	struct sockaddr_storage addr;
166*0Sstevel@tonic-gate 	struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr;
167*0Sstevel@tonic-gate 	socklen_t addrlen;
168*0Sstevel@tonic-gate 	char ntop[NI_MAXHOST];
169*0Sstevel@tonic-gate 	const char *result;
170*0Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	/* Get IP address of client. */
173*0Sstevel@tonic-gate 	addrlen = sizeof (addr);
174*0Sstevel@tonic-gate 	memset(&addr, 0, sizeof (addr));
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	if (remote) {
177*0Sstevel@tonic-gate 		if (getpeername(socket, (struct sockaddr *)&addr, &addrlen)
178*0Sstevel@tonic-gate 		    < 0) {
179*0Sstevel@tonic-gate 			debug("get_socket_ipaddr: getpeername failed: %.100s",
180*0Sstevel@tonic-gate 			    strerror(errno));
181*0Sstevel@tonic-gate 			return (NULL);
182*0Sstevel@tonic-gate 		}
183*0Sstevel@tonic-gate 	} else {
184*0Sstevel@tonic-gate 		if (getsockname(socket, (struct sockaddr *)&addr, &addrlen)
185*0Sstevel@tonic-gate 		    < 0) {
186*0Sstevel@tonic-gate 			debug("get_socket_ipaddr: getsockname failed: %.100s",
187*0Sstevel@tonic-gate 			    strerror(errno));
188*0Sstevel@tonic-gate 			return (NULL);
189*0Sstevel@tonic-gate 		}
190*0Sstevel@tonic-gate 	}
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 	/* Get the address in ascii. */
193*0Sstevel@tonic-gate 	if (getnameinfo((struct sockaddr *)&addr, addrlen, ntop, sizeof (ntop),
194*0Sstevel@tonic-gate 	    NULL, 0, flags) != 0) {
195*0Sstevel@tonic-gate 		error("get_socket_ipaddr: getnameinfo %d failed", flags);
196*0Sstevel@tonic-gate 		return (NULL);
197*0Sstevel@tonic-gate 	}
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	if (addr.ss_family == AF_INET) {
200*0Sstevel@tonic-gate 		return (xstrdup(ntop));
201*0Sstevel@tonic-gate 	}
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 	result = inet_ntop_native(addr.ss_family,
204*0Sstevel@tonic-gate 	    addr6->sin6_addr.s6_addr, abuf, sizeof (abuf));
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	return (xstrdup(result));
207*0Sstevel@tonic-gate }
208*0Sstevel@tonic-gate #if 0
209*0Sstevel@tonic-gate static char *
210*0Sstevel@tonic-gate get_socket_address(int socket, int remote, int flags)
211*0Sstevel@tonic-gate {
212*0Sstevel@tonic-gate 	struct sockaddr_storage addr;
213*0Sstevel@tonic-gate 	socklen_t addrlen;
214*0Sstevel@tonic-gate 	char ntop[NI_MAXHOST];
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 	/* Get IP address of client. */
217*0Sstevel@tonic-gate 	addrlen = sizeof(addr);
218*0Sstevel@tonic-gate 	memset(&addr, 0, sizeof(addr));
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 	if (remote) {
221*0Sstevel@tonic-gate 		if (getpeername(socket, (struct sockaddr *)&addr, &addrlen)
222*0Sstevel@tonic-gate 		    < 0)
223*0Sstevel@tonic-gate 			return NULL;
224*0Sstevel@tonic-gate 	} else {
225*0Sstevel@tonic-gate 		if (getsockname(socket, (struct sockaddr *)&addr, &addrlen)
226*0Sstevel@tonic-gate 		    < 0)
227*0Sstevel@tonic-gate 			return NULL;
228*0Sstevel@tonic-gate 	}
229*0Sstevel@tonic-gate 	/* Get the address in ascii. */
230*0Sstevel@tonic-gate 	if (getnameinfo((struct sockaddr *)&addr, addrlen, ntop, sizeof(ntop),
231*0Sstevel@tonic-gate 	    NULL, 0, flags) != 0) {
232*0Sstevel@tonic-gate 		error("get_socket_ipaddr: getnameinfo %d failed", flags);
233*0Sstevel@tonic-gate 		return NULL;
234*0Sstevel@tonic-gate 	}
235*0Sstevel@tonic-gate 	return xstrdup(ntop);
236*0Sstevel@tonic-gate }
237*0Sstevel@tonic-gate #endif
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate char *
get_peer_ipaddr(int socket)240*0Sstevel@tonic-gate get_peer_ipaddr(int socket)
241*0Sstevel@tonic-gate {
242*0Sstevel@tonic-gate 	char *p;
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 	if ((p = get_socket_address(socket, 1, NI_NUMERICHOST)) != NULL)
245*0Sstevel@tonic-gate 		return p;
246*0Sstevel@tonic-gate 	return xstrdup("UNKNOWN");
247*0Sstevel@tonic-gate }
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate char *
get_local_ipaddr(int socket)250*0Sstevel@tonic-gate get_local_ipaddr(int socket)
251*0Sstevel@tonic-gate {
252*0Sstevel@tonic-gate 	char *p;
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 	if ((p = get_socket_address(socket, 0, NI_NUMERICHOST)) != NULL)
255*0Sstevel@tonic-gate 		return p;
256*0Sstevel@tonic-gate 	return xstrdup("UNKNOWN");
257*0Sstevel@tonic-gate }
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate char *
get_local_name(int socket)260*0Sstevel@tonic-gate get_local_name(int socket)
261*0Sstevel@tonic-gate {
262*0Sstevel@tonic-gate 	return get_socket_address(socket, 0, NI_NAMEREQD);
263*0Sstevel@tonic-gate }
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate /*
266*0Sstevel@tonic-gate  * Returns the IP-address of the remote host as a string.  The returned
267*0Sstevel@tonic-gate  * string must not be freed.
268*0Sstevel@tonic-gate  */
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate const char *
get_remote_ipaddr(void)271*0Sstevel@tonic-gate get_remote_ipaddr(void)
272*0Sstevel@tonic-gate {
273*0Sstevel@tonic-gate 	static char *canonical_host_ip = NULL;
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 	/* Check whether we have cached the ipaddr. */
276*0Sstevel@tonic-gate 	if (canonical_host_ip == NULL) {
277*0Sstevel@tonic-gate 		if (packet_connection_is_on_socket()) {
278*0Sstevel@tonic-gate 			canonical_host_ip =
279*0Sstevel@tonic-gate 			    get_peer_ipaddr(packet_get_connection_in());
280*0Sstevel@tonic-gate 			if (canonical_host_ip == NULL)
281*0Sstevel@tonic-gate 				fatal_cleanup();
282*0Sstevel@tonic-gate 		} else {
283*0Sstevel@tonic-gate 			/* If not on socket, return UNKNOWN. */
284*0Sstevel@tonic-gate 			canonical_host_ip = xstrdup("UNKNOWN");
285*0Sstevel@tonic-gate 		}
286*0Sstevel@tonic-gate 	}
287*0Sstevel@tonic-gate 	return canonical_host_ip;
288*0Sstevel@tonic-gate }
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate const char *
get_remote_name_or_ip(u_int utmp_len,int verify_reverse_mapping)291*0Sstevel@tonic-gate get_remote_name_or_ip(u_int utmp_len, int verify_reverse_mapping)
292*0Sstevel@tonic-gate {
293*0Sstevel@tonic-gate 	static const char *remote = "";
294*0Sstevel@tonic-gate 	if (utmp_len > 0)
295*0Sstevel@tonic-gate 		remote = get_canonical_hostname(verify_reverse_mapping);
296*0Sstevel@tonic-gate 	if (utmp_len == 0 || strlen(remote) > utmp_len)
297*0Sstevel@tonic-gate 		remote = get_remote_ipaddr();
298*0Sstevel@tonic-gate 	return remote;
299*0Sstevel@tonic-gate }
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate /* Returns the local/remote port for the socket. */
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate static int
get_sock_port(int sock,int local)304*0Sstevel@tonic-gate get_sock_port(int sock, int local)
305*0Sstevel@tonic-gate {
306*0Sstevel@tonic-gate 	struct sockaddr_storage from;
307*0Sstevel@tonic-gate 	socklen_t fromlen;
308*0Sstevel@tonic-gate 	char strport[NI_MAXSERV];
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate 	/* Get IP address of client. */
311*0Sstevel@tonic-gate 	fromlen = sizeof(from);
312*0Sstevel@tonic-gate 	memset(&from, 0, sizeof(from));
313*0Sstevel@tonic-gate 	if (local) {
314*0Sstevel@tonic-gate 		if (getsockname(sock, (struct sockaddr *)&from, &fromlen) < 0) {
315*0Sstevel@tonic-gate 			error("getsockname failed: %.100s", strerror(errno));
316*0Sstevel@tonic-gate 			return 0;
317*0Sstevel@tonic-gate 		}
318*0Sstevel@tonic-gate 	} else {
319*0Sstevel@tonic-gate 		if (getpeername(sock, (struct sockaddr *) & from, &fromlen) < 0) {
320*0Sstevel@tonic-gate 			debug("getpeername failed: %.100s", strerror(errno));
321*0Sstevel@tonic-gate 			fatal_cleanup();
322*0Sstevel@tonic-gate 		}
323*0Sstevel@tonic-gate 	}
324*0Sstevel@tonic-gate 	/* Return port number. */
325*0Sstevel@tonic-gate 	if (getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0,
326*0Sstevel@tonic-gate 	    strport, sizeof(strport), NI_NUMERICSERV) != 0)
327*0Sstevel@tonic-gate 		fatal("get_sock_port: getnameinfo NI_NUMERICSERV failed");
328*0Sstevel@tonic-gate 	return atoi(strport);
329*0Sstevel@tonic-gate }
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate /* Returns remote/local port number for the current connection. */
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate static int
get_port(int local)334*0Sstevel@tonic-gate get_port(int local)
335*0Sstevel@tonic-gate {
336*0Sstevel@tonic-gate 	/*
337*0Sstevel@tonic-gate 	 * If the connection is not a socket, return 65535.  This is
338*0Sstevel@tonic-gate 	 * intentionally chosen to be an unprivileged port number.
339*0Sstevel@tonic-gate 	 */
340*0Sstevel@tonic-gate 	if (!packet_connection_is_on_socket())
341*0Sstevel@tonic-gate 		return 65535;
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate 	/* Get socket and return the port number. */
344*0Sstevel@tonic-gate 	return get_sock_port(packet_get_connection_in(), local);
345*0Sstevel@tonic-gate }
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate int
get_peer_port(int sock)348*0Sstevel@tonic-gate get_peer_port(int sock)
349*0Sstevel@tonic-gate {
350*0Sstevel@tonic-gate 	return get_sock_port(sock, 0);
351*0Sstevel@tonic-gate }
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate int
get_remote_port(void)354*0Sstevel@tonic-gate get_remote_port(void)
355*0Sstevel@tonic-gate {
356*0Sstevel@tonic-gate 	return get_port(0);
357*0Sstevel@tonic-gate }
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate int
get_local_port(void)360*0Sstevel@tonic-gate get_local_port(void)
361*0Sstevel@tonic-gate {
362*0Sstevel@tonic-gate 	return get_port(1);
363*0Sstevel@tonic-gate }
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate /*
366*0Sstevel@tonic-gate  * Taken from inetd.c
367*0Sstevel@tonic-gate  * This is a wrapper function for inet_ntop(). In case the af is AF_INET6
368*0Sstevel@tonic-gate  * and the address pointed by src is a IPv4-mapped IPv6 address, it
369*0Sstevel@tonic-gate  * returns printable IPv4 address, not IPv4-mapped IPv6 address. In other cases
370*0Sstevel@tonic-gate  * it behaves just like inet_ntop().
371*0Sstevel@tonic-gate  */
372*0Sstevel@tonic-gate static const char *
inet_ntop_native(int af,const void * src,char * dst,size_t size)373*0Sstevel@tonic-gate inet_ntop_native(int af, const void *src, char *dst, size_t size)
374*0Sstevel@tonic-gate {
375*0Sstevel@tonic-gate 	struct in_addr src4;
376*0Sstevel@tonic-gate 	const char *result;
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate 	if (af == AF_INET6) {
379*0Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)src)) {
380*0Sstevel@tonic-gate 			IN6_V4MAPPED_TO_INADDR((struct in6_addr *)src, &src4);
381*0Sstevel@tonic-gate 			result = inet_ntop(AF_INET, &src4, dst, size);
382*0Sstevel@tonic-gate 		} else {
383*0Sstevel@tonic-gate 			result = inet_ntop(AF_INET6, src, dst, size);
384*0Sstevel@tonic-gate 		}
385*0Sstevel@tonic-gate 	} else {
386*0Sstevel@tonic-gate 		result = inet_ntop(af, src, dst, size);
387*0Sstevel@tonic-gate 	}
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate 	return (result);
390*0Sstevel@tonic-gate }
391