I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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