Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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