Ejemplo n.º 1
0
	async def handle_in_q(self):
		try:
			while True:
				
				preread = 6
				lb = await asyncio.wait_for(self.reader.readexactly(preread), self.timeout)
				if lb is None:
					logger.debug('Server timed out!')
					return
				if lb == b'':
					logger.debug('Server finished!')
					return

				if self.is_plain_msg is True:
					remaining_length = calcualte_length(lb) - preread
				else:
					remaining_length = int.from_bytes(lb[:4], byteorder = 'big', signed = False)
					remaining_length = (remaining_length + 4) - preread
				#print('Reading %s' % remaining_length)

				remaining_data = await asyncio.wait_for(self.reader.readexactly(remaining_length), self.timeout)
				
				await self.in_queue.put((lb+remaining_data, None))
				
		
		#except asyncio.CancelledError:
		#	return
		except Exception as e:
			#logger.exception('handle_in_q')
			await self.in_queue.put((None, e))

		finally:
			self.handle_out_task.cancel()
Ejemplo n.º 2
0
    def get_one_message(self, data):
        if len(data) < 6:
            return None

        if self.is_plain_msg is True:
            dl = calcualte_length(data[:6])
        else:
            dl = int.from_bytes(data[:4], byteorder='big', signed=False)
            dl = dl + 4

        #print(dl)
        if len(data) >= dl:
            return data[:dl]
Ejemplo n.º 3
0
    async def __handle_incoming(self):
        try:
            while True:
                message_data, err = await self.network.in_queue.get()
                if err is not None:
                    logger.debug(
                        'Client terminating bc __handle_incoming got an error!'
                    )
                    raise err

                #print('Incoming message data: %s' % message_data)
                if self.bind_ok is True:
                    if self.__encrypt_messages is True:
                        #removing size
                        message_data = message_data[4:]
                        try:
                            # seq number doesnt matter here, a it's in the header
                            message_data, err = await self.auth.decrypt(
                                message_data, 0)
                            if err is not None:
                                raise err
                            #print('Decrypted %s' % message_data.hex())
                            #print('Decrypted %s' % message_data)
                        except:
                            import traceback
                            traceback.print_exc()
                            raise

                    elif self.__sign_messages is True:
                        #print('Signed %s' % message_data)
                        message_data = message_data[4:]
                        try:
                            message_data = await self.auth.unsign(message_data)
                        #	print('Unsinged %s' % message_data)
                        except:
                            import traceback
                            traceback.print_exc()
                            raise

                msg_len = calcualte_length(message_data)
                msg_total_len = len(message_data)
                messages = []
                if msg_len == msg_total_len:
                    message = LDAPMessage.load(message_data)
                    messages.append(message)

                else:
                    #print('multi-message!')
                    while len(message_data) > 0:
                        msg_len = calcualte_length(message_data)
                        message = LDAPMessage.load(message_data[:msg_len])
                        messages.append(message)

                        message_data = message_data[msg_len:]

                message_id = messages[0]['messageID'].native
                if message_id not in self.message_table:
                    self.message_table[message_id] = []
                self.message_table[message_id].extend(messages)
                if message_id not in self.message_table_notify:
                    self.message_table_notify[message_id] = asyncio.Event()
                self.message_table_notify[message_id].set()

        except asyncio.CancelledError:
            self.status = MSLDAPClientStatus.STOPPED
            return

        except Exception as e:
            self.status = MSLDAPClientStatus.ERROR
            self.lasterror = e
            for msgid in self.message_table_notify:
                self.message_table[msgid] = [e]
                self.message_table_notify[msgid].set()

        self.status = MSLDAPClientStatus.STOPPED