modbus-tcp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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:%d\n", ctx_tcp->ip, ctx_tcp->port);
  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. if (ctx->debug) {
  314. printf("Connecting to [%s]:%s\n", ctx_tcp_pi->node, ctx_tcp_pi->service);
  315. }
  316. rc = _connect(s, ai_ptr->ai_addr, ai_ptr->ai_addrlen, &ctx->response_timeout);
  317. if (rc == -1) {
  318. close(s);
  319. continue;
  320. }
  321. ctx->s = s;
  322. break;
  323. }
  324. freeaddrinfo(ai_list);
  325. if (ctx->s < 0) {
  326. return -1;
  327. }
  328. return 0;
  329. }
  330. /* Closes the network connection and socket in TCP mode */
  331. void _modbus_tcp_close(modbus_t *ctx)
  332. {
  333. shutdown(ctx->s, SHUT_RDWR);
  334. close(ctx->s);
  335. }
  336. int _modbus_tcp_flush(modbus_t *ctx)
  337. {
  338. int rc;
  339. int rc_sum = 0;
  340. do {
  341. /* Extract the garbage from the socket */
  342. char devnull[MODBUS_TCP_MAX_ADU_LENGTH];
  343. #ifndef OS_WIN32
  344. rc = recv(ctx->s, devnull, MODBUS_TCP_MAX_ADU_LENGTH, MSG_DONTWAIT);
  345. #else
  346. /* On Win32, it's a bit more complicated to not wait */
  347. fd_set rset;
  348. struct timeval tv;
  349. tv.tv_sec = 0;
  350. tv.tv_usec = 0;
  351. FD_ZERO(&rset);
  352. FD_SET(ctx->s, &rset);
  353. rc = select(ctx->s+1, &rset, NULL, NULL, &tv);
  354. if (rc == -1) {
  355. return -1;
  356. }
  357. if (rc == 1) {
  358. /* There is data to flush */
  359. rc = recv(ctx->s, devnull, MODBUS_TCP_MAX_ADU_LENGTH, 0);
  360. }
  361. #endif
  362. if (rc > 0) {
  363. rc_sum += rc;
  364. }
  365. } while (rc == MODBUS_TCP_MAX_ADU_LENGTH);
  366. return rc_sum;
  367. }
  368. /* Listens for any request from one or many modbus masters in TCP */
  369. int modbus_tcp_listen(modbus_t *ctx, int nb_connection)
  370. {
  371. int new_socket;
  372. int yes;
  373. struct sockaddr_in addr;
  374. modbus_tcp_t *ctx_tcp = ctx->backend_data;
  375. #ifdef OS_WIN32
  376. if (_modbus_tcp_init_win32() == -1) {
  377. return -1;
  378. }
  379. #endif
  380. new_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  381. if (new_socket == -1) {
  382. return -1;
  383. }
  384. yes = 1;
  385. if (setsockopt(new_socket, SOL_SOCKET, SO_REUSEADDR,
  386. (char *) &yes, sizeof(yes)) == -1) {
  387. close(new_socket);
  388. return -1;
  389. }
  390. memset(&addr, 0, sizeof(addr));
  391. addr.sin_family = AF_INET;
  392. /* If the modbus port is < to 1024, we need the setuid root. */
  393. addr.sin_port = htons(ctx_tcp->port);
  394. addr.sin_addr.s_addr = INADDR_ANY;
  395. if (bind(new_socket, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
  396. close(new_socket);
  397. return -1;
  398. }
  399. if (listen(new_socket, nb_connection) == -1) {
  400. close(new_socket);
  401. return -1;
  402. }
  403. return new_socket;
  404. }
  405. int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection)
  406. {
  407. int rc;
  408. struct addrinfo *ai_list;
  409. struct addrinfo *ai_ptr;
  410. struct addrinfo ai_hints;
  411. const char *node;
  412. const char *service;
  413. int new_socket;
  414. modbus_tcp_pi_t *ctx_tcp_pi = ctx->backend_data;
  415. if (ctx_tcp_pi->node[0] == 0)
  416. node = NULL; /* == any */
  417. else
  418. node = ctx_tcp_pi->node;
  419. if (ctx_tcp_pi->service[0] == 0)
  420. service = "502";
  421. else
  422. service = ctx_tcp_pi->service;
  423. memset(&ai_hints, 0, sizeof (ai_hints));
  424. ai_hints.ai_flags |= AI_PASSIVE;
  425. #ifdef AI_ADDRCONFIG
  426. ai_hints.ai_flags |= AI_ADDRCONFIG;
  427. #endif
  428. ai_hints.ai_family = AF_UNSPEC;
  429. ai_hints.ai_socktype = SOCK_STREAM;
  430. ai_hints.ai_addr = NULL;
  431. ai_hints.ai_canonname = NULL;
  432. ai_hints.ai_next = NULL;
  433. ai_list = NULL;
  434. rc = getaddrinfo(node, service, &ai_hints, &ai_list);
  435. if (rc != 0)
  436. return -1;
  437. new_socket = -1;
  438. for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
  439. int s;
  440. s = socket(ai_ptr->ai_family, ai_ptr->ai_socktype,
  441. ai_ptr->ai_protocol);
  442. if (s < 0) {
  443. if (ctx->debug) {
  444. perror("socket");
  445. }
  446. continue;
  447. } else {
  448. int yes = 1;
  449. rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
  450. (void *) &yes, sizeof (yes));
  451. if (rc != 0) {
  452. close(s);
  453. if (ctx->debug) {
  454. perror("setsockopt");
  455. }
  456. continue;
  457. }
  458. }
  459. rc = bind(s, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
  460. if (rc != 0) {
  461. close(s);
  462. if (ctx->debug) {
  463. perror("bind");
  464. }
  465. continue;
  466. }
  467. rc = listen(s, nb_connection);
  468. if (rc != 0) {
  469. close(s);
  470. if (ctx->debug) {
  471. perror("listen");
  472. }
  473. continue;
  474. }
  475. new_socket = s;
  476. break;
  477. }
  478. freeaddrinfo(ai_list);
  479. if (new_socket < 0) {
  480. return -1;
  481. }
  482. return new_socket;
  483. }
  484. /* On success, the function return a non-negative integer that is a descriptor
  485. for the accepted socket. On error, -1 is returned, and errno is set
  486. appropriately. */
  487. int modbus_tcp_accept(modbus_t *ctx, int *socket)
  488. {
  489. struct sockaddr_in addr;
  490. socklen_t addrlen;
  491. addrlen = sizeof(addr);
  492. #ifdef HAVE_ACCEPT4
  493. /* Inherit socket flags and use accept4 call */
  494. ctx->s = accept4(*socket, (struct sockaddr *)&addr, &addrlen, SOCK_CLOEXEC);
  495. #else
  496. ctx->s = accept(*socket, (struct sockaddr *)&addr, &addrlen);
  497. #endif
  498. if (ctx->s == -1) {
  499. close(*socket);
  500. *socket = 0;
  501. return -1;
  502. }
  503. if (ctx->debug) {
  504. printf("The client connection from %s is accepted\n",
  505. inet_ntoa(addr.sin_addr));
  506. }
  507. return ctx->s;
  508. }
  509. int modbus_tcp_pi_accept(modbus_t *ctx, int *socket)
  510. {
  511. struct sockaddr_storage addr;
  512. socklen_t addrlen;
  513. addrlen = sizeof(addr);
  514. ctx->s = accept(*socket, (void *)&addr, &addrlen);
  515. if (ctx->s == -1) {
  516. close(*socket);
  517. *socket = 0;
  518. }
  519. if (ctx->debug) {
  520. printf("The client connection is accepted.\n");
  521. }
  522. return ctx->s;
  523. }
  524. int _modbus_tcp_select(modbus_t *ctx, fd_set *rset, struct timeval *tv, int length_to_read)
  525. {
  526. int s_rc;
  527. while ((s_rc = select(ctx->s+1, rset, NULL, NULL, tv)) == -1) {
  528. if (errno == EINTR) {
  529. if (ctx->debug) {
  530. fprintf(stderr, "A non blocked signal was caught\n");
  531. }
  532. /* Necessary after an error */
  533. FD_ZERO(rset);
  534. FD_SET(ctx->s, rset);
  535. } else {
  536. return -1;
  537. }
  538. }
  539. if (s_rc == 0) {
  540. errno = ETIMEDOUT;
  541. return -1;
  542. }
  543. return s_rc;
  544. }
  545. const modbus_backend_t _modbus_tcp_backend = {
  546. _MODBUS_BACKEND_TYPE_TCP,
  547. _MODBUS_TCP_HEADER_LENGTH,
  548. _MODBUS_TCP_CHECKSUM_LENGTH,
  549. MODBUS_TCP_MAX_ADU_LENGTH,
  550. _modbus_set_slave,
  551. _modbus_tcp_build_request_basis,
  552. _modbus_tcp_build_response_basis,
  553. _modbus_tcp_prepare_response_tid,
  554. _modbus_tcp_send_msg_pre,
  555. _modbus_tcp_send,
  556. _modbus_tcp_receive,
  557. _modbus_tcp_recv,
  558. _modbus_tcp_check_integrity,
  559. _modbus_tcp_pre_check_confirmation,
  560. _modbus_tcp_connect,
  561. _modbus_tcp_close,
  562. _modbus_tcp_flush,
  563. _modbus_tcp_select
  564. };
  565. const modbus_backend_t _modbus_tcp_pi_backend = {
  566. _MODBUS_BACKEND_TYPE_TCP,
  567. _MODBUS_TCP_HEADER_LENGTH,
  568. _MODBUS_TCP_CHECKSUM_LENGTH,
  569. MODBUS_TCP_MAX_ADU_LENGTH,
  570. _modbus_set_slave,
  571. _modbus_tcp_build_request_basis,
  572. _modbus_tcp_build_response_basis,
  573. _modbus_tcp_prepare_response_tid,
  574. _modbus_tcp_send_msg_pre,
  575. _modbus_tcp_send,
  576. _modbus_tcp_receive,
  577. _modbus_tcp_recv,
  578. _modbus_tcp_check_integrity,
  579. _modbus_tcp_pre_check_confirmation,
  580. _modbus_tcp_pi_connect,
  581. _modbus_tcp_close,
  582. _modbus_tcp_flush,
  583. _modbus_tcp_select
  584. };
  585. modbus_t* modbus_new_tcp(const char *ip, int port)
  586. {
  587. modbus_t *ctx;
  588. modbus_tcp_t *ctx_tcp;
  589. size_t dest_size;
  590. size_t ret_size;
  591. #if defined(OS_BSD)
  592. /* MSG_NOSIGNAL is unsupported on *BSD so we install an ignore
  593. handler for SIGPIPE. */
  594. struct sigaction sa;
  595. sa.sa_handler = SIG_IGN;
  596. if (sigaction(SIGPIPE, &sa, NULL) < 0) {
  597. /* The debug flag can't be set here... */
  598. fprintf(stderr, "Coud not install SIGPIPE handler.\n");
  599. return NULL;
  600. }
  601. #endif
  602. ctx = (modbus_t *) malloc(sizeof(modbus_t));
  603. _modbus_init_common(ctx);
  604. /* Could be changed after to reach a remote serial Modbus device */
  605. ctx->slave = MODBUS_TCP_SLAVE;
  606. ctx->backend = &(_modbus_tcp_backend);
  607. ctx->backend_data = (modbus_tcp_t *) malloc(sizeof(modbus_tcp_t));
  608. ctx_tcp = (modbus_tcp_t *)ctx->backend_data;
  609. dest_size = sizeof(char) * 16;
  610. ret_size = strlcpy(ctx_tcp->ip, ip, dest_size);
  611. if (ret_size == 0) {
  612. fprintf(stderr, "The IP string is empty\n");
  613. modbus_free(ctx);
  614. errno = EINVAL;
  615. return NULL;
  616. }
  617. if (ret_size >= dest_size) {
  618. fprintf(stderr, "The IP string has been truncated\n");
  619. modbus_free(ctx);
  620. errno = EINVAL;
  621. return NULL;
  622. }
  623. ctx_tcp->port = port;
  624. ctx_tcp->t_id = 0;
  625. return ctx;
  626. }
  627. modbus_t* modbus_new_tcp_pi(const char *node, const char *service)
  628. {
  629. modbus_t *ctx;
  630. modbus_tcp_pi_t *ctx_tcp_pi;
  631. size_t dest_size;
  632. size_t ret_size;
  633. ctx = (modbus_t *) malloc(sizeof(modbus_t));
  634. _modbus_init_common(ctx);
  635. /* Could be changed after to reach a remote serial Modbus device */
  636. ctx->slave = MODBUS_TCP_SLAVE;
  637. ctx->backend = &(_modbus_tcp_pi_backend);
  638. ctx->backend_data = (modbus_tcp_pi_t *) malloc(sizeof(modbus_tcp_pi_t));
  639. ctx_tcp_pi = (modbus_tcp_pi_t *)ctx->backend_data;
  640. dest_size = sizeof(char) * _MODBUS_TCP_PI_NODE_LENGTH;
  641. ret_size = strlcpy(ctx_tcp_pi->node, node, dest_size);
  642. if (ret_size == 0) {
  643. fprintf(stderr, "The node string is empty\n");
  644. modbus_free(ctx);
  645. errno = EINVAL;
  646. return NULL;
  647. }
  648. if (ret_size >= dest_size) {
  649. fprintf(stderr, "The node string has been truncated\n");
  650. modbus_free(ctx);
  651. errno = EINVAL;
  652. return NULL;
  653. }
  654. dest_size = sizeof(char) * _MODBUS_TCP_PI_SERVICE_LENGTH;
  655. ret_size = strlcpy(ctx_tcp_pi->service, service, dest_size);
  656. if (ret_size == 0) {
  657. fprintf(stderr, "The service string is empty\n");
  658. modbus_free(ctx);
  659. errno = EINVAL;
  660. return NULL;
  661. }
  662. if (ret_size >= dest_size) {
  663. fprintf(stderr, "The service string has been truncated\n");
  664. modbus_free(ctx);
  665. errno = EINVAL;
  666. return NULL;
  667. }
  668. ctx_tcp_pi->t_id = 0;
  669. return ctx;
  670. }