Blame view

3rdparty/libmodbus/modbus-tcp.c 21.1 KB
500c015a   Peter M. Groen   Setting up workin...
1
2
3
4
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  #include <errno.h>
500c015a   Peter M. Groen   Setting up workin...
5
  #include <unistd.h>
500c015a   Peter M. Groen   Setting up workin...
6
7
8
  #include <signal.h>
  #include <sys/types.h>
  
500c015a   Peter M. Groen   Setting up workin...
9
10
11
12
13
14
15
16
17
18
19
20
21
  # include <sys/socket.h>
  # include <sys/ioctl.h>
  
  #if defined(__OpenBSD__) || (defined(__FreeBSD__) && __FreeBSD__ < 5)
  # define OS_BSD
  # include <netinet/in_systm.h>
  #endif
  
  # include <netinet/in.h>
  # include <netinet/ip.h>
  # include <netinet/tcp.h>
  # include <arpa/inet.h>
  # include <netdb.h>
46785270   Peter M. Groen   Setting up workin...
22
  
5735b406   Peter M. Groen   Revert "Setting u...
23
  
500c015a   Peter M. Groen   Setting up workin...
24
25
26
27
  #if !defined(MSG_NOSIGNAL)
  #define MSG_NOSIGNAL 0
  #endif
  
5735b406   Peter M. Groen   Revert "Setting u...
28
29
30
31
  #if defined(_AIX) && !defined(MSG_DONTWAIT)
  #define MSG_DONTWAIT MSG_NONBLOCK
  #endif
  
500c015a   Peter M. Groen   Setting up workin...
32
33
34
35
36
  #include "modbus-private.h"
  
  #include "modbus-tcp.h"
  #include "modbus-tcp-private.h"
  
5735b406   Peter M. Groen   Revert "Setting u...
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  #ifdef OS_WIN32
  static int _modbus_tcp_init_win32(void)
  {
      /* Initialise Windows Socket API */
      WSADATA wsaData;
  
      if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
          fprintf(stderr, "WSAStartup() returned error code %d\n",
                  (unsigned int)GetLastError());
          errno = EIO;
          return -1;
      }
      return 0;
  }
  #endif
  
500c015a   Peter M. Groen   Setting up workin...
53
54
55
  static int _modbus_set_slave(modbus_t *ctx, int slave)
  {
      /* Broadcast address is 0 (MODBUS_BROADCAST_ADDRESS) */
5735b406   Peter M. Groen   Revert "Setting u...
56
      if (slave >= 0 && slave <= 247) {
500c015a   Peter M. Groen   Setting up workin...
57
          ctx->slave = slave;
5735b406   Peter M. Groen   Revert "Setting u...
58
      } else if (slave == MODBUS_TCP_SLAVE) {
500c015a   Peter M. Groen   Setting up workin...
59
60
61
          /* The special value MODBUS_TCP_SLAVE (0xFF) can be used in TCP mode to
           * restore the default value. */
          ctx->slave = slave;
5735b406   Peter M. Groen   Revert "Setting u...
62
      } else {
500c015a   Peter M. Groen   Setting up workin...
63
64
65
66
67
68
69
70
          errno = EINVAL;
          return -1;
      }
  
      return 0;
  }
  
  /* Builds a TCP request header */
5735b406   Peter M. Groen   Revert "Setting u...
71
72
73
  static int _modbus_tcp_build_request_basis(modbus_t *ctx, int function,
                                             int addr, int nb,
                                             uint8_t *req)
500c015a   Peter M. Groen   Setting up workin...
74
75
76
77
78
79
80
81
  {
      modbus_tcp_t *ctx_tcp = ctx->backend_data;
  
      /* Increase transaction ID */
      if (ctx_tcp->t_id < UINT16_MAX)
          ctx_tcp->t_id++;
      else
          ctx_tcp->t_id = 0;
500c015a   Peter M. Groen   Setting up workin...
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
      req[0] = ctx_tcp->t_id >> 8;
      req[1] = ctx_tcp->t_id & 0x00ff;
  
      /* Protocol Modbus */
      req[2] = 0;
      req[3] = 0;
  
      /* Length will be defined later by set_req_length_tcp at offsets 4
         and 5 */
  
      req[6] = ctx->slave;
      req[7] = function;
      req[8] = addr >> 8;
      req[9] = addr & 0x00ff;
      req[10] = nb >> 8;
      req[11] = nb & 0x00ff;
  
      return _MODBUS_TCP_PRESET_REQ_LENGTH;
  }
  
  /* Builds a TCP response header */
5735b406   Peter M. Groen   Revert "Setting u...
103
  static int _modbus_tcp_build_response_basis(sft_t *sft, uint8_t *rsp)
500c015a   Peter M. Groen   Setting up workin...
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  {
      /* Extract from MODBUS Messaging on TCP/IP Implementation
         Guide V1.0b (page 23/46):
         The transaction identifier is used to associate the future
         response with the request. */
      rsp[0] = sft->t_id >> 8;
      rsp[1] = sft->t_id & 0x00ff;
  
      /* Protocol Modbus */
      rsp[2] = 0;
      rsp[3] = 0;
  
      /* Length will be set later by send_msg (4 and 5) */
  
      /* The slave ID is copied from the indication */
      rsp[6] = sft->slave;
      rsp[7] = sft->function;
  
      return _MODBUS_TCP_PRESET_RSP_LENGTH;
  }
  
  
5735b406   Peter M. Groen   Revert "Setting u...
126
  static int _modbus_tcp_prepare_response_tid(const uint8_t *req, int *req_length)
500c015a   Peter M. Groen   Setting up workin...
127
128
129
130
  {
      return (req[0] << 8) + req[1];
  }
  
5735b406   Peter M. Groen   Revert "Setting u...
131
  static int _modbus_tcp_send_msg_pre(uint8_t *req, int req_length)
500c015a   Peter M. Groen   Setting up workin...
132
133
134
135
136
137
138
139
140
141
  {
      /* Substract the header length to the message length */
      int mbap_length = req_length - 6;
  
      req[4] = mbap_length >> 8;
      req[5] = mbap_length & 0x00FF;
  
      return req_length;
  }
  
5735b406   Peter M. Groen   Revert "Setting u...
142
  static ssize_t _modbus_tcp_send(modbus_t *ctx, const uint8_t *req, int req_length)
500c015a   Peter M. Groen   Setting up workin...
143
144
145
146
147
148
149
150
  {
      /* MSG_NOSIGNAL
         Requests not to send SIGPIPE on errors on stream oriented
         sockets when the other end breaks the connection.  The EPIPE
         error is still returned. */
      return send(ctx->s, (const char *)req, req_length, MSG_NOSIGNAL);
  }
  
5735b406   Peter M. Groen   Revert "Setting u...
151
  static int _modbus_tcp_receive(modbus_t *ctx, uint8_t *req) {
500c015a   Peter M. Groen   Setting up workin...
152
153
154
      return _modbus_receive_msg(ctx, req, MSG_INDICATION);
  }
  
5735b406   Peter M. Groen   Revert "Setting u...
155
156
  static ssize_t _modbus_tcp_recv(modbus_t *ctx, uint8_t *rsp, int rsp_length) {
      return recv(ctx->s, (char *)rsp, rsp_length, 0);
500c015a   Peter M. Groen   Setting up workin...
157
158
  }
  
5735b406   Peter M. Groen   Revert "Setting u...
159
  static int _modbus_tcp_check_integrity(modbus_t *ctx, uint8_t *msg, const int msg_length)
500c015a   Peter M. Groen   Setting up workin...
160
161
162
163
  {
      return msg_length;
  }
  
5735b406   Peter M. Groen   Revert "Setting u...
164
165
  static int _modbus_tcp_pre_check_confirmation(modbus_t *ctx, const uint8_t *req,
                                                const uint8_t *rsp, int rsp_length)
500c015a   Peter M. Groen   Setting up workin...
166
167
168
  {
      /* Check transaction ID */
      if (req[0] != rsp[0] || req[1] != rsp[1]) {
5735b406   Peter M. Groen   Revert "Setting u...
169
          if (ctx->debug) {
500c015a   Peter M. Groen   Setting up workin...
170
171
172
173
174
175
176
177
              fprintf(stderr, "Invalid transaction ID received 0x%X (not 0x%X)\n",
                      (rsp[0] << 8) + rsp[1], (req[0] << 8) + req[1]);
          }
          errno = EMBBADDATA;
          return -1;
      }
  
      /* Check protocol ID */
5735b406   Peter M. Groen   Revert "Setting u...
178
179
      if (rsp[2] != 0x0 && rsp[3] != 0x0) {
          if (ctx->debug) {
500c015a   Peter M. Groen   Setting up workin...
180
181
182
183
184
185
186
187
188
189
              fprintf(stderr, "Invalid protocol ID received 0x%X (not 0x0)\n",
                      (rsp[2] << 8) + rsp[3]);
          }
          errno = EMBBADDATA;
          return -1;
      }
  
      return 0;
  }
  
5735b406   Peter M. Groen   Revert "Setting u...
190
  static int _modbus_tcp_set_ipv4_options(int s)
500c015a   Peter M. Groen   Setting up workin...
191
192
193
194
195
196
197
  {
      int rc;
      int option;
  
      /* Set the TCP no delay flag */
      /* SOL_TCP = IPPROTO_TCP */
      option = 1;
5735b406   Peter M. Groen   Revert "Setting u...
198
199
200
      rc = setsockopt(s, IPPROTO_TCP, TCP_NODELAY,
                      (const void *)&option, sizeof(int));
      if (rc == -1) {
500c015a   Peter M. Groen   Setting up workin...
201
202
203
204
205
206
207
          return -1;
      }
  
      /* If the OS does not offer SOCK_NONBLOCK, fall back to setting FIONBIO to
       * make sockets non-blocking */
      /* Do not care about the return value, this is optional */
  #if !defined(SOCK_NONBLOCK) && defined(FIONBIO)
5735b406   Peter M. Groen   Revert "Setting u...
208
209
210
211
212
213
214
  #ifdef OS_WIN32
      {
          /* Setting FIONBIO expects an unsigned long according to MSDN */
          u_long loption = 1;
          ioctlsocket(s, FIONBIO, &loption);
      }
  #else
500c015a   Peter M. Groen   Setting up workin...
215
216
217
      option = 1;
      ioctl(s, FIONBIO, &option);
  #endif
5735b406   Peter M. Groen   Revert "Setting u...
218
  #endif
500c015a   Peter M. Groen   Setting up workin...
219
  
5735b406   Peter M. Groen   Revert "Setting u...
220
  #ifndef OS_WIN32
500c015a   Peter M. Groen   Setting up workin...
221
222
223
224
225
226
227
228
229
230
231
      /**
       * Cygwin defines IPTOS_LOWDELAY but can't handle that flag so it's
       * necessary to workaround that problem.
       **/
      /* Set the IP low delay option */
      option = IPTOS_LOWDELAY;
      rc = setsockopt(s, IPPROTO_IP, IP_TOS,
                      (const void *)&option, sizeof(int));
      if (rc == -1) {
          return -1;
      }
5735b406   Peter M. Groen   Revert "Setting u...
232
  #endif
500c015a   Peter M. Groen   Setting up workin...
233
234
235
236
  
      return 0;
  }
  
5735b406   Peter M. Groen   Revert "Setting u...
237
238
  static int _connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen,
                      const struct timeval *ro_tv)
500c015a   Peter M. Groen   Setting up workin...
239
240
241
  {
      int rc = connect(sockfd, addr, addrlen);
  
5735b406   Peter M. Groen   Revert "Setting u...
242
243
244
245
246
247
248
249
250
251
  #ifdef OS_WIN32
      int wsaError = 0;
      if (rc == -1) {
          wsaError = WSAGetLastError();
      }
  
      if (wsaError == WSAEWOULDBLOCK || wsaError == WSAEINPROGRESS) {
  #else
      if (rc == -1 && errno == EINPROGRESS) {
  #endif
500c015a   Peter M. Groen   Setting up workin...
252
253
254
255
256
257
258
259
260
          fd_set wset;
          int optval;
          socklen_t optlen = sizeof(optval);
          struct timeval tv = *ro_tv;
  
          /* Wait to be available in writing */
          FD_ZERO(&wset);
          FD_SET(sockfd, &wset);
          rc = select(sockfd + 1, NULL, &wset, NULL, &tv);
5735b406   Peter M. Groen   Revert "Setting u...
261
          if (rc <= 0) {
500c015a   Peter M. Groen   Setting up workin...
262
263
264
265
266
267
              /* Timeout or fail */
              return -1;
          }
  
          /* The connection is established if SO_ERROR and optval are set to 0 */
          rc = getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void *)&optval, &optlen);
5735b406   Peter M. Groen   Revert "Setting u...
268
          if (rc == 0 && optval == 0) {
500c015a   Peter M. Groen   Setting up workin...
269
              return 0;
5735b406   Peter M. Groen   Revert "Setting u...
270
          } else {
500c015a   Peter M. Groen   Setting up workin...
271
272
273
274
275
276
277
278
              errno = ECONNREFUSED;
              return -1;
          }
      }
      return rc;
  }
  
  /* Establishes a modbus TCP connection with a Modbus server. */
5735b406   Peter M. Groen   Revert "Setting u...
279
  static int _modbus_tcp_connect(modbus_t *ctx)
500c015a   Peter M. Groen   Setting up workin...
280
281
282
283
284
285
286
  {
      int rc;
      /* Specialized version of sockaddr for Internet socket address (same size) */
      struct sockaddr_in addr;
      modbus_tcp_t *ctx_tcp = ctx->backend_data;
      int flags = SOCK_STREAM;
  
5735b406   Peter M. Groen   Revert "Setting u...
287
288
289
290
291
292
  #ifdef OS_WIN32
      if (_modbus_tcp_init_win32() == -1) {
          return -1;
      }
  #endif
  
500c015a   Peter M. Groen   Setting up workin...
293
294
295
296
297
298
299
300
301
  #ifdef SOCK_CLOEXEC
      flags |= SOCK_CLOEXEC;
  #endif
  
  #ifdef SOCK_NONBLOCK
      flags |= SOCK_NONBLOCK;
  #endif
  
      ctx->s = socket(PF_INET, flags, 0);
5735b406   Peter M. Groen   Revert "Setting u...
302
      if (ctx->s == -1) {
500c015a   Peter M. Groen   Setting up workin...
303
304
305
306
          return -1;
      }
  
      rc = _modbus_tcp_set_ipv4_options(ctx->s);
5735b406   Peter M. Groen   Revert "Setting u...
307
      if (rc == -1) {
500c015a   Peter M. Groen   Setting up workin...
308
309
310
311
312
          close(ctx->s);
          ctx->s = -1;
          return -1;
      }
  
5735b406   Peter M. Groen   Revert "Setting u...
313
      if (ctx->debug) {
500c015a   Peter M. Groen   Setting up workin...
314
315
316
317
318
319
320
          printf("Connecting to %s:%d\n", ctx_tcp->ip, ctx_tcp->port);
      }
  
      addr.sin_family = AF_INET;
      addr.sin_port = htons(ctx_tcp->port);
      addr.sin_addr.s_addr = inet_addr(ctx_tcp->ip);
      rc = _connect(ctx->s, (struct sockaddr *)&addr, sizeof(addr), &ctx->response_timeout);
5735b406   Peter M. Groen   Revert "Setting u...
321
322
      if (rc == -1) {
          close(ctx->s);
500c015a   Peter M. Groen   Setting up workin...
323
324
325
326
327
328
329
330
          ctx->s = -1;
          return -1;
      }
  
      return 0;
  }
  
  /* Establishes a modbus TCP PI connection with a Modbus server. */
5735b406   Peter M. Groen   Revert "Setting u...
331
  static int _modbus_tcp_pi_connect(modbus_t *ctx)
500c015a   Peter M. Groen   Setting up workin...
332
333
334
335
336
337
338
  {
      int rc;
      struct addrinfo *ai_list;
      struct addrinfo *ai_ptr;
      struct addrinfo ai_hints;
      modbus_tcp_pi_t *ctx_tcp_pi = ctx->backend_data;
  
5735b406   Peter M. Groen   Revert "Setting u...
339
340
341
342
343
344
  #ifdef OS_WIN32
      if (_modbus_tcp_init_win32() == -1) {
          return -1;
      }
  #endif
  
500c015a   Peter M. Groen   Setting up workin...
345
346
347
348
349
350
351
352
353
354
355
      memset(&ai_hints, 0, sizeof(ai_hints));
  #ifdef AI_ADDRCONFIG
      ai_hints.ai_flags |= AI_ADDRCONFIG;
  #endif
      ai_hints.ai_family = AF_UNSPEC;
      ai_hints.ai_socktype = SOCK_STREAM;
      ai_hints.ai_addr = NULL;
      ai_hints.ai_canonname = NULL;
      ai_hints.ai_next = NULL;
  
      ai_list = NULL;
5735b406   Peter M. Groen   Revert "Setting u...
356
357
358
359
      rc = getaddrinfo(ctx_tcp_pi->node, ctx_tcp_pi->service,
                       &ai_hints, &ai_list);
      if (rc != 0) {
          if (ctx->debug) {
500c015a   Peter M. Groen   Setting up workin...
360
361
362
363
364
365
              fprintf(stderr, "Error returned by getaddrinfo: %s\n", gai_strerror(rc));
          }
          errno = ECONNREFUSED;
          return -1;
      }
  
5735b406   Peter M. Groen   Revert "Setting u...
366
      for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
500c015a   Peter M. Groen   Setting up workin...
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
          int flags = ai_ptr->ai_socktype;
          int s;
  
  #ifdef SOCK_CLOEXEC
          flags |= SOCK_CLOEXEC;
  #endif
  
  #ifdef SOCK_NONBLOCK
          flags |= SOCK_NONBLOCK;
  #endif
  
          s = socket(ai_ptr->ai_family, flags, ai_ptr->ai_protocol);
          if (s < 0)
              continue;
  
          if (ai_ptr->ai_family == AF_INET)
              _modbus_tcp_set_ipv4_options(s);
  
5735b406   Peter M. Groen   Revert "Setting u...
385
          if (ctx->debug) {
500c015a   Peter M. Groen   Setting up workin...
386
387
388
389
              printf("Connecting to [%s]:%s\n", ctx_tcp_pi->node, ctx_tcp_pi->service);
          }
  
          rc = _connect(s, ai_ptr->ai_addr, ai_ptr->ai_addrlen, &ctx->response_timeout);
5735b406   Peter M. Groen   Revert "Setting u...
390
          if (rc == -1) {
500c015a   Peter M. Groen   Setting up workin...
391
392
393
394
395
396
397
398
399
400
              close(s);
              continue;
          }
  
          ctx->s = s;
          break;
      }
  
      freeaddrinfo(ai_list);
  
5735b406   Peter M. Groen   Revert "Setting u...
401
      if (ctx->s < 0) {
500c015a   Peter M. Groen   Setting up workin...
402
403
404
405
406
407
408
          return -1;
      }
  
      return 0;
  }
  
  /* Closes the network connection and socket in TCP mode */
5735b406   Peter M. Groen   Revert "Setting u...
409
  static void _modbus_tcp_close(modbus_t *ctx)
500c015a   Peter M. Groen   Setting up workin...
410
  {
5735b406   Peter M. Groen   Revert "Setting u...
411
      if (ctx->s != -1) {
500c015a   Peter M. Groen   Setting up workin...
412
413
414
415
416
417
          shutdown(ctx->s, SHUT_RDWR);
          close(ctx->s);
          ctx->s = -1;
      }
  }
  
5735b406   Peter M. Groen   Revert "Setting u...
418
  static int _modbus_tcp_flush(modbus_t *ctx)
500c015a   Peter M. Groen   Setting up workin...
419
420
421
422
  {
      int rc;
      int rc_sum = 0;
  
5735b406   Peter M. Groen   Revert "Setting u...
423
      do {
500c015a   Peter M. Groen   Setting up workin...
424
425
          /* Extract the garbage from the socket */
          char devnull[MODBUS_TCP_MAX_ADU_LENGTH];
5735b406   Peter M. Groen   Revert "Setting u...
426
  #ifndef OS_WIN32
783ce3c5   Peter M. Groen   Setting up workin...
427
          rc = recv(ctx->s, devnull, MODBUS_TCP_MAX_ADU_LENGTH, MSG_DONTWAIT);
5735b406   Peter M. Groen   Revert "Setting u...
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
  #else
          /* On Win32, it's a bit more complicated to not wait */
          fd_set rset;
          struct timeval tv;
  
          tv.tv_sec = 0;
          tv.tv_usec = 0;
          FD_ZERO(&rset);
          FD_SET(ctx->s, &rset);
          rc = select(ctx->s+1, &rset, NULL, NULL, &tv);
          if (rc == -1) {
              return -1;
          }
  
          if (rc == 1) {
              /* There is data to flush */
              rc = recv(ctx->s, devnull, MODBUS_TCP_MAX_ADU_LENGTH, 0);
          }
  #endif
          if (rc > 0) {
500c015a   Peter M. Groen   Setting up workin...
448
449
              rc_sum += rc;
          }
5735b406   Peter M. Groen   Revert "Setting u...
450
      } while (rc == MODBUS_TCP_MAX_ADU_LENGTH);
500c015a   Peter M. Groen   Setting up workin...
451
452
453
454
455
  
      return rc_sum;
  }
  
  /* Listens for any request from one or many modbus masters in TCP */
5735b406   Peter M. Groen   Revert "Setting u...
456
  int modbus_tcp_listen(modbus_t *ctx, int nb_connection)
500c015a   Peter M. Groen   Setting up workin...
457
458
459
460
461
462
  {
      int new_s;
      int enable;
      struct sockaddr_in addr;
      modbus_tcp_t *ctx_tcp;
  
5735b406   Peter M. Groen   Revert "Setting u...
463
      if (ctx == NULL) {
500c015a   Peter M. Groen   Setting up workin...
464
465
466
467
468
469
          errno = EINVAL;
          return -1;
      }
  
      ctx_tcp = ctx->backend_data;
  
5735b406   Peter M. Groen   Revert "Setting u...
470
471
472
473
474
475
  #ifdef OS_WIN32
      if (_modbus_tcp_init_win32() == -1) {
          return -1;
      }
  #endif
  
500c015a   Peter M. Groen   Setting up workin...
476
      new_s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
5735b406   Peter M. Groen   Revert "Setting u...
477
      if (new_s == -1) {
500c015a   Peter M. Groen   Setting up workin...
478
479
480
481
          return -1;
      }
  
      enable = 1;
5735b406   Peter M. Groen   Revert "Setting u...
482
483
      if (setsockopt(new_s, SOL_SOCKET, SO_REUSEADDR,
                     (char *)&enable, sizeof(enable)) == -1) {
500c015a   Peter M. Groen   Setting up workin...
484
485
486
487
488
489
          close(new_s);
          return -1;
      }
  
      memset(&addr, 0, sizeof(addr));
      addr.sin_family = AF_INET;
500c015a   Peter M. Groen   Setting up workin...
490
491
      /* If the modbus port is < to 1024, we need the setuid root. */
      addr.sin_port = htons(ctx_tcp->port);
5735b406   Peter M. Groen   Revert "Setting u...
492
      if (ctx_tcp->ip[0] == '0') {
500c015a   Peter M. Groen   Setting up workin...
493
494
          /* Listen any addresses */
          addr.sin_addr.s_addr = htonl(INADDR_ANY);
5735b406   Peter M. Groen   Revert "Setting u...
495
      } else {
500c015a   Peter M. Groen   Setting up workin...
496
497
498
          /* Listen only specified IP address */
          addr.sin_addr.s_addr = inet_addr(ctx_tcp->ip);
      }
5735b406   Peter M. Groen   Revert "Setting u...
499
      if (bind(new_s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
500c015a   Peter M. Groen   Setting up workin...
500
501
502
503
          close(new_s);
          return -1;
      }
  
5735b406   Peter M. Groen   Revert "Setting u...
504
      if (listen(new_s, nb_connection) == -1) {
500c015a   Peter M. Groen   Setting up workin...
505
506
507
508
509
510
511
          close(new_s);
          return -1;
      }
  
      return new_s;
  }
  
5735b406   Peter M. Groen   Revert "Setting u...
512
  int modbus_tcp_pi_listen(modbus_t *ctx, int nb_connection)
500c015a   Peter M. Groen   Setting up workin...
513
514
515
516
517
518
519
520
521
522
  {
      int rc;
      struct addrinfo *ai_list;
      struct addrinfo *ai_ptr;
      struct addrinfo ai_hints;
      const char *node;
      const char *service;
      int new_s;
      modbus_tcp_pi_t *ctx_tcp_pi;
  
5735b406   Peter M. Groen   Revert "Setting u...
523
      if (ctx == NULL) {
500c015a   Peter M. Groen   Setting up workin...
524
525
526
527
528
529
          errno = EINVAL;
          return -1;
      }
  
      ctx_tcp_pi = ctx->backend_data;
  
5735b406   Peter M. Groen   Revert "Setting u...
530
531
532
533
534
535
536
  #ifdef OS_WIN32
      if (_modbus_tcp_init_win32() == -1) {
          return -1;
      }
  #endif
  
      if (ctx_tcp_pi->node[0] == 0) {
500c015a   Peter M. Groen   Setting up workin...
537
          node = NULL; /* == any */
5735b406   Peter M. Groen   Revert "Setting u...
538
      } else {
500c015a   Peter M. Groen   Setting up workin...
539
540
541
          node = ctx_tcp_pi->node;
      }
  
5735b406   Peter M. Groen   Revert "Setting u...
542
      if (ctx_tcp_pi->service[0] == 0) {
500c015a   Peter M. Groen   Setting up workin...
543
          service = "502";
5735b406   Peter M. Groen   Revert "Setting u...
544
      } else {
500c015a   Peter M. Groen   Setting up workin...
545
546
547
          service = ctx_tcp_pi->service;
      }
  
5735b406   Peter M. Groen   Revert "Setting u...
548
      memset(&ai_hints, 0, sizeof (ai_hints));
500c015a   Peter M. Groen   Setting up workin...
549
550
551
552
553
554
555
556
557
558
559
560
561
      /* If node is not NULL, than the AI_PASSIVE flag is ignored. */
      ai_hints.ai_flags |= AI_PASSIVE;
  #ifdef AI_ADDRCONFIG
      ai_hints.ai_flags |= AI_ADDRCONFIG;
  #endif
      ai_hints.ai_family = AF_UNSPEC;
      ai_hints.ai_socktype = SOCK_STREAM;
      ai_hints.ai_addr = NULL;
      ai_hints.ai_canonname = NULL;
      ai_hints.ai_next = NULL;
  
      ai_list = NULL;
      rc = getaddrinfo(node, service, &ai_hints, &ai_list);
5735b406   Peter M. Groen   Revert "Setting u...
562
563
      if (rc != 0) {
          if (ctx->debug) {
500c015a   Peter M. Groen   Setting up workin...
564
565
566
567
568
569
570
              fprintf(stderr, "Error returned by getaddrinfo: %s\n", gai_strerror(rc));
          }
          errno = ECONNREFUSED;
          return -1;
      }
  
      new_s = -1;
5735b406   Peter M. Groen   Revert "Setting u...
571
      for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
500c015a   Peter M. Groen   Setting up workin...
572
573
          int s;
  
5735b406   Peter M. Groen   Revert "Setting u...
574
575
576
577
          s = socket(ai_ptr->ai_family, ai_ptr->ai_socktype,
                     ai_ptr->ai_protocol);
          if (s < 0) {
              if (ctx->debug) {
500c015a   Peter M. Groen   Setting up workin...
578
579
580
                  perror("socket");
              }
              continue;
5735b406   Peter M. Groen   Revert "Setting u...
581
          } else {
500c015a   Peter M. Groen   Setting up workin...
582
              int enable = 1;
5735b406   Peter M. Groen   Revert "Setting u...
583
584
585
              rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
                              (void *)&enable, sizeof (enable));
              if (rc != 0) {
500c015a   Peter M. Groen   Setting up workin...
586
                  close(s);
5735b406   Peter M. Groen   Revert "Setting u...
587
                  if (ctx->debug) {
500c015a   Peter M. Groen   Setting up workin...
588
589
590
591
592
593
594
                      perror("setsockopt");
                  }
                  continue;
              }
          }
  
          rc = bind(s, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
5735b406   Peter M. Groen   Revert "Setting u...
595
          if (rc != 0) {
500c015a   Peter M. Groen   Setting up workin...
596
              close(s);
5735b406   Peter M. Groen   Revert "Setting u...
597
              if (ctx->debug) {
500c015a   Peter M. Groen   Setting up workin...
598
599
600
601
602
603
                  perror("bind");
              }
              continue;
          }
  
          rc = listen(s, nb_connection);
5735b406   Peter M. Groen   Revert "Setting u...
604
          if (rc != 0) {
500c015a   Peter M. Groen   Setting up workin...
605
              close(s);
5735b406   Peter M. Groen   Revert "Setting u...
606
              if (ctx->debug) {
500c015a   Peter M. Groen   Setting up workin...
607
608
609
610
611
612
613
614
615
616
                  perror("listen");
              }
              continue;
          }
  
          new_s = s;
          break;
      }
      freeaddrinfo(ai_list);
  
5735b406   Peter M. Groen   Revert "Setting u...
617
      if (new_s < 0) {
500c015a   Peter M. Groen   Setting up workin...
618
619
620
621
622
623
624
625
626
627
628
          return -1;
      }
  
      return new_s;
  }
  
  int modbus_tcp_accept(modbus_t *ctx, int *s)
  {
      struct sockaddr_in addr;
      socklen_t addrlen;
  
5735b406   Peter M. Groen   Revert "Setting u...
629
      if (ctx == NULL) {
500c015a   Peter M. Groen   Setting up workin...
630
631
632
633
634
635
636
637
638
639
640
641
          errno = EINVAL;
          return -1;
      }
  
      addrlen = sizeof(addr);
  #ifdef HAVE_ACCEPT4
      /* Inherit socket flags and use accept4 call */
      ctx->s = accept4(*s, (struct sockaddr *)&addr, &addrlen, SOCK_CLOEXEC);
  #else
      ctx->s = accept(*s, (struct sockaddr *)&addr, &addrlen);
  #endif
  
5735b406   Peter M. Groen   Revert "Setting u...
642
      if (ctx->s == -1) {
500c015a   Peter M. Groen   Setting up workin...
643
644
645
646
647
          close(*s);
          *s = -1;
          return -1;
      }
  
5735b406   Peter M. Groen   Revert "Setting u...
648
      if (ctx->debug) {
500c015a   Peter M. Groen   Setting up workin...
649
650
651
652
653
654
655
          printf("The client connection from %s is accepted\n",
                 inet_ntoa(addr.sin_addr));
      }
  
      return ctx->s;
  }
  
5735b406   Peter M. Groen   Revert "Setting u...
656
  int modbus_tcp_pi_accept(modbus_t *ctx, int *s)
500c015a   Peter M. Groen   Setting up workin...
657
658
659
660
  {
      struct sockaddr_storage addr;
      socklen_t addrlen;
  
5735b406   Peter M. Groen   Revert "Setting u...
661
      if (ctx == NULL) {
500c015a   Peter M. Groen   Setting up workin...
662
663
664
665
666
667
668
669
670
671
672
          errno = EINVAL;
          return -1;
      }
  
      addrlen = sizeof(addr);
  #ifdef HAVE_ACCEPT4
      /* Inherit socket flags and use accept4 call */
      ctx->s = accept4(*s, (struct sockaddr *)&addr, &addrlen, SOCK_CLOEXEC);
  #else
      ctx->s = accept(*s, (struct sockaddr *)&addr, &addrlen);
  #endif
5735b406   Peter M. Groen   Revert "Setting u...
673
      if (ctx->s == -1) {
500c015a   Peter M. Groen   Setting up workin...
674
675
676
677
          close(*s);
          *s = -1;
      }
  
5735b406   Peter M. Groen   Revert "Setting u...
678
      if (ctx->debug) {
500c015a   Peter M. Groen   Setting up workin...
679
680
681
682
683
684
685
686
687
          printf("The client connection is accepted.\n");
      }
  
      return ctx->s;
  }
  
  static int _modbus_tcp_select(modbus_t *ctx, fd_set *rset, struct timeval *tv, int length_to_read)
  {
      int s_rc;
5735b406   Peter M. Groen   Revert "Setting u...
688
689
690
      while ((s_rc = select(ctx->s+1, rset, NULL, NULL, tv)) == -1) {
          if (errno == EINTR) {
              if (ctx->debug) {
500c015a   Peter M. Groen   Setting up workin...
691
692
693
694
695
                  fprintf(stderr, "A non blocked signal was caught\n");
              }
              /* Necessary after an error */
              FD_ZERO(rset);
              FD_SET(ctx->s, rset);
5735b406   Peter M. Groen   Revert "Setting u...
696
          } else {
500c015a   Peter M. Groen   Setting up workin...
697
698
699
700
              return -1;
          }
      }
  
5735b406   Peter M. Groen   Revert "Setting u...
701
      if (s_rc == 0) {
500c015a   Peter M. Groen   Setting up workin...
702
703
704
705
706
707
708
          errno = ETIMEDOUT;
          return -1;
      }
  
      return s_rc;
  }
  
5735b406   Peter M. Groen   Revert "Setting u...
709
  static void _modbus_tcp_free(modbus_t *ctx) {
500c015a   Peter M. Groen   Setting up workin...
710
711
712
713
      free(ctx->backend_data);
      free(ctx);
  }
  
5735b406   Peter M. Groen   Revert "Setting u...
714
  const modbus_backend_t _modbus_tcp_backend = {
500c015a   Peter M. Groen   Setting up workin...
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
      _MODBUS_BACKEND_TYPE_TCP,
      _MODBUS_TCP_HEADER_LENGTH,
      _MODBUS_TCP_CHECKSUM_LENGTH,
      MODBUS_TCP_MAX_ADU_LENGTH,
      _modbus_set_slave,
      _modbus_tcp_build_request_basis,
      _modbus_tcp_build_response_basis,
      _modbus_tcp_prepare_response_tid,
      _modbus_tcp_send_msg_pre,
      _modbus_tcp_send,
      _modbus_tcp_receive,
      _modbus_tcp_recv,
      _modbus_tcp_check_integrity,
      _modbus_tcp_pre_check_confirmation,
      _modbus_tcp_connect,
      _modbus_tcp_close,
      _modbus_tcp_flush,
      _modbus_tcp_select,
      _modbus_tcp_free
  };
  
  
5735b406   Peter M. Groen   Revert "Setting u...
737
  const modbus_backend_t _modbus_tcp_pi_backend = {
500c015a   Peter M. Groen   Setting up workin...
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
      _MODBUS_BACKEND_TYPE_TCP,
      _MODBUS_TCP_HEADER_LENGTH,
      _MODBUS_TCP_CHECKSUM_LENGTH,
      MODBUS_TCP_MAX_ADU_LENGTH,
      _modbus_set_slave,
      _modbus_tcp_build_request_basis,
      _modbus_tcp_build_response_basis,
      _modbus_tcp_prepare_response_tid,
      _modbus_tcp_send_msg_pre,
      _modbus_tcp_send,
      _modbus_tcp_receive,
      _modbus_tcp_recv,
      _modbus_tcp_check_integrity,
      _modbus_tcp_pre_check_confirmation,
      _modbus_tcp_pi_connect,
      _modbus_tcp_close,
      _modbus_tcp_flush,
      _modbus_tcp_select,
      _modbus_tcp_free
  };
  
5735b406   Peter M. Groen   Revert "Setting u...
759
  modbus_t* modbus_new_tcp(const char *ip, int port)
500c015a   Peter M. Groen   Setting up workin...
760
761
762
763
764
765
  {
      modbus_t *ctx;
      modbus_tcp_t *ctx_tcp;
      size_t dest_size;
      size_t ret_size;
  
5735b406   Peter M. Groen   Revert "Setting u...
766
767
768
769
770
771
772
773
774
775
776
777
778
779
  #if defined(OS_BSD)
      /* MSG_NOSIGNAL is unsupported on *BSD so we install an ignore
         handler for SIGPIPE. */
      struct sigaction sa;
  
      sa.sa_handler = SIG_IGN;
      if (sigaction(SIGPIPE, &sa, NULL) < 0) {
          /* The debug flag can't be set here... */
          fprintf(stderr, "Coud not install SIGPIPE handler.\n");
          return NULL;
      }
  #endif
  
      ctx = (modbus_t *)malloc(sizeof(modbus_t));
500c015a   Peter M. Groen   Setting up workin...
780
781
782
783
784
785
786
      _modbus_init_common(ctx);
  
      /* Could be changed after to reach a remote serial Modbus device */
      ctx->slave = MODBUS_TCP_SLAVE;
  
      ctx->backend = &_modbus_tcp_backend;
  
5735b406   Peter M. Groen   Revert "Setting u...
787
      ctx->backend_data = (modbus_tcp_t *)malloc(sizeof(modbus_tcp_t));
500c015a   Peter M. Groen   Setting up workin...
788
789
      ctx_tcp = (modbus_tcp_t *)ctx->backend_data;
  
5735b406   Peter M. Groen   Revert "Setting u...
790
      if (ip != NULL) {
500c015a   Peter M. Groen   Setting up workin...
791
792
          dest_size = sizeof(char) * 16;
          ret_size = strlcpy(ctx_tcp->ip, ip, dest_size);
5735b406   Peter M. Groen   Revert "Setting u...
793
          if (ret_size == 0) {
500c015a   Peter M. Groen   Setting up workin...
794
795
796
797
798
799
              fprintf(stderr, "The IP string is empty\n");
              modbus_free(ctx);
              errno = EINVAL;
              return NULL;
          }
  
5735b406   Peter M. Groen   Revert "Setting u...
800
          if (ret_size >= dest_size) {
500c015a   Peter M. Groen   Setting up workin...
801
802
803
804
805
              fprintf(stderr, "The IP string has been truncated\n");
              modbus_free(ctx);
              errno = EINVAL;
              return NULL;
          }
5735b406   Peter M. Groen   Revert "Setting u...
806
      } else {
500c015a   Peter M. Groen   Setting up workin...
807
808
809
810
811
812
813
814
815
          ctx_tcp->ip[0] = '0';
      }
      ctx_tcp->port = port;
      ctx_tcp->t_id = 0;
  
      return ctx;
  }
  
  
5735b406   Peter M. Groen   Revert "Setting u...
816
  modbus_t* modbus_new_tcp_pi(const char *node, const char *service)
500c015a   Peter M. Groen   Setting up workin...
817
  {
5735b406   Peter M. Groen   Revert "Setting u...
818
      modbus_t *ctx;
500c015a   Peter M. Groen   Setting up workin...
819
      modbus_tcp_pi_t *ctx_tcp_pi;
5735b406   Peter M. Groen   Revert "Setting u...
820
821
      size_t dest_size;
      size_t ret_size;
500c015a   Peter M. Groen   Setting up workin...
822
823
824
825
826
827
828
829
830
831
832
833
  
      ctx = (modbus_t *)malloc(sizeof(modbus_t));
      _modbus_init_common(ctx);
  
      /* Could be changed after to reach a remote serial Modbus device */
      ctx->slave = MODBUS_TCP_SLAVE;
  
      ctx->backend = &_modbus_tcp_pi_backend;
  
      ctx->backend_data = (modbus_tcp_pi_t *)malloc(sizeof(modbus_tcp_pi_t));
      ctx_tcp_pi = (modbus_tcp_pi_t *)ctx->backend_data;
  
5735b406   Peter M. Groen   Revert "Setting u...
834
      if (node == NULL) {
500c015a   Peter M. Groen   Setting up workin...
835
836
          /* The node argument can be empty to indicate any hosts */
          ctx_tcp_pi->node[0] = 0;
5735b406   Peter M. Groen   Revert "Setting u...
837
      } else {
500c015a   Peter M. Groen   Setting up workin...
838
839
          dest_size = sizeof(char) * _MODBUS_TCP_PI_NODE_LENGTH;
          ret_size = strlcpy(ctx_tcp_pi->node, node, dest_size);
5735b406   Peter M. Groen   Revert "Setting u...
840
          if (ret_size == 0) {
500c015a   Peter M. Groen   Setting up workin...
841
842
843
844
845
846
              fprintf(stderr, "The node string is empty\n");
              modbus_free(ctx);
              errno = EINVAL;
              return NULL;
          }
  
5735b406   Peter M. Groen   Revert "Setting u...
847
          if (ret_size >= dest_size) {
500c015a   Peter M. Groen   Setting up workin...
848
849
850
851
852
853
854
              fprintf(stderr, "The node string has been truncated\n");
              modbus_free(ctx);
              errno = EINVAL;
              return NULL;
          }
      }
  
5735b406   Peter M. Groen   Revert "Setting u...
855
      if (service != NULL) {
500c015a   Peter M. Groen   Setting up workin...
856
857
          dest_size = sizeof(char) * _MODBUS_TCP_PI_SERVICE_LENGTH;
          ret_size = strlcpy(ctx_tcp_pi->service, service, dest_size);
5735b406   Peter M. Groen   Revert "Setting u...
858
      } else {
500c015a   Peter M. Groen   Setting up workin...
859
860
861
862
          /* Empty service is not allowed, error catched below. */
          ret_size = 0;
      }
  
5735b406   Peter M. Groen   Revert "Setting u...
863
      if (ret_size == 0) {
500c015a   Peter M. Groen   Setting up workin...
864
865
866
867
868
869
          fprintf(stderr, "The service string is empty\n");
          modbus_free(ctx);
          errno = EINVAL;
          return NULL;
      }
  
5735b406   Peter M. Groen   Revert "Setting u...
870
      if (ret_size >= dest_size) {
500c015a   Peter M. Groen   Setting up workin...
871
872
873
874
875
876
877
878
879
880
          fprintf(stderr, "The service string has been truncated\n");
          modbus_free(ctx);
          errno = EINVAL;
          return NULL;
      }
  
      ctx_tcp_pi->t_id = 0;
  
      return ctx;
  }