xref: /haiku/src/add-ons/kernel/network/ppp/shared/libkernelppp/KPPPUtils.cpp (revision 92a8026e02acf7895727ebda87c3bebacd3fa082)
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 #include <OS.h>
9 
10 #include <KPPPUtils.h>
11 #include <KPPPInterface.h>
12 
13 
14 bool
15 IsProtocolAllowed(const PPPProtocol& protocol)
16 {
17 	if(protocol.ProtocolNumber() == PPP_LCP_PROTOCOL)
18 		return true;
19 	else if(protocol.Interface().State() != PPP_OPENED_STATE)
20 		return false;
21 	else if(protocol.Interface().Phase() > PPP_AUTHENTICATION_PHASE
22 			|| (protocol.Interface().Phase() >= PPP_ESTABLISHMENT_PHASE
23 				&& protocol.Flags() & PPP_ALWAYS_ALLOWED))
24 		return true;
25 	else
26 		return false;
27 }
28 
29 
30 // These are very simple send/receive_data functions with a timeout
31 // and there is a race condition beween has_data() and send/receive_data().
32 // Timeouts in ms.
33 status_t
34 send_data_with_timeout(thread_id thread, int32 code, void *buffer,
35 	size_t buffer_size, uint32 timeout)
36 {
37 	for(uint32 tries = 0; tries < timeout; tries++) {
38 		if(has_data(thread))
39 			snooze(1000);
40 	}
41 
42 	if(!has_data(thread))
43 		return send_data(thread, code, buffer, buffer_size);
44 	else
45 		return B_TIMED_OUT;
46 }
47 
48 
49 status_t
50 receive_data_with_timeout(thread_id *sender, int32 *code, void *buffer,
51 	size_t buffer_size, uint32 timeout)
52 {
53 	for(uint32 tries = 0; tries < timeout; tries++) {
54 		if(!has_data(find_thread(NULL))) {
55 			snooze(1000);
56 			continue;
57 		}
58 	}
59 
60 	if(has_data(find_thread(NULL))) {
61 		*code = receive_data(sender, buffer, buffer_size);
62 		return B_OK;
63 	} else
64 		return B_TIMED_OUT;
65 }
66