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"
25 #include "meshlink_internal.h"
32 int maxoutbufsize = 0;
34 /* Status and error notification routines */
36 bool send_status(meshlink_handle_t *mesh, connection_t *c, int statusno, const char *statusstring) {
38 statusstring = "Status";
40 return send_request(mesh, c, "%d %d %s", STATUS, statusno, statusstring);
43 bool status_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
45 char statusstring[MAX_STRING_SIZE];
47 if(sscanf(request, "%*d %d " MAX_STRING, &statusno, statusstring) != 2) {
48 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "STATUS",
49 c->name, c->hostname);
53 logger(DEBUG_STATUS, LOG_NOTICE, "Status message from %s (%s): %d: %s",
54 c->name, c->hostname, statusno, statusstring);
59 bool send_error(meshlink_handle_t *mesh, connection_t *c, int err, const char *errstring) {
63 return send_request(mesh, c, "%d %d %s", ERROR, err, errstring);
66 bool error_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
68 char errorstring[MAX_STRING_SIZE];
70 if(sscanf(request, "%*d %d " MAX_STRING, &err, errorstring) != 2) {
71 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ERROR",
72 c->name, c->hostname);
76 logger(DEBUG_ERROR, LOG_NOTICE, "Error message from %s (%s): %d: %s",
77 c->name, c->hostname, err, errorstring);
82 bool send_termreq(meshlink_handle_t *mesh, connection_t *c) {
83 return send_request(mesh, c, "%d", TERMREQ);
86 bool termreq_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
90 bool send_ping(meshlink_handle_t *mesh, connection_t *c) {
91 c->status.pinged = true;
92 c->last_ping_time = mesh->loop.now.tv_sec;
94 return send_request(mesh, c, "%d", PING);
97 bool ping_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
98 return send_pong(mesh, c);
101 bool send_pong(meshlink_handle_t *mesh, connection_t *c) {
102 return send_request(mesh, c, "%d", PONG);
105 bool pong_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
106 c->status.pinged = false;
108 /* Succesful connection, reset timeout if this is an outgoing connection. */
111 c->outgoing->timeout = 0;
112 c->outgoing->cfg = NULL;
114 freeaddrinfo(c->outgoing->ai);
115 c->outgoing->ai = NULL;
116 c->outgoing->aip = NULL;
122 /* Sending and receiving packets via TCP */
124 bool send_tcppacket(meshlink_handle_t *mesh, connection_t *c, const vpn_packet_t *packet) {
125 /* If there already is a lot of data in the outbuf buffer, discard this packet.
126 We use a very simple Random Early Drop algorithm. */
128 if(2.0 * c->outbuf.len / (float)maxoutbufsize - 1 > (float)rand()/(float)RAND_MAX)
131 if(!send_request(mesh, c, "%d %hd", PACKET, packet->len))
134 return send_meta(mesh, c, (char *)packet->data, packet->len);
137 bool tcppacket_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
140 if(sscanf(request, "%*d %hd", &len) != 1) {
141 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "PACKET", c->name,
146 /* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */