diff options
author | Daniel Stenberg <daniel@haxx.se> | 2020-04-20 14:53:50 +0200 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2020-04-20 22:48:57 +0200 |
commit | bae87dc0d93d8e56bd1038d4a46477992eaa1bdd (patch) | |
tree | 4c6c57aa114b17a19cdd8d39cb5e06335ce23c61 /tests/server | |
parent | 018dd775c868a53c15fdaba7c67aaae6c5fd35c1 (diff) |
tests: run the RTSP test server on a dynamic port number
To avoid port collisions.
Closes #5272
Diffstat (limited to 'tests/server')
-rw-r--r-- | tests/server/rtspd.c | 62 |
1 files changed, 56 insertions, 6 deletions
diff --git a/tests/server/rtspd.c b/tests/server/rtspd.c index 2d95d8359..6ee7787b1 100644 --- a/tests/server/rtspd.c +++ b/tests/server/rtspd.c @@ -1049,6 +1049,7 @@ int main(int argc, char *argv[]) int flag; unsigned short port = DEFAULT_PORT; const char *pidname = ".rtsp.pid"; + const char *portfile = NULL; struct httprequest req; int rc; int error; @@ -1075,6 +1076,11 @@ int main(int argc, char *argv[]) if(argc>arg) pidname = argv[arg++]; } + else if(!strcmp("--portfile", argv[arg])) { + arg++; + if(argc>arg) + portfile = argv[arg++]; + } else if(!strcmp("--logfile", argv[arg])) { arg++; if(argc>arg) @@ -1099,12 +1105,6 @@ int main(int argc, char *argv[]) if(argc>arg) { char *endptr; unsigned long ulnum = strtoul(argv[arg], &endptr, 10); - if((endptr != argv[arg] + strlen(argv[arg])) || - (ulnum < 1025UL) || (ulnum > 65535UL)) { - fprintf(stderr, "rtspd: invalid --port argument (%s)\n", - argv[arg]); - return 0; - } port = curlx_ultous(ulnum); arg++; } @@ -1121,6 +1121,7 @@ int main(int argc, char *argv[]) " --version\n" " --logfile [file]\n" " --pidfile [file]\n" + " --portfile [file]\n" " --ipv4\n" " --ipv6\n" " --port [port]\n" @@ -1188,6 +1189,49 @@ int main(int argc, char *argv[]) goto server_cleanup; } + if(!port) { + /* The system was supposed to choose a port number, figure out which + port we actually got and update the listener port value with it. */ + curl_socklen_t la_size; + srvr_sockaddr_union_t localaddr; +#ifdef ENABLE_IPV6 + if(!use_ipv6) +#endif + la_size = sizeof(localaddr.sa4); +#ifdef ENABLE_IPV6 + else + la_size = sizeof(localaddr.sa6); +#endif + memset(&localaddr.sa, 0, (size_t)la_size); + if(getsockname(sock, &localaddr.sa, &la_size) < 0) { + error = SOCKERRNO; + logmsg("getsockname() failed with error: (%d) %s", + error, strerror(error)); + sclose(sock); + goto server_cleanup; + } + switch(localaddr.sa.sa_family) { + case AF_INET: + port = ntohs(localaddr.sa4.sin_port); + break; +#ifdef ENABLE_IPV6 + case AF_INET6: + port = ntohs(localaddr.sa6.sin6_port); + break; +#endif + default: + break; + } + if(!port) { + /* Real failure, listener port shall not be zero beyond this point. */ + logmsg("Apparently getsockname() succeeded, with listener port zero."); + logmsg("A valid reason for this failure is a binary built without"); + logmsg("proper network library linkage. This might not be the only"); + logmsg("reason, but double check it before anything else."); + sclose(sock); + goto server_cleanup; + } + } logmsg("Running %s version on port %d", ipv_inuse, (int)port); /* start accepting connections */ @@ -1208,6 +1252,12 @@ int main(int argc, char *argv[]) if(!wrotepidfile) goto server_cleanup; + if(portfile) { + wrotepidfile = write_portfile(portfile, port); + if(!wrotepidfile) + goto server_cleanup; + } + for(;;) { msgsock = accept(sock, NULL, NULL); |