예제 #1
0
파일: Server.cs 프로젝트: schmalle/MysqlPot
        /*
         * starts the listerner
         */
        public void startInternal(String username, String token, String host)
        {
            Mysql x = new Mysql(0, m_writer);

            // set initial state
            m_state = State.NOT_CONNECTED;

            // start socket and wait for incoming client connection // add here multi client stuff
               	m_socket.Start();
            TcpClient client = m_socket.AcceptTcpClient();
            NetworkStream ns = client.GetStream();
            StreamWriter sw = new StreamWriter(ns);
            sw.AutoFlush = true;

                if (!ns.CanRead)
                {
                    Console.WriteLine("Info: Stream no readable....");
                    return;
                }

                var pi = ns.GetType().GetProperty("Socket", BindingFlags.NonPublic | BindingFlags.Instance);
                var socketIP = ((Socket)pi.GetValue(ns, null)).RemoteEndPoint.ToString();
                Console.WriteLine("Connecting client: " + socketIP);

                // send the server greeting (static for now)
                handleNotConnected(x, ns, client);
                ns = client.GetStream();

              			while(socketCheck(ns))
              			{

                    Console.WriteLine("Info: Read " + readBytes + " bytes....");

                     // if not connected, do a straight forward "send a greeting packet"
                    if (m_state == State.NOT_CONNECTED)
                    {
                        Console.WriteLine("Info: Mysqlpot in state NOT CONNECTED, not possible state, error");
                    }
                    else if (m_state == State.GREETING_PACKET_SEND)
                    {
                        Console.WriteLine("Info: Mysqlpot in state GREETING_PACKET_SEND");
                        x.handleLoginPacket(data, socketIP, token, username, host);
                        m_state = State.LOGIN_OK;

                        Console.WriteLine("Info: Login packet retrieved, now trying to generate answer packet...");
                        byte[] okPacket = x.generateOKPacket(x.getPacketNumber(data) + 1, 1); // packetnumber, affeceted lines

                        Console.WriteLine("Info: Now writing OK packet back on the line..");

                        sw.Write (System.Text.Encoding.UTF8.GetString(okPacket).ToCharArray());
                        ns = client.GetStream();

                    }
                    else if (m_state == State.LOGIN_OK)
                    {
                        Console.WriteLine("Info: Mysqlpot in state LOGIN_OK");

                        byte[] queryString = x.handleQueryPacket(data, socketIP, token, username, host);
                        byte[] answer = x.getAnswerPacket(queryString, x.getPacketNumber(data) + 1);
                        if (answer != null)
                        {
                            ns.Write(answer, 0, answer.Length);

                            // sw.Write (System.Text.Encoding.UTF8.GetString(answer).ToCharArray());
                            ns = client.GetStream();

                        }
                        else
                        {
                            Console.WriteLine("Info: Detected no suitable answer for the SQL query");
                        }

                    }
                    else
                    {
                        Console.WriteLine("Info: Mysqlpot in unknown state");
                        break;
                    }

              			}

            Console.WriteLine("Error: Ausbruch aus dem while loop");

            ns.Close();
              		client.Close();

              		m_socket.Stop();
            m_state = State.NOT_CONNECTED;
        }
예제 #2
0
파일: Server.cs 프로젝트: schmalle/MysqlPot
        /**
         * handles the send of the greeting packet if not send already
         *
         */
        private void handleNotConnected(Mysql x, NetworkStream ns, TcpClient client)
        {
            byte[] dataOut = x.getGreetingPacket (0);
            if (dataOut != null)
            {
                m_state = State.GREETING_PACKET_SEND;
                ns.Write (dataOut, 0, dataOut.Length);
                ns.Flush ();
                ns = client.GetStream();

                Console.WriteLine("Info: Send greeting packet....");

            }
            else
            {
                Console.WriteLine("Info: Error retrieving greeting packet....");
            }
        }
예제 #3
0
        /*
         * starts the listerner
         */
        public void startInternal(String username, String token, String host)
        {
            Mysql x = new Mysql(0, m_writer);

            // set initial state
            m_state = State.NOT_CONNECTED;

            // start socket and wait for incoming client connection // add here multi client stuff
            m_socket.Start();
            TcpClient     client = m_socket.AcceptTcpClient();
            NetworkStream ns     = client.GetStream();
            StreamWriter  sw     = new StreamWriter(ns);

            sw.AutoFlush = true;

            if (!ns.CanRead)
            {
                Console.WriteLine("Info: Stream no readable....");
                return;
            }

            var pi       = ns.GetType().GetProperty("Socket", BindingFlags.NonPublic | BindingFlags.Instance);
            var socketIP = ((Socket)pi.GetValue(ns, null)).RemoteEndPoint.ToString();

            Console.WriteLine("Connecting client: " + socketIP);

            // send the server greeting (static for now)
            handleNotConnected(x, ns, client);
            ns = client.GetStream();

            while (socketCheck(ns))
            {
                Console.WriteLine("Info: Read " + readBytes + " bytes....");

                // if not connected, do a straight forward "send a greeting packet"
                if (m_state == State.NOT_CONNECTED)
                {
                    Console.WriteLine("Info: Mysqlpot in state NOT CONNECTED, not possible state, error");
                }
                else if (m_state == State.GREETING_PACKET_SEND)
                {
                    Console.WriteLine("Info: Mysqlpot in state GREETING_PACKET_SEND");
                    x.handleLoginPacket(data, socketIP, token, username, host);
                    m_state = State.LOGIN_OK;

                    Console.WriteLine("Info: Login packet retrieved, now trying to generate answer packet...");
                    byte[] okPacket = x.generateOKPacket(x.getPacketNumber(data) + 1, 1);                             // packetnumber, affeceted lines

                    Console.WriteLine("Info: Now writing OK packet back on the line..");

                    sw.Write(System.Text.Encoding.UTF8.GetString(okPacket).ToCharArray());
                    ns = client.GetStream();
                }
                else if (m_state == State.LOGIN_OK)
                {
                    Console.WriteLine("Info: Mysqlpot in state LOGIN_OK");

                    byte[] queryString = x.handleQueryPacket(data, socketIP, token, username, host);
                    byte[] answer      = x.getAnswerPacket(queryString, x.getPacketNumber(data) + 1);
                    if (answer != null)
                    {
                        ns.Write(answer, 0, answer.Length);

                        // sw.Write (System.Text.Encoding.UTF8.GetString(answer).ToCharArray());
                        ns = client.GetStream();
                    }
                    else
                    {
                        Console.WriteLine("Info: Detected no suitable answer for the SQL query");
                    }
                }
                else
                {
                    Console.WriteLine("Info: Mysqlpot in unknown state");
                    break;
                }
            }

            Console.WriteLine("Error: Ausbruch aus dem while loop");

            ns.Close();
            client.Close();

            m_socket.Stop();
            m_state = State.NOT_CONNECTED;
        }               // start()