NetworkSettings.h revision 1f94cdcd
1/*
2 * Copyright 2006-2015, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Axel D��rfler, axeld@pinc-software.de
7 */
8#ifndef SETTINGS_H
9#define SETTINGS_H
10
11
12#include <vector>
13
14#include <Message.h>
15#include <Messenger.h>
16#include <NetworkAddress.h>
17#include <Path.h>
18#include <StringList.h>
19
20
21namespace BNetworkKit {
22
23
24class BNetworkInterfaceSettings;
25class BNetworkServiceSettings;
26
27
28class BNetworkSettings {
29public:
30	static	const uint32		kMsgInterfaceSettingsUpdated = 'SUif';
31	static	const uint32		kMsgNetworkSettingsUpdated = 'SUnw';
32	static	const uint32		kMsgServiceSettingsUpdated = 'SUsv';
33
34public:
35								BNetworkSettings();
36								~BNetworkSettings();
37
38			status_t			GetNextInterface(uint32& cookie,
39									BMessage& interface);
40			status_t			GetInterface(const char* name,
41									BMessage& interface);
42			status_t			AddInterface(const BMessage& interface);
43			status_t			RemoveInterface(const char* name);
44			BNetworkInterfaceSettings
45								Interface(const char* name);
46
47			int32				CountNetworks() const;
48			status_t			GetNextNetwork(uint32& cookie,
49									BMessage& network) const;
50			status_t			GetNetwork(const char* name,
51									BMessage& network);
52			status_t			AddNetwork(const BMessage& network);
53			status_t			RemoveNetwork(const char* name);
54
55			const BMessage&		Services() const;
56			status_t			GetNextService(uint32& cookie,
57									BMessage& service);
58			status_t			GetService(const char* name, BMessage& service);
59			status_t			AddService(const BMessage& service);
60			status_t			RemoveService(const char* name);
61			BNetworkServiceSettings
62								Service(const char* name);
63
64			status_t			StartMonitoring(const BMessenger& target);
65			status_t			StopMonitoring(const BMessenger& target);
66
67			status_t			Update(BMessage* message);
68
69private:
70			status_t			_Load(const char* name = NULL,
71									uint32* _type = NULL);
72			status_t			_Save(const char* name = NULL);
73			BPath				_Path(BPath& parent, const char* name);
74			status_t			_GetPath(const char* name, BPath& path);
75
76			status_t			_StartWatching(const char* name,
77									const BMessenger& target);
78
79			bool				_IsWatching(const BMessenger& target) const
80									{ return fListener == target; }
81			bool				_IsWatching() const
82									{ return fListener.IsValid(); }
83
84			status_t			_ConvertNetworkToSettings(BMessage& message);
85			status_t			_ConvertNetworkFromSettings(BMessage& message);
86
87			status_t			_GetItem(BMessage& container,
88									const char* itemField,
89									const char* nameField, const char* name,
90									int32& _index, BMessage& item);
91			status_t			_RemoveItem(BMessage& container,
92									const char* itemField,
93									const char* nameField, const char* name,
94									const char* store = NULL);
95
96private:
97			BMessenger			fListener;
98			BMessage			fInterfaces;
99			BMessage			fNetworks;
100			BMessage			fServices;
101};
102
103
104class BNetworkInterfaceAddressSettings {
105public:
106								BNetworkInterfaceAddressSettings();
107								BNetworkInterfaceAddressSettings(
108									const BMessage& data);
109								BNetworkInterfaceAddressSettings(
110									const BNetworkInterfaceAddressSettings&
111										other);
112								~BNetworkInterfaceAddressSettings();
113
114			int					Family() const;
115			void				SetFamily(int family);
116			bool				IsAutoConfigure() const;
117			void				SetAutoConfigure(bool configure);
118
119			const BNetworkAddress&
120								Address() const;
121			BNetworkAddress&	Address();
122			const BNetworkAddress&
123								Mask() const;
124			BNetworkAddress&	Mask();
125			const BNetworkAddress&
126								Peer() const;
127			BNetworkAddress&	Peer();
128			const BNetworkAddress&
129								Broadcast() const;
130			BNetworkAddress&	Broadcast();
131			const BNetworkAddress&
132								Gateway() const;
133			BNetworkAddress&	Gateway();
134
135			status_t			GetMessage(BMessage& data) const;
136
137			BNetworkInterfaceAddressSettings&
138								operator=(
139									const BNetworkInterfaceAddressSettings&
140										other);
141
142private:
143			int32				fFamily;
144			bool				fAutoConfigure;
145			BNetworkAddress		fAddress;
146			BNetworkAddress		fMask;
147			BNetworkAddress		fPeer;
148			BNetworkAddress		fBroadcast;
149			BNetworkAddress		fGateway;
150};
151
152
153class BNetworkInterfaceSettings {
154public:
155								BNetworkInterfaceSettings();
156								BNetworkInterfaceSettings(
157									const BMessage& message);
158								~BNetworkInterfaceSettings();
159
160			const char*			Name() const;
161			void				SetName(const char* name);
162
163			int32				Flags() const;
164			void				SetFlags(int32 flags);
165			int32				MTU() const;
166			void				SetMTU(int32 mtu);
167			int32				Metric() const;
168			void				SetMetric(int32 metric);
169
170			int32				CountAddresses() const;
171			const BNetworkInterfaceAddressSettings&
172								AddressAt(int32 index) const;
173			BNetworkInterfaceAddressSettings&
174								AddressAt(int32 index);
175			int32				FindFirstAddress(int family) const;
176			void				AddAddress(const
177									BNetworkInterfaceAddressSettings& address);
178			void				RemoveAddress(int32 index);
179
180			bool				IsAutoConfigure(int family) const;
181
182			status_t			GetMessage(BMessage& data) const;
183
184private:
185			BString				fName;
186			int32				fFlags;
187			int32				fMTU;
188			int32				fMetric;
189			std::vector<BNetworkInterfaceAddressSettings>
190								fAddresses;
191};
192
193
194class BNetworkServiceAddressSettings {
195public:
196								BNetworkServiceAddressSettings();
197								BNetworkServiceAddressSettings(
198									const BMessage& data, int family = -1,
199									int type = -1, int protocol = -1,
200									int port = -1);
201								~BNetworkServiceAddressSettings();
202
203			int					Family() const;
204			void				SetFamily(int family);
205			int					Protocol() const;
206			void				SetProtocol(int protocol);
207			int					Type() const;
208			void				SetType(int type);
209
210			const BNetworkAddress&
211								Address() const;
212			BNetworkAddress&	Address();
213
214			status_t			GetMessage(BMessage& data) const;
215
216			bool				operator==(
217									const BNetworkServiceAddressSettings& other)
218										const;
219
220private:
221			int32				fFamily;
222			int					fProtocol;
223			int					fType;
224			BNetworkAddress		fAddress;
225};
226
227
228class BNetworkServiceSettings {
229public:
230								BNetworkServiceSettings();
231								BNetworkServiceSettings(
232									const BMessage& message);
233								~BNetworkServiceSettings();
234
235			status_t			InitCheck() const;
236
237			const char*			Name() const;
238			void				SetName(const char* name);
239			bool				IsStandAlone() const;
240			void				SetStandAlone(bool alone);
241			bool				IsEnabled() const;
242			void				SetEnabled(bool enable);
243
244			int					Family() const;
245			void				SetFamily(int family);
246			int					Protocol() const;
247			void				SetProtocol(int protocol);
248			int					Type() const;
249			void				SetType(int type);
250			int					Port() const;
251			void				SetPort(int port);
252
253			int32				CountArguments() const;
254			const char*			ArgumentAt(int32 index) const;
255			void				AddArgument(const char* argument);
256			void				RemoveArgument(int32 index);
257
258			int32				CountAddresses() const;
259			const BNetworkServiceAddressSettings&
260								AddressAt(int32 index) const;
261			void				AddAddress(const
262									BNetworkServiceAddressSettings& address);
263			void				RemoveAddress(int32 index);
264
265			status_t			GetMessage(BMessage& data) const;
266
267private:
268			BString				fName;
269			int32				fFamily;
270			int32				fType;
271			int32				fProtocol;
272			int32				fPort;
273			bool				fEnabled;
274			bool				fStandAlone;
275			BStringList			fArguments;
276			std::vector<BNetworkServiceAddressSettings>
277								fAddresses;
278};
279
280
281}	// namespace BNetworkKit
282
283
284#endif	// SETTINGS_H
285