pqissl::Initiate_Connection remove duplicated code

This commit is contained in:
Gioacchino Mazzurco 2018-02-26 16:30:27 +01:00
parent f4a6090150
commit 7d765ec04e
No known key found for this signature in database
GPG key ID: A1FBCA3872E87051

View file

@ -577,33 +577,28 @@ int pqissl::Delay_Connection()
} }
rslog(RSL_WARNING, pqisslzone, rslog(RSL_WARNING, pqisslzone,
"pqissl::Initiate_Connection() Already Attempt in Progress!"); "pqissl::Delay_Connection() Already Attempt in Progress!");
return -1; return -1;
} }
int pqissl::Initiate_Connection() int pqissl::Initiate_Connection()
{ {
int err; #ifdef PQISSL_DEBUG
struct sockaddr_storage addr = remote_addr; std::cout << __PRETTY_FUNCTION__ << std::endl;
#ifdef PQISSL_LOG_DEBUG
rslog(RSL_DEBUG_BASIC, pqisslzone,
"pqissl::Initiate_Connection() Attempting Outgoing Connection....");
#endif #endif
if (waiting != WAITING_DELAY) int err;
sockaddr_storage addr = remote_addr;
if(waiting != WAITING_DELAY)
{ {
rslog(RSL_WARNING, pqisslzone, std::cerr << __PRETTY_FUNCTION__ << " Already Attempt in Progress!"
"pqissl::Initiate_Connection() Already Attempt in Progress!"); << std::endl;
return -1; return -1;
} }
#ifdef PQISSL_LOG_DEBUG
rslog(RSL_DEBUG_BASIC, pqisslzone,
"pqissl::Initiate_Connection() Opening Socket");
#endif
// open socket connection to addr. // open socket connection to addr.
int osock = unix_socket(PF_INET6, SOCK_STREAM, 0); int osock = unix_socket(PF_INET6, SOCK_STREAM, 0);
@ -631,7 +626,7 @@ int pqissl::Initiate_Connection()
"pqissl::Initiate_Connection() Making Non-Blocking"); "pqissl::Initiate_Connection() Making Non-Blocking");
#endif #endif
err = unix_fcntl_nonblock(osock); err = unix_fcntl_nonblock(osock);
if (err < 0) if (err < 0)
{ {
std::string out; std::string out;
@ -728,61 +723,24 @@ int pqissl::Initiate_Connection()
sockaddr_storage_ipv4_to_ipv6(addr); sockaddr_storage_ipv4_to_ipv6(addr);
if (0 != (err = unix_connect(osock, addr))) if (0 != (err = unix_connect(osock, addr)))
{ {
std::string out; switch (errno)
rs_sprintf(out, "pqissl::Initiate_Connection() connect returns:%d -> errno: %d error: %s\n", err, errno, socket_errorType(errno).c_str());
if (errno == EINPROGRESS)
{ {
// set state to waiting..... case EINPROGRESS:
waiting = WAITING_SOCK_CONNECT; waiting = WAITING_SOCK_CONNECT;
sockfd = osock; sockfd = osock;
#ifdef PQISSL_LOG_DEBUG
out += " EINPROGRESS Waiting for Socket Connection";
rslog(RSL_DEBUG_BASIC, pqisslzone, out);
#endif
return 0; return 0;
} default:
else if ((errno == ENETUNREACH) || (errno == ETIMEDOUT)) std::cerr << __PRETTY_FUNCTION__ << " Failure connect "
{ << sockaddr_storage_tostring(addr)
out += "ENETUNREACHABLE: cert: " + PeerId().toStdString(); << " returns: "
rslog(RSL_WARNING, pqisslzone, out); << err << " -> errno: " << errno << " "
<< socket_errorType(errno) << std::endl;
// Then send unreachable message.
net_internal_close(osock); net_internal_close(osock);
osock=-1; osock = -1;
//reset();
waiting = WAITING_FAIL_INTERFACE; waiting = WAITING_FAIL_INTERFACE;
return -1; return -1;
} }
/* IF we get here ---- we Failed for some other reason.
* Should abandon this interface
* Known reasons to get here: EINVAL (bad address)
*/
rs_sprintf_append(out, "Error: Connection Failed: %d - %s", errno, socket_errorType(errno).c_str());
net_internal_close(osock);
osock=-1;
waiting = WAITING_FAIL_INTERFACE;
rslog(RSL_WARNING, pqisslzone, out);
// extra output for the moment.
std::cerr << out;
return -1;
}
else
{
#ifdef PQISSL_LOG_DEBUG
rslog(RSL_DEBUG_BASIC, pqisslzone,
"pqissl::Init_Connection() connect returned 0");
#endif
} }
waiting = WAITING_SOCK_CONNECT; waiting = WAITING_SOCK_CONNECT;