1 /* 2 * Copyright 2010, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _NETWORK_INTERFACE_H 6 #define _NETWORK_INTERFACE_H 7 8 9 #include <net/if.h> 10 #include <net/if_types.h> 11 12 #include <ObjectList.h> 13 #include <NetworkAddress.h> 14 15 16 class BNetworkInterface; 17 18 19 class BNetworkInterfaceAddress { 20 public: 21 BNetworkInterfaceAddress(); 22 ~BNetworkInterfaceAddress(); 23 24 status_t SetTo(const BNetworkInterface& interface, 25 int32 index); 26 27 void SetAddress(const BNetworkAddress& address); 28 void SetMask(const BNetworkAddress& mask); 29 void SetBroadcast(const BNetworkAddress& broadcast); 30 void SetDestination( 31 const BNetworkAddress& destination); 32 33 BNetworkAddress& Address() { return fAddress; } 34 BNetworkAddress& Mask() { return fMask; } 35 BNetworkAddress& Broadcast() { return fBroadcast; } 36 BNetworkAddress& Destination() { return fBroadcast; } 37 38 const BNetworkAddress& Address() const { return fAddress; } 39 const BNetworkAddress& Mask() const { return fMask; } 40 const BNetworkAddress& Broadcast() const { return fBroadcast; } 41 const BNetworkAddress& Destination() const { return fBroadcast; } 42 43 void SetFlags(uint32 flags); 44 uint32 Flags() const { return fFlags; } 45 46 int32 Index() const { return fIndex; } 47 48 private: 49 int32 fIndex; 50 BNetworkAddress fAddress; 51 BNetworkAddress fMask; 52 BNetworkAddress fBroadcast; 53 uint32 fFlags; 54 }; 55 56 57 class BNetworkInterface { 58 public: 59 BNetworkInterface(); 60 BNetworkInterface(const char* name); 61 BNetworkInterface(uint32 index); 62 ~BNetworkInterface(); 63 64 void Unset(); 65 void SetTo(const char* name); 66 status_t SetTo(uint32 index); 67 68 bool Exists() const; 69 70 const char* Name() const; 71 uint32 Index() const; 72 uint32 Flags() const; 73 uint32 MTU() const; 74 int32 Media() const; 75 uint32 Metric() const; 76 uint32 Type() const; 77 status_t GetStats(ifreq_stats& stats); 78 bool HasLink() const; 79 80 status_t SetFlags(uint32 flags); 81 status_t SetMTU(uint32 mtu); 82 status_t SetMedia(int32 media); 83 status_t SetMetric(uint32 metric); 84 85 int32 CountAddresses() const; 86 status_t GetAddressAt(int32 index, 87 BNetworkInterfaceAddress& address); 88 int32 FindAddress(const BNetworkAddress& address); 89 int32 FindFirstAddress(int family); 90 91 status_t AddAddress( 92 const BNetworkInterfaceAddress& address); 93 status_t AddAddress(const BNetworkAddress& address); 94 status_t SetAddress( 95 const BNetworkInterfaceAddress& address); 96 status_t RemoveAddress( 97 const BNetworkInterfaceAddress& address); 98 status_t RemoveAddress(const BNetworkAddress& address); 99 status_t RemoveAddressAt(int32 index); 100 101 status_t GetHardwareAddress(BNetworkAddress& address); 102 103 status_t AddRoute(const route_entry& route); 104 status_t AddDefaultRoute(const BNetworkAddress& gateway); 105 status_t RemoveRoute(const route_entry& route); 106 status_t RemoveRoute(int family, 107 const route_entry& route); 108 status_t RemoveDefaultRoute(int family); 109 status_t GetRoutes(int family, BObjectList<route_entry>& routes) const; 110 status_t GetDefaultRoute(int family, BNetworkAddress& gateway) const; 111 112 status_t AutoConfigure(int family); 113 114 private: 115 char fName[IF_NAMESIZE]; 116 BList fAddresses; 117 }; 118 119 120 #endif // _NETWORK_INTERFACE_H 121