xref: /haiku/src/add-ons/kernel/network/ppp/shared/libppp/PPPInterface.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 /*!	\class PPPInterface
9 	\brief This class represents a PPP interface living in kernel space.
10 
11 	Use this class to control PPP interfaces and get the current interface settings.
12 	You can also use it to enable/disable report messages.
13 */
14 
15 #include "PPPInterface.h"
16 
17 #include <cstring>
18 #include <unistd.h>
19 #include "_libppputils.h"
20 
21 #include <Path.h>
22 #include <Directory.h>
23 #include <Entry.h>
24 
25 
26 PPPInterface::PPPInterface(ppp_interface_id ID = PPP_UNDEFINED_INTERFACE_ID)
27 {
28 	fFD = open(get_stack_driver_path(), O_RDWR);
29 
30 	SetTo(ID);
31 }
32 
33 
34 PPPInterface::PPPInterface(const PPPInterface& copy)
35 {
36 	fFD = open(get_stack_driver_path(), O_RDWR);
37 
38 	SetTo(copy.ID());
39 }
40 
41 
42 PPPInterface::~PPPInterface()
43 {
44 	if(fFD >= 0)
45 		close(fFD);
46 }
47 
48 
49 status_t
50 PPPInterface::InitCheck() const
51 {
52 	if(fFD < 0 || fID == PPP_UNDEFINED_INTERFACE_ID)
53 		return B_ERROR;
54 
55 	return B_OK;
56 }
57 
58 
59 status_t
60 PPPInterface::SetTo(ppp_interface_id ID)
61 {
62 	if(fFD < 0)
63 		return B_ERROR;
64 
65 	fID = ID;
66 
67 	status_t error = Control(PPPC_GET_INTERFACE_INFO, &fInfo, sizeof(fInfo));
68 	if(error != B_OK) {
69 		memset(&fInfo, 0, sizeof(fInfo));
70 		fID = PPP_UNDEFINED_INTERFACE_ID;
71 	}
72 
73 	return error;
74 }
75 
76 
77 status_t
78 PPPInterface::Control(uint32 op, void *data, size_t length) const
79 {
80 	if(InitCheck() != B_OK)
81 		return B_ERROR;
82 
83 	ppp_control_info control;
84 	control_net_module_args args;
85 
86 	args.name = PPP_INTERFACE_MODULE_NAME;
87 	args.op = PPPC_CONTROL_INTERFACE;
88 	args.data = &control;
89 	args.length = sizeof(control);
90 
91 	control.index = ID();
92 	control.op = op;
93 	control.data = data;
94 	control.length = length;
95 
96 	return ioctl(fFD, NET_STACK_CONTROL_NET_MODULE, &args);
97 }
98 
99 
100 status_t
101 PPPInterface::GetSettingsEntry(BEntry *entry) const
102 {
103 	if(InitCheck() != B_OK || !entry || strlen(Name()) == 0)
104 		return B_ERROR;
105 
106 	BDirectory directory(PPP_INTERFACE_SETTINGS_PATH);
107 	return directory.FindEntry(Name(), entry, true);
108 }
109 
110 
111 bool
112 PPPInterface::GetInterfaceInfo(ppp_interface_info_t *info) const
113 {
114 	if(InitCheck() != B_OK || !info)
115 		return false;
116 
117 	memcpy(info, &fInfo, sizeof(fInfo));
118 
119 	return true;
120 }
121 
122 
123 bool
124 PPPInterface::HasSettings(const driver_settings *settings) const
125 {
126 	if(InitCheck() != B_OK || !settings)
127 		return false;
128 
129 	if(Control(PPPC_HAS_INTERFACE_SETTINGS, const_cast<driver_settings*>(settings),
130 			sizeof(driver_settings)) == B_OK)
131 		return true;
132 
133 	return false;
134 }
135 
136 
137 void
138 PPPInterface::SetProfile(const driver_settings *profile) const
139 {
140 	if(InitCheck() != B_OK || !profile)
141 		return;
142 
143 	Control(PPPC_SET_PROFILE, const_cast<driver_settings*>(profile), 0);
144 }
145 
146 
147 bool
148 PPPInterface::Up() const
149 {
150 	if(InitCheck() != B_OK)
151 		return false;
152 
153 	int32 id = ID();
154 	control_net_module_args args;
155 	args.name = PPP_INTERFACE_MODULE_NAME;
156 	args.op = PPPC_BRING_INTERFACE_UP;
157 	args.data = &id;
158 	args.length = sizeof(id);
159 
160 	return ioctl(fFD, NET_STACK_CONTROL_NET_MODULE, &args) == B_OK;
161 }
162 
163 
164 bool
165 PPPInterface::Down() const
166 {
167 	if(InitCheck() != B_OK)
168 		return false;
169 
170 	int32 id = ID();
171 	control_net_module_args args;
172 	args.name = PPP_INTERFACE_MODULE_NAME;
173 	args.op = PPPC_BRING_INTERFACE_DOWN;
174 	args.data = &id;
175 	args.length = sizeof(id);
176 
177 	return ioctl(fFD, NET_STACK_CONTROL_NET_MODULE, &args) == B_OK;
178 }
179 
180 
181 bool
182 PPPInterface::EnableReports(ppp_report_type type, thread_id thread,
183 	int32 flags = PPP_NO_FLAGS) const
184 {
185 	ppp_report_request request;
186 	request.type = type;
187 	request.thread = thread;
188 	request.flags = flags;
189 
190 	return Control(PPPC_ENABLE_REPORTS, &request, sizeof(request)) == B_OK;
191 }
192 
193 
194 bool
195 PPPInterface::DisableReports(ppp_report_type type, thread_id thread) const
196 {
197 	ppp_report_request request;
198 	request.type = type;
199 	request.thread = thread;
200 
201 	return Control(PPPC_DISABLE_REPORTS, &request, sizeof(request)) == B_OK;
202 }
203