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