def connect(self): try: Msg = "connect Init" PCA_GenLib.WriteLog(Msg, 9) PCA_SCTPClientSocket.Connector.connect(self) time.sleep(1) Msg = "send M3UA ASUP_UP" PCA_GenLib.WriteLog(Msg, 2) Message = self.M3UAMessage.getASUP_UP() self.parser.parse(Message) response_message = self.handler.getHandlerResponse() ServerID = self.handler.getTID() DebugStr = self.handler.getDebugStr() Msg = "send : %s*" % DebugStr PCA_GenLib.WriteLog(Msg, 1) self.sendDataToSocket(Message) #Msg = "send = *\n%s\n*" % PCA_GenLib.HexDump(self.M3UA_ASUP_UP) #PCA_GenLib.WriteLog(Msg,2) Message = self.readDataFromSocket() if Message != None: #Msg = "receive = *\n%s\n*" % PCA_GenLib.HexDump(Message) #PCA_GenLib.WriteLog(Msg,2) self.parser.parse(Message) response_message = self.handler.getHandlerResponse() ServerID = self.handler.getTID() DebugStr = self.handler.getDebugStr() Msg = "recv : %s*" % DebugStr PCA_GenLib.WriteLog(Msg, 1) message_type = ord(Message[3]) Msg = "message type = %s" % message_type PCA_GenLib.WriteLog(Msg, 2) if message_type == 4: Msg = "ASUP up ack" PCA_GenLib.WriteLog(Msg, 2) Message = self.M3UAMessage.getASP_Active() self.parser.parse(Message) response_message = self.handler.getHandlerResponse() ServerID = self.handler.getTID() DebugStr = self.handler.getDebugStr() Msg = "send : %s*" % DebugStr PCA_GenLib.WriteLog(Msg, 1) self.sendDataToSocket(Message) #Msg = "send = *\n%s\n*" % PCA_GenLib.HexDump(self.M3UA_ASP_Active) Message = self.readDataFromSocket() if Message != None: #Msg = "receive = *\n%s\n*" % PCA_GenLib.HexDump(Message) #PCA_GenLib.WriteLog(Msg,1) self.parser.parse(Message) response_message = self.handler.getHandlerResponse() ServerID = self.handler.getTID() DebugStr = self.handler.getDebugStr() Msg = "recv : %s*" % DebugStr PCA_GenLib.WriteLog(Msg, 1) Msg = "connect OK" PCA_GenLib.WriteLog(Msg, 9) except: Msg = "connect error : <%s>,<%s> " % (sys.exc_type, sys.exc_value) PCA_GenLib.WriteLog(Msg, 0) raise
def getMessageError(self): try: # #<TCAP tcap_begin:62>=<4804010000006b1a281806 ...... # <TCAP Originating TID:48>=<01000000>* # <TCAP dialog_portion:6b>=<2818060700118605010101a00d600ba109060704000001001503>* # # <TCAP external_tag:28>=<060700118605010101a00d600ba109060704000001001503>* # <TCAP oid:06>=<00118605010101>* # <TCAP Single_ASN1_Type:a0>=<600ba109060704000001001503>* # <TCAP dialog_request:60>=<a109060704000001001503>* # <TCAP Application_Context:a1>=<060704000001001503>* # <TCAP Application_Context_name:06 >=<04000001001503>* # <MAP component_type_invoke:a1>=<0201020.......82009099041411000c918896820000200000020661f138269b01> # <MAP invoke_id:02>=<02> # <MAP opCode:02>=<mo-ForwardSM> ############################################### # Transaction Portion ############################################### tag = chr(0x49) tag_data = chr(0x94) + chr(0x00) + chr(0x00) + chr(0x00) transaction_portion = self.constructTLV(tag, tag_data) ############################################### # Dialog Portion ############################################### Application_Context_name = chr(0x04) + chr(0x00) + chr(0x00) + chr( 0x01) + chr(0x00) + chr(0x15) + chr(0x03) Application_Context_name_Tag = chr(0x06) Application_Context = self.constructTLV( Application_Context_name_Tag, Application_Context_name) Application_Context_Tag = chr(0xa1) Application_Context_TLV = self.constructTLV( Application_Context_Tag, Application_Context) app_result_user_diag_tag = chr(0x02) app_result_user_diag_value = chr(0x00) app_result_user_diag = self.constructTLV( app_result_user_diag_tag, app_result_user_diag_value) result_user_diag_tag = chr(0xa1) result_user_diag_TLV = self.constructTLV(result_user_diag_tag, app_result_user_diag) result_source_diag_tag = chr(0xa3) result_source_diag_TLV = self.constructTLV(result_source_diag_tag, result_user_diag_TLV) app_result_tag = chr(0x02) app_result_value = chr(0x00) app_result = self.constructTLV(app_result_tag, app_result_value) result_tag = chr(0xa2) result_TLV = self.constructTLV(result_tag, app_result) #dialog_response = self.constructTLV(Application_Context_Tag,Application_Context) dialog_response = Application_Context_TLV + result_TLV + result_source_diag_TLV dialog_response_tag = chr(0x61) dialog_response_TLV = self.constructTLV(dialog_response_tag, dialog_response) #Single_ASN1 = self.constructTLV(dialog_response_tag,dialog_response) Single_ASN1_Tag = chr(0xa0) Single_ASN1_TLV = self.constructTLV(Single_ASN1_Tag, dialog_response_TLV) oid_tag = chr(0x06) oid_data = chr(0x00) + chr(0x11) + chr(0x86) + chr(0x05) + chr( 0x01) + chr(0x01) + chr(0x01) oid_tlv = self.constructTLV(oid_tag, oid_data) tcap_external = oid_tlv + Single_ASN1_TLV tcap_external_tag = chr(0x28) tcap_external_tlv = self.constructTLV(tcap_external_tag, tcap_external) dialog_portion_tag = chr(0x6b) dialog_portion = self.constructTLV(dialog_portion_tag, tcap_external_tlv) ############################################### # Component Portion -- MAP ############################################### tag = chr(0x6c) tag_data = self.MAPMessage.getMessage() component_portion = self.constructTLV(tag, tag_data) tcap_data = transaction_portion + dialog_portion + component_portion message_length = len(tcap_data) message_length_hex = struct.pack("!B", message_length) TCAP_Tag = chr(0x64) tcap_message = TCAP_Tag + message_length_hex + tcap_data self.Message = tcap_message Msg = "DEBUG TCAP = *\n%s\n*" % PCA_GenLib.HexDump(self.Message) PCA_GenLib.WriteLog(Msg, 3) return self.Message except: Msg = "getMessage Error :<%s>,<%s>" % (sys.exc_type, sys.exc_value) PCA_GenLib.WriteLog(Msg, 0) raise
def getHandlerResponse(self): try: Msg = "getHandlerResponse Init " PCA_GenLib.WriteLog(Msg,9) ############################################### # Transaction Portion ############################################### # No map message , then should be a tcap only for long message if self.component_portion_avail == 0 and self.tcap_begin == 1: Msg = "TCAP begin message ready for tcap continue" PCA_GenLib.WriteLog(Msg,3) self.TCAP_Tag = chr(0x65) # tcap continue tag #Source TCAP ID tag = chr(0x48) tag_data = struct.pack("!I",self.tcap_tid) self.tcap_tid = self.tcap_tid + 1 self.transaction_portion = self.constructTLV(tag,tag_data) + self.transaction_portion ############################################### # Dialog Portion ############################################### if self.TCAP_Tag == PCA_TCAPParameters.tcap_abort: # MT-FSM V2 Application_Context_name = chr(0x04)+chr(0x00)+chr(0x00)+chr(0x01)+chr(0x00)+chr(0x19)+chr(0x02) Application_Context_name_Tag = chr(0x06) Application_Context = self.constructTLV(Application_Context_name_Tag,Application_Context_name) Application_Context_Tag = chr(0xa1) Application_Context_TLV = self.constructTLV(Application_Context_Tag,Application_Context) app_result_tag = chr(0x02) app_result_value = chr(0x02) app_result = self.constructTLV(app_result_tag,app_result_value) app_result_user_diag_tag = chr(0x02) app_result_user_diag_value = chr(0x00) app_result_user_diag = self.constructTLV(app_result_user_diag_tag,app_result_user_diag_value) result_user_diag_tag = chr(0xa1) result_user_diag_TLV = self.constructTLV(result_user_diag_tag,app_result_user_diag) app_result_tag = chr(0x02) app_result_value = chr(0x01) app_result = self.constructTLV(app_result_tag,app_result_value) result_tag = chr(0xa2) result_TLV = self.constructTLV(result_tag,app_result) result_source_diag_tag = chr(0xa3) result_source_diag_TLV = self.constructTLV(result_source_diag_tag,result_user_diag_TLV) dialog_response_tag = chr(0x61) dialog_response = Application_Context_TLV+result_TLV+result_source_diag_TLV dialog_response_TLV = self.constructTLV(dialog_response_tag,dialog_response) Single_ASN1_Tag = chr(0xa0) Single_ASN1_TLV = self.constructTLV(Single_ASN1_Tag,dialog_response_TLV) oid_tag = chr(0x06) oid_data = self.TCAP_Message["TCAP oid"][1] oid_tlv = self.constructTLV(oid_tag,oid_data) tcap_external = oid_tlv + Single_ASN1_TLV tcap_external_tag = chr(0x28) tcap_external_tlv = self.constructTLV(tcap_external_tag,tcap_external) dialog_portion_tag = chr(0x6b) dialog_portion = self.constructTLV(dialog_portion_tag,tcap_external_tlv) # Not MAP v1 and not TCAP continue message elif self.Is_MAP_v1 != 0 and self.tcap_continue == 0: Application_Context_name = self.TCAP_Message["TCAP ap_context_name"][1] Application_Context_name_Tag = chr(0x06) Application_Context = self.constructTLV(Application_Context_name_Tag,Application_Context_name) Application_Context_Tag = chr(0xa1) Application_Context_TLV = self.constructTLV(Application_Context_Tag,Application_Context) app_result_tag = chr(0x02) app_result_value = chr(0x00) app_result = self.constructTLV(app_result_tag,app_result_value) app_result_user_diag_tag = chr(0x02) app_result_user_diag_value = chr(0x00) app_result_user_diag = self.constructTLV(app_result_user_diag_tag,app_result_user_diag_value) result_user_diag_tag = chr(0xa1) result_user_diag_TLV = self.constructTLV(result_user_diag_tag,app_result_user_diag) app_result_tag = chr(0x02) app_result_value = chr(0x00) app_result = self.constructTLV(app_result_tag,app_result_value) result_tag = chr(0xa2) result_TLV = self.constructTLV(result_tag,app_result) result_source_diag_tag = chr(0xa3) result_source_diag_TLV = self.constructTLV(result_source_diag_tag,result_user_diag_TLV) dialog_response_tag = chr(0x61) dialog_response = Application_Context_TLV+result_TLV+result_source_diag_TLV dialog_response_TLV = self.constructTLV(dialog_response_tag,dialog_response) Single_ASN1_Tag = chr(0xa0) Single_ASN1_TLV = self.constructTLV(Single_ASN1_Tag,dialog_response_TLV) oid_tag = chr(0x06) oid_data = self.TCAP_Message["TCAP oid"][1] oid_tlv = self.constructTLV(oid_tag,oid_data) tcap_external = oid_tlv + Single_ASN1_TLV tcap_external_tag = chr(0x28) tcap_external_tlv = self.constructTLV(tcap_external_tag,tcap_external) dialog_portion_tag = chr(0x6b) dialog_portion = self.constructTLV(dialog_portion_tag,tcap_external_tlv) else: # MAP v1 or tcap continue message no dialog portion dialog_portion = '' Msg = "no dialog portion" PCA_GenLib.WriteLog(Msg,2) ############################################### # Component Portion ############################################### if self.component_portion_avail == 1 and self.TCAP_Tag != PCA_TCAPParameters.tcap_abort: tag = chr(0x6c) tag_data = self.component_portion component_portion_tlv = self.constructTLV(tag,tag_data) #Msg = "DEBUG component portion = *\n%s\n*" % PCA_GenLib.HexDump(component_portion_tlv) #PCA_GenLib.WriteLog(Msg,1) else: component_portion_tlv = '' Msg = "no component portion" PCA_GenLib.WriteLog(Msg,2) tcap_data = self.transaction_portion + dialog_portion + component_portion_tlv #tcap_data = self.transaction_portion + dialog_portion message_length = len(tcap_data) message_length_hex = struct.pack("!B",message_length) tcap_message = self.TCAP_Tag + message_length_hex + tcap_data self.Message = tcap_message Msg = "getHandlerResponse OK" PCA_GenLib.WriteLog(Msg,9) return self.Message except: Msg = "getHandlerResponse error : <%s>,<%s> " % (sys.exc_type,sys.exc_value) PCA_GenLib.WriteLog(Msg,0) raise
def parse(self, source, Traffic_Type): try: Msg = "parser init" PCA_GenLib.WriteLog(Msg, 9) orig_data = source name = 'none' self.StartParsing = 0 TID = "na" content = "na" #Msg = "DEBUG SCCP = *\n%s\n*" % PCA_GenLib.HexDump(source) #PCA_GenLib.WriteLog(Msg,0) ############################################ # Message type code # Mandatory fixed part # Mandatory variable part # Optional part if (source != None): self._cont_handler.startDocument() self.StartParsing = 1 name = "Traffic Type" attrs = Traffic_Type content = Traffic_Type self.set_handler(name, attrs, content) name = "Message Type" attrs = source[0] message_type = '' try: content = PCA_SCCPParameters.SCCP_message_type[attrs] except: content = "undef message type =<%s>" % PCA_GenLib.HexDump( attrs) message_type = content self.set_handler(name, attrs, content) ################################################# # Number of Tag depend on message type ################################################# if (message_type == "XUDTS_Extended_unitdata") or (message_type == "UDTS_unitdata"): source = source[1:] name = "Protocol Class" attrs = source[0] content = ord(attrs) self.set_handler(name, attrs, content) if (message_type == "XUDTS_Extended_unitdata"): source = source[1:] name = "Hop Counter" attrs = source[0] content = ord(attrs) self.set_handler(name, attrs, content) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, name, content) source = source[1:] name = "P called address" attrs = source[0] content = ord(attrs) P_called_address = content called_party_address = source[P_called_address + 1:] self.set_handler(name, attrs, content) #self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) source = source[1:] name = "P calling address" attrs = source[0] content = ord(attrs) P_calling_address = content calling_party_address = source[content + 1:] self.set_handler(name, attrs, content) #self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) source = source[1:] name = "P tcap Param" attrs = source[0] content = ord(attrs) P_tcap_parameter = content tcap_parameter = source[content + 1:] self.set_handler(name, attrs, content) #self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) source = source[1:] name = "P2_Option_parm" attrs = source[0] content = ord(attrs) self.set_handler(name, attrs, content) #self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) #source = source[4:] #if len(source) != 0: # Msg = "rest data =\n%s" % PCA_GenLib.HexDump(source) # PCA_GenLib.WriteLog(Msg,2) Msg = "parsing called_party_address " PCA_GenLib.WriteLog(Msg, 2) self.DebugStr = "%s,Called Party Address Info :" % self.DebugStr self.parseAddress(called_party_address, P_calling_address - P_called_address, "called") self.DebugStr = "%s,Calling Party Address Info :" % self.DebugStr self.parseAddress(calling_party_address, P_tcap_parameter - P_calling_address, "calling") #################################################### # Parsing TCAP #################################################### #Msg = "DEBUG tcap =\n%s" % PCA_GenLib.HexDump(tcap_parameter) #PCA_GenLib.WriteLog(Msg,0) self.set_handler('tcap_parameter', tcap_parameter, "tcap_parameter") #self.DebugStr = "%s,TCAP MSG=%s" % (self.DebugStr,TCAP_DebugStr) else: Msg = "SCCP message type = %s not implement yet" % message_type PCA_GenLib.WriteLog(Msg, 0) if self.StartParsing == 1: self._cont_handler.endDocument(orig_data, self.DebugStr) Msg = "parser OK" PCA_GenLib.WriteLog(Msg, 9) except: Msg = "parser :<%s>,<%s>,name=<%s>" % (sys.exc_type, sys.exc_value, name) PCA_GenLib.WriteLog(Msg, 0) Msg = "orig data =\n%s" % PCA_GenLib.HexDump(orig_data) PCA_GenLib.WriteLog(Msg, 0) raise
def __init__(self,XMLCFG): try: Msg = "PCA_SMPPClientSocket init ..." PCA_GenLib.WriteLog(Msg,9) Msg = "Connector init ..." PCA_GenLib.WriteLog(Msg,1) self.XMLCFG = XMLCFG Tag = "REMOTE_HOST" host = PCA_XMLParser.GetXMLTagValue(XMLCFG,Tag) Tag = "CONNECT_PORT" connect_port = PCA_XMLParser.GetXMLTagValue(XMLCFG,Tag) self.host = host self.connect_port = string.atoi(connect_port) Msg = "Host=<%s>,Port=<%s>" % (self.host,self.connect_port) PCA_GenLib.WriteLog(Msg,7) Msg = "Call Socket..." PCA_GenLib.WriteLog(Msg,7) # make a TCP/IP spocket object self.SocketDescriptor = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # /* Set SO_REUSEADDR socket option to allow socket reuse */ self.SocketDescriptor.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) Msg = "setsockopt..SO_REUSEADDR." PCA_GenLib.WriteLog(Msg,8) # /* Set SO_KEEPALIVE socket option */ self.SocketDescriptor.setsockopt( socket.SOL_SOCKET, socket.SO_KEEPALIVE,1 ) Msg = "setsockopt...SO_KEEPALIVE" PCA_GenLib.WriteLog(Msg,8) try: Tag = "BIND_PORT" bind_port = PCA_XMLParser.GetXMLTagValue(XMLCFG,Tag) self.bind_port = string.atoi(bind_port) Msg = "bind port number = <%s>" % self.bind_port PCA_GenLib.WriteLog(Msg,7) self.SocketDescriptor.bind(('', self.bind_port)) # bind it to server port number except: Msg = "bind error..." PCA_GenLib.WriteLog(Msg,8) Msg = "Connector OK." PCA_GenLib.WriteLog(Msg,9) Msg = "PCA_SMPPClientSocket OK." PCA_GenLib.WriteLog(Msg,9) except : Msg = "PCA_SMPPClientSocket Initial error : <%s>,<%s> " % (sys.exc_type,sys.exc_value) PCA_GenLib.WriteLog(Msg,0) raise
def getDetailMessage(message,parameter_list,display_flags): try: Msg = "-----------------------------------------------------------------" PCA_GenLib.WriteLog(Msg,2) sccp_msg_dict = {} for m3ua_key in sorted(message): if m3ua_key == "M3UA sccp_msg_dict": sccp_msg_dict = message[m3ua_key][0] else: Msg = "<%s>=<%s>,hex=<%s>*" % (m3ua_key,message[m3ua_key][0],PCA_GenLib.getHexString(message[m3ua_key][1])) PCA_GenLib.WriteLog(Msg,display_flags) parameter_list[m3ua_key] = message[m3ua_key] tcap_msg_dict = {} for sccp_key in sorted(sccp_msg_dict): if sccp_key == "SCCP tcap_msg_dict": tcap_msg_dict = sccp_msg_dict[sccp_key][0] else: Msg = "<%s>=<%s>,hex=<%s>*" % (sccp_key,sccp_msg_dict[sccp_key][0],PCA_GenLib.getHexString(sccp_msg_dict[sccp_key][1])) PCA_GenLib.WriteLog(Msg,display_flags) parameter_list[sccp_key] = sccp_msg_dict[sccp_key] map_msg_dict = {} for tcap_key in sorted(tcap_msg_dict): if tcap_key == "TCAP map_msg_dict": map_msg_dict = tcap_msg_dict[tcap_key][0] else: Msg = "<%s>=<%s>,hex=<%s>*" % (tcap_key,tcap_msg_dict[tcap_key][0],PCA_GenLib.getHexString(tcap_msg_dict[tcap_key][1])) PCA_GenLib.WriteLog(Msg,display_flags) parameter_list[tcap_key] = tcap_msg_dict[tcap_key] for map_key in sorted(map_msg_dict): Msg = "<%s>=<%s>,hex=<%s>*" % (map_key,map_msg_dict[map_key][0],PCA_GenLib.getHexString(map_msg_dict[map_key][1])) PCA_GenLib.WriteLog(Msg,display_flags) parameter_list[map_key] = map_msg_dict[map_key] Msg = "-----------------------------------------------------------------" PCA_GenLib.WriteLog(Msg,2) #return parameter_list except: Msg = "getDetailMessage error : <%s>,<%s> " % (sys.exc_type,sys.exc_value) PCA_GenLib.WriteLog(Msg,0) raise
def parseTLV(self, data): try: Msg = "parseTLV Init " PCA_GenLib.WriteLog(Msg, 9) source = data tlv_desc = 'na' tlv_type = 'na' name = 'na' number_of_tlv = 0 #Msg = "MAP parseTLV data =\n%s" % PCA_GenLib.HexDump(source) #PCA_GenLib.WriteLog(Msg,0) tag = "" while len(source) > 0: number_of_tlv = number_of_tlv + 1 if number_of_tlv > 100: Msg = "number of TLV > 100 " PCA_GenLib.WriteLog(Msg, 0) break self.tag_index = self.tag_index + 1 name = "Tag" attrs = source[0] tag_desc = "na" try: #content = "%s:%s" % (PCA_TCAPParameters.Tag_Desc[attrs],PCA_GenLib.getHexString(attrs)) tag_desc = PCA_TCAPParameters.Tag_Desc[attrs] content = tag_desc except: #content = "undef:%s" % PCA_GenLib.getHexString(attrs) content = "undef:%s" % PCA_GenLib.getHexString(attrs) #tag = content ##tag = "%s:%s" % (content,PCA_GenLib.getHexString(attrs)) tag = "%s" % content #self.set_handler(name,chr(0x00),content) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, name, content) tag_class = ord(attrs) & 0xc0 tag_class = tag_class >> 6 Tag_Type = 'Primitive' if (ord(attrs) & 0x20): attrs = source[0:2] content = PCA_GenLib.getHexString(attrs) Tag_Type = 'Constructor' else: content = ord(attrs) Tag_Type = 'Primitive' name = "tag type" content = Tag_Type #self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) # name = "%s form" % tag #self.set_handler(name,chr(0x00),Tag_Type) #name = "%s-%s" % (name,PCA_TCAPParameters.tag_class[tag_class]) #if Tag_Type == 'Primitive': # attrs = struct.pack("!b",ord(attrs) & 0x1f) #name = "%s %s" % (tag,tag_index) #self.set_handler(name,attrs,content) #self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) Tag_form = "Extended format" if (ord(source[0]) & 0x1f) == 0x1f: Tag_form = "Extended format" source = source[2:] #source = source[1:] else: Tag_form = "One octet format" source = source[1:] name = "tag form" content = Tag_form #self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) name = "length" name = "%s length" % tag attrs = source[0] content = ord(attrs) tag_length_form = "short" if content & 0x80: tag_length_form = "long" long_tag_length = chr(content & 0x7F) + source[1] content = struct.unpack("!H", long_tag_length)[0] tag_length = content #tag_length = struct.unpack("!B" else: tag_length_form = "short" content = struct.unpack("!B", attrs)[0] tag_length = content #self.set_handler(name,attrs,content) name = "%s %s" % (tag_length_form, name) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, name, content) if tag_length_form == "short": source = source[1:] else: source = source[2:] name = "value" name = "%s value" % tag attrs = source[0:tag_length] if tag_desc == "oid": try: content = PCA_GenLib.getOctString(attrs) self.app_context = PCA_TCAPParameters.app_context[ content] Msg = "app_context = %s" % self.app_context PCA_GenLib.WriteLog(Msg, 3) self.DebugStr = "%s,<application>=<%s>" % ( self.DebugStr, self.app_context) content = self.app_context except: Msg = "undef ctx %s" % PCA_GenLib.getOctString(attrs) PCA_GenLib.WriteLog(Msg, 3) content = "undef ctx %s" % PCA_GenLib.getOctString( attrs) elif tag_desc == "tcap_begin": self.Is_TCAP_begin = 1 content = PCA_GenLib.getHexString(attrs) elif tag_desc == "tcap_end": self.Is_TCAP_begin = 0 content = PCA_GenLib.getHexString(attrs) else: content = PCA_GenLib.getHexString(attrs) if Tag_Type == 'Constructor': if tag_desc == "component_portion": Msg = "GSM 0340 layer , not parsing now" PCA_GenLib.WriteLog(Msg, 3) #################################################### # Parsing MAP #################################################### Msg = "MAP application data =<\n%s\n>" % PCA_GenLib.HexDump( attrs) PCA_GenLib.WriteLog(Msg, 3) dll_file_name = self.app_context Msg = "dll_file_name = <%s>" % dll_file_name PCA_GenLib.WriteLog(Msg, 3) #dll_file_name = "PCA_MAPParser" Script_File = PCA_DLL.DLL(dll_file_name) factory_function = "Parser" factory_component = Script_File.symbol( factory_function) parser = factory_component() Script_File = PCA_DLL.DLL(dll_file_name) factory_function = "Handler" factory_component = Script_File.symbol( factory_function) handler = factory_component() parser.setContentHandler(handler) parser.parse(attrs, self.Is_TCAP_begin, self.app_context) response_message = handler.getHandlerResponse() self.set_handler('map_msg_dict', chr(0x00), response_message) response_ServerID = handler.getTID() response_DebugStr = handler.getDebugStr() self.DebugStr = "%s,<MAP MSG>=%s" % (self.DebugStr, response_DebugStr) else: self.parseTLV(attrs) self.set_handler(tag, attrs, content) else: self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, name, content) self.set_handler(tag, attrs, content) try: source = source[tag_length:] except IndexError: Msg = "parseTLV index error : <%s>,<%s>,name=<%s> " % ( sys.exc_type, sys.exc_value, name) PCA_GenLib.WriteLog(Msg, 0) break Msg = "parseTLV Ok " PCA_GenLib.WriteLog(Msg, 9) except: Msg = "parseTLV error : <%s>,<%s>,name=<%s> " % ( sys.exc_type, sys.exc_value, name) PCA_GenLib.WriteLog(Msg, 0)
def parseTLV(self,data): try: Msg = "parseTLV Init " PCA_GenLib.WriteLog(Msg,9) source = data tlv_desc = 'na' tlv_type = 'na' name = 'na' number_of_tlv = 0 #Msg = "MAP parseTLV data =\n%s" % PCA_GenLib.HexDump(source) #PCA_GenLib.WriteLog(Msg,0) tag = "" while len(source) > 0: number_of_tlv = number_of_tlv + 1 if number_of_tlv > 100: Msg = "number of TLV > 100 " PCA_GenLib.WriteLog(Msg,0) break self.tag_index = self.tag_index + 1 name = "Tag" attrs = source[0] tag_desc = "na" try: tag_desc = PCA_MAPParameters.Tag_Desc[attrs] content = tag_desc if content == "invoke_id": if self.invoke_id == 1: tag_desc = "opCode" content = tag_desc self.invoke_id = 2 else: self.invoke_id = 1 else: content = tag_desc except: content = "undef:%s" % PCA_GenLib.getHexString(attrs) #tag = content #tag = "%s:%s" % (content,PCA_GenLib.getHexString(attrs)) tag = "%s" % content #self.set_handler(name,chr(0x00),content) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) tag_class = ord(attrs) & 0xc0 tag_class = tag_class >> 6 Tag_Type = 'Primitive' if (ord(attrs) & 0x20): attrs = source[0:2] content = PCA_GenLib.getHexString(attrs) Tag_Type = 'Constructor' else: content = ord(attrs) Tag_Type = 'Primitive' name = "tag type" content = Tag_Type Tag_form = "Extended format" if (ord(source[0]) & 0x1f) == 0x1f: Tag_form = "Extended format" source = source[2:] #source = source[1:] else: Tag_form = "One octet format" source = source[1:] name = "tag form" content = Tag_form #self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) name = "length" name = "%s length" % tag attrs = source[0] content = ord(attrs) tag_length_form = "short" if content & 0x80: tag_length_form = "long" long_tag_length = chr(content & 0x7F) + source[1] content = struct.unpack("!H",long_tag_length)[0] tag_length = content #tag_length = struct.unpack("!B" else: tag_length_form = "short" content = struct.unpack("!B",attrs)[0] tag_length = content #self.set_handler(name,attrs,content) name = "%s %s" % (tag_length_form,name) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) if tag_length_form == "short": source = source[1:] else: source = source[2:] name = "value" name = "%s value" % tag attrs = source[0:tag_length] # OpCode if self.invoke_id == 2 and tag_desc == "opCode": content = ord(attrs) content = PCA_MAPParameters.op_code[content] elif tag_desc == "Originator_address" or tag_desc == "SC_Address" or tag_desc == "msisdn": if tag_desc == "msisdn": digit = PCA_GenLib.getHexBCDString(attrs) content = "%s" % digit[0:15] else: TOA = PCA_GenLib.getHexString(attrs[0]) digit = PCA_GenLib.getHexBCDString(attrs[1:]) content = "%s:%s" % (TOA,digit) else: content = PCA_GenLib.getHexString(attrs) #self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) #self.set_handler(tag,attrs,content) if Tag_Type == 'Constructor': self.parseTLV(attrs) # DEBUG ONLY self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) self.set_handler(tag,attrs,content) elif tag_desc == "SM_RP_UI" : if self.Is_TCAP_begin == 1: self.parseGSM0340_request(attrs) else: if self.app_context == "shortMsgMT_Relay_v3": # MT response self.parseGSM0340_MTresponse(attrs) else: self.parseGSM0340_response(attrs) else: self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) self.set_handler(tag,attrs,content) try: source = source[tag_length:] except IndexError: Msg = "parseTLV index error : <%s>,<%s>,name=<%s> " % (sys.exc_type,sys.exc_value,name) PCA_GenLib.WriteLog(Msg,0) break Msg = "parseTLV Ok " PCA_GenLib.WriteLog(Msg,9) except: Msg = "parseTLV error : <%s>,<%s>,name=<%s> " % (sys.exc_type,sys.exc_value,name) PCA_GenLib.WriteLog(Msg,0)
def getMessage(self, map_type, parameter_list): try: MAP_Tag = chr(0xa2) #if parameter_list["TCAP oid 1"][0] == "shortMsgMO_Relay_v3": if map_type == "MO-FSM-Ack": Msg = "construct MAP_MO_FSM Ack" PCA_GenLib.WriteLog(Msg, 2) MAP_Tag = chr(0xa2) udhi = chr(0x01) tp_parameter_indicator = chr(0x00) CurrentSeconds = time.time() date_tuple = time.localtime(CurrentSeconds) YY = "%04d" % date_tuple[0] YY = YY[2:4] MM = "%02d" % date_tuple[1] DD = "%02d" % date_tuple[2] HH = "%02d" % date_tuple[3] MI = "%02d" % date_tuple[4] SS = "%02d" % date_tuple[5] YYYYMMDD1 = YY + MM + DD YYYYMMDD = PCA_GenLib.converStringToReverseBCD(YYYYMMDD1) HHMISS1 = HH + MI + SS HHMISS = PCA_GenLib.converStringToReverseBCD(HHMISS1) TimeZone = chr(0x23) tp_sc_timestamp = YYYYMMDD + HHMISS + TimeZone SM_RP_UI_data_parameter = udhi + tp_parameter_indicator + tp_sc_timestamp tag = chr(0x04) tag_data = SM_RP_UI_data_parameter SM_RP_UI_data = self.constructTLV(tag, tag_data) tag = chr(0x30) tag_data = SM_RP_UI_data SM_RP_UI = self.constructTLV(tag, tag_data) tag = chr(0x02) tag_data = parameter_list["MAP opCode"][1] opCode = self.constructTLV(tag, tag_data) tag = chr(0x30) tag_data = opCode + SM_RP_UI result_tretres = self.constructTLV(tag, tag_data) tag = chr(0x02) tag_data = chr(0x02) invoke_id = self.constructTLV(tag, tag_data) map_data = invoke_id + result_tretres elif map_type == "SRI-SM": Msg = "construct SRI-SM request" PCA_GenLib.WriteLog(Msg, 2) MAP_Tag = chr(0xa1) noa = chr(0x91) #digits = chr(0x88)+chr(0x96)+chr(0x62)+chr(0x05)+chr(0x40)+chr(0x00) digits = PCA_GenLib.converStringToReverseBCD(self.sc_address) tag = chr(0x82) tag_data = noa + digits sc_address = self.constructTLV(tag, tag_data) tag = chr(0x81) tag_data = chr(0x01) SM_RP_PRI = self.constructTLV(tag, tag_data) noa = chr(0x91) digits = PCA_GenLib.converStringToReverseBCD( parameter_list["recipient"]) tag = chr(0x80) tag_data = noa + digits msisdn = self.constructTLV(tag, tag_data) tag = chr(0x30) tag_data = msisdn + SM_RP_PRI + sc_address address_info = self.constructTLV(tag, tag_data) tag = chr(0x02) tag_data = chr(0x2d) opCode = self.constructTLV(tag, tag_data) tag = chr(0x02) tag_data = chr(0x02) invoke_id = self.constructTLV(tag, tag_data) map_data = invoke_id + opCode + address_info elif map_type == "MT-FSM": Msg = "construct MT-FSM request" PCA_GenLib.WriteLog(Msg, 2) MAP_Tag = chr(0xa1) TP_RP = chr(0x04) digits = PCA_GenLib.converStringToReverseBCD( parameter_list["originator"]) address_len = struct.pack("!B", len(digits) * 2) toa = chr(0x91) TP_OA = address_len + toa + digits TP_PID = chr(0x00) TP_DCS = chr(0x00) CurrentSeconds = time.time() date_tuple = time.localtime(CurrentSeconds) YY = "%04d" % date_tuple[0] YY = YY[2:4] MM = "%02d" % date_tuple[1] DD = "%02d" % date_tuple[2] HH = "%02d" % date_tuple[3] MI = "%02d" % date_tuple[4] SS = "%02d" % date_tuple[5] YYYYMMDD1 = YY + MM + DD YYYYMMDD = PCA_GenLib.converStringToReverseBCD(YYYYMMDD1) HHMISS1 = HH + MI + SS HHMISS = PCA_GenLib.converStringToReverseBCD(HHMISS1) TP_SC_timestamp = YYYYMMDD + HHMISS + chr(0x23) #TP_user_data = "abc123" #Msg = "##############################################################" #PCA_GenLib.WriteLog(Msg,1) #for key in sorted(parameter_list): # Msg = "<%s>=<%s>,hex=<%s>*" % (key,parameter_list[key][0],PCA_GenLib.getHexString(parameter_list[key][1])) # PCA_GenLib.WriteLog(Msg,3) TP_user_data = parameter_list["sms_text"] TP_user_data_length = struct.pack("!B", len(TP_user_data)) tag = chr(0x04) tag_data = TP_RP + TP_OA + TP_PID + TP_DCS + TP_SC_timestamp + TP_user_data_length + TP_user_data SM_RP_PRI = self.constructTLV(tag, tag_data) noa = chr(0x91) digits = PCA_GenLib.converStringToReverseBCD(self.sc_address) tag = chr(0x84) tag_data = noa + digits sc_address = self.constructTLV(tag, tag_data) #noa = chr(0x91) digits = PCA_GenLib.converStringToReverseBCD( parameter_list["recipient_imsi"]) tag = chr(0x80) #tag_data = noa + digits tag_data = digits imsi = self.constructTLV(tag, tag_data) tag = chr(0x30) tag_data = imsi + sc_address + SM_RP_PRI address_info = self.constructTLV(tag, tag_data) tag = chr(0x02) tag_data = chr(0x2c) opCode = self.constructTLV(tag, tag_data) tag = chr(0x02) tag_data = chr(0x02) invoke_id = self.constructTLV(tag, tag_data) map_data = invoke_id + opCode + address_info else: MAP_Tag = chr(0xa3) tag = chr(0x02) tag_data = chr(0x24) result_tretres = self.constructTLV(tag, tag_data) tag = chr(0x02) tag_data = chr(0x02) invoke_id = self.constructTLV(tag, tag_data) map_data = invoke_id + result_tretres message_length = len(map_data) message_length_hex = struct.pack("!b", message_length) map_message = MAP_Tag + message_length_hex + map_data self.Message = map_message #Msg = "DEBUG MAP = *\n%s\n*" % PCA_GenLib.HexDump(self.Message) #PCA_GenLib.WriteLog(Msg,0) return self.Message except: Msg = "getMessage Error :<%s>,<%s>" % (sys.exc_type, sys.exc_value) PCA_GenLib.WriteLog(Msg, 0) raise
def dispatcher(self,TimeOut): try: Msg = "dispatcher " PCA_GenLib.WriteLog(Msg,9) while 1: readables, writeables, exceptions = select.select(self.ReadSet,[], [],TimeOut) for self.SocketConnection in readables: ################################## #### for ready input sockets ##### ################################## if self.SocketConnection in self.SocketConnectionPool: #################################### ## port socket: accept new client ## ## accept should not block ## #################################### connection, address = self.SocketConnection.accept() Msg = 'Dispatcher New Connection <%s> from :%s' % (id(connection),address) # connection is a new socket PCA_GenLib.WriteLog(Msg,1) self.ReadSet.append(connection) # add to select list, wait else: try: ################################################## ## Check the first request message from client ## ################################################## if not self.ConnectionLoginState.has_key(id(self.SocketConnection)): self.ConnectionLoginState[id(self.SocketConnection)] = 'N' Msg = "Set ConnectionLoginState <%s> to N " % id(self.SocketConnection) PCA_GenLib.WriteLog(Msg,2) ClientMessage = self.SocketConnection.recv(2048) if not ClientMessage: self.SocketConnection.close() # close here and remv from self.ReadSet.remove(self.SocketConnection) # del list else reselected Msg = "Del ConnectionLoginState <%s>" % id(self.SocketConnection) PCA_GenLib.WriteLog(Msg,1) del self.ConnectionLoginState[id(self.SocketConnection)] Msg = "Client Close Connection ....address = ('%s',%s)" % (address[0],address[1]) PCA_GenLib.WriteLog(Msg,1) else: ClientMessage = "server first echo : %s" % ClientMessage self.sendDataToSocket(self.SocketConnection,ClientMessage) else: Message = self.readDataFromSocket(self.SocketConnection,Length=1024,TimeOut = 1.0) if Message != None: Message = "server rest echo : %s" % Message self.sendDataToSocket(self.SocketConnection,Message) except socket.error: Msg = "Dispatcher error : <%s>,<%s> " % (sys.exc_type,sys.exc_value) PCA_GenLib.WriteLog(Msg,0) self.SocketConnection.close() # close here and remv from self.ReadSet.remove(self.SocketConnection) # del list else reselected del self.ConnectionLoginState[id(self.SocketConnection)] Msg = "dispatcher OK" PCA_GenLib.WriteLog(Msg,9) except: Msg = "dispatcher error : <%s>,<%s> " % (sys.exc_type,sys.exc_value) PCA_GenLib.WriteLog(Msg,0) self.close() raise
def getHandlerResponse(self): try: Msg = "getHandlerResponse Init " PCA_GenLib.WriteLog(Msg,9) tag = chr(0x02) tag_data = self.MAP_Message["MAP invoke_id"][1] invoke_id = self.constructTLV(tag,tag_data) tag = chr(0x02) tag_data = self.MAP_Message["MAP opCode"][1] opCode = self.constructTLV(tag,tag_data) if self.MT_FSM_ERROR == "N": # Success MAP_Tag = chr(0xa2) tag = chr(0x04) tag_data = chr(0x00)+chr(0x00) SM_RP_UI_data = self.constructTLV(tag,tag_data) tag = chr(0x30) tag_data = SM_RP_UI_data SM_RP_UI = self.constructTLV(tag,tag_data) tag = chr(0x30) tag_data = opCode + SM_RP_UI result_tretres = self.constructTLV(tag,tag_data) else: # Failure MAP_Tag = chr(0xa3) tag = chr(0x02) #tag_data = chr(0x1b) tag_data = chr(0x06) #T_ABSENT_DETACHED error_code = self.constructTLV(tag,tag_data) tag = chr(0x30) #tag_data = chr(0x02) + chr(0x01) + chr(0x00) # absent_sub_diag_SM : 0 - no paging response via the MSC tag_data = chr(0x02) + chr(0x01) + chr(0x01) # absent_sub_diag_SM : 1 - IMSI Detatch #tag_data = chr(0x02) + chr(0x01) + chr(0x03) # absent_sub_diag_SM : deregistered in the HLR for non GPRS #tag_data = chr(0x02) + chr(0x01) + chr(0x07) #7 - deregistered in the HLR for GPRS SM_RP_UI = self.constructTLV(tag,tag_data) #tag = chr(0x30) #tag_data = opCode + error_code + SM_RP_UI #result_tretres = self.constructTLV(tag,tag_data) result_tretres = error_code + SM_RP_UI #Msg = "Sleep 70 seconds" #PCA_GenLib.WriteLog(Msg,1) #time.sleep(70) map_data = invoke_id + result_tretres message_length = len(map_data) message_length_hex = struct.pack("!b",message_length) map_message = MAP_Tag + message_length_hex + map_data self.Message = map_message Msg = "getHandlerResponse OK" PCA_GenLib.WriteLog(Msg,9) return self.Message except: Msg = "getHandlerResponse error : <%s>,<%s> " % (sys.exc_type,sys.exc_value) PCA_GenLib.WriteLog(Msg,0) raise
def parse(self, source): try: Msg = "parser init" PCA_GenLib.WriteLog(Msg, 9) self.SOURCD_ID = 'HeartBeat' self.DebugStr = ' ' orig_data = source name = 'none' self.StartParsing = 1 start_pos = string.find(source, chr(0x00)) system_id = source[0:start_pos] self.DebugStr = "system_id = <%s>" % system_id name = "system_id" attrs = system_id content = attrs self.set_handler(name, attrs, content) source = source[start_pos + 1:] start_pos = string.find(source, chr(0x00)) password = source[0:start_pos] self.DebugStr = "%s , password = <%s>" % (self.DebugStr, password) source = source[start_pos + 1:] start_pos = string.find(source, chr(0x00)) system_type = source[0:start_pos] self.DebugStr = "%s , system_type = <%s>" % (self.DebugStr, system_type) source = source[start_pos + 1:] interface_version = struct.unpack("!b", source[0])[0] self.DebugStr = "%s , interface_version = <%s>" % ( self.DebugStr, interface_version) source = source[1:] ton = struct.unpack("!b", source[0])[0] self.DebugStr = "%s , ton = <%s>" % (self.DebugStr, ton) source = source[1:] npi = struct.unpack("!b", source[0])[0] self.DebugStr = "%s , npi = <%s>" % (self.DebugStr, npi) source = source[1:] start_pos = string.find(source, chr(0x00)) address_range = source[0:start_pos] name = "address_range" attrs = address_range content = attrs self.set_handler(name, attrs, content) self.DebugStr = "%s , address_range = <%s>" % (self.DebugStr, address_range) if self.StartParsing == 1: self._cont_handler.endDocument(self.DebugStr, self.TID, self.SOURCD_ID) Msg = "parser OK" PCA_GenLib.WriteLog(Msg, 9) except: Msg = "parser :<%s>,<%s>,name=<%s>" % (sys.exc_type, sys.exc_value, name) PCA_GenLib.WriteLog(Msg, 0) Msg = "orig data =\n%s" % PCA_GenLib.HexDump(orig_data) PCA_GenLib.WriteLog(Msg, 0) Msg = "rest data =\n%s" % PCA_GenLib.HexDump(source) PCA_GenLib.WriteLog(Msg, 0) if self.StartParsing == 1: self._cont_handler.endDocument(self.DebugStr, self.TID, self.SOURCD_ID) return
def getSystem_ID(self): Msg = "bind tranceiver system_id = %s" % self.system_id PCA_GenLib.WriteLog(Msg, 0) return self.system_id
def getMessage(self,map_type,parameter_list): try: # #<TCAP tcap_begin:62>=<4804010000006b1a281806 ...... # <TCAP Originating TID:48>=<01000000>* # <TCAP dialog_portion:6b>=<2818060700118605010101a00d600ba109060704000001001503>* # # <TCAP external_tag:28>=<060700118605010101a00d600ba109060704000001001503>* # <TCAP oid:06>=<00118605010101>* # <TCAP Single_ASN1_Type:a0>=<600ba109060704000001001503>* # <TCAP dialog_request:60>=<a109060704000001001503>* # <TCAP Application_Context:a1>=<060704000001001503>* # <TCAP Application_Context_name:06 >=<04000001001503>* # <MAP component_type_invoke:a1>=<0201020.......82009099041411000c918896820000200000020661f138269b01> # <MAP invoke_id:02>=<02> # <MAP opCode:02>=<mo-ForwardSM> if map_type == "MO-FSM-Ack" or map_type == "SRI-SM-Ack" or map_type == "MT-FSM-Ack" or map_type == "TCAP-Begin" or map_type == "MT-FSM-Ack-continue" or map_type == "reportSM-DeliveryStatus-Ack" or map_type == "MO-FSM-segment": TCAP_Tag = chr(0x64) ############################################### # Transaction Portion ############################################### if map_type == "TCAP-Begin" or map_type == "MT-FSM-Ack-continue": TCAP_Tag = chr(0x65) # tcap continue tag tag = chr(0x48) if map_type == "TCAP-Begin" : tag_data = struct.pack("!I",self.tcap_tid) self.tcap_tid = self.tcap_tid + 1 else: tag_data = parameter_list["TCAP Destination TID"][1] transaction_portion = self.constructTLV(tag,tag_data) tag = chr(0x49) tag_data = parameter_list["TCAP Originating TID"][1] transaction_portion_d = self.constructTLV(tag,tag_data) transaction_portion = transaction_portion + transaction_portion_d elif map_type == "MO-FSM-segment": total_segment = parameter_list['total_segment'][0] current_segment = parameter_list['current_segment'][0] if current_segment != total_segment: TCAP_Tag = chr(0x65)+chr(0x81) # tcap continue tag tag = chr(0x48) tag_data = parameter_list["TCAP Destination TID"][1] transaction_portion = self.constructTLV(tag,tag_data) tag = chr(0x49) tag_data = parameter_list["TCAP Originating TID"][1] transaction_portion_d = self.constructTLV(tag,tag_data) transaction_portion = transaction_portion + transaction_portion_d else: # Final segment should be a tcap begin TCAP_Tag = chr(0x62)+chr(0x81) tag = chr(0x48) tag_data = struct.pack("!I",self.tcap_tid) self.tcap_tid = self.tcap_tid + 1 transaction_portion = self.constructTLV(tag,tag_data) else: tag = chr(0x49) tag_data = parameter_list["TCAP Originating TID"][1] transaction_portion = self.constructTLV(tag,tag_data) ############################################### # Dialog Portion ############################################### if map_type != "MT-FSM-Ack-continue": if map_type == "MO-FSM-segment": Application_Context_name = chr(0x04)+chr(0x00)+chr(0x00)+chr(0x01)+chr(0x00)+chr(0x15)+chr(0x03) Application_Context_name_Tag = chr(0x06) Application_Context = self.constructTLV(Application_Context_name_Tag,Application_Context_name) if map_type == "MO-FSM-segment": Application_Context_Tag = chr(0xa1) + chr(0x81) else: Application_Context_Tag = chr(0xa1) Application_Context_TLV = self.constructTLV(Application_Context_Tag,Application_Context) else: try: Application_Context_name = parameter_list["TCAP oid 1"][1] Application_Context_name_Tag = chr(0x06) Application_Context = self.constructTLV(Application_Context_name_Tag,Application_Context_name) Application_Context_Tag = chr(0xa1) Application_Context_TLV = self.constructTLV(Application_Context_Tag,Application_Context) except: Msg = "no TCAP OID 1 MAP v1 ?" PCA_GenLib.WriteLog(Msg,1) Application_Context_TLV = '' app_result_user_diag_tag = chr(0x02) app_result_user_diag_value = chr(0x00) app_result_user_diag = self.constructTLV(app_result_user_diag_tag,app_result_user_diag_value) result_user_diag_tag = chr(0xa1) result_user_diag_TLV = self.constructTLV(result_user_diag_tag,app_result_user_diag) result_source_diag_tag = chr(0xa3) result_source_diag_TLV = self.constructTLV(result_source_diag_tag,result_user_diag_TLV) app_result_tag = chr(0x02) app_result_value = chr(0x00) app_result = self.constructTLV(app_result_tag,app_result_value) result_tag = chr(0xa2) result_TLV = self.constructTLV(result_tag,app_result) if map_type != "MT-FSM-Ack-continue": dialog_response = Application_Context_TLV+result_TLV+result_source_diag_TLV if map_type == "MO-FSM-segment": dialog_request = Application_Context_TLV dialog_request_tag = chr(0x60) dialog_request_TLV = self.constructTLV(dialog_request_tag,dialog_request) Single_ASN1_Tag = chr(0xa0) Single_ASN1_TLV = self.constructTLV(Single_ASN1_Tag,dialog_request_TLV) oid_tag = chr(0x06) oid_data = chr(0x00)+chr(0x11)+chr(0x86)+chr(0x05)+chr(0x01)+chr(0x01)+chr(0x01) oid_tlv = self.constructTLV(oid_tag,oid_data) tcap_external = oid_tlv + Single_ASN1_TLV tcap_external_tag = chr(0x28) tcap_external_tlv = self.constructTLV(tcap_external_tag,tcap_external) dialog_portion_tag = chr(0x6b) dialog_portion = self.constructTLV(dialog_portion_tag,tcap_external_tlv) else: dialog_response_tag = chr(0x61) dialog_response_TLV = self.constructTLV(dialog_response_tag,dialog_response) Single_ASN1_Tag = chr(0xa0) Single_ASN1_TLV = self.constructTLV(Single_ASN1_Tag,dialog_response_TLV) oid_tag = chr(0x06) try: oid_data = parameter_list["TCAP oid"][1] oid_tlv = self.constructTLV(oid_tag,oid_data) tcap_external = oid_tlv + Single_ASN1_TLV tcap_external_tag = chr(0x28) tcap_external_tlv = self.constructTLV(tcap_external_tag,tcap_external) dialog_portion_tag = chr(0x6b) dialog_portion = self.constructTLV(dialog_portion_tag,tcap_external_tlv) except: Msg = "no TCAP OID MAP v1 ?" PCA_GenLib.WriteLog(Msg,1) dialog_portion = '' elif map_type == "SRI-SM": TCAP_Tag = chr(0x62) ############################################### # Transaction Portion ############################################### tag = chr(0x48) tag_data = struct.pack("!I",self.tcap_tid) self.tcap_tid = self.tcap_tid + 1 transaction_portion = self.constructTLV(tag,tag_data) ############################################### # Dialog Portion ############################################### Application_Context_name = chr(0x04)+chr(0x00)+chr(0x00)+chr(0x01)+chr(0x00)+chr(0x14)+chr(0x03) Application_Context_name_Tag = chr(0x06) Application_Context = self.constructTLV(Application_Context_name_Tag,Application_Context_name) Application_Context_Tag = chr(0xa1) Application_Context_TLV = self.constructTLV(Application_Context_Tag,Application_Context) dialog_request = Application_Context_TLV dialog_request_tag = chr(0x60) dialog_request_TLV = self.constructTLV(dialog_request_tag,dialog_request) Single_ASN1_Tag = chr(0xa0) Single_ASN1_TLV = self.constructTLV(Single_ASN1_Tag,dialog_request_TLV) oid_tag = chr(0x06) oid_data = chr(0x00)+chr(0x11)+chr(0x86)+chr(0x05)+chr(0x01)+chr(0x01)+chr(0x01) oid_tlv = self.constructTLV(oid_tag,oid_data) tcap_external = oid_tlv + Single_ASN1_TLV tcap_external_tag = chr(0x28) tcap_external_tlv = self.constructTLV(tcap_external_tag,tcap_external) dialog_portion_tag = chr(0x6b) dialog_portion = self.constructTLV(dialog_portion_tag,tcap_external_tlv) elif map_type == "MT-FSM" or map_type == "MO-FSM" or map_type == "alertSC": TCAP_Tag = chr(0x62) ############################################### # Transaction Portion ############################################### tag = chr(0x48) tag_data = struct.pack("!I",self.tcap_tid) self.tcap_tid = self.tcap_tid + 1 transaction_portion = self.constructTLV(tag,tag_data) ############################################### # Dialog Portion ############################################### if map_type == "MT-FSM" : Application_Context_name = chr(0x04)+chr(0x00)+chr(0x00)+chr(0x01)+chr(0x00)+chr(0x19)+chr(0x03) elif map_type == "alertSC": Application_Context_name = chr(0x04)+chr(0x00)+chr(0x00)+chr(0x01)+chr(0x00)+chr(0x17)+chr(0x02) else: Application_Context_name = chr(0x04)+chr(0x00)+chr(0x00)+chr(0x01)+chr(0x00)+chr(0x15)+chr(0x03) Application_Context_name_Tag = chr(0x06) Application_Context = self.constructTLV(Application_Context_name_Tag,Application_Context_name) Application_Context_Tag = chr(0xa1) Application_Context_TLV = self.constructTLV(Application_Context_Tag,Application_Context) dialog_request = Application_Context_TLV dialog_request_tag = chr(0x60) dialog_request_TLV = self.constructTLV(dialog_request_tag,dialog_request) Single_ASN1_Tag = chr(0xa0) Single_ASN1_TLV = self.constructTLV(Single_ASN1_Tag,dialog_request_TLV) oid_tag = chr(0x06) oid_data = chr(0x00)+chr(0x11)+chr(0x86)+chr(0x05)+chr(0x01)+chr(0x01)+chr(0x01) oid_tlv = self.constructTLV(oid_tag,oid_data) tcap_external = oid_tlv + Single_ASN1_TLV tcap_external_tag = chr(0x28) tcap_external_tlv = self.constructTLV(tcap_external_tag,tcap_external) dialog_portion_tag = chr(0x6b) dialog_portion = self.constructTLV(dialog_portion_tag,tcap_external_tlv) elif map_type == "MO-FSM-Begin": TCAP_Tag = chr(0x62) ############################################### # Transaction Portion ############################################### tag = chr(0x48) tag_data = struct.pack("!I",self.tcap_tid) self.tcap_tid = self.tcap_tid + 1 transaction_portion = self.constructTLV(tag,tag_data) ############################################### # Dialog Portion ############################################### Application_Context_name = chr(0x04)+chr(0x00)+chr(0x00)+chr(0x01)+chr(0x00)+chr(0x15)+chr(0x03) Application_Context_name_Tag = chr(0x06) Application_Context = self.constructTLV(Application_Context_name_Tag,Application_Context_name) Application_Context_Tag = chr(0xa1) Application_Context_TLV = self.constructTLV(Application_Context_Tag,Application_Context) ## This looks strange , check later padding = chr(0x80)+chr(0x02)+chr(0x07) protocol_version = chr(0x80) dialog_request_tag = chr(0x60) dialog_request = padding + protocol_version + Application_Context_TLV dialog_request_TLV = self.constructTLV(dialog_request_tag,dialog_request) Single_ASN1_Tag = chr(0xa0) Single_ASN1_TLV = self.constructTLV(Single_ASN1_Tag,dialog_request_TLV) oid_tag = chr(0x06) oid_data = chr(0x00)+chr(0x11)+chr(0x86)+chr(0x05)+chr(0x01)+chr(0x01)+chr(0x01) oid_tlv = self.constructTLV(oid_tag,oid_data) tcap_external = oid_tlv + Single_ASN1_TLV tcap_external_tag = chr(0x28) tcap_external_tlv = self.constructTLV(tcap_external_tag,tcap_external) dialog_portion_tag = chr(0x6b) dialog_portion = self.constructTLV(dialog_portion_tag,tcap_external_tlv) else: Msg = "unknow map_type = %s ignore response" % map_type PCA_GenLib.WriteLog(Msg,0) ############################################### # Component Portion -- MAP ############################################### if map_type == "TCAP-Begin" or map_type == "MO-FSM-Begin": tcap_data = transaction_portion + dialog_portion else: if map_type == "MO-FSM-segment": tag = chr(0x6c) + chr(0x81) else: tag = chr(0x6c) tag_data = self.MAPMessage.getMessage(map_type,parameter_list) component_portion = self.constructTLV(tag,tag_data) if map_type == "MT-FSM-Ack-continue": tcap_data = transaction_portion + component_portion elif map_type == "MO-FSM-segment": total_segment = parameter_list['total_segment'][0] current_segment = parameter_list['current_segment'][0] if current_segment != total_segment: tcap_data = transaction_portion + component_portion else: tcap_data = transaction_portion + dialog_portion + component_portion else: tcap_data = transaction_portion + dialog_portion + component_portion message_length = len(tcap_data) message_length_hex = struct.pack("!B",message_length) tcap_message = TCAP_Tag + message_length_hex + tcap_data self.Message = tcap_message #Msg = "DEBUG TCAP = *\n%s\n*" % PCA_GenLib.HexDump(self.Message) #PCA_GenLib.WriteLog(Msg,1) return self.Message except: Msg = "getMessage Error :<%s>,<%s>" % (sys.exc_type,sys.exc_value) PCA_GenLib.WriteLog(Msg,0) raise
def dispatcher(self,TimeOut): try: Msg = "dispatcher " PCA_GenLib.WriteLog(Msg,9) while 1: readables, writeables, exceptions = select.select(self.ReadSet,[], [],TimeOut) for self.SocketConnection in readables: ################################## #### for ready input sockets ##### ################################## if self.SocketConnection in self.SocketConnectionPool: #################################### ## port socket: accept new client ## ## accept should not block ## #################################### connection, address = self.SocketConnection.accept() Msg = 'Dispatcher New Connection <%s> from :%s' % (id(connection),address) # connection is a new socket PCA_GenLib.WriteLog(Msg,1) self.ReadSet.append(connection) # add to select list, wait else: try: ################################################## ## Check the first request message from client ## ################################################## if not self.ConnectionLoginState.has_key(id(self.SocketConnection)): self.ConnectionLoginState[id(self.SocketConnection)] = 'N' Msg = "Set ConnectionLoginState <%s> to N " % id(self.SocketConnection) PCA_GenLib.WriteLog(Msg,1) ClientMessage = self.SocketConnection.recv(2048) if not ClientMessage: self.SocketConnection.close() # close here and remv from self.ReadSet.remove(self.SocketConnection) # del list else reselected Msg = "Del ConnectionLoginState <%s>" % id(self.SocketConnection) PCA_GenLib.WriteLog(Msg,1) del self.ConnectionLoginState[id(self.SocketConnection)] Msg = "Client Close Connection ....address = ('%s',%s)" % (address[0],address[1]) PCA_GenLib.WriteLog(Msg,1) else: self.handle_event(self.SocketConnection,ClientMessage) except socket.error: Msg = "Dispatcher error : <%s>,<%s> " % (sys.exc_type,sys.exc_value) PCA_GenLib.WriteLog(Msg,0) self.SocketConnection.close() # close here and remv from self.ReadSet.remove(self.SocketConnection) # del list else reselected del self.ConnectionLoginState[id(self.SocketConnection)] Msg = "check external command" PCA_GenLib.WriteLog(Msg,9) try: self.handle_cmd(self.SocketConnection,"/tmp/pca.cmd") except AttributeError: Msg = "association not established yet" PCA_GenLib.WriteLog(Msg,1) Msg = "dispatcher OK" PCA_GenLib.WriteLog(Msg,9) except: Msg = "dispatcher error : <%s>,<%s> " % (sys.exc_type,sys.exc_value) PCA_GenLib.WriteLog(Msg,0) self.close() raise
def __init__(self, XMLCFG): try: Msg = "Acceptor Init ..." PCA_GenLib.WriteLog(Msg, 9) self.XMLCFG = XMLCFG Tag = "LISTEN_HOST" host = PCA_XMLParser.GetXMLTagValue(XMLCFG, Tag) self.host = host Tag = "LISTEN_PORT" port = PCA_XMLParser.GetXMLTagValue(XMLCFG, Tag) self.port = string.atoi(port) Msg = "Listen Host=<%s>,Port=<%s>" % (self.host, self.port) PCA_GenLib.WriteLog(Msg, 1) numPortSocks = 1 # number of ports for client connects # make main sockets for accepting new client requests self.SocketConnectionPool, self.ReadSet, self.WriteSet = [], [], [] for i in range(numPortSocks): Msg = "Call Socket..." PCA_GenLib.WriteLog(Msg, 9) SocketDescriptor = socket.socket( socket.AF_INET, socket.SOCK_STREAM) # make a TCP/IP spocket object # you should add "setsockopt(level, optname, value)" here # /* Set SO_REUSEADDR socket option to allow socket reuse */ SocketDescriptor.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) Msg = "setsockopt..SO_REUSEADDR." PCA_GenLib.WriteLog(Msg, 9) # /* Set SO_KEEPALIVE socket option */ SocketDescriptor.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) Msg = "setsockopt...SO_KEEPALIVE" PCA_GenLib.WriteLog(Msg, 9) SocketDescriptor.bind( ('', self.port)) # bind it to server port number Msg = "setsockopt..bind port = <%s>." % self.port PCA_GenLib.WriteLog(Msg, 0) SocketDescriptor.listen(5) # listen, allow 5 pending connects Msg = "setsockopt..Listen.SocketFD=<%s>" % (SocketDescriptor) PCA_GenLib.WriteLog(Msg, 9) self.SocketConnectionPool.append( SocketDescriptor) # add to main list to identify self.ReadSet.append( SocketDescriptor) # add to select inputs list self.WriteSet.append( SocketDescriptor) # add to select output list Msg = "Acceptor Initial Ok " PCA_GenLib.WriteLog(Msg, 9) except: Msg = "Acceptor Initial error : <%s>,<%s> " % (sys.exc_type, sys.exc_value) PCA_GenLib.WriteLog(Msg, 0) raise
def handle_event(self,conn,Message): try: Msg = "handle_event init" PCA_GenLib.WriteLog(Msg,9) #Msg = "DEBUG con=<%s> msg = *\n%s\n*" % (id(conn),PCA_GenLib.HexDump(Message)) #PCA_GenLib.WriteLog(Msg,0) self.parser.parse(Message,"IN") response_message = self.handler.getHandlerResponse() ServerID = self.handler.getTID() DebugStr = self.handler.getDebugStr() ################################################ # will not display M3UA heartbeat message ################################################ if string.find(DebugStr,"BEAT") == -1: Msg = "----------------------------------------------------------------------------------" PCA_GenLib.WriteLog(Msg,1) Msg = "recv : %s*" % DebugStr PCA_GenLib.WriteLog(Msg,1) #ASP Up (ASPUP) or "ASP Active (ASPAC) or "Heartbeat (BEAT)" or Heartbeat Acknowledgement (BEAT ACK)" #if response_message != None and string.find(DebugStr,"tcap_end") == -1 and string.find(DebugStr,"tcap_continue") == -1: if string.find(DebugStr,"tcap_continue") != -1 and string.find(DebugStr,"shortMsgMO_Relay_v3") != -1: Msg = "tcap tcap_continue , ready send MO-segment message" PCA_GenLib.WriteLog(Msg,1) request_parameter_list = {} (orig_tid,dest_tid) = self.handler.getTCAP_ID() request_parameter_list["TCAP Originating TID"] = (orig_tid,orig_tid) request_parameter_list["TCAP Destination TID"] = (dest_tid,dest_tid) self.sendMO_segment(conn,request_parameter_list) elif response_message != None and string.find(DebugStr,"tcap_end") == -1 : Msg = "send = *\n%s\n*" % PCA_GenLib.HexDump(response_message) PCA_GenLib.WriteLog(Msg,2) self.parser.parse(response_message,"IN") #response_message_ = self.handler.getHandlerResponse() ServerID = self.handler.getTID() DebugStr = self.handler.getDebugStr() if string.find(DebugStr,"BEAT") == -1: Msg = "send : %s*" % DebugStr PCA_GenLib.WriteLog(Msg,1) self.sendDataToSocket(conn,response_message) elif g_current_segment < g_total_segment : Msg = "segment info current=<%s>,total=<%s> we may need to send segment msg" % (g_current_segment, g_total_segment) PCA_GenLib.WriteLog(Msg,1) request_parameter_list = {} (orig_tid,dest_tid) = self.handler.getTCAP_ID() request_parameter_list["TCAP Originating TID"] = (orig_tid,orig_tid) request_parameter_list["TCAP Destination TID"] = (dest_tid,dest_tid) self.sendMO_segment(conn,request_parameter_list) ################################################ # Send MO-FSM once link active ################################################ #if string.find(DebugStr,"ASPAC") != -1: #Msg = "send : %s*" % DebugStr #PCA_GenLib.WriteLog(Msg,1) #Msg = "send SSNM DUNA" #PCA_GenLib.WriteLog(Msg,1) #self.sendSSNM(conn,"DUNA") #Msg = "send SSNM " #PCA_GenLib.WriteLog(Msg,2) #self.sendSSNM(conn,"DAVA") #Msg = "send alertSC " #PCA_GenLib.WriteLog(Msg,1) #self.send_alertServiceCentre(conn) else: if string.find(DebugStr,"sendRoutingInfoForSM") != -1: Msg = "sri-sm response , ready send MT-FSM" PCA_GenLib.WriteLog(Msg,1) (imsi,NNN) = self.handler.getSRI_SM_resp() Msg = "sri-sm response ,imsi=<%s>,NNN=<%s>, ready send MT-FSM" % (imsi,NNN) PCA_GenLib.WriteLog(Msg,1) self.MT(conn,NNN,imsi) else: Msg = "tcap end or no response message , not response back ...." PCA_GenLib.WriteLog(Msg,1) Msg = "handle_event OK" PCA_GenLib.WriteLog(Msg,9) except: Msg = "handle_event Error :<%s>,<%s>" % (sys.exc_type,sys.exc_value) PCA_GenLib.WriteLog(Msg,0) raise
def dispatcher(self, TimeOut=0.2): try: #event loop: listen and multiplex until server process killed Msg = "dispatcher server starting" PCA_GenLib.WriteLog(Msg, 9) RecvDataCNT = 0 while 1: readables, writeables, exceptions = select( self.ReadSet, [], [], TimeOut) for self.SocketConnection in readables: ################################## #### for ready input sockets ##### ################################## if self.SocketConnection in self.SocketConnectionPool: #################################### ## port socket: accept new client ## ## accept should not block ## #################################### connection, address = self.SocketConnection.accept() Msg = 'Dispatcher New Connection <%s> from :%s' % ( id(connection), address ) # connection is a new socket PCA_GenLib.WriteLog(Msg, 1) self.ReadSet.append( connection) # add to select list, wait self.WriteSet.append( connection) # add to select list, wait self.ConnectionLoginState[id(connection)] = 'N' Msg = "Set ConnectionLoginState <%s> to N " % id( connection) PCA_GenLib.WriteLog(Msg, 1) else: try: RecvDataCNT = RecvDataCNT + 1 ClientMessage = self.SocketConnection.recv(1) if not ClientMessage: Msg = "Client Close Connection ..id=%s" % id( self.SocketConnection) PCA_GenLib.WriteLog(Msg, 1) self.SocketConnection.close( ) # close here and remv from self.ReadSet.remove( self.SocketConnection ) # del list else reselected self.WriteSet.remove( self.SocketConnection ) # del list else reselected Msg = "Del ConnectionLoginState <%s>" % id( self.SocketConnection) PCA_GenLib.WriteLog(Msg, 1) del self.ConnectionLoginState[id( self.SocketConnection)] else: ################################### ### Got Data Message From Client ## ################################### #Message = self.readDataFromSocket(ClientMessage) self.handle_event(self.SocketConnection, ClientMessage) except socket.error: Msg = "dispatcher socket error : <%s>,<%s> " % ( sys.exc_type, sys.exc_value) PCA_GenLib.WriteLog(Msg, 0) self.SocketConnection.close( ) # close here and remv from self.ReadSet.remove(self.SocketConnection ) # del list else reselected self.WriteSet.remove(self.SocketConnection ) # del list else reselected del self.ConnectionLoginState[id( self.SocketConnection)] break if len(readables) == 0 or RecvDataCNT >= 10: RecvDataCNT = 0 self.handle_timeout() except: Msg = "dispatcher error : <%s>,<%s> " % (sys.exc_type, sys.exc_value) PCA_GenLib.WriteLog(Msg, 0) raise
def parse(self, source, Is_TCAP_begin, app_context): try: Msg = "parser init" PCA_GenLib.WriteLog(Msg, 9) orig_data = source name = 'none' self.StartParsing = 0 TID = "na" content = "na" Msg = "MAP SRI-SM data =<\n%s\n>" % PCA_GenLib.HexDump(source) PCA_GenLib.WriteLog(Msg, 3) if (source != None): self._cont_handler.startDocument() self.StartParsing = 1 self.DebugStr = "" name = "MAP Tag" attrs = source[0] Tag_Type = 'Primitive' if (ord(attrs) & 0x40): name = "%s-Constructor" % name attrs = source[0:2] content = PCA_GenLib.getHexString(attrs) else: name = "%s-Primitive" % name content = ord(attrs) self.set_handler(name, attrs, content) if Tag_Type == 'Primitive': source = source[1:] else: source = source[2:] self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, name, content) name = "length" attrs = source[0] content = ord(attrs) tag_length = content self.set_handler(name, attrs, content) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, name, content) source = source[1:] name = "value" attrs = source[0:tag_length] content = PCA_GenLib.getHexString(attrs) self.set_handler(name, attrs, content) #self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) #self.parseTLV(attrs) source = attrs name = "invoke" tag_name = name attrs = source[0] content = ord(attrs) self.set_handler(name, attrs, content) source = source[1:] name = "length" attrs = source[0] content = ord(attrs) tag_length = content self.set_handler(name, attrs, content) #self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) source = source[1:] #name = "invoke value" name = "invoke_id" attrs = source[0] content = ord(attrs) tag_value = content self.set_handler(name, attrs, content) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, tag_name, tag_value) if self.StartParsing == 1: self._cont_handler.endDocument(orig_data, self.DebugStr) Msg = "parser OK" PCA_GenLib.WriteLog(Msg, 9) except: Msg = "parser :<%s>,<%s>,name=<%s>" % (sys.exc_type, sys.exc_value, name) PCA_GenLib.WriteLog(Msg, 1) Msg = "orig data =\n%s" % PCA_GenLib.HexDump(orig_data) PCA_GenLib.WriteLog(Msg, 1)
def parse(self, source): try: Msg = "parser init" PCA_GenLib.WriteLog(Msg,9) self.SOURCD_ID = 'HeartBeat' self.DebugStr = 'na' orig_data = source name = 'none' self.StartParsing = 0 if (source != None) and len(source) > 0: self._cont_handler.startDocument() self.StartParsing = 1 name = "command_length" attrs = source[0:4] content = struct.unpack("!i",attrs)[0] command_length = content self.set_handler(name,attrs,content) source = source[4:] name = "command_id" attrs = source[0:4] content = struct.unpack("!i",attrs)[0] command_id = 'na' try: command_id = PCA_SMPP_Parameter_Tag.command_id_dict[attrs] except: command_id = 'undef' Msg = " undef SMPP command id =\n%s" % PCA_GenLib.HexDump(attrs) PCA_GenLib.WriteLog(Msg,0) content = command_id self.set_handler(name,attrs,content) source = source[4:] name = "command_status" attrs = source[0:4] content = struct.unpack("!i",attrs)[0] command_status = content self.set_handler(name,attrs,content) source = source[4:] name = "sequence_number" attrs = source[0:4] content = struct.unpack("!I",attrs)[0] self.TID = content sequence_number = content self.set_handler(name,attrs,content) Msg = "REQ_TID=<%s>,command_length=<%s>,command_id=<%s>,command_status=<%s>,sequence_number=<%s>" % (self.TID,command_length,command_id,command_status,sequence_number) #PCA_GenLib.WriteLog(Msg,2) self.DebugStr = Msg source = source[4:] if command_id == "submit_sm": start_pos = string.find(source,chr(0x00)) attrs = source[0:start_pos] name = "service_type" content = attrs self.set_handler(name,attrs,content) self.DebugStr = "%s , %s = <%s>" % (self.DebugStr,name,content) source = source[start_pos+1:] attrs = source[0] content = struct.unpack("!b",attrs)[0] name = "source_addr_ton" self.set_handler(name,attrs,content) self.DebugStr = "%s , %s = <%s>" % (self.DebugStr,name,content) source = source[1:] attrs = source[0] content = struct.unpack("!b",attrs)[0] name = "source_addr_npi" self.set_handler(name,attrs,content) self.DebugStr = "%s , %s = <%s>" % (self.DebugStr,name,content) source = source[1:] start_pos = string.find(source,chr(0x00)) attrs = source[0:start_pos] content = attrs name = "source_addr" self.set_handler(name,attrs,content) self.DebugStr = "%s , %s = <%s>" % (self.DebugStr,name,content) source = source[start_pos+1:] attrs = source[0] content = struct.unpack("!b",attrs)[0] name = "dest_addr_ton" self.set_handler(name,attrs,content) self.DebugStr = "%s , %s = <%s>" % (self.DebugStr,name,content) source = source[1:] attrs = source[0] content = struct.unpack("!b",attrs)[0] name = "dest_addr_npi" self.set_handler(name,attrs,content) self.DebugStr = "%s , %s = <%s>" % (self.DebugStr,name,content) source = source[1:] start_pos = string.find(source,chr(0x00)) attrs = source[0:start_pos] content = attrs name = "destination_addr" self.set_handler(name,attrs,content) self.DebugStr = "%s , %s = <%s>" % (self.DebugStr,name,content) source = source[start_pos+1:] attrs = source[0] content = struct.unpack("!b",attrs)[0] name = "esm_class" self.set_handler(name,attrs,content) self.DebugStr = "%s , %s = <%s>" % (self.DebugStr,name,content) source = source[1:] attrs = source[0] content = struct.unpack("!b",attrs)[0] name = "protocol_id" self.set_handler(name,attrs,content) self.DebugStr = "%s , %s = <%s>" % (self.DebugStr,name,content) source = source[1:] attrs = source[0] content = struct.unpack("!b",attrs)[0] name = "priority_flag" self.set_handler(name,attrs,content) self.DebugStr = "%s , %s = <%s>" % (self.DebugStr,name,content) source = source[1:] start_pos = string.find(source,chr(0x00)) attrs = source[0:start_pos] content = attrs name = "schedule_delivery_time" self.set_handler(name,attrs,content) self.DebugStr = "%s , %s = <%s>" % (self.DebugStr,name,content) source = source[start_pos+1:] start_pos = string.find(source,chr(0x00)) attrs = source[0:start_pos] content = attrs name = "validity_period" self.set_handler(name,attrs,content) self.DebugStr = "%s , %s = <%s>" % (self.DebugStr,name,content) source = source[start_pos+1:] attrs = source[0] content = struct.unpack("!b",attrs)[0] name = "registered_delivery" self.set_handler(name,attrs,content) self.DebugStr = "%s , %s = <%s>" % (self.DebugStr,name,content) source = source[1:] attrs = source[0] content = struct.unpack("!b",attrs)[0] name = "replace_if_present_flag" self.set_handler(name,attrs,content) self.DebugStr = "%s , %s = <%s>" % (self.DebugStr,name,content) source = source[1:] attrs = source[0] content = struct.unpack("!b",attrs)[0] name = "data_coding" self.set_handler(name,attrs,content) self.DebugStr = "%s , %s = <%s>" % (self.DebugStr,name,content) source = source[1:] attrs = source[0] content = struct.unpack("!b",attrs)[0] name = "sm_default_msg_id" self.set_handler(name,attrs,content) self.DebugStr = "%s , %s = <%s>" % (self.DebugStr,name,content) source = source[1:] attrs = source[0] content = struct.unpack("!B",attrs)[0] sm_length = content name = "sm_length" self.set_handler(name,attrs,content) self.DebugStr = "%s , %s = <%s>" % (self.DebugStr,name,content) source = source[1:] attrs = source[:sm_length] content = attrs name = "short_message text" self.set_handler(name,attrs,content) self.DebugStr = "%s , %s = <%s>" % (self.DebugStr,name,content) Msg = self.DebugStr elif command_id == "bind_transmitter" or command_id == "bind_transceiver": start_pos = string.find(source,chr(0x00)) system_id = source[0:start_pos] name = "system_id" attrs = system_id content = attrs self.set_handler(name,attrs,content) self.DebugStr = "%s , %s = <%s>" % (self.DebugStr,name,content) source = source[start_pos+1:] start_pos = string.find(source,chr(0x00)) password = source[0:start_pos] name = "password" attrs = password content = attrs self.set_handler(name,attrs,content) self.DebugStr = "%s , %s = <%s>" % (self.DebugStr,name,content) source = source[start_pos+1:] start_pos = string.find(source,chr(0x00)) system_type = source[0:start_pos] name = "system_type" attrs = system_type content = attrs self.set_handler(name,attrs,content) self.DebugStr = "%s , %s = <%s>" % (self.DebugStr,name,content) Msg = self.DebugStr PCA_GenLib.WriteLog(Msg,2) if self.StartParsing == 1: self._cont_handler.endDocument(self.DebugStr,self.TID,self.SOURCD_ID,source) Msg = "parser OK" PCA_GenLib.WriteLog(Msg,9) except: Msg = "parser :<%s>,<%s>,name=<%s>" % (sys.exc_type,sys.exc_value,name) PCA_GenLib.WriteLog(Msg,0) Msg = "orig data =\n%s" % PCA_GenLib.HexDump(orig_data) PCA_GenLib.WriteLog(Msg,0) Msg = "rest data =\n%s" % PCA_GenLib.HexDump(source) PCA_GenLib.WriteLog(Msg,0) if self.StartParsing == 1: self._cont_handler.endDocument(self.DebugStr,self.TID,self.SOURCD_ID,'undef') raise
def parseAddress(self, data, length, address_type): try: Msg = "parseAddress Init " PCA_GenLib.WriteLog(Msg, 9) source = data[0:length] tlv_desc = 'na' tlv_type = 'na' name = 'na' Msg = "Address length = <%s> data =\n%s" % ( length, PCA_GenLib.HexDump(source)) PCA_GenLib.WriteLog(Msg, 2) while len(source) > 0: name = "Address Indicator" name = "%s %s" % (address_type, name) attrs = source[0] content = ord(attrs) self.set_handler(name, attrs, content) source = source[1:] if (content & 0x01): name = "%s Address PC" % address_type self.set_handler(name, attrs, "Point Code Present") self.DebugStr = "%s <Point Code Present>" % self.DebugStr name = "PC" name = "%s %s" % (address_type, name) attrs = source[0:2] content = struct.unpack("!H", attrs[1] + attrs[0])[0] self.set_handler(name, attrs, content) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, name, content) source = source[2:] name = "SSN" name = "%s %s" % (address_type, name) attrs = source[0] content = ord(attrs) self.set_handler(name, attrs, content) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, name, content) #source = source[1:] else: self.DebugStr = "%s <Point Code Not Present>" % self.DebugStr name = "%s Address PC" % address_type self.set_handler(name, attrs, "Point Code Not Present") name = "SSN" name = "%s %s" % (address_type, name) attrs = source[0] content = ord(attrs) self.set_handler(name, attrs, content) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, name, content) source = source[1:] name = "Translation Type" name = "%s %s" % (address_type, name) attrs = source[0] content = ord(attrs) self.set_handler(name, attrs, content) source = source[1:] name = "Numbering plan" name = "%s %s" % (address_type, name) attrs = source[0] content = ord(attrs) content = content & 0xF0 content = content >> 4 self.set_handler(name, attrs, content) name = "Encoding scheme" name = "%s %s" % (address_type, name) attrs = source[0] content = ord(attrs) content = content & 0x0F self.set_handler(name, attrs, content) source = source[1:] name = "Nature of Addr" name = "%s %s" % (address_type, name) attrs = source[0] content = ord(attrs) self.set_handler(name, attrs, content) source = source[1:] name = "Digits" name = "%s %s" % (address_type, name) attrs = source content = PCA_GenLib.getHexBCDString(attrs) self.set_handler(name, attrs, content) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, name, content) break Msg = "parseAddress Ok " PCA_GenLib.WriteLog(Msg, 9) except: Msg = "parseAddress error : <%s>,<%s>,name=<%s> " % ( sys.exc_type, sys.exc_value, name) PCA_GenLib.WriteLog(Msg, 0)
def getPayloadData(self, map_type, parameter_list, parameter_list_request): try: version = PCA_M3UAParameters.version reserve = PCA_M3UAParameters.reserve message_class = PCA_M3UAParameters.Transfer_Messages message_type = PCA_M3UAParameters.DATA M3UA_header = version + reserve + message_class + message_type Tag = PCA_M3UAParameters.Routing_Context_Tag Length = struct.pack("!H", 8) Data = struct.pack("!i", self.routing_context) Routing_context = Tag + Length + Data protocol_data_tag = chr(0x02) + chr(0x10) if map_type == "MO-FSM-segment": OPC = struct.pack("!i", self.opc) else: try: dpc = parameter_list['M3UA DPC'][0] OPC = struct.pack("!i", dpc) except: OPC = struct.pack("!i", self.opc) DPC = struct.pack("!i", self.dpc) SI = chr(0x03) NI = struct.pack("!B", self.ni) MP = chr(0x00) try: SLS = parameter_list['M3UA SLS'][1] except: SLS = chr(0x04) sccp_message = self.SCCPMessage.getMessage(map_type, parameter_list, parameter_list_request) protocol_data_msg = OPC + DPC + SI + NI + MP + SLS + sccp_message message_length = len(protocol_data_msg) + 4 message_length_hex = struct.pack("!H", message_length) protocol_data = protocol_data_tag + message_length_hex + protocol_data_msg M3UA_data = M3UA_header + Routing_context + protocol_data M3UA_data_len = len(M3UA_data) % 4 M3UA_data_len_padding = 4 - M3UA_data_len message_padding = '' if M3UA_data_len_padding < 4: for i in range(M3UA_data_len_padding): M3UA_data = M3UA_data + chr(0x00) message_padding = message_padding + chr(0x00) message_length = len(M3UA_data) + 4 message_length_hex = struct.pack("!i", message_length) M3UA_Payload = M3UA_header + message_length_hex + Routing_context + protocol_data + message_padding return M3UA_Payload except: Msg = "getPayloadData Error :<%s>,<%s>" % (sys.exc_type, sys.exc_value) PCA_GenLib.WriteLog(Msg, 0) raise
def parse(self, source): try: Msg = "parser init" PCA_GenLib.WriteLog(Msg, 9) self.SOURCD_ID = 'HeartBeat' self.DebugStr = 'na' orig_data = source name = 'none' self.StartParsing = 0 #Msg = "DEBUG orig data =\n%s" % PCA_GenLib.HexDump(orig_data) #PCA_GenLib.WriteLog(Msg,0) if (source != None) and len(source) > 0: self._cont_handler.startDocument() self.StartParsing = 1 name = "command_length" attrs = source[0:4] content = struct.unpack("!i", attrs)[0] command_length = content self.set_handler(name, attrs, content) source = source[4:] name = "command_id" attrs = source[0:4] content = struct.unpack("!i", attrs)[0] try: command_id = self.command_id_dict[attrs] except: command_id = 'undef' Msg = "undef command_id=<%s>" % content PCA_GenLib.WriteLog(Msg, 0) content = command_id self.set_handler(name, attrs, content) source = source[4:] name = "command_status" attrs = source[0:4] content = struct.unpack("!i", attrs)[0] command_status = content self.set_handler(name, attrs, content) source = source[4:] name = "sequence_number" attrs = source[0:4] content = struct.unpack("!i", attrs)[0] self.TID = content sequence_number = content self.set_handler(name, attrs, content) Msg = "REQ_TID=<%s>,command_length=<%s>,command_id=<%s>,command_status=<%s>,sequence_number=<%s>" % ( self.TID, command_length, command_id, command_status, sequence_number) PCA_GenLib.WriteLog(Msg, 2) self.DebugStr = Msg dll_file_name = command_id Script_File = PCA_DLL.DLL(dll_file_name) factory_function = "Parser" factory_component = Script_File.symbol(factory_function) self.parser = factory_component() factory_function = "Handler" factory_component = Script_File.symbol(factory_function) self.handler = factory_component() self.parser.setContentHandler(self.handler) self.parser.parse(source[4:]) response_message = self.handler.getHandlerResponse() if command_id == "bind_receiver" or command_id == "bind_tranceiver": address_range = self.handler.getADDRESS_RANGE() name = "address_range" attrs = address_range content = attrs self.set_handler(name, attrs, content) AIM = self.handler.getSystem_ID() name = "system_id" attrs = AIM content = attrs self.set_handler(name, attrs, content) elif command_id == "submit_sm": dest_address = self.handler.getDEST_ADDR() name = "dest_address" attrs = dest_address content = attrs self.set_handler(name, attrs, content) TXT = self.handler.getTXT() name = "TXT" attrs = TXT content = attrs self.set_handler(name, attrs, content) message_id = response_message name = "message_id" attrs = message_id content = attrs self.set_handler(name, attrs, content) DELIVER_SM_PDU = self.handler.getDELIVER_SM_PDU() name = "DELIVER_SM_PDU" attrs = DELIVER_SM_PDU content = attrs self.set_handler(name, attrs, content) #Msg = "set DELIVER_SM_PDU" #PCA_GenLib.WriteLog(Msg,0) DebugStr = self.handler.getDebugStr() self.DebugStr = "%s %s" % (self.DebugStr, DebugStr) if self.StartParsing == 1: self._cont_handler.endDocument(self.DebugStr, self.TID, self.SOURCD_ID, response_message) Msg = "parser OK" PCA_GenLib.WriteLog(Msg, 9) except: Msg = "parser :<%s>,<%s>,name=<%s>" % (sys.exc_type, sys.exc_value, name) PCA_GenLib.WriteLog(Msg, 0) Msg = "orig data =\n%s" % PCA_GenLib.HexDump(orig_data) PCA_GenLib.WriteLog(Msg, 0) Msg = "rest data =\n%s" % PCA_GenLib.HexDump(source) PCA_GenLib.WriteLog(Msg, 0) if self.StartParsing == 1: self._cont_handler.endDocument(self.DebugStr, self.TID, self.SOURCD_ID, 'undef') raise
def parseTLV(self, data): try: Msg = "parseTLV Init " PCA_GenLib.WriteLog(Msg, 9) source = data tlv_desc = 'na' tlv_type = 'na' name = 'na' while len(source) > 0: #Msg = "parseTLV len = %s data =\n%s " % (len(source),PCA_GenLib.HexDump(source)) #PCA_GenLib.WriteLog(Msg,0) name = "Tag" attrs = source[0:2] try: content = PCA_M3UAParameters.TAG_DESC[attrs] tlv_desc = content except: content = "unknow tag =<%s>" % PCA_GenLib.HexDump(attrs) tag_name = content try: tlv_type = PCA_M3UAParameters.TAG_TYPE[attrs] except: tlv_type = "unknow tag type =<%s>" % PCA_GenLib.HexDump( attrs) self.set_handler(name, attrs, content) #self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) source = source[2:] name = "Length" attrs = source[0:2] content = struct.unpack("!H", attrs)[0] content = content - 4 length = content self.set_handler(name, attrs, content) source = source[2:] name = "Value" attrs = source[0:length] if tlv_type == 'unsigned integer' and length == 2: content = struct.unpack("!H", attrs)[0] elif tlv_type == 'unsigned integer' and length == 4: content = struct.unpack("!i", attrs)[0] elif tlv_type == 'string': content = attrs else: content = "tag data =<\n%s\n>" % PCA_GenLib.HexDump(attrs) protocol_data = attrs Msg = "tag_len = <%s>,protocol data length=<%s> =<\n%s\n>" % ( length, len(protocol_data), PCA_GenLib.HexDump(protocol_data)) PCA_GenLib.WriteLog(Msg, 3) if tlv_desc != 'Protocol_Data': if tag_name == "Traffic Mode Type": try: content = PCA_M3UAParameters.Traffic_Mode_Type[ content] except: content = "undef_traffic_mode_type_value %s" % content self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, tag_name, content) self.set_handler(tag_name, attrs, content) #################################################### # MTP3 #################################################### if tlv_desc == 'Protocol_Data': name = "OPC" attrs = protocol_data[0:4] content = struct.unpack("!i", attrs)[0] self.set_handler(name, attrs, content) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, name, content) protocol_data = protocol_data[4:] name = "DPC" attrs = protocol_data[0:4] content = struct.unpack("!i", attrs)[0] self.set_handler(name, attrs, content) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, name, content) protocol_data = protocol_data[4:] name = "SI" attrs = protocol_data[0] content = ord(attrs) if content == 3: content = "SCCP" else: content = "undefined in parameters value = %s " % content self.set_handler(name, attrs, content) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, name, content) protocol_data = protocol_data[1:] name = "NI" attrs = protocol_data[0] content = ord(attrs) self.set_handler(name, attrs, content) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, name, content) protocol_data = protocol_data[1:] name = "MP" attrs = protocol_data[0] content = ord(attrs) self.set_handler(name, attrs, content) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, name, content) protocol_data = protocol_data[1:] name = "SLS" attrs = protocol_data[0] content = ord(attrs) self.set_handler(name, attrs, content) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, name, content) protocol_data = protocol_data[1:] #Msg = "rest protocol data =<\n%s\n>" % PCA_GenLib.HexDump(protocol_data) #PCA_GenLib.WriteLog(Msg,0) #################################################### # Parsing SCCP #################################################### SCCP_Message = protocol_data sccp_parser = PCA_SCCPParser.Parser() sccp_handler = PCA_SCCPParser.Handler() sccp_parser.setContentHandler(sccp_handler) sccp_parser.parse(SCCP_Message) sccp_response_message = sccp_handler.getHandlerResponse() self.set_handler('sccp_msg_dict', chr(0x00), sccp_response_message) SCCP_ServerID = sccp_handler.getTID() SCCP_DebugStr = sccp_handler.getDebugStr() Msg = "SCCP DebutStr = %s" % SCCP_DebugStr PCA_GenLib.WriteLog(Msg, 2) self.DebugStr = "%s,SCCP MSG = %s" % (self.DebugStr, SCCP_DebugStr) #break #Msg = "parseTLV len of sccp message = %s ,data_length=%s, data=\n%s " % (length,len(source),PCA_GenLib.HexDump(source)) #PCA_GenLib.WriteLog(Msg,0) source = source[length:] Msg = "parseTLV Ok " PCA_GenLib.WriteLog(Msg, 9) except: Msg = "parseTLV error : <%s>,<%s>,name=<%s> " % ( sys.exc_type, sys.exc_value, name) PCA_GenLib.WriteLog(Msg, 2)
def getMessage(self, map_type, parameter_list): try: MAP_Tag = chr(0xa2) #if parameter_list["TCAP oid 1"][0] == "shortMsgMO_Relay_v3": if map_type == "MO-FSM-Ack": Msg = "construct MAP_MO_FSM Ack" PCA_GenLib.WriteLog(Msg, 1) MAP_Tag = chr(0xa2) udhi = chr(0x01) tp_parameter_indicator = chr(0x00) CurrentSeconds = time.time() date_tuple = time.localtime(CurrentSeconds) YY = "%04d" % date_tuple[0] YY = YY[2:4] MM = "%02d" % date_tuple[1] DD = "%02d" % date_tuple[2] HH = "%02d" % date_tuple[3] MI = "%02d" % date_tuple[4] SS = "%02d" % date_tuple[5] YYYYMMDD1 = YY + MM + DD YYYYMMDD = PCA_GenLib.converStringToReverseBCD(YYYYMMDD1) HHMISS1 = HH + MI + SS HHMISS = PCA_GenLib.converStringToReverseBCD(HHMISS1) TimeZone = chr(0x23) tp_sc_timestamp = YYYYMMDD + HHMISS + TimeZone SM_RP_UI_data_parameter = udhi + tp_parameter_indicator + tp_sc_timestamp tag = chr(0x04) tag_data = SM_RP_UI_data_parameter SM_RP_UI_data = self.constructTLV(tag, tag_data) tag = chr(0x30) tag_data = SM_RP_UI_data SM_RP_UI = self.constructTLV(tag, tag_data) tag = chr(0x02) tag_data = parameter_list["MAP opCode"][1] opCode = self.constructTLV(tag, tag_data) tag = chr(0x30) tag_data = opCode + SM_RP_UI result_tretres = self.constructTLV(tag, tag_data) tag = chr(0x02) tag_data = chr(0x02) invoke_id = self.constructTLV(tag, tag_data) map_data = invoke_id + result_tretres ######################################################### # ######################################################### elif map_type == "SRI-SM": Msg = "construct SRI-SM request" PCA_GenLib.WriteLog(Msg, 2) MAP_Tag = chr(0xa1) noa = chr(0x91) digits = PCA_GenLib.converStringToReverseBCD( parameter_list["fsg_sca"][1]) tag = chr(0x82) tag_data = noa + digits sc_address = self.constructTLV(tag, tag_data) tag = chr(0x81) tag_data = chr(0x01) SM_RP_PRI = self.constructTLV(tag, tag_data) noa = chr(0x91) digits = PCA_GenLib.converStringToReverseBCD( parameter_list["recipient"][1]) tag = chr(0x80) tag_data = noa + digits msisdn = self.constructTLV(tag, tag_data) tag = chr(0x30) tag_data = msisdn + SM_RP_PRI + sc_address address_info = self.constructTLV(tag, tag_data) tag = chr(0x02) tag_data = struct.pack("!B", PCA_MAPParameters.sendRoutingInfoForSM) opCode = self.constructTLV(tag, tag_data) tag = chr(0x02) tag_data = chr(0x7d) invoke_id = self.constructTLV(tag, tag_data) map_data = invoke_id + opCode + address_info elif map_type == "alertSC": Msg = "construct alertSC request" PCA_GenLib.WriteLog(Msg, 2) MAP_Tag = chr(0xa1) tag = chr(0x02) tag_data = chr(0x01) invoke_id = self.constructTLV(tag, tag_data) tag = chr(0x02) tag_data = tag_data = struct.pack( "!b", PCA_MAPParameters.alertServiceCentre) opCode = self.constructTLV(tag, tag_data) alert_MSISDN = parameter_list['alert_MSISDN'][0] noa = chr(0x91) digits = PCA_GenLib.converStringToReverseBCD(alert_MSISDN) tag = chr(0x04) tag_data = noa + digits msisdn = self.constructTLV(tag, tag_data) noa = chr(0x91) digits = PCA_GenLib.converStringToReverseBCD(self.sc_address) tag = chr(0x04) tag_data = noa + digits sc_address = self.constructTLV(tag, tag_data) tag = chr(0x30) tag_data = msisdn + sc_address address_info = self.constructTLV(tag, tag_data) map_data = invoke_id + opCode + address_info #Msg = "DEBUG map_data = *\n%s\n*" % PCA_GenLib.HexDump(map_data) #PCA_GenLib.WriteLog(Msg,1) elif map_type == "MO-FSM" or map_type == "MO-FSM-segment": Msg = "construct MO-FSM request" PCA_GenLib.WriteLog(Msg, 2) MAP_Tag = chr(0xa1) ## MO recipient address digits = PCA_GenLib.converStringToReverseBCD( parameter_list['recipient'][0]) address_len = struct.pack("!B", len(parameter_list['recipient'][0])) toa = chr(0x91) TP_DA = address_len + toa + digits TP_PID = chr(0x00) TP_DCS = chr(0x00) if map_type == "MO-FSM-segment": MAP_Tag = chr(0xa1) + chr(0x81) User_data_header_length = chr(0x05) Information_Element_id = chr(0x00) Information_Element_id_length = chr(0x03) message_id = chr(0x0a) total_segment = parameter_list['total_segment'][0] current_segment = parameter_list['current_segment'][0] message_part = struct.pack("!B", total_segment) message_part_number = struct.pack("!B", current_segment) UDH = User_data_header_length + Information_Element_id + Information_Element_id_length + message_id + message_part + message_part_number TP_user_data = parameter_list['sms_text'][0] #(TP_user_data_len_int,TP_user_data_gsm) = smspdu.pdu.pack7bit(TP_user_data) #TP_user_data_len_int = parameter_list['sms_text_length'][0] #TP_user_data_gsm = parameter_list['sms_text'][0] if map_type == "MO-FSM-segment": TP_RP = chr(0x41) TP_MR_int = random.randint(0, 250) TP_MR = struct.pack("!B", TP_MR_int) (TP_user_data_len_int, TP_user_data_gsm ) = smspdu.pdu.pack7bit(chr(0x00) + UDH + TP_user_data) Msg = "Text before = *\n%s\n*" % PCA_GenLib.HexDump( UDH + TP_user_data) PCA_GenLib.WriteLog(Msg, 2) Msg = "Text after = *\n%s\n*" % PCA_GenLib.HexDump( TP_user_data_gsm) PCA_GenLib.WriteLog(Msg, 2) TP_user_data_gsm = UDH + TP_user_data_gsm[6:] Msg = "Text after UDH = *\n%s\n*" % PCA_GenLib.HexDump( TP_user_data_gsm) PCA_GenLib.WriteLog(Msg, 2) #TP_user_data_len_int = TP_user_data_len_int - 1 TP_user_data_length = struct.pack("!B", TP_user_data_len_int) else: TP_RP = chr(0x21) # 21 request for DR TP_RP = chr(0x01) TP_MR_int = random.randint(0, 250) TP_MR = struct.pack("!B", TP_MR_int) #TP_user_data_gsm = chr(0x41)+chr(0x35)+chr(0x1a) TP_user_data_gsm = parameter_list['sms_text'][0] message_length_int = parameter_list['sms_text_length'][0] TP_user_data_length = struct.pack("!B", message_length_int) if map_type == "MO-FSM-segment": tag = chr(0x04) + chr(0x81) else: tag = chr(0x04) tag_data = TP_RP + TP_MR + TP_DA + TP_PID + TP_DCS + TP_user_data_length + TP_user_data_gsm SM_RP_PRI = self.constructTLV(tag, tag_data) Msg = "DEBUG SM_RP_PRI = *\n%s\n*" % PCA_GenLib.HexDump( SM_RP_PRI) PCA_GenLib.WriteLog(Msg, 3) noa = chr(0x91) digits = PCA_GenLib.converStringToReverseBCD(self.sc_address) tag = chr(0x84) tag_data = noa + digits sc_address = self.constructTLV(tag, tag_data) ## MO originator address noa = chr(0x91) digits = PCA_GenLib.converStringToReverseBCD( parameter_list['originator'][0]) tag = chr(0x82) tag_data = noa + digits msisdn = self.constructTLV(tag, tag_data) digits = PCA_GenLib.converStringToReverseBCD( parameter_list['imsi'][0]) Msg = "DEBUG IMSI = *\n%s\n*" % PCA_GenLib.HexDump(digits) PCA_GenLib.WriteLog(Msg, 3) tag = chr(0x04) tag_data = digits imsi = self.constructTLV(tag, tag_data) #imsi = chr(0x04) + chr(0x08) + digits if map_type == "MO-FSM-segment": tag = chr(0x30) + chr(0x81) tag_data = sc_address + msisdn + SM_RP_PRI + imsi else: tag = chr(0x30) tag_data = sc_address + msisdn + SM_RP_PRI + imsi address_info = self.constructTLV(tag, tag_data) tag = chr(0x02) tag_data = chr(0x2e) #MO-FSM opCode = self.constructTLV(tag, tag_data) tag = chr(0x02) tag_data = chr(0x02) invoke_id = self.constructTLV(tag, tag_data) #map_data = invoke_id + opCode + address_info + imsi map_data = invoke_id + opCode + address_info Msg = "DEBUG map_data = *\n%s\n*" % PCA_GenLib.HexDump( map_data) PCA_GenLib.WriteLog(Msg, 3) elif map_type == "MT-FSM": Msg = "construct MT-FSM request" PCA_GenLib.WriteLog(Msg, 2) MAP_Tag = chr(0xa1) TP_RP = chr(0x04) digits = PCA_GenLib.converStringToReverseBCD( parameter_list["originator"][0]) address_len = struct.pack("!B", len(digits) * 2) toa = chr(0x91) TP_OA = address_len + toa + digits TP_PID = chr(0x00) TP_DCS = chr(0x00) CurrentSeconds = time.time() date_tuple = time.localtime(CurrentSeconds) YY = "%04d" % date_tuple[0] YY = YY[2:4] MM = "%02d" % date_tuple[1] DD = "%02d" % date_tuple[2] HH = "%02d" % date_tuple[3] MI = "%02d" % date_tuple[4] SS = "%02d" % date_tuple[5] YYYYMMDD1 = YY + MM + DD YYYYMMDD = PCA_GenLib.converStringToReverseBCD(YYYYMMDD1) HHMISS1 = HH + MI + SS HHMISS = PCA_GenLib.converStringToReverseBCD(HHMISS1) TP_SC_timestamp = YYYYMMDD + HHMISS + chr(0x23) #TP_user_data = "abc123" #Msg = "##############################################################" #PCA_GenLib.WriteLog(Msg,1) #for key in sorted(parameter_list): # Msg = "<%s>=<%s>,hex=<%s>*" % (key,parameter_list[key][0],PCA_GenLib.getHexString(parameter_list[key][1])) # PCA_GenLib.WriteLog(Msg,3) TP_user_data = parameter_list['sms_text'][0] message_length_int = parameter_list['sms_text_length'][0] TP_user_data_length = struct.pack("!B", message_length_int) tag = chr(0x04) tag_data = TP_RP + TP_OA + TP_PID + TP_DCS + TP_SC_timestamp + TP_user_data_length + TP_user_data SM_RP_PRI = self.constructTLV(tag, tag_data) noa = chr(0x91) digits = PCA_GenLib.converStringToReverseBCD( parameter_list["sca"][0]) tag = chr(0x84) tag_data = noa + digits sc_address = self.constructTLV(tag, tag_data) #noa = chr(0x91) digits = PCA_GenLib.converStringToReverseBCD( parameter_list["imsi"][0]) tag = chr(0x80) #tag_data = noa + digits tag_data = digits imsi = self.constructTLV(tag, tag_data) tag = chr(0x30) tag_data = imsi + sc_address + SM_RP_PRI address_info = self.constructTLV(tag, tag_data) tag = chr(0x02) tag_data = struct.pack("!B", PCA_MAPParameters.mt_ForwardSM) opCode = self.constructTLV(tag, tag_data) tag = chr(0x02) tag_data = chr(0x02) invoke_id = self.constructTLV(tag, tag_data) map_data = invoke_id + opCode + address_info ######################################################## # MT-FSM ack ######################################################## elif map_type == "MT-FSM-Ack" or map_type == "MT-FSM-Ack-continue": Msg = "construct MT-FSM-Ack" PCA_GenLib.WriteLog(Msg, 2) tag = chr(0x02) tag_data = parameter_list["MAP invoke_id"][1] invoke_id = self.constructTLV(tag, tag_data) tag = chr(0x02) tag_data = parameter_list["MAP opCode"][1] opCode = self.constructTLV(tag, tag_data) mt_fsm_return_error = 0 if mt_fsm_return_error == 0: # Success MAP_Tag = chr(0xa2) tag = chr(0x04) tag_data = chr(0x00) + chr(0x00) SM_RP_UI_data = self.constructTLV(tag, tag_data) tag = chr(0x30) tag_data = SM_RP_UI_data SM_RP_UI = self.constructTLV(tag, tag_data) tag = chr(0x30) tag_data = opCode + SM_RP_UI result_tretres = self.constructTLV(tag, tag_data) else: # Failure MAP_Tag = chr(0xa3) tag = chr(0x02) #tag_data = chr(0x1b) tag_data = chr(0x06) #T_ABSENT_DETACHED #tag_data = chr(0x29) #AbsentSubscriberSmNoPageMsc error_code = self.constructTLV(tag, tag_data) tag = chr(0x30) tag_data = chr(0x02) + chr(0x01) + chr(0x01) SM_RP_UI = self.constructTLV(tag, tag_data) #tag = chr(0x30) #tag_data = opCode + error_code + SM_RP_UI #result_tretres = self.constructTLV(tag,tag_data) result_tretres = error_code + SM_RP_UI #map_data = invoke_id + result_tretres if map_type == "MT-FSM-Ack": #tag = chr(0x02) #tag_data = chr(0x24) #error_code = self.constructTLV(tag,tag_data) # MT-FSM return tcap error #map_data = invoke_id + error_code # MT-FSM return ok map_data = invoke_id + result_tretres else: map_data = invoke_id elif map_type == "TCAP-Begin": Msg = "construct TCAP-Begin" PCA_GenLib.WriteLog(Msg, 1) map_data = chr(0x00) + chr(0x00) elif map_type == "reportSM-DeliveryStatus-Ack": tag = chr(0x02) tag_data = parameter_list["MAP invoke_id"][1] invoke_id = self.constructTLV(tag, tag_data) map_data = invoke_id else: Msg = "undef map_type in MAPMessage ignore " PCA_GenLib.WriteLog(Msg, 1) map_data = chr(0x00) message_length = len(map_data) message_length_hex = struct.pack("!B", message_length) map_message = MAP_Tag + message_length_hex + map_data self.Message = map_message return self.Message except: Msg = "getMessage Error :<%s>,<%s>" % (sys.exc_type, sys.exc_value) PCA_GenLib.WriteLog(Msg, 0) raise
def parse(self, source): try: Msg = "parser init" PCA_GenLib.WriteLog(Msg, 9) orig_data = source name = 'none' self.StartParsing = 0 TID = "na" #0 1 2 3 #0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ #| Version | Reserved | Message Class | Message Type | #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ #| Message Length | #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ #\ \ #/ / if (source != None): self._cont_handler.startDocument() self.StartParsing = 1 name = "Version" attrs = source[0] content = ord(attrs) self.set_handler(name, attrs, content) source = source[1:] name = "Reserved" attrs = source[0] content = ord(attrs) self.set_handler(name, attrs, content) source = source[1:] name = "Message Class" attrs = source[0] message_class = attrs try: content = PCA_M3UAParameters.message_class[attrs] except: content = "Reserved" self.set_handler(name, attrs, content) self.DebugStr = "<%s>=<%s>" % (name, content) source = source[1:] name = "Message Type" attrs = source[0] try: message_class_type = PCA_M3UAParameters.message_class_type[ message_class] content = message_class_type[attrs] except: Msg = "Undef message class" PCA_GenLib.WriteLog(Msg, 0) content = "Reserved" self.set_handler(name, attrs, content) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, name, content) source = source[1:] name = "Message Length" attrs = source[0:4] content = struct.unpack("!i", attrs)[0] self.set_handler(name, attrs, content) #self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) source = source[4:] if len(source) != 0: #Msg = "rest data =\n%s" % PCA_GenLib.HexDump(source) #PCA_GenLib.WriteLog(Msg,0) Msg = "calling parse TLV" PCA_GenLib.WriteLog(Msg, 2) self.parseTLV(source) if self.StartParsing == 1: self._cont_handler.endDocument(orig_data, self.DebugStr) Msg = "parser OK" PCA_GenLib.WriteLog(Msg, 9) except: Msg = "parser :<%s>,<%s>,name=<%s>" % (sys.exc_type, sys.exc_value, name) PCA_GenLib.WriteLog(Msg, 0) Msg = "orig data =\n%s" % PCA_GenLib.HexDump(orig_data) PCA_GenLib.WriteLog(Msg, 0) raise
def characters(self,content): try: Msg = "characters Init " PCA_GenLib.WriteLog(Msg,9) self.TCAP_Message[self.MessageName] = (content,self.attrs) Msg = "%-20s=<%-25s>,Hex=%s" % (self.MessageName ,content,PCA_GenLib.HexDump(self.attrs)) PCA_GenLib.WriteLog(Msg,2) if self.MessageName != "TCAP map_message": Msg = "%s=%s" % (self.MessageName ,content) PCA_GenLib.WriteLog(Msg,3) if self.MessageName == "TCAP ap_context_name": self.dll_file_name = content self.Is_MAP_v1 = 1 elif self.MessageName == "TCAP Originating TID": self.orig_tid = self.attrs self.tcap_begin = 1 tag_data = self.attrs tag = chr(0x49) # Dest TID Tag self.transaction_portion = self.transaction_portion + self.constructTLV(tag,tag_data) elif self.MessageName == "TCAP Destination TID": self.tcap_begin = 0 self.dest_tid = self.attrs elif self.MessageName == "TCAP tcap_begin": self.tcap_begin = 1 elif self.MessageName == "TCAP tcap_continue": self.tcap_continue = 1 elif self.MessageName == "TCAP map_message": self.component_portion_avail = 1 try: # Use this to check if map v1 or tcap continue self.TCAP_Message["TCAP Application_Context"][1] Msg = "dll_file_name = <%s>" % self.dll_file_name PCA_GenLib.WriteLog(Msg,1) except: if self.tcap_begin == 1 or self.tcap_continue == 1: Msg = "MAP v1 or tcap continue message" PCA_GenLib.WriteLog(Msg,1) try: opCode = PCA_MAPParameters.op_code[ord(content[7])] if opCode == "mo-ForwardSM": opCode = "mt-ForwardSM" except: opCode = PCA_MAPParameters.op_code[ord(content[8])] if opCode == "mo-ForwardSM": opCode = "mt-ForwardSM" Msg = "MAP v1 get op code = %s" % opCode PCA_GenLib.WriteLog(Msg,1) if opCode == "sendRoutingInfoForSM": self.dll_file_name = "shortMsgGateway_SRI_v1" elif opCode == "mt-ForwardSM": self.dll_file_name = "shortMsgMT_Relay_v3" else: self.dll_file_name = "PCA_MAPParser" else: Msg = "not tcap begin message" PCA_GenLib.WriteLog(Msg,1) self.dll_file_name = "PCA_MAPParser" # Test abort for MAP fallback test #if self.dll_file_name == "shortMsgMT_Relay_v3": #if self.dll_file_name == "shortMsgGateway_SRI_v3": # Msg = "SRI v3 send TCAP abort" # PCA_GenLib.WriteLog(Msg,1) # self.TCAP_Tag = PCA_TCAPParameters.tcap_abort #if self.tcap_begin == 0 or self.tcap_continue != 1: # self.dll_file_name = "PCA_MAPParser" Msg = "%s : Parser DLL Name = %s" % (self.MessageName,self.dll_file_name) PCA_GenLib.WriteLog(Msg,1) Script_File = PCA_DLL.DLL(self.dll_file_name) factory_function="Parser" factory_component = Script_File.symbol(factory_function) parser = factory_component() Script_File = PCA_DLL.DLL(self.dll_file_name) factory_function="Handler" factory_component = Script_File.symbol(factory_function) handler = factory_component(self.XMLCFG) parser.setContentHandler(handler) parser.parse(content,self.tcap_begin,self.dll_file_name) response_message = handler.getHandlerResponse() if string.find(self.dll_file_name ,"shortMsgGateway_SRI") != -1: (self.imsi,self.NNN) = handler.getSRI_SM_resp() Msg = "sri-sm response ,imsi=<%s>,NNN=<%s>, ready send MT-FSM" % (self.imsi,self.NNN) PCA_GenLib.WriteLog(Msg,1) #self.set_handler('map_msg_dict',chr(0x00),response_message) response_ServerID = handler.getTID() self.response_DebugStr = handler.getDebugStr() #self.DebugStr = "%s,<MAP MSG>=%s" % (self.DebugStr,response_DebugStr) self.component_portion = response_message Msg = "characters OK" PCA_GenLib.WriteLog(Msg,9) except: Msg = "characters Error :<%s>,<%s> , self.MessageName = %s" % (sys.exc_type,sys.exc_value,self.MessageName) PCA_GenLib.WriteLog(Msg,0) raise
def readDataFromSocket(self,Length=1024,TimeOut = 1.0,ReadAttempts = 1): try: Msg = "readDataFromSocket " PCA_GenLib.WriteLog(Msg,9) self.ReadSet = [] self.ReadSet.append(self.SocketDescriptor) # add to select inputs list Msg = "Length to read = <%s> " % Length PCA_GenLib.WriteLog(Msg,8) Msg = "TimeOut = <%s> Seconds " % TimeOut PCA_GenLib.WriteLog(Msg,8) Msg = "ReadAttempts = <%s> " % ReadAttempts PCA_GenLib.WriteLog(Msg,8) Message = None for i in range(ReadAttempts): readables, writeables, exceptions = select.select(self.ReadSet, [], [],TimeOut) for SocketFD in readables: if (SocketFD == self.SocketDescriptor): Total_Message_Size = self.SocketDescriptor.recv(2) if (Total_Message_Size != None): Total_Message_Type = Total_Message_Size[0] Total_Message_Length = Total_Message_Size[1] TotalMessageSizeLength = struct.unpack("!b",Total_Message_Length)[0] Msg = "Total Message Size value length = <%s> " % TotalMessageSizeLength PCA_GenLib.WriteLog(Msg,6) ###################################################### ### Get message size for recv ## ###################################################### if (TotalMessageSizeLength == 1): Message_Size = self.SocketDescriptor.recv(1) RestMessageSize = struct.unpack("!b",Message_Size)[0] Msg = "Rest Message Size length=(1) for recv = <%s> " % RestMessageSize PCA_GenLib.WriteLog(Msg,6) else: Message_Size = self.SocketDescriptor.recv(2) RestMessageSize = struct.unpack("!h",Message_Size)[0] Msg = "Rest Message Size length=(2) for recv = <%s> " % RestMessageSize PCA_GenLib.WriteLog(Msg,6) CTPMessage = self.SocketDescriptor.recv(RestMessageSize) Message = Total_Message_Size+Message_Size+CTPMessage else: Msg = "server close connection" PCA_GenLib.WriteLog(Msg,0) raise socket.error,"server close connection" Msg = "ReadDataFromSocket OK" PCA_GenLib.WriteLog(Msg,9) return Message Msg = "ReadDataFromSocket retry time out !" PCA_GenLib.WriteLog(Msg,3) return None except socket.error: Msg = "ReadDataFromSocket socket error : <%s>,<%s> " % (sys.exc_type,sys.exc_value) PCA_GenLib.WriteLog(Msg,0) raise except: Msg = "ReadDataFromSocket error : <%s>,<%s> " % (sys.exc_type,sys.exc_value) PCA_GenLib.WriteLog(Msg,0) raise
def parseTLV(self,data): try: Msg = "parseTLV Init " PCA_GenLib.WriteLog(Msg,9) source = data tlv_desc = 'na' tlv_type = 'na' name = 'na' number_of_tlv = 0 tag_desc = "" #Msg = "MAP parseTLV data =\n%s" % PCA_GenLib.HexDump(source) #PCA_GenLib.WriteLog(Msg,0) tag = "" while len(source) > 0: number_of_tlv = number_of_tlv + 1 if number_of_tlv > 100: Msg = "number of TLV > 100 " PCA_GenLib.WriteLog(Msg,0) break self.tag_index = self.tag_index + 1 name = "Tag" attrs = source[0] tag_desc = "na" try: #content = "%s:%s" % (PCA_TCAPParameters.Tag_Desc[attrs],PCA_GenLib.getHexString(attrs)) if self.tcap_hearder_get == 0: self.tcap_hearder_get = 1 tag_desc = PCA_TCAPParameters.Tag_Header_Desc[attrs] else: tag_desc = PCA_TCAPParameters.Tag_Desc[attrs] #tag_desc = PCA_TCAPParameters.Tag_Desc[attrs] Msg = "tag_desc = <%s>" % tag_desc PCA_GenLib.WriteLog(Msg,3) content = tag_desc except: #content = "undef:%s" % PCA_GenLib.getHexString(attrs) content = "undef:%s" % PCA_GenLib.getHexString(attrs) #Msg = "PCA DEBUG error : <%s>,<%s> content = <%s>" % (sys.exc_type,sys.exc_value,content) #PCA_GenLib.WriteLog(Msg,0) #tag = content ##tag = "%s:%s" % (content,PCA_GenLib.getHexString(attrs)) tag = "%s" % content #self.set_handler(name,chr(0x00),content) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) tag_class = ord(attrs) & 0xc0 tag_class = tag_class >> 6 Tag_Type = 'Primitive' if (ord(attrs) & 0x20): attrs = source[0:2] content = PCA_GenLib.getHexString(attrs) Tag_Type = 'Constructor' else: content = ord(attrs) Tag_Type = 'Primitive' name = "tag type" content = Tag_Type #self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) # name = "%s form" % tag #self.set_handler(name,chr(0x00),Tag_Type) #name = "%s-%s" % (name,PCA_TCAPParameters.tag_class[tag_class]) #if Tag_Type == 'Primitive': # attrs = struct.pack("!b",ord(attrs) & 0x1f) #name = "%s %s" % (tag,tag_index) #self.set_handler(name,attrs,content) #self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) Tag_form = "Extended format" if (ord(source[0]) & 0x1f) == 0x1f: Tag_form = "Extended format" source = source[2:] #source = source[1:] else: Tag_form = "One octet format" source = source[1:] name = "tag form" content = Tag_form #self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) name = "length" name = "%s length" % tag attrs = source[0] content = ord(attrs) tag_length_form = "short" if content & 0x80: tag_length_form = "long" long_tag_length = chr(content & 0x7F) + source[1] content = struct.unpack("!H",long_tag_length)[0] tag_length = content #tag_length = struct.unpack("!B" else: tag_length_form = "short" content = struct.unpack("!B",attrs)[0] tag_length = content #self.set_handler(name,attrs,content) name = "%s %s" % (tag_length_form,name) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) if tag_length_form == "short": source = source[1:] else: source = source[2:] name = "value" name = "%s value" % tag attrs = source[0:tag_length] if tag_desc == "oid": try: Msg = "oid debug =\n%s" % PCA_GenLib.HexDump(attrs) PCA_GenLib.WriteLog(Msg,3) content = PCA_GenLib.getOctString(attrs) self.app_context = PCA_TCAPParameters.app_context[content] Msg = "app_context = %s" % self.app_context PCA_GenLib.WriteLog(Msg,3) self.DebugStr = "%s,<application>=<%s>" % (self.DebugStr,self.app_context) content = self.app_context tag = 'ap_context_name' except: Msg = "id-as-dialog %s" % PCA_GenLib.getOctString(attrs) PCA_GenLib.WriteLog(Msg,2) content = "id-as-dialog %s" % PCA_GenLib.getOctString(attrs) elif tag_desc == "tcap_begin": self.Is_TCAP_begin = 1 content = PCA_GenLib.getHexString(attrs) elif tag_desc == "tcap_end": self.Is_TCAP_begin = 0 content = PCA_GenLib.getHexString(attrs) elif tag_desc == "tcap_continue": self.Is_TCAP_begin = 2 content = PCA_GenLib.getHexString(attrs) self.set_handler(tag_desc,attrs,content) #Msg = "PCA DEBUG tcap_continue " #PCA_GenLib.WriteLog(Msg,1) self.app_context = "shortMsgMT_Relay_v3_continue" self.set_handler("ap_context_name",self.app_context,self.app_context) else: content = PCA_GenLib.getHexString(attrs) #Msg = "PCA DEBUG tag_desc=<%s>,content=<%s>" % (tag_desc,content) #PCA_GenLib.WriteLog(Msg,1) if Tag_Type == 'Constructor': if tag_desc == "component_portion": Msg = "GSM 0340 layer , not parsing now" PCA_GenLib.WriteLog(Msg,3) self.set_handler("map_message",chr(0x00),attrs) #################################################### # Parsing MAP #################################################### #Msg = "MAP application data =<\n%s\n>" % PCA_GenLib.HexDump(attrs) #PCA_GenLib.WriteLog(Msg,3) else: self.parseTLV(attrs) #if tag_desc == "tcap_continue": # self.set_handler(tag_desc,attrs,content) #Msg = "PCA DEBUG Possible MT-FSM-Req tacp continue .... " #PCA_GenLib.WriteLog(Msg,1) #else: self.set_handler(tag,attrs,content) else: self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) self.set_handler(tag,attrs,content) try: source = source[tag_length:] except IndexError: Msg = "parseTLV index error : <%s>,<%s>,name=<%s> " % (sys.exc_type,sys.exc_value,name) PCA_GenLib.WriteLog(Msg,0) break Msg = "parseTLV Ok " PCA_GenLib.WriteLog(Msg,9) except: Msg = "parseTLV error : <%s>,<%s>,name=<%s> " % (sys.exc_type,sys.exc_value,name) PCA_GenLib.WriteLog(Msg,0)
def MainTest(XMLCFG): try: print 'Start Program ...' try: PCA_GenLib.DBXMLCFGInit(XMLCFG) Server = Connector(XMLCFG) try: Server.connect() #for i in range(1): # Message = chr(0x01)+ chr(0x00)+ chr(0x03)+ chr(0x01)+ chr(0x00)+ chr(0x00)+ chr(0x00)+ chr(0x08) # Server.sendDataToSocket(Message) parser = PCA_M3UAParser.Parser() handler = PCA_M3UAParser.Handler() parser.setContentHandler(handler) while (1): Message = Server.readDataFromSocket() if Message != None: Msg = "receive = *\n%s\n*" % PCA_GenLib.HexDump( Message) PCA_GenLib.WriteLog(Msg, 2) parser.parse(Message) response_message = handler.getHandlerResponse() ServerID = handler.getTID() DebugStr = handler.getDebugStr() Msg = "recv : %s*" % DebugStr PCA_GenLib.WriteLog(Msg, 1) for m3ua_key in response_message: if m3ua_key == "M3UA sccp_msg_dict": sccp_msg_dict = response_message[m3ua_key] for sccp_key in sccp_msg_dict: if sccp_key == "SCCP tcap_msg_dict": tcap_msg_dict = sccp_msg_dict[ sccp_key] for tcap_key in tcap_msg_dict: Msg = "key=<%s> : value=<%s>*" % ( tcap_key, tcap_msg_dict[tcap_key]) PCA_GenLib.WriteLog(Msg, 1) else: Msg = "key=<%s> : value=<%s>*" % ( sccp_key, sccp_msg_dict[sccp_key]) PCA_GenLib.WriteLog(Msg, 1) else: Msg = "key=<%s> : value=<%s>*" % ( m3ua_key, response_message[m3ua_key]) PCA_GenLib.WriteLog(Msg, 1) time.sleep(20) finally: Server.Close() finally: PCA_GenLib.CloseLog() return 0 except: print '\n\n uncaught ! < ', sys.exc_type, sys.exc_value, ' >' import traceback traceback.print_exc() raise