* Added methods to manipulate routes.

* Added AutoConfigure() method.
* Implemented missing Index() method.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@39592 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2010-11-23 21:03:11 +00:00
parent 2977e7888f
commit 2087545cc0
3 changed files with 117 additions and 0 deletions

View File

@ -100,6 +100,15 @@ public:
status_t GetHardwareAddress(BNetworkAddress& address);
status_t AddRoute(const route_entry& route);
status_t AddDefaultRoute(const BNetworkAddress& gateway);
status_t RemoveRoute(const route_entry& route);
status_t RemoveRoute(int family,
const route_entry& route);
status_t RemoveDefaultRoute(int family);
status_t AutoConfigure(int family);
private:
char fName[IF_NAMESIZE];
BList fAddresses;

View File

@ -5,6 +5,7 @@ UsePrivateHeaders net shared ;
UseHeaders [ FDirName $(HAIKU_TOP) src libs compat freebsd_network compat ]
: true ;
UseHeaders [ FDirName $(HAIKU_TOP) src libs compat freebsd_wlan ] : true ;
UseHeaders [ FDirName $(HAIKU_TOP) src servers net ] : true ;
SharedLibrary libbnetapi.so :
init.cpp

View File

@ -11,6 +11,9 @@
#include <sys/sockio.h>
#include <AutoDeleter.h>
#include <Messenger.h>
#include <NetServer.h>
static int
@ -27,6 +30,22 @@ family_from_interface_address(const BNetworkInterfaceAddress& address)
}
static int
family_from_route(const route_entry& route)
{
if (route.destination != NULL && route.destination->sa_family != AF_UNSPEC)
return route.destination->sa_family;
if (route.mask != NULL && route.mask->sa_family != AF_UNSPEC)
return route.mask->sa_family;
if (route.gateway != NULL && route.gateway->sa_family != AF_UNSPEC)
return route.gateway->sa_family;
if (route.source != NULL && route.source->sa_family != AF_UNSPEC)
return route.source->sa_family;
return AF_UNSPEC;
}
static status_t
do_ifaliasreq(const char* name, int32 option, BNetworkInterfaceAddress& address,
bool readBack = false)
@ -223,6 +242,17 @@ BNetworkInterface::Name() const
}
uint32
BNetworkInterface::Index() const
{
ifreq request;
if (do_request(AF_INET, request, Name(), SIOCGIFINDEX) != B_OK)
return 0;
return request.ifr_index;
}
uint32
BNetworkInterface::Flags() const
{
@ -479,3 +509,80 @@ BNetworkInterface::GetHardwareAddress(BNetworkAddress& address)
address.SetTo(request.ifr_addr);
return B_OK;
}
status_t
BNetworkInterface::AddRoute(const route_entry& route)
{
int family = family_from_route(route);
if (family == AF_UNSPEC)
return B_BAD_VALUE;
ifreq request;
request.ifr_route = route;
return do_request(family, request, Name(), SIOCADDRT);
}
status_t
BNetworkInterface::AddDefaultRoute(const BNetworkAddress& gateway)
{
route_entry route;
memset(&route, 0, sizeof(route_entry));
route.flags = RTF_STATIC | RTF_DEFAULT | RTF_GATEWAY;
route.gateway = const_cast<sockaddr*>(&gateway.SockAddr());
return AddRoute(route);
}
status_t
BNetworkInterface::RemoveRoute(const route_entry& route)
{
int family = family_from_route(route);
if (family == AF_UNSPEC)
return B_BAD_VALUE;
return RemoveRoute(family, route);
}
status_t
BNetworkInterface::RemoveRoute(int family, const route_entry& route)
{
ifreq request;
request.ifr_route = route;
return do_request(family, request, Name(), SIOCDELRT);
}
status_t
BNetworkInterface::RemoveDefaultRoute(int family)
{
route_entry route;
memset(&route, 0, sizeof(route_entry));
route.flags = RTF_STATIC | RTF_DEFAULT;
return RemoveRoute(family, route);
}
status_t
BNetworkInterface::AutoConfigure(int family)
{
BMessage message(kMsgConfigureInterface);
message.AddString("device", Name());
BMessage address;
address.AddInt32("family", family);
address.AddBool("auto_config", true);
message.AddMessage("address", &address);
BMessenger networkServer(kNetServerSignature);
BMessage reply;
status_t status = networkServer.SendMessage(&message, &reply);
if (status == B_OK)
reply.FindInt32("status", &status);
return status;
}