xref: /haiku/src/add-ons/kernel/network/ppp/shared/libkernelppp/headers/KPPPInterface.h (revision 9eb55bc1d104b8fda80898f8b25c94d8000c8255)
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 		uint32 IdleSince() const
89 			{ return fIdleSince; }
90 		uint32 DisconnectAfterIdleSince() const
91 			{ return fDisconnectAfterIdleSince; }
92 
93 		bool SetMRU(uint32 MRU);
94 		uint32 MRU() const
95 			{ return fMRU; }
96 				// this is the smallest MRU that we and the peer have
97 		bool SetInterfaceMTU(uint32 interfaceMTU)
98 			{ return SetMRU(interfaceMTU - fHeaderLength); }
99 		uint32 InterfaceMTU() const
100 			{ return fInterfaceMTU; }
101 				// this is the MRU including protocol overhead
102 		uint32 PacketOverhead() const;
103 			// including device and encapsulator headers
104 
105 		virtual status_t Control(uint32 op, void *data, size_t length);
106 
107 		bool SetDevice(KPPPDevice *device);
108 		KPPPDevice *Device() const
109 			{ return fDevice; }
110 
111 		bool AddProtocol(KPPPProtocol *protocol);
112 		bool RemoveProtocol(KPPPProtocol *protocol);
113 		int32 CountProtocols() const;
114 		KPPPProtocol *ProtocolAt(int32 index) const;
115 		KPPPProtocol *FirstProtocol() const
116 			{ return fFirstProtocol; }
117 		KPPPProtocol *ProtocolFor(uint16 protocolNumber,
118 			KPPPProtocol *start = NULL) const;
119 
120 		// multilink methods
121 		bool AddChild(KPPPInterface *child);
122 		bool RemoveChild(KPPPInterface *child);
123 		int32 CountChildren() const
124 			{ return fChildren.CountItems(); }
125 		KPPPInterface *ChildAt(int32 index) const;
126 		KPPPInterface *Parent() const
127 			{ return fParent; }
128 		bool IsMultilink() const
129 			{ return fIsMultilink; }
130 
131 		void SetAutoRedial(bool autoRedial = true);
132 		bool DoesAutoRedial() const
133 			{ return fAutoRedial; }
134 
135 		void SetDialOnDemand(bool dialOnDemand = true);
136 		bool DoesDialOnDemand() const
137 			{ return fDialOnDemand; }
138 
139 		ppp_mode Mode() const
140 			{ return fMode; }
141 			// client or server mode?
142 		ppp_state State() const
143 			{ return fStateMachine.State(); }
144 		ppp_phase Phase() const
145 			{ return fStateMachine.Phase(); }
146 
147 		// Protocol-Field-Compression
148 		bool SetPFCOptions(uint8 pfcOptions);
149 		uint8 PFCOptions() const
150 			{ return fPFCOptions; }
151 		ppp_pfc_state LocalPFCState() const
152 			{ return fLocalPFCState; }
153 				// the local PFC state says if we accepted a request from the peer
154 				// i.e.: we may use PFC in outgoing packets
155 		ppp_pfc_state PeerPFCState() const
156 			{ return fPeerPFCState; }
157 				// the peer PFC state says if the peer accepted a request us
158 				// i.e.: the peer might send PFC-compressed packets to us
159 		bool UseLocalPFC() const
160 			{ return LocalPFCState() == PPP_PFC_ACCEPTED; }
161 
162 		virtual bool Up();
163 			// in server mode Up() listens for an incoming connection
164 		virtual bool Down();
165 		bool IsUp() const;
166 
167 		KPPPReportManager& ReportManager()
168 			{ return fReportManager; }
169 		bool Report(ppp_report_type type, int32 code, void *data, int32 length)
170 			{ return ReportManager().Report(type, code, data, length); }
171 			// returns false if reply was bad (or an error occured)
172 
173 		bool LoadModules(driver_settings *settings,
174 			int32 start, int32 count);
175 		bool LoadModule(const char *name, driver_parameter *parameter,
176 			ppp_module_key_type type);
177 
178 		virtual bool IsAllowedToSend() const;
179 			// always returns true
180 
181 		virtual status_t Send(struct mbuf *packet, uint16 protocolNumber);
182 			// sends the packet to the next handler (normally the device)
183 		virtual status_t Receive(struct mbuf *packet, uint16 protocolNumber);
184 
185 		status_t ReceiveFromDevice(struct mbuf *packet);
186 			// This must not block KPPPDevice::Send()!
187 
188 		void Pulse();
189 			// this manages all timeouts, etc.
190 
191 	private:
192 		bool RegisterInterface();
193 			// adds us to the manager module and
194 			// saves the returned ifnet structure
195 		bool UnregisterInterface();
196 
197 		void SetName(const char *name);
198 
199 		status_t StackControl(uint32 op, void *data);
200 			// stack routes ioctls to interface
201 		status_t StackControlEachHandler(uint32 op, void *data);
202 			// this calls StackControl() with the given parameters for each handler
203 
204 		void CalculateInterfaceMTU();
205 		void CalculateBaudRate();
206 
207 		// these two methods are used by the open/close_event_threads
208 		void CallOpenEvent()
209 			{ StateMachine().OpenEvent(); }
210 		void CallCloseEvent()
211 			{ StateMachine().CloseEvent(); }
212 
213 		void Redial(uint32 delay);
214 
215 		// multilink methods
216 		void SetParent(KPPPInterface *parent)
217 			{ fParent = parent; }
218 
219 	private:
220 		ppp_interface_id fID;
221 			// the manager assigns an ID to every interface
222 		driver_settings *fSettings;
223 		struct ifnet *fIfnet;
224 
225 		thread_id fUpThread, fOpenEventThread, fCloseEventThread;
226 
227 		thread_id fRedialThread;
228 		uint32 fDialRetry, fDialRetriesLimit;
229 		uint32 fDialRetryDelay, fRedialDelay;
230 
231 		ppp_interface_module_info *fManager;
232 
233 		uint32 fIdleSince, fDisconnectAfterIdleSince;
234 		uint32 fMRU, fInterfaceMTU, fHeaderLength;
235 
236 		KPPPInterface *fParent;
237 		TemplateList<KPPPInterface*> fChildren;
238 		bool fIsMultilink;
239 
240 		bool fAutoRedial, fDialOnDemand;
241 
242 		ppp_mode fMode;
243 		ppp_pfc_state fLocalPFCState, fPeerPFCState;
244 		uint8 fPFCOptions;
245 
246 		KPPPDevice *fDevice;
247 		KPPPProtocol *fFirstProtocol;
248 		TemplateList<char*> fModules;
249 
250 		KPPPStateMachine fStateMachine;
251 		KPPPLCP fLCP;
252 		KPPPProfile fProfile;
253 		KPPPReportManager fReportManager;
254 		BLocker& fLock;
255 		int32 fDeleteCounter;
256 };
257 
258 
259 #endif
260