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