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 bool 138 PPPInterface::Up() const 139 { 140 if(InitCheck() != B_OK) 141 return false; 142 143 int32 id = ID(); 144 control_net_module_args args; 145 args.name = PPP_INTERFACE_MODULE_NAME; 146 args.op = PPPC_BRING_INTERFACE_UP; 147 args.data = &id; 148 args.length = sizeof(id); 149 150 return ioctl(fFD, NET_STACK_CONTROL_NET_MODULE, &args) == B_OK; 151 } 152 153 154 bool 155 PPPInterface::Down() const 156 { 157 if(InitCheck() != B_OK) 158 return false; 159 160 int32 id = ID(); 161 control_net_module_args args; 162 args.name = PPP_INTERFACE_MODULE_NAME; 163 args.op = PPPC_BRING_INTERFACE_DOWN; 164 args.data = &id; 165 args.length = sizeof(id); 166 167 return ioctl(fFD, NET_STACK_CONTROL_NET_MODULE, &args) == B_OK; 168 } 169 170 171 bool 172 PPPInterface::EnableReports(ppp_report_type type, thread_id thread, 173 int32 flags = PPP_NO_FLAGS) const 174 { 175 ppp_report_request request; 176 request.type = type; 177 request.thread = thread; 178 request.flags = flags; 179 180 return Control(PPPC_ENABLE_REPORTS, &request, sizeof(request)) == B_OK; 181 } 182 183 184 bool 185 PPPInterface::DisableReports(ppp_report_type type, thread_id thread) const 186 { 187 ppp_report_request request; 188 request.type = type; 189 request.thread = thread; 190 191 return Control(PPPC_DISABLE_REPORTS, &request, sizeof(request)) == B_OK; 192 } 193