New feature in v3: cast!

Arran Cudbard-Bell a.cudbardb at freeradius.org
Sun May 12 00:48:18 CEST 2013


On 11 May 2013, at 10:19, Alan DeKok <aland at DEPLOYINGRADIUS.COM> wrote:

>  Not fishing casts, but data type casts. :)  See "man unlang" for details.
> 
>  if (<ipaddr>127.0.0.1 < &Framed-IP-Address) {
> 	...
>  }
> 
>  It allows you to do type-specific comparisons.  v2 usually did string
> comparisons for conditions.  To get type-specific comparisons, you
> sometimes needed to put the data into an intermediate variable.
> 
>  That was ugly.  The casting is nicer.
> 
>  That being said, the above example *did* work in v2, with some changes:
> 
>  if (Framed-IP-Address > 127.0.0.1) {
> 	...
>  }
> 
>  That worked by parsing the RHS into a temporary variable, and then
> doing type-specific comparisons.  But you couldn't do:
> 
>  if (<date>"January 1 2014" < "%{sql: SELECT ...}") {
> 	...
>  }
> 


FreeRADIUS will attempt to infer right operand type from the left operand. This worked fine where the left operand was an attribute reference, but not if it was just a string.

Without the cast

if ("January 1 2014" < "%{sql: SELECT ...}") {
	...
}

would be compared lexicographically which may have given the incorrect result.

Where either operand is an attribute reference, if it's not already of the correct type it will be printed to a string and then re-parsed as if a new  valuepair of the cast type was being created.

For the above example the 2.1.x equivalent would have been:

update request {
	Tmp-Date-0 := "January 1 2014"
}

if (Tmp-Date-0 < "%{sql: SELECT ...}") {
	...
}

One useful thing which i'm not sure Alan has implemented yet is:

if (<cidr>&Framed-IP-Address == 192.168.0.0/24) {

}

Without the cast "192.168.0.0/24" would either fail to parse or parse as "192.168.0.0".

With the cast, Framed-IP-Address gets converted to a /32, and the first 24 bits from the values are compared.

Arran Cudbard-Bell <a.cudbardb at freeradius.org>
FreeRADIUS Development Team



More information about the Freeradius-Devel mailing list