1
0
Fork 0

Merge pull request #177 from jxmx/ipbinding

Add Option to Bind to Address
ycs232-kbc
Jonathan Naylor 4 years ago committed by GitHub
commit f718166569
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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 std::string& bindaddr)
{ {
::fprintf(stdout, "Opening YSF network connection\n"); if (bindaddr.length() > 0)
::fprintf(stdout, "Opening YSF network connection on address %s\n", bindaddr.c_str());
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 std::string& 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 std::string& 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 ( bindaddr.length() > 0){
int validaddr = inet_pton(AF_INET, bindaddr.c_str(), &(addr.sin_addr));
if (validaddr != 1){
LogError("The BindAddress in the .ini is invalid - %s", bindaddr.c_str());
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 std::string& 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,7 @@ 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(); ret = network.open(m_conf.getNetworkBindAddr());
if (!ret) { if (!ret) {
::LogFinalise(); ::LogFinalise();
return; return;

@ -18,3 +18,6 @@ FileRoot=YSFReflector
[Network] [Network]
Port=42000 Port=42000
Debug=0 Debug=0
# By default, listen on all IPs. To bind to
# a specific IP uncomment and set a local address
#BindAddress=192.0.2.1

Loading…
Cancel
Save