xref: /haiku/src/add-ons/kernel/network/ppp/shared/libkernelppp/headers/KPPPInterface.h (revision 83b1a68c52ba3e0e8796282759f694b7fdddf06d)
1 /*
2  * Copyright 2003-2006, Haiku Inc.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #ifndef _K_PPP_INTERFACE__H
7 #define _K_PPP_INTERFACE__H
8 
9 #include <driver_settings.h>
10 
11 #ifndef _K_PPP_DEFS__H
12 #include <KPPPDefs.h>
13 #endif
14 
15 #ifndef _PPP_CONTROL__H
16 #include <PPPControl.h>
17 #endif
18 
19 #ifndef _K_PPP_LCP__H
20 #include <KPPPLCP.h>
21 #endif
22 
23 #ifndef _K_PPP_REPORT_MANAGER__H
24 #include <KPPPReportManager.h>
25 #endif
26 
27 #ifndef _K_PPP_STATE_MACHINE__H
28 #include <KPPPStateMachine.h>
29 #endif
30 
31 #include <TemplateList.h>
32 
33 class KPPPDevice;
34 class KPPPProtocol;
35 class KPPPOptionHandler;
36 
37 struct ppp_interface_module_info;
38 struct ppp_module_info;
39 struct ppp_interface_entry;
40 
41 
42 class KPPPInterface : public KPPPLayer {
43 		friend class PPPManager;
44 		friend class KPPPStateMachine;
45 
46 	private:
47 		//!	Copies are not allowed.
48 		KPPPInterface(const KPPPInterface& copy);
49 		KPPPInterface& operator= (const KPPPInterface& copy);
50 
51 		// only PPPManager may construct us!
52 		KPPPInterface(const char *name, ppp_interface_entry *entry,
53 			ppp_interface_id ID, const driver_settings *settings,
54 			KPPPInterface *parent = NULL);
55 		~KPPPInterface();
56 
57 	public:
58 		void Delete();
59 
60 		virtual status_t InitCheck() const;
61 
62 		//!	Returns this interface's unique identifier.
63 		ppp_interface_id ID() const
64 			{ return fID; }
65 
66 		//!	Returns interface's settings.
67 		driver_settings* Settings() const
68 			{ return fSettings; }
69 
70 		//!	Returns the KPPPStateMachine of this interface.
71 		KPPPStateMachine& StateMachine()
72 			{ return fStateMachine; }
73 		//!	Returns the KPPPLCP protocol of this interface.
74 		KPPPLCP& LCP()
75 			{ return fLCP; }
76 
77 		//!	Returns the interfac's ifnet structure that is exported to the netstack.
78 		struct ifnet *Ifnet() const
79 			{ return fIfnet; }
80 
81 		const char *Username() const;
82 		const char *Password() const;
83 
84 		//!	Delay in miliseconds between a connect retry.
85 		uint32 ConnectRetryDelay() const
86 			{ return fConnectRetryDelay; }
87 		//!	Delay in miliseconds to wait until reconnecting.
88 		uint32 ReconnectDelay() const
89 			{ return fReconnectDelay; }
90 
91 		//! Time when connection was established successfully.
92 		bigtime_t ConnectedSince() const
93 			{ return fConnectedSince; }
94 
95 		//!	Used for reporting that a packet was send/received (updates idle time).
96 		void UpdateIdleSince()
97 			{ fUpdateIdleSince = true; }
98 		//!	Returns the time in seconds when the last packet was received.
99 		uint32 IdleSince() const
100 			{ return fIdleSince; }
101 		//!	If no packets were received this number of seconds we will disconnect.
102 		uint32 DisconnectAfterIdleSince() const
103 			{ return fDisconnectAfterIdleSince; }
104 
105 		bool SetMRU(uint32 MRU);
106 		//!	This is the smallest MRU that we and the peer have.
107 		uint32 MRU() const
108 			{ return fMRU; }
109 		//!	Sets interface's maximum transfer unit (will set ifnet value accordingly).
110 		bool SetInterfaceMTU(uint32 interfaceMTU)
111 			{ return SetMRU(interfaceMTU - fHeaderLength); }
112 		//!	This is the MRU including protocol overhead.
113 		uint32 InterfaceMTU() const
114 			{ return fInterfaceMTU; }
115 		//!	Includes the length of all device and encapsulator headers.
116 		uint32 PacketOverhead() const;
117 
118 		virtual status_t Control(uint32 op, void *data, size_t length);
119 
120 		bool SetDevice(KPPPDevice *device);
121 		//!	Returns interface's transport device.
122 		KPPPDevice *Device() const
123 			{ return fDevice; }
124 
125 		bool AddProtocol(KPPPProtocol *protocol);
126 		bool RemoveProtocol(KPPPProtocol *protocol);
127 		int32 CountProtocols() const;
128 		KPPPProtocol *ProtocolAt(int32 index) const;
129 		//!	Returns first protocol in chain.
130 		KPPPProtocol *FirstProtocol() const
131 			{ return fFirstProtocol; }
132 		KPPPProtocol *ProtocolFor(uint16 protocolNumber,
133 			KPPPProtocol *start = NULL) const;
134 
135 		// multilink methods
136 		bool AddChild(KPPPInterface *child);
137 		bool RemoveChild(KPPPInterface *child);
138 		//!	Returns the number of child interfaces that this interface has.
139 		int32 CountChildren() const
140 			{ return fChildren.CountItems(); }
141 		KPPPInterface *ChildAt(int32 index) const;
142 		//!	Returns this interface's parent.
143 		KPPPInterface *Parent() const
144 			{ return fParent; }
145 		//!	Returns whether we are a multilink-interface.
146 		bool IsMultilink() const
147 			{ return fIsMultilink; }
148 
149 		void SetAutoReconnect(bool autoReconnect = true);
150 		//!	Returns whether this interface reconnects automatically.
151 		bool DoesAutoReconnect() const
152 			{ return fAutoReconnect; }
153 
154 		void SetConnectOnDemand(bool connectOnDemand = true);
155 		//!	Returns whether connect-on-demand (auto-connect) is enabled.
156 		bool DoesConnectOnDemand() const
157 			{ return fConnectOnDemand; }
158 
159 		void SetAskBeforeConnecting(bool ask);
160 		//!	Returns whether the user is asked before establishing the connection.
161 		bool DoesAskBeforeConnecting() const
162 			{ return fAskBeforeConnecting; }
163 
164 		//!	Clients are in \c PPP_CLIENT_MODE and servers are in \c PPP_SERVER_MODE.
165 		ppp_mode Mode() const
166 			{ return fMode; }
167 		//!	Current state of the state machine (see enum: \c ppp_state).
168 		ppp_state State() const
169 			{ return fStateMachine.State(); }
170 		//!	Current phase of the state machine (see enum: \c ppp_phase).
171 		ppp_phase Phase() const
172 			{ return fStateMachine.Phase(); }
173 
174 		// Protocol-Field-Compression
175 		bool SetPFCOptions(uint8 pfcOptions);
176 		//!	PFC option flags as defined in enum: \c ppp_pfc_options.
177 		uint8 PFCOptions() const
178 			{ return fPFCOptions; }
179 		/*!	\brief Local PFC state
180 
181 			Values defined in \c ppp_pfc_state. \n
182 			The local PFC state says if we accepted a request from the peer
183 			i.e.: we may use PFC in outgoing packets.
184 		*/
185 		ppp_pfc_state LocalPFCState() const
186 			{ return fLocalPFCState; }
187 		/*!	\brief Peer PFC state
188 
189 			Values defined in \c ppp_pfc_state. \n
190 			The peer PFC state says if the peer accepted a request us
191 			i.e.: the peer might send PFC-compressed packets to us
192 		*/
193 		ppp_pfc_state PeerPFCState() const
194 			{ return fPeerPFCState; }
195 		//!	Shortcut for check if local state is \c PPP_PFC_ACCEPTED.
196 		bool UseLocalPFC() const
197 			{ return LocalPFCState() == PPP_PFC_ACCEPTED; }
198 
199 		virtual bool Up();
200 			// in server mode Up() listens for an incoming connection
201 		virtual bool Down();
202 		bool WaitForConnection();
203 		//!	Returns if the interface is connected.
204 		bool IsUp() const
205 			{ return Phase() == PPP_ESTABLISHED_PHASE; }
206 
207 		//!	Returns interface's report manager.
208 		KPPPReportManager& ReportManager()
209 			{ return fReportManager; }
210 		//!	Shortcut to KPPPReportManager::Report() of this interface's report manager.
211 		bool Report(ppp_report_type type, int32 code, void *data, int32 length)
212 			{ return ReportManager().Report(type, code, data, length); }
213 			// returns false if reply was bad (or an error occured)
214 
215 		bool LoadModules(driver_settings *settings,
216 			int32 start, int32 count);
217 		bool LoadModule(const char *name, driver_parameter *parameter,
218 			ppp_module_key_type type);
219 
220 		virtual bool IsAllowedToSend() const;
221 			// always returns true
222 
223 		virtual status_t Send(struct mbuf *packet, uint16 protocolNumber);
224 			// sends the packet to the next handler (normally the device)
225 		virtual status_t Receive(struct mbuf *packet, uint16 protocolNumber);
226 
227 		status_t ReceiveFromDevice(struct mbuf *packet);
228 			// This must not block KPPPDevice::Send()!
229 
230 		void Pulse();
231 			// this manages all timeouts, etc.
232 
233 	private:
234 		bool RegisterInterface();
235 			// adds us to the manager module and
236 			// saves the returned ifnet structure
237 		bool UnregisterInterface();
238 
239 		status_t StackControl(uint32 op, void *data);
240 			// stack routes ioctls to interface
241 		status_t StackControlEachHandler(uint32 op, void *data);
242 			// this calls StackControl() with the given parameters for each handler
243 
244 		void CalculateInterfaceMTU();
245 		void CalculateBaudRate();
246 
247 		void Reconnect(uint32 delay);
248 
249 		// multilink methods
250 		//!	Set the parent of this interface.
251 		void SetParent(KPPPInterface *parent)
252 			{ fParent = parent; }
253 
254 	private:
255 		ppp_interface_id fID;
256 			// the manager assigns an ID to every interface
257 		driver_settings *fSettings;
258 		struct ifnet *fIfnet;
259 
260 		char *fUsername, *fPassword;
261 
262 		thread_id fReconnectThread;
263 		uint32 fConnectAttempt, fConnectRetriesLimit;
264 		uint32 fConnectRetryDelay, fReconnectDelay;
265 
266 		ppp_interface_module_info *fManager;
267 
268 		ppp_statistics fStatistics;
269 		bigtime_t fConnectedSince;
270 		uint32 fIdleSince, fDisconnectAfterIdleSince;
271 		bool fUpdateIdleSince;
272 		uint32 fMRU, fInterfaceMTU, fHeaderLength;
273 
274 		KPPPInterface *fParent;
275 		TemplateList<KPPPInterface*> fChildren;
276 		bool fIsMultilink;
277 
278 		bool fAutoReconnect, fConnectOnDemand, fAskBeforeConnecting;
279 
280 		ppp_mode fMode;
281 		ppp_pfc_state fLocalPFCState, fPeerPFCState;
282 		uint8 fPFCOptions;
283 
284 		KPPPDevice *fDevice;
285 		KPPPProtocol *fFirstProtocol;
286 		TemplateList<char*> fModules;
287 
288 		KPPPStateMachine fStateMachine;
289 		KPPPLCP fLCP;
290 		KPPPReportManager fReportManager;
291 		BLocker& fLock;
292 		int32 fDeleteCounter;
293 };
294 
295 
296 #endif
297