modded version Dirk-Willem van Gulik's Bonjour/Zerconf library http://mbed.org/users/dirkx/code/Bonjour/

Dependents:   OSCtoCVConverter

Fork of Bonjour by Dirk-Willem van Gulik (NXP/mbed)

Committer:
casiotone401
Date:
Thu Oct 16 14:13:21 2014 +0000
Revision:
8:275256b5d807
Parent:
0:355018f44c9f
minor change

Who changed what in which revision?

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