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>
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.
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.
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.
24 #include "connection.h"
32 int maxoutbufsize = 0;
34 /* Status and error notification routines */
36 bool send_status(connection_t *c, int statusno, const char *statusstring) {
38 statusstring = "Status";
40 return send_request(c, "%d %d %s", STATUS, statusno, statusstring);
43 bool status_h(connection_t *c) {
45 char statusstring[MAX_STRING_SIZE];
47 if(sscanf(c->buffer, "%*d %d " MAX_STRING, &statusno, statusstring) != 2) {
48 logger(LOG_ERR, "Got bad %s from %s (%s)", "STATUS",
49 c->name, c->hostname);
53 ifdebug(STATUS) logger(LOG_NOTICE, "Status message from %s (%s): %d: %s",
54 c->name, c->hostname, statusno, statusstring);
59 bool send_error(connection_t *c, int err, const char *errstring) {
63 return send_request(c, "%d %d %s", ERROR, err, errstring);
66 bool error_h(connection_t *c) {
68 char errorstring[MAX_STRING_SIZE];
70 if(sscanf(c->buffer, "%*d %d " MAX_STRING, &err, errorstring) != 2) {
71 logger(LOG_ERR, "Got bad %s from %s (%s)", "ERROR",
72 c->name, c->hostname);
76 ifdebug(ERROR) logger(LOG_NOTICE, "Error message from %s (%s): %d: %s",
77 c->name, c->hostname, err, errorstring);
79 terminate_connection(c, c->status.active);
84 bool send_termreq(connection_t *c) {
85 return send_request(c, "%d", TERMREQ);
88 bool termreq_h(connection_t *c) {
89 terminate_connection(c, c->status.active);
94 bool send_ping(connection_t *c) {
95 c->status.pinged = true;
96 c->last_ping_time = now;
98 return send_request(c, "%d", PING);
101 bool ping_h(connection_t *c) {
105 bool send_pong(connection_t *c) {
106 return send_request(c, "%d", PONG);
109 bool pong_h(connection_t *c) {
110 c->status.pinged = false;
112 /* Succesful connection, reset timeout if this is an outgoing connection. */
115 c->outgoing->timeout = 0;
116 c->outgoing->cfg = NULL;
118 freeaddrinfo(c->outgoing->ai);
119 c->outgoing->ai = NULL;
120 c->outgoing->aip = NULL;
126 /* Sending and receiving packets via TCP */
128 bool send_tcppacket(connection_t *c, const vpn_packet_t *packet) {
129 /* If there already is a lot of data in the outbuf buffer, discard this packet.
130 We use a very simple Random Early Drop algorithm. */
132 if(2.0 * c->outbuflen / (float)maxoutbufsize - 1 > (float)rand()/(float)RAND_MAX)
135 if(!send_request(c, "%d %hd", PACKET, packet->len))
138 return send_meta(c, (char *)packet->data, packet->len) && flush_meta(c);
141 bool tcppacket_h(connection_t *c) {
144 if(sscanf(c->buffer, "%*d %hd", &len) != 1) {
145 logger(LOG_ERR, "Got bad %s from %s (%s)", "PACKET", c->name,
150 /* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */