1/*
2 * Copyright 2003-2004, Haiku Inc.
3 * Distributed under the terms of the MIT License.
4 */
5
6#ifndef _K_PPP_STATE_MACHINE__H
7#define _K_PPP_STATE_MACHINE__H
8
9#include <KPPPDefs.h>
10
11class KPPPProtocol;
12
13#ifndef _K_PPP_INTERFACE__H
14#include <KPPPInterface.h>
15#endif
16
17#include <lock.h>
18#include <util/AutoLock.h>
19
20class PPPManager;
21class KPPPInterface;
22class KPPPLCP;
23
24
25class KPPPStateMachine {
26		friend class PPPManager;
27		friend class KPPPInterface;
28		friend class KPPPLCP;
29
30	private:
31		// may only be constructed/destructed by KPPPInterface
32		KPPPStateMachine(KPPPInterface& interface);
33		~KPPPStateMachine();
34
35		// copies are not allowed!
36		KPPPStateMachine(const KPPPStateMachine& copy);
37		KPPPStateMachine& operator= (const KPPPStateMachine& copy);
38
39	public:
40		//!	Returns the interface that owns this state machine.
41		KPPPInterface& Interface() const
42			{ return fInterface; }
43		//!	Returns the LCP protocol object belonging to this state machine.
44		KPPPLCP& LCP() const
45			{ return fLCP; }
46
47		//!	Returns the current state as defined in RFC 1661.
48		ppp_state State() const
49			{ return fState; }
50		//!	Returns the internal phase.
51		ppp_phase Phase() const
52			{ return fPhase; }
53
54		uint8 NextID();
55
56		//!	Sets our packets' magic number. Used by Link-Quality-Monitoring.
57		void SetMagicNumber(uint32 magicNumber)
58			{ fMagicNumber = magicNumber; }
59		//!	Returns our packets' magic number.
60		uint32 MagicNumber() const
61			{ return fMagicNumber; }
62
63		// public actions
64		bool Reconfigure();
65		bool SendEchoRequest();
66		bool SendDiscardRequest();
67
68		// public events:
69		// NOTE: Local/PeerAuthenticationAccepted/Denied MUST be called before
70		// Up(Failed)/DownEvent in order to allow changing the configuration of
71		// the next phase's protocols before they are brought up.
72		void LocalAuthenticationRequested();
73		void LocalAuthenticationAccepted(const char *name);
74		void LocalAuthenticationDenied(const char *name);
75		//!	Returns the name/login string we used for authentication.
76		const char *LocalAuthenticationName() const
77			{ return fLocalAuthenticationName; }
78		//!	Returns our local authentication status.
79		ppp_authentication_status LocalAuthenticationStatus() const
80			{ return fLocalAuthenticationStatus; }
81
82		void PeerAuthenticationRequested();
83		void PeerAuthenticationAccepted(const char *name);
84		void PeerAuthenticationDenied(const char *name);
85		//!	Returns the name/login string the peer used for authentication.
86		const char *PeerAuthenticationName() const
87			{ return fPeerAuthenticationName; }
88		//!	Returns the peer's authentication status.
89		ppp_authentication_status PeerAuthenticationStatus() const
90			{ return fPeerAuthenticationStatus; }
91
92		// sub-interface events
93		void UpFailedEvent(KPPPInterface& interface);
94		void UpEvent(KPPPInterface& interface);
95		void DownEvent(KPPPInterface& interface);
96
97		// protocol events
98		void UpFailedEvent(KPPPProtocol *protocol);
99		void UpEvent(KPPPProtocol *protocol);
100		void DownEvent(KPPPProtocol *protocol);
101
102		// device events
103		bool TLSNotify();
104		bool TLFNotify();
105		void UpFailedEvent();
106		void UpEvent();
107		void DownEvent();
108
109	private:
110		// private StateMachine methods
111		void NewState(ppp_state next);
112		void NewPhase(ppp_phase next);
113
114		// private events
115		void OpenEvent();
116		void ContinueOpenEvent();
117		void CloseEvent();
118		void TOGoodEvent();
119		void TOBadEvent();
120		void RCRGoodEvent(net_buffer *packet);
121		void RCRBadEvent(net_buffer *nak, net_buffer *reject);
122		void RCAEvent(net_buffer *packet);
123		void RCNEvent(net_buffer *packet);
124		void RTREvent(net_buffer *packet);
125		void RTAEvent(net_buffer *packet);
126		void RUCEvent(net_buffer *packet, uint16 protocolNumber,
127			uint8 code = PPP_PROTOCOL_REJECT);
128		void RXJGoodEvent(net_buffer *packet);
129		void RXJBadEvent(net_buffer *packet);
130		void RXREvent(net_buffer *packet);
131
132		// general events (for Good/Bad events)
133		void TimerEvent();
134		void RCREvent(net_buffer *packet);
135		void RXJEvent(net_buffer *packet);
136
137		// actions
138		void IllegalEvent(ppp_event event);
139		void ThisLayerUp();
140		void ThisLayerDown();
141		void ThisLayerStarted();
142		void ThisLayerFinished();
143		void InitializeRestartCount();
144		void ZeroRestartCount();
145		bool SendConfigureRequest();
146		bool SendConfigureAck(net_buffer *packet);
147		bool SendConfigureNak(net_buffer *packet);
148		bool SendTerminateRequest();
149		bool SendTerminateAck(net_buffer *request = NULL);
150		bool SendCodeReject(net_buffer *packet, uint16 protocolNumber, uint8 code);
151		bool SendEchoReply(net_buffer *request);
152
153		void BringProtocolsUp();
154		uint32 BringPhaseUp();
155
156		void DownProtocols();
157		void ResetLCPHandlers();
158
159	private:
160		KPPPInterface& fInterface;
161		KPPPLCP& fLCP;
162
163		ppp_state fState;
164		ppp_phase fPhase;
165
166		int32 fID;
167		uint32 fMagicNumber;
168		int32 fLastConnectionReportCode;
169
170		ppp_authentication_status fLocalAuthenticationStatus,
171			fPeerAuthenticationStatus;
172		char *fLocalAuthenticationName, *fPeerAuthenticationName;
173
174		// counters and timers
175		int32 fMaxRequest, fMaxTerminate, fMaxNak;
176		int32 fRequestCounter, fTerminateCounter, fNakCounter;
177		uint8 fRequestID, fTerminateID, fEchoID;
178			// the ID we used for the last configure/terminate/echo request
179		bigtime_t fNextTimeout;
180
181		mutex fLock;
182};
183
184
185#endif
186