//Flush DNS cache FIFO Monitor bool FlushDNSFIFOMonitor( void) { //Initialization unlink(FIFO_PATH_NAME); std::shared_ptr<char> Buffer(new char[FILE_BUFFER_SIZE]()); memset(Buffer.get(), 0, FILE_BUFFER_SIZE); int FileFIFO = 0; //Create FIFO and create its notify monitor. if (mkfifo(FIFO_PATH_NAME, O_CREAT) < 0 || chmod(FIFO_PATH_NAME, S_IRUSR|S_IWUSR|S_IWGRP|S_IWOTH) < 0) { PrintError(LOG_ERROR_SYSTEM, L"Create FIFO error", errno, nullptr, 0); unlink(FIFO_PATH_NAME); return false; } //FIFO Monitor for (;;) { Sleep(LOOP_INTERVAL_TIME_NO_DELAY); //Open FIFO. FileFIFO = open(FIFO_PATH_NAME, O_RDONLY, 0); if (FileFIFO < 0) { PrintError(LOG_ERROR_SYSTEM, L"Create FIFO error", errno, nullptr, 0); unlink(FIFO_PATH_NAME); return false; } //Read file data. memset(Buffer.get(), 0, FILE_BUFFER_SIZE); if (read(FileFIFO, Buffer.get(), FILE_BUFFER_SIZE) >= strlen(FIFO_MESSAGE_FLUSH_DNS) && memcmp(Buffer.get(), FIFO_MESSAGE_FLUSH_DNS, strlen(FIFO_MESSAGE_FLUSH_DNS)) == 0) FlushAllDNSCache(); //Close FIFO. close(FileFIFO); FileFIFO = 0; } //Monitor terminated close(FileFIFO); unlink(FIFO_PATH_NAME); PrintError(LOG_ERROR_SYSTEM, L"FIFO module Monitor terminated", 0, nullptr, 0); return true; }
//Flush DNS cache FIFO Monitor bool FlushDNSFIFOMonitor( void) { //Initialization unlink(FIFO_PATH_NAME); std::shared_ptr<char> Buffer(new char[PACKET_MAXSIZE]()); memset(Buffer.get(), 0, PACKET_MAXSIZE); int FIFO_FD = 0; //Create FIFO. if (mkfifo(FIFO_PATH_NAME, O_CREAT) < EXIT_SUCCESS || chmod(FIFO_PATH_NAME, S_IRUSR|S_IWUSR|S_IWGRP|S_IWOTH) < EXIT_SUCCESS) { PrintError(LOG_ERROR_SYSTEM, L"Create FIFO error", errno, nullptr, 0); unlink(FIFO_PATH_NAME); return false; } //Open FIFO. FIFO_FD = open(FIFO_PATH_NAME, O_RDONLY, 0); if (FIFO_FD < EXIT_SUCCESS) { PrintError(LOG_ERROR_SYSTEM, L"Create FIFO error", errno, nullptr, 0); unlink(FIFO_PATH_NAME); return false; } //FIFO Monitor for (;;) { if (read(FIFO_FD, Buffer.get(), PACKET_MAXSIZE) > 0 && memcmp(Buffer.get(), FIFO_MESSAGE_FLUSH_DNS, strlen(FIFO_MESSAGE_FLUSH_DNS)) == EXIT_SUCCESS) FlushAllDNSCache(); memset(Buffer.get(), 0, PACKET_MAXSIZE); Sleep(MONITOR_LOOP_INTERVAL_TIME); } //Monitor terminated close(FIFO_FD); unlink(FIFO_PATH_NAME); PrintError(LOG_ERROR_SYSTEM, L"FIFO module Monitor terminated", 0, nullptr, 0); return true; }
//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 __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; }