Skip to content

Commit 0581337

Browse files
authored
Merge branch 'freebsd' into freebsd-merge
2 parents b5b7ea4 + f9cc001 commit 0581337

File tree

4 files changed

+13
-207
lines changed

4 files changed

+13
-207
lines changed

NetworkInterface.cpp

Lines changed: 9 additions & 203 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616
#include <unistd.h>
1717

1818
#include <arpa/inet.h>
19-
#include <linux/netlink.h>
20-
#include <linux/rtnetlink.h>
21-
#include <linux/ethtool.h>
22-
#include <linux/sockios.h>
2319
#include <net/ethernet.h>
2420
#include <net/if.h>
2521
#include <netinet/in.h>
@@ -31,6 +27,7 @@
3127
#include <sstream>
3228

3329

30+
3431
static std::string
3532
SpeedToString(struct ethtool_cmd* edata)
3633
{
@@ -109,20 +106,7 @@ NetworkInterface::Name() const
109106
std::string
110107
NetworkInterface::HardwareAddress() const
111108
{
112-
struct ifreq ifr;
113-
if (_DoRequest(SIOCGIFHWADDR, ifr) != 0)
114-
return "";
115-
116-
struct sockaddr* addr = (struct sockaddr*)&ifr.ifr_hwaddr;
117-
std::ostringstream stream;
118-
for (size_t i = 0; i < ETHER_ADDR_LEN; i++) {
119-
int byte = addr->sa_data[i] & 0xFF;
120-
if (i != 0)
121-
stream << ":";
122-
stream << std::hex << std::setw(2) << std::setfill('0') << byte;
123-
}
124-
125-
return stream.str();
109+
return "";
126110
}
127111

128112

@@ -151,46 +135,21 @@ NetworkInterface::IPAddress() const
151135
std::string
152136
NetworkInterface::NetMask() const
153137
{
154-
struct ifreq ifr;
155-
if (_DoRequest(SIOCGIFNETMASK, ifr) != 0)
156-
return "";
157-
158-
struct sockaddr_in* ipaddr = (struct sockaddr_in*)&ifr.ifr_netmask;
159-
return inet_ntoa(ipaddr->sin_addr);
138+
return "";
160139
}
161140

162141

163142
std::string
164143
NetworkInterface::Network() const
165144
{
166-
struct ifreq ifrNetMask;
167-
if (_DoRequest(SIOCGIFNETMASK, ifrNetMask) != 0)
168-
return "";
169-
170-
struct ifreq ifrIpAddress;
171-
if (_DoRequest(SIOCGIFADDR, ifrIpAddress) != 0)
172-
return "";
173-
174-
struct sockaddr_in* ipAddr = (struct sockaddr_in*)&ifrIpAddress.ifr_addr;
175-
struct sockaddr_in* netMask = (struct sockaddr_in*)&ifrNetMask.ifr_netmask;
176-
177-
struct in_addr networkAddress;
178-
::memset(&networkAddress, 0, sizeof(networkAddress));
179-
networkAddress.s_addr = ipAddr->sin_addr.s_addr & netMask->sin_addr.s_addr;
180-
181-
return inet_ntoa(networkAddress);
145+
return "";
182146
}
183147

184148

185149
std::string
186150
NetworkInterface::BroadcastAddress() const
187151
{
188-
struct ifreq ifr;
189-
if (_DoRequest(SIOCGIFBRDADDR, ifr) != 0)
190-
return "";
191-
192-
struct sockaddr_in* ipaddr = (struct sockaddr_in*)&ifr.ifr_broadaddr;
193-
return inet_ntoa(ipaddr->sin_addr);
152+
return "";
194153
}
195154

196155

@@ -207,16 +166,6 @@ NetworkInterface::HasDefaultGateway() const
207166
std::string
208167
NetworkInterface::DefaultGateway() const
209168
{
210-
std::list<route_info> routeInfo;
211-
if (_GetRoutes(routeInfo) <= 0)
212-
return "";
213-
214-
std::list<route_info>::const_iterator i;
215-
for (i = routeInfo.begin(); i != routeInfo.end(); i++) {
216-
if (i->dstAddr.s_addr == 0)
217-
return (char*)inet_ntoa(i->gateway);
218-
}
219-
220169
return "";
221170
}
222171

@@ -233,17 +182,7 @@ NetworkInterface::Type() const
233182
std::string
234183
NetworkInterface::Speed() const
235184
{
236-
struct ifreq ifr;
237-
struct ethtool_cmd edata;
238-
239-
ifr.ifr_data = (char*)&edata;
240-
241-
edata.cmd = ETHTOOL_GSET;
242-
243-
if (_DoRequest(SIOCETHTOOL, ifr) != 0)
244-
return "0";
245-
246-
return SpeedToString(&edata);
185+
return "";
247186
}
248187

249188

@@ -267,163 +206,30 @@ NetworkInterface::SpeedWithUnit() const
267206
std::string
268207
NetworkInterface::Status() const
269208
{
270-
struct ifreq ifr;
271-
if (_DoRequest(SIOCGIFFLAGS, ifr) != 0)
272-
return "";
273-
274-
return (ifr.ifr_flags & IFF_UP) ? "Up" : "Down";
209+
return "";
275210
}
276211

277212

278213
bool
279214
NetworkInterface::IsLoopback() const
280215
{
281-
struct ifreq ifr;
282-
if (_DoRequest(SIOCGIFFLAGS, ifr) != 0)
283-
return "";
284-
285-
return ifr.ifr_flags & IFF_LOOPBACK;
216+
return false;
286217
}
287218

288219

289220
int
290221
NetworkInterface::_DoRequest(int request, struct ifreq& ifr) const
291222
{
292-
size_t ifNameLen = fName.size();
293-
::memcpy(ifr.ifr_name, fName.c_str(), ifNameLen);
294-
ifr.ifr_name[ifNameLen] = 0;
295-
296-
int fd = ::socket(AF_INET, SOCK_DGRAM, 0);
297-
if (fd == -1)
298-
return errno;
299-
300223
int status = 0;
301-
if (::ioctl(fd, request, &ifr) < 0)
302-
status = errno;
303-
304-
::close(fd);
305224

306225
return status;
307226
}
308227

309228

310-
const static int kBufSize = 8192;
311-
312-
static bool
313-
ParseRoutes(struct nlmsghdr* nlHdr, route_info* rtInfo,
314-
const char* interfaceName)
315-
{
316-
rtmsg* rtMsg = (rtmsg*)NLMSG_DATA(nlHdr);
317-
318-
// If the route is not for AF_INET or does not belong to main routing table then return.
319-
if ((rtMsg->rtm_family != AF_INET) || (rtMsg->rtm_table != RT_TABLE_MAIN))
320-
return false;
321-
322-
int rtLen = RTM_PAYLOAD(nlHdr);
323-
for (rtattr* rtAttr = (rtattr*)RTM_RTA(rtMsg);
324-
RTA_OK(rtAttr, rtLen);
325-
rtAttr = RTA_NEXT(rtAttr, rtLen)) {
326-
switch (rtAttr->rta_type) {
327-
case RTA_OIF:
328-
if_indextoname(*(int*)RTA_DATA(rtAttr), rtInfo->ifName);
329-
break;
330-
case RTA_GATEWAY:
331-
rtInfo->gateway = *(in_addr*)RTA_DATA(rtAttr);
332-
break;
333-
case RTA_PREFSRC:
334-
rtInfo->srcAddr = *(in_addr*)RTA_DATA(rtAttr);
335-
break;
336-
case RTA_DST:
337-
rtInfo->dstAddr = *(in_addr*)RTA_DATA(rtAttr);
338-
break;
339-
default:
340-
break;
341-
}
342-
}
343-
344-
// If the route is for a different interface, return false
345-
// TODO: Check if we can filter the list beforehand
346-
return ::strcmp(interfaceName, rtInfo->ifName) == 0;
347-
}
348-
349-
350-
static int
351-
ReadRouteInfoFromSocket(int sockFd, char *bufPtr, int seqNum, int pId)
352-
{
353-
nlmsghdr* nlHdr = NULL;
354-
int msgLen = 0;
355-
do {
356-
// Receive response from the kernel
357-
int readLen = 0;
358-
if ((readLen = ::recv(sockFd, bufPtr, kBufSize - msgLen, 0)) < 0)
359-
return -1;
360-
361-
nlHdr = (nlmsghdr*)bufPtr;
362-
363-
// Check if the header is valid
364-
if ((NLMSG_OK(nlHdr, readLen) == (int)0) ||
365-
(nlHdr->nlmsg_type == NLMSG_ERROR)) {
366-
return -1;
367-
}
368-
369-
// Check if the its the last message
370-
if (nlHdr->nlmsg_type == NLMSG_DONE) {
371-
break;
372-
} else {
373-
// Else move the pointer to buffer appropriately
374-
bufPtr += readLen;
375-
msgLen += readLen;
376-
}
377-
378-
// Check if its a multi part message
379-
if ((nlHdr->nlmsg_flags & NLM_F_MULTI) == 0)
380-
break;
381-
} while(((int)nlHdr->nlmsg_seq != seqNum) || ((int)nlHdr->nlmsg_pid != pId));
382-
383-
return msgLen;
384-
}
385229

386230

387231
int
388232
NetworkInterface::_GetRoutes(std::list<route_info>& routeList) const
389233
{
390-
int sock = 0;
391-
if ((sock = ::socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0)
392-
return errno;
393-
394-
char msgBuf[kBufSize];
395-
::memset(msgBuf, 0, kBufSize);
396-
struct nlmsghdr* nlMsg = (struct nlmsghdr*)msgBuf;
397-
398-
int msgSeq = 0;
399-
// Fill in the nlmsg header
400-
nlMsg->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
401-
nlMsg->nlmsg_type = RTM_GETROUTE;
402-
nlMsg->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
403-
nlMsg->nlmsg_seq = msgSeq++;
404-
nlMsg->nlmsg_pid = getpid();
405-
406-
if (::send(sock, nlMsg, nlMsg->nlmsg_len, 0) < 0) {
407-
::close(sock);
408-
return errno;
409-
}
410-
411-
// Read the response
412-
int len = 0;
413-
if ((len = ReadRouteInfoFromSocket(sock, msgBuf, msgSeq, getpid())) < 0) {
414-
::close(sock);
415-
return len;
416-
}
417-
418-
// Parse and add to list
419-
for (; NLMSG_OK(nlMsg, len) != (int)0; nlMsg = NLMSG_NEXT(nlMsg, len)) {
420-
route_info rtInfo;
421-
memset(&rtInfo, 0, sizeof(rtInfo));
422-
if (ParseRoutes(nlMsg, &rtInfo, Name().c_str()))
423-
routeList.push_back(rtInfo);
424-
}
425-
426-
::close(sock);
427-
428-
return routeList.size();
234+
return 0;
429235
}

NetworkRoster.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#define NETWORKROSTER_H_
1010

1111
#include <netinet/in.h>
12-
#include <net/route.h>
12+
//#include <net/route.h>
1313
#include <string>
1414
#include <vector>
1515

@@ -19,7 +19,7 @@ class NetworkRoster {
1919
NetworkRoster();
2020
~NetworkRoster();
2121

22-
int CountInterfaces(int family = AF_INET);
22+
int CountInterfaces(int family);
2323
int GetNextInterface(unsigned int* cookie, NetworkInterface& interface);
2424

2525
private:

Support.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ CommandExists(const char* command)
127127
int result = -1;
128128
int systemStatus = ::system(fullCommand.c_str());
129129
if (systemStatus == 0)
130-
result = WEXITSTATUS(systemStatus);
130+
result = 0;
131131

132132
return result == 0;
133133
}

backends/UnameBackend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ UnameBackend::Run()
4141
osInfo.fields["hostname"] = uName.nodename;
4242
osInfo.fields["comments"] = uName.version;
4343
osInfo.fields["release"] = uName.release;
44-
osInfo.fields["domainname"] = uName.domainname;
44+
//osInfo.fields["domainname"] = uName.domainname;
4545
osInfo.fields["architecture"] = uName.machine;
4646
osInfo.fields["kernelname"] = uName.sysname;
4747

0 commit comments

Comments
 (0)