Ejemplo n.º 1
0
    def recv(self, x=MTU):
        # type: (int) -> Optional[Packet]
        data, sa_ll, ts = self._recv_raw(self.ins, x)
        if sa_ll[2] == socket.PACKET_OUTGOING:
            return None
        if sa_ll[3] in conf.l2types:
            cls = conf.l2types.num2layer[sa_ll[3]]  # type: Type[Packet]
            lvl = 2
        elif sa_ll[1] in conf.l3types:
            cls = conf.l3types.num2layer[sa_ll[1]]
            lvl = 3
        else:
            cls = conf.default_l2
            warning("Unable to guess type (interface=%s protocol=%#x family=%i). Using %s", sa_ll[0], sa_ll[1], sa_ll[3], cls.name)  # noqa: E501
            lvl = 3

        try:
            pkt = cls(data)
        except KeyboardInterrupt:
            raise
        except Exception:
            if conf.debug_dissector:
                raise
            pkt = conf.raw_layer(data)

        if lvl == 2:
            pkt = pkt.payload

        if pkt is not None:
            if ts is None:
                from scapy.arch.linux import get_last_packet_timestamp
                ts = get_last_packet_timestamp(self.ins)
            pkt.time = ts
        return pkt
Ejemplo n.º 2
0
    def recv_raw(self, x=0xffff):
        # type: (int) -> Tuple[Optional[Type[Packet]], Optional[bytes], Optional[float]]  # noqa: E501
        """
        Receives a packet, then returns a tuple containing
        (cls, pkt_data, time)
        """
        try:
            pkt, _, ts = self._recv_raw(self.ins, x)
        except BlockingIOError:  # noqa: F821
            warning('Captured no data, socket in non-blocking mode.')
            return None, None, None
        except socket.timeout:
            warning('Captured no data, socket read timed out.')
            return None, None, None
        except OSError as e:
            # something bad happened (e.g. the interface went down)
            warning("Captured no data. %s" % e)
            if e.errno == 84:
                warning("Maybe a consecutive frame was missed. "
                        "Increasing `stmin` could solve this problem.")
            elif e.errno == 110:
                warning('Captured no data, socket read timed out.')
            else:
                self.close()
            return None, None, None

        if ts is None:
            ts = get_last_packet_timestamp(self.ins)
        return self.basecls, pkt, ts
Ejemplo n.º 3
0
    def recv(self, x=CAN_FRAME_SIZE):
        try:
            pkt, sa_ll = self.ins.recvfrom(x)
        except BlockingIOError:  # noqa: F821
            warning("Captured no data, socket in non-blocking mode.")
            return None
        except socket.timeout:
            warning("Captured no data, socket read timed out.")
            return None
        except OSError:
            # something bad happened (e.g. the interface went down)
            warning("Captured no data.")
            return None

        # need to change the byte order of the first four bytes,
        # required by the underlying Linux SocketCAN frame format
        if not conf.contribs['CAN']['swap-bytes']:
            pkt = struct.pack("<I12s", *struct.unpack(">I12s", pkt))

        len = pkt[4]
        canpkt = self.basecls(pkt[:len + 8])
        canpkt.time = get_last_packet_timestamp(self.ins)
        if self.remove_padding:
            return canpkt
        else:
            return canpkt / Padding(pkt[len + 8:])
Ejemplo n.º 4
0
def try_receive_packet(s, packet_class=IP):
    try:
        packet = packet_class(s.recv(1024))
        packet.time = get_last_packet_timestamp(s)
        return packet
    except socket.error as e:
        if ERROR_NO_DATA == e[0]:
            return None
        else:
            raise
Ejemplo n.º 5
0
def try_receive_packet(s, packet_class=IP):
    try:
        packet = packet_class(s.recv(1024))
        packet.time = get_last_packet_timestamp(s)
        return packet
    except socket.error as e:
        if ERROR_NO_DATA == e[0]:
            return None
        else:
            raise
Ejemplo n.º 6
0
    def recv(self, x=0xffff):
        try:
            pkt, sa_ll = self.ins.recvfrom(x)
        except BlockingIOError:
            warning('Captured no data, socket in non-blocking mode.')
            return None
        except socket.timeout:
            warning('Captured no data, socket read timed out.')
            return None
        except OSError:
            # something bad happened (e.g. the interface went down)
            warning("Captured no data.")
            return None

        q = self.basecls(pkt)
        q.time = get_last_packet_timestamp(self.ins)
        return q
Ejemplo n.º 7
0
    def recv_raw(self, x=0xffff):
        """Receives a packet, then returns a tuple containing (cls, pkt_data, time)"""
        try:
            pkt = self.ins.recvfrom(x)[0]
        except BlockingIOError:  # noqa: F821
            warning('Captured no data, socket in non-blocking mode.')
            return None
        except socket.timeout:
            warning('Captured no data, socket read timed out.')
            return None
        except OSError:
            # something bad happened (e.g. the interface went down)
            warning("Captured no data.")
            return None

        ts = get_last_packet_timestamp(self.ins)
        return self.basecls, pkt, ts
Ejemplo n.º 8
0
    def recv_raw(self, x=0xffff):
        """Receives a packet, then returns a tuple containing (cls, pkt_data, time)"""  # noqa: E501
        try:
            pkt = self.ins.recvfrom(x)[0]
        except BlockingIOError:         # noqa: F821
            warning('Captured no data, socket in non-blocking mode.')
            return None
        except socket.timeout:
            warning('Captured no data, socket read timed out.')
            return None
        except OSError:
            # something bad happened (e.g. the interface went down)
            warning("Captured no data.")
            return None

        ts = get_last_packet_timestamp(self.ins)
        return self.basecls, pkt, ts
Ejemplo n.º 9
0
Archivo: can.py Proyecto: zbx91/sniffer
    def recv(self, x=CAN_FRAME_SIZE):
        # Fetching the Arb ID, DLC and Data
        try:
            pkt, sa_ll = self.ins.recvfrom(x)
        except BlockingIOError:
            warning('Captured no data, socket in non-blocking mode.')
            return None
        except socket.timeout:
            warning('Captured no data, socket read timed out.')
            return None
        except OSError:
            # something bad happened (e.g. the interface went down)
            warning("Captured no data.")
            return None

        q = CAN(pkt)
        q.time = get_last_packet_timestamp(self.ins)
        return q
Ejemplo n.º 10
0
    def recv_raw(self, x=CAN_MTU):
        # type: (int) -> Tuple[Optional[Type[Packet]], Optional[bytes], Optional[float]]  # noqa: E501
        """Returns a tuple containing (cls, pkt_data, time)"""
        pkt = None
        try:
            pkt = self.ins.recv(x)
        except BlockingIOError:  # noqa: F821
            warning("Captured no data, socket in non-blocking mode.")
        except socket.timeout:
            warning("Captured no data, socket read timed out.")
        except OSError:
            # something bad happened (e.g. the interface went down)
            warning("Captured no data.")

        # need to change the byte order of the first four bytes,
        # required by the underlying Linux SocketCAN frame format
        if not conf.contribs['CAN']['swap-bytes'] and pkt is not None:
            pkt = struct.pack("<I12s", *struct.unpack(">I12s", pkt))

        return self.basecls, pkt, get_last_packet_timestamp(self.ins)
Ejemplo n.º 11
0
    def recv_raw(self, x=0xffff):
        # type: (int) -> Tuple[Optional[Type[Packet]], Optional[bytes], Optional[float]]  # noqa: E501
        """
        Receives a packet, then returns a tuple containing
        (cls, pkt_data, time)
        """
        try:
            pkt, _, ts = self._recv_raw(self.ins, x)
        except BlockingIOError:  # noqa: F821
            warning('Captured no data, socket in non-blocking mode.')
            return None, None, None
        except socket.timeout:
            warning('Captured no data, socket read timed out.')
            return None, None, None
        except OSError:
            # something bad happened (e.g. the interface went down)
            warning("Captured no data.")
            self.close()
            return None, None, None

        if ts is None:
            ts = get_last_packet_timestamp(self.ins)
        return self.basecls, pkt, ts
Ejemplo n.º 12
0
    def recv(self, x=CAN_FRAME_SIZE):
        try:
            pkt, sa_ll = self.ins.recvfrom(x)
        except BlockingIOError:  # noqa: F821
            warning("Captured no data, socket in non-blocking mode.")
            return None
        except socket.timeout:
            warning("Captured no data, socket read timed out.")
            return None
        except OSError:
            # something bad happened (e.g. the interface went down)
            warning("Captured no data.")
            return None

        # need to change the byteoder of the first four bytes,
        # required by the underlying Linux SocketCAN frame format
        pkt = struct.pack("<I12s", *struct.unpack(">I12s", pkt))
        len = pkt[4]
        canpkt = CAN(pkt[:len + 8])
        canpkt.time = get_last_packet_timestamp(self.ins)
        if self.remove_padding:
            return canpkt
        else:
            return canpkt / Padding(pkt[len + 8:])