ReadByte() публичный Метод

public ReadByte ( ) : byte
Результат byte
Пример #1
0
        internal void ReadOk(bool read)
        {
            try
            {
                if (read)
                {
                    packet = stream.ReadPacket();
                }
                byte marker = (byte)packet.ReadByte();
                if (marker != 0)
                {
                    throw new MySqlException("Out of sync with server", true, null);
                }

                packet.ReadFieldLength(); /* affected rows */
                packet.ReadFieldLength(); /* last insert id */
                if (packet.HasMoreData)
                {
                    serverStatus = (ServerStatusFlags)packet.ReadInteger(2);
                    packet.ReadInteger(2); /* warning count */
                    if (packet.HasMoreData)
                    {
                        packet.ReadLenString(); /* message */
                    }
                }
            }
            catch (MySqlException ex)
            {
                HandleException(ex);
                throw;
            }
        }
Пример #2
0
        /// <summary>
        /// ReadPacket is called by NativeDriver to start reading the next
        /// packet on the stream.
        /// </summary>
        public MySqlPacket ReadPacket()
        {
            //Debug.Assert(packet.Position == packet.Length);

            // make sure we have read all the data from the previous packet
            //Debug.Assert(HasMoreData == false, "HasMoreData is true in OpenPacket");

            LoadPacket();

            // now we check if this packet is a server error
            if (packet.Buffer[0] == 0xff)
            {
                packet.ReadByte(); // read off the 0xff

                int    code = packet.ReadInteger(2);
                string msg  = String.Empty;

                if (packet.Version.isAtLeast(5, 5, 0))
                {
                    msg = packet.ReadString(Encoding.UTF8);
                }
                else
                {
                    msg = packet.ReadString();
                }

                if (msg.StartsWith("#", StringComparison.Ordinal))
                {
                    msg.Substring(1, 5); /* state code */
                    msg = msg.Substring(6);
                }
                throw new MySqlException(msg, code);
            }
            return(packet);
        }
Пример #3
0
        private void GetColumnData(MySqlField field)
        {
            stream.Encoding          = Encoding;
            packet                   = stream.ReadPacket();
            field.Encoding           = Encoding;
            field.CatalogName        = packet.ReadLenString();
            field.DatabaseName       = packet.ReadLenString();
            field.TableName          = packet.ReadLenString();
            field.RealTableName      = packet.ReadLenString();
            field.ColumnName         = packet.ReadLenString();
            field.OriginalColumnName = packet.ReadLenString();
            packet.ReadByte();
            field.CharacterSetIndex = packet.ReadInteger(2);
            field.ColumnLength      = packet.ReadInteger(4);
            MySqlDbType type = (MySqlDbType)packet.ReadByte();
            ColumnFlags colFlags;

            if ((connectionFlags & ClientFlags.LONG_FLAG) != 0)
            {
                colFlags = (ColumnFlags)packet.ReadInteger(2);
            }
            else
            {
                colFlags = (ColumnFlags)packet.ReadByte();
            }
            field.Scale = (byte)packet.ReadByte();

            if (packet.HasMoreData)
            {
                packet.ReadInteger(2);                 // reserved
            }

            if (type == MySqlDbType.Decimal || type == MySqlDbType.NewDecimal)
            {
                field.Precision = (byte)(field.ColumnLength - 2);
                if ((colFlags & ColumnFlags.UNSIGNED) != 0)
                {
                    field.Precision++;
                }
            }

            field.SetTypeAndFlags(type, colFlags);
        }
Пример #4
0
        public int PrepareStatement(string sql, ref MySqlField[] parameters)
        {
            //TODO: check this
            //ClearFetchedRow();

            packet.Length = sql.Length * 4 + 5;
            byte[] buffer = packet.Buffer;
            int    len    = Encoding.GetBytes(sql, 0, sql.Length, packet.Buffer, 5);

            packet.Position = len + 5;
            buffer[4]       = (byte)DBCmd.PREPARE;
            ExecutePacket(packet);

            packet = stream.ReadPacket();

            int marker = packet.ReadByte();

            if (marker != 0)
            {
                throw new MySqlException("Expected prepared statement marker");
            }

            int statementId = packet.ReadInteger(4);
            int numCols     = packet.ReadInteger(2);
            int numParams   = packet.ReadInteger(2);

            //TODO: find out what this is needed for
            packet.ReadInteger(3);
            if (numParams > 0)
            {
                parameters = owner.GetColumns(numParams);
                // we set the encoding for each parameter back to our connection encoding
                // since we can't trust what is coming back from the server
                for (int i = 0; i < parameters.Length; i++)
                {
                    parameters[i].Encoding = Encoding;
                }
            }

            if (numCols > 0)
            {
                while (numCols-- > 0)
                {
                    packet = stream.ReadPacket();
                    //TODO: handle streaming packets
                }

                ReadEOF();
            }

            return(statementId);
        }
Пример #5
0
        private void AuthenticateSSPI()
        {
            string targetName = ""; // target name (required by Kerberos)

            // First packet sent by server should include target name (for
            // Kerberos) as UTF8 string. It might however be prepended by junk
            // at the start of the string (0xfe"authentication_win_client"\0,
            // see Bug#57442), this junk will be ignored. Target name can also
            // be an empty string if server is not running in a domain environment,
            // in this case authentication will fallback to NTLM.

            // Note that 0xfe byte at the start could also indicate that windows
            // authentication is not supported by sérver, we throw an exception
            // if this happens.

            packet = stream.ReadPacket();
            byte b = packet.ReadByte();

            if (b == 0xfe)
            {
                string authMethod = packet.ReadString();
                if (authMethod.Equals(AuthenticationWindowsPlugin))
                {
                    targetName = packet.ReadString(Encoding.UTF8);
                }
                else
                {
                    // User has requested Windows authentication,  bail out.
                    throw new MySqlException("unexpected authentication method " +
                                             authMethod);
                }
            }
            else
            {
                targetName = Encoding.UTF8.GetString(packet.Buffer, 0, packet.Buffer.Length);
            }

            // Do SSPI authentication handshake
            SSPI sspi = new SSPI(targetName, stream.BaseStream, stream.SequenceByte, version);

            sspi.AuthenticateClient();

            // read ok packet.
            packet = stream.ReadPacket();
            ReadOk(false);
        }
Пример #6
0
        public void Open()
        {
            // connect to one of our specified hosts
            try
            {
                baseStream = StreamCreator.GetStream(Settings);
                //#if !CF && !RT
                //         if (Settings.IncludeSecurityAsserts)
                //            MySqlSecurityPermission.CreatePermissionSet(false).Assert();
                //#endif
            }
            catch (System.Security.SecurityException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new MySqlException("Resources.UnableToConnectToHost",
                                         (int)MySqlErrorCode.UnableToConnectToHost, ex);
            }
            if (baseStream == null)
            {
                throw new MySqlException("Resources.UnableToConnectToHost",
                                         (int)MySqlErrorCode.UnableToConnectToHost);
            }

            int maxSinglePacket = 255 * 255 * 255;

            stream = new MySqlStream(baseStream, Encoding, false);

            stream.ResetTimeout((int)Settings.ConnectionTimeout * 1000);

            // read off the welcome packet and parse out it's values
            packet = stream.ReadPacket();
            int    protocol      = packet.ReadByte();
            string versionString = packet.ReadString();

            owner.isFabric = versionString.EndsWith("fabric", StringComparison.OrdinalIgnoreCase);
            version        = DBVersion.Parse(versionString);
            if (!owner.isFabric && !version.isAtLeast(5, 0, 0))
            {
                throw new NotSupportedException("Resources.ServerTooOld");
            }

            threadId = packet.ReadInteger(4);

            byte[] seedPart1 = packet.ReadStringAsBytes();

            maxSinglePacket = (256 * 256 * 256) - 1;

            // read in Server capabilities if they are provided
            ClientFlags serverCaps = 0;

            if (packet.HasMoreData)
            {
                serverCaps = (ClientFlags)packet.ReadInteger(2);
            }

            /* New protocol with 16 bytes to describe server characteristics */
            owner.ConnectionCharSetIndex = (int)packet.ReadByte();

            serverStatus = (ServerStatusFlags)packet.ReadInteger(2);

            // Since 5.5, high bits of server caps are stored after status.
            // Previously, it was part of reserved always 0x00 13-byte filler.
            uint serverCapsHigh = (uint)packet.ReadInteger(2);

            serverCaps |= (ClientFlags)(serverCapsHigh << 16);

            packet.Position += 11;
            byte[] seedPart2 = packet.ReadStringAsBytes();
            encryptionSeed = new byte[seedPart1.Length + seedPart2.Length];
            seedPart1.CopyTo(encryptionSeed, 0);
            seedPart2.CopyTo(encryptionSeed, seedPart1.Length);

            string authenticationMethod = "";

            if ((serverCaps & ClientFlags.PLUGIN_AUTH) != 0)
            {
                authenticationMethod = packet.ReadString();
            }
            else
            {
                // Some MySql versions like 5.1, don't give name of plugin, default to native password.
                authenticationMethod = "mysql_native_password";
            }

            // based on our settings, set our connection flags
            SetConnectionFlags(serverCaps);

            packet.Clear();
            packet.WriteInteger((int)connectionFlags, 4);
            packet.WriteInteger(maxSinglePacket, 4);
            packet.WriteByte(33);             //character set utf-8
            packet.Write(new byte[23]);

#if !CF && !RT
            if ((serverCaps & ClientFlags.SSL) == 0)
            {
                if ((Settings.SslMode != MySqlSslMode.None) &&
                    (Settings.SslMode != MySqlSslMode.Preferred))
                {
                    // Client requires SSL connections.
                    string message = String.Format("Resources.NoServerSSLSupport",

                                                   Settings.Server);
                    throw new MySqlException(message);
                }
            }
            else if (Settings.SslMode != MySqlSslMode.None)
            {
                stream.SendPacket(packet);
                StartSSL();
                packet.Clear();
                packet.WriteInteger((int)connectionFlags, 4);
                packet.WriteInteger(maxSinglePacket, 4);
                packet.WriteByte(33);                 //character set utf-8
                packet.Write(new byte[23]);
            }
#endif

#if RT
            if (Settings.SslMode != MySqlSslMode.None)
            {
                throw new NotImplementedException("SSL not supported in this WinRT release.");
            }
#endif

            Authenticate(authenticationMethod, false);

            // if we are using compression, then we use our CompressedStream class
            // to hide the ugliness of managing the compression
            if ((connectionFlags & ClientFlags.COMPRESS) != 0)
            {
                stream = new MySqlStream(baseStream, Encoding, true);
            }

            // give our stream the server version we are connected to.
            // We may have some fields that are read differently based
            // on the version of the server we are connected to.
            packet.Version      = version;
            stream.MaxBlockSize = maxSinglePacket;
        }
Пример #7
0
		IMySqlValue IMySqlValue.ReadValue(MySqlPacket packet, long length, bool nullVal)
		{
			if (nullVal)
				return new MySqlUByte(true);

			if (length == -1)
				return new MySqlUByte((byte)packet.ReadByte());
			else
				return new MySqlUByte(Byte.Parse(packet.ReadString(length)));
		}
Пример #8
0
		void IMySqlValue.SkipValue(MySqlPacket packet)
		{
			packet.ReadByte();
		}
Пример #9
0
 void IMySqlValue.SkipValue(MySqlPacket packet)
 {
   int len = packet.ReadByte();
   packet.Position += len;
 }
Пример #10
0
    IMySqlValue IMySqlValue.ReadValue(MySqlPacket packet, long length, bool nullVal)
    {

      if (nullVal) return new MySqlDateTime(type, true);

      if (length >= 0)
      {
        var value = packet.ReadString(length);
        return ParseMySql(value);
      }

      long bufLength = packet.ReadByte();
      int year = 0, month = 0, day = 0;
      int hour = 0, minute = 0, second = 0, millisecond = 0;
      if (bufLength >= 4)
      {
        year = packet.ReadInteger(2);
        month = packet.ReadByte();
        day = packet.ReadByte();
      }

      if (bufLength > 4)
      {
        hour = packet.ReadByte();
        minute = packet.ReadByte();
        second = packet.ReadByte();
      }

      if (bufLength > 7)
      {
        millisecond = packet.Read3ByteInt();
        packet.ReadByte();
      }

      return new MySqlDateTime(type, year, month, day, hour, minute, second, millisecond);
    }
Пример #11
0
        IMySqlValue IMySqlValue.ReadValue(MySqlPacket packet, long length, bool nullVal)
        {
            if (nullVal)
                return new MySqlByte(true);

            if (length == -1)
                return new MySqlByte((sbyte)packet.ReadByte());
            else
            {
                string s = packet.ReadString(length);
                MySqlByte b = new MySqlByte(SByte.Parse(s, NumberStyles.Any, CultureInfo.InvariantCulture));
                b.TreatAsBoolean = TreatAsBoolean;
                return b;
            }
        }
Пример #12
0
        public void Open()
        {
            //try
            //{
            //    this.baseStream = StreamCreator.GetStream(this.Settings);
            //    if (this.Settings.IncludeSecurityAsserts)
            //    {
            //        MySqlSecurityPermission.CreatePermissionSet(false).Assert();
            //    }
            //}
            //catch (SecurityException)
            //{
            //    throw;
            //}
            //catch (Exception inner)
            //{
            //    throw new MySqlException(Resources.UnableToConnectToHost, 1042, inner);
            //}
            //if (this.baseStream == null)
            //{
            //    throw new MySqlException(Resources.UnableToConnectToHost, 1042);
            //}
            //this.stream = new MySqlStream(this.baseStream, this.Encoding, false);
            //this.stream.ResetTimeout((int)(this.Settings.ConnectionTimeout * 1000u));
            //this.packet = this.stream.ReadPacket();
            //this.packet.ReadByte();
            //string text = this.packet.ReadString();
            //this.owner.isFabric = text.EndsWith("fabric", StringComparison.OrdinalIgnoreCase);
            //this.isEnterprise = text.ToLowerInvariant().Contains("-enterprise-");
            //this.version = DBVersion.Parse(text);
            //if (!this.owner.isFabric && !this.version.isAtLeast(5, 0, 0))
            //{
            //    throw new NotSupportedException(Resources.ServerTooOld);
            //}
            //this.threadId = this.packet.ReadInteger(4);
            //byte[] array = this.packet.ReadStringAsBytes();
            //int num = 16777215;
            //ClientFlags clientFlags = (ClientFlags)0uL;
            //if (this.packet.HasMoreData)
            //{
            //    clientFlags = (ClientFlags)((long)this.packet.ReadInteger(2));
            //}
            //this.owner.ConnectionCharSetIndex = (int)this.packet.ReadByte();
            //this.serverStatus = (ServerStatusFlags)this.packet.ReadInteger(2);
            //uint num2 = (uint)this.packet.ReadInteger(2);
            //clientFlags |= (ClientFlags)(num2 << 16);
            //this.packet.Position += 11;
            //byte[] array2 = this.packet.ReadStringAsBytes();
            //this.encryptionSeed = new byte[array.Length + array2.Length];
            //array.CopyTo(this.encryptionSeed, 0);
            //array2.CopyTo(this.encryptionSeed, array.Length);
            //string authMethod;
            //if ((clientFlags & ClientFlags.PLUGIN_AUTH) != (ClientFlags)0uL)
            //{
            //    authMethod = this.packet.ReadString();
            //}
            //else
            //{
            //    authMethod = "mysql_native_password";
            //}
            //this.SetConnectionFlags(clientFlags);
            //this.packet.Clear();
            //this.packet.WriteInteger((long)((int)this.connectionFlags), 4);
            //this.packet.WriteInteger((long)num, 4);
            //this.packet.WriteByte(33);
            //this.packet.Write(new byte[23]);
            //if ((clientFlags & ClientFlags.SSL) == (ClientFlags)0uL)
            //{
            //    if (this.Settings.SslMode != MySqlSslMode.None && this.Settings.SslMode != MySqlSslMode.Preferred)
            //    {
            //        throw new MySqlException(string.Format(Resources.NoServerSSLSupport, this.Settings.Server));
            //    }
            //}
            //else if (this.Settings.SslMode != MySqlSslMode.None)
            //{
            //    this.stream.SendPacket(this.packet);
            //    this.StartSSL();
            //    this.packet.Clear();
            //    this.packet.WriteInteger((long)((int)this.connectionFlags), 4);
            //    this.packet.WriteInteger((long)num, 4);
            //    this.packet.WriteByte(33);
            //    this.packet.Write(new byte[23]);
            //}
            //this.Authenticate(authMethod, false);
            //if ((this.connectionFlags & ClientFlags.COMPRESS) != (ClientFlags)0uL)
            //{
            //    this.stream = new MySqlStream(this.baseStream, this.Encoding, true);
            //}
            //this.packet.Version = this.version;
            //this.stream.MaxBlockSize = num;
            try
            {
                baseStream = StreamCreator.GetStream(Settings);
                if (Settings.IncludeSecurityAsserts)
                {
                    MySqlSecurityPermission.CreatePermissionSet(false).Assert();
                }
            }
            catch (System.Security.SecurityException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new MySqlException(Resources.UnableToConnectToHost,
                                         1042, ex);
            }
            if (baseStream == null)
            {
                throw new MySqlException(Resources.UnableToConnectToHost,
                                         1042);
            }

            int maxSinglePacket = 255 * 255 * 255;

            stream = new MySqlStream(baseStream, Encoding, false);

            stream.ResetTimeout((int)Settings.ConnectionTimeout * 1000);

            // read off the welcome packet and parse out it's values
            packet = stream.ReadPacket();
            int    protocol      = packet.ReadByte();
            string versionString = packet.ReadString();

            owner.isFabric = versionString.EndsWith("fabric", StringComparison.OrdinalIgnoreCase);
            version        = DBVersion.Parse(versionString);
            if (!owner.isFabric && !version.isAtLeast(5, 0, 0))
            {
                throw new NotSupportedException(Resources.ServerTooOld);
            }
            threadId = packet.ReadInteger(4);

            byte[] array = this.packet.ReadStringAsBytes();

            maxSinglePacket = (256 * 256 * 256) - 1;

            // read in Server capabilities if they are provided
            ClientFlags serverCaps = 0;

            if (packet.HasMoreData)
            {
                serverCaps = (ClientFlags)packet.ReadInteger(2);
            }

            /* New protocol with 16 bytes to describe server characteristics */
            owner.ConnectionCharSetIndex = (int)packet.ReadByte();

            serverStatus = (ServerStatusFlags)packet.ReadInteger(2);

            // Since 5.5, high bits of server caps are stored after status.
            // Previously, it was part of reserved always 0x00 13-byte filler.
            uint serverCapsHigh = (uint)packet.ReadInteger(2);

            serverCaps |= (ClientFlags)(serverCapsHigh << 16);

            packet.Position += 11;
            byte[] array2 = this.packet.ReadStringAsBytes();
            this.encryptionSeed = new byte[array.Length + array2.Length];
            array.CopyTo(this.encryptionSeed, 0);
            array2.CopyTo(this.encryptionSeed, array.Length);

            string authenticationMethod = "";

            if ((serverCaps & ClientFlags.PLUGIN_AUTH) != 0)
            {
                authenticationMethod = packet.ReadString();
            }
            else
            {
                // Some MyCat versions like 5.1, don't give name of plugin, default to native password.
                authenticationMethod = "mysql_native_password";
            }

            // based on our settings, set our connection flags
            SetConnectionFlags(serverCaps);

            packet.Clear();
            packet.WriteInteger((int)connectionFlags, 4);

            if ((serverCaps & ClientFlags.SSL) == 0)
            {
                if ((Settings.SslMode != MySqlSslMode.None) &&
                    (Settings.SslMode != MySqlSslMode.Preferred))
                {
                    // Client requires SSL connections.
                    string message = String.Format(Resources.NoServerSSLSupport,
                                                   Settings.Server);
                    throw new MySqlException(message);
                }
            }
            else if (Settings.SslMode != MySqlSslMode.None)
            {
                stream.SendPacket(packet);
                StartSSL();
                packet.Clear();
                packet.WriteInteger((int)connectionFlags, 4);
            }

            packet.WriteInteger(maxSinglePacket, 4);
            packet.WriteByte(8);
            packet.Write(new byte[23]);

            Authenticate(authenticationMethod, false);

            // if we are using compression, then we use our CompressedStream class
            // to hide the ugliness of managing the compression
            if ((connectionFlags & ClientFlags.COMPRESS) != 0)
            {
                stream = new MySqlStream(baseStream, Encoding, true);
            }

            // give our stream the server version we are connected to.
            // We may have some fields that are read differently based
            // on the version of the server we are connected to.
            packet.Version      = version;
            stream.MaxBlockSize = maxSinglePacket;
        }
Пример #13
0
        public void Open()
        {
            // connect to one of our specified hosts
            try
            {
#if !CF
                if (Settings.ConnectionProtocol == MySqlConnectionProtocol.SharedMemory)
                {
                    SharedMemoryStream str = new SharedMemoryStream(Settings.SharedMemoryName);
                    str.Open(Settings.ConnectionTimeout);
                    baseStream = str;
                }
                else
                {
#endif
                string pipeName = Settings.PipeName;
                if (Settings.ConnectionProtocol != MySqlConnectionProtocol.NamedPipe)
                {
                    pipeName = null;
                }
                StreamCreator sc = new StreamCreator(Settings.Server, Settings.Port, pipeName,
                                                     Settings.Keepalive, this.Version);
#if !CF
                MySqlSecurityPermission.CreatePermissionSet(false).Assert();
#endif
                baseStream = sc.GetStream(Settings.ConnectionTimeout);
#if !CF
            }
#endif
            }
            catch (System.Security.SecurityException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new MySqlException(Resources.UnableToConnectToHost,
                                         (int)MySqlErrorCode.UnableToConnectToHost, ex);
            }
            if (baseStream == null)
            {
                throw new MySqlException(Resources.UnableToConnectToHost,
                                         (int)MySqlErrorCode.UnableToConnectToHost);
            }

            int maxSinglePacket = 255 * 255 * 255;
            stream = new MySqlStream(baseStream, Encoding, false);

            stream.ResetTimeout((int)Settings.ConnectionTimeout * 1000);

            // read off the welcome packet and parse out it's values
            packet = stream.ReadPacket();
            int protocol         = packet.ReadByte();
            string versionString = packet.ReadString();
            version = DBVersion.Parse(versionString);
            if (!version.isAtLeast(5, 0, 0))
            {
                throw new NotSupportedException(Resources.ServerTooOld);
            }
            threadId       = packet.ReadInteger(4);
            encryptionSeed = packet.ReadString();

            maxSinglePacket = (256 * 256 * 256) - 1;

            // read in Server capabilities if they are provided
            ClientFlags serverCaps = 0;
            if (packet.HasMoreData)
            {
                serverCaps = (ClientFlags)packet.ReadInteger(2);
            }

            /* New protocol with 16 bytes to describe server characteristics */
            owner.ConnectionCharSetIndex = (int)packet.ReadByte();

            serverStatus = (ServerStatusFlags)packet.ReadInteger(2);

            // Since 5.5, high bits of server caps are stored after status.
            // Previously, it was part of reserved always 0x00 13-byte filler.
            uint serverCapsHigh = (uint)packet.ReadInteger(2);
            serverCaps |= (ClientFlags)(serverCapsHigh << 16);

            packet.Position += 11;
            string seedPart2 = packet.ReadString();
            encryptionSeed += seedPart2;

            string authenticationMethod = "";
            if ((serverCaps & ClientFlags.PLUGIN_AUTH) != 0)
            {
                authenticationMethod = packet.ReadString();
            }

            // based on our settings, set our connection flags
            SetConnectionFlags(serverCaps);

            packet.Clear();
            packet.WriteInteger((int)connectionFlags, 4);

#if !CF
            if ((serverCaps & ClientFlags.SSL) == 0)
            {
                if ((Settings.SslMode != MySqlSslMode.None) &&
                    (Settings.SslMode != MySqlSslMode.Preferred))
                {
                    // Client requires SSL connections.
                    string message = String.Format(Resources.NoServerSSLSupport,
                                                   Settings.Server);
                    throw new MySqlException(message);
                }
            }
            else if (Settings.SslMode != MySqlSslMode.None)
            {
                stream.SendPacket(packet);
                StartSSL();
                packet.Clear();
                packet.WriteInteger((int)connectionFlags, 4);
            }
#endif

            packet.WriteInteger(maxSinglePacket, 4);
            packet.WriteByte(8);
            packet.Write(new byte[23]);

            Authenticate(false);

            // if we are using compression, then we use our CompressedStream class
            // to hide the ugliness of managing the compression
            if ((connectionFlags & ClientFlags.COMPRESS) != 0)
            {
                stream = new MySqlStream(baseStream, Encoding, true);
            }

            // give our stream the server version we are connected to.
            // We may have some fields that are read differently based
            // on the version of the server we are connected to.
            packet.Version      = version;
            stream.MaxBlockSize = maxSinglePacket;
        }
        IMySqlValue IMySqlValue.ReadValue(MySqlPacket packet, long length, bool nullVal)
        {
            if (nullVal) return new MySqlDateTime(type, true);

            if (length >= 0)
            {
                string value = packet.ReadString(length);
                return ParseMySql(value, packet.Version.isAtLeast(4, 1, 0));
            }

            long bufLength = packet.ReadByte();
            int year = 0, month = 0, day = 0;
            int hour = 0, minute = 0, second = 0;

            if (bufLength >= 4)
            {
                year = packet.ReadInteger(2);
                month = packet.ReadByte();
                day = packet.ReadByte();
            }

            if (bufLength > 4)
            {
                hour = packet.ReadByte();
                minute = packet.ReadByte();
                second = packet.ReadByte();
            }

            if (bufLength > 7)
                packet.ReadInteger(4);

            return new MySqlDateTime(type, year, month, day, hour, minute, second);
        }
Пример #15
0
    public int PrepareStatement(string sql, ref MySqlField[] parameters)
    {
      //TODO: check this
      //ClearFetchedRow();

      packet.Length = sql.Length * 4 + 5;
      byte[] buffer = packet.Buffer;
      int len = Encoding.GetBytes(sql, 0, sql.Length, packet.Buffer, 5);
      packet.Position = len + 5;
      buffer[4] = (byte)DBCmd.PREPARE;
      ExecutePacket(packet);

      packet = stream.ReadPacket();

      int marker = packet.ReadByte();
      if (marker != 0)
        throw new MySqlException("Expected prepared statement marker");

      int statementId = packet.ReadInteger(4);
      int numCols = packet.ReadInteger(2);
      int numParams = packet.ReadInteger(2);
      //TODO: find out what this is needed for
      packet.ReadInteger(3);
      if (numParams > 0)
      {
        parameters = owner.GetColumns(numParams);
        // we set the encoding for each parameter back to our connection encoding
        // since we can't trust what is coming back from the server
        for (int i = 0; i < parameters.Length; i++)
          parameters[i].Encoding = Encoding;
      }

      if (numCols > 0)
      {
        while (numCols-- > 0)
        {
          packet = stream.ReadPacket();
          //TODO: handle streaming packets
        }

        ReadEOF();
      }

      return statementId;
    }
Пример #16
0
    private void GetColumnData(MySqlField field)
    {
      stream.Encoding = Encoding;
      packet = stream.ReadPacket();
      field.Encoding = Encoding;
      field.CatalogName = packet.ReadLenString();
      field.DatabaseName = packet.ReadLenString();
      field.TableName = packet.ReadLenString();
      field.RealTableName = packet.ReadLenString();
      field.ColumnName = packet.ReadLenString();
      field.OriginalColumnName = packet.ReadLenString();
      packet.ReadByte();
      field.CharacterSetIndex = packet.ReadInteger(2);
      field.ColumnLength = packet.ReadInteger(4);
      MySqlDbType type = (MySqlDbType)packet.ReadByte();
      ColumnFlags colFlags;
      if ((connectionFlags & ClientFlags.LONG_FLAG) != 0)
        colFlags = (ColumnFlags)packet.ReadInteger(2);
      else
        colFlags = (ColumnFlags)packet.ReadByte();
      field.Scale = (byte)packet.ReadByte();

      if (packet.HasMoreData)
      {
        packet.ReadInteger(2); // reserved
      }

      if (type == MySqlDbType.Decimal || type == MySqlDbType.NewDecimal)
      {
        field.Precision = (byte)(field.ColumnLength - 2);
        if ((colFlags & ColumnFlags.UNSIGNED) != 0)
          field.Precision++;
      }

      field.SetTypeAndFlags(type, colFlags);
    }
Пример #17
0
    public void Open()
    {
      // connect to one of our specified hosts
      try
      {
#if !CF
        if (Settings.ConnectionProtocol == MySqlConnectionProtocol.SharedMemory)
        {
          SharedMemoryStream str = new SharedMemoryStream(Settings.SharedMemoryName);
          str.Open(Settings.ConnectionTimeout);
          baseStream = str;
        }
        else
        {
#endif
          string pipeName = Settings.PipeName;
          if (Settings.ConnectionProtocol != MySqlConnectionProtocol.NamedPipe)
            pipeName = null;
          StreamCreator sc = new StreamCreator(Settings.Server, Settings.Port, pipeName,
              Settings.Keepalive, this.Version);
#if !CF
         if (Settings.IncludeSecurityAsserts)
            MySqlSecurityPermission.CreatePermissionSet(false).Assert();
#endif
          baseStream = sc.GetStream(Settings.ConnectionTimeout);
#if !CF
        }
#endif
      }
      catch (System.Security.SecurityException)
      {
        throw;
      }
      catch (Exception ex)
      {
        throw new MySqlException(Resources.UnableToConnectToHost,
            (int)MySqlErrorCode.UnableToConnectToHost, ex);
      }
      if (baseStream == null)
        throw new MySqlException(Resources.UnableToConnectToHost,
            (int)MySqlErrorCode.UnableToConnectToHost);

      int maxSinglePacket = 255 * 255 * 255;
      stream = new MySqlStream(baseStream, Encoding, false);

      stream.ResetTimeout((int)Settings.ConnectionTimeout * 1000);

      // read off the welcome packet and parse out it's values
      packet = stream.ReadPacket();
      int protocol = packet.ReadByte();
      string versionString = packet.ReadString();
      version = DBVersion.Parse(versionString);
      if (!version.isAtLeast(5, 0, 0))
        throw new NotSupportedException(Resources.ServerTooOld);
      threadId = packet.ReadInteger(4);
      encryptionSeed = packet.ReadString();

      maxSinglePacket = (256 * 256 * 256) - 1;

      // read in Server capabilities if they are provided
      ClientFlags serverCaps = 0;
      if (packet.HasMoreData)
        serverCaps = (ClientFlags)packet.ReadInteger(2);

      /* New protocol with 16 bytes to describe server characteristics */
      owner.ConnectionCharSetIndex = (int)packet.ReadByte();

      serverStatus = (ServerStatusFlags)packet.ReadInteger(2);

      // Since 5.5, high bits of server caps are stored after status.
      // Previously, it was part of reserved always 0x00 13-byte filler.
      uint serverCapsHigh = (uint)packet.ReadInteger(2);
      serverCaps |= (ClientFlags)(serverCapsHigh << 16);

      packet.Position += 11;
      string seedPart2 = packet.ReadString();
      encryptionSeed += seedPart2;

      string authenticationMethod = "";
      if ((serverCaps & ClientFlags.PLUGIN_AUTH) != 0)
      {
        authenticationMethod = packet.ReadString();
      }
      else
      {
        // Some MySql versions like 5.1, don't give name of plugin, default to native password.
        authenticationMethod = "mysql_native_password";
      }

      // based on our settings, set our connection flags
      SetConnectionFlags(serverCaps);

      packet.Clear();
      packet.WriteInteger((int)connectionFlags, 4);

#if !CF
      if ((serverCaps & ClientFlags.SSL) == 0)
      {
        if ((Settings.SslMode != MySqlSslMode.None)
        && (Settings.SslMode != MySqlSslMode.Preferred))
        {
          // Client requires SSL connections.
          string message = String.Format(Resources.NoServerSSLSupport,
              Settings.Server);
          throw new MySqlException(message);
        }
      }
      else if (Settings.SslMode != MySqlSslMode.None)
      {
        stream.SendPacket(packet);
        StartSSL();
        packet.Clear();
        packet.WriteInteger((int)connectionFlags, 4);
      }
#endif

      packet.WriteInteger(maxSinglePacket, 4);
      packet.WriteByte(8);
      packet.Write(new byte[23]);

      Authenticate(authenticationMethod, false);

      // if we are using compression, then we use our CompressedStream class
      // to hide the ugliness of managing the compression
      if ((connectionFlags & ClientFlags.COMPRESS) != 0)
        stream = new MySqlStream(baseStream, Encoding, true);

      // give our stream the server version we are connected to.  
      // We may have some fields that are read differently based 
      // on the version of the server we are connected to.
      packet.Version = version;
      stream.MaxBlockSize = maxSinglePacket;
    }
Пример #18
0
		IMySqlValue IMySqlValue.ReadValue(MySqlPacket packet, long length, bool nullVal)
		{
			if (nullVal) return new MySqlTimeSpan(true);

			if (length >= 0)
			{
				string value = packet.ReadString(length);
				ParseMySql(value);
				return this;
			}

			long bufLength = packet.ReadByte();
			int negate = 0;
			if (bufLength > 0)
				negate = packet.ReadByte();

			isNull = false;
			if (bufLength == 0)
				isNull = true;
			else if (bufLength == 5)
				mValue = new TimeSpan(packet.ReadInteger(4), 0, 0, 0);
			else if (bufLength == 8)
				mValue = new TimeSpan(packet.ReadInteger(4),
					 packet.ReadByte(), packet.ReadByte(), packet.ReadByte());
			else
				mValue = new TimeSpan(packet.ReadInteger(4),
					 packet.ReadByte(), packet.ReadByte(), packet.ReadByte(),
					 packet.ReadInteger(4) / 1000000);

			if (negate == 1)
				mValue = mValue.Negate();
			return this;
		}
Пример #19
0
        public void Open()
        {
            // connect to one of our specified hosts
            try
            {
                baseStream = StreamCreator.GetStream(Settings);
                //#if !CF && !RT
                //         if (Settings.IncludeSecurityAsserts)
                //            MySqlSecurityPermission.CreatePermissionSet(false).Assert();
                //#endif
            }
            catch (System.Security.SecurityException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new MySqlException("Resources.UnableToConnectToHost",
                    (int)MySqlErrorCode.UnableToConnectToHost, ex);
            }
            if (baseStream == null)
                throw new MySqlException("Resources.UnableToConnectToHost",
                    (int)MySqlErrorCode.UnableToConnectToHost);

            int maxSinglePacket = 255 * 255 * 255;
            stream = new MySqlStream(baseStream, Encoding, false);

            stream.ResetTimeout((int)Settings.ConnectionTimeout * 1000);

            // read off the welcome packet and parse out it's values
            packet = stream.ReadPacket();
            int protocol = packet.ReadByte();
            string versionString = packet.ReadString();
            owner.isFabric = versionString.EndsWith("fabric", StringComparison.OrdinalIgnoreCase);
            version = DBVersion.Parse(versionString);
            if (!owner.isFabric && !version.isAtLeast(5, 0, 0))
                throw new NotSupportedException("Resources.ServerTooOld");

            threadId = packet.ReadInteger(4);

            byte[] seedPart1 = packet.ReadStringAsBytes();

            maxSinglePacket = (256 * 256 * 256) - 1;

            // read in Server capabilities if they are provided
            ClientFlags serverCaps = 0;
            if (packet.HasMoreData)
                serverCaps = (ClientFlags)packet.ReadInteger(2);

            /* New protocol with 16 bytes to describe server characteristics */
            owner.ConnectionCharSetIndex = (int)packet.ReadByte();

            serverStatus = (ServerStatusFlags)packet.ReadInteger(2);

            // Since 5.5, high bits of server caps are stored after status.
            // Previously, it was part of reserved always 0x00 13-byte filler.
            uint serverCapsHigh = (uint)packet.ReadInteger(2);
            serverCaps |= (ClientFlags)(serverCapsHigh << 16);

            packet.Position += 11;
            byte[] seedPart2 = packet.ReadStringAsBytes();
            encryptionSeed = new byte[seedPart1.Length + seedPart2.Length];
            seedPart1.CopyTo(encryptionSeed, 0);
            seedPart2.CopyTo(encryptionSeed, seedPart1.Length);

            string authenticationMethod = "";
            if ((serverCaps & ClientFlags.PLUGIN_AUTH) != 0)
            {
                authenticationMethod = packet.ReadString();
            }
            else
            {
                // Some MySql versions like 5.1, don't give name of plugin, default to native password.
                authenticationMethod = "mysql_native_password";
            }

            // based on our settings, set our connection flags
            SetConnectionFlags(serverCaps);

            packet.Clear();
            packet.WriteInteger((int)connectionFlags, 4);
            packet.WriteInteger(maxSinglePacket, 4);
            packet.WriteByte(33); //character set utf-8
            packet.Write(new byte[23]);

            #if !CF && !RT
            if ((serverCaps & ClientFlags.SSL) == 0)
            {
                if ((Settings.SslMode != MySqlSslMode.None)
                && (Settings.SslMode != MySqlSslMode.Preferred))
                {
                    // Client requires SSL connections.
                    string message = String.Format("Resources.NoServerSSLSupport",

                        Settings.Server);
                    throw new MySqlException(message);
                }
            }
            else if (Settings.SslMode != MySqlSslMode.None)
            {
                stream.SendPacket(packet);
                StartSSL();
                packet.Clear();
                packet.WriteInteger((int)connectionFlags, 4);
                packet.WriteInteger(maxSinglePacket, 4);
                packet.WriteByte(33); //character set utf-8
                packet.Write(new byte[23]);
            }
            #endif

            #if RT
              if (Settings.SslMode != MySqlSslMode.None)
              {
            throw new NotImplementedException("SSL not supported in this WinRT release.");
              }
            #endif

            Authenticate(authenticationMethod, false);

            // if we are using compression, then we use our CompressedStream class
            // to hide the ugliness of managing the compression
            if ((connectionFlags & ClientFlags.COMPRESS) != 0)
                stream = new MySqlStream(baseStream, Encoding, true);

            // give our stream the server version we are connected to.
            // We may have some fields that are read differently based
            // on the version of the server we are connected to.
            packet.Version = version;
            stream.MaxBlockSize = maxSinglePacket;
        }
Пример #20
0
        public void Open()
        {
            // connect to one of our specified hosts
            try
            {
            #if !CF
                if (Settings.ConnectionProtocol == MySqlConnectionProtocol.SharedMemory)
                {
                    SharedMemoryStream str = new SharedMemoryStream(Settings.SharedMemoryName);
                    str.Open(Settings.ConnectionTimeout);
                    baseStream = str;
                }
                else
                {
            #endif
                    string pipeName = Settings.PipeName;
                    if (Settings.ConnectionProtocol != MySqlConnectionProtocol.NamedPipe)
                        pipeName = null;
                    StreamCreator sc = new StreamCreator(Settings.Server, Settings.Port, pipeName,
                        Settings.Keepalive);
                    baseStream = sc.GetStream(Settings.ConnectionTimeout);
            #if !CF
                }
            #endif
            }
            catch (Exception ex)
            {
                    throw new MySqlException(MySqlResources.UnableToConnectToHost,
                    (int) MySqlErrorCode.UnableToConnectToHost, ex);
            }

            if (baseStream == null)
                    throw new MySqlException(MySqlResources.UnableToConnectToHost,
                    (int)MySqlErrorCode.UnableToConnectToHost);

            int maxSinglePacket = 255*255*255;
            stream = new MySqlStream(baseStream, Encoding, false);

            stream.ResetTimeout((int)Settings.ConnectionTimeout*1000);

            // read off the welcome packet and parse out it's values
            packet = stream.ReadPacket();
            int protocol = packet.ReadByte();
            string versionString = packet.ReadString();
            version = DBVersion.Parse(versionString);
            if (!version.isAtLeast(4, 1, 1))
                    throw new NotSupportedException(MySqlResources.ServerTooOld);
            threadId = packet.ReadInteger(4);
            encryptionSeed = packet.ReadString();

            maxSinglePacket = (256*256*256) - 1;

            // read in Server capabilities if they are provided
            ClientFlags serverCaps = 0;
            if (packet.HasMoreData)
                serverCaps = (ClientFlags) packet.ReadInteger(2);

            /* New protocol with 16 bytes to describe server characteristics */
            owner.ConnectionCharSetIndex = (int)packet.ReadByte();

            serverStatus = (ServerStatusFlags) packet.ReadInteger(2);
            packet.Position += 13;
            string seedPart2 = packet.ReadString();
            encryptionSeed += seedPart2;

            // based on our settings, set our connection flags
            SetConnectionFlags(serverCaps);

            packet.Clear();
            packet.WriteInteger((int) connectionFlags, 4);

            #if !CF
            if ((serverCaps & ClientFlags.SSL) ==0)
            {
                if ((Settings.SslMode != MySqlSslMode.None)
                && (Settings.SslMode != MySqlSslMode.Preferred))
                {
                    // Client requires SSL connections.
                         string message = String.Format(MySqlResources.NoServerSSLSupport,
                        Settings.Server);
                    throw new MySqlException(message);
                }
            }
            else if (Settings.SslMode != MySqlSslMode.None)
            {
                stream.SendPacket(packet);
                StartSSL();
                packet.Clear();
                packet.WriteInteger((int) connectionFlags, 4);
            }
            #endif

            packet.WriteInteger(maxSinglePacket, 4);
            packet.WriteByte(8);
            packet.Write(new byte[23]);

            Authenticate();

            // if we are using compression, then we use our CompressedStream class
            // to hide the ugliness of managing the compression
            if ((connectionFlags & ClientFlags.COMPRESS) != 0)
                stream = new MySqlStream(baseStream, Encoding, true);

            // give our stream the server version we are connected to.
            // We may have some fields that are read differently based
            // on the version of the server we are connected to.
            packet.Version = version;
            stream.MaxBlockSize = maxSinglePacket;
        }
Пример #21
0
    private void AuthenticateSSPI()
    {

      string targetName = ""; // target name (required by Kerberos)

      // First packet sent by server should include target name (for
      // Kerberos) as UTF8 string. It might however be prepended by junk 
      // at the start of the string (0xfe"authentication_win_client"\0,
      // see Bug#57442), this junk will be ignored. Target name can also 
      // be an empty string if server is not running in a domain environment, 
      // in this case authentication will fallback to NTLM.

      // Note that 0xfe byte at the start could also indicate that windows
      // authentication is not supported by sérver, we throw an exception
      // if this happens.

      packet = stream.ReadPacket();
      byte b = packet.ReadByte();
      if (b == 0xfe)
      {
        string authMethod = packet.ReadString();
        if (authMethod.Equals(AuthenticationWindowsPlugin))
        {
          targetName = packet.ReadString(Encoding.UTF8);
        }
        else
        {
          // User has requested Windows authentication,  bail out.
          throw new MySqlException("unexpected authentication method " +
              authMethod);
        }
      }
      else
      {
        targetName = Encoding.UTF8.GetString(packet.Buffer, 0, packet.Buffer.Length);
      }

      // Do SSPI authentication handshake
      SSPI sspi = new SSPI(targetName, stream.BaseStream, stream.SequenceByte, version);
      sspi.AuthenticateClient();

      // read ok packet.
      packet = stream.ReadPacket();
      ReadOk(false);
    }