smtp_client_transport.c
Go to the documentation of this file.
1 /**
2  * @file smtp_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.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL SMTP_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "smtp/smtp_client.h"
38 #include "debug.h"
39 
40 //Check TCP/IP stack configuration
41 #if (SMTP_CLIENT_SUPPORT == ENABLED)
42 
43 
44 /**
45  * @brief Open network connection
46  * @param[in] context Pointer to the SMTP 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  //Successful processing
74  return NO_ERROR;
75 }
76 
77 
78 /**
79  * @brief Establish network connection
80  * @param[in] context Pointer to the SMTP client context
81  * @param[in] serverIpAddr IP address of the SMTP server to connect to
82  * @param[in] serverPort TCP port number that will be used to establish the
83  * connection
84  * @return Error code
85  **/
86 
88  const IpAddr *serverIpAddr, uint16_t serverPort)
89 {
90  //Establish TCP connection
91  return socketConnect(context->socket, serverIpAddr, serverPort);
92 }
93 
94 
95 /**
96  * @brief Open secure connection
97  * @param[in] context Pointer to the SMTP client context
98  * @return Error code
99  **/
100 
102 {
103 #if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
104  error_t error;
105 
106  //Allocate TLS context
107  context->tlsContext = tlsInit();
108  //Failed to allocate TLS context?
109  if(context->tlsContext == NULL)
110  return ERROR_OPEN_FAILED;
111 
112  //Select client operation mode
113  error = tlsSetConnectionEnd(context->tlsContext, TLS_CONNECTION_END_CLIENT);
114  //Any error to report?
115  if(error)
116  return error;
117 
118  //Bind TLS to the relevant socket
119  error = tlsSetSocket(context->tlsContext, context->socket);
120  //Any error to report?
121  if(error)
122  return error;
123 
124  //Set TX and RX buffer size
125  error = tlsSetBufferSize(context->tlsContext,
127  //Any error to report?
128  if(error)
129  return error;
130 
131  //Restore TLS session
132  error = tlsRestoreSessionState(context->tlsContext, &context->tlsSession);
133  //Any error to report?
134  if(error)
135  return error;
136 
137  //Invoke user-defined callback, if any
138  if(context->tlsInitCallback != NULL)
139  {
140  //Perform TLS related initialization
141  error = context->tlsInitCallback(context, context->tlsContext);
142  //Any error to report?
143  if(error)
144  return error;
145  }
146 
147  //Successful processing
148  return NO_ERROR;
149 #else
150  //Not implemented
151  return ERROR_NOT_IMPLEMENTED;
152 #endif
153 }
154 
155 
156 /**
157  * @brief Establish secure connection
158  * @param[in] context Pointer to the SMTP client context
159  * @return Error code
160  **/
161 
163 {
164 #if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
165  error_t error;
166 
167  //Establish TLS connection
168  error = tlsConnect(context->tlsContext);
169 
170  //Check status code
171  if(!error)
172  {
173  //Save TLS session
174  error = tlsSaveSessionState(context->tlsContext, &context->tlsSession);
175  }
176 
177  //Return status code
178  return error;
179 #else
180  //Not implemented
181  return ERROR_NOT_IMPLEMENTED;
182 #endif
183 }
184 
185 
186 /**
187  * @brief Shutdown network connection
188  * @param[in] context Pointer to the SMTP client context
189  * @return Error code
190  **/
191 
193 {
194  error_t error;
195 
196  //Initialize status code
197  error = NO_ERROR;
198 
199 #if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
200  //Valid TLS context?
201  if(context->tlsContext != NULL)
202  {
203  //Shutdown TLS session
204  error = tlsShutdown(context->tlsContext);
205  }
206 #endif
207 
208  //Check status code
209  if(!error)
210  {
211  //Valid TCP socket?
212  if(context->socket != NULL)
213  {
214  //Shutdown TCP connection
215  error = socketShutdown(context->socket, SOCKET_SD_BOTH);
216  }
217  }
218 
219  //Return status code
220  return error;
221 }
222 
223 
224 /**
225  * @brief Close network connection
226  * @param[in] context Pointer to the SMTP client context
227  **/
228 
230 {
231 #if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
232  //Release TLS context
233  if(context->tlsContext != NULL)
234  {
235  tlsFree(context->tlsContext);
236  context->tlsContext = NULL;
237  }
238 #endif
239 
240  //Close TCP connection
241  if(context->socket != NULL)
242  {
243  socketClose(context->socket);
244  context->socket = NULL;
245  }
246 }
247 
248 
249 /**
250  * @brief Send data using the relevant transport protocol
251  * @param[in] context Pointer to the SMTP client context
252  * @param[in] data Pointer to a buffer containing the data to be transmitted
253  * @param[in] length Number of bytes to be transmitted
254  * @param[out] written Actual number of bytes written (optional parameter)
255  * @param[in] flags Set of flags that influences the behavior of this function
256  * @return Error code
257  **/
258 
260  size_t length, size_t *written, uint_t flags)
261 {
262  error_t error;
263 
264 #if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
265  //TLS-secured connection?
266  if(context->tlsContext != NULL)
267  {
268  //Send TLS-encrypted data
269  error = tlsWrite(context->tlsContext, data, length, written, flags);
270  }
271  else
272 #endif
273  {
274  //Transmit data
275  error = socketSend(context->socket, data, length, written, flags);
276  }
277 
278  //Return status code
279  return error;
280 }
281 
282 
283 /**
284  * @brief Receive data using the relevant transport protocol
285  * @param[in] context Pointer to the SMTP client context
286  * @param[out] data Buffer into which received data will be placed
287  * @param[in] size Maximum number of bytes that can be received
288  * @param[out] received Number of bytes that have been received
289  * @param[in] flags Set of flags that influences the behavior of this function
290  * @return Error code
291  **/
292 
294  size_t size, size_t *received, uint_t flags)
295 {
296  error_t error;
297 
298 #if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
299  //TLS-secured connection?
300  if(context->tlsContext != NULL)
301  {
302  //Receive TLS-encrypted data
303  error = tlsRead(context->tlsContext, data, size, received, flags);
304  }
305  else
306 #endif
307  {
308  //Receive data
309  error = socketReceive(context->socket, data, size, received, flags);
310  }
311 
312  //Return status code
313  return error;
314 }
315 
316 #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:67
error_t tlsSetConnectionEnd(TlsContext *context, TlsConnectionEnd entity)
Set operation mode (client or server)
Definition: tls.c:385
IP network address.
Definition: ip.h:90
@ ERROR_NOT_IMPLEMENTED
Definition: error.h:66
Transport protocol abstraction layer.
uint8_t data[]
Definition: ethernet.h:224
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:2094
@ SOCKET_TYPE_STREAM
Definition: socket.h:92
error_t tlsRestoreSessionState(TlsContext *context, const TlsSessionState *session)
Restore TLS session.
Definition: tls.c:3012
error_t smtpClientOpenSecureConnection(SmtpClientContext *context)
Open secure connection.
@ ERROR_OPEN_FAILED
Definition: error.h:75
error_t tlsShutdown(TlsContext *context)
Gracefully close TLS session.
Definition: tls.c:2603
#define tlsSetSocket(context, socket)
Definition: tls.h:970
SMTP client (Simple Mail Transfer Protocol)
#define SmtpClientContext
Definition: smtp_client.h:161
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
error_t smtpClientShutdownConnection(SmtpClientContext *context)
Shutdown network connection.
void smtpClientCloseConnection(SmtpClientContext *context)
Close network connection.
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
#define SMTP_CLIENT_TLS_RX_BUFFER_SIZE
Definition: smtp_client.h:102
error_t tlsSaveSessionState(const TlsContext *context, TlsSessionState *session)
Save TLS session.
Definition: tls.c:2943
uint8_t length
Definition: tcp.h:375
#define SMTP_CLIENT_TLS_TX_BUFFER_SIZE
Definition: smtp_client.h:95
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:2285
#define socketBindToInterface
Definition: net_legacy.h:193
error_t smtpClientEstablishConnection(SmtpClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Establish network connection.
error_t smtpClientOpenConnection(SmtpClientContext *context)
Open network connection.
error_t tlsSetBufferSize(TlsContext *context, size_t txBufferSize, size_t rxBufferSize)
Set TLS buffer size.
Definition: tls.c:557
error_t smtpClientReceiveData(SmtpClientContext *context, void *data, size_t size, size_t *received, uint_t flags)
Receive data using the relevant transport protocol.
error_t smtpClientEstablishSecureConnection(SmtpClientContext *context)
Establish secure connection.
Socket * socketOpenEx(NetContext *context, uint_t type, uint_t protocol)
Create a socket.
Definition: socket.c:146
@ TLS_CONNECTION_END_CLIENT
Definition: tls.h:1012
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:2148
void tlsFree(TlsContext *context)
Release TLS context.
Definition: tls.c:2765
uint8_t flags
Definition: tcp.h:358
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
error_t smtpClientSendData(SmtpClientContext *context, const void *data, size_t length, size_t *written, uint_t flags)
Send data using the relevant transport protocol.
@ 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:1819
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.