From 81be46eaeb69bc1e97039cc02836f3ead09459c7 Mon Sep 17 00:00:00 2001 From: Ben Bruers Date: Thu, 7 Nov 2019 10:41:51 +0100 Subject: [PATCH 1/4] Added conversion of hostname to IP --- influxdb.hpp | 24 ++++++++++++++++++++++-- test/main.cpp | 5 +++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/influxdb.hpp b/influxdb.hpp index 15fa3d7..4d33462 100644 --- a/influxdb.hpp +++ b/influxdb.hpp @@ -26,6 +26,7 @@ #include #include #include + #include #include #define closesocket close #endif @@ -37,8 +38,26 @@ namespace influxdb_cpp { std::string db_; std::string usr_; std::string pwd_; - server_info(const std::string& host, int port, const std::string& db = "", const std::string& usr = "", const std::string& pwd = "") - : host_(host), port_(port), db_(db), usr_(usr), pwd_(pwd) {} + server_info(const std::string& host, int port, const std::string& db = "", const std::string& usr = "", const std::string& pwd = "") { + port_ = port; + db_ = db; + usr_ = usr; + pwd_ = pwd; + + //convert hostname to ip-address + hostent * record = gethostbyname(host.c_str()); + if(record == NULL) + { + printf("Cannot resolve IP address from hostname: %s is unavailable\n", host.c_str()); + exit(-1); + } + in_addr * address = (in_addr * )record->h_addr; + std::string ip_address = inet_ntoa(* address); + + printf("Resolved IP address from hostname: %s.\n", ip_address.c_str()); + + host_ = ip_address; + } }; namespace detail { struct meas_caller; @@ -200,6 +219,7 @@ namespace influxdb_cpp { addr.sin_family = AF_INET; addr.sin_port = htons(si.port_); + if((addr.sin_addr.s_addr = inet_addr(si.host_.c_str())) == INADDR_NONE) return -1; if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) return -2; diff --git a/test/main.cpp b/test/main.cpp index 8dd9a00..2c06754 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -39,6 +39,11 @@ int main(int argc, char const *argv[]) influxdb_cpp::query(resp, "show databases", si); cout << resp << endl; + // query from table, but use hostname instead of IP + influxdb_cpp::server_info si_new("localhost", 8086, "", "test", "test"); + influxdb_cpp::query(resp, "show databases", si); + cout << resp << endl; + // create_db influxdb_cpp::create_db(resp, "x", si_new); cout << resp << endl; From 06882f723fd83e5869c8b74b79869f470aa3362e Mon Sep 17 00:00:00 2001 From: Ben Bruers Date: Thu, 7 Nov 2019 10:53:05 +0100 Subject: [PATCH 2/4] Fixing build error --- influxdb.hpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/influxdb.hpp b/influxdb.hpp index 4d33462..01dffa3 100644 --- a/influxdb.hpp +++ b/influxdb.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #ifdef _WIN32 #define NOMINMAX @@ -45,14 +46,14 @@ namespace influxdb_cpp { pwd_ = pwd; //convert hostname to ip-address - hostent * record = gethostbyname(host.c_str()); - if(record == NULL) - { - printf("Cannot resolve IP address from hostname: %s is unavailable\n", host.c_str()); - exit(-1); - } - in_addr * address = (in_addr * )record->h_addr; - std::string ip_address = inet_ntoa(* address); + hostent * record = gethostbyname(host.c_str()); + if(record == NULL) + { + printf("Cannot resolve IP address from hostname: %s is unavailable. Try to ping the host.\n", host.c_str()); + exit(-1); + } + in_addr * address = (in_addr * )record->h_addr; + std::string ip_address = inet_ntoa(* address); printf("Resolved IP address from hostname: %s.\n", ip_address.c_str()); From 449086d9a6e4fd203e3fed9a2dfaad30d8b27834 Mon Sep 17 00:00:00 2001 From: Ben Bruers Date: Thu, 7 Nov 2019 10:53:38 +0100 Subject: [PATCH 3/4] Fixing build error 2 --- influxdb.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/influxdb.hpp b/influxdb.hpp index 01dffa3..089cd23 100644 --- a/influxdb.hpp +++ b/influxdb.hpp @@ -50,7 +50,7 @@ namespace influxdb_cpp { if(record == NULL) { printf("Cannot resolve IP address from hostname: %s is unavailable. Try to ping the host.\n", host.c_str()); - exit(-1); + std::exit(-1); } in_addr * address = (in_addr * )record->h_addr; std::string ip_address = inet_ntoa(* address); From 3f9f3ad5931c9130787a021dda6fbac2c4615870 Mon Sep 17 00:00:00 2001 From: Ben Bruers Date: Thu, 7 Nov 2019 10:58:20 +0100 Subject: [PATCH 4/4] CI reordered commands, hostname fix --- test/main.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/main.cpp b/test/main.cpp index 2c06754..4607838 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -35,16 +35,16 @@ int main(int argc, char const *argv[]) cout << ret << endl; // query from table - influxdb_cpp::server_info si_new("127.0.0.1", 8086, "", "test", "test"); influxdb_cpp::query(resp, "show databases", si); cout << resp << endl; // query from table, but use hostname instead of IP - influxdb_cpp::server_info si_new("localhost", 8086, "", "test", "test"); - influxdb_cpp::query(resp, "show databases", si); + influxdb_cpp::server_info si_hostname("localhost", 8086, "testx", "test", "test"); + influxdb_cpp::query(resp, "show databases", si_hostname); cout << resp << endl; // create_db + influxdb_cpp::server_info si_new("127.0.0.1", 8086, "", "test", "test"); influxdb_cpp::create_db(resp, "x", si_new); cout << resp << endl; return 0;