From fc8d8d6a43ca60fed934f2129c1742e7fa729349 Mon Sep 17 00:00:00 2001
From: yang-g <yangg@google.com>
Date: Mon, 27 Jul 2015 23:07:05 -0700
Subject: [PATCH] add support for detecting spurious outside connection.

---
 test/core/util/reconnect_server.c | 22 +++++++++++++++++++++-
 test/core/util/reconnect_server.h |  1 +
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/test/core/util/reconnect_server.c b/test/core/util/reconnect_server.c
index 263d75f494..74a8d44259 100644
--- a/test/core/util/reconnect_server.c
+++ b/test/core/util/reconnect_server.c
@@ -33,16 +33,17 @@
 
 #include "test/core/util/reconnect_server.h"
 
+#include <arpa/inet.h>
 #include <grpc/grpc.h>
 #include <grpc/support/alloc.h>
 #include <grpc/support/host_port.h>
 #include <grpc/support/log.h>
 #include <grpc/support/sync.h>
 #include <grpc/support/time.h>
+#include <string.h>
 #include "src/core/iomgr/endpoint.h"
 #include "src/core/iomgr/tcp_server.h"
 #include "test/core/util/port.h"
-#include <arpa/inet.h>
 
 static void pretty_print_backoffs(reconnect_server *server) {
   gpr_timespec diff;
@@ -65,11 +66,27 @@ static void pretty_print_backoffs(reconnect_server *server) {
 }
 
 static void on_connect(void *arg, grpc_endpoint *tcp) {
+  char *peer;
+  char *last_colon;
   reconnect_server *server = (reconnect_server *)arg;
   gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME);
   timestamp_list *new_tail;
+  peer = grpc_endpoint_get_peer(tcp);
   grpc_endpoint_shutdown(tcp);
   grpc_endpoint_destroy(tcp);
+  if (peer) {
+    last_colon = strrchr(peer, ':');
+    if (server->peer == NULL) {
+      server->peer = peer;
+    } else {
+      if (last_colon == NULL) {
+        gpr_log(GPR_ERROR, "peer does not contain a ':'");
+      } else if (strncmp(server->peer, peer, last_colon - peer) != 0) {
+        gpr_log(GPR_ERROR, "mismatched peer! %s vs %s", server->peer, peer);
+      }
+      gpr_free(peer);
+    }
+  }
   new_tail = gpr_malloc(sizeof(timestamp_list));
   new_tail->timestamp = now;
   new_tail->next = NULL;
@@ -90,6 +107,7 @@ void reconnect_server_init(reconnect_server *server) {
   server->pollsets[0] = &server->pollset;
   server->head = NULL;
   server->tail = NULL;
+  server->peer = NULL;
 }
 
 void reconnect_server_start(reconnect_server *server, int port) {
@@ -127,6 +145,8 @@ void reconnect_server_clear_timestamps(reconnect_server *server) {
     server->head = new_head;
   }
   server->tail = NULL;
+  gpr_free(server->peer);
+  server->peer = NULL;
 }
 
 static void do_nothing(void *ignored) {}
diff --git a/test/core/util/reconnect_server.h b/test/core/util/reconnect_server.h
index 43934ac7ad..8a278e0c7a 100644
--- a/test/core/util/reconnect_server.h
+++ b/test/core/util/reconnect_server.h
@@ -53,6 +53,7 @@ typedef struct reconnect_server {
   grpc_pollset *pollsets[1];
   timestamp_list *head;
   timestamp_list *tail;
+  char *peer;
 } reconnect_server;
 
 void reconnect_server_init(reconnect_server *server);
-- 
GitLab