summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorent Le Coz <louiz@louiz.org>2014-05-28 02:03:36 +0200
committerFlorent Le Coz <louiz@louiz.org>2014-05-28 02:03:36 +0200
commite033b6a3ae2923b040eb08cfa5376efe0a3da898 (patch)
treebf37f989fbf4c34581d6d7ededb1350c226fce60
parent6c2d03da4ea0443624b6bf434b6a654c12e48438 (diff)
downloadbiboumi-e033b6a3ae2923b040eb08cfa5376efe0a3da898.tar.gz
biboumi-e033b6a3ae2923b040eb08cfa5376efe0a3da898.tar.bz2
biboumi-e033b6a3ae2923b040eb08cfa5376efe0a3da898.tar.xz
biboumi-e033b6a3ae2923b040eb08cfa5376efe0a3da898.zip
On connection in-progress, save the whole addrinfo struct, not just ai_addr
-rw-r--r--src/network/socket_handler.cpp22
-rw-r--r--src/network/socket_handler.hpp1
2 files changed, 7 insertions, 16 deletions
diff --git a/src/network/socket_handler.cpp b/src/network/socket_handler.cpp
index 2baad3d..d509513 100644
--- a/src/network/socket_handler.cpp
+++ b/src/network/socket_handler.cpp
@@ -82,22 +82,9 @@ void SocketHandler::connect(const std::string& address, const std::string& port)
sg.add_callback([&addr_res](){ freeaddrinfo(addr_res); });
}
else
- {
- // This function is called again, use the saved addrinfo structure,
- // instead of re-doing the whole getaddrinfo process. We insert only
- // the meaningful values in the structure, and indicate that these are
- // the only possible values with ai_next = NULL.
- addr_res = (struct addrinfo*)malloc(sizeof(struct addrinfo));
- if (!addr_res)
- {
- this->close();
- this->on_connection_failed("memory error");
- return ;
- }
- sg.add_callback([&addr_res](){ free(addr_res); });
- addr_res->ai_next = NULL;
- addr_res->ai_addr = &this->ai_addr;
- addr_res->ai_addrlen = this->ai_addrlen;
+ { // This function is called again, use the saved addrinfo structure,
+ // instead of re-doing the whole getaddrinfo process.
+ addr_res = &this->addrinfo;
}
for (struct addrinfo* rp = addr_res; rp; rp = rp->ai_next)
@@ -131,6 +118,9 @@ void SocketHandler::connect(const std::string& address, const std::string& port)
// Save the addrinfo structure, to use it on the next call
this->ai_addrlen = rp->ai_addrlen;
memcpy(&this->ai_addr, rp->ai_addr, this->ai_addrlen);
+ memcpy(&this->addrinfo, rp, sizeof(struct addrinfo));
+ this->addrinfo.ai_addr = &this->ai_addr;
+ this->addrinfo.ai_next = nullptr;
return ;
}
log_info("Connection failed:" << strerror(errno));
diff --git a/src/network/socket_handler.hpp b/src/network/socket_handler.hpp
index e6a36bf..0b59757 100644
--- a/src/network/socket_handler.hpp
+++ b/src/network/socket_handler.hpp
@@ -131,6 +131,7 @@ protected:
* connect()ing to it, to reuse it directly when connect() is called
* again.
*/
+ struct addrinfo addrinfo;
struct sockaddr ai_addr;
socklen_t ai_addrlen;