% Regression tests for nativecansocket ~ python3_only vcan_socket # More information at http://www.secdev.org/projects/UTscapy/ ############ ############ + Configuration of CAN virtual sockets = Load module ~ conf command needs_root linux load_layer("can") conf.contribs['CANSocket'] = {'use-python-can': False} from scapy.contrib.cansocket_native import * conf.contribs['CAN'] = {'swap-bytes': False} = Setup string for vcan ~ conf command bashCommand = "/bin/bash -c 'sudo modprobe vcan; sudo ip link add name vcan0 type vcan; sudo ip link set dev vcan0 up'" = Load os ~ conf command needs_root linux import os import threading from time import sleep = Setup vcan0 ~ conf command needs_root linux 0 == os.system(bashCommand) + Basic Packet Tests() = CAN Packet init ~ needs_root linux canframe = CAN(identifier=0x7ff,length=8,data=b'\x01\x02\x03\x04\x05\x06\x07\x08') bytes(canframe) == b'\x00\x00\x07\xff\x08\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08' + Basic Socket Tests() = CAN Socket Init ~ needs_root linux sock1 = CANSocket(iface="vcan0") = CAN Socket send recv small packet ~ needs_root linux def sender(): sleep(0.1) sock2 = CANSocket(iface="vcan0") sock2.send(CAN(identifier=0x7ff,length=1,data=b'\x01')) sock2.close() thread = threading.Thread(target=sender) thread.start() rx = sock1.recv() rx == CAN(identifier=0x7ff,length=1,data=b'\x01') thread.join() = CAN Socket send recv ~ needs_root linux def sender(): sleep(0.1) sock2 = CANSocket(iface="vcan0") sock2.send(CAN(identifier=0x7ff,length=8,data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.close() thread = threading.Thread(target=sender) thread.start() rx = sock1.recv() rx == CAN(identifier=0x7ff,length=8,data=b'\x01\x02\x03\x04\x05\x06\x07\x08') thread.join() = CAN Socket basecls test ~ needs_root linux def sender(): sleep(0.1) sock2 = CANSocket(iface="vcan0") sock2.send(CAN(identifier=0x7ff,length=8,data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.close() sock1.basecls = Raw thread = threading.Thread(target=sender) thread.start() rx = sock1.recv() rx == Raw(bytes(CAN(identifier=0x7ff,length=8,data=b'\x01\x02\x03\x04\x05\x06\x07\x08'))) sock1.basecls = CAN thread.join() + Advanced Socket Tests() = CAN Socket sr1 ~ needs_root linux tx = CAN(identifier=0x7ff,length=8,data=b'\x01\x02\x03\x04\x05\x06\x07\x08') = CAN Socket sr1 init time ~ needs_root linux tx.sent_time == None def sender(): sleep(0.1) sock2 = CANSocket(iface="vcan0") sock2.send(tx) sock2.close() thread = threading.Thread(target=sender) thread.start() rx = None rx = sock1.sr1(tx) rx == tx sock1.close() thread.join() = CAN Socket sr1 time check ~ needs_root linux tx.sent_time < rx.time and rx.time > 0 = srcan ~ needs_root linux tx = CAN(identifier=0x7ff,length=8,data=b'\x01\x02\x03\x04\x05\x06\x07\x08') = srcan check init time ~ needs_root linux assert tx.sent_time == None def sender(): sleep(0.1) sock2 = CANSocket(iface="vcan0") sock2.send(tx) sock2.close() thread = threading.Thread(target=sender) thread.start() rx = None rx = srcan(tx, "vcan0", timeout=1) rx = rx[0][0][1] tx == rx thread.join() = srcan check init time basecls ~ needs_root linux def sender(): sleep(0.1) sock2 = CANSocket(iface="vcan0") sock2.send(tx) sock2.close() thread = threading.Thread(target=sender) thread.start() rx = None rx = srcan(tx, "vcan0", timeout=1, basecls=Raw) rx = rx[0][0][1] Raw(bytes(tx)) == rx thread.join() = srcan check rx and tx ~ needs_root linux tx.sent_time > 0 and rx.time > 0 and tx.sent_time < rx.time = sniff with filtermask 0x7ff ~ needs_root linux sock1 = CANSocket(iface='vcan0', can_filters=[{'can_id': 0x200, 'can_mask': 0x7ff}]) def sender(): sock2 = CANSocket(iface="vcan0") sock2.send(CAN(identifier=0x200, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x300, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x300, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x200, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x100, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x200, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.close() thread = threading.Thread(target=sender) packets = sock1.sniff(timeout=0.2, started_callback=thread.start) len(packets) == 3 sock1.close() thread.join() = sniff with filtermask 0x700 ~ needs_root linux sock1 = CANSocket(iface='vcan0', can_filters=[{'can_id': 0x200, 'can_mask': 0x700}]) def sender(): sock2 = CANSocket(iface="vcan0") sock2.send(CAN(identifier=0x212, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x300, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x2ff, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x1ff, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x200, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x2aa, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.close() thread = threading.Thread(target=sender) packets = sock1.sniff(timeout=0.2, started_callback=thread.start) len(packets) == 4 sock1.close() thread.join() = sniff with filtermask 0x0ff ~ needs_root linux sock1 = CANSocket(iface='vcan0', can_filters=[{'can_id': 0x200, 'can_mask': 0x0ff}]) def sender(): sock2 = CANSocket(iface="vcan0") sock2.send(CAN(identifier=0x200, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x301, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x300, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x1ff, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x700, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x100, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.close() thread = threading.Thread(target=sender) packets = sock1.sniff(timeout=0.2, started_callback=thread.start) len(packets) == 4 sock1.close() thread.join() = sniff with multiple filters ~ needs_root linux sock1 = CANSocket(iface='vcan0', can_filters=[{'can_id': 0x200, 'can_mask': 0x7ff}, {'can_id': 0x400, 'can_mask': 0x7ff}, {'can_id': 0x600, 'can_mask': 0x7ff}, {'can_id': 0x7ff, 'can_mask': 0x7ff}]) def sender(): sock2 = CANSocket(iface="vcan0") sock2.send(CAN(identifier=0x200, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x300, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x400, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x500, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x600, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x700, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x7ff, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.close() thread = threading.Thread(target=sender) packets = sock1.sniff(timeout=0.2, started_callback=thread.start) len(packets) == 4 sock1.close() thread.join() = sniff with filtermask 0x7ff and inverse filter ~ needs_root linux sock1 = CANSocket(iface='vcan0', can_filters=[{'can_id': 0x200 | CAN_INV_FILTER, 'can_mask': 0x7ff}]) def sender(): sock2 = CANSocket(iface="vcan0") sock2.send(CAN(identifier=0x200, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x200, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x300, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x200, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x100, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(identifier=0x200, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.close() thread = threading.Thread(target=sender) packets = sock1.sniff(timeout=0.2, started_callback=thread.start) len(packets) == 2 sock1.close() thread.join() = sniff with filtermask 0x1FFFFFFF ~ needs_root linux sock1 = CANSocket(iface='vcan0', can_filters=[{'can_id': 0x10000000, 'can_mask': 0x1fffffff}]) def sender(): sock2 = CANSocket(iface="vcan0") sock2.send(CAN(flags='extended', identifier=0x10010000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(flags='extended', identifier=0x10020000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(flags='extended', identifier=0x10000000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(flags='extended', identifier=0x10030000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(flags='extended', identifier=0x10040000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.send(CAN(flags='extended', identifier=0x10000000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock2.close() thread = threading.Thread(target=sender) packets = sock1.sniff(timeout=0.2, started_callback=thread.start) len(packets) == 2 sock1.close() thread.join() = sniff with filtermask 0x1FFFFFFF and inverse filter ~ needs_root linux sock1 = CANSocket(iface='vcan0', can_filters=[{'can_id': 0x10000000 | CAN_INV_FILTER, 'can_mask': 0x1fffffff}]) if six.PY3: thread = threading.Thread(target=sender) packets = sock1.sniff(timeout=0.2, started_callback=thread.start) len(packets) == 4 sock1.close() = CAN Socket sr1 with receive own messages ~ needs_root linux sock1 = CANSocket(iface="vcan0", receive_own_messages=True) tx = CAN(identifier=0x7ff,length=8,data=b'\x01\x02\x03\x04\x05\x06\x07\x08') rx = None rx = sock1.sr1(tx) tx == rx tx.sent_time < rx.time and tx == rx and rx.time > 0 sock1.close() = srcan ~ needs_root linux tx = CAN(identifier=0x7ff,length=8,data=b'\x01\x02\x03\x04\x05\x06\x07\x08') rx = None rx = srcan(tx, iface="vcan0", receive_own_messages=True, timeout=1) tx == rx[0][0][1] + bridge and sniff tests = bridge and sniff setup vcan1 package forwarding ~ needs_root linux bashCommand = "/bin/bash -c 'sudo ip link add name vcan1 type vcan; sudo ip link set dev vcan1 up'" 0 == os.system(bashCommand) sock0 = CANSocket(iface='vcan0') sock1 = CANSocket(iface='vcan1') def senderVCan0(): sock0.send(CAN(flags='extended', identifier=0x10010000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10020000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10000000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10030000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10040000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10000000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) bridgeStarted = threading.Event() def bridge(): global bridgeStarted bSock0 = CANSocket(iface="vcan0") bSock1 = CANSocket(iface='vcan1') def pnr(pkt): return pkt bridgeStarted.set() bridge_and_sniff(if1=bSock0, if2=bSock1, xfrm12=pnr, xfrm21=pnr, timeout=0.2) bSock0.close() bSock1.close() threadBridge = threading.Thread(target=bridge) threadBridge.start() threadSender = threading.Thread(target=senderVCan0) bridgeStarted.wait() packetsVCan1 = sock1.sniff(timeout=0.2, started_callback=threadSender.start) len(packetsVCan1) == 6 threadSender.join() threadBridge.join() sock1.close() sock0.close() = bridge and sniff setup vcan0 package forwarding ~ needs_root linux sock0 = CANSocket(iface='vcan0') sock1 = CANSocket(iface='vcan1') def senderVCan1(): sock1.send(CAN(flags='extended', identifier=0x10010000, length=8, data=b'\x01\x02\x03\x04\x05\x04\x05\x06')) sock1.send(CAN(flags='extended', identifier=0x10010000, length=8, data=b'\x01\x02\x03\x04\x05\x04\x05\x06')) sock1.send(CAN(flags='extended', identifier=0x80, length=8, data=b'\x01\x02\x03\x04\x05\x04\x05\x06')) sock1.send(CAN(flags='extended', identifier=0x10010000, length=8, data=b'\x01\x02\x03\x04\x05\x04\x05\x06')) bridgeStarted = threading.Event() def bridge(): global bridgeStarted bSock0 = CANSocket(iface="vcan0") bSock1 = CANSocket(iface='vcan1') def pnr(pkt): return pkt bridgeStarted.set() bridge_and_sniff(if1=bSock0, if2=bSock1, xfrm12=pnr, xfrm21=pnr, timeout=0.2) bSock0.close() bSock1.close() threadBridge = threading.Thread(target=bridge) threadBridge.start() threadSender = threading.Thread(target=senderVCan1) bridgeStarted.wait() packetsVCan0 = sock0.sniff(timeout=0.2, started_callback=threadSender.start) len(packetsVCan0) == 4 sock0.close() sock1.close() threadSender.join() threadBridge.join() =bridge and sniff setup vcan0 vcan1 package forwarding both directions ~ needs_root linux sock0 = CANSocket(iface='vcan0') sock1 = CANSocket(iface='vcan1') def senderBothVCans(): sock0.send(CAN(flags='extended', identifier=0x25, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x20, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x25, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x25, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x20, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x30, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock1.send(CAN(flags='extended', identifier=0x40, length=8, data=b'\x01\x02\x03\x04\x05\x04\x05\x06')) sock1.send(CAN(flags='extended', identifier=0x40, length=8, data=b'\x01\x02\x03\x04\x05\x04\x05\x06')) sock1.send(CAN(flags='extended', identifier=0x80, length=8, data=b'\x01\x02\x03\x04\x05\x04\x05\x06')) sock1.send(CAN(flags='extended', identifier=0x40, length=8, data=b'\x01\x02\x03\x04\x05\x04\x05\x06')) bridgeStarted = threading.Event() def bridge(): global bridgeStarted bSock0 = CANSocket(iface="vcan0") bSock1 = CANSocket(iface='vcan1') def pnr(pkt): return pkt bridgeStarted.set() bridge_and_sniff(if1=bSock0, if2=bSock1, xfrm12=pnr, xfrm21=pnr, timeout=0.2) bSock0.close() bSock1.close() threadBridge = threading.Thread(target=bridge) threadBridge.start() threadSender = threading.Thread(target=senderBothVCans) bridgeStarted.wait() packetsVCan0 = sock0.sniff(timeout=0.1, count=6, started_callback=threadSender.start) packetsVCan1 = sock1.sniff(timeout=0.1) len(packetsVCan0) == 4 len(packetsVCan1) == 6 sock0.close() sock1.close() threadSender.join() threadBridge.join() =bridge and sniff setup vcan1 package change ~ needs_root linux sock0 = CANSocket(iface='vcan0') sock1 = CANSocket(iface='vcan1', can_filters=[{'can_id': 0x10010000, 'can_mask': 0x1fffffff}]) def senderVCan0(): sleep(0.1) sock0.send(CAN(flags='extended', identifier=0x10010000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10020000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10000000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10030000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10040000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10000000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) bridgeStarted = threading.Event() def bridgeWithPackageChangeVCan0ToVCan1(): global bridgeStarted bSock0 = CANSocket(iface="vcan0") bSock1 = CANSocket(iface="vcan1") def pnr(pkt): pkt.data = b'\x08\x07\x06\x05\x04\x03\x02\x01' pkt.identifier = 0x10010000 return pkt bridgeStarted.set() bridge_and_sniff(if1=bSock0, if2=bSock1, xfrm12=pnr, timeout=0.2) bSock0.close() bSock1.close() threadBridge = threading.Thread(target=bridgeWithPackageChangeVCan0ToVCan1) threadBridge.start() threadSender = threading.Thread(target=senderVCan0) bridgeStarted.wait() packetsVCan1 = sock1.sniff(timeout=0.2, started_callback=threadSender.start) len(packetsVCan1) == 6 sock0.close() sock1.close() threadSender.join() threadBridge.join() =bridge and sniff setup vcan0 package change ~ needs_root linux sock0 = CANSocket(iface='vcan0', can_filters=[{'can_id': 0x10010000, 'can_mask': 0x1fffffff}]) sock1 = CANSocket(iface='vcan1') def senderVCan1(): sleep(0.1) sock1.send(CAN(flags='extended', identifier=0x10010000, length=8, data=b'\x01\x02\x03\x04\x05\x04\x05\x06')) sock1.send(CAN(flags='extended', identifier=0x10010000, length=8, data=b'\x01\x02\x03\x04\x05\x04\x05\x06')) sock1.send(CAN(flags='extended', identifier=0x10050000, length=8, data=b'\x01\x02\x03\x04\x05\x04\x05\x06')) sock1.send(CAN(flags='extended', identifier=0x10010000, length=8, data=b'\x01\x02\x03\x04\x05\x04\x05\x06')) bridgeStarted = threading.Event() def bridgeWithPackageChangeVCan1ToVCan0(): global bridgeStarted bSock0 = CANSocket(iface="vcan0") bSock1 = CANSocket(iface="vcan1") def pnr(pkt): pkt.data = b'\x08\x07\x06\x05\x04\x03\x02\x01' pkt.identifier = 0x10010000 return pkt bridgeStarted.set() bridge_and_sniff(if1=bSock0, if2=bSock1, xfrm21=pnr, timeout=0.2) bSock0.close() bSock1.close() threadBridge = threading.Thread(target=bridgeWithPackageChangeVCan1ToVCan0) threadBridge.start() threadSender = threading.Thread(target=senderVCan1) bridgeStarted.wait() packetsVCan0 = sock0.sniff(timeout=0.2, started_callback=threadSender.start) len(packetsVCan0) == 4 sock0.close() sock1.close() threadSender.join() threadBridge.join() =bridge and sniff setup vcan0 and vcan1 package change in both directions ~ needs_root linux sock0 = CANSocket(iface='vcan0', can_filters=[{'can_id': 0x10010000, 'can_mask': 0x1fffffff}]) sock1 = CANSocket(iface='vcan1', can_filters=[{'can_id': 0x10010000, 'can_mask': 0x1fffffff}]) def senderBothVCans(): sock0.send(CAN(flags='extended', identifier=0x10010000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10020000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10000000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10030000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10040000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10000000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock1.send(CAN(flags='extended', identifier=0x10010000, length=8, data=b'\x01\x02\x03\x04\x05\x04\x05\x06')) sock1.send(CAN(flags='extended', identifier=0x10010000, length=8, data=b'\x01\x02\x03\x04\x05\x04\x05\x06')) sock1.send(CAN(flags='extended', identifier=0x10050000, length=8, data=b'\x01\x02\x03\x04\x05\x04\x05\x06')) sock1.send(CAN(flags='extended', identifier=0x10010000, length=8, data=b'\x01\x02\x03\x04\x05\x04\x05\x06')) bridgeStarted = threading.Event() def bridgeWithPackageChangeBothDirections(): global bridgeStarted bSock0 = CANSocket(iface="vcan0") bSock1 = CANSocket(iface="vcan1") def pnr(pkt): pkt.data = b'\x08\x07\x06\x05\x04\x03\x02\x01' pkt.identifier = 0x10010000 return pkt bridgeStarted.set() bridge_and_sniff(if1=bSock0, if2=bSock1, xfrm12=pnr, xfrm21=pnr, timeout=0.2) bSock0.close() bSock1.close() threadBridge = threading.Thread(target=bridgeWithPackageChangeBothDirections) threadBridge.start() threadSender = threading.Thread(target=senderBothVCans) bridgeStarted.wait() threadSender.start() packetsVCan0 = sock0.sniff(timeout=0.1) packetsVCan1 = sock1.sniff(timeout=0.1) len(packetsVCan0) == 4 len(packetsVCan1) == 6 sock0.close() sock1.close() threadSender.join() threadBridge.join() =bridge and sniff setup vcan0 package remove ~ needs_root linux sock0 = CANSocket(iface='vcan0') sock1 = CANSocket(iface='vcan1') def senderVCan0(): sock0.send(CAN(flags='extended', identifier=0x10010000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10020000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10000000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10030000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10040000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10000000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) bridgeStarted = threading.Event() def bridgeWithRemovePackageFromVCan0ToVCan1(): global bridgeStarted bSock0 = CANSocket(iface="vcan0") bSock1 = CANSocket(iface="vcan1") def pnr(pkt): if(pkt.identifier == 0x10020000): pkt = None else: pkt = pkt return pkt bridgeStarted.set() bridge_and_sniff(if1=bSock0, if2=bSock1, xfrm12=pnr, timeout=0.2) bSock0.close() bSock1.close() threadBridge = threading.Thread(target=bridgeWithRemovePackageFromVCan0ToVCan1) threadBridge.start() threadSender = threading.Thread(target=senderVCan0) bridgeStarted.wait() threadSender.start() packetsVCan1 = sock1.sniff(timeout=0.2) len(packetsVCan1) == 5 sock0.close() sock1.close() threadSender.join() threadBridge.join() =bridge and sniff setup vcan1 package remove ~ needs_root linux sock0 = CANSocket(iface='vcan0') sock1 = CANSocket(iface='vcan1') def senderVCan1(): sock1.send(CAN(flags='extended', identifier=0x10010000, length=8, data=b'\x01\x02\x03\x04\x05\x04\x05\x06')) sock1.send(CAN(flags='extended', identifier=0x10010000, length=8, data=b'\x01\x02\x03\x04\x05\x04\x05\x06')) sock1.send(CAN(flags='extended', identifier=0x10050000, length=8, data=b'\x01\x02\x03\x04\x05\x04\x05\x06')) sock1.send(CAN(flags='extended', identifier=0x10010000, length=8, data=b'\x01\x02\x03\x04\x05\x04\x05\x06')) bridgeStarted = threading.Event() def bridgeWithRemovePackageFromVCan1ToVCan0(): global bridgeStarted bSock0 = CANSocket(iface="vcan0") bSock1 = CANSocket(iface="vcan1") def pnr(pkt): if(pkt.identifier == 0x10050000): pkt = None else: pkt = pkt return pkt bridgeStarted.set() bridge_and_sniff(if1=bSock0, if2=bSock1, xfrm21=pnr, timeout=0.2) bSock0.close() bSock1.close() threadBridge = threading.Thread(target=bridgeWithRemovePackageFromVCan1ToVCan0) threadBridge.start() threadSender = threading.Thread(target=senderVCan1) bridgeStarted.wait() threadSender.start() packetsVCan0 = sock0.sniff(timeout=0.2) len(packetsVCan0) == 3 sock0.close() sock1.close() threadSender.join() threadBridge.join() =bridge and sniff setup vcan0 and vcan1 package remove both directions ~ needs_root linux sock0 = CANSocket(iface="vcan0") sock1 = CANSocket(iface="vcan1") def senderBothVCans(): sock0.send(CAN(flags='extended', identifier=0x10010000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10020000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10000000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10030000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10040000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock0.send(CAN(flags='extended', identifier=0x10000000, length=8, data=b'\x01\x02\x03\x04\x05\x06\x07\x08')) sock1.send(CAN(flags='extended', identifier=0x10010000, length=8, data=b'\x01\x02\x03\x04\x05\x04\x05\x06')) sock1.send(CAN(flags='extended', identifier=0x10010000, length=8, data=b'\x01\x02\x03\x04\x05\x04\x05\x06')) sock1.send(CAN(flags='extended', identifier=0x10050000, length=8, data=b'\x01\x02\x03\x04\x05\x04\x05\x06')) sock1.send(CAN(flags='extended', identifier=0x10010000, length=8, data=b'\x01\x02\x03\x04\x05\x04\x05\x06')) bridgeStarted = threading.Event() def bridgeWithRemovePackageInBothDirections(): global bridgeStarted bSock0 = CANSocket(iface="vcan0") bSock1 = CANSocket(iface="vcan1") def pnrA(pkt): if(pkt.identifier == 0x10020000): pkt = None else: pkt = pkt return pkt def pnrB(pkt): if (pkt.identifier == 0x10050000): pkt = None else: pkt = pkt return pkt bridgeStarted.set() bridge_and_sniff(if1=bSock0, if2=bSock1, xfrm12=pnrA, xfrm21=pnrB, timeout=0.2) bSock0.close() bSock1.close() threadBridge = threading.Thread(target=bridgeWithRemovePackageInBothDirections) threadBridge.start() threadSender = threading.Thread(target=senderBothVCans) bridgeStarted.wait() packetsVCan0 = sock0.sniff(timeout=0.1, started_callback=threadSender.start) packetsVCan1 = sock1.sniff(timeout=0.1) len(packetsVCan0) == 3 len(packetsVCan1) == 5 sock0.close() sock1.close() threadSender.join() threadBridge.join()