xref: /haiku/src/add-ons/kernel/network/ppp/shared/libkernelppp/headers/KPPPStateMachine.h (revision d5cd5d63ff0ad395989db6cf4841a64d5b545d1d)
1 //----------------------------------------------------------------------
2 //  This software is part of the OpenBeOS distribution and is covered
3 //  by the OpenBeOS license.
4 //
5 //  Copyright (c) 2003 Waldemar Kornewald, Waldemar.Kornewald@web.de
6 //---------------------------------------------------------------------
7 
8 #ifndef _K_PPP_STATE_MACHINE__H
9 #define _K_PPP_STATE_MACHINE__H
10 
11 #include <KPPPDefs.h>
12 
13 class PPPProtocol;
14 
15 #ifndef _K_PPP_INTERFACE__H
16 #include <KPPPInterface.h>
17 #endif
18 
19 #include <Locker.h>
20 
21 
22 class PPPStateMachine {
23 		friend class PPPInterface;
24 		friend class PPPLCP;
25 
26 	private:
27 		// may only be constructed/destructed by PPPInterface
28 		PPPStateMachine(PPPInterface& interface);
29 		~PPPStateMachine();
30 
31 		// copies are not allowed!
32 		PPPStateMachine(const PPPStateMachine& copy);
33 		PPPStateMachine& operator= (const PPPStateMachine& copy);
34 
35 	public:
36 		PPPInterface& Interface() const
37 			{ return fInterface; }
38 		PPPLCP& LCP() const
39 			{ return fLCP; }
40 
41 		ppp_state State() const
42 			{ return fState; }
43 		ppp_phase Phase() const
44 			{ return fPhase; }
45 
46 		uint8 NextID();
47 			// return the next id for LCP packets
48 
49 		void SetMagicNumber(uint32 magicNumber)
50 			{ fMagicNumber = magicNumber; }
51 		uint32 MagicNumber() const
52 			{ return fMagicNumber; }
53 
54 		// public actions
55 		bool Reconfigure();
56 		bool SendEchoRequest();
57 		bool SendDiscardRequest();
58 
59 		// public events:
60 		// NOTE: Local/PeerAuthenticationAccepted/Denied MUST be called before
61 		// Up(Failed)/DownEvent in order to allow changing the configuration of
62 		// the next phase's protocols before they are brought up.
63 		void LocalAuthenticationRequested();
64 		void LocalAuthenticationAccepted(const char *name);
65 		void LocalAuthenticationDenied(const char *name);
66 		const char *LocalAuthenticationName() const
67 			{ return fLocalAuthenticationName; }
68 		ppp_authentication_status LocalAuthenticationStatus() const
69 			{ return fLocalAuthenticationStatus; }
70 
71 		void PeerAuthenticationRequested();
72 		void PeerAuthenticationAccepted(const char *name);
73 		void PeerAuthenticationDenied(const char *name);
74 		const char *PeerAuthenticationName() const
75 			{ return fPeerAuthenticationName; }
76 		ppp_authentication_status PeerAuthenticationStatus() const
77 			{ return fPeerAuthenticationStatus; }
78 
79 		// sub-interface events
80 		void UpFailedEvent(PPPInterface& interface);
81 		void UpEvent(PPPInterface& interface);
82 		void DownEvent(PPPInterface& interface);
83 
84 		// protocol events
85 		void UpFailedEvent(PPPProtocol *protocol);
86 		void UpEvent(PPPProtocol *protocol);
87 		void DownEvent(PPPProtocol *protocol);
88 
89 		// device events
90 		bool TLSNotify();
91 		bool TLFNotify();
92 		void UpFailedEvent();
93 		void UpEvent();
94 		void DownEvent();
95 
96 	private:
97 		// private StateMachine methods
98 		void NewState(ppp_state next);
99 		void NewPhase(ppp_phase next);
100 
101 		// private events
102 		void OpenEvent();
103 		void CloseEvent();
104 		void TOGoodEvent();
105 		void TOBadEvent();
106 		void RCRGoodEvent(struct mbuf *packet);
107 		void RCRBadEvent(struct mbuf *nak, struct mbuf *reject);
108 		void RCAEvent(struct mbuf *packet);
109 		void RCNEvent(struct mbuf *packet);
110 		void RTREvent(struct mbuf *packet);
111 		void RTAEvent(struct mbuf *packet);
112 		void RUCEvent(struct mbuf *packet, uint16 protocolNumber,
113 			uint8 code = PPP_PROTOCOL_REJECT);
114 		void RXJGoodEvent(struct mbuf *packet);
115 		void RXJBadEvent(struct mbuf *packet);
116 		void RXREvent(struct mbuf *packet);
117 
118 		// general events (for Good/Bad events)
119 		void TimerEvent();
120 		void RCREvent(struct mbuf *packet);
121 		void RXJEvent(struct mbuf *packet);
122 
123 		// actions
124 		void IllegalEvent(ppp_event event);
125 		void ThisLayerUp();
126 		void ThisLayerDown();
127 		void ThisLayerStarted();
128 		void ThisLayerFinished();
129 		void InitializeRestartCount();
130 		void ZeroRestartCount();
131 		bool SendConfigureRequest();
132 		bool SendConfigureAck(struct mbuf *packet);
133 		bool SendConfigureNak(struct mbuf *packet);
134 		bool SendTerminateRequest();
135 		bool SendTerminateAck(struct mbuf *request = NULL);
136 		bool SendCodeReject(struct mbuf *packet, uint16 protocolNumber, uint8 code);
137 		bool SendEchoReply(struct mbuf *request);
138 
139 		void BringProtocolsUp();
140 		uint32 BringPhaseUp();
141 
142 		void DownProtocols();
143 		void ResetLCPHandlers();
144 
145 	private:
146 		PPPInterface& fInterface;
147 		PPPLCP& fLCP;
148 
149 		ppp_state fState;
150 		ppp_phase fPhase;
151 
152 		vint32 fID;
153 		uint32 fMagicNumber;
154 
155 		ppp_authentication_status fLocalAuthenticationStatus,
156 			fPeerAuthenticationStatus;
157 		char *fLocalAuthenticationName, *fPeerAuthenticationName;
158 
159 		// counters and timers
160 		int32 fMaxRequest, fMaxTerminate, fMaxNak;
161 		int32 fRequestCounter, fTerminateCounter, fNakCounter;
162 		uint8 fRequestID, fTerminateID, fEchoID;
163 			// the ID we used for the last configure/terminate/echo request
164 		bigtime_t fNextTimeout;
165 
166 		BLocker fLock;
167 };
168 
169 
170 #endif
171