xref: /haiku/src/add-ons/kernel/network/ppp/shared/libkernelppp/KPPPUtils.cpp (revision 21258e2674226d6aa732321b6f8494841895af5f)
1 /*
2  * Copyright 2003-2007, Waldemar Kornewald <wkornew@gmx.net>
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include <OS.h>
7 
8 #include <KPPPUtils.h>
9 #include <KPPPInterface.h>
10 
11 
12 //!	Returns if this protocol is allowed to handle packets in the current state.
13 bool
14 IsProtocolAllowed(const KPPPProtocol& protocol)
15 {
16 	if (protocol.ProtocolNumber() == PPP_LCP_PROTOCOL)
17 		return true;
18 	else if (protocol.Interface().State() != PPP_OPENED_STATE)
19 		return false;
20 	else if (protocol.Interface().Phase() > PPP_AUTHENTICATION_PHASE
21 			|| (protocol.Interface().Phase() >= PPP_ESTABLISHMENT_PHASE
22 				&& protocol.Flags() & PPP_ALWAYS_ALLOWED))
23 		return true;
24 	else
25 		return false;
26 }
27 
28 
29 status_t
30 send_data_with_timeout(thread_id thread, int32 code, void *buffer,
31 	size_t buffer_size, uint32 timeout)
32 {
33 	for (uint32 tries = 0; tries < timeout; tries += 5) {
34 		if (has_data(thread))
35 			snooze(5000);
36 		else
37 			break;
38 	}
39 
40 	if (!has_data(thread))
41 		return send_data(thread, code, buffer, buffer_size);
42 	else
43 		return B_TIMED_OUT;
44 }
45 
46 
47 status_t
48 receive_data_with_timeout(thread_id *sender, int32 *code, void *buffer,
49 	size_t buffer_size, uint32 timeout)
50 {
51 	thread_id me = find_thread(NULL);
52 	for (uint32 tries = 0; tries < timeout; tries += 5) {
53 		if (!has_data(me))
54 			snooze(5000);
55 		else
56 			break;
57 	}
58 
59 	if (has_data(find_thread(NULL))) {
60 		*code = receive_data(sender, buffer, buffer_size);
61 		return B_OK;
62 	} else
63 		return B_TIMED_OUT;
64 }
65