xref: /haiku/src/add-ons/kernel/network/ppp/shared/libkernelppp/KPPPUtils.cpp (revision 9eb55bc1d104b8fda80898f8b25c94d8000c8255)
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 <OS.h>
9 
10 #include <KPPPUtils.h>
11 #include <KPPPInterface.h>
12 
13 
14 bool
15 IsProtocolAllowed(const KPPPProtocol& 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 += 5) {
38 		if(has_data(thread))
39 			snooze(5000);
40 		else
41 			break;
42 	}
43 
44 	if(!has_data(thread))
45 		return send_data(thread, code, buffer, buffer_size);
46 	else
47 		return B_TIMED_OUT;
48 }
49 
50 
51 status_t
52 receive_data_with_timeout(thread_id *sender, int32 *code, void *buffer,
53 	size_t buffer_size, uint32 timeout)
54 {
55 	for(uint32 tries = 0; tries < timeout; tries += 5) {
56 		if(!has_data(find_thread(NULL)))
57 			snooze(5000);
58 		else
59 			break;
60 	}
61 
62 	if(has_data(find_thread(NULL))) {
63 		*code = receive_data(sender, buffer, buffer_size);
64 		return B_OK;
65 	} else
66 		return B_TIMED_OUT;
67 }
68