EthernetNetIf Compatibility.

Dependents:   XBeeWiFi_SPI_example

Fork of NetServicesSource by Donatien Garnier

Committer:
donatien
Date:
Fri Jul 09 14:46:47 2010 +0000
Revision:
4:fd826cad83c0
Parent:
0:632c9925f013
Child:
5:dd63a1e02b1b

        

Who changed what in which revision?

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