modbus_client_transport.c
Go to the documentation of this file.
1 /**
2  * @file modbus_client_transport.c
3  * @brief Transport protocol abstraction layer
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL MODBUS_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "modbus/modbus_client.h"
38 #include "debug.h"
39 
40 //Check TCP/IP stack configuration
41 #if (MODBUS_CLIENT_SUPPORT == ENABLED)
42 
43 
44 /**
45  * @brief Open network connection
46  * @param[in] context Pointer to the Modbus/TCP client context
47  * @return Error code
48  **/
49 
51 {
52  error_t error;
53 
54  //Open a TCP socket
55  context->socket = socketOpenEx(context->netContext, SOCKET_TYPE_STREAM,
57  //Failed to open socket?
58  if(context->socket == NULL)
59  return ERROR_OPEN_FAILED;
60 
61  //Associate the socket with the relevant interface
62  error = socketBindToInterface(context->socket, context->interface);
63  //Any error to report?
64  if(error)
65  return error;
66 
67  //Set timeout
68  error = socketSetTimeout(context->socket, context->timeout);
69  //Any error to report?
70  if(error)
71  return error;
72 
73 #if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
74  //TLS-secured connection?
75  if(context->tlsInitCallback != NULL)
76  {
77  //Allocate TLS context
78  context->tlsContext = tlsInit();
79  //Failed to allocate TLS context?
80  if(context->tlsContext == NULL)
81  return ERROR_OPEN_FAILED;
82 
83  //Devices must provide TLS v1.2 or better
84  error = tlsSetVersion(context->tlsContext, TLS_VERSION_1_2,
86  //Any error to report?
87  if(error)
88  return error;
89 
90  //Select client operation mode
91  error = tlsSetConnectionEnd(context->tlsContext,
93  //Any error to report?
94  if(error)
95  return error;
96 
97  //Bind TLS to the relevant socket
98  error = tlsSetSocket(context->tlsContext, context->socket);
99  //Any error to report?
100  if(error)
101  return error;
102 
103  //Set TX and RX buffer size
104  error = tlsSetBufferSize(context->tlsContext,
106  //Any error to report?
107  if(error)
108  return error;
109 
110  //Restore TLS session
111  error = tlsRestoreSessionState(context->tlsContext, &context->tlsSession);
112  //Any error to report?
113  if(error)
114  return error;
115 
116  //Perform TLS related initialization
117  error = context->tlsInitCallback(context, context->tlsContext);
118  //Any error to report?
119  if(error)
120  return error;
121  }
122 #endif
123 
124  //Successful processing
125  return NO_ERROR;
126 }
127 
128 
129 /**
130  * @brief Establish network connection
131  * @param[in] context Pointer to the Modbus/TCP client context
132  * @param[in] serverIpAddr IP address of the Modbus/TCP server to connect to
133  * @param[in] serverPort TCP port number that will be used to establish the
134  * connection
135  * @return Error code
136  **/
137 
139  const IpAddr *serverIpAddr, uint16_t serverPort)
140 {
141  error_t error;
142 
143  //Establish TCP connection
144  error = socketConnect(context->socket, serverIpAddr, serverPort);
145  //Any error to report?
146  if(error)
147  return error;
148 
149 #if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
150  //TLS-secured connection?
151  if(context->tlsContext != NULL)
152  {
153  //Establish TLS connection
154  error = tlsConnect(context->tlsContext);
155  //Any error to report?
156  if(error)
157  return error;
158 
159  //At any time after the server has received the client Finished
160  //message, it may send a NewSessionTicket message (refer to RFC 8446,
161  //section 4.6.1)
162  context->tlsSessionSaved = FALSE;
163  }
164 #endif
165 
166  //Successful processing
167  return NO_ERROR;
168 }
169 
170 
171 /**
172  * @brief Shutdown network connection
173  * @param[in] context Pointer to the Modbus/TCP client context
174  * @return Error code
175  **/
176 
178 {
179  error_t error;
180 
181  //Initialize status code
182  error = NO_ERROR;
183 
184 #if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
185  //Valid TLS context?
186  if(context->tlsContext != NULL)
187  {
188  //Shutdown TLS session
189  error = tlsShutdown(context->tlsContext);
190  }
191 #endif
192 
193  //Check status code
194  if(!error)
195  {
196  //Valid TCP socket?
197  if(context->socket != NULL)
198  {
199  //Shutdown TCP connection
200  error = socketShutdown(context->socket, SOCKET_SD_BOTH);
201  }
202  }
203 
204  //Return status code
205  return error;
206 }
207 
208 
209 /**
210  * @brief Close network connection
211  * @param[in] context Pointer to the Modbus/TCP client context
212  **/
213 
215 {
216 #if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
217  //Release TLS context
218  if(context->tlsContext != NULL)
219  {
220  tlsFree(context->tlsContext);
221  context->tlsContext = NULL;
222  }
223 #endif
224 
225  //Close TCP connection
226  if(context->socket != NULL)
227  {
228  socketClose(context->socket);
229  context->socket = NULL;
230  }
231 }
232 
233 
234 /**
235  * @brief Send data using the relevant transport protocol
236  * @param[in] context Pointer to the Modbus/TCP client context
237  * @param[in] data Pointer to a buffer containing the data to be transmitted
238  * @param[in] length Number of bytes to be transmitted
239  * @param[out] written Actual number of bytes written (optional parameter)
240  * @param[in] flags Set of flags that influences the behavior of this function
241  * @return Error code
242  **/
243 
245  size_t length, size_t *written, uint_t flags)
246 {
247  error_t error;
248 
249 #if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
250  //TLS-secured connection?
251  if(context->tlsContext != NULL)
252  {
253  //Send TLS-encrypted data
254  error = tlsWrite(context->tlsContext, data, length, written, flags);
255  }
256  else
257 #endif
258  {
259  //Transmit data
260  error = socketSend(context->socket, data, length, written, flags);
261  }
262 
263  //Return status code
264  return error;
265 }
266 
267 
268 /**
269  * @brief Receive data using the relevant transport protocol
270  * @param[in] context Pointer to the Modbus/TCP client context
271  * @param[out] data Buffer into which received data will be placed
272  * @param[in] size Maximum number of bytes that can be received
273  * @param[out] received Number of bytes that have been received
274  * @param[in] flags Set of flags that influences the behavior of this function
275  * @return Error code
276  **/
277 
279  size_t size, size_t *received, uint_t flags)
280 {
281  error_t error;
282 
283 #if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
284  //TLS-secured connection?
285  if(context->tlsContext != NULL)
286  {
287  //Receive TLS-encrypted data
288  error = tlsRead(context->tlsContext, data, size, received, flags);
289 
290  //Check status code
291  if(!error)
292  {
293  //At any time after the server has received the client Finished
294  //message, it may send a NewSessionTicket message (refer to RFC 8446,
295  //section 4.6.1)
296  if(!context->tlsSessionSaved)
297  {
298  //Save TLS session
299  error = tlsSaveSessionState(context->tlsContext,
300  &context->tlsSession);
301 
302  //Update flag
303  context->tlsSessionSaved = TRUE;
304  }
305  }
306  }
307  else
308 #endif
309  {
310  //Receive data
311  error = socketReceive(context->socket, data, size, received, flags);
312  }
313 
314  //Return status code
315  return error;
316 }
317 
318 #endif
error_t socketSend(Socket *socket, const void *data, size_t length, size_t *written, uint_t flags)
Send data to a connected socket.
Definition: socket.c:1514
TlsContext * tlsInit(void)
TLS context initialization.
Definition: tls.c:68
Modbus/TCP client.
error_t tlsSetConnectionEnd(TlsContext *context, TlsConnectionEnd entity)
Set operation mode (client or server)
Definition: tls.c:371
error_t modbusClientSendData(ModbusClientContext *context, const void *data, size_t length, size_t *written, uint_t flags)
Send data using the relevant transport protocol.
IP network address.
Definition: ip.h:90
#define MODBUS_CLIENT_TLS_TX_BUFFER_SIZE
Definition: modbus_client.h:61
error_t modbusClientOpenConnection(ModbusClientContext *context)
Open network connection.
#define TRUE
Definition: os_port.h:50
uint8_t data[]
Definition: ethernet.h:224
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:2094
#define ModbusClientContext
Definition: modbus_client.h:86
@ SOCKET_TYPE_STREAM
Definition: socket.h:92
error_t modbusClientEstablishConnection(ModbusClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Establish network connection.
error_t tlsRestoreSessionState(TlsContext *context, const TlsSessionState *session)
Restore TLS session.
Definition: tls.c:3073
@ ERROR_OPEN_FAILED
Definition: error.h:75
error_t tlsSetVersion(TlsContext *context, uint16_t versionMin, uint16_t versionMax)
Set minimum and maximum versions permitted.
Definition: tls.c:302
error_t tlsShutdown(TlsContext *context)
Gracefully close TLS session.
Definition: tls.c:2621
#define FALSE
Definition: os_port.h:46
#define tlsSetSocket(context, socket)
Definition: tls.h:1008
void modbusClientCloseConnection(ModbusClientContext *context)
Close network connection.
error_t
Error codes.
Definition: error.h:43
error_t socketReceive(Socket *socket, void *data, size_t size, size_t *received, uint_t flags)
Receive data from a connected socket.
Definition: socket.c:1724
#define TLS_VERSION_1_2
Definition: tls.h:97
#define TLS_VERSION_1_3
Definition: tls.h:98
error_t socketConnect(Socket *socket, const IpAddr *remoteIpAddr, uint16_t remotePort)
Establish a connection to a specified socket.
Definition: socket.c:1377
error_t socketShutdown(Socket *socket, uint_t how)
Disable reception, transmission, or both.
Definition: socket.c:2052
error_t tlsSaveSessionState(const TlsContext *context, TlsSessionState *session)
Save TLS session.
Definition: tls.c:3004
uint8_t length
Definition: tcp.h:375
#define MODBUS_CLIENT_TLS_RX_BUFFER_SIZE
Definition: modbus_client.h:68
error_t tlsRead(TlsContext *context, void *data, size_t size, size_t *received, uint_t flags)
Receive application data from a the remote host using TLS.
Definition: tls.c:2286
#define socketBindToInterface
Definition: net_legacy.h:193
Transport protocol abstraction layer.
error_t tlsSetBufferSize(TlsContext *context, size_t txBufferSize, size_t rxBufferSize)
Set TLS buffer size.
Definition: tls.c:543
Socket * socketOpenEx(NetContext *context, uint_t type, uint_t protocol)
Create a socket.
Definition: socket.c:146
@ TLS_CONNECTION_END_CLIENT
Definition: tls.h:1050
error_t tlsWrite(TlsContext *context, const void *data, size_t length, size_t *written, uint_t flags)
Send application data to the remote host using TLS.
Definition: tls.c:2145
void tlsFree(TlsContext *context)
Release TLS context.
Definition: tls.c:2816
error_t modbusClientReceiveData(ModbusClientContext *context, void *data, size_t size, size_t *received, uint_t flags)
Receive data using the relevant transport protocol.
error_t modbusClientShutdownConnection(ModbusClientContext *context)
Shutdown network connection.
uint8_t flags
Definition: tcp.h:358
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
@ SOCKET_SD_BOTH
Definition: socket.h:161
@ SOCKET_IP_PROTO_TCP
Definition: socket.h:107
error_t socketSetTimeout(Socket *socket, systime_t timeout)
Set timeout value for blocking operations.
Definition: socket.c:169
error_t tlsConnect(TlsContext *context)
Initiate the TLS handshake.
Definition: tls.c:1805
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.