Skip to content

Commit be10f70

Browse files
committed
pcm-sensor-server: add bind ip argument
1 parent 9899e74 commit be10f70

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/pcm-sensor-server.cpp

+19-8
Original file line numberDiff line numberDiff line change
@@ -3684,8 +3684,8 @@ void my_get_callback( HTTPServer* hs, HTTPRequest const & req, HTTPResponse & re
36843684
}
36853685
}
36863686

3687-
int startHTTPServer( unsigned short port ) {
3688-
HTTPServer server( "", port );
3687+
int startHTTPServer( std::string const & ip, unsigned short port ) {
3688+
HTTPServer server( ip, port );
36893689
try {
36903690
// HEAD is GET without body, we will remove the body in execute()
36913691
server.registerCallback( HTTPRequestMethod::GET, my_get_callback );
@@ -3699,8 +3699,8 @@ int startHTTPServer( unsigned short port ) {
36993699
}
37003700

37013701
#if defined (USE_SSL)
3702-
int startHTTPSServer( unsigned short port, std::string const & cFile, std::string const & pkFile) {
3703-
HTTPSServer server( "", port );
3702+
int startHTTPSServer( std::string const & ip, unsigned short port, std::string const & cFile, std::string const & pkFile) {
3703+
HTTPSServer server( ip, port );
37043704
try {
37053705
server.setPrivateKeyFile ( pkFile );
37063706
server.setCertificateFile( cFile );
@@ -3724,6 +3724,7 @@ void printHelpText( std::string const & programName ) {
37243724
#if defined (USE_SSL)
37253725
std::cout << " -s : Use https protocol (default port " << DEFAULT_HTTPS_PORT << ")\n";
37263726
#endif
3727+
std::cout << " -h bind ip : Bind to ip address. (default ip is 0.0.0.0)\n";
37273728
std::cout << " -p portnumber : Run on port <portnumber> (default port is " << DEFAULT_HTTP_PORT << ")\n";
37283729
std::cout << " -r|--reset : Reset programming of the performance counters.\n";
37293730
std::cout << " -D|--debug level : level = 0: no debug info, > 0 increase verbosity.\n";
@@ -3762,6 +3763,7 @@ int mainThrows(int argc, char * argv[]) {
37623763
#endif
37633764
bool forceRTMAbortMode = false;
37643765
bool printTopology = false;
3766+
std::string host = "";
37653767
unsigned short port = 0;
37663768
unsigned short debug_level = 0;
37673769
std::string certificateFile;
@@ -3788,6 +3790,14 @@ int mainThrows(int argc, char * argv[]) {
37883790
for ( int i=1; i < argc; ++i ) {
37893791
if ( check_argument_equals( argv[i], {"-d"} ) )
37903792
daemonMode = true;
3793+
else if ( check_argument_equals( argv[i], {"-h"} ) )
3794+
{
3795+
if ( (++i) < argc ) {
3796+
host = argv[i];
3797+
} else {
3798+
throw std::runtime_error( "main: Error no bind ip argument given" );
3799+
}
3800+
}
37913801
else if ( check_argument_equals( argv[i], {"-p"} ) )
37923802
{
37933803
if ( (++i) < argc ) {
@@ -4054,19 +4064,20 @@ int mainThrows(int argc, char * argv[]) {
40544064
deleteAndNullify( tp );
40554065
exit( 0 );
40564066
}
4067+
const auto hostString = host.length() > 0 ? host : "localhost";
40574068
#if defined (USE_SSL)
40584069
if ( useSSL ) {
40594070
if ( port == 0 )
40604071
port = DEFAULT_HTTPS_PORT;
4061-
std::cerr << "Starting SSL enabled server on https://localhost:" << port << "/\n";
4062-
startHTTPSServer( port, certificateFile, privateKeyFile );
4072+
std::cerr << "Starting SSL enabled server on https://" << hostString << ":" << port << "/\n";
4073+
startHTTPSServer( host, port, certificateFile, privateKeyFile );
40634074
} else
40644075
#endif
40654076
{
40664077
if ( port == 0 )
40674078
port = DEFAULT_HTTP_PORT;
4068-
std::cerr << "Starting plain HTTP server on http://localhost:" << port << "/\n";
4069-
startHTTPServer( port );
4079+
std::cerr << "Starting plain HTTP server on http://" << hostString << ":" << port << "/\n";
4080+
startHTTPServer( host, port );
40704081
}
40714082
delete pcmInstance;
40724083
} else if ( pid > 0 ) {

0 commit comments

Comments
 (0)