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-2019 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 1.9.2
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
56  //Failed to open socket?
57  if(context->socket == NULL)
58  return ERROR_OPEN_FAILED;
59 
60  //Associate the socket with the relevant interface
61  error = socketBindToInterface(context->socket, context->interface);
62  //Any error to report?
63  if(error)
64  return error;
65 
66  //Set timeout
67  error = socketSetTimeout(context->socket, context->timeout);
68  //Any error to report?
69  if(error)
70  return error;
71 
72  //Successful processing
73  return NO_ERROR;
74 }
75 
76 
77 /**
78  * @brief Establish network connection
79  * @param[in] context Pointer to the SMTP client context
80  * @param[in] serverIpAddr IP address of the SMTP server to connect to
81  * @param[in] serverPort TCP port number that will be used to establish the
82  * connection
83  * @return Error code
84  **/
85 
87  const IpAddr *serverIpAddr, uint16_t serverPort)
88 {
89  //Establish TCP connection
90  return socketConnect(context->socket, serverIpAddr, serverPort);
91 }
92 
93 
94 /**
95  * @brief Open secure connection
96  * @param[in] context Pointer to the SMTP client context
97  * @return Error code
98  **/
99 
101 {
102 #if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
103  error_t error;
104 
105  //Allocate TLS context
106  context->tlsContext = tlsInit();
107  //Failed to allocate TLS context?
108  if(context->tlsContext == NULL)
109  return ERROR_OPEN_FAILED;
110 
111  //Select client operation mode
112  error = tlsSetConnectionEnd(context->tlsContext, TLS_CONNECTION_END_CLIENT);
113  //Any error to report?
114  if(error)
115  return error;
116 
117  //Bind TLS to the relevant socket
118  error = tlsSetSocket(context->tlsContext, context->socket);
119  //Any error to report?
120  if(error)
121  return error;
122 
123  //Set TX and RX buffer size
124  error = tlsSetBufferSize(context->tlsContext,
126  //Any error to report?
127  if(error)
128  return error;
129 
130  //Restore TLS session
131  error = tlsRestoreSessionState(context->tlsContext, &context->tlsSession);
132  //Any error to report?
133  if(error)
134  return error;
135 
136  //Invoke user-defined callback, if any
137  if(context->tlsInitCallback != NULL)
138  {
139  //Perform TLS related initialization
140  error = context->tlsInitCallback(context, context->tlsContext);
141  //Any error to report?
142  if(error)
143  return error;
144  }
145 
146  //Successful processing
147  return NO_ERROR;
148 #else
149  //Not implemented
150  return ERROR_NOT_IMPLEMENTED;
151 #endif
152 }
153 
154 
155 /**
156  * @brief Establish secure connection
157  * @param[in] context Pointer to the SMTP client context
158  * @return Error code
159  **/
160 
162 {
163 #if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
164  error_t error;
165 
166  //Establish TLS connection
167  error = tlsConnect(context->tlsContext);
168 
169  //Check status code
170  if(!error)
171  {
172  //Save TLS session
173  error = tlsSaveSessionState(context->tlsContext, &context->tlsSession);
174  }
175 
176  //Return status code
177  return error;
178 #else
179  //Not implemented
180  return ERROR_NOT_IMPLEMENTED;
181 #endif
182 }
183 
184 
185 /**
186  * @brief Shutdown network connection
187  * @param[in] context Pointer to the SMTP client context
188  * @return Error code
189  **/
190 
192 {
193  error_t error;
194 
195  //Initialize status code
196  error = NO_ERROR;
197 
198 #if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
199  //Valid TLS context?
200  if(context->tlsContext != NULL)
201  {
202  //Shutdown TLS session
203  error = tlsShutdown(context->tlsContext);
204  }
205 #endif
206 
207  //Check status code
208  if(!error)
209  {
210  //Valid TCP socket?
211  if(context->socket != NULL)
212  {
213  //Shutdown TCP connection
214  error = socketShutdown(context->socket, SOCKET_SD_BOTH);
215  }
216  }
217 
218  //Return status code
219  return error;
220 }
221 
222 
223 /**
224  * @brief Close network connection
225  * @param[in] context Pointer to the SMTP client context
226  **/
227 
229 {
230 #if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
231  //Release TLS context
232  if(context->tlsContext != NULL)
233  {
234  tlsFree(context->tlsContext);
235  context->tlsContext = NULL;
236  }
237 #endif
238 
239  //Close TCP connection
240  if(context->socket != NULL)
241  {
242  socketClose(context->socket);
243  context->socket = NULL;
244  }
245 }
246 
247 
248 /**
249  * @brief Send data using the relevant transport protocol
250  * @param[in] context Pointer to the SMTP client context
251  * @param[in] data Pointer to a buffer containing the data to be transmitted
252  * @param[in] length Number of bytes to be transmitted
253  * @param[out] written Actual number of bytes written (optional parameter)
254  * @param[in] flags Set of flags that influences the behavior of this function
255  * @return Error code
256  **/
257 
259  size_t length, size_t *written, uint_t flags)
260 {
261  error_t error;
262 
263 #if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
264  //TLS-secured connection?
265  if(context->tlsContext != NULL)
266  {
267  //Send TLS-encrypted data
268  error = tlsWrite(context->tlsContext, data, length, written, flags);
269  }
270  else
271 #endif
272  {
273  //Transmit data
274  error = socketSend(context->socket, data, length, written, flags);
275  }
276 
277  //Return status code
278  return error;
279 }
280 
281 
282 /**
283  * @brief Receive data using the relevant transport protocol
284  * @param[in] context Pointer to the SMTP client context
285  * @param[out] data Buffer into which received data will be placed
286  * @param[in] size Maximum number of bytes that can be received
287  * @param[out] received Number of bytes that have been received
288  * @param[in] flags Set of flags that influences the behavior of this function
289  * @return Error code
290  **/
291 
293  size_t size, size_t *received, uint_t flags)
294 {
295  error_t error;
296 
297 #if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
298  //TLS-secured connection?
299  if(context->tlsContext != NULL)
300  {
301  //Receive TLS-encrypted data
302  error = tlsRead(context->tlsContext, data, size, received, flags);
303  }
304  else
305 #endif
306  {
307  //Receive data
308  error = socketReceive(context->socket, data, size, received, flags);
309  }
310 
311  //Return status code
312  return error;
313 }
314 
315 #endif
void smtpClientCloseConnection(SmtpClientContext *context)
Close network connection.
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:1624
error_t tlsSetBufferSize(TlsContext *context, size_t txBufferSize, size_t rxBufferSize)
Set TLS buffer size.
Definition: tls.c:481
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:586
error_t tlsRestoreSessionState(TlsContext *context, const TlsSessionState *session)
Restore TLS session.
Definition: tls.c:2546
uint8_t flags
Definition: tcp.h:314
TCP/IP stack core.
Debugging facilities.
error_t tlsConnect(TlsContext *context)
Initiate the TLS handshake.
Definition: tls.c:1533
error_t tlsSetConnectionEnd(TlsContext *context, TlsConnectionEnd entity)
Set operation mode (client or server)
Definition: tls.c:312
Transport protocol abstraction layer.
IP network address.
Definition: ip.h:59
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:1759
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:799
error_t tlsSaveSessionState(const TlsContext *context, TlsSessionState *session)
Save TLS session.
Definition: tls.c:2388
error_t smtpClientEstablishConnection(SmtpClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Establish network connection.
error_t socketSetTimeout(Socket *socket, systime_t timeout)
Set timeout value for blocking operations.
Definition: socket.c:218
#define tlsSetSocket(context, socket)
Definition: tls.h:822
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 tlsShutdown(TlsContext *context)
Gracefully close TLS session.
Definition: tls.c:2073
void tlsFree(TlsContext *context)
Release TLS context.
Definition: tls.c:2233
SMTP client (Simple Mail Transfer Protocol)
error_t socketShutdown(Socket *socket, uint_t how)
Disable reception, transmission, or both.
Definition: socket.c:762
#define SmtpClientContext
Definition: smtp_client.h:156
Success.
Definition: error.h:44
error_t socketConnect(Socket *socket, const IpAddr *remoteIpAddr, uint16_t remotePort)
Establish a connection to a specified socket.
Definition: socket.c:359
error_t
Error codes.
Definition: error.h:42
unsigned int uint_t
Definition: compiler_port.h:45
Socket * socketOpen(uint_t type, uint_t protocol)
Create a socket (UDP or TCP)
Definition: socket.c:94
uint8_t data[]
Definition: dtls_misc.h:169
error_t smtpClientOpenSecureConnection(SmtpClientContext *context)
Open secure connection.
error_t smtpClientEstablishSecureConnection(SmtpClientContext *context)
Establish secure connection.
TlsContext * tlsInit(void)
TLS context initialization.
Definition: tls.c:65
error_t smtpClientOpenConnection(SmtpClientContext *context)
Open network connection.
error_t smtpClientSendData(SmtpClientContext *context, const void *data, size_t length, size_t *written, uint_t flags)
Send data using the relevant transport protocol.
error_t smtpClientShutdownConnection(SmtpClientContext *context)
Shutdown network connection.
#define SMTP_CLIENT_TLS_RX_BUFFER_SIZE
Definition: smtp_client.h:102
uint8_t length
Definition: dtls_misc.h:142
#define SMTP_CLIENT_TLS_TX_BUFFER_SIZE
Definition: smtp_client.h:95
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:492
error_t socketBindToInterface(Socket *socket, NetInterface *interface)
Bind a socket to a particular network interface.
Definition: socket.c:311