From: Nicolas Van Eenaeme Date: Tue, 19 Nov 2013 22:25:45 +0000 (+0100) Subject: Added the ability to get the local port of a memcache_instance_st. X-Git-Tag: 1.0.18~7^2~1 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=ec50b15e196594c059c2d6d9a5a7b810b714bd53;hp=e5e68ea1b5afaf29870a5f5faf5908d32fbd1c70;p=m6w6%2Flibmemcached Added the ability to get the local port of a memcache_instance_st. Sometimes when you're hunting bugs on a production webserver (with multiple workers) you want to tcpdump and see what's going on. This patch adds the memcached_server_srcport method to the library which returns the local sourceport of the connection (if there's a connection). We use it a lot. --- diff --git a/libmemcached-1.0/server.h b/libmemcached-1.0/server.h index 856c0e7e..abd0bfb9 100644 --- a/libmemcached-1.0/server.h +++ b/libmemcached-1.0/server.h @@ -103,6 +103,9 @@ const char *memcached_server_name(const memcached_instance_st * self); LIBMEMCACHED_API in_port_t memcached_server_port(const memcached_instance_st * self); +LIBMEMCACHED_API +in_port_t memcached_server_srcport(const memcached_instance_st * self); + LIBMEMCACHED_API void memcached_instance_next_retry(const memcached_instance_st * self, const time_t absolute_time); diff --git a/libmemcached/server.cc b/libmemcached/server.cc index b38120ad..c4747ddc 100644 --- a/libmemcached/server.cc +++ b/libmemcached/server.cc @@ -213,6 +213,24 @@ in_port_t memcached_server_port(const memcached_instance_st * self) return self->port(); } +in_port_t memcached_server_srcport(const memcached_instance_st * self) +{ + WATCHPOINT_ASSERT(self); + if (self == NULL || self->fd == INVALID_SOCKET || (self->type != MEMCACHED_CONNECTION_TCP && self->type != MEMCACHED_CONNECTION_UDP)) + { + return 0; + } + + struct sockaddr_in sin; + socklen_t addrlen= sizeof(sin); + if (getsockname(self->fd, (struct sockaddr*)&sin, &addrlen) != -1) + { + return ntohs(sin.sin_port); + } + + return -1; +} + uint32_t memcached_server_response_count(const memcached_instance_st * self) { WATCHPOINT_ASSERT(self);