Sergey Pastor / 1

Dependents:   Nucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers smtp_client.h Source File

smtp_client.h

Go to the documentation of this file.
00001 /**
00002  * @file smtp_client.h
00003  * @brief SMTP client (Simple Mail Transfer Protocol)
00004  *
00005  * @section License
00006  *
00007  * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
00008  *
00009  * This file is part of CycloneTCP Open.
00010  *
00011  * This program is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU General Public License
00013  * as published by the Free Software Foundation; either version 2
00014  * of the License, or (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software Foundation,
00023  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00024  *
00025  * @author Oryx Embedded SARL (www.oryx-embedded.com)
00026  * @version 1.7.6
00027  **/
00028 
00029 #ifndef _SMTP_CLIENT_H
00030 #define _SMTP_CLIENT_H
00031 
00032 //Dependencies
00033 #include "core/socket.h"
00034 
00035 //SMTP client support
00036 #ifndef SMTP_CLIENT_SUPPORT
00037    #define SMTP_CLIENT_SUPPORT ENABLED
00038 #elif (SMTP_CLIENT_SUPPORT != ENABLED && SMTP_CLIENT_SUPPORT != DISABLED)
00039    #error SMTP_CLIENT_SUPPORT parameter is not valid
00040 #endif
00041 
00042 //Default timeout
00043 #ifndef SMTP_CLIENT_DEFAULT_TIMEOUT
00044    #define SMTP_CLIENT_DEFAULT_TIMEOUT 10000
00045 #elif (SMTP_CLIENT_DEFAULT_TIMEOUT < 1000)
00046    #error SMTP_CLIENT_DEFAULT_TIMEOUT parameter is not valid
00047 #endif
00048 
00049 //Maximum line length
00050 #ifndef SMTP_CLIENT_MAX_LINE_LENGTH
00051    #define SMTP_CLIENT_MAX_LINE_LENGTH 512
00052 #elif (SMTP_CLIENT_MAX_LINE_LENGTH < 64)
00053    #error SMTP_CLIENT_MAX_LINE_LENGTH parameter is not valid
00054 #endif
00055 
00056 //SMTP over SSL/TLS
00057 #ifndef SMTP_CLIENT_TLS_SUPPORT
00058    #define SMTP_CLIENT_TLS_SUPPORT DISABLED
00059 #elif (SMTP_CLIENT_TLS_SUPPORT != ENABLED && SMTP_CLIENT_TLS_SUPPORT != DISABLED)
00060    #error SMTP_CLIENT_TLS_SUPPORT parameter is not valid
00061 #endif
00062 
00063 //LOGIN authentication support
00064 #ifndef SMTP_CLIENT_LOGIN_AUTH_SUPPORT
00065    #define SMTP_CLIENT_LOGIN_AUTH_SUPPORT ENABLED
00066 #elif (SMTP_CLIENT_LOGIN_AUTH_SUPPORT != ENABLED && SMTP_CLIENT_LOGIN_AUTH_SUPPORT != DISABLED)
00067    #error SMTP_CLIENT_LOGIN_AUTH_SUPPORT parameter is not valid
00068 #endif
00069 
00070 //PLAIN authentication support
00071 #ifndef SMTP_CLIENT_PLAIN_AUTH_SUPPORT
00072    #define SMTP_CLIENT_PLAIN_AUTH_SUPPORT ENABLED
00073 #elif (SMTP_CLIENT_PLAIN_AUTH_SUPPORT != ENABLED && SMTP_CLIENT_PLAIN_AUTH_SUPPORT != DISABLED)
00074    #error SMTP_CLIENT_PLAIN_AUTH_SUPPORT parameter is not valid
00075 #endif
00076 
00077 //CRAM-MD5 authentication support
00078 #ifndef SMTP_CLIENT_CRAM_MD5_AUTH_SUPPORT
00079    #define SMTP_CLIENT_CRAM_MD5_AUTH_SUPPORT DISABLED
00080 #elif (SMTP_CLIENT_CRAM_MD5_AUTH_SUPPORT != ENABLED && SMTP_CLIENT_CRAM_MD5_AUTH_SUPPORT != DISABLED)
00081    #error SMTP_CLIENT_CRAM_MD5_AUTH_SUPPORT parameter is not valid
00082 #endif
00083 
00084 //SMTP over SSL/TLS supported?
00085 #if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
00086    #include "crypto.h"
00087    #include "tls.h"
00088 #endif
00089 
00090 //LOGIN or PLAIN authentication supported?
00091 #if (SMTP_CLIENT_LOGIN_AUTH_SUPPORT == ENABLED || SMTP_CLIENT_PLAIN_AUTH_SUPPORT == ENABLED)
00092    #include "crypto.h"
00093    #include "base64.h"
00094 #endif
00095 
00096 //CRAM-MD5 authentication supported?
00097 #if (SMTP_CLIENT_CRAM_MD5_AUTH_SUPPORT == ENABLED)
00098    #include "crypto.h"
00099    #include "base64.h"
00100    #include "hmac.h"
00101    #include "md5.h"
00102 #endif
00103 
00104 //SMTP port number
00105 #define SMTP_PORT 25
00106 //SMTPS port number (SMTP over SSL/TLS)
00107 #define SMTPS_PORT 465
00108 //SMTP message submission port number
00109 #define SMTP_SUBMISSION_PORT 587
00110 
00111 //Test macros for SMTP response codes
00112 #define SMTP_REPLY_CODE_2YZ(code) ((code) >= 200 && (code) < 300)
00113 #define SMTP_REPLY_CODE_3YZ(code) ((code) >= 300 && (code) < 400)
00114 #define SMTP_REPLY_CODE_4YZ(code) ((code) >= 400 && (code) < 500)
00115 #define SMTP_REPLY_CODE_5YZ(code) ((code) >= 500 && (code) < 600)
00116 
00117 
00118 /**
00119  * @brief Recipient type
00120  **/
00121 
00122 typedef enum
00123 {
00124    SMTP_RCPT_TYPE_TO  = 1,
00125    SMTP_RCPT_TYPE_CC  = 2,
00126    SMTP_RCPT_TYPE_BCC = 4,
00127 } SmtpRecipientType;
00128 
00129 
00130 /**
00131  * @brief Authentication information
00132  **/
00133 
00134 typedef struct
00135 {
00136    NetInterface *interface;
00137    const char_t *serverName;
00138    uint16_t serverPort;
00139    const char_t *userName;
00140    const char_t *password;
00141 #if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
00142    bool_t useTls;
00143    const PrngAlgo *prngAlgo;
00144    void *prngContext;
00145 #endif
00146 } SmtpAuthInfo;
00147 
00148 
00149 /**
00150  * @brief Mail address
00151  **/
00152 
00153 typedef struct
00154 {
00155    char_t *name;
00156    char_t *addr;
00157    uint_t type;
00158 } SmtpMailAddr;
00159 
00160 
00161 /**
00162  * @brief Mail contents
00163  **/
00164 
00165 typedef struct
00166 {
00167    SmtpMailAddr from;
00168    const SmtpMailAddr *recipients;
00169    uint_t recipientCount;
00170    char_t *dateTime;
00171    const char_t *subject;
00172    const char_t *body;
00173 } SmtpMail;
00174 
00175 
00176 /**
00177  * @brief SMTP client context
00178  **/
00179 
00180 typedef struct
00181 {
00182    Socket *socket;                                  ///<Underlying socket
00183    bool_t authLoginSupported;                       ///<LOGIN authentication mechanism supported
00184    bool_t authPlainSupported;                       ///<PLAIN authentication mechanism supported
00185    bool_t authCramMd5Supported;                     ///<CRAM-MD5 authentication mechanism supported
00186    bool_t startTlsSupported;                        ///<STARTTLS command supported
00187    char_t buffer[SMTP_CLIENT_MAX_LINE_LENGTH / 2];  ///<Memory buffer for input/output operations
00188    char_t buffer2[SMTP_CLIENT_MAX_LINE_LENGTH / 2];
00189 #if (SMTP_CLIENT_TLS_SUPPORT == ENABLED)
00190    TlsContext *tlsContext;                          ///<TLS context
00191 #endif
00192 } SmtpClientContext;
00193 
00194 
00195 //Callback function to parse a response line
00196 typedef error_t (*SmtpReplyCallback)(SmtpClientContext *context, char_t *replyLine, uint_t replyCode);
00197 
00198 //SMTP related functions
00199 error_t smtpSendMail(const SmtpAuthInfo *authInfo, const SmtpMail *mail);
00200 
00201 error_t smtpEhloReplyCallback(SmtpClientContext *context,
00202    char_t *replyLine, uint_t replyCode);
00203 
00204 error_t smtpSendAuthLogin(SmtpClientContext *context, const SmtpAuthInfo *authInfo);
00205 error_t smtpSendAuthPlain(SmtpClientContext *context, const SmtpAuthInfo *authInfo);
00206 error_t smtpSendAuthCramMd5(SmtpClientContext *context, const SmtpAuthInfo *authInfo);
00207 
00208 error_t smtpSendData(SmtpClientContext *context, const SmtpMail *mail);
00209 
00210 error_t smtpSendCommand(SmtpClientContext *context, const char_t *command,
00211    uint_t *replyCode, SmtpReplyCallback callback);
00212 
00213 error_t smtpWrite(SmtpClientContext *context, const void *data, size_t length, uint_t flags);
00214 error_t smtpRead(SmtpClientContext *context, void *data, size_t size, size_t *received, uint_t flags);
00215 
00216 #endif
00217