]> git.meshlink.io Git - meshlink/blob - src/protocol_misc.c
Get rid of ->hostname.
[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 "meshlink_internal.h"
26 #include "meta.h"
27 #include "net.h"
28 #include "netutl.h"
29 #include "protocol.h"
30 #include "utils.h"
31
32 int maxoutbufsize = 0;
33
34 /* Status and error notification routines */
35
36 bool send_status(meshlink_handle_t *mesh, connection_t *c, int statusno, const char *statusstring) {
37         if(!statusstring)
38                 statusstring = "Status";
39
40         return send_request(mesh, c, "%d %d %s", STATUS, statusno, statusstring);
41 }
42
43 bool status_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
44         int statusno;
45         char statusstring[MAX_STRING_SIZE];
46
47         if(sscanf(request, "%*d %d " MAX_STRING, &statusno, statusstring) != 2) {
48                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "STATUS", c->name);
49                 return false;
50         }
51
52         logger(mesh, MESHLINK_INFO, "Status message from %s: %d: %s", c->name, statusno, statusstring);
53
54         return true;
55 }
56
57 bool send_error(meshlink_handle_t *mesh, connection_t *c, int err, const char *errstring) {
58         if(!errstring)
59                 errstring = "Error";
60
61         return send_request(mesh, c, "%d %d %s", ERROR, err, errstring);
62 }
63
64 bool error_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
65         int err;
66         char errorstring[MAX_STRING_SIZE];
67
68         if(sscanf(request, "%*d %d " MAX_STRING, &err, errorstring) != 2) {
69                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ERROR", c->name);
70                 return false;
71         }
72
73         logger(mesh, MESHLINK_INFO, "Error message from %s: %d: %s", c->name, err, errorstring);
74
75         return false;
76 }
77
78 bool send_termreq(meshlink_handle_t *mesh, connection_t *c) {
79         return send_request(mesh, c, "%d", TERMREQ);
80 }
81
82 bool termreq_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
83         return false;
84 }
85
86 bool send_ping(meshlink_handle_t *mesh, connection_t *c) {
87         c->status.pinged = true;
88         c->last_ping_time = mesh->loop.now.tv_sec;
89
90         return send_request(mesh, c, "%d", PING);
91 }
92
93 bool ping_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
94         return send_pong(mesh, c);
95 }
96
97 bool send_pong(meshlink_handle_t *mesh, connection_t *c) {
98         return send_request(mesh, c, "%d", PONG);
99 }
100
101 bool pong_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
102         c->status.pinged = false;
103
104         /* Succesful connection, reset timeout if this is an outgoing connection. */
105
106         if(c->outgoing) {
107                 c->outgoing->timeout = 0;
108                 c->outgoing->cfg = NULL;
109                 if(c->outgoing->ai)
110                         freeaddrinfo(c->outgoing->ai);
111                 c->outgoing->ai = NULL;
112                 c->outgoing->aip = NULL;
113         }
114
115         return true;
116 }
117
118 /* Sending and receiving packets via TCP */
119
120 bool send_tcppacket(meshlink_handle_t *mesh, connection_t *c, const vpn_packet_t *packet) {
121         /* If there already is a lot of data in the outbuf buffer, discard this packet.
122            We use a very simple Random Early Drop algorithm. */
123
124         if(2.0 * c->outbuf.len / (float)maxoutbufsize - 1 > (float)rand() / (float)RAND_MAX)
125                 return true;
126
127         if(!send_request(mesh, c, "%d %hd", PACKET, packet->len))
128                 return false;
129
130         return send_meta(mesh, c, (char *)packet->data, packet->len);
131 }
132
133 bool tcppacket_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
134         short int len;
135
136         if(sscanf(request, "%*d %hd", &len) != 1) {
137                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "PACKET", c->name);
138                 return false;
139         }
140
141         /* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */
142
143         c->tcplen = len;
144
145         return true;
146 }