internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: secdev/scapy

Issue:
RFC error (from stack over flow)
What do you guys think of this:
http://stackoverflow.com/a/39439585/5459467

> That struct.pack has no chance of working, it at least should be:
> 
> return struct.pack("HH", *self.mladdr)+self.payload.hashnet()
> So that struct.pack has no chance of receiving the two arguments needed for "HH". That is a bug.


Discussion:

Resolving patch:
diff --git a/scapy/layers/inet6.py b/scapy/layers/inet6.py
--- a/scapy/layers/inet6.py
+++ b/scapy/layers/inet6.py
@@ -1352,7 +1352,9 @@ class ICMPv6MLQuery(_ICMPv6ML): # RFC 2710
     overload_fields = {IPv6: { "dst": "ff02::1", "hlim": 1, "nh": 58 }} 
     def hashret(self):
         if self.mladdr != "::":
-            return struct.pack("HH",self.mladdr)+self.payload.hashret()
+            return (
+                inet_pton(socket.AF_INET6, self.mladdr) + self.payload.hashret()
+            )
         else:
             return self.payload.hashret()
         


Test patch:
Repository: secdev/scapy

Issue:
trace3D() is broken
Here is the backtrack obtained by calling `trace3D()` on a `TracerouteResult` object

```python
>>> tr.trace3D()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "scapy/layers/inet.py", line 1055, in trace3D
    trace = self.get_trace()
  File "scapy/layers/inet.py", line 1040, in get_trace
    m = min(x for x, y in k.itervalues() if y[1])
  File "scapy/layers/inet.py", line 1040, in <genexpr>
    m = min(x for x, y in k.itervalues() if y[1])
TypeError: 'bool' object has no attribute '__getitem__'
```



Discussion:

Resolving patch:
diff --git a/scapy/layers/inet.py b/scapy/layers/inet.py
--- a/scapy/layers/inet.py
+++ b/scapy/layers/inet.py
@@ -1037,7 +1037,7 @@ def get_trace(self):
             trace[d][s[IP].ttl] = r[IP].src, ICMP not in r
         for k in trace.itervalues():
             try:
-                m = min(x for x, y in k.itervalues() if y[1])
+                m = min(x for x, y in k.itervalues() if y)
             except ValueError:
                 continue
             for l in k.keys():  # use .keys(): k is modified in the loop
diff --git a/scapy/layers/inet6.py b/scapy/layers/inet6.py
--- a/scapy/layers/inet6.py
+++ b/scapy/layers/inet6.py
@@ -2998,7 +2998,7 @@ def get_trace(self):
 
         for k in trace.itervalues():
             try:
-                m = min(x for x, y in k.itervalues() if y[1])
+                m = min(x for x, y in k.itervalues() if y)
             except ValueError:
                 continue
             for l in k.keys():  # use .keys(): k is modified in the loop


Test patch:
Repository: secdev/scapy

Issue:
unknown.version in egg.info and in the banner
Hi,

I'm porting 2.3.3 to OpenBSD. I had two issues:
1. after the install with setuptools, the .egg-info generated is called 'lib/python2.7/site-packages/scapy-unknown.version-py2.7.egg-info'

I patched setup.py to hardcode the version then it worked
```
--- setup.py.orig       Tue Oct 18 10:44:43 2016
+++ setup.py    Mon Oct 31 17:19:45 2016
@@ -47,7 +47,7 @@ if os.name == "nt":
 
 setup(
     name='scapy',
-    version=__import__('scapy').VERSION,
+    version='2.3.3',
     packages=[
         'scapy',
         'scapy/arch',

```
I now have lib/python2.7/site-packages/scapy-2.3.3-py2.7.egg-info

2. running scapy it says "Welcome to Scapy (unknown.version)" even with the setup.py change. I went through scapy-2.3.3/scapy/main.py but I didn't find anything suspicious.


Discussion:
Can you check if the code provided in #326 fixes the problem? Thanks!

Both problem are still there with your diff.

OK I think I understand. You're getting the archive from a Github link and are not using `git clone`, are you?

That's it, it fetches https://github.com/secdev/scapy/archive/v2.3.3/scapy-2.3.3.tar.gz

Then the updated PR contains a patch for `scapy/__init__.py` that should work for you.

With just `scapy/__init__.py` patch, the .egg-info is solved.
I still have the greeting "Welcome to Scapy (unknown.version)".
So what should I do in your opinion? Backport only the `__init__.py` or also the setup.py?
What should I expect to be fixed? Only the .egg-info thing or also the greeter?

Hi @danieljakots,

you should use this URL:

https://pypi.python.org/packages/ac/14/c792a14b9f8bc4bb9c74c0594c167a2da36e31964098d9e27202142cbd7d/scapy-2.3.3.tgz#md5=65939356f08760ebef836796d3320b3b

instead of github.

The version will be correct.

New archives should be downloaded from here: https://pypi.python.org/simple/scapy/

Hey @robin-jarry fetching them through pypi removes the need to patch the setup.py but I still have "Welcome to Scapy (unknown.version)"


Resolving patch:
diff --git a/scapy/__init__.py b/scapy/__init__.py
--- a/scapy/__init__.py
+++ b/scapy/__init__.py
@@ -71,7 +71,17 @@ def _version():
                 tag = f.read()
             return tag
         except:
-            return 'unknown.version'
+            # Rely on git archive "export-subst" git attribute.
+            # See 'man gitattributes' for more details.
+            git_archive_id = '$Format:%h %d$'
+            sha1 = git_archive_id.strip().split()[0]
+            match = re.search(r'tag:(\S+)', git_archive_id)
+            if match:
+                return match.group(1)
+            elif sha1:
+                return sha1
+            else:
+                return 'unknown.version'
 
 VERSION = _version()
 


Test patch:
Repository: secdev/scapy

Issue:
Fixed a bug in the sendrecv method when using multi-threading
Like other people on the Internet I used to have troubles with the sr1 method, when using multi-threading (and pyqt4 in my case).
To reproduce the bug, start a simple Qt application, then try to send and receive any packet using the sr1 method wherever you want and it will result in something like the following:

```
      File "TestCase.py", line 123, in sendSYNnRecACK
        synackpkt = sr1(pck, verbose=0, timeout=1, retry=-2)
      File "/usr/local/lib/python2.7/dist-packages/scapy/sendrecv.py", line 354, in sr1 
        a,b=sndrcv(s,x,*args,**kargs)
      File "/usr/local/lib/python2.7/dist-packages/scapy/sendrecv.py", line 135, in sndrcv
        inp, out, err = select(inmask,[],[], remaintime)
    select.error: (4, 'Appel syst\xc3\xa8me interrompu')
```

The last error meaning "System call interrupted".

As I am not a good developer it may be awful code (I let you judge of that) but it allowed me to solve my issue.



Discussion:

Resolving patch:
diff --git a/scapy/sendrecv.py b/scapy/sendrecv.py
--- a/scapy/sendrecv.py
+++ b/scapy/sendrecv.py
@@ -10,7 +10,8 @@
 import errno
 import cPickle,os,sys,time,subprocess
 import itertools
-from select import select
+from select import select, error as select_error
+
 from scapy.arch.consts import DARWIN, FREEBSD, OPENBSD
 from scapy.data import *
 from scapy.config import conf
@@ -140,8 +141,9 @@ def sndrcv(pks, pkt, timeout = None, inter = 0, verbose=None, chainCC=0, retry=0
                                 inp = []
                                 try:
                                     inp, out, err = select(inmask,[],[], remaintime)
-                                except IOError, exc:
-                                    if exc.errno != errno.EINTR:
+                                except (IOError, select_error) as exc:
+                                    # select.error has no .errno attribute
+                                    if exc.args[0] != errno.EINTR:
                                         raise
                                 if len(inp) == 0:
                                     break


Test patch:
Repository: secdev/scapy

Issue:
Invalid GRE proto binding for Ether over GRE
```
>>> GRE() / Ether()
<GRE  proto=0x1 |<Ether  |>>
```

According to https://tools.ietf.org/html/rfc1701 the valid protocol number should be 0x6558.


Discussion:

Resolving patch:
diff --git a/scapy/arch/windows/__init__.py b/scapy/arch/windows/__init__.py
--- a/scapy/arch/windows/__init__.py
+++ b/scapy/arch/windows/__init__.py
@@ -33,6 +33,8 @@
     socket.IPPROTO_AH=51
 if not hasattr(socket, 'IPPROTO_ESP'):
     socket.IPPROTO_ESP=50
+if not hasattr(socket, 'IPPROTO_GRE'):
+    socket.IPPROTO_GRE=47
 
 from scapy.arch import pcapdnet
 from scapy.arch.pcapdnet import *
diff --git a/scapy/layers/inet6.py b/scapy/layers/inet6.py
--- a/scapy/layers/inet6.py
+++ b/scapy/layers/inet6.py
@@ -3861,6 +3861,7 @@ def ra_reply_callback(req, iface):
 
 bind_layers(Ether,     IPv6,     type = 0x86dd )
 bind_layers(CookedLinux, IPv6,   proto = 0x86dd )
+bind_layers(GRE,       IPv6,     proto = 0x86dd )
 bind_layers(Loopback,  IPv6,     type = 0x1c )
 bind_layers(IPerror6,  TCPerror, nh = socket.IPPROTO_TCP )
 bind_layers(IPerror6,  UDPerror, nh = socket.IPPROTO_UDP )
@@ -3869,3 +3870,4 @@ def ra_reply_callback(req, iface):
 bind_layers(IP,        IPv6,     proto = socket.IPPROTO_IPV6 )
 bind_layers(IPv6,      IPv6,     nh = socket.IPPROTO_IPV6 )
 bind_layers(IPv6,      IP,       nh = socket.IPPROTO_IPIP )
+bind_layers(IPv6,      GRE,      nh = socket.IPPROTO_GRE )
diff --git a/scapy/layers/l2.py b/scapy/layers/l2.py
--- a/scapy/layers/l2.py
+++ b/scapy/layers/l2.py
@@ -1090,7 +1090,7 @@ class Dot1AD(Dot1Q):
 bind_layers( GRE,           LLC,           proto=122)
 bind_layers( GRE,           Dot1Q,         proto=33024)
 bind_layers( GRE,           Dot1AD,        type=0x88a8)
-bind_layers( GRE,           Ether,         proto=1)
+bind_layers( GRE,           Ether,         proto=0x6558)
 bind_layers( GRE,           ARP,           proto=2054)
 bind_layers( GRE,           EAPOL,         proto=34958)
 bind_layers( GRE,           GRErouting,    { "routing_present" : 1 } )


Test patch:
Repository: secdev/scapy

Issue:
Fuzzing random crash
Performing
`>>> str(fuzz(IP()/ICMP()))`
randomly crash:

```
>>> str(fuzz(IP()/ICMP()))
'U\x83\x00\x1c\x1fb\xe0\x00\xa3\x01\xc7\xda\xc0\xa8\x00w\x7f\x00\x00\x01\xa5\xc1\xb9\xfcn\xcf1r'
>>> str(fuzz(IP()/ICMP()))
Traceback (most recent call last):
  File "<console>", line 2, in <module>
  File "C:\Python27\lib\site-packages\scapy\packet.py", line 280, in __str__
    return self.build()
  File "C:\Python27\lib\site-packages\scapy\packet.py", line 357, in build
    p = self.do_build()
  File "C:\Python27\lib\site-packages\scapy\packet.py", line 347, in do_build
    pay = self.do_build_payload()
  File "C:\Python27\lib\site-packages\scapy\packet.py", line 339, in do_build_payload
    return self.payload.do_build()
  File "C:\Python27\lib\site-packages\scapy\packet.py", line 344, in do_build
    pkt = self.self_build()
  File "C:\Python27\lib\site-packages\scapy\packet.py", line 335, in self_build
    p = f.addfield(self, p, val)
  File "C:\Python27\lib\site-packages\scapy\fields.py", line 149, in addfield
    return self.fld.addfield(pkt,s,val)
  File "C:\Python27\lib\site-packages\scapy\fields.py", line 74, in addfield
    return s+struct.pack(self.fmt, self.i2m(pkt,val))
error: 'H' format requires 0 <= number <= 65535
```

@guedou What you used in https://github.com/secdev/scapy/pull/522


Discussion:
That's a _known problem_ with `fuzz()`. As far as I remember, `fuzz()` feeds invalid (i.e out of bounds) values. I did not try to fix it. 

Resolving patch:
diff --git a/scapy/packet.py b/scapy/packet.py
--- a/scapy/packet.py
+++ b/scapy/packet.py
@@ -1459,9 +1459,10 @@ def fuzz(p, _inplace=0):
                     print "fuzzing", repr(r)
                     fuzz(r, _inplace=1)
             elif f.default is not None:
-                rnd = f.randval()
-                if rnd is not None:
-                    q.default_fields[f.name] = rnd
+                if not isinstance(f, ConditionalField) or f._evalcond(q):
+                    rnd = f.randval()
+                    if rnd is not None:
+                        q.default_fields[f.name] = rnd
         q = q.payload
     return p
 


Test patch:
diff --git a/test/regression.uts b/test/regression.uts
--- a/test/regression.uts
+++ b/test/regression.uts
@@ -403,7 +403,7 @@ bind_layers(IP, ICMP, frag=0, proto=1)
 = fuzz
 ~ not_pypy
 random.seed(0x2807)
-str(fuzz(IP()/ICMP())) == '\xe5S\x00\x1c\x9dC \x007\x01(H\x7f\x00\x00\x01\x7f\x00\x00\x01*\xdb\xf7,9\x8e\xa4i'
+str(fuzz(IP()/ICMP())) == 'u\x14\x00\x1c\xc2\xf6\x80\x00\xde\x01k\xd3\x7f\x00\x00\x01\x7f\x00\x00\x01y\xc9>\xa6\x84\xd8\xc2\xb7'
 
 = Building some packets
 ~ basic IP TCP UDP NTP LLC SNAP Dot11
Repository: secdev/scapy

Issue:
srloop produces output despite of verbose = 0 (almost mute)
# cat testsrloop.py
```
from scapy.all import srloop,IP,ICMP

print "aa"
srloop(IP(dst="8.8.8.8") / ICMP(), verbose=0, count=1)
print "bb"

```
# python testsrloop.py
aa
bb**nd...**

_Note extra output here_

# pip freeze |grep scapy
scapy==2.3.3
#



Discussion:

Resolving patch:
diff --git a/scapy/sendrecv.py b/scapy/sendrecv.py
--- a/scapy/sendrecv.py
+++ b/scapy/sendrecv.py
@@ -436,7 +436,8 @@ def __sr_loop(srfunc, pkts, prn=lambda x:x[1].summary(), prnfail=lambda x:x.summ
                     break
                 count -= 1
             start = time.time()
-            print("\rsend...\r", end=' ')
+            if verbose > 1:
+                print("\rsend...\r", end=' ')
             res = srfunc(pkts, timeout=timeout, verbose=0, chainCC=1, *args, **kargs)
             n += len(res[0])+len(res[1])
             r += len(res[0])


Test patch:
Repository: secdev/scapy

Issue:
Dot11QoS QoS Control bits in reverse order
Looking at the 802.11-2012 spec and Wireshark output, it appears that the TID, EOSP, Ack Policy, and Reserved fields are in reverse order of what they should be.  I made Pull Request #785 with what I think is the correct ordering.

I modified the existing `Dot11QoS - build` unit test by adding a TID parameter to the Dot11Qos layer substituting the actual string that I got with my updated version.  If you would prefer a separate test, I could create a separate test.

Before this change Wireshark and Scapy reported different TIDs for a locally captured packet trace.  With this change, Scapy and Wireshark agree on TID values.


Discussion:

Resolving patch:
diff --git a/scapy/layers/dot11.py b/scapy/layers/dot11.py
--- a/scapy/layers/dot11.py
+++ b/scapy/layers/dot11.py
@@ -178,10 +178,10 @@ def unwep(self, key=None, warn=1):
 
 class Dot11QoS(Packet):
     name = "802.11 QoS"
-    fields_desc = [ BitField("TID",None,4),
-                    BitField("EOSP",None,1),
+    fields_desc = [ BitField("Reserved",None,1),
                     BitField("Ack Policy",None,2),
-                    BitField("Reserved",None,1),
+                    BitField("EOSP",None,1),
+                    BitField("TID",None,4),
                     ByteField("TXOP",None) ]
     def guess_payload_class(self, payload):
         if isinstance(self.underlayer, Dot11):


Test patch:
diff --git a/test/regression.uts b/test/regression.uts
--- a/test/regression.uts
+++ b/test/regression.uts
@@ -8611,8 +8611,8 @@ Dot11 in p and p.addr3 == "00:00:00:00:00:00"
 p.mysummary() == '802.11 Management 0 00:00:00:00:00:00 > 00:00:00:00:00:00'
 
 = Dot11QoS - build
-s = str(Dot11(type=2, subtype=8)/Dot11QoS())
-s == b'\x88\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
+s = str(Dot11(type=2, subtype=8)/Dot11QoS(TID=4))
+s == b'\x88\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
 
 = Dot11QoS - dissection
 p = Dot11(s)
Repository: secdev/scapy

Issue:
test_pyx problem
I have a unit test that uses scapy library like this:
```
$ cat ut.py 
from scapy.all import *

def test_foo():
    pass
```
The problem is that testing framework (pytest) detects internal scapy function test_pyx as a test:
```
ut.py::test_foo PASSED
ut.py::test_pyx <- venv/src/scapy/scapy/consts.py PASSED
```
This is because test_pyx function from scapy/consts.py is unnecessarily imported from scapy.all
and pytest treats all test_* functions as tests.

Scapy from current master branch.




Discussion:

Resolving patch:
diff --git a/scapy/consts.py b/scapy/consts.py
--- a/scapy/consts.py
+++ b/scapy/consts.py
@@ -27,7 +27,7 @@
     MATPLOTLIB_DEFAULT_PLOT_KARGS = dict()
     log_loading.info("Can't import matplotlib. Won't be able to plot.")
 
-def test_pyx():
+def _test_pyx():
     """Returns if PyX is correctly installed or not"""
     try:
         with open(os.devnull, 'wb') as devnull:
@@ -39,7 +39,7 @@ def test_pyx():
 
 try:
     import pyx
-    if test_pyx():
+    if _test_pyx():
         PYX = 1
     else:
         log_loading.warning("PyX dependencies are not installed ! Please install TexLive or MikTeX.")


Test patch:
diff --git a/test/regression.uts b/test/regression.uts
--- a/test/regression.uts
+++ b/test/regression.uts
@@ -9148,8 +9148,8 @@ test_afterglow()
 
 = psdump()
 
-print(test_pyx())
-if test_pyx():
+print("PYX: %d" % PYX)
+if PYX:
     import tempfile
     import os
     filename = tempfile.mktemp(suffix=".ps")
@@ -9160,8 +9160,8 @@ if test_pyx():
 
 = pdfdump()
 
-print(test_pyx())
-if test_pyx():
+print("PYX: %d" % PYX)
+if PYX:
     import tempfile
     import os
     filename = tempfile.mktemp(suffix=".pdf")
Repository: secdev/scapy

Issue:
scapy import fail: NameError: name 'get_working_if' is not defined
Hello,

On an Ubuntu 16.04.3 LTS, when launching `scapy` CLI or importing `scapy.route` without any interface in use, I got the following error:
```Python
Traceback (most recent call last):
  File "/usr/local/bin/scapy", line 25, in <module>
    interact()
  File "tools/scapy/scapy/main.py", line 421, in interact
    init_session(session_name, mydict)
  File "tools/scapy/scapy/main.py", line 293, in init_session
    scapy_builtins = {k: v for k, v in six.iteritems(importlib.import_module(".all", "scapy").__dict__) if _validate_local(k)}
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "tools/scapy/scapy/all.py", line 25, in <module>
    from scapy.route import *
  File "tools/scapy/scapy/route.py", line 195, in <module>
    conf.iface = get_working_if()
NameError: name 'get_working_if' is not defined
```

A bisect leads to the recent commit fd50b349263256e0aaa69780937ae02d4f4ee46c, more likely to [this code](https://github.com/secdev/scapy/commit/fd50b349263256e0aaa69780937ae02d4f4ee46c#diff-3fc486d3e1085d11c80c20bab07375f2R194)

I'm not sure to correctly understand for this code snippet and how it works on the different OS. This is why I prefer opening an issue :smiley: 


Discussion:
How about
```
diff --git a/scapy/route.py b/scapy/route.py
index 580fe012..986b00d4 100644
--- a/scapy/route.py
+++ b/scapy/route.py
@@ -11,7 +11,7 @@ from __future__ import absolute_import
 from scapy.utils import atol, ltoa, itom, pretty_routes
 from scapy.config import conf
 from scapy.error import Scapy_Exception, warning
-from scapy.arch import WINDOWS
+from scapy.arch import WINDOWS, get_working_if
 import scapy.consts
 import scapy.modules.six as six
```
(sorry if my commits break so many things... But all those things are not shown during unit tests :/)

Resolving patch:
diff --git a/scapy/route.py b/scapy/route.py
--- a/scapy/route.py
+++ b/scapy/route.py
@@ -11,7 +11,7 @@
 from scapy.utils import atol, ltoa, itom, pretty_routes
 from scapy.config import conf
 from scapy.error import Scapy_Exception, warning
-from scapy.arch import WINDOWS
+from scapy.arch import WINDOWS, get_working_if
 import scapy.consts
 import scapy.modules.six as six
 


Test patch:
Repository: secdev/scapy

Issue:
Automaton.graph broken on Linux
Hello,

The `do_graph` function in [scapy.utils](https://github.com/secdev/scapy/blob/master/scapy/utils.py#L467) on `!WINDOWS` will act as:
```Python
...
            target = "| %s" % conf.prog.display
...
    if isinstance(target, str):
        target = open(os.path.abspath(target), "wb")
```

As a result, asking for the graph of an Automaton will lead to the creation of a file named `| display` and no graph displayed.

To reproduce:
```Python
from scapy.all import *

class Test(Automaton):

    @ATMT.state(initial=True)
    def xxx(self):
        pass

Test.graph()
```
This will creates the file mentioned above.

A bisect leads to the commit 1fd5116590e760b8cd43ce41b8aaedec25251bcb which indeed removes the call to `os.popen2`.
This is not a PR because I'm not sure on how to fix this bug without breaking the behavior on others OS...



Discussion:

Resolving patch:
diff --git a/scapy/utils.py b/scapy/utils.py
--- a/scapy/utils.py
+++ b/scapy/utils.py
@@ -429,7 +429,7 @@ def __enter__(self):
     def __exit__(self, exc_type, exc_value, traceback):
         if exc_type == OSError:
             msg = "%s: executing %r failed"
-            log_scapy.error(msg, self.name, conf.prog.wireshark, exc_info=1)
+            log_runtime.error(msg, self.name, conf.prog.wireshark, exc_info=1)
             return True  # Suppress the exception
 
 class ContextManagerCaptureOutput(object):
@@ -491,14 +491,19 @@ def do_graph(graph,prog=None,format=None,target=None,type=None,string=None,optio
             target = get_temp_file(autoext="."+format)
             start_viewer = True
         else:
-            target = "| %s" % conf.prog.display
+            with ContextManagerSubprocess("do_graph()"):
+                target = subprocess.Popen([conf.prog.display], stdin=subprocess.PIPE).stdin
     if format is not None:
         format = "-T%s" % format
     if isinstance(target, str):
         target = open(os.path.abspath(target), "wb")
-    proc = subprocess.Popen("%s %s %s" % (prog, options or "", format or ""),
+    proc = subprocess.Popen("\"%s\" %s %s" % (prog, options or "", format or ""),
                             shell=True, stdin=subprocess.PIPE, stdout=target)
     proc.communicate(input=raw(graph))
+    try:
+        target.close()
+    except:
+        pass
     if start_viewer:
         # Workaround for file not found error: We wait until tempfile is written.
         waiting_start = time.time()


Test patch: