From ec50b15e196594c059c2d6d9a5a7b810b714bd53 Mon Sep 17 00:00:00 2001 From: Nicolas Van Eenaeme Date: Tue, 19 Nov 2013 23:25:45 +0100 Subject: [PATCH] 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. --- libmemcached-1.0/server.h | 3 +++ libmemcached/server.cc | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) 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); -- 2.30.2