From: Timo Sirainen Date: Mon, 31 May 2004 18:56:43 +0000 (+0300) Subject: Added net_getpeername() X-Git-Tag: 1.1.alpha1~4008 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=235eb6fda7ad8db6cc3b1b76f9d04fc8c7fbcee3;p=thirdparty%2Fdovecot%2Fcore.git Added net_getpeername() --HG-- branch : HEAD --- diff --git a/src/lib/network.c b/src/lib/network.c index 6f317e78ae..fbb8d3ba1f 100644 --- a/src/lib/network.c +++ b/src/lib/network.c @@ -487,6 +487,23 @@ int net_getsockname(int fd, struct ip_addr *addr, unsigned int *port) return 0; } +int net_getpeername(int fd, struct ip_addr *addr, unsigned int *port) +{ + union sockaddr_union so; + socklen_t addrlen; + + i_assert(fd >= 0); + + addrlen = sizeof(so); + if (getpeername(fd, (struct sockaddr *) &so, &addrlen) == -1) + return -1; + + if (addr != NULL) sin_get_ip(&so, addr); + if (port != NULL) *port = sin_get_port(&so); + + return 0; +} + const char *net_ip2addr(const struct ip_addr *ip) { #ifdef HAVE_IPV6 diff --git a/src/lib/network.h b/src/lib/network.h index b48c039900..50635a716f 100644 --- a/src/lib/network.h +++ b/src/lib/network.h @@ -87,8 +87,10 @@ const char *net_gethosterror(int error); some error with name server) */ int net_hosterror_notfound(int error); -/* Get socket address/port */ +/* Get socket local address/port */ int net_getsockname(int fd, struct ip_addr *addr, unsigned int *port); +/* Get socket remote address/port */ +int net_getpeername(int fd, struct ip_addr *addr, unsigned int *port); /* Returns ip_addr as string, or NULL if ip is invalid. */ const char *net_ip2addr(const struct ip_addr *ip);