Skip to content

Commit

Permalink
Pcap live device casts cleanup (#1401)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimi1010 authored May 24, 2024
1 parent 4c32a95 commit 284f30a
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions Pcap++/src/PcapLiveDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ PcapLiveDevice::PcapLiveDevice(pcap_if_t* pInterface, bool calculateMTU, bool ca

void PcapLiveDevice::onPacketArrives(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet)
{
PcapLiveDevice* pThis = (PcapLiveDevice*)user;
PcapLiveDevice* pThis = reinterpret_cast<PcapLiveDevice*>(user);
if (pThis == nullptr)
{
PCPP_LOG_ERROR("Unable to extract PcapLiveDevice instance");
Expand All @@ -149,7 +149,7 @@ void PcapLiveDevice::onPacketArrives(uint8_t* user, const struct pcap_pkthdr* pk

void PcapLiveDevice::onPacketArrivesNoCallback(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet)
{
PcapLiveDevice* pThis = (PcapLiveDevice*)user;
PcapLiveDevice* pThis = reinterpret_cast<PcapLiveDevice*>(user);
if (pThis == nullptr)
{
PCPP_LOG_ERROR("Unable to extract PcapLiveDevice instance");
Expand All @@ -164,7 +164,7 @@ void PcapLiveDevice::onPacketArrivesNoCallback(uint8_t* user, const struct pcap_

void PcapLiveDevice::onPacketArrivesBlockingMode(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet)
{
PcapLiveDevice* pThis = (PcapLiveDevice*)user;
PcapLiveDevice* pThis = reinterpret_cast<PcapLiveDevice*>(user);
if (pThis == nullptr)
{
PCPP_LOG_ERROR("Unable to extract PcapLiveDevice instance");
Expand Down Expand Up @@ -673,7 +673,7 @@ void PcapLiveDevice::getStatistics(PcapStats& stats) const

bool PcapLiveDevice::doMtuCheck(int packetPayloadLength)
{
if (packetPayloadLength > (int)m_DeviceMtu)
if (packetPayloadLength > static_cast<int>(m_DeviceMtu))
{
PCPP_LOG_ERROR("Payload length [" << packetPayloadLength << "] is larger than device MTU [" << m_DeviceMtu << "]");
return false;
Expand All @@ -685,12 +685,12 @@ bool PcapLiveDevice::sendPacket(RawPacket const& rawPacket, bool checkMtu)
{
if (checkMtu)
{
RawPacket *rPacket = (RawPacket *)&rawPacket;
RawPacket* rPacket = const_cast<RawPacket*>(&rawPacket);
Packet parsedPacket = Packet(rPacket, OsiModelDataLinkLayer);
return sendPacket(&parsedPacket, true);
}
// Send packet without Mtu check
return sendPacket(((RawPacket&)rawPacket).getRawData(), ((RawPacket&)rawPacket).getRawDataLen());
return sendPacket(rawPacket.getRawData(), rawPacket.getRawDataLen());
}

bool PcapLiveDevice::sendPacket(const uint8_t* packetData, int packetDataLength, int packetPayloadLength)
Expand Down Expand Up @@ -740,10 +740,10 @@ bool PcapLiveDevice::sendPacket(Packet* packet, bool checkMtu)
switch (packet->getFirstLayer()->getOsiModelLayer())
{
case (pcpp::OsiModelDataLinkLayer):
packetPayloadLength = (int)packet->getFirstLayer()->getLayerPayloadSize();
packetPayloadLength = static_cast<int>(packet->getFirstLayer()->getLayerPayloadSize());
break;
case (pcpp::OsiModelNetworkLayer):
packetPayloadLength = (int)packet->getFirstLayer()->getDataLen();
packetPayloadLength = static_cast<int>(packet->getFirstLayer()->getDataLen());
break;
default:
// if packet layer is not known, do not perform MTU check.
Expand Down Expand Up @@ -805,15 +805,15 @@ void PcapLiveDevice::setDeviceMtu()
}

uint32_t mtuValue = 0;
LPADAPTER adapter = PacketOpenAdapter((char*)m_Name.c_str());
LPADAPTER adapter = PacketOpenAdapter(const_cast<char*>(m_Name.c_str()));
if (adapter == NULL)
{
PCPP_LOG_ERROR("Error in retrieving MTU: Adapter is NULL");
return;
}

uint8_t buffer[512];
PACKET_OID_DATA* oidData = (PACKET_OID_DATA*)buffer;
PACKET_OID_DATA* oidData = reinterpret_cast<PACKET_OID_DATA*>(buffer);
oidData->Oid = OID_GEN_MAXIMUM_TOTAL_SIZE;
oidData->Length = sizeof(uint32_t);
memcpy(oidData->Data, &mtuValue, sizeof(uint32_t));
Expand Down Expand Up @@ -879,15 +879,15 @@ void PcapLiveDevice::setDeviceMacAddress()
{
#if defined(_WIN32)

LPADAPTER adapter = PacketOpenAdapter((char*)m_Name.c_str());
LPADAPTER adapter = PacketOpenAdapter(const_cast<char*>(m_Name.c_str()));
if (adapter == NULL)
{
PCPP_LOG_ERROR("Error in retrieving MAC address: Adapter is NULL");
return;
}

uint8_t buffer[512];
PACKET_OID_DATA* oidData = (PACKET_OID_DATA*)buffer;
PACKET_OID_DATA* oidData = reinterpret_cast<PACKET_OID_DATA*>(buffer);
oidData->Oid = OID_802_3_CURRENT_ADDRESS;
oidData->Length = 6;
oidData->Data[0] = 0;
Expand Down Expand Up @@ -983,13 +983,13 @@ void PcapLiveDevice::setDefaultGateway()
#if defined(_WIN32)
ULONG outBufLen = sizeof (IP_ADAPTER_INFO);
uint8_t* buffer = new uint8_t[outBufLen];
PIP_ADAPTER_INFO adapterInfo = (IP_ADAPTER_INFO*)buffer;
PIP_ADAPTER_INFO adapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(buffer);
DWORD retVal = 0;

retVal = GetAdaptersInfo(adapterInfo, &outBufLen);
uint8_t* buffer2 = new uint8_t[outBufLen];
if (retVal == ERROR_BUFFER_OVERFLOW)
adapterInfo = (IP_ADAPTER_INFO *)buffer2;
adapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(buffer2);

retVal = GetAdaptersInfo(adapterInfo, &outBufLen);

Expand Down Expand Up @@ -1086,7 +1086,7 @@ void PcapLiveDevice::setDefaultGateway()

IPv4Address PcapLiveDevice::getIPv4Address() const
{
for(const auto &addrIter : m_Addresses)
for(const auto& addrIter : m_Addresses)
{
if (Logger::getInstance().isDebugEnabled(PcapLogModuleLiveDevice) && addrIter.addr != nullptr)
{
Expand Down Expand Up @@ -1117,7 +1117,7 @@ IPv4Address PcapLiveDevice::getIPv4Address() const

IPv6Address PcapLiveDevice::getIPv6Address() const
{
for (const auto &addrIter : m_Addresses)
for (const auto& addrIter : m_Addresses)
{
if (Logger::getInstance().isDebugEnabled(PcapLogModuleLiveDevice) && addrIter.addr != nullptr)
{
Expand Down

0 comments on commit 284f30a

Please sign in to comment.