* Fixed a memory leak in if_nameindex() - it never freed the buffer that
stored the interfaces from SIOCGIFCONF. * Fixed iteration over the ifreq structures; the returned structure per interface may not be equal to the size of ifreq. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@37802 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2006, Haiku, Inc. All Rights Reserved.
|
* Copyright 2006-2010, Haiku, Inc. All Rights Reserved.
|
||||||
* Distributed under the terms of the MIT License.
|
* Distributed under the terms of the MIT License.
|
||||||
*
|
*
|
||||||
* Authors:
|
* Authors:
|
||||||
@@ -14,6 +14,8 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <AutoDeleter.h>
|
||||||
|
|
||||||
|
|
||||||
namespace BPrivate {
|
namespace BPrivate {
|
||||||
class Socket {
|
class Socket {
|
||||||
@@ -41,7 +43,7 @@ namespace BPrivate {
|
|||||||
|
|
||||||
|
|
||||||
unsigned
|
unsigned
|
||||||
if_nametoindex(const char *name)
|
if_nametoindex(const char* name)
|
||||||
{
|
{
|
||||||
BPrivate::Socket socket;
|
BPrivate::Socket socket;
|
||||||
if (socket.FD() < 0)
|
if (socket.FD() < 0)
|
||||||
@@ -56,8 +58,8 @@ if_nametoindex(const char *name)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
char *
|
char*
|
||||||
if_indextoname(unsigned index, char *nameBuffer)
|
if_indextoname(unsigned index, char* nameBuffer)
|
||||||
{
|
{
|
||||||
BPrivate::Socket socket;
|
BPrivate::Socket socket;
|
||||||
if (socket.FD() < 0)
|
if (socket.FD() < 0)
|
||||||
@@ -74,7 +76,7 @@ if_indextoname(unsigned index, char *nameBuffer)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct if_nameindex *
|
struct if_nameindex*
|
||||||
if_nameindex(void)
|
if_nameindex(void)
|
||||||
{
|
{
|
||||||
BPrivate::Socket socket;
|
BPrivate::Socket socket;
|
||||||
@@ -93,19 +95,24 @@ if_nameindex(void)
|
|||||||
if (interfaces == NULL)
|
if (interfaces == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
MemoryDeleter deleter(interfaces);
|
||||||
|
|
||||||
config.ifc_len = count * sizeof(struct ifreq);
|
config.ifc_len = count * sizeof(struct ifreq);
|
||||||
config.ifc_req = interfaces;
|
config.ifc_req = interfaces;
|
||||||
if (ioctl(socket.FD(), SIOCGIFCONF, &config, sizeof(struct ifconf)) < 0)
|
if (ioctl(socket.FD(), SIOCGIFCONF, &config, sizeof(struct ifconf)) < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
struct if_nameindex *interfaceArray = (struct if_nameindex *)malloc(
|
struct if_nameindex* interfaceArray = (struct if_nameindex*)malloc(
|
||||||
sizeof(struct if_nameindex) * (count + 1));
|
sizeof(struct if_nameindex) * (count + 1));
|
||||||
if (interfaceArray == NULL)
|
if (interfaceArray == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
interfaceArray[i].if_index = interfaces[i].ifr_index;
|
interfaceArray[i].if_index = interfaces->ifr_index;
|
||||||
interfaceArray[i].if_name = strdup(interfaces[i].ifr_name);
|
interfaceArray[i].if_name = strdup(interfaces->ifr_name);
|
||||||
|
|
||||||
|
interfaces
|
||||||
|
= (ifreq*)((char*)interfaces + _SIZEOF_ADDR_IFREQ(interfaces[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
interfaceArray[count].if_index = 0;
|
interfaceArray[count].if_index = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user