resolve net device host during connect (fixes #336)

This commit is contained in:
john30
2020-04-11 16:31:47 +02:00
parent dfa034be18
commit 9e152daaa8
2 changed files with 27 additions and 20 deletions
+15 -15
View File
@@ -74,21 +74,10 @@ Device* Device::create(const char* name, bool checkDevice, bool readOnly, bool i
free(in); free(in);
return nullptr; // invalid port return nullptr; // invalid port
} }
struct sockaddr_in address;
memset(reinterpret_cast<char*>(&address), 0, sizeof(address));
*portpos = 0; *portpos = 0;
if (inet_aton(addrpos, &address.sin_addr) == 0) { char* hostOrIp = strdup(addrpos);
struct hostent* h = gethostbyname(addrpos);
if (h == nullptr) {
free(in);
return nullptr; // invalid host
}
memcpy(&address.sin_addr, h->h_addr_list[0], h->h_length);
}
free(in); free(in);
address.sin_family = AF_INET; return new NetworkDevice(name, hostOrIp, port, readOnly, initialSend, udp);
address.sin_port = (in_port_t)htons((uint16_t)port);
return new NetworkDevice(name, address, readOnly, initialSend, udp);
} }
return new SerialDevice(name, checkDevice, readOnly, initialSend); return new SerialDevice(name, checkDevice, readOnly, initialSend);
} }
@@ -296,13 +285,24 @@ result_t NetworkDevice::open() {
if (m_fd != -1) { if (m_fd != -1) {
close(); close();
} }
struct sockaddr_in address;
memset(reinterpret_cast<char*>(&address), 0, sizeof(address));
if (inet_aton(m_hostOrIp, &address.sin_addr) == 0) {
struct hostent* h = gethostbyname(m_hostOrIp);
if (h == nullptr) {
return RESULT_ERR_GENERIC_IO; // invalid host
}
memcpy(&address.sin_addr, h->h_addr_list[0], h->h_length);
}
address.sin_family = AF_INET;
address.sin_port = (in_port_t)htons(m_port);
m_fd = socket(AF_INET, m_udp ? SOCK_DGRAM : SOCK_STREAM, 0); m_fd = socket(AF_INET, m_udp ? SOCK_DGRAM : SOCK_STREAM, 0);
if (m_fd < 0) { if (m_fd < 0) {
return RESULT_ERR_GENERIC_IO; return RESULT_ERR_GENERIC_IO;
} }
int ret; int ret;
if (m_udp) { if (m_udp) {
struct sockaddr_in address = m_address;
address.sin_addr.s_addr = INADDR_ANY; address.sin_addr.s_addr = INADDR_ANY;
ret = bind(m_fd, (struct sockaddr*)&address, sizeof(address)); ret = bind(m_fd, (struct sockaddr*)&address, sizeof(address));
} else { } else {
@@ -318,7 +318,7 @@ result_t NetworkDevice::open() {
setsockopt(m_fd, IPPROTO_TCP, TCP_KEEPCNT, reinterpret_cast<void*>(&value), sizeof(value)); setsockopt(m_fd, IPPROTO_TCP, TCP_KEEPCNT, reinterpret_cast<void*>(&value), sizeof(value));
} }
if (ret >= 0) { if (ret >= 0) {
ret = connect(m_fd, (struct sockaddr*)&m_address, sizeof(m_address)); ret = connect(m_fd, (struct sockaddr*)&address, sizeof(address));
} }
if (ret < 0) { if (ret < 0) {
close(); close();
+12 -5
View File
@@ -237,19 +237,23 @@ class NetworkDevice : public Device {
* Construct a new instance. * Construct a new instance.
* @param name the device name (e.g. "/dev/ttyUSB0" for serial, "127.0.0.1:1234" for network). * @param name the device name (e.g. "/dev/ttyUSB0" for serial, "127.0.0.1:1234" for network).
* @param address the socket address of the device. * @param address the socket address of the device.
* @param hostOrIp the host name or IP address of the device.
* @param port the TCP or UDP port of the device.
* @param readOnly whether to allow read access to the device only. * @param readOnly whether to allow read access to the device only.
* @param initialSend whether to send an initial @a ESC symbol in @a open(). * @param initialSend whether to send an initial @a ESC symbol in @a open().
* @param udp true for UDP, false to TCP. * @param udp true for UDP, false to TCP.
*/ */
NetworkDevice(const char* name, const struct sockaddr_in& address, bool readOnly, bool initialSend, NetworkDevice(const char* name, const char* hostOrIp, uint16_t port, bool readOnly, bool initialSend, bool udp)
bool udp) : Device(name, true, readOnly, initialSend), m_hostOrIp(hostOrIp), m_port(port), m_udp(udp),
: Device(name, true, readOnly, initialSend), m_address(address), m_udp(udp),
m_buffer(nullptr), m_bufSize(0), m_bufLen(0), m_bufPos(0) {} m_buffer(nullptr), m_bufSize(0), m_bufLen(0), m_bufPos(0) {}
/** /**
* Destructor. * Destructor.
*/ */
virtual ~NetworkDevice() { virtual ~NetworkDevice() {
if (m_hostOrIp) {
free((void*)m_hostOrIp);
}
if (m_buffer) { if (m_buffer) {
free(m_buffer); free(m_buffer);
} }
@@ -279,8 +283,11 @@ class NetworkDevice : public Device {
private: private:
/** the socket address of the device. */ /** the host name or IP address of the device. */
const struct sockaddr_in m_address; const char* m_hostOrIp;
/** the TCP or UDP port of the device. */
const uint16_t m_port;
/** true for UDP, false to TCP. */ /** true for UDP, false to TCP. */
const bool m_udp; const bool m_udp;