Fix use-after-free in final log message on tincd exit.
[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 "list.h"
29 #include "logger.h"
30 #include "net.h"
31 #include "rsa.h"
32 #include "subnet.h"
33 #include "utils.h"
34 #include "xalloc.h"
35
36 list_t *connection_list;
37 connection_t *everyone;
38
39 void init_connections(void) {
40         connection_list = list_alloc((list_action_t) free_connection);
41         everyone = new_connection();
42         everyone->name = xstrdup("everyone");
43         everyone->hostname = xstrdup("BROADCAST");
44 }
45
46 void exit_connections(void) {
47         list_delete_list(connection_list);
48         connection_list = NULL;
49
50         free_connection(everyone);
51         everyone = NULL;
52 }
53
54 connection_t *new_connection(void) {
55         return xzalloc(sizeof(connection_t));
56 }
57
58 void free_connection(connection_t *c) {
59         if(!c) {
60                 return;
61         }
62
63 #ifndef DISABLE_LEGACY
64         cipher_close(c->incipher);
65         digest_close(c->indigest);
66         cipher_close(c->outcipher);
67         digest_close(c->outdigest);
68         rsa_free(c->rsa);
69 #endif
70
71         sptps_stop(&c->sptps);
72         ecdsa_free(c->ecdsa);
73
74         free(c->hischallenge);
75         free(c->mychallenge);
76
77         buffer_clear(&c->inbuf);
78         buffer_clear(&c->outbuf);
79
80         io_del(&c->io);
81
82         if(c->socket > 0) {
83                 if(c->status.tarpit) {
84                         tarpit(c->socket);
85                 } else {
86                         closesocket(c->socket);
87                 }
88         }
89
90         free(c->name);
91         free(c->hostname);
92
93         if(c->config_tree) {
94                 exit_configuration(&c->config_tree);
95         }
96
97         free(c);
98 }
99
100 void connection_add(connection_t *c) {
101         list_insert_tail(connection_list, c);
102 }
103
104 void connection_del(connection_t *c) {
105         list_delete(connection_list, c);
106 }
107
108 bool dump_connections(connection_t *cdump) {
109         for list_each(connection_t, c, connection_list) {
110                 send_request(cdump, "%d %d %s %s %x %d %x",
111                              CONTROL, REQ_DUMP_CONNECTIONS,
112                              c->name, c->hostname, c->options, c->socket,
113                              bitfield_to_int(&c->status, sizeof(c->status)));
114         }
115
116         return send_request(cdump, "%d %d", CONTROL, REQ_DUMP_CONNECTIONS);
117 }