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_INTERFACE__H 9 #define _K_PPP_INTERFACE__H 10 11 #include <driver_settings.h> 12 13 #include <KPPPDefs.h> 14 15 #ifndef _K_PPP_LCP__H 16 #include <KPPPLCP.h> 17 #endif 18 19 #include <KPPPReportManager.h> 20 21 #ifndef _K_PPP_STATE_MACHINE__H 22 #include <KPPPStateMachine.h> 23 #endif 24 25 #include <List.h> 26 #include <LockerHelper.h> 27 28 class PPPDevice; 29 class PPPProtocol; 30 class PPPOptionHandler; 31 32 struct ppp_interface_module_info; 33 struct ppp_module_info; 34 35 36 class PPPInterface : public PPPLayer { 37 friend class PPPStateMachine; 38 friend class PPPManager; 39 40 private: 41 // copies are not allowed! 42 PPPInterface(const PPPInterface& copy); 43 PPPInterface& operator= (const PPPInterface& copy); 44 45 // only PPPManager may construct us! 46 PPPInterface(interface_id ID, const driver_settings *settings, 47 PPPInterface *parent = NULL); 48 ~PPPInterface(); 49 50 public: 51 void Delete(); 52 53 virtual status_t InitCheck() const; 54 55 interface_id ID() const 56 { return fID; } 57 58 driver_settings* Settings() const 59 { return fSettings; } 60 61 PPPStateMachine& StateMachine() 62 { return fStateMachine; } 63 PPPLCP& LCP() 64 { return fLCP; } 65 66 struct ifnet *Ifnet() const 67 { return fIfnet; } 68 69 // delays 70 uint32 DialRetryDelay() const 71 { return fDialRetryDelay; } 72 uint32 RedialDelay() const 73 { return fRedialDelay; } 74 75 // idle handling 76 uint32 IdleSince() const 77 { return fIdleSince; } 78 uint32 DisconnectAfterIdleSince() const 79 { return fDisconnectAfterIdleSince; } 80 81 bool SetMRU(uint32 MRU); 82 uint32 MRU() const 83 { return fMRU; } 84 // this is the smallest MRU that we and the peer have 85 bool SetInterfaceMTU(uint32 interfaceMTU) 86 { return SetMRU(interfaceMTU - fHeaderLength); } 87 uint32 InterfaceMTU() const 88 { return fInterfaceMTU; } 89 // this is the MRU including protocol overhead 90 91 virtual status_t Control(uint32 op, void *data, size_t length); 92 93 bool SetDevice(PPPDevice *device); 94 PPPDevice *Device() const 95 { return fDevice; } 96 97 bool AddProtocol(PPPProtocol *protocol); 98 bool RemoveProtocol(PPPProtocol *protocol); 99 int32 CountProtocols() const; 100 PPPProtocol *ProtocolAt(int32 index) const; 101 PPPProtocol *FirstProtocol() const 102 { return fFirstProtocol; } 103 PPPProtocol *ProtocolFor(uint16 protocolNumber, PPPProtocol *start = NULL) const; 104 105 // multilink methods 106 bool AddChild(PPPInterface *child); 107 bool RemoveChild(PPPInterface *child); 108 int32 CountChildren() const 109 { return fChildren.CountItems(); } 110 PPPInterface *ChildAt(int32 index) const; 111 PPPInterface *Parent() const 112 { return fParent; } 113 bool IsMultilink() const 114 { return fIsMultilink; } 115 116 void SetAutoRedial(bool autoRedial = true); 117 bool DoesAutoRedial() const 118 { return fAutoRedial; } 119 120 void SetDialOnDemand(bool dialOnDemand = true); 121 bool DoesDialOnDemand() const 122 { return fDialOnDemand; } 123 124 ppp_mode Mode() const 125 { return fMode; } 126 // client or server mode? 127 ppp_state State() const 128 { return fStateMachine.State(); } 129 ppp_phase Phase() const 130 { return fStateMachine.Phase(); } 131 132 // Protocol-Field-Compression 133 bool SetPFCOptions(uint8 pfcOptions); 134 uint8 PFCOptions() const 135 { return fPFCOptions; } 136 ppp_pfc_state LocalPFCState() const 137 { return fLocalPFCState; } 138 // the local PFC state says if we accepted a request from the peer 139 // i.e.: we may use PFC in outgoing packets 140 ppp_pfc_state PeerPFCState() const 141 { return fPeerPFCState; } 142 // the peer PFC state says if the peer accepted a request us 143 // i.e.: the peer might send PFC-compressed packets to us 144 bool UseLocalPFC() const 145 { return LocalPFCState() & PPP_PFC_ACCEPTED; } 146 147 virtual bool Up(); 148 // in server mode Up() listens for an incoming connection 149 virtual bool Down(); 150 bool IsUp() const; 151 152 PPPReportManager& ReportManager() 153 { return fReportManager; } 154 bool Report(ppp_report_type type, int32 code, void *data, int32 length) 155 { return ReportManager().Report(type, code, data, length); } 156 // returns false if reply was bad (or an error occured) 157 158 bool LoadModules(driver_settings *settings, 159 int32 start, int32 count); 160 bool LoadModule(const char *name, driver_parameter *parameter, 161 ppp_module_key_type type); 162 163 virtual bool IsAllowedToSend() const; 164 // always returns true 165 166 virtual status_t Send(struct mbuf *packet, uint16 protocolNumber); 167 // sends the packet to the next handler (normally the device) 168 virtual status_t Receive(struct mbuf *packet, uint16 protocolNumber); 169 170 status_t ReceiveFromDevice(struct mbuf *packet); 171 // This must not block PPPDevice::Send()! 172 173 void Pulse(); 174 // this manages all timeouts, etc. 175 176 private: 177 bool RegisterInterface(); 178 // adds us to the manager module and 179 // saves the returned ifnet structure 180 bool UnregisterInterface(); 181 182 status_t StackControl(uint32 op, void *data); 183 // stack routes ioctls to interface 184 status_t StackControlEachHandler(uint32 op, void *data); 185 // this calls StackControl() with the given parameters for each handler 186 187 void CalculateInterfaceMTU(); 188 void CalculateBaudRate(); 189 190 // these two methods are used by the open/close_event_threads 191 void CallOpenEvent() 192 { StateMachine().OpenEvent(); } 193 void CallCloseEvent() 194 { StateMachine().CloseEvent(); } 195 196 void Redial(uint32 delay); 197 198 // multilink methods 199 void SetParent(PPPInterface *parent) 200 { fParent = parent; } 201 202 private: 203 interface_id fID; 204 // the manager assigns an ID to every interface 205 driver_settings *fSettings; 206 struct ifnet *fIfnet; 207 208 thread_id fUpThread, fOpenEventThread, fCloseEventThread; 209 210 thread_id fRedialThread; 211 uint32 fDialRetry, fDialRetriesLimit; 212 uint32 fDialRetryDelay, fRedialDelay; 213 214 ppp_interface_module_info *fManager; 215 216 uint32 fIdleSince, fDisconnectAfterIdleSince; 217 uint32 fMRU, fInterfaceMTU, fHeaderLength; 218 219 PPPInterface *fParent; 220 List<PPPInterface*> fChildren; 221 bool fIsMultilink; 222 223 bool fAutoRedial, fDialOnDemand; 224 225 ppp_mode fMode; 226 ppp_pfc_state fLocalPFCState, fPeerPFCState; 227 uint8 fPFCOptions; 228 229 PPPDevice *fDevice; 230 PPPProtocol *fFirstProtocol; 231 List<char*> fModules; 232 233 PPPStateMachine fStateMachine; 234 PPPLCP fLCP; 235 PPPReportManager fReportManager; 236 BLocker& fLock; 237 int32 fDeleteCounter; 238 }; 239 240 241 #endif 242