| 465 | |
| 466 | |
| 467 | class L3bpfSocket(L2bpfSocket): |
| 468 | |
| 469 | def __init__(self, |
| 470 | iface=None, # type: Optional[_GlobInterfaceType] |
| 471 | type=ETH_P_ALL, # type: int |
| 472 | promisc=None, # type: Optional[bool] |
| 473 | filter=None, # type: Optional[str] |
| 474 | nofilter=0, # type: int |
| 475 | monitor=False, # type: bool |
| 476 | ): |
| 477 | super(L3bpfSocket, self).__init__( |
| 478 | iface=iface, |
| 479 | type=type, |
| 480 | promisc=promisc, |
| 481 | filter=filter, |
| 482 | nofilter=nofilter, |
| 483 | monitor=monitor, |
| 484 | ) |
| 485 | self.filter = filter |
| 486 | self.send_socks = {network_name(self.iface): self} |
| 487 | |
| 488 | def recv(self, x: int = BPF_BUFFER_LENGTH, **kwargs: Any) -> Optional['Packet']: |
| 489 | """Receive on layer 3""" |
| 490 | r = SuperSocket.recv(self, x, **kwargs) |
| 491 | if r: |
| 492 | r.payload.time = r.time |
| 493 | return r.payload |
| 494 | return r |
| 495 | |
| 496 | def send(self, pkt): |
| 497 | # type: (Packet) -> int |
| 498 | """Send a packet""" |
| 499 | from scapy.layers.l2 import Loopback |
| 500 | |
| 501 | # Use the routing table to find the output interface |
| 502 | iff = pkt.route()[0] |
| 503 | if iff is None: |
| 504 | iff = network_name(conf.iface) |
| 505 | |
| 506 | # Assign the network interface to the BPF handle |
| 507 | if iff not in self.send_socks: |
| 508 | self.send_socks[iff] = L3bpfSocket( |
| 509 | iface=iff, |
| 510 | type=self.type, |
| 511 | filter=self.filter, |
| 512 | promisc=self.promisc, |
| 513 | ) |
| 514 | fd = self.send_socks[iff] |
| 515 | |
| 516 | # Build the frame |
| 517 | # |
| 518 | # LINKTYPE_NULL / DLT_NULL (Loopback) is a special case. From the |
| 519 | # bpf(4) man page (from macOS/Darwin, but also for BSD): |
| 520 | # |
| 521 | # "A packet can be sent out on the network by writing to a bpf file |
| 522 | # descriptor. [...] Currently only writes to Ethernets and SLIP links |
| 523 | # are supported." |
| 524 | # |
no outgoing calls
no test coverage detected
searching dependent graphs…