Don't call ERR_remove_state().
[oweals/tinc.git] / src / protocol_misc.c
1 /*
2     protocol_misc.c -- handle the meta-protocol, miscellaneous functions
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2012 Guus Sliepen <guus@tinc-vpn.org>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include "conf.h"
24 #include "connection.h"
25 #include "logger.h"
26 #include "meta.h"
27 #include "net.h"
28 #include "netutl.h"
29 #include "protocol.h"
30 #include "utils.h"
31
32 int maxoutbufsize = 0;
33
34 /* Status and error notification routines */
35
36 bool send_status(connection_t *c, int statusno, const char *statusstring) {
37         if(!statusstring) {
38                 statusstring = "Status";
39         }
40
41         return send_request(c, "%d %d %s", STATUS, statusno, statusstring);
42 }
43
44 bool status_h(connection_t *c) {
45         int statusno;
46         char statusstring[MAX_STRING_SIZE];
47
48         if(sscanf(c->buffer, "%*d %d " MAX_STRING, &statusno, statusstring) != 2) {
49                 logger(LOG_ERR, "Got bad %s from %s (%s)", "STATUS",
50                        c->name, c->hostname);
51                 return false;
52         }
53
54         ifdebug(STATUS) logger(LOG_NOTICE, "Status message from %s (%s): %d: %s",
55                                c->name, c->hostname, statusno, statusstring);
56
57         return true;
58 }
59
60 bool send_error(connection_t *c, int err, const char *errstring) {
61         if(!errstring) {
62                 errstring = "Error";
63         }
64
65         return send_request(c, "%d %d %s", ERROR, err, errstring);
66 }
67
68 bool error_h(connection_t *c) {
69         int err;
70         char errorstring[MAX_STRING_SIZE];
71
72         if(sscanf(c->buffer, "%*d %d " MAX_STRING, &err, errorstring) != 2) {
73                 logger(LOG_ERR, "Got bad %s from %s (%s)", "ERROR",
74                        c->name, c->hostname);
75                 return false;
76         }
77
78         ifdebug(ERROR) logger(LOG_NOTICE, "Error message from %s (%s): %d: %s",
79                               c->name, c->hostname, err, errorstring);
80
81         terminate_connection(c, c->status.active);
82
83         return true;
84 }
85
86 bool send_termreq(connection_t *c) {
87         return send_request(c, "%d", TERMREQ);
88 }
89
90 bool termreq_h(connection_t *c) {
91         terminate_connection(c, c->status.active);
92
93         return true;
94 }
95
96 bool send_ping(connection_t *c) {
97         c->status.pinged = true;
98         c->last_ping_time = now;
99
100         return send_request(c, "%d", PING);
101 }
102
103 bool ping_h(connection_t *c) {
104         return send_pong(c);
105 }
106
107 bool send_pong(connection_t *c) {
108         return send_request(c, "%d", PONG);
109 }
110
111 bool pong_h(connection_t *c) {
112         c->status.pinged = false;
113
114         /* Succesful connection, reset timeout if this is an outgoing connection. */
115
116         if(c->outgoing) {
117                 c->outgoing->timeout = 0;
118                 c->outgoing->cfg = NULL;
119
120                 if(c->outgoing->ai) {
121                         freeaddrinfo(c->outgoing->ai);
122                 }
123
124                 c->outgoing->ai = NULL;
125                 c->outgoing->aip = NULL;
126         }
127
128         return true;
129 }
130
131 /* Sending and receiving packets via TCP */
132
133 bool send_tcppacket(connection_t *c, const vpn_packet_t *packet) {
134         /* If there already is a lot of data in the outbuf buffer, discard this packet.
135            We use a very simple Random Early Drop algorithm. */
136
137         if(2.0 * c->outbuflen / (float)maxoutbufsize - 1 > (float)rand() / (float)RAND_MAX) {
138                 return true;
139         }
140
141         if(!send_request(c, "%d %hd", PACKET, packet->len)) {
142                 return false;
143         }
144
145         return send_meta(c, (char *)packet->data, packet->len) && flush_meta(c);
146 }
147
148 bool tcppacket_h(connection_t *c) {
149         short int len;
150
151         if(sscanf(c->buffer, "%*d %hd", &len) != 1) {
152                 logger(LOG_ERR, "Got bad %s from %s (%s)", "PACKET", c->name,
153                        c->hostname);
154                 return false;
155         }
156
157         /* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */
158
159         c->tcplen = len;
160
161         return true;
162 }