Skip to content

Value lists and negated matchers

Daniel Frantík edited this page Nov 15, 2026 · 2 revisions

Value lists and negated matchers

Two kinds of RouterOS field hold more than one plain value: a list — dst-port=22,8291, connection-state=established,related — and a matcher negated with ! — src-address=!10.0.0.0/8. tik4net types both, so a port list is a list of ports and the ! is a flag rather than part of the text. Read TikField first: every type on this page sits inside one.

using System;
using System.Linq;
using tik4net;
using tik4net.Objects;
using tik4net.Objects.Ip.Firewall;

using ITikConnection connection = new TikConnectionSetup("192.168.88.1", "admin", "").Create(TikConnectionType.Api);

var rule = new FirewallFilter
{
    Chain = "input",
    SrcAddress = "!10.0.0.0/8",                                  // a negated matcher, in the router's spelling
    DstPort = new TikValueList<TikPortRange>(22, 8291),          // a list of ports
};
Console.WriteLine(rule.SrcAddress.IsNegated);                   // True
Console.WriteLine(rule.DstPort);                                // 22,8291

Value lists: TikValueList<T>

A field that holds several values — ports, connection states, address types, DNS servers — is a TikField<TikValueList<T>?>. The list is immutable: With and Without return a new one, so assign the result back. Each item is a TikValue<T>, so an item can be a word T lacks without the rest of the list losing its type. A port list holds TikPortRange items and a number list (vlan-ids, netwatch http-codes) TikNumberRange items, each a single value (22, an int converts) or a range (new TikPortRange(1000, 2000), read and written 1000-2000).

TikField<TikValueList<TikPortRange>?> dstPort = new TikValueList<TikPortRange>(22, 8291, new TikPortRange(1000, 2000));
dstPort = dstPort.Value!.With(443);                          // assign the new list back
Console.WriteLine(dstPort);                                  // 22,8291,1000-2000,443

TikNegatableField<TikValueList<TikPortRange>?> notThese = TikValueList<TikPortRange>.Not(22, 8291);
Console.WriteLine(notThese);                                 // !22,8291 — the whole list negated

var topics = new TikValueList<TikNegatableValue<string>>("info", "!debug");
Console.WriteLine(topics);                                   // info,!debug — one member negated

var flags = TikValueList<TikNegatableValue<FirewallTcpFlag>>.Parse("syn,!ack");  // the router's spelling
Console.WriteLine(flags.Contains(TikNegatableValue<FirewallTcpFlag>.Not(FirewallTcpFlag.Ack)));   // True

Parse reads a list the way the router writes it — items separated by ,; a ! in front of an item negates it on a list of TikNegatableValue<T> and is part of the item on any other — and refuses an item that is not a T; a word from another RouterOS version goes in as TikValue<T>.FromWire. A string does not convert to a list implicitly — write rule.DstPort = Tik.Wire("22,8291"), which parses the text for the property, or Parse: C# applies one user-defined conversion per assignment, and the property's is the one from the list. A ! on the whole list is not part of the list, so Parse refuses !,syn; write it as TikValueList<T>.Not(list), or parse the whole field with TikNegatableField<TikValueList<T>?>.Parse("!,syn").

The constructor takes items, never text: an item containing , — new TikValueList<string>("a,b") — is refused with an ArgumentException, by the constructors and by With alike, because the router would read it as two items. Text that may hold several values goes through Parse; the compiler warns about a constant item containing , as TIK003, with a fix that passes one argument per item.

  • Two lists are equal when they hold the same items the same number of times, in any order. The router prints some lists in an order of its own (ack,!syn,fin reads back fin,ack,!syn), and that must not look like a change. A save sends the items in the list's order, and a port list keeps it on the router.
  • Which ! a field takes is the property's type: a TikNegatableField<TikValueList<T>?> for the whole list, TikNegatableValue<T> items for each member. A negated member on any other list does not compile; a negated empty list is refused before anything is sent. The type also decides the whole-list spelling: !22,8291 on a plain list, !,syn,!ack on a list of negatable members, where !syn would be one negated member.
  • tcp-flags over the API, REST and the CLI: a text set replaces only the half it names, plain or negated members. An update whose new value lacks a half the row holds is refused before sending, with the reason; name at least one member of each kind, or write over WinBox native (StructuredWrites), which writes both halves exactly.

One list item: TikValue<T>

Each item of a value list is a TikValue<T>: the value, or the router's word when T cannot hold it. A word keeps what the router printed, a ! included, and is written back as it came.

Member Meaning
Value the value; throws TikUnparsedValueException when this is the router's word
IsWord, RawValue the router's word that T cannot hold, and the word itself
FromWire(word) the router's own word, sent as written

A TikValue<T> takes no !. On a field whose members the router negates one by one — tcp-flags=syn,!ack, hotspot, logging topics — the item type is TikNegatableValue<T>: a member and its own IsNegated, written TikNegatableValue<T>.Not(member). A plain member converts to it implicitly, and a string member takes the router's spelling ("!debug" is Not("debug")). Reading one goes through the item: flag.Value.IsNegated, flag.Value.Value.

Negated matchers

A firewall-style matcher can be negated: src-address=!10.0.0.0/8 matches every address outside that network, and connection-state=!established,related every state but those two. Such a property is a TikNegatableField<T>. The ! is not part of the value: it reads as IsNegated, the value itself parses as usual, and TikNegatableField<T>.Not(value) writes one. A string? matcher also takes the router's own spelling — RouterOS has no literal leading ! on these fields, so a string that starts with one is the negation:

var rule = connection.LoadAll<FirewallFilter>().First(r => r.SrcAddress.IsNegated);
Console.WriteLine(rule.SrcAddress.Value);                     // 10.0.0.0/8
Console.WriteLine(rule.SrcAddress);                           // !10.0.0.0/8

rule.SrcAddress = "!192.168.0.0/16";                          // the same as Not("192.168.0.0/16")
Console.WriteLine(rule.SrcAddress.IsNegated);                 // True — saved as src-address=!192.168.0.0/16
Console.WriteLine(rule.SrcAddress == TikNegatableField<string?>.Not("192.168.0.0/16"));   // True

rule.DstPort = TikValueList<TikPortRange>.Not(22, 8291);
rule.DstPort = rule.DstPort.WithoutNegation();                // the matcher without its '!'
rule.Comment = "!important";                                  // a TikField<string?>: the '!' is text
  • A negated value is never equal to the plain one. rule.SrcAddress == "10.0.0.0/8" is false for !10.0.0.0/8, and <, > are false for a negated number. Compare with "!10.0.0.0/8" or TikNegatableField<string?>.Not("10.0.0.0/8"), or test IsNegated and .Value.
  • A number, an enum or a list has no "!" spelling. Write TikNegatableField<int?>.Not(22) or TikValueList<TikPortRange>.Not(22, 8291), or parse the router's text: TikNegatableField<int?>.Parse("!22").
  • The ! negates the whole value, a list included: dst-port=!22,8291 is "neither 22 nor 8291". A field whose members are negated one by one, such as tcp-flags=syn,!ack, holds TikNegatableValue<T> items, each with its own !.
  • Only the matchers RouterOS negates are TikNegatableField<T>: in the shipped entities the firewall filter, NAT, mangle and raw rules, bridge filter and NAT rules, hotspot walled-garden entries and web-proxy access rules. Everywhere else a property is a TikField<T>, a leading ! is text (a comment may start with one), and a negation assigned to it does not compile.

See also

Clone this wiki locally