Fix zero padding
- zfill() is a func of the string to be putting in a '0'. bytearray().zfill(n) -> bytearray(n) - unify notation of zero with other code. '\0' -> '\x00' Signed-off-by: HIYAMA Manabu <hiyama.manabu@po.ntts.co.jp> Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
parent
19c205b897
commit
fc264cc971
@ -62,7 +62,7 @@ class ipv4(packet_base.PacketBase):
|
||||
return msg, ipv4.get_packet_type(proto)
|
||||
|
||||
def serialize(self, payload, prev):
|
||||
hdr = bytearray().zfill(self.header_length * 4)
|
||||
hdr = bytearray(self.header_length * 4)
|
||||
version = self.version << 4 | self.header_length
|
||||
flags = self.flags << 13 | self.offset
|
||||
if self.total_length == 0:
|
||||
|
@ -54,7 +54,7 @@ class tcp(packet_base.PacketBase):
|
||||
return msg, None
|
||||
|
||||
def serialize(self, payload, prev):
|
||||
h = bytearray().zfill(self.length)
|
||||
h = bytearray(self.length)
|
||||
offset = self.offset << 4
|
||||
struct.pack_into(tcp._PACK_STR, h, 0, self.src_port, self.dst_port,
|
||||
self.seq, self.ack, offset, self.bits,
|
||||
@ -69,7 +69,7 @@ class tcp(packet_base.PacketBase):
|
||||
ph = struct.pack('!IIBBH', prev.src, prev.dst, 0, 6, length)
|
||||
f = ph + h + payload
|
||||
if len(f) % 2:
|
||||
f += '\0'
|
||||
f += '\x00'
|
||||
self.csum = socket.htons(packet_utils.checksum(f))
|
||||
struct.pack_into('!H', h, 16, self.csum)
|
||||
return h
|
||||
|
@ -47,7 +47,7 @@ class udp(packet_base.PacketBase):
|
||||
ph = struct.pack('!IIBBH', prev.src, prev.dst, 0, 17, self.length)
|
||||
f = ph + h + payload
|
||||
if len(f) % 2:
|
||||
f += '\0'
|
||||
f += '\x00'
|
||||
self.csum = socket.htons(packet_utils.checksum(f))
|
||||
h = struct.pack(udp._PACK_STR, self.src_port, self.dst_port,
|
||||
self.length, self.csum)
|
||||
|
@ -93,7 +93,7 @@ class MsgBase(object):
|
||||
|
||||
self.version = self.datapath.ofproto.OFP_VERSION
|
||||
self.msg_type = self.cls_msg_type
|
||||
self.buf = bytearray().zfill(self.datapath.ofproto.OFP_HEADER_SIZE)
|
||||
self.buf = bytearray(self.datapath.ofproto.OFP_HEADER_SIZE)
|
||||
|
||||
def _serialize_header(self):
|
||||
# buffer length is determined after trailing data is formated.
|
||||
@ -122,7 +122,7 @@ class MsgBase(object):
|
||||
|
||||
def msg_pack_into(fmt, buf, offset, *args):
|
||||
if len(buf) < offset:
|
||||
buf += bytearray().zfill(offset - len(buf))
|
||||
buf += bytearray(offset - len(buf))
|
||||
|
||||
if len(buf) == offset:
|
||||
buf += struct.pack(fmt, *args)
|
||||
@ -130,7 +130,7 @@ def msg_pack_into(fmt, buf, offset, *args):
|
||||
|
||||
needed_len = offset + struct.calcsize(fmt)
|
||||
if len(buf) < needed_len:
|
||||
buf += bytearray().zfill(needed_len - len(buf))
|
||||
buf += bytearray(needed_len - len(buf))
|
||||
|
||||
struct.pack_into(fmt, buf, offset, *args)
|
||||
|
||||
|
@ -252,7 +252,7 @@ class TestMsgPackInto(unittest.TestCase):
|
||||
def _test_msg_pack_into(self, offset_type='e'):
|
||||
fmt = '!HH'
|
||||
len_ = struct.calcsize(fmt)
|
||||
buf = bytearray().zfill(len_)
|
||||
buf = bytearray(len_)
|
||||
offset = len_
|
||||
arg1 = 1
|
||||
arg2 = 2
|
||||
|
Loading…
Reference in New Issue
Block a user