PageRenderTime 47ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/twisted/internet/udp.py

https://github.com/adaschevici/twisted
Python | 507 lines | 457 code | 14 blank | 36 comment | 6 complexity | 1873f0e9890c9abc47f54bfb0d64cb01 MD5 | raw file
  1. # -*- test-case-name: twisted.test.test_udp -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Various asynchronous UDP classes.
  6. Please do not use this module directly.
  7. @var _sockErrReadIgnore: list of symbolic error constants (from the C{errno}
  8. module) representing socket errors where the error is temporary and can be
  9. ignored.
  10. @var _sockErrReadRefuse: list of symbolic error constants (from the C{errno}
  11. module) representing socket errors that indicate connection refused.
  12. """
  13. from __future__ import division, absolute_import
  14. # System Imports
  15. import socket
  16. import operator
  17. import struct
  18. import warnings
  19. from zope.interface import implementer
  20. from twisted.python.runtime import platformType
  21. if platformType == 'win32':
  22. from errno import WSAEWOULDBLOCK
  23. from errno import WSAEINTR, WSAEMSGSIZE, WSAETIMEDOUT
  24. from errno import WSAECONNREFUSED, WSAECONNRESET, WSAENETRESET
  25. from errno import WSAEINPROGRESS
  26. from errno import WSAENOPROTOOPT as ENOPROTOOPT
  27. # Classify read and write errors
  28. _sockErrReadIgnore = [WSAEINTR, WSAEWOULDBLOCK, WSAEMSGSIZE, WSAEINPROGRESS]
  29. _sockErrReadRefuse = [WSAECONNREFUSED, WSAECONNRESET, WSAENETRESET,
  30. WSAETIMEDOUT]
  31. # POSIX-compatible write errors
  32. EMSGSIZE = WSAEMSGSIZE
  33. ECONNREFUSED = WSAECONNREFUSED
  34. EAGAIN = WSAEWOULDBLOCK
  35. EINTR = WSAEINTR
  36. else:
  37. from errno import EWOULDBLOCK, EINTR, EMSGSIZE, ECONNREFUSED, EAGAIN
  38. from errno import ENOPROTOOPT
  39. _sockErrReadIgnore = [EAGAIN, EINTR, EWOULDBLOCK]
  40. _sockErrReadRefuse = [ECONNREFUSED]
  41. # Twisted Imports
  42. from twisted.internet import base, defer, address
  43. from twisted.python import log, failure
  44. from twisted.internet import abstract, error, interfaces
  45. @implementer(
  46. interfaces.IListeningPort, interfaces.IUDPTransport,
  47. interfaces.ISystemHandle)
  48. class Port(base.BasePort):
  49. """
  50. UDP port, listening for packets.
  51. @ivar maxThroughput: Maximum number of bytes read in one event
  52. loop iteration.
  53. @ivar addressFamily: L{socket.AF_INET} or L{socket.AF_INET6}, depending on
  54. whether this port is listening on an IPv4 address or an IPv6 address.
  55. @ivar _realPortNumber: Actual port number being listened on. The
  56. value will be C{None} until this L{Port} is listening.
  57. @ivar _preexistingSocket: If not C{None}, a L{socket.socket} instance which
  58. was created and initialized outside of the reactor and will be used to
  59. listen for connections (instead of a new socket being created by this
  60. L{Port}).
  61. """
  62. addressFamily = socket.AF_INET
  63. socketType = socket.SOCK_DGRAM
  64. maxThroughput = 256 * 1024
  65. _realPortNumber = None
  66. _preexistingSocket = None
  67. def __init__(self, port, proto, interface='', maxPacketSize=8192, reactor=None):
  68. """
  69. @param port: A port number on which to listen.
  70. @type port: C{int}
  71. @param proto: A C{DatagramProtocol} instance which will be
  72. connected to the given C{port}.
  73. @type proto: L{twisted.internet.protocol.DatagramProtocol}
  74. @param interface: The local IPv4 or IPv6 address to which to bind;
  75. defaults to '', ie all IPv4 addresses.
  76. @type interface: C{str}
  77. @param maxPacketSize: The maximum packet size to accept.
  78. @type maxPacketSize: C{int}
  79. @param reactor: A reactor which will notify this C{Port} when
  80. its socket is ready for reading or writing. Defaults to
  81. C{None}, ie the default global reactor.
  82. @type reactor: L{interfaces.IReactorFDSet}
  83. """
  84. base.BasePort.__init__(self, reactor)
  85. self.port = port
  86. self.protocol = proto
  87. self.maxPacketSize = maxPacketSize
  88. self.interface = interface
  89. self.setLogStr()
  90. self._connectedAddr = None
  91. self._setAddressFamily()
  92. @classmethod
  93. def _fromListeningDescriptor(cls, reactor, fd, addressFamily, protocol,
  94. maxPacketSize):
  95. """
  96. Create a new L{Port} based on an existing listening
  97. I{SOCK_DGRAM} socket.
  98. @param reactor: A reactor which will notify this L{Port} when
  99. its socket is ready for reading or writing. Defaults to
  100. C{None}, ie the default global reactor.
  101. @type reactor: L{interfaces.IReactorFDSet}
  102. @param fd: An integer file descriptor associated with a listening
  103. socket. The socket must be in non-blocking mode. Any additional
  104. attributes desired, such as I{FD_CLOEXEC}, must also be set already.
  105. @type fd: C{int}
  106. @param addressFamily: The address family (sometimes called I{domain}) of
  107. the existing socket. For example, L{socket.AF_INET}.
  108. @param addressFamily: C{int}
  109. @param protocol: A C{DatagramProtocol} instance which will be
  110. connected to the C{port}.
  111. @type proto: L{twisted.internet.protocol.DatagramProtocol}
  112. @param maxPacketSize: The maximum packet size to accept.
  113. @type maxPacketSize: C{int}
  114. @return: A new instance of C{cls} wrapping the socket given by C{fd}.
  115. @rtype: L{Port}
  116. """
  117. port = socket.fromfd(fd, addressFamily, cls.socketType)
  118. interface = port.getsockname()[0]
  119. self = cls(None, protocol, interface=interface, reactor=reactor,
  120. maxPacketSize=maxPacketSize)
  121. self._preexistingSocket = port
  122. return self
  123. def __repr__(self):
  124. if self._realPortNumber is not None:
  125. return "<%s on %s>" % (self.protocol.__class__, self._realPortNumber)
  126. else:
  127. return "<%s not connected>" % (self.protocol.__class__,)
  128. def getHandle(self):
  129. """
  130. Return a socket object.
  131. """
  132. return self.socket
  133. def startListening(self):
  134. """
  135. Create and bind my socket, and begin listening on it.
  136. This is called on unserialization, and must be called after creating a
  137. server to begin listening on the specified port.
  138. """
  139. self._bindSocket()
  140. self._connectToProtocol()
  141. def _bindSocket(self):
  142. """
  143. Prepare and assign a L{socket.socket} instance to
  144. C{self.socket}.
  145. Either creates a new SOCK_DGRAM L{socket.socket} bound to
  146. C{self.interface} and C{self.port} or takes an existing
  147. L{socket.socket} provided via the
  148. L{interfaces.IReactorSocket.adoptDatagramPort} interface.
  149. """
  150. if self._preexistingSocket is None:
  151. # Create a new socket and make it listen
  152. try:
  153. skt = self.createInternetSocket()
  154. skt.bind((self.interface, self.port))
  155. except socket.error as le:
  156. raise error.CannotListenError(self.interface, self.port, le)
  157. else:
  158. # Re-use the externally specified socket
  159. skt = self._preexistingSocket
  160. self._preexistingSocket = None
  161. # Make sure that if we listened on port 0, we update that to
  162. # reflect what the OS actually assigned us.
  163. self._realPortNumber = skt.getsockname()[1]
  164. log.msg("%s starting on %s" % (
  165. self._getLogPrefix(self.protocol), self._realPortNumber))
  166. self.connected = 1
  167. self.socket = skt
  168. self.fileno = self.socket.fileno
  169. def _connectToProtocol(self):
  170. self.protocol.makeConnection(self)
  171. self.startReading()
  172. def doRead(self):
  173. """
  174. Called when my socket is ready for reading.
  175. """
  176. read = 0
  177. while read < self.maxThroughput:
  178. try:
  179. data, addr = self.socket.recvfrom(self.maxPacketSize)
  180. except socket.error as se:
  181. no = se.args[0]
  182. if no in _sockErrReadIgnore:
  183. return
  184. if no in _sockErrReadRefuse:
  185. if self._connectedAddr:
  186. self.protocol.connectionRefused()
  187. return
  188. raise
  189. else:
  190. read += len(data)
  191. if self.addressFamily == socket.AF_INET6:
  192. # Remove the flow and scope ID from the address tuple,
  193. # reducing it to a tuple of just (host, port).
  194. #
  195. # TODO: This should be amended to return an object that can
  196. # unpack to (host, port) but also includes the flow info
  197. # and scope ID. See http://tm.tl/6826
  198. addr = addr[:2]
  199. try:
  200. self.protocol.datagramReceived(data, addr)
  201. except:
  202. log.err()
  203. def write(self, datagram, addr=None):
  204. """
  205. Write a datagram.
  206. @type datagram: C{str}
  207. @param datagram: The datagram to be sent.
  208. @type addr: C{tuple} containing C{str} as first element and C{int} as
  209. second element, or C{None}
  210. @param addr: A tuple of (I{stringified IPv4 or IPv6 address},
  211. I{integer port number}); can be C{None} in connected mode.
  212. """
  213. if self._connectedAddr:
  214. assert addr in (None, self._connectedAddr)
  215. try:
  216. return self.socket.send(datagram)
  217. except socket.error as se:
  218. no = se.args[0]
  219. if no == EINTR:
  220. return self.write(datagram)
  221. elif no == EMSGSIZE:
  222. raise error.MessageLengthError("message too long")
  223. elif no == ECONNREFUSED:
  224. self.protocol.connectionRefused()
  225. else:
  226. raise
  227. else:
  228. assert addr != None
  229. if (not abstract.isIPAddress(addr[0])
  230. and not abstract.isIPv6Address(addr[0])
  231. and addr[0] != "<broadcast>"):
  232. raise error.InvalidAddressError(
  233. addr[0],
  234. "write() only accepts IP addresses, not hostnames")
  235. if ((abstract.isIPAddress(addr[0]) or addr[0] == "<broadcast>")
  236. and self.addressFamily == socket.AF_INET6):
  237. raise error.InvalidAddressError(
  238. addr[0],
  239. "IPv6 port write() called with IPv4 or broadcast address")
  240. if (abstract.isIPv6Address(addr[0])
  241. and self.addressFamily == socket.AF_INET):
  242. raise error.InvalidAddressError(
  243. addr[0], "IPv4 port write() called with IPv6 address")
  244. try:
  245. return self.socket.sendto(datagram, addr)
  246. except socket.error as se:
  247. no = se.args[0]
  248. if no == EINTR:
  249. return self.write(datagram, addr)
  250. elif no == EMSGSIZE:
  251. raise error.MessageLengthError("message too long")
  252. elif no == ECONNREFUSED:
  253. # in non-connected UDP ECONNREFUSED is platform dependent, I
  254. # think and the info is not necessarily useful. Nevertheless
  255. # maybe we should call connectionRefused? XXX
  256. return
  257. else:
  258. raise
  259. def writeSequence(self, seq, addr):
  260. self.write("".join(seq), addr)
  261. def connect(self, host, port):
  262. """
  263. 'Connect' to remote server.
  264. """
  265. if self._connectedAddr:
  266. raise RuntimeError("already connected, reconnecting is not currently supported")
  267. if not abstract.isIPAddress(host) and not abstract.isIPv6Address(host):
  268. raise error.InvalidAddressError(
  269. host, 'not an IPv4 or IPv6 address.')
  270. self._connectedAddr = (host, port)
  271. self.socket.connect((host, port))
  272. def _loseConnection(self):
  273. self.stopReading()
  274. if self.connected: # actually means if we are *listening*
  275. self.reactor.callLater(0, self.connectionLost)
  276. def stopListening(self):
  277. if self.connected:
  278. result = self.d = defer.Deferred()
  279. else:
  280. result = None
  281. self._loseConnection()
  282. return result
  283. def loseConnection(self):
  284. warnings.warn("Please use stopListening() to disconnect port", DeprecationWarning, stacklevel=2)
  285. self.stopListening()
  286. def connectionLost(self, reason=None):
  287. """
  288. Cleans up my socket.
  289. """
  290. log.msg('(UDP Port %s Closed)' % self._realPortNumber)
  291. self._realPortNumber = None
  292. base.BasePort.connectionLost(self, reason)
  293. self.protocol.doStop()
  294. self.socket.close()
  295. del self.socket
  296. del self.fileno
  297. if hasattr(self, "d"):
  298. self.d.callback(None)
  299. del self.d
  300. def setLogStr(self):
  301. """
  302. Initialize the C{logstr} attribute to be used by C{logPrefix}.
  303. """
  304. logPrefix = self._getLogPrefix(self.protocol)
  305. self.logstr = "%s (UDP)" % logPrefix
  306. def _setAddressFamily(self):
  307. """
  308. Resolve address family for the socket.
  309. """
  310. if abstract.isIPv6Address(self.interface):
  311. self.addressFamily = socket.AF_INET6
  312. elif abstract.isIPAddress(self.interface):
  313. self.addressFamily = socket.AF_INET
  314. elif self.interface:
  315. raise error.InvalidAddressError(
  316. self.interface, 'not an IPv4 or IPv6 address.')
  317. def logPrefix(self):
  318. """
  319. Return the prefix to log with.
  320. """
  321. return self.logstr
  322. def getHost(self):
  323. """
  324. Return the local address of the UDP connection
  325. @returns: the local address of the UDP connection
  326. @rtype: L{IPv4Address} or L{IPv6Address}
  327. """
  328. addr = self.socket.getsockname()
  329. if self.addressFamily == socket.AF_INET:
  330. return address.IPv4Address('UDP', *addr)
  331. elif self.addressFamily == socket.AF_INET6:
  332. return address.IPv6Address('UDP', *(addr[:2]))
  333. def setBroadcastAllowed(self, enabled):
  334. """
  335. Set whether this port may broadcast. This is disabled by default.
  336. @param enabled: Whether the port may broadcast.
  337. @type enabled: L{bool}
  338. """
  339. self.socket.setsockopt(
  340. socket.SOL_SOCKET, socket.SO_BROADCAST, enabled)
  341. def getBroadcastAllowed(self):
  342. """
  343. Checks if broadcast is currently allowed on this port.
  344. @return: Whether this port may broadcast.
  345. @rtype: L{bool}
  346. """
  347. return operator.truth(
  348. self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST))
  349. class MulticastMixin:
  350. """
  351. Implement multicast functionality.
  352. """
  353. def getOutgoingInterface(self):
  354. i = self.socket.getsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF)
  355. return socket.inet_ntoa(struct.pack("@i", i))
  356. def setOutgoingInterface(self, addr):
  357. """Returns Deferred of success."""
  358. return self.reactor.resolve(addr).addCallback(self._setInterface)
  359. def _setInterface(self, addr):
  360. i = socket.inet_aton(addr)
  361. self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF, i)
  362. return 1
  363. def getLoopbackMode(self):
  364. return self.socket.getsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP)
  365. def setLoopbackMode(self, mode):
  366. mode = struct.pack("b", operator.truth(mode))
  367. self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, mode)
  368. def getTTL(self):
  369. return self.socket.getsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL)
  370. def setTTL(self, ttl):
  371. ttl = struct.pack("B", ttl)
  372. self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)
  373. def joinGroup(self, addr, interface=""):
  374. """Join a multicast group. Returns Deferred of success."""
  375. return self.reactor.resolve(addr).addCallback(self._joinAddr1, interface, 1)
  376. def _joinAddr1(self, addr, interface, join):
  377. return self.reactor.resolve(interface).addCallback(self._joinAddr2, addr, join)
  378. def _joinAddr2(self, interface, addr, join):
  379. addr = socket.inet_aton(addr)
  380. interface = socket.inet_aton(interface)
  381. if join:
  382. cmd = socket.IP_ADD_MEMBERSHIP
  383. else:
  384. cmd = socket.IP_DROP_MEMBERSHIP
  385. try:
  386. self.socket.setsockopt(socket.IPPROTO_IP, cmd, addr + interface)
  387. except socket.error as e:
  388. return failure.Failure(error.MulticastJoinError(addr, interface, *e.args))
  389. def leaveGroup(self, addr, interface=""):
  390. """Leave multicast group, return Deferred of success."""
  391. return self.reactor.resolve(addr).addCallback(self._joinAddr1, interface, 0)
  392. @implementer(interfaces.IMulticastTransport)
  393. class MulticastPort(MulticastMixin, Port):
  394. """
  395. UDP Port that supports multicasting.
  396. """
  397. def __init__(self, port, proto, interface='', maxPacketSize=8192,
  398. reactor=None, listenMultiple=False):
  399. """
  400. @see: L{twisted.internet.interfaces.IReactorMulticast.listenMulticast}
  401. """
  402. Port.__init__(self, port, proto, interface, maxPacketSize, reactor)
  403. self.listenMultiple = listenMultiple
  404. def createInternetSocket(self):
  405. skt = Port.createInternetSocket(self)
  406. if self.listenMultiple:
  407. skt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  408. if hasattr(socket, "SO_REUSEPORT"):
  409. try:
  410. skt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
  411. except socket.error as le:
  412. # RHEL6 defines SO_REUSEPORT but it doesn't work
  413. if le.errno == ENOPROTOOPT:
  414. pass
  415. else:
  416. raise
  417. return skt