Search in sources :

Example 1 with HeaderPacket

use of com.alibaba.otter.canal.parse.driver.mysql.packets.HeaderPacket 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)

Example 2 with HeaderPacket

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

the class ChannelBufferHelper method assembleHeaderPacket.

public final HeaderPacket assembleHeaderPacket(ChannelBuffer buffer) {
    HeaderPacket header = new HeaderPacket();
    byte[] headerBytes = new byte[MSC.HEADER_PACKET_LENGTH];
    buffer.readBytes(headerBytes);
    header.fromBytes(headerBytes);
    return header;
}
Also used : HeaderPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.HeaderPacket) PacketWithHeaderPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.PacketWithHeaderPacket)

Example 3 with HeaderPacket

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

the class ChannelBufferHelper method createHeaderWithPacketNumberPlusOne.

public final ChannelBuffer createHeaderWithPacketNumberPlusOne(int bodyLength, byte packetNumber) {
    HeaderPacket header = new HeaderPacket();
    header.setPacketBodyLength(bodyLength);
    header.setPacketSequenceNumber((byte) (packetNumber + 1));
    return ChannelBuffers.wrappedBuffer(header.toBytes());
}
Also used : HeaderPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.HeaderPacket) PacketWithHeaderPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.PacketWithHeaderPacket)

Example 4 with HeaderPacket

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

the class PacketManager method write.

public static void write(SocketChannel ch, byte[] body, byte packetSeqNumber) throws IOException {
    HeaderPacket header = new HeaderPacket();
    header.setPacketBodyLength(body.length);
    header.setPacketSequenceNumber(packetSeqNumber);
    write(ch, new ByteBuffer[] { ByteBuffer.wrap(header.toBytes()), ByteBuffer.wrap(body) });
}
Also used : HeaderPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.HeaderPacket)

Example 5 with HeaderPacket

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

the class PacketManager method readHeader.

public static HeaderPacket readHeader(SocketChannel ch, int len) throws IOException {
    HeaderPacket header = new HeaderPacket();
    header.fromBytes(readBytesAsBuffer(ch, len).array());
    return header;
}
Also used : HeaderPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.HeaderPacket)

Aggregations

HeaderPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.HeaderPacket)11 PacketWithHeaderPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.PacketWithHeaderPacket)3 BinlogDumpCommandPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.client.BinlogDumpCommandPacket)2 ErrorPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.server.ErrorPacket)2 IOException (java.io.IOException)2 ClientAuthenticationPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.client.ClientAuthenticationPacket)1 QuitCommandPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.client.QuitCommandPacket)1 HandshakeInitializationPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.server.HandshakeInitializationPacket)1 Reply323Packet (com.alibaba.otter.canal.parse.driver.mysql.packets.server.Reply323Packet)1