mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-06-30 19:12:48 -05:00
Improve sk_peer_info.
Previously, it returned a human-readable string suitable for log files, which tried to say something useful about the remote end of a socket. Now it returns a whole SocketPeerInfo structure, of which that human-friendly log string is just one field, but also some of the same information - remote IP address and port, in particular - is provided in machine-readable form where it's available.
This commit is contained in:
@ -270,7 +270,7 @@ static const char *sk_handle_socket_error(Socket *s)
|
||||
return hs->error;
|
||||
}
|
||||
|
||||
static char *sk_handle_peer_info(Socket *s)
|
||||
static SocketPeerInfo *sk_handle_peer_info(Socket *s)
|
||||
{
|
||||
HandleSocket *hs = container_of(s, HandleSocket, sock);
|
||||
ULONG pid;
|
||||
@ -299,8 +299,14 @@ static char *sk_handle_peer_info(Socket *s)
|
||||
* to log what we can find out about the client end.
|
||||
*/
|
||||
if (p_GetNamedPipeClientProcessId &&
|
||||
p_GetNamedPipeClientProcessId(hs->send_H, &pid))
|
||||
return dupprintf("process id %lu", (unsigned long)pid);
|
||||
p_GetNamedPipeClientProcessId(hs->send_H, &pid)) {
|
||||
SocketPeerInfo *pi = snew(SocketPeerInfo);
|
||||
pi->addressfamily = ADDRTYPE_LOCAL;
|
||||
pi->addr_text = NULL;
|
||||
pi->port = -1;
|
||||
pi->log_text = dupprintf("process id %lu", (unsigned long)pid);
|
||||
return pi;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
@ -904,7 +904,7 @@ static int sk_net_write_oob(Socket *s, const void *data, int len);
|
||||
static void sk_net_write_eof(Socket *s);
|
||||
static void sk_net_set_frozen(Socket *s, int is_frozen);
|
||||
static const char *sk_net_socket_error(Socket *s);
|
||||
static char *sk_net_peer_info(Socket *s);
|
||||
static SocketPeerInfo *sk_net_peer_info(Socket *s);
|
||||
|
||||
extern char *do_select(SOCKET skt, int startup);
|
||||
|
||||
@ -1763,7 +1763,7 @@ static const char *sk_net_socket_error(Socket *sock)
|
||||
return s->error;
|
||||
}
|
||||
|
||||
static char *sk_net_peer_info(Socket *sock)
|
||||
static SocketPeerInfo *sk_net_peer_info(Socket *sock)
|
||||
{
|
||||
NetSocket *s = container_of(sock, NetSocket, sock);
|
||||
#ifdef NO_IPV6
|
||||
@ -1773,26 +1773,43 @@ static char *sk_net_peer_info(Socket *sock)
|
||||
char buf[INET6_ADDRSTRLEN];
|
||||
#endif
|
||||
int addrlen = sizeof(addr);
|
||||
SocketPeerInfo *pi;
|
||||
|
||||
if (p_getpeername(s->s, (struct sockaddr *)&addr, &addrlen) < 0)
|
||||
return NULL;
|
||||
|
||||
pi = snew(SocketPeerInfo);
|
||||
pi->addressfamily = ADDRTYPE_UNSPEC;
|
||||
pi->addr_text = NULL;
|
||||
pi->port = -1;
|
||||
pi->log_text = NULL;
|
||||
|
||||
if (((struct sockaddr *)&addr)->sa_family == AF_INET) {
|
||||
return dupprintf
|
||||
("%s:%d",
|
||||
p_inet_ntoa(((struct sockaddr_in *)&addr)->sin_addr),
|
||||
(int)p_ntohs(((struct sockaddr_in *)&addr)->sin_port));
|
||||
pi->addressfamily = ADDRTYPE_IPV4;
|
||||
memcpy(pi->addr_bin.ipv4, &((struct sockaddr_in *)&addr)->sin_addr, 4);
|
||||
pi->port = p_ntohs(((struct sockaddr_in *)&addr)->sin_port);
|
||||
pi->addr_text = dupstr(
|
||||
p_inet_ntoa(((struct sockaddr_in *)&addr)->sin_addr));
|
||||
pi->log_text = dupprintf("%s:%d", pi->addr_text, pi->port);
|
||||
|
||||
#ifndef NO_IPV6
|
||||
} else if (((struct sockaddr *)&addr)->sa_family == AF_INET6) {
|
||||
return dupprintf
|
||||
("[%s]:%d",
|
||||
p_inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&addr)->sin6_addr,
|
||||
buf, sizeof(buf)),
|
||||
(int)p_ntohs(((struct sockaddr_in6 *)&addr)->sin6_port));
|
||||
pi->addressfamily = ADDRTYPE_IPV6;
|
||||
memcpy(pi->addr_bin.ipv6,
|
||||
&((struct sockaddr_in6 *)&addr)->sin6_addr, 16);
|
||||
pi->port = p_ntohs(((struct sockaddr_in6 *)&addr)->sin6_port);
|
||||
pi->addr_text = dupstr(
|
||||
p_inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&addr)->sin6_addr,
|
||||
buf, sizeof(buf)));
|
||||
pi->log_text = dupprintf("[%s]:%d", pi->addr_text, pi->port);
|
||||
|
||||
#endif
|
||||
} else {
|
||||
sfree(pi);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return pi;
|
||||
}
|
||||
|
||||
static void sk_net_set_frozen(Socket *sock, int is_frozen)
|
||||
|
@ -68,7 +68,7 @@ static const char *sk_namedpipeserver_socket_error(Socket *s)
|
||||
return ps->error;
|
||||
}
|
||||
|
||||
static char *sk_namedpipeserver_peer_info(Socket *s)
|
||||
static SocketPeerInfo *sk_namedpipeserver_peer_info(Socket *s)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
Reference in New Issue
Block a user