internal InitialHandshakePacket(ByteArrayReader reader) { reader.ReadByte(c_protocolVersion); ServerVersion = reader.ReadNullTerminatedByteString(); ConnectionId = reader.ReadInt32(); AuthPluginData = reader.ReadByteString(8); reader.ReadByte(0); var capabilityFlagsLow = reader.ReadUInt16(); if (reader.BytesRemaining > 0) { var charSet = (CharacterSet)reader.ReadByte(); ServerStatus status = (ServerStatus)reader.ReadInt16(); var capabilityFlagsHigh = reader.ReadUInt16(); ProtocolCapabilities = (ProtocolCapabilities)(capabilityFlagsHigh << 16 | capabilityFlagsLow); var authPluginDataLength = reader.ReadByte(); var unused = reader.ReadByteString(10); if ((ProtocolCapabilities & ProtocolCapabilities.SecureConnection) != 0) { var authPluginData2 = reader.ReadByteString(Math.Max(13, authPluginDataLength - 8)); var concatenated = new byte[AuthPluginData.Length + authPluginData2.Length]; Buffer.BlockCopy(AuthPluginData, 0, concatenated, 0, AuthPluginData.Length); Buffer.BlockCopy(authPluginData2, 0, concatenated, AuthPluginData.Length, authPluginData2.Length); AuthPluginData = concatenated; } if ((ProtocolCapabilities & ProtocolCapabilities.PluginAuth) != 0) { AuthPluginName = Encoding.UTF8.GetString(reader.ReadNullTerminatedByteString()); } } }
private async Task GetRealServerDetailsAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) { try { await SendAsync(QueryPayload.Create("SELECT CONNECTION_ID(), VERSION();"), ioBehavior, cancellationToken).ConfigureAwait(false); // column count: 2 await ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false); // CONNECTION_ID() and VERSION() columns await ReceiveReplyAsync(ioBehavior, CancellationToken.None).ConfigureAwait(false); await ReceiveReplyAsync(ioBehavior, CancellationToken.None).ConfigureAwait(false); PayloadData payload; if (!SupportsDeprecateEof) { payload = await ReceiveReplyAsync(ioBehavior, CancellationToken.None).ConfigureAwait(false); EofPayload.Create(payload); } // first (and only) row int? connectionId = default; string serverVersion = null; payload = await ReceiveReplyAsync(ioBehavior, CancellationToken.None).ConfigureAwait(false); var reader = new ByteArrayReader(payload.ArraySegment); var length = reader.ReadLengthEncodedIntegerOrNull(); if (length != -1) { connectionId = int.Parse(Encoding.UTF8.GetString(reader.ReadByteString(length)), CultureInfo.InvariantCulture); } length = reader.ReadLengthEncodedIntegerOrNull(); if (length != -1) { serverVersion = Encoding.UTF8.GetString(reader.ReadByteString(length)); } // OK/EOF payload payload = await ReceiveReplyAsync(ioBehavior, CancellationToken.None).ConfigureAwait(false); if (OkPayload.IsOk(payload, SupportsDeprecateEof)) { OkPayload.Create(payload, SupportsDeprecateEof); } else { EofPayload.Create(payload); } if (connectionId.HasValue && serverVersion != null) { ConnectionId = connectionId.Value; ServerVersion = new ServerVersion(serverVersion); } } catch (MySqlException) { } }
internal InitialHandshakePacket(ByteArrayReader reader) { reader.ReadByte(c_protocolVersion); ServerVersion = reader.ReadNullTerminatedByteString(); var connectionId = reader.ReadInt32(); AuthPluginData = reader.ReadByteString(8); reader.ReadByte(0); var capabilityFlagsLow = reader.ReadUInt16(); if (reader.BytesRemaining > 0) { var charSet = (CharacterSet) reader.ReadByte(); ServerStatus status = (ServerStatus) reader.ReadInt16(); var capabilityFlagsHigh = reader.ReadUInt16(); ProtocolCapabilities = (ProtocolCapabilities) (capabilityFlagsHigh << 16 | capabilityFlagsLow); var authPluginDataLength = reader.ReadByte(); var unused = reader.ReadByteString(10); if (ProtocolCapabilities.HasFlag(ProtocolCapabilities.SecureConnection) && authPluginDataLength > 0) { var authPluginData2 = reader.ReadByteString(Math.Max(13, authPluginDataLength - 8)); var concatenated = new byte[AuthPluginData.Length + authPluginData2.Length]; Buffer.BlockCopy(AuthPluginData, 0, concatenated, 0, AuthPluginData.Length); Buffer.BlockCopy(authPluginData2, 0, concatenated, AuthPluginData.Length, authPluginData2.Length); AuthPluginData = concatenated; } if (ProtocolCapabilities.HasFlag(ProtocolCapabilities.PluginAuth)) AuthPluginName = Encoding.UTF8.GetString(reader.ReadNullTerminatedByteString()); } }
public static InitialHandshakePayload Create(ReadOnlySpan <byte> span) { var reader = new ByteArrayReader(span); reader.ReadByte(c_protocolVersion); var serverVersion = reader.ReadNullTerminatedByteString(); var connectionId = reader.ReadInt32(); byte[] authPluginData = null; var authPluginData1 = reader.ReadByteString(8); string authPluginName = null; reader.ReadByte(0); var protocolCapabilities = (ProtocolCapabilities)reader.ReadUInt16(); if (reader.BytesRemaining > 0) { var charSet = (CharacterSet)reader.ReadByte(); var status = (ServerStatus)reader.ReadInt16(); var capabilityFlagsHigh = reader.ReadUInt16(); protocolCapabilities |= (ProtocolCapabilities)((ulong)capabilityFlagsHigh << 16); var authPluginDataLength = reader.ReadByte(); var unused = reader.ReadByteString(6); long extendedCapabilites = reader.ReadInt32(); if ((protocolCapabilities & ProtocolCapabilities.LongPassword) == 0) { // MariaDB clears the CLIENT_LONG_PASSWORD flag to indicate it's not a MySQL Server protocolCapabilities |= (ProtocolCapabilities)(extendedCapabilites << 32); } if ((protocolCapabilities & ProtocolCapabilities.SecureConnection) != 0) { var authPluginData2 = reader.ReadByteString(Math.Max(13, authPluginDataLength - 8)); authPluginData = new byte[authPluginData1.Length + authPluginData2.Length]; authPluginData1.CopyTo(authPluginData); authPluginData2.CopyTo(new Span <byte>(authPluginData).Slice(authPluginData1.Length)); } if ((protocolCapabilities & ProtocolCapabilities.PluginAuth) != 0) { authPluginName = Encoding.UTF8.GetString(reader.ReadNullOrEofTerminatedByteString()); } } if (authPluginData is null) { authPluginData = authPluginData1.ToArray(); } if (reader.BytesRemaining != 0) { throw new FormatException("Extra bytes at end of payload."); } return(new InitialHandshakePayload(protocolCapabilities, serverVersion.ToArray(), connectionId, authPluginData, authPluginName)); }
public static ErrorPayload Create(PayloadData payload) { var reader = new ByteArrayReader(payload.ArraySegment); reader.ReadByte(Signature); var errorCode = reader.ReadUInt16(); reader.ReadByte(0x23); var state = Encoding.ASCII.GetString(reader.ReadByteString(5)); var message = Encoding.UTF8.GetString(reader.ReadByteString(payload.ArraySegment.Count - 9)); return new ErrorPayload(errorCode, state, message); }
public static ErrorPayload Create(PayloadData payload) { var reader = new ByteArrayReader(payload.ArraySegment); reader.ReadByte(Signature); var errorCode = reader.ReadUInt16(); reader.ReadByte(0x23); var state = Encoding.ASCII.GetString(reader.ReadByteString(5)); var message = Encoding.UTF8.GetString(reader.ReadByteString(payload.ArraySegment.Count - 9)); return(new ErrorPayload(errorCode, state, message)); }
public static AuthenticationMoreDataPayload Create(PayloadData payload) { var reader = new ByteArrayReader(payload.ArraySegment); reader.ReadByte(Signature); return(new AuthenticationMoreDataPayload(reader.ReadByteString(reader.BytesRemaining))); }
public static AuthenticationMoreDataPayload Create(ReadOnlySpan <byte> span) { var reader = new ByteArrayReader(span); reader.ReadByte(Signature); return(new AuthenticationMoreDataPayload(reader.ReadByteString(reader.BytesRemaining).ToArray())); }
public static InitialHandshakePayload Create(PayloadData payload) { var reader = new ByteArrayReader(payload.ArraySegment); reader.ReadByte(c_protocolVersion); var serverVersion = reader.ReadNullTerminatedByteString(); var connectionId = reader.ReadInt32(); byte[] authPluginData = null; var authPluginData1 = reader.ReadByteString(8); string authPluginName = null; reader.ReadByte(0); var protocolCapabilities = (ProtocolCapabilities)reader.ReadUInt16(); if (reader.BytesRemaining > 0) { var charSet = (CharacterSet)reader.ReadByte(); var status = (ServerStatus)reader.ReadInt16(); var capabilityFlagsHigh = reader.ReadUInt16(); protocolCapabilities |= (ProtocolCapabilities)(capabilityFlagsHigh << 16); var authPluginDataLength = reader.ReadByte(); var unused = reader.ReadByteString(10); if ((protocolCapabilities & ProtocolCapabilities.SecureConnection) != 0) { var authPluginData2 = reader.ReadByteString(Math.Max(13, authPluginDataLength - 8)); authPluginData = new byte[authPluginData1.Length + authPluginData2.Length]; authPluginData1.CopyTo(authPluginData); authPluginData2.CopyTo(new Span <byte>(authPluginData).Slice(authPluginData1.Length)); } if ((protocolCapabilities & ProtocolCapabilities.PluginAuth) != 0) { authPluginName = Encoding.UTF8.GetString(reader.ReadNullOrEofTerminatedByteString()); } } if (authPluginData == null) { authPluginData = authPluginData1.ToArray(); } if (reader.BytesRemaining != 0) { throw new FormatException("Extra bytes at end of payload."); } return(new InitialHandshakePayload(protocolCapabilities, serverVersion.ToArray(), connectionId, authPluginData, authPluginName)); }
public static AuthenticationMethodSwitchRequestPayload Create(PayloadData payload) { var reader = new ByteArrayReader(payload.ArraySegment); reader.ReadByte(Signature); var name = Encoding.UTF8.GetString(reader.ReadNullTerminatedByteString()); var data = reader.ReadByteString(reader.BytesRemaining); return new AuthenticationMethodSwitchRequestPayload(name, data); }
public static LocalInfilePayload Create(PayloadData payload) { var reader = new ByteArrayReader(payload.ArraySegment); reader.ReadByte(Signature); var fileName = Encoding.UTF8.GetString(reader.ReadByteString(reader.BytesRemaining)); return(new LocalInfilePayload(fileName)); }
public static LocalInfilePayload Create(ReadOnlySpan <byte> span) { var reader = new ByteArrayReader(span); reader.ReadByte(Signature); var fileName = Encoding.UTF8.GetString(reader.ReadByteString(reader.BytesRemaining)); return(new LocalInfilePayload(fileName)); }
public static AuthenticationMethodSwitchRequestPayload Create(PayloadData payload) { var reader = new ByteArrayReader(payload.ArraySegment); reader.ReadByte(Signature); var name = Encoding.UTF8.GetString(reader.ReadNullTerminatedByteString()); var data = reader.ReadByteString(reader.BytesRemaining); return(new AuthenticationMethodSwitchRequestPayload(name, data)); }
public static ErrorPayload Create(PayloadData payload) { var reader = new ByteArrayReader(payload.ArraySegment); reader.ReadByte(Signature); var errorCode = reader.ReadUInt16(); var stateMarker = Encoding.ASCII.GetString(reader.ReadByteString(1)); string state, message; if (stateMarker == "#") { state = Encoding.ASCII.GetString(reader.ReadByteString(5)); message = Encoding.UTF8.GetString(reader.ReadByteString(payload.ArraySegment.Count - 9)); } else { state = "HY000"; message = stateMarker + Encoding.UTF8.GetString(reader.ReadByteString(payload.ArraySegment.Count - 4)); } return(new ErrorPayload(errorCode, state, message)); }
public static ErrorPayload Create(ReadOnlySpan <byte> span) { var reader = new ByteArrayReader(span); reader.ReadByte(Signature); var errorCode = reader.ReadUInt16(); var stateMarker = Encoding.ASCII.GetString(reader.ReadByteString(1)); string state, message; if (stateMarker == "#") { state = Encoding.ASCII.GetString(reader.ReadByteString(5)); message = Encoding.UTF8.GetString(reader.ReadByteString(span.Length - 9)); } else { state = "HY000"; message = stateMarker + Encoding.UTF8.GetString(reader.ReadByteString(span.Length - 4)); } return(new ErrorPayload(errorCode, state, message)); }
public static AuthenticationMethodSwitchRequestPayload Create(ReadOnlySpan <byte> span) { var reader = new ByteArrayReader(span); reader.ReadByte(Signature); string name; byte[] data; if (span.Length == 1) { // if the packet is just the header byte (0xFE), it's an "Old Authentication Method Switch Request Packet" // (possibly sent by a server that doesn't support CLIENT_PLUGIN_AUTH) name = "mysql_old_password"; data = new byte[0]; } else { name = Encoding.UTF8.GetString(reader.ReadNullTerminatedByteString()); data = reader.ReadByteString(reader.BytesRemaining).ToArray(); } return(new AuthenticationMethodSwitchRequestPayload(name, data)); }