Пример #1
0
    def gen_handler(self):
        p = self.protocol

        if self.is_client:
            ts_init = time.time()
            hs_mine = self.generate_request()
            p.transport.writeSequence((self.version_packet, hs_mine))

        # wait for the "version packet"
        s = yield 1
        version_other, = _s_uchar.unpack(s.read(1))

        if not self.version_supported(version_other):
            self.defaultHandshakeFailed("protocol version mismatch")
            return

        # now, the primary handshake packet
        s = yield self.packet_bytes

        hs_other = s.read(self.packet_bytes)

        if not self.is_client:
            ts_init = time.time()
            hs_mine = self.generate_request(context=hs_other)
            p.transport.writeSequence((self.version_packet, hs_mine))

        p.transport.write(self.generate_response(hs_other))

        # and now, the handshake response
        s = yield self.packet_bytes

        hs_response = buffer(s.read(self.packet_bytes))
        ts_received_back = time.time()

        if not self.verify_response(hs_mine, hs_response):
            self.defaultHandshakeFailed("response verification failed")
            return

        p.handshakeSucceeded(ts_init - self.epoch, ts_received_back - ts_init)
Пример #2
0
    def gen_handler(self):
        p = self.protocol

        if self.is_client:
            ts_init = time.time()
            hs_mine = self.generate_request()
            p.transport.writeSequence((self.version_packet, hs_mine))

        # wait for the "version packet"
        s = yield 1
        version_other, = _s_uchar.unpack(s.read(1))

        if not self.version_supported(version_other):
            self.defaultHandshakeFailed('protocol version mismatch')
            return

        # now, the primary handshake packet
        s = yield self.packet_bytes

        hs_other = s.read(self.packet_bytes)

        if not self.is_client:
            ts_init = time.time()
            hs_mine = self.generate_request(context=hs_other)
            p.transport.writeSequence((self.version_packet, hs_mine))

        p.transport.write(self.generate_response(hs_other))

        # and now, the handshake response
        s = yield self.packet_bytes

        hs_response = buffer(s.read(self.packet_bytes))
        ts_received_back = time.time()

        if not self.verify_response(hs_mine, hs_response):
            self.defaultHandshakeFailed('response verification failed')
            return

        p.handshakeSucceeded(ts_init - self.epoch, ts_received_back - ts_init)
Пример #3
0
    def gen_handler(self):
        s = ''

        while 1:
            if len(s) < 1:
                s = yield 1     # need 1 byte to read "basic header"

            htype, csid = read_header_head(s)
            a = csid
            csid_sel = min(csid, 2)
            head_size = _sizes_1[csid_sel] + _sizes_2[htype]

            if len(s) < head_size:
                s = yield head_size # bytes to read the rest of the header

            if csid == 0:
                csid = _s_uchar.unpack(s.read(1))[0] + 64
            elif csid == 1:
                csid = _s_ushort_l.unpack(s.read(2))[0] + 64

            m_time, m_size, m_type, m_msid = None, None, None, None
            c_h, h_base, accbody, to_read = None, None, None, None
            m_time_ext = False
            h_absolute = (htype == 0)

            # check csid in the message and chunk stream caches
            if csid in self.chstr_map:
                c_h, accbody, to_read = self.chstr_map[csid]
                m_time_ext = (c_h.time >= 0x00ffffff)
            elif not h_absolute and csid in self.msg_map:
                h_base = self.msg_map[csid]
                m_time_ext = (h_base.time >= 0x00ffffff)
            else:
                # TODO: check/warn header is absolute here
                pass

            if htype == 3:
                pass
            elif htype == 2:
                _time_1, m_time = _s_time.unpack(s.read(3))
                m_time += _time_1 << 8
            else:
                (_time_1, m_time, _size_1,
                 m_size, m_type) = _s_time_size_type.unpack(s.read(7))
                bla = m_time
                bla1 = m_size
                m_time += _time_1 << 8
                m_size += _size_1 << 8
                if htype == 0:
                    m_msid, = _s_ulong_l.unpack(s.read(4))

            if m_time == 0x00ffffff or m_time is None and m_time_ext:
                print 'F*****G EXTENDED mt:%s ext:%s size:%s a:%s csid:%s '\
                        'htype:%s hs:%s' % (m_time, m_time_ext,
                        m_size, a, csid, htype, head_size)
#                import pdb; pdb.set_trace()
                if len(s) < 4:
                    s = yield 4 # 4 bytes of "extended timestamp"
                m_time, = _s_ulong_b.unpack(s.read(4))
#                if m_size is not None:
#                    m_size += 4
                m_time = None

            h = Header(csid, m_time, m_size, m_type, m_msid)


            # fill header if cached entry was found earlier
            if c_h:
                # TODO: check/warn header consistency (with the cached
                # first one)
                h = c_h
            elif h_base:
                h = absolutize(h, h_base)
                to_read = h.size
            else:
                to_read = h.size

            need_bytes = min(to_read, self.chunk_size)
            if need_bytes > 0:
                if len(s) < need_bytes:
                    s = yield need_bytes

                if accbody is None:
                    accbody = s.read_seq(need_bytes)
                else:
                    accbody += s.read_seq(need_bytes)
            else:
                if accbody is None:
                    accbody = []

            if to_read > self.chunk_size:
                self.chstr_map[h.cs_id] = (h, accbody,
                                           to_read - self.chunk_size)
            else:
                self.chstr_map.pop(h.cs_id, None)
                self.msg_map[h.cs_id] = h

                if h.cs_id == 2 and 0 < h.type < 8 and h.ms_id == 0:
                    self.controlMessageReceived(h, vecbuf.VecBuf(accbody))
                else:
                    self.protocol.messageReceived(h, vecbuf.VecBuf(accbody))
Пример #4
0
    def gen_handler(self):
        s = ''

        while 1:
            if len(s) < 1:
                s = yield 1  # need 1 byte to read "basic header"

            htype, csid = read_header_head(s)
            a = csid
            csid_sel = min(csid, 2)
            head_size = _sizes_1[csid_sel] + _sizes_2[htype]

            if len(s) < head_size:
                s = yield head_size  # bytes to read the rest of the header

            if csid == 0:
                csid = _s_uchar.unpack(s.read(1))[0] + 64
            elif csid == 1:
                csid = _s_ushort_l.unpack(s.read(2))[0] + 64

            m_time, m_size, m_type, m_msid = None, None, None, None
            c_h, h_base, accbody, to_read = None, None, None, None
            m_time_ext = False
            h_absolute = (htype == 0)

            # check csid in the message and chunk stream caches
            if csid in self.chstr_map:
                c_h, accbody, to_read = self.chstr_map[csid]
                m_time_ext = (c_h.time >= 0x00ffffff)
            elif not h_absolute and csid in self.msg_map:
                h_base = self.msg_map[csid]
                m_time_ext = (h_base.time >= 0x00ffffff)
            else:
                # TODO: check/warn header is absolute here
                pass

            if htype == 3:
                pass
            elif htype == 2:
                _time_1, m_time = _s_time.unpack(s.read(3))
                m_time += _time_1 << 8
            else:
                (_time_1, m_time, _size_1, m_size,
                 m_type) = _s_time_size_type.unpack(s.read(7))
                bla = m_time
                bla1 = m_size
                m_time += _time_1 << 8
                m_size += _size_1 << 8
                if htype == 0:
                    m_msid, = _s_ulong_l.unpack(s.read(4))

            if m_time == 0x00ffffff or m_time is None and m_time_ext:
                if len(s) < 4:
                    s = yield 4  # 4 bytes of "extended timestamp"
                m_time, = _s_ulong_b.unpack(s.read(4))

            h = Header(csid, m_time, m_size, m_type, m_msid)

            # fill header if cached entry was found earlier
            if c_h:
                # TODO: check/warn header consistency (with the cached
                # first one)
                h = c_h
            elif h_base:
                h = absolutize(h, h_base)
                to_read = h.size
            else:
                to_read = h.size

            need_bytes = min(to_read, self.chunk_size)
            if need_bytes > 0:
                if len(s) < need_bytes:
                    s = yield need_bytes

                if accbody is None:
                    accbody = s.read_seq(need_bytes)
                else:
                    accbody += s.read_seq(need_bytes)
            else:
                if accbody is None:
                    accbody = []

            if to_read > self.chunk_size:
                self.chstr_map[h.cs_id] = (h, accbody,
                                           to_read - self.chunk_size)
            else:
                self.chstr_map.pop(h.cs_id, None)
                self.msg_map[h.cs_id] = h

                if h.cs_id == 2 and 0 < h.type < 8 and h.ms_id == 0:
                    self.controlMessageReceived(h, vecbuf.VecBuf(accbody))
                else:
                    self.protocol.messageReceived(h, vecbuf.VecBuf(accbody))