//*****************************************************************************
// RCF - Remote Call Framework
// Copyright (c) 2005. All rights reserved.
// Developed by Jarl Lindrud.
// Contact: jlindrud@hotmail.com .
//*****************************************************************************
#ifndef INCLUDE_RCF_IPSERVERTRANSPORT_HPP
#define INCLUDE_RCF_IPSERVERTRANSPORT_HPP
#include <string>
#include <vector>
#include <RCF/UsingBsdSockets.hpp>
#include <RCF/ThreadLibrary.hpp>
#include <RCF/Tools.hpp>
namespace RCF {
class I_IpServerTransport
{
public:
I_IpServerTransport() :
mReadWriteMutex(WriterPriority),
mNetworkInterface("127.0.0.1")
{}
void setNetworkInterface(const std::string &networkInterface)
{
WriteLock writeLock(mReadWriteMutex);
mNetworkInterface = networkInterface;
}
std::string getNetworkInterface() const
{
ReadLock readLock(mReadWriteMutex);
return mNetworkInterface;
}
void setAllowedClientIps(const std::vector<std::string> &allowedClientIps)
{
WriteLock writeLock(mReadWriteMutex);
mAllowedIps.assign(allowedClientIps.begin(), allowedClientIps.end());
// translate the ips into 32 bit values, network order
mAllowedAddrs.clear();
for (unsigned int i=0; i<mAllowedIps.size(); i++)
{
const std::string &ip = mAllowedIps[i];
hostent *hostDesc = ::gethostbyname( ip.c_str() );
if (hostDesc == NULL)
{
int err = Platform::OS::BsdSockets::GetLastError();
std::string strErr = Platform::OS::GetErrorString(err);
RCF_TRACE("gethostbyname() failed")(ip)(err)(strErr);
mAllowedAddrs.push_back(INADDR_NONE);
}
else
{
in_addr addr = *((in_addr*) hostDesc->h_addr_list[0]);
mAllowedAddrs.push_back(addr.s_addr);
}
}
}
std::vector<std::string> getAllowedClientIps() const
{
ReadLock readLock(mReadWriteMutex);
return mAllowedIps;
}
bool isClientIpAllowed(const std::string &ip) const
{
ReadLock readLock(mReadWriteMutex);
return
mAllowedIps.empty() ||
(std::find(mAllowedIps.begin(), mAllowedIps.end(), ip) != mAllowedIps.end());
}
bool isClientAddrAllowed(const sockaddr_in &addr) const
{
ReadLock readLock(mReadWriteMutex);
return
mAllowedAddrs.empty() ||
(std::find(mAllowedAddrs.begin(), mAllowedAddrs.end(), addr.sin_addr.s_addr) != mAllowedAddrs.end());
}
private:
std::string mNetworkInterface;
std::vector<std::string> mAllowedIps;
std::vector<u_long> mAllowedAddrs;
mutable ReadWriteMutex mReadWriteMutex;
};
} // namespace RCF
#endif // ! INCLUDE_RCF_IPSERVERTRANSPORT_HPP