xref: /haiku/src/add-ons/kernel/network/ppp/shared/libkernelppp/headers/KPPPInterface.h (revision 67bce78b48ed6d01b5a8eef89f5694c372b7e0a1)
1 //-----------------------------------------------------------------------
2 //  This software is part of the OpenBeOS distribution and is covered
3 //  by the OpenBeOS license.
4 //
5 //  Copyright (c) 2003-2004 Waldemar Kornewald, Waldemar.Kornewald@web.de
6 //-----------------------------------------------------------------------
7 
8 #ifndef _K_PPP_INTERFACE__H
9 #define _K_PPP_INTERFACE__H
10 
11 #include <driver_settings.h>
12 
13 #ifndef _K_PPP_DEFS__H
14 #include <KPPPDefs.h>
15 #endif
16 
17 #ifndef _K_PPP_LCP__H
18 #include <KPPPLCP.h>
19 #endif
20 
21 #ifndef _K_PPP_PROFILE__H
22 #include <KPPPProfile.h>
23 #endif
24 
25 #ifndef _K_PPP_REPORT_MANAGER__H
26 #include <KPPPReportManager.h>
27 #endif
28 
29 #ifndef _K_PPP_STATE_MACHINE__H
30 #include <KPPPStateMachine.h>
31 #endif
32 
33 #include <TemplateList.h>
34 
35 class KPPPDevice;
36 class KPPPProtocol;
37 class KPPPOptionHandler;
38 
39 struct ppp_interface_module_info;
40 struct ppp_module_info;
41 struct ppp_interface_entry;
42 
43 
44 class KPPPInterface : public KPPPLayer {
45 		friend class PPPManager;
46 		friend class KPPPStateMachine;
47 		friend class KPPPInterfaceAccess;
48 
49 	private:
50 		// copies are not allowed!
51 		KPPPInterface(const KPPPInterface& copy);
52 		KPPPInterface& operator= (const KPPPInterface& copy);
53 
54 		// only PPPManager may construct us!
55 		KPPPInterface(const char *name, ppp_interface_entry *entry,
56 			ppp_interface_id ID, const driver_settings *settings,
57 			const driver_settings *profile, KPPPInterface *parent = NULL);
58 		~KPPPInterface();
59 
60 	public:
61 		void Delete();
62 
63 		virtual status_t InitCheck() const;
64 
65 		ppp_interface_id ID() const
66 			{ return fID; }
67 
68 		driver_settings* Settings() const
69 			{ return fSettings; }
70 
71 		KPPPStateMachine& StateMachine()
72 			{ return fStateMachine; }
73 		KPPPLCP& LCP()
74 			{ return fLCP; }
75 		KPPPProfile& Profile()
76 			{ return fProfile; }
77 
78 		struct ifnet *Ifnet() const
79 			{ return fIfnet; }
80 
81 		// delays
82 		uint32 DialRetryDelay() const
83 			{ return fDialRetryDelay; }
84 		uint32 RedialDelay() const
85 			{ return fRedialDelay; }
86 
87 		// idle handling
88 		void UpdateIdleSince()
89 			{ fUpdateIdleSince = true; }
90 		uint32 IdleSince() const
91 			{ return fIdleSince; }
92 		uint32 DisconnectAfterIdleSince() const
93 			{ return fDisconnectAfterIdleSince; }
94 
95 		bool SetMRU(uint32 MRU);
96 		uint32 MRU() const
97 			{ return fMRU; }
98 				// this is the smallest MRU that we and the peer have
99 		bool SetInterfaceMTU(uint32 interfaceMTU)
100 			{ return SetMRU(interfaceMTU - fHeaderLength); }
101 		uint32 InterfaceMTU() const
102 			{ return fInterfaceMTU; }
103 				// this is the MRU including protocol overhead
104 		uint32 PacketOverhead() const;
105 			// including device and encapsulator headers
106 
107 		virtual status_t Control(uint32 op, void *data, size_t length);
108 
109 		bool SetDevice(KPPPDevice *device);
110 		KPPPDevice *Device() const
111 			{ return fDevice; }
112 
113 		bool AddProtocol(KPPPProtocol *protocol);
114 		bool RemoveProtocol(KPPPProtocol *protocol);
115 		int32 CountProtocols() const;
116 		KPPPProtocol *ProtocolAt(int32 index) const;
117 		KPPPProtocol *FirstProtocol() const
118 			{ return fFirstProtocol; }
119 		KPPPProtocol *ProtocolFor(uint16 protocolNumber,
120 			KPPPProtocol *start = NULL) const;
121 
122 		// multilink methods
123 		bool AddChild(KPPPInterface *child);
124 		bool RemoveChild(KPPPInterface *child);
125 		int32 CountChildren() const
126 			{ return fChildren.CountItems(); }
127 		KPPPInterface *ChildAt(int32 index) const;
128 		KPPPInterface *Parent() const
129 			{ return fParent; }
130 		bool IsMultilink() const
131 			{ return fIsMultilink; }
132 
133 		void SetAutoRedial(bool autoRedial = true);
134 		bool DoesAutoRedial() const
135 			{ return fAutoRedial; }
136 
137 		void SetDialOnDemand(bool dialOnDemand = true);
138 		bool DoesDialOnDemand() const
139 			{ return fDialOnDemand; }
140 
141 		ppp_mode Mode() const
142 			{ return fMode; }
143 			// client or server mode?
144 		ppp_state State() const
145 			{ return fStateMachine.State(); }
146 		ppp_phase Phase() const
147 			{ return fStateMachine.Phase(); }
148 
149 		// Protocol-Field-Compression
150 		bool SetPFCOptions(uint8 pfcOptions);
151 		uint8 PFCOptions() const
152 			{ return fPFCOptions; }
153 		ppp_pfc_state LocalPFCState() const
154 			{ return fLocalPFCState; }
155 				// the local PFC state says if we accepted a request from the peer
156 				// i.e.: we may use PFC in outgoing packets
157 		ppp_pfc_state PeerPFCState() const
158 			{ return fPeerPFCState; }
159 				// the peer PFC state says if the peer accepted a request us
160 				// i.e.: the peer might send PFC-compressed packets to us
161 		bool UseLocalPFC() const
162 			{ return LocalPFCState() == PPP_PFC_ACCEPTED; }
163 
164 		virtual bool Up();
165 			// in server mode Up() listens for an incoming connection
166 		virtual bool Down();
167 		bool IsUp() const;
168 
169 		KPPPReportManager& ReportManager()
170 			{ return fReportManager; }
171 		bool Report(ppp_report_type type, int32 code, void *data, int32 length)
172 			{ return ReportManager().Report(type, code, data, length); }
173 			// returns false if reply was bad (or an error occured)
174 
175 		bool LoadModules(driver_settings *settings,
176 			int32 start, int32 count);
177 		bool LoadModule(const char *name, driver_parameter *parameter,
178 			ppp_module_key_type type);
179 
180 		virtual bool IsAllowedToSend() const;
181 			// always returns true
182 
183 		virtual status_t Send(struct mbuf *packet, uint16 protocolNumber);
184 			// sends the packet to the next handler (normally the device)
185 		virtual status_t Receive(struct mbuf *packet, uint16 protocolNumber);
186 
187 		status_t ReceiveFromDevice(struct mbuf *packet);
188 			// This must not block KPPPDevice::Send()!
189 
190 		void Pulse();
191 			// this manages all timeouts, etc.
192 
193 	private:
194 		bool RegisterInterface();
195 			// adds us to the manager module and
196 			// saves the returned ifnet structure
197 		bool UnregisterInterface();
198 
199 		void UpdateProfile();
200 
201 		status_t StackControl(uint32 op, void *data);
202 			// stack routes ioctls to interface
203 		status_t StackControlEachHandler(uint32 op, void *data);
204 			// this calls StackControl() with the given parameters for each handler
205 
206 		void CalculateInterfaceMTU();
207 		void CalculateBaudRate();
208 
209 		// these two methods are used by the open/close_event_threads
210 		void CallOpenEvent()
211 			{ StateMachine().OpenEvent(); }
212 		void CallCloseEvent()
213 			{ StateMachine().CloseEvent(); }
214 
215 		void Redial(uint32 delay);
216 
217 		// multilink methods
218 		void SetParent(KPPPInterface *parent)
219 			{ fParent = parent; }
220 
221 	private:
222 		ppp_interface_id fID;
223 			// the manager assigns an ID to every interface
224 		driver_settings *fSettings;
225 		struct ifnet *fIfnet;
226 
227 		thread_id fUpThread, fOpenEventThread, fCloseEventThread;
228 
229 		thread_id fRedialThread;
230 		uint32 fDialRetry, fDialRetriesLimit;
231 		uint32 fDialRetryDelay, fRedialDelay;
232 
233 		ppp_interface_module_info *fManager;
234 
235 		uint32 fIdleSince, fDisconnectAfterIdleSince;
236 		bool fUpdateIdleSince;
237 		uint32 fMRU, fInterfaceMTU, fHeaderLength;
238 
239 		KPPPInterface *fParent;
240 		TemplateList<KPPPInterface*> fChildren;
241 		bool fIsMultilink;
242 
243 		bool fAutoRedial, fDialOnDemand;
244 
245 		ppp_mode fMode;
246 		ppp_pfc_state fLocalPFCState, fPeerPFCState;
247 		uint8 fPFCOptions;
248 
249 		KPPPDevice *fDevice;
250 		KPPPProtocol *fFirstProtocol;
251 		TemplateList<char*> fModules;
252 
253 		KPPPStateMachine fStateMachine;
254 		KPPPLCP fLCP;
255 		KPPPProfile fProfile;
256 		KPPPReportManager fReportManager;
257 		BLocker& fLock;
258 		int32 fDeleteCounter;
259 };
260 
261 
262 #endif
263