xref: /haiku/src/kits/network/libnetapi/notifications.cpp (revision 8e8f7748d39f8407894a5ab79f7b4f93bc4f4652)
1 /*
2  * Copyright 2008, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 /*!	The notifications API uses the generic syscall interface of the
7 	network's stack notification module.
8 */
9 
10 #include <net_notifications.h>
11 
12 #include <MessengerPrivate.h>
13 #include <generic_syscall_defs.h>
14 #include <syscalls.h>
15 
16 
17 static status_t
18 check_for_notifications_syscall(void)
19 {
20 	uint32 version = 0;
21 	return _kern_generic_syscall(NET_NOTIFICATIONS_SYSCALLS, B_SYSCALL_INFO,
22 		&version, sizeof(version));
23 }
24 
25 
26 //	#pragma mark -
27 
28 
29 status_t
30 start_watching_network(uint32 flags, const BMessenger& target)
31 {
32 	if (check_for_notifications_syscall() != B_OK)
33 		return B_NOT_SUPPORTED;
34 
35 	BMessenger::Private targetPrivate(const_cast<BMessenger&>(target));
36 	net_notifications_control control;
37 	control.flags = flags;
38 	control.port = targetPrivate.Port();
39 	control.token = targetPrivate.Token();
40 
41 	return _kern_generic_syscall(NET_NOTIFICATIONS_SYSCALLS,
42 		NET_NOTIFICATIONS_CONTROL_WATCHING, &control,
43 		sizeof(net_notifications_control));
44 }
45 
46 
47 status_t
48 start_watching_network(uint32 flags, const BHandler* handler,
49 	const BLooper* looper)
50 {
51 	const BMessenger target(handler, looper);
52 	return start_watching_network(flags, target);
53 }
54 
55 status_t
56 stop_watching_network(const BMessenger& target)
57 {
58 	return start_watching_network(0, target);
59 		// start_watching_network() without flags just stops everything
60 }
61 
62 
63 status_t
64 stop_watching_network(const BHandler* handler, const BLooper* looper)
65 {
66 	const BMessenger target(handler, looper);
67 	return stop_watching_network(target);
68 }
69