]> git.meshlink.io Git - meshlink/blob - src/meta.c
Improve support for submeshes.
[meshlink] / src / meta.c
1 /*
2     meta.c -- handle the meta communication
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 "connection.h"
23 #include "logger.h"
24 #include "meshlink_internal.h"
25 #include "meta.h"
26 #include "net.h"
27 #include "protocol.h"
28 #include "utils.h"
29 #include "xalloc.h"
30
31 bool send_meta_sptps(void *handle, uint8_t type, const void *buffer, size_t length) {
32         (void)type;
33         connection_t *c = handle;
34         meshlink_handle_t *mesh = c->mesh;
35
36         if(!c) {
37                 logger(mesh, MESHLINK_ERROR, "send_meta_sptps() called with NULL pointer!");
38                 abort();
39         }
40
41         buffer_add(&c->outbuf, (const char *)buffer, length);
42         io_set(&mesh->loop, &c->io, IO_READ | IO_WRITE);
43
44         return true;
45 }
46
47 bool send_meta(meshlink_handle_t *mesh, connection_t *c, const char *buffer, int length) {
48         if(!c) {
49                 logger(mesh, MESHLINK_ERROR, "send_meta() called with NULL pointer!");
50                 abort();
51         }
52
53         logger(mesh, MESHLINK_DEBUG, "Sending %d bytes of metadata to %s", length, c->name);
54
55         if(c->allow_request == ID) {
56                 buffer_add(&c->outbuf, buffer, length);
57                 io_set(&mesh->loop, &c->io, IO_READ | IO_WRITE);
58                 return true;
59         }
60
61         return sptps_send_record(&c->sptps, 0, buffer, length);
62 }
63
64 void broadcast_meta(meshlink_handle_t *mesh, connection_t *from, const char *buffer, int length) {
65         for list_each(connection_t, c, mesh->connections)
66                 if(c != from && c->status.active) {
67                         send_meta(mesh, c, buffer, length);
68                 }
69 }
70
71 void broadcast_submesh_meta(meshlink_handle_t *mesh, connection_t *from, submesh_t *s,
72                             const char *buffer, int length) {
73         for list_each(connection_t, c, mesh->connections)
74                 if(c != from && c->status.active) {
75                         if(c->node && submesh_allows_node(s, c->node)) {
76                                 send_meta(mesh, c, buffer, length);
77                         }
78                 }
79 }
80
81 bool receive_meta_sptps(void *handle, uint8_t type, const void *data, uint16_t length) {
82         connection_t *c = handle;
83         meshlink_handle_t *mesh = c->mesh;
84         char *request = (char *)data;
85
86         if(!c) {
87                 logger(mesh, MESHLINK_ERROR, "receive_meta_sptps() called with NULL pointer!");
88                 abort();
89         }
90
91         if(type == SPTPS_HANDSHAKE) {
92                 if(c->allow_request == ACK) {
93                         return send_ack(mesh, c);
94                 } else {
95                         return true;
96                 }
97         }
98
99         if(!request) {
100                 return true;
101         }
102
103         /* Are we receiving a TCPpacket? */
104
105         if(c->tcplen) {
106                 abort(); // TODO: get rid of tcplen altogether
107         }
108
109         /* Change newline to null byte, just like non-SPTPS requests */
110
111         if(request[length - 1] == '\n') {
112                 request[length - 1] = 0;
113         }
114
115         /* Otherwise we are waiting for a request */
116
117         return receive_request(mesh, c, request);
118 }
119
120 bool receive_meta(meshlink_handle_t *mesh, connection_t *c) {
121         int inlen;
122         char inbuf[MAXBUFSIZE];
123
124         inlen = recv(c->socket, inbuf, sizeof(inbuf), 0);
125
126         if(inlen <= 0) {
127                 if(!inlen || !errno) {
128                         logger(mesh, MESHLINK_INFO, "Connection closed by %s", c->name);
129                 } else if(sockwouldblock(sockerrno)) {
130                         return true;
131                 } else {
132                         logger(mesh, MESHLINK_ERROR, "Metadata socket read error for %s: %s", c->name, sockstrerror(sockerrno));
133                 }
134
135                 return false;
136         }
137
138         if(c->allow_request == ID) {
139                 buffer_add(&c->inbuf, inbuf, inlen);
140
141                 char *request = buffer_readline(&c->inbuf);
142
143                 if(request) {
144                         if(!receive_request(mesh, c, request) || c->allow_request == ID) {
145                                 return false;
146                         }
147
148                         int left = c->inbuf.len - c->inbuf.offset;
149
150                         if(left > 0) {
151                                 return sptps_receive_data(&c->sptps, buffer_read(&c->inbuf, left), left);
152                         } else {
153                                 return true;
154                         }
155                 }
156
157                 if(c->inbuf.len >= sizeof(inbuf)) {
158                         logger(mesh, MESHLINK_ERROR, "Input buffer full for %s", c->name);
159                         return false;
160                 } else {
161                         return true;
162                 }
163         }
164
165         return sptps_receive_data(&c->sptps, inbuf, inlen);
166 }