Dave Wallace | cf9356d | 2024-07-23 01:28:19 -0400 | [diff] [blame] | 1 | # NOTE: This patch copied from https://github.com/secdev/scapy |
| 2 | # commit 3e6900776698cd5472c5405294414d5b672a3f18 |
| 3 | # |
| 4 | diff --git a/scapy/layers/ppp.py b/scapy/layers/ppp.py |
| 5 | index b5cd42b4..e0f4c593 100644 |
| 6 | --- a/scapy/layers/ppp.py |
| 7 | +++ b/scapy/layers/ppp.py |
| 8 | @@ -292,6 +292,14 @@ class _PPPProtoField(EnumField): |
| 9 | |
| 10 | See RFC 1661 section 2 |
| 11 | <https://tools.ietf.org/html/rfc1661#section-2> |
| 12 | + |
| 13 | + The generated proto field is two bytes when not specified, or when specified |
| 14 | + as an integer or a string: |
| 15 | + PPP() |
| 16 | + PPP(proto=0x21) |
| 17 | + PPP(proto="Internet Protocol version 4") |
| 18 | + To explicitly forge a one byte proto field, use the bytes representation: |
| 19 | + PPP(proto=b'\x21') |
| 20 | """ |
| 21 | def getfield(self, pkt, s): |
| 22 | if ord(s[:1]) & 0x01: |
| 23 | @@ -304,12 +312,18 @@ class _PPPProtoField(EnumField): |
| 24 | return super(_PPPProtoField, self).getfield(pkt, s) |
| 25 | |
| 26 | def addfield(self, pkt, s, val): |
| 27 | - if val < 0x100: |
| 28 | - self.fmt = "!B" |
| 29 | - self.sz = 1 |
| 30 | + if isinstance(val, bytes): |
| 31 | + if len(val) == 1: |
| 32 | + fmt, sz = "!B", 1 |
| 33 | + elif len(val) == 2: |
| 34 | + fmt, sz = "!H", 2 |
| 35 | + else: |
| 36 | + raise TypeError('Invalid length for PPP proto') |
| 37 | + val = struct.Struct(fmt).unpack(val)[0] |
| 38 | else: |
| 39 | - self.fmt = "!H" |
| 40 | - self.sz = 2 |
| 41 | + fmt, sz = "!H", 2 |
| 42 | + self.fmt = fmt |
| 43 | + self.sz = sz |
| 44 | self.struct = struct.Struct(self.fmt) |
| 45 | return super(_PPPProtoField, self).addfield(pkt, s, val) |