Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Added support for CAN_ISOTP protocol
  • Loading branch information
pylessard committed Jul 26, 2017
commit 4d609c08e748168f03ab5ed8b9976996f3fcdf02
72 changes: 70 additions & 2 deletions Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1869,7 +1869,9 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
}
#endif

#if defined(AF_CAN) && defined(CAN_RAW) && defined(CAN_BCM)
#if defined(AF_CAN)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use ifdef for simple checks: #ifdef AF_CAN


#if defined(CAN_RAW) && defined(CAN_BCM)
case AF_CAN:
switch (s->sock_proto) {
case CAN_RAW:
Expand All @@ -1880,7 +1882,6 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
PyObject *interfaceName;
struct ifreq ifr;
Py_ssize_t len;

addr = (struct sockaddr_can *)addr_ret;

if (!PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter,
Expand Down Expand Up @@ -1913,6 +1914,54 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
Py_DECREF(interfaceName);
return 1;
}
#endif

#if defined(CAN_ISOTP)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#ifdef CAN_ISOTP

case CAN_ISOTP:
{
struct sockaddr_can *addr;
PyObject *interfaceName;
struct ifreq ifr;
Py_ssize_t len;
unsigned long int rx_id, tx_id;

addr = (struct sockaddr_can *)addr_ret;

if (!PyArg_ParseTuple(args, "O&kk", PyUnicode_FSConverter,
&interfaceName,
&rx_id,
&tx_id))
return 0;

len = PyBytes_GET_SIZE(interfaceName);

if (len == 0) {
ifr.ifr_ifindex = 0;
} else if ((size_t)len < sizeof(ifr.ifr_name)) {
strncpy(ifr.ifr_name, PyBytes_AS_STRING(interfaceName), sizeof(ifr.ifr_name));
ifr.ifr_name[(sizeof(ifr.ifr_name))-1] = '\0';
if (ioctl(s->sock_fd, SIOCGIFINDEX, &ifr) < 0) {
s->errorhandler();
Py_DECREF(interfaceName);
return 0;
}
} else {
PyErr_SetString(PyExc_OSError,
"AF_CAN interface name too long");
Py_DECREF(interfaceName);
return 0;
}

addr->can_family = AF_CAN;
addr->can_ifindex = ifr.ifr_ifindex;
addr->can_addr.tp.rx_id = rx_id;
addr->can_addr.tp.tx_id = tx_id;

*len_ret = sizeof(*addr);
Py_DECREF(interfaceName);
return 1;
}
#endif
default:
PyErr_SetString(PyExc_OSError,
"getsockaddrarg: unsupported CAN protocol");
Expand Down Expand Up @@ -7018,6 +7067,25 @@ PyInit__socket(void)
PyModule_AddIntConstant(m, "CAN_BCM_RX_TIMEOUT", RX_TIMEOUT);
PyModule_AddIntConstant(m, "CAN_BCM_RX_CHANGED", RX_CHANGED);
#endif
#ifdef HAVE_LINUX_CAN_ISOTP_H
PyModule_AddIntMacro(m, SOL_CAN_ISOTP);
PyModule_AddIntMacro(m, CAN_ISOTP);
PyModule_AddIntMacro(m, CAN_ISOTP_OPTS);
PyModule_AddIntMacro(m, CAN_ISOTP_RECV_FC);
PyModule_AddIntMacro(m, CAN_ISOTP_TX_STMIN);
PyModule_AddIntMacro(m, CAN_ISOTP_RX_STMIN);
PyModule_AddIntMacro(m, CAN_ISOTP_LL_OPTS);
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);
#endif
#ifdef SOL_RDS
PyModule_AddIntMacro(m, SOL_RDS);
#endif
Expand Down
4 changes: 4 additions & 0 deletions Modules/socketmodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ typedef int socklen_t;
#include <linux/can/bcm.h>
#endif

#ifdef HAVE_LINUX_CAN_ISOTP_H
#include <linux/can/isotp.h>
#endif

#ifdef HAVE_SYS_SYS_DOMAIN_H
#include <sys/sys_domain.h>
#endif
Expand Down
4 changes: 2 additions & 2 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -8127,8 +8127,8 @@ fi
done


# On Linux, can.h and can/raw.h require sys/socket.h
for ac_header in linux/can.h linux/can/raw.h linux/can/bcm.h
# On Linux, can.h, can/raw.h, can/isotp.h require sys/socket.h
for ac_header in linux/can.h linux/can/raw.h linux/can/bcm.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" "
Expand Down
4 changes: 2 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2120,8 +2120,8 @@ AC_CHECK_HEADERS(linux/netlink.h,,,[
#endif
])

# On Linux, can.h and can/raw.h require sys/socket.h
AC_CHECK_HEADERS(linux/can.h linux/can/raw.h linux/can/bcm.h,,,[
# On Linux, can.h, can/raw.h, can/isotp.h require sys/socket.h
AC_CHECK_HEADERS(linux/can.h linux/can/raw.h linux/can/bcm.h linux/can/isotp.h,,,[
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
Expand Down
3 changes: 3 additions & 0 deletions pyconfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,9 @@
/* Define to 1 if you have the <linux/can.h> header file. */
#undef HAVE_LINUX_CAN_H

/* Define to 1 if you have the <linux/can/isotp.h> header file. */
#undef HAVE_LINUX_CAN_ISOTP_H

/* Define if compiling using Linux 3.6 or later. */
#undef HAVE_LINUX_CAN_RAW_FD_FRAMES

Expand Down