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
@@ -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 - // #pragma mark -
@@ -107,6 +97,9 @@ InterfaceListItem::DrawItem(BView* owner, BRect /*bounds*/, bool complete)
BBitmap* stateIcon(NULL); BBitmap* stateIcon(NULL);
BString interfaceState; BString interfaceState;
BNetworkAddress addrIPv4 = fSettings->GetAddr(AF_INET);
BNetworkAddress addrIPv6 = fSettings->GetAddr(AF_INET6);
if (!list) if (!list)
return; return;
@@ -133,7 +126,8 @@ InterfaceListItem::DrawItem(BView* owner, BRect /*bounds*/, bool complete)
} else if (!fInterface.HasLink()) { } else if (!fInterface.HasLink()) {
interfaceState << "no link"; interfaceState << "no link";
stateIcon = fIconOffline; stateIcon = fIconOffline;
} else if (!fSettings->IP() && fSettings->AutoConfigure()) { } else if (addrIPv4.IsEmpty() && addrIPv6.IsEmpty()
&& fSettings->AutoConfigure()) {
interfaceState << "connecting" B_UTF8_ELLIPSIS; interfaceState << "connecting" B_UTF8_ELLIPSIS;
stateIcon = fIconPending; stateIcon = fIconPending;
} else { } else {
@@ -179,19 +173,28 @@ InterfaceListItem::DrawItem(BView* owner, BRect /*bounds*/, bool complete)
list->DrawString(interfaceState, statePt); list->DrawString(interfaceState, statePt);
if (!fSettings->IsDisabled()) { if (!fSettings->IsDisabled()) {
// Render IPv4 Address
BString v4str("IPv4: "); BString v4str("IPv4: ");
v4str << fSettings->IP();
v4str << " /"; if (addrIPv4.IsEmpty())
v4str << netmaskbitcount(fSettings->Netmask()); v4str << "none";
v4str << " ("; else {
v4str << addrIPv4.ToString();
}
if (fSettings->AutoConfigure()) if (fSettings->AutoConfigure())
v4str << "DHCP"; v4str << " (DHCP)";
else else
v4str << "manual"; v4str << " (manual)";
v4str << ")";
list->DrawString(v4str.String(), v4addrPt); 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(); owner->PopState();
@@ -27,29 +27,6 @@
#include "NetworkSettings.h" #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 { class InterfaceListItem : public BListItem {
public: public:
InterfaceListItem(const char* name); InterfaceListItem(const char* name);
@@ -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. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
* Andre Alves Garzia, [email protected] * Andre Alves Garzia, [email protected]
* Axel Dörfler, [email protected]. * Axel Dörfler, [email protected].
* Vegard Wærp, [email protected] * Vegard Wærp, [email protected]
* Alexander von Gluck, [email protected]
*/ */
@@ -38,7 +39,8 @@ NetworkSettings::NetworkSettings(const char* name)
fDisabled(false), fDisabled(false),
fNameServers(5, true) 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; fName = name;
@@ -48,7 +50,8 @@ NetworkSettings::NetworkSettings(const char* name)
NetworkSettings::~NetworkSettings() NetworkSettings::~NetworkSettings()
{ {
close(fSocket); close(fSocket4);
close(fSocket6);
} }
@@ -71,45 +74,39 @@ NetworkSettings::_PrepareRequest(struct ifreq& request)
void void
NetworkSettings::ReadConfiguration() NetworkSettings::ReadConfiguration()
{ {
ifreq request; ifreq ifReq;
if (!_PrepareRequest(request)) sockaddr_in* inetAddress = NULL;
if (!_PrepareRequest(ifReq))
return; return;
BString text = "dummy"; // Obtain IPv4 address.
char address[32]; if (ioctl(fSocket4, SIOCGIFADDR, &ifReq, sizeof(ifReq)) < 0)
sockaddr_in* inetAddress = NULL;
// Obtain IP.
if (ioctl(fSocket, SIOCGIFADDR, &request, sizeof(request)) < 0)
return; return;
fIPv4Addr = *((sockaddr_in *)(&(ifReq.ifr_addr)));
inetAddress = (sockaddr_in*)&request.ifr_addr; // Obtain IPv4 netmask
if (inet_ntop(AF_INET, &inetAddress->sin_addr, address, if (ioctl(fSocket4, SIOCGIFNETMASK, &ifReq, sizeof(ifReq)) < 0)
sizeof(address)) == NULL) {
return; return;
} fIPv4Mask = *((sockaddr_in *)(&(ifReq.ifr_mask)));
fIP = address; // Obtain IPv6 address.
if (ioctl(fSocket6, SIOCGIFADDR, &ifReq, sizeof(ifReq)) < 0)
// Obtain netmask.
if (ioctl(fSocket, SIOCGIFNETMASK, &request, sizeof(request)) < 0)
return; return;
fIPv6Addr = *((sockaddr_in6 *)(&(ifReq.ifr_addr)));
inetAddress = (sockaddr_in*)&request.ifr_mask; // Obtain IPv6 netmask
if (inet_ntop(AF_INET, &inetAddress->sin_addr, address, if (ioctl(fSocket6, SIOCGIFNETMASK, &ifReq, sizeof(ifReq)) < 0)
sizeof(address)) == NULL) {
return; return;
} fIPv6Mask = *((sockaddr_in6 *)(&(ifReq.ifr_mask)));
fNetmask = address;
// Obtain gateway // Obtain gateway
ifconf config; ifconf ifCfg;
config.ifc_len = sizeof(config.ifc_value); ifCfg.ifc_len = sizeof(ifCfg.ifc_value);
if (ioctl(fSocket, SIOCGRTSIZE, &config, sizeof(struct ifconf)) < 0) if (ioctl(fSocket4, SIOCGRTSIZE, &ifCfg, sizeof(ifCfg)) < 0)
return; return;
uint32 size = (uint32)config.ifc_value; uint32 size = (uint32)ifCfg.ifc_value;
if (size == 0) if (size == 0)
return; return;
@@ -118,10 +115,10 @@ NetworkSettings::ReadConfiguration()
return; return;
MemoryDeleter bufferDeleter(buffer); MemoryDeleter bufferDeleter(buffer);
config.ifc_len = size; ifCfg.ifc_len = size;
config.ifc_buf = buffer; ifCfg.ifc_buf = buffer;
if (ioctl(fSocket, SIOCGRTTABLE, &config, sizeof(struct ifconf)) < 0) if (ioctl(fSocket4, SIOCGRTTABLE, &ifCfg, sizeof(ifCfg)) < 0)
return; return;
ifreq* interface = (ifreq*)buffer; ifreq* interface = (ifreq*)buffer;
@@ -148,8 +145,8 @@ NetworkSettings::ReadConfiguration()
} }
uint32 flags = 0; uint32 flags = 0;
if (ioctl(fSocket, SIOCGIFFLAGS, &request, sizeof(struct ifreq)) == 0) if (ioctl(fSocket4, SIOCGIFFLAGS, &ifReq, sizeof(ifReq)) == 0)
flags = request.ifr_flags; flags = ifReq.ifr_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;
@@ -219,3 +216,44 @@ NetworkSettings::ReadConfiguration()
fDomain = state->dnsrch[0]; 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();
}
@@ -10,6 +10,8 @@
#define SETTINGS_H #define SETTINGS_H
#include <NetworkDevice.h>
#include <NetworkInterface.h>
#include <ObjectList.h> #include <ObjectList.h>
#include <String.h> #include <String.h>
@@ -19,24 +21,28 @@ public:
NetworkSettings(const char* name); NetworkSettings(const char* name);
virtual ~NetworkSettings(); virtual ~NetworkSettings();
void SetIP(const BString& ip) // void SetIP(const BString& ip)
{ fIP = ip; } // { fIP = ip; }
void SetGateway(const BString& ip) // void SetGateway(const BString& ip)
{ fGateway = ip; } // { fGateway = ip; }
void SetNetmask(const BString& ip) // void SetNetmask(const BString& ip)
{ fNetmask = ip; } // { fNetmask = ip; }
void SetDomain(const BString& domain) // void SetDomain(const BString& domain)
{ fDomain = domain; } // { fDomain = domain; }
void SetAutoConfigure(bool autoConfigure) // void SetAutoConfigure(bool autoConfigure)
{ fAuto = autoConfigure; } // { fAuto = autoConfigure; }
void SetDisabled(bool disabled) void SetDisabled(bool disabled)
{ fDisabled = disabled; } { fDisabled = disabled; }
void SetWirelessNetwork(const char* name) // void SetWirelessNetwork(const char* name)
{ fWirelessNetwork.SetTo(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* Gateway() { return fGateway.String(); }
const char* Netmask() { return fNetmask.String(); }
const char* Name() { return fName.String(); } const char* Name() { return fName.String(); }
const char* Domain() { return fDomain.String(); } const char* Domain() { return fDomain.String(); }
bool AutoConfigure() { return fAuto; } bool AutoConfigure() { return fAuto; }
@@ -50,12 +56,17 @@ public:
private: private:
bool _PrepareRequest(struct ifreq& request); bool _PrepareRequest(struct ifreq& request);
BString fIP; int fSocket4;
int fSocket6;
BNetworkAddress fIPv4Addr;
BNetworkAddress fIPv4Mask;
BNetworkAddress fIPv6Addr;
BNetworkAddress fIPv6Mask;
BString fGateway; BString fGateway;
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;