| 1 |
#ifndef _PROTO_NET |
| 2 |
#define _PROTO_NET |
| 3 |
|
| 4 |
// The ProtoNet classes provide APIs for getting information |
| 5 |
// on the computer host's network interfaces, configured |
| 6 |
// addresses, etc. |
| 7 |
// It includes a class for actively monitoring changes in |
| 8 |
// in the status of network interfaces and configuration |
| 9 |
// |
| 10 |
// Common code for these functions and classes is implemented in |
| 11 |
// "src/common/protoNet.cpp" and system-specific code is implemented |
| 12 |
// in other files (e.g. src/unix/unixNet.cpp, src/win32/win32Net.cpp, etc) |
| 13 |
|
| 14 |
// For classes defined here, general a "factory" design pattern is used where |
| 15 |
// a static "Create()" method is called to create system-specific instantiations |
| 16 |
// of a given class and any virtual interface methods defined here are invoked |
| 17 |
// using system-specific code as needed. |
| 18 |
|
| 19 |
|
| 20 |
#include "protoAddress.h" |
| 21 |
#include "protoChannel.h" |
| 22 |
|
| 23 |
namespace ProtoNet |
| 24 |
{ |
| 25 |
/** |
| 26 |
* @class ProtoNet::Monitor |
| 27 |
* |
| 28 |
* @brief This class provides a means |
| 29 |
* to receive notifications of changes |
| 30 |
* in the computer's network status or |
| 31 |
* configuration. |
| 32 |
* |
| 33 |
*/ |
| 34 |
|
| 35 |
enum InterfaceStatus |
| 36 |
{ |
| 37 |
IFACE_UNKNOWN, |
| 38 |
IFACE_UP, |
| 39 |
IFACE_DOWN |
| 40 |
}; |
| 41 |
|
| 42 |
class Monitor : public ProtoChannel |
| 43 |
{ |
| 44 |
public: |
| 45 |
static Monitor* Create(); |
| 46 |
~Monitor(); |
| 47 |
|
| 48 |
// NOTE: Overrides of "Open()" and "Close()" MUST call the ProtoChannel |
| 49 |
// Open/Close base class methods last/first, respectively. |
| 50 |
virtual bool Open() |
| 51 |
{return ProtoChannel::Open();} |
| 52 |
|
| 53 |
virtual void Close() |
| 54 |
{ProtoChannel::Close();} |
| 55 |
|
| 56 |
// Can use the ProtoChannel "SetNotifier()" and "SetListener()" methods to |
| 57 |
// "wire up" the Monitor notifications to, respectively, ProtoDispatcher (or other) |
| 58 |
// asynchronous I/O notification and a "listener" method contained |
| 59 |
// in another class |
| 60 |
|
| 61 |
// Use the GetNextEvent() to fetch the next network status update event |
| 62 |
// (This should be called upon a ProtoChannel::NOTIFY_INPUT notification event) |
| 63 |
// from the system. Note that other functions may need to be invoked to |
| 64 |
// get details about a particular event (e.g., change in interface "flags", etc) |
| 65 |
|
| 66 |
// Note the set of Event types will be expanded over time as needed and depending |
| 67 |
// upon OS-support (i.e. not all events might be supported on all OS types) |
| 68 |
|
| 69 |
class Event |
| 70 |
{ |
| 71 |
public: |
| 72 |
enum Type |
| 73 |
{ |
| 74 |
NULL_EVENT = 0, |
| 75 |
IFACE_UP, |
| 76 |
IFACE_DOWN, |
| 77 |
IFACE_ADDR_NEW, |
| 78 |
IFACE_ADDR_DELETE, |
| 79 |
IFACE_STATE, // change in flags, etc |
| 80 |
UNKNOWN_EVENT |
| 81 |
}; |
| 82 |
|
| 83 |
enum {IFNAME_MAX = 255}; |
| 84 |
|
| 85 |
Event(); |
| 86 |
~Event(); |
| 87 |
|
| 88 |
void SetType(Type eventType) |
| 89 |
{event_type = eventType;} |
| 90 |
void SetInterfaceIndex(int ifaceIndex) |
| 91 |
{iface_index = ifaceIndex;} |
| 92 |
void SetAddress(ProtoAddress& addr) |
| 93 |
{iface_addr = addr;} |
| 94 |
void SetInterfaceName(const char* name) |
| 95 |
{ |
| 96 |
#ifdef WIN32 |
| 97 |
strncpy_s(iface_name, IFNAME_MAX, name, IFNAME_MAX); |
| 98 |
#else |
| 99 |
strncpy(iface_name, name, IFNAME_MAX); |
| 100 |
#endif // if/else WIN32 |
| 101 |
} |
| 102 |
|
| 103 |
Type GetType() const |
| 104 |
{return event_type;} |
| 105 |
int GetInterfaceIndex() const |
| 106 |
{return iface_index;} |
| 107 |
const ProtoAddress& GetAddress() const |
| 108 |
{return iface_addr;} |
| 109 |
ProtoAddress& AccessAddress() |
| 110 |
{return iface_addr;} |
| 111 |
const char* GetInterfaceName() const |
| 112 |
{return iface_name;} |
| 113 |
|
| 114 |
|
| 115 |
private: |
| 116 |
Type event_type; |
| 117 |
int iface_index; |
| 118 |
ProtoAddress iface_addr; |
| 119 |
char iface_name[IFNAME_MAX+1]; |
| 120 |
|
| 121 |
}; // end class ProtoNet::Monitor::Event |
| 122 |
|
| 123 |
virtual bool GetNextEvent(Event& event) = 0; |
| 124 |
|
| 125 |
protected: |
| 126 |
Monitor(); |
| 127 |
|
| 128 |
}; // end class ProtoNet::Monitor |
| 129 |
|
| 130 |
// These are the base ProtoNet methods that must be implemented in |
| 131 |
// with specific operating system calls (i.e. in unixNet.cpp, win32Net.cpp, etc) |
| 132 |
unsigned int GetInterfaceIndices(unsigned int* indexArray, unsigned int indexArraySize); |
| 133 |
|
| 134 |
// get iface name by index |
| 135 |
unsigned int GetInterfaceName(unsigned int index, char* buffer, unsigned int buflen); |
| 136 |
|
| 137 |
// get iface index by name |
| 138 |
unsigned int GetInterfaceIndex(const char* interfaceName); |
| 139 |
|
| 140 |
// get all addrs of "addrType" for given given "ifName" |
| 141 |
bool GetInterfaceAddressList(const char* ifName, |
| 142 |
ProtoAddress::Type addrType, |
| 143 |
ProtoAddressList& addrList, |
| 144 |
unsigned int* ifIndex = NULL); |
| 145 |
|
| 146 |
// get name that matches given ifAddr (may be an alias name) |
| 147 |
// (returns name length so you can verify buflen was sufficient) |
| 148 |
unsigned int GetInterfaceName(const ProtoAddress& ifAddr, char* buffer, unsigned int buflen); |
| 149 |
#ifdef WIN32 |
| 150 |
unsigned int GetInterfaceFriendlyName(const ProtoAddress& ifaceAddress, char* buffer, unsigned int buflen); |
| 151 |
unsigned int GetInterfaceFriendlyName(unsigned int ifaceIndex, char* buffer, unsigned int buflen); |
| 152 |
bool GetInterfaceAddressDhcp(const char* ifName, const ProtoAddress& ifAddr); |
| 153 |
// TODO: Fix functions to have one definition |
| 154 |
bool AddInterfaceAddress(const char* ifaceName, const ProtoAddress& addr, unsigned int maskLen, bool dhcp_enabled=false); |
| 155 |
bool GetInterfaceIpAddress(unsigned int index, ProtoAddress& ifAddr); |
| 156 |
#else |
| 157 |
bool AddInterfaceAddress(const char* ifaceName, const ProtoAddress& addr, unsigned int maskLen); |
| 158 |
|
| 159 |
#endif //WIN32 |
| 160 |
unsigned int GetInterfaceAddressMask(const char* ifName, const ProtoAddress& ifAddr); // returns masklen? |
| 161 |
bool RemoveInterfaceAddress(const char* ifaceName, const ProtoAddress& addr, unsigned int maskLen = 0); |
| 162 |
|
| 163 |
#ifndef WIN32 // TBD - implement these for WIN32 |
| 164 |
|
| 165 |
|
| 166 |
bool GetGroupMemberships(const char* ifaceName, ProtoAddress::Type addrType, ProtoAddressList& addrList); |
| 167 |
|
| 168 |
#endif // !WIN32 |
| 169 |
|
| 170 |
///////////////////////////////////////////////////////// |
| 171 |
// These are implemented in "protoNet.cpp" using the above |
| 172 |
// 'base' functions. |
| 173 |
|
| 174 |
unsigned int GetInterfaceCount(); |
| 175 |
|
| 176 |
unsigned int GetInterfaceIndex(const ProtoAddress& ifAddr); |
| 177 |
|
| 178 |
bool GetInterfaceAddress(const char* ifName, |
| 179 |
ProtoAddress::Type addrType, |
| 180 |
ProtoAddress& theAddress, |
| 181 |
unsigned int* ifIndex = NULL); |
| 182 |
|
| 183 |
bool GetInterfaceAddress(unsigned int ifIndex, |
| 184 |
ProtoAddress::Type addrType, |
| 185 |
ProtoAddress& theAddress); |
| 186 |
|
| 187 |
bool GetHostAddressList(ProtoAddress::Type addrType, |
| 188 |
ProtoAddressList& addrList); |
| 189 |
|
| 190 |
bool GetInterfaceAddressList(unsigned int ifIndex, |
| 191 |
ProtoAddress::Type addrType, |
| 192 |
ProtoAddressList& addrList); |
| 193 |
|
| 194 |
bool FindLocalAddress(ProtoAddress::Type addrType, ProtoAddress& theAddress); |
| 195 |
|
| 196 |
InterfaceStatus GetInterfaceStatus(const char* ifaceName); |
| 197 |
InterfaceStatus GetInterfaceStatus(unsigned int ifaceIndex); |
| 198 |
|
| 199 |
#ifdef WIN32 |
| 200 |
// TODO: make common function |
| 201 |
bool AddInterfaceAddress(unsigned int ifaceIndex, const ProtoAddress& addr, unsigned int maskLen, bool dhcp_enabled=false); |
| 202 |
|
| 203 |
#else |
| 204 |
bool AddInterfaceAddress(unsigned int ifaceIndex, const ProtoAddress& addr, unsigned int maskLen); |
| 205 |
#endif // WIN32 |
| 206 |
|
| 207 |
bool RemoveInterfaceAddress(unsigned int ifaceIndex, const ProtoAddress& addr, unsigned int maskLen = 0); |
| 208 |
|
| 209 |
unsigned int GetInterfaceAddressMask(unsigned int ifIndex, const ProtoAddress& ifAddr); |
| 210 |
#ifdef WIN32 |
| 211 |
bool GetInterfaceAddressDhcp(unsigned int ifIndex, const ProtoAddress& ifAddr); |
| 212 |
#endif // WIN32 |
| 213 |
} // end namespace ProtoNet |
| 214 |
|
| 215 |
|
| 216 |
#endif // !_PROTO_NET |