From 2087545cc057c6c96cf1b14736e58ba4e4012d4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Tue, 23 Nov 2010 21:03:11 +0000 Subject: [PATCH] * 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 --- headers/os/net/NetworkInterface.h | 9 ++ src/kits/network/libnetapi/Jamfile | 1 + .../network/libnetapi/NetworkInterface.cpp | 107 ++++++++++++++++++ 3 files changed, 117 insertions(+) diff --git a/headers/os/net/NetworkInterface.h b/headers/os/net/NetworkInterface.h index 431b71c183..db9192681f 100644 --- a/headers/os/net/NetworkInterface.h +++ b/headers/os/net/NetworkInterface.h @@ -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; diff --git a/src/kits/network/libnetapi/Jamfile b/src/kits/network/libnetapi/Jamfile index aa7280bc79..73106e157b 100644 --- a/src/kits/network/libnetapi/Jamfile +++ b/src/kits/network/libnetapi/Jamfile @@ -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 diff --git a/src/kits/network/libnetapi/NetworkInterface.cpp b/src/kits/network/libnetapi/NetworkInterface.cpp index fce99cdd25..05c45ea207 100644 --- a/src/kits/network/libnetapi/NetworkInterface.cpp +++ b/src/kits/network/libnetapi/NetworkInterface.cpp @@ -11,6 +11,9 @@ #include #include +#include + +#include 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(&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; +}