Remove unnecessary status bitfield conversions.
[tinc] / src / connection.c
1 /*
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>
6
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.
11
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.
16
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.
20 */
21
22 #include "system.h"
23
24 #include "list.h"
25 #include "cipher.h"
26 #include "conf.h"
27 #include "control_common.h"
28 #include "logger.h"
29 #include "net.h"
30 #include "rsa.h"
31 #include "utils.h"
32 #include "xalloc.h"
33
34 list_t connection_list = {
35         .head = NULL,
36         .tail = NULL,
37         .count = 0,
38         .delete = (list_action_t) free_connection,
39 };
40
41 connection_t *everyone;
42
43 void init_connections(void) {
44         everyone = new_connection();
45         everyone->name = xstrdup("everyone");
46         everyone->hostname = xstrdup("BROADCAST");
47 }
48
49 void exit_connections(void) {
50         list_empty_list(&connection_list);
51
52         free_connection(everyone);
53         everyone = NULL;
54 }
55
56 connection_t *new_connection(void) {
57         return xzalloc(sizeof(connection_t));
58 }
59
60 void free_connection(connection_t *c) {
61         if(!c) {
62                 return;
63         }
64
65 #ifndef DISABLE_LEGACY
66         cipher_close(&c->incipher);
67         digest_close(&c->indigest);
68         cipher_close(&c->outcipher);
69         digest_close(&c->outdigest);
70         rsa_free(c->rsa);
71 #endif
72
73         sptps_stop(&c->sptps);
74         ecdsa_free(c->ecdsa);
75
76         free(c->hischallenge);
77         free(c->mychallenge);
78
79         buffer_clear(&c->inbuf);
80         buffer_clear(&c->outbuf);
81
82         io_del(&c->io);
83
84         if(c->socket > 0) {
85                 if(c->status.tarpit) {
86                         tarpit(c->socket);
87                 } else {
88                         closesocket(c->socket);
89                 }
90         }
91
92         free(c->name);
93         free(c->hostname);
94
95         if(c->config_tree) {
96                 exit_configuration(&c->config_tree);
97         }
98
99         free(c);
100 }
101
102 void connection_add(connection_t *c) {
103         list_insert_tail(&connection_list, c);
104 }
105
106 void connection_del(connection_t *c) {
107         list_delete(&connection_list, c);
108 }
109
110 bool dump_connections(connection_t *cdump) {
111         for list_each(connection_t, c, &connection_list) {
112                 send_request(cdump, "%d %d %s %s %x %d %x",
113                              CONTROL, REQ_DUMP_CONNECTIONS,
114                              c->name, c->hostname, c->options, c->socket,
115                              c->status.value);
116         }
117
118         return send_request(cdump, "%d %d", CONTROL, REQ_DUMP_CONNECTIONS);
119 }