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 1 commit
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Pcap++/header/PcapLiveDevice.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#pragma once

#include <atomic>
#include <vector>
#include <functional>
#include <net/route.h>
#include <string.h>
#include <thread>
#include <functional>
#include <vector>

#include "IpAddress.h"
#include "Packet.h"
Expand Down Expand Up @@ -578,4 +579,10 @@ namespace pcpp
pcap_t* doOpen(const DeviceConfiguration& config);
};

//route message struct for communication in BSD / APPLE device
struct BSDRoutingMessage{
struct rt_msghdr header;
char messageSpace[512];
};
zhengfeihe marked this conversation as resolved.
Show resolved Hide resolved

} // namespace pcpp
86 changes: 70 additions & 16 deletions Pcap++/src/PcapLiveDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#endif // if defined(_WIN32)
#if defined(__APPLE__) || defined(__FreeBSD__)
#include <net/if_dl.h>
#include <net/route.h>
tigercosmos marked this conversation as resolved.
Show resolved Hide resolved
#include <sys/sysctl.h>
#endif

Expand Down Expand Up @@ -1055,31 +1056,84 @@ void PcapLiveDevice::setDefaultGateway()
}
}
#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");

#define ROUNDUP(a) \
Dimi1010 marked this conversation as resolved.
Show resolved Hide resolved
((a) > 0 ? (1 + (((a) - 1) | (sizeof(uint32_t) - 1))) : sizeof(uint32_t))
#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))

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 .

int sockfd = socket(PF_ROUTE, SOCK_RAW, 0);
if (sockfd < 0) {
PCPP_LOG_DEBUG("Error retrieving default gateway address: couldn't get open routing socket");
zhengfeihe marked this conversation as resolved.
Show resolved Hide resolved
return ;
}

bzero((char *)&routingMessage, sizeof(routingMessage));
Dimi1010 marked this conversation as resolved.
Show resolved Hide resolved
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 | RTA_IFP;
routingMessage.header.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;

struct sockaddr_in so_dst , so_mask;
struct sockaddr_dl so_ifp;
memset(&so_dst, 0, sizeof(so_dst));
memset(&so_mask, 0, sizeof(so_mask));
memset(&so_ifp, 0, sizeof(so_ifp));
zhengfeihe marked this conversation as resolved.
Show resolved Hide resolved

int len = sizeof(sockaddr_in);
zhengfeihe marked this conversation as resolved.
Show resolved Hide resolved
bcopy((char *)&(so_dst), spacePtr, len);
zhengfeihe marked this conversation as resolved.
Show resolved Hide resolved
spacePtr += len;
bcopy((char *)&(so_mask), spacePtr, len);
spacePtr += len;
routingMessage.header.rtm_msglen += 2*len ;
len = sizeof(sockaddr_dl);
bcopy((char *)&(so_ifp), spacePtr, len);
spacePtr += len;


if (write(sockfd, (char*)&routingMessage, routingMessage.header.rtm_msglen) < 0) {
PCPP_LOG_DEBUG("Error retrieving default gateway address: couldn't write into the routing socket");
zhengfeihe marked this conversation as resolved.
Show resolved Hide resolved
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, (char*)&routingMessage, sizeof(routingMessage)) < 0) {
PCPP_LOG_DEBUG("Error retrieving default gateway address: couldn't read from the routing socket");
return;
}

struct sockaddr_in *gate = nullptr;
struct sockaddr_dl *ifp = nullptr;
struct sockaddr *sa = nullptr;
spacePtr = ((char*)(&routingMessage.header+1));
for (int i = 1; i; i <<= 1)
zhengfeihe marked this conversation as resolved.
Show resolved Hide resolved
{
if (i & routingMessage.header.rtm_addrs)
{
sa = (struct sockaddr *)spacePtr;
switch (i)
zhengfeihe marked this conversation as resolved.
Show resolved Hide resolved
{
case RTA_GATEWAY:
gate = (sockaddr_in* )sa;
Dimi1010 marked this conversation as resolved.
Show resolved Hide resolved
break;
case RTA_IFP:
if (sa->sa_family == AF_LINK &&
((sockaddr_dl *)sa)->sdl_nlen)
ifp = (sockaddr_dl *)sa;
Dimi1010 marked this conversation as resolved.
Show resolved Hide resolved
break;
}
ADVANCE(spacePtr, sa);
zhengfeihe marked this conversation as resolved.
Show resolved Hide resolved
}
}
try
{
m_DefaultGateway = IPv4Address(ifaceInfo);
m_DefaultGateway = IPv4Address(gate->sin_addr.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(gate->sin_addr) << ": " << e.what());
}
#endif
}
Expand Down
Loading