]> git.meshlink.io Git - meshlink/blob - src/protocol_misc.c
Fix memory leaks from timers.
[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         int statusno;
38         char statusstring[MAX_STRING_SIZE];
39
40         if(sscanf(request, "%*d %d " MAX_STRING, &statusno, statusstring) != 2) {
41                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "STATUS", c->name);
42                 return false;
43         }
44
45         logger(mesh, MESHLINK_INFO, "Status message from %s: %d: %s", c->name, statusno, statusstring);
46
47         return true;
48 }
49
50 bool error_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
51         int err;
52         char errorstring[MAX_STRING_SIZE];
53
54         if(sscanf(request, "%*d %d " MAX_STRING, &err, errorstring) != 2) {
55                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ERROR", c->name);
56                 return false;
57         }
58
59         logger(mesh, MESHLINK_INFO, "Error message from %s: %d: %s", c->name, err, errorstring);
60
61         return false;
62 }
63
64 bool termreq_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
65         (void)mesh;
66         (void)c;
67         (void)request;
68         return false;
69 }
70
71 bool send_ping(meshlink_handle_t *mesh, connection_t *c) {
72         c->status.pinged = true;
73         c->last_ping_time = mesh->loop.now.tv_sec;
74
75         return send_request(mesh, c, NULL, "%d", PING);
76 }
77
78 bool ping_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
79         (void)request;
80         return send_pong(mesh, c);
81 }
82
83 bool send_pong(meshlink_handle_t *mesh, connection_t *c) {
84         return send_request(mesh, c, NULL, "%d", PONG);
85 }
86
87 bool pong_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
88         (void)mesh;
89         (void)request;
90         c->status.pinged = false;
91
92         /* Successful connection, reset timeout if this is an outgoing connection. */
93
94         // TODO: completely remove this outgoing, let the autoconnect algorithm handle it
95         if(c->outgoing) {
96                 c->outgoing->timeout = 0;
97                 c->outgoing->state = OUTGOING_START;
98
99                 if(c->outgoing->ai) {
100                         freeaddrinfo(c->outgoing->ai);
101                 }
102
103                 c->outgoing->ai = NULL;
104                 c->outgoing->aip = NULL;
105         }
106
107         return true;
108 }
109
110 /* Sending and receiving packets via TCP */
111
112 bool tcppacket_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
113         short int len;
114
115         if(sscanf(request, "%*d %hd", &len) != 1) {
116                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "PACKET", c->name);
117                 return false;
118         }
119
120         // This should never happen with MeshLink.
121         return false;
122 }