haiku/headers/os/net/NetworkInterface.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

126 lines
3.5 KiB
C++

/*
* Copyright 2010, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _NETWORK_INTERFACE_H
#define _NETWORK_INTERFACE_H
#include <net/if.h>
#include <net/if_types.h>
#include <ObjectList.h>
#include <NetworkAddress.h>
class BNetworkInterface;
class BNetworkRoute;
class BNetworkInterfaceAddress {
public:
BNetworkInterfaceAddress();
~BNetworkInterfaceAddress();
status_t SetTo(const BNetworkInterface& interface,
int32 index);
void SetAddress(const BNetworkAddress& address);
void SetMask(const BNetworkAddress& mask);
void SetBroadcast(const BNetworkAddress& broadcast);
void SetDestination(
const BNetworkAddress& destination);
BNetworkAddress& Address() { return fAddress; }
BNetworkAddress& Mask() { return fMask; }
BNetworkAddress& Broadcast() { return fBroadcast; }
BNetworkAddress& Destination() { return fBroadcast; }
const BNetworkAddress& Address() const { return fAddress; }
const BNetworkAddress& Mask() const { return fMask; }
const BNetworkAddress& Broadcast() const { return fBroadcast; }
const BNetworkAddress& Destination() const { return fBroadcast; }
void SetFlags(uint32 flags);
uint32 Flags() const { return fFlags; }
int32 Index() const { return fIndex; }
private:
int32 fIndex;
BNetworkAddress fAddress;
BNetworkAddress fMask;
BNetworkAddress fBroadcast;
uint32 fFlags;
};
class BNetworkInterface {
public:
BNetworkInterface();
BNetworkInterface(const char* name);
BNetworkInterface(uint32 index);
~BNetworkInterface();
void Unset();
void SetTo(const char* name);
status_t SetTo(uint32 index);
bool Exists() const;
const char* Name() const;
uint32 Index() const;
uint32 Flags() const;
uint32 MTU() const;
int32 Media() const;
uint32 Metric() const;
uint32 Type() const;
status_t GetStats(ifreq_stats& stats);
bool HasLink() const;
status_t SetFlags(uint32 flags);
status_t SetMTU(uint32 mtu);
status_t SetMedia(int32 media);
status_t SetMetric(uint32 metric);
int32 CountAddresses() const;
status_t GetAddressAt(int32 index,
BNetworkInterfaceAddress& address);
int32 FindAddress(const BNetworkAddress& address);
int32 FindFirstAddress(int family);
status_t AddAddress(
const BNetworkInterfaceAddress& address);
status_t AddAddress(const BNetworkAddress& address);
status_t SetAddress(
const BNetworkInterfaceAddress& address);
status_t RemoveAddress(
const BNetworkInterfaceAddress& address);
status_t RemoveAddress(const BNetworkAddress& address);
status_t RemoveAddressAt(int32 index);
status_t GetHardwareAddress(BNetworkAddress& address);
status_t AddRoute(const BNetworkRoute& route);
status_t AddDefaultRoute(const BNetworkAddress& gateway);
status_t RemoveRoute(const BNetworkRoute& route);
status_t RemoveRoute(int family,
const BNetworkRoute& route);
status_t RemoveDefaultRoute(int family);
status_t GetRoutes(int family,
BObjectList<BNetworkRoute>& routes) const;
status_t GetDefaultRoute(int family,
BNetworkRoute& route) const;
status_t GetDefaultGateway(int family,
BNetworkAddress& gateway) const;
status_t AutoConfigure(int family);
private:
char fName[IF_NAMESIZE];
BList fAddresses;
};
#endif // _NETWORK_INTERFACE_H