From be10f705d1369af044251c9dc8d64fc846e2c6a7 Mon Sep 17 00:00:00 2001 From: Markus Schmidl Date: Mon, 21 Apr 2025 20:04:06 +0200 Subject: [PATCH 1/4] pcm-sensor-server: add bind ip argument --- src/pcm-sensor-server.cpp | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/pcm-sensor-server.cpp b/src/pcm-sensor-server.cpp index 2c122fec..0a7f8d28 100644 --- a/src/pcm-sensor-server.cpp +++ b/src/pcm-sensor-server.cpp @@ -3684,8 +3684,8 @@ void my_get_callback( HTTPServer* hs, HTTPRequest const & req, HTTPResponse & re } } -int startHTTPServer( unsigned short port ) { - HTTPServer server( "", port ); +int startHTTPServer( std::string const & ip, unsigned short port ) { + HTTPServer server( ip, port ); try { // HEAD is GET without body, we will remove the body in execute() server.registerCallback( HTTPRequestMethod::GET, my_get_callback ); @@ -3699,8 +3699,8 @@ int startHTTPServer( unsigned short port ) { } #if defined (USE_SSL) -int startHTTPSServer( unsigned short port, std::string const & cFile, std::string const & pkFile) { - HTTPSServer server( "", port ); +int startHTTPSServer( std::string const & ip, unsigned short port, std::string const & cFile, std::string const & pkFile) { + HTTPSServer server( ip, port ); try { server.setPrivateKeyFile ( pkFile ); server.setCertificateFile( cFile ); @@ -3724,6 +3724,7 @@ void printHelpText( std::string const & programName ) { #if defined (USE_SSL) std::cout << " -s : Use https protocol (default port " << DEFAULT_HTTPS_PORT << ")\n"; #endif + std::cout << " -h bind ip : Bind to ip address. (default ip is 0.0.0.0)\n"; std::cout << " -p portnumber : Run on port (default port is " << DEFAULT_HTTP_PORT << ")\n"; std::cout << " -r|--reset : Reset programming of the performance counters.\n"; std::cout << " -D|--debug level : level = 0: no debug info, > 0 increase verbosity.\n"; @@ -3762,6 +3763,7 @@ int mainThrows(int argc, char * argv[]) { #endif bool forceRTMAbortMode = false; bool printTopology = false; + std::string host = ""; unsigned short port = 0; unsigned short debug_level = 0; std::string certificateFile; @@ -3788,6 +3790,14 @@ int mainThrows(int argc, char * argv[]) { for ( int i=1; i < argc; ++i ) { if ( check_argument_equals( argv[i], {"-d"} ) ) daemonMode = true; + else if ( check_argument_equals( argv[i], {"-h"} ) ) + { + if ( (++i) < argc ) { + host = argv[i]; + } else { + throw std::runtime_error( "main: Error no bind ip argument given" ); + } + } else if ( check_argument_equals( argv[i], {"-p"} ) ) { if ( (++i) < argc ) { @@ -4054,19 +4064,20 @@ int mainThrows(int argc, char * argv[]) { deleteAndNullify( tp ); exit( 0 ); } + const auto hostString = host.length() > 0 ? host : "localhost"; #if defined (USE_SSL) if ( useSSL ) { if ( port == 0 ) port = DEFAULT_HTTPS_PORT; - std::cerr << "Starting SSL enabled server on https://localhost:" << port << "/\n"; - startHTTPSServer( port, certificateFile, privateKeyFile ); + std::cerr << "Starting SSL enabled server on https://" << hostString << ":" << port << "/\n"; + startHTTPSServer( host, port, certificateFile, privateKeyFile ); } else #endif { if ( port == 0 ) port = DEFAULT_HTTP_PORT; - std::cerr << "Starting plain HTTP server on http://localhost:" << port << "/\n"; - startHTTPServer( port ); + std::cerr << "Starting plain HTTP server on http://" << hostString << ":" << port << "/\n"; + startHTTPServer( host, port ); } delete pcmInstance; } else if ( pid > 0 ) { From df18c348e2df013186417152f7996588ec183b88 Mon Sep 17 00:00:00 2001 From: Markus Schmidl Date: Mon, 21 Apr 2025 20:08:30 +0200 Subject: [PATCH 2/4] pcm-sensor-server: use -l for bind ip --- src/pcm-sensor-server.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pcm-sensor-server.cpp b/src/pcm-sensor-server.cpp index 0a7f8d28..9d98e56b 100644 --- a/src/pcm-sensor-server.cpp +++ b/src/pcm-sensor-server.cpp @@ -3724,7 +3724,7 @@ void printHelpText( std::string const & programName ) { #if defined (USE_SSL) std::cout << " -s : Use https protocol (default port " << DEFAULT_HTTPS_PORT << ")\n"; #endif - std::cout << " -h bind ip : Bind to ip address. (default ip is 0.0.0.0)\n"; + std::cout << " -l bind ip : Bind to ip address. (default ip is 0.0.0.0)\n"; std::cout << " -p portnumber : Run on port (default port is " << DEFAULT_HTTP_PORT << ")\n"; std::cout << " -r|--reset : Reset programming of the performance counters.\n"; std::cout << " -D|--debug level : level = 0: no debug info, > 0 increase verbosity.\n"; @@ -3790,7 +3790,7 @@ int mainThrows(int argc, char * argv[]) { for ( int i=1; i < argc; ++i ) { if ( check_argument_equals( argv[i], {"-d"} ) ) daemonMode = true; - else if ( check_argument_equals( argv[i], {"-h"} ) ) + else if ( check_argument_equals( argv[i], {"-l"} ) ) { if ( (++i) < argc ) { host = argv[i]; From 6d2d67dcc11610ffafe4b2a375f6d5558e29ae22 Mon Sep 17 00:00:00 2001 From: Markus Schmidl Date: Mon, 21 Apr 2025 21:18:36 +0200 Subject: [PATCH 3/4] pcm-sensor-server: update docs for ip6 address bind --- src/pcm-sensor-server.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pcm-sensor-server.cpp b/src/pcm-sensor-server.cpp index 9d98e56b..bd6139a5 100644 --- a/src/pcm-sensor-server.cpp +++ b/src/pcm-sensor-server.cpp @@ -3724,7 +3724,7 @@ void printHelpText( std::string const & programName ) { #if defined (USE_SSL) std::cout << " -s : Use https protocol (default port " << DEFAULT_HTTPS_PORT << ")\n"; #endif - std::cout << " -l bind ip : Bind to ip address. (default ip is 0.0.0.0)\n"; + std::cout << " -l bind ip6 : Bind to ip6 address. (default ip6 is ::)\n"; std::cout << " -p portnumber : Run on port (default port is " << DEFAULT_HTTP_PORT << ")\n"; std::cout << " -r|--reset : Reset programming of the performance counters.\n"; std::cout << " -D|--debug level : level = 0: no debug info, > 0 increase verbosity.\n"; From 949cbbe06de47b3f6dec7d7958c823201250564a Mon Sep 17 00:00:00 2001 From: Markus Schmidl Date: Thu, 24 Apr 2025 14:46:43 +0200 Subject: [PATCH 4/4] fix ipv6 formatting and remame variables to highlight that this is not a hostname, but an ipv6 address --- src/pcm-sensor-server.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/pcm-sensor-server.cpp b/src/pcm-sensor-server.cpp index cff62e09..14d7c1e6 100644 --- a/src/pcm-sensor-server.cpp +++ b/src/pcm-sensor-server.cpp @@ -3765,7 +3765,7 @@ int mainThrows(int argc, char * argv[]) { #endif bool forceRTMAbortMode = false; bool printTopology = false; - std::string host = ""; + std::string ip6Address = ""; unsigned short port = 0; unsigned short debug_level = 0; std::string certificateFile; @@ -3793,7 +3793,7 @@ int mainThrows(int argc, char * argv[]) { else if ( check_argument_equals( argv[i], {"-l"} ) ) { if ( (++i) < argc ) { - host = argv[i]; + ip6Address = argv[i]; } else { throw std::runtime_error( "main: Error no bind ip argument given" ); } @@ -4082,20 +4082,20 @@ int mainThrows(int argc, char * argv[]) { } // Now that everything is set we can start the http(s) server - const auto hostString = host.length() > 0 ? host : "localhost"; + const auto formattedIp6Address = ip6Address.length() > 0 ? "[" + ip6Address + "]" : "localhost"; #if defined (USE_SSL) if ( useSSL ) { if ( port == 0 ) port = DEFAULT_HTTPS_PORT; - std::cerr << "Starting SSL enabled server on https://" << hostString << ":" << port << "/\n"; - startHTTPSServer( host, port, certificateFile, privateKeyFile ); + std::cerr << "Starting SSL enabled server on https://" << formattedIp6Address << ":" << port << "/\n"; + startHTTPSServer( ip6Address, port, certificateFile, privateKeyFile ); } else #endif { if ( port == 0 ) port = DEFAULT_HTTP_PORT; - std::cerr << "Starting plain HTTP server on http://" << hostString << ":" << port << "/\n"; - startHTTPServer( host, port ); + std::cerr << "Starting plain HTTP server on http://" << formattedIp6Address << ":" << port << "/\n"; + startHTTPServer( ip6Address, port ); } delete pcmInstance; } else if ( pid > 0 ) {