On connection in-progress, save the whole addrinfo struct, not just ai_addr
This commit is contained in:
@@ -82,22 +82,9 @@ void SocketHandler::connect(const std::string& address, const std::string& port)
|
|||||||
sg.add_callback([&addr_res](){ freeaddrinfo(addr_res); });
|
sg.add_callback([&addr_res](){ freeaddrinfo(addr_res); });
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{ // This function is called again, use the saved addrinfo structure,
|
||||||
// This function is called again, use the saved addrinfo structure,
|
// instead of re-doing the whole getaddrinfo process.
|
||||||
// instead of re-doing the whole getaddrinfo process. We insert only
|
addr_res = &this->addrinfo;
|
||||||
// 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (struct addrinfo* rp = addr_res; rp; rp = rp->ai_next)
|
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
|
// Save the addrinfo structure, to use it on the next call
|
||||||
this->ai_addrlen = rp->ai_addrlen;
|
this->ai_addrlen = rp->ai_addrlen;
|
||||||
memcpy(&this->ai_addr, rp->ai_addr, this->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 ;
|
return ;
|
||||||
}
|
}
|
||||||
log_info("Connection failed:" << strerror(errno));
|
log_info("Connection failed:" << strerror(errno));
|
||||||
|
|||||||
@@ -131,6 +131,7 @@ protected:
|
|||||||
* connect()ing to it, to reuse it directly when connect() is called
|
* connect()ing to it, to reuse it directly when connect() is called
|
||||||
* again.
|
* again.
|
||||||
*/
|
*/
|
||||||
|
struct addrinfo addrinfo;
|
||||||
struct sockaddr ai_addr;
|
struct sockaddr ai_addr;
|
||||||
socklen_t ai_addrlen;
|
socklen_t ai_addrlen;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user