2e610cb9f656fdbc73eb5fef7bcd99389ffce6b9
[tinc] / src / protocol_misc.c
1 /*
2     protocol_misc.c -- handle the meta-protocol, miscellaneous functions
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2009 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 {
38         cp();
39
40         if(!statusstring)
41                 statusstring = "Status";
42
43         return send_request(c, "%d %d %s", STATUS, statusno, statusstring);
44 }
45
46 bool status_h(connection_t *c)
47 {
48         int statusno;
49         char statusstring[MAX_STRING_SIZE];
50
51         cp();
52
53         if(sscanf(c->buffer, "%*d %d " MAX_STRING, &statusno, statusstring) != 2) {
54                 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "STATUS",
55                            c->name, c->hostname);
56                 return false;
57         }
58
59         ifdebug(STATUS) logger(LOG_NOTICE, _("Status message from %s (%s): %d: %s"),
60                            c->name, c->hostname, statusno, statusstring);
61
62         return true;
63 }
64
65 bool send_error(connection_t *c, int err, const char *errstring)
66 {
67         cp();
68
69         if(!errstring)
70                 errstring = "Error";
71
72         return send_request(c, "%d %d %s", ERROR, err, errstring);
73 }
74
75 bool error_h(connection_t *c)
76 {
77         int err;
78         char errorstring[MAX_STRING_SIZE];
79
80         cp();
81
82         if(sscanf(c->buffer, "%*d %d " MAX_STRING, &err, errorstring) != 2) {
83                 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ERROR",
84                            c->name, c->hostname);
85                 return false;
86         }
87
88         ifdebug(ERROR) logger(LOG_NOTICE, _("Error message from %s (%s): %d: %s"),
89                            c->name, c->hostname, err, errorstring);
90
91         terminate_connection(c, c->status.active);
92
93         return true;
94 }
95
96 bool send_termreq(connection_t *c)
97 {
98         cp();
99
100         return send_request(c, "%d", TERMREQ);
101 }
102
103 bool termreq_h(connection_t *c)
104 {
105         cp();
106
107         terminate_connection(c, c->status.active);
108
109         return true;
110 }
111
112 bool send_ping(connection_t *c)
113 {
114         cp();
115
116         c->status.pinged = true;
117         c->last_ping_time = now;
118
119         return send_request(c, "%d", PING);
120 }
121
122 bool ping_h(connection_t *c)
123 {
124         cp();
125
126         return send_pong(c);
127 }
128
129 bool send_pong(connection_t *c)
130 {
131         cp();
132
133         return send_request(c, "%d", PONG);
134 }
135
136 bool pong_h(connection_t *c)
137 {
138         cp();
139
140         c->status.pinged = false;
141
142         /* Succesful connection, reset timeout if this is an outgoing connection. */
143
144         if(c->outgoing)
145                 c->outgoing->timeout = 0;
146
147         return true;
148 }
149
150 /* Sending and receiving packets via TCP */
151
152 bool send_tcppacket(connection_t *c, vpn_packet_t *packet)
153 {
154         cp();
155
156         /* If there already is a lot of data in the outbuf buffer, discard this packet.
157            We use a very simple Random Early Drop algorithm. */
158
159         if(2.0 * c->outbuflen / (float)maxoutbufsize - 1 > (float)rand()/(float)RAND_MAX)
160                 return true;
161
162         if(!send_request(c, "%d %hd", PACKET, packet->len))
163                 return false;
164
165         return send_meta(c, (char *)packet->data, packet->len);
166 }
167
168 bool tcppacket_h(connection_t *c)
169 {
170         short int len;
171
172         cp();
173
174         if(sscanf(c->buffer, "%*d %hd", &len) != 1) {
175                 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "PACKET", c->name,
176                            c->hostname);
177                 return false;
178         }
179
180         /* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */
181
182         c->tcplen = len;
183
184         return true;
185 }