Example #1
0
        static void authenticate(String server)
        {
            utility _utility        = new utility();
            bool    socketException = false;

Connect:
            Console.WriteLine("Connecting to AMI session: " + retry + "\n");
            _utility.notify("Connecting to AMI host - 192.168.67.215", "Attempting to re-establish connection to AMI host \n\nTime:" + DateTime.Now.ToString());

            // Connect to the asterisk server.
            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            IPEndPoint myEndPoint     = new IPEndPoint(IPAddress.Any, 9901);
            IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(server), 5038);

            try {
                if (!socketException)
                {
                    serverSocket.Bind(myEndPoint);
                    serverSocket.Listen(4);
                }

                clientSocket.Connect(serverEndPoint);

                // Login to the server; manager.conf needs to be setup with matching credentials.
                clientSocket.Send(Encoding.ASCII.GetBytes("Action:Login\r\nUsername: atladmin\r\nSecret: 7mmT@XAy\r\nActionID: 5\r\n\r\n"));

                if (clientSocket.Poll(100000, SelectMode.SelectError))
                {
                    Console.WriteLine("poll interval");
                    if (!clientSocket.Connected)
                    {
                        // Something bad has happened, shut down
                        Console.WriteLine("disconnected");
                    }
                    else
                    {
                        // There is data waiting to be read"
                    }
                }

                int bytesRead, bytes = 0;

                do
                {
                    byte[] buffer  = new byte[10024];
                    byte[] buffer2 = new byte[10024];
                    bytesRead = clientSocket.Receive(buffer);

                    string response = Encoding.ASCII.GetString(buffer, 0, bytesRead);

                    if (!socketException)
                    {
                        serverSocket.BeginAccept(new AsyncCallback(AcceptCallBack), serverSocket);
                    }
                    //  bytes=socketAccept.Receive(buffer2);

                    string responseData = Encoding.ASCII.GetString(buffer2, 0, bytes);
                    Console.WriteLine(responseData);

                    String[] pars = response.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.None);
                    if (response.IndexOf("\r\n\r\n") > -1)
                    {
                        Console.WriteLine(response);
                        Task task = new Task(() => _utility.consumeResponse(pars));
                        task.Start();
                    }
                    if (Regex.Match(response, "Message: Authentication accepted", RegexOptions.IgnoreCase).Success)
                    {
                        Console.Write("Login Successfull");
                        _utility.notify("Connection to AMI host sucessfull - 192.168.67.215", "Alert: Successfully connected to AMI host \n\nTime:" + DateTime.Now.ToString());
                    }

                    //Let's get pretty parsing and checking events
                } while (bytesRead != 0);

                Console.WriteLine("Connection to server lost.");
                _utility.notify("AMI Connection Lost - 192.168.67.215", "Alert: Connection to server lost \n\nTime:" + DateTime.Now.ToString());
                //  _utility._sqlcon.Dispose();
                // serverSocket.Shutdown(SocketShutdown.Both);
                // serverSocket.Disconnect(true);
                socketException = true;
                //   retry=retry+1;
                goto Connect;
                //Console.ReadLine();
            }
            catch (SocketException ex)
            {
                // _utility._sqlcon.Dispose();
                _utility.notify("AMI Connection Lost - 192.168.67.215", "Alert: " + ex.Message.ToString() + "\n\nTime:" + DateTime.Now.ToString());
                goto Connect;
            }
        }
Example #2
0
        static void authenticate(String server)
        {
            bool socketException = false;

Connect:
            Console.WriteLine("Connecting to AMI session: 192.168.66.50 \n");

            // Connect to the asterisk server.
            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            IPEndPoint myEndPoint     = new IPEndPoint(IPAddress.Any, 9900);
            IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(server), 5038);
            utility    _utility       = new utility();

            try {
                if (!socketException)
                {
                    serverSocket.Bind(myEndPoint);
                    serverSocket.Listen(4);
                }

                clientSocket.Connect(serverEndPoint);

                // Login to the server; manager.conf needs to be setup with matching credentials.
                clientSocket.Send(Encoding.ASCII.GetBytes("Action:Login\r\nUsername: atladmin\r\nSecret: 7mmT@XAy\r\nActionID: 4\r\n\r\n"));


                int bytesRead, bytes = 0;

                do
                {
                    byte[] buffer  = new byte[10024];
                    byte[] buffer2 = new byte[10024];
                    bytesRead = clientSocket.Receive(buffer);

                    string response = Encoding.ASCII.GetString(buffer, 0, bytesRead);

                    if (!socketException)
                    {
                        serverSocket.BeginAccept(new AsyncCallback(AcceptCallBack), serverSocket);
                    }
                    //  bytes=socketAccept.Receive(buffer2);

                    string responseData = Encoding.ASCII.GetString(buffer2, 0, bytes);
                    Console.WriteLine(responseData);

                    String[] pars = response.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.None);
                    if (response.IndexOf("\r\n\r\n") > -1)
                    {
                        Console.WriteLine(response);
                        Task task = new Task(() => _utility.consumeResponse(pars));
                        task.Start();
                    }
                    if (Regex.Match(response, "Message: Authentication accepted", RegexOptions.IgnoreCase).Success)
                    {
                        Console.Write("Login Successfull");
                    }

                    //Let's get pretty parsing and checking events
                } while (bytesRead != 0);

                Console.WriteLine("Connection to server lost.");
                //  _utility._sqlcon.Dispose();
                // serverSocket.Shutdown(SocketShutdown.Both);
                // serverSocket.Disconnect(true);
                socketException = true;
                goto Connect;
                //Console.ReadLine();
            }
            catch (SocketException ex)
            {
                // _utility._sqlcon.Dispose();
                goto Connect;
            }
        }