From a0fd8688dc405799b676e21d7a0741512ad9aca1 Mon Sep 17 00:00:00 2001
From: Thomas Faivre <thomas.faivre@6wind.com>
Date: Thu, 9 Mar 2017 11:55:13 +0100
Subject: [PATCH] Fix FlagValue dump behavior.

When running the command method on a packet, by default, the __repr__
method of the value is used to dump a field.
Although, the __repr__ method of the new FlagValue class is not a valid
python syntax:

In [2]: IP(flags=0).command()
Out[2]: 'IP(flags=<Flag 0 ()>)'

In [3]: IP(flags=<Flag 0 ()>)
  File "<ipython-input-3-1e8a4a1eb25a>", line 1
    IP(flags=<Flag 0 ()>)
             ^
SyntaxError: invalid syntax

Also, when print a FlagValue with value of 0, flagrepr returns an empty
string which can be confusing:

In [1]: IP(flags=0)
Out[1]: <IP  flags= |>

Use int value instead in both cases.

Fixes: fc6a4caaf1f7 ("Introduce FlagValue(int) objects to represent FlagsField() values")
Signed-off-by: Thomas Faivre <thomas.faivre@6wind.com>
---
 scapy/fields.py | 2 +-
 scapy/packet.py | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/scapy/fields.py b/scapy/fields.py
index 0843adc4..86ccf781 100644
--- a/scapy/fields.py
+++ b/scapy/fields.py
@@ -1053,7 +1053,7 @@ class FlagValue(object):
                 r.append(self.names[i])
             i += 1
             x >>= 1
-        return ("+" if self.multi else "").join(r)
+        return ("+" if self.multi else "").join(r) if r else int(self)
     def __repr__(self):
         return "<Flag %d (%s)>" % (self, self.flagrepr())
     def __deepcopy__(self, memo):
diff --git a/scapy/packet.py b/scapy/packet.py
index 2ac07d15..cfd760b5 100644
--- a/scapy/packet.py
+++ b/scapy/packet.py
@@ -1196,6 +1196,8 @@ A side effect is that, to obtain "{" and "}" characters, you must use
                 fv = fv.command()
             elif fld.islist and fld.holds_packets and type(fv) is list:
                 fv = "[%s]" % ",".join( map(Packet.command, fv))
+            elif isinstance(fld, FlagsField):
+                fv = int(fv)
             else:
                 fv = repr(fv)
             f.append("%s=%s" % (fn, fv))
-- 
GitLab