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