xref: /haiku/src/add-ons/kernel/network/ppp/shared/libkernelppp/KPPPDevice.cpp (revision 3e216965baa8d58a67bf7372e2bfa13d999f5a9d)
1 /*
2  * Copyright 2003-2004, Waldemar Kornewald <wkornew@gmx.net>
3  * Distributed under the terms of the MIT License.
4  */
5 
6 /*!	\class KPPPDevice
7 	\brief Represents a device at the lowest level of the communcation stack.
8 
9 	A device may be, for example: Modem, PPPoE, PPTP. \n
10 	It encapsulates the packet and sends it over a line to the other end.
11 	The device is the first layer that receives a packet.
12 */
13 
14 #include <KPPPDevice.h>
15 
16 #include <net/if.h>
17 #include <core_funcs.h>
18 
19 #include <PPPControl.h>
20 
21 
22 /*!	\brief Initializes the device.
23 
24 	\param name The device's type name (e.g.: PPPoE).
25 	\param overhead Length of the header that is prepended to each packet.
26 	\param interface Owning interface.
27 	\param settings Device's settings.
28 */
29 KPPPDevice::KPPPDevice(const char *name, uint32 overhead, KPPPInterface& interface,
30 		driver_parameter *settings)
31 	: KPPPLayer(name, PPP_DEVICE_LEVEL, overhead),
32 	fMTU(1500),
33 	fInterface(interface),
34 	fSettings(settings),
35 	fConnectionPhase(PPP_DOWN_PHASE)
36 {
37 }
38 
39 
40 //!	Destructor. Removes device from interface.
41 KPPPDevice::~KPPPDevice()
42 {
43 	if(Interface().Device() == this)
44 		Interface().SetDevice(NULL);
45 }
46 
47 
48 /*!	\brief Allows private extensions.
49 
50 	If you override this method you must call the parent's method for unknown ops.
51 */
52 status_t
53 KPPPDevice::Control(uint32 op, void *data, size_t length)
54 {
55 	switch(op) {
56 		case PPPC_GET_DEVICE_INFO: {
57 			if(length < sizeof(ppp_device_info_t) || !data)
58 				return B_NO_MEMORY;
59 
60 			ppp_device_info *info = (ppp_device_info*) data;
61 			memset(info, 0, sizeof(ppp_device_info_t));
62 			if(Name())
63 				strncpy(info->name, Name(), PPP_HANDLER_NAME_LENGTH_LIMIT);
64 			info->MTU = MTU();
65 			info->inputTransferRate = InputTransferRate();
66 			info->outputTransferRate = OutputTransferRate();
67 			info->outputBytesCount = CountOutputBytes();
68 			info->isUp = IsUp();
69 		} break;
70 
71 		default:
72 			return B_BAD_VALUE;
73 	}
74 
75 	return B_OK;
76 }
77 
78 
79 //!	Returns \c true (indicates to KPPPInterface we are always allowed to send).
80 bool
81 KPPPDevice::IsAllowedToSend() const
82 {
83 	return true;
84 		// our connection status will be reported in Send()
85 }
86 
87 
88 //!	This method is never used.
89 status_t
90 KPPPDevice::Receive(struct mbuf *packet, uint16 protocolNumber)
91 {
92 	// let the interface handle the packet
93 	if(protocolNumber == 0)
94 		return Interface().ReceiveFromDevice(packet);
95 	else
96 		return Interface().Receive(packet, protocolNumber);
97 }
98 
99 
100 /*!	\brief Report that device is going up.
101 
102 	Called by Up(). \n
103 	From now on, the connection attempt can may be aborted by calling Down().
104 
105 	\return
106 		- \c true: You are allowed to connect.
107 		- \c false: You should abort immediately. Down() will \e not be called!
108 */
109 bool
110 KPPPDevice::UpStarted()
111 {
112 	fConnectionPhase = PPP_ESTABLISHMENT_PHASE;
113 
114 	return Interface().StateMachine().TLSNotify();
115 }
116 
117 
118 /*!	\brief Report that device is going down.
119 
120 	Called by Down().
121 
122 	\return
123 		- \c true: You are allowed to disconnect.
124 		- \c false: You must not disconnect!
125 */
126 bool
127 KPPPDevice::DownStarted()
128 {
129 	fConnectionPhase = PPP_TERMINATION_PHASE;
130 
131 	return Interface().StateMachine().TLFNotify();
132 }
133 
134 
135 //!	Reports that device failed going up. May only be called after Up() was called.
136 void
137 KPPPDevice::UpFailedEvent()
138 {
139 	fConnectionPhase = PPP_DOWN_PHASE;
140 
141 	Interface().StateMachine().UpFailedEvent();
142 }
143 
144 
145 //!	Reports that device went up. May only be called after Up() was called.
146 void
147 KPPPDevice::UpEvent()
148 {
149 	fConnectionPhase = PPP_ESTABLISHED_PHASE;
150 
151 	Interface().StateMachine().UpEvent();
152 }
153 
154 
155 //!	Reports that device went down. This may be called to indicate connection loss.
156 void
157 KPPPDevice::DownEvent()
158 {
159 	fConnectionPhase = PPP_DOWN_PHASE;
160 
161 	Interface().StateMachine().DownEvent();
162 }
163