From 79cbeafe9f948217d388045882015bdd00df706c Mon Sep 17 00:00:00 2001 From: Tim Newsome Date: Tue, 26 Dec 2017 13:44:26 -0800 Subject: [PATCH] Support listening on port 0. When listening on port 0, the system will assign a random open port. We use this to run multiple OpenOCD instances against multiple simulators as part of regression testing. This mechanism means the various test instances don't have to coordinate to ensure they don't reuse any ports. The required changes are minimal: 1. Don't increment the port number when it's 0. 2. Print out which port was assigned by the system. Change-Id: I404c801fc405e9d8eb8420562c02e78d4db6242f Signed-off-by: Tim Newsome Reviewed-on: http://openocd.zylin.com/4316 Tested-by: jenkins Reviewed-by: Andreas Fritiofson --- src/server/gdb_server.c | 8 +++++++- src/server/server.c | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/server/gdb_server.c b/src/server/gdb_server.c index fc0868b0a..b2d13d72f 100644 --- a/src/server/gdb_server.c +++ b/src/server/gdb_server.c @@ -3090,7 +3090,13 @@ static int gdb_target_add_one(struct target *target) if (!*end) { if (parse_long(gdb_port_next, &portnumber) == ERROR_OK) { free(gdb_port_next); - gdb_port_next = alloc_printf("%d", portnumber+1); + if (portnumber) { + gdb_port_next = alloc_printf("%d", portnumber+1); + } else { + /* Don't increment if gdb_port is 0, since we're just + * trying to allocate an unused port. */ + gdb_port_next = strdup("0"); + } } } } diff --git a/src/server/server.c b/src/server/server.c index 517d62a79..1e52e979d 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -298,6 +298,12 @@ int add_service(char *name, free_service(c); return ERROR_FAIL; } + + struct sockaddr_in addr_in; + socklen_t addr_in_size = sizeof(addr_in); + getsockname(c->fd, (struct sockaddr *)&addr_in, &addr_in_size); + LOG_INFO("Listening on port %hu for %s connections", + ntohs(addr_in.sin_port), name); } else if (c->type == CONNECTION_STDINOUT) { c->fd = fileno(stdin);