Skip to content

Commit 19eeafe

Browse files
authored
Cleanup DoIP sockets (#4533)
* Cleanup DoIP sockets * change get_addr_info
1 parent 30b0398 commit 19eeafe

2 files changed

Lines changed: 14 additions & 95 deletions

File tree

scapy/contrib/automotive/doip.py

Lines changed: 10 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
)
4040
from scapy.layers.inet import TCP, UDP
4141
from scapy.packet import Packet, bind_layers, bind_bottom_up
42-
from scapy.supersocket import StreamSocket, SSLStreamSocket
42+
from scapy.supersocket import SSLStreamSocket
4343

4444

4545
# ISO 13400-2 sect 9.2
@@ -361,21 +361,23 @@ def __init__(self,
361361
self.force_tls = force_tls
362362
self.context = context
363363
try:
364-
self._init_socket(socket.AF_INET)
364+
self._init_socket()
365365
except Exception:
366366
self.close()
367367
raise
368368

369-
def _init_socket(self, sock_family=socket.AF_INET):
370-
# type: (int) -> None
369+
def _init_socket(self):
370+
# type: () -> None
371371
connected = False
372+
addrinfo = socket.getaddrinfo(self.ip, self.port, proto=socket.IPPROTO_TCP)
373+
sock_family = addrinfo[0][0]
374+
372375
s = socket.socket(sock_family, socket.SOCK_STREAM)
373376
s.settimeout(5)
374377
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
375378
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
376379

377380
if not self.force_tls:
378-
addrinfo = socket.getaddrinfo(self.ip, self.port, proto=socket.IPPROTO_TCP)
379381
s.connect(addrinfo[0][-1])
380382
connected = True
381383
DoIPSSLStreamSocket.__init__(self, s)
@@ -450,66 +452,7 @@ def _activate_routing(self): # type: (...) -> int
450452
return -1
451453

452454

453-
class DoIPSocket6(DoIPSocket):
454-
"""Socket for DoIP communication. This sockets automatically
455-
sends a routing activation request as soon as a TCP or TLS connection is
456-
established.
457-
458-
:param ip: IPv6 address of destination
459-
:param port: destination port, usually 13400
460-
:param tls_port: destination port for TLS connection, usually 3496
461-
:param activate_routing: If true, routing activation request is
462-
automatically sent
463-
:param source_address: DoIP source address
464-
:param target_address: DoIP target address, this is automatically
465-
determined if routing activation request is sent
466-
:param activation_type: This allows to set a different activation type for
467-
the routing activation request
468-
:param reserved_oem: Optional parameter to set value for reserved_oem field
469-
of routing activation request
470-
:param force_tls: Skip establishing of a TCP connection and directly try to
471-
connect via SSL/TLS
472-
:param context: Optional ssl.SSLContext object for initialization of ssl socket
473-
connections.
474-
475-
Example:
476-
>>> socket = DoIPSocket6("2001:16b8:3f0e:2f00:21a:37ff:febf:edb9")
477-
>>> socket_link_local = DoIPSocket6("fe80::30e8:80ff:fe07:6d43%eth1")
478-
>>> pkt = DoIP(payload_type=0x8001, source_address=0xe80, target_address=0x1000) / UDS() / UDS_RDBI(identifiers=[0x1000])
479-
>>> resp = socket.sr1(pkt, timeout=1)
480-
""" # noqa: E501
481-
482-
def __init__(self,
483-
ip='::1', # type: str
484-
port=13400, # type: int
485-
tls_port=3496, # type: int
486-
activate_routing=True, # type: bool
487-
source_address=0xe80, # type: int
488-
target_address=0, # type: int
489-
activation_type=0, # type: int
490-
reserved_oem=b"", # type: bytes
491-
force_tls=False, # type: bool
492-
context=None # type: Optional[ssl.SSLContext]
493-
): # type: (...) -> None
494-
self.ip = ip
495-
self.port = port
496-
self.tls_port = tls_port
497-
self.activate_routing = activate_routing
498-
self.source_address = source_address
499-
self.target_address = target_address
500-
self.activation_type = activation_type
501-
self.reserved_oem = reserved_oem
502-
self.buffer = b""
503-
self.force_tls = force_tls
504-
self.context = context
505-
try:
506-
self._init_socket(socket.AF_INET6)
507-
except Exception:
508-
self.close()
509-
raise
510-
511-
512-
class _UDS_DoIPSocketBase(StreamSocket):
455+
class UDS_DoIPSocket(DoIPSocket):
513456
"""
514457
Application-Layer socket for DoIP endpoints. This socket takes care about
515458
the encapsulation of UDS packets into DoIP packets.
@@ -524,8 +467,8 @@ def send(self, x):
524467
# type: (Union[Packet, bytes]) -> int
525468
if isinstance(x, UDS):
526469
pkt = DoIP(payload_type=0x8001,
527-
source_address=self.source_address, # type: ignore
528-
target_address=self.target_address # type: ignore
470+
source_address=self.source_address,
471+
target_address=self.target_address
529472
) / x
530473
else:
531474
pkt = x
@@ -545,28 +488,4 @@ def recv(self, x=MTU, **kwargs):
545488
else:
546489
return pkt
547490

548-
549-
class UDS_DoIPSocket(_UDS_DoIPSocketBase, DoIPSocket):
550-
"""
551-
Application-Layer socket for DoIP endpoints. This socket takes care about
552-
the encapsulation of UDS packets into DoIP packets.
553-
554-
Example:
555-
>>> socket = UDS_DoIPSocket("169.254.117.238")
556-
>>> pkt = UDS() / UDS_RDBI(identifiers=[0x1000])
557-
>>> resp = socket.sr1(pkt, timeout=1)
558-
"""
559-
pass
560-
561-
562-
class UDS_DoIPSocket6(_UDS_DoIPSocketBase, DoIPSocket6):
563-
"""
564-
Application-Layer socket for DoIP endpoints. This socket takes care about
565-
the encapsulation of UDS packets into DoIP packets.
566-
567-
Example:
568-
>>> socket = UDS_DoIPSocket6("2001:16b8:3f0e:2f00:21a:37ff:febf:edb9")
569-
>>> pkt = UDS() / UDS_RDBI(identifiers=[0x1000])
570-
>>> resp = socket.sr1(pkt, timeout=1)
571-
"""
572491
pass

test/contrib/automotive/doip.uts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ def server():
528528
server_thread = threading.Thread(target=server)
529529
server_thread.start()
530530
server_up.wait(timeout=1)
531-
sock = DoIPSocket6(activate_routing=False)
531+
sock = DoIPSocket(ip="::1", activate_routing=False)
532532

533533
pkts = sock.sniff(timeout=1, count=2)
534534
server_thread.join(timeout=1)
@@ -668,7 +668,7 @@ server_up.wait(timeout=1)
668668
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
669669
context.check_hostname = False
670670
context.verify_mode = ssl.CERT_NONE
671-
sock = DoIPSocket6(activate_routing=False, force_tls=True, context=context)
671+
sock = DoIPSocket(ip="::1", activate_routing=False, force_tls=True, context=context)
672672

673673
pkts = sock.sniff(timeout=1, count=2)
674674
server_thread.join(timeout=1)
@@ -705,7 +705,7 @@ server_up.wait(timeout=1)
705705
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
706706
context.check_hostname = False
707707
context.verify_mode = ssl.CERT_NONE
708-
sock = UDS_DoIPSocket6(activate_routing=False, force_tls=True, context=context)
708+
sock = UDS_DoIPSocket(ip="::1", activate_routing=False, force_tls=True, context=context)
709709

710710
pkts = sock.sniff(timeout=1, count=2)
711711
server_thread.join(timeout=1)
@@ -765,7 +765,7 @@ context.check_hostname = False
765765
context.verify_mode = ssl.CERT_NONE
766766

767767

768-
sock = UDS_DoIPSocket6(ip="::1", context=context)
768+
sock = UDS_DoIPSocket(ip="::1", context=context)
769769

770770
pkts = sock.sniff(timeout=1, count=2)
771771
server_tcp_thread.join(timeout=1)

0 commit comments

Comments
 (0)