modbus-tcp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /*
  2. * Copyright © 2001-2011 Stéphane Raimbault <stephane.raimbault@gmail.com>
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /* For accept4 when available */
  19. #define _GNU_SOURCE
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #ifndef _MSC_VER
  25. #include <unistd.h>
  26. #endif
  27. #include <signal.h>
  28. #include <sys/types.h>
  29. #if defined(_WIN32)
  30. # define OS_WIN32
  31. /* ws2_32.dll has getaddrinfo and freeaddrinfo on Windows XP and later.
  32. * minwg32 headers check WINVER before allowing the use of these */
  33. # ifndef WINVER
  34. # define WINVER 0x0501
  35. # endif
  36. # include <ws2tcpip.h>
  37. # define SHUT_RDWR 2
  38. # define close closesocket
  39. #else
  40. # include <sys/socket.h>
  41. # include <sys/ioctl.h>
  42. #if defined(__OpenBSD__) || (defined(__FreeBSD__) && __FreeBSD__ < 5)
  43. # define OS_BSD
  44. # include <netinet/in_systm.h>
  45. #endif
  46. # include <netinet/in.h>
  47. # include <netinet/ip.h>
  48. # include <netinet/tcp.h>
  49. # include <arpa/inet.h>
  50. # include <netdb.h>
  51. #endif
  52. #if !defined(MSG_NOSIGNAL)
  53. #define MSG_NOSIGNAL 0
  54. #endif
  55. #include "modbus-private.h"
  56. #include "modbus-tcp.h"
  57. #include "modbus-tcp-private.h"
  58. #ifdef OS_WIN32
  59. static int _modbus_tcp_init_win32(void)
  60. {
  61. /* Initialise Windows Socket API */
  62. WSADATA wsaData;
  63. if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
  64. fprintf(stderr, "WSAStartup() returned error code %d\n",
  65. (unsigned int)GetLastError());
  66. errno = EIO;
  67. return -1;
  68. }
  69. return 0;
  70. }
  71. #endif
  72. static int _modbus_set_slave(modbus_t *ctx, int slave)
  73. {
  74. /* Broadcast address is 0 (MODBUS_BROADCAST_ADDRESS) */
  75. if (slave >= 0 && slave <= 247) {
  76. ctx->slave = slave;
  77. } else if (slave == MODBUS_TCP_SLAVE) {
  78. /* The special value MODBUS_TCP_SLAVE (0xFF) can be used in TCP mode to
  79. * restore the default value. */
  80. ctx->slave = slave;
  81. } else {
  82. errno = EINVAL;
  83. return -1;
  84. }
  85. return 0;
  86. }
  87. /* Builds a TCP request header */
  88. int _modbus_tcp_build_request_basis(modbus_t *ctx, int function,
  89. int addr, int nb,
  90. uint8_t *req)
  91. {
  92. modbus_tcp_t *ctx_tcp = ctx->backend_data;
  93. /* Increase transaction ID */
  94. if (ctx_tcp->t_id < UINT16_MAX)
  95. ctx_tcp->t_id++;
  96. else
  97. ctx_tcp->t_id = 0;
  98. req[0] = ctx_tcp->t_id >> 8;
  99. req[1] = ctx_tcp->t_id & 0x00ff;
  100. /* Protocol Modbus */
  101. req[2] = 0;
  102. req[3] = 0;
  103. /* Length will be defined later by set_req_length_tcp at offsets 4
  104. and 5 */
  105. req[6] = ctx->slave;
  106. req[7] = function;
  107. req[8] = addr >> 8;
  108. req[9] = addr & 0x00ff;
  109. req[10] = nb >> 8;
  110. req[11] = nb & 0x00ff;
  111. return _MODBUS_TCP_PRESET_REQ_LENGTH;
  112. }
  113. /* Builds a TCP response header */
  114. int _modbus_tcp_build_response_basis(sft_t *sft, uint8_t *rsp)
  115. {
  116. /* Extract from MODBUS Messaging on TCP/IP Implementation
  117. Guide V1.0b (page 23/46):
  118. The transaction identifier is used to associate the future
  119. response with the request. */
  120. rsp[0] = sft->t_id >> 8;
  121. rsp[1] = sft->t_id & 0x00ff;
  122. /* Protocol Modbus */
  123. rsp[2] = 0;
  124. rsp[3] = 0;
  125. /* Length will be set later by send_msg (4 and 5) */
  126. /* The slave ID is copied from the indication */
  127. rsp[6] = sft->slave;
  128. rsp[7] = sft->function;
  129. return _MODBUS_TCP_PRESET_RSP_LENGTH;
  130. }
  131. int _modbus_tcp_prepare_response_tid(const uint8_t *req, int *req_length)
  132. {
  133. return (req[0] << 8) + req[1];
  134. }
  135. int _modbus_tcp_send_msg_pre(uint8_t *req, int req_length)
  136. {
  137. /* Substract the header length to the message length */
  138. int mbap_length = req_length - 6;
  139. req[4] = mbap_length >> 8;
  140. req[5] = mbap_length & 0x00FF;
  141. return req_length;
  142. }
  143. ssize_t _modbus_tcp_send(modbus_t *ctx, const uint8_t *req, int req_length)
  144. {
  145. /* MSG_NOSIGNAL
  146. Requests not to send SIGPIPE on errors on stream oriented
  147. sockets when the other end breaks the connection. The EPIPE
  148. error is still returned. */
  149. return send(ctx->s, (const char*)req, req_length, MSG_NOSIGNAL);
  150. }
  151. int _modbus_tcp_receive(modbus_t *ctx, uint8_t *req) {
  152. return _modbus_receive_msg(ctx, req, MSG_INDICATION);
  153. }
  154. ssize_t _modbus_tcp_recv(modbus_t *ctx, uint8_t *rsp, int rsp_length) {
  155. return recv(ctx->s, (char *)rsp, rsp_length, 0);
  156. }
  157. int _modbus_tcp_check_integrity(modbus_t *ctx, uint8_t *msg, const int msg_length)
  158. {
  159. return msg_length;
  160. }
  161. int _modbus_tcp_pre_check_confirmation(modbus_t *ctx, const uint8_t *req,
  162. const uint8_t *rsp, int rsp_length)
  163. {
  164. /* Check TID */
  165. if (req[0] != rsp[0] || req[1] != rsp[1]) {
  166. if (ctx->debug) {
  167. fprintf(stderr, "Invalid TID received 0x%X (not 0x%X)\n",
  168. (rsp[0] << 8) + rsp[1], (req[0] << 8) + req[1]);
  169. }
  170. errno = EMBBADDATA;
  171. return -1;
  172. } else {
  173. return 0;
  174. }
  175. }
  176. static int _modbus_tcp_set_ipv4_options(int s)
  177. {
  178. int rc;
  179. int option;
  180. /* Set the TCP no delay flag */
  181. /* SOL_TCP = IPPROTO_TCP */
  182. option = 1;
  183. rc = setsockopt(s, IPPROTO_TCP, TCP_NODELAY,
  184. (const void *)&option, sizeof(int));
  185. if (rc == -1) {
  186. return -1;
  187. }
  188. #ifndef OS_WIN32
  189. /**
  190. * Cygwin defines IPTOS_LOWDELAY but can't handle that flag so it's
  191. * necessary to workaround that problem.
  192. **/
  193. /* Set the IP low delay option */
  194. option = IPTOS_LOWDELAY;
  195. rc = setsockopt(s, IPPROTO_IP, IP_TOS,
  196. (const void *)&option, sizeof(int));
  197. if (rc == -1) {
  198. return -1;
  199. }
  200. #endif
  201. return 0;
  202. }
  203. static int _connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen,
  204. struct timeval *tv)
  205. {
  206. int rc;
  207. rc = connect(sockfd, addr, addrlen);
  208. if (rc == -1 && errno == EINPROGRESS) {
  209. fd_set wset;
  210. int optval;
  211. socklen_t optlen = sizeof(optval);
  212. /* Wait to be available in writing */
  213. FD_ZERO(&wset);
  214. FD_SET(sockfd, &wset);
  215. rc = select(sockfd + 1, NULL, &wset, NULL, tv);
  216. if (rc <= 0) {
  217. /* Timeout or fail */
  218. return -1;
  219. }
  220. /* The connection is established if SO_ERROR and optval are set to 0 */
  221. rc = getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void *)&optval, &optlen);
  222. if (rc == 0 && optval == 0) {
  223. return 0;
  224. } else {
  225. errno = ECONNREFUSED;
  226. return -1;
  227. }
  228. }
  229. return rc;
  230. }
  231. /* Establishes a modbus TCP connection with a Modbus server. */
  232. static int _modbus_tcp_connect(modbus_t *ctx)
  233. {
  234. int rc;
  235. /* Specialized version of sockaddr for Internet socket address (same size) */
  236. struct sockaddr_in addr;
  237. modbus_tcp_t *ctx_tcp = ctx->backend_data;
  238. int flags = SOCK_STREAM;
  239. #ifdef OS_WIN32
  240. if (_modbus_tcp_init_win32() == -1) {
  241. return -1;
  242. }
  243. #endif
  244. #ifdef SOCK_CLOEXEC
  245. flags |= SOCK_CLOEXEC;
  246. #endif
  247. #ifdef SOCK_NONBLOCK
  248. flags |= SOCK_NONBLOCK;
  249. #endif
  250. ctx->s = socket(PF_INET, flags, 0);
  251. if (ctx->s == -1) {
  252. return -1;
  253. }
  254. rc = _modbus_tcp_set_ipv4_options(ctx->s);
  255. if (rc == -1) {
  256. close(ctx->s);
  257. return -1;
  258. }
  259. if (ctx->debug) {
  260. printf("Connecting to %s:%d\n", ctx_tcp->ip, ctx_tcp->port);
  261. }
  262. addr.sin_family = AF_INET;
  263. addr.sin_port = htons(ctx_tcp->port);
  264. addr.sin_addr.s_addr = inet_addr(ctx_tcp->ip);
  265. rc = _connect(ctx->s, (struct sockaddr *)&addr, sizeof(addr), &ctx->response_timeout);
  266. if (rc == -1) {
  267. close(ctx->s);
  268. return -1;
  269. }
  270. return 0;
  271. }
  272. /* Establishes a modbus TCP PI connection with a Modbus server. */
  273. static int _modbus_tcp_pi_connect(modbus_t *ctx)
  274. {
  275. int rc;
  276. struct addrinfo *ai_list;
  277. struct addrinfo *ai_ptr;
  278. struct addrinfo ai_hints;
  279. modbus_tcp_pi_t *ctx_tcp_pi = ctx->backend_data;
  280. #ifdef OS_WIN32
  281. if (_modbus_tcp_init_win32() == -1) {
  282. return -1;
  283. }
  284. #endif
  285. memset(&ai_hints, 0, sizeof(ai_hints));
  286. #ifdef AI_ADDRCONFIG
  287. ai_hints.ai_flags |= AI_ADDRCONFIG;
  288. #endif
  289. ai_hints.ai_family = AF_UNSPEC;
  290. ai_hints.ai_socktype = SOCK_STREAM;
  291. ai_hints.ai_addr = NULL;
  292. ai_hints.ai_canonname = NULL;
  293. ai_hints.ai_next = NULL;
  294. ai_list = NULL;
  295. rc = getaddrinfo(ctx_tcp_pi->node, ctx_tcp_pi->service,
  296. &ai_hints, &ai_list);
  297. if (rc != 0) {
  298. if (ctx->debug) {
  299. fprintf(stderr, "Error returned by getaddrinfo: %s\n", gai_strerror(rc));
  300. }
  301. errno = ECONNREFUSED;
  302. return -1;
  303. }
  304. for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
  305. int flags = ai_ptr->ai_socktype;
  306. int s;
  307. #ifdef SOCK_CLOEXEC
  308. flags |= SOCK_CLOEXEC;
  309. #endif
  310. #ifdef SOCK_NONBLOCK
  311. flags |= SOCK_NONBLOCK;
  312. #endif
  313. s = socket(ai_ptr->ai_family, flags, ai_ptr->ai_protocol);
  314. if (s < 0)
  315. continue;
  316. if (ai_ptr->ai_family == AF_INET)
  317. _modbus_tcp_set_ipv4_options(s);
  318. if (ctx->debug) {
  319. printf("Connecting to [%s]:%s\n", ctx_tcp_pi->node, ctx_tcp_pi->service);
  320. }
  321. rc = _connect(s, ai_ptr->ai_addr, ai_ptr->ai_addrlen, &ctx->response_timeout);
  322. if (rc == -1) {
  323. close(s);
  324. continue;
  325. }
  326. ctx->s = s;
  327. break;
  328. }
  329. freeaddrinfo(ai_list);
  330. if (ctx->s < 0) {
  331. return -1;
  332. }
  333. return 0;
  334. }
  335. /* Closes the network connection and socket in TCP mode */
  336. void _modbus_tcp_close(modbus_t *ctx)
  337. {
  338. shutdown(ctx->s, SHUT_RDWR);
  339. close(ctx->s);
  340. }
  341. int _modbus_tcp_flush(modbus_t *ctx)
  342. {
  343. int rc;
  344. int rc_sum = 0;
  345. do {
  346. /* Extract the garbage from the socket */
  347. char devnull[MODBUS_TCP_MAX_ADU_LENGTH];
  348. #ifndef OS_WIN32
  349. rc = recv(ctx->s, devnull, MODBUS_TCP_MAX_ADU_LENGTH, MSG_DONTWAIT);
  350. #else
  351. /* On Win32, it's a bit more complicated to not wait */
  352. fd_set rset;
  353. struct timeval tv;
  354. tv.tv_sec = 0;
  355. tv.tv_usec = 0;
  356. FD_ZERO(&rset);
  357. FD_SET(ctx->s, &rset);
  358. rc = select(ctx->s+1, &rset, NULL, NULL, &tv);
  359. if (rc == -1) {
  360. return -1;
  361. }
  362. if (rc == 1) {
  363. /* There is data to flush */
  364. rc = recv(ctx->s, devnull, MODBUS_TCP_MAX_ADU_LENGTH, 0);
  365. }
  366. #endif
  367. if (rc > 0) {
  368. rc_sum += rc;
  369. }
  370. } while (rc == MODBUS_TCP_MAX_ADU_LENGTH);
  371. return rc_sum;
  372. }
  373. /* Listens for any request from one or many modbus masters in TCP */
  374. int modbus_tcp_listen(modbus_t *ctx, int nb_connection)
  375. {
  376. int new_socket;
  377. int yes;
  378. struct sockaddr_in addr;
  379. modbus_tcp_t *ctx_tcp = ctx->backend_data;
  380. #ifdef OS_WIN32
  381. if (_modbus_tcp_init_win32() == -1) {
  382. return -1;
  383. }
  384. #endif
  385. new_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  386. if (new_socket == -1) {
  387. return -1;
  388. }
  389. yes = 1;
  390. if (setsockopt(new_socket, SOL_SOCKET, SO_REUSEADDR,
  391. (char *) &yes, sizeof(yes)) == -1) {
  392. close(new_socket);
  393. return -1;
  394. }
  395. memset(&addr, 0, sizeof(addr));
  396. addr.sin_family = AF_INET;
  397. /* If the modbus port is < to 1024, we need the setuid root. */
  398. addr.sin_port = htons(ctx_tcp->port);
  399. addr.sin_addr.s_addr = INADDR_ANY;
  400. if (bind(new_socket, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
  401. close(new_socket);
  402. return -1;
  403. }
  404. if (listen(new_socket, nb_connection) == -1) {
  405. close(new_socket);
  406. return -1;
  407. }
  408. return new_socket;
  409. }
  410. int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection)
  411. {
  412. int rc;
  413. struct addrinfo *ai_list;
  414. struct addrinfo *ai_ptr;
  415. struct addrinfo ai_hints;
  416. const char *node;
  417. const char *service;
  418. int new_socket;
  419. modbus_tcp_pi_t *ctx_tcp_pi = ctx->backend_data;
  420. if (ctx_tcp_pi->node[0] == 0)
  421. node = NULL; /* == any */
  422. else
  423. node = ctx_tcp_pi->node;
  424. if (ctx_tcp_pi->service[0] == 0)
  425. service = "502";
  426. else
  427. service = ctx_tcp_pi->service;
  428. memset(&ai_hints, 0, sizeof (ai_hints));
  429. ai_hints.ai_flags |= AI_PASSIVE;
  430. #ifdef AI_ADDRCONFIG
  431. ai_hints.ai_flags |= AI_ADDRCONFIG;
  432. #endif
  433. ai_hints.ai_family = AF_UNSPEC;
  434. ai_hints.ai_socktype = SOCK_STREAM;
  435. ai_hints.ai_addr = NULL;
  436. ai_hints.ai_canonname = NULL;
  437. ai_hints.ai_next = NULL;
  438. ai_list = NULL;
  439. rc = getaddrinfo(node, service, &ai_hints, &ai_list);
  440. if (rc != 0) {
  441. if (ctx->debug) {
  442. fprintf(stderr, "Error returned by getaddrinfo: %s\n", gai_strerror(rc));
  443. }
  444. errno = ECONNREFUSED;
  445. return -1;
  446. }
  447. new_socket = -1;
  448. for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
  449. int s;
  450. s = socket(ai_ptr->ai_family, ai_ptr->ai_socktype,
  451. ai_ptr->ai_protocol);
  452. if (s < 0) {
  453. if (ctx->debug) {
  454. perror("socket");
  455. }
  456. continue;
  457. } else {
  458. int yes = 1;
  459. rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
  460. (void *) &yes, sizeof (yes));
  461. if (rc != 0) {
  462. close(s);
  463. if (ctx->debug) {
  464. perror("setsockopt");
  465. }
  466. continue;
  467. }
  468. }
  469. rc = bind(s, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
  470. if (rc != 0) {
  471. close(s);
  472. if (ctx->debug) {
  473. perror("bind");
  474. }
  475. continue;
  476. }
  477. rc = listen(s, nb_connection);
  478. if (rc != 0) {
  479. close(s);
  480. if (ctx->debug) {
  481. perror("listen");
  482. }
  483. continue;
  484. }
  485. new_socket = s;
  486. break;
  487. }
  488. freeaddrinfo(ai_list);
  489. if (new_socket < 0) {
  490. return -1;
  491. }
  492. return new_socket;
  493. }
  494. /* On success, the function return a non-negative integer that is a descriptor
  495. for the accepted socket. On error, -1 is returned, and errno is set
  496. appropriately. */
  497. int modbus_tcp_accept(modbus_t *ctx, int *socket)
  498. {
  499. struct sockaddr_in addr;
  500. socklen_t addrlen;
  501. addrlen = sizeof(addr);
  502. #ifdef HAVE_ACCEPT4
  503. /* Inherit socket flags and use accept4 call */
  504. ctx->s = accept4(*socket, (struct sockaddr *)&addr, &addrlen, SOCK_CLOEXEC);
  505. #else
  506. ctx->s = accept(*socket, (struct sockaddr *)&addr, &addrlen);
  507. #endif
  508. if (ctx->s == -1) {
  509. close(*socket);
  510. *socket = 0;
  511. return -1;
  512. }
  513. if (ctx->debug) {
  514. printf("The client connection from %s is accepted\n",
  515. inet_ntoa(addr.sin_addr));
  516. }
  517. return ctx->s;
  518. }
  519. int modbus_tcp_pi_accept(modbus_t *ctx, int *socket)
  520. {
  521. struct sockaddr_storage addr;
  522. socklen_t addrlen;
  523. addrlen = sizeof(addr);
  524. ctx->s = accept(*socket, (void *)&addr, &addrlen);
  525. if (ctx->s == -1) {
  526. close(*socket);
  527. *socket = 0;
  528. }
  529. if (ctx->debug) {
  530. printf("The client connection is accepted.\n");
  531. }
  532. return ctx->s;
  533. }
  534. int _modbus_tcp_select(modbus_t *ctx, fd_set *rset, struct timeval *tv, int length_to_read)
  535. {
  536. int s_rc;
  537. while ((s_rc = select(ctx->s+1, rset, NULL, NULL, tv)) == -1) {
  538. if (errno == EINTR) {
  539. if (ctx->debug) {
  540. fprintf(stderr, "A non blocked signal was caught\n");
  541. }
  542. /* Necessary after an error */
  543. FD_ZERO(rset);
  544. FD_SET(ctx->s, rset);
  545. } else {
  546. return -1;
  547. }
  548. }
  549. if (s_rc == 0) {
  550. errno = ETIMEDOUT;
  551. return -1;
  552. }
  553. return s_rc;
  554. }
  555. void _modbus_tcp_free(modbus_t *ctx) {
  556. free(ctx->backend_data);
  557. free(ctx);
  558. }
  559. const modbus_backend_t _modbus_tcp_backend = {
  560. _MODBUS_BACKEND_TYPE_TCP,
  561. _MODBUS_TCP_HEADER_LENGTH,
  562. _MODBUS_TCP_CHECKSUM_LENGTH,
  563. MODBUS_TCP_MAX_ADU_LENGTH,
  564. _modbus_set_slave,
  565. _modbus_tcp_build_request_basis,
  566. _modbus_tcp_build_response_basis,
  567. _modbus_tcp_prepare_response_tid,
  568. _modbus_tcp_send_msg_pre,
  569. _modbus_tcp_send,
  570. _modbus_tcp_receive,
  571. _modbus_tcp_recv,
  572. _modbus_tcp_check_integrity,
  573. _modbus_tcp_pre_check_confirmation,
  574. _modbus_tcp_connect,
  575. _modbus_tcp_close,
  576. _modbus_tcp_flush,
  577. _modbus_tcp_select,
  578. _modbus_tcp_free
  579. };
  580. const modbus_backend_t _modbus_tcp_pi_backend = {
  581. _MODBUS_BACKEND_TYPE_TCP,
  582. _MODBUS_TCP_HEADER_LENGTH,
  583. _MODBUS_TCP_CHECKSUM_LENGTH,
  584. MODBUS_TCP_MAX_ADU_LENGTH,
  585. _modbus_set_slave,
  586. _modbus_tcp_build_request_basis,
  587. _modbus_tcp_build_response_basis,
  588. _modbus_tcp_prepare_response_tid,
  589. _modbus_tcp_send_msg_pre,
  590. _modbus_tcp_send,
  591. _modbus_tcp_receive,
  592. _modbus_tcp_recv,
  593. _modbus_tcp_check_integrity,
  594. _modbus_tcp_pre_check_confirmation,
  595. _modbus_tcp_pi_connect,
  596. _modbus_tcp_close,
  597. _modbus_tcp_flush,
  598. _modbus_tcp_select,
  599. _modbus_tcp_free
  600. };
  601. modbus_t* modbus_new_tcp(const char *ip, int port)
  602. {
  603. modbus_t *ctx;
  604. modbus_tcp_t *ctx_tcp;
  605. size_t dest_size;
  606. size_t ret_size;
  607. #if defined(OS_BSD)
  608. /* MSG_NOSIGNAL is unsupported on *BSD so we install an ignore
  609. handler for SIGPIPE. */
  610. struct sigaction sa;
  611. sa.sa_handler = SIG_IGN;
  612. if (sigaction(SIGPIPE, &sa, NULL) < 0) {
  613. /* The debug flag can't be set here... */
  614. fprintf(stderr, "Coud not install SIGPIPE handler.\n");
  615. return NULL;
  616. }
  617. #endif
  618. ctx = (modbus_t *) malloc(sizeof(modbus_t));
  619. _modbus_init_common(ctx);
  620. /* Could be changed after to reach a remote serial Modbus device */
  621. ctx->slave = MODBUS_TCP_SLAVE;
  622. ctx->backend = &(_modbus_tcp_backend);
  623. ctx->backend_data = (modbus_tcp_t *) malloc(sizeof(modbus_tcp_t));
  624. ctx_tcp = (modbus_tcp_t *)ctx->backend_data;
  625. dest_size = sizeof(char) * 16;
  626. ret_size = strlcpy(ctx_tcp->ip, ip, dest_size);
  627. if (ret_size == 0) {
  628. fprintf(stderr, "The IP string is empty\n");
  629. modbus_free(ctx);
  630. errno = EINVAL;
  631. return NULL;
  632. }
  633. if (ret_size >= dest_size) {
  634. fprintf(stderr, "The IP string has been truncated\n");
  635. modbus_free(ctx);
  636. errno = EINVAL;
  637. return NULL;
  638. }
  639. ctx_tcp->port = port;
  640. ctx_tcp->t_id = 0;
  641. return ctx;
  642. }
  643. modbus_t* modbus_new_tcp_pi(const char *node, const char *service)
  644. {
  645. modbus_t *ctx;
  646. modbus_tcp_pi_t *ctx_tcp_pi;
  647. size_t dest_size;
  648. size_t ret_size;
  649. ctx = (modbus_t *) malloc(sizeof(modbus_t));
  650. _modbus_init_common(ctx);
  651. /* Could be changed after to reach a remote serial Modbus device */
  652. ctx->slave = MODBUS_TCP_SLAVE;
  653. ctx->backend = &(_modbus_tcp_pi_backend);
  654. ctx->backend_data = (modbus_tcp_pi_t *) malloc(sizeof(modbus_tcp_pi_t));
  655. ctx_tcp_pi = (modbus_tcp_pi_t *)ctx->backend_data;
  656. dest_size = sizeof(char) * _MODBUS_TCP_PI_NODE_LENGTH;
  657. ret_size = strlcpy(ctx_tcp_pi->node, node, dest_size);
  658. if (ret_size == 0) {
  659. fprintf(stderr, "The node string is empty\n");
  660. modbus_free(ctx);
  661. errno = EINVAL;
  662. return NULL;
  663. }
  664. if (ret_size >= dest_size) {
  665. fprintf(stderr, "The node string has been truncated\n");
  666. modbus_free(ctx);
  667. errno = EINVAL;
  668. return NULL;
  669. }
  670. dest_size = sizeof(char) * _MODBUS_TCP_PI_SERVICE_LENGTH;
  671. ret_size = strlcpy(ctx_tcp_pi->service, service, dest_size);
  672. if (ret_size == 0) {
  673. fprintf(stderr, "The service string is empty\n");
  674. modbus_free(ctx);
  675. errno = EINVAL;
  676. return NULL;
  677. }
  678. if (ret_size >= dest_size) {
  679. fprintf(stderr, "The service string has been truncated\n");
  680. modbus_free(ctx);
  681. errno = EINVAL;
  682. return NULL;
  683. }
  684. ctx_tcp_pi->t_id = 0;
  685. return ctx;
  686. }