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