Try fix get_local_ip_for_gateway for Linux
This commit is contained in:
@@ -241,43 +241,43 @@ namespace platf {
|
|||||||
return "00:00:00:00:00:00"s;
|
return "00:00:00:00:00:00"s;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string
|
std::string
|
||||||
get_local_ip_for_gateway() {
|
get_local_ip_for_gateway() {
|
||||||
int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
|
int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
BOOST_LOG(warning) << "Socket creation failed";
|
BOOST_LOG(warning) << "Socket creation failed: " << strerror(errno);
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
char buffer[8192];
|
char buffer[8192];
|
||||||
struct nlmsghdr *nlMsg = (struct nlmsghdr *)buffer;
|
struct nlmsghdr *nlMsg = (struct nlmsghdr *)buffer;
|
||||||
struct rtmsg *rtMsg = (struct rtmsg *)NLMSG_DATA(nlMsg);
|
struct rtmsg *rtMsg = (struct rtmsg *)NLMSG_DATA(nlMsg);
|
||||||
struct rtattr *rtAttr;
|
struct rtattr *rtAttr;
|
||||||
int len = 0;
|
int len = 0;
|
||||||
|
|
||||||
memset(nlMsg, 0, sizeof(struct nlmsghdr));
|
memset(nlMsg, 0, sizeof(struct nlmsghdr));
|
||||||
nlMsg->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
|
nlMsg->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
|
||||||
nlMsg->nlmsg_type = RTM_GETROUTE;
|
nlMsg->nlmsg_type = RTM_GETROUTE;
|
||||||
nlMsg->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
|
nlMsg->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
|
||||||
nlMsg->nlmsg_seq = 1;
|
nlMsg->nlmsg_seq = 1;
|
||||||
nlMsg->nlmsg_pid = getpid();
|
nlMsg->nlmsg_pid = getpid();
|
||||||
|
|
||||||
if (send(fd, nlMsg, nlMsg->nlmsg_len, 0) < 0) {
|
if (send(fd, nlMsg, nlMsg->nlmsg_len, 0) < 0) {
|
||||||
close(fd);
|
BOOST_LOG(warning) << "Send message failed: " << strerror(errno);
|
||||||
BOOST_LOG(warning) << "Send message failed";
|
close(fd);
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
len = recv(fd, nlMsg, sizeof(buffer), 0);
|
std::string local_ip;
|
||||||
if (len < 0) {
|
bool found = false;
|
||||||
close(fd);
|
|
||||||
BOOST_LOG(warning) << "Receive message failed";
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string local_ip;
|
|
||||||
|
|
||||||
|
while ((len = recv(fd, nlMsg, sizeof(buffer), 0)) > 0) {
|
||||||
for (; NLMSG_OK(nlMsg, len); nlMsg = NLMSG_NEXT(nlMsg, len)) {
|
for (; NLMSG_OK(nlMsg, len); nlMsg = NLMSG_NEXT(nlMsg, len)) {
|
||||||
|
if (nlMsg->nlmsg_type == NLMSG_DONE) {
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
rtMsg = (struct rtmsg *)NLMSG_DATA(nlMsg);
|
rtMsg = (struct rtmsg *)NLMSG_DATA(nlMsg);
|
||||||
if (rtMsg->rtm_family != AF_INET || rtMsg->rtm_table != RT_TABLE_MAIN)
|
if (rtMsg->rtm_family != AF_INET || rtMsg->rtm_table != RT_TABLE_MAIN)
|
||||||
continue;
|
continue;
|
||||||
@@ -304,18 +304,22 @@ namespace platf {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (gateway.s_addr != 0 && local.s_addr != 0) {
|
if (gateway.s_addr != 0 && local.s_addr != 0) {
|
||||||
local_ip = inet_ntoa(local);
|
local_ip = inet_ntoa(local);
|
||||||
break;
|
found = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
close(fd);
|
if (found) break;
|
||||||
|
}
|
||||||
|
|
||||||
if (local_ip.empty()) {
|
close(fd);
|
||||||
BOOST_LOG(warning) << "No associated IP address found for the default gateway";
|
|
||||||
}
|
|
||||||
|
|
||||||
return local_ip;
|
if (local_ip.empty()) {
|
||||||
|
BOOST_LOG(warning) << "No associated IP address found for the default gateway";
|
||||||
|
}
|
||||||
|
|
||||||
|
return local_ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
bp::child
|
bp::child
|
||||||
|
|||||||
Reference in New Issue
Block a user