Return bound port number from grpc_server_add_http2_port.
Allows tests to bind to port 0 and still have clients connect to them.
Change on 2015/01/12 by ctiller <ctiller@google.com>
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=83800669
diff --git a/src/core/iomgr/sockaddr_utils.c b/src/core/iomgr/sockaddr_utils.c
index f709d35..eca14a4 100644
--- a/src/core/iomgr/sockaddr_utils.c
+++ b/src/core/iomgr/sockaddr_utils.c
@@ -153,3 +153,31 @@
errno = save_errno;
return ret;
}
+
+int grpc_sockaddr_get_port(const struct sockaddr *addr) {
+ switch (addr->sa_family) {
+ case AF_INET:
+ return ntohs(((struct sockaddr_in *)addr)->sin_port);
+ case AF_INET6:
+ return ntohs(((struct sockaddr_in6 *)addr)->sin6_port);
+ default:
+ gpr_log(GPR_ERROR, "Unknown socket family %d in %s", addr->sa_family,
+ __FUNCTION__);
+ return 0;
+ }
+}
+
+int grpc_sockaddr_set_port(const struct sockaddr *addr, int port) {
+ switch (addr->sa_family) {
+ case AF_INET:
+ ((struct sockaddr_in *)addr)->sin_port = htons(port);
+ return 1;
+ case AF_INET6:
+ ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
+ return 1;
+ default:
+ gpr_log(GPR_ERROR, "Unknown socket family %d in %s", addr->sa_family,
+ __FUNCTION__);
+ return 0;
+ }
+}