change to VodafoneUSB Modem interface

Fork of MySQLClient by Donatien Garnier

Committer:
loc_tran17
Date:
Fri Nov 22 07:38:25 2013 +0000
Revision:
6:cb3bd3012d79
trial with MySQLClient

Who changed what in which revision?

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