From c6a0095ad307a43fc83497bbb3efbc60052f6ada Mon Sep 17 00:00:00 2001 From: George Hazan Date: Tue, 17 Jan 2017 15:56:59 +0300 Subject: - constructor & destructor for NetlibBoundPort - warning fix - randomizer must work even if the system service doesn't exist --- src/mir_app/src/netlib.h | 5 ++++- src/mir_app/src/netlibbind.cpp | 47 ++++++++++++++++++++++++------------------ src/mir_core/src/utils.cpp | 13 +++++++++--- 3 files changed, 41 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/mir_app/src/netlib.h b/src/mir_app/src/netlib.h index 087b7d8cb5..140353d19d 100644 --- a/src/mir_app/src/netlib.h +++ b/src/mir_app/src/netlib.h @@ -168,8 +168,11 @@ struct NetlibConnection : public MZeroedObject unsigned lastPost; }; -struct NetlibBoundPort +struct NetlibBoundPort : public MZeroedObject { + NetlibBoundPort(HNETLIBUSER nlu, NETLIBBIND *nlb); + ~NetlibBoundPort(); + int handleType; SOCKET s; SOCKET s6; diff --git a/src/mir_app/src/netlibbind.cpp b/src/mir_app/src/netlibbind.cpp index fff3f092d2..bbc9c6fee3 100644 --- a/src/mir_app/src/netlibbind.cpp +++ b/src/mir_app/src/netlibbind.cpp @@ -109,14 +109,12 @@ bool BindSocketToPort(const char *szPorts, SOCKET s, SOCKET s6, int* portn) } } -int NetlibFreeBoundPort(struct NetlibBoundPort *nlbp) +int NetlibFreeBoundPort(NetlibBoundPort *nlbp) { - closesocket(nlbp->s); - closesocket(nlbp->s6); if (nlbp->hThread) WaitForSingleObject(nlbp->hThread, INFINITE); Netlib_Logf(nlbp->nlu, "(%u) Port %u closed for incoming connections", nlbp->s, nlbp->wPort); - mir_free(nlbp); + delete nlbp; return 1; } @@ -176,24 +174,16 @@ static void NetlibBindAcceptThread(void* param) MIR_APP_DLL(HNETLIBBIND) Netlib_BindPort(HNETLIBUSER nlu, NETLIBBIND *nlb) { - if (GetNetlibHandleType(nlu) != NLH_USER || !(nlu->user.flags & NUF_INCOMING) || - nlb == NULL || nlb->pfnNewConnection == NULL) { + if (GetNetlibHandleType(nlu) != NLH_USER || !(nlu->user.flags & NUF_INCOMING) || nlb == NULL || nlb->pfnNewConnection == NULL) { SetLastError(ERROR_INVALID_PARAMETER); - return 0; + return nullptr; } NetlibBoundPort *nlbp = (NetlibBoundPort*)mir_calloc(sizeof(NetlibBoundPort)); - 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->pExtra; if (nlbp->s == INVALID_SOCKET && nlbp->s6 == INVALID_SOCKET) { Netlib_Logf(nlu, "%s %d: %s() failed (%u)", __FILE__, __LINE__, "socket", WSAGetLastError()); - mir_free(nlbp); - return 0; + delete nlbp; + return nullptr; } SOCKADDR_IN sin = { 0 }; @@ -237,10 +227,8 @@ MIR_APP_DLL(HNETLIBBIND) Netlib_BindPort(HNETLIBUSER nlu, NETLIBBIND *nlb) if (!foundPort) { Netlib_Logf(nlu, "%s %d: %s() failed (%u)", __FILE__, __LINE__, "bind", WSAGetLastError()); LBL_Error: - closesocket(nlbp->s); - closesocket(nlbp->s6); - mir_free(nlbp); - return 0; + delete nlbp; + return nullptr; } if (nlbp->s != INVALID_SOCKET && listen(nlbp->s, 5)) { @@ -296,3 +284,22 @@ LBL_Error: nlbp->hThread = mir_forkthread(NetlibBindAcceptThread, nlbp); return nlbp; } + +///////////////////////////////////////////////////////////////////////////////////////// + +NetlibBoundPort::NetlibBoundPort(HNETLIBUSER _nlu, NETLIBBIND *nlb) + : handleType(NLH_BOUNDPORT), + nlu(_nlu) +{ + pfnNewConnectionV2 = nlb->pfnNewConnectionV2; + pExtra = nlb->pExtra; + + s = socket(PF_INET, SOCK_STREAM, 0); + s6 = socket(PF_INET6, SOCK_STREAM, 0); +} + +NetlibBoundPort::~NetlibBoundPort() +{ + closesocket(s); + closesocket(s6); +} diff --git a/src/mir_core/src/utils.cpp b/src/mir_core/src/utils.cpp index e2dea519ab..7b26bb6aab 100644 --- a/src/mir_core/src/utils.cpp +++ b/src/mir_core/src/utils.cpp @@ -524,8 +524,15 @@ PGENRANDOM pfnRtlGenRandom; MIR_CORE_DLL(void) Utils_GetRandom(void *pszDest, size_t cbLen) { - if (pszDest != 0 || cbLen != 0 && pfnRtlGenRandom != NULL) + if (pszDest == nullptr || cbLen == 0) + return; + + if (pfnRtlGenRandom != NULL) pfnRtlGenRandom(pszDest, (ULONG)cbLen); - else - memset(pszDest, 0, cbLen); + else { + srand(time(NULL)); + BYTE *p = (BYTE*)pszDest; + for (size_t i = 0; i < cbLen; i++) + p[i] = rand() & 0xFF; + } } -- cgit v1.2.3