2 connection.c -- connection list management
3 Copyright (C) 2000-2013 Guus Sliepen <guus@tinc-vpn.org>,
4 2000-2005 Ivo Timmermans
5 2008 Max Rijevski <maksuf@gmail.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "control_common.h"
34 list_t connection_list = {
38 .delete = (list_action_t) free_connection,
41 connection_t *everyone;
43 void init_connections(void) {
44 everyone = new_connection();
45 everyone->name = xstrdup("everyone");
46 everyone->hostname = xstrdup("BROADCAST");
49 void exit_connections(void) {
50 list_empty_list(&connection_list);
52 free_connection(everyone);
56 connection_t *new_connection(void) {
57 return xzalloc(sizeof(connection_t));
60 #ifndef DISABLE_LEGACY
61 bool init_crypto_by_nid(legacy_crypto_t *c, nid_t cipher, nid_t digest) {
62 if(!cipher_open_by_nid(&c->cipher, cipher)) {
66 if(!digest_open_by_nid(&c->digest, digest, DIGEST_ALGO_SIZE)) {
67 cipher_close(&c->cipher);
71 c->budget = cipher_budget(&c->cipher);
75 bool init_crypto_by_name(legacy_crypto_t *c, const char *cipher, const char *digest) {
76 if(!cipher_open_by_name(&c->cipher, cipher)) {
80 if(!digest_open_by_name(&c->digest, digest, DIGEST_ALGO_SIZE)) {
81 cipher_close(&c->cipher);
85 c->budget = cipher_budget(&c->cipher);
89 bool decrease_budget(legacy_crypto_t *c, size_t bytes) {
90 if(bytes > c->budget) {
98 static void close_legacy_crypto(legacy_crypto_t *c) {
99 cipher_close(&c->cipher);
100 digest_close(&c->digest);
103 legacy_ctx_t *new_legacy_ctx(rsa_t *rsa) {
104 legacy_ctx_t *ctx = xzalloc(sizeof(legacy_ctx_t));
109 void free_legacy_ctx(legacy_ctx_t *ctx) {
111 close_legacy_crypto(&ctx->in);
112 close_legacy_crypto(&ctx->out);
119 void free_connection(connection_t *c) {
124 #ifndef DISABLE_LEGACY
125 free_legacy_ctx(c->legacy);
128 sptps_stop(&c->sptps);
129 ecdsa_free(c->ecdsa);
131 free(c->hischallenge);
132 free(c->mychallenge);
134 buffer_clear(&c->inbuf);
135 buffer_clear(&c->outbuf);
140 if(c->status.tarpit) {
143 closesocket(c->socket);
151 exit_configuration(c->config_tree);
152 c->config_tree = NULL;
158 void connection_add(connection_t *c) {
159 list_insert_tail(&connection_list, c);
162 void connection_del(connection_t *c) {
163 list_delete(&connection_list, c);
166 bool dump_connections(connection_t *cdump) {
167 for list_each(connection_t, c, &connection_list) {
168 send_request(cdump, "%d %d %s %s %x %d %x",
169 CONTROL, REQ_DUMP_CONNECTIONS,
170 c->name, c->hostname, c->options, c->socket,
174 return send_request(cdump, "%d %d", CONTROL, REQ_DUMP_CONNECTIONS);