banana.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. # -*- test-case-name: twisted.spread.test.test_banana -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Banana -- s-exp based protocol.
  6. Future Plans: This module is almost entirely stable. The same caveat applies
  7. to it as applies to L{twisted.spread.jelly}, however. Read its future plans
  8. for more details.
  9. @author: Glyph Lefkowitz
  10. """
  11. from __future__ import absolute_import, division
  12. import copy, struct
  13. from io import BytesIO
  14. from twisted.internet import protocol
  15. from twisted.persisted import styles
  16. from twisted.python import log
  17. from twisted.python.compat import iterbytes, long, _bytesChr as chr
  18. from twisted.python.reflect import fullyQualifiedName
  19. class BananaError(Exception):
  20. pass
  21. def int2b128(integer, stream):
  22. if integer == 0:
  23. stream(chr(0))
  24. return
  25. assert integer > 0, "can only encode positive integers"
  26. while integer:
  27. stream(chr(integer & 0x7f))
  28. integer = integer >> 7
  29. def b1282int(st):
  30. """
  31. Convert an integer represented as a base 128 string into an L{int} or
  32. L{long}.
  33. @param st: The integer encoded in a byte string.
  34. @type st: L{bytes}
  35. @return: The integer value extracted from the byte string.
  36. @rtype: L{int} or L{long}
  37. """
  38. e = 1
  39. i = 0
  40. for char in iterbytes(st):
  41. n = ord(char)
  42. i += (n * e)
  43. e <<= 7
  44. return i
  45. # delimiter characters.
  46. LIST = chr(0x80)
  47. INT = chr(0x81)
  48. STRING = chr(0x82)
  49. NEG = chr(0x83)
  50. FLOAT = chr(0x84)
  51. # "optional" -- these might be refused by a low-level implementation.
  52. LONGINT = chr(0x85)
  53. LONGNEG = chr(0x86)
  54. # really optional; this is part of the 'pb' vocabulary
  55. VOCAB = chr(0x87)
  56. HIGH_BIT_SET = chr(0x80)
  57. def setPrefixLimit(limit):
  58. """
  59. Set the limit on the prefix length for all Banana connections
  60. established after this call.
  61. The prefix length limit determines how many bytes of prefix a banana
  62. decoder will allow before rejecting a potential object as too large.
  63. @type limit: L{int}
  64. @param limit: The number of bytes of prefix for banana to allow when
  65. decoding.
  66. """
  67. global _PREFIX_LIMIT
  68. _PREFIX_LIMIT = limit
  69. _PREFIX_LIMIT = None
  70. setPrefixLimit(64)
  71. SIZE_LIMIT = 640 * 1024 # 640k is all you'll ever need :-)
  72. class Banana(protocol.Protocol, styles.Ephemeral):
  73. """
  74. L{Banana} implements the I{Banana} s-expression protocol, client and
  75. server.
  76. @ivar knownDialects: These are the profiles supported by this Banana
  77. implementation.
  78. @type knownDialects: L{list} of L{bytes}
  79. """
  80. # The specification calls these profiles but this implementation calls them
  81. # dialects instead.
  82. knownDialects = [b"pb", b"none"]
  83. prefixLimit = None
  84. sizeLimit = SIZE_LIMIT
  85. def setPrefixLimit(self, limit):
  86. """
  87. Set the prefix limit for decoding done by this protocol instance.
  88. @see: L{setPrefixLimit}
  89. """
  90. self.prefixLimit = limit
  91. self._smallestLongInt = -2 ** (limit * 7) + 1
  92. self._smallestInt = -2 ** 31
  93. self._largestInt = 2 ** 31 - 1
  94. self._largestLongInt = 2 ** (limit * 7) - 1
  95. def connectionReady(self):
  96. """Surrogate for connectionMade
  97. Called after protocol negotiation.
  98. """
  99. def _selectDialect(self, dialect):
  100. self.currentDialect = dialect
  101. self.connectionReady()
  102. def callExpressionReceived(self, obj):
  103. if self.currentDialect:
  104. self.expressionReceived(obj)
  105. else:
  106. # this is the first message we've received
  107. if self.isClient:
  108. # if I'm a client I have to respond
  109. for serverVer in obj:
  110. if serverVer in self.knownDialects:
  111. self.sendEncoded(serverVer)
  112. self._selectDialect(serverVer)
  113. break
  114. else:
  115. # I can't speak any of those dialects.
  116. log.msg("The client doesn't speak any of the protocols "
  117. "offered by the server: disconnecting.")
  118. self.transport.loseConnection()
  119. else:
  120. if obj in self.knownDialects:
  121. self._selectDialect(obj)
  122. else:
  123. # the client just selected a protocol that I did not suggest.
  124. log.msg("The client selected a protocol the server didn't "
  125. "suggest and doesn't know: disconnecting.")
  126. self.transport.loseConnection()
  127. def connectionMade(self):
  128. self.setPrefixLimit(_PREFIX_LIMIT)
  129. self.currentDialect = None
  130. if not self.isClient:
  131. self.sendEncoded(self.knownDialects)
  132. def gotItem(self, item):
  133. l = self.listStack
  134. if l:
  135. l[-1][1].append(item)
  136. else:
  137. self.callExpressionReceived(item)
  138. buffer = b''
  139. def dataReceived(self, chunk):
  140. buffer = self.buffer + chunk
  141. listStack = self.listStack
  142. gotItem = self.gotItem
  143. while buffer:
  144. assert self.buffer != buffer, "This ain't right: %s %s" % (repr(self.buffer), repr(buffer))
  145. self.buffer = buffer
  146. pos = 0
  147. for ch in iterbytes(buffer):
  148. if ch >= HIGH_BIT_SET:
  149. break
  150. pos = pos + 1
  151. else:
  152. if pos > self.prefixLimit:
  153. raise BananaError("Security precaution: more than %d bytes of prefix" % (self.prefixLimit,))
  154. return
  155. num = buffer[:pos]
  156. typebyte = buffer[pos:pos+1]
  157. rest = buffer[pos+1:]
  158. if len(num) > self.prefixLimit:
  159. raise BananaError("Security precaution: longer than %d bytes worth of prefix" % (self.prefixLimit,))
  160. if typebyte == LIST:
  161. num = b1282int(num)
  162. if num > SIZE_LIMIT:
  163. raise BananaError("Security precaution: List too long.")
  164. listStack.append((num, []))
  165. buffer = rest
  166. elif typebyte == STRING:
  167. num = b1282int(num)
  168. if num > SIZE_LIMIT:
  169. raise BananaError("Security precaution: String too long.")
  170. if len(rest) >= num:
  171. buffer = rest[num:]
  172. gotItem(rest[:num])
  173. else:
  174. return
  175. elif typebyte == INT:
  176. buffer = rest
  177. num = b1282int(num)
  178. gotItem(num)
  179. elif typebyte == LONGINT:
  180. buffer = rest
  181. num = b1282int(num)
  182. gotItem(num)
  183. elif typebyte == LONGNEG:
  184. buffer = rest
  185. num = b1282int(num)
  186. gotItem(-num)
  187. elif typebyte == NEG:
  188. buffer = rest
  189. num = -b1282int(num)
  190. gotItem(num)
  191. elif typebyte == VOCAB:
  192. buffer = rest
  193. num = b1282int(num)
  194. item = self.incomingVocabulary[num]
  195. if self.currentDialect == b'pb':
  196. # the sender issues VOCAB only for dialect pb
  197. gotItem(item)
  198. else:
  199. raise NotImplementedError(
  200. "Invalid item for pb protocol {0!r}".format(item))
  201. elif typebyte == FLOAT:
  202. if len(rest) >= 8:
  203. buffer = rest[8:]
  204. gotItem(struct.unpack("!d", rest[:8])[0])
  205. else:
  206. return
  207. else:
  208. raise NotImplementedError(("Invalid Type Byte %r" % (typebyte,)))
  209. while listStack and (len(listStack[-1][1]) == listStack[-1][0]):
  210. item = listStack.pop()[1]
  211. gotItem(item)
  212. self.buffer = b''
  213. def expressionReceived(self, lst):
  214. """Called when an expression (list, string, or int) is received.
  215. """
  216. raise NotImplementedError()
  217. outgoingVocabulary = {
  218. # Jelly Data Types
  219. b'None' : 1,
  220. b'class' : 2,
  221. b'dereference' : 3,
  222. b'reference' : 4,
  223. b'dictionary' : 5,
  224. b'function' : 6,
  225. b'instance' : 7,
  226. b'list' : 8,
  227. b'module' : 9,
  228. b'persistent' : 10,
  229. b'tuple' : 11,
  230. b'unpersistable' : 12,
  231. # PB Data Types
  232. b'copy' : 13,
  233. b'cache' : 14,
  234. b'cached' : 15,
  235. b'remote' : 16,
  236. b'local' : 17,
  237. b'lcache' : 18,
  238. # PB Protocol Messages
  239. b'version' : 19,
  240. b'login' : 20,
  241. b'password' : 21,
  242. b'challenge' : 22,
  243. b'logged_in' : 23,
  244. b'not_logged_in' : 24,
  245. b'cachemessage' : 25,
  246. b'message' : 26,
  247. b'answer' : 27,
  248. b'error' : 28,
  249. b'decref' : 29,
  250. b'decache' : 30,
  251. b'uncache' : 31,
  252. }
  253. incomingVocabulary = {}
  254. for k, v in outgoingVocabulary.items():
  255. incomingVocabulary[v] = k
  256. def __init__(self, isClient=1):
  257. self.listStack = []
  258. self.outgoingSymbols = copy.copy(self.outgoingVocabulary)
  259. self.outgoingSymbolCount = 0
  260. self.isClient = isClient
  261. def sendEncoded(self, obj):
  262. """
  263. Send the encoded representation of the given object:
  264. @param obj: An object to encode and send.
  265. @raise BananaError: If the given object is not an instance of one of
  266. the types supported by Banana.
  267. @return: L{None}
  268. """
  269. encodeStream = BytesIO()
  270. self._encode(obj, encodeStream.write)
  271. value = encodeStream.getvalue()
  272. self.transport.write(value)
  273. def _encode(self, obj, write):
  274. if isinstance(obj, (list, tuple)):
  275. if len(obj) > SIZE_LIMIT:
  276. raise BananaError(
  277. "list/tuple is too long to send (%d)" % (len(obj),))
  278. int2b128(len(obj), write)
  279. write(LIST)
  280. for elem in obj:
  281. self._encode(elem, write)
  282. elif isinstance(obj, (int, long)):
  283. if obj < self._smallestLongInt or obj > self._largestLongInt:
  284. raise BananaError(
  285. "int/long is too large to send (%d)" % (obj,))
  286. if obj < self._smallestInt:
  287. int2b128(-obj, write)
  288. write(LONGNEG)
  289. elif obj < 0:
  290. int2b128(-obj, write)
  291. write(NEG)
  292. elif obj <= self._largestInt:
  293. int2b128(obj, write)
  294. write(INT)
  295. else:
  296. int2b128(obj, write)
  297. write(LONGINT)
  298. elif isinstance(obj, float):
  299. write(FLOAT)
  300. write(struct.pack("!d", obj))
  301. elif isinstance(obj, bytes):
  302. # TODO: an API for extending banana...
  303. if self.currentDialect == b"pb" and obj in self.outgoingSymbols:
  304. symbolID = self.outgoingSymbols[obj]
  305. int2b128(symbolID, write)
  306. write(VOCAB)
  307. else:
  308. if len(obj) > SIZE_LIMIT:
  309. raise BananaError(
  310. "byte string is too long to send (%d)" % (len(obj),))
  311. int2b128(len(obj), write)
  312. write(STRING)
  313. write(obj)
  314. else:
  315. raise BananaError("Banana cannot send {0} objects: {1!r}".format(
  316. fullyQualifiedName(type(obj)), obj))
  317. # For use from the interactive interpreter
  318. _i = Banana()
  319. _i.connectionMade()
  320. _i._selectDialect(b"none")
  321. def encode(lst):
  322. """Encode a list s-expression."""
  323. encodeStream = BytesIO()
  324. _i.transport = encodeStream
  325. _i.sendEncoded(lst)
  326. return encodeStream.getvalue()
  327. def decode(st):
  328. """
  329. Decode a banana-encoded string.
  330. """
  331. l = []
  332. _i.expressionReceived = l.append
  333. try:
  334. _i.dataReceived(st)
  335. finally:
  336. _i.buffer = b''
  337. del _i.expressionReceived
  338. return l[0]