HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mr_q 0:d8f2f7d5f31b 1
mr_q 0:d8f2f7d5f31b 2 /*
mr_q 0:d8f2f7d5f31b 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) y Segundo Equipo
mr_q 0:d8f2f7d5f31b 4
mr_q 0:d8f2f7d5f31b 5 Permission is hereby granted, free of charge, to any person obtaining a copy
mr_q 0:d8f2f7d5f31b 6 of this software and associated documentation files (the "Software"), to deal
mr_q 0:d8f2f7d5f31b 7 in the Software without restriction, including without limitation the rights
mr_q 0:d8f2f7d5f31b 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
mr_q 0:d8f2f7d5f31b 9 copies of the Software, and to permit persons to whom the Software is
mr_q 0:d8f2f7d5f31b 10 furnished to do so, subject to the following conditions:
mr_q 0:d8f2f7d5f31b 11
mr_q 0:d8f2f7d5f31b 12 The above copyright notice and this permission notice shall be included in
mr_q 0:d8f2f7d5f31b 13 all copies or substantial portions of the Software.
mr_q 0:d8f2f7d5f31b 14
mr_q 0:d8f2f7d5f31b 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
mr_q 0:d8f2f7d5f31b 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
mr_q 0:d8f2f7d5f31b 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
mr_q 0:d8f2f7d5f31b 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
mr_q 0:d8f2f7d5f31b 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mr_q 0:d8f2f7d5f31b 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
mr_q 0:d8f2f7d5f31b 21 THE SOFTWARE.
mr_q 0:d8f2f7d5f31b 22 */
mr_q 0:d8f2f7d5f31b 23
mr_q 0:d8f2f7d5f31b 24 /** \file
mr_q 0:d8f2f7d5f31b 25 SMTP Client header file
mr_q 0:d8f2f7d5f31b 26 */
mr_q 0:d8f2f7d5f31b 27
mr_q 0:d8f2f7d5f31b 28 #ifndef SMTP_CLIENT_H
mr_q 0:d8f2f7d5f31b 29 #define SMTP_CLIENT_H
mr_q 0:d8f2f7d5f31b 30
mr_q 0:d8f2f7d5f31b 31 class EmailMessage;
mr_q 0:d8f2f7d5f31b 32
mr_q 0:d8f2f7d5f31b 33 #include "core/net.h"
mr_q 0:d8f2f7d5f31b 34 #include "api/TCPSocket.h"
mr_q 0:d8f2f7d5f31b 35 #include "api/DNSRequest.h"
mr_q 0:d8f2f7d5f31b 36 #include "EmailMessage.h"
mr_q 0:d8f2f7d5f31b 37 #include "mbed.h"
mr_q 0:d8f2f7d5f31b 38
mr_q 0:d8f2f7d5f31b 39 ///SMTP client results
mr_q 0:d8f2f7d5f31b 40 enum SMTPResult {
mr_q 0:d8f2f7d5f31b 41 SMTP_OK, ///<Success
mr_q 0:d8f2f7d5f31b 42 SMTP_PROCESSING, ///<Processing
mr_q 0:d8f2f7d5f31b 43 SMTP_DNS, ///<Could not resolve name
mr_q 0:d8f2f7d5f31b 44 SMTP_PRTCL, ///<Protocol error
mr_q 0:d8f2f7d5f31b 45 SMTP_TIMEOUT, ///<Connection timeout
mr_q 0:d8f2f7d5f31b 46 SMTP_DISC ///<Disconnected
mr_q 0:d8f2f7d5f31b 47 };
mr_q 0:d8f2f7d5f31b 48
mr_q 0:d8f2f7d5f31b 49 ///SMTP authentication
mr_q 0:d8f2f7d5f31b 50 enum SMTPAuth {
mr_q 0:d8f2f7d5f31b 51 SMTP_AUTH_NONE, ///<No authentication
mr_q 0:d8f2f7d5f31b 52 SMTP_AUTH_PLAIN ///<AUTH PLAIN authentication
mr_q 0:d8f2f7d5f31b 53 };
mr_q 0:d8f2f7d5f31b 54 #include "core/netservice.h"
mr_q 0:d8f2f7d5f31b 55
mr_q 0:d8f2f7d5f31b 56 ///A simple SMTP Client
mr_q 0:d8f2f7d5f31b 57 /**
mr_q 0:d8f2f7d5f31b 58 The SMTPClient is composed of:
mr_q 0:d8f2f7d5f31b 59 - The actual client (SMTPClient)
mr_q 0:d8f2f7d5f31b 60 - A class (EmailMessage) to hold the message addresses and content for sending
mr_q 0:d8f2f7d5f31b 61 */
mr_q 0:d8f2f7d5f31b 62 class SMTPClient : protected NetService {
mr_q 0:d8f2f7d5f31b 63 public:
mr_q 0:d8f2f7d5f31b 64 ///Instantiates the SMTP client
mr_q 0:d8f2f7d5f31b 65 SMTPClient();
mr_q 0:d8f2f7d5f31b 66
mr_q 0:d8f2f7d5f31b 67 ///Destructor for the SMTP client
mr_q 0:d8f2f7d5f31b 68 virtual ~SMTPClient();
mr_q 0:d8f2f7d5f31b 69
mr_q 0:d8f2f7d5f31b 70 ///Full constructor for the SMTP client
mr_q 0:d8f2f7d5f31b 71 /**
mr_q 0:d8f2f7d5f31b 72 @param host : SMTP server host
mr_q 0:d8f2f7d5f31b 73 @param heloDomain : domain name of client
mr_q 0:d8f2f7d5f31b 74 @param user : username
mr_q 0:d8f2f7d5f31b 75 @param password : password
mr_q 0:d8f2f7d5f31b 76 @param auth : authentication type
mr_q 0:d8f2f7d5f31b 77 */
mr_q 0:d8f2f7d5f31b 78 SMTPClient(const Host& host, const char* heloDomain, const char* user, const char* password, SMTPAuth auth);
mr_q 0:d8f2f7d5f31b 79
mr_q 0:d8f2f7d5f31b 80 ///Set server host
mr_q 0:d8f2f7d5f31b 81 /**
mr_q 0:d8f2f7d5f31b 82 @param host : SMTP server host
mr_q 0:d8f2f7d5f31b 83 */
mr_q 0:d8f2f7d5f31b 84 void setServer(const Host& host);
mr_q 0:d8f2f7d5f31b 85
mr_q 0:d8f2f7d5f31b 86 ///Provides a plain authentication feature (Base64 encoded username and password)
mr_q 0:d8f2f7d5f31b 87 /**
mr_q 0:d8f2f7d5f31b 88 @param user : username
mr_q 0:d8f2f7d5f31b 89 @param password : password
mr_q 0:d8f2f7d5f31b 90 */
mr_q 0:d8f2f7d5f31b 91 void setAuth(const char* user, const char* password); // Plain authentication
mr_q 0:d8f2f7d5f31b 92
mr_q 0:d8f2f7d5f31b 93 ///Turns off authentication
mr_q 0:d8f2f7d5f31b 94 void clearAuth(); // Clear authentication
mr_q 0:d8f2f7d5f31b 95
mr_q 0:d8f2f7d5f31b 96 ///Set HELO domain (defaults to localhost if not set)
mr_q 0:d8f2f7d5f31b 97 /**
mr_q 0:d8f2f7d5f31b 98 @param heloDomain : domain name of client (strictly should be fully qualified domain name)
mr_q 0:d8f2f7d5f31b 99 */
mr_q 0:d8f2f7d5f31b 100 void setHeloDomain(const char* heloDomain);
mr_q 0:d8f2f7d5f31b 101
mr_q 0:d8f2f7d5f31b 102 //High Level setup functions
mr_q 0:d8f2f7d5f31b 103 ///Sends the message (blocking)
mr_q 0:d8f2f7d5f31b 104 /**
mr_q 0:d8f2f7d5f31b 105 @param pMessage : pointer to a message
mr_q 0:d8f2f7d5f31b 106
mr_q 0:d8f2f7d5f31b 107 Blocks until completion
mr_q 0:d8f2f7d5f31b 108 */
mr_q 0:d8f2f7d5f31b 109 SMTPResult send(EmailMessage* pMessage); //Blocking
mr_q 0:d8f2f7d5f31b 110
mr_q 0:d8f2f7d5f31b 111 ///Sends the message (non blocking)
mr_q 0:d8f2f7d5f31b 112 /**
mr_q 0:d8f2f7d5f31b 113 @param pMessage : pointer to a message
mr_q 0:d8f2f7d5f31b 114 @param pMethod : callback function
mr_q 0:d8f2f7d5f31b 115
mr_q 0:d8f2f7d5f31b 116 The function returns immediately and calls the callback on completion or error
mr_q 0:d8f2f7d5f31b 117 */
mr_q 0:d8f2f7d5f31b 118 SMTPResult send(EmailMessage* pMessage, void (*pMethod)(SMTPResult)); //Non blocking
mr_q 0:d8f2f7d5f31b 119
mr_q 0:d8f2f7d5f31b 120 ///Sends the message (non blocking)
mr_q 0:d8f2f7d5f31b 121 /**
mr_q 0:d8f2f7d5f31b 122 @param pMessage : pointer to a message
mr_q 0:d8f2f7d5f31b 123 @param pItem : instance of class on which to execute the callback method
mr_q 0:d8f2f7d5f31b 124 @param pMethod : callback method
mr_q 0:d8f2f7d5f31b 125
mr_q 0:d8f2f7d5f31b 126 The function returns immediately and calls the callback on completion or error
mr_q 0:d8f2f7d5f31b 127 */
mr_q 0:d8f2f7d5f31b 128 template<class T>
mr_q 0:d8f2f7d5f31b 129 SMTPResult send(EmailMessage* pMessage, T* pItem, void (T::*pMethod)(SMTPResult)) { //Non blocking
mr_q 0:d8f2f7d5f31b 130 setOnResult(pItem, pMethod);
mr_q 0:d8f2f7d5f31b 131 doSend(pMessage);
mr_q 0:d8f2f7d5f31b 132 return SMTP_PROCESSING;
mr_q 0:d8f2f7d5f31b 133 }
mr_q 0:d8f2f7d5f31b 134
mr_q 0:d8f2f7d5f31b 135 ///Sends the message (non blocking)
mr_q 0:d8f2f7d5f31b 136 /**
mr_q 0:d8f2f7d5f31b 137 @param pMessage : pointer to a message
mr_q 0:d8f2f7d5f31b 138
mr_q 0:d8f2f7d5f31b 139 The function returns immediately and calls the previously set callback on completion or error
mr_q 0:d8f2f7d5f31b 140 */
mr_q 0:d8f2f7d5f31b 141 void doSend(EmailMessage* pMessage);
mr_q 0:d8f2f7d5f31b 142
mr_q 0:d8f2f7d5f31b 143 ///Setup the result callback
mr_q 0:d8f2f7d5f31b 144 /**
mr_q 0:d8f2f7d5f31b 145 @param pMethod : callback function
mr_q 0:d8f2f7d5f31b 146 */
mr_q 0:d8f2f7d5f31b 147 void setOnResult( void (*pMethod)(SMTPResult) );
mr_q 0:d8f2f7d5f31b 148
mr_q 0:d8f2f7d5f31b 149 ///Setup the result callback
mr_q 0:d8f2f7d5f31b 150 /**
mr_q 0:d8f2f7d5f31b 151 @param pItem : instance of class on which to execute the callback method
mr_q 0:d8f2f7d5f31b 152 @param pMethod : callback method
mr_q 0:d8f2f7d5f31b 153 */
mr_q 0:d8f2f7d5f31b 154 class CDummy;
mr_q 0:d8f2f7d5f31b 155 template<class T>
mr_q 0:d8f2f7d5f31b 156 void setOnResult( T* pItem, void (T::*pMethod)(SMTPResult) ) {
mr_q 0:d8f2f7d5f31b 157 m_pCb = NULL;
mr_q 0:d8f2f7d5f31b 158 m_pCbItem = (CDummy*) pItem;
mr_q 0:d8f2f7d5f31b 159 m_pCbMeth = (void (CDummy::*)(SMTPResult)) pMethod;
mr_q 0:d8f2f7d5f31b 160 }
mr_q 0:d8f2f7d5f31b 161
mr_q 0:d8f2f7d5f31b 162 ///Setup timeout
mr_q 0:d8f2f7d5f31b 163 /**
mr_q 0:d8f2f7d5f31b 164 @param ms : time of connection inactivity in ms after which the request should timeout
mr_q 0:d8f2f7d5f31b 165 */
mr_q 0:d8f2f7d5f31b 166 void setTimeout(int ms);
mr_q 0:d8f2f7d5f31b 167
mr_q 0:d8f2f7d5f31b 168 ///Gets the last response from the server
mr_q 0:d8f2f7d5f31b 169 string& getLastResponse();
mr_q 0:d8f2f7d5f31b 170
mr_q 0:d8f2f7d5f31b 171 virtual void poll(); //Called by NetServices
mr_q 0:d8f2f7d5f31b 172
mr_q 0:d8f2f7d5f31b 173 protected:
mr_q 0:d8f2f7d5f31b 174 int rc(char* buf); //Return code
mr_q 0:d8f2f7d5f31b 175 void process(bool writeable); //Main state-machine
mr_q 0:d8f2f7d5f31b 176
mr_q 0:d8f2f7d5f31b 177 void resetTimeout();
mr_q 0:d8f2f7d5f31b 178
mr_q 0:d8f2f7d5f31b 179 void init();
mr_q 0:d8f2f7d5f31b 180 void close();
mr_q 0:d8f2f7d5f31b 181
mr_q 0:d8f2f7d5f31b 182 void setup(EmailMessage* pMessage); //Setup request, make DNS Req if necessary
mr_q 0:d8f2f7d5f31b 183 void connect(); //Start Connection
mr_q 0:d8f2f7d5f31b 184
mr_q 0:d8f2f7d5f31b 185 int tryRead(); //Read data and try to feed output
mr_q 0:d8f2f7d5f31b 186 void readData(); //Data has been read
mr_q 0:d8f2f7d5f31b 187 void writeData(); //Data has been written & buf is free
mr_q 0:d8f2f7d5f31b 188
mr_q 0:d8f2f7d5f31b 189 void onTCPSocketEvent(TCPSocketEvent e);
mr_q 0:d8f2f7d5f31b 190 void onDNSReply(DNSReply r);
mr_q 0:d8f2f7d5f31b 191 void onResult(SMTPResult r); //Called when exchange completed or on failure
mr_q 0:d8f2f7d5f31b 192 void onTimeout(); //Connection has timed out
mr_q 0:d8f2f7d5f31b 193
mr_q 0:d8f2f7d5f31b 194 string encodePlainAuth(); // Encode plain authentication (username and password in based 64)
mr_q 0:d8f2f7d5f31b 195 bool okPostAuthentication(int code); // True if server response is ok following authentication
mr_q 0:d8f2f7d5f31b 196
mr_q 0:d8f2f7d5f31b 197 private:
mr_q 0:d8f2f7d5f31b 198 SMTPResult blockingProcess(); //Called in blocking mode, calls Net::poll() until return code is available
mr_q 0:d8f2f7d5f31b 199
mr_q 0:d8f2f7d5f31b 200 CDummy* m_pCbItem;
mr_q 0:d8f2f7d5f31b 201 void (CDummy::*m_pCbMeth)(SMTPResult);
mr_q 0:d8f2f7d5f31b 202 void (*m_pCb)(SMTPResult);
mr_q 0:d8f2f7d5f31b 203
mr_q 0:d8f2f7d5f31b 204 TCPSocket* m_pTCPSocket;
mr_q 0:d8f2f7d5f31b 205
mr_q 0:d8f2f7d5f31b 206 Timer m_watchdog;
mr_q 0:d8f2f7d5f31b 207 int m_timeout;
mr_q 0:d8f2f7d5f31b 208
mr_q 0:d8f2f7d5f31b 209 DNSRequest* m_pDnsReq;
mr_q 0:d8f2f7d5f31b 210 Host m_server;
mr_q 0:d8f2f7d5f31b 211
mr_q 0:d8f2f7d5f31b 212 bool m_closed;
mr_q 0:d8f2f7d5f31b 213
mr_q 0:d8f2f7d5f31b 214 enum SMTPStep {
mr_q 0:d8f2f7d5f31b 215 SMTP_HELLO,
mr_q 0:d8f2f7d5f31b 216 SMTP_AUTH,
mr_q 0:d8f2f7d5f31b 217 SMTP_FROM,
mr_q 0:d8f2f7d5f31b 218 SMTP_TO,
mr_q 0:d8f2f7d5f31b 219 SMTP_DATA,
mr_q 0:d8f2f7d5f31b 220 SMTP_BODY,
mr_q 0:d8f2f7d5f31b 221 SMTP_BODYMORE,
mr_q 0:d8f2f7d5f31b 222 SMTP_EOF,
mr_q 0:d8f2f7d5f31b 223 SMTP_BYE,
mr_q 0:d8f2f7d5f31b 224 SMTP_CLOSED
mr_q 0:d8f2f7d5f31b 225 };
mr_q 0:d8f2f7d5f31b 226
mr_q 0:d8f2f7d5f31b 227 SMTPStep m_state;
mr_q 0:d8f2f7d5f31b 228
mr_q 0:d8f2f7d5f31b 229 EmailMessage* m_pMessage;
mr_q 0:d8f2f7d5f31b 230
mr_q 0:d8f2f7d5f31b 231 int m_posInMsg;
mr_q 0:d8f2f7d5f31b 232 int m_posInCRLF;
mr_q 0:d8f2f7d5f31b 233
mr_q 0:d8f2f7d5f31b 234 SMTPResult m_blockingResult; //Result if blocking mode
mr_q 0:d8f2f7d5f31b 235 string m_response;
mr_q 0:d8f2f7d5f31b 236 int m_responseCode;
mr_q 0:d8f2f7d5f31b 237 string m_username;
mr_q 0:d8f2f7d5f31b 238 string m_password;
mr_q 0:d8f2f7d5f31b 239 SMTPAuth m_auth;
mr_q 0:d8f2f7d5f31b 240 string m_heloDomain;
mr_q 0:d8f2f7d5f31b 241
mr_q 0:d8f2f7d5f31b 242 vector<string>::iterator m_To;
mr_q 0:d8f2f7d5f31b 243 };
mr_q 0:d8f2f7d5f31b 244
mr_q 0:d8f2f7d5f31b 245 #endif // SMTP_CLIENT_H