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 bool send_ping(connection_t *c) {
35 c->status.pinged = true;
36 c->last_ping_time = now;
38 return send_request(c, "%d", PING);
41 bool ping_h(connection_t *c) {
45 bool send_pong(connection_t *c) {
46 return send_request(c, "%d", PONG);
49 bool pong_h(connection_t *c) {
50 c->status.pinged = false;
52 /* Succesful connection, reset timeout if this is an outgoing connection. */
55 c->outgoing->timeout = 0;
56 c->outgoing->cfg = NULL;
59 freeaddrinfo(c->outgoing->ai);
62 c->outgoing->ai = NULL;
63 c->outgoing->aip = NULL;
69 /* Sending and receiving packets via TCP */
71 bool send_tcppacket(connection_t *c, const vpn_packet_t *packet) {
72 /* If there already is a lot of data in the outbuf buffer, discard this packet.
73 We use a very simple Random Early Drop algorithm. */
75 if(2.0 * c->outbuflen / (float)maxoutbufsize - 1 > (float)rand() / (float)RAND_MAX) {
79 if(!send_request(c, "%d %hd", PACKET, packet->len)) {
83 return send_meta(c, (char *)packet->data, packet->len) && flush_meta(c);
86 bool tcppacket_h(connection_t *c) {
89 if(sscanf(c->buffer, "%*d %hu", &len) != 1) {
90 logger(LOG_ERR, "Got bad %s from %s (%s)", "PACKET", c->name,
95 /* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */