def initParser(self): if self.fileName: try: fd = open(self.fileName, "rb") except Exception, e: print "ERROR: file not loaded. Exception = {0}".format(str(e)) # Read first 4 bytes of diameter event(which is part of 20 byte header) [msgLen, Version] = utils.getFlagAndLength(fd.read(4)) # set file descriptor to start of file fd.seek(0) data = fd.read(msgLen) fd.close()
def findSimpleElementLocation(self, avpCode, msgSize): codeFound = False size = self.nwBuffer.tell() #bytes o_ParsedData = ParsedData() while (size < msgSize) and not codeFound: #print "DEBUG: {0} : {1} : {2}".format(size, msgSize, self.nwBuffer.tell()) # read the AVP Code fmt = struct.Struct('>I') try: (avp,) = fmt.unpack(self.nwBuffer.read(4)) except Exception, e: print "ERROR: Major formatting Issue {0}. Exception : {1}".format(avpCode, str(e)) return o_ParsedData if avp == avpCode: codeFound = True ## Flags And Length [avpLen, avpFlags] = utils.getFlagAndLength(self.nwBuffer.read(4)) avpLenPadded = avpLen + utils.calculatePadding(avpLen) size += 8 if codeFound: o_ParsedData.isParsed = True o_ParsedData.avpSize = avpLen o_ParsedData.padding = utils.calculatePadding(avpLen) if (utils.existVendorID(avpFlags)): # read vendor-id (vendorID,) = fmt.unpack(self.nwBuffer.read(4)) o_ParsedData.vendorID = vendorID size += 4 ## Set the network buffer to the data. ## It's already set to data due to above read. ## So, we can return safely from here codeFound = False return o_ParsedData else: unwanted = self.nwBuffer.read(avpLenPadded - 8) ## (8 bytes is already read) size += (avpLenPadded - 8)
print "ERROR: {0}".format(str(e)) return False fileSize = os.path.getsize(FileName) try: [part_1, ext] = os.path.basename(FileName).split('.') except Exception, e: print "Provide some extension to the file" sys.exit(1) eventCount= 0 fileCount = 1 while True: # Read first 4 bytes of diameter event(which is part of 20 byte header) [msgLen, _version] = utils.getFlagAndLength(fd.read(4)) # move back by 4 bytes fd.seek(-4, 1) data = fd.read(msgLen) eventCount += 1 curSize = fd.tell() if fileSize == curSize: break fd.close() return eventCount if __name__ == "__main__": if len(sys.argv) < 2: