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

Committer:
RodColeman
Date:
Thu Sep 08 10:48:09 2011 +0000
Revision:
0:850eacf3e945
revised fixed length to 178 bytes

Who changed what in which revision?

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