Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.

Committer:
andrewbonney
Date:
Fri Apr 08 14:39:41 2011 +0000
Revision:
0:ec559500a63f

        

Who changed what in which revision?

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