]> git.meshlink.io Git - meshlink/blob - src/protocol_misc.c
Add a configurable fast connection retry period.
[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 error_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
54         assert(request);
55         assert(*request);
56
57         int err;
58         char errorstring[MAX_STRING_SIZE];
59
60         if(sscanf(request, "%*d %d " MAX_STRING, &err, errorstring) != 2) {
61                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ERROR", c->name);
62                 return false;
63         }
64
65         logger(mesh, MESHLINK_INFO, "Error message from %s: %d: %s", c->name, err, errorstring);
66
67         return false;
68 }
69
70 bool termreq_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
71         (void)mesh;
72         (void)c;
73         (void)request;
74
75         assert(request);
76         assert(*request);
77
78         return false;
79 }
80
81 bool send_ping(meshlink_handle_t *mesh, connection_t *c) {
82         c->status.pinged = true;
83         c->last_ping_time = mesh->loop.now.tv_sec;
84
85         return send_request(mesh, c, NULL, "%d", PING);
86 }
87
88 bool ping_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
89         (void)request;
90
91         assert(request);
92         assert(*request);
93
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, NULL, "%d", PONG);
99 }
100
101 bool pong_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
102         (void)mesh;
103         (void)request;
104
105         assert(request);
106         assert(*request);
107
108         c->status.pinged = false;
109
110         /* Successful connection, reset timeout if this is an outgoing connection. */
111
112         // TODO: completely remove this outgoing, let the autoconnect algorithm handle it
113         if(c->outgoing) {
114                 c->outgoing->timeout = 0;
115                 c->outgoing->state = OUTGOING_START;
116
117                 if(c->outgoing->ai) {
118                         freeaddrinfo(c->outgoing->ai);
119                 }
120
121                 c->outgoing->ai = NULL;
122                 c->outgoing->aip = NULL;
123         }
124
125         return true;
126 }
127
128 /* Sending and receiving packets via TCP */
129
130 bool tcppacket_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
131         assert(request);
132         assert(*request);
133
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         // This should never happen with MeshLink.
142         return false;
143 }