2 meta.c -- handle the meta communication
3 Copyright (C) 2014-2017 Guus Sliepen <guus@meshlink.io>,
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.
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.
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.
22 #include "connection.h"
24 #include "meshlink_internal.h"
31 bool send_meta_sptps(void *handle, uint8_t type, const void *buffer, size_t length) {
38 connection_t *c = handle;
39 meshlink_handle_t *mesh = c->mesh;
41 buffer_add(&c->outbuf, (const char *)buffer, length);
42 io_set(&mesh->loop, &c->io, IO_READ | IO_WRITE);
47 bool send_meta(meshlink_handle_t *mesh, connection_t *c, const char *buffer, int length) {
52 logger(mesh, MESHLINK_DEBUG, "Sending %d bytes of metadata to %s", length, c->name);
54 if(c->allow_request == ID) {
55 buffer_add(&c->outbuf, buffer, length);
56 io_set(&mesh->loop, &c->io, IO_READ | IO_WRITE);
60 return sptps_send_record(&c->sptps, 0, buffer, length);
63 void broadcast_meta(meshlink_handle_t *mesh, connection_t *from, const char *buffer, int length) {
67 for list_each(connection_t, c, mesh->connections)
68 if(c != from && c->status.active && !(c->flags & PROTOCOL_TINY)) {
69 send_meta(mesh, c, buffer, length);
73 void broadcast_submesh_meta(meshlink_handle_t *mesh, connection_t *from, const submesh_t *s, const char *buffer, int length) {
77 for list_each(connection_t, c, mesh->connections)
78 if(c != from && c->status.active && !(c->flags & PROTOCOL_TINY)) {
79 if(c->node && submesh_allows_node(s, c->node)) {
80 send_meta(mesh, c, buffer, length);
85 bool receive_meta_sptps(void *handle, uint8_t type, const void *data, uint16_t length) {
87 assert(!length || data);
89 connection_t *c = handle;
90 meshlink_handle_t *mesh = c->mesh;
91 char *request = (char *)data;
94 logger(mesh, MESHLINK_ERROR, "receive_meta_sptps() called with NULL pointer!");
98 if(type == SPTPS_HANDSHAKE) {
99 if(c->allow_request == ACK) {
100 return send_ack(mesh, c);
110 /* Are we receiving a raw packet? */
112 if(c->status.raw_packet) {
113 c->status.raw_packet = false;
115 if(mesh->receive_cb) {
116 mesh->receive_cb(mesh, (meshlink_node_t *)c->node, data, length);
122 /* Change newline to null byte, just like non-SPTPS requests */
124 if(request[length - 1] == '\n') {
125 request[length - 1] = 0;
128 /* Otherwise we are waiting for a request */
130 return receive_request(mesh, c, request);
133 bool receive_meta(meshlink_handle_t *mesh, connection_t *c) {
135 char inbuf[MAXBUFSIZE];
137 inlen = recv(c->socket, inbuf, sizeof(inbuf), 0);
140 if(!inlen || !errno) {
141 logger(mesh, MESHLINK_INFO, "Connection closed by %s", c->name);
142 } else if(sockwouldblock(sockerrno)) {
145 logger(mesh, MESHLINK_ERROR, "Metadata socket read error for %s: %s", c->name, sockstrerror(sockerrno));
151 logger(mesh, MESHLINK_DEBUG, "Received %d bytes of metadata from %s", inlen, c->name);
154 c->node->in_meta += inlen;
157 if(c->allow_request == ID) {
158 buffer_add(&c->inbuf, inbuf, inlen);
160 char *request = buffer_readline(&c->inbuf);
163 if(!receive_request(mesh, c, request) || c->allow_request == ID) {
167 int left = c->inbuf.len - c->inbuf.offset;
170 return sptps_receive_data(&c->sptps, buffer_read(&c->inbuf, left), left);
176 if(c->inbuf.len >= sizeof(inbuf)) {
177 logger(mesh, MESHLINK_ERROR, "Input buffer full for %s", c->name);
184 return sptps_receive_data(&c->sptps, inbuf, inlen);