Merge pull request #8486 from y-zeng/get_tos

Add grpc_endpoint_get_fd and grpc_transport_get_endpoint
diff --git a/src/core/ext/transport/chttp2/transport/chttp2_transport.c b/src/core/ext/transport/chttp2/transport/chttp2_transport.c
index 1ffa916..fb8fbae 100644
--- a/src/core/ext/transport/chttp2/transport/chttp2_transport.c
+++ b/src/core/ext/transport/chttp2/transport/chttp2_transport.c
@@ -2294,6 +2294,14 @@
   return gpr_strdup(((grpc_chttp2_transport *)t)->peer_string);
 }
 
+/*******************************************************************************
+ * MONITORING
+ */
+static grpc_endpoint *chttp2_get_endpoint(grpc_exec_ctx *exec_ctx,
+                                          grpc_transport *t) {
+  return ((grpc_chttp2_transport *)t)->ep;
+}
+
 static const grpc_transport_vtable vtable = {sizeof(grpc_chttp2_stream),
                                              "chttp2",
                                              init_stream,
@@ -2303,7 +2311,8 @@
                                              perform_transport_op,
                                              destroy_stream,
                                              destroy_transport,
-                                             chttp2_get_peer};
+                                             chttp2_get_peer,
+                                             chttp2_get_endpoint};
 
 grpc_transport *grpc_create_chttp2_transport(
     grpc_exec_ctx *exec_ctx, const grpc_channel_args *channel_args,
diff --git a/src/core/ext/transport/cronet/transport/cronet_transport.c b/src/core/ext/transport/cronet/transport/cronet_transport.c
index 2ab9a8c..a4c1101 100644
--- a/src/core/ext/transport/cronet/transport/cronet_transport.c
+++ b/src/core/ext/transport/cronet/transport/cronet_transport.c
@@ -42,6 +42,7 @@
 #include <grpc/support/useful.h>
 
 #include "src/core/ext/transport/chttp2/transport/incoming_metadata.h"
+#include "src/core/lib/iomgr/endpoint.h"
 #include "src/core/lib/iomgr/exec_ctx.h"
 #include "src/core/lib/support/string.h"
 #include "src/core/lib/surface/channel.h"
@@ -1095,6 +1096,11 @@
   return NULL;
 }
 
+static grpc_endpoint *get_endpoint(grpc_exec_ctx *exec_ctx,
+                                   grpc_transport *gt) {
+  return NULL;
+}
+
 static void perform_op(grpc_exec_ctx *exec_ctx, grpc_transport *gt,
                        grpc_transport_op *op) {}
 
@@ -1107,4 +1113,5 @@
                                                   perform_op,
                                                   destroy_stream,
                                                   destroy_transport,
-                                                  get_peer};
+                                                  get_peer,
+                                                  get_endpoint};
diff --git a/src/core/lib/iomgr/endpoint.c b/src/core/lib/iomgr/endpoint.c
index a4b6668..2d300f4 100644
--- a/src/core/lib/iomgr/endpoint.c
+++ b/src/core/lib/iomgr/endpoint.c
@@ -66,6 +66,8 @@
   return ep->vtable->get_peer(ep);
 }
 
+int grpc_endpoint_get_fd(grpc_endpoint* ep) { return ep->vtable->get_fd(ep); }
+
 grpc_workqueue* grpc_endpoint_get_workqueue(grpc_endpoint* ep) {
   return ep->vtable->get_workqueue(ep);
 }
diff --git a/src/core/lib/iomgr/endpoint.h b/src/core/lib/iomgr/endpoint.h
index bf211ca..1609b64 100644
--- a/src/core/lib/iomgr/endpoint.h
+++ b/src/core/lib/iomgr/endpoint.h
@@ -61,6 +61,7 @@
   void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep);
   grpc_resource_user *(*get_resource_user)(grpc_endpoint *ep);
   char *(*get_peer)(grpc_endpoint *ep);
+  int (*get_fd)(grpc_endpoint *ep);
 };
 
 /* When data is available on the connection, calls the callback with slices.
@@ -73,6 +74,10 @@
 
 char *grpc_endpoint_get_peer(grpc_endpoint *ep);
 
+/* Get the file descriptor used by \a ep. Return -1 if \a ep is not using an fd.
+   */
+int grpc_endpoint_get_fd(grpc_endpoint *ep);
+
 /* Retrieve a reference to the workqueue associated with this endpoint */
 grpc_workqueue *grpc_endpoint_get_workqueue(grpc_endpoint *ep);
 
diff --git a/src/core/lib/iomgr/tcp_posix.c b/src/core/lib/iomgr/tcp_posix.c
index c48f4e8..12a4797 100644
--- a/src/core/lib/iomgr/tcp_posix.c
+++ b/src/core/lib/iomgr/tcp_posix.c
@@ -493,6 +493,11 @@
   return gpr_strdup(tcp->peer_string);
 }
 
+static int tcp_get_fd(grpc_endpoint *ep) {
+  grpc_tcp *tcp = (grpc_tcp *)ep;
+  return tcp->fd;
+}
+
 static grpc_workqueue *tcp_get_workqueue(grpc_endpoint *ep) {
   grpc_tcp *tcp = (grpc_tcp *)ep;
   return grpc_fd_get_workqueue(tcp->em_fd);
@@ -511,7 +516,8 @@
                                             tcp_shutdown,
                                             tcp_destroy,
                                             tcp_get_resource_user,
-                                            tcp_get_peer};
+                                            tcp_get_peer,
+                                            tcp_get_fd};
 
 grpc_endpoint *grpc_tcp_create(grpc_fd *em_fd,
                                grpc_resource_quota *resource_quota,
diff --git a/src/core/lib/security/transport/secure_endpoint.c b/src/core/lib/security/transport/secure_endpoint.c
index fba3314..2d32fae 100644
--- a/src/core/lib/security/transport/secure_endpoint.c
+++ b/src/core/lib/security/transport/secure_endpoint.c
@@ -38,6 +38,7 @@
 #include <grpc/support/log.h>
 #include <grpc/support/sync.h>
 #include "src/core/lib/debug/trace.h"
+#include "src/core/lib/iomgr/sockaddr.h"
 #include "src/core/lib/profiling/timers.h"
 #include "src/core/lib/security/transport/tsi_error.h"
 #include "src/core/lib/slice/slice_string_helpers.h"
@@ -366,6 +367,8 @@
   return grpc_endpoint_get_peer(ep->wrapped_ep);
 }
 
+static int endpoint_get_fd(grpc_endpoint *secure_ep) { return -1; }
+
 static grpc_workqueue *endpoint_get_workqueue(grpc_endpoint *secure_ep) {
   secure_endpoint *ep = (secure_endpoint *)secure_ep;
   return grpc_endpoint_get_workqueue(ep->wrapped_ep);
@@ -385,7 +388,8 @@
                                             endpoint_shutdown,
                                             endpoint_destroy,
                                             endpoint_get_resource_user,
-                                            endpoint_get_peer};
+                                            endpoint_get_peer,
+                                            endpoint_get_fd};
 
 grpc_endpoint *grpc_secure_endpoint_create(
     struct tsi_frame_protector *protector, grpc_endpoint *transport,
diff --git a/src/core/lib/transport/transport.c b/src/core/lib/transport/transport.c
index 866cd9e..b448126 100644
--- a/src/core/lib/transport/transport.c
+++ b/src/core/lib/transport/transport.c
@@ -160,6 +160,11 @@
   return transport->vtable->get_peer(exec_ctx, transport);
 }
 
+grpc_endpoint *grpc_transport_get_endpoint(grpc_exec_ctx *exec_ctx,
+                                           grpc_transport *transport) {
+  return transport->vtable->get_endpoint(exec_ctx, transport);
+}
+
 void grpc_transport_stream_op_finish_with_failure(grpc_exec_ctx *exec_ctx,
                                                   grpc_transport_stream_op *op,
                                                   grpc_error *error) {
diff --git a/src/core/lib/transport/transport.h b/src/core/lib/transport/transport.h
index 8916b28..96c2674 100644
--- a/src/core/lib/transport/transport.h
+++ b/src/core/lib/transport/transport.h
@@ -37,6 +37,7 @@
 #include <stddef.h>
 
 #include "src/core/lib/channel/context.h"
+#include "src/core/lib/iomgr/endpoint.h"
 #include "src/core/lib/iomgr/polling_entity.h"
 #include "src/core/lib/iomgr/pollset.h"
 #include "src/core/lib/iomgr/pollset_set.h"
@@ -295,6 +296,10 @@
 char *grpc_transport_get_peer(grpc_exec_ctx *exec_ctx,
                               grpc_transport *transport);
 
+/* Get the endpoint used by \a transport */
+grpc_endpoint *grpc_transport_get_endpoint(grpc_exec_ctx *exec_ctx,
+                                           grpc_transport *transport);
+
 /* Allocate a grpc_transport_op, and preconfigure the on_consumed closure to
    \a on_consumed and then delete the returned transport op */
 grpc_transport_op *grpc_make_transport_op(grpc_closure *on_consumed);
diff --git a/src/core/lib/transport/transport_impl.h b/src/core/lib/transport/transport_impl.h
index fc71406..8553148 100644
--- a/src/core/lib/transport/transport_impl.h
+++ b/src/core/lib/transport/transport_impl.h
@@ -74,6 +74,9 @@
 
   /* implementation of grpc_transport_get_peer */
   char *(*get_peer)(grpc_exec_ctx *exec_ctx, grpc_transport *self);
+
+  /* implementation of grpc_transport_get_endpoint */
+  grpc_endpoint *(*get_endpoint)(grpc_exec_ctx *exec_ctx, grpc_transport *self);
 } grpc_transport_vtable;
 
 /* an instance of a grpc transport */
diff --git a/test/core/internal_api_canaries/iomgr.c b/test/core/internal_api_canaries/iomgr.c
index f1efa87..de03c47 100644
--- a/test/core/internal_api_canaries/iomgr.c
+++ b/test/core/internal_api_canaries/iomgr.c
@@ -85,7 +85,8 @@
                                  grpc_endpoint_shutdown,
                                  grpc_endpoint_destroy,
                                  grpc_endpoint_get_resource_user,
-                                 grpc_endpoint_get_peer};
+                                 grpc_endpoint_get_peer,
+                                 grpc_endpoint_get_fd};
   endpoint.vtable = &vtable;
 
   grpc_endpoint_read(&exec_ctx, &endpoint, NULL, NULL);
diff --git a/test/core/util/mock_endpoint.c b/test/core/util/mock_endpoint.c
index 28ff064..bf6d852 100644
--- a/test/core/util/mock_endpoint.c
+++ b/test/core/util/mock_endpoint.c
@@ -37,6 +37,7 @@
 
 #include <grpc/support/alloc.h>
 #include <grpc/support/string_util.h>
+#include "src/core/lib/iomgr/sockaddr.h"
 
 typedef struct grpc_mock_endpoint {
   grpc_endpoint base;
@@ -105,6 +106,8 @@
   return m->resource_user;
 }
 
+static int me_get_fd(grpc_endpoint *ep) { return -1; }
+
 static grpc_workqueue *me_get_workqueue(grpc_endpoint *ep) { return NULL; }
 
 static const grpc_endpoint_vtable vtable = {
@@ -117,6 +120,7 @@
     me_destroy,
     me_get_resource_user,
     me_get_peer,
+    me_get_fd,
 };
 
 grpc_endpoint *grpc_mock_endpoint_create(void (*on_write)(grpc_slice slice),
diff --git a/test/core/util/passthru_endpoint.c b/test/core/util/passthru_endpoint.c
index 8b323d1..b3405f02 100644
--- a/test/core/util/passthru_endpoint.c
+++ b/test/core/util/passthru_endpoint.c
@@ -37,6 +37,7 @@
 
 #include <grpc/support/alloc.h>
 #include <grpc/support/string_util.h>
+#include "src/core/lib/iomgr/sockaddr.h"
 
 typedef struct passthru_endpoint passthru_endpoint;
 
@@ -146,6 +147,8 @@
   return gpr_strdup("fake:mock_endpoint");
 }
 
+static int me_get_fd(grpc_endpoint *ep) { return -1; }
+
 static grpc_workqueue *me_get_workqueue(grpc_endpoint *ep) { return NULL; }
 
 static grpc_resource_user *me_get_resource_user(grpc_endpoint *ep) {
@@ -163,6 +166,7 @@
     me_destroy,
     me_get_resource_user,
     me_get_peer,
+    me_get_fd,
 };
 
 static void half_init(half *m, passthru_endpoint *parent,