Skip to content

Commit d066d11

Browse files
committed
Remove duplicated code
1 parent 10af736 commit d066d11

File tree

3 files changed

+47
-126
lines changed

3 files changed

+47
-126
lines changed

scapy/layers/dhcp6.py

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,25 @@ def make_reply(self, req):
17171717

17181718
else: # No Rapid Commit in the packet. Reply with an Advertise
17191719

1720+
def _include_options(query, answer):
1721+
"""
1722+
Include options from the DHCPv6 query
1723+
"""
1724+
1725+
# See which options should be included
1726+
reqopts = []
1727+
if query.haslayer(DHCP6OptOptReq): # add only asked ones
1728+
reqopts = query[DHCP6OptOptReq].reqopts
1729+
for o, opt in six.iteritems(self.dhcpv6_options):
1730+
if o in reqopts:
1731+
answer /= opt
1732+
else:
1733+
# advertise everything we have available
1734+
# Should not happen has clients MUST include
1735+
# and ORO in requests (sec 18.1.1) -- arno
1736+
for o, opt in six.iteritems(self.dhcpv6_options):
1737+
answer /= opt
1738+
17201739
if (p.haslayer(DHCP6OptIA_NA) or
17211740
p.haslayer(DHCP6OptIA_TA)):
17221741
# XXX We don't assign addresses at the moment
@@ -1741,16 +1760,7 @@ def make_reply(self, req):
17411760
resp /= DHCP6OptClientId(duid=client_duid)
17421761
resp /= DHCP6OptReconfAccept()
17431762

1744-
# See which options should be included
1745-
reqopts = []
1746-
if p.haslayer(DHCP6OptOptReq): # add only asked ones
1747-
reqopts = p[DHCP6OptOptReq].reqopts
1748-
for o, opt in six.iteritems(self.dhcpv6_options):
1749-
if o in reqopts:
1750-
resp /= opt
1751-
else: # advertise everything we have available
1752-
for o, opt in six.iteritems(self.dhcpv6_options):
1753-
resp /= opt
1763+
_include_options(p, resp)
17541764

17551765
return resp
17561766

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

1765-
# See which options should be included
1766-
reqopts = []
1767-
if p.haslayer(DHCP6OptOptReq): # add only asked ones
1768-
reqopts = p[DHCP6OptOptReq].reqopts
1769-
for o, opt in six.iteritems(self.dhcpv6_options):
1770-
if o in reqopts:
1771-
resp /= opt
1772-
else:
1773-
# advertise everything we have available.
1774-
# Should not happen has clients MUST include
1775-
# and ORO in requests (sec 18.1.1) -- arno
1776-
for o, opt in six.iteritems(self.dhcpv6_options):
1777-
resp /= opt
1775+
_include_options(p, resp)
17781776

17791777
return resp
17801778

@@ -1859,11 +1857,7 @@ def make_reply(self, req):
18591857
resp /= DHCP6OptClientId(duid=client_duid)
18601858

18611859
# Stack requested options if available
1862-
reqopts = []
1863-
if p.haslayer(DHCP6OptOptReq):
1864-
reqopts = p[DHCP6OptOptReq].reqopts
1865-
for o, opt in six.iteritems(self.dhcpv6_options):
1866-
resp /= opt
1860+
_include_options(p, resp)
18671861

18681862
return resp
18691863

scapy/layers/ppp.py

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -353,48 +353,24 @@ class PPP_IPCP_Option_IPAddress(PPP_IPCP_Option):
353353
]
354354

355355

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

366360

367-
class PPP_IPCP_Option_DNS2(PPP_IPCP_Option):
361+
class PPP_IPCP_Option_DNS2(PPP_IPCP_Option_IPAddress):
368362
name = "PPP IPCP Option: DNS2 Address"
369-
fields_desc = [
370-
ByteEnumField("type", 131, _PPP_ipcpopttypes),
371-
FieldLenField("len", None, length_of="data", fmt="B",
372-
adjust=lambda _, val: val + 2),
373-
IPField("data", "0.0.0.0"),
374-
StrLenField("garbage", "", length_from=lambda pkt: pkt.len - 6),
375-
]
363+
type = 131
376364

377365

378-
class PPP_IPCP_Option_NBNS1(PPP_IPCP_Option):
366+
class PPP_IPCP_Option_NBNS1(PPP_IPCP_Option_IPAddress):
379367
name = "PPP IPCP Option: NBNS1 Address"
380-
fields_desc = [
381-
ByteEnumField("type", 130, _PPP_ipcpopttypes),
382-
FieldLenField("len", None, length_of="data", fmt="B",
383-
adjust=lambda _, val: val + 2),
384-
IPField("data", "0.0.0.0"),
385-
StrLenField("garbage", "", length_from=lambda pkt: pkt.len - 6),
386-
]
368+
type = 130
387369

388370

389-
class PPP_IPCP_Option_NBNS2(PPP_IPCP_Option):
371+
class PPP_IPCP_Option_NBNS2(PPP_IPCP_Option_IPAddress):
390372
name = "PPP IPCP Option: NBNS2 Address"
391-
fields_desc = [
392-
ByteEnumField("type", 132, _PPP_ipcpopttypes),
393-
FieldLenField("len", None, length_of="data", fmt="B",
394-
adjust=lambda _, val: val + 2),
395-
IPField("data", "0.0.0.0"),
396-
StrLenField("garbage", "", length_from=lambda pkt: pkt.len - 6),
397-
]
373+
type = 132
398374

399375

400376
class PPP_IPCP(Packet):
@@ -691,33 +667,27 @@ class PPP_LCP_Protocol_Reject(PPP_LCP):
691667
]
692668

693669

694-
class PPP_LCP_Echo(PPP_LCP):
670+
class PPP_LCP_Discard_Request(PPP_LCP):
695671
fields_desc = [
696-
ByteEnumField("code", 9, _PPP_lcptypes),
672+
ByteEnumField("code", 11, _PPP_lcptypes),
697673
XByteField("id", 0),
698674
FieldLenField("len", None, fmt="H", length_of="data",
699675
adjust=lambda _, val: val + 8),
700676
IntField("magic_number", None),
701677
StrLenField("data", "", length_from=lambda pkt: pkt.len - 8),
702678
]
703679

680+
681+
class PPP_LCP_Echo(PPP_LCP_Discard_Request):
682+
code = 9
683+
704684
def answers(self, other):
705685
return (
706686
isinstance(other, PPP_LCP_Echo) and self.code == 10 and
707687
other.code == 9 and self.id == other.id
708688
)
709689

710690

711-
class PPP_LCP_Discard_Request(PPP_LCP):
712-
fields_desc = [
713-
ByteEnumField("code", 11, _PPP_lcptypes),
714-
XByteField("id", 0),
715-
FieldLenField("len", None, fmt="H", length_of="data",
716-
adjust=lambda _, val: val + 8),
717-
IntField("magic_number", None),
718-
StrLenField("data", "", length_from=lambda pkt: pkt.len - 8),
719-
]
720-
721691
# Password authentication protocol (RFC 1334)
722692

723693

scapy/layers/sctp.py

Lines changed: 11 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -404,20 +404,8 @@ class SCTPChunkParamAddIPAddr(_SCTPChunkParam, Packet):
404404
lambda p: p.addr_type == 6), ]
405405

406406

407-
class SCTPChunkParamDelIPAddr(_SCTPChunkParam, Packet):
408-
fields_desc = [ShortEnumField("type", 0xc002, sctpchunkparamtypes),
409-
FieldLenField("len", None, length_of="addr",
410-
adjust=lambda pkt, x:x + 12),
411-
XIntField("correlation_id", None),
412-
ShortEnumField("addr_type", 5, sctpchunkparamtypes),
413-
FieldLenField("addr_len", None, length_of="addr",
414-
adjust=lambda pkt, x:x + 4),
415-
ConditionalField(
416-
IPField("addr", "127.0.0.1"),
417-
lambda p: p.addr_type == 5),
418-
ConditionalField(
419-
IP6Field("addr", "::1"),
420-
lambda p: p.addr_type == 6), ]
407+
class SCTPChunkParamDelIPAddr(SCTPChunkParamAddIPAddr):
408+
type = 0xc002
421409

422410

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

432420

433-
class SCTPChunkParamSetPrimaryAddr(_SCTPChunkParam, Packet):
434-
fields_desc = [ShortEnumField("type", 0xc004, sctpchunkparamtypes),
435-
FieldLenField("len", None, length_of="addr",
436-
adjust=lambda pkt, x:x + 12),
437-
XIntField("correlation_id", None),
438-
ShortEnumField("addr_type", 5, sctpchunkparamtypes),
439-
FieldLenField("addr_len", None, length_of="addr",
440-
adjust=lambda pkt, x:x + 4),
441-
ConditionalField(
442-
IPField("addr", "127.0.0.1"),
443-
lambda p: p.addr_type == 5),
444-
ConditionalField(
445-
IP6Field("addr", "::1"),
446-
lambda p: p.addr_type == 6), ]
421+
class SCTPChunkParamSetPrimaryAddr(SCTPChunkParamAddIPAddr):
422+
type = 0xc004
447423

448424

449425
class SCTPChunkParamSuccessIndication(_SCTPChunkParam, Packet):
@@ -554,17 +530,8 @@ class SCTPChunkInit(_SCTPChunkGuessPayload, Packet):
554530
]
555531

556532

557-
class SCTPChunkInitAck(_SCTPChunkGuessPayload, Packet):
558-
fields_desc = [ByteEnumField("type", 2, sctpchunktypes),
559-
XByteField("flags", None),
560-
FieldLenField("len", None, length_of="params", adjust=lambda pkt, x:x + 20), # noqa: E501
561-
XIntField("init_tag", None),
562-
IntField("a_rwnd", None),
563-
ShortField("n_out_streams", None),
564-
ShortField("n_in_streams", None),
565-
XIntField("init_tsn", None),
566-
ChunkParamField("params", None, length_from=lambda pkt:pkt.len - 20), # noqa: E501
567-
]
533+
class SCTPChunkInitAck(SCTPChunkInit):
534+
type = 2
568535

569536

570537
class GapAckField(Field):
@@ -613,12 +580,8 @@ class SCTPChunkHeartbeatReq(_SCTPChunkGuessPayload, Packet):
613580
]
614581

615582

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

623586

624587
class SCTPChunkAbort(_SCTPChunkGuessPayload, Packet):
@@ -655,7 +618,7 @@ class SCTPChunkError(_SCTPChunkGuessPayload, Packet):
655618
]
656619

657620

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

703666

704-
class SCTPChunkAddressConfAck(_SCTPChunkGuessPayload, Packet):
705-
fields_desc = [ByteEnumField("type", 0x80, sctpchunktypes),
706-
XByteField("flags", None),
707-
FieldLenField("len", None, length_of="params",
708-
adjust=lambda pkt, x:x + 8),
709-
IntField("seq", 0),
710-
ChunkParamField("params", None, length_from=lambda pkt:pkt.len - 8), # noqa: E501
711-
]
667+
class SCTPChunkAddressConfAck(SCTPChunkAddressConf):
668+
type = 0x80
712669

713670

714671
bind_layers(IP, SCTP, proto=IPPROTO_SCTP)

0 commit comments

Comments
 (0)