Example #1
0
    def _getnameinfo(self, sockaddr, flags):
        if not isinstance(flags, int):
            raise TypeError('an integer is required')
        if not isinstance(sockaddr, tuple):
            raise TypeError('getnameinfo() argument 1 must be a tuple')

        waiter = Waiter(self.hub)
        result = self._getaddrinfo(sockaddr[0], str(sockaddr[1]), family=AF_UNSPEC, socktype=SOCK_DGRAM)
        if not result:
            raise
        elif len(result) != 1:
            raise error('sockaddr resolved to multiple addresses')
        family, socktype, proto, name, address = result[0]

        if family == AF_INET:
            if len(sockaddr) != 2:
                raise error("IPv4 sockaddr must be 2 tuple")
        elif family == AF_INET6:
            address = address[:2] + sockaddr[2:]

        self.ares.getnameinfo(waiter, address, flags)
        node, service = waiter.get()
        if service is None:
            service = '0'
        return node, service
 def _lookup_port(self, port, socktype):
     if isinstance(port, basestring):
         try:
             port = int(port)
         except ValueError:
             try:
                 if socktype == 0:
                     try:
                         port = getservbyname(port, 'tcp')
                         socktype = SOCK_STREAM
                     except error:
                         port = getservbyname(port, 'udp')
                         socktype = SOCK_DGRAM
                 elif socktype == SOCK_STREAM:
                     port = getservbyname(port, 'tcp')
                 elif socktype == SOCK_DGRAM:
                     port = getservbyname(port, 'udp')
                 else:
                     raise gaierror(EAI_SERVICE, 'Servname not supported for ai_socktype')
             except error:
                 ex = sys.exc_info()[1]
                 if 'not found' in str(ex):
                     raise gaierror(EAI_SERVICE, 'Servname not supported for ai_socktype')
                 else:
                     raise gaierror(str(ex))
             except UnicodeEncodeError:
                 raise error('Int or String expected')
     elif port is None:
         port = 0
     elif isinstance(port, int):
         pass
     else:
         raise error('Int or String expected')
     port = int(port % 65536)
     return port, socktype
Example #3
0
    def __getnameinfo(self, hostname, port, sockaddr, flags):
        result = self.__getaddrinfo(hostname,
                                    port,
                                    family=AF_UNSPEC,
                                    socktype=SOCK_DGRAM,
                                    proto=0,
                                    flags=0,
                                    fill_in_type_proto=False)
        if len(result) != 1:
            raise error('sockaddr resolved to multiple addresses')

        family, _socktype, _proto, _name, address = result[0]

        if family == AF_INET:
            if len(sockaddr) != 2:
                raise error("IPv4 sockaddr must be 2 tuple")
        elif family == AF_INET6:
            address = address[:2] + sockaddr[2:]

        waiter = Waiter(self.hub)
        self.cares.getnameinfo(waiter, address, flags)
        node, service = waiter.get()

        if service is None and PY3:
            # ares docs: "If the query did not complete
            # successfully, or one of the values was not
            # requested, node or service will be NULL ". Python 2
            # allows that for the service, but Python 3 raises
            # an error. This is tested by test_socket in py 3.4
            err = gaierror(EAI_NONAME, self.EAI_NONAME_MSG)
            err.errno = EAI_NONAME
            raise err

        return node, service or '0'
Example #4
0
    def _getnameinfo(self, sockaddr, flags):
        if not isinstance(flags, int):
            raise TypeError('an integer is required')
        if not isinstance(sockaddr, tuple):
            raise TypeError('getnameinfo() argument 1 must be a tuple')

        waiter = Waiter(self.hub)
        result = self._getaddrinfo(sockaddr[0],
                                   str(sockaddr[1]),
                                   family=AF_UNSPEC,
                                   socktype=SOCK_DGRAM)
        if not result:
            raise
        elif len(result) != 1:
            raise error('sockaddr resolved to multiple addresses')
        family, socktype, proto, name, address = result[0]

        if family == AF_INET:
            if len(sockaddr) != 2:
                raise error("IPv4 sockaddr must be 2 tuple")
        elif family == AF_INET6:
            address = address[:2] + sockaddr[2:]

        self.ares.getnameinfo(waiter, address, flags)
        node, service = waiter.get()
        if service is None:
            service = '0'
        return node, service
Example #5
0
 def _lookup_port(self, port, socktype):
     if isinstance(port, string_types):
         try:
             port = int(port)
         except ValueError:
             try:
                 if socktype == 0:
                     try:
                         port = getservbyname(port, 'tcp')
                         socktype = SOCK_STREAM
                     except error:
                         port = getservbyname(port, 'udp')
                         socktype = SOCK_DGRAM
                 elif socktype == SOCK_STREAM:
                     port = getservbyname(port, 'tcp')
                 elif socktype == SOCK_DGRAM:
                     port = getservbyname(port, 'udp')
                 else:
                     raise gaierror(EAI_SERVICE, 'Servname not supported for ai_socktype')
             except error:
                 ex = sys.exc_info()[1]
                 if 'not found' in str(ex):
                     raise gaierror(EAI_SERVICE, 'Servname not supported for ai_socktype')
                 else:
                     raise gaierror(str(ex))
             except UnicodeEncodeError:
                 raise error('Int or String expected')
     elif port is None:
         port = 0
     elif isinstance(port, int):
         pass
     else:
         raise error('Int or String expected')
     port = int(port % 65536)
     return port, socktype
Example #6
0
    def send(self, data, flags=0):
        if not self.connected:
            # The socket was never connected.
            if not self.was_connected:
                raise error(10057, "Socket is not connected")

            # The socket has been closed already.
            raise error(stdsocket.EBADF, 'Bad file descriptor')

        self.send_buffer.extend(data)
        stackless.schedule()
        return len(data)
Example #7
0
 def connect(self, addr, timeout):
     errno = self._sock.connect_ex(addr)
     if EALREADY == errno or EINPROGRESS == errno or \
        EISCONN  == errno or EWOULDBLOCK == errno:
         id_ = c_uint(id(self)).value
         if id_ not in objects:
             raise error(EPIPE, 'Broken pipe')
         self.x_wait(timeout)
         errno = self._sock.connect_ex(addr)
     if 0 == errno or EISCONN == errno:
         return
     raise error(errno, strerror(errno))
Example #8
0
    def _getnameinfo(self, sockaddr, flags):
        if not isinstance(flags, int):
            raise TypeError('an integer is required')
        if not isinstance(sockaddr, tuple):
            raise TypeError('getnameinfo() argument 1 must be a tuple')

        address = sockaddr[0]
        if not PY3 and isinstance(address, text_type):
            address = address.encode('ascii')

        if not isinstance(address, string_types):
            raise TypeError('sockaddr[0] must be a string, not %s' %
                            type(address).__name__)

        port = sockaddr[1]
        if not isinstance(port, int):
            raise TypeError('port must be an integer, not %s' % type(port))

        waiter = Waiter(self.hub)
        result = self._getaddrinfo(address,
                                   str(sockaddr[1]),
                                   family=AF_UNSPEC,
                                   socktype=SOCK_DGRAM)
        if not result:
            reraise(*sys.exc_info())
        elif len(result) != 1:
            raise error('sockaddr resolved to multiple addresses')
        family, _socktype, _proto, _name, address = result[0]

        if family == AF_INET:
            if len(sockaddr) != 2:
                raise error("IPv4 sockaddr must be 2 tuple")
        elif family == AF_INET6:
            address = address[:2] + sockaddr[2:]

        self.ares.getnameinfo(waiter, address, flags)
        node, service = waiter.get()

        if service is None:
            if PY3:
                # ares docs: "If the query did not complete
                # successfully, or one of the values was not
                # requested, node or service will be NULL ". Python 2
                # allows that for the service, but Python 3 raises
                # an error. This is tested by test_socket in py 3.4
                err = gaierror('nodename nor servname provided, or not known')
                err.errno = 8
                raise err
            service = '0'
        return node, service
Example #9
0
 def _lookup_port(self, port, socktype):
     # pylint:disable=too-many-branches
     socktypes = []
     if isinstance(port, string_types):
         try:
             port = int(port)
         except ValueError:
             try:
                 if socktype == 0:
                     origport = port
                     try:
                         port = getservbyname(port, 'tcp')
                         socktypes.append(SOCK_STREAM)
                     except error:
                         port = getservbyname(port, 'udp')
                         socktypes.append(SOCK_DGRAM)
                     else:
                         try:
                             if port == getservbyname(origport, 'udp'):
                                 socktypes.append(SOCK_DGRAM)
                         except error:
                             pass
                 elif socktype == SOCK_STREAM:
                     port = getservbyname(port, 'tcp')
                 elif socktype == SOCK_DGRAM:
                     port = getservbyname(port, 'udp')
                 else:
                     raise gaierror(
                         EAI_SERVICE,
                         'Servname not supported for ai_socktype')
             except error as ex:
                 if 'not found' in str(ex):
                     raise gaierror(
                         EAI_SERVICE,
                         'Servname not supported for ai_socktype')
                 else:
                     raise gaierror(str(ex))
             except UnicodeEncodeError:
                 raise error('Int or String expected')
     elif port is None:
         port = 0
     elif isinstance(port, integer_types):
         pass
     else:
         raise error('Int or String expected', port, type(port))
     port = int(port % 65536)
     if not socktypes and socktype:
         socktypes.append(socktype)
     return port, socktypes
Example #10
0
def _resolve_special(hostname, family):
    if hostname == '':
        result = getaddrinfo(None, 0, family, SOCK_DGRAM, 0, AI_PASSIVE)
        if len(result) != 1:
            raise error('wildcard resolved to multiple address')
        return result[0][4][0]
    return hostname
Example #11
0
def _resolve_special(hostname, family):
    if hostname == '':
        result = getaddrinfo(None, 0, family, SOCK_DGRAM, 0, AI_PASSIVE)
        if len(result) != 1:
            raise error('wildcard resolved to multiple address')
        return result[0][4][0]
    return hostname
Example #12
0
    def sendall(self, data, flags=0):
        if not self.connected:
            # The socket was never connected.
            if not self.was_connected:
                raise error(10057, "Socket is not connected")

            # The socket has been closed already.
            raise error(stdsocket.EBADF, 'Bad file descriptor')

        # WARNING: this will busy wait until all data is sent
        # It should be possible to do away with the busy wait with
        # the use of a channel.
        self.send_buffer.extend(data)
        while self.send_buffer:
            stackless.schedule()
        return len(data)
Example #13
0
    def _getnameinfo(self, sockaddr, flags):
        if not isinstance(flags, int):
            raise TypeError("an integer is required")
        if not isinstance(sockaddr, tuple):
            raise TypeError("getnameinfo() argument 1 must be a tuple")

        address = sockaddr[0]
        if not PY3 and isinstance(address, text_type):
            address = address.encode("ascii")

        if not isinstance(address, string_types):
            raise TypeError("sockaddr[0] must be a string, not %s" % type(address).__name__)

        port = sockaddr[1]
        if not isinstance(port, int):
            raise TypeError("port must be an integer, not %s" % type(port))

        waiter = Waiter(self.hub)
        result = self._getaddrinfo(address, str(sockaddr[1]), family=AF_UNSPEC, socktype=SOCK_DGRAM)
        if not result:
            reraise(*sys.exc_info())
        elif len(result) != 1:
            raise error("sockaddr resolved to multiple addresses")
        family, _socktype, _proto, _name, address = result[0]

        if family == AF_INET:
            if len(sockaddr) != 2:
                raise error("IPv4 sockaddr must be 2 tuple")
        elif family == AF_INET6:
            address = address[:2] + sockaddr[2:]

        self.ares.getnameinfo(waiter, address, flags)
        node, service = waiter.get()

        if service is None:
            if PY3:
                # ares docs: "If the query did not complete
                # successfully, or one of the values was not
                # requested, node or service will be NULL ". Python 2
                # allows that for the service, but Python 3 raises
                # an error. This is tested by test_socket in py 3.4
                err = gaierror("nodename nor servname provided, or not known")
                err.errno = 8
                raise err
            service = "0"
        return node, service
Example #14
0
 def _lookup_port(self, port, socktype):
     # pylint:disable=too-many-branches
     socktypes = []
     if isinstance(port, string_types):
         try:
             port = int(port)
         except ValueError:
             try:
                 if socktype == 0:
                     origport = port
                     try:
                         port = getservbyname(port, "tcp")
                         socktypes.append(SOCK_STREAM)
                     except error:
                         port = getservbyname(port, "udp")
                         socktypes.append(SOCK_DGRAM)
                     else:
                         try:
                             if port == getservbyname(origport, "udp"):
                                 socktypes.append(SOCK_DGRAM)
                         except error:
                             pass
                 elif socktype == SOCK_STREAM:
                     port = getservbyname(port, "tcp")
                 elif socktype == SOCK_DGRAM:
                     port = getservbyname(port, "udp")
                 else:
                     raise gaierror(EAI_SERVICE, "Servname not supported for ai_socktype")
             except error as ex:
                 if "not found" in str(ex):
                     raise gaierror(EAI_SERVICE, "Servname not supported for ai_socktype")
                 else:
                     raise gaierror(str(ex))
             except UnicodeEncodeError:
                 raise error("Int or String expected")
     elif port is None:
         port = 0
     elif isinstance(port, integer_types):
         pass
     else:
         raise error("Int or String expected", port, type(port))
     port = int(port % 65536)
     if not socktypes and socktype:
         socktypes.append(socktype)
     return port, socktypes
Example #15
0
 def _lookup_port(self, port, socktype):
     socktypes = []
     if isinstance(port, string_types):
         try:
             port = int(port)
         except ValueError:
             try:
                 if socktype == 0:
                     origport = port
                     try:
                         port = getservbyname(port, 'tcp')
                         socktypes.append(SOCK_STREAM)
                     except error:
                         port = getservbyname(port, 'udp')
                         socktypes.append(SOCK_DGRAM)
                     else:
                         try:
                             if port == getservbyname(origport, 'udp'):
                                 socktypes.append(SOCK_DGRAM)
                         except error:
                             pass
                 elif socktype == SOCK_STREAM:
                     port = getservbyname(port, 'tcp')
                 elif socktype == SOCK_DGRAM:
                     port = getservbyname(port, 'udp')
                 else:
                     raise gaierror(EAI_SERVICE, 'Servname not supported for ai_socktype')
             except error as ex:
                 if 'not found' in str(ex):
                     raise gaierror(EAI_SERVICE, 'Servname not supported for ai_socktype')
                 else:
                     raise gaierror(str(ex))
             except UnicodeEncodeError:
                 raise error('Int or String expected')
     elif port is None:
         port = 0
     elif isinstance(port, int):
         pass
     else:
         raise error('Int or String expected')
     port = int(port % 65536)
     if not socktypes and socktype:
         socktypes.append(socktype)
     return port, socktypes
def _resolve_special(hostname, family):
    if not isinstance(hostname, hostname_types):
        raise TypeError("argument 1 must be str, bytes or bytearray, not %s" % (type(hostname),))

    if hostname in (u'', b''):
        result = native_getaddrinfo(None, 0, family, SOCK_DGRAM, 0, AI_PASSIVE)
        if len(result) != 1:
            raise error('wildcard resolved to multiple address')
        return result[0][4][0]
    return hostname
Example #17
0
def _resolve_special(hostname, family):
    if not isinstance(hostname, hostname_types):
        raise TypeError("argument 1 must be str, bytes or bytearray, not %s" % (type(hostname),))

    if hostname == '':
        result = getaddrinfo(None, 0, family, SOCK_DGRAM, 0, AI_PASSIVE)
        if len(result) != 1:
            raise error('wildcard resolved to multiple address')
        return result[0][4][0]
    return hostname
Example #18
0
    def _getnameinfo(self, sockaddr, flags):
        if not isinstance(flags, int):
            raise TypeError('an integer is required')
        if not isinstance(sockaddr, tuple):
            raise TypeError('getnameinfo() argument 1 must be a tuple')

        address = sockaddr[0]
        if not PY3 and isinstance(address, text_type):
            address = address.encode('ascii')

        if not isinstance(address, string_types):
            raise TypeError('sockaddr[0] must be a string, not %s' %
                            type(address).__name__)

        port = sockaddr[1]
        if not isinstance(port, int):
            raise TypeError('port must be an integer, not %s' % type(port))

        waiter = Waiter(self.hub)
        result = self._getaddrinfo(address,
                                   str(sockaddr[1]),
                                   family=AF_UNSPEC,
                                   socktype=SOCK_DGRAM)
        if not result:
            reraise(*sys.exc_info())
        elif len(result) != 1:
            raise error('sockaddr resolved to multiple addresses')
        family, socktype, proto, name, address = result[0]

        if family == AF_INET:
            if len(sockaddr) != 2:
                raise error("IPv4 sockaddr must be 2 tuple")
        elif family == AF_INET6:
            address = address[:2] + sockaddr[2:]

        self.ares.getnameinfo(waiter, address, flags)
        node, service = waiter.get()
        if service is None:
            service = '0'
        return node, service
Example #19
0
    def recv_into(self, buffer, nbytes=0, flags=0):
        logger.debug("recv_into: buflen: %s, nbytes: %s" %
                     (len(buffer), nbytes))

        # recv() must not concatenate two or more data fragments sent with
        # send() on the remote side. Single fragment sent with single send()
        # call should be split into strings of length less than or equal
        # to 'byteCount', and returned by one or more recv() calls.

        remaining_bytes = max(0, len(self.read_bytes) - self.read_index)
        logger.debug("read_index: %s, read_bytes: %s, remaining: %s" %
                     (self.read_index, len(self.read_bytes), remaining_bytes))

        # TODO: Verify this connectivity behaviour.
        # Sockets which have never been connected do this.
        if not self.connected and not self.was_connected:
            raise error(10057, 'Socket is not connected')

        # Sockets which were connected, but no longer are, use
        # up the remaining input.  Observed this with urllib.urlopen
        # where it closes the socket and then allows the caller to
        # use a file to access the body of the web page.
        if not remaining_bytes:
            timeout = self.gettimeout()
            logger.debug("timeout: %s", timeout)
            if timeout == 0:
                if self.recv_channel.balance <= 0:
                    raise BlockingIOError(errno.EWOULDBLOCK, "Would block")

            self.read_bytes = self.recv_channel.receive()
            self.read_index = 0
            remaining_bytes = len(self.read_bytes)

        if remaining_bytes == 0:
            logger.debug("returning 0")
            return 0

        if nbytes == 0:
            nbytes = remaining_bytes

        if len(buffer):
            nbytes = min(len(buffer), nbytes)

        nbytes = min(remaining_bytes, nbytes)

        idx = self.read_index + nbytes
        buffer[:nbytes] = self.read_bytes[self.read_index:idx]
        self.read_index = idx
        logger.debug("returning %s" % nbytes)
        return nbytes
Example #20
0
    def _getnameinfo(self, sockaddr, flags):
        if not isinstance(flags, int):
            raise TypeError("an integer is required")
        if not isinstance(sockaddr, tuple):
            raise TypeError("getnameinfo() argument 1 must be a tuple")

        address = sockaddr[0]
        if not PY3 and isinstance(address, text_type):
            address = address.encode("ascii")

        if not isinstance(address, string_types):
            raise TypeError("sockaddr[0] must be a string, not %s" % type(address).__name__)

        port = sockaddr[1]
        if not isinstance(port, int):
            raise TypeError("port must be an integer, not %s" % type(port))

        waiter = Waiter(self.hub)
        result = self._getaddrinfo(address, str(sockaddr[1]), family=AF_UNSPEC, socktype=SOCK_DGRAM)
        if not result:
            reraise(*sys.exc_info())
        elif len(result) != 1:
            raise error("sockaddr resolved to multiple addresses")
        family, socktype, proto, name, address = result[0]

        if family == AF_INET:
            if len(sockaddr) != 2:
                raise error("IPv4 sockaddr must be 2 tuple")
        elif family == AF_INET6:
            address = address[:2] + sockaddr[2:]

        self.ares.getnameinfo(waiter, address, flags)
        node, service = waiter.get()
        if service is None:
            service = "0"
        return node, service
Example #21
0
 def socket_connect_exception():
     return _socket.error("socket connect not good")
Example #22
0
 def socket_close_exception():
     return _socket.error("socket close not good")
Example #23
0
 def fileno(self):
     fd = self._fileno
     if -1 == fd:
         raise error(EBADF, 'Bad file descriptor')
     return fd