2 protocol_misc.c -- handle the meta-protocol, miscellaneous functions
3 Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "connection.h"
31 int maxoutbufsize = 0;
33 /* Status and error notification routines */
35 bool send_status(connection_t *c, int statusno, const char *statusstring) {
37 statusstring = "Status";
39 return send_request(c, "%d %d %s", STATUS, statusno, statusstring);
42 bool status_h(connection_t *c, const char *request) {
44 char statusstring[MAX_STRING_SIZE];
46 if(sscanf(request, "%*d %d " MAX_STRING, &statusno, statusstring) != 2) {
47 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "STATUS",
48 c->name, c->hostname);
52 logger(DEBUG_STATUS, LOG_NOTICE, "Status message from %s (%s): %d: %s",
53 c->name, c->hostname, statusno, statusstring);
58 bool send_error(connection_t *c, int err, const char *errstring) {
62 return send_request(c, "%d %d %s", ERROR, err, errstring);
65 bool error_h(connection_t *c, const char *request) {
67 char errorstring[MAX_STRING_SIZE];
69 if(sscanf(request, "%*d %d " MAX_STRING, &err, errorstring) != 2) {
70 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ERROR",
71 c->name, c->hostname);
75 logger(DEBUG_ERROR, LOG_NOTICE, "Error message from %s (%s): %d: %s",
76 c->name, c->hostname, err, errorstring);
81 bool send_termreq(connection_t *c) {
82 return send_request(c, "%d", TERMREQ);
85 bool termreq_h(connection_t *c, const char *request) {
89 bool send_ping(connection_t *c) {
90 c->status.pinged = true;
91 c->last_ping_time = now.tv_sec;
93 return send_request(c, "%d", PING);
96 bool ping_h(connection_t *c, const char *request) {
100 bool send_pong(connection_t *c) {
101 return send_request(c, "%d", PONG);
104 bool pong_h(connection_t *c, const char *request) {
105 c->status.pinged = false;
107 /* Succesful connection, reset timeout if this is an outgoing connection. */
110 c->outgoing->timeout = 0;
111 c->outgoing->cfg = NULL;
113 freeaddrinfo(c->outgoing->ai);
114 c->outgoing->ai = NULL;
115 c->outgoing->aip = NULL;
121 /* Sending and receiving packets via TCP */
123 bool send_tcppacket(connection_t *c, const vpn_packet_t *packet) {
124 /* If there already is a lot of data in the outbuf buffer, discard this packet.
125 We use a very simple Random Early Drop algorithm. */
127 if(2.0 * c->outbuf.len / (float)maxoutbufsize - 1 > (float)rand()/(float)RAND_MAX)
130 if(!send_request(c, "%d %hd", PACKET, packet->len))
133 return send_meta(c, (char *)packet->data, packet->len);
136 bool tcppacket_h(connection_t *c, const char *request) {
139 if(sscanf(request, "%*d %hd", &len) != 1) {
140 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "PACKET", c->name,
145 /* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */