xref: /haiku/src/add-ons/kernel/network/ppp/shared/libkernelppp/headers/KPPPProtocol.h (revision bab64f65bb775dc23060e276f1f1c4498ab7af6c)
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 
12 class KPPPInterface;
13 class KPPPOptionHandler;
14 
15 
16 class 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.
Interface()31 		KPPPInterface& Interface() const
32 			{ return fInterface; }
33 		//!	Returns the protocol's settings.
Settings()34 		driver_parameter *Settings() const
35 			{ return fSettings; }
36 
37 		//!	The activation phase is the phase when Up() should be called.
ActivationPhase()38 		ppp_phase ActivationPhase() const
39 			{ return fActivationPhase; }
40 
41 		//!	The protocol number.
ProtocolNumber()42 		uint16 ProtocolNumber() const
43 			{ return fProtocolNumber; }
44 		//!	The address family. Negative values and values > 0xFF are ignored.
AddressFamily()45 		int32 AddressFamily() const
46 			{ return fAddressFamily; }
47 		//!	This protocol's flags.
Flags()48 		int32 Flags() const
49 			{ return fFlags; }
50 		//!	Which side this protocol works for.
Side()51 		ppp_side Side() const
52 			{ return fSide; }
53 
54 		//!	Protocol type. May be NULL.
Type()55 		const char *Type() const
56 			{ return fType; }
57 		//!	The (optional) KPPPOptionHandler object of this protocol.
OptionHandler()58 		KPPPOptionHandler *OptionHandler() const
59 			{ return fOptionHandler; }
60 
61 		//!	Sets the next protocol in the list.
SetNextProtocol(KPPPProtocol * protocol)62 		void SetNextProtocol(KPPPProtocol *protocol)
63 			{ fNextProtocol = protocol; SetNext(protocol); }
64 		//!	Returns the next protocol in the list.
NextProtocol()65 		KPPPProtocol *NextProtocol() const
66 			{ return fNextProtocol; }
67 
68 		void SetEnabled(bool enabled = true);
69 		//!	Returns whether this protocol is enabled.
IsEnabled()70 		bool IsEnabled() const
71 			{ return fEnabled; }
72 
73 		//!	Returns whether an Up() is requested.
IsUpRequested()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?
IsUp()93 		bool IsUp() const
94 			{ return fConnectionPhase == PPP_ESTABLISHED_PHASE; }
95 		//!	Is this protocol down?
IsDown()96 		bool IsDown() const
97 			{ return fConnectionPhase == PPP_DOWN_PHASE; }
98 		//!	Is this protocol going up?
IsGoingUp()99 		bool IsGoingUp() const
100 			{ return fConnectionPhase == PPP_ESTABLISHMENT_PHASE; }
101 		//!	Is this protocol going down?
IsGoingDown()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