Experimental HTTPClient with proxy support

Committer:
igorsk
Date:
Wed Jun 29 16:01:58 2011 +0000
Revision:
0:b56b6a05cad4

        

Who changed what in which revision?

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