Skip to content

Commit 70f7e39

Browse files
first ipv6 implementation
1 parent 749aa12 commit 70f7e39

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

src/Modbus_TCP_Client.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ Client::Client(const std::string &host,
2727
const std::string &service,
2828
modbus_mapping_t *mapping,
2929
std::size_t tcp_timeout) {
30-
const char *host_str = nullptr;
31-
if (!(host.empty() || host == "any" || host == "::" || host == "0.0.0.0")) host_str = host.c_str();
30+
const char *host_str = "::";
31+
if (!(host.empty() || host == "any")) host_str = host.c_str();
3232

3333
// create modbus object
3434
modbus = modbus_new_tcp_pi(host_str, service.c_str());
@@ -72,8 +72,8 @@ Client::Client(const std::string &host,
7272
const std::string &service,
7373
modbus_mapping_t **mappings,
7474
std::size_t tcp_timeout) {
75-
const char *host_str = nullptr;
76-
if (!(host.empty() || host == "any" || host == "::" || host == "0.0.0.0")) host_str = host.c_str();
75+
const char *host_str = "::";
76+
if (!(host.empty() || host == "any")) host_str = host.c_str();
7777

7878
// create modbus object
7979
modbus = modbus_new_tcp_pi(host_str, service.c_str());
@@ -112,7 +112,7 @@ Client::Client(const std::string &host,
112112

113113
void Client::listen() {
114114
// create tcp socket
115-
socket = modbus_tcp_listen(modbus, 1);
115+
socket = modbus_tcp_pi_listen(modbus, 1);
116116
if (socket == -1) {
117117
const std::string error_msg = modbus_strerror(errno);
118118
throw std::runtime_error("failed to create tcp socket: " + error_msg);
@@ -177,7 +177,7 @@ void Client::set_debug(bool debug) {
177177
}
178178

179179
std::string Client::connect_client() {
180-
int tmp = modbus_tcp_accept(modbus, &socket);
180+
int tmp = modbus_tcp_pi_accept(modbus, &socket);
181181
if (tmp < 0) {
182182
const std::string error_msg = modbus_strerror(errno);
183183
throw std::runtime_error("modbus_tcp_accept failed: " + error_msg);
@@ -192,25 +192,21 @@ std::string Client::connect_client() {
192192
throw std::runtime_error("getpeername failed: " + error_msg);
193193
}
194194

195-
char buffer[INET6_ADDRSTRLEN + 1];
196-
uint16_t port;
195+
std::ostringstream sstr;
196+
197+
char buffer[INET6_ADDRSTRLEN + 1];
197198
if (peer_addr.ss_family == AF_INET) {
198199
auto peer_in = reinterpret_cast<const struct sockaddr_in *>(&peer_addr);
199200
inet_ntop(peer_addr.ss_family, &peer_in->sin_addr, buffer, sizeof(buffer));
200-
port = htons(peer_in->sin_port);
201-
}
202-
if (peer_addr.ss_family == AF_INET6) {
201+
sstr << buffer << ':' << htons(peer_in->sin_port);
202+
} else if (peer_addr.ss_family == AF_INET6) {
203203
auto peer_in6 = reinterpret_cast<const struct sockaddr_in6 *>(&peer_addr);
204204
inet_ntop(peer_addr.ss_family, &peer_in6->sin6_addr, buffer, sizeof(buffer));
205-
port = htons(peer_in6->sin6_port);
205+
sstr << buffer << ':' << htons(peer_in6->sin6_port);
206206
} else {
207-
strcpy(buffer, "UNKNOWN");
208-
port = 0;
207+
sstr << "UNKNOWN" << ':' << peer_addr.ss_family;
209208
}
210209

211-
std::ostringstream sstr;
212-
sstr << buffer << ':' << port;
213-
214210
return sstr.str();
215211
}
216212

0 commit comments

Comments
 (0)