def __format_base_messages(self, confobj): if 'base_message_list' not in confobj['GENERIC']: logWarn("Base message list empty", verbose = self.__verbose, TAG = "parseConfig") return [] self.__configs['base_message_list'] = confobj['GENERIC']['base_message_list'] msgs = [] for msg_type in self.__configs['base_message_list']: if int(msg_type) == GTPmessageTypeDigit["echo-request"] : i = 0 while i < self.__num_msg : msgs.append(EchoRequest(version = self.__version)) i += 1 elif int(msg_type) == GTPmessageTypeDigit["echo-response"] : i = 0 while i < self.__num_msg : msgs.append(EchoResponse(1, version = self.__version)) i += 1 else: raise Exception("%s:%s - Invalid base msg type " "%d"%(self.__class__.__name__, "__format_base_messages", int(msg_type))) return msgs
def stop(self): if not self.is_running: logWarn("is not running", verbose=self.is_verbose, TAG=self.TAG_NAME) return self.is_running = False logOk("Stopped", verbose=self.is_verbose, TAG=self.TAG_NAME)
def run(self): self.is_running = True count = 0 while self.sock is not None and self.is_running: try: data, addr = self.sock.recvfrom(1024) if len(data) > 8: (flags, resp_msg_type, length, sequence_or_teid) = struct.unpack("!BBHL", data[:8]) version = flags & 0xF0 if version != 0x40: logWarn("Unsupported GTP version %02x" % (version), verbose=self.is_verbose, TAG=self.TAG_NAME) continue if not message_queue.has_key(addr[0]): logWarn("Unmanaged IP %s" % (addr[0]), verbose=self.is_verbose, TAG=self.TAG_NAME) continue req_msg_type = GTPResponse2Request[resp_msg_type] if not message_queue[addr[0]].has_key(req_msg_type): logWarn("Unsolicited response msg %d" % (resp_msg_type), verbose=self.is_verbose, TAG=self.TAG_NAME) continue logWarn("Received response to sent msg %s from ip %s" % (GTPmessageTypeStr[req_msg_type], addr[0]), verbose=self.is_verbose, TAG=self.TAG_NAME) if req_msg_type == GTPmessageTypeDigit["echo-request"]: message_queue[addr[0]][req_msg_type][0]['reply'] = 1 else: for elem in message_queue[addr[0]][req_msg_type]: if elem.has_key('local_teid') and \ elem['local_teid'] == sequence_or_teid : elem['reply'] = 1 elem['remote_teid'] = self.__getFTEID( data[12:]) break count += 1 logNormal("RECEIVED #%d messages" % (count), verbose=self.is_verbose, TAG=self.TAG_NAME) except timeout, e: if addr[0]: logErr("%s TIMEOUT_ERROR" % (addr[0]), TAG=self.TAG_NAME) else: logErr("TIMEOUT_ERROR", TAG=self.TAG_NAME) pass except error, e: if e.errno == errno.EBADFD: if addr[0]: logErr("%s BAD_FILE_DESCRIPTOR_ERROR" % (addr[0]), TAG=self.TAG_NAME) else: logErr("BAD_FILE_DESCRIPTOR_ERROR", TAG=self.TAG_NAME) break elif e.errno == errno.EPIPE: if addr[0]: logErr("%s BROKEN_PIPE_ERROR" % (addr[0]), TAG=self.TAG_NAME) else: logErr("BROKEN_PIPE_ERROR", TAG=self.TAG_NAME) break else: logErr("UNKNOWN ERROR: %s" % (e), TAG=self.TAG_NAME) break
def __format_interface_msg(self, confobj): msgs = [] if confobj is None: raise Exception("%s:%s - Configuration Object is None. " %(self.__class__.__name__, "__format_interface_msg")) if '3gpp_messages_list' not in confobj['GENERIC']: logWarn("3gpp message list empty", verbose = self.__verbose, TAG = "parseConfig") return [] self.__configs['3gpp_messages_list'] = confobj['GENERIC']['3gpp_messages_list'] if 'IES' not in confobj.sections: raise ConfigObjError('Section IES is required') if 'interface' not in confobj['GENERIC']: raise ConfigObjError('Value "GENERIC.interface" is required') self.__configs['interface'] = confobj['GENERIC']['interface'] for msg_type in self.__configs['3gpp_messages_list']: if int(msg_type) == GTPmessageTypeDigit["create-session-request"] : i = 0 while i < self.__num_msg : msgs.append(CreateSessionRequest( source_ip = confobj['GENERIC']['source_ip'], interface = int(self.__configs['interface']), imsi = confobj['IES']['imsi'], mcc = confobj['IES']['mcc'], mnc = confobj['IES']['mnc'], lac = int(confobj['IES']['lac']), rac = int(confobj['IES']['rac']), apn = confobj['IES']['apn'], p_dns = confobj['IES']['primary_dns'], s_dns = confobj['IES']['secondary_dns'], gsn = confobj['IES']['gsn'], phone= confobj['IES']['msisdn'], imei = confobj['IES']['imei'], rat_type = confobj['IES']['rat_type'], ebi = int(confobj['IES']['ebi']) ) ) i += 1 elif int(msg_type) == GTPmessageTypeDigit["create-session-response"] : i = 0 while i < self.__num_msg : msgs.append(CreateSessionResponse( int(confobj['GENERIC']['teid'], 16), int(confobj['GENERIC']['sqn'], 16), confobj['GENERIC']['source_ip'], int(self.__configs['interface']), p_dns = confobj['IES']['primary_dns'], s_dns = confobj['IES']['secondary_dns'], ) ) i += 1 elif int(msg_type) == GTPmessageTypeDigit["delete-session-request"] : mcc = confobj['IES']['mcc'] mnc = confobj['IES']['mnc'] lac = int(confobj['IES']['lac']) rac = int(confobj['IES']['rac']) ebi = int(confobj['IES']['ebi']) interface = int(self.__configs['interface']) for t in confobj['GENERIC']['teid']: msgs.append(DeleteSessionRequest(int(t, 16), source_ip = confobj['GENERIC']['source_ip'], mcc = mcc, mnc = mnc, lac = lac, rac = rac, ebi = ebi, interface = interface ) ) elif int(msg_type) == GTPmessageTypeDigit["delete-session-response"] : for t,s in zip(confobj['GENERIC']['teid'], confobj['GENERIC']['sqn']): msgs.append(DeleteSessionResponse(teid = int(t, 16), sqn = int(s, 16) ) ) elif int(msg_type) == GTPmessageTypeDigit["create-bearer-request"] : source_ip = confobj['GENERIC']['source_ip'] interface = int(self.__configs['interface']) ebi = int(confobj['IES']['ebi']) for t,s in zip(confobj['GENERIC']['teid'], confobj['GENERIC']['sqn']): msgs.append(CreateBearerRequest(teid = int(t, 16), source_ip = source_ip, interface = interface, ebi = ebi, sqn = int(s, 16) ) ) elif int(msg_type) == GTPmessageTypeDigit["modify-bearer-request"] : source_ip = confobj['GENERIC']['source_ip'] interface = int(self.__configs['interface']) ebi = int(confobj['IES']['ebi']) nit = int(confobj['IES']['node_id_type']) mcc = int(confobj['IES']['mcc']) mnc = int(confobj['IES']['mnc']) if 'fteid' in confobj['IES']: fteid = int(confobj['IES']['fteid']) else: fteid = 0 for t,s in zip(confobj['GENERIC']['teid'], confobj['GENERIC']['sqn']): msgs.append(ModifyBearerRequest(teid = int(t, 16), source_ip = source_ip, interface = interface, ebi = ebi, sqn = int(s, 16), nit = nit, fteid = fteid, mcc = mcc, mnc = mnc ) ) elif int(msg_type) == GTPmessageTypeDigit["modify-bearer-response"] : source_ip = confobj['GENERIC']['source_ip'] interface = int(self.__configs['interface']) ebi = int(confobj['IES']['ebi']) for t,s in zip(confobj['GENERIC']['teid'], confobj['GENERIC']['sqn']): msgs.append(ModifyBearerResponse(teid = int(t, 16), source_ip = source_ip, interface = interface, ebi = ebi, sqn = int(s, 16) ) ) elif int(msg_type) == GTPmessageTypeDigit["create-bearer-response"] : source_ip = confobj['GENERIC']['source_ip'] interface = int(self.__configs['interface']) ebi = int(confobj['IES']['ebi']) for t,s in zip(confobj['GENERIC']['teid'], confobj['GENERIC']['sqn']): msgs.append(CreateBearerResponse(teid = int(t, 16), source_ip = source_ip, interface = interface, ebi = ebi, sqn = int(s, 16) ) ) elif int(msg_type) == GTPmessageTypeDigit["delete-bearer-request"] : source_ip = confobj['GENERIC']['source_ip'] interface = int(self.__configs['interface']) ebi = int(confobj['IES']['ebi']) nit = int(confobj['IES']['node_id_type']) cause = int(confobj['IES']['cause']) mcc = int(confobj['IES']['mcc']) mnc = int(confobj['IES']['mnc']) for t,s in zip(confobj['GENERIC']['teid'], confobj['GENERIC']['sqn']): msgs.append(DeleteBearerRequest(teid = int(t, 16), source_ip = source_ip, interface = interface, ebi = ebi, sqn = int(s, 16), nit = nit, cause = cause, mcc = mcc, mnc = mnc ) ) elif int(msg_type) == GTPmessageTypeDigit["delete-bearer-response"] : source_ip = confobj['GENERIC']['source_ip'] interface = int(self.__configs['interface']) ebi = int(confobj['IES']['ebi']) for t,s in zip(confobj['GENERIC']['teid'], confobj['GENERIC']['sqn']): msgs.append(DeleteBearerResponse(teid = int(t, 16), source_ip = source_ip, interface = interface, ebi = ebi, sqn = int(s, 16) ) ) elif int(msg_type) == GTPmessageTypeDigit["delete-pdn-connection-set-request"] : source_ip = confobj['GENERIC']['source_ip'] nit = int(confobj['IES']['node_id_type']) mcc = int(confobj['IES']['mcc']) mnc = int(confobj['IES']['mnc']) for s in confobj['GENERIC']['sqn']: msgs.append(DeletePDNConnectionSetRequest( source_ip = source_ip,sqn = int(s, 16), nit = nit, mcc = mcc, mnc = mnc) ) return msgs