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