Fix many typos
[libmodbus.git] / src / modbus-tcp.c
blob51642737064e55342e3ccf1c9f76ddf0222589fe
1 /*
2 * Copyright © 2001-2013 Stéphane Raimbault <stephane.raimbault@gmail.com>
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 */
7 #if defined(_WIN32)
8 # define OS_WIN32
9 /* ws2_32.dll has getaddrinfo and freeaddrinfo on Windows XP and later.
10 * minwg32 headers check WINVER before allowing the use of these */
11 # ifndef WINVER
12 # define WINVER 0x0501
13 # endif
14 #endif
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <errno.h>
20 #ifndef _MSC_VER
21 #include <unistd.h>
22 #endif
23 #include <signal.h>
24 #include <sys/types.h>
26 #if defined(_WIN32)
27 /* Already set in modbus-tcp.h but it seems order matters in VS2005 */
28 # include <winsock2.h>
29 # include <ws2tcpip.h>
30 # define SHUT_RDWR 2
31 # define close closesocket
32 #else
33 # include <sys/socket.h>
34 # include <sys/ioctl.h>
36 #if defined(__OpenBSD__) || (defined(__FreeBSD__) && __FreeBSD__ < 5)
37 # define OS_BSD
38 # include <netinet/in_systm.h>
39 #endif
41 # include <netinet/in.h>
42 # include <netinet/ip.h>
43 # include <netinet/tcp.h>
44 # include <arpa/inet.h>
45 # include <netdb.h>
46 #endif
48 #if !defined(MSG_NOSIGNAL)
49 #define MSG_NOSIGNAL 0
50 #endif
52 #if defined(_AIX) && !defined(MSG_DONTWAIT)
53 #define MSG_DONTWAIT MSG_NONBLOCK
54 #endif
56 #include "modbus-private.h"
58 #include "modbus-tcp.h"
59 #include "modbus-tcp-private.h"
61 #ifdef OS_WIN32
62 static int _modbus_tcp_init_win32(void)
64 /* Initialise Windows Socket API */
65 WSADATA wsaData;
67 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
68 fprintf(stderr, "WSAStartup() returned error code %d\n",
69 (unsigned int)GetLastError());
70 errno = EIO;
71 return -1;
73 return 0;
75 #endif
77 static int _modbus_set_slave(modbus_t *ctx, int slave)
79 /* Broadcast address is 0 (MODBUS_BROADCAST_ADDRESS) */
80 if (slave >= 0 && slave <= 247) {
81 ctx->slave = slave;
82 } else if (slave == MODBUS_TCP_SLAVE) {
83 /* The special value MODBUS_TCP_SLAVE (0xFF) can be used in TCP mode to
84 * restore the default value. */
85 ctx->slave = slave;
86 } else {
87 errno = EINVAL;
88 return -1;
91 return 0;
94 /* Builds a TCP request header */
95 static int _modbus_tcp_build_request_basis(modbus_t *ctx, int function,
96 int addr, int nb,
97 uint8_t *req)
99 modbus_tcp_t *ctx_tcp = ctx->backend_data;
101 /* Increase transaction ID */
102 if (ctx_tcp->t_id < UINT16_MAX)
103 ctx_tcp->t_id++;
104 else
105 ctx_tcp->t_id = 0;
106 req[0] = ctx_tcp->t_id >> 8;
107 req[1] = ctx_tcp->t_id & 0x00ff;
109 /* Protocol Modbus */
110 req[2] = 0;
111 req[3] = 0;
113 /* Length will be defined later by set_req_length_tcp at offsets 4
114 and 5 */
116 req[6] = ctx->slave;
117 req[7] = function;
118 req[8] = addr >> 8;
119 req[9] = addr & 0x00ff;
120 req[10] = nb >> 8;
121 req[11] = nb & 0x00ff;
123 return _MODBUS_TCP_PRESET_REQ_LENGTH;
126 /* Builds a TCP response header */
127 static int _modbus_tcp_build_response_basis(sft_t *sft, uint8_t *rsp)
129 /* Extract from MODBUS Messaging on TCP/IP Implementation
130 Guide V1.0b (page 23/46):
131 The transaction identifier is used to associate the future
132 response with the request. */
133 rsp[0] = sft->t_id >> 8;
134 rsp[1] = sft->t_id & 0x00ff;
136 /* Protocol Modbus */
137 rsp[2] = 0;
138 rsp[3] = 0;
140 /* Length will be set later by send_msg (4 and 5) */
142 /* The slave ID is copied from the indication */
143 rsp[6] = sft->slave;
144 rsp[7] = sft->function;
146 return _MODBUS_TCP_PRESET_RSP_LENGTH;
150 static int _modbus_tcp_prepare_response_tid(const uint8_t *req, int *req_length)
152 return (req[0] << 8) + req[1];
155 static int _modbus_tcp_send_msg_pre(uint8_t *req, int req_length)
157 /* Subtract the header length to the message length */
158 int mbap_length = req_length - 6;
160 req[4] = mbap_length >> 8;
161 req[5] = mbap_length & 0x00FF;
163 return req_length;
166 static ssize_t _modbus_tcp_send(modbus_t *ctx, const uint8_t *req, int req_length)
168 /* MSG_NOSIGNAL
169 Requests not to send SIGPIPE on errors on stream oriented
170 sockets when the other end breaks the connection. The EPIPE
171 error is still returned. */
172 return send(ctx->s, (const char *)req, req_length, MSG_NOSIGNAL);
175 static int _modbus_tcp_receive(modbus_t *ctx, uint8_t *req) {
176 return _modbus_receive_msg(ctx, req, MSG_INDICATION);
179 static ssize_t _modbus_tcp_recv(modbus_t *ctx, uint8_t *rsp, int rsp_length) {
180 return recv(ctx->s, (char *)rsp, rsp_length, 0);
183 static int _modbus_tcp_check_integrity(modbus_t *ctx, uint8_t *msg, const int msg_length)
185 return msg_length;
188 static int _modbus_tcp_pre_check_confirmation(modbus_t *ctx, const uint8_t *req,
189 const uint8_t *rsp, int rsp_length)
191 /* Check transaction ID */
192 if (req[0] != rsp[0] || req[1] != rsp[1]) {
193 if (ctx->debug) {
194 fprintf(stderr, "Invalid transaction ID received 0x%X (not 0x%X)\n",
195 (rsp[0] << 8) + rsp[1], (req[0] << 8) + req[1]);
197 errno = EMBBADDATA;
198 return -1;
201 /* Check protocol ID */
202 if (rsp[2] != 0x0 && rsp[3] != 0x0) {
203 if (ctx->debug) {
204 fprintf(stderr, "Invalid protocol ID received 0x%X (not 0x0)\n",
205 (rsp[2] << 8) + rsp[3]);
207 errno = EMBBADDATA;
208 return -1;
211 return 0;
214 static int _modbus_tcp_set_ipv4_options(int s)
216 int rc;
217 int option;
219 /* Set the TCP no delay flag */
220 /* SOL_TCP = IPPROTO_TCP */
221 option = 1;
222 rc = setsockopt(s, IPPROTO_TCP, TCP_NODELAY,
223 (const void *)&option, sizeof(int));
224 if (rc == -1) {
225 return -1;
228 /* If the OS does not offer SOCK_NONBLOCK, fall back to setting FIONBIO to
229 * make sockets non-blocking */
230 /* Do not care about the return value, this is optional */
231 #if !defined(SOCK_NONBLOCK) && defined(FIONBIO)
232 #ifdef OS_WIN32
234 /* Setting FIONBIO expects an unsigned long according to MSDN */
235 u_long loption = 1;
236 ioctlsocket(s, FIONBIO, &loption);
238 #else
239 option = 1;
240 ioctl(s, FIONBIO, &option);
241 #endif
242 #endif
244 #ifndef OS_WIN32
246 * Cygwin defines IPTOS_LOWDELAY but can't handle that flag so it's
247 * necessary to workaround that problem.
249 /* Set the IP low delay option */
250 option = IPTOS_LOWDELAY;
251 rc = setsockopt(s, IPPROTO_IP, IP_TOS,
252 (const void *)&option, sizeof(int));
253 if (rc == -1) {
254 return -1;
256 #endif
258 return 0;
261 static int _connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen,
262 const struct timeval *ro_tv)
264 int rc = connect(sockfd, addr, addrlen);
266 #ifdef OS_WIN32
267 int wsaError = 0;
268 if (rc == -1) {
269 wsaError = WSAGetLastError();
272 if (wsaError == WSAEWOULDBLOCK || wsaError == WSAEINPROGRESS) {
273 #else
274 if (rc == -1 && errno == EINPROGRESS) {
275 #endif
276 fd_set wset;
277 int optval;
278 socklen_t optlen = sizeof(optval);
279 struct timeval tv = *ro_tv;
281 /* Wait to be available in writing */
282 FD_ZERO(&wset);
283 FD_SET(sockfd, &wset);
284 rc = select(sockfd + 1, NULL, &wset, NULL, &tv);
285 if (rc <= 0) {
286 /* Timeout or fail */
287 return -1;
290 /* The connection is established if SO_ERROR and optval are set to 0 */
291 rc = getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void *)&optval, &optlen);
292 if (rc == 0 && optval == 0) {
293 return 0;
294 } else {
295 errno = ECONNREFUSED;
296 return -1;
299 return rc;
302 /* Establishes a modbus TCP connection with a Modbus server. */
303 static int _modbus_tcp_connect(modbus_t *ctx)
305 int rc;
306 /* Specialized version of sockaddr for Internet socket address (same size) */
307 struct sockaddr_in addr;
308 modbus_tcp_t *ctx_tcp = ctx->backend_data;
309 int flags = SOCK_STREAM;
311 #ifdef OS_WIN32
312 if (_modbus_tcp_init_win32() == -1) {
313 return -1;
315 #endif
317 #ifdef SOCK_CLOEXEC
318 flags |= SOCK_CLOEXEC;
319 #endif
321 #ifdef SOCK_NONBLOCK
322 flags |= SOCK_NONBLOCK;
323 #endif
325 ctx->s = socket(PF_INET, flags, 0);
326 if (ctx->s == -1) {
327 return -1;
330 rc = _modbus_tcp_set_ipv4_options(ctx->s);
331 if (rc == -1) {
332 close(ctx->s);
333 ctx->s = -1;
334 return -1;
337 if (ctx->debug) {
338 printf("Connecting to %s:%d\n", ctx_tcp->ip, ctx_tcp->port);
341 addr.sin_family = AF_INET;
342 addr.sin_port = htons(ctx_tcp->port);
343 addr.sin_addr.s_addr = inet_addr(ctx_tcp->ip);
344 rc = _connect(ctx->s, (struct sockaddr *)&addr, sizeof(addr), &ctx->response_timeout);
345 if (rc == -1) {
346 close(ctx->s);
347 ctx->s = -1;
348 return -1;
351 return 0;
354 /* Establishes a modbus TCP PI connection with a Modbus server. */
355 static int _modbus_tcp_pi_connect(modbus_t *ctx)
357 int rc;
358 struct addrinfo *ai_list;
359 struct addrinfo *ai_ptr;
360 struct addrinfo ai_hints;
361 modbus_tcp_pi_t *ctx_tcp_pi = ctx->backend_data;
363 #ifdef OS_WIN32
364 if (_modbus_tcp_init_win32() == -1) {
365 return -1;
367 #endif
369 memset(&ai_hints, 0, sizeof(ai_hints));
370 #ifdef AI_ADDRCONFIG
371 ai_hints.ai_flags |= AI_ADDRCONFIG;
372 #endif
373 ai_hints.ai_family = AF_UNSPEC;
374 ai_hints.ai_socktype = SOCK_STREAM;
375 ai_hints.ai_addr = NULL;
376 ai_hints.ai_canonname = NULL;
377 ai_hints.ai_next = NULL;
379 ai_list = NULL;
380 rc = getaddrinfo(ctx_tcp_pi->node, ctx_tcp_pi->service,
381 &ai_hints, &ai_list);
382 if (rc != 0) {
383 if (ctx->debug) {
384 fprintf(stderr, "Error returned by getaddrinfo: %s\n", gai_strerror(rc));
386 errno = ECONNREFUSED;
387 return -1;
390 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
391 int flags = ai_ptr->ai_socktype;
392 int s;
394 #ifdef SOCK_CLOEXEC
395 flags |= SOCK_CLOEXEC;
396 #endif
398 #ifdef SOCK_NONBLOCK
399 flags |= SOCK_NONBLOCK;
400 #endif
402 s = socket(ai_ptr->ai_family, flags, ai_ptr->ai_protocol);
403 if (s < 0)
404 continue;
406 if (ai_ptr->ai_family == AF_INET)
407 _modbus_tcp_set_ipv4_options(s);
409 if (ctx->debug) {
410 printf("Connecting to [%s]:%s\n", ctx_tcp_pi->node, ctx_tcp_pi->service);
413 rc = _connect(s, ai_ptr->ai_addr, ai_ptr->ai_addrlen, &ctx->response_timeout);
414 if (rc == -1) {
415 close(s);
416 continue;
419 ctx->s = s;
420 break;
423 freeaddrinfo(ai_list);
425 if (ctx->s < 0) {
426 return -1;
429 return 0;
432 /* Closes the network connection and socket in TCP mode */
433 static void _modbus_tcp_close(modbus_t *ctx)
435 if (ctx->s != -1) {
436 shutdown(ctx->s, SHUT_RDWR);
437 close(ctx->s);
438 ctx->s = -1;
442 static int _modbus_tcp_flush(modbus_t *ctx)
444 int rc;
445 int rc_sum = 0;
447 do {
448 /* Extract the garbage from the socket */
449 char devnull[MODBUS_TCP_MAX_ADU_LENGTH];
450 #ifndef OS_WIN32
451 rc = recv(ctx->s, devnull, MODBUS_TCP_MAX_ADU_LENGTH, MSG_DONTWAIT);
452 #else
453 /* On Win32, it's a bit more complicated to not wait */
454 fd_set rset;
455 struct timeval tv;
457 tv.tv_sec = 0;
458 tv.tv_usec = 0;
459 FD_ZERO(&rset);
460 FD_SET(ctx->s, &rset);
461 rc = select(ctx->s+1, &rset, NULL, NULL, &tv);
462 if (rc == -1) {
463 return -1;
466 if (rc == 1) {
467 /* There is data to flush */
468 rc = recv(ctx->s, devnull, MODBUS_TCP_MAX_ADU_LENGTH, 0);
470 #endif
471 if (rc > 0) {
472 rc_sum += rc;
474 } while (rc == MODBUS_TCP_MAX_ADU_LENGTH);
476 return rc_sum;
479 /* Listens for any request from one or many modbus masters in TCP */
480 int modbus_tcp_listen(modbus_t *ctx, int nb_connection)
482 int new_s;
483 int enable;
484 int flags;
485 struct sockaddr_in addr;
486 modbus_tcp_t *ctx_tcp;
488 if (ctx == NULL) {
489 errno = EINVAL;
490 return -1;
493 ctx_tcp = ctx->backend_data;
495 #ifdef OS_WIN32
496 if (_modbus_tcp_init_win32() == -1) {
497 return -1;
499 #endif
501 flags = SOCK_STREAM;
503 #ifdef SOCK_CLOEXEC
504 flags |= SOCK_CLOEXEC;
505 #endif
507 new_s = socket(PF_INET, flags, IPPROTO_TCP);
508 if (new_s == -1) {
509 return -1;
512 enable = 1;
513 if (setsockopt(new_s, SOL_SOCKET, SO_REUSEADDR,
514 (char *)&enable, sizeof(enable)) == -1) {
515 close(new_s);
516 return -1;
519 memset(&addr, 0, sizeof(addr));
520 addr.sin_family = AF_INET;
521 /* If the modbus port is < to 1024, we need the setuid root. */
522 addr.sin_port = htons(ctx_tcp->port);
523 if (ctx_tcp->ip[0] == '0') {
524 /* Listen any addresses */
525 addr.sin_addr.s_addr = htonl(INADDR_ANY);
526 } else {
527 /* Listen only specified IP address */
528 addr.sin_addr.s_addr = inet_addr(ctx_tcp->ip);
530 if (bind(new_s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
531 close(new_s);
532 return -1;
535 if (listen(new_s, nb_connection) == -1) {
536 close(new_s);
537 return -1;
540 return new_s;
543 int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection)
545 int rc;
546 struct addrinfo *ai_list;
547 struct addrinfo *ai_ptr;
548 struct addrinfo ai_hints;
549 const char *node;
550 const char *service;
551 int new_s;
552 modbus_tcp_pi_t *ctx_tcp_pi;
554 if (ctx == NULL) {
555 errno = EINVAL;
556 return -1;
559 ctx_tcp_pi = ctx->backend_data;
561 #ifdef OS_WIN32
562 if (_modbus_tcp_init_win32() == -1) {
563 return -1;
565 #endif
567 if (ctx_tcp_pi->node[0] == 0) {
568 node = NULL; /* == any */
569 } else {
570 node = ctx_tcp_pi->node;
573 if (ctx_tcp_pi->service[0] == 0) {
574 service = "502";
575 } else {
576 service = ctx_tcp_pi->service;
579 memset(&ai_hints, 0, sizeof (ai_hints));
580 /* If node is not NULL, than the AI_PASSIVE flag is ignored. */
581 ai_hints.ai_flags |= AI_PASSIVE;
582 #ifdef AI_ADDRCONFIG
583 ai_hints.ai_flags |= AI_ADDRCONFIG;
584 #endif
585 ai_hints.ai_family = AF_UNSPEC;
586 ai_hints.ai_socktype = SOCK_STREAM;
587 ai_hints.ai_addr = NULL;
588 ai_hints.ai_canonname = NULL;
589 ai_hints.ai_next = NULL;
591 ai_list = NULL;
592 rc = getaddrinfo(node, service, &ai_hints, &ai_list);
593 if (rc != 0) {
594 if (ctx->debug) {
595 fprintf(stderr, "Error returned by getaddrinfo: %s\n", gai_strerror(rc));
597 errno = ECONNREFUSED;
598 return -1;
601 new_s = -1;
602 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
603 int flags = ai_ptr->ai_socktype;
604 int s;
606 #ifdef SOCK_CLOEXEC
607 flags |= SOCK_CLOEXEC;
608 #endif
610 s = socket(ai_ptr->ai_family, flags, ai_ptr->ai_protocol);
611 if (s < 0) {
612 if (ctx->debug) {
613 perror("socket");
615 continue;
616 } else {
617 int enable = 1;
618 rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
619 (void *)&enable, sizeof (enable));
620 if (rc != 0) {
621 close(s);
622 if (ctx->debug) {
623 perror("setsockopt");
625 continue;
629 rc = bind(s, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
630 if (rc != 0) {
631 close(s);
632 if (ctx->debug) {
633 perror("bind");
635 continue;
638 rc = listen(s, nb_connection);
639 if (rc != 0) {
640 close(s);
641 if (ctx->debug) {
642 perror("listen");
644 continue;
647 new_s = s;
648 break;
650 freeaddrinfo(ai_list);
652 if (new_s < 0) {
653 return -1;
656 return new_s;
659 int modbus_tcp_accept(modbus_t *ctx, int *s)
661 struct sockaddr_in addr;
662 socklen_t addrlen;
664 if (ctx == NULL) {
665 errno = EINVAL;
666 return -1;
669 addrlen = sizeof(addr);
670 #ifdef HAVE_ACCEPT4
671 /* Inherit socket flags and use accept4 call */
672 ctx->s = accept4(*s, (struct sockaddr *)&addr, &addrlen, SOCK_CLOEXEC);
673 #else
674 ctx->s = accept(*s, (struct sockaddr *)&addr, &addrlen);
675 #endif
677 if (ctx->s == -1) {
678 return -1;
681 if (ctx->debug) {
682 printf("The client connection from %s is accepted\n",
683 inet_ntoa(addr.sin_addr));
686 return ctx->s;
689 int modbus_tcp_pi_accept(modbus_t *ctx, int *s)
691 struct sockaddr_storage addr;
692 socklen_t addrlen;
694 if (ctx == NULL) {
695 errno = EINVAL;
696 return -1;
699 addrlen = sizeof(addr);
700 #ifdef HAVE_ACCEPT4
701 /* Inherit socket flags and use accept4 call */
702 ctx->s = accept4(*s, (struct sockaddr *)&addr, &addrlen, SOCK_CLOEXEC);
703 #else
704 ctx->s = accept(*s, (struct sockaddr *)&addr, &addrlen);
705 #endif
707 if (ctx->s == -1) {
708 return -1;
711 if (ctx->debug) {
712 printf("The client connection is accepted.\n");
715 return ctx->s;
718 static int _modbus_tcp_select(modbus_t *ctx, fd_set *rset, struct timeval *tv, int length_to_read)
720 int s_rc;
721 while ((s_rc = select(ctx->s+1, rset, NULL, NULL, tv)) == -1) {
722 if (errno == EINTR) {
723 if (ctx->debug) {
724 fprintf(stderr, "A non blocked signal was caught\n");
726 /* Necessary after an error */
727 FD_ZERO(rset);
728 FD_SET(ctx->s, rset);
729 } else {
730 return -1;
734 if (s_rc == 0) {
735 errno = ETIMEDOUT;
736 return -1;
739 return s_rc;
742 static void _modbus_tcp_free(modbus_t *ctx) {
743 free(ctx->backend_data);
744 free(ctx);
747 const modbus_backend_t _modbus_tcp_backend = {
748 _MODBUS_BACKEND_TYPE_TCP,
749 _MODBUS_TCP_HEADER_LENGTH,
750 _MODBUS_TCP_CHECKSUM_LENGTH,
751 MODBUS_TCP_MAX_ADU_LENGTH,
752 _modbus_set_slave,
753 _modbus_tcp_build_request_basis,
754 _modbus_tcp_build_response_basis,
755 _modbus_tcp_prepare_response_tid,
756 _modbus_tcp_send_msg_pre,
757 _modbus_tcp_send,
758 _modbus_tcp_receive,
759 _modbus_tcp_recv,
760 _modbus_tcp_check_integrity,
761 _modbus_tcp_pre_check_confirmation,
762 _modbus_tcp_connect,
763 _modbus_tcp_close,
764 _modbus_tcp_flush,
765 _modbus_tcp_select,
766 _modbus_tcp_free
770 const modbus_backend_t _modbus_tcp_pi_backend = {
771 _MODBUS_BACKEND_TYPE_TCP,
772 _MODBUS_TCP_HEADER_LENGTH,
773 _MODBUS_TCP_CHECKSUM_LENGTH,
774 MODBUS_TCP_MAX_ADU_LENGTH,
775 _modbus_set_slave,
776 _modbus_tcp_build_request_basis,
777 _modbus_tcp_build_response_basis,
778 _modbus_tcp_prepare_response_tid,
779 _modbus_tcp_send_msg_pre,
780 _modbus_tcp_send,
781 _modbus_tcp_receive,
782 _modbus_tcp_recv,
783 _modbus_tcp_check_integrity,
784 _modbus_tcp_pre_check_confirmation,
785 _modbus_tcp_pi_connect,
786 _modbus_tcp_close,
787 _modbus_tcp_flush,
788 _modbus_tcp_select,
789 _modbus_tcp_free
792 modbus_t* modbus_new_tcp(const char *ip, int port)
794 modbus_t *ctx;
795 modbus_tcp_t *ctx_tcp;
796 size_t dest_size;
797 size_t ret_size;
799 #if defined(OS_BSD)
800 /* MSG_NOSIGNAL is unsupported on *BSD so we install an ignore
801 handler for SIGPIPE. */
802 struct sigaction sa;
804 sa.sa_handler = SIG_IGN;
805 if (sigaction(SIGPIPE, &sa, NULL) < 0) {
806 /* The debug flag can't be set here... */
807 fprintf(stderr, "Could not install SIGPIPE handler.\n");
808 return NULL;
810 #endif
812 ctx = (modbus_t *)malloc(sizeof(modbus_t));
813 if (ctx == NULL) {
814 return NULL;
816 _modbus_init_common(ctx);
818 /* Could be changed after to reach a remote serial Modbus device */
819 ctx->slave = MODBUS_TCP_SLAVE;
821 ctx->backend = &_modbus_tcp_backend;
823 ctx->backend_data = (modbus_tcp_t *)malloc(sizeof(modbus_tcp_t));
824 if (ctx->backend_data == NULL) {
825 modbus_free(ctx);
826 errno = ENOMEM;
827 return NULL;
829 ctx_tcp = (modbus_tcp_t *)ctx->backend_data;
831 if (ip != NULL) {
832 dest_size = sizeof(char) * 16;
833 ret_size = strlcpy(ctx_tcp->ip, ip, dest_size);
834 if (ret_size == 0) {
835 fprintf(stderr, "The IP string is empty\n");
836 modbus_free(ctx);
837 errno = EINVAL;
838 return NULL;
841 if (ret_size >= dest_size) {
842 fprintf(stderr, "The IP string has been truncated\n");
843 modbus_free(ctx);
844 errno = EINVAL;
845 return NULL;
847 } else {
848 ctx_tcp->ip[0] = '0';
850 ctx_tcp->port = port;
851 ctx_tcp->t_id = 0;
853 return ctx;
857 modbus_t* modbus_new_tcp_pi(const char *node, const char *service)
859 modbus_t *ctx;
860 modbus_tcp_pi_t *ctx_tcp_pi;
861 size_t dest_size;
862 size_t ret_size;
864 ctx = (modbus_t *)malloc(sizeof(modbus_t));
865 if (ctx == NULL) {
866 return NULL;
868 _modbus_init_common(ctx);
870 /* Could be changed after to reach a remote serial Modbus device */
871 ctx->slave = MODBUS_TCP_SLAVE;
873 ctx->backend = &_modbus_tcp_pi_backend;
875 ctx->backend_data = (modbus_tcp_pi_t *)malloc(sizeof(modbus_tcp_pi_t));
876 if (ctx->backend_data == NULL) {
877 modbus_free(ctx);
878 errno = ENOMEM;
879 return NULL;
881 ctx_tcp_pi = (modbus_tcp_pi_t *)ctx->backend_data;
883 if (node == NULL) {
884 /* The node argument can be empty to indicate any hosts */
885 ctx_tcp_pi->node[0] = 0;
886 } else {
887 dest_size = sizeof(char) * _MODBUS_TCP_PI_NODE_LENGTH;
888 ret_size = strlcpy(ctx_tcp_pi->node, node, dest_size);
889 if (ret_size == 0) {
890 fprintf(stderr, "The node string is empty\n");
891 modbus_free(ctx);
892 errno = EINVAL;
893 return NULL;
896 if (ret_size >= dest_size) {
897 fprintf(stderr, "The node string has been truncated\n");
898 modbus_free(ctx);
899 errno = EINVAL;
900 return NULL;
904 if (service != NULL) {
905 dest_size = sizeof(char) * _MODBUS_TCP_PI_SERVICE_LENGTH;
906 ret_size = strlcpy(ctx_tcp_pi->service, service, dest_size);
907 } else {
908 /* Empty service is not allowed, error caught below. */
909 ret_size = 0;
912 if (ret_size == 0) {
913 fprintf(stderr, "The service string is empty\n");
914 modbus_free(ctx);
915 errno = EINVAL;
916 return NULL;
919 if (ret_size >= dest_size) {
920 fprintf(stderr, "The service string has been truncated\n");
921 modbus_free(ctx);
922 errno = EINVAL;
923 return NULL;
926 ctx_tcp_pi->t_id = 0;
928 return ctx;