From cf88a76a134127be4784b4fa9b2bcc2e0ba51c08 Mon Sep 17 00:00:00 2001 From: davidkneipp Date: Mon, 21 Oct 2024 10:48:09 +1000 Subject: [PATCH 1/2] Query IP on AAR if service URN not present --- lib/database.py | 17 +++++++++++++++++ lib/diameter.py | 14 +++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/database.py b/lib/database.py index 36559aa..63a3639 100755 --- a/lib/database.py +++ b/lib/database.py @@ -2100,6 +2100,23 @@ def Get_Serving_APNs(self, subscriber_id: int) -> dict: return apnDict + def Get_Serving_APN_By_IP(self, subscriberIp): + self.logTool.log(service='Database', level='debug', message="Getting Serving APN " + str(apn_id) + " with subscriber_id " + str(subscriber_id), redisClient=self.redisMessaging) + Session = sessionmaker(bind = self.engine) + session = Session() + + try: + result = session.query(SERVING_APN).filter_by(subscriber_routing=subscriberIp).first() + except Exception as E: + self.logTool.log(service='Database', level='debug', message=E, redisClient=self.redisMessaging) + self.safe_close(session) + raise ValueError(E) + result = result.__dict__ + result.pop('_sa_instance_state') + + self.safe_close(session) + return result + def Get_Charging_Rule(self, charging_rule_id): self.logTool.log(service='Database', level='debug', message="Called Get_Charging_Rule() for charging_rule_id " + str(charging_rule_id), redisClient=self.redisMessaging) Session = sessionmaker(bind = self.engine) diff --git a/lib/diameter.py b/lib/diameter.py index 4cbdfc4..96519a9 100755 --- a/lib/diameter.py +++ b/lib/diameter.py @@ -3010,6 +3010,18 @@ def Answer_16777236_265(self, packet_vars, avps): emergencySubscriber = True except Exception as e: emergencySubscriberData = None + + """ + If we didn't get a service urn, check if the IP represents a bearer for a local subscriber. + """ + try: + if not serviceUrn: + ipServingApn = self.database.Get_Serving_APN_By_IP(subscriberIp=ueIp) + ipApnName = '' + if ipServingApn: + ipApnName = self.database.Get_APN(apn_id=ipServingApn.get('apn', None)) + except Exception as e: + ipApnName = '' if '@' in subscriptionId: subscriberIdentifier = subscriptionId.split('@')[0] @@ -3106,7 +3118,7 @@ def Answer_16777236_265(self, packet_vars, avps): else: subscriberId = subscriberDetails.get('subscriber_id', None) if serviceUrn: - if 'sos' in str(serviceUrn).lower(): + if 'sos' in str(serviceUrn).lower() or 'sos' in ipApnName.lower(): registeredEmergencySubscriber = True apnId = (self.database.Get_APN_by_Name(apn="sos")).get('apn_id', None) else: From 289d2e8e03428e03a936974168ca3d4c25a38ebd Mon Sep 17 00:00:00 2001 From: davidkneipp Date: Mon, 21 Oct 2024 11:45:21 +1000 Subject: [PATCH 2/2] Working IP-based lookup --- lib/database.py | 1 - lib/diameter.py | 17 ++++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/database.py b/lib/database.py index 63a3639..730cfe0 100755 --- a/lib/database.py +++ b/lib/database.py @@ -2101,7 +2101,6 @@ def Get_Serving_APNs(self, subscriber_id: int) -> dict: return apnDict def Get_Serving_APN_By_IP(self, subscriberIp): - self.logTool.log(service='Database', level='debug', message="Getting Serving APN " + str(apn_id) + " with subscriber_id " + str(subscriber_id), redisClient=self.redisMessaging) Session = sessionmaker(bind = self.engine) session = Session() diff --git a/lib/diameter.py b/lib/diameter.py index 96519a9..eef01cb 100755 --- a/lib/diameter.py +++ b/lib/diameter.py @@ -3015,14 +3015,21 @@ def Answer_16777236_265(self, packet_vars, avps): If we didn't get a service urn, check if the IP represents a bearer for a local subscriber. """ try: - if not serviceUrn: + self.logTool.log(service='HSS', level='debug', message=f"[diameter.py] [Answer_16777236_265] [AAA] Service URN: {serviceUrn}", redisClient=self.redisMessaging) + if not serviceUrn or serviceUrn == 'None' or serviceUrn == None: + self.logTool.log(service='HSS', level='debug', message=f"[diameter.py] [Answer_16777236_265] [AAA] Checking Get_Serving_APN_By_IP", redisClient=self.redisMessaging) ipServingApn = self.database.Get_Serving_APN_By_IP(subscriberIp=ueIp) + self.logTool.log(service='HSS', level='debug', message=f"[diameter.py] [Answer_16777236_265] [AAA] IP Serving APN: {ipServingApn}", redisClient=self.redisMessaging) ipApnName = '' if ipServingApn: - ipApnName = self.database.Get_APN(apn_id=ipServingApn.get('apn', None)) + ipApnName = self.database.Get_APN(apn_id=int(ipServingApn.get('apn', {}))) + ipApnName = ipApnName.get('apn', None) except Exception as e: + self.logTool.log(service='HSS', level='debug', message=f"[diameter.py] [Answer_16777236_265] [AAA] Exception: {traceback.format_exc()}", redisClient=self.redisMessaging) ipApnName = '' + self.logTool.log(service='HSS', level='debug', message=f"[diameter.py] [Answer_16777236_265] [AAA] IP APN Name: {ipApnName}", redisClient=self.redisMessaging) + if '@' in subscriptionId: subscriberIdentifier = subscriptionId.split('@')[0] # Subscriber Identifier can be either an IMSI or an MSISDN @@ -3118,7 +3125,11 @@ def Answer_16777236_265(self, packet_vars, avps): else: subscriberId = subscriberDetails.get('subscriber_id', None) if serviceUrn: - if 'sos' in str(serviceUrn).lower() or 'sos' in ipApnName.lower(): + if 'sos' in str(serviceUrn).lower(): + registeredEmergencySubscriber = True + apnId = (self.database.Get_APN_by_Name(apn="sos")).get('apn_id', None) + elif ipApnName: + if 'sos' in ipApnName.lower(): registeredEmergencySubscriber = True apnId = (self.database.Get_APN_by_Name(apn="sos")).get('apn_id', None) else: