Python struct.pack() Examples

The following are 30 code examples for showing how to use struct.pack(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may want to check out the right sidebar which shows the related API usage.

You may also want to check out all available functions/classes of the module struct , or try the search function .

Example 1
Project: vergeml   Author: mme   File: cache.py    License: MIT License 6 votes vote down vote up
def __init__(self, path, mode):
        assert mode in ("r", "w")

        self.path = path
        self.file = open(self.path, mode + "b")
        self.mmfile = None
        self.mode = mode
        self.cnt = _CacheFileContent()

        if mode == "r":
            # Read the last part of the file which contains the contents of the
            # cache.
            self.cnt.read(self.file, self.path)
            self.mmfile = mmap.mmap(self.file.fileno(), 0, access=mmap.ACCESS_READ)
        else:
            # The 8 bytes header contain the position of the content index.
            # We fill this header with zeroes and write the actual position
            # once all samples have been written to the cache
            self.file.write(struct.pack('<Q', 0)) 
Example 2
Project: ALF   Author: blackberry   File: ogg.py    License: Apache License 2.0 6 votes vote down vote up
def fuzz(self, fuzzer):
        """Return a fuzzed version of this page."""
        # TODO, for these vorbis header pages, should fuzz the parameters within structures
        # https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-600004.2
        #print "fuzz",
        if self.special == b"\x01vorbis":
            #print "spc1",
            hdr = self.fuzz_header(fuzzer)
            vorbhdr = bytes(fuzzer(self.data[0][7:])[:22])
            data = [b"".join([b"\x01vorbis", vorbhdr, b"\x01" * (23 - len(vorbhdr))])]
        elif self.special: # unknown "special" page
            #print "spcx (k)"
            return b"".join([self.header] + self.data)
        else:
            #print "nspc",
            hdr = self.fuzz_header(fuzzer)
            data = [bytes(fuzzer(i)[:255]) for i in self.data[:255]]
        out = b"".join([hdr, bytes([len(data)])] + [bytes([len(i)]) for i in data] + data)
        pagecrc = ogg_crc32(out)
        out = b"".join([out[:22], struct.pack("<I", pagecrc), out[26:]])
        #print "ok"
        return out 
Example 3
Project: multibootusb   Author: mbusb   File: win32.py    License: GNU General Public License v2.0 6 votes vote down vote up
def ZapMBRGPT(self, disk_size, sector_size, add1MB):
        self.assert_physical_drive()
        # Implementation borrowed from rufus: https://github.com/pbatard/rufus
        num_sectors_to_clear \
            = (add1MB and 2048 or 0) + self.MAX_SECTORS_TO_CLEAR
        zeroBuf = b'\0' * sector_size
        for i in range(num_sectors_to_clear):
            self.WriteFile(zeroBuf)
        offset = disk_size - self.MAX_SECTORS_TO_CLEAR * sector_size
        win32file.SetFilePointer(self.h, offset, win32con.FILE_BEGIN)
        for i in range(num_sectors_to_clear):
            self.WriteFile(zeroBuf)
        # We need to append paddings as CREATE_DISK structure contains a union.
        param = struct.pack('<IIIHH8s',
                            winioctlcon.PARTITION_STYLE_MBR, 0xdeadbeef,
                            0,0,0,b'abcdefgh')
        win32file.DeviceIoControl(
            self.h, winioctlcon.IOCTL_DISK_CREATE_DISK, param, 0, None) 
Example 4
Project: The-chat-room   Author: 11ze   File: vachat.py    License: MIT License 6 votes vote down vote up
def run(self):
        while True:
            try:
                self.sock.connect(self.ADDR)
                break
            except:
                time.sleep(3)
                continue
        print("AUDIO client connected...")
        self.stream = self.p.open(format=FORMAT,
                             channels=CHANNELS,
                             rate=RATE,
                             input=True,
                             frames_per_buffer=CHUNK)
        while self.stream.is_active():
            frames = []
            for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
                data = self.stream.read(CHUNK)
                frames.append(data)
            senddata = pickle.dumps(frames)
            try:
                self.sock.sendall(struct.pack("L", len(senddata)) + senddata)
            except:
                break 
Example 5
Project: knob   Author: francozappa   File: cmds.py    License: MIT License 6 votes vote down vote up
def hciCallback(self, record):
                hcipkt, orig_len, inc_len, flags, drops, recvtime = record

                dummy = "\x00\x00\x00"      # TODO: Figure out purpose of these fields
                direction = p8(flags & 0x01)
                packet = dummy + direction + hcipkt.getRaw()
                length = len(packet)
                ts_sec =  recvtime.second #+ timestamp.minute*60 + timestamp.hour*60*60 #FIXME timestamp not set
                ts_usec = recvtime.microsecond
                pcap_packet = struct.pack('@ I I I I', ts_sec, ts_usec, length, length) + packet
                try:
                    self.wireshark_process.stdin.write(pcap_packet)
                    self.wireshark_process.stdin.flush()
                    log.debug("HciMonitorController._callback: done")
                except IOError as e:
                    log.warn("HciMonitorController._callback: broken pipe. terminate.")
                    self.killMonitor() 
Example 6
Project: knob   Author: francozappa   File: cmds.py    License: MIT License 6 votes vote down vote up
def lmpCallback(self, lmp_packet, sendByOwnDevice, src, dest, timestamp):
                eth_header = dest + src + "\xff\xf0"
                meta_data  = "\x00"*6 if sendByOwnDevice else "\x01\x00\x00\x00\x00\x00"
                packet_header = "\x19\x00\x00" + p8(len(lmp_packet)<<3 | 7)

                packet = eth_header + meta_data + packet_header + lmp_packet
                packet += "\x00\x00" # CRC
                length = len(packet)
                ts_sec =  timestamp.second + timestamp.minute*60 + timestamp.hour*60*60
                ts_usec = timestamp.microsecond
                pcap_packet = struct.pack('@ I I I I', ts_sec, ts_usec, length, length) + packet
                try:
                    self.wireshark_process.stdin.write(pcap_packet)
                    self.wireshark_process.stdin.flush()
                    log.debug("LmpMonitorController._callback: done")
                except IOError as e:
                    log.warn("LmpMonitorController._callback: broken pipe. terminate.")
                    self.killMonitor() 
Example 7
Project: XFLTReaT   Author: earthquake   File: TCP_generic.py    License: MIT License 6 votes vote down vote up
def send(self, channel_type, message, additional_data):
		if channel_type == common.CONTROL_CHANNEL_BYTE:
			transformed_message = self.transform(self.encryption, common.CONTROL_CHANNEL_BYTE+message, 1)
		else:
			transformed_message = self.transform(self.encryption, common.DATA_CHANNEL_BYTE+message, 1)

		common.internal_print("{0} sent: {1}".format(self.module_short, len(transformed_message)), 0, self.verbosity, common.DEBUG)

		# WORKAROUND?!
		# Windows: It looks like when the buffer fills up the OS does not do
		# congestion control, instead throws and exception/returns with
		# WSAEWOULDBLOCK which means that we need to try it again later.
		# So we sleep 100ms and hope that the buffer has more space for us.
		# If it does then it sends the data, otherwise tries it in an infinite
		# loop...
		while True:
			try:
				return self.comms_socket.send(struct.pack(">H", len(transformed_message))+transformed_message)
			except socket.error as se:
				if se.args[0] == 10035: # WSAEWOULDBLOCK
					time.sleep(0.1)
					pass
				else:
					raise 
Example 8
Project: XFLTReaT   Author: earthquake   File: DNS.py    License: MIT License 6 votes vote down vote up
def cmh_autotune(self, module, message, additional_data, cm):
		message = message[len(self.CONTROL_AUTOTUNE)+2:]
		# get tune type, requested record type, length and encoding for crafting the answer
		(query_type, RRtype, length, encode_class) = struct.unpack("<BHHH", message[0:7])
		if self.DNS_proto.get_RR_type(RRtype)[0] == None:
			return True
		
		# extra parameters added to be able to response in the proper way
		additional_data = additional_data + (True, self.download_encoding_list[encode_class], self.DNS_proto.get_RR_type(RRtype)[0])		
		if (query_type == 0) or (query_type == 3):
			# record && downstream length discovery
			message = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(length))
		if query_type == 1:
			# A record name length discovery
			message = struct.pack("<i", binascii.crc32(message[7:]))
		if query_type == 2:
			# checking download encoding, echoing back request payload
			message = message[7:]

		module.send(common.CONTROL_CHANNEL_BYTE, self.CONTROL_AUTOTUNE_CLIENT+message, additional_data)
		return True

	# tune control message handler
	# client sets the record type and encodings by calling this
	# server side 
Example 9
Project: XFLTReaT   Author: earthquake   File: DNS.py    License: MIT License 6 votes vote down vote up
def do_changesettings(self, additional_data):
		message = struct.pack("<HHHH", self.settings[4], self.settings[0], self.settings[1], self.settings[3])
		self.send(common.CONTROL_CHANNEL_BYTE, self.CONTROL_TUNEME+message, additional_data)

		if (self.record_list[self.settings[4]][0] == "CNAME"):
			self.recordtype = self.settings[5]
		else:
			self.recordtype = self.record_list[self.settings[4]][0]
		self.RRtype_num = self.DNS_proto.reverse_RR_type_num(self.recordtype)
		self.upload_encoding_class = self.upload_encoding_list[self.settings[0]]
		self.download_encoding_class = self.download_encoding_list[self.settings[1]]

		self.qMTU = self.DNS_proto.reverse_RR_type("A")[4](self.settings[2]-1, self.hostname, 3, self.upload_encoding_class)
		self.set_mtu_ugly(self.DNS_proto.reverse_RR_type(self.recordtype)[4](self.settings[3]-1, "", 3, self.download_encoding_class))

		return

	# function used for autotuning, crafting tune type requests, sending the 
	# packets then checking the answer.
	# return
	# True: if the packet went thru and the answer is correct. Encoding, size
	# 	were correct
	# False: packet failed. Too big or crippled packet, wrong encoding etc. 
Example 10
Project: XFLTReaT   Author: earthquake   File: encoding.py    License: MIT License 6 votes vote down vote up
def decode(self, text):
		if len(text) % 5:
			return None

		decoded_text = ""
		text = text.replace("{",".")
		for i in range(0, len(text)/5):
			encoded_text = text[i*5:(i+1)*5]

			N0 = ord(encoded_text[0]) - 33
			N1 = ord(encoded_text[1]) - 33
			N2 = ord(encoded_text[2]) - 33
			N3 = ord(encoded_text[3]) - 33
			N4 = ord(encoded_text[4]) - 33
			c = N0*52200625 + N1*614125 + N2*7225 + N3*85 + N4

			decoded_text += struct.pack(">I", c)
		return decoded_text 
Example 11
Project: XFLTReaT   Author: earthquake   File: dns_proto.py    License: MIT License 6 votes vote down vote up
def build_answer(self, transaction_id, record, orig_question):
		if record == None:
			flag = 0x8503 # 1000 0100 0000 0011
			answer_num = 0
			answers = ""
			additional_record_num = 0
			additional_records = ""
		else:
			flag = 0x8500 #	1000 0100 0000 0000
			RRtype = self.reverse_RR_type(record[0])
			if RRtype[1] == None:
				answer_num = 0
				answers = ""
				additional_record_num = 0
				additional_records = ""
			else:
				answer_num = 1
				(answer_num, answers, additional_record_num, additional_records) = RRtype[1](record)

		dns_header = struct.pack(">HHHHHH", transaction_id, flag, 1, answer_num, 0, additional_record_num)

		return dns_header + orig_question + answers + additional_records 
Example 12
Project: XFLTReaT   Author: earthquake   File: interface.py    License: MIT License 6 votes vote down vote up
def freebsd_tun_alloc(self, dev, flags):
		try:
			sockfd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
			ifr = struct.pack('<16si', 'tun', 0)
			self.iface_name = fcntl.ioctl(sockfd, self.IOCTL_FREEBSD_SIOCIFCREATE2, ifr)
			self.iface_name = self.iface_name.rstrip("\x00")

			buff = array.array('c', dev+"\x00")
			caddr_t, _ = buff.buffer_info()
			ifr = struct.pack('16sP', self.iface_name, caddr_t);

			fcntl.ioctl(sockfd, self.IOCTL_FREEBSD_SIOCSIFNAME, ifr)
			tun = os.open("/dev/"+self.iface_name, os.O_RDWR | os.O_NONBLOCK)
			self.iface_name = dev

		except IOError as e:
			print e
			common.internal_print("Error: Cannot create tunnel. Is {0} in use?".format(dev), -1)
			sys.exit(-1)

		return tun 
Example 13
Project: smbprotocol   Author: jborean93   File: structure.py    License: MIT License 6 votes vote down vote up
def pack(self):
        """
        Packs the field value into a byte string so it can be sent to the
        server.

        :param structure: The message structure class object
        :return: A byte string of the packed field's value
        """
        value = self._get_calculated_value(self.value)
        packed_value = self._pack_value(value)
        size = self._get_calculated_size(self.size, packed_value)
        if len(packed_value) != size:
            raise ValueError("Invalid packed data length for field %s of %d "
                             "does not fit field size of %d"
                             % (self.name, len(packed_value), size))

        return packed_value 
Example 14
Project: smbprotocol   Author: jborean93   File: structure.py    License: MIT License 6 votes vote down vote up
def __init__(self, size=None, **kwargs):
        """
        [MS-DTYP] 0.0 2017-09-15

        2.3.3 FILETIME
        The FILETIME structure is a 64-it value that represents the number of
        100 nanoseconds intervals that have elapsed since January 1, 1601 UTC.
        This is used to convert the FILETIME int value to a native Python
        datetime object.

        While the format FILETIME is used when communicating with the server,
        this type allows Python code to interact with datetime objects natively
        with all the conversions handled at pack/unpack time.

        :param size: Must be set to None or 8, this is so we can check/override
        :param kwargs: Any other kwarg to be sent to Field()
        """
        if not (size is None or size == 8):
            raise InvalidFieldDefinition("DateTimeField type must have a size "
                                         "of 8 not %d" % size)
        super(DateTimeField, self).__init__(size=8, **kwargs) 
Example 15
Project: ipmisim   Author: rhtyd   File: ipmisim.py    License: Apache License 2.0 6 votes vote down vote up
def __new__(cls, *args, **kwargs):
        if cls.__instance == None or (len(args) > 0 and args[0] == 'reset'):
            cls.__instance = object.__new__(cls)
            cls.__instance.name = "IpmiServer Context"

            # Initialize ctx state
            self = cls.__instance
            self.device_name = "CloudStack IPMI Sim"
            self.sessions = dict()
            self.uuid = uuid.uuid4()
            self.kg = None
            self.authdata = collections.OrderedDict()

            lanchannel = 1
            authtype = 0b10000000
            authstatus = 0b00000100
            chancap = 0b00000010
            oemdata = (0, 0, 0, 0)
            self.authcap = struct.pack('BBBBBBBBB', 0, lanchannel, authtype, authstatus, chancap, *oemdata)
            self.bmc = self._configure_users()
            logger.info('CloudStack IPMI Sim BMC initialized')
        return cls.__instance 
Example 16
Project: facebook-wda   Author: openatx   File: usbmux.py    License: MIT License 6 votes vote down vote up
def send_packet(self, payload: dict, reqtype: int = 8):
        """
        Args:
            payload: required

            # The following args only used in the first request
            reqtype: request type, always 8 
            tag: int
        """
        body_data = plistlib.dumps(payload)
        if self._first:  # first package
            length = 16 + len(body_data)
            header = struct.pack(
                "IIII", length, 1, reqtype,
                self._tag)  # version: 1, request: 8(?), tag: 1(?)
        else:
            header = struct.pack(">I", len(body_data))
        self.sendall(header + body_data) 
Example 17
Project: Paradrop   Author: ParadropLabs   File: addresses.py    License: Apache License 2.0 6 votes vote down vote up
def maxIpaddr(ipaddr, netmask):
    """
        Takes a quad dot format IP address string and makes it the largest valid value still in the same subnet.

        Returns:
            Max quad dot IP string or None if error
    """
    try:
        val = struct.unpack("!I", socket.inet_aton(ipaddr))[0]
        nm = struct.unpack("!I", socket.inet_aton(netmask))[0]
        inc = struct.unpack("!I", socket.inet_aton("0.0.0.254"))[0]
        val &= nm
        val |= inc
        return socket.inet_ntoa(struct.pack('!I', val))
    except Exception:
        return None 
Example 18
Project: Gurux.DLMS.Python   Author: Gurux   File: GXDLMSIp4Setup.py    License: GNU General Public License v2.0 5 votes vote down vote up
def setValue(self, settings, e):
        if e.index == 1:
            self.logicalName = _GXCommon.toLogicalName(e.value)
        elif e.index == 2:
            if isinstance(e.value, str):
                self.dataLinkLayerReference = e.value
            else:
                self.dataLinkLayerReference = _GXCommon.toLogicalName(e.value)
        elif e.index == 3:
            self.ipAddress = socket.inet_ntoa(struct.pack("!I", e.value))
        elif e.index == 4:
            self.multicastIPAddress = []
            if e.value:
                for it in e.value:
                    self.multicastIPAddress.append(socket.inet_ntoa(struct.pack("!I", it)))
        elif e.index == 5:
            self.ipOptions = []
            if e.value:
                for it in e.value:
                    item = GXDLMSIp4SetupIpOption()
                    item.type_ = it[0]
                    item.length = it[1]
                    item.data = it[2]
                    self.ipOptions.append(item)
        elif e.index == 6:
            self.subnetMask = socket.inet_ntoa(struct.pack("!I", e.value))
        elif e.index == 7:
            self.gatewayIPAddress = socket.inet_ntoa(struct.pack("!I", e.value))
        elif e.index == 8:
            self.useDHCP = e.value
        elif e.index == 9:
            self.primaryDNSAddress = socket.inet_ntoa(struct.pack("!I", e.value))
        elif e.index == 10:
            self.secondaryDNSAddress = socket.inet_ntoa(struct.pack("!I", e.value))
        else:
            e.error = ErrorCode.READ_WRITE_DENIED 
Example 19
Project: Gurux.DLMS.Python   Author: Gurux   File: GXByteBuffer.py    License: GNU General Public License v2.0 5 votes vote down vote up
def setFloat(self, value, index=None):
        if index is None:
            self.setFloat(value, self.size)
            self.size += 4
        else:
            self.set(struct.pack("f", value), index) 
Example 20
Project: Gurux.DLMS.Python   Author: Gurux   File: GXByteBuffer.py    License: GNU General Public License v2.0 5 votes vote down vote up
def setDouble(self, value, index=None):
        if index is None:
            self.setDouble(value, self.size)
            self.size += 8
        else:
            self.set(struct.pack("d", value), index) 
Example 21
Project: vergeml   Author: mme   File: cache.py    License: MIT License 5 votes vote down vote up
def write(self, file):
        """Write the content index to file and update the header.
        """
        pos = file.tell()
        pickle.dump((self.index, self.meta, self.info), file)
        file.seek(0)

        # update the header with the position of the content index.
        file.write(struct.pack('<Q', pos)) 
Example 22
Project: vergeml   Author: mme   File: cache.py    License: MIT License 5 votes vote down vote up
def write(self, data, meta):

        if isinstance(data, tuple) and len(data) == 2:
            # write (x,y) pairs

            # serialize independent from each other
            type1, data1 = self._serialize_data(data[0])
            type2, data2 = self._serialize_data(data[1])

            pos = len(data1)
            data = io.BytesIO()

            # an entry wich consists of two items carries the position
            # of the second item in its header.
            data.write(struct.pack('<Q', pos))

            data.write(data1)
            data.write(data2)
            data = data.getvalue()

            # mark the entry as pair
            type_ = (type1, type2)

        else:
            type_, data = self._serialize_data(data)

        super().write(data, meta)
        self.cnt.info.append(type_) 
Example 23
Project: ALF   Author: blackberry   File: SockPuppet.py    License: Apache License 2.0 5 votes vote down vote up
def send_data(self, data):
        is_ack = (data["cmd"] == self.ACK)
        data = pickle.dumps(data, pickle.HIGHEST_PROTOCOL)
        data_len = len(data)
        assert data_len < 0xFFFFFFFF, "Transfer too large!"
        log.debug("-> sending %d bytes", data_len)
        self.conn.sendall(struct.pack("I", data_len))
        self.conn.sendall(data)
        if not is_ack:
            assert self.recv_data()["cmd"] == self.ACK
            log.debug("ACK received") 
Example 24
Project: ALF   Author: blackberry   File: file_fixer.py    License: Apache License 2.0 5 votes vote down vote up
def fix_png(data):
    """
    Fix the signature and checksums on a fuzzed PNG image.
    """
    out = [b"\x89PNG\r\n\x1A\n"]
    data = bytes(data[8:])
    chunk = 0
    while len(data) >= 8:
        chunklen = data[:4]
        out.append(chunklen)
        chunklen = struct.unpack("!I", chunklen)[0]
        if chunk == 0:
            chunkname = b"IHDR" # make sure the first tag is correct
        else:
            chunkname = data[4:8]
            #chunkname = bytes(_coerce_ascii(c) for c in data[4:8])
        out.append(chunkname)
        data = data[8:]
        if len(data) < chunklen:
            break
        else:
            chunkdata = data[:chunklen]
            chunkcrc = zlib.crc32(chunkname) & 0xFFFFFFFF
            chunkcrc = zlib.crc32(chunkdata, chunkcrc) & 0xFFFFFFFF
            out.append(chunkdata)
            out.append(struct.pack("!I", chunkcrc))
            data = data[chunklen+4:] # skip the old crc
        chunk += 1
    out.append(data)
    return b"".join(out) 
Example 25
Project: wechatpy   Author: wechatpy   File: base.py    License: MIT License 5 votes vote down vote up
def _encrypt(self, text, _id):
        text = to_binary(text)
        tmp_list = []
        tmp_list.append(to_binary(self.get_random_string()))
        length = struct.pack(b"I", socket.htonl(len(text)))
        tmp_list.append(length)
        tmp_list.append(text)
        tmp_list.append(to_binary(_id))

        text = b"".join(tmp_list)
        text = PKCS7Encoder.encode(text)

        ciphertext = to_binary(self.cipher.encrypt(text))
        return base64.b64encode(ciphertext) 
Example 26
Project: cherrypy   Author: cherrypy   File: encoding.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def compress(body, compress_level):
    """Compress 'body' at the given compress_level."""
    import zlib

    # See http://www.gzip.org/zlib/rfc-gzip.html
    yield b'\x1f\x8b'       # ID1 and ID2: gzip marker
    yield b'\x08'           # CM: compression method
    yield b'\x00'           # FLG: none set
    # MTIME: 4 bytes
    yield struct.pack('<L', int(time.time()) & int('FFFFFFFF', 16))
    yield b'\x02'           # XFL: max compression, slowest algo
    yield b'\xff'           # OS: unknown

    crc = zlib.crc32(b'')
    size = 0
    zobj = zlib.compressobj(compress_level,
                            zlib.DEFLATED, -zlib.MAX_WBITS,
                            zlib.DEF_MEM_LEVEL, 0)
    for line in body:
        size += len(line)
        crc = zlib.crc32(line, crc)
        yield zobj.compress(line)
    yield zobj.flush()

    # CRC32: 4 bytes
    yield struct.pack('<L', crc & int('FFFFFFFF', 16))
    # ISIZE: 4 bytes
    yield struct.pack('<L', size & int('FFFFFFFF', 16)) 
Example 27
Project: iSDX   Author: sdn-ixp   File: utils.py    License: Apache License 2.0 5 votes vote down vote up
def craft_arp_packet(packet, dst_mac):
    arp_packet = [
        packet["htype"],
        packet["ptype"],
        packet["hlen"],
        packet["plen"],
        struct.pack("!h", 2),
        binascii.unhexlify(dst_mac.replace(':', '')),
        socket.inet_aton(packet["dst_ip"]),
        binascii.unhexlify(packet["src_mac"].replace(':', '')),
        socket.inet_aton(packet["src_ip"])]

    return arp_packet 
Example 28
Project: iSDX   Author: sdn-ixp   File: utils.py    License: Apache License 2.0 5 votes vote down vote up
def craft_garp_response(SPA, TPA, SHA, THA, eth_src, eth_dst):
    "Craft Gratuitous ARP Response Message"
    """
    Format ARP reply:
    eth_src = VMAC, eth_dst = requester_mac, SHA = VMAC, SPA = vnhip, THA = requester_mac, TPA = requester_ip

    Format gratuitous ARP:
    eth_src = VMAC, eth_dst = 00..00<part_id>, SHA = VMAC, SPA = vnhip, THA = VMAC, TPA = vnhip
    """
    arp_packet = [
        # HTYPE
        struct.pack("!h", 1),
        # PTYPE (IPv4)
        struct.pack("!h", 0x0800),
        # HLEN
        struct.pack("!B", 6),
        # PLEN
        struct.pack("!B", 4),
        # OPER (reply)
        struct.pack("!h", 2),
        # SHA
        binascii.unhexlify(SHA.replace(':', '')),
        # SPA
        socket.inet_aton(str(SPA)),
        # THA
        binascii.unhexlify(THA.replace(':', '')),
        # TPA
        socket.inet_aton(str(TPA))
    ]
    eth_frame = [
        # Destination address:
        binascii.unhexlify(eth_dst.replace(':', '')),
        # Source address:
        binascii.unhexlify(eth_src.replace(':', '')),
        # Protocol
        struct.pack("!h", ETH_TYPE_ARP),
        # Data
        ''.join(arp_packet)
    ]
    return ''.join(eth_frame) 
Example 29
Project: iSDX   Author: sdn-ixp   File: decision_process.py    License: Apache License 2.0 5 votes vote down vote up
def long_to_ip(ip):
    return socket.inet_ntoa(struct.pack('!L', ip)) 
Example 30
Project: kaldi-python-io   Author: funcwj   File: _io_kernel.py    License: Apache License 2.0 5 votes vote down vote up
def write_int32(fd, int32):
    """
        Write a int32 number
    """
    fd.write(str.encode('\04'))
    int_pack = struct.pack('i', int32)
    fd.write(int_pack)