1*7dc08ffcSJunyu Lai# How to contribute 2*7dc08ffcSJunyu Lai 3*7dc08ffcSJunyu LaiContributors are essential to Scapy (as they are to most open source 4*7dc08ffcSJunyu Laiprojects). Here is some advice to help you help the project! 5*7dc08ffcSJunyu Lai 6*7dc08ffcSJunyu Lai## Project objectives 7*7dc08ffcSJunyu Lai 8*7dc08ffcSJunyu LaiWe try to keep Scapy as powerful as possible, to support as many 9*7dc08ffcSJunyu Laiprotocols and platforms as possible, to keep and make the code (and 10*7dc08ffcSJunyu Laithe commit history) as clean as possible. 11*7dc08ffcSJunyu Lai 12*7dc08ffcSJunyu LaiSince Scapy can be slow and memory consuming, we try to limit CPU and 13*7dc08ffcSJunyu Laimemory usage, particularly in parts of the code often called. 14*7dc08ffcSJunyu Lai 15*7dc08ffcSJunyu Lai## What to contribute? 16*7dc08ffcSJunyu Lai 17*7dc08ffcSJunyu LaiYou want to spend to time working on Scapy but have no (or little) 18*7dc08ffcSJunyu Laiidea what to do? You can look for open issues 19*7dc08ffcSJunyu Lai[labeled "contributions wanted"](https://github.com/secdev/scapy/labels/contributions%20wanted), or look at the [contributions roadmap](https://github.com/secdev/scapy/issues/399) 20*7dc08ffcSJunyu Lai 21*7dc08ffcSJunyu LaiIf you have any ideas of useful contributions that you cannot (or do 22*7dc08ffcSJunyu Lainot want to) do yourself, open an issue and use the label 23*7dc08ffcSJunyu Lai"contributions wanted". 24*7dc08ffcSJunyu Lai 25*7dc08ffcSJunyu LaiOnce you have chosen a contribution, open an issue to let other people 26*7dc08ffcSJunyu Laiknow you're working on it (or assign the existing issue to yourself) 27*7dc08ffcSJunyu Laiand track your progress. You might want to ask whether you're working 28*7dc08ffcSJunyu Laiin an appropriate direction, to avoid the frustration of seeing your 29*7dc08ffcSJunyu Laicontribution rejected after a lot of work. 30*7dc08ffcSJunyu Lai 31*7dc08ffcSJunyu Lai## Reporting issues 32*7dc08ffcSJunyu Lai 33*7dc08ffcSJunyu Lai### Questions 34*7dc08ffcSJunyu Lai 35*7dc08ffcSJunyu LaiIt is OK so submit issues to ask questions (more than OK, 36*7dc08ffcSJunyu Laiencouraged). There is a label "question" that you can use for that. 37*7dc08ffcSJunyu Lai 38*7dc08ffcSJunyu Lai### Bugs 39*7dc08ffcSJunyu Lai 40*7dc08ffcSJunyu LaiIf you have installed Scapy through a package manager (from your Linux 41*7dc08ffcSJunyu Laior BSD system, from PyPI, etc.), please get and install the current 42*7dc08ffcSJunyu Laidevelopment code, and check that the bug still exists before 43*7dc08ffcSJunyu Laisubmitting an issue. 44*7dc08ffcSJunyu Lai 45*7dc08ffcSJunyu LaiPlease label your issues "bug". 46*7dc08ffcSJunyu Lai 47*7dc08ffcSJunyu LaiIf you're not sure whether a behavior is a bug or not, submit an issue 48*7dc08ffcSJunyu Laiand ask, don't be shy! 49*7dc08ffcSJunyu Lai 50*7dc08ffcSJunyu Lai### Enhancements / feature requests 51*7dc08ffcSJunyu Lai 52*7dc08ffcSJunyu LaiIf you want a feature in Scapy, but cannot implement it yourself or 53*7dc08ffcSJunyu Laiwant some hints on how to do that, open an issue with label 54*7dc08ffcSJunyu Lai"enhancement". 55*7dc08ffcSJunyu Lai 56*7dc08ffcSJunyu LaiExplain if possible the API you would like to have (e.g., give examples 57*7dc08ffcSJunyu Laiof function calls, packet creations, etc.). 58*7dc08ffcSJunyu Lai 59*7dc08ffcSJunyu Lai## Submitting pull requests 60*7dc08ffcSJunyu Lai 61*7dc08ffcSJunyu Lai### Coding style & conventions 62*7dc08ffcSJunyu Lai 63*7dc08ffcSJunyu LaiFirst, Scapy "legacy" code contains a lot of code that do not comply 64*7dc08ffcSJunyu Laiwith the following recommendations, but we try to comply with the some 65*7dc08ffcSJunyu Laiguidelines for new code. 66*7dc08ffcSJunyu Lai 67*7dc08ffcSJunyu Lai - The code should be PEP-8 compliant; you can check your code with 68*7dc08ffcSJunyu Lai [pep8](https://pypi.python.org/pypi/pep8). 69*7dc08ffcSJunyu Lai - [Pylint](http://www.pylint.org/) can help you write good Python 70*7dc08ffcSJunyu Lai code (even if respecting Pylint rules is sometimes either too hard 71*7dc08ffcSJunyu Lai or even undesirable; human brain needed!). 72*7dc08ffcSJunyu Lai - [Google Python Style Guide](https://google.github.io/styleguide/pyguide.html) 73*7dc08ffcSJunyu Lai is a nice read! 74*7dc08ffcSJunyu Lai - Avoid creating unnecessary `list` objects, particularly if they 75*7dc08ffcSJunyu Lai can be huge (e.g., when possible, use `scapy.modules.six.range()` instead of 76*7dc08ffcSJunyu Lai `range()`, `for line in fdesc` instead of `for line in 77*7dc08ffcSJunyu Lai fdesc.readlines()`; more generally prefer generators over lists). 78*7dc08ffcSJunyu Lai 79*7dc08ffcSJunyu Lai### Tests 80*7dc08ffcSJunyu Lai 81*7dc08ffcSJunyu LaiPlease consider adding tests for your new features or that trigger the 82*7dc08ffcSJunyu Laibug you are fixing. This will prevent a regression from being 83*7dc08ffcSJunyu Laiunnoticed. 84*7dc08ffcSJunyu Lai 85*7dc08ffcSJunyu Lai### New protocols 86*7dc08ffcSJunyu Lai 87*7dc08ffcSJunyu LaiNew protocols can go either in `scapy/layers` or to 88*7dc08ffcSJunyu Lai`scapy/contrib`. Protocols in `scapy/layers` should be usually found 89*7dc08ffcSJunyu Laion common networks, while protocols in `scapy/contrib` should be 90*7dc08ffcSJunyu Laiuncommon or specific. 91*7dc08ffcSJunyu Lai 92*7dc08ffcSJunyu Lai### Features 93*7dc08ffcSJunyu Lai 94*7dc08ffcSJunyu LaiProtocol-related features should be implemented within the same module 95*7dc08ffcSJunyu Laias the protocol layers(s) (e.g., `traceroute()` is implemented in 96*7dc08ffcSJunyu Lai`scapy/layers/inet.py`). 97*7dc08ffcSJunyu Lai 98*7dc08ffcSJunyu LaiOther features may be implemented in a module (`scapy/modules`) or a 99*7dc08ffcSJunyu Laicontribution (`scapy/contrib`). 100*7dc08ffcSJunyu Lai 101*7dc08ffcSJunyu Lai### Core 102*7dc08ffcSJunyu Lai 103*7dc08ffcSJunyu LaiIf you contribute to Scapy's core (e.g., `scapy/base_classes.py`, 104*7dc08ffcSJunyu Lai`scapy/packet.py`, etc.), please be very careful with performances and 105*7dc08ffcSJunyu Laimemory footprint, as it is easy to write Python code that wastes 106*7dc08ffcSJunyu Laimemory or CPU cycles. 107*7dc08ffcSJunyu Lai 108*7dc08ffcSJunyu LaiAs an example, Packet().__init__() is called each time a **layer** is 109*7dc08ffcSJunyu Laiparsed from a string (during a network capture or a PCAP file 110*7dc08ffcSJunyu Lairead). Adding inefficient code here will have a disastrous effect on 111*7dc08ffcSJunyu LaiScapy's performances. 112*7dc08ffcSJunyu Lai 113*7dc08ffcSJunyu Lai### Python 2 and 3 compatibility 114*7dc08ffcSJunyu Lai 115*7dc08ffcSJunyu LaiThe project aims to provide code that works both on Python 2 and Python 3. Therefore, some rules need to be apply to achieve compatibility: 116*7dc08ffcSJunyu Lai- byte-string must be defined as `b"\x00\x01\x02"` 117*7dc08ffcSJunyu Lai- exceptions must comply with the new Python 3 format: `except SomeError as e:` 118*7dc08ffcSJunyu Lai- lambdas must be written using a single argument when using tuples: use `lambda x_y: x_y[0] + f(x_y[1])` instead of `lambda (x, y): x + f(y)`. 119*7dc08ffcSJunyu Lai- use int instead of long 120*7dc08ffcSJunyu Lai- use list comprehension instead of map() and filter() 121*7dc08ffcSJunyu Lai- use scapy.modules.six.range instead of xrange and range 122*7dc08ffcSJunyu Lai- use scapy.modules.six.itervalues(dict) instead of dict.values() or dict.itervalues() 123*7dc08ffcSJunyu Lai- use scapy.modules.six.string_types instead of basestring 124*7dc08ffcSJunyu Lai- `__bool__ = __nonzero__` must be used when declaring `__nonzero__` methods 125*7dc08ffcSJunyu Lai- `io.BytesIO` must be used instead of `StringIO` when using bytes 126*7dc08ffcSJunyu Lai- `__cmp__` must not be used. 127*7dc08ffcSJunyu Lai- UserDict should be imported via `six.UserDict` 128*7dc08ffcSJunyu Lai 129*7dc08ffcSJunyu Lai### Code review 130*7dc08ffcSJunyu Lai 131*7dc08ffcSJunyu LaiMaintainers tend to be picky, and you might feel frustrated that your 132*7dc08ffcSJunyu Laicode (which is perfectly working in your use case) is not merged 133*7dc08ffcSJunyu Laifaster. 134*7dc08ffcSJunyu Lai 135*7dc08ffcSJunyu LaiPlease don't be offended, and keep in mind that maintainers are 136*7dc08ffcSJunyu Laiconcerned about code maintainability and readability, commit history 137*7dc08ffcSJunyu Lai(we use the history a lot, for example to find regressions or 138*7dc08ffcSJunyu Laiunderstand why certain decisions have been made), performances, 139*7dc08ffcSJunyu Laiintegration in Scapy, API consistency (so that someone who knows how 140*7dc08ffcSJunyu Laito use Scapy will know how to use your code), etc. 141*7dc08ffcSJunyu Lai 142*7dc08ffcSJunyu Lai**Thanks for reading, happy hacking!** 143