summaryrefslogtreecommitdiff
path: root/canohost.c
diff options
context:
space:
mode:
authorColin Watson <cjwatson@debian.org>2005-01-04 13:07:27 +0000
committerColin Watson <cjwatson@debian.org>2005-01-04 13:07:27 +0000
commitfd0f611b70a83d80fe8793af785542ee5541b7cd (patch)
treebededd22bb7eeec52e20083237ab7e4113445a16 /canohost.c
parentc44fe9a5b9d3db96a7249b04d915f17e4a3a3b04 (diff)
parentebd2ce335af5861020c79fddb1ae35c03bf036cf (diff)
Merge 3.9p1 to the trunk.
Diffstat (limited to 'canohost.c')
-rw-r--r--canohost.c38
1 files changed, 22 insertions, 16 deletions
diff --git a/canohost.c b/canohost.c
index a0067afc1..8ad684d6c 100644
--- a/canohost.c
+++ b/canohost.c
@@ -12,7 +12,7 @@
12 */ 12 */
13 13
14#include "includes.h" 14#include "includes.h"
15RCSID("$OpenBSD: canohost.c,v 1.38 2003/09/23 20:17:11 markus Exp $"); 15RCSID("$OpenBSD: canohost.c,v 1.41 2004/07/21 11:51:29 djm Exp $");
16 16
17#include "packet.h" 17#include "packet.h"
18#include "xmalloc.h" 18#include "xmalloc.h"
@@ -28,7 +28,7 @@ static void ipv64_normalise_mapped(struct sockaddr_storage *, socklen_t *);
28 */ 28 */
29 29
30static char * 30static char *
31get_remote_hostname(int socket, int use_dns) 31get_remote_hostname(int sock, int use_dns)
32{ 32{
33 struct sockaddr_storage from; 33 struct sockaddr_storage from;
34 int i; 34 int i;
@@ -39,13 +39,13 @@ get_remote_hostname(int socket, int use_dns)
39 /* Get IP address of client. */ 39 /* Get IP address of client. */
40 fromlen = sizeof(from); 40 fromlen = sizeof(from);
41 memset(&from, 0, sizeof(from)); 41 memset(&from, 0, sizeof(from));
42 if (getpeername(socket, (struct sockaddr *)&from, &fromlen) < 0) { 42 if (getpeername(sock, (struct sockaddr *)&from, &fromlen) < 0) {
43 debug("getpeername failed: %.100s", strerror(errno)); 43 debug("getpeername failed: %.100s", strerror(errno));
44 cleanup_exit(255); 44 cleanup_exit(255);
45 } 45 }
46 46
47 if (from.ss_family == AF_INET) 47 if (from.ss_family == AF_INET)
48 check_ip_options(socket, ntop); 48 check_ip_options(sock, ntop);
49 49
50 ipv64_normalise_mapped(&from, &fromlen); 50 ipv64_normalise_mapped(&from, &fromlen);
51 51
@@ -138,7 +138,7 @@ get_remote_hostname(int socket, int use_dns)
138 */ 138 */
139/* IPv4 only */ 139/* IPv4 only */
140static void 140static void
141check_ip_options(int socket, char *ipaddr) 141check_ip_options(int sock, char *ipaddr)
142{ 142{
143#ifdef IP_OPTIONS 143#ifdef IP_OPTIONS
144 u_char options[200]; 144 u_char options[200];
@@ -152,7 +152,7 @@ check_ip_options(int socket, char *ipaddr)
152 else 152 else
153 ipproto = IPPROTO_IP; 153 ipproto = IPPROTO_IP;
154 option_size = sizeof(options); 154 option_size = sizeof(options);
155 if (getsockopt(socket, ipproto, IP_OPTIONS, options, 155 if (getsockopt(sock, ipproto, IP_OPTIONS, options,
156 &option_size) >= 0 && option_size != 0) { 156 &option_size) >= 0 && option_size != 0) {
157 text[0] = '\0'; 157 text[0] = '\0';
158 for (i = 0; i < option_size; i++) 158 for (i = 0; i < option_size; i++)
@@ -227,7 +227,7 @@ get_canonical_hostname(int use_dns)
227 * The returned string must be freed. 227 * The returned string must be freed.
228 */ 228 */
229static char * 229static char *
230get_socket_address(int socket, int remote, int flags) 230get_socket_address(int sock, int remote, int flags)
231{ 231{
232 struct sockaddr_storage addr; 232 struct sockaddr_storage addr;
233 socklen_t addrlen; 233 socklen_t addrlen;
@@ -238,11 +238,11 @@ get_socket_address(int socket, int remote, int flags)
238 memset(&addr, 0, sizeof(addr)); 238 memset(&addr, 0, sizeof(addr));
239 239
240 if (remote) { 240 if (remote) {
241 if (getpeername(socket, (struct sockaddr *)&addr, &addrlen) 241 if (getpeername(sock, (struct sockaddr *)&addr, &addrlen)
242 < 0) 242 < 0)
243 return NULL; 243 return NULL;
244 } else { 244 } else {
245 if (getsockname(socket, (struct sockaddr *)&addr, &addrlen) 245 if (getsockname(sock, (struct sockaddr *)&addr, &addrlen)
246 < 0) 246 < 0)
247 return NULL; 247 return NULL;
248 } 248 }
@@ -261,29 +261,29 @@ get_socket_address(int socket, int remote, int flags)
261} 261}
262 262
263char * 263char *
264get_peer_ipaddr(int socket) 264get_peer_ipaddr(int sock)
265{ 265{
266 char *p; 266 char *p;
267 267
268 if ((p = get_socket_address(socket, 1, NI_NUMERICHOST)) != NULL) 268 if ((p = get_socket_address(sock, 1, NI_NUMERICHOST)) != NULL)
269 return p; 269 return p;
270 return xstrdup("UNKNOWN"); 270 return xstrdup("UNKNOWN");
271} 271}
272 272
273char * 273char *
274get_local_ipaddr(int socket) 274get_local_ipaddr(int sock)
275{ 275{
276 char *p; 276 char *p;
277 277
278 if ((p = get_socket_address(socket, 0, NI_NUMERICHOST)) != NULL) 278 if ((p = get_socket_address(sock, 0, NI_NUMERICHOST)) != NULL)
279 return p; 279 return p;
280 return xstrdup("UNKNOWN"); 280 return xstrdup("UNKNOWN");
281} 281}
282 282
283char * 283char *
284get_local_name(int socket) 284get_local_name(int sock)
285{ 285{
286 return get_socket_address(socket, 0, NI_NAMEREQD); 286 return get_socket_address(sock, 0, NI_NAMEREQD);
287} 287}
288 288
289/* 289/*
@@ -382,7 +382,13 @@ get_peer_port(int sock)
382int 382int
383get_remote_port(void) 383get_remote_port(void)
384{ 384{
385 return get_port(0); 385 static int port = -1;
386
387 /* Cache to avoid getpeername() on a dead connection */
388 if (port == -1)
389 port = get_port(0);
390
391 return port;
386} 392}
387 393
388int 394int