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)
def stringEncoder(content): padding = utils.calculatePadding(len(content)) packedData = struct.pack("{0}s{1}x".format(len(content), padding), str(content)) return [packedData, len(content), padding]
padding = utils.calculatePadding(len(content)) packedData = struct.pack("{0}s{1}x".format(len(content), padding), str(content)) return [packedData, len(content), padding] def ipv4AddressEncoder(content): import socket try: packedData = socket.inet_aton(content) except Exception, e: print "WARN: incorrect IP address: {0}".format(content) packedData = socket.inet_aton("10.10.10.10") return [packedData, 4, 0] def ipv4AddressEncoderE164(content): import socket try: ip = socket.inet_aton(content) pack = struct.pack("!2B4s", 0, 1, ip).encode("hex") except Exception, e: print "WARN: incorrect IP address: {0}".format(content) pack = socket.inet_aton("10.10.10.10") pack = struct.pack("!2B4s", 0, 1, pack).encode("hex") return [pack, len(pack), utils.calculatePadding(len(pack))]