Repository navigation
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,8291A 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))); // TrueParse 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,finreads backfin,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: aTikNegatableField<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,8291on a plain list,!,syn,!ackon a list of negatable members, where!synwould be one negated member. -
tcp-flagsover the API, REST and the CLI: a textsetreplaces 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.
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.
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"orTikNegatableField<string?>.Not("10.0.0.0/8"), or testIsNegatedand.Value. -
A number, an enum or a list has no
"!"spelling. WriteTikNegatableField<int?>.Not(22)orTikValueList<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,8291is "neither 22 nor 8291". A field whose members are negated one by one, such astcp-flags=syn,!ack, holdsTikNegatableValue<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 aTikField<T>, a leading!is text (a comment may start with one), and a negation assigned to it does not compile.
- TikField — the wrapper around every one of these
-
Entity value types —
TikPortRange,TikNumberRangeand the other item types - TikListMerge — comparing and syncing whole lists of rows
Start
- Getting started — first project
- From the console to tik4net — what you type, what you call
- Building an application — a service, reconnecting, logging
- Library overview
- CRUD at all three levels
- One task, every transport
Connecting
- Connecting to a router — setup, options, connection string
- Transports & capabilities — the matrix
- Threading & concurrency
- API · API-SSL
- REST
-
CLI transports
- Telnet · SSH · WinBox CLI
- MAC-layer transports — no IP needed
- WinBox native · GUI-like syntax
- RoMON — through a neighbouring router
- MNDP discovery
Working with data
- Low-level API — raw sentences
- ADO.NET-like API — commands, parameters
- CLI raw commands · tips & tricks
- Command translation
- Async & cancellation
- Monitors & streaming — ping, torch, listen
O/R mapper
-
High-level API
- Reading data · Create, update, delete
- Change tracking · List merge
- TikField · Value lists & negated matchers · Value types · Helpers
- Entity reference — every covered menu
- Custom entities
Safety
- Safe Mode — rollback protection
- Exception handling
RouterOS
Testing & diagnostics
Extending & tools
Project