Esempio n. 1
0
    def select(sockets, remain=None):
        # type: (List[SuperSocket], Optional[float]) -> List[SuperSocket]
        """This function is called during sendrecv() routine to wait for
        sockets to be ready to receive
        """
        obj_pipes = [x.impl.rx_queue for x in sockets if
                     isinstance(x, ISOTPSoftSocket) and not x.closed]

        ready_pipes = select_objects(obj_pipes, remain)

        return [x for x in sockets if isinstance(x, ISOTPSoftSocket) and
                not x.closed and x.impl.rx_queue in ready_pipes]
Esempio n. 2
0
    def close(self):
        # type: () -> None
        try:
            if select_objects([self.tx_queue], 0):
                warning("TX queue not empty")
                time.sleep(0.1)
        except OSError:
            pass

        try:
            if select_objects([self.rx_queue], 0):
                warning("RX queue not empty")
        except OSError:
            pass

        self.closed = True
        try:
            self.rx_handle.cancel()
        except Scapy_Exception:
            pass
        try:
            self.tx_handle.cancel()
        except Scapy_Exception:
            pass
Esempio n. 3
0
 def run(self):
     log_interactive.info("Pipe engine thread started.")
     try:
         for p in self.active_pipes:
             p.start()
         sources = self.active_sources
         sources.add(self)
         exhausted = set([])
         RUN = True
         STOP_IF_EXHAUSTED = False
         while RUN and (not STOP_IF_EXHAUSTED or len(sources) > 1):
             fds = select_objects(sources,
                                  2,
                                  customTypes=(AutoSource, PipeEngine))
             for fd in fds:
                 if fd is self:
                     cmd = self._read_cmd()
                     if cmd == "X":
                         RUN = False
                         break
                     elif cmd == "B":
                         STOP_IF_EXHAUSTED = True
                     elif cmd == "A":
                         sources = self.active_sources - exhausted
                         sources.add(self)
                     else:
                         warning(
                             "Unknown internal pipe engine command: %r. Ignoring."
                             % cmd)
                 elif fd in sources:
                     try:
                         fd.deliver()
                     except Exception as e:
                         log_interactive.exception(
                             "piping from %s failed: %s" % (fd.name, e))
                     else:
                         if fd.exhausted():
                             exhausted.add(fd)
                             sources.remove(fd)
     except KeyboardInterrupt:
         pass
     finally:
         try:
             for p in self.active_pipes:
                 p.stop()
         finally:
             self.thread_lock.release()
             log_interactive.info("Pipe engine thread stopped.")
Esempio n. 4
0
    def _send(self):
        # type: () -> None
        if self.tx_state == ISOTP_IDLE:
            if select_objects([self.tx_queue], 0):
                pkt = self.tx_queue.recv()
                if pkt:
                    self.begin_send(pkt)

        if not self.closed:
            self.tx_handle = TimeoutScheduler.schedule(self.rx_tx_poll_rate,
                                                       self._send)
        else:
            try:
                self.tx_handle.cancel()
            except Scapy_Exception:
                pass
Esempio n. 5
0
 def run(self):
     log_interactive.info("Pipe engine thread started.")
     try:
         for p in self.active_pipes:
             p.start()
         sources = self.active_sources
         sources.add(self)
         exhausted = set([])
         RUN = True
         STOP_IF_EXHAUSTED = False
         while RUN and (not STOP_IF_EXHAUSTED or len(sources) > 1):
             fds = select_objects(sources, 2)
             for fd in fds:
                 if fd is self:
                     cmd = self._read_cmd()
                     if cmd == "X":
                         RUN = False
                         break
                     elif cmd == "B":
                         STOP_IF_EXHAUSTED = True
                     elif cmd == "A":
                         sources = self.active_sources - exhausted
                         sources.add(self)
                     else:
                         warning("Unknown internal pipe engine command: %r. Ignoring." % cmd)  # noqa: E501
                 elif fd in sources:
                     try:
                         fd.deliver()
                     except Exception as e:
                         log_interactive.exception("piping from %s failed: %s" % (fd.name, e))  # noqa: E501
                     else:
                         if fd.exhausted():
                             exhausted.add(fd)
                             sources.remove(fd)
     except KeyboardInterrupt:
         pass
     finally:
         try:
             for p in self.active_pipes:
                 p.stop()
         finally:
             self.thread_lock.release()
             log_interactive.info("Pipe engine thread stopped.")
Esempio n. 6
0
 def select(sockets, remain=None):
     return select_objects(sockets, remain)
Esempio n. 7
0
 def select(sockets, remain=conf.recv_poll_rate):
     # type: (List[SuperSocket], Optional[float]) -> List[SuperSocket]
     sock = [
         s for s in sockets if isinstance(s, ObjectPipe) and not s._closed
     ]
     return cast(List[SuperSocket], select_objects(sock, remain))