Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

common.py 9.1KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. # -*- test-case-name: twisted.names.test -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Base functionality useful to various parts of Twisted Names.
  6. """
  7. import socket
  8. from zope.interface import implementer
  9. from twisted.internet import defer, error, interfaces
  10. from twisted.logger import Logger
  11. from twisted.names import dns
  12. from twisted.names.error import (
  13. DNSFormatError,
  14. DNSNameError,
  15. DNSNotImplementedError,
  16. DNSQueryRefusedError,
  17. DNSServerError,
  18. DNSUnknownError,
  19. )
  20. # Helpers for indexing the three-tuples that get thrown around by this code a
  21. # lot.
  22. _ANS, _AUTH, _ADD = range(3)
  23. EMPTY_RESULT = (), (), ()
  24. @implementer(interfaces.IResolver)
  25. class ResolverBase:
  26. """
  27. L{ResolverBase} is a base class for implementations of
  28. L{interfaces.IResolver} which deals with a lot
  29. of the boilerplate of implementing all of the lookup methods.
  30. @cvar _errormap: A C{dict} mapping DNS protocol failure response codes
  31. to exception classes which will be used to represent those failures.
  32. """
  33. _log = Logger()
  34. _errormap = {
  35. dns.EFORMAT: DNSFormatError,
  36. dns.ESERVER: DNSServerError,
  37. dns.ENAME: DNSNameError,
  38. dns.ENOTIMP: DNSNotImplementedError,
  39. dns.EREFUSED: DNSQueryRefusedError,
  40. }
  41. typeToMethod = None
  42. def __init__(self):
  43. self.typeToMethod = {}
  44. for (k, v) in typeToMethod.items():
  45. self.typeToMethod[k] = getattr(self, v)
  46. def exceptionForCode(self, responseCode):
  47. """
  48. Convert a response code (one of the possible values of
  49. L{dns.Message.rCode} to an exception instance representing it.
  50. @since: 10.0
  51. """
  52. return self._errormap.get(responseCode, DNSUnknownError)
  53. def query(self, query, timeout=None):
  54. try:
  55. method = self.typeToMethod[query.type]
  56. except KeyError:
  57. self._log.debug(
  58. "Query of unknown type {query.type} for {query.name.name!r}",
  59. query=query,
  60. )
  61. return defer.maybeDeferred(
  62. self._lookup, query.name.name, dns.IN, query.type, timeout
  63. )
  64. else:
  65. return defer.maybeDeferred(method, query.name.name, timeout)
  66. def _lookup(self, name, cls, type, timeout):
  67. return defer.fail(NotImplementedError("ResolverBase._lookup"))
  68. def lookupAddress(self, name, timeout=None):
  69. return self._lookup(dns.domainString(name), dns.IN, dns.A, timeout)
  70. def lookupIPV6Address(self, name, timeout=None):
  71. return self._lookup(dns.domainString(name), dns.IN, dns.AAAA, timeout)
  72. def lookupAddress6(self, name, timeout=None):
  73. return self._lookup(dns.domainString(name), dns.IN, dns.A6, timeout)
  74. def lookupMailExchange(self, name, timeout=None):
  75. return self._lookup(dns.domainString(name), dns.IN, dns.MX, timeout)
  76. def lookupNameservers(self, name, timeout=None):
  77. return self._lookup(dns.domainString(name), dns.IN, dns.NS, timeout)
  78. def lookupCanonicalName(self, name, timeout=None):
  79. return self._lookup(dns.domainString(name), dns.IN, dns.CNAME, timeout)
  80. def lookupMailBox(self, name, timeout=None):
  81. return self._lookup(dns.domainString(name), dns.IN, dns.MB, timeout)
  82. def lookupMailGroup(self, name, timeout=None):
  83. return self._lookup(dns.domainString(name), dns.IN, dns.MG, timeout)
  84. def lookupMailRename(self, name, timeout=None):
  85. return self._lookup(dns.domainString(name), dns.IN, dns.MR, timeout)
  86. def lookupPointer(self, name, timeout=None):
  87. return self._lookup(dns.domainString(name), dns.IN, dns.PTR, timeout)
  88. def lookupAuthority(self, name, timeout=None):
  89. return self._lookup(dns.domainString(name), dns.IN, dns.SOA, timeout)
  90. def lookupNull(self, name, timeout=None):
  91. return self._lookup(dns.domainString(name), dns.IN, dns.NULL, timeout)
  92. def lookupWellKnownServices(self, name, timeout=None):
  93. return self._lookup(dns.domainString(name), dns.IN, dns.WKS, timeout)
  94. def lookupService(self, name, timeout=None):
  95. return self._lookup(dns.domainString(name), dns.IN, dns.SRV, timeout)
  96. def lookupHostInfo(self, name, timeout=None):
  97. return self._lookup(dns.domainString(name), dns.IN, dns.HINFO, timeout)
  98. def lookupMailboxInfo(self, name, timeout=None):
  99. return self._lookup(dns.domainString(name), dns.IN, dns.MINFO, timeout)
  100. def lookupText(self, name, timeout=None):
  101. return self._lookup(dns.domainString(name), dns.IN, dns.TXT, timeout)
  102. def lookupSenderPolicy(self, name, timeout=None):
  103. return self._lookup(dns.domainString(name), dns.IN, dns.SPF, timeout)
  104. def lookupResponsibility(self, name, timeout=None):
  105. return self._lookup(dns.domainString(name), dns.IN, dns.RP, timeout)
  106. def lookupAFSDatabase(self, name, timeout=None):
  107. return self._lookup(dns.domainString(name), dns.IN, dns.AFSDB, timeout)
  108. def lookupZone(self, name, timeout=None):
  109. return self._lookup(dns.domainString(name), dns.IN, dns.AXFR, timeout)
  110. def lookupNamingAuthorityPointer(self, name, timeout=None):
  111. return self._lookup(dns.domainString(name), dns.IN, dns.NAPTR, timeout)
  112. def lookupAllRecords(self, name, timeout=None):
  113. return self._lookup(dns.domainString(name), dns.IN, dns.ALL_RECORDS, timeout)
  114. # IResolverSimple
  115. def getHostByName(self, name, timeout=None, effort=10):
  116. name = dns.domainString(name)
  117. # XXX - respect timeout
  118. # XXX - this should do A and AAAA lookups, not ANY (see RFC 8482).
  119. # https://twistedmatrix.com/trac/ticket/9691
  120. d = self.lookupAllRecords(name, timeout)
  121. d.addCallback(self._cbRecords, name, effort)
  122. return d
  123. def _cbRecords(self, records, name, effort):
  124. (ans, auth, add) = records
  125. result = extractRecord(self, dns.Name(name), ans + auth + add, effort)
  126. if not result:
  127. raise error.DNSLookupError(name)
  128. return result
  129. def extractRecord(resolver, name, answers, level=10):
  130. """
  131. Resolve a name to an IP address, following I{CNAME} records and I{NS}
  132. referrals recursively.
  133. This is an implementation detail of L{ResolverBase.getHostByName}.
  134. @param resolver: The resolver to use for the next query (unless handling
  135. an I{NS} referral).
  136. @type resolver: L{IResolver}
  137. @param name: The name being looked up.
  138. @type name: L{dns.Name}
  139. @param answers: All of the records returned by the previous query (answers,
  140. authority, and additional concatenated).
  141. @type answers: L{list} of L{dns.RRHeader}
  142. @param level: Remaining recursion budget. This is decremented at each
  143. recursion. The query returns L{None} when it reaches 0.
  144. @type level: L{int}
  145. @returns: The first IPv4 or IPv6 address (as a dotted quad or colon
  146. quibbles), or L{None} when no result is found.
  147. @rtype: native L{str} or L{None}
  148. """
  149. if not level:
  150. return None
  151. # FIXME: twisted.python.compat monkeypatches this if missing, so this
  152. # condition is always true. https://twistedmatrix.com/trac/ticket/9753
  153. if hasattr(socket, "inet_ntop"):
  154. for r in answers:
  155. if r.name == name and r.type == dns.A6:
  156. return socket.inet_ntop(socket.AF_INET6, r.payload.address)
  157. for r in answers:
  158. if r.name == name and r.type == dns.AAAA:
  159. return socket.inet_ntop(socket.AF_INET6, r.payload.address)
  160. for r in answers:
  161. if r.name == name and r.type == dns.A:
  162. return socket.inet_ntop(socket.AF_INET, r.payload.address)
  163. for r in answers:
  164. if r.name == name and r.type == dns.CNAME:
  165. result = extractRecord(resolver, r.payload.name, answers, level - 1)
  166. if not result:
  167. return resolver.getHostByName(r.payload.name.name, effort=level - 1)
  168. return result
  169. # No answers, but maybe there's a hint at who we should be asking about
  170. # this
  171. for r in answers:
  172. if r.type != dns.NS:
  173. continue
  174. from twisted.names import client
  175. nsResolver = client.Resolver(
  176. servers=[
  177. (r.payload.name.name.decode("ascii"), dns.PORT),
  178. ]
  179. )
  180. def queryAgain(records):
  181. (ans, auth, add) = records
  182. return extractRecord(nsResolver, name, ans + auth + add, level - 1)
  183. return nsResolver.lookupAddress(name.name).addCallback(queryAgain)
  184. typeToMethod = {
  185. dns.A: "lookupAddress",
  186. dns.AAAA: "lookupIPV6Address",
  187. dns.A6: "lookupAddress6",
  188. dns.NS: "lookupNameservers",
  189. dns.CNAME: "lookupCanonicalName",
  190. dns.SOA: "lookupAuthority",
  191. dns.MB: "lookupMailBox",
  192. dns.MG: "lookupMailGroup",
  193. dns.MR: "lookupMailRename",
  194. dns.NULL: "lookupNull",
  195. dns.WKS: "lookupWellKnownServices",
  196. dns.PTR: "lookupPointer",
  197. dns.HINFO: "lookupHostInfo",
  198. dns.MINFO: "lookupMailboxInfo",
  199. dns.MX: "lookupMailExchange",
  200. dns.TXT: "lookupText",
  201. dns.SPF: "lookupSenderPolicy",
  202. dns.RP: "lookupResponsibility",
  203. dns.AFSDB: "lookupAFSDatabase",
  204. dns.SRV: "lookupService",
  205. dns.NAPTR: "lookupNamingAuthorityPointer",
  206. dns.AXFR: "lookupZone",
  207. dns.ALL_RECORDS: "lookupAllRecords",
  208. }