NetworkCaptureLogger: Move ErrorState struct to Common/Network

This commit is contained in:
Sepalani 2022-05-26 16:50:53 +04:00
parent 41ce3a83d5
commit cbadc6e81a
4 changed files with 31 additions and 35 deletions

View File

@ -186,4 +186,22 @@ u16 ComputeNetworkChecksum(const void* data, u16 length, u32 initial_value)
checksum = (checksum >> 16) + (checksum & 0xFFFF);
return ~static_cast<u16>(checksum);
}
NetworkErrorState SaveNetworkErrorState()
{
return {
errno,
#ifdef _WIN32
WSAGetLastError(),
#endif
};
}
void RestoreNetworkErrorState(const NetworkErrorState& state)
{
errno = state.error;
#ifdef _WIN32
WSASetLastError(state.wsa_error);
#endif
}
} // namespace Common

View File

@ -99,8 +99,18 @@ struct UDPHeader
};
static_assert(sizeof(UDPHeader) == UDPHeader::SIZE);
struct NetworkErrorState
{
int error;
#ifdef _WIN32
int wsa_error;
#endif
};
MACAddress GenerateMacAddress(MACConsumer type);
std::string MacAddressToString(const MACAddress& mac);
std::optional<MACAddress> StringToMacAddress(std::string_view mac_string);
u16 ComputeNetworkChecksum(const void* data, u16 length, u32 initial_value = 0);
NetworkErrorState SaveNetworkErrorState();
void RestoreNetworkErrorState(const NetworkErrorState& state);
} // namespace Common

View File

@ -15,6 +15,7 @@
#include "Common/IOFile.h"
#include "Common/Network.h"
#include "Common/PcapFile.h"
#include "Common/ScopeGuard.h"
#include "Core/Config/MainSettings.h"
#include "Core/ConfigManager.h"
@ -90,24 +91,6 @@ void PCAPSSLCaptureLogger::OnNewSocket(s32 socket)
m_write_sequence_number[socket] = 0;
}
PCAPSSLCaptureLogger::ErrorState PCAPSSLCaptureLogger::SaveState() const
{
return {
errno,
#ifdef _WIN32
WSAGetLastError(),
#endif
};
}
void PCAPSSLCaptureLogger::RestoreState(const PCAPSSLCaptureLogger::ErrorState& state) const
{
errno = state.error;
#ifdef _WIN32
WSASetLastError(state.wsa_error);
#endif
}
void PCAPSSLCaptureLogger::LogSSLRead(const void* data, std::size_t length, s32 socket)
{
if (!Config::Get(Config::MAIN_NETWORK_SSL_DUMP_READ))
@ -135,7 +118,8 @@ void PCAPSSLCaptureLogger::LogWrite(const void* data, std::size_t length, s32 so
void PCAPSSLCaptureLogger::Log(LogType log_type, const void* data, std::size_t length, s32 socket,
sockaddr* other)
{
const auto state = SaveState();
const auto state = Common::SaveNetworkErrorState();
Common::ScopeGuard guard([&state] { Common::RestoreNetworkErrorState(state); });
sockaddr_in sock;
sockaddr_in peer;
sockaddr_in* from;
@ -144,16 +128,10 @@ void PCAPSSLCaptureLogger::Log(LogType log_type, const void* data, std::size_t l
socklen_t peer_len = sizeof(sock);
if (getsockname(socket, reinterpret_cast<sockaddr*>(&sock), &sock_len) != 0)
{
RestoreState(state);
return;
}
if (other == nullptr && getpeername(socket, reinterpret_cast<sockaddr*>(&peer), &peer_len) != 0)
{
RestoreState(state);
return;
}
if (log_type == LogType::Read)
{
@ -168,7 +146,6 @@ void PCAPSSLCaptureLogger::Log(LogType log_type, const void* data, std::size_t l
LogIPv4(log_type, reinterpret_cast<const u8*>(data), static_cast<u16>(length), socket, *from,
*to);
RestoreState(state);
}
void PCAPSSLCaptureLogger::LogIPv4(LogType log_type, const u8* data, u16 length, s32 socket,

View File

@ -99,15 +99,6 @@ private:
Read,
Write,
};
struct ErrorState
{
int error;
#ifdef _WIN32
int wsa_error;
#endif
};
ErrorState SaveState() const;
void RestoreState(const ErrorState& state) const;
void Log(LogType log_type, const void* data, std::size_t length, s32 socket, sockaddr* other);
void LogIPv4(LogType log_type, const u8* data, u16 length, s32 socket, const sockaddr_in& from,