-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Fix FlagsField & dissection improvements #2035
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -204,8 +204,12 @@ def do_init_cached_fields(self): | |
|
||
# Deepcopy default references | ||
for fname in Packet.class_default_fields_ref[cls_name]: | ||
value = copy.deepcopy(self.default_fields[fname]) | ||
setattr(self, fname, value) | ||
value = self.default_fields[fname] | ||
try: | ||
self.fields[fname] = value.copy() | ||
except AttributeError: | ||
# Python 2.7 - list only | ||
self.fields[fname] = value[:] | ||
|
||
def prepare_cached_fields(self, flist): | ||
""" | ||
|
@@ -435,8 +439,13 @@ def __repr__(self): | |
repr(self.payload), | ||
ct.punct(">")) | ||
|
||
def __str__(self): | ||
return str(self.build()) | ||
if six.PY2: | ||
def __str__(self): | ||
return self.build() | ||
else: | ||
def __str__(self): | ||
warning("Calling str(pkt) on Python 3 makes no sense!") | ||
return str(self.build()) | ||
|
||
def __bytes__(self): | ||
return self.build() | ||
|
@@ -488,9 +497,14 @@ def copy_fields_dict(self, fields): | |
def clear_cache(self): | ||
"""Clear the raw packet cache for the field and all its subfields""" | ||
self.raw_packet_cache = None | ||
for _, fval in six.iteritems(self.fields): | ||
if isinstance(fval, Packet): | ||
fval.clear_cache() | ||
for fld, fval in six.iteritems(self.fields): | ||
fld = self.get_field(fld) | ||
if fld.holds_packets: | ||
if isinstance(fval, Packet): | ||
fval.clear_cache() | ||
elif isinstance(fval, list): | ||
for fsubval in fval: | ||
fsubval.clear_cache() | ||
self.payload.clear_cache() | ||
|
||
def self_build(self, field_pos_list=None): | ||
|
@@ -838,9 +852,12 @@ def guess_payload_class(self, payload): | |
""" | ||
for t in self.aliastypes: | ||
for fval, cls in t.payload_guess: | ||
if all(hasattr(self, k) and v == self.getfieldval(k) | ||
for k, v in six.iteritems(fval)): | ||
return cls | ||
try: | ||
if all(v == self.getfieldval(k) | ||
for k, v in six.iteritems(fval)): | ||
return cls | ||
except AttributeError: | ||
pass | ||
|
||
return self.default_payload_class(payload) | ||
|
||
def default_payload_class(self, payload): | ||
|
@@ -924,19 +941,25 @@ def __iterlen__(self): | |
six.iteritems(self.overloaded_fields)) | ||
if isinstance(val, VolatileValue)] + list(self.fields) | ||
length = 1 | ||
|
||
def is_valid_gen_tuple(x): | ||
if not isinstance(x, tuple): | ||
return False | ||
return len(x) == 2 and all(isinstance(z, int) for z in x) | ||
|
||
for field in fields: | ||
fld, val = self.getfield_and_val(field) | ||
if hasattr(val, "__iterlen__"): | ||
length *= val.__iterlen__() | ||
elif isinstance(val, tuple) and len(val) == 2 and all(hasattr(z, "__int__") for z in val): # noqa: E501 | ||
length *= (val[1] - val[0]) | ||
elif is_valid_gen_tuple(val): | ||
length *= (val[1] - val[0] + 1) | ||
|
||
elif isinstance(val, list) and not fld.islist: | ||
len2 = 0 | ||
for x in val: | ||
if hasattr(x, "__iterlen__"): | ||
len2 += x.__iterlen__() | ||
elif isinstance(x, tuple) and len(x) == 2 and all(hasattr(z, "__int__") for z in x): # noqa: E501 | ||
len2 += (x[1] - x[0]) | ||
elif is_valid_gen_tuple(x): | ||
len2 += (x[1] - x[0] + 1) | ||
elif isinstance(x, list): | ||
len2 += len(x) | ||
else: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10706,15 +10706,6 @@ assert not any(getattr(p1.flags | p2.flags, f) for f in 'FRPECN') | |
assert TCP(flags="SA").flags & TCP(flags="S").flags == TCP(flags="S").flags | ||
assert TCP(flags="SA").flags | TCP(flags="S").flags == TCP(flags="SA").flags | ||
|
||
= Using tuples and lists as flag values | ||
~ IP TCP | ||
|
||
plist = PacketList(list(IP()/TCP(flags=(0, 2**9 - 1)))) | ||
assert [p[TCP].flags for p in plist] == [x for x in range(512)] | ||
|
||
plist = PacketList(list(IP()/TCP(flags=["S", "SA", "A"]))) | ||
assert [p[TCP].flags for p in plist] == [2, 18, 16] | ||
|
||
|
||
|
||
############ | ||
############ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was triggered by the
FlagsField
bug