Skip to content

Commit

Permalink
change c style code to cpp style
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengfeihe committed Jun 8, 2024
1 parent 3a50896 commit 6d90884
Showing 1 changed file with 34 additions and 34 deletions.
68 changes: 34 additions & 34 deletions Pcap++/src/PcapLiveDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1109,70 +1109,70 @@ void PcapLiveDevice::setDefaultGateway()
return;
}

struct sockaddr_in *gate = nullptr;
struct in_addr *gateAddr = nullptr;
struct sockaddr_dl *ifp = nullptr;
struct sockaddr *sa = nullptr;
spacePtr = (reinterpret_cast<char*>(&routingMessage.header+1));
for (int i = 1; i > 0; i <<= 1)
for (int i = 1; i != 0; i <<= 1)
{
if (i & routingMessage.header.rtm_addrs)
{
sa = reinterpret_cast<sockaddr *>(spacePtr);
switch (i)
{
case RTA_GATEWAY:
gate = (sockaddr_in* )sa;
gateAddr = internal::sockaddr2in_addr(sa);
break;
case RTA_IFP:
if (sa->sa_family == AF_LINK &&
((sockaddr_dl *)sa)->sdl_nlen)
ifp = (sockaddr_dl *)sa;
reinterpret_cast<sockaddr_dl*>(sa)->sdl_nlen)
ifp = reinterpret_cast<sockaddr_dl *>(sa);
break;
}
// Make sure the increment is the nearest multiple of the size of uint32_t
spacePtr += sa->sa_len > 0 ? (1 + (((sa->sa_len) - 1) | (sizeof(uint32_t) - 1))) : sizeof(uint32_t);
}
}
if(gate == nullptr || ifp == nullptr)
if(gateAddr == nullptr || ifp == nullptr)
{
PCPP_LOG_ERROR("Error retrieving default gateway address: Empty Message related to gate");
return;
}
try
{
m_DefaultGateway = IPv4Address(gate->sin_addr.s_addr);
m_DefaultGateway = IPv4Address(gateAddr->s_addr);
}
catch(const std::exception& e)
{
PCPP_LOG_ERROR("Error retrieving default gateway address: "<< inet_ntoa(gate->sin_addr) << ": " << e.what());
PCPP_LOG_ERROR("Error retrieving default gateway address: "<< inet_ntoa(*gateAddr) << ": " << e.what());
}
#elif defined(__FreeBSD__)
std::string command = "netstat -nr | grep default | grep " + m_Name;
std::string ifaceInfo = executeShellCommand(command);
if (ifaceInfo == "")
{
PCPP_LOG_DEBUG("Error retrieving default gateway address: couldn't get netstat output");
return;
}

// remove the word "default"
ifaceInfo.erase(0, 7);

// remove spaces
while (ifaceInfo.at(0) == ' ')
ifaceInfo.erase(0,1);

// erase string after gateway IP address
ifaceInfo.resize(ifaceInfo.find(' ', 0));

try
{
m_DefaultGateway = IPv4Address(ifaceInfo);
}
catch(const std::exception& e)
{
PCPP_LOG_ERROR("Error retrieving default gateway address: "<< ifaceInfo << ": " << e.what());
}
std::string command = "netstat -nr | grep default | grep " + m_Name;
std::string ifaceInfo = executeShellCommand(command);
if (ifaceInfo == "")
{
PCPP_LOG_DEBUG("Error retrieving default gateway address: couldn't get netstat output");
return;
}

// remove the word "default"
ifaceInfo.erase(0, 7);

// remove spaces
while (ifaceInfo.at(0) == ' ')
ifaceInfo.erase(0,1);

// erase string after gateway IP address
ifaceInfo.resize(ifaceInfo.find(' ', 0));

try
{
m_DefaultGateway = IPv4Address(ifaceInfo);
}
catch(const std::exception& e)
{
PCPP_LOG_ERROR("Error retrieving default gateway address: "<< ifaceInfo << ": " << e.what());
}
#endif
}

Expand Down

0 comments on commit 6d90884

Please sign in to comment.