session-type.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* session-type.c -- SSH session smob.
  2. *
  3. * Copyright (C) 2013, 2014, 2015, 2016 Artyom V. Poptsov <poptsov.artyom@gmail.com>
  4. *
  5. * This file is part of Guile-SSH.
  6. *
  7. * Guile-SSH is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * Guile-SSH is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Guile-SSH. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <libguile.h>
  22. #include <libssh/libssh.h>
  23. #include <string.h>
  24. #include "session-type.h"
  25. #include "channel-type.h"
  26. #include "session-func.h"
  27. #include "error.h"
  28. #include "common.h"
  29. scm_t_bits session_tag; /* Smob tag. */
  30. SCM
  31. mark_session (SCM session_smob)
  32. {
  33. struct session_data *sd = _scm_to_session_data (session_smob);
  34. return sd->callbacks;
  35. }
  36. /* Handle GC'ing of the session smob. */
  37. size_t
  38. free_session (SCM session)
  39. {
  40. struct session_data *sd = (struct session_data *) SCM_SMOB_DATA (session);
  41. ssh_disconnect (sd->ssh_session);
  42. ssh_free (sd->ssh_session);
  43. SCM_SET_SMOB_DATA (session, NULL);
  44. return 0;
  45. }
  46. /* Create a new session. */
  47. SCM_DEFINE (guile_ssh_make_session, "%make-session", 0, 0, 0,
  48. (),
  49. "\
  50. Create a new session.\
  51. ")
  52. {
  53. SCM smob;
  54. struct session_data *session_data
  55. = (struct session_data *) scm_gc_malloc (sizeof (struct session_data),
  56. "session");
  57. session_data->ssh_session = ssh_new ();
  58. if (session_data->ssh_session == NULL)
  59. return SCM_BOOL_F;
  60. session_data->callbacks = SCM_BOOL_F;
  61. SCM_NEWSMOB (smob, session_tag, session_data);
  62. return smob;
  63. }
  64. static int
  65. print_session (SCM session, SCM port, scm_print_state *pstate)
  66. {
  67. struct session_data *sd = _scm_to_session_data (session);
  68. char *user = NULL;
  69. char *host = NULL;
  70. unsigned int ssh_port;
  71. int res;
  72. scm_puts ("#<session ", port);
  73. res = ssh_options_get (sd->ssh_session, SSH_OPTIONS_USER, &user);
  74. scm_display ((res == SSH_OK) ? scm_from_locale_string (user) : SCM_UNDEFINED,
  75. port);
  76. ssh_string_free_char (user);
  77. scm_putc ('@', port);
  78. res = ssh_options_get (sd->ssh_session, SSH_OPTIONS_HOST, &host);
  79. scm_display ((res == SSH_OK) ? scm_from_locale_string (host) : SCM_UNDEFINED,
  80. port);
  81. ssh_string_free_char (host);
  82. scm_putc (':', port);
  83. res = ssh_options_get_port (sd->ssh_session, &ssh_port);
  84. scm_display ((res == SSH_OK) ? scm_from_int (ssh_port) : SCM_UNDEFINED, port);
  85. scm_puts (ssh_is_connected (sd->ssh_session) ? " (connected) "
  86. : " (disconnected) ",
  87. port);
  88. scm_display (_scm_object_hex_address (session), port);
  89. scm_putc ('>', port);
  90. return 1;
  91. }
  92. /* Predicates */
  93. SCM_DEFINE (guile_ssh_is_session_p, "session?", 1, 0, 0,
  94. (SCM x),
  95. "\
  96. Return #t if X is a SSH session, #f otherwise.\
  97. ")
  98. {
  99. return scm_from_bool (SCM_SMOB_PREDICATE (session_tag, x));
  100. }
  101. SCM
  102. equalp_session (SCM x1, SCM x2)
  103. {
  104. struct session_data *session1 = _scm_to_session_data (x1);
  105. struct session_data *session2 = _scm_to_session_data (x2);
  106. if ((! session1) || (! session2))
  107. return SCM_BOOL_F;
  108. else if (session1 != session2)
  109. return SCM_BOOL_F;
  110. else
  111. return SCM_BOOL_T;
  112. }
  113. /* Helper procedures */
  114. /* Convert SCM object to a SSH session */
  115. struct session_data*
  116. _scm_to_session_data (SCM x)
  117. {
  118. scm_assert_smob_type (session_tag, x);
  119. return (struct session_data *) SCM_SMOB_DATA (x);
  120. }
  121. /* session smob initialization. */
  122. void
  123. init_session_type (void)
  124. {
  125. session_tag = scm_make_smob_type ("session",
  126. sizeof (struct session_data));
  127. scm_set_smob_mark (session_tag, mark_session);
  128. scm_set_smob_free (session_tag, free_session);
  129. scm_set_smob_print (session_tag, print_session);
  130. scm_set_smob_equalp (session_tag, equalp_session);
  131. #include "session-type.x"
  132. }
  133. /* session.c ends here */