| 1 |
|
| 2 |
:LastChangedDate: $LastChangedDate$ |
| 3 |
:LastChangedRevision: $LastChangedRevision$ |
| 4 |
:LastChangedBy: $LastChangedBy$ |
| 5 |
|
| 6 |
Banana Protocol Specifications |
| 7 |
============================== |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
Introduction |
| 15 |
------------ |
| 16 |
|
| 17 |
Banana is an efficient, extendable protocol for sending and receiving s-expressions. |
| 18 |
A s-expression in this context is a list composed of bytes, integers, large integers, floats and/or s-expressions. |
| 19 |
Unicode is not supported (but can be encoded to and decoded from bytes on the way into and out of Banana). |
| 20 |
Unsupported types must be converted into a supported type before sending them with Banana. |
| 21 |
|
| 22 |
|
| 23 |
Banana Encodings |
| 24 |
---------------- |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
The banana protocol is a stream of data composed of elements. Each element has the |
| 30 |
following general structure - first, the length of element encoded in base-128, least significant |
| 31 |
bit first. For example length 4674 will be sent as ``0x42 0x24`` . For certain element |
| 32 |
types the length will be omitted (e.g. float) or have a different meaning (it is the actual |
| 33 |
value of integer elements). |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
Following the length is a delimiter byte, which tells us what kind of element this |
| 41 |
is. Depending on the element type, there will then follow the number of bytes specified |
| 42 |
in the length. The byte's high-bit will always be set, so that we can differentiate |
| 43 |
between it and the length (since the length bytes use 128-base, their high bit will |
| 44 |
never be set). |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
Element Types |
| 52 |
------------- |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
Given a series of bytes that gave us length N, these are the different delimiter bytes: |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
List -- 0x80 |
| 67 |
|
| 68 |
|
| 69 |
The following bytes are a list of N elements. Lists may be nested, |
| 70 |
and a child list counts as only one element to its parent (regardless |
| 71 |
of how many elements the child list contains). |
| 72 |
|
| 73 |
Integer -- 0x81 |
| 74 |
|
| 75 |
The value of this element is the positive integer N. Following bytes are not part of this element. Integers can have values of 0 <= N <= 2147483647. |
| 76 |
|
| 77 |
String -- 0x82 |
| 78 |
|
| 79 |
The following N bytes are a string element. |
| 80 |
|
| 81 |
Negative Integer -- 0x83 |
| 82 |
|
| 83 |
The value of this element is the integer N * -1, i.e. -N. Following bytes are not part of this element. Negative integers can have values of 0 >= -N >= -2147483648. |
| 84 |
|
| 85 |
Float - 0x84 |
| 86 |
|
| 87 |
The next 8 bytes are the float encoded in IEEE 754 floating-point "double format" bit layout. |
| 88 |
No length bytes should have been defined. |
| 89 |
|
| 90 |
Large Integer -- 0x85 |
| 91 |
|
| 92 |
The value of this element is the positive large integer N. Following bytes are not part of this element. Large integers have no size limitation. |
| 93 |
|
| 94 |
Large Negative Integer -- 0x86 |
| 95 |
|
| 96 |
The value of this element is the negative large integer -N. Following bytes are not part of this element. Large integers have no size limitation. |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
Large integers are intended for arbitrary length integers. Regular integers types (positive and negative) are limited to 32-bit values. |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
Examples |
| 112 |
~~~~~~~~ |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
Here are some examples of elements and their encodings - the type bytes are marked in bold: |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
``1`` |
| 127 |
|
| 128 |
``0x01 **0x81**`` |
| 129 |
|
| 130 |
``-1`` |
| 131 |
|
| 132 |
``0x01 **0x83**`` |
| 133 |
|
| 134 |
``1.5`` |
| 135 |
|
| 136 |
``**0x84** 0x3f 0xf8 0x00 0x00 0x00 0x00 0x00 0x00`` |
| 137 |
|
| 138 |
``"hello"`` |
| 139 |
|
| 140 |
``0x05 **0x82** 0x68 0x65 0x6c 0x6c 0x6f`` |
| 141 |
|
| 142 |
``[]`` |
| 143 |
|
| 144 |
``0x00 **0x80**`` |
| 145 |
|
| 146 |
``[1, 23]`` |
| 147 |
|
| 148 |
``0x02 **0x80** 0x01 **0x81** 0x17 **0x81**`` |
| 149 |
|
| 150 |
``123456789123456789`` |
| 151 |
|
| 152 |
``0x15 0x3e 0x41 0x66 0x3a 0x69 0x26 0x5b 0x01 **0x85**`` |
| 153 |
|
| 154 |
``[1, ["hello"]]`` |
| 155 |
|
| 156 |
``0x02 **0x80** 0x01 **0x81** 0x01 **0x80** 0x05 **0x82** 0x68 0x65 0x6c 0x6c 0x6f`` |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
Profiles |
| 165 |
-------- |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
The Banana protocol is extendable. Therefore, it supports the concept of profiles. Profiles allow |
| 171 |
developers to extend the banana protocol, adding new element types, while still keeping backwards |
| 172 |
compatibility with implementations that don't support the extensions. The profile used in each |
| 173 |
session is determined at the handshake stage (see below.) |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
A profile is specified by a unique string. This specification defines two profiles |
| 181 |
- ``"none"`` and ``"pb"`` . The ``"none"`` profile is the standard |
| 182 |
profile that should be supported by all Banana implementations. |
| 183 |
Additional profiles may be added in the future. |
| 184 |
|
| 185 |
Extensions defined by a profile may only be used if that profile has been selected by client and server. |
| 186 |
|
| 187 |
|
| 188 |
The ``"none"`` Profile |
| 189 |
~~~~~~~~~~~~~~~~~~~~~~~ |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
The ``"none"`` profile is identical to the delimiter types listed above. It is highly recommended |
| 195 |
that all Banana clients and servers support the ``"none"`` profile. |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
The ``"pb"`` Profile |
| 203 |
~~~~~~~~~~~~~~~~~~~~~ |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
The ``"pb"`` profile is intended for use with the Perspective Broker protocol, that runs on top |
| 209 |
of Banana. Basically, it converts commonly used PB strings into shorter versions, thus |
| 210 |
minimizing bandwidth usage. It starts with a single byte, which tells us to which string element |
| 211 |
to convert it, and ends with the delimiter byte, ``0x87`` , which should not be prefixed |
| 212 |
by a length. |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
0x01 |
| 222 |
'None' |
| 223 |
|
| 224 |
0x02 |
| 225 |
'class' |
| 226 |
|
| 227 |
0x03 |
| 228 |
'dereference' |
| 229 |
|
| 230 |
0x04 |
| 231 |
'reference' |
| 232 |
|
| 233 |
0x05 |
| 234 |
'dictionary' |
| 235 |
|
| 236 |
0x06 |
| 237 |
'function' |
| 238 |
|
| 239 |
0x07 |
| 240 |
'instance' |
| 241 |
|
| 242 |
0x08 |
| 243 |
'list' |
| 244 |
|
| 245 |
0x09 |
| 246 |
'module' |
| 247 |
|
| 248 |
0x0a |
| 249 |
'persistent' |
| 250 |
|
| 251 |
0x0b |
| 252 |
'tuple' |
| 253 |
|
| 254 |
0x0c |
| 255 |
'unpersistable' |
| 256 |
|
| 257 |
0x0d |
| 258 |
'copy' |
| 259 |
|
| 260 |
0x0e |
| 261 |
'cache' |
| 262 |
|
| 263 |
0x0f |
| 264 |
'cached' |
| 265 |
|
| 266 |
0x10 |
| 267 |
'remote' |
| 268 |
|
| 269 |
0x11 |
| 270 |
'local' |
| 271 |
|
| 272 |
0x12 |
| 273 |
'lcache' |
| 274 |
|
| 275 |
0x13 |
| 276 |
'version' |
| 277 |
|
| 278 |
0x14 |
| 279 |
'login' |
| 280 |
|
| 281 |
0x15 |
| 282 |
'password' |
| 283 |
|
| 284 |
0x16 |
| 285 |
'challenge' |
| 286 |
|
| 287 |
0x17 |
| 288 |
'logged_in' |
| 289 |
|
| 290 |
0x18 |
| 291 |
'not_logged_in' |
| 292 |
|
| 293 |
0x19 |
| 294 |
'cachemessage' |
| 295 |
|
| 296 |
0x1a |
| 297 |
'message' |
| 298 |
|
| 299 |
0x1b |
| 300 |
'answer' |
| 301 |
|
| 302 |
0x1c |
| 303 |
'error' |
| 304 |
|
| 305 |
0x1d |
| 306 |
'decref' |
| 307 |
|
| 308 |
0x1e |
| 309 |
'decache' |
| 310 |
|
| 311 |
0x1f |
| 312 |
'uncache' |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
Protocol Handshake and Behaviour |
| 321 |
-------------------------------- |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
The initiating side of the connection will be referred to as "client" , and the other |
| 327 |
side as "server" . |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
Upon connection, the server will send the client a list of string elements, signifying |
| 335 |
the profiles it supports. It is recommended that ``"none"`` be included in this list. The client |
| 336 |
then sends the server a string from this list, telling the server which profile it wants to |
| 337 |
use. At this point the whole session will use this profile. |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
Once a profile has been established, the two sides may start exchanging elements. There is no |
| 345 |
limitation on order or dependencies of messages. Any such limitation (e.g. "server can only send an element to client in response to a request from client" ) is application specific. |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
Upon receiving illegal messages, failed handshakes, etc., a Banana client or server should |
| 353 |
close its connection. |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|