From 41d4206692bec4352e7ea02ddb0222eab6b71884 Mon Sep 17 00:00:00 2001 From: Stefano Ceccherini Date: Mon, 18 Nov 2013 10:11:40 +0100 Subject: [PATCH] Added family parameter to the GetRoutes() API. Moved common code to a private file. --- headers/os/net/NetworkInterface.h | 4 +- headers/os/net/NetworkRoster.h | 3 +- headers/private/net/RouteSupport.h | 14 ++++ src/kits/network/libnetapi/Jamfile | 3 +- .../network/libnetapi/NetworkInterface.cpp | 68 ++--------------- src/kits/network/libnetapi/NetworkRoster.cpp | 58 +-------------- src/kits/network/libnetapi/RouteSupport.cpp | 74 +++++++++++++++++++ src/preferences/network/Settings.cpp | 2 +- 8 files changed, 103 insertions(+), 123 deletions(-) create mode 100644 headers/private/net/RouteSupport.h create mode 100644 src/kits/network/libnetapi/RouteSupport.cpp diff --git a/headers/os/net/NetworkInterface.h b/headers/os/net/NetworkInterface.h index 5a96acea99..9e46cdb975 100644 --- a/headers/os/net/NetworkInterface.h +++ b/headers/os/net/NetworkInterface.h @@ -106,8 +106,8 @@ public: status_t RemoveRoute(int family, const route_entry& route); status_t RemoveDefaultRoute(int family); - status_t GetRoutes(BObjectList& routes) const; - status_t GetDefaultRoute(BNetworkAddress& gateway) const; + status_t GetRoutes(int family, BObjectList& routes) const; + status_t GetDefaultRoute(int family, BNetworkAddress& gateway) const; status_t AutoConfigure(int family); diff --git a/headers/os/net/NetworkRoster.h b/headers/os/net/NetworkRoster.h index 388b31b386..0e1fc56262 100644 --- a/headers/os/net/NetworkRoster.h +++ b/headers/os/net/NetworkRoster.h @@ -31,7 +31,8 @@ public: status_t RemoveInterface( const BNetworkInterface& interface); - status_t GetRoutes(BObjectList& routes) const; + status_t GetRoutes(int family, + BObjectList& routes) const; int32 CountPersistentNetworks() const; status_t GetNextPersistentNetwork(uint32* cookie, diff --git a/headers/private/net/RouteSupport.h b/headers/private/net/RouteSupport.h new file mode 100644 index 0000000000..b40c51801e --- /dev/null +++ b/headers/private/net/RouteSupport.h @@ -0,0 +1,14 @@ +/* + * Copyright 2013, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + */ + +#ifndef ROUTESUPPORT_H_ +#define ROUTESUPPORT_H_ + +#include + +status_t get_routes(const char* interfaceName, + int family, BObjectList& routes); + +#endif /* __ROUTESUPPORT_H_ */ diff --git a/src/kits/network/libnetapi/Jamfile b/src/kits/network/libnetapi/Jamfile index bea137c245..37d5272b54 100644 --- a/src/kits/network/libnetapi/Jamfile +++ b/src/kits/network/libnetapi/Jamfile @@ -45,7 +45,8 @@ for architectureObject in [ MultiArchSubDirSetup ] { NetworkDevice.cpp NetworkInterface.cpp NetworkRoster.cpp - + RouteSupport.cpp + AbstractSocket.cpp DatagramSocket.cpp Socket.cpp diff --git a/src/kits/network/libnetapi/NetworkInterface.cpp b/src/kits/network/libnetapi/NetworkInterface.cpp index 44959e2bde..51614eca1f 100644 --- a/src/kits/network/libnetapi/NetworkInterface.cpp +++ b/src/kits/network/libnetapi/NetworkInterface.cpp @@ -13,7 +13,7 @@ #include #include #include - +#include static int family_from_interface_address(const BNetworkInterfaceAddress& address) @@ -567,75 +567,17 @@ BNetworkInterface::RemoveDefaultRoute(int family) status_t -BNetworkInterface::GetRoutes(BObjectList& routes) const +BNetworkInterface::GetRoutes(int family, BObjectList& routes) const { - // TODO: Code duplication between this method - // and BNetworkRoster::GetRoutes(). Move code into - // common function - int socket = ::socket(AF_INET, SOCK_DGRAM, 0); - if (socket < 0) - return errno; - - FileDescriptorCloser fdCloser(socket); - - // Obtain gateway - ifconf config; - config.ifc_len = sizeof(config.ifc_value); - if (ioctl(socket, SIOCGRTSIZE, &config, sizeof(struct ifconf)) < 0) - return errno; - - uint32 size = (uint32)config.ifc_value; - if (size == 0) - return B_ERROR; - - void* buffer = malloc(size); - if (buffer == NULL) - return B_NO_MEMORY; - - MemoryDeleter bufferDeleter(buffer); - config.ifc_len = size; - config.ifc_buf = buffer; - - if (ioctl(socket, SIOCGRTTABLE, &config, sizeof(struct ifconf)) < 0) - return errno; - - ifreq* interface = (ifreq*)buffer; - ifreq* end = (ifreq*)((uint8*)buffer + size); - - while (interface < end) { - route_entry& route = interface->ifr_route; - if (!strcmp(interface->ifr_name, Name())) { - route_entry* newRoute = new (std::nothrow) route_entry; - if (newRoute == NULL) - return B_NO_MEMORY; - memcpy(newRoute, &interface->ifr_route, sizeof(route_entry)); - if (!routes.AddItem(newRoute)) { - delete newRoute; - return B_NO_MEMORY; - } - } - - int32 addressSize = 0; - if (route.destination != NULL) - addressSize += route.destination->sa_len; - if (route.mask != NULL) - addressSize += route.mask->sa_len; - if (route.gateway != NULL) - addressSize += route.gateway->sa_len; - - interface = (ifreq *)((addr_t)interface + IF_NAMESIZE - + sizeof(route_entry) + addressSize); - } - - return B_OK; + return get_routes(Name(), family, routes); } status_t -BNetworkInterface::GetDefaultRoute(BNetworkAddress& gateway) const +BNetworkInterface::GetDefaultRoute(int family, BNetworkAddress& gateway) const { BObjectList routes(1, true); - status_t status = GetRoutes(routes); + status_t status = GetRoutes(family, routes); if (status != B_OK) return status; diff --git a/src/kits/network/libnetapi/NetworkRoster.cpp b/src/kits/network/libnetapi/NetworkRoster.cpp index 22e5b86c23..cc23d64130 100644 --- a/src/kits/network/libnetapi/NetworkRoster.cpp +++ b/src/kits/network/libnetapi/NetworkRoster.cpp @@ -15,6 +15,7 @@ #include #include #include +#include // TODO: using AF_INET for the socket isn't really a smart idea, as one @@ -163,62 +164,9 @@ BNetworkRoster::RemoveInterface(const BNetworkInterface& interface) status_t -BNetworkRoster::GetRoutes(BObjectList& routes) const +BNetworkRoster::GetRoutes(int family, BObjectList& routes) const { - int socket = ::socket(AF_INET, SOCK_DGRAM, 0); - if (socket < 0) - return errno; - - FileDescriptorCloser fdCloser(socket); - - // Obtain gateway - ifconf config; - config.ifc_len = sizeof(config.ifc_value); - if (ioctl(socket, SIOCGRTSIZE, &config, sizeof(struct ifconf)) < 0) - return errno; - - uint32 size = (uint32)config.ifc_value; - if (size == 0) - return B_ERROR; - - void* buffer = malloc(size); - if (buffer == NULL) - return B_NO_MEMORY; - - MemoryDeleter bufferDeleter(buffer); - config.ifc_len = size; - config.ifc_buf = buffer; - - if (ioctl(socket, SIOCGRTTABLE, &config, sizeof(struct ifconf)) < 0) - return errno; - - ifreq* interface = (ifreq*)buffer; - ifreq* end = (ifreq*)((uint8*)buffer + size); - - while (interface < end) { - route_entry* route = new (std::nothrow) route_entry; - if (route == NULL) - return B_NO_MEMORY; - - memcpy(route, &interface->ifr_route, sizeof(route_entry)); - if (!routes.AddItem(route)) { - delete route; - return B_NO_MEMORY; - } - - int32 addressSize = 0; - if (route->destination != NULL) - addressSize += route->destination->sa_len; - if (route->mask != NULL) - addressSize += route->mask->sa_len; - if (route->gateway != NULL) - addressSize += route->gateway->sa_len; - - interface = (ifreq *)((addr_t)interface + IF_NAMESIZE - + sizeof(route_entry) + addressSize); - } - - return B_OK; + return get_routes(NULL, family, routes); } diff --git a/src/kits/network/libnetapi/RouteSupport.cpp b/src/kits/network/libnetapi/RouteSupport.cpp new file mode 100644 index 0000000000..85c2fd3d37 --- /dev/null +++ b/src/kits/network/libnetapi/RouteSupport.cpp @@ -0,0 +1,74 @@ +/* + * Copyright 2013, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + */ + +#include +#include +#include + +#include +#include +#include + +status_t +get_routes(const char* interfaceName, int family, BObjectList& routes) +{ + int socket = ::socket(family, SOCK_DGRAM, 0); + if (socket < 0) + return errno; + + FileDescriptorCloser fdCloser(socket); + + // Obtain gateway + ifconf config; + config.ifc_len = sizeof(config.ifc_value); + if (ioctl(socket, SIOCGRTSIZE, &config, sizeof(struct ifconf)) < 0) + return errno; + + uint32 size = (uint32)config.ifc_value; + if (size == 0) + return B_ERROR; + + void* buffer = malloc(size); + if (buffer == NULL) + return B_NO_MEMORY; + + MemoryDeleter bufferDeleter(buffer); + config.ifc_len = size; + config.ifc_buf = buffer; + + if (ioctl(socket, SIOCGRTTABLE, &config, sizeof(struct ifconf)) < 0) + return errno; + + ifreq* interface = (ifreq*)buffer; + ifreq* end = (ifreq*)((uint8*)buffer + size); + + while (interface < end) { + route_entry& route = interface->ifr_route; + if (interfaceName == NULL + || !strcmp(interface->ifr_name, interfaceName)) { + route_entry* newRoute = new (std::nothrow) route_entry; + if (newRoute == NULL) + return B_NO_MEMORY; + memcpy(newRoute, &interface->ifr_route, sizeof(route_entry)); + if (!routes.AddItem(newRoute)) { + delete newRoute; + return B_NO_MEMORY; + } + } + + int32 addressSize = 0; + if (route.destination != NULL) + addressSize += route.destination->sa_len; + if (route.mask != NULL) + addressSize += route.mask->sa_len; + if (route.gateway != NULL) + addressSize += route.gateway->sa_len; + + interface = (ifreq *)((addr_t)interface + IF_NAMESIZE + + sizeof(route_entry) + addressSize); + } + + return B_OK; +} diff --git a/src/preferences/network/Settings.cpp b/src/preferences/network/Settings.cpp index 9c8c752bf7..9fb7ea2096 100644 --- a/src/preferences/network/Settings.cpp +++ b/src/preferences/network/Settings.cpp @@ -88,7 +88,7 @@ Settings::ReadConfiguration() fNetmask = address.Mask().ToString(); BNetworkAddress gatewayAddress; - if (interface.GetDefaultRoute(gatewayAddress) != B_OK) + if (interface.GetDefaultRoute(AF_INET, gatewayAddress) != B_OK) return; fGateway = gatewayAddress.ToString();