xref: /haiku/src/add-ons/kernel/network/ppp/shared/libkernelppp/KPPPProtocol.cpp (revision 67bce78b48ed6d01b5a8eef89f5694c372b7e0a1)
1 //-----------------------------------------------------------------------
2 //  This software is part of the OpenBeOS distribution and is covered
3 //  by the OpenBeOS license.
4 //
5 //  Copyright (c) 2003-2004 Waldemar Kornewald, Waldemar.Kornewald@web.de
6 //-----------------------------------------------------------------------
7 
8 #include <KPPPInterface.h>
9 #include <KPPPUtils.h>
10 #include <PPPControl.h>
11 #include "settings_tools.h"
12 
13 #include <cstring>
14 
15 
16 KPPPProtocol::KPPPProtocol(const char *name, ppp_phase activationPhase,
17 		uint16 protocolNumber, ppp_level level, int32 addressFamily,
18 		uint32 overhead, KPPPInterface& interface,
19 		driver_parameter *settings, int32 flags = PPP_NO_FLAGS,
20 		const char *type = NULL, KPPPOptionHandler *optionHandler = NULL)
21 	: KPPPLayer(name, level, overhead),
22 	fActivationPhase(activationPhase),
23 	fProtocolNumber(protocolNumber),
24 	fAddressFamily(addressFamily),
25 	fInterface(interface),
26 	fSettings(settings),
27 	fFlags(flags),
28 	fOptionHandler(optionHandler),
29 	fNextProtocol(NULL),
30 	fEnabled(true),
31 	fUpRequested(true),
32 	fConnectionPhase(PPP_DOWN_PHASE)
33 {
34 	if(type)
35 		fType = strdup(type);
36 	else
37 		fType = NULL;
38 
39 	const char *sideString = get_parameter_value("side", settings);
40 	if(sideString)
41 		fSide = get_side_string_value(sideString, PPP_LOCAL_SIDE);
42 	else {
43 		if(interface.Mode() == PPP_CLIENT_MODE)
44 			fSide = PPP_LOCAL_SIDE;
45 		else
46 			fSide = PPP_PEER_SIDE;
47 	}
48 }
49 
50 
51 KPPPProtocol::~KPPPProtocol()
52 {
53 	Interface().RemoveProtocol(this);
54 
55 	free(fType);
56 }
57 
58 
59 void
60 KPPPProtocol::Uninit()
61 {
62 	// do nothing by default
63 }
64 
65 
66 status_t
67 KPPPProtocol::Control(uint32 op, void *data, size_t length)
68 {
69 	switch(op) {
70 		case PPPC_GET_PROTOCOL_INFO: {
71 			if(length < sizeof(ppp_protocol_info_t) || !data)
72 				return B_ERROR;
73 
74 			ppp_protocol_info *info = (ppp_protocol_info*) data;
75 			memset(info, 0, sizeof(ppp_protocol_info_t));
76 			if(Name())
77 				strncpy(info->name, Name(), PPP_HANDLER_NAME_LENGTH_LIMIT);
78 			if(Type())
79 				strncpy(info->type, Type(), PPP_HANDLER_NAME_LENGTH_LIMIT);
80 			info->activationPhase = ActivationPhase();
81 			info->addressFamily = AddressFamily();
82 			info->flags = Flags();
83 			info->side = Side();
84 			info->level = Level();
85 			info->overhead = Overhead();
86 			info->connectionPhase = fConnectionPhase;
87 			info->protocolNumber = ProtocolNumber();
88 			info->isEnabled = IsEnabled();
89 			info->isUpRequested = IsUpRequested();
90 		} break;
91 
92 		case PPPC_ENABLE:
93 			if(length < sizeof(uint32) || !data)
94 				return B_ERROR;
95 
96 			SetEnabled(*((uint32*)data));
97 		break;
98 
99 		default:
100 			return B_BAD_VALUE;
101 	}
102 
103 	return B_OK;
104 }
105 
106 
107 status_t
108 KPPPProtocol::StackControl(uint32 op, void *data)
109 {
110 	switch(op) {
111 		default:
112 			return B_BAD_VALUE;
113 	}
114 
115 	return B_OK;
116 }
117 
118 
119 void
120 KPPPProtocol::SetEnabled(bool enabled = true)
121 {
122 	fEnabled = enabled;
123 
124 	if(!enabled) {
125 		if(IsUp() || IsGoingUp())
126 			Down();
127 	} else if(!IsUp() && !IsGoingUp() && IsUpRequested() && Interface().IsUp())
128 		Up();
129 }
130 
131 
132 bool
133 KPPPProtocol::IsAllowedToSend() const
134 {
135 	return IsEnabled() && IsUp() && IsProtocolAllowed(*this);
136 }
137 
138 
139 void
140 KPPPProtocol::UpStarted()
141 {
142 	fConnectionPhase = PPP_ESTABLISHMENT_PHASE;
143 }
144 
145 
146 void
147 KPPPProtocol::DownStarted()
148 {
149 	fConnectionPhase = PPP_TERMINATION_PHASE;
150 }
151 
152 
153 void
154 KPPPProtocol::UpFailedEvent()
155 {
156 	fConnectionPhase = PPP_DOWN_PHASE;
157 
158 	Interface().StateMachine().UpFailedEvent(this);
159 }
160 
161 
162 void
163 KPPPProtocol::UpEvent()
164 {
165 	fConnectionPhase = PPP_ESTABLISHED_PHASE;
166 
167 	Interface().StateMachine().UpEvent(this);
168 }
169 
170 
171 void
172 KPPPProtocol::DownEvent()
173 {
174 	fConnectionPhase = PPP_DOWN_PHASE;
175 
176 	Interface().StateMachine().DownEvent(this);
177 }
178