diff options
author | George Hazan <george.hazan@gmail.com> | 2012-06-30 13:33:28 +0000 |
---|---|---|
committer | George Hazan <george.hazan@gmail.com> | 2012-06-30 13:33:28 +0000 |
commit | b8a02a2b98e7b0e6a8a54cb140a873039d9c6d3d (patch) | |
tree | 8dd46393de0047d87492a37e4ab057f143153aa8 /src/modules/netlib | |
parent | b0632a264c7ce0120af88e915a455c3167f1d5c5 (diff) |
- setting thread names;
- warning fixes;
- code cleaning;
- option to disable sounds during idle
git-svn-id: http://svn.miranda-ng.org/main/trunk@692 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'src/modules/netlib')
-rw-r--r-- | src/modules/netlib/netlibopenconn.cpp | 438 |
1 files changed, 182 insertions, 256 deletions
diff --git a/src/modules/netlib/netlibopenconn.cpp b/src/modules/netlib/netlibopenconn.cpp index 3bc19e684f..b96c27aa4d 100644 --- a/src/modules/netlib/netlibopenconn.cpp +++ b/src/modules/netlib/netlibopenconn.cpp @@ -81,8 +81,7 @@ int WaitUntilWritable(SOCKET s, DWORD dwTimeout) FD_ZERO(&writefd);
FD_SET(s, &writefd);
- switch(select(0, 0, &writefd, 0, &tv))
- {
+ switch(select(0, 0, &writefd, 0, &tv)) {
case 0:
SetLastError(ERROR_TIMEOUT);
case SOCKET_ERROR:
@@ -96,8 +95,7 @@ bool RecvUntilTimeout(struct NetlibConnection *nlc, char *buf, int len, int flag int nReceived = 0;
DWORD dwTimeNow, dwCompleteTime = GetTickCount() + dwTimeout;
- while ((dwTimeNow = GetTickCount()) < dwCompleteTime)
- {
+ while ((dwTimeNow = GetTickCount()) < dwCompleteTime) {
if (WaitUntilReadable(nlc->s, dwCompleteTime - dwTimeNow) <= 0) return false;
nReceived = NLRecv(nlc, buf, len, flags);
if (nReceived <= 0) return false;
@@ -130,39 +128,36 @@ static int NetlibInitSocks4Connection(NetlibConnection *nlc, NetlibUser *nlu, NE //if cannot resolve host, try resolving through proxy (requires SOCKS4a)
DWORD ip = DnsLookup(nlu, nloc->szHost);
*(PDWORD)&pInit[4] = ip ? ip : 0x01000000;
- if ( !ip)
- {
+ if ( !ip) {
memcpy(&pInit[len], nloc->szHost, nHostLen);
len += nHostLen;
}
- if (NLSend(nlc, pInit, (int)len, MSG_DUMPPROXY) == SOCKET_ERROR)
- {
+ if (NLSend(nlc, pInit, (int)len, MSG_DUMPPROXY) == SOCKET_ERROR) {
NetlibLogf(nlu, "%s %d: %s() failed (%u)", __FILE__, __LINE__, "NLSend", GetLastError());
return 0;
}
char reply[8];
- if ( !RecvUntilTimeout(nlc, reply, sizeof(reply), MSG_DUMPPROXY, RECV_DEFAULT_TIMEOUT))
- {
+ if ( !RecvUntilTimeout(nlc, reply, sizeof(reply), MSG_DUMPPROXY, RECV_DEFAULT_TIMEOUT)) {
NetlibLogf(nlu, "%s %d: %s() failed (%u)", __FILE__, __LINE__, "RecvUntilTimeout", GetLastError());
return 0;
}
- switch ((BYTE)reply[1])
- {
- case 90: return 1;
- case 91: SetLastError(ERROR_ACCESS_DENIED); break;
- case 92: SetLastError(ERROR_CONNECTION_UNAVAIL); break;
- case 93: SetLastError(ERROR_INVALID_ACCESS); break;
- default: SetLastError(ERROR_INVALID_DATA); break;
+ switch ((BYTE)reply[1]) {
+ case 90: return 1;
+ case 91: SetLastError(ERROR_ACCESS_DENIED); break;
+ case 92: SetLastError(ERROR_CONNECTION_UNAVAIL); break;
+ case 93: SetLastError(ERROR_INVALID_ACCESS); break;
+ default: SetLastError(ERROR_INVALID_DATA); break;
}
NetlibLogf(nlu, "%s %d: Proxy connection failed (%x %u)", __FILE__, __LINE__, (BYTE)reply[1], GetLastError());
return 0;
}
static int NetlibInitSocks5Connection(struct NetlibConnection *nlc, struct NetlibUser *nlu, NETLIBOPENCONNECTION *nloc)
-{ //rfc1928
+{
+ //rfc1928
BYTE buf[258];
buf[0]=5; //yep, socks5
@@ -214,46 +209,42 @@ static int NetlibInitSocks5Connection(struct NetlibConnection *nlc, struct Netli }
}
- {
- PBYTE pInit;
- int nHostLen;
- DWORD hostIP;
+ PBYTE pInit;
+ int nHostLen;
+ DWORD hostIP;
- if (nlc->dnsThroughProxy)
- {
- hostIP = inet_addr(nloc->szHost);
- if (hostIP == INADDR_NONE)
- nHostLen = lstrlenA(nloc->szHost)+1;
- else nHostLen = 4;
- }
- else
- {
- hostIP = DnsLookup(nlu, nloc->szHost);
- if (hostIP == 0)
- return 0;
- nHostLen = 4;
- }
- pInit=(PBYTE)mir_alloc(6+nHostLen);
- pInit[0]=5; //SOCKS5
- pInit[1]= nloc->flags & NLOCF_UDP ? 3 : 1; //connect or UDP
- pInit[2]=0; //reserved
- if (hostIP == INADDR_NONE) { //DNS lookup through proxy
- pInit[3]=3;
- pInit[4]=nHostLen-1;
- memcpy(pInit+5, nloc->szHost, nHostLen-1);
- }
- else {
- pInit[3]=1;
- *(PDWORD)(pInit+4)=hostIP;
- }
- *(PWORD)(pInit+4+nHostLen)=htons(nloc->wPort);
- if (NLSend(nlc, (char*)pInit, 6+nHostLen, MSG_DUMPPROXY) == SOCKET_ERROR) {
- NetlibLogf(nlu, "%s %d: %s() failed (%u)", __FILE__, __LINE__, "NLSend", GetLastError());
- mir_free(pInit);
+ if (nlc->dnsThroughProxy) {
+ hostIP = inet_addr(nloc->szHost);
+ if (hostIP == INADDR_NONE)
+ nHostLen = lstrlenA(nloc->szHost)+1;
+ else nHostLen = 4;
+ }
+ else {
+ hostIP = DnsLookup(nlu, nloc->szHost);
+ if (hostIP == 0)
return 0;
- }
+ nHostLen = 4;
+ }
+ pInit=(PBYTE)mir_alloc(6+nHostLen);
+ pInit[0]=5; //SOCKS5
+ pInit[1]= nloc->flags & NLOCF_UDP ? 3 : 1; //connect or UDP
+ pInit[2]=0; //reserved
+ if (hostIP == INADDR_NONE) { //DNS lookup through proxy
+ pInit[3]=3;
+ pInit[4]=nHostLen-1;
+ memcpy(pInit+5, nloc->szHost, nHostLen-1);
+ }
+ else {
+ pInit[3]=1;
+ *(PDWORD)(pInit+4)=hostIP;
+ }
+ *(PWORD)(pInit+4+nHostLen)=htons(nloc->wPort);
+ if (NLSend(nlc, (char*)pInit, 6+nHostLen, MSG_DUMPPROXY) == SOCKET_ERROR) {
+ NetlibLogf(nlu, "%s %d: %s() failed (%u)", __FILE__, __LINE__, "NLSend", GetLastError());
mir_free(pInit);
+ return 0;
}
+ mir_free(pInit);
if ( !RecvUntilTimeout(nlc, (char*)buf, 5, MSG_DUMPPROXY, RECV_DEFAULT_TIMEOUT)) {
NetlibLogf(nlu, "%s %d: %s() failed (%u)", __FILE__, __LINE__, "RecvUntilTimeout", GetLastError());
@@ -264,52 +255,50 @@ static int NetlibInitSocks5Connection(struct NetlibConnection *nlc, struct Netli const char* err = "Unknown response";
if (buf[0] != 5)
SetLastError(ERROR_BAD_FORMAT);
- else
- {
- switch(buf[1])
- {
- case 1: SetLastError(ERROR_GEN_FAILURE); err = "General failure"; break;
- case 2: SetLastError(ERROR_ACCESS_DENIED); err = "Connection not allowed by ruleset"; break;
- case 3: SetLastError(WSAENETUNREACH); err = "Network unreachable"; break;
- case 4: SetLastError(WSAEHOSTUNREACH); err = "Host unreachable"; break;
- case 5: SetLastError(WSAECONNREFUSED); err = "Connection refused by destination host"; break;
- case 6: SetLastError(WSAETIMEDOUT); err = "TTL expired"; break;
- case 7: SetLastError(ERROR_CALL_NOT_IMPLEMENTED); err = "Command not supported / protocol error"; break;
- case 8: SetLastError(ERROR_INVALID_ADDRESS); err = "Address type not supported"; break;
- default: SetLastError(ERROR_INVALID_DATA); break;
+ else {
+ switch(buf[1]) {
+ case 1: SetLastError(ERROR_GEN_FAILURE); err = "General failure"; break;
+ case 2: SetLastError(ERROR_ACCESS_DENIED); err = "Connection not allowed by ruleset"; break;
+ case 3: SetLastError(WSAENETUNREACH); err = "Network unreachable"; break;
+ case 4: SetLastError(WSAEHOSTUNREACH); err = "Host unreachable"; break;
+ case 5: SetLastError(WSAECONNREFUSED); err = "Connection refused by destination host"; break;
+ case 6: SetLastError(WSAETIMEDOUT); err = "TTL expired"; break;
+ case 7: SetLastError(ERROR_CALL_NOT_IMPLEMENTED); err = "Command not supported / protocol error"; break;
+ case 8: SetLastError(ERROR_INVALID_ADDRESS); err = "Address type not supported"; break;
+ default: SetLastError(ERROR_INVALID_DATA); break;
}
}
NetlibLogf(nlu, "%s %d: Proxy conection failed. %s.", __FILE__, __LINE__, err);
return 0;
}
- {
- int nRecvSize = 0;
- switch(buf[3]) {
- case 1:// ipv4 addr
- nRecvSize = 5;
- break;
- case 3:// dns name addr
- nRecvSize = buf[4] + 2;
- break;
- case 4:// ipv6 addr
- nRecvSize = 17;
- break;
- default:
- NetlibLogf(nlu, "%s %d: %s() unknown address type (%u)", __FILE__, __LINE__, "NetlibInitSocks5Connection", (int)buf[3]);
- return 0;
- }
- if ( !RecvUntilTimeout(nlc, (char*)buf, nRecvSize, MSG_DUMPPROXY, RECV_DEFAULT_TIMEOUT)) {
- NetlibLogf(nlu, "%s %d: %s() failed (%u)", __FILE__, __LINE__, "RecvUntilTimeout", GetLastError());
- return 0;
- }
+
+ int nRecvSize = 0;
+ switch(buf[3]) {
+ case 1:// ipv4 addr
+ nRecvSize = 5;
+ break;
+ case 3:// dns name addr
+ nRecvSize = buf[4] + 2;
+ break;
+ case 4:// ipv6 addr
+ nRecvSize = 17;
+ break;
+ default:
+ NetlibLogf(nlu, "%s %d: %s() unknown address type (%u)", __FILE__, __LINE__, "NetlibInitSocks5Connection", (int)buf[3]);
+ return 0;
}
+ if ( !RecvUntilTimeout(nlc, (char*)buf, nRecvSize, MSG_DUMPPROXY, RECV_DEFAULT_TIMEOUT)) {
+ NetlibLogf(nlu, "%s %d: %s() failed (%u)", __FILE__, __LINE__, "RecvUntilTimeout", GetLastError());
+ return 0;
+ }
//connected
return 1;
}
static bool NetlibInitHttpsConnection(struct NetlibConnection *nlc, struct NetlibUser *nlu, NETLIBOPENCONNECTION *nloc)
-{ //rfc2817
+{
+ //rfc2817
NETLIBHTTPREQUEST nlhrSend = {0}, *nlhrReply;
char szUrl[512];
@@ -317,11 +306,8 @@ static bool NetlibInitHttpsConnection(struct NetlibConnection *nlc, struct Netli nlhrSend.requestType = REQUEST_CONNECT;
nlhrSend.flags = NLHRF_GENERATEHOST | NLHRF_DUMPPROXY | NLHRF_SMARTAUTHHEADER | NLHRF_HTTP11 | NLHRF_NOPROXY | NLHRF_REDIRECT;
if (nlc->dnsThroughProxy)
- {
mir_snprintf(szUrl, SIZEOF(szUrl), "%s:%u", nloc->szHost, nloc->wPort);
- }
- else
- {
+ else {
DWORD ip = DnsLookup(nlu, nloc->szHost);
if (ip == 0) return false;
mir_snprintf(szUrl, SIZEOF(szUrl), "%s:%u", inet_ntoa(*(PIN_ADDR)&ip), nloc->wPort);
@@ -330,18 +316,18 @@ static bool NetlibInitHttpsConnection(struct NetlibConnection *nlc, struct Netli nlc->usingHttpGateway = true;
- if (NetlibHttpSendRequest((WPARAM)nlc, (LPARAM)&nlhrSend) == SOCKET_ERROR)
- {
+ if (NetlibHttpSendRequest((WPARAM)nlc, (LPARAM)&nlhrSend) == SOCKET_ERROR) {
nlc->usingHttpGateway = false;
return 0;
}
+
nlhrReply = NetlibHttpRecv(nlc, MSG_DUMPPROXY | MSG_RAW, MSG_DUMPPROXY | MSG_RAW, true);
nlc->usingHttpGateway = false;
- if (nlhrReply == NULL) return false;
- if (nlhrReply->resultCode < 200 || nlhrReply->resultCode >= 300)
- {
- if (nlhrReply->resultCode == 403 && nlc->dnsThroughProxy)
- {
+ if (nlhrReply == NULL)
+ return false;
+
+ if (nlhrReply->resultCode < 200 || nlhrReply->resultCode >= 300) {
+ if (nlhrReply->resultCode == 403 && nlc->dnsThroughProxy) {
NetlibHttpFreeRequestStruct(0, (LPARAM)nlhrReply);
nlc->dnsThroughProxy = 0;
return NetlibInitHttpsConnection(nlc, nlu, nloc);
@@ -386,8 +372,7 @@ static bool my_connectIPv4(NetlibConnection *nlc, NETLIBOPENCONNECTION * nloc) if (dwTimeout == 0) dwTimeout = 30;
// this is for XP SP2 where there is a default connection attempt limit of 10/second
- if (connectionTimeout)
- {
+ if (connectionTimeout) {
WaitForSingleObject(hConnectionOpenMutex, 10000);
int waitdiff = GetTickCount() - g_LastConnectionTick;
if (waitdiff < connectionTimeout) SleepEx(connectionTimeout, TRUE);
@@ -402,8 +387,7 @@ static bool my_connectIPv4(NetlibConnection *nlc, NETLIBOPENCONNECTION * nloc) SOCKADDR_IN sin = {0};
sin.sin_family = AF_INET;
- if (nlc->proxyType)
- {
+ if (nlc->proxyType) {
if ( !nlc->szProxyServer) return false;
if (nloc)
@@ -414,8 +398,7 @@ static bool my_connectIPv4(NetlibConnection *nlc, NETLIBOPENCONNECTION * nloc) sin.sin_port = htons(nlc->wProxyPort);
he = gethostbyname(nlc->szProxyServer);
}
- else
- {
+ else {
if ( !nloc || !nloc->szHost || nloc->szHost[0] == '[' || strchr(nloc->szHost, ':')) return false;
NetlibLogf(nlc->nlu, "(%p) Connecting to server %s:%d....", nlc, nloc->szHost, nloc->wPort);
@@ -423,8 +406,7 @@ static bool my_connectIPv4(NetlibConnection *nlc, NETLIBOPENCONNECTION * nloc) he = gethostbyname(nloc->szHost);
}
- for (char** har = he->h_addr_list; *har && !Miranda_Terminated(); ++har)
- {
+ for (char** har = he->h_addr_list; *har && !Miranda_Terminated(); ++har) {
sin.sin_addr.s_addr = *(u_long*)*har;
char* szIp = NetlibAddressToString((SOCKADDR_INET_M*)&sin);
@@ -433,36 +415,33 @@ static bool my_connectIPv4(NetlibConnection *nlc, NETLIBOPENCONNECTION * nloc) retry:
nlc->s = socket(AF_INET, nloc->flags & NLOCF_UDP ? SOCK_DGRAM : SOCK_STREAM, 0);
- if (nlc->s == INVALID_SOCKET) return false;
+ if (nlc->s == INVALID_SOCKET)
+ return false;
// return the socket to non blocking
- if (ioctlsocket(nlc->s, FIONBIO, ¬blocking) != 0) return false;
+ if (ioctlsocket(nlc->s, FIONBIO, ¬blocking) != 0)
+ return false;
- if (nlc->nlu->settings.specifyOutgoingPorts && nlc->nlu->settings.szOutgoingPorts && nlc->nlu->settings.szOutgoingPorts[0])
- {
+ if (nlc->nlu->settings.specifyOutgoingPorts && nlc->nlu->settings.szOutgoingPorts && nlc->nlu->settings.szOutgoingPorts[0]) {
if ( !BindSocketToPort(nlc->nlu->settings.szOutgoingPorts, nlc->s, INVALID_SOCKET, &nlc->nlu->inportnum))
NetlibLogf(nlc->nlu, "Netlib connect: Not enough ports for outgoing connections specified");
}
// try a connect
- if (connect(nlc->s, (LPSOCKADDR)&sin, sizeof(sin)) == 0)
- {
+ if (connect(nlc->s, (LPSOCKADDR)&sin, sizeof(sin)) == 0) {
rc = 0;
break;
}
// didn't work, was it cos of nonblocking?
- if (WSAGetLastError() != WSAEWOULDBLOCK)
- {
+ if (WSAGetLastError() != WSAEWOULDBLOCK) {
rc = SOCKET_ERROR;
-
closesocket(nlc->s);
nlc->s = INVALID_SOCKET;
continue;
}
- for (;;)
- {
+ while (true) {
fd_set r, w, e;
FD_ZERO(&r); FD_ZERO(&w); FD_ZERO(&e);
FD_SET(nlc->s, &r);
@@ -471,48 +450,39 @@ retry: if ((rc = select(0, &r, &w, &e, &tv)) == SOCKET_ERROR)
break;
- if (rc > 0)
- {
- if (FD_ISSET(nlc->s, &w))
- {
+ if (rc > 0) {
+ if (FD_ISSET(nlc->s, &w)){
// connection was successful
rc = 0;
}
- if (FD_ISSET(nlc->s, &r))
- {
+ if (FD_ISSET(nlc->s, &r)) {
// connection was closed
rc = SOCKET_ERROR;
lasterr = WSAECONNRESET;
}
- if (FD_ISSET(nlc->s, &e))
- {
+ if (FD_ISSET(nlc->s, &e)) {
// connection failed.
int len = sizeof(lasterr);
rc = SOCKET_ERROR;
getsockopt(nlc->s, SOL_SOCKET, SO_ERROR, (char*)&lasterr, &len);
- if (lasterr == WSAEADDRINUSE && ++retrycnt <= 2)
- {
+ if (lasterr == WSAEADDRINUSE && ++retrycnt <= 2) {
closesocket(nlc->s);
goto retry;
}
}
break;
}
- else if (Miranda_Terminated())
- {
+ else if (Miranda_Terminated()) {
rc = SOCKET_ERROR;
lasterr = ERROR_TIMEOUT;
break;
}
- else if (nloc->cbSize == sizeof(NETLIBOPENCONNECTION) && nloc->flags & NLOCF_V2 &&
- nloc->waitcallback != NULL && nloc->waitcallback(&dwTimeout) == 0)
- {
+ else if (nloc->cbSize == sizeof(NETLIBOPENCONNECTION) && nloc->flags & NLOCF_V2 && nloc->waitcallback != NULL && nloc->waitcallback(&dwTimeout) == 0) {
rc = SOCKET_ERROR;
lasterr = ERROR_TIMEOUT;
break;
}
- if (--dwTimeout == 0)
- {
+ if (--dwTimeout == 0) {
rc = SOCKET_ERROR;
lasterr = ERROR_TIMEOUT;
break;
@@ -543,8 +513,7 @@ static bool my_connectIPv6(NetlibConnection *nlc, NETLIBOPENCONNECTION * nloc) if (dwTimeout == 0) dwTimeout = 30;
// this is for XP SP2 where there is a default connection attempt limit of 10/second
- if (connectionTimeout)
- {
+ if (connectionTimeout) {
WaitForSingleObject(hConnectionOpenMutex, 10000);
int waitdiff = GetTickCount() - g_LastConnectionTick;
if (waitdiff < connectionTimeout) SleepEx(connectionTimeout, TRUE);
@@ -560,20 +529,18 @@ static bool my_connectIPv6(NetlibConnection *nlc, NETLIBOPENCONNECTION * nloc) hints.ai_family = AF_UNSPEC;
- if (nloc->flags & NLOCF_UDP)
- {
+ if (nloc->flags & NLOCF_UDP) {
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
}
- else
- {
+ else {
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
}
- if (nlc->proxyType)
- {
- if ( !nlc->szProxyServer) return false;
+ if (nlc->proxyType) {
+ if ( !nlc->szProxyServer)
+ return false;
if (nloc)
NetlibLogf(nlc->nlu, "(%p) Connecting to proxy %s:%d for %s:%d ....", nlc, nlc->szProxyServer, nlc->wProxyPort, nloc->szHost, nloc->wPort);
@@ -581,40 +548,39 @@ static bool my_connectIPv6(NetlibConnection *nlc, NETLIBOPENCONNECTION * nloc) NetlibLogf(nlc->nlu, "(%p) Connecting to proxy %s:%d ....", nlc, nlc->szProxyServer, nlc->wProxyPort);
_itoa(nlc->wProxyPort, szPort, 10);
- if (MyGetaddrinfo(nlc->szProxyServer, szPort, &hints, &air))
- {
+ if (MyGetaddrinfo(nlc->szProxyServer, szPort, &hints, &air)) {
NetlibLogf(nlc->nlu, "%s %d: %s() for host %s failed (%u)", __FILE__, __LINE__, "getaddrinfo", nlc->szProxyServer, WSAGetLastError());
return false;
}
}
- else
- {
- if ( !nloc || !nloc->szHost) return false;
+ else {
+ if ( !nloc || !nloc->szHost)
+ return false;
+
NetlibLogf(nlc->nlu, "(%p) Connecting to server %s:%d....", nlc, nloc->szHost, nloc->wPort);
_itoa(nlc->nloc.wPort, szPort, 10);
- if (MyGetaddrinfo(nlc->nloc.szHost, szPort, &hints, &air))
- {
+ if (MyGetaddrinfo(nlc->nloc.szHost, szPort, &hints, &air)) {
NetlibLogf(nlc->nlu, "%s %d: %s() for host %s failed (%u)", __FILE__, __LINE__, "getaddrinfo", nlc->nloc.szHost, WSAGetLastError());
return false;
}
}
- for (ai = air; ai && !Miranda_Terminated(); ai = ai->ai_next)
- {
+ for (ai = air; ai && !Miranda_Terminated(); ai = ai->ai_next) {
char* szIp = NetlibAddressToString((SOCKADDR_INET_M*)ai->ai_addr);
NetlibLogf(nlc->nlu, "(%p) Connecting to ip %s ....", nlc, szIp);
mir_free(szIp);
retry:
nlc->s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
- if (nlc->s == INVALID_SOCKET) return false;
+ if (nlc->s == INVALID_SOCKET)
+ return false;
// return the socket to non blocking
- if (ioctlsocket(nlc->s, FIONBIO, ¬blocking) != 0) return false;
+ if (ioctlsocket(nlc->s, FIONBIO, ¬blocking) != 0)
+ return false;
- if (nlc->nlu->settings.specifyOutgoingPorts && nlc->nlu->settings.szOutgoingPorts && nlc->nlu->settings.szOutgoingPorts[0])
- {
+ if (nlc->nlu->settings.specifyOutgoingPorts && nlc->nlu->settings.szOutgoingPorts && nlc->nlu->settings.szOutgoingPorts[0]) {
SOCKET s = ai->ai_family == AF_INET ? nlc->s : INVALID_SOCKET;
SOCKET s6 = ai->ai_family == AF_INET6 ? nlc->s : INVALID_SOCKET;
if ( !BindSocketToPort(nlc->nlu->settings.szOutgoingPorts, s, s6, &nlc->nlu->inportnum))
@@ -622,24 +588,20 @@ retry: }
// try a connect
- if (connect(nlc->s, ai->ai_addr, (int)ai->ai_addrlen) == 0)
- {
+ if (connect(nlc->s, ai->ai_addr, (int)ai->ai_addrlen) == 0) {
rc = 0;
break;
}
// didn't work, was it cos of nonblocking?
- if (WSAGetLastError() != WSAEWOULDBLOCK)
- {
+ if (WSAGetLastError() != WSAEWOULDBLOCK) {
rc = SOCKET_ERROR;
-
closesocket(nlc->s);
nlc->s = INVALID_SOCKET;
continue;
}
- for (;;) // timeout loop
- {
+ while (true) { // timeout loop
fd_set r, w, e;
FD_ZERO(&r); FD_ZERO(&w); FD_ZERO(&e);
FD_SET(nlc->s, &r);
@@ -648,22 +610,18 @@ retry: if ((rc = select(0, &r, &w, &e, &tv)) == SOCKET_ERROR)
break;
- if (rc > 0)
- {
- if (FD_ISSET(nlc->s, &w))
- {
+ if (rc > 0) {
+ if (FD_ISSET(nlc->s, &w)){
// connection was successful
rc = 0;
lasterr = 0;
}
- if (FD_ISSET(nlc->s, &r))
- {
+ if (FD_ISSET(nlc->s, &r)) {
// connection was closed
rc = SOCKET_ERROR;
lasterr = WSAECONNRESET;
}
- if (FD_ISSET(nlc->s, &e))
- {
+ if (FD_ISSET(nlc->s, &e)) {
// connection failed.
int len = sizeof(lasterr);
rc = SOCKET_ERROR;
@@ -677,21 +635,17 @@ retry: }
break;
}
- else if (Miranda_Terminated())
- {
+ else if (Miranda_Terminated()) {
rc = SOCKET_ERROR;
lasterr = ERROR_TIMEOUT;
break;
}
- else if (nloc->cbSize == sizeof(NETLIBOPENCONNECTION) && nloc->flags & NLOCF_V2 &&
- nloc->waitcallback != NULL && nloc->waitcallback(&dwTimeout) == 0)
- {
+ else if (nloc->cbSize == sizeof(NETLIBOPENCONNECTION) && nloc->flags & NLOCF_V2 && nloc->waitcallback != NULL && nloc->waitcallback(&dwTimeout) == 0) {
rc = SOCKET_ERROR;
lasterr = ERROR_TIMEOUT;
break;
}
- if (--dwTimeout == 0)
- {
+ if (--dwTimeout == 0) {
rc = SOCKET_ERROR;
lasterr = ERROR_TIMEOUT;
break;
@@ -726,8 +680,7 @@ static int NetlibHttpFallbackToDirect(struct NetlibConnection *nlc, struct Netli nlc->proxyAuthNeeded = false;
nlc->proxyType = 0;
mir_free(nlc->szProxyServer); nlc->szProxyServer = NULL;
- if ( !my_connect(nlc, nloc))
- {
+ if ( !my_connect(nlc, nloc)) {
NetlibLogf(nlu, "%s %d: %s() failed (%u)", __FILE__, __LINE__, "connect", WSAGetLastError());
return false;
}
@@ -742,16 +695,11 @@ bool NetlibDoConnect(NetlibConnection *nlc) mir_free(nlc->szProxyServer); nlc->szProxyServer = NULL;
bool usingProxy = false, forceHttps = false;
- if (nlu->settings.useProxy)
- {
+ if (nlu->settings.useProxy) {
if (nlu->settings.proxyType == PROXYTYPE_IE)
- {
usingProxy = NetlibGetIeProxyConn(nlc, false);
- }
- else
- {
- if (nlu->settings.szProxyServer && nlu->settings.szProxyServer[0])
- {
+ else {
+ if (nlu->settings.szProxyServer && nlu->settings.szProxyServer[0]) {
nlc->szProxyServer = mir_strdup(nlu->settings.szProxyServer);
nlc->wProxyPort = nlu->settings.wProxyPort;
nlc->proxyType = nlu->settings.proxyType;
@@ -761,34 +709,26 @@ bool NetlibDoConnect(NetlibConnection *nlc) }
retry:
- if (usingProxy)
- {
- if ( !my_connect(nlc, nloc))
- {
+ if (usingProxy) {
+ if ( !my_connect(nlc, nloc)) {
usingProxy = false;
nlc->proxyType = 0;
}
}
+
if ( !usingProxy)
- {
my_connect(nlc, nloc);
- }
- if (nlc->s == INVALID_SOCKET)
- {
- if (usingProxy && (nlc->proxyType == PROXYTYPE_HTTPS || nlc->proxyType == PROXYTYPE_HTTP))
- {
+ if (nlc->s == INVALID_SOCKET) {
+ if (usingProxy && (nlc->proxyType == PROXYTYPE_HTTPS || nlc->proxyType == PROXYTYPE_HTTP)) {
usingProxy = false;
- if ( !NetlibHttpFallbackToDirect(nlc, nlu, nloc))
- {
+ if ( !NetlibHttpFallbackToDirect(nlc, nlu, nloc)) {
NetlibLogf(nlu, "%s %d: %s() failed (%u)", __FILE__, __LINE__, "connect", WSAGetLastError());
return false;
}
}
- else
- {
- if (nlu->settings.useProxy && !usingProxy && nlu->settings.proxyType == PROXYTYPE_IE && !forceHttps)
- {
+ else {
+ if (nlu->settings.useProxy && !usingProxy && nlu->settings.proxyType == PROXYTYPE_IE && !forceHttps) {
forceHttps = true;
usingProxy = NetlibGetIeProxyConn(nlc, true);
if (usingProxy) goto retry;
@@ -798,25 +738,24 @@ retry: }
}
- if (usingProxy && !((nloc->flags & (NLOCF_HTTP | NLOCF_SSL)) == NLOCF_HTTP &&
- (nlc->proxyType == PROXYTYPE_HTTP || nlc->proxyType == PROXYTYPE_HTTPS)))
- {
- if ( !WaitUntilWritable(nlc->s, 30000)) return false;
+ if (usingProxy && !((nloc->flags & (NLOCF_HTTP | NLOCF_SSL)) == NLOCF_HTTP && (nlc->proxyType == PROXYTYPE_HTTP || nlc->proxyType == PROXYTYPE_HTTPS))) {
+ if ( !WaitUntilWritable(nlc->s, 30000))
+ return false;
- switch (nlc->proxyType)
- {
+ switch (nlc->proxyType) {
case PROXYTYPE_SOCKS4:
- if ( !NetlibInitSocks4Connection(nlc, nlu, nloc)) return false;
+ if ( !NetlibInitSocks4Connection(nlc, nlu, nloc))
+ return false;
break;
case PROXYTYPE_SOCKS5:
- if ( !NetlibInitSocks5Connection(nlc, nlu, nloc)) return false;
+ if ( !NetlibInitSocks5Connection(nlc, nlu, nloc))
+ return false;
break;
case PROXYTYPE_HTTPS:
nlc->proxyAuthNeeded = true;
- if ( !NetlibInitHttpsConnection(nlc, nlu, nloc))
- {
+ if ( !NetlibInitHttpsConnection(nlc, nlu, nloc)) {
usingProxy = false;
if ( !NetlibHttpFallbackToDirect(nlc, nlu, nloc))
return false;
@@ -825,21 +764,18 @@ retry: case PROXYTYPE_HTTP:
nlc->proxyAuthNeeded = true;
- if ( !(nlu->user.flags & NUF_HTTPGATEWAY || nloc->flags & NLOCF_HTTPGATEWAY) || nloc->flags & NLOCF_SSL)
- {
+ if ( !(nlu->user.flags & NUF_HTTPGATEWAY || nloc->flags & NLOCF_HTTPGATEWAY) || nloc->flags & NLOCF_SSL) {
//NLOCF_HTTP not specified and no HTTP gateway available: try HTTPS
- if ( !NetlibInitHttpsConnection(nlc, nlu, nloc))
- {
+ if ( !NetlibInitHttpsConnection(nlc, nlu, nloc)) {
//can't do HTTPS: try direct
usingProxy = false;
if ( !NetlibHttpFallbackToDirect(nlc, nlu, nloc))
return false;
}
}
- else
- {
- if ( !NetlibInitHttpConnection(nlc, nlu, nloc)) return false;
- }
+ else if ( !NetlibInitHttpConnection(nlc, nlu, nloc))
+ return false;
+
break;
default:
@@ -848,8 +784,7 @@ retry: return false;
}
}
- else if (nloc->flags & NLOCF_HTTPGATEWAY)
- {
+ else if (nloc->flags & NLOCF_HTTPGATEWAY) {
if ( !NetlibInitHttpConnection(nlc, nlu, nloc)) return false;
nlc->usingDirectHttpGateway = true;
}
@@ -857,9 +792,7 @@ retry: NetlibLogf(nlu, "(%d) Connected to %s:%d", nlc->s, nloc->szHost, nloc->wPort);
if (NLOCF_SSL & nloc->flags)
- {
return NetlibStartSsl((WPARAM)nlc, 0) != 0;
- }
return true;
}
@@ -868,11 +801,8 @@ bool NetlibReconnect(NetlibConnection *nlc) {
char buf[4];
bool opened = nlc->s != INVALID_SOCKET;
-
- if (opened)
- {
- switch (WaitUntilReadable(nlc->s, 0, true))
- {
+ if (opened) {
+ switch (WaitUntilReadable(nlc->s, 0, true)) {
case SOCKET_ERROR:
opened = false;
break;
@@ -890,16 +820,15 @@ bool NetlibReconnect(NetlibConnection *nlc) NetlibDoClose(nlc, true);
}
- if ( !opened)
- {
- if (Miranda_Terminated()) return false;
- if (nlc->usingHttpGateway)
- {
+ if ( !opened) {
+ if (Miranda_Terminated())
+ return false;
+
+ if (nlc->usingHttpGateway) {
nlc->proxyAuthNeeded = true;
return my_connect(nlc, &nlc->nloc);
}
- else
- return NetlibDoConnect(nlc);
+ return NetlibDoConnect(nlc);
}
return true;
}
@@ -907,19 +836,18 @@ bool NetlibReconnect(NetlibConnection *nlc) INT_PTR NetlibOpenConnection(WPARAM wParam, LPARAM lParam)
{
NETLIBOPENCONNECTION *nloc = (NETLIBOPENCONNECTION*)lParam;
- struct NetlibUser *nlu = (struct NetlibUser*)wParam;
- struct NetlibConnection *nlc;
-
- NetlibLogf(nlu, "Connection request to %s:%d (Flags %x)....", nloc->szHost, nloc->wPort, nloc->flags);
-
- if (GetNetlibHandleType(nlu) != NLH_USER || !(nlu->user.flags & NUF_OUTGOING) || nloc == NULL ||
- (nloc->cbSize != NETLIBOPENCONNECTION_V1_SIZE && nloc->cbSize != sizeof(NETLIBOPENCONNECTION)) ||
- nloc->szHost == NULL || nloc->wPort == 0)
- {
+ if (nloc == NULL || nloc->cbSize != sizeof(NETLIBOPENCONNECTION) || nloc->szHost == NULL || nloc->wPort == 0) {
SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}
- nlc = (struct NetlibConnection*)mir_calloc(sizeof(struct NetlibConnection));
+
+ NetlibUser *nlu = (NetlibUser*)wParam;
+ if (GetNetlibHandleType(nlu) != NLH_USER || !(nlu->user.flags & NUF_OUTGOING))
+ return 0;
+
+ NetlibLogf(nlu, "Connection request to %s:%d (Flags %x)....", nloc->szHost, nloc->wPort, nloc->flags);
+
+ NetlibConnection *nlc = (NetlibConnection*)mir_calloc(sizeof(struct NetlibConnection));
nlc->handleType = NLH_CONNECTION;
nlc->nlu = nlu;
nlc->nloc = *nloc;
@@ -934,17 +862,14 @@ INT_PTR NetlibOpenConnection(WPARAM wParam, LPARAM lParam) NetlibInitializeNestedCS(&nlc->ncsSend);
NetlibInitializeNestedCS(&nlc->ncsRecv);
- if ( !NetlibDoConnect(nlc))
- {
+ if ( !NetlibDoConnect(nlc)) {
FreePartiallyInitedConnection(nlc);
return 0;
}
- if (iUPnPCleanup == 0)
- {
+ if (iUPnPCleanup == 0) {
EnterCriticalSection(&csNetlibUser);
- if (iUPnPCleanup == 0)
- {
+ if (iUPnPCleanup == 0) {
iUPnPCleanup = 1;
forkthread(NetlibUPnPCleanup, 0, NULL);
}
@@ -957,7 +882,8 @@ INT_PTR NetlibOpenConnection(WPARAM wParam, LPARAM lParam) INT_PTR NetlibStartSsl(WPARAM wParam, LPARAM lParam)
{
NetlibConnection *nlc = (NetlibConnection*)wParam;
- if (nlc == NULL) return 0;
+ if (nlc == NULL)
+ return 0;
NETLIBSSL *sp = (NETLIBSSL*)lParam;
const char *szHost = sp ? sp->host : nlc->nloc.szHost;
|