Skip to content

Commit 0d17f5f

Browse files
committed
Read and parse all of /proc/net/dev_mcast
Reading from Linux procfs with a single read() does not guarantee reading of the full buffer length.
1 parent 6764857 commit 0d17f5f

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

osdep/LinuxEthernetTap.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <ctype.h>
2727
#include <errno.h>
2828
#include <fcntl.h>
29+
#include <fstream>
2930
#include <ifaddrs.h>
3031
#include <linux/if.h>
3132
#include <linux/if_addr.h>
@@ -524,32 +525,30 @@ void LinuxEthernetTap::setFriendlyName(const char* friendlyName)
524525

525526
void LinuxEthernetTap::scanMulticastGroups(std::vector<MulticastGroup>& added, std::vector<MulticastGroup>& removed)
526527
{
527-
char *ptr, *ptr2;
528-
unsigned char mac[6];
529528
std::vector<MulticastGroup> newGroups;
530529

531-
int fd = ::open("/proc/net/dev_mcast", O_RDONLY);
532-
if (fd > 0) {
533-
char buf[131072];
534-
int n = (int)::read(fd, buf, sizeof(buf));
535-
if ((n > 0) && (n < (int)sizeof(buf))) {
536-
buf[n] = (char)0;
537-
for (char* l = strtok_r(buf, "\r\n", &ptr); (l); l = strtok_r((char*)0, "\r\n", &ptr)) {
538-
int fno = 0;
539-
char* devname = (char*)0;
540-
char* mcastmac = (char*)0;
541-
for (char* f = strtok_r(l, " \t", &ptr2); (f); f = strtok_r((char*)0, " \t", &ptr2)) {
542-
if (fno == 1)
543-
devname = f;
544-
else if (fno == 4)
545-
mcastmac = f;
546-
++fno;
547-
}
548-
if ((devname) && (! strcmp(devname, _dev.c_str())) && (mcastmac) && (Utils::unhex(mcastmac, mac, 6) == 6))
549-
newGroups.push_back(MulticastGroup(MAC(mac, 6), 0));
550-
}
530+
std::ifstream ifs("/proc/net/dev_mcast");
531+
while (ifs.good()) {
532+
char l[256];
533+
534+
ifs.getline(l, sizeof(l));
535+
if (l[0] == '\0')
536+
continue;
537+
538+
int fno = 0;
539+
unsigned char mac[6];
540+
char* ptr;
541+
char* devname = (char*)0;
542+
char* mcastmac = (char*)0;
543+
for (char* f = strtok_r(l, " \t", &ptr); (f); f = strtok_r((char*)0, " \t", &ptr)) {
544+
if (fno == 1)
545+
devname = f;
546+
else if (fno == 4)
547+
mcastmac = f;
548+
++fno;
551549
}
552-
::close(fd);
550+
if ((devname) && (! strcmp(devname, _dev.c_str())) && (mcastmac) && (Utils::unhex(mcastmac, mac, 6) == 6))
551+
newGroups.push_back(MulticastGroup(MAC(mac, 6), 0));
553552
}
554553

555554
std::vector<InetAddress> allIps(ips());

0 commit comments

Comments
 (0)