version 1.27.2.3, 2002/10/11 14:53:06 |
version 1.28, 2001/12/05 03:56:39 |
|
|
*/ |
*/ |
|
|
static char * |
static char * |
get_remote_hostname(int socket, int verify_reverse_mapping) |
get_remote_hostname(int socket, int reverse_mapping_check) |
{ |
{ |
struct sockaddr_storage from; |
struct sockaddr_storage from; |
int i; |
int i; |
|
|
debug("getpeername failed: %.100s", strerror(errno)); |
debug("getpeername failed: %.100s", strerror(errno)); |
fatal_cleanup(); |
fatal_cleanup(); |
} |
} |
|
if (from.ss_family == AF_INET) |
|
check_ip_options(socket, ntop); |
|
|
if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop), |
if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop), |
NULL, 0, NI_NUMERICHOST) != 0) |
NULL, 0, NI_NUMERICHOST) != 0) |
fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed"); |
fatal("get_remote_hostname: getnameinfo NI_NUMERICHOST failed"); |
|
|
if (from.ss_family == AF_INET) |
|
check_ip_options(socket, ntop); |
|
|
|
debug3("Trying to reverse map address %.100s.", ntop); |
debug3("Trying to reverse map address %.100s.", ntop); |
/* Map the IP address to a host name. */ |
/* Map the IP address to a host name. */ |
if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name), |
if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name), |
NULL, 0, NI_NAMEREQD) != 0) { |
NULL, 0, NI_NAMEREQD) != 0) { |
/* Host name not found. Use ip address. */ |
/* Host name not found. Use ip address. */ |
#if 0 |
|
log("Could not reverse map address %.100s.", ntop); |
log("Could not reverse map address %.100s.", ntop); |
#endif |
|
return xstrdup(ntop); |
return xstrdup(ntop); |
} |
} |
|
|
|
|
if (isupper(name[i])) |
if (isupper(name[i])) |
name[i] = tolower(name[i]); |
name[i] = tolower(name[i]); |
|
|
if (!verify_reverse_mapping) |
if (!reverse_mapping_check) |
return xstrdup(name); |
return xstrdup(name); |
/* |
/* |
* Map it back to an IP address and check that the given |
* Map it back to an IP address and check that the given |
|
|
else |
else |
ipproto = IPPROTO_IP; |
ipproto = IPPROTO_IP; |
option_size = sizeof(options); |
option_size = sizeof(options); |
if (getsockopt(socket, ipproto, IP_OPTIONS, options, |
if (getsockopt(socket, ipproto, IP_OPTIONS, (void *)options, |
&option_size) >= 0 && option_size != 0) { |
&option_size) >= 0 && option_size != 0) { |
text[0] = '\0'; |
text[0] = '\0'; |
for (i = 0; i < option_size; i++) |
for (i = 0; i < option_size; i++) |
|
|
*/ |
*/ |
|
|
const char * |
const char * |
get_canonical_hostname(int verify_reverse_mapping) |
get_canonical_hostname(int reverse_mapping_check) |
{ |
{ |
static char *canonical_host_name = NULL; |
static char *canonical_host_name = NULL; |
static int verify_reverse_mapping_done = 0; |
static int reverse_mapping_checked = 0; |
|
|
/* Check if we have previously retrieved name with same option. */ |
/* Check if we have previously retrieved name with same option. */ |
if (canonical_host_name != NULL) { |
if (canonical_host_name != NULL) { |
if (verify_reverse_mapping_done != verify_reverse_mapping) |
if (reverse_mapping_checked != reverse_mapping_check) |
xfree(canonical_host_name); |
xfree(canonical_host_name); |
else |
else |
return canonical_host_name; |
return canonical_host_name; |
|
|
/* Get the real hostname if socket; otherwise return UNKNOWN. */ |
/* Get the real hostname if socket; otherwise return UNKNOWN. */ |
if (packet_connection_is_on_socket()) |
if (packet_connection_is_on_socket()) |
canonical_host_name = get_remote_hostname( |
canonical_host_name = get_remote_hostname( |
packet_get_connection_in(), verify_reverse_mapping); |
packet_get_connection_in(), reverse_mapping_check); |
else |
else |
canonical_host_name = xstrdup("UNKNOWN"); |
canonical_host_name = xstrdup("UNKNOWN"); |
|
|
verify_reverse_mapping_done = verify_reverse_mapping; |
reverse_mapping_checked = reverse_mapping_check; |
return canonical_host_name; |
return canonical_host_name; |
} |
} |
|
|
|
|
|
|
if (remote) { |
if (remote) { |
if (getpeername(socket, (struct sockaddr *)&addr, &addrlen) |
if (getpeername(socket, (struct sockaddr *)&addr, &addrlen) |
< 0) |
< 0) { |
|
debug("get_socket_ipaddr: getpeername failed: %.100s", |
|
strerror(errno)); |
return NULL; |
return NULL; |
|
} |
} else { |
} else { |
if (getsockname(socket, (struct sockaddr *)&addr, &addrlen) |
if (getsockname(socket, (struct sockaddr *)&addr, &addrlen) |
< 0) |
< 0) { |
|
debug("get_socket_ipaddr: getsockname failed: %.100s", |
|
strerror(errno)); |
return NULL; |
return NULL; |
|
} |
} |
} |
/* Get the address in ascii. */ |
/* Get the address in ascii. */ |
if (getnameinfo((struct sockaddr *)&addr, addrlen, ntop, sizeof(ntop), |
if (getnameinfo((struct sockaddr *)&addr, addrlen, ntop, sizeof(ntop), |
NULL, 0, flags) != 0) { |
NULL, 0, flags) != 0) { |
error("get_socket_ipaddr: getnameinfo %d failed", flags); |
error("get_socket_ipaddr: getnameinfo %d failed", flags); |
return NULL; |
return NULL; |
} |
} |
|
|
char * |
char * |
get_peer_ipaddr(int socket) |
get_peer_ipaddr(int socket) |
{ |
{ |
char *p; |
return get_socket_address(socket, 1, NI_NUMERICHOST); |
|
|
if ((p = get_socket_address(socket, 1, NI_NUMERICHOST)) != NULL) |
|
return p; |
|
return xstrdup("UNKNOWN"); |
|
} |
} |
|
|
char * |
char * |
get_local_ipaddr(int socket) |
get_local_ipaddr(int socket) |
{ |
{ |
char *p; |
return get_socket_address(socket, 0, NI_NUMERICHOST); |
|
|
if ((p = get_socket_address(socket, 0, NI_NUMERICHOST)) != NULL) |
|
return p; |
|
return xstrdup("UNKNOWN"); |
|
} |
} |
|
|
char * |
char * |
|
|
} |
} |
|
|
const char * |
const char * |
get_remote_name_or_ip(u_int utmp_len, int verify_reverse_mapping) |
get_remote_name_or_ip(u_int utmp_len, int reverse_mapping_check) |
{ |
{ |
static const char *remote = ""; |
static const char *remote = ""; |
if (utmp_len > 0) |
if (utmp_len > 0) |
remote = get_canonical_hostname(verify_reverse_mapping); |
remote = get_canonical_hostname(reverse_mapping_check); |
if (utmp_len == 0 || strlen(remote) > utmp_len) |
if (utmp_len == 0 || strlen(remote) > utmp_len) |
remote = get_remote_ipaddr(); |
remote = get_remote_ipaddr(); |
return remote; |
return remote; |
|
|
} |
} |
/* Return port number. */ |
/* Return port number. */ |
if (getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0, |
if (getnameinfo((struct sockaddr *)&from, fromlen, NULL, 0, |
strport, sizeof(strport), NI_NUMERICSERV) != 0) |
strport, sizeof(strport), NI_NUMERICSERV) != 0) |
fatal("get_sock_port: getnameinfo NI_NUMERICSERV failed"); |
fatal("get_sock_port: getnameinfo NI_NUMERICSERV failed"); |
return atoi(strport); |
return atoi(strport); |
} |
} |