aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/lib/iomgr/tcp_server_posix.c
diff options
context:
space:
mode:
authorGravatar Dan Born <dborn@google.com>2016-10-03 20:33:25 -0700
committerGravatar Dan Born <dborn@google.com>2016-10-03 20:33:25 -0700
commit4e826be32ee6d67f3e71971e3475fdd8d2f6176c (patch)
treee92b544e467ff5728da5e985bd3fc8dcf358fb21 /src/core/lib/iomgr/tcp_server_posix.c
parent93b09478f0264f62e577b215dea7bc908abc6b98 (diff)
Correctly count port indices and siblings
Diffstat (limited to 'src/core/lib/iomgr/tcp_server_posix.c')
-rw-r--r--src/core/lib/iomgr/tcp_server_posix.c41
1 files changed, 23 insertions, 18 deletions
diff --git a/src/core/lib/iomgr/tcp_server_posix.c b/src/core/lib/iomgr/tcp_server_posix.c
index 2d3f6cf9a7..596f8280d4 100644
--- a/src/core/lib/iomgr/tcp_server_posix.c
+++ b/src/core/lib/iomgr/tcp_server_posix.c
@@ -648,35 +648,40 @@ done:
}
}
-unsigned grpc_tcp_server_port_fd_count(grpc_tcp_server *s,
- unsigned port_index) {
- unsigned num_fds = 0;
+/* Return listener at port_index or NULL. */
+static grpc_tcp_listener *get_port_index(grpc_tcp_server *s,
+ unsigned port_index) {
+ unsigned num_ports = 0;
grpc_tcp_listener *sp;
- for (sp = s->head; sp && port_index != 0; sp = sp->next) {
+ for (sp = s->head; sp; sp = sp->next) {
if (!sp->is_sibling) {
- --port_index;
+ if (++num_ports > port_index) {
+ return sp;
+ }
}
}
- for (; sp; sp = sp->sibling, ++num_fds)
- ;
+ return NULL;
+}
+
+unsigned grpc_tcp_server_port_fd_count(grpc_tcp_server *s,
+ unsigned port_index) {
+ unsigned num_fds = 0;
+ grpc_tcp_listener *sp = get_port_index(s, port_index);
+ for (; sp; sp = sp->sibling) {
+ ++num_fds;
+ }
return num_fds;
}
int grpc_tcp_server_port_fd(grpc_tcp_server *s, unsigned port_index,
unsigned fd_index) {
- grpc_tcp_listener *sp;
- for (sp = s->head; sp && port_index != 0; sp = sp->next) {
- if (!sp->is_sibling) {
- --port_index;
+ grpc_tcp_listener *sp = get_port_index(s, port_index);
+ for (; sp; sp = sp->sibling, --fd_index) {
+ if (fd_index == 0) {
+ return sp->fd;
}
}
- for (; sp && fd_index != 0; sp = sp->sibling, --fd_index)
- ;
- if (sp) {
- return sp->fd;
- } else {
- return -1;
- }
+ return -1;
}
void grpc_tcp_server_start(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s,