Webserver+3d print
cyclone_tcp/smtp/smtp_client.h@0:8918a71cdbe9, 2017-02-04 (annotated)
- Committer:
- Sergunb
- Date:
- Sat Feb 04 18:15:49 2017 +0000
- Revision:
- 0:8918a71cdbe9
nothing else
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sergunb | 0:8918a71cdbe9 | 1 | /** |
Sergunb | 0:8918a71cdbe9 | 2 | * @file smtp_client.h |
Sergunb | 0:8918a71cdbe9 | 3 | * @brief SMTP client (Simple Mail Transfer Protocol) |
Sergunb | 0:8918a71cdbe9 | 4 | * |
Sergunb | 0:8918a71cdbe9 | 5 | * @section License |
Sergunb | 0:8918a71cdbe9 | 6 | * |
Sergunb | 0:8918a71cdbe9 | 7 | * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved. |
Sergunb | 0:8918a71cdbe9 | 8 | * |
Sergunb | 0:8918a71cdbe9 | 9 | * This file is part of CycloneTCP Open. |
Sergunb | 0:8918a71cdbe9 | 10 | * |
Sergunb | 0:8918a71cdbe9 | 11 | * This program is free software; you can redistribute it and/or |
Sergunb | 0:8918a71cdbe9 | 12 | * modify it under the terms of the GNU General Public License |
Sergunb | 0:8918a71cdbe9 | 13 | * as published by the Free Software Foundation; either version 2 |
Sergunb | 0:8918a71cdbe9 | 14 | * of the License, or (at your option) any later version. |
Sergunb | 0:8918a71cdbe9 | 15 | * |
Sergunb | 0:8918a71cdbe9 | 16 | * This program is distributed in the hope that it will be useful, |
Sergunb | 0:8918a71cdbe9 | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Sergunb | 0:8918a71cdbe9 | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Sergunb | 0:8918a71cdbe9 | 19 | * GNU General Public License for more details. |
Sergunb | 0:8918a71cdbe9 | 20 | * |
Sergunb | 0:8918a71cdbe9 | 21 | * You should have received a copy of the GNU General Public License |
Sergunb | 0:8918a71cdbe9 | 22 | * along with this program; if not, write to the Free Software Foundation, |
Sergunb | 0:8918a71cdbe9 | 23 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
Sergunb | 0:8918a71cdbe9 | 24 | * |
Sergunb | 0:8918a71cdbe9 | 25 | * @author Oryx Embedded SARL (www.oryx-embedded.com) |
Sergunb | 0:8918a71cdbe9 | 26 | * @version 1.7.6 |
Sergunb | 0:8918a71cdbe9 | 27 | **/ |
Sergunb | 0:8918a71cdbe9 | 28 | |
Sergunb | 0:8918a71cdbe9 | 29 | #ifndef _SMTP_CLIENT_H |
Sergunb | 0:8918a71cdbe9 | 30 | #define _SMTP_CLIENT_H |
Sergunb | 0:8918a71cdbe9 | 31 | |
Sergunb | 0:8918a71cdbe9 | 32 | //Dependencies |
Sergunb | 0:8918a71cdbe9 | 33 | #include "core/socket.h" |
Sergunb | 0:8918a71cdbe9 | 34 | |
Sergunb | 0:8918a71cdbe9 | 35 | //SMTP client support |
Sergunb | 0:8918a71cdbe9 | 36 | #ifndef SMTP_CLIENT_SUPPORT |
Sergunb | 0:8918a71cdbe9 | 37 | #define SMTP_CLIENT_SUPPORT ENABLED |
Sergunb | 0:8918a71cdbe9 | 38 | #elif (SMTP_CLIENT_SUPPORT != ENABLED && SMTP_CLIENT_SUPPORT != DISABLED) |
Sergunb | 0:8918a71cdbe9 | 39 | #error SMTP_CLIENT_SUPPORT parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 40 | #endif |
Sergunb | 0:8918a71cdbe9 | 41 | |
Sergunb | 0:8918a71cdbe9 | 42 | //Default timeout |
Sergunb | 0:8918a71cdbe9 | 43 | #ifndef SMTP_CLIENT_DEFAULT_TIMEOUT |
Sergunb | 0:8918a71cdbe9 | 44 | #define SMTP_CLIENT_DEFAULT_TIMEOUT 10000 |
Sergunb | 0:8918a71cdbe9 | 45 | #elif (SMTP_CLIENT_DEFAULT_TIMEOUT < 1000) |
Sergunb | 0:8918a71cdbe9 | 46 | #error SMTP_CLIENT_DEFAULT_TIMEOUT parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 47 | #endif |
Sergunb | 0:8918a71cdbe9 | 48 | |
Sergunb | 0:8918a71cdbe9 | 49 | //Maximum line length |
Sergunb | 0:8918a71cdbe9 | 50 | #ifndef SMTP_CLIENT_MAX_LINE_LENGTH |
Sergunb | 0:8918a71cdbe9 | 51 | #define SMTP_CLIENT_MAX_LINE_LENGTH 512 |
Sergunb | 0:8918a71cdbe9 | 52 | #elif (SMTP_CLIENT_MAX_LINE_LENGTH < 64) |
Sergunb | 0:8918a71cdbe9 | 53 | #error SMTP_CLIENT_MAX_LINE_LENGTH parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 54 | #endif |
Sergunb | 0:8918a71cdbe9 | 55 | |
Sergunb | 0:8918a71cdbe9 | 56 | //SMTP over SSL/TLS |
Sergunb | 0:8918a71cdbe9 | 57 | #ifndef SMTP_CLIENT_TLS_SUPPORT |
Sergunb | 0:8918a71cdbe9 | 58 | #define SMTP_CLIENT_TLS_SUPPORT DISABLED |
Sergunb | 0:8918a71cdbe9 | 59 | #elif (SMTP_CLIENT_TLS_SUPPORT != ENABLED && SMTP_CLIENT_TLS_SUPPORT != DISABLED) |
Sergunb | 0:8918a71cdbe9 | 60 | #error SMTP_CLIENT_TLS_SUPPORT parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 61 | #endif |
Sergunb | 0:8918a71cdbe9 | 62 | |
Sergunb | 0:8918a71cdbe9 | 63 | //LOGIN authentication support |
Sergunb | 0:8918a71cdbe9 | 64 | #ifndef SMTP_CLIENT_LOGIN_AUTH_SUPPORT |
Sergunb | 0:8918a71cdbe9 | 65 | #define SMTP_CLIENT_LOGIN_AUTH_SUPPORT ENABLED |
Sergunb | 0:8918a71cdbe9 | 66 | #elif (SMTP_CLIENT_LOGIN_AUTH_SUPPORT != ENABLED && SMTP_CLIENT_LOGIN_AUTH_SUPPORT != DISABLED) |
Sergunb | 0:8918a71cdbe9 | 67 | #error SMTP_CLIENT_LOGIN_AUTH_SUPPORT parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 68 | #endif |
Sergunb | 0:8918a71cdbe9 | 69 | |
Sergunb | 0:8918a71cdbe9 | 70 | //PLAIN authentication support |
Sergunb | 0:8918a71cdbe9 | 71 | #ifndef SMTP_CLIENT_PLAIN_AUTH_SUPPORT |
Sergunb | 0:8918a71cdbe9 | 72 | #define SMTP_CLIENT_PLAIN_AUTH_SUPPORT ENABLED |
Sergunb | 0:8918a71cdbe9 | 73 | #elif (SMTP_CLIENT_PLAIN_AUTH_SUPPORT != ENABLED && SMTP_CLIENT_PLAIN_AUTH_SUPPORT != DISABLED) |
Sergunb | 0:8918a71cdbe9 | 74 | #error SMTP_CLIENT_PLAIN_AUTH_SUPPORT parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 75 | #endif |
Sergunb | 0:8918a71cdbe9 | 76 | |
Sergunb | 0:8918a71cdbe9 | 77 | //CRAM-MD5 authentication support |
Sergunb | 0:8918a71cdbe9 | 78 | #ifndef SMTP_CLIENT_CRAM_MD5_AUTH_SUPPORT |
Sergunb | 0:8918a71cdbe9 | 79 | #define SMTP_CLIENT_CRAM_MD5_AUTH_SUPPORT DISABLED |
Sergunb | 0:8918a71cdbe9 | 80 | #elif (SMTP_CLIENT_CRAM_MD5_AUTH_SUPPORT != ENABLED && SMTP_CLIENT_CRAM_MD5_AUTH_SUPPORT != DISABLED) |
Sergunb | 0:8918a71cdbe9 | 81 | #error SMTP_CLIENT_CRAM_MD5_AUTH_SUPPORT parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 82 | #endif |
Sergunb | 0:8918a71cdbe9 | 83 | |
Sergunb | 0:8918a71cdbe9 | 84 | //SMTP over SSL/TLS supported? |
Sergunb | 0:8918a71cdbe9 | 85 | #if (SMTP_CLIENT_TLS_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 86 | #include "crypto.h" |
Sergunb | 0:8918a71cdbe9 | 87 | #include "tls.h" |
Sergunb | 0:8918a71cdbe9 | 88 | #endif |
Sergunb | 0:8918a71cdbe9 | 89 | |
Sergunb | 0:8918a71cdbe9 | 90 | //LOGIN or PLAIN authentication supported? |
Sergunb | 0:8918a71cdbe9 | 91 | #if (SMTP_CLIENT_LOGIN_AUTH_SUPPORT == ENABLED || SMTP_CLIENT_PLAIN_AUTH_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 92 | #include "crypto.h" |
Sergunb | 0:8918a71cdbe9 | 93 | #include "base64.h" |
Sergunb | 0:8918a71cdbe9 | 94 | #endif |
Sergunb | 0:8918a71cdbe9 | 95 | |
Sergunb | 0:8918a71cdbe9 | 96 | //CRAM-MD5 authentication supported? |
Sergunb | 0:8918a71cdbe9 | 97 | #if (SMTP_CLIENT_CRAM_MD5_AUTH_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 98 | #include "crypto.h" |
Sergunb | 0:8918a71cdbe9 | 99 | #include "base64.h" |
Sergunb | 0:8918a71cdbe9 | 100 | #include "hmac.h" |
Sergunb | 0:8918a71cdbe9 | 101 | #include "md5.h" |
Sergunb | 0:8918a71cdbe9 | 102 | #endif |
Sergunb | 0:8918a71cdbe9 | 103 | |
Sergunb | 0:8918a71cdbe9 | 104 | //SMTP port number |
Sergunb | 0:8918a71cdbe9 | 105 | #define SMTP_PORT 25 |
Sergunb | 0:8918a71cdbe9 | 106 | //SMTPS port number (SMTP over SSL/TLS) |
Sergunb | 0:8918a71cdbe9 | 107 | #define SMTPS_PORT 465 |
Sergunb | 0:8918a71cdbe9 | 108 | //SMTP message submission port number |
Sergunb | 0:8918a71cdbe9 | 109 | #define SMTP_SUBMISSION_PORT 587 |
Sergunb | 0:8918a71cdbe9 | 110 | |
Sergunb | 0:8918a71cdbe9 | 111 | //Test macros for SMTP response codes |
Sergunb | 0:8918a71cdbe9 | 112 | #define SMTP_REPLY_CODE_2YZ(code) ((code) >= 200 && (code) < 300) |
Sergunb | 0:8918a71cdbe9 | 113 | #define SMTP_REPLY_CODE_3YZ(code) ((code) >= 300 && (code) < 400) |
Sergunb | 0:8918a71cdbe9 | 114 | #define SMTP_REPLY_CODE_4YZ(code) ((code) >= 400 && (code) < 500) |
Sergunb | 0:8918a71cdbe9 | 115 | #define SMTP_REPLY_CODE_5YZ(code) ((code) >= 500 && (code) < 600) |
Sergunb | 0:8918a71cdbe9 | 116 | |
Sergunb | 0:8918a71cdbe9 | 117 | |
Sergunb | 0:8918a71cdbe9 | 118 | /** |
Sergunb | 0:8918a71cdbe9 | 119 | * @brief Recipient type |
Sergunb | 0:8918a71cdbe9 | 120 | **/ |
Sergunb | 0:8918a71cdbe9 | 121 | |
Sergunb | 0:8918a71cdbe9 | 122 | typedef enum |
Sergunb | 0:8918a71cdbe9 | 123 | { |
Sergunb | 0:8918a71cdbe9 | 124 | SMTP_RCPT_TYPE_TO = 1, |
Sergunb | 0:8918a71cdbe9 | 125 | SMTP_RCPT_TYPE_CC = 2, |
Sergunb | 0:8918a71cdbe9 | 126 | SMTP_RCPT_TYPE_BCC = 4, |
Sergunb | 0:8918a71cdbe9 | 127 | } SmtpRecipientType; |
Sergunb | 0:8918a71cdbe9 | 128 | |
Sergunb | 0:8918a71cdbe9 | 129 | |
Sergunb | 0:8918a71cdbe9 | 130 | /** |
Sergunb | 0:8918a71cdbe9 | 131 | * @brief Authentication information |
Sergunb | 0:8918a71cdbe9 | 132 | **/ |
Sergunb | 0:8918a71cdbe9 | 133 | |
Sergunb | 0:8918a71cdbe9 | 134 | typedef struct |
Sergunb | 0:8918a71cdbe9 | 135 | { |
Sergunb | 0:8918a71cdbe9 | 136 | NetInterface *interface; |
Sergunb | 0:8918a71cdbe9 | 137 | const char_t *serverName; |
Sergunb | 0:8918a71cdbe9 | 138 | uint16_t serverPort; |
Sergunb | 0:8918a71cdbe9 | 139 | const char_t *userName; |
Sergunb | 0:8918a71cdbe9 | 140 | const char_t *password; |
Sergunb | 0:8918a71cdbe9 | 141 | #if (SMTP_CLIENT_TLS_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 142 | bool_t useTls; |
Sergunb | 0:8918a71cdbe9 | 143 | const PrngAlgo *prngAlgo; |
Sergunb | 0:8918a71cdbe9 | 144 | void *prngContext; |
Sergunb | 0:8918a71cdbe9 | 145 | #endif |
Sergunb | 0:8918a71cdbe9 | 146 | } SmtpAuthInfo; |
Sergunb | 0:8918a71cdbe9 | 147 | |
Sergunb | 0:8918a71cdbe9 | 148 | |
Sergunb | 0:8918a71cdbe9 | 149 | /** |
Sergunb | 0:8918a71cdbe9 | 150 | * @brief Mail address |
Sergunb | 0:8918a71cdbe9 | 151 | **/ |
Sergunb | 0:8918a71cdbe9 | 152 | |
Sergunb | 0:8918a71cdbe9 | 153 | typedef struct |
Sergunb | 0:8918a71cdbe9 | 154 | { |
Sergunb | 0:8918a71cdbe9 | 155 | char_t *name; |
Sergunb | 0:8918a71cdbe9 | 156 | char_t *addr; |
Sergunb | 0:8918a71cdbe9 | 157 | uint_t type; |
Sergunb | 0:8918a71cdbe9 | 158 | } SmtpMailAddr; |
Sergunb | 0:8918a71cdbe9 | 159 | |
Sergunb | 0:8918a71cdbe9 | 160 | |
Sergunb | 0:8918a71cdbe9 | 161 | /** |
Sergunb | 0:8918a71cdbe9 | 162 | * @brief Mail contents |
Sergunb | 0:8918a71cdbe9 | 163 | **/ |
Sergunb | 0:8918a71cdbe9 | 164 | |
Sergunb | 0:8918a71cdbe9 | 165 | typedef struct |
Sergunb | 0:8918a71cdbe9 | 166 | { |
Sergunb | 0:8918a71cdbe9 | 167 | SmtpMailAddr from; |
Sergunb | 0:8918a71cdbe9 | 168 | const SmtpMailAddr *recipients; |
Sergunb | 0:8918a71cdbe9 | 169 | uint_t recipientCount; |
Sergunb | 0:8918a71cdbe9 | 170 | char_t *dateTime; |
Sergunb | 0:8918a71cdbe9 | 171 | const char_t *subject; |
Sergunb | 0:8918a71cdbe9 | 172 | const char_t *body; |
Sergunb | 0:8918a71cdbe9 | 173 | } SmtpMail; |
Sergunb | 0:8918a71cdbe9 | 174 | |
Sergunb | 0:8918a71cdbe9 | 175 | |
Sergunb | 0:8918a71cdbe9 | 176 | /** |
Sergunb | 0:8918a71cdbe9 | 177 | * @brief SMTP client context |
Sergunb | 0:8918a71cdbe9 | 178 | **/ |
Sergunb | 0:8918a71cdbe9 | 179 | |
Sergunb | 0:8918a71cdbe9 | 180 | typedef struct |
Sergunb | 0:8918a71cdbe9 | 181 | { |
Sergunb | 0:8918a71cdbe9 | 182 | Socket *socket; ///<Underlying socket |
Sergunb | 0:8918a71cdbe9 | 183 | bool_t authLoginSupported; ///<LOGIN authentication mechanism supported |
Sergunb | 0:8918a71cdbe9 | 184 | bool_t authPlainSupported; ///<PLAIN authentication mechanism supported |
Sergunb | 0:8918a71cdbe9 | 185 | bool_t authCramMd5Supported; ///<CRAM-MD5 authentication mechanism supported |
Sergunb | 0:8918a71cdbe9 | 186 | bool_t startTlsSupported; ///<STARTTLS command supported |
Sergunb | 0:8918a71cdbe9 | 187 | char_t buffer[SMTP_CLIENT_MAX_LINE_LENGTH / 2]; ///<Memory buffer for input/output operations |
Sergunb | 0:8918a71cdbe9 | 188 | char_t buffer2[SMTP_CLIENT_MAX_LINE_LENGTH / 2]; |
Sergunb | 0:8918a71cdbe9 | 189 | #if (SMTP_CLIENT_TLS_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 190 | TlsContext *tlsContext; ///<TLS context |
Sergunb | 0:8918a71cdbe9 | 191 | #endif |
Sergunb | 0:8918a71cdbe9 | 192 | } SmtpClientContext; |
Sergunb | 0:8918a71cdbe9 | 193 | |
Sergunb | 0:8918a71cdbe9 | 194 | |
Sergunb | 0:8918a71cdbe9 | 195 | //Callback function to parse a response line |
Sergunb | 0:8918a71cdbe9 | 196 | typedef error_t (*SmtpReplyCallback)(SmtpClientContext *context, char_t *replyLine, uint_t replyCode); |
Sergunb | 0:8918a71cdbe9 | 197 | |
Sergunb | 0:8918a71cdbe9 | 198 | //SMTP related functions |
Sergunb | 0:8918a71cdbe9 | 199 | error_t smtpSendMail(const SmtpAuthInfo *authInfo, const SmtpMail *mail); |
Sergunb | 0:8918a71cdbe9 | 200 | |
Sergunb | 0:8918a71cdbe9 | 201 | error_t smtpEhloReplyCallback(SmtpClientContext *context, |
Sergunb | 0:8918a71cdbe9 | 202 | char_t *replyLine, uint_t replyCode); |
Sergunb | 0:8918a71cdbe9 | 203 | |
Sergunb | 0:8918a71cdbe9 | 204 | error_t smtpSendAuthLogin(SmtpClientContext *context, const SmtpAuthInfo *authInfo); |
Sergunb | 0:8918a71cdbe9 | 205 | error_t smtpSendAuthPlain(SmtpClientContext *context, const SmtpAuthInfo *authInfo); |
Sergunb | 0:8918a71cdbe9 | 206 | error_t smtpSendAuthCramMd5(SmtpClientContext *context, const SmtpAuthInfo *authInfo); |
Sergunb | 0:8918a71cdbe9 | 207 | |
Sergunb | 0:8918a71cdbe9 | 208 | error_t smtpSendData(SmtpClientContext *context, const SmtpMail *mail); |
Sergunb | 0:8918a71cdbe9 | 209 | |
Sergunb | 0:8918a71cdbe9 | 210 | error_t smtpSendCommand(SmtpClientContext *context, const char_t *command, |
Sergunb | 0:8918a71cdbe9 | 211 | uint_t *replyCode, SmtpReplyCallback callback); |
Sergunb | 0:8918a71cdbe9 | 212 | |
Sergunb | 0:8918a71cdbe9 | 213 | error_t smtpWrite(SmtpClientContext *context, const void *data, size_t length, uint_t flags); |
Sergunb | 0:8918a71cdbe9 | 214 | error_t smtpRead(SmtpClientContext *context, void *data, size_t size, size_t *received, uint_t flags); |
Sergunb | 0:8918a71cdbe9 | 215 | |
Sergunb | 0:8918a71cdbe9 | 216 | #endif |
Sergunb | 0:8918a71cdbe9 | 217 |