private void parseChoiceType(Match match) { Dictionary <string, IType> members = new Dictionary <string, IType>(); foreach (string el in match.Groups["content"].Value.Split(",")) { Match syntaxMatch = Expressions.Types.syntax.Match(el.Trim()); var cons = parseConstraint(syntaxMatch.Groups["cons"].Value); IType baseType = file.fetchType(syntaxMatch.Groups["type"].Value); if (cons != null) { members.Add(syntaxMatch.Groups["name"].Value, baseType.derive(syntaxMatch.Groups["name"].Value, cons.Item1, cons.Item2)); } else { members.Add(syntaxMatch.Groups["name"].Value, baseType); } } file.AddType(new ChoiceType(match.Groups["name"].Value, members)); }
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"); } } }