]> git.meshlink.io Git - meshlink/blob - src/protocol_misc.c
Never automatically try to bind to ports >= 32768.
[meshlink] / 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         assert(request);
38         assert(*request);
39
40         int statusno;
41         char statusstring[MAX_STRING_SIZE];
42
43         if(sscanf(request, "%*d %d " MAX_STRING, &statusno, statusstring) != 2) {
44                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "STATUS", c->name);
45                 return false;
46         }
47
48         logger(mesh, MESHLINK_INFO, "Status message from %s: %d: %s", c->name, statusno, statusstring);
49
50         return true;
51 }
52
53 bool send_error(meshlink_handle_t *mesh, connection_t *c, request_error_t err, const char *message) {
54         send_request(mesh, c, NULL, "%d %d %s", ERROR, err, message);
55         flush_meta(mesh, c);
56         return false;
57 }
58
59 bool error_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
60         assert(request);
61         assert(*request);
62
63         int err;
64         char errorstring[MAX_STRING_SIZE];
65
66         if(sscanf(request, "%*d %d " MAX_STRING, &err, errorstring) != 2) {
67                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ERROR", c->name);
68                 return false;
69         }
70
71         logger(mesh, MESHLINK_INFO, "Error message from %s: %d: %s", c->name, err, errorstring);
72
73         switch(err) {
74         case BLACKLISTED:
75                 if(mesh->blacklisted_cb) {
76                         mesh->blacklisted_cb(mesh, (meshlink_node_t *)lookup_node(mesh, c->name));
77                 }
78         }
79
80         return false;
81 }
82
83 bool termreq_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
84         (void)mesh;
85         (void)c;
86         (void)request;
87
88         assert(request);
89         assert(*request);
90
91         return false;
92 }
93
94 bool send_ping(meshlink_handle_t *mesh, connection_t *c) {
95         c->status.pinged = true;
96         c->last_ping_time = mesh->loop.now.tv_sec;
97
98         return send_request(mesh, c, NULL, "%d", PING);
99 }
100
101 bool ping_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
102         (void)request;
103
104         assert(request);
105         assert(*request);
106
107         return send_pong(mesh, c);
108 }
109
110 bool send_pong(meshlink_handle_t *mesh, connection_t *c) {
111         return send_request(mesh, c, NULL, "%d", PONG);
112 }
113
114 bool pong_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
115         (void)mesh;
116         (void)request;
117
118         assert(request);
119         assert(*request);
120
121         c->status.pinged = false;
122
123         /* Successful connection, reset timeout if this is an outgoing connection. */
124
125         if(c->outgoing) {
126                 reset_outgoing(c->outgoing);
127         }
128
129         return true;
130 }
131
132 /* Sending and receiving packets via TCP */
133
134 bool tcppacket_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
135         assert(request);
136         assert(*request);
137
138         short int len;
139
140         if(sscanf(request, "%*d %hd", &len) != 1) {
141                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "PACKET", c->name);
142                 return false;
143         }
144
145         // This should never happen with MeshLink.
146         return false;
147 }
148
149 bool send_raw_packet(meshlink_handle_t *mesh, connection_t *c, const vpn_packet_t *packet) {
150         size_t hdrsize = sizeof(meshlink_packethdr_t);
151         assert(packet->len >= hdrsize);
152         return send_request(mesh, c, NULL, "%d", PACKET) && send_meta(mesh, c, (const char *)packet->data + hdrsize, packet->len - hdrsize);
153 }
154
155 bool raw_packet_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
156         (void)mesh;
157         (void)request;
158         c->status.raw_packet = true;
159         return true;
160 }