NetServices Stack source
Dependents: HelloWorld ServoInterfaceBoardExample1 4180_Lab4
services/email/smtp/SMTPClient.cpp@1:abb442332fa8, 2010-06-14 (annotated)
- Committer:
- donatien
- Date:
- Mon Jun 14 10:33:54 2010 +0000
- Revision:
- 1:abb442332fa8
- Parent:
- 0:632c9925f013
- Child:
- 5:dd63a1e02b1b
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:632c9925f013 | 1 | |
donatien | 0:632c9925f013 | 2 | /* |
donatien | 0:632c9925f013 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
donatien | 0:632c9925f013 | 4 | |
donatien | 0:632c9925f013 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
donatien | 0:632c9925f013 | 6 | of this software and associated documentation files (the "Software"), to deal |
donatien | 0:632c9925f013 | 7 | in the Software without restriction, including without limitation the rights |
donatien | 0:632c9925f013 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
donatien | 0:632c9925f013 | 9 | copies of the Software, and to permit persons to whom the Software is |
donatien | 0:632c9925f013 | 10 | furnished to do so, subject to the following conditions: |
donatien | 0:632c9925f013 | 11 | |
donatien | 0:632c9925f013 | 12 | The above copyright notice and this permission notice shall be included in |
donatien | 0:632c9925f013 | 13 | all copies or substantial portions of the Software. |
donatien | 0:632c9925f013 | 14 | |
donatien | 0:632c9925f013 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
donatien | 0:632c9925f013 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
donatien | 0:632c9925f013 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
donatien | 0:632c9925f013 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
donatien | 0:632c9925f013 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 0:632c9925f013 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
donatien | 0:632c9925f013 | 21 | THE SOFTWARE. |
donatien | 0:632c9925f013 | 22 | */ |
donatien | 0:632c9925f013 | 23 | |
donatien | 1:abb442332fa8 | 24 | #include "SMTPClient.h" |
donatien | 0:632c9925f013 | 25 | |
donatien | 0:632c9925f013 | 26 | #if 0 |
donatien | 0:632c9925f013 | 27 | |
donatien | 0:632c9925f013 | 28 | #include <stdio.h> |
donatien | 0:632c9925f013 | 29 | |
donatien | 0:632c9925f013 | 30 | #define __DEBUG |
donatien | 0:632c9925f013 | 31 | #include "dbg.h" |
donatien | 0:632c9925f013 | 32 | |
donatien | 0:632c9925f013 | 33 | #define BUF_SIZE 128 |
donatien | 0:632c9925f013 | 34 | #define CHUNK_SIZE 512 |
donatien | 0:632c9925f013 | 35 | |
donatien | 0:632c9925f013 | 36 | SMTPClient::SMTPClient() : m_pMessage(NULL), m_nextState(SMTP_HELLO), |
donatien | 0:632c9925f013 | 37 | m_pCbItem(NULL), m_pCbMeth(NULL), m_watchdog(), m_timeout(0), m_posInMsg(0), m_closed(true), m_host() |
donatien | 0:632c9925f013 | 38 | { |
donatien | 0:632c9925f013 | 39 | setTimeout(SMTP_REQUEST_TIMEOUT); |
donatien | 0:632c9925f013 | 40 | } |
donatien | 0:632c9925f013 | 41 | |
donatien | 0:632c9925f013 | 42 | SMTPClient::~SMTPClient() |
donatien | 0:632c9925f013 | 43 | { |
donatien | 0:632c9925f013 | 44 | close(); |
donatien | 0:632c9925f013 | 45 | } |
donatien | 0:632c9925f013 | 46 | |
donatien | 0:632c9925f013 | 47 | void SMTPClient::setHost(const Host& host) |
donatien | 0:632c9925f013 | 48 | { |
donatien | 0:632c9925f013 | 49 | m_host = host; |
donatien | 0:632c9925f013 | 50 | } |
donatien | 0:632c9925f013 | 51 | |
donatien | 0:632c9925f013 | 52 | void SMTPClient::send(EmailMessage* pMessage) |
donatien | 0:632c9925f013 | 53 | { |
donatien | 0:632c9925f013 | 54 | init(); |
donatien | 0:632c9925f013 | 55 | m_posInMsg = 0; |
donatien | 0:632c9925f013 | 56 | m_nextState = SMTP_HELLO; |
donatien | 0:632c9925f013 | 57 | if( !m_pTCPSocket->connect(m_host) ) |
donatien | 0:632c9925f013 | 58 | { |
donatien | 0:632c9925f013 | 59 | close(); |
donatien | 0:632c9925f013 | 60 | onResult(SMTP_DISC); |
donatien | 0:632c9925f013 | 61 | } |
donatien | 0:632c9925f013 | 62 | } |
donatien | 0:632c9925f013 | 63 | |
donatien | 0:632c9925f013 | 64 | void SMTPClient::init() //Create and setup socket if needed |
donatien | 0:632c9925f013 | 65 | { |
donatien | 0:632c9925f013 | 66 | close(); //Remove previous elements |
donatien | 0:632c9925f013 | 67 | if(!m_closed) //Already opened |
donatien | 0:632c9925f013 | 68 | return; |
donatien | 0:632c9925f013 | 69 | m_nextState = SMTP_HELLO; |
donatien | 0:632c9925f013 | 70 | m_pTCPSocket = new TCPSocket; |
donatien | 0:632c9925f013 | 71 | m_pTCPSocket->setOnEvent(this, &SMTPClient::onTCPSocketEvent); |
donatien | 0:632c9925f013 | 72 | m_closed = false; |
donatien | 0:632c9925f013 | 73 | } |
donatien | 0:632c9925f013 | 74 | |
donatien | 0:632c9925f013 | 75 | void SMTPClient::close() |
donatien | 0:632c9925f013 | 76 | { |
donatien | 0:632c9925f013 | 77 | if(m_closed) |
donatien | 0:632c9925f013 | 78 | return; |
donatien | 0:632c9925f013 | 79 | m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else |
donatien | 0:632c9925f013 | 80 | m_watchdog.detach(); |
donatien | 0:632c9925f013 | 81 | m_pTCPSocket->resetOnEvent(); |
donatien | 0:632c9925f013 | 82 | m_pTCPSocket->close(); |
donatien | 0:632c9925f013 | 83 | delete m_pTCPSocket; |
donatien | 0:632c9925f013 | 84 | m_pTCPSocket = NULL; |
donatien | 0:632c9925f013 | 85 | } |
donatien | 0:632c9925f013 | 86 | |
donatien | 0:632c9925f013 | 87 | int SMTPClient::rc(char* buf) //Parse return code |
donatien | 0:632c9925f013 | 88 | { |
donatien | 0:632c9925f013 | 89 | int rc; |
donatien | 0:632c9925f013 | 90 | int len = sscanf(buf, "%d %*[^\r\n]\r\n", &rc); |
donatien | 0:632c9925f013 | 91 | if(len != 1) |
donatien | 0:632c9925f013 | 92 | return -1; |
donatien | 0:632c9925f013 | 93 | return rc; |
donatien | 0:632c9925f013 | 94 | } |
donatien | 0:632c9925f013 | 95 | |
donatien | 0:632c9925f013 | 96 | #define MIN(a,b) ((a)<(b))?(a):(b) |
donatien | 0:632c9925f013 | 97 | void SMTPClient::process(bool moreData) //Main state-machine |
donatien | 0:632c9925f013 | 98 | { |
donatien | 0:632c9925f013 | 99 | char buf[BUF_SIZE] = {0}; |
donatien | 0:632c9925f013 | 100 | if(moreData) |
donatien | 0:632c9925f013 | 101 | { |
donatien | 0:632c9925f013 | 102 | if( m_nextState != SMTP_BODYMORE ) |
donatien | 0:632c9925f013 | 103 | { |
donatien | 0:632c9925f013 | 104 | return; |
donatien | 0:632c9925f013 | 105 | } |
donatien | 0:632c9925f013 | 106 | } |
donatien | 0:632c9925f013 | 107 | if(!moreData) //Receive next frame |
donatien | 0:632c9925f013 | 108 | { |
donatien | 0:632c9925f013 | 109 | m_pTCPSocket->recv(buf, BUF_SIZE - 1); |
donatien | 0:632c9925f013 | 110 | } |
donatien | 0:632c9925f013 | 111 | |
donatien | 0:632c9925f013 | 112 | IpAddr myIp(0,0,0,0); |
donatien | 0:632c9925f013 | 113 | string to; |
donatien | 0:632c9925f013 | 114 | int sendLen; |
donatien | 0:632c9925f013 | 115 | |
donatien | 0:632c9925f013 | 116 | DBG("In state %d", m_nextState); |
donatien | 0:632c9925f013 | 117 | |
donatien | 0:632c9925f013 | 118 | switch(m_nextState) |
donatien | 0:632c9925f013 | 119 | { |
donatien | 0:632c9925f013 | 120 | case SMTP_HELLO: |
donatien | 0:632c9925f013 | 121 | if( rc(buf) != 220 ) |
donatien | 0:632c9925f013 | 122 | { close(); onResult(SMTP_PRTCL); return; } |
donatien | 0:632c9925f013 | 123 | myIp = Net::getDefaultIf()->getIp(); |
donatien | 0:632c9925f013 | 124 | sprintf(buf, "HELO %d.%d.%d.%d\r\n", myIp[0], myIp[1], myIp[2], myIp[3]); |
donatien | 0:632c9925f013 | 125 | m_nextState = SMTP_FROM; |
donatien | 0:632c9925f013 | 126 | break; |
donatien | 0:632c9925f013 | 127 | case SMTP_FROM: |
donatien | 0:632c9925f013 | 128 | if( rc(buf) != 250 ) |
donatien | 0:632c9925f013 | 129 | { close(); onResult(SMTP_PRTCL); return; } |
donatien | 0:632c9925f013 | 130 | sprintf(buf, "MAIL FROM:<%s>\r\n", m_pMessage->m_from.c_str()); |
donatien | 0:632c9925f013 | 131 | break; |
donatien | 0:632c9925f013 | 132 | case SMTP_TO: |
donatien | 0:632c9925f013 | 133 | if( rc(buf) != 250 ) |
donatien | 0:632c9925f013 | 134 | { close(); onResult(SMTP_PRTCL); return; } |
donatien | 0:632c9925f013 | 135 | to = m_pMessage->m_lTo.front(); |
donatien | 0:632c9925f013 | 136 | sprintf(buf, "RCPT TO:<%s>\r\n", to.c_str()); |
donatien | 0:632c9925f013 | 137 | m_pMessage->m_lTo.pop(); |
donatien | 0:632c9925f013 | 138 | if(m_pMessage->m_lTo.empty()) |
donatien | 0:632c9925f013 | 139 | { |
donatien | 0:632c9925f013 | 140 | m_nextState = SMTP_DATA; |
donatien | 0:632c9925f013 | 141 | } |
donatien | 0:632c9925f013 | 142 | break; |
donatien | 0:632c9925f013 | 143 | case SMTP_DATA: |
donatien | 0:632c9925f013 | 144 | if( rc(buf) != 250 ) |
donatien | 0:632c9925f013 | 145 | { close(); onResult(SMTP_PRTCL); return; } |
donatien | 0:632c9925f013 | 146 | sprintf(buf, "DATA\r\n"); |
donatien | 0:632c9925f013 | 147 | break; |
donatien | 0:632c9925f013 | 148 | case SMTP_BODY: |
donatien | 0:632c9925f013 | 149 | if( rc(buf) != 354 ) |
donatien | 0:632c9925f013 | 150 | { close(); onResult(SMTP_PRTCL); return; } |
donatien | 0:632c9925f013 | 151 | m_nextState = SMTP_BODYMORE; |
donatien | 0:632c9925f013 | 152 | case SMTP_BODYMORE: |
donatien | 0:632c9925f013 | 153 | sendLen = 0; |
donatien | 0:632c9925f013 | 154 | if( m_posInMsg < m_pMessage->m_content.length() ) |
donatien | 0:632c9925f013 | 155 | { |
donatien | 0:632c9925f013 | 156 | sendLen = MIN( (m_pMessage->m_content.length() - m_posInMsg), CHUNK_SIZE ); |
donatien | 0:632c9925f013 | 157 | m_pTCPSocket->send( m_pMessage->m_content.c_str() + m_posInMsg, sendLen ); |
donatien | 0:632c9925f013 | 158 | m_posInMsg += sendLen; |
donatien | 0:632c9925f013 | 159 | } |
donatien | 0:632c9925f013 | 160 | if( m_posInMsg == m_pMessage->m_content.length() ) |
donatien | 0:632c9925f013 | 161 | { |
donatien | 0:632c9925f013 | 162 | sprintf(buf, "\r\n.\r\n"); //EOF |
donatien | 0:632c9925f013 | 163 | m_nextState = SMTP_EOF; |
donatien | 0:632c9925f013 | 164 | } |
donatien | 0:632c9925f013 | 165 | break; |
donatien | 0:632c9925f013 | 166 | case SMTP_EOF: |
donatien | 0:632c9925f013 | 167 | if( rc(buf) != 250 ) |
donatien | 0:632c9925f013 | 168 | { close(); onResult(SMTP_PRTCL); return; } |
donatien | 0:632c9925f013 | 169 | sprintf(buf, "QUIT\r\n"); |
donatien | 0:632c9925f013 | 170 | m_nextState = SMTP_BYE; |
donatien | 0:632c9925f013 | 171 | break; |
donatien | 0:632c9925f013 | 172 | case SMTP_BYE: |
donatien | 0:632c9925f013 | 173 | if( rc(buf) != 221 ) |
donatien | 0:632c9925f013 | 174 | { close(); onResult(SMTP_PRTCL); return; } |
donatien | 0:632c9925f013 | 175 | close(); |
donatien | 0:632c9925f013 | 176 | onResult(SMTP_OK); |
donatien | 0:632c9925f013 | 177 | break; |
donatien | 0:632c9925f013 | 178 | } |
donatien | 0:632c9925f013 | 179 | |
donatien | 0:632c9925f013 | 180 | if( m_nextState != SMTP_BODYMORE ) |
donatien | 0:632c9925f013 | 181 | { |
donatien | 0:632c9925f013 | 182 | m_pTCPSocket->send( buf, strlen(buf) ); |
donatien | 0:632c9925f013 | 183 | } |
donatien | 0:632c9925f013 | 184 | } |
donatien | 0:632c9925f013 | 185 | |
donatien | 0:632c9925f013 | 186 | void SMTPClient::setTimeout(int ms) |
donatien | 0:632c9925f013 | 187 | { |
donatien | 0:632c9925f013 | 188 | m_timeout = 1000*ms; |
donatien | 0:632c9925f013 | 189 | resetTimeout(); |
donatien | 0:632c9925f013 | 190 | } |
donatien | 0:632c9925f013 | 191 | |
donatien | 0:632c9925f013 | 192 | void SMTPClient::resetTimeout() |
donatien | 0:632c9925f013 | 193 | { |
donatien | 0:632c9925f013 | 194 | m_watchdog.detach(); |
donatien | 0:632c9925f013 | 195 | m_watchdog.attach_us<SMTPClient>(this, &SMTPClient::onTimeout, m_timeout); |
donatien | 0:632c9925f013 | 196 | } |
donatien | 0:632c9925f013 | 197 | |
donatien | 0:632c9925f013 | 198 | void SMTPClient::onTimeout() //Connection has timed out |
donatien | 0:632c9925f013 | 199 | { |
donatien | 0:632c9925f013 | 200 | close(); |
donatien | 0:632c9925f013 | 201 | onResult(SMTP_TIMEOUT); |
donatien | 0:632c9925f013 | 202 | } |
donatien | 0:632c9925f013 | 203 | |
donatien | 0:632c9925f013 | 204 | void SMTPClient::onTCPSocketEvent(TCPSocketEvent e) |
donatien | 0:632c9925f013 | 205 | { |
donatien | 0:632c9925f013 | 206 | switch(e) |
donatien | 0:632c9925f013 | 207 | { |
donatien | 0:632c9925f013 | 208 | case TCPSOCKET_READABLE: |
donatien | 0:632c9925f013 | 209 | resetTimeout(); |
donatien | 0:632c9925f013 | 210 | process(false); |
donatien | 0:632c9925f013 | 211 | break; |
donatien | 0:632c9925f013 | 212 | case TCPSOCKET_WRITEABLE: |
donatien | 0:632c9925f013 | 213 | resetTimeout(); |
donatien | 0:632c9925f013 | 214 | process(true); |
donatien | 0:632c9925f013 | 215 | break; |
donatien | 0:632c9925f013 | 216 | case TCPSOCKET_CONTIMEOUT: |
donatien | 0:632c9925f013 | 217 | case TCPSOCKET_CONRST: |
donatien | 0:632c9925f013 | 218 | case TCPSOCKET_CONABRT: |
donatien | 0:632c9925f013 | 219 | case TCPSOCKET_ERROR: |
donatien | 0:632c9925f013 | 220 | onResult(SMTP_DISC); |
donatien | 0:632c9925f013 | 221 | DBG("\r\nConnection error in SMTP Client.\r\n"); |
donatien | 0:632c9925f013 | 222 | close(); |
donatien | 0:632c9925f013 | 223 | break; |
donatien | 0:632c9925f013 | 224 | case TCPSOCKET_DISCONNECTED: |
donatien | 0:632c9925f013 | 225 | if(m_nextState != SMTP_BYE) |
donatien | 0:632c9925f013 | 226 | { |
donatien | 0:632c9925f013 | 227 | onResult(SMTP_DISC); |
donatien | 0:632c9925f013 | 228 | DBG("\r\nConnection error in SMTP Client.\r\n"); |
donatien | 0:632c9925f013 | 229 | close(); |
donatien | 0:632c9925f013 | 230 | } |
donatien | 0:632c9925f013 | 231 | break; |
donatien | 0:632c9925f013 | 232 | } |
donatien | 0:632c9925f013 | 233 | } |
donatien | 0:632c9925f013 | 234 | |
donatien | 0:632c9925f013 | 235 | void SMTPClient::onResult(SMTPResult r) //Must be called by impl when the request completes |
donatien | 0:632c9925f013 | 236 | { |
donatien | 0:632c9925f013 | 237 | if(m_pCbItem && m_pCbMeth) |
donatien | 0:632c9925f013 | 238 | (m_pCbItem->*m_pCbMeth)(r); |
donatien | 0:632c9925f013 | 239 | } |
donatien | 0:632c9925f013 | 240 | |
donatien | 0:632c9925f013 | 241 | #endif |