#include "protoNet.h"
#include "protoDebug.h"
unsigned int ProtoNet::GetInterfaceCount()
{
return GetInterfaceIndices(NULL, 0);
}
unsigned int ProtoNet::GetInterfaceIndex(const ProtoAddress& ifAddr)
{
char ifName[256];
ifName[255] = '\0';
if (GetInterfaceName(ifAddr, ifName, 255))
{
return GetInterfaceIndex(ifName);
}
else
{
return 0;
}
}
bool ProtoNet::GetInterfaceAddressList(unsigned int ifIndex,
ProtoAddress::Type addrType,
ProtoAddressList& addrList)
{
char ifName[256];
ifName[255] = '\0';
if (GetInterfaceName(ifIndex, ifName, 255))
{
return GetInterfaceAddressList(ifName, addrType, addrList);
}
else
{
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() error: invalid interface index?!\n");
return false;
}
}
bool ProtoNet::GetHostAddressList(ProtoAddress::Type addrType,
ProtoAddressList& addrList)
{
unsigned int ifCount = GetInterfaceCount();
if (0 == ifCount)
{
PLOG(PL_WARN, "ProtoNet::GetHostAddressList() warning: no interfaces?!\n");
return true;
}
unsigned int* ifIndices = new unsigned int[ifCount];
if (NULL == ifIndices)
{
PLOG(PL_ERROR, "ProtoNet::GetHostAddressList() new ifIndices[] error: %s\n", GetErrorString());
return false;
}
ifCount = GetInterfaceIndices(ifIndices, ifCount);
for (unsigned int i = 0; i < ifCount; i++)
{
if (!GetInterfaceAddressList(ifIndices[i], addrType, addrList))
{
PLOG(PL_DEBUG, "ProtoNet::GetHostAddressList() error: unable to get addresses for iface index %d\n", ifIndices[i]);
}
}
delete[] ifIndices;
return true; }
#ifndef WIN32
bool ProtoNet::FindLocalAddress(ProtoAddress::Type addrType, ProtoAddress& theAddress)
{
ProtoAddressList addrList;
if (GetHostAddressList(addrType, addrList))
{
ProtoAddressList::Iterator iterator(addrList);
while (iterator.GetNextAddress(theAddress))
{
if (!theAddress.IsLoopback())
return true;
}
}
return false;
} #endif
bool ProtoNet::GetInterfaceAddress(const char* ifName,
ProtoAddress::Type addrType,
ProtoAddress& theAddress,
unsigned int* ifIndex)
{
ProtoAddressList addrList;
GetInterfaceAddressList(ifName, addrType, addrList, ifIndex);
return addrList.GetFirstAddress(theAddress);
}
bool ProtoNet::GetInterfaceAddress(unsigned int ifIndex,
ProtoAddress::Type addrType,
ProtoAddress& theAddress)
{
ProtoAddressList addrList;
GetInterfaceAddressList(ifIndex, addrType, addrList);
return addrList.GetFirstAddress(theAddress);
}
#ifndef WIN32
unsigned int ProtoNet::GetInterfaceAddressMask(unsigned int ifIndex, const ProtoAddress& ifAddr)
{
#ifndef WIN32
char ifName[256];
ifName[255] = '\0';
if (GetInterfaceName(ifIndex, ifName, 255))
{
return GetInterfaceAddressMask(ifName, ifAddr);
}
else
{
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressMask() error: invalid interface index?!\n");
return 0;
}
#else
PLOG(PL_ERROR,"ProtoNet::GetInterfaceAddressMask() error: function not implemented for WIN32\n");
return 0;
#endif
}
bool ProtoNet::AddInterfaceAddress(unsigned int ifIndex, const ProtoAddress& addr, unsigned int maskLen)
{
#ifndef WIN32
char ifName[256];
ifName[255] = '\0';
if (GetInterfaceName(ifIndex, ifName, 255))
{
return AddInterfaceAddress(ifName, addr, maskLen);
}
else
{
PLOG(PL_ERROR, "ProtoNet::AddInterfaceAddress() error: invalid interface index?!\n");
return false;
}
#else
PLOG(PL_ERROR,"ProtoNet::AddInterfaceAddress() error: function not implemented in WIN32\n");
return false;
#endif
}
bool ProtoNet::RemoveInterfaceAddress(unsigned int ifIndex, const ProtoAddress& addr, unsigned int maskLen)
{
#ifndef WIN32
char ifName[256];
ifName[255] = '\0';
if (GetInterfaceName(ifIndex, ifName, 255))
{
return RemoveInterfaceAddress(ifName, addr, maskLen);
}
else
{
PLOG(PL_ERROR, "ProtoNet::AddInterfaceAddress() error: invalid interface index?!\n");
return false;
}
#else
PLOG(PL_ERROR,"ProtoNet::RemoveInterfaceAddress() error: function not implemented in WIN32\n");
return false;
#endif
} #endif
ProtoNet::Monitor::Monitor()
{
StartInputNotification();
}
ProtoNet::Monitor::~Monitor()
{
Close();
}
ProtoNet::Monitor::Event::Event()
: event_type(UNKNOWN_EVENT), iface_index(0)
{
strcpy(iface_name, "???");
iface_name[IFNAME_MAX] = '\0';
}
ProtoNet::Monitor::Event::~Event()
{
}