1/*
2 * Copyright 2003-2004, Haiku Inc.
3 * Distributed under the terms of the MIT License.
4 */
5
6#ifndef _K_PPP_PROTOCOL__H
7#define _K_PPP_PROTOCOL__H
8
9#include <KPPPDefs.h>
10#include <KPPPLayer.h>
11
12class KPPPInterface;
13class KPPPOptionHandler;
14
15
16class KPPPProtocol : public KPPPLayer {
17	protected:
18		// KPPPProtocol must be subclassed
19		KPPPProtocol(const char *name, ppp_phase activationPhase,
20			uint16 protocolNumber, ppp_level level, int32 addressFamily,
21			uint32 overhead, KPPPInterface& interface,
22			driver_parameter *settings, int32 flags = PPP_NO_FLAGS,
23			const char *type = NULL, KPPPOptionHandler *optionHandler = NULL);
24
25	public:
26		virtual ~KPPPProtocol();
27
28		virtual void Uninit();
29
30		//!	Returns the interface that owns this protocol.
31		KPPPInterface& Interface() const
32			{ return fInterface; }
33		//!	Returns the protocol's settings.
34		driver_parameter *Settings() const
35			{ return fSettings; }
36
37		//!	The activation phase is the phase when Up() should be called.
38		ppp_phase ActivationPhase() const
39			{ return fActivationPhase; }
40
41		//!	The protocol number.
42		uint16 ProtocolNumber() const
43			{ return fProtocolNumber; }
44		//!	The address family. Negative values and values > 0xFF are ignored.
45		int32 AddressFamily() const
46			{ return fAddressFamily; }
47		//!	This protocol's flags.
48		int32 Flags() const
49			{ return fFlags; }
50		//!	Which side this protocol works for.
51		ppp_side Side() const
52			{ return fSide; }
53
54		//!	Protocol type. May be NULL.
55		const char *Type() const
56			{ return fType; }
57		//!	The (optional) KPPPOptionHandler object of this protocol.
58		KPPPOptionHandler *OptionHandler() const
59			{ return fOptionHandler; }
60
61		//!	Sets the next protocol in the list.
62		void SetNextProtocol(KPPPProtocol *protocol)
63			{ fNextProtocol = protocol; SetNext(protocol); }
64		//!	Returns the next protocol in the list.
65		KPPPProtocol *NextProtocol() const
66			{ return fNextProtocol; }
67
68		void SetEnabled(bool enabled = true);
69		//!	Returns whether this protocol is enabled.
70		bool IsEnabled() const
71			{ return fEnabled; }
72
73		//!	Returns whether an Up() is requested.
74		bool IsUpRequested() const
75			{ return fUpRequested; }
76
77		virtual status_t Control(uint32 op, void *data, size_t length);
78		virtual status_t StackControl(uint32 op, void *data);
79			// called by netstack (forwarded by KPPPInterface)
80
81		/*!	\brief Bring this protocol up.
82
83			You must call \c UpStarted() from here.
84		*/
85		virtual bool Up() = 0;
86		/*!	\brief Bring this protocol down.
87
88			You must call DownStarted() from here. \n
89			If ConnectOnDemand is supported check for ConnectOnDemand settings change.
90		*/
91		virtual bool Down() = 0;
92		//!	Is this protocol up?
93		bool IsUp() const
94			{ return fConnectionPhase == PPP_ESTABLISHED_PHASE; }
95		//!	Is this protocol down?
96		bool IsDown() const
97			{ return fConnectionPhase == PPP_DOWN_PHASE; }
98		//!	Is this protocol going up?
99		bool IsGoingUp() const
100			{ return fConnectionPhase == PPP_ESTABLISHMENT_PHASE; }
101		//!	Is this protocol going down?
102		bool IsGoingDown() const
103			{ return fConnectionPhase == PPP_TERMINATION_PHASE; }
104
105		virtual bool IsAllowedToSend() const;
106
107		//!	Encapsulate the packet with the given protocol number.
108		virtual status_t Send(net_buffer *packet, uint16 protocolNumber) = 0;
109		//!	Receive a packet with the given protocol number.
110		virtual status_t Receive(net_buffer *packet, uint16 protocolNumber) = 0;
111
112	protected:
113		//!	\brief Requests Up() to be called.
114		void SetUpRequested(bool requested = true)
115			{ fUpRequested = requested; }
116
117		// Report that we are going up/down
118		// (from now on, the Up() process can be aborted).
119		void UpStarted();
120		void DownStarted();
121
122		// report up/down events
123		void UpFailedEvent();
124		void UpEvent();
125		void DownEvent();
126
127	protected:
128		ppp_side fSide;
129
130	private:
131		ppp_phase fActivationPhase;
132		uint16 fProtocolNumber;
133		int32 fAddressFamily;
134		KPPPInterface& fInterface;
135		driver_parameter *fSettings;
136		int32 fFlags;
137		char *fType;
138		KPPPOptionHandler *fOptionHandler;
139
140		KPPPProtocol *fNextProtocol;
141		bool fEnabled;
142		bool fUpRequested;
143		ppp_phase fConnectionPhase;
144};
145
146
147#endif
148