PageRenderTime 9ms CodeModel.GetById 4ms app.highlight 60ms RepoModel.GetById 1ms app.codeStats 1ms

/twisted/internet/tcp.py

https://github.com/adaschevici/twisted
Python | 1180 lines | 830 code | 89 blank | 261 comment | 52 complexity | 07e2cbed341bd06d3793ad4d766edf4c MD5 | raw file
   1# -*- test-case-name: twisted.test.test_tcp -*-
   2# Copyright (c) Twisted Matrix Laboratories.
   3# See LICENSE for details.
   4
   5"""
   6Various asynchronous TCP/IP classes.
   7
   8End users shouldn't use this module directly - use the reactor APIs instead.
   9"""
  10
  11from __future__ import division, absolute_import
  12
  13# System Imports
  14import types
  15import socket
  16import sys
  17import operator
  18import struct
  19
  20from zope.interface import implementer
  21
  22from twisted.python.compat import _PY3, lazyByteSlice
  23from twisted.python.runtime import platformType
  24from twisted.python import versions, deprecate
  25
  26try:
  27    # Try to get the memory BIO based startTLS implementation, available since
  28    # pyOpenSSL 0.10
  29    from twisted.internet._newtls import (
  30        ConnectionMixin as _TLSConnectionMixin,
  31        ClientMixin as _TLSClientMixin,
  32        ServerMixin as _TLSServerMixin)
  33except ImportError:
  34    # There is no version of startTLS available
  35    class _TLSConnectionMixin(object):
  36        TLS = False
  37
  38
  39    class _TLSClientMixin(object):
  40        pass
  41
  42
  43    class _TLSServerMixin(object):
  44        pass
  45
  46
  47if platformType == 'win32':
  48    # no such thing as WSAEPERM or error code 10001 according to winsock.h or MSDN
  49    EPERM = object()
  50    from errno import WSAEINVAL as EINVAL
  51    from errno import WSAEWOULDBLOCK as EWOULDBLOCK
  52    from errno import WSAEINPROGRESS as EINPROGRESS
  53    from errno import WSAEALREADY as EALREADY
  54    from errno import WSAECONNRESET as ECONNRESET
  55    from errno import WSAEISCONN as EISCONN
  56    from errno import WSAENOTCONN as ENOTCONN
  57    from errno import WSAEINTR as EINTR
  58    from errno import WSAENOBUFS as ENOBUFS
  59    from errno import WSAEMFILE as EMFILE
  60    # No such thing as WSAENFILE, either.
  61    ENFILE = object()
  62    # Nor ENOMEM
  63    ENOMEM = object()
  64    EAGAIN = EWOULDBLOCK
  65    from errno import WSAECONNRESET as ECONNABORTED
  66
  67    from twisted.python.win32 import formatError as strerror
  68else:
  69    from errno import EPERM
  70    from errno import EINVAL
  71    from errno import EWOULDBLOCK
  72    from errno import EINPROGRESS
  73    from errno import EALREADY
  74    from errno import ECONNRESET
  75    from errno import EISCONN
  76    from errno import ENOTCONN
  77    from errno import EINTR
  78    from errno import ENOBUFS
  79    from errno import EMFILE
  80    from errno import ENFILE
  81    from errno import ENOMEM
  82    from errno import EAGAIN
  83    from errno import ECONNABORTED
  84
  85    from os import strerror
  86
  87
  88from errno import errorcode
  89
  90# Twisted Imports
  91from twisted.internet import base, address, fdesc
  92from twisted.internet.task import deferLater
  93from twisted.python import log, failure, reflect
  94from twisted.python.util import untilConcludes
  95from twisted.internet.error import CannotListenError
  96from twisted.internet import abstract, main, interfaces, error
  97
  98# Not all platforms have, or support, this flag.
  99_AI_NUMERICSERV = getattr(socket, "AI_NUMERICSERV", 0)
 100
 101
 102# The type for service names passed to socket.getservbyname:
 103if _PY3:
 104    _portNameType = str
 105else:
 106    _portNameType = types.StringTypes
 107
 108
 109
 110class _SocketCloser(object):
 111    """
 112    @ivar _shouldShutdown: Set to C{True} if C{shutdown} should be called
 113        before callling C{close} on the underlying socket.
 114    @type _shouldShutdown: C{bool}
 115    """
 116    _shouldShutdown = True
 117
 118    def _closeSocket(self, orderly):
 119        # The call to shutdown() before close() isn't really necessary, because
 120        # we set FD_CLOEXEC now, which will ensure this is the only process
 121        # holding the FD, thus ensuring close() really will shutdown the TCP
 122        # socket. However, do it anyways, just to be safe.
 123        skt = self.socket
 124        try:
 125            if orderly:
 126                if self._shouldShutdown:
 127                    skt.shutdown(2)
 128            else:
 129                # Set SO_LINGER to 1,0 which, by convention, causes a
 130                # connection reset to be sent when close is called,
 131                # instead of the standard FIN shutdown sequence.
 132                self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,
 133                                       struct.pack("ii", 1, 0))
 134
 135        except socket.error:
 136            pass
 137        try:
 138            skt.close()
 139        except socket.error:
 140            pass
 141
 142
 143
 144class _AbortingMixin(object):
 145    """
 146    Common implementation of C{abortConnection}.
 147
 148    @ivar _aborting: Set to C{True} when C{abortConnection} is called.
 149    @type _aborting: C{bool}
 150    """
 151    _aborting = False
 152
 153    def abortConnection(self):
 154        """
 155        Aborts the connection immediately, dropping any buffered data.
 156
 157        @since: 11.1
 158        """
 159        if self.disconnected or self._aborting:
 160            return
 161        self._aborting = True
 162        self.stopReading()
 163        self.stopWriting()
 164        self.doRead = lambda *args, **kwargs: None
 165        self.doWrite = lambda *args, **kwargs: None
 166        self.reactor.callLater(0, self.connectionLost,
 167                               failure.Failure(error.ConnectionAborted()))
 168
 169
 170
 171@implementer(interfaces.ITCPTransport, interfaces.ISystemHandle)
 172class Connection(_TLSConnectionMixin, abstract.FileDescriptor, _SocketCloser,
 173                 _AbortingMixin):
 174    """
 175    Superclass of all socket-based FileDescriptors.
 176
 177    This is an abstract superclass of all objects which represent a TCP/IP
 178    connection based socket.
 179
 180    @ivar logstr: prefix used when logging events related to this connection.
 181    @type logstr: C{str}
 182    """
 183
 184
 185    def __init__(self, skt, protocol, reactor=None):
 186        abstract.FileDescriptor.__init__(self, reactor=reactor)
 187        self.socket = skt
 188        self.socket.setblocking(0)
 189        self.fileno = skt.fileno
 190        self.protocol = protocol
 191
 192
 193    def getHandle(self):
 194        """Return the socket for this connection."""
 195        return self.socket
 196
 197
 198    def doRead(self):
 199        """Calls self.protocol.dataReceived with all available data.
 200
 201        This reads up to self.bufferSize bytes of data from its socket, then
 202        calls self.dataReceived(data) to process it.  If the connection is not
 203        lost through an error in the physical recv(), this function will return
 204        the result of the dataReceived call.
 205        """
 206        try:
 207            data = self.socket.recv(self.bufferSize)
 208        except socket.error as se:
 209            if se.args[0] == EWOULDBLOCK:
 210                return
 211            else:
 212                return main.CONNECTION_LOST
 213
 214        return self._dataReceived(data)
 215
 216
 217    def _dataReceived(self, data):
 218        if not data:
 219            return main.CONNECTION_DONE
 220        rval = self.protocol.dataReceived(data)
 221        if rval is not None:
 222            offender = self.protocol.dataReceived
 223            warningFormat = (
 224                'Returning a value other than None from %(fqpn)s is '
 225                'deprecated since %(version)s.')
 226            warningString = deprecate.getDeprecationWarningString(
 227                offender, versions.Version('Twisted', 11, 0, 0),
 228                format=warningFormat)
 229            deprecate.warnAboutFunction(offender, warningString)
 230        return rval
 231
 232
 233    def writeSomeData(self, data):
 234        """
 235        Write as much as possible of the given data to this TCP connection.
 236
 237        This sends up to C{self.SEND_LIMIT} bytes from C{data}.  If the
 238        connection is lost, an exception is returned.  Otherwise, the number
 239        of bytes successfully written is returned.
 240        """
 241        # Limit length of buffer to try to send, because some OSes are too
 242        # stupid to do so themselves (ahem windows)
 243        limitedData = lazyByteSlice(data, 0, self.SEND_LIMIT)
 244
 245        try:
 246            return untilConcludes(self.socket.send, limitedData)
 247        except socket.error as se:
 248            if se.args[0] in (EWOULDBLOCK, ENOBUFS):
 249                return 0
 250            else:
 251                return main.CONNECTION_LOST
 252
 253
 254    def _closeWriteConnection(self):
 255        try:
 256            self.socket.shutdown(1)
 257        except socket.error:
 258            pass
 259        p = interfaces.IHalfCloseableProtocol(self.protocol, None)
 260        if p:
 261            try:
 262                p.writeConnectionLost()
 263            except:
 264                f = failure.Failure()
 265                log.err()
 266                self.connectionLost(f)
 267
 268
 269    def readConnectionLost(self, reason):
 270        p = interfaces.IHalfCloseableProtocol(self.protocol, None)
 271        if p:
 272            try:
 273                p.readConnectionLost()
 274            except:
 275                log.err()
 276                self.connectionLost(failure.Failure())
 277        else:
 278            self.connectionLost(reason)
 279
 280
 281
 282    def connectionLost(self, reason):
 283        """See abstract.FileDescriptor.connectionLost().
 284        """
 285        # Make sure we're not called twice, which can happen e.g. if
 286        # abortConnection() is called from protocol's dataReceived and then
 287        # code immediately after throws an exception that reaches the
 288        # reactor. We can't rely on "disconnected" attribute for this check
 289        # since twisted.internet._oldtls does evil things to it:
 290        if not hasattr(self, "socket"):
 291            return
 292        abstract.FileDescriptor.connectionLost(self, reason)
 293        self._closeSocket(not reason.check(error.ConnectionAborted))
 294        protocol = self.protocol
 295        del self.protocol
 296        del self.socket
 297        del self.fileno
 298        protocol.connectionLost(reason)
 299
 300
 301    logstr = "Uninitialized"
 302
 303    def logPrefix(self):
 304        """Return the prefix to log with when I own the logging thread.
 305        """
 306        return self.logstr
 307
 308    def getTcpNoDelay(self):
 309        return operator.truth(self.socket.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY))
 310
 311    def setTcpNoDelay(self, enabled):
 312        self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, enabled)
 313
 314    def getTcpKeepAlive(self):
 315        return operator.truth(self.socket.getsockopt(socket.SOL_SOCKET,
 316                                                     socket.SO_KEEPALIVE))
 317
 318    def setTcpKeepAlive(self, enabled):
 319        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, enabled)
 320
 321
 322
 323
 324class _BaseBaseClient(object):
 325    """
 326    Code shared with other (non-POSIX) reactors for management of general
 327    outgoing connections.
 328
 329    Requirements upon subclasses are documented as instance variables rather
 330    than abstract methods, in order to avoid MRO confusion, since this base is
 331    mixed in to unfortunately weird and distinctive multiple-inheritance
 332    hierarchies and many of these attributes are provided by peer classes
 333    rather than descendant classes in those hierarchies.
 334
 335    @ivar addressFamily: The address family constant (C{socket.AF_INET},
 336        C{socket.AF_INET6}, C{socket.AF_UNIX}) of the underlying socket of this
 337        client connection.
 338    @type addressFamily: C{int}
 339
 340    @ivar socketType: The socket type constant (C{socket.SOCK_STREAM} or
 341        C{socket.SOCK_DGRAM}) of the underlying socket.
 342    @type socketType: C{int}
 343
 344    @ivar _requiresResolution: A flag indicating whether the address of this
 345        client will require name resolution.  C{True} if the hostname of said
 346        address indicates a name that must be resolved by hostname lookup,
 347        C{False} if it indicates an IP address literal.
 348    @type _requiresResolution: C{bool}
 349
 350    @cvar _commonConnection: Subclasses must provide this attribute, which
 351        indicates the L{Connection}-alike class to invoke C{__init__} and
 352        C{connectionLost} on.
 353    @type _commonConnection: C{type}
 354
 355    @ivar _stopReadingAndWriting: Subclasses must implement in order to remove
 356        this transport from its reactor's notifications in response to a
 357        terminated connection attempt.
 358    @type _stopReadingAndWriting: 0-argument callable returning C{None}
 359
 360    @ivar _closeSocket: Subclasses must implement in order to close the socket
 361        in response to a terminated connection attempt.
 362    @type _closeSocket: 1-argument callable; see L{_SocketCloser._closeSocket}
 363
 364    @ivar _collectSocketDetails: Clean up references to the attached socket in
 365        its underlying OS resource (such as a file descriptor or file handle),
 366        as part of post connection-failure cleanup.
 367    @type _collectSocketDetails: 0-argument callable returning C{None}.
 368
 369    @ivar reactor: The class pointed to by C{_commonConnection} should set this
 370        attribute in its constructor.
 371    @type reactor: L{twisted.internet.interfaces.IReactorTime},
 372        L{twisted.internet.interfaces.IReactorCore},
 373        L{twisted.internet.interfaces.IReactorFDSet}
 374    """
 375
 376    addressFamily = socket.AF_INET
 377    socketType = socket.SOCK_STREAM
 378
 379    def _finishInit(self, whenDone, skt, error, reactor):
 380        """
 381        Called by subclasses to continue to the stage of initialization where
 382        the socket connect attempt is made.
 383
 384        @param whenDone: A 0-argument callable to invoke once the connection is
 385            set up.  This is C{None} if the connection could not be prepared
 386            due to a previous error.
 387
 388        @param skt: The socket object to use to perform the connection.
 389        @type skt: C{socket._socketobject}
 390
 391        @param error: The error to fail the connection with.
 392
 393        @param reactor: The reactor to use for this client.
 394        @type reactor: L{twisted.internet.interfaces.IReactorTime}
 395        """
 396        if whenDone:
 397            self._commonConnection.__init__(self, skt, None, reactor)
 398            reactor.callLater(0, whenDone)
 399        else:
 400            reactor.callLater(0, self.failIfNotConnected, error)
 401
 402
 403    def resolveAddress(self):
 404        """
 405        Resolve the name that was passed to this L{_BaseBaseClient}, if
 406        necessary, and then move on to attempting the connection once an
 407        address has been determined.  (The connection will be attempted
 408        immediately within this function if either name resolution can be
 409        synchronous or the address was an IP address literal.)
 410
 411        @note: You don't want to call this method from outside, as it won't do
 412            anything useful; it's just part of the connection bootstrapping
 413            process.  Also, although this method is on L{_BaseBaseClient} for
 414            historical reasons, it's not used anywhere except for L{Client}
 415            itself.
 416
 417        @return: C{None}
 418        """
 419        if self._requiresResolution:
 420            d = self.reactor.resolve(self.addr[0])
 421            d.addCallback(lambda n: (n,) + self.addr[1:])
 422            d.addCallbacks(self._setRealAddress, self.failIfNotConnected)
 423        else:
 424            self._setRealAddress(self.addr)
 425
 426
 427    def _setRealAddress(self, address):
 428        """
 429        Set the resolved address of this L{_BaseBaseClient} and initiate the
 430        connection attempt.
 431
 432        @param address: Depending on whether this is an IPv4 or IPv6 connection
 433            attempt, a 2-tuple of C{(host, port)} or a 4-tuple of C{(host,
 434            port, flow, scope)}.  At this point it is a fully resolved address,
 435            and the 'host' portion will always be an IP address, not a DNS
 436            name.
 437        """
 438        self.realAddress = address
 439        self.doConnect()
 440
 441
 442    def failIfNotConnected(self, err):
 443        """
 444        Generic method called when the attemps to connect failed. It basically
 445        cleans everything it can: call connectionFailed, stop read and write,
 446        delete socket related members.
 447        """
 448        if (self.connected or self.disconnected or
 449            not hasattr(self, "connector")):
 450            return
 451
 452        self._stopReadingAndWriting()
 453        try:
 454            self._closeSocket(True)
 455        except AttributeError:
 456            pass
 457        else:
 458            self._collectSocketDetails()
 459        self.connector.connectionFailed(failure.Failure(err))
 460        del self.connector
 461
 462
 463    def stopConnecting(self):
 464        """
 465        If a connection attempt is still outstanding (i.e.  no connection is
 466        yet established), immediately stop attempting to connect.
 467        """
 468        self.failIfNotConnected(error.UserError())
 469
 470
 471    def connectionLost(self, reason):
 472        """
 473        Invoked by lower-level logic when it's time to clean the socket up.
 474        Depending on the state of the connection, either inform the attached
 475        L{Connector} that the connection attempt has failed, or inform the
 476        connected L{IProtocol} that the established connection has been lost.
 477
 478        @param reason: the reason that the connection was terminated
 479        @type reason: L{Failure}
 480        """
 481        if not self.connected:
 482            self.failIfNotConnected(error.ConnectError(string=reason))
 483        else:
 484            self._commonConnection.connectionLost(self, reason)
 485            self.connector.connectionLost(reason)
 486
 487
 488
 489class BaseClient(_BaseBaseClient, _TLSClientMixin, Connection):
 490    """
 491    A base class for client TCP (and similiar) sockets.
 492
 493    @ivar realAddress: The address object that will be used for socket.connect;
 494        this address is an address tuple (the number of elements dependent upon
 495        the address family) which does not contain any names which need to be
 496        resolved.
 497    @type realAddress: C{tuple}
 498
 499    @ivar _base: L{Connection}, which is the base class of this class which has
 500        all of the useful file descriptor methods.  This is used by
 501        L{_TLSServerMixin} to call the right methods to directly manipulate the
 502        transport, as is necessary for writing TLS-encrypted bytes (whereas
 503        those methods on L{Server} will go through another layer of TLS if it
 504        has been enabled).
 505    """
 506
 507    _base = Connection
 508    _commonConnection = Connection
 509
 510    def _stopReadingAndWriting(self):
 511        """
 512        Implement the POSIX-ish (i.e.
 513        L{twisted.internet.interfaces.IReactorFDSet}) method of detaching this
 514        socket from the reactor for L{_BaseBaseClient}.
 515        """
 516        if hasattr(self, "reactor"):
 517            # this doesn't happen if we failed in __init__
 518            self.stopReading()
 519            self.stopWriting()
 520
 521
 522    def _collectSocketDetails(self):
 523        """
 524        Clean up references to the socket and its file descriptor.
 525
 526        @see: L{_BaseBaseClient}
 527        """
 528        del self.socket, self.fileno
 529
 530
 531    def createInternetSocket(self):
 532        """(internal) Create a non-blocking socket using
 533        self.addressFamily, self.socketType.
 534        """
 535        s = socket.socket(self.addressFamily, self.socketType)
 536        s.setblocking(0)
 537        fdesc._setCloseOnExec(s.fileno())
 538        return s
 539
 540
 541    def doConnect(self):
 542        """
 543        Initiate the outgoing connection attempt.
 544
 545        @note: Applications do not need to call this method; it will be invoked
 546            internally as part of L{IReactorTCP.connectTCP}.
 547        """
 548        self.doWrite = self.doConnect
 549        self.doRead = self.doConnect
 550        if not hasattr(self, "connector"):
 551            # this happens when connection failed but doConnect
 552            # was scheduled via a callLater in self._finishInit
 553            return
 554
 555        err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
 556        if err:
 557            self.failIfNotConnected(error.getConnectError((err, strerror(err))))
 558            return
 559
 560        # doConnect gets called twice.  The first time we actually need to
 561        # start the connection attempt.  The second time we don't really
 562        # want to (SO_ERROR above will have taken care of any errors, and if
 563        # it reported none, the mere fact that doConnect was called again is
 564        # sufficient to indicate that the connection has succeeded), but it
 565        # is not /particularly/ detrimental to do so.  This should get
 566        # cleaned up some day, though.
 567        try:
 568            connectResult = self.socket.connect_ex(self.realAddress)
 569        except socket.error as se:
 570            connectResult = se.args[0]
 571        if connectResult:
 572            if connectResult == EISCONN:
 573                pass
 574            # on Windows EINVAL means sometimes that we should keep trying:
 575            # http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/connect_2.asp
 576            elif ((connectResult in (EWOULDBLOCK, EINPROGRESS, EALREADY)) or
 577                  (connectResult == EINVAL and platformType == "win32")):
 578                self.startReading()
 579                self.startWriting()
 580                return
 581            else:
 582                self.failIfNotConnected(error.getConnectError((connectResult, strerror(connectResult))))
 583                return
 584
 585        # If I have reached this point without raising or returning, that means
 586        # that the socket is connected.
 587        del self.doWrite
 588        del self.doRead
 589        # we first stop and then start, to reset any references to the old doRead
 590        self.stopReading()
 591        self.stopWriting()
 592        self._connectDone()
 593
 594
 595    def _connectDone(self):
 596        """
 597        This is a hook for when a connection attempt has succeeded.
 598
 599        Here, we build the protocol from the
 600        L{twisted.internet.protocol.ClientFactory} that was passed in, compute
 601        a log string, begin reading so as to send traffic to the newly built
 602        protocol, and finally hook up the protocol itself.
 603
 604        This hook is overridden by L{ssl.Client} to initiate the TLS protocol.
 605        """
 606        self.protocol = self.connector.buildProtocol(self.getPeer())
 607        self.connected = 1
 608        logPrefix = self._getLogPrefix(self.protocol)
 609        self.logstr = "%s,client" % logPrefix
 610        self.startReading()
 611        self.protocol.makeConnection(self)
 612
 613
 614
 615_NUMERIC_ONLY = socket.AI_NUMERICHOST | _AI_NUMERICSERV
 616
 617def _resolveIPv6(ip, port):
 618    """
 619    Resolve an IPv6 literal into an IPv6 address.
 620
 621    This is necessary to resolve any embedded scope identifiers to the relevant
 622    C{sin6_scope_id} for use with C{socket.connect()}, C{socket.listen()}, or
 623    C{socket.bind()}; see U{RFC 3493 <https://tools.ietf.org/html/rfc3493>} for
 624    more information.
 625
 626    @param ip: An IPv6 address literal.
 627    @type ip: C{str}
 628
 629    @param port: A port number.
 630    @type port: C{int}
 631
 632    @return: a 4-tuple of C{(host, port, flow, scope)}, suitable for use as an
 633        IPv6 address.
 634
 635    @raise socket.gaierror: if either the IP or port is not numeric as it
 636        should be.
 637    """
 638    return socket.getaddrinfo(ip, port, 0, 0, 0, _NUMERIC_ONLY)[0][4]
 639
 640
 641
 642class _BaseTCPClient(object):
 643    """
 644    Code shared with other (non-POSIX) reactors for management of outgoing TCP
 645    connections (both TCPv4 and TCPv6).
 646
 647    @note: In order to be functional, this class must be mixed into the same
 648        hierarchy as L{_BaseBaseClient}.  It would subclass L{_BaseBaseClient}
 649        directly, but the class hierarchy here is divided in strange ways out
 650        of the need to share code along multiple axes; specifically, with the
 651        IOCP reactor and also with UNIX clients in other reactors.
 652
 653    @ivar _addressType: The Twisted _IPAddress implementation for this client
 654    @type _addressType: L{IPv4Address} or L{IPv6Address}
 655
 656    @ivar connector: The L{Connector} which is driving this L{_BaseTCPClient}'s
 657        connection attempt.
 658
 659    @ivar addr: The address that this socket will be connecting to.
 660    @type addr: If IPv4, a 2-C{tuple} of C{(str host, int port)}.  If IPv6, a
 661        4-C{tuple} of (C{str host, int port, int ignored, int scope}).
 662
 663    @ivar createInternetSocket: Subclasses must implement this as a method to
 664        create a python socket object of the appropriate address family and
 665        socket type.
 666    @type createInternetSocket: 0-argument callable returning
 667        C{socket._socketobject}.
 668    """
 669
 670    _addressType = address.IPv4Address
 671
 672    def __init__(self, host, port, bindAddress, connector, reactor=None):
 673        # BaseClient.__init__ is invoked later
 674        self.connector = connector
 675        self.addr = (host, port)
 676
 677        whenDone = self.resolveAddress
 678        err = None
 679        skt = None
 680
 681        if abstract.isIPAddress(host):
 682            self._requiresResolution = False
 683        elif abstract.isIPv6Address(host):
 684            self._requiresResolution = False
 685            self.addr = _resolveIPv6(host, port)
 686            self.addressFamily = socket.AF_INET6
 687            self._addressType = address.IPv6Address
 688        else:
 689            self._requiresResolution = True
 690        try:
 691            skt = self.createInternetSocket()
 692        except socket.error as se:
 693            err = error.ConnectBindError(se.args[0], se.args[1])
 694            whenDone = None
 695        if whenDone and bindAddress is not None:
 696            try:
 697                if abstract.isIPv6Address(bindAddress[0]):
 698                    bindinfo = _resolveIPv6(*bindAddress)
 699                else:
 700                    bindinfo = bindAddress
 701                skt.bind(bindinfo)
 702            except socket.error as se:
 703                err = error.ConnectBindError(se.args[0], se.args[1])
 704                whenDone = None
 705        self._finishInit(whenDone, skt, err, reactor)
 706
 707
 708    def getHost(self):
 709        """
 710        Returns an L{IPv4Address} or L{IPv6Address}.
 711
 712        This indicates the address from which I am connecting.
 713        """
 714        return self._addressType('TCP', *self.socket.getsockname()[:2])
 715
 716
 717    def getPeer(self):
 718        """
 719        Returns an L{IPv4Address} or L{IPv6Address}.
 720
 721        This indicates the address that I am connected to.
 722        """
 723        # an ipv6 realAddress has more than two elements, but the IPv6Address
 724        # constructor still only takes two.
 725        return self._addressType('TCP', *self.realAddress[:2])
 726
 727
 728    def __repr__(self):
 729        s = '<%s to %s at %x>' % (self.__class__, self.addr, id(self))
 730        return s
 731
 732
 733
 734class Client(_BaseTCPClient, BaseClient):
 735    """
 736    A transport for a TCP protocol; either TCPv4 or TCPv6.
 737
 738    Do not create these directly; use L{IReactorTCP.connectTCP}.
 739    """
 740
 741
 742
 743class Server(_TLSServerMixin, Connection):
 744    """
 745    Serverside socket-stream connection class.
 746
 747    This is a serverside network connection transport; a socket which came from
 748    an accept() on a server.
 749
 750    @ivar _base: L{Connection}, which is the base class of this class which has
 751        all of the useful file descriptor methods.  This is used by
 752        L{_TLSServerMixin} to call the right methods to directly manipulate the
 753        transport, as is necessary for writing TLS-encrypted bytes (whereas
 754        those methods on L{Server} will go through another layer of TLS if it
 755        has been enabled).
 756    """
 757    _base = Connection
 758
 759    _addressType = address.IPv4Address
 760
 761    def __init__(self, sock, protocol, client, server, sessionno, reactor):
 762        """
 763        Server(sock, protocol, client, server, sessionno)
 764
 765        Initialize it with a socket, a protocol, a descriptor for my peer (a
 766        tuple of host, port describing the other end of the connection), an
 767        instance of Port, and a session number.
 768        """
 769        Connection.__init__(self, sock, protocol, reactor)
 770        if len(client) != 2:
 771            self._addressType = address.IPv6Address
 772        self.server = server
 773        self.client = client
 774        self.sessionno = sessionno
 775        self.hostname = client[0]
 776
 777        logPrefix = self._getLogPrefix(self.protocol)
 778        self.logstr = "%s,%s,%s" % (logPrefix,
 779                                    sessionno,
 780                                    self.hostname)
 781        if self.server is not None:
 782            self.repstr = "<%s #%s on %s>" % (self.protocol.__class__.__name__,
 783                                              self.sessionno,
 784                                              self.server._realPortNumber)
 785        self.startReading()
 786        self.connected = 1
 787
 788    def __repr__(self):
 789        """
 790        A string representation of this connection.
 791        """
 792        return self.repstr
 793
 794
 795    @classmethod
 796    def _fromConnectedSocket(cls, fileDescriptor, addressFamily, factory,
 797                             reactor):
 798        """
 799        Create a new L{Server} based on an existing connected I{SOCK_STREAM}
 800        socket.
 801
 802        Arguments are the same as to L{Server.__init__}, except where noted.
 803
 804        @param fileDescriptor: An integer file descriptor associated with a
 805            connected socket.  The socket must be in non-blocking mode.  Any
 806            additional attributes desired, such as I{FD_CLOEXEC}, must also be
 807            set already.
 808
 809        @param addressFamily: The address family (sometimes called I{domain})
 810            of the existing socket.  For example, L{socket.AF_INET}.
 811
 812        @return: A new instance of C{cls} wrapping the socket given by
 813            C{fileDescriptor}.
 814        """
 815        addressType = address.IPv4Address
 816        if addressFamily == socket.AF_INET6:
 817            addressType = address.IPv6Address
 818        skt = socket.fromfd(fileDescriptor, addressFamily, socket.SOCK_STREAM)
 819        addr = skt.getpeername()
 820        protocolAddr = addressType('TCP', addr[0], addr[1])
 821        localPort = skt.getsockname()[1]
 822
 823        protocol = factory.buildProtocol(protocolAddr)
 824        if protocol is None:
 825            skt.close()
 826            return
 827
 828        self = cls(skt, protocol, addr, None, addr[1], reactor)
 829        self.repstr = "<%s #%s on %s>" % (
 830            self.protocol.__class__.__name__, self.sessionno, localPort)
 831        protocol.makeConnection(self)
 832        return self
 833
 834
 835    def getHost(self):
 836        """
 837        Returns an L{IPv4Address} or L{IPv6Address}.
 838
 839        This indicates the server's address.
 840        """
 841        host, port = self.socket.getsockname()[:2]
 842        return self._addressType('TCP', host, port)
 843
 844
 845    def getPeer(self):
 846        """
 847        Returns an L{IPv4Address} or L{IPv6Address}.
 848
 849        This indicates the client's address.
 850        """
 851        return self._addressType('TCP', *self.client[:2])
 852
 853
 854
 855@implementer(interfaces.IListeningPort)
 856class Port(base.BasePort, _SocketCloser):
 857    """
 858    A TCP server port, listening for connections.
 859
 860    When a connection is accepted, this will call a factory's buildProtocol
 861    with the incoming address as an argument, according to the specification
 862    described in L{twisted.internet.interfaces.IProtocolFactory}.
 863
 864    If you wish to change the sort of transport that will be used, the
 865    C{transport} attribute will be called with the signature expected for
 866    C{Server.__init__}, so it can be replaced.
 867
 868    @ivar deferred: a deferred created when L{stopListening} is called, and
 869        that will fire when connection is lost. This is not to be used it
 870        directly: prefer the deferred returned by L{stopListening} instead.
 871    @type deferred: L{defer.Deferred}
 872
 873    @ivar disconnecting: flag indicating that the L{stopListening} method has
 874        been called and that no connections should be accepted anymore.
 875    @type disconnecting: C{bool}
 876
 877    @ivar connected: flag set once the listen has successfully been called on
 878        the socket.
 879    @type connected: C{bool}
 880
 881    @ivar _type: A string describing the connections which will be created by
 882        this port.  Normally this is C{"TCP"}, since this is a TCP port, but
 883        when the TLS implementation re-uses this class it overrides the value
 884        with C{"TLS"}.  Only used for logging.
 885
 886    @ivar _preexistingSocket: If not C{None}, a L{socket.socket} instance which
 887        was created and initialized outside of the reactor and will be used to
 888        listen for connections (instead of a new socket being created by this
 889        L{Port}).
 890    """
 891
 892    socketType = socket.SOCK_STREAM
 893
 894    transport = Server
 895    sessionno = 0
 896    interface = ''
 897    backlog = 50
 898
 899    _type = 'TCP'
 900
 901    # Actual port number being listened on, only set to a non-None
 902    # value when we are actually listening.
 903    _realPortNumber = None
 904
 905    # An externally initialized socket that we will use, rather than creating
 906    # our own.
 907    _preexistingSocket = None
 908
 909    addressFamily = socket.AF_INET
 910    _addressType = address.IPv4Address
 911
 912    def __init__(self, port, factory, backlog=50, interface='', reactor=None):
 913        """Initialize with a numeric port to listen on.
 914        """
 915        base.BasePort.__init__(self, reactor=reactor)
 916        self.port = port
 917        self.factory = factory
 918        self.backlog = backlog
 919        if abstract.isIPv6Address(interface):
 920            self.addressFamily = socket.AF_INET6
 921            self._addressType = address.IPv6Address
 922        self.interface = interface
 923
 924
 925    @classmethod
 926    def _fromListeningDescriptor(cls, reactor, fd, addressFamily, factory):
 927        """
 928        Create a new L{Port} based on an existing listening I{SOCK_STREAM}
 929        socket.
 930
 931        Arguments are the same as to L{Port.__init__}, except where noted.
 932
 933        @param fd: An integer file descriptor associated with a listening
 934            socket.  The socket must be in non-blocking mode.  Any additional
 935            attributes desired, such as I{FD_CLOEXEC}, must also be set already.
 936
 937        @param addressFamily: The address family (sometimes called I{domain}) of
 938            the existing socket.  For example, L{socket.AF_INET}.
 939
 940        @return: A new instance of C{cls} wrapping the socket given by C{fd}.
 941        """
 942        port = socket.fromfd(fd, addressFamily, cls.socketType)
 943        interface = port.getsockname()[0]
 944        self = cls(None, factory, None, interface, reactor)
 945        self._preexistingSocket = port
 946        return self
 947
 948
 949    def __repr__(self):
 950        if self._realPortNumber is not None:
 951            return "<%s of %s on %s>" % (self.__class__,
 952                self.factory.__class__, self._realPortNumber)
 953        else:
 954            return "<%s of %s (not listening)>" % (self.__class__, self.factory.__class__)
 955
 956    def createInternetSocket(self):
 957        s = base.BasePort.createInternetSocket(self)
 958        if platformType == "posix" and sys.platform != "cygwin":
 959            s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
 960        return s
 961
 962
 963    def startListening(self):
 964        """Create and bind my socket, and begin listening on it.
 965
 966        This is called on unserialization, and must be called after creating a
 967        server to begin listening on the specified port.
 968        """
 969        if self._preexistingSocket is None:
 970            # Create a new socket and make it listen
 971            try:
 972                skt = self.createInternetSocket()
 973                if self.addressFamily == socket.AF_INET6:
 974                    addr = _resolveIPv6(self.interface, self.port)
 975                else:
 976                    addr = (self.interface, self.port)
 977                skt.bind(addr)
 978            except socket.error as le:
 979                raise CannotListenError(self.interface, self.port, le)
 980            skt.listen(self.backlog)
 981        else:
 982            # Re-use the externally specified socket
 983            skt = self._preexistingSocket
 984            self._preexistingSocket = None
 985            # Avoid shutting it down at the end.
 986            self._shouldShutdown = False
 987
 988        # Make sure that if we listened on port 0, we update that to
 989        # reflect what the OS actually assigned us.
 990        self._realPortNumber = skt.getsockname()[1]
 991
 992        log.msg("%s starting on %s" % (
 993                self._getLogPrefix(self.factory), self._realPortNumber))
 994
 995        # The order of the next 5 lines is kind of bizarre.  If no one
 996        # can explain it, perhaps we should re-arrange them.
 997        self.factory.doStart()
 998        self.connected = True
 999        self.socket = skt
1000        self.fileno = self.socket.fileno
1001        self.numberAccepts = 100
1002
1003        self.startReading()
1004
1005
1006    def _buildAddr(self, address):
1007        host, port = address[:2]
1008        return self._addressType('TCP', host, port)
1009
1010
1011    def doRead(self):
1012        """Called when my socket is ready for reading.
1013
1014        This accepts a connection and calls self.protocol() to handle the
1015        wire-level protocol.
1016        """
1017        try:
1018            if platformType == "posix":
1019                numAccepts = self.numberAccepts
1020            else:
1021                # win32 event loop breaks if we do more than one accept()
1022                # in an iteration of the event loop.
1023                numAccepts = 1
1024            for i in range(numAccepts):
1025                # we need this so we can deal with a factory's buildProtocol
1026                # calling our loseConnection
1027                if self.disconnecting:
1028                    return
1029                try:
1030                    skt, addr = self.socket.accept()
1031                except socket.error as e:
1032                    if e.args[0] in (EWOULDBLOCK, EAGAIN):
1033                        self.numberAccepts = i
1034                        break
1035                    elif e.args[0] == EPERM:
1036                        # Netfilter on Linux may have rejected the
1037                        # connection, but we get told to try to accept()
1038                        # anyway.
1039                        continue
1040                    elif e.args[0] in (EMFILE, ENOBUFS, ENFILE, ENOMEM, ECONNABORTED):
1041
1042                        # Linux gives EMFILE when a process is not allowed
1043                        # to allocate any more file descriptors.  *BSD and
1044                        # Win32 give (WSA)ENOBUFS.  Linux can also give
1045                        # ENFILE if the system is out of inodes, or ENOMEM
1046                        # if there is insufficient memory to allocate a new
1047                        # dentry.  ECONNABORTED is documented as possible on
1048                        # both Linux and Windows, but it is not clear
1049                        # whether there are actually any circumstances under
1050                        # which it can happen (one might expect it to be
1051                        # possible if a client sends a FIN or RST after the
1052                        # server sends a SYN|ACK but before application code
1053                        # calls accept(2), however at least on Linux this
1054                        # _seems_ to be short-circuited by syncookies.
1055
1056                        log.msg("Could not accept new connection (%s)" % (
1057                            errorcode[e.args[0]],))
1058                        break
1059                    raise
1060
1061                fdesc._setCloseOnExec(skt.fileno())
1062                protocol = self.factory.buildProtocol(self._buildAddr(addr))
1063                if protocol is None:
1064                    skt.close()
1065                    continue
1066                s = self.sessionno
1067                self.sessionno = s+1
1068                transport = self.transport(skt, protocol, addr, self, s, self.reactor)
1069                protocol.makeConnection(transport)
1070            else:
1071                self.numberAccepts = self.numberAccepts+20
1072        except:
1073            # Note that in TLS mode, this will possibly catch SSL.Errors
1074            # raised by self.socket.accept()
1075            #
1076            # There is no "except SSL.Error:" above because SSL may be
1077            # None if there is no SSL support.  In any case, all the
1078            # "except SSL.Error:" suite would probably do is log.deferr()
1079            # and return, so handling it here works just as well.
1080            log.deferr()
1081
1082    def loseConnection(self, connDone=failure.Failure(main.CONNECTION_DONE)):
1083        """
1084        Stop accepting connections on this port.
1085
1086        This will shut down the socket and call self.connectionLost().  It
1087        returns a deferred which will fire successfully when the port is
1088        actually closed, or with a failure if an error occurs shutting down.
1089        """
1090        self.disconnecting = True
1091        self.stopReading()
1092        if self.connected:
1093            self.deferred = deferLater(
1094                self.reactor, 0, self.connectionLost, connDone)
1095            return self.deferred
1096
1097    stopListening = loseConnection
1098
1099    def _logConnectionLostMsg(self):
1100        """
1101        Log message for closing port
1102        """
1103        log.msg('(%s Port %s Closed)' % (self._type, self._realPortNumber))
1104
1105
1106    def connectionLost(self, reason):
1107        """
1108        Cleans up the socket.
1109        """
1110        self._logConnectionLostMsg()
1111        self._realPortNumber = None
1112
1113        base.BasePort.connectionLost(self, reason)
1114        self.connected = False
1115        self._closeSocket(True)
1116        del self.socket
1117        del self.fileno
1118
1119        try:
1120            self.factory.doStop()
1121        finally:
1122            self.disconnecting = False
1123
1124
1125    def logPrefix(self):
1126        """Returns the name of my class, to prefix log entries with.
1127        """
1128        return reflect.qual(self.factory.__class__)
1129
1130
1131    def getHost(self):
1132        """
1133        Return an L{IPv4Address} or L{IPv6Address} indicating the listening
1134        address of this port.
1135        """
1136        host, port = self.socket.getsockname()[:2]
1137        return self._addressType('TCP', host, port)
1138
1139
1140
1141class Connector(base.BaseConnector):
1142    """
1143    A L{Connector} provides of L{twisted.internet.interfaces.IConnector} for
1144    all POSIX-style reactors.
1145
1146    @ivar _addressType: the type returned by L{Connector.getDestination}.
1147        Either L{IPv4Address} or L{IPv6Address}, depending on the type of
1148        address.
1149    @type _addressType: C{type}
1150    """
1151    _addressType = address.IPv4Address
1152
1153    def __init__(self, host, port, factory, timeout, bindAddress, reactor=None):
1154        if isinstance(port, _portNameType):
1155            try:
1156                port = socket.getservbyname(port, 'tcp')
1157            except socket.error as e:
1158                raise error.ServiceNameUnknownError(string="%s (%r)" % (e, port))
1159        self.host, self.port = host, port
1160        if abstract.isIPv6Address(host):
1161            self._addressType = address.IPv6Address
1162        self.bindAddress = bindAddress
1163        base.BaseConnector.__init__(self, factory, timeout, reactor)
1164
1165
1166    def _makeTransport(self):
1167        """
1168        Create a L{Client} bound to this L{Connector}.
1169
1170        @return: a new L{Client}
1171        @rtype: L{Client}
1172        """
1173        return Client(self.host, self.port, self.bindAddress, self, self.reactor)
1174
1175
1176    def getDestination(self):
1177        """
1178        @see: L{twisted.internet.interfaces.IConnector.getDestination}.
1179        """
1180        return self._addressType('TCP', self.host, self.port)