Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added conversion of hostname to IP #21

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions influxdb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>

#ifdef _WIN32
#define NOMINMAX
Expand All @@ -26,6 +27,7 @@
#include <sys/socket.h>
#include <sys/uio.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#define closesocket close
#endif
Expand All @@ -37,8 +39,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. Try to ping the host.\n", host.c_str());
std::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;
Expand Down Expand Up @@ -200,6 +220,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;
Expand Down
7 changes: 6 additions & 1 deletion test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +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_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;
Expand Down