Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the way to get default gateway on macOS #1379

Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
82b02d1
improve macOS get default gateway
zhengfeihe May 1, 2024
dc56b9a
Merge branch 'dev' into feature/improve_getting_default_gateway_metho…
tigercosmos May 3, 2024
5f6750a
fix based on comments
zhengfeihe May 5, 2024
d9b34b2
Merge branch 'dev' into feature/improve_getting_default_gateway_metho…
tigercosmos May 5, 2024
2605bed
fix header file issue in windows system
zhengfeihe May 5, 2024
8d520c6
Merge branch 'seladb:master' into feature/improve_getting_default_gat…
zhengfeihe May 17, 2024
66296ae
Merge branch 'dev' into feature/improve_getting_default_gateway_metho…
tigercosmos May 22, 2024
e4a1289
Update Pcap++/src/PcapLiveDevice.cpp
tigercosmos May 22, 2024
d699597
Merge branch 'dev' into feature/improve_getting_default_gateway_metho…
zhengfeihe Jun 5, 2024
3a50896
separate freeBSD implementation from macOS
zhengfeihe Jun 5, 2024
0c9db45
change c style code to cpp style
zhengfeihe Jun 8, 2024
6236fb8
Merge branch 'dev' into feature/improve_getting_default_gateway_metho…
tigercosmos Jun 8, 2024
968f4ca
separate MacOS and FreeBSD header files
zhengfeihe Jun 9, 2024
d8b6cf1
Merge branch 'dev' into feature/improve_getting_default_gateway_metho…
tigercosmos Jun 9, 2024
7543cbd
add sys header for macOS
zhengfeihe Jun 9, 2024
4072592
use tabs for indent
zhengfeihe Jun 11, 2024
4948284
simplify logic by removing unnecessary code
zhengfeihe Jun 19, 2024
cd42e43
Merge branch 'seladb:master' into feature/improve_getting_default_gat…
zhengfeihe Jun 19, 2024
395c25e
Merge branch 'dev' into feature/improve_getting_default_gateway_metho…
tigercosmos Jun 20, 2024
16a9a32
Fix failed tests
zhengfeihe Jun 22, 2024
4e7bf51
Merge branch 'dev' into feature/improve_getting_default_gateway_metho…
zhengfeihe Jun 22, 2024
605f1ac
fix indent problems
zhengfeihe Jun 23, 2024
b0bb71b
Merge branch 'dev' into feature/improve_getting_default_gateway_metho…
seladb Jun 23, 2024
32d4e29
Fix formatting
seladb Jun 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Pcap++/header/PcapLiveDevice.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#pragma once

#include <atomic>
#include <vector>
#include <functional>
#include <string.h>
#include <thread>
#include <functional>
#include <vector>

#include "IpAddress.h"
#include "Packet.h"
Expand Down
114 changes: 96 additions & 18 deletions Pcap++/src/PcapLiveDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,18 @@
#include <poll.h>
#include <pcap/pcap.h>
#endif // if defined(_WIN32)
#if defined(__APPLE__) || defined(__FreeBSD__)
#if defined(__APPLE__)
#include <net/if_dl.h>
#include <sys/sysctl.h>
#include <net/route.h>
tigercosmos marked this conversation as resolved.
Show resolved Hide resolved
#endif

#if defined(__FreeBSD__)
#include <sys/sysctl.h>
#include <net/if_dl.h>
#endif


// TODO: FIX FreeBSD
// On Mac OS X and FreeBSD timeout of -1 causes pcap_open_live to fail.
// A value of 1ms first solve the issue but since Jan. 2024 an issue
Expand Down Expand Up @@ -1056,33 +1063,104 @@ void PcapLiveDevice::setDefaultGateway()
PCPP_LOG_ERROR("Error retrieving default gateway address: " << e.what());
}
}
#elif defined(__APPLE__) || 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");
#elif defined(__APPLE__)

//route message struct for communication in APPLE device
struct BSDRoutingMessage{
struct rt_msghdr header;
char messageSpace[512];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wondering if 512 is a proper number?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The number used is consistent with the one in the macOS source code. I believe it would be safer to retain it as is.

};

struct BSDRoutingMessage routingMessage;
char* spacePtr = routingMessage.messageSpace;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please notice the pre-commit failure: https://github.com/seladb/PcapPlusPlus/actions/runs/9585549253/job/26431847597?pr=1379

no need to initialize spacePtr here because it's also initialized in line 1104

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the unused initialization .

// It creates a raw socket that can be used for routing-related operations
int sockfd = socket(PF_ROUTE, SOCK_RAW, 0);
if (sockfd < 0) {
PCPP_LOG_ERROR("Error retrieving default gateway address: couldn't get open routing socket");
return ;
}
memset(reinterpret_cast<char*>(&routingMessage), 0, sizeof(routingMessage));
routingMessage.header.rtm_msglen = sizeof(struct rt_msghdr);
routingMessage.header.rtm_version = RTM_VERSION;
routingMessage.header.rtm_type = RTM_GET;
routingMessage.header.rtm_addrs = RTA_DST | RTA_NETMASK ;
routingMessage.header.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;

routingMessage.header.rtm_msglen += 2*sizeof(sockaddr_in) ;

if (write(sockfd, reinterpret_cast<char*>(&routingMessage), routingMessage.header.rtm_msglen) < 0) {
PCPP_LOG_ERROR("Error retrieving default gateway address: couldn't write into the routing socket");
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));
// Read the response from the route socket
if (read(sockfd, reinterpret_cast<char*>(&routingMessage), sizeof(routingMessage)) < 0) {
PCPP_LOG_ERROR("Error retrieving default gateway address: couldn't read from the routing socket");
return;
}

struct in_addr *gateAddr = nullptr;
struct sockaddr *sa = nullptr;
spacePtr = (reinterpret_cast<char*>(&routingMessage.header+1));
auto rtmAddrs = routingMessage.header.rtm_addrs;
int index = 0;
auto roundUpClosetMultiple = [](int multiple, int num){
Dimi1010 marked this conversation as resolved.
Show resolved Hide resolved
return ((num+multiple-1)/multiple)*multiple;
};
while (rtmAddrs)
{
if (rtmAddrs & 1) {
sa = reinterpret_cast<sockaddr *>(spacePtr);
if(index == RTA_GATEWAY) {
gateAddr = internal::sockaddr2in_addr(sa);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Use tabs for indent.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's why we need clang-format lol

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhengfeihe please update to tabs instead of spaces

break;
}
spacePtr += sa->sa_len > 0 ? roundUpClosetMultiple(sizeof(uint32_t), sa->sa_len) : sizeof(uint32_t);
}
index++;
rtmAddrs >>= 1;
}
Dimi1010 marked this conversation as resolved.
Show resolved Hide resolved

if(gateAddr == nullptr)
{
PCPP_LOG_ERROR("Error retrieving default gateway address: Empty Message related to gate");
return;
}
try
{
m_DefaultGateway = IPv4Address(ifaceInfo);
m_DefaultGateway = IPv4Address(gateAddr->s_addr);
}
catch(const std::exception& e)
{
PCPP_LOG_ERROR("Error retrieving default gateway address: "<< ifaceInfo << ": " << 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;
tigercosmos marked this conversation as resolved.
Show resolved Hide resolved
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