]> git.meshlink.io Git - meshlink/blob - src/protocol_misc.c
Remove everything GPL that is not copyright Guus Sliepen, update copyright statements.
[meshlink] / src / protocol_misc.c
1 /*
2     protocol_misc.c -- handle the meta-protocol, miscellaneous functions
3     Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
4
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.
9
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.
14
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.
18 */
19
20 #include "system.h"
21
22 #include "conf.h"
23 #include "connection.h"
24 #include "logger.h"
25 #include "meta.h"
26 #include "net.h"
27 #include "netutl.h"
28 #include "protocol.h"
29 #include "utils.h"
30
31 int maxoutbufsize = 0;
32
33 /* Status and error notification routines */
34
35 bool send_status(connection_t *c, int statusno, const char *statusstring) {
36         if(!statusstring)
37                 statusstring = "Status";
38
39         return send_request(c, "%d %d %s", STATUS, statusno, statusstring);
40 }
41
42 bool status_h(connection_t *c, const char *request) {
43         int statusno;
44         char statusstring[MAX_STRING_SIZE];
45
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);
49                 return false;
50         }
51
52         logger(DEBUG_STATUS, LOG_NOTICE, "Status message from %s (%s): %d: %s",
53                            c->name, c->hostname, statusno, statusstring);
54
55         return true;
56 }
57
58 bool send_error(connection_t *c, int err, const char *errstring) {
59         if(!errstring)
60                 errstring = "Error";
61
62         return send_request(c, "%d %d %s", ERROR, err, errstring);
63 }
64
65 bool error_h(connection_t *c, const char *request) {
66         int err;
67         char errorstring[MAX_STRING_SIZE];
68
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);
72                 return false;
73         }
74
75         logger(DEBUG_ERROR, LOG_NOTICE, "Error message from %s (%s): %d: %s",
76                            c->name, c->hostname, err, errorstring);
77
78         return false;
79 }
80
81 bool send_termreq(connection_t *c) {
82         return send_request(c, "%d", TERMREQ);
83 }
84
85 bool termreq_h(connection_t *c, const char *request) {
86         return false;
87 }
88
89 bool send_ping(connection_t *c) {
90         c->status.pinged = true;
91         c->last_ping_time = now.tv_sec;
92
93         return send_request(c, "%d", PING);
94 }
95
96 bool ping_h(connection_t *c, const char *request) {
97         return send_pong(c);
98 }
99
100 bool send_pong(connection_t *c) {
101         return send_request(c, "%d", PONG);
102 }
103
104 bool pong_h(connection_t *c, const char *request) {
105         c->status.pinged = false;
106
107         /* Succesful connection, reset timeout if this is an outgoing connection. */
108
109         if(c->outgoing) {
110                 c->outgoing->timeout = 0;
111                 c->outgoing->cfg = NULL;
112                 if(c->outgoing->ai)
113                         freeaddrinfo(c->outgoing->ai);
114                 c->outgoing->ai = NULL;
115                 c->outgoing->aip = NULL;
116         }
117
118         return true;
119 }
120
121 /* Sending and receiving packets via TCP */
122
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. */
126
127         if(2.0 * c->outbuf.len / (float)maxoutbufsize - 1 > (float)rand()/(float)RAND_MAX)
128                 return true;
129
130         if(!send_request(c, "%d %hd", PACKET, packet->len))
131                 return false;
132
133         return send_meta(c, (char *)packet->data, packet->len);
134 }
135
136 bool tcppacket_h(connection_t *c, const char *request) {
137         short int len;
138
139         if(sscanf(request, "%*d %hd", &len) != 1) {
140                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "PACKET", c->name,
141                            c->hostname);
142                 return false;
143         }
144
145         /* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */
146
147         c->tcplen = len;
148
149         return true;
150 }