Skip to content

Remove duplicated code #2105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 21 additions & 26 deletions scapy/layers/dhcp6.py
Original file line number Diff line number Diff line change
Expand Up @@ -1699,6 +1699,25 @@ def make_reply(self, req):
msgtype = p.msgtype
trid = p.trid

def _include_options(query, answer):
"""
Include options from the DHCPv6 query
"""

# See which options should be included
reqopts = []
if query.haslayer(DHCP6OptOptReq): # add only asked ones
reqopts = query[DHCP6OptOptReq].reqopts
for o, opt in six.iteritems(self.dhcpv6_options):
if o in reqopts:
answer /= opt
else:
# advertise everything we have available
# Should not happen has clients MUST include
# and ORO in requests (sec 18.1.1) -- arno
for o, opt in six.iteritems(self.dhcpv6_options):
answer /= opt

if msgtype == 1: # SOLICIT (See Sect 17.1 and 17.2 of RFC 3315)

# XXX We don't support address or prefix assignment
Expand Down Expand Up @@ -1741,16 +1760,7 @@ def make_reply(self, req):
resp /= DHCP6OptClientId(duid=client_duid)
resp /= DHCP6OptReconfAccept()

# See which options should be included
reqopts = []
if p.haslayer(DHCP6OptOptReq): # add only asked ones
reqopts = p[DHCP6OptOptReq].reqopts
for o, opt in six.iteritems(self.dhcpv6_options):
if o in reqopts:
resp /= opt
else: # advertise everything we have available
for o, opt in six.iteritems(self.dhcpv6_options):
resp /= opt
_include_options(p, resp)

return resp

Expand All @@ -1762,19 +1772,7 @@ def make_reply(self, req):
resp /= DHCP6OptServerId(duid=self.duid)
resp /= DHCP6OptClientId(duid=client_duid)

# See which options should be included
reqopts = []
if p.haslayer(DHCP6OptOptReq): # add only asked ones
reqopts = p[DHCP6OptOptReq].reqopts
for o, opt in six.iteritems(self.dhcpv6_options):
if o in reqopts:
resp /= opt
else:
# advertise everything we have available.
# Should not happen has clients MUST include
# and ORO in requests (sec 18.1.1) -- arno
for o, opt in six.iteritems(self.dhcpv6_options):
resp /= opt
_include_options(p, resp)

return resp

Expand Down Expand Up @@ -1859,9 +1857,6 @@ def make_reply(self, req):
resp /= DHCP6OptClientId(duid=client_duid)

# Stack requested options if available
reqopts = []
if p.haslayer(DHCP6OptOptReq):
reqopts = p[DHCP6OptOptReq].reqopts
for o, opt in six.iteritems(self.dhcpv6_options):
resp /= opt

Expand Down
58 changes: 14 additions & 44 deletions scapy/layers/ppp.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,48 +353,24 @@ class PPP_IPCP_Option_IPAddress(PPP_IPCP_Option):
]


class PPP_IPCP_Option_DNS1(PPP_IPCP_Option):
class PPP_IPCP_Option_DNS1(PPP_IPCP_Option_IPAddress):
name = "PPP IPCP Option: DNS1 Address"
fields_desc = [
ByteEnumField("type", 129, _PPP_ipcpopttypes),
FieldLenField("len", None, length_of="data", fmt="B",
adjust=lambda _, val: val + 2),
IPField("data", "0.0.0.0"),
StrLenField("garbage", "", length_from=lambda pkt: pkt.len - 6),
]
type = 129


class PPP_IPCP_Option_DNS2(PPP_IPCP_Option):
class PPP_IPCP_Option_DNS2(PPP_IPCP_Option_IPAddress):
name = "PPP IPCP Option: DNS2 Address"
fields_desc = [
ByteEnumField("type", 131, _PPP_ipcpopttypes),
FieldLenField("len", None, length_of="data", fmt="B",
adjust=lambda _, val: val + 2),
IPField("data", "0.0.0.0"),
StrLenField("garbage", "", length_from=lambda pkt: pkt.len - 6),
]
type = 131


class PPP_IPCP_Option_NBNS1(PPP_IPCP_Option):
class PPP_IPCP_Option_NBNS1(PPP_IPCP_Option_IPAddress):
name = "PPP IPCP Option: NBNS1 Address"
fields_desc = [
ByteEnumField("type", 130, _PPP_ipcpopttypes),
FieldLenField("len", None, length_of="data", fmt="B",
adjust=lambda _, val: val + 2),
IPField("data", "0.0.0.0"),
StrLenField("garbage", "", length_from=lambda pkt: pkt.len - 6),
]
type = 130


class PPP_IPCP_Option_NBNS2(PPP_IPCP_Option):
class PPP_IPCP_Option_NBNS2(PPP_IPCP_Option_IPAddress):
name = "PPP IPCP Option: NBNS2 Address"
fields_desc = [
ByteEnumField("type", 132, _PPP_ipcpopttypes),
FieldLenField("len", None, length_of="data", fmt="B",
adjust=lambda _, val: val + 2),
IPField("data", "0.0.0.0"),
StrLenField("garbage", "", length_from=lambda pkt: pkt.len - 6),
]
type = 132


class PPP_IPCP(Packet):
Expand Down Expand Up @@ -691,33 +667,27 @@ class PPP_LCP_Protocol_Reject(PPP_LCP):
]


class PPP_LCP_Echo(PPP_LCP):
class PPP_LCP_Discard_Request(PPP_LCP):
fields_desc = [
ByteEnumField("code", 9, _PPP_lcptypes),
ByteEnumField("code", 11, _PPP_lcptypes),
XByteField("id", 0),
FieldLenField("len", None, fmt="H", length_of="data",
adjust=lambda _, val: val + 8),
IntField("magic_number", None),
StrLenField("data", "", length_from=lambda pkt: pkt.len - 8),
]


class PPP_LCP_Echo(PPP_LCP_Discard_Request):
code = 9

def answers(self, other):
return (
isinstance(other, PPP_LCP_Echo) and self.code == 10 and
other.code == 9 and self.id == other.id
)


class PPP_LCP_Discard_Request(PPP_LCP):
fields_desc = [
ByteEnumField("code", 11, _PPP_lcptypes),
XByteField("id", 0),
FieldLenField("len", None, fmt="H", length_of="data",
adjust=lambda _, val: val + 8),
IntField("magic_number", None),
StrLenField("data", "", length_from=lambda pkt: pkt.len - 8),
]

# Password authentication protocol (RFC 1334)


Expand Down
65 changes: 11 additions & 54 deletions scapy/layers/sctp.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,20 +404,8 @@ class SCTPChunkParamAddIPAddr(_SCTPChunkParam, Packet):
lambda p: p.addr_type == 6), ]


class SCTPChunkParamDelIPAddr(_SCTPChunkParam, Packet):
fields_desc = [ShortEnumField("type", 0xc002, sctpchunkparamtypes),
FieldLenField("len", None, length_of="addr",
adjust=lambda pkt, x:x + 12),
XIntField("correlation_id", None),
ShortEnumField("addr_type", 5, sctpchunkparamtypes),
FieldLenField("addr_len", None, length_of="addr",
adjust=lambda pkt, x:x + 4),
ConditionalField(
IPField("addr", "127.0.0.1"),
lambda p: p.addr_type == 5),
ConditionalField(
IP6Field("addr", "::1"),
lambda p: p.addr_type == 6), ]
class SCTPChunkParamDelIPAddr(SCTPChunkParamAddIPAddr):
type = 0xc002


class SCTPChunkParamErrorIndication(_SCTPChunkParam, Packet):
Expand All @@ -430,20 +418,8 @@ class SCTPChunkParamErrorIndication(_SCTPChunkParam, Packet):
4, padwith=b"\x00"), ]


class SCTPChunkParamSetPrimaryAddr(_SCTPChunkParam, Packet):
fields_desc = [ShortEnumField("type", 0xc004, sctpchunkparamtypes),
FieldLenField("len", None, length_of="addr",
adjust=lambda pkt, x:x + 12),
XIntField("correlation_id", None),
ShortEnumField("addr_type", 5, sctpchunkparamtypes),
FieldLenField("addr_len", None, length_of="addr",
adjust=lambda pkt, x:x + 4),
ConditionalField(
IPField("addr", "127.0.0.1"),
lambda p: p.addr_type == 5),
ConditionalField(
IP6Field("addr", "::1"),
lambda p: p.addr_type == 6), ]
class SCTPChunkParamSetPrimaryAddr(SCTPChunkParamAddIPAddr):
type = 0xc004


class SCTPChunkParamSuccessIndication(_SCTPChunkParam, Packet):
Expand Down Expand Up @@ -554,17 +530,8 @@ class SCTPChunkInit(_SCTPChunkGuessPayload, Packet):
]


class SCTPChunkInitAck(_SCTPChunkGuessPayload, Packet):
fields_desc = [ByteEnumField("type", 2, sctpchunktypes),
XByteField("flags", None),
FieldLenField("len", None, length_of="params", adjust=lambda pkt, x:x + 20), # noqa: E501
XIntField("init_tag", None),
IntField("a_rwnd", None),
ShortField("n_out_streams", None),
ShortField("n_in_streams", None),
XIntField("init_tsn", None),
ChunkParamField("params", None, length_from=lambda pkt:pkt.len - 20), # noqa: E501
]
class SCTPChunkInitAck(SCTPChunkInit):
type = 2


class GapAckField(Field):
Expand Down Expand Up @@ -613,12 +580,8 @@ class SCTPChunkHeartbeatReq(_SCTPChunkGuessPayload, Packet):
]


class SCTPChunkHeartbeatAck(_SCTPChunkGuessPayload, Packet):
fields_desc = [ByteEnumField("type", 5, sctpchunktypes),
XByteField("flags", None),
FieldLenField("len", None, length_of="params", adjust=lambda pkt, x:x + 4), # noqa: E501
ChunkParamField("params", None, length_from=lambda pkt:pkt.len - 4), # noqa: E501
]
class SCTPChunkHeartbeatAck(SCTPChunkHeartbeatReq):
type = 5


class SCTPChunkAbort(_SCTPChunkGuessPayload, Packet):
Expand Down Expand Up @@ -655,7 +618,7 @@ class SCTPChunkError(_SCTPChunkGuessPayload, Packet):
]


class SCTPChunkCookieEcho(_SCTPChunkGuessPayload, Packet):
class SCTPChunkCookieEcho(SCTPChunkError):
fields_desc = [ByteEnumField("type", 10, sctpchunktypes),
XByteField("flags", None),
FieldLenField("len", None, length_of="cookie", adjust=lambda pkt, x:x + 4), # noqa: E501
Expand Down Expand Up @@ -701,14 +664,8 @@ class SCTPChunkAddressConf(_SCTPChunkGuessPayload, Packet):
]


class SCTPChunkAddressConfAck(_SCTPChunkGuessPayload, Packet):
fields_desc = [ByteEnumField("type", 0x80, sctpchunktypes),
XByteField("flags", None),
FieldLenField("len", None, length_of="params",
adjust=lambda pkt, x:x + 8),
IntField("seq", 0),
ChunkParamField("params", None, length_from=lambda pkt:pkt.len - 8), # noqa: E501
]
class SCTPChunkAddressConfAck(SCTPChunkAddressConf):
type = 0x80


bind_layers(IP, SCTP, proto=IPPROTO_SCTP)
Expand Down