//MailSlot of flush DNS cache Monitor bool __fastcall FlushDNSMailSlotMonitor( void) { //System security setting std::shared_ptr<SECURITY_ATTRIBUTES> SecurityAttributes(new SECURITY_ATTRIBUTES()); std::shared_ptr<SECURITY_DESCRIPTOR> SecurityDescriptor(new SECURITY_DESCRIPTOR()); std::shared_ptr<char> ACL_Buffer(new char[PACKET_MAXSIZE]()); memset(ACL_Buffer.get(), 0, PACKET_MAXSIZE); PSID SID_Value = nullptr; InitializeSecurityDescriptor(SecurityDescriptor.get(), SECURITY_DESCRIPTOR_REVISION); InitializeAcl((PACL)ACL_Buffer.get(), PACKET_MAXSIZE, ACL_REVISION); ConvertStringSidToSidW(SID_ADMINISTRATORS_GROUP, &SID_Value); AddAccessAllowedAce((PACL)ACL_Buffer.get(), ACL_REVISION, GENERIC_ALL, SID_Value); SetSecurityDescriptorDacl(SecurityDescriptor.get(), true, (PACL)ACL_Buffer.get(), false); SecurityAttributes->lpSecurityDescriptor = SecurityDescriptor.get(); SecurityAttributes->bInheritHandle = true; //Create mailslot. HANDLE hSlot = CreateMailslotW(MAILSLOT_NAME, PACKET_MAXSIZE - 1U, MAILSLOT_WAIT_FOREVER, SecurityAttributes.get()); if (hSlot == INVALID_HANDLE_VALUE) { LocalFree(SID_Value); PrintError(LOG_ERROR_SYSTEM, L"Create mailslot error", GetLastError(), nullptr, 0); return false; } ACL_Buffer.reset(); LocalFree(SID_Value); //Initialization BOOL Result = FALSE; bool FlushDNS = false; DWORD cbMessage = 0, cMessage = 0, cbRead = 0; std::shared_ptr<wchar_t> lpszBuffer(new wchar_t[PACKET_MAXSIZE]()); wmemset(lpszBuffer.get(), 0, PACKET_MAXSIZE); //MailSlot Monitor for (;;) { cbMessage = 0; cMessage = 0; //Get mailslot messages. Result = GetMailslotInfo(hSlot, nullptr, &cbMessage, &cMessage, nullptr); if (Result == FALSE) { PrintError(LOG_ERROR_SYSTEM, L"Mailslot Monitor initialization error", GetLastError(), nullptr, 0); CloseHandle(hSlot); return false; } //Wait for messages. if (cbMessage == MAILSLOT_NO_MESSAGE) { Sleep(LOOP_INTERVAL_TIME_MONITOR); continue; } //Got messages. FlushDNS = false; while (cMessage > 0) { Result = ReadFile(hSlot, lpszBuffer.get(), cbMessage, &cbRead, nullptr); if (Result == FALSE) { PrintError(LOG_ERROR_SYSTEM, L"MailSlot read messages error", GetLastError(), nullptr, 0); CloseHandle(hSlot); return false; } if (!FlushDNS && memcmp(lpszBuffer.get(), MAILSLOT_MESSAGE_FLUSH_DNS, wcslen(MAILSLOT_MESSAGE_FLUSH_DNS)) == EXIT_SUCCESS) { FlushDNS = true; FlushAllDNSCache(); } memset(lpszBuffer.get(), 0, PACKET_MAXSIZE); //Get other mailslot messages. Result = GetMailslotInfo(hSlot, nullptr, &cbMessage, &cMessage, nullptr); if (Result == FALSE) { PrintError(LOG_ERROR_SYSTEM, L"Mailslot Monitor initialization error", GetLastError(), nullptr, 0); CloseHandle(hSlot); return false; } } Sleep(LOOP_INTERVAL_TIME_MONITOR); } //Monitor terminated CloseHandle(hSlot); PrintError(LOG_ERROR_SYSTEM, L"MailSlot module Monitor terminated", 0, nullptr, 0); return false; }
//MailSlot of flush DNS cache Monitor bool FlushDNSMailSlotMonitor( void) { //System security setting std::shared_ptr<uint8_t> ACL_Buffer(new uint8_t[FILE_BUFFER_SIZE]()); memset(ACL_Buffer.get(), 0, FILE_BUFFER_SIZE); SECURITY_ATTRIBUTES SecurityAttributes; SECURITY_DESCRIPTOR SecurityDescriptor; memset(&SecurityAttributes, 0, sizeof(SecurityAttributes)); memset(&SecurityDescriptor, 0, sizeof(SecurityDescriptor)); PSID SID_Value = nullptr; InitializeSecurityDescriptor(&SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION); InitializeAcl((PACL)ACL_Buffer.get(), FILE_BUFFER_SIZE, ACL_REVISION); ConvertStringSidToSidW(SID_ADMINISTRATORS_GROUP, &SID_Value); AddAccessAllowedAce((PACL)ACL_Buffer.get(), ACL_REVISION, GENERIC_ALL, SID_Value); SetSecurityDescriptorDacl(&SecurityDescriptor, true, (PACL)ACL_Buffer.get(), false); SecurityAttributes.lpSecurityDescriptor = &SecurityDescriptor; SecurityAttributes.bInheritHandle = true; //Create mailslot. HANDLE hSlot = CreateMailslotW(MAILSLOT_NAME, FILE_BUFFER_SIZE - 1U, MAILSLOT_WAIT_FOREVER, &SecurityAttributes); if (hSlot == INVALID_HANDLE_VALUE) { LocalFree(SID_Value); PrintError(LOG_LEVEL_2, LOG_ERROR_SYSTEM, L"Create mailslot error", GetLastError(), nullptr, 0); return false; } ACL_Buffer.reset(); LocalFree(SID_Value); //Initialization std::shared_ptr<wchar_t> lpszBuffer(new wchar_t[FILE_BUFFER_SIZE]()); wmemset(lpszBuffer.get(), 0, FILE_BUFFER_SIZE); std::wstring Message; std::string Domain; DWORD cbMessage = 0; BOOL Result = 0; //MailSlot monitor for (;;) { //Reset parameters. wmemset(lpszBuffer.get(), 0, FILE_BUFFER_SIZE); cbMessage = 0; //Read message from mailslot. Result = ReadFile(hSlot, lpszBuffer.get(), FILE_BUFFER_SIZE, &cbMessage, nullptr); if (Result == FALSE) { PrintError(LOG_LEVEL_3, LOG_ERROR_SYSTEM, L"MailSlot read messages error", GetLastError(), nullptr, 0); CloseHandle(hSlot); return false; } else { Message = lpszBuffer.get(); Domain.clear(); //Read message. if (Message == MAILSLOT_MESSAGE_FLUSH_DNS) //Flush all DNS cache. { FlushDNSCache(nullptr); } else if (Message.find(MAILSLOT_MESSAGE_FLUSH_DNS_DOMAIN) == 0 && //Flush single domain cache. Message.length() > wcslen(MAILSLOT_MESSAGE_FLUSH_DNS_DOMAIN) + DOMAIN_MINSIZE && //Domain length check Message.length() < wcslen(MAILSLOT_MESSAGE_FLUSH_DNS_DOMAIN) + DOMAIN_MAXSIZE) { if (WCSToMBSString(Message.c_str() + wcslen(MAILSLOT_MESSAGE_FLUSH_DNS_DOMAIN), DOMAIN_MAXSIZE, Domain) && Domain.length() > DOMAIN_MINSIZE && Domain.length() < DOMAIN_MAXSIZE) FlushDNSCache((const uint8_t *)Domain.c_str()); else PrintError(LOG_LEVEL_2, LOG_ERROR_SYSTEM, L"Convert multiple byte or wide char string error", 0, nullptr, 0); } else { Sleep(Parameter.FileRefreshTime); } } } //Monitor terminated CloseHandle(hSlot); PrintError(LOG_LEVEL_2, LOG_ERROR_SYSTEM, L"MailSlot module Monitor terminated", 0, nullptr, 0); return false; }
int main(int argc, char *argv[]) { std::auto_ptr<cmd> pSetup(new cmd); if( GetSetupCommand( pSetup.get(), argc, argv) != false ) { return 1; } else { try { std::auto_ptr<FileIO> pInFile(new FileIO); std::auto_ptr<FileIO> pOutFile(new FileIO); pInFile->FileOpen(pSetup->inFile.c_str(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING); unsigned int iSizeBuffer = pSetup->sizeBuffer; DWORD *pvBuffer = new DWORD[iSizeBuffer]; std::string outNameFile = pSetup->outFile + "."; std::string outFile = pSetup->outDir; std::string CommandLine; DWORD nReadBytes = 0; DWORD nWriteBytes = 0; pInFile->SetPointer(0, FILE_BEGIN); pOutFile->SetPointer(0,FILE_BEGIN); int64 potSizeFile = pSetup->outFileSize; unsigned int nByteFofRead = iSizeBuffer; static int IndexFile = 0; bool bCreateFile = true; while(true) { if(nByteFofRead >= potSizeFile) { nByteFofRead = potSizeFile; potSizeFile = 0; } pInFile->Read(pvBuffer, nByteFofRead, &nReadBytes); if(nReadBytes > 0) { if(bCreateFile) { nByteFofRead = iSizeBuffer; ++IndexFile; char temp[6] = {0}; prefixFile(IndexFile, temp); outFile = pSetup->outDir + outNameFile + temp; if(pSetup->makeBatFile){ CommandLine += outNameFile + temp; CommandLine += " + "; } pOutFile->FileOpen(outFile.c_str(), GENERIC_WRITE, FILE_SHARE_READ, CREATE_ALWAYS); } bCreateFile = false; } else { break; } if(nByteFofRead < potSizeFile) { potSizeFile -= iSizeBuffer; } pOutFile->Write(pvBuffer, nReadBytes, &nWriteBytes); if(potSizeFile == 0) { bCreateFile = true; potSizeFile = pSetup->outFileSize; pOutFile->Close(); } } // delete pInFile; // delete pOutFile; delete []pvBuffer; if(pSetup->makeBatFile){ CreateBatFile(CommandLine, pSetup->outFile.c_str(), pSetup->outDir.c_str()); } } catch (DWORD &codErro) { std::auto_ptr<char> lpszBuffer(new(char[255])); DWORD cchBuffer = 255; ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, codErro, NULL, lpszBuffer.get(), cchBuffer, NULL); printf("Error Code: %d\nError Name: %s", static_cast<unsigned int>(codErro), lpszBuffer.get()); } catch(...) { printf("\ncatch(...)\n"); } } return 0; }
//MailSlot of flush DNS cache Monitor bool __fastcall FlushDNSMailSlotMonitor( void) { //System security setting std::shared_ptr<char> ACL_Buffer(new char[FILE_BUFFER_SIZE]()); memset(ACL_Buffer.get(), 0, FILE_BUFFER_SIZE); SECURITY_ATTRIBUTES SecurityAttributes; SECURITY_DESCRIPTOR SecurityDescriptor; memset(&SecurityAttributes, 0, sizeof(SECURITY_ATTRIBUTES)); memset(&SecurityDescriptor, 0, sizeof(SECURITY_DESCRIPTOR)); PSID SID_Value = nullptr; InitializeSecurityDescriptor(&SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION); InitializeAcl((PACL)ACL_Buffer.get(), FILE_BUFFER_SIZE, ACL_REVISION); ConvertStringSidToSidW(SID_ADMINISTRATORS_GROUP, &SID_Value); AddAccessAllowedAce((PACL)ACL_Buffer.get(), ACL_REVISION, GENERIC_ALL, SID_Value); SetSecurityDescriptorDacl(&SecurityDescriptor, true, (PACL)ACL_Buffer.get(), false); SecurityAttributes.lpSecurityDescriptor = &SecurityDescriptor; SecurityAttributes.bInheritHandle = true; //Create mailslot. HANDLE hSlot = CreateMailslotW(MAILSLOT_NAME, FILE_BUFFER_SIZE - 1U, MAILSLOT_WAIT_FOREVER, &SecurityAttributes); if (hSlot == INVALID_HANDLE_VALUE) { LocalFree(SID_Value); PrintError(LOG_LEVEL_2, LOG_ERROR_SYSTEM, L"Create mailslot error", GetLastError(), nullptr, 0); return false; } ACL_Buffer.reset(); LocalFree(SID_Value); //Initialization std::shared_ptr<wchar_t> lpszBuffer(new wchar_t[FILE_BUFFER_SIZE]()); wmemset(lpszBuffer.get(), 0, FILE_BUFFER_SIZE); DWORD cbMessage = 0; BOOL Result = 0; //MailSlot monitor for (;;) { Sleep(LOOP_INTERVAL_TIME_NO_DELAY); //Reset parameters. wmemset(lpszBuffer.get(), 0, FILE_BUFFER_SIZE); cbMessage = 0; //Read message from mailslot. Result = ReadFile(hSlot, lpszBuffer.get(), FILE_BUFFER_SIZE, &cbMessage, nullptr); if (Result == FALSE) { PrintError(LOG_LEVEL_3, LOG_ERROR_SYSTEM, L"MailSlot read messages error", GetLastError(), nullptr, 0); CloseHandle(hSlot); return false; } else if (memcmp(lpszBuffer.get(), MAILSLOT_MESSAGE_FLUSH_DNS, sizeof(wchar_t) * wcslen(MAILSLOT_MESSAGE_FLUSH_DNS)) == 0) { FlushAllDNSCache(); } else { Sleep(LOOP_INTERVAL_TIME_MONITOR); } } //Monitor terminated CloseHandle(hSlot); PrintError(LOG_LEVEL_2, LOG_ERROR_SYSTEM, L"MailSlot module Monitor terminated", 0, nullptr, 0); return false; }