| 52 | desc = "read/write packets at a given CAN interface using PF_CAN sockets" |
| 53 | |
| 54 | def __init__(self, |
| 55 | channel=None, # type: Optional[str] |
| 56 | receive_own_messages=False, # type: bool |
| 57 | can_filters=None, # type: Optional[List[Dict[str, int]]] |
| 58 | fd=False, # type: bool |
| 59 | basecls=CAN, # type: Type[Packet] |
| 60 | **kwargs # type: Dict[str, Any] |
| 61 | ): |
| 62 | # type: (...) -> None |
| 63 | bustype = cast(Optional[str], kwargs.pop("bustype", None)) |
| 64 | if bustype and bustype != "socketcan": |
| 65 | warning("You created a NativeCANSocket. " |
| 66 | "If you're providing the argument 'bustype', please use " |
| 67 | "the correct one to achieve compatibility with python-can" |
| 68 | "/PythonCANSocket. \n'bustype=socketcan'") |
| 69 | |
| 70 | self.MTU = CAN_MTU |
| 71 | self.fd = fd |
| 72 | self.basecls = basecls |
| 73 | self.channel = conf.contribs['NativeCANSocket']['channel'] if \ |
| 74 | channel is None else channel |
| 75 | self.ins = socket.socket(socket.PF_CAN, |
| 76 | socket.SOCK_RAW, |
| 77 | socket.CAN_RAW) |
| 78 | try: |
| 79 | self.ins.setsockopt(socket.SOL_CAN_RAW, |
| 80 | socket.CAN_RAW_RECV_OWN_MSGS, |
| 81 | struct.pack("i", receive_own_messages)) |
| 82 | except Exception as exception: |
| 83 | raise Scapy_Exception( |
| 84 | "Could not modify receive own messages (%s)", exception |
| 85 | ) |
| 86 | |
| 87 | try: |
| 88 | # Receive Auxiliary Data (Timestamps) |
| 89 | self.ins.setsockopt( |
| 90 | socket.SOL_SOCKET, |
| 91 | SO_TIMESTAMPNS, |
| 92 | 1 |
| 93 | ) |
| 94 | self.auxdata_available = True |
| 95 | except OSError: |
| 96 | # Note: Auxiliary Data is only supported since |
| 97 | # Linux 2.6.21 |
| 98 | msg = "Your Linux Kernel does not support Auxiliary Data!" |
| 99 | log_runtime.info(msg) |
| 100 | |
| 101 | if self.fd: |
| 102 | try: |
| 103 | self.ins.setsockopt(socket.SOL_CAN_RAW, |
| 104 | socket.CAN_RAW_FD_FRAMES, |
| 105 | 1) |
| 106 | self.MTU = CAN_FD_MTU |
| 107 | except Exception as exception: |
| 108 | raise Scapy_Exception( |
| 109 | "Could not enable CAN FD support (%s)", exception |
| 110 | ) |
| 111 | |