| 1 |
= Comparisons |
| 2 |
|
| 3 |
.Syntax |
| 4 |
[source,unlang] |
| 5 |
---- |
| 6 |
lhs OP rhs |
| 7 |
---- |
| 8 |
|
| 9 |
The most common use-case for conditions is to perform comparisons. |
| 10 |
The `lhs` and `rhs` of a conditional comparison can be |
| 11 |
xref:attr.adoc[&Attribute-Name] or xref:type/index.adoc[data]. The |
| 12 |
the `OP` is an operator, commonly `==` or `\<=`. It is used to |
| 13 |
control how the two other portions of the condition are compared. |
| 14 |
|
| 15 |
== The Comparison Operators |
| 16 |
|
| 17 |
The comparison operators are given below. |
| 18 |
|
| 19 |
[options="header"] |
| 20 |
|===== |
| 21 |
| Operator | Description |
| 22 |
| < | less than |
| 23 |
| \<= | less than or equals |
| 24 |
| == | equals |
| 25 |
| != | not equals |
| 26 |
| >= | greater than or equals |
| 27 |
| > | greater than |
| 28 |
| xref:condition/regex.adoc[=~] | regular expression matches |
| 29 |
| xref:condition/regex.adoc[!~] | regular expression does not match |
| 30 |
|===== |
| 31 |
|
| 32 |
The comparison operators perform _type-specific_ comparisons. The |
| 33 |
only exceptions are the xref:condition/regex.adoc[regular expression] operators, |
| 34 |
which interpret the `lhs` as a printable string, and the `rhs` as a |
| 35 |
regular expression. |
| 36 |
|
| 37 |
== IP Address Comparisons |
| 38 |
|
| 39 |
The type-specific comparisons operate as expected for most data types. |
| 40 |
The only exception is data types that are IP addresses or IP address |
| 41 |
prefixes. For those data types, the comparisons are done via the |
| 42 |
following rules: |
| 43 |
|
| 44 |
* Any unqualified IP address is assumed to have a /32 prefix (IPv4) |
| 45 |
or a /128 prefix (IPv6). |
| 46 |
|
| 47 |
* If the prefixes of the left and right sides are equal, then the comparisons |
| 48 |
are performed on the IP address portion. |
| 49 |
|
| 50 |
* If the prefixes of the left and right sides are not equal, then the |
| 51 |
comparisons are performed as _set membership checks_. |
| 52 |
|
| 53 |
The syntax allows conditions such as `192.0.2.1 < 192.0.2/24`. This |
| 54 |
condition will return `true`, as the IP address `192.0.2.1' is within |
| 55 |
the network `192.0.2/24`. |
| 56 |
|
| 57 |
== Casting |
| 58 |
|
| 59 |
In some situations, it is useful to force the left side to be |
| 60 |
interpreted as a particular data type. |
| 61 |
|
| 62 |
[NOTE] |
| 63 |
The data types used by the cast *must* be a type taken from the RADIUS |
| 64 |
dictionaries, e.g., `ipaddr`, `integer`, etc. These types are not the |
| 65 |
same as the xref:type/index.adoc[data types] used in the |
| 66 |
configuration files. |
| 67 |
|
| 68 |
.Syntax |
| 69 |
[source,unlang] |
| 70 |
---- |
| 71 |
<cast>lhs OP rhs |
| 72 |
---- |
| 73 |
|
| 74 |
The `cast` text can be any one of the standard RADIUS dictionary data |
| 75 |
types, as with the following example: |
| 76 |
|
| 77 |
.Example |
| 78 |
[source,unlang] |
| 79 |
---- |
| 80 |
<ipaddr>&Class == 127.0.0.1 |
| 81 |
---- |
| 82 |
|
| 83 |
In this example, the `Class` attribute is treated as if it was an IPv4 |
| 84 |
address and is compared to the address `127.0.0.1` |
| 85 |
|
| 86 |
Casting is most useful when the left side of a comparison is a |
| 87 |
dynamically expanded string. The cast ensures that the comparison is |
| 88 |
done in a type-safe manner, instead of performing a string comparison. |
| 89 |
|
| 90 |
.Example |
| 91 |
[source,unlang] |
| 92 |
---- |
| 93 |
<integer>`/bin/echo 00` == 0 |
| 94 |
---- |
| 95 |
|
| 96 |
In this example, the string output of the `echo` program is interpreted as an |
| 97 |
integer. It is then compared to the right side via integer |
| 98 |
comparisons. Since the integer `00` is equivalent to the integer `0`, |
| 99 |
the comparison will match. If the comparison had been performed via |
| 100 |
string equality checks, then the comparison would fail, because the |
| 101 |
strings `00` and `0` are different. |
| 102 |
|
| 103 |
// Copyright (C) 2020 Network RADIUS SAS. Licenced under CC-by-NC 4.0. |
| 104 |
// Development of this documentation was sponsored by Network RADIUS SAS. |