Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:41:36 2011 +0000
Revision:
0:8f5825f330b0
setDataLen hacked to 180bytes

Who changed what in which revision?

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