xref: /haiku/src/add-ons/kernel/network/ppp/shared/libkernelppp/_KPPPPFCHandler.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 "_KPPPPFCHandler.h"
9 
10 #include <KPPPConfigurePacket.h>
11 
12 
13 static const uint8 kPFCType = 0x7;
14 
15 
16 _KPPPPFCHandler::_KPPPPFCHandler(ppp_pfc_state& localPFCState,
17 		ppp_pfc_state& peerPFCState, KPPPInterface& interface)
18 	: KPPPOptionHandler("PFC Handler", kPFCType, interface, NULL),
19 	fLocalPFCState(localPFCState),
20 	fPeerPFCState(peerPFCState)
21 {
22 }
23 
24 
25 status_t
26 _KPPPPFCHandler::AddToRequest(KPPPConfigurePacket& request)
27 {
28 	// is PFC not requested or was it rejected?
29 	if(fLocalPFCState == PPP_PFC_REJECTED
30 			|| (Interface().PFCOptions() & PPP_REQUEST_PFC) == 0)
31 		return B_OK;
32 
33 	// add PFC request
34 	ppp_configure_item item;
35 	item.type = kPFCType;
36 	item.length = 2;
37 	return request.AddItem(&item) ? B_OK : B_ERROR;
38 }
39 
40 
41 status_t
42 _KPPPPFCHandler::ParseNak(const KPPPConfigurePacket& nak)
43 {
44 	// naks do not contain PFC items
45 	if(nak.ItemWithType(kPFCType))
46 		return B_ERROR;
47 
48 	return B_OK;
49 }
50 
51 
52 status_t
53 _KPPPPFCHandler::ParseReject(const KPPPConfigurePacket& reject)
54 {
55 	if(reject.ItemWithType(kPFCType)) {
56 		fLocalPFCState = PPP_PFC_REJECTED;
57 
58 		if(Interface().PFCOptions() & PPP_FORCE_PFC_REQUEST)
59 			return B_ERROR;
60 	}
61 
62 	return B_OK;
63 }
64 
65 
66 status_t
67 _KPPPPFCHandler::ParseAck(const KPPPConfigurePacket& ack)
68 {
69 	if(ack.ItemWithType(kPFCType))
70 		fLocalPFCState = PPP_PFC_ACCEPTED;
71 	else {
72 		fLocalPFCState = PPP_PFC_DISABLED;
73 
74 		if(Interface().PFCOptions() & PPP_FORCE_PFC_REQUEST)
75 			return B_ERROR;
76 	}
77 
78 	return B_OK;
79 }
80 
81 
82 status_t
83 _KPPPPFCHandler::ParseRequest(const KPPPConfigurePacket& request,
84 	int32 index, KPPPConfigurePacket& nak, KPPPConfigurePacket& reject)
85 {
86 	if(!request.ItemWithType(kPFCType))
87 		return B_OK;
88 
89 	if((Interface().PFCOptions() & PPP_ALLOW_PFC) == 0) {
90 		ppp_configure_item item;
91 		item.type = kPFCType;
92 		item.length = 2;
93 		return reject.AddItem(&item) ? B_OK : B_ERROR;
94 	}
95 
96 	return B_OK;
97 }
98 
99 
100 status_t
101 _KPPPPFCHandler::SendingAck(const KPPPConfigurePacket& ack)
102 {
103 	ppp_configure_item *item = ack.ItemWithType(kPFCType);
104 
105 	if(item && (Interface().PFCOptions() & PPP_ALLOW_PFC) == 0)
106 		return B_ERROR;
107 
108 	if(item)
109 		fPeerPFCState = PPP_PFC_ACCEPTED;
110 	else
111 		fPeerPFCState = PPP_PFC_DISABLED;
112 
113 	return B_OK;
114 }
115 
116 
117 void
118 _KPPPPFCHandler::Reset()
119 {
120 	fLocalPFCState = fPeerPFCState = PPP_PFC_DISABLED;
121 }
122