]> git.meshlink.io Git - meshlink-tiny/blob - src/protocol_misc.c
Add a metering test.
[meshlink-tiny] / src / protocol_misc.c
1 /*
2     protocol_misc.c -- handle the meta-protocol, miscellaneous functions
3     Copyright (C) 2014-2017 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 status_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
37         (void)mesh;
38         (void)c;
39         (void)request;
40
41         return true;
42 }
43
44 bool send_error(meshlink_handle_t *mesh, connection_t *c, request_error_t err, const char *message) {
45         send_request(mesh, c, "%d %d %s", ERROR, err, message);
46         flush_meta(mesh, c);
47         return false;
48 }
49
50 bool error_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
51         (void)mesh;
52         (void)c;
53         (void)request;
54
55         return false;
56 }
57
58 bool termreq_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
59         (void)mesh;
60         (void)c;
61         (void)request;
62
63         return false;
64 }
65
66 bool send_ping(meshlink_handle_t *mesh, connection_t *c) {
67         c->status.pinged = true;
68         c->last_ping_time = mesh->loop.now.tv_sec;
69
70         return send_request(mesh, c, "%d", PING);
71 }
72
73 bool ping_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
74         (void)request;
75
76         assert(request);
77         assert(*request);
78
79         return send_pong(mesh, c);
80 }
81
82 bool send_pong(meshlink_handle_t *mesh, connection_t *c) {
83         return send_request(mesh, c, "%d", PONG);
84 }
85
86 bool pong_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
87         (void)mesh;
88         (void)request;
89
90         assert(request);
91         assert(*request);
92
93         c->status.pinged = false;
94
95         /* Successful connection, reset timeout if this is an outgoing connection. */
96
97         if(c->outgoing) {
98                 reset_outgoing(c->outgoing);
99         }
100
101         return true;
102 }
103
104 bool send_raw_packet(meshlink_handle_t *mesh, connection_t *c, const vpn_packet_t *packet) {
105         return send_request(mesh, c, "%d", PACKET) && send_meta(mesh, c, (const char *)packet->data, packet->len);
106 }
107
108 bool raw_packet_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
109         (void)mesh;
110         (void)request;
111         c->status.raw_packet = true;
112         return true;
113 }