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