Added family parameter to the GetRoutes() API.
Moved common code to a private file.
This commit is contained in:
parent
ba3f67bfb4
commit
41d4206692
|
@ -106,8 +106,8 @@ public:
|
||||||
status_t RemoveRoute(int family,
|
status_t RemoveRoute(int family,
|
||||||
const route_entry& route);
|
const route_entry& route);
|
||||||
status_t RemoveDefaultRoute(int family);
|
status_t RemoveDefaultRoute(int family);
|
||||||
status_t GetRoutes(BObjectList<route_entry>& routes) const;
|
status_t GetRoutes(int family, BObjectList<route_entry>& routes) const;
|
||||||
status_t GetDefaultRoute(BNetworkAddress& gateway) const;
|
status_t GetDefaultRoute(int family, BNetworkAddress& gateway) const;
|
||||||
|
|
||||||
status_t AutoConfigure(int family);
|
status_t AutoConfigure(int family);
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,8 @@ public:
|
||||||
status_t RemoveInterface(
|
status_t RemoveInterface(
|
||||||
const BNetworkInterface& interface);
|
const BNetworkInterface& interface);
|
||||||
|
|
||||||
status_t GetRoutes(BObjectList<route_entry>& routes) const;
|
status_t GetRoutes(int family,
|
||||||
|
BObjectList<route_entry>& routes) const;
|
||||||
|
|
||||||
int32 CountPersistentNetworks() const;
|
int32 CountPersistentNetworks() const;
|
||||||
status_t GetNextPersistentNetwork(uint32* cookie,
|
status_t GetNextPersistentNetwork(uint32* cookie,
|
||||||
|
|
|
@ -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 <ObjectList.h>
|
||||||
|
|
||||||
|
status_t get_routes(const char* interfaceName,
|
||||||
|
int family, BObjectList<route_entry>& routes);
|
||||||
|
|
||||||
|
#endif /* __ROUTESUPPORT_H_ */
|
|
@ -45,7 +45,8 @@ for architectureObject in [ MultiArchSubDirSetup ] {
|
||||||
NetworkDevice.cpp
|
NetworkDevice.cpp
|
||||||
NetworkInterface.cpp
|
NetworkInterface.cpp
|
||||||
NetworkRoster.cpp
|
NetworkRoster.cpp
|
||||||
|
RouteSupport.cpp
|
||||||
|
|
||||||
AbstractSocket.cpp
|
AbstractSocket.cpp
|
||||||
DatagramSocket.cpp
|
DatagramSocket.cpp
|
||||||
Socket.cpp
|
Socket.cpp
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
#include <AutoDeleter.h>
|
#include <AutoDeleter.h>
|
||||||
#include <Messenger.h>
|
#include <Messenger.h>
|
||||||
#include <NetServer.h>
|
#include <NetServer.h>
|
||||||
|
#include <RouteSupport.h>
|
||||||
|
|
||||||
static int
|
static int
|
||||||
family_from_interface_address(const BNetworkInterfaceAddress& address)
|
family_from_interface_address(const BNetworkInterfaceAddress& address)
|
||||||
|
@ -567,75 +567,17 @@ BNetworkInterface::RemoveDefaultRoute(int family)
|
||||||
|
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
BNetworkInterface::GetRoutes(BObjectList<route_entry>& routes) const
|
BNetworkInterface::GetRoutes(int family, BObjectList<route_entry>& routes) const
|
||||||
{
|
{
|
||||||
// TODO: Code duplication between this method
|
return get_routes(Name(), family, routes);
|
||||||
// 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
BNetworkInterface::GetDefaultRoute(BNetworkAddress& gateway) const
|
BNetworkInterface::GetDefaultRoute(int family, BNetworkAddress& gateway) const
|
||||||
{
|
{
|
||||||
BObjectList<route_entry> routes(1, true);
|
BObjectList<route_entry> routes(1, true);
|
||||||
status_t status = GetRoutes(routes);
|
status_t status = GetRoutes(family, routes);
|
||||||
if (status != B_OK)
|
if (status != B_OK)
|
||||||
return status;
|
return status;
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
#include <net_notifications.h>
|
#include <net_notifications.h>
|
||||||
#include <AutoDeleter.h>
|
#include <AutoDeleter.h>
|
||||||
#include <NetServer.h>
|
#include <NetServer.h>
|
||||||
|
#include <RouteSupport.h>
|
||||||
|
|
||||||
|
|
||||||
// TODO: using AF_INET for the socket isn't really a smart idea, as one
|
// 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
|
status_t
|
||||||
BNetworkRoster::GetRoutes(BObjectList<route_entry>& routes) const
|
BNetworkRoster::GetRoutes(int family, BObjectList<route_entry>& routes) const
|
||||||
{
|
{
|
||||||
int socket = ::socket(AF_INET, SOCK_DGRAM, 0);
|
return get_routes(NULL, family, routes);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,74 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2013, Haiku, Inc. All Rights Reserved.
|
||||||
|
* Distributed under the terms of the MIT License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <net/if.h>
|
||||||
|
#include <sys/sockio.h>
|
||||||
|
|
||||||
|
#include <AutoDeleter.h>
|
||||||
|
#include <ObjectList.h>
|
||||||
|
#include <RouteSupport.h>
|
||||||
|
|
||||||
|
status_t
|
||||||
|
get_routes(const char* interfaceName, int family, BObjectList<route_entry>& 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;
|
||||||
|
}
|
|
@ -88,7 +88,7 @@ Settings::ReadConfiguration()
|
||||||
fNetmask = address.Mask().ToString();
|
fNetmask = address.Mask().ToString();
|
||||||
|
|
||||||
BNetworkAddress gatewayAddress;
|
BNetworkAddress gatewayAddress;
|
||||||
if (interface.GetDefaultRoute(gatewayAddress) != B_OK)
|
if (interface.GetDefaultRoute(AF_INET, gatewayAddress) != B_OK)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
fGateway = gatewayAddress.ToString();
|
fGateway = gatewayAddress.ToString();
|
||||||
|
|
Loading…
Reference in New Issue