1
0
Fork 0

add configuiration to bind to specific IP

ycs232-kbc
Jason D. McCormick 4 years ago
parent 815165141c
commit 0cfd041de9

@ -45,7 +45,8 @@ m_logFileLevel(0U),
m_logFilePath(), m_logFilePath(),
m_logFileRoot(), m_logFileRoot(),
m_networkPort(0U), m_networkPort(0U),
m_networkDebug(false) m_networkDebug(false),
m_networkBindAddr()
{ {
} }
@ -112,6 +113,8 @@ bool CConf::read()
m_networkPort = (unsigned int)::atoi(value); m_networkPort = (unsigned int)::atoi(value);
else if (::strcmp(key, "Debug") == 0) else if (::strcmp(key, "Debug") == 0)
m_networkDebug = ::atoi(value) == 1; m_networkDebug = ::atoi(value) == 1;
else if (::strcmp(key, "BindAddress") == 0)
m_networkBindAddr = value;
} }
} }
@ -169,3 +172,8 @@ bool CConf::getNetworkDebug() const
{ {
return m_networkDebug; return m_networkDebug;
} }
std::string CConf::getNetworkBindAddr() const
{
return m_networkBindAddr;
}

@ -47,6 +47,7 @@ public:
// The Network section // The Network section
unsigned int getNetworkPort() const; unsigned int getNetworkPort() const;
bool getNetworkDebug() const; bool getNetworkDebug() const;
std::string getNetworkBindAddr() const;
private: private:
std::string m_file; std::string m_file;
@ -63,6 +64,7 @@ private:
unsigned int m_networkPort; unsigned int m_networkPort;
bool m_networkDebug; bool m_networkDebug;
std::string m_networkBindAddr;
}; };
#endif #endif

@ -44,11 +44,14 @@ CNetwork::~CNetwork()
delete[] m_status; delete[] m_status;
} }
bool CNetwork::open() bool CNetwork::open(const char* bindaddr)
{ {
::fprintf(stdout, "Opening YSF network connection\n"); if (strlen(bindaddr) > 0)
::fprintf(stdout, "Opening YSF network connection on address %s\n", bindaddr);
else
::fprintf(stdout, "Opening YSF network connection on all interfaces\n");
return m_socket.open(); return m_socket.open(bindaddr);
} }
bool CNetwork::writeData(const unsigned char* data, const in_addr& address, unsigned int port) bool CNetwork::writeData(const unsigned char* data, const in_addr& address, unsigned int port)

@ -31,7 +31,7 @@ public:
CNetwork(unsigned int port, unsigned int id, const std::string& name, const std::string& description, bool debug); CNetwork(unsigned int port, unsigned int id, const std::string& name, const std::string& description, bool debug);
~CNetwork(); ~CNetwork();
bool open(); bool open(const char* bindaddr);
bool writeData(const unsigned char* data, const in_addr& address, unsigned int port); bool writeData(const unsigned char* data, const in_addr& address, unsigned int port);
bool writePoll(const in_addr& address, unsigned int port); bool writePoll(const in_addr& address, unsigned int port);

@ -102,7 +102,7 @@ in_addr CUDPSocket::lookup(const std::string& hostname)
#endif #endif
} }
bool CUDPSocket::open() bool CUDPSocket::open(const char* bindaddr)
{ {
m_fd = ::socket(PF_INET, SOCK_DGRAM, 0); m_fd = ::socket(PF_INET, SOCK_DGRAM, 0);
if (m_fd < 0) { if (m_fd < 0) {
@ -119,7 +119,16 @@ bool CUDPSocket::open()
::memset(&addr, 0x00, sizeof(sockaddr_in)); ::memset(&addr, 0x00, sizeof(sockaddr_in));
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_port = htons(m_port); addr.sin_port = htons(m_port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if ( strlen(bindaddr) > 0){
int validaddr = inet_pton(AF_INET, bindaddr, &(addr.sin_addr));
if (validaddr != 1){
LogError("The BindAddress in the .ini is invalid - %s", bindaddr);
return false;
}
} else {
addr.sin_addr.s_addr = htonl(INADDR_ANY);
}
if (!m_address.empty()) { if (!m_address.empty()) {
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)

@ -40,7 +40,7 @@ public:
CUDPSocket(unsigned int port = 0U); CUDPSocket(unsigned int port = 0U);
~CUDPSocket(); ~CUDPSocket();
bool open(); bool open(const char* bindaddr);
int read(unsigned char* buffer, unsigned int length, in_addr& address, unsigned int& port); int read(unsigned char* buffer, unsigned int length, in_addr& address, unsigned int& port);
bool write(const unsigned char* buffer, unsigned int length, const in_addr& address, unsigned int port); bool write(const unsigned char* buffer, unsigned int length, const in_addr& address, unsigned int port);

@ -164,7 +164,9 @@ void CYSFReflector::run()
CNetwork network(m_conf.getNetworkPort(), m_conf.getId(), m_conf.getName(), m_conf.getDescription(), m_conf.getNetworkDebug()); CNetwork network(m_conf.getNetworkPort(), m_conf.getId(), m_conf.getName(), m_conf.getDescription(), m_conf.getNetworkDebug());
ret = network.open(); const char* bindaddr = m_conf.getNetworkBindAddr().c_str();
ret = network.open(bindaddr);
if (!ret) { if (!ret) {
::LogFinalise(); ::LogFinalise();
return; return;

Loading…
Cancel
Save