channel.scm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. ;;; channel.scm -- API for SSH channel manipulation.
  2. ;; Copyright (C) 2013, 2014, 2015, 2016 Artyom V. Poptsov <poptsov.artyom@gmail.com>
  3. ;;
  4. ;; This file is a part of Guile-SSH.
  5. ;;
  6. ;; Guile-SSH is free software: you can redistribute it and/or
  7. ;; modify it under the terms of the GNU General Public License as
  8. ;; published by the Free Software Foundation, either version 3 of the
  9. ;; License, or (at your option) any later version.
  10. ;;
  11. ;; Guile-SSH is distributed in the hope that it will be useful, but
  12. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;; General Public License for more details.
  15. ;;
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with Guile-SSH. If not, see
  18. ;; <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; This module contains API that is used for working with SSH
  21. ;; channels.
  22. ;;
  23. ;; These procedures are exported:
  24. ;;
  25. ;; channel?
  26. ;; make-channel
  27. ;; channel-open-session
  28. ;; channel-request-env
  29. ;; channel-request-exec
  30. ;; channel-request-pty
  31. ;; channel-request-shell
  32. ;; channel-open-forward
  33. ;; channel-cancel-forward
  34. ;; channel-set-pty-size!
  35. ;; channel-set-stream!
  36. ;; channel-get-stream
  37. ;; channel-open?
  38. ;; channel-send-eof
  39. ;; channel-eof?
  40. ;;; Code:
  41. (define-module (ssh channel)
  42. #:use-module (ssh log)
  43. #:use-module (ssh session)
  44. #:export (channel
  45. channel?
  46. make-channel
  47. channel-open-session
  48. channel-request-env
  49. channel-request-exec
  50. channel-request-pty
  51. channel-request-shell
  52. channel-open-forward
  53. channel-listen-forward
  54. channel-accept-forward
  55. channel-cancel-forward
  56. channel-request-send-exit-status
  57. channel-set-pty-size!
  58. channel-set-stream!
  59. channel-get-stream
  60. channel-get-session
  61. channel-get-exit-status
  62. channel-open?
  63. channel-send-eof
  64. channel-eof?))
  65. (define* (make-channel session #:optional (mode OPEN_BOTH))
  66. (cond
  67. ((string-contains mode OPEN_BOTH)
  68. (%make-channel session (logior RDNG WRTNG)))
  69. ((string-contains mode OPEN_READ)
  70. (%make-channel session RDNG))
  71. ((string-contains mode OPEN_WRITE)
  72. (%make-channel session WRTNG))
  73. (else
  74. (throw 'guile-ssh-error "Wrong mode" mode))))
  75. (define* (channel-open-forward channel
  76. #:key (source-host "localhost") local-port
  77. remote-host (remote-port local-port))
  78. "Open a TCP/IP forwarding channel. Connect to a REMOTE-HOST and REMOTE-PORT,
  79. and use SOURCE-HOST and LOCAL-PORT as origination of connections.
  80. If the SOURCE-HOST is not set, then \"localhost\" is used. If REMOTE-PORT is
  81. not set, then it will be set to LOCAL-PORT value.
  82. Please note that the procedure does not bind the LOCAL-PORT and does not
  83. automatically forward the content of a socket to the channel."
  84. (%channel-open-forward channel
  85. remote-host remote-port
  86. source-host local-port))
  87. (define* (channel-listen-forward session #:key (address #f) (port 0))
  88. "Send the \"tcpip-forward\" global request using SESSION to ask the server
  89. to begin listening for inbound connections on the specified ADDRESS and PORT.
  90. If ADDRESS is not specified (or set to #f) then the server binds all addresses
  91. on all protocol families supported by the server. When 0 is passed as a PORT
  92. then server allocates the next unprivileged port.
  93. The procedure returns two values: the first value is the result of the
  94. operation (either 'ok', 'again' or 'error'), and the second value is the bound
  95. port number (if PORT was set to 0)."
  96. (%channel-listen-forward session address port))
  97. (define* (channel-accept-forward session #:optional (timeout 0))
  98. "Accept an incoming TCP/IP forwarding channel and get information about
  99. incoming connection. Return two values: the first value is the incoming
  100. channel, and the second value is a port number on which the connection was
  101. issued."
  102. (%channel-accept-forward session timeout))
  103. (define (channel-send-eof channel)
  104. "Send an end of file (EOF) on the CHANNEL. This action doesn't close the
  105. channel; you may still read from it but not write. Throw 'guile-ssh-error' on
  106. an error. Return value is undefined."
  107. (%channel-send-eof channel))
  108. ;;;
  109. (load-extension "libguile-ssh" "init_channel")
  110. ;;; channel.scm ends here.