From 0cf7cd74175104fc27d57a296ccf0c618dee3096 Mon Sep 17 00:00:00 2001 From: Patrick Menschel Date: Sun, 16 May 2021 20:09:13 +0200 Subject: [PATCH 1/9] Add import for linux/can/isotp.h IsoTp CAN protocol is available since Kernel v5.10 but several constants from isotp.h have not found their way into socket module. This commit adds that header file if configure finds it. Signed-off-by: Patrick Menschel --- Modules/socketmodule.h | 4 ++++ configure | 2 +- configure.ac | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Modules/socketmodule.h b/Modules/socketmodule.h index e4f375d5e8100a..ec60990fe388da 100644 --- a/Modules/socketmodule.h +++ b/Modules/socketmodule.h @@ -144,6 +144,10 @@ typedef int socklen_t; #include #endif +#ifdef HAVE_LINUX_CAN_ISOTP_H +#include +#endif + #ifdef HAVE_LINUX_CAN_J1939_H #include #endif diff --git a/configure b/configure index e1d450131fe5a0..b007bfdbabab62 100755 --- a/configure +++ b/configure @@ -8406,7 +8406,7 @@ done # On Linux, can.h, can/bcm.h, can/j1939.h, can/raw.h require sys/socket.h -for ac_header in linux/can.h linux/can/bcm.h linux/can/j1939.h linux/can/raw.h +for ac_header in linux/can.h linux/can/bcm.h linux/can/j1939.h linux/can/raw.h linux/can/isotp.h do : as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh` ac_fn_c_check_header_compile "$LINENO" "$ac_header" "$as_ac_Header" " diff --git a/configure.ac b/configure.ac index 4fc269a7309299..bbea9eaf98d39b 100644 --- a/configure.ac +++ b/configure.ac @@ -2270,7 +2270,7 @@ AC_CHECK_HEADERS(linux/vm_sockets.h,,,[ ]) # On Linux, can.h, can/bcm.h, can/j1939.h, can/raw.h require sys/socket.h -AC_CHECK_HEADERS(linux/can.h linux/can/bcm.h linux/can/j1939.h linux/can/raw.h,,,[ +AC_CHECK_HEADERS(linux/can.h linux/can/bcm.h linux/can/j1939.h linux/can/raw.h linux/can/isotp.h,,,[ #ifdef HAVE_SYS_SOCKET_H #include #endif From 17535b5c634bc7a18eab9b7dc16aa40893dc8120 Mon Sep 17 00:00:00 2001 From: Patrick Menschel Date: Wed, 19 May 2021 17:29:33 +0200 Subject: [PATCH 2/9] Add IsoTp constants in socketmodule.c Use PyModule_AddIntMacro() to add all constants from isotp.h . Signed-off-by: Patrick Menschel --- Modules/socketmodule.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 79559c04d5104d..ce8f443a0c15e6 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -7785,6 +7785,45 @@ PyInit__socket(void) PyModule_AddIntMacro(m, J1939_FILTER_MAX); #endif +#ifdef HAVE_LINUX_CAN_ISOTP_H + PyModule_AddIntMacro(m, SOL_CAN_ISOTP); + + /* for socket options affecting the socket (not the global system) */ + PyModule_AddIntMacro(m, CAN_ISOTP_OPTS); + PyModule_AddIntMacro(m, CAN_ISOTP_RECV_FC); + + /* sockopts to force stmin timer values for protocol regression tests */ + PyModule_AddIntMacro(m, CAN_ISOTP_TX_STMIN); + PyModule_AddIntMacro(m, CAN_ISOTP_RX_STMIN); + PyModule_AddIntMacro(m, CAN_ISOTP_LL_OPTS); + + /* flags for isotp behaviour */ + PyModule_AddIntMacro(m, CAN_ISOTP_LISTEN_MODE); + PyModule_AddIntMacro(m, CAN_ISOTP_EXTEND_ADDR); + PyModule_AddIntMacro(m, CAN_ISOTP_TX_PADDING); + PyModule_AddIntMacro(m, CAN_ISOTP_RX_PADDING); + PyModule_AddIntMacro(m, CAN_ISOTP_CHK_PAD_LEN); + PyModule_AddIntMacro(m, CAN_ISOTP_CHK_PAD_DATA); + PyModule_AddIntMacro(m, CAN_ISOTP_HALF_DUPLEX); + PyModule_AddIntMacro(m, CAN_ISOTP_FORCE_TXSTMIN); + PyModule_AddIntMacro(m, CAN_ISOTP_FORCE_RXSTMIN); + PyModule_AddIntMacro(m, CAN_ISOTP_RX_EXT_ADDR); + PyModule_AddIntMacro(m, CAN_ISOTP_WAIT_TX_DONE); + PyModule_AddIntMacro(m, CAN_ISOTP_SF_BROADCAST); + + /* default values */ + PyModule_AddIntMacro(m, CAN_ISOTP_DEFAULT_FLAGS); + PyModule_AddIntMacro(m, CAN_ISOTP_DEFAULT_EXT_ADDRESS); + PyModule_AddIntMacro(m, CAN_ISOTP_DEFAULT_PAD_CONTENT); + PyModule_AddIntMacro(m, CAN_ISOTP_DEFAULT_FRAME_TXTIME); + PyModule_AddIntMacro(m, CAN_ISOTP_DEFAULT_RECV_BS); + PyModule_AddIntMacro(m, CAN_ISOTP_DEFAULT_RECV_STMIN); + PyModule_AddIntMacro(m, CAN_ISOTP_DEFAULT_RECV_WFTMAX); + + PyModule_AddIntMacro(m, CAN_ISOTP_DEFAULT_LL_MTU); + PyModule_AddIntMacro(m, CAN_ISOTP_DEFAULT_LL_TX_DL); + PyModule_AddIntMacro(m, CAN_ISOTP_DEFAULT_LL_TX_FLAGS); +#endif #ifdef SOL_RDS PyModule_AddIntMacro(m, SOL_RDS); #endif From 1f7c2c54e8561930640fb1cfcd5c674e7805eb22 Mon Sep 17 00:00:00 2001 From: Patrick Menschel Date: Wed, 19 May 2021 17:47:34 +0200 Subject: [PATCH 3/9] Add test for isotp constants into unit test. Signed-off-by: Patrick Menschel --- Lib/test/test_socket.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 828d1f3dcc6701..52a85d64a08e88 100755 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -2165,6 +2165,46 @@ def testCrucialConstants(self): socket.PF_CAN socket.CAN_ISOTP socket.SOCK_DGRAM + + def testIsoTpConstants(self): + socket.SOL_CAN_ISOTP + + # for socket options affecting the socket (not the global system) + socket.CAN_ISOTP_OPTS + socket.CAN_ISOTP_RECV_FC + + # sockopts to force stmin timer values for protocol regression tests + socket.CAN_ISOTP_TX_STMIN + socket.CAN_ISOTP_RX_STMIN + socket.CAN_ISOTP_LL_OPTS + + # flags for isotp behaviour + socket.CAN_ISOTP_LISTEN_MODE + socket.CAN_ISOTP_EXTEND_ADDR + socket.CAN_ISOTP_TX_PADDING + socket.CAN_ISOTP_RX_PADDING + socket.CAN_ISOTP_CHK_PAD_LEN + socket.CAN_ISOTP_CHK_PAD_DATA + socket.CAN_ISOTP_HALF_DUPLEX + socket.CAN_ISOTP_FORCE_TXSTMIN + socket.CAN_ISOTP_FORCE_RXSTMIN + socket.CAN_ISOTP_RX_EXT_ADDR + socket.CAN_ISOTP_WAIT_TX_DONE + socket.CAN_ISOTP_SF_BROADCAST + + # default values + socket.CAN_ISOTP_DEFAULT_FLAGS + socket.CAN_ISOTP_DEFAULT_EXT_ADDRESS + socket.CAN_ISOTP_DEFAULT_PAD_CONTENT + socket.CAN_ISOTP_DEFAULT_FRAME_TXTIME + socket.CAN_ISOTP_DEFAULT_RECV_BS + socket.CAN_ISOTP_DEFAULT_EXT_ADDRESS + socket.CAN_ISOTP_DEFAULT_RECV_STMIN + socket.CAN_ISOTP_DEFAULT_RECV_WFTMAX + + socket.CAN_ISOTP_DEFAULT_LL_MTU + socket.CAN_ISOTP_DEFAULT_LL_TX_DL + socket.CAN_ISOTP_DEFAULT_LL_TX_FLAGS def testCreateSocket(self): with socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW) as s: From bc9118c460b772a86940be24e7b61cf6cc11e5ba Mon Sep 17 00:00:00 2001 From: Patrick Menschel Date: Wed, 19 May 2021 18:36:51 +0200 Subject: [PATCH 4/9] Add entry to Doc/whatsnew/3.10.rst Signed-off-by: Patrick Menschel --- Doc/whatsnew/3.10.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst index 926679e6f32dc5..1ae7fb0cbc2e2c 100644 --- a/Doc/whatsnew/3.10.rst +++ b/Doc/whatsnew/3.10.rst @@ -1175,6 +1175,9 @@ The exception :exc:`socket.timeout` is now an alias of :exc:`TimeoutError`. Add option to create MPTCP sockets with ``IPPROTO_MPTCP`` (Contributed by Rui Cunha in :issue:`43571`.) +Add constants for IsoTp CAN protocol. +(Contributed by Patrick Menschel) + ssl --- From 523bf9fe6718486c61572c6c7be3e076da04b2b8 Mon Sep 17 00:00:00 2001 From: Patrick Menschel Date: Wed, 19 May 2021 19:08:55 +0200 Subject: [PATCH 5/9] Fix a whitespace issue in test_socket.py This one slipped by, sorry. Signed-off-by: Patrick Menschel --- Lib/test/test_socket.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 52a85d64a08e88..4a433cb421a8fc 100755 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -2165,7 +2165,7 @@ def testCrucialConstants(self): socket.PF_CAN socket.CAN_ISOTP socket.SOCK_DGRAM - + def testIsoTpConstants(self): socket.SOL_CAN_ISOTP From eb283c21ad923019982c916b70344a4e7709c9da Mon Sep 17 00:00:00 2001 From: Patrick Menschel Date: Wed, 19 May 2021 19:47:21 +0200 Subject: [PATCH 6/9] Add issue number 42653 and blurb. The issue number was found eventually. Apparently there already was a PR with this topic. Signed-off-by: Patrick Menschel --- Doc/whatsnew/3.10.rst | 2 +- .../Core and Builtins/2021-05-19-19-41-15.bpo-42653.qvfUpA.rst | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2021-05-19-19-41-15.bpo-42653.qvfUpA.rst diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst index 1ae7fb0cbc2e2c..54f176a9e3e103 100644 --- a/Doc/whatsnew/3.10.rst +++ b/Doc/whatsnew/3.10.rst @@ -1176,7 +1176,7 @@ Add option to create MPTCP sockets with ``IPPROTO_MPTCP`` (Contributed by Rui Cunha in :issue:`43571`.) Add constants for IsoTp CAN protocol. -(Contributed by Patrick Menschel) +(Contributed by Patrick Menschel in :issue:`42653`.) ssl --- diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-05-19-19-41-15.bpo-42653.qvfUpA.rst b/Misc/NEWS.d/next/Core and Builtins/2021-05-19-19-41-15.bpo-42653.qvfUpA.rst new file mode 100644 index 00000000000000..aa6e4b115b346f --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2021-05-19-19-41-15.bpo-42653.qvfUpA.rst @@ -0,0 +1 @@ +Add constants for IsoTp CAN protocol. From 2f19691b3558f6cee0a1254efef42af03e7f08a8 Mon Sep 17 00:00:00 2001 From: Patrick Menschel Date: Sat, 22 May 2021 15:50:28 +0200 Subject: [PATCH 7/9] Fix: Setting of HAVE_LINUX_CAN_ISOTP_H by configure For unknown reason, HAVE_LINUX_CAN_ISOTP_H was not set during build. After changing the order of items in that particular line of configure.ac, it now works as expected. Signed-off-by: Patrick Menschel --- configure.ac | 4 ++-- pyconfig.h.in | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index bbea9eaf98d39b..c184abba32dfdd 100644 --- a/configure.ac +++ b/configure.ac @@ -2269,8 +2269,8 @@ AC_CHECK_HEADERS(linux/vm_sockets.h,,,[ #endif ]) -# On Linux, can.h, can/bcm.h, can/j1939.h, can/raw.h require sys/socket.h -AC_CHECK_HEADERS(linux/can.h linux/can/bcm.h linux/can/j1939.h linux/can/raw.h linux/can/isotp.h,,,[ +# On Linux, can.h, can/bcm.h, linux/can/isotp.h, can/j1939.h, can/raw.h require sys/socket.h +AC_CHECK_HEADERS(linux/can.h linux/can/bcm.h linux/can/isotp.h linux/can/j1939.h linux/can/raw.h,,,[ #ifdef HAVE_SYS_SOCKET_H #include #endif diff --git a/pyconfig.h.in b/pyconfig.h.in index 63438d857a070c..2c7a3086e565e1 100644 --- a/pyconfig.h.in +++ b/pyconfig.h.in @@ -643,6 +643,9 @@ /* Define to 1 if you have the header file. */ #undef HAVE_LINUX_CAN_J1939_H +/* Define to 1 if you have the header file. */ +#undef HAVE_LINUX_CAN_ISOTP_H + /* Define if compiling using Linux 3.6 or later. */ #undef HAVE_LINUX_CAN_RAW_FD_FRAMES From 5860ad030f4b68cf82a8e5fec48b39b74ffa1579 Mon Sep 17 00:00:00 2001 From: Patrick Menschel Date: Sat, 22 May 2021 18:16:04 +0200 Subject: [PATCH 8/9] Fix: Change order of headers in configure This may not be necessary but it is clean. Also normalize the comment line above. Signed-off-by: Patrick Menschel --- configure | 4 ++-- configure.ac | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/configure b/configure index b007bfdbabab62..bd8d509f262fd4 100755 --- a/configure +++ b/configure @@ -8405,8 +8405,8 @@ fi done -# On Linux, can.h, can/bcm.h, can/j1939.h, can/raw.h require sys/socket.h -for ac_header in linux/can.h linux/can/bcm.h linux/can/j1939.h linux/can/raw.h linux/can/isotp.h +# On Linux, can.h, can/bcm.h, can/isotp.h, can/j1939.h, can/raw.h require sys/socket.h +for ac_header in linux/can.h linux/can/bcm.h linux/can/isotp.h linux/can/j1939.h linux/can/raw.h do : as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh` ac_fn_c_check_header_compile "$LINENO" "$ac_header" "$as_ac_Header" " diff --git a/configure.ac b/configure.ac index c184abba32dfdd..d6856441431d35 100644 --- a/configure.ac +++ b/configure.ac @@ -2269,7 +2269,7 @@ AC_CHECK_HEADERS(linux/vm_sockets.h,,,[ #endif ]) -# On Linux, can.h, can/bcm.h, linux/can/isotp.h, can/j1939.h, can/raw.h require sys/socket.h +# On Linux, can.h, can/bcm.h, can/isotp.h, can/j1939.h, can/raw.h require sys/socket.h AC_CHECK_HEADERS(linux/can.h linux/can/bcm.h linux/can/isotp.h linux/can/j1939.h linux/can/raw.h,,,[ #ifdef HAVE_SYS_SOCKET_H #include From 8132cb162fb2fc21cd0f224df492bc945f0eaadc Mon Sep 17 00:00:00 2001 From: Patrick Menschel Date: Sun, 23 May 2021 08:48:52 +0200 Subject: [PATCH 9/9] Fix: Optionally include CAN_ISOTP_SF_BROADCAST CAN_ISOTP_SF_BROADCAST is new and not always available. This was the case on a recent RaspberryPi Kernel. Therefore make it optional and exclude it from test. Also add condition for testing the constants. Signed-off-by: Patrick Menschel --- Lib/test/test_socket.py | 5 ++++- Modules/socketmodule.c | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 4a433cb421a8fc..af6d631b758663 100755 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -2166,6 +2166,8 @@ def testCrucialConstants(self): socket.CAN_ISOTP socket.SOCK_DGRAM + @unittest.skipUnless(hasattr(socket, "SOL_CAN_ISOTP"), + 'Constants from isotp.h required for this test.') def testIsoTpConstants(self): socket.SOL_CAN_ISOTP @@ -2190,7 +2192,8 @@ def testIsoTpConstants(self): socket.CAN_ISOTP_FORCE_RXSTMIN socket.CAN_ISOTP_RX_EXT_ADDR socket.CAN_ISOTP_WAIT_TX_DONE - socket.CAN_ISOTP_SF_BROADCAST + # This constant is new and not always available + # socket.CAN_ISOTP_SF_BROADCAST # default values socket.CAN_ISOTP_DEFAULT_FLAGS diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index ce8f443a0c15e6..7ec6343f22a5e3 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -7809,7 +7809,10 @@ PyInit__socket(void) PyModule_AddIntMacro(m, CAN_ISOTP_FORCE_RXSTMIN); PyModule_AddIntMacro(m, CAN_ISOTP_RX_EXT_ADDR); PyModule_AddIntMacro(m, CAN_ISOTP_WAIT_TX_DONE); +#ifdef CAN_ISOTP_SF_BROADCAST + /* This constant is new and not always available */ PyModule_AddIntMacro(m, CAN_ISOTP_SF_BROADCAST); +#endif /* default values */ PyModule_AddIntMacro(m, CAN_ISOTP_DEFAULT_FLAGS);