Make NetworkSettings more friendly to network family (IPv4 and IPv6); Start using BNetworkAddress instead of BString for address storage; NetworkSettings will handle setting and reading current interface settings, please excuse the mess atm in NetworkSettings.h

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@40392 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Alexander von Gluck IV 2011-02-08 17:47:39 +00:00
parent 435492aefc
commit 1b4f0abf9d
4 changed files with 122 additions and 93 deletions

View File

@ -52,16 +52,6 @@ our_image(image_info& image)
}
/* Takes the provided ip-style netmask and converts
* it to an unsigned int, then counts the binary 1's
*/
int
netmaskbitcount(const char* buf)
{
return(PopCount(inet_addr(buf)));
}
// #pragma mark -
@ -107,6 +97,9 @@ InterfaceListItem::DrawItem(BView* owner, BRect /*bounds*/, bool complete)
BBitmap* stateIcon(NULL);
BString interfaceState;
BNetworkAddress addrIPv4 = fSettings->GetAddr(AF_INET);
BNetworkAddress addrIPv6 = fSettings->GetAddr(AF_INET6);
if (!list)
return;
@ -133,7 +126,8 @@ InterfaceListItem::DrawItem(BView* owner, BRect /*bounds*/, bool complete)
} else if (!fInterface.HasLink()) {
interfaceState << "no link";
stateIcon = fIconOffline;
} else if (!fSettings->IP() && fSettings->AutoConfigure()) {
} else if (addrIPv4.IsEmpty() && addrIPv6.IsEmpty()
&& fSettings->AutoConfigure()) {
interfaceState << "connecting" B_UTF8_ELLIPSIS;
stateIcon = fIconPending;
} else {
@ -179,19 +173,28 @@ InterfaceListItem::DrawItem(BView* owner, BRect /*bounds*/, bool complete)
list->DrawString(interfaceState, statePt);
if (!fSettings->IsDisabled()) {
// Render IPv4 Address
BString v4str("IPv4: ");
v4str << fSettings->IP();
v4str << " /";
v4str << netmaskbitcount(fSettings->Netmask());
v4str << " (";
if (addrIPv4.IsEmpty())
v4str << "none";
else {
v4str << addrIPv4.ToString();
}
if (fSettings->AutoConfigure())
v4str << "DHCP";
v4str << " (DHCP)";
else
v4str << "manual";
v4str << ")";
v4str << " (manual)";
list->DrawString(v4str.String(), v4addrPt);
list->DrawString("IPv6: none (auto)", v6addrPt);
// Render IPv6 Address (if present)
if (!addrIPv6.IsEmpty()) {
BString v6str("IPv6: ");
v6str << addrIPv6.ToString();
list->DrawString(v6str, v6addrPt);
}
}
owner->PopState();

View File

@ -27,29 +27,6 @@
#include "NetworkSettings.h"
/* GCC 3.4 and onward have a built-in "population count"
* feature that takes advantage of a cpu instruction
*/
// TODO : remove when access to BNetworkAddress is complete
#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
# define PopCount(buffer) __builtin_popcount(buffer)
#else
/* On < GCC 3.4 we do it the old fashon way */
int PopCount(unsigned buf)
{
buf = buf - ((buf >> 1) & 0x55555555);
buf = (buf & 0x33333333) + ((buf >> 2) & 0x33333333);
buf = (buf + (buf >> 4)) & 0x0F0F0F0F;
buf = buf + (buf >> 8);
buf = buf + (buf >> 16);
return buf & 0x0000003F;
}
#endif
int netmaskbitcount(const char* buf);
class InterfaceListItem : public BListItem {
public:
InterfaceListItem(const char* name);

View File

@ -1,11 +1,12 @@
/*
* Copyright 2004-2010 Haiku Inc. All rights reserved.
* Copyright 2004-2011 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Andre Alves Garzia, andre@andregarzia.com
* Axel Dörfler, axeld@pinc-software.de.
* Vegard Wærp, vegarwa@online.no
* Alexander von Gluck, kallisti5@unixzen.com
*/
@ -38,7 +39,8 @@ NetworkSettings::NetworkSettings(const char* name)
fDisabled(false),
fNameServers(5, true)
{
fSocket = socket(AF_INET, SOCK_DGRAM, 0);
fSocket4 = socket(AF_INET, SOCK_DGRAM, 0);
fSocket6 = socket(AF_INET6, SOCK_DGRAM, 0);
fName = name;
@ -48,7 +50,8 @@ NetworkSettings::NetworkSettings(const char* name)
NetworkSettings::~NetworkSettings()
{
close(fSocket);
close(fSocket4);
close(fSocket6);
}
@ -71,45 +74,39 @@ NetworkSettings::_PrepareRequest(struct ifreq& request)
void
NetworkSettings::ReadConfiguration()
{
ifreq request;
if (!_PrepareRequest(request))
ifreq ifReq;
sockaddr_in* inetAddress = NULL;
if (!_PrepareRequest(ifReq))
return;
BString text = "dummy";
char address[32];
sockaddr_in* inetAddress = NULL;
// Obtain IP.
if (ioctl(fSocket, SIOCGIFADDR, &request, sizeof(request)) < 0)
// Obtain IPv4 address.
if (ioctl(fSocket4, SIOCGIFADDR, &ifReq, sizeof(ifReq)) < 0)
return;
fIPv4Addr = *((sockaddr_in *)(&(ifReq.ifr_addr)));
inetAddress = (sockaddr_in*)&request.ifr_addr;
if (inet_ntop(AF_INET, &inetAddress->sin_addr, address,
sizeof(address)) == NULL) {
// Obtain IPv4 netmask
if (ioctl(fSocket4, SIOCGIFNETMASK, &ifReq, sizeof(ifReq)) < 0)
return;
}
fIPv4Mask = *((sockaddr_in *)(&(ifReq.ifr_mask)));
fIP = address;
// Obtain netmask.
if (ioctl(fSocket, SIOCGIFNETMASK, &request, sizeof(request)) < 0)
// Obtain IPv6 address.
if (ioctl(fSocket6, SIOCGIFADDR, &ifReq, sizeof(ifReq)) < 0)
return;
fIPv6Addr = *((sockaddr_in6 *)(&(ifReq.ifr_addr)));
inetAddress = (sockaddr_in*)&request.ifr_mask;
if (inet_ntop(AF_INET, &inetAddress->sin_addr, address,
sizeof(address)) == NULL) {
// Obtain IPv6 netmask
if (ioctl(fSocket6, SIOCGIFNETMASK, &ifReq, sizeof(ifReq)) < 0)
return;
}
fNetmask = address;
fIPv6Mask = *((sockaddr_in6 *)(&(ifReq.ifr_mask)));
// Obtain gateway
ifconf config;
config.ifc_len = sizeof(config.ifc_value);
if (ioctl(fSocket, SIOCGRTSIZE, &config, sizeof(struct ifconf)) < 0)
ifconf ifCfg;
ifCfg.ifc_len = sizeof(ifCfg.ifc_value);
if (ioctl(fSocket4, SIOCGRTSIZE, &ifCfg, sizeof(ifCfg)) < 0)
return;
uint32 size = (uint32)config.ifc_value;
uint32 size = (uint32)ifCfg.ifc_value;
if (size == 0)
return;
@ -118,10 +115,10 @@ NetworkSettings::ReadConfiguration()
return;
MemoryDeleter bufferDeleter(buffer);
config.ifc_len = size;
config.ifc_buf = buffer;
ifCfg.ifc_len = size;
ifCfg.ifc_buf = buffer;
if (ioctl(fSocket, SIOCGRTTABLE, &config, sizeof(struct ifconf)) < 0)
if (ioctl(fSocket4, SIOCGRTTABLE, &ifCfg, sizeof(ifCfg)) < 0)
return;
ifreq* interface = (ifreq*)buffer;
@ -148,8 +145,8 @@ NetworkSettings::ReadConfiguration()
}
uint32 flags = 0;
if (ioctl(fSocket, SIOCGIFFLAGS, &request, sizeof(struct ifreq)) == 0)
flags = request.ifr_flags;
if (ioctl(fSocket4, SIOCGIFFLAGS, &ifReq, sizeof(ifReq)) == 0)
flags = ifReq.ifr_flags;
fAuto = (flags & (IFF_AUTO_CONFIGURED | IFF_CONFIGURING)) != 0;
fDisabled = (flags & IFF_UP) == 0;
@ -219,3 +216,44 @@ NetworkSettings::ReadConfiguration()
fDomain = state->dnsrch[0];
}
}
BNetworkAddress
NetworkSettings::GetAddr(int family)
{
if (family == AF_INET6)
return fIPv6Addr;
return fIPv4Addr;
}
const char*
NetworkSettings::GetIP(int family)
{
if (family == AF_INET6)
return fIPv6Addr.ToString();
return fIPv4Addr.ToString();
}
const char*
NetworkSettings::GetNetmask(int family)
{
if (family == AF_INET6)
return fIPv6Mask.ToString();
return fIPv4Mask.ToString();
}
int32
NetworkSettings::GetPrefixLen(int family)
{
if (family == AF_INET6)
return fIPv6Mask.PrefixLength();
return fIPv4Mask.PrefixLength();
}

View File

@ -10,6 +10,8 @@
#define SETTINGS_H
#include <NetworkDevice.h>
#include <NetworkInterface.h>
#include <ObjectList.h>
#include <String.h>
@ -19,24 +21,28 @@ public:
NetworkSettings(const char* name);
virtual ~NetworkSettings();
void SetIP(const BString& ip)
{ fIP = ip; }
void SetGateway(const BString& ip)
{ fGateway = ip; }
void SetNetmask(const BString& ip)
{ fNetmask = ip; }
void SetDomain(const BString& domain)
{ fDomain = domain; }
void SetAutoConfigure(bool autoConfigure)
{ fAuto = autoConfigure; }
// void SetIP(const BString& ip)
// { fIP = ip; }
// void SetGateway(const BString& ip)
// { fGateway = ip; }
// void SetNetmask(const BString& ip)
// { fNetmask = ip; }
// void SetDomain(const BString& domain)
// { fDomain = domain; }
// void SetAutoConfigure(bool autoConfigure)
// { fAuto = autoConfigure; }
void SetDisabled(bool disabled)
{ fDisabled = disabled; }
void SetWirelessNetwork(const char* name)
{ fWirelessNetwork.SetTo(name); }
// void SetWirelessNetwork(const char* name)
// { fWirelessNetwork.SetTo(name); }
BNetworkAddress GetAddr(int family);
const char* GetIP(int family);
const char* GetNetmask(int family);
int32 GetPrefixLen(int family);
const char* IP() { return fIP.String(); }
const char* Gateway() { return fGateway.String(); }
const char* Netmask() { return fNetmask.String(); }
const char* Name() { return fName.String(); }
const char* Domain() { return fDomain.String(); }
bool AutoConfigure() { return fAuto; }
@ -50,12 +56,17 @@ public:
private:
bool _PrepareRequest(struct ifreq& request);
BString fIP;
int fSocket4;
int fSocket6;
BNetworkAddress fIPv4Addr;
BNetworkAddress fIPv4Mask;
BNetworkAddress fIPv6Addr;
BNetworkAddress fIPv6Mask;
BString fGateway;
BString fNetmask;
BString fName;
BString fDomain;
int fSocket;
bool fAuto;
bool fDisabled;
BObjectList<BString> fNameServers;