Exemple #1
0
 public override string decode(ASPFile context, string value)
 {
     if (isExplicit == "EXPLICIT")
     {
         var info = context.decodeType(value);
         return("(" + classId + " " + addr + "):" + info.Item1.decode(context, info.Item3));
     }
     else
     {
         var oid = Convert.ToInt32(value.Substring(0, 8), 2);
         value = value.Substring(8);
         string str = (oid / 40).ToString() + "." + (oid % 40).ToString();
         while (value.Length > 0)
         {
             string v       = value.Substring(0, 8);
             string fullval = v.Substring(1, 7);
             while (v[0] == '1')
             {
                 value    = value.Substring(8);
                 v        = value.Substring(0, 8);
                 fullval += v.Substring(1, 7);
             }
             oid   = Convert.ToInt32(fullval, 2);
             value = value.Substring(8);
             str  += "." + oid.ToString();
         }
         ITreeNode node = context.findPath(str.Split("."), context.root);
         //if (node != null) return name + ": " + node.getName();
         //else
         return(name + ": " + str);
     }
 }
    private void parseImport(Match match)
    {
        string matchStr = match.Groups[1].Value + "\n";

        for (Match importMatch = Expressions.Imports.singleImport.Match(matchStr); importMatch.Success; importMatch = importMatch.NextMatch())
        {
            string fileName = importMatch.Groups[2].Value + ".txt";
            if (File.Exists(fileName))
            {
                ASPFile importFile = new ASPFile(File.ReadAllText(fileName));
                foreach (string import in importMatch.Groups[1].Value.Trim().Split(","))
                {
                    ITreeNode node = importFile.findNode(import.Trim());
                    if (node != null)
                    {
                        file.mergeTree(node);
                    }
                    if (importFile.tryFetchType(import.Trim(), out IType type))
                    {
                        file.AddType(type);
                    }
                }
            }
            else
            {
                Console.WriteLine("file " + fileName + " not found");
            }
        }
    }
Exemple #3
0
    public override string decode(ASPFile context, string value)
    {
        string ret = "{ ";

        while (value.Length > 0)
        {
            var info = context.decodeType(value);
            ret  += info.Item1.decode(context, info.Item3.Substring(0, (int)info.Item2 * 8)) + ", ";
            value = info.Item3.Substring((int)info.Item2 * 8);
        }
        return(name + ": " + ret.Substring(0, ret.Length - 2) + " }");
    }
Exemple #4
0
 public override string decode(ASPFile context, string value)
 {
     if (isExplicit == "EXPLICIT")
     {
         var info = context.decodeType(value);
         return("(" + classId + " " + addr + "):" + info.Item1.decode(context, info.Item3));
     }
     else
     {
         return(name + ": " + (value == "11111111" ? "TRUE" : "FALSE"));
     }
 }
Exemple #5
0
 public override string decode(ASPFile context, string value)
 {
     if (isExplicit == "EXPLICIT")
     {
         var info = context.decodeType(value);
         return("(" + classId + " " + addr + "):" + info.Item1.decode(context, info.Item3));
     }
     else
     {
         var hex = string.Join("", Enumerable.Range(0, value.Length / 8).Select(i => Convert.ToByte(value.Substring(i * 8, 8), 2).ToString("X2")));
         return(name + ": " + BigInteger.Parse(hex, NumberStyles.AllowHexSpecifier).ToString());
     }
 }
Exemple #6
0
 public override string decode(ASPFile context, string value)
 {
     if (isExplicit == "EXPLICIT")
     {
         var info = context.decodeType(value);
         return("(" + classId + " " + addr + "):" + info.Item1.decode(context, info.Item3));
     }
     else
     {
         string str = "";
         while (value.Length > 0)
         {
             str  += (char)Convert.ToInt32(value.Substring(0, 8), 2);
             value = value.Substring(8);
         }
         return(name + ": " + "\"" + str + "\"");
     }
 }
Exemple #7
0
        static void Main(string[] args)
        {
            while (!quit)
            {
                if (node == null)
                {
                    Console.Write(">");
                }
                else
                {
                    Console.Write(String.Join('.', getPath(node)) + " $ ");
                }
                string[] command = Console.ReadLine().Split(" ");
                if (command[0] == "quit" && command.Length == 1)
                {
                    quit = true;
                }
                else if (command[0] == "test" && command.Length == 1)
                {
                    var str = new OIDType(null, Int64.MinValue, Int64.MaxValue, null, null, null).encode("{ISO(1) org(3) dod(6) internet(257)}");

                    string hex = String.Concat(
                        Regex.Matches(str, "....").Cast <Match>()
                        .Select(m => Convert.ToInt32(m.Value, 2)
                                .ToString("x1"))
                        );
                    Console.WriteLine(str);
                    Console.WriteLine(hex);

                    var test = file.decodeType(str);
                    Console.WriteLine(test);
                }
                else if (command[0] == "start" && command.Length == 1)
                {
                    Console.WriteLine("parsing file RFC1213 - MIB.txt");
                    file = new ASPFile(File.ReadAllText("RFC1213-MIB.txt"));
                    node = file.root;
                }
                else if (command[0] == "decode" && command.Length > 1)
                {
                    var toDecode = Regex.Replace(string.Join(' ', command.AsSpan(1, command.Length - 1).ToArray()), @"\s+", "");
                    toDecode = String.Join(String.Empty, toDecode.Select(c => Convert.ToString(Convert.ToInt32(c.ToString(), 16), 2).PadLeft(4, '0')));
                    var info = file.decodeType(toDecode);
                    Console.WriteLine(info.Item1.decode(file, info.Item3));
                }
                else if (command[0] == "cls" && command.Length == 1)
                {
                    Console.Clear();
                }
                else if (command[0] == "parse" && command.Length == 2)
                {
                    Console.WriteLine("parsing file " + command[1]);
                    file = new ASPFile(File.ReadAllText(command[1]));
                    node = file.root;
                }
                else if (command[0] == "cd" && command.Length == 2)
                {
                    ITreeNode newNode = file.findPath(command[1].Split('.'), node);
                    if (newNode != null)
                    {
                        node = newNode;
                    }
                    else
                    {
                        Console.WriteLine("node not found");
                    }
                }
                else if (command[0] == "ls" && command.Length == 1)
                {
                    printChildren(node);
                }
                else if (command[0] == "discard" && command.Length == 1)
                {
                    file = null;
                    node = null;
                }
                else if (command[0] == "find" && command.Length == 2)
                {
                    ITreeNode newNode = file.findNode(command[1]);
                    if (newNode != null)
                    {
                        node = newNode;
                    }
                    else
                    {
                        Console.WriteLine("node not found");
                    }
                }
                else if (command[0] == "info" && command.Length == 1)
                {
                    printInfo(node);
                }
                else if (command[0] == "new" && command.Length == 1)
                {
                    file = new ASPFile("");
                    node = file.root;
                }
                else if (command[0] == "def" && command.Length > 1)
                {
                    string line = string.Join(' ', command.AsSpan(1, command.Length - 1).ToArray());
                    new Parser(file).parseAnyType(Utils.preprocessText(line + ' '));
                }
                else if (command[0] == "encode" && command.Length > 2)
                {
                    string value   = string.Join(' ', command.AsSpan(2, command.Length - 2).ToArray());
                    string encoded = file.fetchType(command[1]).encode(value);
                    if (encoded != null)
                    {
                        Console.WriteLine(Regex.Replace(encoded, ".{8}", "$0 "));
                        var hex = string.Join("       ",
                                              Enumerable.Range(0, encoded.Length / 8).Select(i => Convert.ToByte(encoded.Substring(i * 8, 8), 2).ToString("X2")));
                        Console.WriteLine(hex);
                    }
                    else
                    {
                        Console.WriteLine("invalid input");
                    }
                }
                else if (command[0] == "encode-node" && command.Length > 1)
                {
                    if (node.GetType() == typeof(ObjectType))
                    {
                        ObjectType objType  = (ObjectType)node;
                        string     toEncode = string.Join(' ', command.AsSpan(1, command.Length - 1).ToArray());
                        string     encoded  = objType.type.encode(toEncode);
                        if (encoded != null)
                        {
                            Console.WriteLine(Regex.Replace(encoded, ".{8}", "$0 "));
                            var hex = string.Join("       ",
                                                  Enumerable.Range(0, encoded.Length / 8).Select(i => Convert.ToByte(encoded.Substring(i * 8, 8), 2).ToString("X2")));
                            Console.WriteLine(hex);
                        }
                        else
                        {
                            Console.WriteLine("invalid input");
                        }
                    }
                    else
                    {
                        Console.WriteLine("node not encodable");
                    }
                }
                else if (command[0] == "listen" && command.Length == 1)
                {
                    IPEndPoint serverEndpoint = new IPEndPoint(IPAddress.Any, 161);
                    UdpClient  socket         = new UdpClient(serverEndpoint);
                    IPEndPoint remote         = new IPEndPoint(IPAddress.Any, 0);

                    var data     = socket.Receive(ref remote);
                    var byteData = string.Join("", Enumerable.Range(0, data.Length).Select(i => data[i].ToString("X2")));
                    byteData = String.Join(String.Empty, byteData.Select(c => Convert.ToString(Convert.ToInt32(c.ToString(), 16), 2).PadLeft(4, '0')));
                    var info    = file.decodeType(byteData);
                    var decoded = info.Item1.decode(file, info.Item3);
                    Console.WriteLine(decoded);

                    var requestId = Regex.Match(decoded, @"GetRequestPDU: { INTEGER: ([0-9]+),").Groups[1];
                    var oid       = Regex.Match(decoded, @"OBJECT IDENTIFIER: ([.0-9]+),").Groups[1];
                    var resp      = file.fetchType("Message").encode("{ version 0, community public, data { requestid " + requestId + ", errorstatus 0, errorindex 0, variablebindings { { name " + oid + ", value str AAAAA } } } }");
                    resp = string.Join("", Enumerable.Range(0, resp.Length / 8).Select(i => Convert.ToByte(resp.Substring(i * 8, 8), 2).ToString("X2")));
                    data = new byte[1024];
                    for (int i = 0; i < resp.Length; i += 2)
                    {
                        data[i / 2] = Byte.Parse(resp.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);
                    }

                    socket.Send(data, resp.Length / 2, remote);
                }
                else
                {
                    Console.WriteLine("unknown command");
                }
            }
        }
 public Parser(ASPFile f)
 {
     file = f;
 }
Exemple #9
0
 public override string decode(ASPFile context, string value)
 {
     throw new NotImplementedException();
 }
Exemple #10
0
 public override string decode(ASPFile context, string value)
 {
     return(name + ": " + "NULL");
 }
Exemple #11
0
 public abstract string decode(ASPFile context, string value);