diff --git a/src/add-ons/kernel/network/ppp/shared/Jamfile b/src/add-ons/kernel/network/ppp/shared/Jamfile index f4c597f297..8416f6724a 100644 --- a/src/add-ons/kernel/network/ppp/shared/Jamfile +++ b/src/add-ons/kernel/network/ppp/shared/Jamfile @@ -1,3 +1,4 @@ SubDir OBOS_TOP src add-ons kernel network ppp shared ; SubInclude OBOS_TOP src add-ons kernel network ppp shared libkernelppp ; +SubInclude OBOS_TOP src add-ons kernel network ppp shared libppp ; diff --git a/src/add-ons/kernel/network/ppp/shared/libppp/Jamfile b/src/add-ons/kernel/network/ppp/shared/libppp/Jamfile new file mode 100644 index 0000000000..2d0ba590ef --- /dev/null +++ b/src/add-ons/kernel/network/ppp/shared/libppp/Jamfile @@ -0,0 +1,16 @@ +SubDir OBOS_TOP src add-ons kernel network ppp shared libppp ; + +SEARCH_SOURCE += [ FDirName $(OBOS_TOP) src add-ons kernel network ppp shared libkernelppp ] ; + +UsePrivateHeaders net ; +UseHeaders [ FDirName $(OBOS_TOP) src add-ons kernel network ppp shared libppp headers ] ; +UseHeaders [ FDirName $(OBOS_TOP) src add-ons kernel network ppp shared libkernelppp headers ] ; +UseHeaders [ FDirName $(OBOS_TOP) src add-ons kernel network ppp shared libkernelppp ] ; + + +StaticLibrary ppp : + settings_tools.cpp + + PPPInterface.cpp + PPPManager.cpp +; diff --git a/src/add-ons/kernel/network/ppp/shared/libppp/PPPInterface.cpp b/src/add-ons/kernel/network/ppp/shared/libppp/PPPInterface.cpp new file mode 100644 index 0000000000..c532e0fff4 --- /dev/null +++ b/src/add-ons/kernel/network/ppp/shared/libppp/PPPInterface.cpp @@ -0,0 +1,149 @@ +//---------------------------------------------------------------------- +// This software is part of the OpenBeOS distribution and is covered +// by the OpenBeOS license. +// +// Copyright (c) 2003 Waldemar Kornewald, Waldemar.Kornewald@web.de +//--------------------------------------------------------------------- + +#include "PPPInterface.h" + +#include +#include + + +PPPInterface::PPPInterface(interface_id ID = PPP_UNDEFINED_INTERFACE_ID) +{ + fFD = open("/dev/net/stack", O_RDWR); + + SetTo(ID); +} + + +PPPInterface::PPPInterface(const PPPInterface& copy) +{ + fFD = open("/dev/net/stack", O_RDWR); + + SetTo(copy.ID()); +} + + +PPPInterface::~PPPInterface() +{ + if(fFD >= 0) + close(fFD); +} + + +status_t +PPPInterface::InitCheck() const +{ + if(fFD < 0 || fID == PPP_UNDEFINED_INTERFACE_ID) + return B_ERROR; + + return B_OK; +} + + +status_t +PPPInterface::SetTo(interface_id ID) +{ + if(fFD < 0) + return B_ERROR; + + fID = ID; + + return Control(PPPC_GET_INTERFACE_INFO, &fInfo, sizeof(fInfo)); +} + + +status_t +PPPInterface::Control(uint32 op, void *data, size_t length) const +{ + if(InitCheck() != B_OK) + return B_ERROR; + + ppp_control_info control; + control_net_module_args args; + + args.name = PPP_INTERFACE_MODULE_NAME; + args.op = PPPC_CONTROL_INTERFACE; + args.data = &control; + args.length = sizeof(control); + + control.index = ID(); + control.op = op; + control.data = data; + control.length = length; + + return ioctl(fFD, NET_STACK_CONTROL_NET_MODULE, &args); +} + + +bool +PPPInterface::GetInterfaceInfo(ppp_interface_info *info) const +{ + if(InitCheck() != B_OK || !info) + return false; + + memcpy(info, &fInfo, sizeof(fInfo)); + + return true; +} + + +bool +PPPInterface::Up() const +{ + if(InitCheck() != B_OK) + return false; + + int32 id = ID(); + control_net_module_args args; + args.name = PPP_INTERFACE_MODULE_NAME; + args.op = PPPC_BRING_INTERFACE_UP; + args.data = &id; + args.length = sizeof(id); + + return ioctl(fFD, NET_STACK_CONTROL_NET_MODULE, &args) == B_OK; +} + + +bool +PPPInterface::Down() const +{ + if(InitCheck() != B_OK) + return false; + + int32 id = ID(); + control_net_module_args args; + args.name = PPP_INTERFACE_MODULE_NAME; + args.op = PPPC_BRING_INTERFACE_DOWN; + args.data = &id; + args.length = sizeof(id); + + return ioctl(fFD, NET_STACK_CONTROL_NET_MODULE, &args) == B_OK; +} + + +bool +PPPInterface::EnableReports(ppp_report_type type, thread_id thread, + int32 flags = PPP_NO_FLAGS) const +{ + ppp_report_request request; + request.type = type; + request.thread = thread; + request.flags = flags; + + return Control(PPPC_ENABLE_REPORTS, &request, sizeof(request)) == B_OK; +} + + +bool +PPPInterface::DisableReports(ppp_report_type type, thread_id thread) const +{ + ppp_report_request request; + request.type = type; + request.thread = thread; + + return Control(PPPC_DISABLE_REPORTS, &request, sizeof(request)) == B_OK; +} diff --git a/src/add-ons/kernel/network/ppp/shared/libppp/PPPManager.cpp b/src/add-ons/kernel/network/ppp/shared/libppp/PPPManager.cpp new file mode 100644 index 0000000000..1c0a1b0e02 --- /dev/null +++ b/src/add-ons/kernel/network/ppp/shared/libppp/PPPManager.cpp @@ -0,0 +1,210 @@ +//---------------------------------------------------------------------- +// This software is part of the OpenBeOS distribution and is covered +// by the OpenBeOS license. +// +// Copyright (c) 2003 Waldemar Kornewald, Waldemar.Kornewald@web.de +//--------------------------------------------------------------------- + +#include "PPPManager.h" +#include "PPPInterface.h" + +#include +#include +#include + + +PPPManager::PPPManager() +{ + fFD = open("/dev/net/stack", O_RDWR); +} + + +PPPManager::~PPPManager() +{ + if(fFD >= 0) + close(fFD); +} + + +status_t +PPPManager::InitCheck() const +{ + if(fFD < 0) + return B_ERROR; + else + return B_OK; +} + + +status_t +PPPManager::Control(uint32 op, void *data, size_t length) const +{ + if(InitCheck() != B_OK) + return B_ERROR; + + control_net_module_args args; + args.name = PPP_INTERFACE_MODULE_NAME; + args.op = op; + args.data = data; + args.length = length; + + return ioctl(fFD, NET_STACK_CONTROL_NET_MODULE, &args); +} + +interface_id +PPPManager::CreateInterface(const driver_settings *settings) const +{ + ppp_interface_settings_info info; + info.settings = settings; + + if(Control(PPPC_CREATE_INTERFACE, &info, sizeof(info)) != B_OK) + return PPP_UNDEFINED_INTERFACE_ID; + else + return info.interface; +} + + +bool +PPPManager::DeleteInterface(interface_id ID) const +{ + if(Control(PPPC_DELETE_INTERFACE, &ID, sizeof(ID)) != B_OK) + return false; + else + return true; +} + + +interface_id* +PPPManager::Interfaces(int32 *count, + ppp_interface_filter filter = PPP_REGISTERED_INTERFACES) const +{ + int32 requestCount; + interface_id *interfaces; + + // loop until we get all interfaces + while(true) { + requestCount = *count = CountInterfaces(filter); + if(*count == -1) + return NULL; + + requestCount += 10; + // request some more interfaces in case some are added in the mean time + interfaces = new interface_id[requestCount]; + *count = GetInterfaces(interfaces, requestCount, filter); + if(*count == -1) { + delete interfaces; + return NULL; + } + + if(*count < requestCount) + break; + + delete interfaces; + } + + return interfaces; +} + + +int32 +PPPManager::GetInterfaces(interface_id *interfaces, int32 count, + ppp_interface_filter filter = PPP_REGISTERED_INTERFACES) const +{ + ppp_get_interfaces_info info; + info.interfaces = interfaces; + info.count = count; + info.filter = filter; + + if(Control(PPPC_GET_INTERFACES, &info, sizeof(info)) != B_OK) + return -1; + else + return info.resultCount; +} + + +interface_id +PPPManager::InterfaceWithSettings(const driver_settings *settings) const +{ + int32 count; + interface_id *interfaces = Interfaces(&count, PPP_ALL_INTERFACES); + + if(!interfaces) + return PPP_UNDEFINED_INTERFACE_ID; + + interface_id id = PPP_UNDEFINED_INTERFACE_ID; + PPPInterface interface; + ppp_interface_info info; + + for(int32 index = 0; index < count; index++) { + interface.SetTo(interfaces[index]); + if(interface.InitCheck() == B_OK && interface.GetInterfaceInfo(&info) + && equal_driver_settings(settings, info.settings)) { + id = interface.ID(); + break; + } + } + + delete interfaces; + + return id; +} + + +interface_id +PPPManager::InterfaceWithUnit(int32 if_unit) +{ + int32 count; + interface_id *interfaces = Interfaces(&count, PPP_REGISTERED_INTERFACES); + + if(!interfaces) + return PPP_UNDEFINED_INTERFACE_ID; + + interface_id id = PPP_UNDEFINED_INTERFACE_ID; + PPPInterface interface; + ppp_interface_info info; + + for(int32 index = 0; index < count; index++) { + interface.SetTo(interfaces[index]); + if(interface.InitCheck() == B_OK && interface.GetInterfaceInfo(&info) + && info.if_unit == if_unit) { + id = interface.ID(); + break; + } + } + + delete interfaces; + + return id; +} + + +int32 +PPPManager::CountInterfaces(ppp_interface_filter filter = + PPP_REGISTERED_INTERFACES) const +{ + return Control(PPPC_COUNT_INTERFACES, &filter, sizeof(filter)); +} + + +bool +PPPManager::EnableReports(ppp_report_type type, thread_id thread, + int32 flags = PPP_NO_FLAGS) const +{ + ppp_report_request request; + request.type = type; + request.thread = thread; + request.flags = flags; + + return Control(PPPC_ENABLE_REPORTS, &request, sizeof(request)) == B_OK; +} + + +bool +PPPManager::DisableReports(ppp_report_type type, thread_id thread) const +{ + ppp_report_request request; + request.type = type; + request.thread = thread; + + return Control(PPPC_DISABLE_REPORTS, &request, sizeof(request)) == B_OK; +} diff --git a/src/add-ons/kernel/network/ppp/shared/libppp/headers/PPPInterface.h b/src/add-ons/kernel/network/ppp/shared/libppp/headers/PPPInterface.h new file mode 100644 index 0000000000..969825a15c --- /dev/null +++ b/src/add-ons/kernel/network/ppp/shared/libppp/headers/PPPInterface.h @@ -0,0 +1,50 @@ +//---------------------------------------------------------------------- +// This software is part of the OpenBeOS distribution and is covered +// by the OpenBeOS license. +// +// Copyright (c) 2003 Waldemar Kornewald, Waldemar.Kornewald@web.de +//--------------------------------------------------------------------- + +#ifndef _PPP_INTERFACE__H +#define _PPP_INTERFACE__H + +#include + + +class PPPInterface { + public: + PPPInterface(interface_id ID = PPP_UNDEFINED_INTERFACE_ID); + PPPInterface(const PPPInterface& copy); + ~PPPInterface(); + + status_t InitCheck() const; + + status_t SetTo(interface_id ID); + interface_id ID() const + { return fID; } + + status_t Control(uint32 op, void *data, size_t length) const; + + bool GetInterfaceInfo(ppp_interface_info *info) const; + + bool Up() const; + bool Down() const; + + bool EnableReports(ppp_report_type type, thread_id thread, + int32 flags = PPP_NO_FLAGS) const; + bool DisableReports(ppp_report_type type, thread_id thread) const; + + PPPInterface& operator= (const PPPInterface& copy) + { SetTo(copy.ID()); return *this; } + PPPInterface& operator= (interface_id ID) + { SetTo(ID); return *this; } + + private: + interface_id fID; + + int fFD; + ppp_interface_info fInfo; +}; + + +#endif diff --git a/src/add-ons/kernel/network/ppp/shared/libppp/headers/PPPManager.h b/src/add-ons/kernel/network/ppp/shared/libppp/headers/PPPManager.h new file mode 100644 index 0000000000..1e62994eba --- /dev/null +++ b/src/add-ons/kernel/network/ppp/shared/libppp/headers/PPPManager.h @@ -0,0 +1,51 @@ +//---------------------------------------------------------------------- +// This software is part of the OpenBeOS distribution and is covered +// by the OpenBeOS license. +// +// Copyright (c) 2003 Waldemar Kornewald, Waldemar.Kornewald@web.de +//--------------------------------------------------------------------- + +#ifndef _PPP_MANAGER__H +#define _PPP_MANAGER__H + +#include + + +class PPPManager { + private: + // copies are not allowed/needed + PPPManager(const PPPManager& copy); + PPPManager& operator= (const PPPManager& copy); + + public: + PPPManager(); + ~PPPManager(); + + status_t InitCheck() const; + + status_t Control(uint32 op, void *data, size_t length) const; + + interface_id CreateInterface(const driver_settings *settings) const; + bool DeleteInterface(interface_id ID) const; + + interface_id *Interfaces(int32 *count, + ppp_interface_filter filter = PPP_REGISTERED_INTERFACES) const; + // the user is responsible for deleting the returned array! + int32 GetInterfaces(interface_id *interfaces, int32 count, + ppp_interface_filter filter = PPP_REGISTERED_INTERFACES) const; + // make sure interfaces has enough space for count items + interface_id InterfaceWithSettings(const driver_settings *settings) const; + interface_id InterfaceWithUnit(int32 if_unit); + int32 CountInterfaces(ppp_interface_filter filter = + PPP_REGISTERED_INTERFACES) const; + + bool EnableReports(ppp_report_type type, thread_id thread, + int32 flags = PPP_NO_FLAGS) const; + bool DisableReports(ppp_report_type type, thread_id thread) const; + + private: + int fFD; +}; + + +#endif