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 #pragma diag_remark 177
mr_q 0:d8f2f7d5f31b 2 /*
mr_q 0:d8f2f7d5f31b 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
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 #include "MySQLClient.h"
mr_q 0:d8f2f7d5f31b 25 #include "sha1.h" //For 4.1+ passwords
mr_q 0:d8f2f7d5f31b 26 #include "mycrypt.h" //For 4.0- passwords
mr_q 0:d8f2f7d5f31b 27
mr_q 0:d8f2f7d5f31b 28 //#define __DEBUG
mr_q 0:d8f2f7d5f31b 29 #include "dbg/dbg.h"
mr_q 0:d8f2f7d5f31b 30
mr_q 0:d8f2f7d5f31b 31 #define MYSQL_TIMEOUT_MS 45000
mr_q 0:d8f2f7d5f31b 32 #define MYSQL_PORT 3306
mr_q 0:d8f2f7d5f31b 33
mr_q 0:d8f2f7d5f31b 34 #define BUF_SIZE 256
mr_q 0:d8f2f7d5f31b 35
mr_q 0:d8f2f7d5f31b 36 #define CLIENT_LONG_PASSWORD 1
mr_q 0:d8f2f7d5f31b 37 #define CLIENT_CONNECT_WITH_DB 8
mr_q 0:d8f2f7d5f31b 38 #define CLIENT_PROTOCOL_41 512
mr_q 0:d8f2f7d5f31b 39 #define CLIENT_INTERACTIVE 1024
mr_q 0:d8f2f7d5f31b 40 #define CLIENT_SECURE_CONNECTION 32768
mr_q 0:d8f2f7d5f31b 41
mr_q 0:d8f2f7d5f31b 42 #define MIN(a,b) ((a)<(b)?(a):(b))
mr_q 0:d8f2f7d5f31b 43 #define ABS(a) (((a)>0)?(a):0)
mr_q 0:d8f2f7d5f31b 44
mr_q 0:d8f2f7d5f31b 45 //MySQL commands
mr_q 0:d8f2f7d5f31b 46 #define COM_QUIT 0x01 //Exit
mr_q 0:d8f2f7d5f31b 47 #define COM_QUERY 0x03 //Execute an SQL query
mr_q 0:d8f2f7d5f31b 48
mr_q 0:d8f2f7d5f31b 49 //#define htons( x ) ( (( x << 8 ) & 0xFF00) | (( x >> 8 ) & 0x00FF) )
mr_q 0:d8f2f7d5f31b 50 #define ntohs( x ) (htons(x))
mr_q 0:d8f2f7d5f31b 51
mr_q 0:d8f2f7d5f31b 52 /*#define htonl( x ) ( (( x << 24 ) & 0xFF000000) \
mr_q 0:d8f2f7d5f31b 53 | (( x << 8 ) & 0x00FF0000) \
mr_q 0:d8f2f7d5f31b 54 | (( x >> 8 ) & 0x0000FF00) \
mr_q 0:d8f2f7d5f31b 55 | (( x >> 24 ) & 0x000000FF) )*/
mr_q 0:d8f2f7d5f31b 56 #define htonl( x ) (x)
mr_q 0:d8f2f7d5f31b 57 #define ntohl( x ) (htonl(x))
mr_q 0:d8f2f7d5f31b 58
mr_q 0:d8f2f7d5f31b 59 MySQLClient::MySQLClient() : NetService(false) /*Not owned by the pool*/, m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL),
mr_q 0:d8f2f7d5f31b 60 m_pTCPSocket(NULL), m_watchdog(), m_timeout(MYSQL_TIMEOUT_MS*1000), m_pDnsReq(NULL), m_closed(true),
mr_q 0:d8f2f7d5f31b 61 m_host(), m_user(), m_password(), m_db(), m_state(MYSQL_CLOSED)
mr_q 0:d8f2f7d5f31b 62 {
mr_q 0:d8f2f7d5f31b 63 m_buf = new byte[BUF_SIZE];
mr_q 0:d8f2f7d5f31b 64 m_pPos = m_buf;
mr_q 0:d8f2f7d5f31b 65 m_len = 0;
mr_q 0:d8f2f7d5f31b 66 m_size = BUF_SIZE;
mr_q 0:d8f2f7d5f31b 67 }
mr_q 0:d8f2f7d5f31b 68
mr_q 0:d8f2f7d5f31b 69 MySQLClient::~MySQLClient()
mr_q 0:d8f2f7d5f31b 70 {
mr_q 0:d8f2f7d5f31b 71 close();
mr_q 0:d8f2f7d5f31b 72 delete[] m_buf;
mr_q 0:d8f2f7d5f31b 73 }
mr_q 0:d8f2f7d5f31b 74
mr_q 0:d8f2f7d5f31b 75 //High Level setup functions
mr_q 0:d8f2f7d5f31b 76 MySQLResult MySQLClient::open(Host& host, const string& user, const string& password, const string& db, void (*pMethod)(MySQLResult)) //Non blocking
mr_q 0:d8f2f7d5f31b 77 {
mr_q 0:d8f2f7d5f31b 78 setOnResult(pMethod);
mr_q 0:d8f2f7d5f31b 79 setup(host, user, password, db);
mr_q 0:d8f2f7d5f31b 80 return MYSQL_PROCESSING;
mr_q 0:d8f2f7d5f31b 81 }
mr_q 0:d8f2f7d5f31b 82
mr_q 0:d8f2f7d5f31b 83 #if 0 //Ref only
mr_q 0:d8f2f7d5f31b 84 template<class T>
mr_q 0:d8f2f7d5f31b 85 MySQLResult MySQLClient::open(Host& host, const string& user, const string& password, const string& db, T* pItem, void (T::*pMethod)(MySQLResult)) //Non blocking
mr_q 0:d8f2f7d5f31b 86 {
mr_q 0:d8f2f7d5f31b 87 setOnResult(pItem, pMethod);
mr_q 0:d8f2f7d5f31b 88 setup(host, user, password, db);
mr_q 0:d8f2f7d5f31b 89 return MYSQL_PROCESSING;
mr_q 0:d8f2f7d5f31b 90 }
mr_q 0:d8f2f7d5f31b 91 #endif
mr_q 0:d8f2f7d5f31b 92
mr_q 0:d8f2f7d5f31b 93 MySQLResult MySQLClient::sql(string& sqlCommand)
mr_q 0:d8f2f7d5f31b 94 {
mr_q 0:d8f2f7d5f31b 95 if(m_state!=MYSQL_COMMANDS)
mr_q 0:d8f2f7d5f31b 96 return MYSQL_SETUP;
mr_q 0:d8f2f7d5f31b 97 sendCommand(COM_QUERY, (byte*)sqlCommand.data(), sqlCommand.length());
mr_q 0:d8f2f7d5f31b 98 return MYSQL_PROCESSING;
mr_q 0:d8f2f7d5f31b 99 }
mr_q 0:d8f2f7d5f31b 100
mr_q 0:d8f2f7d5f31b 101 MySQLResult MySQLClient::exit()
mr_q 0:d8f2f7d5f31b 102 {
mr_q 0:d8f2f7d5f31b 103 sendCommand(COM_QUIT, NULL, 0);
mr_q 0:d8f2f7d5f31b 104 close();
mr_q 0:d8f2f7d5f31b 105 return MYSQL_OK;
mr_q 0:d8f2f7d5f31b 106 }
mr_q 0:d8f2f7d5f31b 107
mr_q 0:d8f2f7d5f31b 108 void MySQLClient::setOnResult( void (*pMethod)(MySQLResult) )
mr_q 0:d8f2f7d5f31b 109 {
mr_q 0:d8f2f7d5f31b 110 m_pCb = pMethod;
mr_q 0:d8f2f7d5f31b 111 m_pCbItem = NULL;
mr_q 0:d8f2f7d5f31b 112 m_pCbMeth = NULL;
mr_q 0:d8f2f7d5f31b 113 }
mr_q 0:d8f2f7d5f31b 114
mr_q 0:d8f2f7d5f31b 115 #if 0 //Ref only
mr_q 0:d8f2f7d5f31b 116 template<class T>
mr_q 0:d8f2f7d5f31b 117 void MySQLClient::setOnResult( T* pItem, void (T::*pMethod)(MySQLResult) )
mr_q 0:d8f2f7d5f31b 118 {
mr_q 0:d8f2f7d5f31b 119 m_pCb = NULL;
mr_q 0:d8f2f7d5f31b 120 m_pCbItem = (CDummy*) pItem;
mr_q 0:d8f2f7d5f31b 121 m_pCbMeth = (void (CDummy::*)(MySQLResult)) pMethod;
mr_q 0:d8f2f7d5f31b 122 }
mr_q 0:d8f2f7d5f31b 123 #endif
mr_q 0:d8f2f7d5f31b 124
mr_q 0:d8f2f7d5f31b 125 void MySQLClient::setTimeout(int ms)
mr_q 0:d8f2f7d5f31b 126 {
mr_q 0:d8f2f7d5f31b 127 m_timeout = 1000*ms;
mr_q 0:d8f2f7d5f31b 128 }
mr_q 0:d8f2f7d5f31b 129
mr_q 0:d8f2f7d5f31b 130 void MySQLClient::poll() //Called by NetServices
mr_q 0:d8f2f7d5f31b 131 {
mr_q 0:d8f2f7d5f31b 132 if(m_closed)
mr_q 0:d8f2f7d5f31b 133 {
mr_q 0:d8f2f7d5f31b 134 return;
mr_q 0:d8f2f7d5f31b 135 }
mr_q 0:d8f2f7d5f31b 136 if(m_watchdog.read_us()>m_timeout)
mr_q 0:d8f2f7d5f31b 137 {
mr_q 0:d8f2f7d5f31b 138 onTimeout();
mr_q 0:d8f2f7d5f31b 139 }
mr_q 0:d8f2f7d5f31b 140 }
mr_q 0:d8f2f7d5f31b 141
mr_q 0:d8f2f7d5f31b 142 void MySQLClient::resetTimeout()
mr_q 0:d8f2f7d5f31b 143 {
mr_q 0:d8f2f7d5f31b 144 m_watchdog.reset();
mr_q 0:d8f2f7d5f31b 145 m_watchdog.start();
mr_q 0:d8f2f7d5f31b 146 }
mr_q 0:d8f2f7d5f31b 147
mr_q 0:d8f2f7d5f31b 148 void MySQLClient::init()
mr_q 0:d8f2f7d5f31b 149 {
mr_q 0:d8f2f7d5f31b 150 close(); //Remove previous elements
mr_q 0:d8f2f7d5f31b 151 if(!m_closed) //Already opened
mr_q 0:d8f2f7d5f31b 152 return;
mr_q 0:d8f2f7d5f31b 153 m_state = MYSQL_HANDSHAKE;
mr_q 0:d8f2f7d5f31b 154 m_pTCPSocket = new TCPSocket;
mr_q 0:d8f2f7d5f31b 155 m_pTCPSocket->setOnEvent(this, &MySQLClient::onTCPSocketEvent);
mr_q 0:d8f2f7d5f31b 156 m_closed = false;
mr_q 0:d8f2f7d5f31b 157 }
mr_q 0:d8f2f7d5f31b 158
mr_q 0:d8f2f7d5f31b 159 void MySQLClient::close()
mr_q 0:d8f2f7d5f31b 160 {
mr_q 0:d8f2f7d5f31b 161 if(m_closed)
mr_q 0:d8f2f7d5f31b 162 return;
mr_q 0:d8f2f7d5f31b 163 m_state = MYSQL_CLOSED;
mr_q 0:d8f2f7d5f31b 164 m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else
mr_q 0:d8f2f7d5f31b 165 m_watchdog.stop(); //Stop timeout
mr_q 0:d8f2f7d5f31b 166 m_watchdog.reset();
mr_q 0:d8f2f7d5f31b 167 m_pTCPSocket->resetOnEvent();
mr_q 0:d8f2f7d5f31b 168 m_pTCPSocket->close();
mr_q 0:d8f2f7d5f31b 169 delete m_pTCPSocket;
mr_q 0:d8f2f7d5f31b 170 m_pTCPSocket = NULL;
mr_q 0:d8f2f7d5f31b 171 if( m_pDnsReq )
mr_q 0:d8f2f7d5f31b 172 {
mr_q 0:d8f2f7d5f31b 173 m_pDnsReq->close();
mr_q 0:d8f2f7d5f31b 174 delete m_pDnsReq;
mr_q 0:d8f2f7d5f31b 175 m_pDnsReq = NULL;
mr_q 0:d8f2f7d5f31b 176 }
mr_q 0:d8f2f7d5f31b 177 }
mr_q 0:d8f2f7d5f31b 178
mr_q 0:d8f2f7d5f31b 179 void MySQLClient::setup(Host& host, const string& user, const string& password, const string& db) //Setup connection, make DNS Req if necessary
mr_q 0:d8f2f7d5f31b 180 {
mr_q 0:d8f2f7d5f31b 181 init(); //Initialize client in known state, create socket
mr_q 0:d8f2f7d5f31b 182 resetTimeout();
mr_q 0:d8f2f7d5f31b 183 m_host = host;
mr_q 0:d8f2f7d5f31b 184 if(!host.getPort())
mr_q 0:d8f2f7d5f31b 185 host.setPort( MYSQL_PORT ); //Default port
mr_q 0:d8f2f7d5f31b 186
mr_q 0:d8f2f7d5f31b 187 m_user = user;
mr_q 0:d8f2f7d5f31b 188 m_password = password;
mr_q 0:d8f2f7d5f31b 189
mr_q 0:d8f2f7d5f31b 190 m_db = db;
mr_q 0:d8f2f7d5f31b 191
mr_q 0:d8f2f7d5f31b 192 if( !host.getIp().isNull() )
mr_q 0:d8f2f7d5f31b 193 {
mr_q 0:d8f2f7d5f31b 194 connect();
mr_q 0:d8f2f7d5f31b 195 }
mr_q 0:d8f2f7d5f31b 196 else //Need to do a DNS Query...
mr_q 0:d8f2f7d5f31b 197 {
mr_q 0:d8f2f7d5f31b 198 DBG("DNS Query...\n");
mr_q 0:d8f2f7d5f31b 199 m_pDnsReq = new DNSRequest();
mr_q 0:d8f2f7d5f31b 200 m_pDnsReq->setOnReply(this, &MySQLClient::onDNSReply);
mr_q 0:d8f2f7d5f31b 201 m_pDnsReq->resolve(&m_host);
mr_q 0:d8f2f7d5f31b 202 DBG("MySQLClient : DNSRequest %p\n", m_pDnsReq);
mr_q 0:d8f2f7d5f31b 203 }
mr_q 0:d8f2f7d5f31b 204 }
mr_q 0:d8f2f7d5f31b 205
mr_q 0:d8f2f7d5f31b 206 void MySQLClient::connect() //Start Connection
mr_q 0:d8f2f7d5f31b 207 {
mr_q 0:d8f2f7d5f31b 208 resetTimeout();
mr_q 0:d8f2f7d5f31b 209 DBG("Connecting...\n");
mr_q 0:d8f2f7d5f31b 210 m_pTCPSocket->connect(m_host);
mr_q 0:d8f2f7d5f31b 211 m_packetId = 0;
mr_q 0:d8f2f7d5f31b 212 }
mr_q 0:d8f2f7d5f31b 213
mr_q 0:d8f2f7d5f31b 214 void MySQLClient::handleHandshake()
mr_q 0:d8f2f7d5f31b 215 {
mr_q 0:d8f2f7d5f31b 216 readData();
mr_q 0:d8f2f7d5f31b 217 if( ! (( m_len > 1 ) && ( memchr( m_buf + 1, 0, m_len ) != NULL )) )
mr_q 0:d8f2f7d5f31b 218 {
mr_q 0:d8f2f7d5f31b 219 DBG("Connected but could not find pcsz...\n");
mr_q 0:d8f2f7d5f31b 220 onResult(MYSQL_PRTCL);
mr_q 0:d8f2f7d5f31b 221 return;
mr_q 0:d8f2f7d5f31b 222 }
mr_q 0:d8f2f7d5f31b 223
mr_q 0:d8f2f7d5f31b 224 DBG("Connected to server: %d bytes read ; Protocol version %d, mysql-%s.\n", m_len, m_buf[0], &m_buf[1]);
mr_q 0:d8f2f7d5f31b 225
mr_q 0:d8f2f7d5f31b 226 m_pPos = (byte*) memchr( (char*)(m_buf + 1), 0, m_len ) + 1;
mr_q 0:d8f2f7d5f31b 227
mr_q 0:d8f2f7d5f31b 228 sendAuth();
mr_q 0:d8f2f7d5f31b 229 }
mr_q 0:d8f2f7d5f31b 230
mr_q 0:d8f2f7d5f31b 231 void MySQLClient::sendAuth()
mr_q 0:d8f2f7d5f31b 232 {
mr_q 0:d8f2f7d5f31b 233 if( m_len - (m_pPos - m_buf) != 44)
mr_q 0:d8f2f7d5f31b 234 {
mr_q 0:d8f2f7d5f31b 235 //We only support protocol >= mysql-4.1
mr_q 0:d8f2f7d5f31b 236 DBG("Message after pcsz has wrong len (%d != 44)...\n", m_len - (m_pPos - m_buf));
mr_q 0:d8f2f7d5f31b 237 onResult(MYSQL_PRTCL);
mr_q 0:d8f2f7d5f31b 238 return;
mr_q 0:d8f2f7d5f31b 239 }
mr_q 0:d8f2f7d5f31b 240
mr_q 0:d8f2f7d5f31b 241 uint16_t serverFlags = *((uint16_t*)&m_pPos[13]);
mr_q 0:d8f2f7d5f31b 242 DBG("Server capabilities are %04X.\n", serverFlags);
mr_q 0:d8f2f7d5f31b 243
mr_q 0:d8f2f7d5f31b 244 uint32_t clientFlags = CLIENT_CONNECT_WITH_DB | CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION | CLIENT_INTERACTIVE;;
mr_q 0:d8f2f7d5f31b 245
mr_q 0:d8f2f7d5f31b 246 //if(serverFlags & CLIENT_LONG_PASSWORD)
mr_q 0:d8f2f7d5f31b 247
mr_q 0:d8f2f7d5f31b 248 DBG("Using auth 4.1+\n");
mr_q 0:d8f2f7d5f31b 249 //Encrypt pw using scramble
mr_q 0:d8f2f7d5f31b 250 byte scramble[20+20]={0};
mr_q 0:d8f2f7d5f31b 251 memcpy(scramble, m_pPos+4, 8);
mr_q 0:d8f2f7d5f31b 252 memcpy(scramble+8, m_pPos+31, 12); // *(m_pPos+43) == 0 (zero-terminated char*)
mr_q 0:d8f2f7d5f31b 253
mr_q 0:d8f2f7d5f31b 254 byte stage1_hash[20] = {0};
mr_q 0:d8f2f7d5f31b 255 sha1( (byte*)m_password.data(), m_password.length(), stage1_hash );
mr_q 0:d8f2f7d5f31b 256
mr_q 0:d8f2f7d5f31b 257 sha1( stage1_hash, 20, ((byte*)scramble + 20) );
mr_q 0:d8f2f7d5f31b 258
mr_q 0:d8f2f7d5f31b 259 byte token[20] = {0};
mr_q 0:d8f2f7d5f31b 260 sha1( scramble, 40, token );
mr_q 0:d8f2f7d5f31b 261
mr_q 0:d8f2f7d5f31b 262 for(int i=0;i<20;i++)
mr_q 0:d8f2f7d5f31b 263 token[i] = token[i] ^ stage1_hash[i];
mr_q 0:d8f2f7d5f31b 264
mr_q 0:d8f2f7d5f31b 265 clientFlags |= CLIENT_LONG_PASSWORD;
mr_q 0:d8f2f7d5f31b 266
mr_q 0:d8f2f7d5f31b 267 DBG("Building response\n");
mr_q 0:d8f2f7d5f31b 268 //Build response
mr_q 0:d8f2f7d5f31b 269
mr_q 0:d8f2f7d5f31b 270 //BE
mr_q 0:d8f2f7d5f31b 271 *((uint32_t*)&m_buf[0]) = htonl(clientFlags);
mr_q 0:d8f2f7d5f31b 272 *((uint32_t*)&m_buf[4]) = BUF_SIZE; //Max packets size
mr_q 0:d8f2f7d5f31b 273 m_buf[8] = 8; //latin1 charset
mr_q 0:d8f2f7d5f31b 274 memset((char*)(m_buf+9),0,23);
mr_q 0:d8f2f7d5f31b 275 strcpy((char*)(m_buf+32),m_user.c_str());
mr_q 0:d8f2f7d5f31b 276 m_pPos = m_buf + 32 + m_user.length() + 1;
mr_q 0:d8f2f7d5f31b 277 m_pPos[0] = 20;
mr_q 0:d8f2f7d5f31b 278 memcpy((char*)&m_pPos[1],token,20);
mr_q 0:d8f2f7d5f31b 279 strcpy((char*)(m_pPos+21),m_db.c_str());
mr_q 0:d8f2f7d5f31b 280 m_len = 32 + m_user.length() + 1 + 21 + m_db.length() + 1;
mr_q 0:d8f2f7d5f31b 281
mr_q 0:d8f2f7d5f31b 282 //Save first part of scramble in case we need it again
mr_q 0:d8f2f7d5f31b 283 memcpy(&m_buf[BUF_SIZE-8], scramble, 8);
mr_q 0:d8f2f7d5f31b 284
mr_q 0:d8f2f7d5f31b 285 m_state = MYSQL_AUTH;
mr_q 0:d8f2f7d5f31b 286
mr_q 0:d8f2f7d5f31b 287 DBG("Writing data\n");
mr_q 0:d8f2f7d5f31b 288 writeData();
mr_q 0:d8f2f7d5f31b 289 }
mr_q 0:d8f2f7d5f31b 290
mr_q 0:d8f2f7d5f31b 291 void MySQLClient::handleAuthResult()
mr_q 0:d8f2f7d5f31b 292 {
mr_q 0:d8f2f7d5f31b 293 readData();
mr_q 0:d8f2f7d5f31b 294 if(m_len==1 && *m_buf==0xfe)
mr_q 0:d8f2f7d5f31b 295 {
mr_q 0:d8f2f7d5f31b 296 //Re-send auth using 4.0- auth
mr_q 0:d8f2f7d5f31b 297 sendAuth323();
mr_q 0:d8f2f7d5f31b 298 return;
mr_q 0:d8f2f7d5f31b 299 }
mr_q 0:d8f2f7d5f31b 300 m_watchdog.stop(); //Stop timeout
mr_q 0:d8f2f7d5f31b 301 m_watchdog.reset();
mr_q 0:d8f2f7d5f31b 302 if(m_len<2)
mr_q 0:d8f2f7d5f31b 303 {
mr_q 0:d8f2f7d5f31b 304 DBG("Response too short..\n");
mr_q 0:d8f2f7d5f31b 305 onResult(MYSQL_PRTCL);
mr_q 0:d8f2f7d5f31b 306 return;
mr_q 0:d8f2f7d5f31b 307 }
mr_q 0:d8f2f7d5f31b 308 DBG("RC=%d ",m_buf[0]);
mr_q 0:d8f2f7d5f31b 309 if(m_buf[0]==0)
mr_q 0:d8f2f7d5f31b 310 {
mr_q 0:d8f2f7d5f31b 311 m_buf[m_len] = 0;
mr_q 0:d8f2f7d5f31b 312 m_pPos = m_buf + 1;
mr_q 0:d8f2f7d5f31b 313 m_pPos += m_buf[1] +1;
mr_q 0:d8f2f7d5f31b 314 m_pPos += m_pPos[0];
mr_q 0:d8f2f7d5f31b 315 m_pPos += 1;
mr_q 0:d8f2f7d5f31b 316 DBG("(OK) : Server status %d, Message : %s\n", *((uint16_t*)&m_pPos[0]), m_pPos+4);
mr_q 0:d8f2f7d5f31b 317 onResult(MYSQL_OK);
mr_q 0:d8f2f7d5f31b 318 }
mr_q 0:d8f2f7d5f31b 319 else
mr_q 0:d8f2f7d5f31b 320 {
mr_q 0:d8f2f7d5f31b 321 m_buf[m_len] = 0;
mr_q 0:d8f2f7d5f31b 322 DBG("(Error %d) : %s\n", *((uint16_t*)&m_buf[1]), &m_buf[9]); //LE
mr_q 0:d8f2f7d5f31b 323 onResult(MYSQL_AUTHFAILED);
mr_q 0:d8f2f7d5f31b 324 return;
mr_q 0:d8f2f7d5f31b 325 }
mr_q 0:d8f2f7d5f31b 326 m_state = MYSQL_COMMANDS;
mr_q 0:d8f2f7d5f31b 327 }
mr_q 0:d8f2f7d5f31b 328
mr_q 0:d8f2f7d5f31b 329 void MySQLClient::sendAuth323()
mr_q 0:d8f2f7d5f31b 330 {
mr_q 0:d8f2f7d5f31b 331 DBG("Using auth 4.0-\n");
mr_q 0:d8f2f7d5f31b 332 byte scramble[8]={0};
mr_q 0:d8f2f7d5f31b 333
mr_q 0:d8f2f7d5f31b 334 memcpy(scramble, &m_buf[BUF_SIZE-8], 8); //Recover scramble
mr_q 0:d8f2f7d5f31b 335
mr_q 0:d8f2f7d5f31b 336 //memcpy(scramble+8, m_pPos+31, 12); // *(m_pPos+43) == 0 (zero-terminated char*)
mr_q 0:d8f2f7d5f31b 337
mr_q 0:d8f2f7d5f31b 338 byte token[9]={0};
mr_q 0:d8f2f7d5f31b 339
mr_q 0:d8f2f7d5f31b 340 scramble_323((char*)token, (const char*)scramble, m_password.c_str());
mr_q 0:d8f2f7d5f31b 341
mr_q 0:d8f2f7d5f31b 342 DBG("Building response\n");
mr_q 0:d8f2f7d5f31b 343 //Build response
mr_q 0:d8f2f7d5f31b 344
mr_q 0:d8f2f7d5f31b 345 memcpy((char*)m_buf,token,9);
mr_q 0:d8f2f7d5f31b 346 m_len = 9;
mr_q 0:d8f2f7d5f31b 347
mr_q 0:d8f2f7d5f31b 348 #if 0
mr_q 0:d8f2f7d5f31b 349 *((uint32_t*)&m_buf[0]) = htonl(clientFlags);
mr_q 0:d8f2f7d5f31b 350 *((uint32_t*)&m_buf[4]) = BUF_SIZE; //Max packets size
mr_q 0:d8f2f7d5f31b 351 m_buf[8] = 8; //latin1 charset
mr_q 0:d8f2f7d5f31b 352 memset((char*)(m_buf+9),0,23);
mr_q 0:d8f2f7d5f31b 353 strcpy((char*)(m_buf+32),m_user.c_str());
mr_q 0:d8f2f7d5f31b 354 m_pPos = m_buf + 32 + m_user.length() + 1;
mr_q 0:d8f2f7d5f31b 355 m_pPos[0] = 8;
mr_q 0:d8f2f7d5f31b 356 memcpy((char*)&m_pPos[1],token+1,8);
mr_q 0:d8f2f7d5f31b 357 strcpy((char*)(m_pPos+9),m_db.c_str());
mr_q 0:d8f2f7d5f31b 358 m_len = 32 + m_user.length() + 1 + 9 + m_db.length() + 1;
mr_q 0:d8f2f7d5f31b 359 #endif
mr_q 0:d8f2f7d5f31b 360
mr_q 0:d8f2f7d5f31b 361 DBG("Writing data\n");
mr_q 0:d8f2f7d5f31b 362 writeData();
mr_q 0:d8f2f7d5f31b 363 }
mr_q 0:d8f2f7d5f31b 364
mr_q 0:d8f2f7d5f31b 365 void MySQLClient::sendCommand(byte command, byte* arg, int len)
mr_q 0:d8f2f7d5f31b 366 {
mr_q 0:d8f2f7d5f31b 367 DBG("Sending command %d, payload of len %d\n", command, len);
mr_q 0:d8f2f7d5f31b 368 m_packetId=0;//Reset packet ID (New sequence)
mr_q 0:d8f2f7d5f31b 369 m_buf[0] = command;
mr_q 0:d8f2f7d5f31b 370 memcpy(&m_buf[1], arg, len);
mr_q 0:d8f2f7d5f31b 371 m_len = 1 + len;
mr_q 0:d8f2f7d5f31b 372 writeData();
mr_q 0:d8f2f7d5f31b 373 m_watchdog.start();
mr_q 0:d8f2f7d5f31b 374 }
mr_q 0:d8f2f7d5f31b 375
mr_q 0:d8f2f7d5f31b 376 void MySQLClient::handleCommandResult()
mr_q 0:d8f2f7d5f31b 377 {
mr_q 0:d8f2f7d5f31b 378 readData();
mr_q 0:d8f2f7d5f31b 379 m_watchdog.stop(); //Stop timeout
mr_q 0:d8f2f7d5f31b 380 m_watchdog.reset();
mr_q 0:d8f2f7d5f31b 381 if(m_len<2)
mr_q 0:d8f2f7d5f31b 382 {
mr_q 0:d8f2f7d5f31b 383 DBG("Response too short..\n");
mr_q 0:d8f2f7d5f31b 384 onResult(MYSQL_PRTCL);
mr_q 0:d8f2f7d5f31b 385 return;
mr_q 0:d8f2f7d5f31b 386 }
mr_q 0:d8f2f7d5f31b 387 DBG("RC=%d ",m_buf[0]);
mr_q 0:d8f2f7d5f31b 388 if(m_buf[0]==0)
mr_q 0:d8f2f7d5f31b 389 {
mr_q 0:d8f2f7d5f31b 390 DBG("(OK)\n");
mr_q 0:d8f2f7d5f31b 391 onResult(MYSQL_OK);
mr_q 0:d8f2f7d5f31b 392 }
mr_q 0:d8f2f7d5f31b 393 else
mr_q 0:d8f2f7d5f31b 394 {
mr_q 0:d8f2f7d5f31b 395 m_buf[m_len] = 0;
mr_q 0:d8f2f7d5f31b 396 DBG("(SQL Error %d) : %s\n", *((uint16_t*)&m_buf[1]), &m_buf[9]); //LE
mr_q 0:d8f2f7d5f31b 397 onResult(MYSQL_SQL);
mr_q 0:d8f2f7d5f31b 398 return;
mr_q 0:d8f2f7d5f31b 399 }
mr_q 0:d8f2f7d5f31b 400 }
mr_q 0:d8f2f7d5f31b 401
mr_q 0:d8f2f7d5f31b 402 void MySQLClient::readData() //Copy to buf
mr_q 0:d8f2f7d5f31b 403 {
mr_q 0:d8f2f7d5f31b 404 byte head[4];
mr_q 0:d8f2f7d5f31b 405 int ret = m_pTCPSocket->recv((char*)head, 4); //Packet header
mr_q 0:d8f2f7d5f31b 406 m_len = *((uint16_t*)&head[0]);
mr_q 0:d8f2f7d5f31b 407 m_packetId = head[3];
mr_q 0:d8f2f7d5f31b 408 DBG("Packet Id %d of length %d\n", head[3], m_len);
mr_q 0:d8f2f7d5f31b 409 m_packetId++;
mr_q 0:d8f2f7d5f31b 410 if(ret>0)
mr_q 0:d8f2f7d5f31b 411 ret = m_pTCPSocket->recv((char*)m_buf, m_len);
mr_q 0:d8f2f7d5f31b 412 if(ret < 0)//Error
mr_q 0:d8f2f7d5f31b 413 {
mr_q 0:d8f2f7d5f31b 414 onResult(MYSQL_CONN);
mr_q 0:d8f2f7d5f31b 415 return;
mr_q 0:d8f2f7d5f31b 416 }
mr_q 0:d8f2f7d5f31b 417 if(ret < m_len)
mr_q 0:d8f2f7d5f31b 418 {
mr_q 0:d8f2f7d5f31b 419 DBG("WARN: Incomplete packet\n");
mr_q 0:d8f2f7d5f31b 420 }
mr_q 0:d8f2f7d5f31b 421 m_len = ret;
mr_q 0:d8f2f7d5f31b 422 }
mr_q 0:d8f2f7d5f31b 423
mr_q 0:d8f2f7d5f31b 424 void MySQLClient::writeData() //Copy from buf
mr_q 0:d8f2f7d5f31b 425 {
mr_q 0:d8f2f7d5f31b 426 byte head[4] = { 0 };
mr_q 0:d8f2f7d5f31b 427 *((uint16_t*)&head[0]) = m_len;
mr_q 0:d8f2f7d5f31b 428 head[3] = m_packetId;
mr_q 0:d8f2f7d5f31b 429 DBG("Packet Id %d\n", head[3]);
mr_q 0:d8f2f7d5f31b 430 m_packetId++;
mr_q 0:d8f2f7d5f31b 431 int ret = m_pTCPSocket->send((char*)head, 4); //Packet header
mr_q 0:d8f2f7d5f31b 432 if(ret>0)
mr_q 0:d8f2f7d5f31b 433 ret = m_pTCPSocket->send((char*)m_buf, m_len);
mr_q 0:d8f2f7d5f31b 434 if(ret < 0)//Error
mr_q 0:d8f2f7d5f31b 435 {
mr_q 0:d8f2f7d5f31b 436 onResult(MYSQL_CONN);
mr_q 0:d8f2f7d5f31b 437 return;
mr_q 0:d8f2f7d5f31b 438 }
mr_q 0:d8f2f7d5f31b 439 m_len = 0;//FIXME... incomplete packets handling
mr_q 0:d8f2f7d5f31b 440 }
mr_q 0:d8f2f7d5f31b 441
mr_q 0:d8f2f7d5f31b 442 void MySQLClient::onTCPSocketEvent(TCPSocketEvent e)
mr_q 0:d8f2f7d5f31b 443 {
mr_q 0:d8f2f7d5f31b 444 DBG("Event %d in MySQLClient::onTCPSocketEvent()\n", e);
mr_q 0:d8f2f7d5f31b 445
mr_q 0:d8f2f7d5f31b 446 if(m_closed)
mr_q 0:d8f2f7d5f31b 447 {
mr_q 0:d8f2f7d5f31b 448 DBG("WARN: Discarded\n");
mr_q 0:d8f2f7d5f31b 449 return;
mr_q 0:d8f2f7d5f31b 450 }
mr_q 0:d8f2f7d5f31b 451
mr_q 0:d8f2f7d5f31b 452 switch(e)
mr_q 0:d8f2f7d5f31b 453 {
mr_q 0:d8f2f7d5f31b 454 case TCPSOCKET_READABLE: //Incoming data
mr_q 0:d8f2f7d5f31b 455 resetTimeout();
mr_q 0:d8f2f7d5f31b 456 if(m_state == MYSQL_HANDSHAKE)
mr_q 0:d8f2f7d5f31b 457 handleHandshake();
mr_q 0:d8f2f7d5f31b 458 else if(m_state == MYSQL_AUTH)
mr_q 0:d8f2f7d5f31b 459 handleAuthResult();
mr_q 0:d8f2f7d5f31b 460 else if(m_state == MYSQL_COMMANDS)
mr_q 0:d8f2f7d5f31b 461 handleCommandResult();
mr_q 0:d8f2f7d5f31b 462 break;
mr_q 0:d8f2f7d5f31b 463 case TCPSOCKET_WRITEABLE: //We can send data
mr_q 0:d8f2f7d5f31b 464 resetTimeout();
mr_q 0:d8f2f7d5f31b 465 break;
mr_q 0:d8f2f7d5f31b 466 case TCPSOCKET_CONNECTED: //Connected, wait for handshake packet
mr_q 0:d8f2f7d5f31b 467 resetTimeout();
mr_q 0:d8f2f7d5f31b 468 break;
mr_q 0:d8f2f7d5f31b 469 case TCPSOCKET_CONTIMEOUT:
mr_q 0:d8f2f7d5f31b 470 case TCPSOCKET_CONRST:
mr_q 0:d8f2f7d5f31b 471 case TCPSOCKET_CONABRT:
mr_q 0:d8f2f7d5f31b 472 case TCPSOCKET_ERROR:
mr_q 0:d8f2f7d5f31b 473 DBG("Connection error.\n");
mr_q 0:d8f2f7d5f31b 474 onResult(MYSQL_CONN);
mr_q 0:d8f2f7d5f31b 475 case TCPSOCKET_DISCONNECTED:
mr_q 0:d8f2f7d5f31b 476 //There might still be some data available for reading
mr_q 0:d8f2f7d5f31b 477 //So if we are in a reading state, do not close the socket yet
mr_q 0:d8f2f7d5f31b 478 if(m_state != MYSQL_CLOSED)
mr_q 0:d8f2f7d5f31b 479 {
mr_q 0:d8f2f7d5f31b 480 onResult(MYSQL_CONN);
mr_q 0:d8f2f7d5f31b 481 }
mr_q 0:d8f2f7d5f31b 482 DBG("Connection closed by remote host.\n");
mr_q 0:d8f2f7d5f31b 483 break;
mr_q 0:d8f2f7d5f31b 484 }
mr_q 0:d8f2f7d5f31b 485 }
mr_q 0:d8f2f7d5f31b 486
mr_q 0:d8f2f7d5f31b 487 void MySQLClient::onDNSReply(DNSReply r)
mr_q 0:d8f2f7d5f31b 488 {
mr_q 0:d8f2f7d5f31b 489 if(m_closed)
mr_q 0:d8f2f7d5f31b 490 {
mr_q 0:d8f2f7d5f31b 491 DBG("WARN: Discarded\n");
mr_q 0:d8f2f7d5f31b 492 return;
mr_q 0:d8f2f7d5f31b 493 }
mr_q 0:d8f2f7d5f31b 494
mr_q 0:d8f2f7d5f31b 495 if( r != DNS_FOUND )
mr_q 0:d8f2f7d5f31b 496 {
mr_q 0:d8f2f7d5f31b 497 DBG("Could not resolve hostname.\n");
mr_q 0:d8f2f7d5f31b 498 onResult(MYSQL_DNS);
mr_q 0:d8f2f7d5f31b 499 return;
mr_q 0:d8f2f7d5f31b 500 }
mr_q 0:d8f2f7d5f31b 501
mr_q 0:d8f2f7d5f31b 502 DBG("DNS Resolved to %d.%d.%d.%d.\n",m_host.getIp()[0],m_host.getIp()[1],m_host.getIp()[2],m_host.getIp()[3]);
mr_q 0:d8f2f7d5f31b 503 //If no error, m_host has been updated by m_pDnsReq so we're set to go !
mr_q 0:d8f2f7d5f31b 504 m_pDnsReq->close();
mr_q 0:d8f2f7d5f31b 505 delete m_pDnsReq;
mr_q 0:d8f2f7d5f31b 506 m_pDnsReq = NULL;
mr_q 0:d8f2f7d5f31b 507 connect();
mr_q 0:d8f2f7d5f31b 508 }
mr_q 0:d8f2f7d5f31b 509
mr_q 0:d8f2f7d5f31b 510 void MySQLClient::onResult(MySQLResult r) //Called when exchange completed or on failure
mr_q 0:d8f2f7d5f31b 511 {
mr_q 0:d8f2f7d5f31b 512 if(m_pCbItem && m_pCbMeth)
mr_q 0:d8f2f7d5f31b 513 (m_pCbItem->*m_pCbMeth)(r);
mr_q 0:d8f2f7d5f31b 514 else if(m_pCb)
mr_q 0:d8f2f7d5f31b 515 m_pCb(r);
mr_q 0:d8f2f7d5f31b 516
mr_q 0:d8f2f7d5f31b 517 if( (r==MYSQL_DNS) || (r==MYSQL_PRTCL) || (r==MYSQL_AUTHFAILED) || (r==MYSQL_TIMEOUT) || (r==MYSQL_CONN) ) //Fatal error, close connection
mr_q 0:d8f2f7d5f31b 518 close();
mr_q 0:d8f2f7d5f31b 519 }
mr_q 0:d8f2f7d5f31b 520
mr_q 0:d8f2f7d5f31b 521 void MySQLClient::onTimeout() //Connection has timed out
mr_q 0:d8f2f7d5f31b 522 {
mr_q 0:d8f2f7d5f31b 523 DBG("Timed out.\n");
mr_q 0:d8f2f7d5f31b 524 onResult(MYSQL_TIMEOUT);
mr_q 0:d8f2f7d5f31b 525 close();
mr_q 0:d8f2f7d5f31b 526 }