| 563 | desc = "read/write packets at layer 2 using only libpcap" |
| 564 | |
| 565 | def __init__(self, |
| 566 | iface=None, # type: Optional[_GlobInterfaceType] |
| 567 | type=ETH_P_ALL, # type: int |
| 568 | promisc=None, # type: Optional[bool] |
| 569 | filter=None, # type: Optional[str] |
| 570 | nofilter=0, # type: int |
| 571 | monitor=None # type: Optional[bool] |
| 572 | ): |
| 573 | # type: (...) -> None |
| 574 | if iface is None: |
| 575 | iface = conf.iface |
| 576 | self.iface = iface |
| 577 | self.type = type |
| 578 | if promisc is not None: |
| 579 | self.promisc = promisc |
| 580 | else: |
| 581 | self.promisc = conf.sniff_promisc |
| 582 | self.monitor = monitor |
| 583 | fd = open_pcap( |
| 584 | device=iface, |
| 585 | snaplen=MTU, |
| 586 | promisc=self.promisc, |
| 587 | to_ms=100, |
| 588 | monitor=self.monitor, |
| 589 | ) |
| 590 | super(L2pcapSocket, self).__init__(fd) |
| 591 | try: |
| 592 | if not WINDOWS: |
| 593 | ioctl( |
| 594 | self.pcap_fd.fileno(), |
| 595 | BIOCIMMEDIATE, |
| 596 | struct.pack("I", 1) |
| 597 | ) |
| 598 | except Exception: |
| 599 | pass |
| 600 | if nofilter: |
| 601 | if type != ETH_P_ALL: |
| 602 | # PF_PACKET stuff. Need to emulate this for pcap |
| 603 | filter = "ether proto %i" % type |
| 604 | else: |
| 605 | filter = None |
| 606 | else: |
| 607 | if conf.except_filter: |
| 608 | if filter: |
| 609 | filter = "(%s) and not (%s)" % (filter, conf.except_filter) # noqa: E501 |
| 610 | else: |
| 611 | filter = "not (%s)" % conf.except_filter |
| 612 | if type != ETH_P_ALL: |
| 613 | # PF_PACKET stuff. Need to emulate this for pcap |
| 614 | if filter: |
| 615 | filter = "(ether proto %i) and (%s)" % (type, filter) |
| 616 | else: |
| 617 | filter = "ether proto %i" % type |
| 618 | self.filter = filter |
| 619 | if filter: |
| 620 | self.pcap_fd.setfilter(filter) |
| 621 | |
| 622 | def send(self, x): |