NetworkStatus: Use the network API
Use the Network API instead of querying the net_stack via socket.
This commit is contained in:
@@ -73,33 +73,6 @@ const uint32 kMinIconWidth = 16;
|
|||||||
const uint32 kMinIconHeight = 16;
|
const uint32 kMinIconHeight = 16;
|
||||||
|
|
||||||
|
|
||||||
class SocketOpener {
|
|
||||||
public:
|
|
||||||
SocketOpener()
|
|
||||||
{
|
|
||||||
fSocket = socket(AF_INET, SOCK_DGRAM, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
~SocketOpener()
|
|
||||||
{
|
|
||||||
close(fSocket);
|
|
||||||
}
|
|
||||||
|
|
||||||
status_t InitCheck()
|
|
||||||
{
|
|
||||||
return fSocket >= 0 ? B_OK : B_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
operator int() const
|
|
||||||
{
|
|
||||||
return fSocket;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
int fSocket;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// #pragma mark -
|
// #pragma mark -
|
||||||
|
|
||||||
|
|
||||||
@@ -352,57 +325,26 @@ NetworkStatusView::Draw(BRect updateRect)
|
|||||||
void
|
void
|
||||||
NetworkStatusView::_ShowConfiguration(BMessage* message)
|
NetworkStatusView::_ShowConfiguration(BMessage* message)
|
||||||
{
|
{
|
||||||
static const struct information_entry {
|
|
||||||
const char* label;
|
|
||||||
int32 control;
|
|
||||||
} kInformationEntries[] = {
|
|
||||||
{ B_TRANSLATE("Address"), SIOCGIFADDR },
|
|
||||||
{ B_TRANSLATE("Broadcast"), SIOCGIFBRDADDR },
|
|
||||||
{ B_TRANSLATE("Netmask"), SIOCGIFNETMASK },
|
|
||||||
{ NULL }
|
|
||||||
};
|
|
||||||
|
|
||||||
SocketOpener socket;
|
|
||||||
if (socket.InitCheck() != B_OK)
|
|
||||||
return;
|
|
||||||
|
|
||||||
const char* name;
|
const char* name;
|
||||||
if (message->FindString("interface", &name) != B_OK)
|
if (message->FindString("interface", &name) != B_OK)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
ifreq request;
|
BNetworkInterface networkInterface(name);
|
||||||
if (!_PrepareRequest(request, name))
|
if (!networkInterface.Exists())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
BNetworkInterfaceAddress address;
|
||||||
|
networkInterface.GetAddressAt(0, address);
|
||||||
|
// TODO: We should get all addresses,
|
||||||
|
// not just the first one.
|
||||||
BString text(B_TRANSLATE("%ifaceName information:\n"));
|
BString text(B_TRANSLATE("%ifaceName information:\n"));
|
||||||
text.ReplaceFirst("%ifaceName", name);
|
text.ReplaceFirst("%ifaceName", name);
|
||||||
|
|
||||||
size_t boldLength = text.Length();
|
size_t boldLength = text.Length();
|
||||||
|
|
||||||
for (int i = 0; kInformationEntries[i].label; i++) {
|
text << "\n" << B_TRANSLATE("Address") << ": " << address.Address().ToString();
|
||||||
if (ioctl(socket, kInformationEntries[i].control, &request,
|
text << "\n" << B_TRANSLATE("Broadcast") << ": " << address.Broadcast().ToString();
|
||||||
sizeof(request)) < 0) {
|
text << "\n" << B_TRANSLATE("Netmask") << ": " << address.Mask().ToString();
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
char address[32];
|
|
||||||
sockaddr_in* inetAddress = NULL;
|
|
||||||
switch (kInformationEntries[i].control) {
|
|
||||||
case SIOCGIFNETMASK:
|
|
||||||
inetAddress = (sockaddr_in*)&request.ifr_mask;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
inetAddress = (sockaddr_in*)&request.ifr_addr;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inet_ntop(AF_INET, &inetAddress->sin_addr, address,
|
|
||||||
sizeof(address)) == NULL) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
text << "\n" << kInformationEntries[i].label << ": " << address;
|
|
||||||
}
|
|
||||||
|
|
||||||
BAlert* alert = new BAlert(name, text.String(), B_TRANSLATE("OK"));
|
BAlert* alert = new BAlert(name, text.String(), B_TRANSLATE("OK"));
|
||||||
alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
|
alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
|
||||||
@@ -523,17 +465,6 @@ NetworkStatusView::_AboutRequested()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool
|
|
||||||
NetworkStatusView::_PrepareRequest(struct ifreq& request, const char* name)
|
|
||||||
{
|
|
||||||
if (strlen(name) > IF_NAMESIZE)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
strcpy(request.ifr_name, name);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int32
|
int32
|
||||||
NetworkStatusView::_DetermineInterfaceStatus(
|
NetworkStatusView::_DetermineInterfaceStatus(
|
||||||
const BNetworkInterface& interface)
|
const BNetworkInterface& interface)
|
||||||
|
|||||||
@@ -56,8 +56,6 @@ class NetworkStatusView : public BView {
|
|||||||
void _Init();
|
void _Init();
|
||||||
void _UpdateBitmaps();
|
void _UpdateBitmaps();
|
||||||
void _ShowConfiguration(BMessage* message);
|
void _ShowConfiguration(BMessage* message);
|
||||||
bool _PrepareRequest(struct ifreq& request,
|
|
||||||
const char* name);
|
|
||||||
int32 _DetermineInterfaceStatus(
|
int32 _DetermineInterfaceStatus(
|
||||||
const BNetworkInterface& interface);
|
const BNetworkInterface& interface);
|
||||||
void _Update(bool force = false);
|
void _Update(bool force = false);
|
||||||
|
|||||||
Reference in New Issue
Block a user