aboutsummaryrefslogtreecommitdiff
path: root/src/init.c
blob: a8dfe0306082a6b61ae21669b7ee52575bbc7595 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * init.c - initialization and finalization of the library
 *
 * This file is part of the SSH Library
 *
 * Copyright (c) 2003-2009 by Aris Adamantiadis
 *
 * The SSH Library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 *
 * The SSH Library 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 Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with the SSH Library; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 */

#include "config.h"
#include "libssh/priv.h"
#include "libssh/socket.h"
#include "libssh/dh.h"
#include "libssh/poll.h"
#include "libssh/threads.h"

#ifdef _WIN32
#include <winsock2.h>
#endif

#ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
#define CONSTRUCTOR_ATTRIBUTE __attribute__((constructor))
#else
#define CONSTRUCTOR_ATTRIBUTE
#endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */

#ifdef HAVE_DESTRUCTOR_ATTRIBUTE
#define DESTRUCTOR_ATTRIBUTE __attribute__((destructor))
#else
#define DESTRUCTOR_ATTRIBUTE
#endif /* HAVE_DESTRUCTOR_ATTRIBUTE */

/* Declare static mutex */
static SSH_MUTEX ssh_init_mutex = SSH_MUTEX_STATIC_INIT;

/* Counter for initializations */
static int _ssh_initialized = 0;

/* Cache the returned value */
static int _ssh_init_ret = 0;

void libssh_constructor(void) CONSTRUCTOR_ATTRIBUTE;
void libssh_destructor(void) DESTRUCTOR_ATTRIBUTE;

static int _ssh_init(unsigned constructor) {

    int rc = 0;

    if (!constructor) {
        ssh_mutex_lock(&ssh_init_mutex);
    }

    _ssh_initialized++;

    if (_ssh_initialized > 1) {
        rc = _ssh_init_ret;
        goto _ret;
    }

    rc = ssh_threads_init();
    if (rc) {
        goto _ret;
    }

    rc = ssh_crypto_init();
    if (rc) {
        goto _ret;
    }

    rc = ssh_dh_init();
    if (rc) {
        goto _ret;
    }

    rc = ssh_socket_init();
    if (rc) {
        goto _ret;
    }

_ret:
    _ssh_init_ret = rc;

    if (!constructor) {
        ssh_mutex_unlock(&ssh_init_mutex);
    }

    return rc;
}

/**
 * @brief Initialize global cryptographic data structures.
 *
 * This functions is automatically called when the library is loaded.
 *
 * @returns             0 on success, -1 if an error occured.
 */
void libssh_constructor(void)
{

    int rc;

    rc = _ssh_init(1);

    if (rc < 0) {
        fprintf(stderr, "Error in auto_init()\n");
    }

    return;
}

/**
 * @defgroup libssh The libssh API
 *
 * The libssh library is implementing the SSH protocols and some of its
 * extensions. This group of functions is mostly used to implement an SSH
 * client.
 * Some function are needed to implement an SSH server too.
 *
 * @{
 */

/**
 * @brief Initialize global cryptographic data structures.
 *
 * Since version 0.8.0, it is not necessary to call this function on systems
 * which are fully supported with regards to threading (that is, system with
 * pthreads available).
 *
 * If the library is already initialized, increments the _ssh_initialized
 * counter and return the error code cached in _ssh_init_ret.
 *
 * @returns             SSH_OK on success, SSH_ERROR if an error occurred.
 */
int ssh_init(void) {
    return _ssh_init(0);
}

static int _ssh_finalize(unsigned destructor) {

    if (!destructor) {
        ssh_mutex_lock(&ssh_init_mutex);

        if (_ssh_initialized > 1) {
            _ssh_initialized--;
            goto _ret;
        }

        if (_ssh_initialized == 1) {
            if (_ssh_init_ret < 0) {
                goto _ret;
            }
        }
    }

    /* If the counter reaches zero or it is the destructor calling, finalize */
    ssh_dh_finalize();
    ssh_crypto_finalize();
    ssh_socket_cleanup();
    /* It is important to finalize threading after CRYPTO because
     * it still depends on it */
    ssh_threads_finalize();

    _ssh_initialized = 0;

_ret:
    if (!destructor) {
        ssh_mutex_unlock(&ssh_init_mutex);
    }
    return 0;
}

/**
 * @brief Finalize and cleanup all libssh and cryptographic data structures.
 *
 * This function is automatically called when the library is unloaded.
 *
 * @returns             SSH_OK on success, SSH_ERROR if an error occurred.
 *
 */
void libssh_destructor(void)
{
    int rc;

    rc = _ssh_finalize(1);

    if (rc < 0) {
        fprintf(stderr, "Error in libssh_destructor()\n");
    }
}

/**
 * @brief Finalize and cleanup all libssh and cryptographic data structures.
 *
 * Since version 0.8.0, it is not necessary to call this function, since it is
 * automatically called when the library is unloaded.
 *
 * If ssh_init() is called explicitly, then ssh_finalize() must be called
 * explicitly.
 *
 * When called, decrements the counter _ssh_initialized. If the counter reaches
 * zero, then the libssh and cryptographic data structures are cleaned up.
 *
 * @returns             0 on succes, -1 if an error occured.
 *
 @returns 0 otherwise
 */
int ssh_finalize(void) {
    return _ssh_finalize(0);
}

#ifdef _WIN32

#if defined(_MSC_VER) && !defined(LIBSSH_STATIC)
/* Library constructor and destructor */
BOOL WINAPI DllMain(HINSTANCE hinstDLL,
                    DWORD fdwReason,
                    LPVOID lpvReserved)
{
    int rc = 0;

    switch(fdwReason) {
    case DLL_PROCESS_ATTACH:
        rc = _ssh_init(1);
        if (rc != 0) {
            fprintf(stderr, "DllMain: ssh_init failed!");
            return FALSE;
        }
        break;
    case DLL_PROCESS_DETACH:
        _ssh_finalize(1);
        break;
    default:
        break;
    }

    return TRUE;
}
#endif /* _MSC_VER && !LIBSSH_STATIC */

#endif /* _WIN32 */

/** @} */