|
| 1 | +# This file is part of ssh-python. |
| 2 | +# Copyright (C) 2018 Panos Kittenis |
| 3 | +# |
| 4 | +# This library is free software; you can redistribute it and/or |
| 5 | +# modify it under the terms of the GNU Lesser General Public |
| 6 | +# License as published by the Free Software Foundation, version 2.1. |
| 7 | +# |
| 8 | +# This library is distributed in the hope that it will be useful, |
| 9 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | +# Lesser General Public License for more details. |
| 12 | +# |
| 13 | +# You should have received a copy of the GNU Lesser General Public |
| 14 | +# License along with this library; if not, write to the Free Software |
| 15 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-130 |
| 16 | + |
| 17 | +from cpython cimport PyObject_AsFileDescriptor |
| 18 | + |
| 19 | +from channel cimport Channel |
| 20 | +from session cimport Session |
| 21 | + |
| 22 | +cimport c_ssh |
| 23 | + |
| 24 | +from utils cimport handle_ssh_error_codes |
| 25 | + |
| 26 | + |
| 27 | +cdef class Flag: |
| 28 | + |
| 29 | + @staticmethod |
| 30 | + cdef Flag from_flag(c_ssh.ssh_connector_flags_e flag): |
| 31 | + cdef Flag _flag = Flag.__new__(Flag) |
| 32 | + _flag._flag = flag |
| 33 | + return _flag |
| 34 | + |
| 35 | + def __eq__(self, Flag other): |
| 36 | + return self._flag == other._flag |
| 37 | + |
| 38 | + def __str__(self): |
| 39 | + return str(self._flag) |
| 40 | + |
| 41 | + def __repr__(self): |
| 42 | + return self.__str__() |
| 43 | + |
| 44 | + |
| 45 | +CONNECTOR_STDOUT = Flag.from_flag( |
| 46 | + c_ssh.ssh_connector_flags_e.SSH_CONNECTOR_STDOUT) |
| 47 | +CONNECTOR_STDERR = Flag.from_flag( |
| 48 | + c_ssh.ssh_connector_flags_e.SSH_CONNECTOR_STDERR) |
| 49 | +CONNECTOR_BOTH = Flag.from_flag( |
| 50 | + c_ssh.ssh_connector_flags_e.SSH_CONNECTOR_BOTH) |
| 51 | + |
| 52 | + |
| 53 | +cdef class Connector: |
| 54 | + |
| 55 | + def __cinit__(self, Session session): |
| 56 | + self.session = session |
| 57 | + |
| 58 | + def __dealloc__(self): |
| 59 | + if self._connector is not NULL: |
| 60 | + c_ssh.ssh_connector_free(self._connector) |
| 61 | + self._connector = NULL |
| 62 | + |
| 63 | + @staticmethod |
| 64 | + cdef Connector from_ptr(c_ssh.ssh_connector _connector, Session session): |
| 65 | + cdef Connector connector = Connector.__new__(Connector, session) |
| 66 | + connector._connector = _connector |
| 67 | + return connector |
| 68 | + |
| 69 | + def set_in_channel(self, Channel channel, Flag flag): |
| 70 | + cdef int rc |
| 71 | + with nogil: |
| 72 | + rc = c_ssh.ssh_connector_set_in_channel( |
| 73 | + self._connector, channel._channel, flag._flag) |
| 74 | + return handle_ssh_error_codes(rc, self.session._session) |
| 75 | + |
| 76 | + def set_out_channel(self, Channel channel, Flag flag): |
| 77 | + cdef int rc |
| 78 | + with nogil: |
| 79 | + rc = c_ssh.ssh_connector_set_out_channel( |
| 80 | + self._connector, channel._channel, flag._flag) |
| 81 | + return handle_ssh_error_codes(rc, self.session._session) |
| 82 | + |
| 83 | + def set_in_fd(self, socket): |
| 84 | + cdef c_ssh.socket_t _sock = PyObject_AsFileDescriptor(socket) |
| 85 | + with nogil: |
| 86 | + c_ssh.ssh_connector_set_in_fd(self._connector, _sock) |
| 87 | + |
| 88 | + def set_out_fd(self, socket): |
| 89 | + cdef c_ssh.socket_t _sock = PyObject_AsFileDescriptor(socket) |
| 90 | + with nogil: |
| 91 | + c_ssh.ssh_connector_set_out_fd(self._connector, _sock) |
0 commit comments