Esempio n. 1
0
 static string GetInput()
 {
     if (!IsConnected())
     {
         Thread.Sleep(1000);
         return("");
     }
     try
     {
         string RXString = string.Empty;
         while (true)
         {
             Connection.Poll(-1, SelectMode.SelectRead);     // Wait until there's data to read
             byte[] RXData = new byte[Connection.Available];
             Connection.Receive(RXData);
             RXString += new string(Toolbox.Bytes2Chars(RXData));
             KeepAlive = 0;                                                          // Clear KeepAlive watchdog
             if (RXData[RXData.Length - 2] == 13 && RXData[RXData.Length - 1] == 10) // Wait for a CRLF
             {
                 Debug.Print("IN: " + RXString.Trim());                              // TODO: Remove after testing
                 return(RXString.Trim());                                            // TODO: Replace try/catch with real input validation
             }
         }
     }
     catch (SocketException e)
     {
         Debug.Print("Exception @ GetInput(): " + e.ErrorCode);
         return("");
     }
 }
Esempio n. 2
0
        public static string Read(string file)
        {
            if (!ConfigFolder.Exists)
            {
                Init();
            }
            FileInfo ConfigFile = new FileInfo(@"\SD\MiniGate\Config\" + file);

            if (ConfigFile.Exists)
            {
                return(new String(Toolbox.Bytes2Chars(File.ReadAllBytes(@"\SD\MiniGate\Config\" + file))));
            }
            return("");
        }
Esempio n. 3
0
        static string GetInput()
        {
            if (!IsConnected())
            {
                return(string.Empty);
            }
            bool quit;

            try
            {
                while (true)
                {
                    quit = true;
                    Connection.Poll(-1, SelectMode.SelectRead);     // Wait until data is available on the socket
                    byte[] RXData = new byte[Connection.Available];
                    Connection.Receive(RXData);
                    if (RXData[0] == CR && RXData[1] == LF)
                    {
                        quit = false;                       // Ignore CRLF Sequence
                    }
                    for (int i = 0; i < RXData.Length; ++i) // I stole some of this part from the NETMF Toolbox Telnet Server
                    {
                        if (RXData[i] == IAC)               // Check for Telnet commands
                        {
                            byte Command = RXData[++i];
                            // Lets only support DO, DONT for now
                            if (Command == DO || Command == DONT)  // || Command == WILL || Command == WONT)
                            {
                                // Lets see what the other party wants to tell us
                                byte Option = RXData[++i];
                                if (Option != ECHO)
                                {
                                    Connection.Send(new byte[] { IAC, WONT, Option });                  // We don't want to do much except control echoing
                                }
                            }
                            quit = false;   // We don't want to return yet
                        }
                    }
                    if (quit)
                    {
                        return(new String(Toolbox.Bytes2Chars(RXData)).Trim());            // TODO: Should probably have some more input validation
                    }
                }
            }
            catch
            {
                return(string.Empty);        // Returning null borks other stuff
            }
        }
Esempio n. 4
0
        private static void Parse()                   // Where most of the packet decoding magic happens
        {
            if (bytes > 17)                           // If the packet is less than 18 bytes, it's obviously broken, so don't even bother.
            {
                ushort CalcFCS = 0xFFFF;              // We will check the FCS before going on

                for (int i = 0; i < (bytes - 2); i++) // Loop thru all bytes in the packet except the FCS field
                {
                    byte inbyte = indata[i];
                    for (int k = 0; k < 8; k++)                       // Loop thru all 8 bits in this byte
                    {
                        bool inbit  = ((inbyte & 0x01) == 0x01);      // Grab the LSB of the current byte
                        bool fcsbit = ((CalcFCS & 0x0001) == 0x0001); // Grab the LSB of the current FCS value
                        CalcFCS >>= 1;                                // Shift the current FCS value one bit right
                        if (fcsbit != inbit)
                        {
                            CalcFCS = (ushort)(CalcFCS ^ 0x8408); // If the LSB of this byte and the bit that was shifted off the FCS don't match, XOR the FCS with 0x8408
                        }
                        inbyte >>= 1;                             // Shift this byte right to get ready for the next bit
                    }
                }
                CalcFCS = (ushort)(CalcFCS ^ 0xFFFF);      // XOR The FCS with 0xFFFF

                if ((indata[bytes - 1] == (CalcFCS >> 8)) && (indata[bytes - 2] == (CalcFCS & 0xFF)))
                {
                    int      NumCalls  = 0;
                    byte[][] Callsigns = new byte[10][];
                    byte[]   SSIDs     = new byte[10];
                    bool[]   HBit      = new bool[10];
                    string   Via       = "";

                    for (int i = 0; i <= 9; i++)
                    {
                        Callsigns[i] = new byte[6];     // Initialize "jagged" callsign array
                    }

                    for (int a = 0; a <= 9; a++)    // Loop through up to 10 callsigns in the address field
                    {
                        NumCalls++;

                        for (int i = 0; i <= 5; i++)
                        {
                            Callsigns[a][i] = (byte)(indata[((a * 7) + i)] >> 1);   // Get the byte for each letter of this call, and shift it right one bit
                        }

                        SSIDs[a] = (byte)((indata[((a + 1) * 7) - 1] & 0x1E) >> 1); // Get the SSID of this call (bits 4-1 of the last octect of this call)
                        HBit[a]  = ((indata[((a + 1) * 7) - 1] & 0x80) == 0x80);    // See if the "H bit" of this SSID octet is set (indicates if this digi slot has been used)

                        if ((indata[((a + 1) * 7) - 1] & 0x01) == 0x01)
                        {
                            break;                                                  // Exit the loop if this is the last call in the address field
                        }
                    }

                    if ((indata[NumCalls * 7] == 0x03) && (indata[(NumCalls * 7) + 1] == 0xF0))     // Don't bother going on if this isn't a UI packet
                    {                                                                               // We'd check this sooner, but need to know where these bytes are in the packet first
                        if (NumCalls > 2)
                        {
                            Via = ",";
                            for (int i = 2; i < NumCalls; i++)
                            {
                                string Callsign = new String(Toolbox.Bytes2Chars(Callsigns[i]));
                                Via = Via + Callsign.Trim();
                                if (SSIDs[i] != 0x00)
                                {
                                    Via = Via + "-" + SSIDs[i].ToString();                      // Only add the SSID if it's not zero
                                }
                                if (HBit[i])
                                {
                                    Via = Via + "*";            // Add a "*" if this digi slot has been used
                                }
                                if ((i + 1) < NumCalls)
                                {
                                    Via = Via + ",";                        // Add a "," if there are more digi's in the list
                                }
                            }
                        }
                        string Source = new String(Toolbox.Bytes2Chars(Callsigns[1]));
                        Source = Source.Trim();
                        if (SSIDs[1] != 0x00)
                        {
                            Source = Source + "-" + SSIDs[1].ToString();
                        }
                        string Dest = new String(Toolbox.Bytes2Chars(Callsigns[0]));
                        Dest = Dest.Trim();
                        if (SSIDs[0] != 0x00)
                        {
                            Dest = Dest + "-" + SSIDs[0].ToString();
                        }
                        string Payload = new String(Toolbox.Bytes2Chars(Utility.ExtractRangeFromArray(indata, (NumCalls * 7) + 2, bytes - (NumCalls * 7) - 4)));
                        if (Via.IndexOf("TCPIP") == -1 && Via.IndexOf("TCPXX") == -1)
                        {
                            APRSIS.SendData(Source + ">" + Dest + Via + ",qAR," + MiniGate.FullCall + ":" + Payload + "\r\n");
                        }
                        if (TelnetServer.RFMonEnable)
                        {
                            TelnetServer.SendData(Source + ">" + Dest + Via + ":" + Payload + "\r\n");
                        }
                    }
                }
            }
            Array.Clear(indata, 0, bytes + 1);
            bytes = 0;
        }