Use the network kit api instead of talking to the netstack directly.

Use BNetworkInterface and BNetworkInterfaceAddress in the Settings class
instead of using ioctls. This works for everything except the default
route, for which there is no API yet.
This commit is contained in:
Stefano Ceccherini
2013-04-21 15:45:38 +02:00
parent bf88d81ea6
commit be1038406b
2 changed files with 37 additions and 45 deletions
+37 -44
View File
@@ -26,6 +26,7 @@
#include <driver_settings.h> #include <driver_settings.h>
#include <File.h> #include <File.h>
#include <FindDirectory.h> #include <FindDirectory.h>
#include <NetworkInterface.h>
#include <Path.h> #include <Path.h>
#include <String.h> #include <String.h>
@@ -38,7 +39,6 @@ Settings::Settings(const char* name)
fDisabled(false), fDisabled(false),
fNameServers(5, true) fNameServers(5, true)
{ {
fSocket = socket(AF_INET, SOCK_DGRAM, 0);
fName = name; fName = name;
ReadConfiguration(); ReadConfiguration();
@@ -47,7 +47,6 @@ Settings::Settings(const char* name)
Settings::~Settings() Settings::~Settings()
{ {
close(fSocket);
} }
@@ -67,61 +66,38 @@ Settings::_PrepareRequest(struct ifreq& request)
} }
void static status_t
Settings::ReadConfiguration() GetDefaultGateway(BString& gateway)
{ {
ifreq request; // TODO: This method is here because BNetworkInterface
if (!_PrepareRequest(request)) // doesn't yet have any route getting methods
return;
BString text = "dummy"; int socket = ::socket(AF_INET, SOCK_DGRAM, 0);
char address[32]; if (socket < 0)
sockaddr_in* inetAddress = NULL; return errno;
// Obtain IP. FileDescriptorCloser fdCloser(socket);
if (ioctl(fSocket, SIOCGIFADDR, &request, sizeof(request)) < 0)
return;
inetAddress = (sockaddr_in*)&request.ifr_addr;
if (inet_ntop(AF_INET, &inetAddress->sin_addr, address,
sizeof(address)) == NULL) {
return;
}
fIP = address;
// Obtain netmask.
if (ioctl(fSocket, SIOCGIFNETMASK, &request, sizeof(request)) < 0)
return;
inetAddress = (sockaddr_in*)&request.ifr_mask;
if (inet_ntop(AF_INET, &inetAddress->sin_addr, address,
sizeof(address)) == NULL) {
return;
}
fNetmask = address;
// Obtain gateway // Obtain gateway
ifconf config; ifconf config;
config.ifc_len = sizeof(config.ifc_value); config.ifc_len = sizeof(config.ifc_value);
if (ioctl(fSocket, SIOCGRTSIZE, &config, sizeof(struct ifconf)) < 0) if (ioctl(socket, SIOCGRTSIZE, &config, sizeof(struct ifconf)) < 0)
return; return errno;
uint32 size = (uint32)config.ifc_value; uint32 size = (uint32)config.ifc_value;
if (size == 0) if (size == 0)
return; return B_ERROR;
void* buffer = malloc(size); void* buffer = malloc(size);
if (buffer == NULL) if (buffer == NULL)
return; return B_NO_MEMORY;
MemoryDeleter bufferDeleter(buffer); MemoryDeleter bufferDeleter(buffer);
config.ifc_len = size; config.ifc_len = size;
config.ifc_buf = buffer; config.ifc_buf = buffer;
if (ioctl(fSocket, SIOCGRTTABLE, &config, sizeof(struct ifconf)) < 0) if (ioctl(socket, SIOCGRTTABLE, &config, sizeof(struct ifconf)) < 0)
return; return errno;
ifreq* interface = (ifreq*)buffer; ifreq* interface = (ifreq*)buffer;
ifreq* end = (ifreq*)((uint8*)buffer + size); ifreq* end = (ifreq*)((uint8*)buffer + size);
@@ -130,8 +106,8 @@ Settings::ReadConfiguration()
route_entry& route = interface->ifr_route; route_entry& route = interface->ifr_route;
if ((route.flags & RTF_GATEWAY) != 0) { if ((route.flags & RTF_GATEWAY) != 0) {
inetAddress = (sockaddr_in*)route.gateway; sockaddr_in* inetAddress = (sockaddr_in*)route.gateway;
fGateway = inet_ntoa(inetAddress->sin_addr); gateway = inet_ntoa(inetAddress->sin_addr);
} }
int32 addressSize = 0; int32 addressSize = 0;
@@ -146,9 +122,26 @@ Settings::ReadConfiguration()
+ sizeof(route_entry) + addressSize); + sizeof(route_entry) + addressSize);
} }
uint32 flags = 0; return B_OK;
if (ioctl(fSocket, SIOCGIFFLAGS, &request, sizeof(struct ifreq)) == 0) }
flags = request.ifr_flags;
void
Settings::ReadConfiguration()
{
BNetworkInterface interface(fName);
BNetworkInterfaceAddress address;
if (interface.GetAddressAt(0, address) != B_OK)
return;
fIP = address.Address().ToString();
fNetmask = address.Mask().ToString();
if (GetDefaultGateway(fGateway) != B_OK)
return;
uint32 flags = interface.Flags();
fAuto = (flags & (IFF_AUTO_CONFIGURED | IFF_CONFIGURING)) != 0; fAuto = (flags & (IFF_AUTO_CONFIGURED | IFF_CONFIGURING)) != 0;
fDisabled = (flags & IFF_UP) == 0; fDisabled = (flags & IFF_UP) == 0;
-1
View File
@@ -55,7 +55,6 @@ private:
BString fNetmask; BString fNetmask;
BString fName; BString fName;
BString fDomain; BString fDomain;
int fSocket;
bool fAuto; bool fAuto;
bool fDisabled; bool fDisabled;
BObjectList<BString> fNameServers; BObjectList<BString> fNameServers;