Forum Discussion
Andy_Herrman_22
Nimbostratus
Jul 20, 2007Hex values in switch
I have an iRule that extracts some data from a TCP connection and uses it for the persistence value. The first 4 bytes are treated as a magic value and the persistence is only applied if the magic value is correct (defined by the protocol)
The protocol documentation defines all constants like this in hex, and I'd prefer to have the values in the code also be in hex. From the documentation it looks like TCL handles hex values properly, but I'm having trouble getting it to work.
Here's my iRule:
when CLIENT_ACCEPTED {
TCP::collect 8
}
when CLIENT_DATA {
set dataVal [TCP::payload]
set numScanned [binary scan $dataVal II magic id]
if { $numScanned < 2 } {
binary scan $dataVal H16 hexRep
log local0. "TCP connection didn't receive enough data: $hexRep"
return
}
switch -- $magic {
0x003A1F87 {
magic == 0x003a1f87 == 3809159
set persistVal $id
log local0. "Persisting connection: $persistVal"
persist uie $persistVal
}
default {
log local0. "Got socket connection with invalid magic value: $magic"
}
}
}
Using this iRule the default case is always hit. However, if I replace the hex value with the decimal value:
...
switch -- $magic {
3809159{
magic == 0x003a1f87 == 3809159
...
it works fine.
How come the hex value isn't working but the decimal one is? Is there a way to use hex, or am I stuck with dec?
- Al_Carandang_11
Nimbostratus
I believe switch does string comparisons only. A series of if statements would probably be needed if you wanted to do numeric comparisons on the hex values.
Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects