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