Skip to content
Snippets Groups Projects
Commit 4d8b461e authored by Guillaume Valadon's avatar Guillaume Valadon Committed by GitHub
Browse files

Merge pull request #545 from p-l-/enh-flags

Flags: allow operations (| & ==) with strings
parents f982008e a6b12fc6
No related branches found
No related tags found
No related merge requests found
...@@ -1015,40 +1015,33 @@ class LEFieldLenField(FieldLenField): ...@@ -1015,40 +1015,33 @@ class LEFieldLenField(FieldLenField):
class FlagValue(object): class FlagValue(object):
__slots__ = ["value", "names", "multi"] __slots__ = ["value", "names", "multi"]
@staticmethod def _fixvalue(self, value):
def __fixvalue(value, names):
if isinstance(value, basestring): if isinstance(value, basestring):
if isinstance(names, list): value = value.split('+') if self.multi else list(value)
value = value.split('+')
else:
value = list(value)
if isinstance(value, list): if isinstance(value, list):
y = 0 y = 0
for i in value: for i in value:
y |= 1 << names.index(i) y |= 1 << self.names.index(i)
value = y value = y
return value return None if value is None else int(value)
def __init__(self, value, names): def __init__(self, value, names):
self.value = (value.value if isinstance(value, self.__class__)
else self.__fixvalue(value, names))
self.multi = isinstance(names, list) self.multi = isinstance(names, list)
self.names = names self.names = names
self.value = self._fixvalue(value)
def __int__(self): def __int__(self):
return self.value return self.value
def __cmp__(self, other): def __cmp__(self, other):
if isinstance(other, self.__class__): return cmp(self.value, self._fixvalue(other))
return cmp(self.value, other.value)
return cmp(self.value, other)
def __and__(self, other): def __and__(self, other):
return self.__class__(self.value & int(other), self.names) return self.__class__(self.value & self._fixvalue(other), self.names)
__rand__ = __and__ __rand__ = __and__
def __or__(self, other): def __or__(self, other):
return self.__class__(self.value | int(other), self.names) return self.__class__(self.value | self._fixvalue(other), self.names)
__ror__ = __or__ __ror__ = __or__
def __lshift__(self, other): def __lshift__(self, other):
return self.value << int(other) return self.value << self._fixvalue(other)
def __rshift__(self, other): def __rshift__(self, other):
return self.value >> int(other) return self.value >> self._fixvalue(other)
def __nonzero__(self): def __nonzero__(self):
return bool(self.value) return bool(self.value)
def flagrepr(self): def flagrepr(self):
......
...@@ -7211,6 +7211,12 @@ assert not pkt.flags.MF ...@@ -7211,6 +7211,12 @@ assert not pkt.flags.MF
assert pkt.flags.DF assert pkt.flags.DF
assert not pkt.flags.evil assert not pkt.flags.evil
assert repr(pkt.flags) == '<Flag 2 (DF)>' assert repr(pkt.flags) == '<Flag 2 (DF)>'
pkt.flags |= 'evil+MF'
pkt.flags &= 'DF+MF'
assert pkt.flags.MF
assert pkt.flags.DF
assert not pkt.flags.evil
assert repr(pkt.flags) == '<Flag 3 (MF+DF)>'
pkt = IP(flags=3) pkt = IP(flags=3)
assert pkt.flags.MF assert pkt.flags.MF
...@@ -7240,6 +7246,12 @@ assert pkt.flags.U ...@@ -7240,6 +7246,12 @@ assert pkt.flags.U
assert pkt.flags.AU assert pkt.flags.AU
assert not any(getattr(pkt.flags, f) for f in 'FSRPECN') assert not any(getattr(pkt.flags, f) for f in 'FSRPECN')
assert repr(pkt.flags) == '<Flag 48 (AU)>' assert repr(pkt.flags) == '<Flag 48 (AU)>'
pkt.flags &= 'SFA'
pkt.flags |= 'P'
assert pkt.flags.P
assert pkt.flags.A
assert pkt.flags.PA
assert not any(getattr(pkt.flags, f) for f in 'FSRUECN')
pkt = TCP(flags=56) pkt = TCP(flags=56)
assert all(getattr(pkt.flags, f) for f in 'PAU') assert all(getattr(pkt.flags, f) for f in 'PAU')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment