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