summaryrefslogtreecommitdiff
path: root/src/modules/netlib/netlibbind.cpp
diff options
context:
space:
mode:
authorGeorge Hazan <george.hazan@gmail.com>2012-07-02 22:30:27 +0000
committerGeorge Hazan <george.hazan@gmail.com>2012-07-02 22:30:27 +0000
commit980bcaff693a86971750d6e9ffc8ba1e561b8b3a (patch)
treed8a422e9ccd2357d6e28368447bee8cfcd26123a /src/modules/netlib/netlibbind.cpp
parent990f961261d92fccadb16b495171bb619c0183f0 (diff)
the core without manual critical sections' control
git-svn-id: http://svn.miranda-ng.org/main/trunk@730 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'src/modules/netlib/netlibbind.cpp')
-rw-r--r--src/modules/netlib/netlibbind.cpp168
1 files changed, 79 insertions, 89 deletions
diff --git a/src/modules/netlib/netlibbind.cpp b/src/modules/netlib/netlibbind.cpp
index 0b6aca709d..582358b619 100644
--- a/src/modules/netlib/netlibbind.cpp
+++ b/src/modules/netlib/netlibbind.cpp
@@ -25,97 +25,86 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
bool BindSocketToPort(const char *szPorts, SOCKET s, SOCKET s6, int* portn)
{
- SOCKADDR_IN sin = {0};
- sin.sin_family = AF_INET;
-
- SOCKADDR_IN6 sin6 = {0};
- sin6.sin6_family = AF_INET6;
-
- EnterCriticalSection(&csNetlibUser);
-
- if (--*portn < 0 && (s != INVALID_SOCKET || s6 != INVALID_SOCKET))
- {
- BindSocketToPort(szPorts, INVALID_SOCKET, INVALID_SOCKET, portn);
- if (*portn == 0)
- {
- LeaveCriticalSection(&csNetlibUser);
- return false;
- }
- WORD num;
- CallService(MS_UTILS_GETRANDOM, sizeof(WORD), (LPARAM)&num);
- *portn = num % *portn;
- }
-
- bool before=false;
- for (;;)
- {
- const char *psz;
- char *pszEnd;
- int portMin, portMax, port, portnum = 0;
-
- for (psz=szPorts;*psz;)
- {
- while (*psz == ' ' || *psz == ',') psz++;
- portMin = strtol(psz, &pszEnd, 0);
- if (pszEnd == psz) break;
- while (*pszEnd == ' ') pszEnd++;
- if (*pszEnd == '-')
- {
- psz = pszEnd + 1;
- portMax = strtol(psz, &pszEnd, 0);
- if (pszEnd == psz) portMax = 65535;
- if (portMin > portMax)
- {
- port = portMin;
- portMin = portMax;
- portMax = port;
- }
- }
- else portMax = portMin;
- if (portMax >= 1)
- {
- if (portMin <= 0) portMin = 1;
- for (port = portMin; port <= portMax; port++)
- {
- if (port > 65535) break;
-
- ++portnum;
-
- if (s == INVALID_SOCKET) continue;
- if ( !before && portnum <= *portn) continue;
- if (before && portnum >= *portn)
- {
- LeaveCriticalSection(&csNetlibUser);
- return false;
- }
-
- sin.sin_port = htons((WORD)port);
+ SOCKADDR_IN sin = {0};
+ sin.sin_family = AF_INET;
+
+ SOCKADDR_IN6 sin6 = {0};
+ sin6.sin6_family = AF_INET6;
+
+ mir_cslock lck(csNetlibUser);
+
+ if (--*portn < 0 && (s != INVALID_SOCKET || s6 != INVALID_SOCKET)) {
+ BindSocketToPort(szPorts, INVALID_SOCKET, INVALID_SOCKET, portn);
+ if (*portn == 0)
+ return false;
+
+ WORD num;
+ CallService(MS_UTILS_GETRANDOM, sizeof(WORD), (LPARAM)&num);
+ *portn = num % *portn;
+ }
+
+ bool before=false;
+ while (true) {
+ const char *psz;
+ char *pszEnd;
+ int portMin, portMax, port, portnum = 0;
+
+ for (psz=szPorts;*psz;) {
+ while (*psz == ' ' || *psz == ',') psz++;
+ portMin = strtol(psz, &pszEnd, 0);
+ if (pszEnd == psz)
+ break;
+ while (*pszEnd == ' ')
+ pszEnd++;
+ if (*pszEnd == '-') {
+ psz = pszEnd + 1;
+ portMax = strtol(psz, &pszEnd, 0);
+ if (pszEnd == psz) portMax = 65535;
+ if (portMin > portMax) {
+ port = portMin;
+ portMin = portMax;
+ portMax = port;
+ }
+ }
+ else portMax = portMin;
+ if (portMax >= 1) {
+ if (portMin <= 0) portMin = 1;
+ for (port = portMin; port <= portMax; port++) {
+ if (port > 65535)
+ break;
+
+ ++portnum;
+
+ if (s == INVALID_SOCKET) continue;
+ if ( !before && portnum <= *portn) continue;
+ if (before && portnum >= *portn)
+ return false;
+
+ sin.sin_port = htons((WORD)port);
bool bV4Mapped = s == INVALID_SOCKET || bind(s, (SOCKADDR*)&sin, sizeof(sin)) == 0;
sin6.sin6_port = htons((WORD)port);
bool bV6Mapped = s6 == INVALID_SOCKET || bind(s6, (PSOCKADDR)&sin6, sizeof(sin6)) == 0;
- if (bV4Mapped && bV6Mapped)
- {
- LeaveCriticalSection(&csNetlibUser);
- *portn = portnum + 1;
- return true;
- }
- }
- }
- psz = pszEnd;
- }
- if (*portn < 0)
- {
- *portn = portnum;
- LeaveCriticalSection(&csNetlibUser);
- return true;
- }
- else if (*portn >= portnum)
- *portn = 0;
- else
- before = true;
- }
+ if (bV4Mapped && bV6Mapped) {
+ *portn = portnum + 1;
+ return true;
+ }
+ }
+ }
+ psz = pszEnd;
+ }
+
+ if (*portn < 0) {
+ *portn = portnum;
+ return true;
+ }
+
+ if (*portn >= portnum)
+ *portn = 0;
+ else
+ before = true;
+ }
}
int NetlibFreeBoundPort(struct NetlibBoundPort *nlbp)
@@ -174,8 +163,9 @@ static unsigned __stdcall NetlibBindAcceptThread(void* param)
nlc->hOkToCloseEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
NetlibInitializeNestedCS(&nlc->ncsSend);
NetlibInitializeNestedCS(&nlc->ncsRecv);
-
- if (nlbp->pfnNewConnectionV2) nlbp->pfnNewConnectionV2(nlc, ntohl(sin.Ipv4.sin_addr.S_un.S_addr), nlbp->pExtra);
+
+ if (nlbp->pfnNewConnectionV2)
+ nlbp->pfnNewConnectionV2(nlc, ntohl(sin.Ipv4.sin_addr.S_un.S_addr), nlbp->pExtra);
}
NetlibUPnPDeletePortMapping(nlbp->wExPort, "TCP");
return 0;
@@ -204,7 +194,7 @@ INT_PTR NetlibBindPort(WPARAM wParam, LPARAM lParam)
nlbp->handleType = NLH_BOUNDPORT;
nlbp->nlu = nlu;
nlbp->pfnNewConnectionV2 = nlb->pfnNewConnectionV2;
-
+
nlbp->s = socket(PF_INET, SOCK_STREAM, 0);
nlbp->s6 = socket(PF_INET6, SOCK_STREAM, 0);
nlbp->pExtra = (nlb->cbSize != NETLIBBIND_SIZEOF_V1) ? nlb->pExtra : NULL;