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 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 parseGSM0340_request(self, data): try: Msg = "parseGSM0340_request Init " PCA_GenLib.WriteLog(Msg, 2) Msg = "GSM 0340 data =\n%s" % PCA_GenLib.HexDump(data) PCA_GenLib.WriteLog(Msg, 2) mt_fsm_gsm_0340_pdu = data tag = mt_fsm_gsm_0340_pdu[0] source = mt_fsm_gsm_0340_pdu[1:] name = "OA length" attrs = source[0] content = ord(attrs) tag_length = content tag_length = tag_length / 2 self.set_handler(name, attrs, tag_length) source = source[1:] name = "GSM0340 originating address" attrs = source[0:tag_length + 1] toa = PCA_GenLib.getHexString(attrs[1]) tag_value = "%s:%s" % (toa, PCA_GenLib.getHexBCDString(attrs[1:])) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, name, tag_value) self.set_handler(name, attrs, tag_value) source = source[tag_length + 1:] pid = source[0] dcs = source[1] time_stamp = source[2:2 + 6] source = source[9:] name = "GSM0340 user data length" attrs = source[0] content = ord(attrs) user_data_length = content self.set_handler(name, attrs, content) source = source[1:] tag_name = "GSM0340 sms text" attrs = source[0:user_data_length] Msg = "PCA DEBUG SMS Text = %s " % attrs PCA_GenLib.WriteLog(Msg, 1) #tag_value = PCA_GenLib.getHexString(attrs) tag_value = smspdu.pdu.unpack7bit(attrs) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, tag_name, tag_value) self.set_handler("sms text", attrs, content) Msg = "parseGSM0340_request ok " PCA_GenLib.WriteLog(Msg, 9) except: Msg = "parseGSM0340_request error : <%s>,<%s>" % (sys.exc_type, sys.exc_value) PCA_GenLib.WriteLog(Msg, 0) Msg = "parseGSM0340_request error source =\n%s" % PCA_GenLib.HexDump( source) PCA_GenLib.WriteLog(Msg, 3)
def MO(self,conn,originator_address,recipient_address,imsi,sms_text_data): try: Msg = "MO" PCA_GenLib.WriteLog(Msg,9) request_parameter_list = {} request_parameter_list['originator'] = (originator_address,originator_address) request_parameter_list['recipient'] = (recipient_address,recipient_address) request_parameter_list['imsi'] = (imsi,imsi) (sms_text_length,sms_text) = smspdu.pdu.pack7bit(sms_text_data) Msg = "---------------------------------------" PCA_GenLib.WriteLog(Msg,2) Msg = "len=<%s> ascii = *\n%s\n*" % (len(sms_text_data),PCA_GenLib.HexDump(sms_text_data)) PCA_GenLib.WriteLog(Msg,2) Msg = "len=<%s> gsm7bit = *\n%s\n*" % (sms_text_length,PCA_GenLib.HexDump(sms_text)) PCA_GenLib.WriteLog(Msg,2) Msg = "---------------------------------------" PCA_GenLib.WriteLog(Msg,2) request_parameter_list['sms_text'] = (sms_text,sms_text) request_parameter_list['sms_text_length'] = (sms_text_length,sms_text_length) Message = self.M3UAMessage.getPayloadData("MO-FSM",request_parameter_list,request_parameter_list) self.sendDataToSocket(conn,Message) Msg = "MO OK" PCA_GenLib.WriteLog(Msg,9) except: Msg = "MO Error :<%s>,<%s>" % (sys.exc_type,sys.exc_value) PCA_GenLib.WriteLog(Msg,0) raise
def characters(self, content): try: Msg = "characters Init " PCA_GenLib.WriteLog(Msg, 9) self.MAP_Message[self.MessageName] = (content, self.attrs) Msg = "%-20s=<%-25s>,Hex=%s" % (self.MessageName, content, PCA_GenLib.HexDump(self.attrs)) PCA_GenLib.WriteLog(Msg, 3) Msg = "%s=%s" % (self.MessageName, content) PCA_GenLib.WriteLog(Msg, 1) if self.MessageName == "MAP opCode": self.opCode = content elif self.MessageName == "MAP imsi value": self.imsi = content elif self.MessageName == "MAP NNN value": self.NNN = content Msg = "characters OK" PCA_GenLib.WriteLog(Msg, 9) except: Msg = "characters Error :<%s>,<%s>" % (sys.exc_type, sys.exc_value) PCA_GenLib.WriteLog(Msg, 0) raise
def MainTest(XMLCFG): try: print 'Start Program ...' try: PCA_GenLib.DBXMLCFGInit(XMLCFG) import PCA_M3UAMessage M3UAMessage = PCA_M3UAMessage.Writer(XMLCFG) try: #Message = M3UAMessage.getASUP_UP() Message = M3UAMessage.getASP_Active() Msg = "message = *\n%s\n*" % PCA_GenLib.HexDump(Message) PCA_GenLib.WriteLog(Msg, 1) parser = Parser() handler = Handler() parser.setContentHandler(handler) parser.parse(Message) response_message = handler.getHandlerResponse() ServerID = handler.getTID() DebugStr = handler.getDebugStr() print "DebutStr = %s" % DebugStr finally: x = 1 finally: PCA_GenLib.CloseLog() return 0 except: print '\n\n uncaught ! < ', sys.exc_type, sys.exc_value, ' >' import traceback traceback.print_exc() raise
def characters(self, content): try: Msg = "characters Init " PCA_GenLib.WriteLog(Msg, 9) Msg = "%-20s=<%-25s>,Hex=%s" % (self.MessageName, content, PCA_GenLib.HexDump(self.attrs)) PCA_GenLib.WriteLog(Msg, 3) try: if self.Message[self.MessageName] != None: x = 1 self.dup_tag = self.dup_tag + 1 name = "%s %s" % (self.MessageName, self.dup_tag) self.Message[name] = (content, self.attrs) except: self.Message[self.MessageName] = (content, self.attrs) Msg = "characters OK" PCA_GenLib.WriteLog(Msg, 9) except: Msg = "characters 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" self.tag_index = 0 self.Is_TCAP_begin = Is_TCAP_begin self.app_context = app_context Msg = "MAP data =<\n%s\n>" % PCA_GenLib.HexDump(source) PCA_GenLib.WriteLog(Msg, 3) ############################################ # Message Type Tag # Total Message Length # Transaction Portion Information Element # Dialogue Portion Information Element # Dialog Portion Tag + External Tag + OID Tag # Structed Tag + ASN.1 Type Tag + application context name # Dialog Request Tag + Dialog Request length + # Component Portion Tag # Component Portion Length # Component Type Tag # Component Type Length if (source != None): self._cont_handler.startDocument() self.StartParsing = 1 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 parseGSM0340_request(self, data): try: Msg = "parseGSM0340_request Init " PCA_GenLib.WriteLog(Msg, 9) Msg = "GSM 0340 data =\n%s" % PCA_GenLib.HexDump(data) PCA_GenLib.WriteLog(Msg, 3) gsm_0340_pdu = data tag = gsm_0340_pdu[0] TP_MR = gsm_0340_pdu[1] source = gsm_0340_pdu[2:] name = "GSM0340 recipient length" attrs = source[0] content = ord(attrs) tag_length = content tag_length = tag_length / 2 self.set_handler(name, attrs, tag_length) source = source[1:] name = "GSM0340 recipient address" attrs = source[0:tag_length + 1] toa = PCA_GenLib.getHexString(attrs[1]) #tag_value = "%s:%s" % (toa,PCA_GenLib.getHexBCDString(attrs[1:])) tag_value = "%s" % (PCA_GenLib.getHexBCDString(attrs[1:])) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, name, tag_value) self.set_handler(name, attrs, tag_value) source = source[tag_length + 1:] pid = source[0] dcs = source[1] validity_period = source[2] name = "GSM0340 user data length" attrs = source[3] content = ord(attrs) user_data_length = content #self.set_handler(name,attrs,content) source = source[4:] tag_name = "GSM0340 sms text" attrs = source[0:user_data_length] Msg = "PCA DEBUG MO Text = %s " % attrs PCA_GenLib.WriteLog(Msg, 2) tag_value = smspdu.pdu.unpack7bit(attrs) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, tag_name, tag_value) self.set_handler("sms text", attrs, tag_value) Msg = "parseGSM0340_request ok " PCA_GenLib.WriteLog(Msg, 9) except: Msg = "parseGSM0340_request error : <%s>,<%s>" % (sys.exc_type, sys.exc_value) PCA_GenLib.WriteLog(Msg, 3)
def parse(self, source): try: Msg = "parser init" PCA_GenLib.WriteLog(Msg,9) orig_data = source name = 'none' self.StartParsing = 0 TID = "na" content = "na" Msg = "MAP shortMORelay Response data =<\n%s\n>" % PCA_GenLib.HexDump(source) PCA_GenLib.WriteLog(Msg,5) if (source != None) : self._cont_handler.startDocument() self.StartParsing = 1 self.DebugStr = "" 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 parseGSM0340_MTresponse(self, data): try: Msg = "parseGSM0340_MTresponse Init " PCA_GenLib.WriteLog(Msg, 9) Msg = "GSM 0340 data =\n%s" % PCA_GenLib.HexDump(data) PCA_GenLib.WriteLog(Msg, 3) Msg = "parseGSM0340_MTresponse ok " PCA_GenLib.WriteLog(Msg, 9) except: Msg = "parseGSM0340_MTresponse error : <%s>,<%s>" % (sys.exc_type, sys.exc_value) PCA_GenLib.WriteLog(Msg, 0)
def characters(self, content): try: Msg = "characters Init " PCA_GenLib.WriteLog(Msg, 9) Msg = "%-20s=<%-25s>,Hex=%s" % (self.MessageName, content, PCA_GenLib.HexDump(self.attrs)) PCA_GenLib.WriteLog(Msg, 2) self.SCCP_Message[self.MessageName] = (content, self.attrs) if self.MessageName == "Traffic Type": self.Traffic_Type = content elif self.MessageName == "SCCP calling Digits": Msg = "CgPA = <%s>" % content PCA_GenLib.WriteLog(Msg, 2) elif self.MessageName == "SCCP called Digits": Msg = "CdPA = <%s>" % content PCA_GenLib.WriteLog(Msg, 2) elif self.MessageName == "SCCP tcap_parameter": TCAP_Message = self.attrs tcap_parser = PCA_TCAPParser.Parser() tcap_handler = PCA_TCAPParser.Handler(self.XMLCFG) tcap_parser.setContentHandler(tcap_handler) tcap_parser.parse(TCAP_Message) self.tcap_response_message = tcap_handler.getHandlerResponse() TCAP_ServerID = tcap_handler.getTID() self.TCAP_DebugStr = tcap_handler.getDebugStr() (self.orig_tid, self.dest_tid) = tcap_handler.getTCAP_ID() (self.imsi, self.NNN) = tcap_handler.getSRI_SM_resp() #Msg = "sri-sm response ,imsi=<%s>,NNN=<%s>, ready send MT-FSM" % (self.imsi,self.NNN) #PCA_GenLib.WriteLog(Msg,2) else: Msg = "%s=%s" % (self.MessageName, content) PCA_GenLib.WriteLog(Msg, 2) Msg = "characters OK" PCA_GenLib.WriteLog(Msg, 9) except: Msg = "characters Error :<%s>,<%s>" % (sys.exc_type, sys.exc_value) PCA_GenLib.WriteLog(Msg, 0) raise
def characters(self,content): try: Msg = "characters Init " PCA_GenLib.WriteLog(Msg,9) Msg = "%-20s=<%-25s>,Hex=%s" % (self.MessageName ,content,PCA_GenLib.HexDump(self.attrs)) PCA_GenLib.WriteLog(Msg,3) self.Message[self.MessageName] = content Msg = "characters OK" PCA_GenLib.WriteLog(Msg,9) except: Msg = "characters Error :<%s>,<%s>" % (sys.exc_type,sys.exc_value) PCA_GenLib.WriteLog(Msg,0) raise
def parseGSM0340_SRI_SM_response(self, data): name = 'undef' try: Msg = "parseGSM0340_SRI_SM_response Init " PCA_GenLib.WriteLog(Msg, 9) Msg = "GSM 0340 data =\n%s" % PCA_GenLib.HexDump(data) PCA_GenLib.WriteLog(Msg, 2) name = "GSM0340 tp_udhi" attrs = data[0] content = ord(attrs) self.set_handler(name, attrs, content) name = "GSM0340 tp_mti" attrs = data[1] content = ord(attrs) self.set_handler(name, attrs, content) name = "GSM0340 tp_date" attrs = data[2:5] content = PCA_GenLib.getHexString(attrs) content = "%s%s%s%s%s%s" % (content[1], content[0], content[3], content[2], content[5], content[4]) self.set_handler(name, attrs, content) name = "GSM0340 tp_time" attrs = data[5:8] content = PCA_GenLib.getHexString(attrs) content = "%s%s%s%s%s%s" % (content[1], content[0], content[3], content[2], content[5], content[4]) self.set_handler(name, attrs, content) name = "GSM0340 tp_timezone" attrs = data[8] content = ord(attrs) self.set_handler(name, attrs, content) Msg = "parseGSM0340_SRI_SM_response ok " PCA_GenLib.WriteLog(Msg, 9) except: Msg = "parseGSM0340_SRI_SM_response error : <%s>,<%s>,name=<%s>" % ( sys.exc_type, sys.exc_value, name) PCA_GenLib.WriteLog(Msg, 0)
def sendSSNM(self,conn,SSNM_TYPE): try: Msg = "sendSSNM" PCA_GenLib.WriteLog(Msg,9) Message = self.M3UAMessage.getSSNM(SSNM_TYPE) if Message != None: Msg = "send = *\n%s\n*" % PCA_GenLib.HexDump(Message) PCA_GenLib.WriteLog(Msg,2) self.sendDataToSocket(conn,Message) Msg = "sendSSNM OK" PCA_GenLib.WriteLog(Msg,9) except: Msg = "sendSSNM Error :<%s>,<%s>" % (sys.exc_type,sys.exc_value) PCA_GenLib.WriteLog(Msg,0) raise
def MainTest(XMLCFG): try: print 'Start Program ...' try: PCA_GenLib.DBXMLCFGInit(XMLCFG) Message = Writer(XMLCFG) try: #M3UA_ASUP_UP = Message.getASUP_UP() data = Message.getASP_Active() Msg = "message = *\n%s\n*" % PCA_GenLib.HexDump(data) PCA_GenLib.WriteLog(Msg,1) finally: x=1 finally: PCA_GenLib.CloseLog() return 0 except: print '\n\n uncaught ! < ',sys.exc_type,sys.exc_value,' >' import traceback traceback.print_exc() 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 characters(self, content): try: Msg = "characters Init " PCA_GenLib.WriteLog(Msg, 9) Msg = "%-15s=<%-25s>,Hex=%s" % (self.MessageName, content, PCA_GenLib.HexDump(self.attrs)) #Msg = "<%s>=<%s>,Hex=%s" % (self.MessageName ,content,PCA_GenLib.HexDump(self.attrs)) PCA_GenLib.WriteLog(Msg, 3) if self.MessageName == "Traffic Type": self.Traffic_Type = content elif self.MessageName == "protocol_data": #################################################### # Parsing SCCP #################################################### SCCP_Message = content sccp_parser = PCA_SCCPParser.Parser() sccp_handler = PCA_SCCPParser.Handler(self.XMLCFG) sccp_parser.setContentHandler(sccp_handler) sccp_parser.parse(SCCP_Message, self.Traffic_Type) sccp_response_message = sccp_handler.getHandlerResponse() SCCP_ServerID = sccp_handler.getTID() self.SCCP_DebugStr = sccp_handler.getDebugStr() (self.orig_tid, self.dest_tid) = sccp_handler.getTCAP_ID() (self.imsi, self.NNN) = sccp_handler.getSRI_SM_resp() protocol_data_msg = self.opc + self.dpc + self.SI + self.NI + self.MP + self.SLS + sccp_response_message message_length = len(protocol_data_msg) + 4 message_length_hex = struct.pack("!H", message_length) protocol_data = self.protocol_data_tag + message_length_hex + protocol_data_msg M3UA_data = self.M3UA_header + self.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) self.Message = self.M3UA_header + message_length_hex + self.Routing_context + protocol_data + message_padding Msg = "send Payload Data (DATA) Response back to client" PCA_GenLib.WriteLog(Msg, 2) #self.Message = self.M3UAMessage.getPayloadData(sccp_response_message) elif self.MessageName == "SLS": self.SLS = self.attrs elif self.MessageName == "Message Type": Msg = "recv : %s " % content PCA_GenLib.WriteLog(Msg, 2) if content == "Payload Data (DATA)": Msg = "Got Payload Data (DATA)" PCA_GenLib.WriteLog(Msg, 2) elif content == "ASP Up (ASPUP)": self.Message = self.M3UAMessage.getASUP_UP_Ack() Msg = "send ASUP_UP_Ack back to client" PCA_GenLib.WriteLog(Msg, 1) elif content == "ASP Active (ASPAC)": self.Message = self.M3UAMessage.getASP_Active_Ack() Msg = "send ASP Active (ASPAC)_Ack back to client" PCA_GenLib.WriteLog(Msg, 1) elif content == "Heartbeat (BEAT)": self.Message = self.M3UAMessage.getASPSM_BEAT_Ack() Msg = "send Heartbeat (BEAT)_Ack back to client" PCA_GenLib.WriteLog(Msg, 2) elif content == "Heartbeat Acknowledgement (BEAT ACK)": self.Message = None Msg = "Heartbeat (BEAT)_Ack no response" PCA_GenLib.WriteLog(Msg, 2) elif content == "Destination State Audit (DAUD)": Msg = "Destination State Audit (DAUD) send DAVA back" PCA_GenLib.WriteLog(Msg, 1) self.Message = self.M3UAMessage.getSSNM("DAVA") elif content == "Destination Available (DAVA)": Msg = "Destination Available (DAVA) no response back" PCA_GenLib.WriteLog(Msg, 1) self.Message = None elif content == "ASP Active Acknowledgement (ASPAC ACK)": Msg = "ASP Active Acknowledgement (ASPAC ACK) no response back" PCA_GenLib.WriteLog(Msg, 1) self.Message = None elif content == "ASP Up (ASPUP) ACK": Msg = "ASP Up (ASPUP) ACK no response back" PCA_GenLib.WriteLog(Msg, 2) self.Message = None else: Msg = "UNDEF Message Type = <%s>" % content PCA_GenLib.WriteLog(Msg, 1) else: Msg = "No action for this yet" PCA_GenLib.WriteLog(Msg, 3) Msg = "characters OK" PCA_GenLib.WriteLog(Msg, 9) except: Msg = "characters 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" #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 = "Traffic Type" attrs = Traffic_Type content = Traffic_Type self.set_handler(name, attrs, content) 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) 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 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) 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:] name = "protocol_data" attrs = chr(0x00) content = protocol_data self.set_handler(name, attrs, content) 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 handle_event(self, SocketEventFD, ClientMessage): try: Msg = "handle_event Init" PCA_GenLib.WriteLog(Msg, 9) ####################################################### ## Read Request from Client ### ## ### ####################################################### Message = self.readDataFromSocket(SocketEventFD, Length=3, TimeOut=10.0, ReadAttempts=1) if Message == None: Msg = "read SMPP length error " PCA_GenLib.WriteLog(Msg, 1) raise socket.error Msg = "DEBUG recv from Client =\n%s" % PCA_GenLib.HexDump(Message) PCA_GenLib.WriteLog(Msg, 2) MessageLength = ClientMessage + Message MessageLength_Int = struct.unpack("!i", MessageLength)[0] if (MessageLength_Int != 0) and (MessageLength_Int < 4096): Message = self.readDataFromSocket(SocketEventFD, Length=MessageLength_Int - 4, TimeOut=5.0, ReadAttempts=1) if Message == None: Msg = "read SMPP PDU error " PCA_GenLib.WriteLog(Msg, 1) raise socket.error else: Msg = "read SMPP PDU error incorrect length = <%s> " % MessageLength_Int PCA_GenLib.WriteLog(Msg, 1) raise socket.error SocketMessage = MessageLength + Message Msg = "recv from Client =\n%s" % PCA_GenLib.HexDump(SocketMessage) PCA_GenLib.WriteLog(Msg, 2) self.parser.parse(SocketMessage) response_message = self.handler.getHandlerResponse() ServerID = self.handler.getTID() DebugStr = self.handler.getDebugStr() if response_message != None: command_id = self.handler.getCOMMAND_ID() Msg = "command_id=<%s>" % command_id PCA_GenLib.WriteLog(Msg, 2) if command_id[-4:] == "resp": Msg = "SMPP response message no need to ack command_id=<%s>" % command_id PCA_GenLib.WriteLog(Msg, 1) else: Msg = "send back to Client =\n%s" % PCA_GenLib.HexDump( response_message) PCA_GenLib.WriteLog(Msg, 2) result = self.sendDataToSocket(SocketEventFD, response_message, TimeOut=2, WriteAttempts=3) if result != None: Msg = "send back to client ok : data recv from client : ServerID=<%s>,%s" % ( ServerID, DebugStr) PCA_GenLib.WriteLog(Msg, 1) if command_id == "bind_receiver" or command_id == "bind_tranceiver": Msg = "save socket fd and address range" PCA_GenLib.WriteLog(Msg, 2) if self.msg_routing == "Address": address_range = self.handler.getADDRESS_RANGE() Msg = "save to bind_recever_socket_fd address_range=<%s>,SocketEventFD=<%s>" % ( address_range, id(SocketEventFD)) PCA_GenLib.WriteLog(Msg, 1) self.bind_recever_socket_fd[ address_range] = SocketEventFD else: AIM = self.handler.getSystem_ID() Msg = "save to bind_recever_socket_fd AIM=<%s>,SocketEventFD=<%s>" % ( AIM, id(SocketEventFD)) PCA_GenLib.WriteLog(Msg, 1) self.bind_recever_socket_fd[ AIM] = SocketEventFD elif command_id == "submit_sm": Msg = "check destination address" PCA_GenLib.WriteLog(Msg, 2) if self.msg_routing == "Address": dest_address = self.handler.getDEST_ADDR() else: dest_address = self.handler.getTXT() Msg = "dest_address = <%s>" % dest_address PCA_GenLib.WriteLog(Msg, 2) Msg = "check if need to send delivery sm to exists bind_recever connection" PCA_GenLib.WriteLog(Msg, 2) if string.find(DebugStr, "registered_delivery = <1>") == -1: Msg = "submit sm did not request DR , ignore send deliver sm by setting dest_address=na" PCA_GenLib.WriteLog(Msg, 2) dest_address = 'na' found_receiver = 0 for address in self.bind_recever_socket_fd.keys(): Msg = "debug connected socket info <%s>" % address PCA_GenLib.WriteLog(Msg, 2) #if re.compile(address).search(dest_address) != None: if dest_address.find(address) != -1: Msg = "found a receiver to delivery dest_addr = <%s>,receiver bind address=<%s>,socket_fd=<%s>" % ( dest_address, address, id(self.bind_recever_socket_fd[address] )) PCA_GenLib.WriteLog(Msg, 1) found_receiver = 1 break if found_receiver == 1 and dest_address != 'na': Msg = "delivery sm to receiver socket_fd = <%s>" % id( self.bind_recever_socket_fd[address]) PCA_GenLib.WriteLog(Msg, 1) message_id = self.handler.getMessage_ID() Msg = "delivery sm message id = <%s>" % ( message_id) PCA_GenLib.WriteLog(Msg, 1) delivery_sm = self.handler.getDELIVER_SM_PDU() ############################################ # convert submit_sm to delivery_sm # this use only for echo test ############################################ #delivery_sm = SocketMessage[0:7]+chr(0x05)+SocketMessage[8:] try: Msg = "send delivery sm to receiver =\n%s" % PCA_GenLib.HexDump( delivery_sm) PCA_GenLib.WriteLog(Msg, 2) result = self.sendDataToSocket( self.bind_recever_socket_fd[address], delivery_sm, TimeOut=2, WriteAttempts=3) if result != None: Msg = "send delivery sm to receiver ok : socket_fd = <%s>" % id( self. bind_recever_socket_fd[address]) PCA_GenLib.WriteLog(Msg, 1) else: Msg = "send delivery sm to receiver failure : socket_fd = <%s>" % id( self. bind_recever_socket_fd[address]) PCA_GenLib.WriteLog(Msg, 1) except socket.error: Msg = "send delivery sm to receiver socket failure ignore : socket_fd = <%s>" % id( self.bind_recever_socket_fd[address]) PCA_GenLib.WriteLog(Msg, 1) else: Msg = "did not find any receiver for this : %s" % dest_address PCA_GenLib.WriteLog(Msg, 2) else: Msg = "send back to client failure timeout : data recv from client : ServerID=<%s>,%s " % ( ServerID, DebugStr) PCA_GenLib.WriteLog(Msg, 1) else: Msg = "Error unknow response" 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 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 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) Msg = "MAP SRI-SM Is_TCAP_begin = <%s>" % Is_TCAP_begin PCA_GenLib.WriteLog(Msg, 3) # SRI request if Is_TCAP_begin == 1: Msg = "MAP SRI-SM DEBUG data begin =<\n%s\n>" % PCA_GenLib.HexDump( source) PCA_GenLib.WriteLog(Msg, 3) source = source[1:] name = "opCode" tag_name = name attrs = source[0] content = ord(attrs) self.set_handler(name, attrs, content) source = source[1:] name = "opCode length" attrs = source[0] content = ord(attrs) tag_length = content self.set_handler(name, attrs, content) source = source[1:] name = "opCode" attrs = source[0] content = ord(attrs) op_code = 'na' try: tag_value = PCA_MAPParameters.op_code[content] op_code = tag_value #Msg = "MAP opcode = <%s>" % tag_value #PCA_GenLib.WriteLog(Msg,1) except: Msg = "unknow opCode Value = %s" % content PCA_GenLib.WriteLog(Msg, 0) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, tag_name, tag_value) self.set_handler(name, attrs, tag_value) source = source[1:] name = "msisdn tag" tag_name = name attrs = source[0] content = ord(attrs) self.set_handler(name, attrs, content) source = source[1:] name = "msisdn tag length" attrs = source[0] content = ord(attrs) tag_length = content #self.set_handler(name,attrs,content) source = source[1:] name = "msisdn tag value" attrs = source[0:tag_length] tag_value = PCA_GenLib.getHexString(attrs) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, tag_name, tag_value) self.set_handler(name, attrs, tag_value) source = attrs name = "msisdn" tag_name = name attrs = source[0] content = ord(attrs) source = source[1:] name = "length" attrs = source[0] content = ord(attrs) tag_length = content source = source[1:] name = "msisdn value" attrs = source[1:tag_length] tag_value = PCA_GenLib.getHexBCDString(attrs) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, tag_name, tag_value) self.set_handler(name, attrs, tag_value) source = source[tag_length:] if op_code != "reportSM-DeliveryStatus": name = "Priority Flag" tag_name = name attrs = source[0] content = ord(attrs) source = source[1:] name = "length" attrs = source[0] content = ord(attrs) tag_length = content source = source[1:] name = "Priority Flag value" attrs = source[0:tag_length] tag_value = PCA_GenLib.getHexString(attrs) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, tag_name, tag_value) self.set_handler(name, attrs, tag_value) source = source[1:] name = "sc-address" tag_name = name attrs = source[0] content = ord(attrs) source = source[1:] name = "sc-address length" attrs = source[0] content = ord(attrs) tag_length = content source = source[1:] name = "sc-address value" attrs = source[1:tag_length] tag_value = PCA_GenLib.getHexBCDString(attrs) Msg = "sc_address = <%s>" % tag_value PCA_GenLib.WriteLog(Msg, 3) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, tag_name, tag_value) self.set_handler(name, attrs, tag_value) source = source[tag_length:] if op_code == "reportSM-DeliveryStatus": name = "sm-DeliveryOutcom" attrs = source[2] content = ord(attrs) self.set_handler(name, attrs, content) Msg = "name = %s , value = %s" % (name, content) PCA_GenLib.WriteLog(Msg, 3) else: # SRI response Msg = "MAP SRI-SM DEBUG data end =<\n%s\n>" % PCA_GenLib.HexDump( source) PCA_GenLib.WriteLog(Msg, 3) source = source[1:] name = "resultretres" tag_name = name attrs = source[0] content = ord(attrs) source = source[1:] name = "length" attrs = source[0] content = ord(attrs) tag_length = content source = source[1:] name = "resultretres value" attrs = source[0:tag_length] source = attrs name = "opCode" tag_name = name attrs = source[0] content = ord(attrs) source = source[1:] name = "length" attrs = source[0] content = ord(attrs) tag_length = content source = source[1:] name = "opCode" attrs = source[0] content = ord(attrs) try: tag_value = PCA_MAPParameters.op_code[content] except: Msg = "unknow opCode Value = %s" % content PCA_GenLib.WriteLog(Msg, 0) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, tag_name, tag_value) self.set_handler(name, attrs, tag_value) source = source[1:] name = "sm-rp-UI" 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) source = source[1:] name = "sm-rp-UI value" attrs = source[0:tag_length] tag_value = PCA_GenLib.getHexString(attrs) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, tag_name, tag_value) self.set_handler(name, attrs, tag_value) source = attrs name = "imsi" tag_name = name attrs = source[0] content = ord(attrs) source = source[1:] name = "length" attrs = source[0] content = ord(attrs) tag_length = content source = source[1:] name = "imsi value" attrs = source[0:tag_length] tag_value = PCA_GenLib.getHexIMSIString(attrs)[0:15] self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, tag_name, tag_value) self.set_handler(name, attrs, tag_value) source = source[tag_length:] name = "location-info-with-LMSI" tag_name = name attrs = source[0] content = ord(attrs) source = source[1:] name = "length" attrs = source[0] content = ord(attrs) tag_length = content source = source[1:] name = "location-info-with-LMSI value" attrs = source[0:tag_length] tag_value = PCA_GenLib.getHexString(attrs) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, tag_name, tag_value) self.set_handler(name, attrs, tag_value) source = attrs name = "NNN" tag_name = name attrs = source[0] content = ord(attrs) source = source[1:] name = "length" attrs = source[0] content = ord(attrs) tag_length = content source = source[1:] name = "NNN value" attrs = source[1:tag_length] #tag_value = PCA_GenLib.getHexString(attrs) tag_value = PCA_GenLib.getHexBCDString(attrs) self.set_handler(name, tag_value, tag_value) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr, tag_name, tag_value) #self.set_handler(name,attrs,tag_value) #source = source[1:] #name = "location GT" #attrs = source #content = PCA_GenLib.getHexBCDString(attrs) #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, 2) Msg = "orig data =\n%s" % PCA_GenLib.HexDump(orig_data) PCA_GenLib.WriteLog(Msg, 2) self.set_handler("opCode", "sendRoutingInfoForSM", "sendRoutingInfoForSM")
def handle_event(self, AcceptorConnection): command_seq_no = 0 try: Msg = "RequestHandler handle_event Init ..." PCA_GenLib.WriteLog(Msg, 9) Message = None self.response_message = None client_connection_id = "%s" % id(AcceptorConnection) ####################################################### ## ## Read Request from Client ## ####################################################### try: Message = self.readDataFromSocket(AcceptorConnection, Length=1024, TimeOut=5.0, ReadAttempts=1) except KeyError: Msg = "RequestHandler readDataFromSocket Key.error , connection already closed" PCA_GenLib.WriteLog(Msg, 2) Msg = "Del ConnectionLoginState" PCA_GenLib.WriteLog(Msg, 0) try: del self.ConnectionLoginState[id(AcceptorConnection)] except: x = 1 raise socket.error except socket.error: Msg = "RequestHandler readDataFromSocket Client socket.error" PCA_GenLib.WriteLog(Msg, 1) Msg = "close Client connection id=<%s>" % client_connection_id PCA_GenLib.WriteLog(Msg, 1) try: self.ReadSet.remove(AcceptorConnection) self.WriteSet.remove(AcceptorConnection) except: x = 1 AcceptorConnection.close() Msg = "Del ConnectionLoginState" PCA_GenLib.WriteLog(Msg, 0) try: del self.ConnectionLoginState[id(AcceptorConnection)] except: x = 1 raise socket.error Msg = "recv from Client : id=<%s>" % (client_connection_id) PCA_GenLib.WriteLog(Msg, 2) self.parser.parse(Message) command_id = self.handler.get_smpp_command_desc() command_seq_no = self.handler.get_smpp_seq_no() #if resp_data == "deliver_sm": Msg = "recv from Client =*%s*" % command_id PCA_GenLib.WriteLog(Msg, 1) ##################################################################### ### Client Message Maybe more than 1 request ##################################################################### if self.ConnectionLoginState[id(AcceptorConnection)] == 'N': Msg = "recv from Client but no bind request yet ,id=<%s>" % ( client_connection_id) PCA_GenLib.WriteLog(Msg, 0) self.SMPPWriter = PCA_SMPPMessage.SMPP_PDU_Writer( command_seq_no) if command_id == "bind_transceiver": (system_id, system_type, passwd) = self.handler.get_smpp_bind_info() if (system_id == self.UID) and ( system_type == self.TYPE) and (passwd == self.PASSWD): self.SMPPWriter.ConstructHeader( PCA_SMPP_Parameter_Tag.bind_transceiver_resp) self.response_message = self.SMPPWriter.ConstructParameter( "gateway" + chr(0x00)) #Msg = " data =\n%s" % PCA_GenLib.HexDump(self.response_message) #PCA_GenLib.WriteLog(Msg,0) self.ConnectionLoginState[id( self.SocketConnection)] = 'Y' else: Msg = "incorrect bind info , send bind error %s " % ( client_connection_id) PCA_GenLib.WriteLog(Msg, 0) Msg = "input id:<%s>:type:<%s>:pwd:<%s> " % ( system_id, system_type, passwd) PCA_GenLib.WriteLog(Msg, 0) bind_error = chr(0x00) + chr(0x00) + chr(0x00) + chr( 0x0d) self.SMPPWriter.ConstructStatus(bind_error) self.SMPPWriter.ConstructHeader( PCA_SMPP_Parameter_Tag.bind_transmitter_resp) self.response_message = self.SMPPWriter.ConstructParameter( "bind error") elif command_id == "bind_receiver": self.SMPPWriter.ConstructHeader( PCA_SMPP_Parameter_Tag.bind_receiver_resp) self.response_message = self.SMPPWriter.ConstructParameter( "gateway") self.ConnectionLoginState[id(self.SocketConnection)] = 'Y' elif command_id == "submit_sm": Msg = " Incorrect BIND Status %s " % (client_connection_id) PCA_GenLib.WriteLog(Msg, 0) smpp_command_id_incorrect_bind_status = chr(0x00) + chr( 0x00) + chr(0x00) + chr(0x04) self.SMPPWriter.ConstructStatus( smpp_command_id_incorrect_bind_status) self.SMPPWriter.ConstructHeader( PCA_SMPP_Parameter_Tag.submit_sm_resp) self.response_message = self.SMPPWriter.ConstructParameter( "submit error") elif command_id == "unbind": self.SMPPWriter.ConstructHeader( PCA_SMPP_Parameter_Tag.unbind_resp) self.response_message = self.SMPPWriter.ConstructParameter( ) elif command_id == "enquire_link": self.SMPPWriter.ConstructHeader( PCA_SMPP_Parameter_Tag.enquire_link_resp) self.response_message = self.SMPPWriter.ConstructParameter( ) else: Msg = " client message =\n%s" % PCA_GenLib.HexDump( Message) PCA_GenLib.WriteLog(Msg, 0) Msg = "unexpected data ,id=<%s> , no response " % ( client_connection_id) PCA_GenLib.WriteLog(Msg, 0) return self.parser.parse(self.response_message) try: Msg = "send response back to Client " PCA_GenLib.WriteLog(Msg, 0) result = self.sendDataToSocket(AcceptorConnection, self.response_message, TimeOut=0.1, WriteAttempts=3) return except: Msg = "send response back to client error : <%s>,<%s> " % ( sys.exc_type, sys.exc_value) PCA_GenLib.WriteLog(Msg, 0) return try: try: if command_id == "submit_sm": ######### Get one XMLServer Connection fd for send request ############## try: destination_addr = self.handler.get_smpp_destination_addr( ) Msg = "destination_addr = %s" % destination_addr PCA_GenLib.WriteLog(Msg, 5) self.target_id = int(destination_addr) % 3 self.target_id = self.target_id + 1 except: self.target_id = 1 Msg = "delivery_sm_resp use source_id=1" PCA_GenLib.WriteLog(Msg, 1) Msg = "target_id = %s" % self.target_id PCA_GenLib.WriteLog(Msg, 5) for (ClientConnector, Source_id) in self.ClientConnectorList: if int(Source_id) == self.target_id: break ServerID = command_seq_no SocketMutex.acquire() try: Msg = "SocketBuffer %s=%s" % ( command_seq_no, id(AcceptorConnection)) PCA_GenLib.WriteLog(Msg, 1) self.SocketBuffer[ServerID] = AcceptorConnection except: Msg = "get SocketBuffer error =\n%s" % self.SocketBuffer PCA_GenLib.WriteLog(Msg, 1) SocketMutex.release() result = ClientConnector.sendDataToSocket( Message, TimeOutSeconds=0.01, WriteAttempts=1) if result != None: Msg = "send to Server Source_id=<%s> ok " % ( Source_id) PCA_GenLib.WriteLog(Msg, 2) else: Msg = "send to Server Source_id=<%s> failure timeout " % ( Source_id) PCA_GenLib.WriteLog(Msg, 1) Msg = "Close Connection: source_id=*%s* " % Source_id PCA_GenLib.WriteLog(Msg, 1) elif command_id == "enquire_link": self.SMPPWriter = PCA_SMPPMessage.SMPP_PDU_Writer( command_seq_no) self.SMPPWriter.ConstructHeader( PCA_SMPP_Parameter_Tag.enquire_link_resp) self.response_message = self.SMPPWriter.ConstructParameter( ) self.parser.parse(self.response_message) Msg = "send response back to Client " PCA_GenLib.WriteLog(Msg, 0) self.sendDataToSocket(AcceptorConnection, self.response_message, 0.1, 3) elif command_id == "unbind": Msg = "unbind requeset , send unbind and remove id=%s" % id( self.SocketConnection) PCA_GenLib.WriteLog(Msg, 2) self.SMPPWriter = PCA_SMPPMessage.SMPP_PDU_Writer( command_seq_no) self.SMPPWriter.ConstructHeader( PCA_SMPP_Parameter_Tag.unbind_resp) self.response_message = self.SMPPWriter.ConstructParameter( ) self.ConnectionLoginState[id( self.SocketConnection)] = 'N' self.parser.parse(self.response_message) Msg = "send response back to Client " PCA_GenLib.WriteLog(Msg, 0) self.sendDataToSocket(AcceptorConnection, self.response_message, 0.1, 3) elif command_id == "deliver_sm_resp": ServerID = command_seq_no SocketMutex.acquire() try: SOURCD_ID = self.SocketBuffer[ServerID] del self.SocketBuffer[ServerID] Msg = "query SocketBuffer for deliver_sm_resp seq_no : %s we got %s" % ( command_seq_no, SOURCD_ID) PCA_GenLib.WriteLog(Msg, 1) except: Msg = "get SocketBuffer error =\n%s" % self.SocketBuffer PCA_GenLib.WriteLog(Msg, 1) SocketMutex.release() Msg = "delivery_sm request , use SOURCD_ID = 1 " PCA_GenLib.WriteLog(Msg, 1) SOURCD_ID = 1 SocketMutex.release() Msg = "send response back to Client " PCA_GenLib.WriteLog(Msg, 0) for (ClientConnector, Source_id) in self.ClientConnectorList: if int(Source_id) == int(SOURCD_ID): break result = ClientConnector.sendDataToSocket( Message, TimeOutSeconds=0.01, WriteAttempts=1) if result != None: Msg = "send to Server Source_id=<%s> ok " % ( Source_id) PCA_GenLib.WriteLog(Msg, 2) else: Msg = "send to Server Source_id=<%s> failure timeout " % ( Source_id) PCA_GenLib.WriteLog(Msg, 1) Msg = "Close Connection: source_id=*%s* " % Source_id PCA_GenLib.WriteLog(Msg, 1) else: Msg = "un-support command , ignore" PCA_GenLib.WriteLog(Msg, 1) except socket.error: Msg = "send to Primary Server Source_id=<%s> socket error " % ( Source_id) PCA_GenLib.WriteLog(Msg, 2) except: Msg = "error : <%s>,<%s> " % (sys.exc_type, sys.exc_value) PCA_GenLib.WriteLog(Msg, 0) Msg = "nothing send back to Client ........ " PCA_GenLib.WriteLog(Msg, 0) Msg = "RequestHandler handle_event Ok ..." PCA_GenLib.WriteLog(Msg, 9) except socket.error: Msg = "RequestHandler handle_event socket exception,delete socket buffer id=<%s>" % client_connection_id PCA_GenLib.WriteLog(Msg, 3) Msg = "close client connection id=<%s>" % client_connection_id PCA_GenLib.WriteLog(Msg, 1) SocketMutex.acquire() try: del self.SocketBuffer[client_connection_id] except: x = 1 SocketMutex.release() time.sleep(0.1) except: PCA_ThreadLib.SetTerminateFlag("TRUE") Msg = "RequestHandler handle_event error : <%s>,<%s> " % ( sys.exc_type, sys.exc_value) PCA_GenLib.WriteLog(Msg, 0) try: Msg = "close Server connection " PCA_GenLib.WriteLog(Msg, 1) ClientConnector.Close() Msg = "close Client connection " PCA_GenLib.WriteLog(Msg, 1) AcceptorConnection.close() except: x = 1 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) Msg = "PCA DEBUG self.DebugStr=%s" % self.DebugStr PCA_GenLib.WriteLog(Msg, 1) #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 parse_old(self, source): try: Msg = "parser init" PCA_GenLib.WriteLog(Msg,9) orig_data = source name = 'none' self.StartParsing = 0 TID = "na" content = "na" Msg = "MAP shortMORelay Response data =<\n%s\n>" % PCA_GenLib.HexDump(source) PCA_GenLib.WriteLog(Msg,5) 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" 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) source = source[1:] name = "resultretres" tag_name = name attrs = source[0] content = ord(attrs) source = source[1:] name = "length" attrs = source[0] content = ord(attrs) tag_length = content source = source[1:] name = "resultretres value" attrs = source[0:tag_length] #content = ord(attrs) source = attrs name = "opCode" tag_name = name attrs = source[0] content = ord(attrs) source = source[1:] name = "length" attrs = source[0] content = ord(attrs) tag_length = content source = source[1:] name = "opCode value" attrs = source[0] content = ord(attrs) try: tag_value = PCA_MAPParameters.op_code[content] except: Msg = "unknow opCode Value = %s" % content PCA_GenLib.WriteLog(Msg,0) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,tag_name,tag_value) self.set_handler(name,attrs,tag_value) source = source[1:] name = "sm-rp-UI" 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) source = source[1:] name = "sm-rp-UI value" attrs = source[0:tag_length] tag_value = PCA_GenLib.getHexString(attrs) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,tag_name,tag_value) self.set_handler(name,attrs,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,0) Msg = "orig data =\n%s" % PCA_GenLib.HexDump(orig_data) 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,3) tag_index = 0 while len(source) > 0: if number_of_tlv > 100: Msg = "number of TLV > 100 " PCA_GenLib.WriteLog(Msg,0) break tag_index = tag_index + 1 name = "Tag" name = "%s %s" % (name,tag_index) attrs = source[0] content = PCA_GenLib.getHexString(attrs) self.set_handler(name,chr(0x00),content) tag_class = ord(attrs) & 0xc0 tag_class = tag_class >> 6 Tag_Type = 'Primitive' if (ord(attrs) & 0x20): name = "C_%s-" % name name = "%s %s" % (name,tag_index) attrs = source[0:2] content = PCA_GenLib.getHexString(attrs) Tag_Type = 'Constructor' else: name = "P_%s-" % name name = "%s %s" % (name,tag_index) content = ord(attrs) Tag_Type = 'Primitive' name = "%s-%s" % (name,PCA_MAPParameters.tag_class[tag_class]) if Tag_Type == 'Primitive': attrs = struct.pack("!b",ord(attrs) & 0x1f) name = "%s %s" % (name,tag_index) self.set_handler(name,attrs,content) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) if Tag_Type == 'Primitive': source = source[1:] else: source = source[2:] name = "length" name = "%s %s" % (name,tag_index) attrs = source[0] content = ord(attrs) tag_length = content tag_length_form = "short" if tag_length > 128: tag_length_form = "long" attrs = source[1] content = ord(attrs) tag_length = content else: tag_length_form = "short" self.set_handler(name,attrs,content) 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 %s" % (name,tag_index) attrs = source[0:tag_length] if Tag_Type == 'Constructor': content = PCA_GenLib.getHexBCDString(attrs[1:]) else: content = PCA_GenLib.getHexString(attrs) self.set_handler(name,attrs,content) self.DebugStr = "%s,<%s>=<%s>" % (self.DebugStr,name,content) if Tag_Type == 'Constructor': self.parseTLV(attrs) 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 shortMsgGateway SRI 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 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": TOA = PCA_GenLib.getHexString(attrs[0]) content = PCA_GenLib.getHexBCDString(attrs[1:]) content = "%s:%s" % (TOA, content) 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 == "shortMsgGateway_SRI_v3": # # SRI response # self.parseGSM0340_SRI_SM_response(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 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 self.bind_error = 0 start_pos = string.find(source, chr(0x00)) system_id = source[0:start_pos] self.DebugStr = "system_id = <%s>" % system_id 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 = PCA_GenLib.getHexString(source[0:start_pos]) 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) 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)) service_type = source[0:start_pos] self.DebugStr = "system_id = <%s>" % service_type source = source[start_pos + 1:] submit_sm_source_addr_ton = source[0] source_addr_ton = struct.unpack("!b", source[0])[0] self.DebugStr = "%s , source_addr_ton = <%s>" % (self.DebugStr, source_addr_ton) source = source[1:] submit_sm_source_addr_npi = source[0] source_addr_npi = struct.unpack("!b", source[0])[0] self.DebugStr = "%s , source_addr_npi = <%s>" % (self.DebugStr, source_addr_npi) source = source[1:] start_pos = string.find(source, chr(0x00)) source_addr = source[0:start_pos] submit_sm_source_addr = source_addr self.DebugStr = "%s , source_addr = <%s>" % (self.DebugStr, source_addr) source = source[start_pos + 1:] submit_sm_dest_addr_ton = source[0] dest_addr_ton = struct.unpack("!b", source[0])[0] self.DebugStr = "%s , dest_addr_ton = <%s>" % (self.DebugStr, dest_addr_ton) source = source[1:] submit_sm_dest_addr_npi = source[0] dest_addr_npi = struct.unpack("!b", source[0])[0] self.DebugStr = "%s , dest_addr_npi = <%s>" % (self.DebugStr, dest_addr_npi) source = source[1:] start_pos = string.find(source, chr(0x00)) dest_addr = source[0:start_pos] submit_sm_dest_addr = dest_addr self.DebugStr = "%s , dest_addr = <%s>" % (self.DebugStr, dest_addr) name = "dest_address" attrs = dest_addr content = attrs self.set_handler(name, attrs, content) source = source[start_pos + 1:] esm_class = struct.unpack("!b", source[0])[0] self.DebugStr = "%s , esm_class = <%s>" % (self.DebugStr, esm_class) source = source[1:] protocol_id = struct.unpack("!b", source[0])[0] self.DebugStr = "%s , protocol_id = <%s>" % (self.DebugStr, protocol_id) source = source[1:] priority_flag = struct.unpack("!b", source[0])[0] self.DebugStr = "%s , priority_flag = <%s>" % (self.DebugStr, priority_flag) source = source[1:] start_pos = string.find(source, chr(0x00)) schedule_delivery_time = source[0:start_pos] self.DebugStr = "%s , schedule_delivery_time = <%s>" % ( self.DebugStr, schedule_delivery_time) source = source[start_pos + 1:] start_pos = string.find(source, chr(0x00)) validity_period = source[0:start_pos] self.DebugStr = "%s , validity_period = <%s>" % (self.DebugStr, validity_period) CurrentSeconds = time.time() + random.randint(1, 100000) date_tuple = time.localtime(CurrentSeconds) message_id = "%02d%02d%02d%02d" % (date_tuple[2:6]) Msg = "submit sm response msg_id=<%s>" % message_id PCA_GenLib.WriteLog(Msg, 2) name = "message_id" attrs = message_id content = attrs self.set_handler(name, attrs, content) source = source[start_pos + 1:] registered_delivery = struct.unpack("!b", source[0])[0] self.DebugStr = "%s , registered_delivery = <%s>" % ( self.DebugStr, registered_delivery) source = source[1:] replace_if_present_flag = struct.unpack("!b", source[0])[0] self.DebugStr = "%s , replace_if_present_flag = <%s>" % ( self.DebugStr, replace_if_present_flag) source = source[1:] data_coding = struct.unpack("!b", source[0])[0] self.DebugStr = "%s , data_coding = <%s>" % (self.DebugStr, data_coding) source = source[1:] sm_default_msg_id = struct.unpack("!b", source[0])[0] self.DebugStr = "%s , sm_default_msg_id = <%s>" % ( self.DebugStr, sm_default_msg_id) source = source[1:] sm_length = struct.unpack("!B", source[0])[0] self.DebugStr = "%s , sm_length = <%s>" % (self.DebugStr, sm_length) source = source[1:] short_message = source[:sm_length] self.DebugStr = "%s , short_message = <%s>" % (self.DebugStr, short_message) name = "TXT" attrs = short_message content = attrs self.set_handler(name, attrs, content) # submit sm request for DR , prepare delivery sm pdu if registered_delivery == 1: service_type = chr(0x00) source_addr_ton = submit_sm_dest_addr_ton source_addr_npi = submit_sm_dest_addr_npi source_addr = submit_sm_dest_addr + chr(0x00) dest_addr_ton = submit_sm_source_addr_ton dest_addr_npi = submit_sm_source_addr_npi dest_address = submit_sm_source_addr + chr(0x00) esm_class = chr(0x00) protocol_id = chr(0x00) priority_flag = chr(0x00) schedule_delivery_time = chr(0x00) validity_period = chr(0x00) registered_delivery = chr(0x00) replace_if_present_flag = chr(0x00) data_coding = chr(0x00) sm_default_msg_id = chr(0x00) CurrentSeconds = time.time() date_tuple = time.localtime(CurrentSeconds) name = "SUBMIT_DATE" submit_date = "%04d%02d%02d%02d%02d" % (date_tuple[0:5]) submit_date = submit_date[2:] deliver_date = submit_date name = "TEXT" text = "id:%s sub:001 dlvrd:001 submit date:%s done date:%s stat:DELIVRD err:000 text:%s" % ( message_id, submit_date, deliver_date, content[0:20]) short_message = text Msg = "delivery_sm_text=<%s>" % text PCA_GenLib.WriteLog(Msg, 1) sm_length = chr(len(short_message)) self.SMPPWriter.ConstructHeader(self.deliver_sm) parm1 = service_type + source_addr_ton + source_addr_npi + source_addr parm2 = dest_addr_ton + dest_addr_npi + dest_address + esm_class + protocol_id + priority_flag parm3 = schedule_delivery_time + validity_period + registered_delivery #msg:set_param_hex('smpp.tlv_0423', '030000') -- error_code: type GSM no error reference_message_id = chr(0x00) + chr(0x1e) + chr(0x00) + chr( 0x09) + message_id + chr(0x00) message_state = chr(0x04) + chr(0x27) + chr(0x00) + chr( 0x01) + chr(0x02) parm4 = replace_if_present_flag + data_coding + sm_default_msg_id + sm_length + short_message + reference_message_id + message_state DELIVER_SM_PDU = self.SMPPWriter.ConstructParameter( parm1, parm2, parm3, parm4) #Msg = "PCA Construct DELIVERY SM PDU" #PCA_GenLib.WriteLog(Msg,1) #Msg = " data =\n%s" % PCA_GenLib.HexDump(DELIVER_SM_PDU) #PCA_GenLib.WriteLog(Msg,0) name = "DELIVER_SM_PDU" attrs = DELIVER_SM_PDU content = attrs self.set_handler(name, attrs, content) 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 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