Search in sources :

Example 1 with ClientAuthenticationPacket

use of com.alibaba.otter.canal.parse.driver.mysql.packets.client.ClientAuthenticationPacket in project canal by alibaba.

the class MysqlConnector method negotiate.

private void negotiate(SocketChannel channel) throws IOException {
    HeaderPacket header = PacketManager.readHeader(channel, 4);
    byte[] body = PacketManager.readBytes(channel, header.getPacketBodyLength());
    if (body[0] < 0) {
        // check field_count
        if (body[0] == -1) {
            ErrorPacket error = new ErrorPacket();
            error.fromBytes(body);
            throw new IOException("handshake exception:\n" + error.toString());
        } else if (body[0] == -2) {
            throw new IOException("Unexpected EOF packet at handshake phase.");
        } else {
            throw new IOException("unpexpected packet with field_count=" + body[0]);
        }
    }
    HandshakeInitializationPacket handshakePacket = new HandshakeInitializationPacket();
    handshakePacket.fromBytes(body);
    // 记录一下connection
    connectionId = handshakePacket.threadId;
    logger.info("handshake initialization packet received, prepare the client authentication packet to send");
    ClientAuthenticationPacket clientAuth = new ClientAuthenticationPacket();
    clientAuth.setCharsetNumber(charsetNumber);
    clientAuth.setUsername(username);
    clientAuth.setPassword(password);
    clientAuth.setServerCapabilities(handshakePacket.serverCapabilities);
    clientAuth.setDatabaseName(defaultSchema);
    clientAuth.setScrumbleBuff(joinAndCreateScrumbleBuff(handshakePacket));
    byte[] clientAuthPkgBody = clientAuth.toBytes();
    HeaderPacket h = new HeaderPacket();
    h.setPacketBodyLength(clientAuthPkgBody.length);
    h.setPacketSequenceNumber((byte) (header.getPacketSequenceNumber() + 1));
    PacketManager.write(channel, new ByteBuffer[] { ByteBuffer.wrap(h.toBytes()), ByteBuffer.wrap(clientAuthPkgBody) });
    logger.info("client authentication packet is sent out.");
    // check auth result
    header = null;
    header = PacketManager.readHeader(channel, 4);
    body = null;
    body = PacketManager.readBytes(channel, header.getPacketBodyLength());
    assert body != null;
    if (body[0] < 0) {
        if (body[0] == -1) {
            ErrorPacket err = new ErrorPacket();
            err.fromBytes(body);
            throw new IOException("Error When doing Client Authentication:" + err.toString());
        } else if (body[0] == -2) {
            auth323(channel, header.getPacketSequenceNumber(), handshakePacket.seed);
        // throw new
        // IOException("Unexpected EOF packet at Client Authentication.");
        } else {
            throw new IOException("unpexpected packet with field_count=" + body[0]);
        }
    }
}
Also used : HeaderPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.HeaderPacket) ErrorPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.server.ErrorPacket) ClientAuthenticationPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.client.ClientAuthenticationPacket) HandshakeInitializationPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.server.HandshakeInitializationPacket) IOException(java.io.IOException)

Aggregations

HeaderPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.HeaderPacket)1 ClientAuthenticationPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.client.ClientAuthenticationPacket)1 ErrorPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.server.ErrorPacket)1 HandshakeInitializationPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.server.HandshakeInitializationPacket)1 IOException (java.io.IOException)1