]> git.meshlink.io Git - meshlink/blob - src/protocol_misc.c
Merge branch 'master' into 1.1
[meshlink] / src / protocol_misc.c
1 /*
2     protocol_misc.c -- handle the meta-protocol, miscellaneous functions
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2006 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
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id$
21 */
22
23 #include "system.h"
24
25 #include "conf.h"
26 #include "connection.h"
27 #include "logger.h"
28 #include "meta.h"
29 #include "net.h"
30 #include "netutl.h"
31 #include "protocol.h"
32 #include "utils.h"
33
34 int maxoutbufsize = 0;
35
36 /* Status and error notification routines */
37
38 bool send_status(connection_t *c, int statusno, const char *statusstring)
39 {
40         cp();
41
42         if(!statusstring)
43                 statusstring = "Status";
44
45         return send_request(c, "%d %d %s", STATUS, statusno, statusstring);
46 }
47
48 bool status_h(connection_t *c, char *request)
49 {
50         int statusno;
51         char statusstring[MAX_STRING_SIZE];
52
53         cp();
54
55         if(sscanf(request, "%*d %d " MAX_STRING, &statusno, statusstring) != 2) {
56                 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "STATUS",
57                            c->name, c->hostname);
58                 return false;
59         }
60
61         ifdebug(STATUS) logger(LOG_NOTICE, _("Status message from %s (%s): %d: %s"),
62                            c->name, c->hostname, statusno, statusstring);
63
64         return true;
65 }
66
67 bool send_error(connection_t *c, int err, const char *errstring)
68 {
69         cp();
70
71         if(!errstring)
72                 errstring = "Error";
73
74         return send_request(c, "%d %d %s", ERROR, err, errstring);
75 }
76
77 bool error_h(connection_t *c, char *request)
78 {
79         int err;
80         char errorstring[MAX_STRING_SIZE];
81
82         cp();
83
84         if(sscanf(request, "%*d %d " MAX_STRING, &err, errorstring) != 2) {
85                 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ERROR",
86                            c->name, c->hostname);
87                 return false;
88         }
89
90         ifdebug(ERROR) logger(LOG_NOTICE, _("Error message from %s (%s): %d: %s"),
91                            c->name, c->hostname, err, errorstring);
92
93         return false;
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, char *request)
104 {
105         cp();
106
107         return false;
108 }
109
110 bool send_ping(connection_t *c)
111 {
112         cp();
113
114         c->status.pinged = true;
115         c->last_ping_time = time(NULL);
116
117         return send_request(c, "%d", PING);
118 }
119
120 bool ping_h(connection_t *c, char *request)
121 {
122         cp();
123
124         return send_pong(c);
125 }
126
127 bool send_pong(connection_t *c)
128 {
129         cp();
130
131         return send_request(c, "%d", PONG);
132 }
133
134 bool pong_h(connection_t *c, char *request)
135 {
136         cp();
137
138         c->status.pinged = false;
139
140         /* Succesful connection, reset timeout if this is an outgoing connection. */
141
142         if(c->outgoing)
143                 c->outgoing->timeout = 0;
144
145         return true;
146 }
147
148 /* Sending and receiving packets via TCP */
149
150 bool send_tcppacket(connection_t *c, vpn_packet_t *packet)
151 {
152         cp();
153
154         /* If there already is a lot of data in the outbuf buffer, discard this packet.
155            We use a very simple Random Early Drop algorithm. */
156
157         if(2.0 * c->buffer->output->off / (float)maxoutbufsize - 1 > (float)rand()/(float)RAND_MAX)
158                 return true;
159
160         if(!send_request(c, "%d %hd", PACKET, packet->len))
161                 return false;
162
163         return send_meta(c, (char *)packet->data, packet->len);
164 }
165
166 bool tcppacket_h(connection_t *c, char *request)
167 {
168         short int len;
169
170         cp();
171
172         if(sscanf(request, "%*d %hd", &len) != 1) {
173                 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "PACKET", c->name,
174                            c->hostname);
175                 return false;
176         }
177
178         /* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */
179
180         c->tcplen = len;
181
182         return true;
183 }