Handling binary protocolsThere are several TrafficScript functions that will help you to manage binary data. Here are some points that may help:
There are also a range of functions that convert between different ASCII encodings of strings, such as hex, base64 and %-encoding, which may also be useful. ExampleSuppose that you have a protocol in which the request looks like the <2-byte msg length><msg> You wish to add a header to the msg and update the length in a request rule: <2 byte msg length2><header><msg> The following TrafficScript rule will suffice: # 1. calculate the length of the entire message: $len_str = request.get( 2 ); # read the 2-byte length $len = string.bytesToInt( $len_str, 2 ); # parse it # 2. read the entire message $msg = request.get( 2 + $len ); # 3. Insert the header just after the length $msg = string.insertBytes( $msg, $header, 2 ); # 4. Update the length $len = $len + string.len( $header ); $len_str = string.intToBytes( $len ); $msg = string.replaceBytes( $msg, $len_str, 0 ); # 5. Change the request data request.set( $msg ); The various TrafficScript functions are documented in detail in the TrafficScript manual. You can download the most recent version of this manual from the Documentation part of this site.
Owen Garrett
[Zeus Dev Team] 08 December 2005
|
Recent Articles
Other Resources
|


