public int Feed(RuleParser parser, bool not) { switch (parser.GetCurrentArg()) { case OptionProtocolLong: case OptionProtocolShort: Protocol = new ValueOrNot <string>(parser.GetNextArg(), not); return(1); case OptionSourceLong: case OptionSourceShort: Source = new ValueOrNot <IpCidr>(IpCidr.Parse(parser.GetNextArg()), IpCidr.Any, not); return(1); case OptionDestinationLong: case OptionDestinationShort: Destination = new ValueOrNot <IpCidr>(IpCidr.Parse(parser.GetNextArg()), IpCidr.Any, not); return(1); case OptionJumpLong: case OptionJumpShort: Jump = parser.GetNextArg(); return(1); case OptionGotoLong: case OptionGotoShort: Goto = parser.GetNextArg(); return(1); case OptionInInterfaceLong: case OptionInInterfaceShort: InInterface = new ValueOrNot <string>(parser.GetNextArg(), not); return(1); case OptionOutInterfaceLong: case OptionOutInterfaceShort: OutInterface = new ValueOrNot <string>(parser.GetNextArg(), not); return(1); case OptionFragmentLong: case OptionFragmentShort: Fragmented = new ValueOrNot <bool>(true, not); return(0); case OptionSetCountersLong: case OptionSetCountersShort: SetCounters = new ValueOrNot <CounterPacketsAndBytes>( new CounterPacketsAndBytes(uint.Parse(parser.GetNextArg(1)), uint.Parse(parser.GetNextArg(2))), not); return(2); } return(0); }
/// <summary> /// Parse an entry for type /// </summary> /// <param name="entry"></param> /// <param name="value"></param> public static void ParseEntry(IpSetEntry entry, String value) { var typeComponents = entry.Set.TypeComponents; var optionComponents = value.Split(new char[] { ',' }); for (int i = 0; i < optionComponents.Length; i++) { switch (typeComponents[i]) { case "ip": if (entry.Cidr.Prefix == 0) { entry.Cidr = new IpCidr(IPAddress.Parse(optionComponents[i])); } else { entry.Cidr2 = new IpCidr(IPAddress.Parse(optionComponents[i])); } break; case "net": entry.Cidr = IpCidr.Parse(optionComponents[i]); var network = entry.Cidr.GetIPNetwork(); if (!Equals(network.Network, entry.Cidr.Address)) { entry.Cidr = new IpCidr(network.Network, entry.Cidr.Prefix); } break; case "port": var s = optionComponents[i].Split(':'); if (s.Length == 1) { entry.Port = ushort.Parse(s[0]); } else { entry.Protocol = s[0].ToLowerInvariant(); entry.Port = ushort.Parse(s[1]); } break; case "mac": entry.Mac = optionComponents[i]; break; } } }
/// <summary> /// Consume arguments /// </summary> /// <param name="position">Rhe position to parse</param> /// <returns>number of arguments consumed</returns> public int FeedToSkip(int position) { Position = position; String option = GetCurrentArg(); if (position == 0) { var set = _sets.GetSetByName(option); if (set == null) { throw new IpTablesNetException(String.Format("The set {0} does not exist", option)); } _entry.Set = set; set.Entries.Add(_entry); } else { var type = _entry.Set.Type; var typeComponents = IpSetTypeHelper.TypeComponents(IpSetTypeHelper.TypeToString(type)).ToArray(); var optionComponents = option.Split(new char[] { ',', ':' }); for (int i = 0; i < optionComponents.Length; i++) { switch (typeComponents[i]) { case "ip": _entry.Cidr = IpCidr.Parse(optionComponents[i]); break; case "port": if (i != 0) { _entry.Protocol = optionComponents[i]; i++; } _entry.Port = ushort.Parse(optionComponents[i]); break; case "mac": _entry.Mac = optionComponents[i]; break; } } } return(0); }