modbus-tcp.c 19 KB

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