| 1 | /* |
| 2 | * Copyright © Stéphane Raimbault <[email protected]> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 | */ |
| 6 | |
| 7 | // clang-format off |
| 8 | #if defined(_WIN32) |
| 9 | # define OS_WIN32 |
| 10 | /* ws2_32.dll has getaddrinfo and freeaddrinfo on Windows XP and later. |
| 11 | * minwg32 headers check WINVER before allowing the use of these */ |
| 12 | # ifndef WINVER |
| 13 | # define WINVER 0x0501 |
| 14 | # endif |
| 15 | #endif |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <stdint.h> |
| 20 | #include <limits.h> |
| 21 | #include <string.h> |
| 22 | #include <errno.h> |
| 23 | #ifndef _MSC_VER |
| 24 | #include <unistd.h> |
| 25 | #endif |
| 26 | #include <signal.h> |
| 27 | #include <sys/types.h> |
| 28 | |
| 29 | #if defined(_WIN32) |
| 30 | /* Already set in modbus-tcp.h but it seems order matters in VS2005 */ |
| 31 | # include <winsock2.h> |
| 32 | # include <ws2tcpip.h> |
| 33 | # define SHUT_RDWR 2 |
| 34 | # define close closesocket |
| 35 | # define strdup _strdup |
| 36 | #else |
| 37 | # include <sys/socket.h> |
| 38 | # include <sys/ioctl.h> |
| 39 | |
| 40 | #if defined(__OpenBSD__) || (defined(__FreeBSD__) && __FreeBSD__ < 5) |
| 41 | # define OS_BSD |
| 42 | # include <netinet/in_systm.h> |
| 43 | #endif |
| 44 | |
| 45 | #ifdef HAVE_NETINET_IN_Hcxx . . $ `config.h:65:9`! |
| 46 | #include <netinet/in.h> |
| 47 | #endif /* HAVE_NETINET_IN_H */ |
| 48 | #ifdef HAVE_NETINET_IP_Hcxx . . $ `config.h:68:9`! |
| 49 | #include <netinet/ip.h> |
| 50 | #endif /* HAVE_NETINET_IP_H */ |
| 51 | # include <netinet/tcp.h> |
| 52 | # include <arpa/inet.h> |
| 53 | # include <netdb.h> |
| 54 | #endif |
| 55 | |
| 56 | #if !defined(MSG_NOSIGNALcxx . . $ `<external>/8962547315054503424/socket.h:242:9`!) |
| 57 | #define MSG_NOSIGNAL 0 |
| 58 | #endif |
| 59 | |
| 60 | #if defined(_AIX) && !defined(MSG_DONTWAITcxx . . $ `<external>/8962547315054503424/socket.h:226:9`!) |
| 61 | #define MSG_DONTWAIT MSG_NONBLOCK |
| 62 | #endif |
| 63 | // clang-format on |
| 64 | |
| 65 | #include "modbus-private.h"cxx . . $ `<file>/src/modbus-private.h`/ |
| 66 | |
| 67 | #include "modbus-tcp-private.h"cxx . . $ `<file>/src/modbus-tcp-private.h`/ |
| 68 | #include "modbus-tcp.h"cxx . . $ `<file>/src/modbus-tcp.h`/ |
| 69 | |
| 70 | #ifdef OS_WIN32 |
| 71 | static int _modbus_tcp_init_win32(void) |
| 72 | { |
| 73 | /* Initialise Windows Socket API */ |
| 74 | WSADATA wsaData; |
| 75 | |
| 76 | if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { |
| 77 | fprintf(stderr, |
| 78 | "WSAStartup() returned error code %d\n", |
| 79 | (unsigned int) GetLastError()); |
| 80 | errno = EIO; |
| 81 | return -1; |
| 82 | } |
| 83 | return 0; |
| 84 | } |
| 85 | #endif |
| 86 | |
| 87 | static int _modbus_set_slavecxx . . $ _modbus_set_slave(2a7e0e283938d456).(modbus_tcxx . . $ modbus_t# *ctx, int slave) |
| 88 | { |
| 89 | int max_slave = (ctx->quirkscxx . . $ _modbus#quirks. & MODBUS_QUIRK_MAX_SLAVEcxx . . $ MODBUS_QUIRK_MAX_SLAVE.) ? 255 : 247; |
| 90 | |
| 91 | /* Broadcast address is 0 (MODBUS_BROADCAST_ADDRESS) */ |
| 92 | if (slave >= 0 && slave <= max_slave) { |
| 93 | ctx->slavecxx . . $ _modbus#slave. = slave; |
| 94 | } else if (slave == MODBUS_TCP_SLAVEcxx . . $ `src/modbus-tcp.h:35:9`!) { |
| 95 | /* The special value MODBUS_TCP_SLAVE (0xFF) can be used in TCP mode to |
| 96 | * restore the default value. */ |
| 97 | ctx->slavecxx . . $ _modbus#slave. = slave; |
| 98 | } else { |
| 99 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | /* Builds a TCP request header */ |
| 107 | static int _modbus_tcp_build_request_basiscxx . . $ _modbus_tcp_build_request_basis(abc53b0a0396e541).( |
| 108 | modbus_tcxx . . $ modbus_t# *ctx, int function, int addr, int nb, uint8_tcxx . . $ uint8_t# *req) |
| 109 | { |
| 110 | modbus_tcp_tcxx . . $ modbus_tcp_t# *ctx_tcp = ctx->backend_datacxx . . $ _modbus#backend_data.; |
| 111 | |
| 112 | /* Increase transaction ID */ |
| 113 | if (ctx_tcp->t_idcxx . . $ _modbus_tcp#t_id. < UINT16_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:123:10`!) |
| 114 | ctx_tcp->t_idcxx . . $ _modbus_tcp#t_id.++; |
| 115 | else |
| 116 | ctx_tcp->t_idcxx . . $ _modbus_tcp#t_id. = 0; |
| 117 | req[0] = ctx_tcp->t_idcxx . . $ _modbus_tcp#t_id. >> 8; |
| 118 | req[1] = ctx_tcp->t_idcxx . . $ _modbus_tcp#t_id. & 0x00ff; |
| 119 | |
| 120 | /* Protocol Modbus */ |
| 121 | req[2] = 0; |
| 122 | req[3] = 0; |
| 123 | |
| 124 | /* Length will be defined later by set_req_length_tcp at offsets 4 |
| 125 | and 5 */ |
| 126 | |
| 127 | req[6] = ctx->slavecxx . . $ _modbus#slave.; |
| 128 | req[7] = function; |
| 129 | req[8] = addr >> 8; |
| 130 | req[9] = addr & 0x00ff; |
| 131 | req[10] = nb >> 8; |
| 132 | req[11] = nb & 0x00ff; |
| 133 | |
| 134 | return _MODBUS_TCP_PRESET_REQ_LENGTHcxx . . $ `src/modbus-tcp-private.h:11:9`!; |
| 135 | } |
| 136 | |
| 137 | /* Builds a TCP response header */ |
| 138 | static int _modbus_tcp_build_response_basiscxx . . $ _modbus_tcp_build_response_basis(acd9c6258f33b2f4).(sft_tcxx . . $ sft_t# *sft, uint8_tcxx . . $ uint8_t# *rsp) |
| 139 | { |
| 140 | /* Extract from MODBUS Messaging on TCP/IP Implementation |
| 141 | Guide V1.0b (page 23/46): |
| 142 | The transaction identifier is used to associate the future |
| 143 | response with the request. */ |
| 144 | rsp[0] = sft->t_idcxx . . $ _sft#t_id. >> 8; |
| 145 | rsp[1] = sft->t_idcxx . . $ _sft#t_id. & 0x00ff; |
| 146 | |
| 147 | /* Protocol Modbus */ |
| 148 | rsp[2] = 0; |
| 149 | rsp[3] = 0; |
| 150 | |
| 151 | /* Length will be set later by send_msg (4 and 5) */ |
| 152 | |
| 153 | /* The slave ID is copied from the indication */ |
| 154 | rsp[6] = sft->slavecxx . . $ _sft#slave.; |
| 155 | rsp[7] = sft->functioncxx . . $ _sft#function.; |
| 156 | |
| 157 | return _MODBUS_TCP_PRESET_RSP_LENGTHcxx . . $ `src/modbus-tcp-private.h:12:9`!; |
| 158 | } |
| 159 | |
| 160 | static int _modbus_tcp_get_response_tidcxx . . $ _modbus_tcp_get_response_tid(b84ccf4e0bb14b06).(const uint8_tcxx . . $ uint8_t# *req) |
| 161 | { |
| 162 | return (req[0] << 8) + req[1]; |
| 163 | } |
| 164 | |
| 165 | static int _modbus_tcp_send_msg_precxx . . $ _modbus_tcp_send_msg_pre(7405b416d0535413).(uint8_tcxx . . $ uint8_t# *req, int req_length) |
| 166 | { |
| 167 | /* Subtract the header length to the message length */ |
| 168 | int mbap_length = req_length - 6; |
| 169 | |
| 170 | req[4] = mbap_length >> 8; |
| 171 | req[5] = mbap_length & 0x00FF; |
| 172 | |
| 173 | return req_length; |
| 174 | } |
| 175 | |
| 176 | static ssize_tcxx . . $ ssize_t# _modbus_tcp_sendcxx . . $ _modbus_tcp_send(ab2063bac0ea77a5).(modbus_tcxx . . $ modbus_t# *ctx, const uint8_tcxx . . $ uint8_t# *req, int req_length) |
| 177 | { |
| 178 | /* MSG_NOSIGNAL |
| 179 | Requests not to send SIGPIPE on errors on stream oriented |
| 180 | sockets when the other end breaks the connection. The EPIPE |
| 181 | error is still returned. */ |
| 182 | return sendcxx . . $ send(4e364047dcdcb5ca).(ctx->scxx . . $ _modbus#s., (const char *) req, req_length, MSG_NOSIGNALcxx . . $ `<external>/8962547315054503424/socket.h:242:9`!); |
| 183 | } |
| 184 | |
| 185 | static int _modbus_tcp_receivecxx . . $ _modbus_tcp_receive(744a031d86f39170).(modbus_tcxx . . $ modbus_t# *ctx, uint8_tcxx . . $ uint8_t# *req) |
| 186 | { |
| 187 | return _modbus_receive_msgcxx . . $ _modbus_receive_msg(85c522f07fbdeb64).(ctx, req, MSG_INDICATIONcxx . . $ MSG_INDICATION.); |
| 188 | } |
| 189 | |
| 190 | static ssize_tcxx . . $ ssize_t# _modbus_tcp_recvcxx . . $ _modbus_tcp_recv(41755522a7cda552).(modbus_tcxx . . $ modbus_t# *ctx, uint8_tcxx . . $ uint8_t# *rsp, int rsp_length) |
| 191 | { |
| 192 | return recvcxx . . $ recv(4136b1fcb15a75da).(ctx->scxx . . $ _modbus#s., (char *) rsp, rsp_length, 0); |
| 193 | } |
| 194 | |
| 195 | static int _modbus_tcp_check_integritycxx . . $ _modbus_tcp_check_integrity(b8ea090d50367a38).(modbus_tcxx . . $ modbus_t# *ctx, uint8_tcxx . . $ uint8_t# *msg, const int msg_length) |
| 196 | { |
| 197 | return msg_length; |
| 198 | } |
| 199 | |
| 200 | static int _modbus_tcp_pre_check_confirmationcxx . . $ _modbus_tcp_pre_check_confirmation(23551e319a37d2dc).(modbus_tcxx . . $ modbus_t# *ctx, |
| 201 | const uint8_tcxx . . $ uint8_t# *req, |
| 202 | const uint8_tcxx . . $ uint8_t# *rsp, |
| 203 | int rsp_length) |
| 204 | { |
| 205 | unsigned int protocol_id; |
| 206 | /* Check transaction ID */ |
| 207 | if (req[0] != rsp[0] || req[1] != rsp[1]) { |
| 208 | if (ctx->debugcxx . . $ _modbus#debug.) { |
| 209 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, |
| 210 | "Invalid transaction ID received 0x%X (not 0x%X)\n", |
| 211 | (rsp[0] << 8) + rsp[1], |
| 212 | (req[0] << 8) + req[1]); |
| 213 | } |
| 214 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EMBBADDATAcxx . . $ `src/modbus.h:147:9`!; |
| 215 | return -1; |
| 216 | } |
| 217 | |
| 218 | /* Check protocol ID */ |
| 219 | protocol_id = (rsp[2] << 8) + rsp[3]; |
| 220 | if (protocol_id != 0x0) { |
| 221 | if (ctx->debugcxx . . $ _modbus#debug.) { |
| 222 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Invalid protocol ID received 0x%X (not 0x0)\n", protocol_id); |
| 223 | } |
| 224 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EMBBADDATAcxx . . $ `src/modbus.h:147:9`!; |
| 225 | return -1; |
| 226 | } |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static int _modbus_tcp_set_ipv4_optionscxx . . $ _modbus_tcp_set_ipv4_options(7864480464b09eea).(int s) |
| 232 | { |
| 233 | int rc; |
| 234 | int option; |
| 235 | |
| 236 | /* Set the TCP no delay flag */ |
| 237 | /* SOL_TCP = IPPROTO_TCP */ |
| 238 | option = 1; |
| 239 | #ifdef _WIN32 |
| 240 | rc = setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (const char *)&option, sizeof(int)); |
| 241 | #else |
| 242 | rc = setsockoptcxx . . $ setsockopt(506cc1d0fc70fdf1).(s, IPPROTO_TCPcxx . . $ `<external>/9581057695539925927/in.h:51:9`!, TCP_NODELAYcxx . . $ `<external>/14446708121216675145/tcp.h:40:9`!, (const void *)&option, sizeof(int)); |
| 243 | #endif |
| 244 | if (rc == -1) { |
| 245 | return -1; |
| 246 | } |
| 247 | |
| 248 | /* If the OS does not offer SOCK_NONBLOCK, fall back to setting FIONBIO to |
| 249 | * make sockets non-blocking */ |
| 250 | /* Do not care about the return value, this is optional */ |
| 251 | #if !defined(SOCK_NONBLOCKcxx . . $ `<external>/5956532346368192945/socket_type.h:54:9`!) && defined(FIONBIOcxx . . $ `<external>/123502480044527469/ioctls.h:53:9`!) |
| 252 | #ifdef OS_WIN32 |
| 253 | { |
| 254 | /* Setting FIONBIO expects an unsigned long according to MSDN */ |
| 255 | u_long loption = 1; |
| 256 | ioctlsocket(s, FIONBIO, &loption); |
| 257 | } |
| 258 | #else |
| 259 | option = 1; |
| 260 | ioctl(s, FIONBIO, &option); |
| 261 | #endif |
| 262 | #endif |
| 263 | |
| 264 | #ifndef OS_WIN32 |
| 265 | /** |
| 266 | * Cygwin defines IPTOS_LOWDELAY but can't handle that flag so it's |
| 267 | * necessary to workaround that problem. |
| 268 | **/ |
| 269 | /* Set the IP low delay option */ |
| 270 | option = IPTOS_LOWDELAYcxx . . $ `<external>/4481066469753325646/ip.h:223:9`!; |
| 271 | #ifdef _WIN32 |
| 272 | rc = setsockopt(s, IPPROTO_IP, IP_TOS, (const char *)&option, sizeof(int)); |
| 273 | #else |
| 274 | rc = setsockoptcxx . . $ setsockopt(506cc1d0fc70fdf1).(s, IPPROTO_IPcxx . . $ `<external>/9581057695539925927/in.h:43:9`!, IP_TOScxx . . $ `<external>/13097018388402718637/in.h:49:16`!, (const void *)&option, sizeof(int)); |
| 275 | #endif |
| 276 | if (rc == -1) { |
| 277 | return -1; |
| 278 | } |
| 279 | #endif |
| 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | static int _connectcxx . . $ _connect(8265182d1515ebe4).(int sockfd, |
| 285 | const struct sockaddrcxx . . $ sockaddr# *addr, |
| 286 | socklen_tcxx . . $ socklen_t# addrlen, |
| 287 | const struct timevalcxx . . $ timeval# *ro_tv) |
| 288 | { |
| 289 | int rc = connectcxx . . $ connect(f39c6c650450bfdd).(sockfd, addr, addrlen); |
| 290 | |
| 291 | #ifdef OS_WIN32 |
| 292 | int wsaError = 0; |
| 293 | if (rc == -1) { |
| 294 | wsaError = WSAGetLastError(); |
| 295 | } |
| 296 | |
| 297 | if (wsaError == WSAEWOULDBLOCK || wsaError == WSAEINPROGRESS) { |
| 298 | #else |
| 299 | if (rc == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EINPROGRESScxx . . $ `<external>/17922322188510340118/errno.h:99:9`!) { |
| 300 | #endif |
| 301 | fd_setcxx . . $ fd_set# wset; |
| 302 | int optval; |
| 303 | socklen_tcxx . . $ socklen_t# optlen = sizeof(optval); |
| 304 | struct timevalcxx . . $ timeval# tv = *ro_tv; |
| 305 | |
| 306 | /* Wait to be available in writing */ |
| 307 | FD_ZEROcxx . . $ `<external>/946878812840035423/select.h:88:9`!(&wset); |
| 308 | if (sockfd >= FD_SETSIZEcxx . . $ `<external>/946878812840035423/select.h:73:9`!) { |
| 309 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 310 | return -1; |
| 311 | } |
| 312 | FD_SETcxx . . $ `<external>/946878812840035423/select.h:85:9`!(sockfd, &wset); |
| 313 | rc = selectcxx . . $ select(6d24c7514c8a5b88).(sockfd + 1, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, &wset, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, &tv); |
| 314 | if (rc < 0) { |
| 315 | /* Fail */ |
| 316 | return -1; |
| 317 | } |
| 318 | |
| 319 | if (rc == 0) { |
| 320 | /* Timeout */ |
| 321 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ETIMEDOUTcxx . . $ `<external>/17922322188510340118/errno.h:94:9`!; |
| 322 | return -1; |
| 323 | } |
| 324 | |
| 325 | /* The connection is established if SO_ERROR and optval are set to 0 */ |
| 326 | rc = getsockoptcxx . . $ getsockopt(b79d9f11efef15f9).(sockfd, SOL_SOCKETcxx . . $ `<external>/9030916627833342711/socket.h:9:9`!, SO_ERRORcxx . . $ `<external>/9030916627833342711/socket.h:14:9`!, (void *) &optval, &optlen); |
| 327 | if (rc == 0 && optval == 0) { |
| 328 | return 0; |
| 329 | } else { |
| 330 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ECONNREFUSEDcxx . . $ `<external>/17922322188510340118/errno.h:95:9`!; |
| 331 | return -1; |
| 332 | } |
| 333 | } |
| 334 | return rc; |
| 335 | } |
| 336 | |
| 337 | /* Establishes a modbus TCP connection with a Modbus server. */ |
| 338 | static int _modbus_tcp_connectcxx . . $ _modbus_tcp_connect(c57adc5f48380e39).(modbus_tcxx . . $ modbus_t# *ctx) |
| 339 | { |
| 340 | int rc; |
| 341 | int s; |
| 342 | /* Specialized version of sockaddr for Internet socket address (same size) */ |
| 343 | struct sockaddr_incxx . . $ sockaddr_in# addr; |
| 344 | modbus_tcp_tcxx . . $ modbus_tcp_t# *ctx_tcp = ctx->backend_datacxx . . $ _modbus#backend_data.; |
| 345 | int flags = SOCK_STREAMcxx . . $ `<external>/5956532346368192945/socket_type.h:28:9`!; |
| 346 | |
| 347 | #ifdef OS_WIN32 |
| 348 | if (_modbus_tcp_init_win32() == -1) { |
| 349 | return -1; |
| 350 | } |
| 351 | #endif |
| 352 | |
| 353 | #ifdef SOCK_CLOEXECcxx . . $ `<external>/5956532346368192945/socket_type.h:51:9`! |
| 354 | flags |= SOCK_CLOEXECcxx . . $ `<external>/5956532346368192945/socket_type.h:51:9`!; |
| 355 | #endif |
| 356 | |
| 357 | #ifdef SOCK_NONBLOCKcxx . . $ `<external>/5956532346368192945/socket_type.h:54:9`! |
| 358 | flags |= SOCK_NONBLOCKcxx . . $ `<external>/5956532346368192945/socket_type.h:54:9`!; |
| 359 | #endif |
| 360 | |
| 361 | s = socketcxx . . $ socket(2326284602b0bb2d).(PF_INETcxx . . $ `<external>/8962547315054503424/socket.h:45:9`!, flags, 0); |
| 362 | if (s < 0) { |
| 363 | return -1; |
| 364 | } |
| 365 | |
| 366 | if (s >= FD_SETSIZEcxx . . $ `<external>/946878812840035423/select.h:73:9`!) { |
| 367 | if (ctx->debugcxx . . $ _modbus#debug.) { |
| 368 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).( |
| 369 | stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, |
| 370 | "ERROR Socket descriptor %d exceeds FD_SETSIZE (%d)\n", |
| 371 | s, |
| 372 | FD_SETSIZEcxx . . $ `<external>/946878812840035423/select.h:73:9`!); |
| 373 | } |
| 374 | closecxx . . $ close(7864480464b09eea).(s); |
| 375 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 376 | return -1; |
| 377 | } |
| 378 | |
| 379 | rc = _modbus_tcp_set_ipv4_optionscxx . . $ _modbus_tcp_set_ipv4_options(7864480464b09eea).(s); |
| 380 | if (rc == -1) { |
| 381 | closecxx . . $ close(7864480464b09eea).(s); |
| 382 | return -1; |
| 383 | } |
| 384 | |
| 385 | if (ctx->debugcxx . . $ _modbus#debug.) { |
| 386 | printfcxx . . $ printf(581fddd72a8db8fc).("Connecting to %s:%d\n", ctx_tcp->ipcxx . . $ _modbus_tcp#ip., ctx_tcp->portcxx . . $ _modbus_tcp#port.); |
| 387 | } |
| 388 | |
| 389 | addr.sin_familycxx . . $ sockaddr_in#sin_family. = AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!; |
| 390 | addr.sin_portcxx . . $ sockaddr_in#sin_port. = htonscxx . . $ `<external>/9581057695539925927/in.h:431:12`!(ctx_tcp->port); |
| 391 | rc = inet_ptoncxx . . $ inet_pton(8d303d671d73812).(addr.sin_familycxx . . $ sockaddr_in#sin_family., ctx_tcp->ipcxx . . $ _modbus_tcp#ip., &(addr.sin_addrcxx . . $ sockaddr_in#sin_addr.)); |
| 392 | if (rc <= 0) { |
| 393 | if (ctx->debugcxx . . $ _modbus#debug.) { |
| 394 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Invalid IP address: %s\n", ctx_tcp->ipcxx . . $ _modbus_tcp#ip.); |
| 395 | } |
| 396 | closecxx . . $ close(7864480464b09eea).(s); |
| 397 | return -1; |
| 398 | } |
| 399 | |
| 400 | rc = _connectcxx . . $ _connect(8265182d1515ebe4).(s, (struct sockaddrcxx . . $ sockaddr# *) &addr, sizeof(addr), &ctx->response_timeoutcxx . . $ _modbus#response_timeout.); |
| 401 | if (rc == -1) { |
| 402 | closecxx . . $ close(7864480464b09eea).(s); |
| 403 | return -1; |
| 404 | } |
| 405 | |
| 406 | /* Replace any previously open socket */ |
| 407 | if (ctx->scxx . . $ _modbus#s. >= 0) { |
| 408 | closecxx . . $ close(7864480464b09eea).(ctx->scxx . . $ _modbus#s.); |
| 409 | } |
| 410 | ctx->scxx . . $ _modbus#s. = s; |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | /* Establishes a modbus TCP PI connection with a Modbus server. */ |
| 416 | static int _modbus_tcp_pi_connectcxx . . $ _modbus_tcp_pi_connect(c57adc5f48380e39).(modbus_tcxx . . $ modbus_t# *ctx) |
| 417 | { |
| 418 | int rc; |
| 419 | int new_s = -1; |
| 420 | struct addrinfocxx . . $ addrinfo# *ai_list; |
| 421 | struct addrinfocxx . . $ addrinfo# *ai_ptr; |
| 422 | struct addrinfocxx . . $ addrinfo# ai_hints; |
| 423 | modbus_tcp_pi_tcxx . . $ modbus_tcp_pi_t# *ctx_tcp_pi = ctx->backend_datacxx . . $ _modbus#backend_data.; |
| 424 | |
| 425 | #ifdef OS_WIN32 |
| 426 | if (_modbus_tcp_init_win32() == -1) { |
| 427 | return -1; |
| 428 | } |
| 429 | #endif |
| 430 | |
| 431 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(&ai_hints, 0, sizeof(ai_hints)); |
| 432 | #ifdef AI_ADDRCONFIGcxx . . $ `<external>/564697342961150458/netdb.h:601:10`! |
| 433 | ai_hints.ai_flagscxx . . $ addrinfo#ai_flags. |= AI_ADDRCONFIGcxx . . $ `<external>/564697342961150458/netdb.h:601:10`!; |
| 434 | #endif |
| 435 | ai_hints.ai_familycxx . . $ addrinfo#ai_family. = AF_UNSPECcxx . . $ `<external>/8962547315054503424/socket.h:93:9`!; |
| 436 | ai_hints.ai_socktypecxx . . $ addrinfo#ai_socktype. = SOCK_STREAMcxx . . $ `<external>/5956532346368192945/socket_type.h:28:9`!; |
| 437 | ai_hints.ai_addrcxx . . $ addrinfo#ai_addr. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 438 | ai_hints.ai_canonnamecxx . . $ addrinfo#ai_canonname. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 439 | ai_hints.ai_nextcxx . . $ addrinfo#ai_next. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 440 | |
| 441 | ai_list = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 442 | rc = getaddrinfocxx . . $ getaddrinfo(f68af21b9849b03e).(ctx_tcp_pi->nodecxx . . $ _modbus_tcp_pi#node., ctx_tcp_pi->servicecxx . . $ _modbus_tcp_pi#service., &ai_hints, &ai_list); |
| 443 | if (rc != 0) { |
| 444 | if (ctx->debugcxx . . $ _modbus#debug.) { |
| 445 | #ifdef HAVE_GAI_STRERRORcxx . . $ `./../config.h:35:9`! |
| 446 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Error returned by getaddrinfo: %s\n", gai_strerrorcxx . . $ gai_strerror(f7ae7e56280f18ae).(rc)); |
| 447 | #else |
| 448 | fprintf(stderr, "Error returned by getaddrinfo: %d\n", rc); |
| 449 | #endif |
| 450 | } |
| 451 | if (ai_list != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 452 | freeaddrinfocxx . . $ freeaddrinfo(aadc8f19092a28e8).(ai_list); |
| 453 | } |
| 454 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ECONNREFUSEDcxx . . $ `<external>/17922322188510340118/errno.h:95:9`!; |
| 455 | return -1; |
| 456 | } |
| 457 | |
| 458 | for (ai_ptr = ai_list; ai_ptr != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; ai_ptr = ai_ptr->ai_nextcxx . . $ addrinfo#ai_next.) { |
| 459 | int flags = ai_ptr->ai_socktypecxx . . $ addrinfo#ai_socktype.; |
| 460 | int s; |
| 461 | |
| 462 | #ifdef SOCK_CLOEXECcxx . . $ `<external>/5956532346368192945/socket_type.h:51:9`! |
| 463 | flags |= SOCK_CLOEXECcxx . . $ `<external>/5956532346368192945/socket_type.h:51:9`!; |
| 464 | #endif |
| 465 | |
| 466 | #ifdef SOCK_NONBLOCKcxx . . $ `<external>/5956532346368192945/socket_type.h:54:9`! |
| 467 | flags |= SOCK_NONBLOCKcxx . . $ `<external>/5956532346368192945/socket_type.h:54:9`!; |
| 468 | #endif |
| 469 | |
| 470 | s = socketcxx . . $ socket(2326284602b0bb2d).(ai_ptr->ai_familycxx . . $ addrinfo#ai_family., flags, ai_ptr->ai_protocolcxx . . $ addrinfo#ai_protocol.); |
| 471 | if (s < 0) |
| 472 | continue; |
| 473 | |
| 474 | if (s >= FD_SETSIZEcxx . . $ `<external>/946878812840035423/select.h:73:9`!) { |
| 475 | if (ctx->debugcxx . . $ _modbus#debug.) { |
| 476 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).( |
| 477 | stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, |
| 478 | "ERROR Socket descriptor %d exceeds FD_SETSIZE (%d)\n", |
| 479 | s, |
| 480 | FD_SETSIZEcxx . . $ `<external>/946878812840035423/select.h:73:9`!); |
| 481 | } |
| 482 | closecxx . . $ close(7864480464b09eea).(s); |
| 483 | continue; |
| 484 | } |
| 485 | |
| 486 | if (ai_ptr->ai_familycxx . . $ addrinfo#ai_family. == AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!) |
| 487 | _modbus_tcp_set_ipv4_optionscxx . . $ _modbus_tcp_set_ipv4_options(7864480464b09eea).(s); |
| 488 | |
| 489 | if (ctx->debugcxx . . $ _modbus#debug.) { |
| 490 | printfcxx . . $ printf(581fddd72a8db8fc).("Connecting to [%s]:%s\n", ctx_tcp_pi->nodecxx . . $ _modbus_tcp_pi#node., ctx_tcp_pi->servicecxx . . $ _modbus_tcp_pi#service.); |
| 491 | } |
| 492 | |
| 493 | rc = _connectcxx . . $ _connect(8265182d1515ebe4).(s, ai_ptr->ai_addrcxx . . $ addrinfo#ai_addr., ai_ptr->ai_addrlencxx . . $ addrinfo#ai_addrlen., &ctx->response_timeoutcxx . . $ _modbus#response_timeout.); |
| 494 | if (rc == -1) { |
| 495 | closecxx . . $ close(7864480464b09eea).(s); |
| 496 | continue; |
| 497 | } |
| 498 | |
| 499 | new_s = s; |
| 500 | break; |
| 501 | } |
| 502 | |
| 503 | freeaddrinfocxx . . $ freeaddrinfo(aadc8f19092a28e8).(ai_list); |
| 504 | |
| 505 | if (new_s < 0) { |
| 506 | return -1; |
| 507 | } |
| 508 | |
| 509 | /* Replace any previously open socket */ |
| 510 | if (ctx->scxx . . $ _modbus#s. >= 0) { |
| 511 | closecxx . . $ close(7864480464b09eea).(ctx->scxx . . $ _modbus#s.); |
| 512 | } |
| 513 | ctx->scxx . . $ _modbus#s. = new_s; |
| 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | static unsigned int _modbus_tcp_is_connectedcxx . . $ _modbus_tcp_is_connected(4468d237960bb42).(modbus_tcxx . . $ modbus_t# *ctx) |
| 519 | { |
| 520 | return ctx->scxx . . $ _modbus#s. >= 0; |
| 521 | } |
| 522 | |
| 523 | /* Closes the network connection and socket in TCP mode */ |
| 524 | static void _modbus_tcp_closecxx . . $ _modbus_tcp_close(24b4870c26038e9).(modbus_tcxx . . $ modbus_t# *ctx) |
| 525 | { |
| 526 | if (ctx->scxx . . $ _modbus#s. >= 0) { |
| 527 | shutdowncxx . . $ shutdown(9b79fb6aee4c0440).(ctx->scxx . . $ _modbus#s., SHUT_RDWRcxx . . $ `<external>/14344695994193663403/socket.h:48:9`!); |
| 528 | closecxx . . $ close(7864480464b09eea).(ctx->scxx . . $ _modbus#s.); |
| 529 | ctx->scxx . . $ _modbus#s. = -1; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | static int _modbus_tcp_flushcxx . . $ _modbus_tcp_flush(c57adc5f48380e39).(modbus_tcxx . . $ modbus_t# *ctx) |
| 534 | { |
| 535 | int rc; |
| 536 | // Use an unsigned 16-bit integer to reduce overflow risk. The flush function |
| 537 | // is not expected to handle huge amounts of data (> 2GB). |
| 538 | uint16_tcxx . . $ uint16_t# rc_sum = 0; |
| 539 | |
| 540 | do { |
| 541 | /* Extract the garbage from the socket */ |
| 542 | char devnull[MODBUS_TCP_MAX_ADU_LENGTHcxx . . $ `src/modbus-tcp.h:40:9`!]; |
| 543 | #ifndef OS_WIN32 |
| 544 | rc = recvcxx . . $ recv(4136b1fcb15a75da).(ctx->scxx . . $ _modbus#s., devnull, MODBUS_TCP_MAX_ADU_LENGTHcxx . . $ `src/modbus-tcp.h:40:9`!, MSG_DONTWAITcxx . . $ `<external>/8962547315054503424/socket.h:226:9`!); |
| 545 | #else |
| 546 | /* On Win32, it's a bit more complicated to not wait */ |
| 547 | fd_set rset; |
| 548 | struct timeval tv; |
| 549 | |
| 550 | tv.tv_sec = 0; |
| 551 | tv.tv_usec = 0; |
| 552 | FD_ZERO(&rset); |
| 553 | if (ctx->s < 0 || ctx->s >= FD_SETSIZE) { |
| 554 | errno = EINVAL; |
| 555 | return -1; |
| 556 | } |
| 557 | FD_SET(ctx->s, &rset); |
| 558 | rc = select(ctx->s + 1, &rset, NULL, NULL, &tv); |
| 559 | if (rc == -1) { |
| 560 | return -1; |
| 561 | } |
| 562 | |
| 563 | if (rc == 1) { |
| 564 | /* There is data to flush */ |
| 565 | rc = recv(ctx->s, devnull, MODBUS_TCP_MAX_ADU_LENGTH, 0); |
| 566 | } |
| 567 | #endif |
| 568 | if (rc > 0) { |
| 569 | // Check for overflow before adding |
| 570 | if (rc_sum <= UINT16_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:123:10`! - rc) { |
| 571 | rc_sum += rc; |
| 572 | } else { |
| 573 | // Handle overflow |
| 574 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EOVERFLOWcxx . . $ `<external>/17922322188510340118/errno.h:59:9`!; |
| 575 | return -1; |
| 576 | } |
| 577 | } |
| 578 | } while (rc == MODBUS_TCP_MAX_ADU_LENGTHcxx . . $ `src/modbus-tcp.h:40:9`!); |
| 579 | |
| 580 | /* Cast is safe: uint16_t always fits in int, and overflow is checked above */ |
| 581 | return (int) rc_sum; |
| 582 | } |
| 583 | |
| 584 | /* Listens for any request from one or many modbus masters in TCP */ |
| 585 | int modbus_tcp_listencxx . . $ modbus_tcp_listen(2a7e0e283938d456).(modbus_tcxx . . $ modbus_t# *ctx, int nb_connection) |
| 586 | { |
| 587 | int new_s; |
| 588 | int enable; |
| 589 | int flags; |
| 590 | struct sockaddr_incxx . . $ sockaddr_in# addr; |
| 591 | modbus_tcp_tcxx . . $ modbus_tcp_t# *ctx_tcp; |
| 592 | int rc; |
| 593 | |
| 594 | if (ctx == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 595 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 596 | return -1; |
| 597 | } |
| 598 | |
| 599 | ctx_tcp = ctx->backend_datacxx . . $ _modbus#backend_data.; |
| 600 | |
| 601 | #ifdef OS_WIN32 |
| 602 | if (_modbus_tcp_init_win32() == -1) { |
| 603 | return -1; |
| 604 | } |
| 605 | #endif |
| 606 | |
| 607 | flags = SOCK_STREAMcxx . . $ `<external>/5956532346368192945/socket_type.h:28:9`!; |
| 608 | |
| 609 | #ifdef SOCK_CLOEXECcxx . . $ `<external>/5956532346368192945/socket_type.h:51:9`! |
| 610 | flags |= SOCK_CLOEXECcxx . . $ `<external>/5956532346368192945/socket_type.h:51:9`!; |
| 611 | #endif |
| 612 | |
| 613 | new_s = socketcxx . . $ socket(2326284602b0bb2d).(PF_INETcxx . . $ `<external>/8962547315054503424/socket.h:45:9`!, flags, IPPROTO_TCPcxx . . $ `<external>/9581057695539925927/in.h:51:9`!); |
| 614 | if (new_s == -1) { |
| 615 | return -1; |
| 616 | } |
| 617 | |
| 618 | enable = 1; |
| 619 | #ifdef _WIN32 |
| 620 | if (setsockopt(new_s, SOL_SOCKET, SO_REUSEADDR, (const char *)&enable, sizeof(enable)) == -1) { |
| 621 | #else |
| 622 | if (setsockoptcxx . . $ setsockopt(506cc1d0fc70fdf1).(new_s, SOL_SOCKETcxx . . $ `<external>/9030916627833342711/socket.h:9:9`!, SO_REUSEADDRcxx . . $ `<external>/9030916627833342711/socket.h:12:9`!, (const void *)&enable, sizeof(enable)) == -1) { |
| 623 | #endif |
| 624 | closecxx . . $ close(7864480464b09eea).(new_s); |
| 625 | return -1; |
| 626 | } |
| 627 | |
| 628 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(&addr, 0, sizeof(addr)); |
| 629 | addr.sin_familycxx . . $ sockaddr_in#sin_family. = AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!; |
| 630 | /* If the modbus port is < to 1024, we need the setuid root. */ |
| 631 | addr.sin_portcxx . . $ sockaddr_in#sin_port. = htonscxx . . $ `<external>/9581057695539925927/in.h:431:12`!(ctx_tcp->port); |
| 632 | if (ctx_tcp->ipcxx . . $ _modbus_tcp#ip.[0] == '0') { |
| 633 | /* Listen any addresses */ |
| 634 | addr.sin_addrcxx . . $ sockaddr_in#sin_addr..s_addrcxx . . $ in_addr#s_addr. = htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(INADDR_ANYcxx . . $ `<external>/9581057695539925927/in.h:198:9`!); |
| 635 | } else { |
| 636 | /* Listen only specified IP address */ |
| 637 | rc = inet_ptoncxx . . $ inet_pton(8d303d671d73812).(addr.sin_familycxx . . $ sockaddr_in#sin_family., ctx_tcp->ipcxx . . $ _modbus_tcp#ip., &(addr.sin_addrcxx . . $ sockaddr_in#sin_addr.)); |
| 638 | if (rc <= 0) { |
| 639 | if (ctx->debugcxx . . $ _modbus#debug.) { |
| 640 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Invalid IP address: %s\n", ctx_tcp->ipcxx . . $ _modbus_tcp#ip.); |
| 641 | } |
| 642 | closecxx . . $ close(7864480464b09eea).(new_s); |
| 643 | return -1; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | if (bindcxx . . $ bind(f39c6c650450bfdd).(new_s, (struct sockaddrcxx . . $ sockaddr# *) &addr, sizeof(addr)) == -1) { |
| 648 | closecxx . . $ close(7864480464b09eea).(new_s); |
| 649 | return -1; |
| 650 | } |
| 651 | |
| 652 | if (listencxx . . $ listen(9b79fb6aee4c0440).(new_s, nb_connection) == -1) { |
| 653 | closecxx . . $ close(7864480464b09eea).(new_s); |
| 654 | return -1; |
| 655 | } |
| 656 | |
| 657 | return new_s; |
| 658 | } |
| 659 | |
| 660 | int modbus_tcp_pi_listencxx . . $ modbus_tcp_pi_listen(2a7e0e283938d456).(modbus_tcxx . . $ modbus_t# *ctx, int nb_connection) |
| 661 | { |
| 662 | int rc; |
| 663 | struct addrinfocxx . . $ addrinfo# *ai_list; |
| 664 | struct addrinfocxx . . $ addrinfo# *ai_ptr; |
| 665 | struct addrinfocxx . . $ addrinfo# ai_hints; |
| 666 | const char *node; |
| 667 | const char *service; |
| 668 | int new_s; |
| 669 | modbus_tcp_pi_tcxx . . $ modbus_tcp_pi_t# *ctx_tcp_pi; |
| 670 | |
| 671 | if (ctx == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 672 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 673 | return -1; |
| 674 | } |
| 675 | |
| 676 | ctx_tcp_pi = ctx->backend_datacxx . . $ _modbus#backend_data.; |
| 677 | |
| 678 | #ifdef OS_WIN32 |
| 679 | if (_modbus_tcp_init_win32() == -1) { |
| 680 | return -1; |
| 681 | } |
| 682 | #endif |
| 683 | |
| 684 | if (ctx_tcp_pi->nodecxx . . $ _modbus_tcp_pi#node.[0] == 0) { |
| 685 | node = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; /* == any */ |
| 686 | } else { |
| 687 | node = ctx_tcp_pi->nodecxx . . $ _modbus_tcp_pi#node.; |
| 688 | } |
| 689 | |
| 690 | if (ctx_tcp_pi->servicecxx . . $ _modbus_tcp_pi#service.[0] == 0) { |
| 691 | service = "502"; |
| 692 | } else { |
| 693 | service = ctx_tcp_pi->servicecxx . . $ _modbus_tcp_pi#service.; |
| 694 | } |
| 695 | |
| 696 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(&ai_hints, 0, sizeof(ai_hints)); |
| 697 | /* If node is not NULL, than the AI_PASSIVE flag is ignored. */ |
| 698 | ai_hints.ai_flagscxx . . $ addrinfo#ai_flags. |= AI_PASSIVEcxx . . $ `<external>/564697342961150458/netdb.h:596:10`!; |
| 699 | #ifdef AI_ADDRCONFIGcxx . . $ `<external>/564697342961150458/netdb.h:601:10`! |
| 700 | ai_hints.ai_flagscxx . . $ addrinfo#ai_flags. |= AI_ADDRCONFIGcxx . . $ `<external>/564697342961150458/netdb.h:601:10`!; |
| 701 | #endif |
| 702 | ai_hints.ai_familycxx . . $ addrinfo#ai_family. = AF_UNSPECcxx . . $ `<external>/8962547315054503424/socket.h:93:9`!; |
| 703 | ai_hints.ai_socktypecxx . . $ addrinfo#ai_socktype. = SOCK_STREAMcxx . . $ `<external>/5956532346368192945/socket_type.h:28:9`!; |
| 704 | ai_hints.ai_addrcxx . . $ addrinfo#ai_addr. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 705 | ai_hints.ai_canonnamecxx . . $ addrinfo#ai_canonname. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 706 | ai_hints.ai_nextcxx . . $ addrinfo#ai_next. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 707 | |
| 708 | ai_list = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 709 | rc = getaddrinfocxx . . $ getaddrinfo(f68af21b9849b03e).(node, service, &ai_hints, &ai_list); |
| 710 | if (rc != 0) { |
| 711 | if (ctx->debugcxx . . $ _modbus#debug.) { |
| 712 | #ifdef HAVE_GAI_STRERRORcxx . . $ `./../config.h:35:9`! |
| 713 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Error returned by getaddrinfo: %s\n", gai_strerrorcxx . . $ gai_strerror(f7ae7e56280f18ae).(rc)); |
| 714 | #else |
| 715 | fprintf(stderr, "Error returned by getaddrinfo: %d\n", rc); |
| 716 | #endif |
| 717 | } |
| 718 | if (ai_list != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 719 | freeaddrinfocxx . . $ freeaddrinfo(aadc8f19092a28e8).(ai_list); |
| 720 | } |
| 721 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ECONNREFUSEDcxx . . $ `<external>/17922322188510340118/errno.h:95:9`!; |
| 722 | return -1; |
| 723 | } |
| 724 | |
| 725 | new_s = -1; |
| 726 | for (ai_ptr = ai_list; ai_ptr != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; ai_ptr = ai_ptr->ai_nextcxx . . $ addrinfo#ai_next.) { |
| 727 | int flags = ai_ptr->ai_socktypecxx . . $ addrinfo#ai_socktype.; |
| 728 | int s; |
| 729 | |
| 730 | #ifdef SOCK_CLOEXECcxx . . $ `<external>/5956532346368192945/socket_type.h:51:9`! |
| 731 | flags |= SOCK_CLOEXECcxx . . $ `<external>/5956532346368192945/socket_type.h:51:9`!; |
| 732 | #endif |
| 733 | |
| 734 | s = socketcxx . . $ socket(2326284602b0bb2d).(ai_ptr->ai_familycxx . . $ addrinfo#ai_family., flags, ai_ptr->ai_protocolcxx . . $ addrinfo#ai_protocol.); |
| 735 | if (s < 0) { |
| 736 | if (ctx->debugcxx . . $ _modbus#debug.) { |
| 737 | perrorcxx . . $ perror(85c52e162fed56f9).("socket"); |
| 738 | } |
| 739 | continue; |
| 740 | } else { |
| 741 | int enable = 1; |
| 742 | #ifdef _WIN32 |
| 743 | rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&enable, sizeof(enable)); |
| 744 | #else |
| 745 | rc = setsockoptcxx . . $ setsockopt(506cc1d0fc70fdf1).(s, SOL_SOCKETcxx . . $ `<external>/9030916627833342711/socket.h:9:9`!, SO_REUSEADDRcxx . . $ `<external>/9030916627833342711/socket.h:12:9`!, (const void *)&enable, sizeof(enable)); |
| 746 | #endif |
| 747 | if (rc != 0) { |
| 748 | closecxx . . $ close(7864480464b09eea).(s); |
| 749 | if (ctx->debugcxx . . $ _modbus#debug.) { |
| 750 | perrorcxx . . $ perror(85c52e162fed56f9).("setsockopt"); |
| 751 | } |
| 752 | continue; |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | rc = bindcxx . . $ bind(f39c6c650450bfdd).(s, ai_ptr->ai_addrcxx . . $ addrinfo#ai_addr., ai_ptr->ai_addrlencxx . . $ addrinfo#ai_addrlen.); |
| 757 | if (rc != 0) { |
| 758 | closecxx . . $ close(7864480464b09eea).(s); |
| 759 | if (ctx->debugcxx . . $ _modbus#debug.) { |
| 760 | perrorcxx . . $ perror(85c52e162fed56f9).("bind"); |
| 761 | } |
| 762 | continue; |
| 763 | } |
| 764 | |
| 765 | rc = listencxx . . $ listen(9b79fb6aee4c0440).(s, nb_connection); |
| 766 | if (rc != 0) { |
| 767 | closecxx . . $ close(7864480464b09eea).(s); |
| 768 | if (ctx->debugcxx . . $ _modbus#debug.) { |
| 769 | perrorcxx . . $ perror(85c52e162fed56f9).("listen"); |
| 770 | } |
| 771 | continue; |
| 772 | } |
| 773 | |
| 774 | new_s = s; |
| 775 | break; |
| 776 | } |
| 777 | freeaddrinfocxx . . $ freeaddrinfo(aadc8f19092a28e8).(ai_list); |
| 778 | |
| 779 | if (new_s < 0) { |
| 780 | return -1; |
| 781 | } |
| 782 | |
| 783 | return new_s; |
| 784 | } |
| 785 | |
| 786 | int modbus_tcp_acceptcxx . . $ modbus_tcp_accept(e415c7f53a3f0b7).(modbus_tcxx . . $ modbus_t# *ctx, int *s) |
| 787 | { |
| 788 | struct sockaddr_incxx . . $ sockaddr_in# addr; |
| 789 | socklen_tcxx . . $ socklen_t# addrlen; |
| 790 | |
| 791 | if (ctx == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 792 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 793 | return -1; |
| 794 | } |
| 795 | |
| 796 | addrlen = sizeof(addr); |
| 797 | #ifdef HAVE_ACCEPT4cxx . . $ `./../config.h:5:9`! |
| 798 | /* Inherit socket flags and use accept4 call */ |
| 799 | ctx->scxx . . $ _modbus#s. = accept4cxx . . $ accept4(ff40fae3a4378b53).(*s, (struct sockaddrcxx . . $ sockaddr# *) &addr, &addrlen, SOCK_CLOEXECcxx . . $ `<external>/5956532346368192945/socket_type.h:51:9`!); |
| 800 | #else |
| 801 | ctx->s = accept(*s, (struct sockaddr *) &addr, &addrlen); |
| 802 | #endif |
| 803 | |
| 804 | if (ctx->scxx . . $ _modbus#s. < 0) { |
| 805 | return -1; |
| 806 | } |
| 807 | |
| 808 | if (ctx->scxx . . $ _modbus#s. >= FD_SETSIZEcxx . . $ `<external>/946878812840035423/select.h:73:9`!) { |
| 809 | if (ctx->debugcxx . . $ _modbus#debug.) { |
| 810 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).( |
| 811 | stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, |
| 812 | "ERROR Socket descriptor %d exceeds FD_SETSIZE (%d)\n", |
| 813 | ctx->scxx . . $ _modbus#s., |
| 814 | FD_SETSIZEcxx . . $ `<external>/946878812840035423/select.h:73:9`!); |
| 815 | } |
| 816 | closecxx . . $ close(7864480464b09eea).(ctx->scxx . . $ _modbus#s.); |
| 817 | ctx->scxx . . $ _modbus#s. = -1; |
| 818 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 819 | return -1; |
| 820 | } |
| 821 | |
| 822 | if (ctx->debugcxx . . $ _modbus#debug.) { |
| 823 | char buf[INET_ADDRSTRLENcxx . . $ `<external>/9581057695539925927/in.h:244:9`!]; |
| 824 | if (inet_ntopcxx . . $ inet_ntop(6a923972b45bea03).(AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!, &(addr.sin_addrcxx . . $ sockaddr_in#sin_addr.), buf, INET_ADDRSTRLENcxx . . $ `<external>/9581057695539925927/in.h:244:9`!) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 825 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Client connection accepted from unparsable IP.\n"); |
| 826 | } else { |
| 827 | printfcxx . . $ printf(581fddd72a8db8fc).("Client connection accepted from %s.\n", buf); |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | return ctx->scxx . . $ _modbus#s.; |
| 832 | } |
| 833 | |
| 834 | int modbus_tcp_pi_acceptcxx . . $ modbus_tcp_pi_accept(e415c7f53a3f0b7).(modbus_tcxx . . $ modbus_t# *ctx, int *s) |
| 835 | { |
| 836 | struct sockaddr_in6cxx . . $ sockaddr_in6# addr; |
| 837 | socklen_tcxx . . $ socklen_t# addrlen; |
| 838 | |
| 839 | if (ctx == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 840 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 841 | return -1; |
| 842 | } |
| 843 | |
| 844 | addrlen = sizeof(addr); |
| 845 | #ifdef HAVE_ACCEPT4cxx . . $ `./../config.h:5:9`! |
| 846 | /* Inherit socket flags and use accept4 call */ |
| 847 | ctx->scxx . . $ _modbus#s. = accept4cxx . . $ accept4(ff40fae3a4378b53).(*s, (struct sockaddrcxx . . $ sockaddr# *) &addr, &addrlen, SOCK_CLOEXECcxx . . $ `<external>/5956532346368192945/socket_type.h:51:9`!); |
| 848 | #else |
| 849 | ctx->s = accept(*s, (struct sockaddr *) &addr, &addrlen); |
| 850 | #endif |
| 851 | |
| 852 | if (ctx->scxx . . $ _modbus#s. < 0) { |
| 853 | return -1; |
| 854 | } |
| 855 | |
| 856 | if (ctx->scxx . . $ _modbus#s. >= FD_SETSIZEcxx . . $ `<external>/946878812840035423/select.h:73:9`!) { |
| 857 | if (ctx->debugcxx . . $ _modbus#debug.) { |
| 858 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).( |
| 859 | stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, |
| 860 | "ERROR Socket descriptor %d exceeds FD_SETSIZE (%d)\n", |
| 861 | ctx->scxx . . $ _modbus#s., |
| 862 | FD_SETSIZEcxx . . $ `<external>/946878812840035423/select.h:73:9`!); |
| 863 | } |
| 864 | closecxx . . $ close(7864480464b09eea).(ctx->scxx . . $ _modbus#s.); |
| 865 | ctx->scxx . . $ _modbus#s. = -1; |
| 866 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 867 | return -1; |
| 868 | } |
| 869 | |
| 870 | if (ctx->debugcxx . . $ _modbus#debug.) { |
| 871 | char buf[INET6_ADDRSTRLENcxx . . $ `<external>/9581057695539925927/in.h:245:9`!]; |
| 872 | if (inet_ntopcxx . . $ inet_ntop(6a923972b45bea03).(AF_INET6cxx . . $ `<external>/8962547315054503424/socket.h:105:9`!, &(addr.sin6_addrcxx . . $ sockaddr_in6#sin6_addr.), buf, INET6_ADDRSTRLENcxx . . $ `<external>/9581057695539925927/in.h:245:9`!) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 873 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Client connection accepted from unparsable IP.\n"); |
| 874 | } else { |
| 875 | printfcxx . . $ printf(581fddd72a8db8fc).("Client connection accepted from %s.\n", buf); |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | return ctx->scxx . . $ _modbus#s.; |
| 880 | } |
| 881 | |
| 882 | static int |
| 883 | _modbus_tcp_selectcxx . . $ _modbus_tcp_select(da6c858c7786b0d5).(modbus_tcxx . . $ modbus_t# *ctx, fd_setcxx . . $ fd_set# *rset, struct timevalcxx . . $ timeval# *tv, int length_to_read) |
| 884 | { |
| 885 | int s_rc; |
| 886 | while ((s_rc = selectcxx . . $ select(6d24c7514c8a5b88).(ctx->scxx . . $ _modbus#s. + 1, rset, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, tv)) == -1) { |
| 887 | if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EINTRcxx . . $ `<external>/8987895939074593727/errno-base.h:8:9`!) { |
| 888 | if (ctx->debugcxx . . $ _modbus#debug.) { |
| 889 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "A non blocked signal was caught\n"); |
| 890 | } |
| 891 | /* Necessary after an error */ |
| 892 | FD_ZEROcxx . . $ `<external>/946878812840035423/select.h:88:9`!(rset); |
| 893 | if (ctx->scxx . . $ _modbus#s. < 0 || ctx->scxx . . $ _modbus#s. >= FD_SETSIZEcxx . . $ `<external>/946878812840035423/select.h:73:9`!) { |
| 894 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 895 | return -1; |
| 896 | } |
| 897 | FD_SETcxx . . $ `<external>/946878812840035423/select.h:85:9`!(ctx->s, rset); |
| 898 | } else { |
| 899 | return -1; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | if (s_rc == 0) { |
| 904 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ETIMEDOUTcxx . . $ `<external>/17922322188510340118/errno.h:94:9`!; |
| 905 | return -1; |
| 906 | } |
| 907 | |
| 908 | return s_rc; |
| 909 | } |
| 910 | |
| 911 | static void _modbus_tcp_freecxx . . $ _modbus_tcp_free(24b4870c26038e9).(modbus_tcxx . . $ modbus_t# *ctx) |
| 912 | { |
| 913 | if (ctx->backend_datacxx . . $ _modbus#backend_data.) { |
| 914 | freecxx . . $ free(5d22bdacc48458e8).(ctx->backend_datacxx . . $ _modbus#backend_data.); |
| 915 | } |
| 916 | freecxx . . $ free(5d22bdacc48458e8).(ctx); |
| 917 | } |
| 918 | |
| 919 | static void _modbus_tcp_pi_freecxx . . $ _modbus_tcp_pi_free(24b4870c26038e9).(modbus_tcxx . . $ modbus_t# *ctx) |
| 920 | { |
| 921 | if (ctx->backend_datacxx . . $ _modbus#backend_data.) { |
| 922 | modbus_tcp_pi_tcxx . . $ modbus_tcp_pi_t# *ctx_tcp_pi = ctx->backend_datacxx . . $ _modbus#backend_data.; |
| 923 | freecxx . . $ free(5d22bdacc48458e8).(ctx_tcp_pi->nodecxx . . $ _modbus_tcp_pi#node.); |
| 924 | freecxx . . $ free(5d22bdacc48458e8).(ctx_tcp_pi->servicecxx . . $ _modbus_tcp_pi#service.); |
| 925 | freecxx . . $ free(5d22bdacc48458e8).(ctx->backend_datacxx . . $ _modbus#backend_data.); |
| 926 | } |
| 927 | |
| 928 | freecxx . . $ free(5d22bdacc48458e8).(ctx); |
| 929 | } |
| 930 | |
| 931 | // clang-format off |
| 932 | const modbus_backend_tcxx . . $ modbus_backend_t# _modbus_tcp_backendcxx . . $ _modbus_tcp_backend. = { |
| 933 | _MODBUS_BACKEND_TYPE_TCPcxx . . $ _MODBUS_BACKEND_TYPE_TCP., |
| 934 | _MODBUS_TCP_HEADER_LENGTHcxx . . $ `src/modbus-tcp-private.h:10:9`!, |
| 935 | _MODBUS_TCP_CHECKSUM_LENGTHcxx . . $ `src/modbus-tcp-private.h:14:9`!, |
| 936 | MODBUS_TCP_MAX_ADU_LENGTHcxx . . $ `src/modbus-tcp.h:40:9`!, |
| 937 | _modbus_set_slavecxx . . $ _modbus_set_slave(2a7e0e283938d456)., |
| 938 | _modbus_tcp_build_request_basiscxx . . $ _modbus_tcp_build_request_basis(abc53b0a0396e541)., |
| 939 | _modbus_tcp_build_response_basiscxx . . $ _modbus_tcp_build_response_basis(acd9c6258f33b2f4)., |
| 940 | _modbus_tcp_get_response_tidcxx . . $ _modbus_tcp_get_response_tid(b84ccf4e0bb14b06)., |
| 941 | _modbus_tcp_send_msg_precxx . . $ _modbus_tcp_send_msg_pre(7405b416d0535413)., |
| 942 | _modbus_tcp_sendcxx . . $ _modbus_tcp_send(ab2063bac0ea77a5)., |
| 943 | _modbus_tcp_receivecxx . . $ _modbus_tcp_receive(744a031d86f39170)., |
| 944 | _modbus_tcp_recvcxx . . $ _modbus_tcp_recv(41755522a7cda552)., |
| 945 | _modbus_tcp_check_integritycxx . . $ _modbus_tcp_check_integrity(b8ea090d50367a38)., |
| 946 | _modbus_tcp_pre_check_confirmationcxx . . $ _modbus_tcp_pre_check_confirmation(23551e319a37d2dc)., |
| 947 | _modbus_tcp_connectcxx . . $ _modbus_tcp_connect(c57adc5f48380e39)., |
| 948 | _modbus_tcp_is_connectedcxx . . $ _modbus_tcp_is_connected(4468d237960bb42)., |
| 949 | _modbus_tcp_closecxx . . $ _modbus_tcp_close(24b4870c26038e9)., |
| 950 | _modbus_tcp_flushcxx . . $ _modbus_tcp_flush(c57adc5f48380e39)., |
| 951 | _modbus_tcp_selectcxx . . $ _modbus_tcp_select(da6c858c7786b0d5)., |
| 952 | _modbus_tcp_freecxx . . $ _modbus_tcp_free(24b4870c26038e9). |
| 953 | }; |
| 954 | |
| 955 | const modbus_backend_tcxx . . $ modbus_backend_t# _modbus_tcp_pi_backendcxx . . $ _modbus_tcp_pi_backend. = { |
| 956 | _MODBUS_BACKEND_TYPE_TCPcxx . . $ _MODBUS_BACKEND_TYPE_TCP., |
| 957 | _MODBUS_TCP_HEADER_LENGTHcxx . . $ `src/modbus-tcp-private.h:10:9`!, |
| 958 | _MODBUS_TCP_CHECKSUM_LENGTHcxx . . $ `src/modbus-tcp-private.h:14:9`!, |
| 959 | MODBUS_TCP_MAX_ADU_LENGTHcxx . . $ `src/modbus-tcp.h:40:9`!, |
| 960 | _modbus_set_slavecxx . . $ _modbus_set_slave(2a7e0e283938d456)., |
| 961 | _modbus_tcp_build_request_basiscxx . . $ _modbus_tcp_build_request_basis(abc53b0a0396e541)., |
| 962 | _modbus_tcp_build_response_basiscxx . . $ _modbus_tcp_build_response_basis(acd9c6258f33b2f4)., |
| 963 | _modbus_tcp_get_response_tidcxx . . $ _modbus_tcp_get_response_tid(b84ccf4e0bb14b06)., |
| 964 | _modbus_tcp_send_msg_precxx . . $ _modbus_tcp_send_msg_pre(7405b416d0535413)., |
| 965 | _modbus_tcp_sendcxx . . $ _modbus_tcp_send(ab2063bac0ea77a5)., |
| 966 | _modbus_tcp_receivecxx . . $ _modbus_tcp_receive(744a031d86f39170)., |
| 967 | _modbus_tcp_recvcxx . . $ _modbus_tcp_recv(41755522a7cda552)., |
| 968 | _modbus_tcp_check_integritycxx . . $ _modbus_tcp_check_integrity(b8ea090d50367a38)., |
| 969 | _modbus_tcp_pre_check_confirmationcxx . . $ _modbus_tcp_pre_check_confirmation(23551e319a37d2dc)., |
| 970 | _modbus_tcp_pi_connectcxx . . $ _modbus_tcp_pi_connect(c57adc5f48380e39)., |
| 971 | _modbus_tcp_is_connectedcxx . . $ _modbus_tcp_is_connected(4468d237960bb42)., |
| 972 | _modbus_tcp_closecxx . . $ _modbus_tcp_close(24b4870c26038e9)., |
| 973 | _modbus_tcp_flushcxx . . $ _modbus_tcp_flush(c57adc5f48380e39)., |
| 974 | _modbus_tcp_selectcxx . . $ _modbus_tcp_select(da6c858c7786b0d5)., |
| 975 | _modbus_tcp_pi_freecxx . . $ _modbus_tcp_pi_free(24b4870c26038e9). |
| 976 | }; |
| 977 | |
| 978 | // clang-format on |
| 979 | |
| 980 | modbus_tcxx . . $ modbus_t# *modbus_new_tcpcxx . . $ modbus_new_tcp(5f123d74b77c7850).(const char *ip, int port) |
| 981 | { |
| 982 | modbus_tcxx . . $ modbus_t# *ctx; |
| 983 | modbus_tcp_tcxx . . $ modbus_tcp_t# *ctx_tcp; |
| 984 | size_t dest_size; |
| 985 | size_t ret_size; |
| 986 | |
| 987 | #if defined(OS_BSD) |
| 988 | /* MSG_NOSIGNAL is unsupported on *BSD so we install an ignore |
| 989 | handler for SIGPIPE. */ |
| 990 | struct sigaction sa; |
| 991 | |
| 992 | memset(&sa, 0, sizeof(sa)); |
| 993 | sa.sa_handler = SIG_IGN; |
| 994 | sigemptyset(&sa.sa_mask); |
| 995 | if (sigaction(SIGPIPE, &sa, NULL) < 0) { |
| 996 | /* The debug flag can't be set here... */ |
| 997 | fprintf(stderr, "Could not install SIGPIPE handler.\n"); |
| 998 | return NULL; |
| 999 | } |
| 1000 | #endif |
| 1001 | |
| 1002 | ctx = (modbus_tcxx . . $ modbus_t# *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(modbus_tcxx . . $ modbus_t#)); |
| 1003 | if (ctx == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 1004 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 1005 | } |
| 1006 | _modbus_init_commoncxx . . $ _modbus_init_common(24b4870c26038e9).(ctx); |
| 1007 | |
| 1008 | /* Could be changed after to reach a remote serial Modbus device */ |
| 1009 | ctx->slavecxx . . $ _modbus#slave. = MODBUS_TCP_SLAVEcxx . . $ `src/modbus-tcp.h:35:9`!; |
| 1010 | |
| 1011 | ctx->backendcxx . . $ _modbus#backend. = &_modbus_tcp_backendcxx . . $ _modbus_tcp_backend.; |
| 1012 | |
| 1013 | ctx->backend_datacxx . . $ _modbus#backend_data. = (modbus_tcp_tcxx . . $ modbus_tcp_t# *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(modbus_tcp_tcxx . . $ modbus_tcp_t#)); |
| 1014 | if (ctx->backend_datacxx . . $ _modbus#backend_data. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 1015 | modbus_freecxx . . $ modbus_free(24b4870c26038e9).(ctx); |
| 1016 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!; |
| 1017 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 1018 | } |
| 1019 | ctx_tcp = (modbus_tcp_tcxx . . $ modbus_tcp_t# *) ctx->backend_datacxx . . $ _modbus#backend_data.; |
| 1020 | |
| 1021 | if (ip != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 1022 | dest_size = sizeof(char) * 16; |
| 1023 | ret_size = strlcpycxx . . $ strlcpy(55abe9014c6d0e39).(ctx_tcp->ipcxx . . $ _modbus_tcp#ip., ip, dest_size); |
| 1024 | if (ret_size == 0) { |
| 1025 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "The IP string is empty\n"); |
| 1026 | modbus_freecxx . . $ modbus_free(24b4870c26038e9).(ctx); |
| 1027 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 1028 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 1029 | } |
| 1030 | |
| 1031 | if (ret_size >= dest_size) { |
| 1032 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "The IP string has been truncated\n"); |
| 1033 | modbus_freecxx . . $ modbus_free(24b4870c26038e9).(ctx); |
| 1034 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 1035 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 1036 | } |
| 1037 | } else { |
| 1038 | ctx_tcp->ipcxx . . $ _modbus_tcp#ip.[0] = '0'; |
| 1039 | ctx_tcp->ipcxx . . $ _modbus_tcp#ip.[1] = '\0'; |
| 1040 | } |
| 1041 | ctx_tcp->portcxx . . $ _modbus_tcp#port. = port; |
| 1042 | ctx_tcp->t_idcxx . . $ _modbus_tcp#t_id. = 0; |
| 1043 | |
| 1044 | return ctx; |
| 1045 | } |
| 1046 | |
| 1047 | modbus_tcxx . . $ modbus_t# *modbus_new_tcp_picxx . . $ modbus_new_tcp_pi(5f5df5bc2aa40d50).(const char *node, const char *service) |
| 1048 | { |
| 1049 | modbus_tcxx . . $ modbus_t# *ctx; |
| 1050 | modbus_tcp_pi_tcxx . . $ modbus_tcp_pi_t# *ctx_tcp_pi; |
| 1051 | |
| 1052 | ctx = (modbus_tcxx . . $ modbus_t# *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(modbus_tcxx . . $ modbus_t#)); |
| 1053 | if (ctx == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 1054 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 1055 | } |
| 1056 | _modbus_init_commoncxx . . $ _modbus_init_common(24b4870c26038e9).(ctx); |
| 1057 | |
| 1058 | /* Could be changed after to reach a remote serial Modbus device */ |
| 1059 | ctx->slavecxx . . $ _modbus#slave. = MODBUS_TCP_SLAVEcxx . . $ `src/modbus-tcp.h:35:9`!; |
| 1060 | |
| 1061 | ctx->backendcxx . . $ _modbus#backend. = &_modbus_tcp_pi_backendcxx . . $ _modbus_tcp_pi_backend.; |
| 1062 | |
| 1063 | ctx->backend_datacxx . . $ _modbus#backend_data. = (modbus_tcp_pi_tcxx . . $ modbus_tcp_pi_t# *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(modbus_tcp_pi_tcxx . . $ modbus_tcp_pi_t#)); |
| 1064 | if (ctx->backend_datacxx . . $ _modbus#backend_data. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 1065 | modbus_freecxx . . $ modbus_free(24b4870c26038e9).(ctx); |
| 1066 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!; |
| 1067 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 1068 | } |
| 1069 | ctx_tcp_pi = (modbus_tcp_pi_tcxx . . $ modbus_tcp_pi_t# *) ctx->backend_datacxx . . $ _modbus#backend_data.; |
| 1070 | ctx_tcp_pi->nodecxx . . $ _modbus_tcp_pi#node. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 1071 | ctx_tcp_pi->servicecxx . . $ _modbus_tcp_pi#service. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 1072 | |
| 1073 | if (node != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 1074 | ctx_tcp_pi->nodecxx . . $ _modbus_tcp_pi#node. = strdupcxx . . $ strdup(a8b8e32347182212).(node); |
| 1075 | } else { |
| 1076 | /* The node argument can be empty to indicate any hosts */ |
| 1077 | ctx_tcp_pi->nodecxx . . $ _modbus_tcp_pi#node. = strdupcxx . . $ strdup(a8b8e32347182212).(""); |
| 1078 | } |
| 1079 | |
| 1080 | if (ctx_tcp_pi->nodecxx . . $ _modbus_tcp_pi#node. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 1081 | modbus_freecxx . . $ modbus_free(24b4870c26038e9).(ctx); |
| 1082 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!; |
| 1083 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 1084 | } |
| 1085 | |
| 1086 | if (service != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! && service[0] != '\0') { |
| 1087 | ctx_tcp_pi->servicecxx . . $ _modbus_tcp_pi#service. = strdupcxx . . $ strdup(a8b8e32347182212).(service); |
| 1088 | } else { |
| 1089 | /* Default Modbus port number */ |
| 1090 | ctx_tcp_pi->servicecxx . . $ _modbus_tcp_pi#service. = strdupcxx . . $ strdup(a8b8e32347182212).("502"); |
| 1091 | } |
| 1092 | |
| 1093 | if (ctx_tcp_pi->servicecxx . . $ _modbus_tcp_pi#service. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 1094 | modbus_freecxx . . $ modbus_free(24b4870c26038e9).(ctx); |
| 1095 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!; |
| 1096 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 1097 | } |
| 1098 | |
| 1099 | ctx_tcp_pi->t_idcxx . . $ _modbus_tcp_pi#t_id. = 0; |
| 1100 | |
| 1101 | return ctx; |
| 1102 | } |