def datagram_received(self, data, addr): socket = self.transport.get_extra_info('socket') \ if self.transport else None try: received_date = datetime.now() reply = Packet(packet=data, dict=self.client.dict, creation_date=received_date) if reply is not None and reply.id in self.pending_requests: req = self.pending_requests[reply.id] packet = req['packet'] reply.secret = packet.secret if packet.VerifyReply(reply, data): if reply.message_authenticator and not \ reply.verify_message_authenticator( original_authenticator=packet.authenticator): self.logger.warn( '[%s:%d:%d] Received invalid reply for id %d. %s' % (self.server, self.port, socket.getsockname()[1] if socket else '', reply.id, 'Invalid Message-Authenticator. Ignoring it.')) self.errors += 1 else: req['future'].set_result(reply) # Remove request for map del self.pending_requests[reply.id] else: self.logger.warn( '[%s:%d:%d] Received invalid reply for id %d. %s' % (self.server, self.port, socket.getsockname()[1] if socket else '', reply.id, 'Ignoring it.')) self.errors += 1 else: self.logger.warn( '[%s:%d:%d] Received invalid reply with id %d: %s.\nIgnoring it.' % ( self.server, self.port, socket.getsockname()[1] if socket else '', (-1, reply.id)[reply is not None], data.hex(), )) self.errors += 1 except Exception as exc: self.logger.error( '[%s:%d:%d] Error on decode packet: %s.' % (self.server, self.port, socket.getsockname()[1] if socket else '', (exc, '\n'.join( traceback.format_exc().splitlines()))[self.client.debug]))
def datagram_received(self, data, addr): self.logger.debug('[%s:%d] Received %d bytes from %s', self.ip, self.port, len(data), addr) receive_date = datetime.utcnow() remote_host = self.__get_remote_host__(addr[0]) if remote_host: try: if self.server.debug: self.logger.info('[%s:%d] Received from %s packet: %s.' % (self.ip, self.port, addr, data.hex())) req = Packet(packet=data, dict=self.server.dict) except Exception as exc: self.logger.error( '[%s:%d] Error on decode packet: %s. Ignore it.' % (self.ip, self.port, exc)) req = None if not req: return try: if req.code in (AccountingResponse, AccessAccept, AccessReject, CoANAK, CoAACK, DisconnectNAK, DisconnectACK): raise ServerPacketError('Invalid response packet %d' % req.code) elif self.server_type == ServerType.Auth: if req.code != AccessRequest: raise ServerPacketError( 'Received not-authentication packet ' 'on authentication port') req = AuthPacket(secret=remote_host.secret, dict=self.server.dict, packet=data) if self.server.enable_pkt_verify and \ req.message_authenticator and \ not req.verify_message_authenticator(): raise PacketError( 'Received invalid Message-Authenticator') elif self.server_type == ServerType.Coa: if req.code != DisconnectRequest and \ req.code != CoARequest: raise ServerPacketError( 'Received not-coa packet on coa port') req = CoAPacket(secret=remote_host.secret, dict=self.server.dict, packet=data) if self.server.enable_pkt_verify: if not req.VerifyCoARequest(): raise PacketError('Packet verification failed') if req.message_authenticator and \ not req.verify_message_authenticator(): raise PacketError( 'Received invalid Message-Authenticator') elif self.server_type == ServerType.Acct: if req.code != AccountingRequest: raise ServerPacketError( 'Received not-accounting packet on ' 'accounting port') req = AcctPacket(secret=remote_host.secret, dict=self.server.dict, packet=data) if self.server.enable_pkt_verify: if not req.VerifyAcctRequest(): raise PacketError('Packet verification failed') if req.message_authenticator and not \ req.verify_message_authenticator(): raise PacketError( 'Received invalid Message-Authenticator') # Call request callback self.request_callback(self, req, addr) self.requests += 1 except Exception as e: self.logger.error( '[%s:%d] Unexpected error for packet from %s: %s' % (self.ip, self.port, addr, (e, '\n'.join(traceback.format_exc().splitlines()) )[self.server.debug])) else: self.logger.error('[%s:%d] Drop package from unknown source %s', self.ip, self.port, addr) process_date = datetime.utcnow() self.logger.debug('[%s:%d] Request from %s processed in %d ms', self.ip, self.port, addr, (process_date - receive_date).microseconds / 1000)