File: server-type.c

package info (click to toggle)
guile-ssh 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,068 kB
  • sloc: ansic: 4,956; lisp: 4,422; makefile: 337; sh: 262
file content (149 lines) | stat: -rw-r--r-- 3,743 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* server-type.c -- SSH server smob.
 *
 * Copyright (C) 2013, 2014, 2015, 2016 Artyom V. Poptsov <poptsov.artyom@gmail.com>
 *
 * This file is part of Guile-SSH
 *
 * Guile-SSH is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * Guile-SSH is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Guile-SSH.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <libguile.h>
#include <libssh/libssh.h>
#include <libssh/server.h>

#include "common.h"
#include "server-type.h"
#include "server-func.h"

static const char* GSSH_SERVER_TYPE_NAME = "server";


scm_t_bits server_tag;          /* Smob tag. */


/* GC callbacks */

static SCM
_mark (SCM server)
{
  gssh_server_t *sd = gssh_server_from_scm (server);
  return sd->options;
}

static size_t
_free (SCM server)
{
  gssh_server_t *sd = (gssh_server_t *) SCM_SMOB_DATA (server);
  ssh_bind_free (sd->bind);
  return 0;
}

static SCM
_equalp (SCM x1, SCM x2)
{
  return compare_objects(x1, x2, (converter_t) gssh_server_from_scm);
}

static int
_print (SCM server, SCM port, scm_print_state *pstate)
{
    gssh_server_t *sd = gssh_server_from_scm (server);
    SCM bindaddr = scm_assoc_ref (sd->options,
                                  gssh_symbol_to_scm (server_options,
                                                     SSH_BIND_OPTIONS_BINDADDR));
    SCM bindport = scm_assoc_ref (sd->options,
                                  gssh_symbol_to_scm (server_options,
                                                     SSH_BIND_OPTIONS_BINDPORT));
    scm_puts ("#<server", port);
    if (scm_is_true (bindaddr))
        {
            scm_putc (' ', port);
            scm_display (bindaddr, port);
        }

    if (scm_is_true (bindport))
        {
            if (scm_is_false (bindaddr))
                scm_putc (' ', port);

            scm_putc (':', port);
            scm_display (bindport, port);
        }

    scm_putc (' ', port);
    scm_display (_scm_object_hex_address (server), port);

    scm_putc ('>', port);

    return 1;
}

gssh_server_t*
make_gssh_server ()
{
  return (gssh_server_t *) scm_gc_malloc (sizeof (gssh_server_t),
                                          GSSH_SERVER_TYPE_NAME);
}


/* Smob specific procedures. */

SCM_DEFINE (guile_ssh_make_server, "%make-server", 0, 0, 0,
            (),
            "Make a new SSH server.")
{
  SCM smob;
  gssh_server_t *server_data = make_gssh_server ();
  server_data->bind = ssh_bind_new ();
  server_data->options = SCM_EOL;
  SCM_NEWSMOB (smob, server_tag, server_data);
  return smob;
}


/* Predicates. */

SCM_DEFINE (guile_ssh_is_server_p, "server?", 1, 0, 0,
            (SCM x),
            "Return #t if X is a SSH server, #f otherwise.")
{
  return scm_from_bool (SCM_SMOB_PREDICATE (server_tag, x));
}


/* Helper procedures. */

/* Convert X to a SSH server. */
gssh_server_t *
gssh_server_from_scm (SCM x)
{
  scm_assert_smob_type (server_tag, x);
  return (gssh_server_t *) SCM_SMOB_DATA (x);
}


/* Server smob initialization. */
void
init_server_type (void)
{
  server_tag = scm_make_smob_type (GSSH_SERVER_TYPE_NAME,
                                   sizeof (gssh_server_t));
  set_smob_callbacks (server_tag, _mark, _free, _equalp, _print);

#include "server-type.x"
}

/* server-type.c ends here */