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() if addr[0] in self.hosts: remote_host = self.hosts[addr[0]] elif '0.0.0.0' in self.hosts: remote_host = self.hosts['0.0.0.0'] else: self.logger.warn('[%s:%d] Drop package from unknown source %s', self.ip, self.port, addr) return try: self.logger.debug('[%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', self.ip, self.port, exc) 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 non-auth packet on auth port') req = AuthPacket(secret=remote_host.secret, dict=self.server.dict, packet=data) if self.server.enable_pkt_verify: if req.VerifyAuthRequest(): raise PacketError('Packet verification failed') elif self.server_type == ServerType.Coa: if req.code != DisconnectRequest and req.code != CoARequest: raise ServerPacketError( 'Received non-coa packet on coa port') req = CoAPacket(secret=remote_host.secret, dict=self.server.dict, packet=data) if self.server.enable_pkt_verify: if req.VerifyCoARequest(): raise PacketError('Packet verification failed') elif self.server_type == ServerType.Acct: if req.code != AccountingRequest: raise ServerPacketError( 'Received non-acct packet on acct port') req = AcctPacket(secret=remote_host.secret, dict=self.server.dict, packet=data) if self.server.enable_pkt_verify: if req.VerifyAcctRequest(): raise PacketError('Packet verification failed') # Call request callback self.request_callback(self, req, addr) except Exception as exc: if self.server.debug: self.logger.exception('[%s:%d] Error for packet from %s', self.ip, self.port, addr) else: self.logger.error('[%s:%d] Error for packet from %s: %s', self.ip, self.port, addr, exc) 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)
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)