1/*
2 * Copyright 2003-2007, Waldemar Kornewald <wkornew@gmx.net>
3 * Distributed under the terms of the MIT License.
4 */
5
6/*!	\class KPPPLayer
7	\brief An abstract layer that can encapsulate/send and receive packets.
8
9	All packet handlers should derive from this class. It does not define a protocol
10	number like KPPPProtocol does. It only has a header overhead and an encapsulation
11	level that is used to determine the order at which the packets get encapsulated
12	by the layers. If this layer does not encapsulate PPP packets you should use
13	PPP_PROTOCOL_LEVEL.
14*/
15
16#ifdef _KERNEL_MODE
17	#include <kernel_cpp.h>
18#endif
19
20#include <KPPPLayer.h>
21
22#include <net_buffer.h>
23
24#include <cstdlib>
25#include <cstring>
26
27extern net_buffer_module_info *gBufferModule;
28
29/*!	\brief Creates a new layer.
30
31	If an error occurs in the constructor you should set \c fInitStatus.
32*/
33KPPPLayer::KPPPLayer(const char *name, ppp_level level, uint32 overhead)
34	: fInitStatus(B_OK),
35	fOverhead(overhead),
36	fName(NULL),
37	fLevel(level),
38	fNext(NULL)
39{
40	SetName(name);
41}
42
43
44//!	Only frees the name.
45KPPPLayer::~KPPPLayer()
46{
47	free(fName);
48}
49
50
51//!	Returns \c fInitStatus. May be overridden to return status-dependend errors.
52status_t
53KPPPLayer::InitCheck() const
54{
55	return fInitStatus;
56}
57
58
59//!	Sends a packet to the next layer in the chain.
60status_t
61KPPPLayer::SendToNext(net_buffer *packet, uint16 protocolNumber) const
62{
63	if (!packet)
64		return B_ERROR;
65
66	// Find the next possible handler for this packet.
67	// Normal protocols (Level() >= PPP_PROTOCOL_LEVEL) do not encapsulate anything.
68	if (Next()) {
69		if (Next()->IsAllowedToSend() && Next()->Level() < PPP_PROTOCOL_LEVEL)
70			return Next()->Send(packet, protocolNumber);
71		else
72			return Next()->SendToNext(packet, protocolNumber);
73	} else {
74		ERROR("KPPPLayer: SendToNext() failed because there is no next handler!\n");
75		gBufferModule->free(packet);
76		return B_ERROR;
77	}
78}
79
80
81/*!	\brief You may override this for periodic tasks.
82
83	This method gets called every \c PPP_PULSE_RATE microseconds.
84*/
85void
86KPPPLayer::Pulse()
87{
88	// do nothing by default
89}
90
91
92//!	Allows changing the name of this layer.
93void
94KPPPLayer::SetName(const char *name)
95{
96	free(fName);
97
98	if (name)
99		fName = strdup(name);
100	else
101		fName = NULL;
102}
103