btformats.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # The contents of this file are subject to the BitTorrent Open Source License
  2. # Version 1.1 (the License). You may not copy or use this file, in either
  3. # source code or executable form, except in compliance with the License. You
  4. # may obtain a copy of the License at http://www.bittorrent.com/license/.
  5. #
  6. # Software distributed under the License is distributed on an AS IS basis,
  7. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  8. # for the specific language governing rights and limitations under the
  9. # License.
  10. # Written by Bram Cohen
  11. from BTL.translation import _
  12. from BitTorrent import BTFailure
  13. ints = (long, int)
  14. def check_peers(message):
  15. if type(message) != dict:
  16. raise BTFailure
  17. if message.has_key('failure reason'):
  18. if type(message['failure reason']) != str:
  19. raise BTFailure, _("failure reason must be a string")
  20. return
  21. if message.has_key('warning message'):
  22. if type(message['warning message']) != str:
  23. raise BTFailure, _("warning message must be a string")
  24. peers = message.get('peers')
  25. if type(peers) == list:
  26. for p in peers:
  27. if type(p) != dict:
  28. raise BTFailure, _("invalid entry in peer list - peer info must be a dict")
  29. if type(p.get('ip')) != str:
  30. raise BTFailure, _("invalid entry in peer list - peer ip must be a string")
  31. port = p.get('port')
  32. if type(port) not in ints or p <= 0:
  33. raise BTFailure, _("invalid entry in peer list - peer port must be an integer")
  34. if p.has_key('peer id'):
  35. peerid = p.get('peer id')
  36. if type(peerid) != str or len(peerid) != 20:
  37. raise BTFailure, _("invalid entry in peer list - invalid peerid")
  38. elif type(peers) != str or len(peers) % 6 != 0:
  39. raise BTFailure, _("invalid peer list")
  40. interval = message.get('interval', 1)
  41. if type(interval) not in ints or interval <= 0:
  42. raise BTFailure, _("invalid announce interval")
  43. minint = message.get('min interval', 1)
  44. if type(minint) not in ints or minint <= 0:
  45. raise BTFailure, _("invalid min announce interval")
  46. if type(message.get('tracker id', '')) != str:
  47. raise BTFailure, _("invalid tracker id")
  48. npeers = message.get('num peers', 0)
  49. if type(npeers) not in ints or npeers < 0:
  50. raise BTFailure, _("invalid peer count")
  51. dpeers = message.get('done peers', 0)
  52. if type(dpeers) not in ints or dpeers < 0:
  53. raise BTFailure, _("invalid seed count")
  54. last = message.get('last', 0)
  55. if type(last) not in ints or last < 0:
  56. raise BTFailure, _('invalid "last" entry')