haiku/headers/os/net/NetworkRoute.h
Michael Lotz 3b7b927dd0 libbnetapi: Add BNetworkRoute to replace use of route_entry.
The BNetworkRoute class manages a route_entry and the sockaddr's
associated with it. It replaces the direct use of route_entry in the
BNetworkInterface API.

Using route_entry is fragile and inconvenient as it only holds pointers
to the sockaddr's. When getting a list of routes from the kernel, each
route_entry is set up so that its pointers point into the single flat
buffer that is passed around. Creating a copy of the route_entry and
then deleting the flat buffer makes the pointers in the copy stale.
Returning these route entries therefore always lead to a use-after-free
when they were eventually used.

BNetworkRoute also takes over the code and functionallity of getting
routes from RouteSupport. The corresponding method in BNetworkRoster is
replaced by a static method in BNetworkRoute.

Also distinguish between the default route and gateway of an interface.
GetDefaultRoute() now gets the default BNetworkRoute for the interface
while GetDefaultGateway() gets the associated gateway address within
that default route. Adjust network preferences panel to this change.

Note that we currently only seem to have per interface default routes
and not an actual global default route. This was already the case before
these changes and I did not further investigate what this means.
2015-04-12 18:50:00 +02:00

76 lines
1.9 KiB
C++

/*
* Copyright 2015, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _NETWORK_ROUTE_H
#define _NETWORK_ROUTE_H
#include <net/route.h>
#include <ObjectList.h>
class BNetworkRoute {
public:
BNetworkRoute();
~BNetworkRoute();
status_t SetTo(const BNetworkRoute& other);
status_t SetTo(const route_entry& routeEntry);
void Adopt(BNetworkRoute& other);
const route_entry& RouteEntry() const;
const sockaddr* Destination() const;
status_t SetDestination(const sockaddr& destination);
void UnsetDestination();
const sockaddr* Mask() const;
status_t SetMask(const sockaddr& mask);
void UnsetMask();
const sockaddr* Gateway() const;
status_t SetGateway(const sockaddr& gateway);
void UnsetGateway();
const sockaddr* Source() const;
status_t SetSource(const sockaddr& source);
void UnsetSource();
uint32 Flags() const;
void SetFlags(uint32 flags);
uint32 MTU() const;
void SetMTU(uint32 mtu);
int AddressFamily() const;
static status_t GetDefaultRoute(int family,
const char* interfaceName,
BNetworkRoute& route);
static status_t GetDefaultGateway(int family,
const char* interfaceName,
sockaddr& gateway);
static status_t GetRoutes(int family,
BObjectList<BNetworkRoute>& routes);
static status_t GetRoutes(int family, const char* interfaceName,
BObjectList<BNetworkRoute>& routes);
static status_t GetRoutes(int family, const char* interfaceName,
uint32 filterFlags,
BObjectList<BNetworkRoute>& routes);
private:
BNetworkRoute(const BNetworkRoute& other);
// unimplemented to disallow copying
status_t _AllocateAndSetAddress(const sockaddr& from,
sockaddr*& to);
void _FreeAndUnsetAddress(sockaddr*& address);
route_entry fRouteEntry;
};
#endif // _NETWORK_ROUTE_H