]> git.meshlink.io Git - meshlink/blob - src/meta.c
only allow meshlink_join() when the library thread is not running
[meshlink] / src / meta.c
1 /*
2     meta.c -- handle the meta communication
3     Copyright (C) 2014 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         connection_t *c = handle;
33         meshlink_handle_t *mesh = c->mesh;
34
35         if(!c) {
36                 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta_sptps() called with NULL pointer!");
37                 abort();
38         }
39
40         buffer_add(&c->outbuf, (const char *)buffer, length);
41         io_set(&mesh->loop, &c->io, IO_READ | IO_WRITE);
42
43         return true;
44 }
45
46 bool send_meta(meshlink_handle_t *mesh, connection_t *c, const char *buffer, int length) {
47         if(!c) {
48                 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta() called with NULL pointer!");
49                 abort();
50         }
51
52         logger(DEBUG_META, LOG_DEBUG, "Sending %d bytes of metadata to %s (%s)", length,
53                            c->name, c->hostname);
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 bool receive_meta_sptps(void *handle, uint8_t type, const void *data, uint16_t length) {
71         connection_t *c = handle;
72         meshlink_handle_t *mesh = c->mesh;
73         char *request = (char *)data;
74
75         if(!c) {
76                 logger(DEBUG_ALWAYS, LOG_ERR, "receive_meta_sptps() called with NULL pointer!");
77                 abort();
78         }
79
80         if(type == SPTPS_HANDSHAKE) {
81                 if(c->allow_request == ACK)
82                         return send_ack(mesh, c);
83                 else
84                         return true;
85         }
86
87         if(!request)
88                 return true;
89
90         /* Are we receiving a TCPpacket? */
91
92         if(c->tcplen) {
93                 if(length != c->tcplen)
94                         return false;
95                 receive_tcppacket(mesh, c, request, length);
96                 c->tcplen = 0;
97                 return true;
98         }
99
100         /* Change newline to null byte, just like non-SPTPS requests */
101
102         if(request[length - 1] == '\n')
103                 request[length - 1] = 0;
104
105         /* Otherwise we are waiting for a request */
106
107         return receive_request(mesh, c, request);
108 }
109
110 bool receive_meta(meshlink_handle_t *mesh, connection_t *c) {
111         int inlen;
112         char inbuf[MAXBUFSIZE];
113         char *bufp = inbuf, *endp;
114
115         /* Strategy:
116            - Read as much as possible from the TCP socket in one go.
117            - Decrypt it.
118            - Check if a full request is in the input buffer.
119            - If yes, process request and remove it from the buffer,
120            then check again.
121            - If not, keep stuff in buffer and exit.
122          */
123
124         buffer_compact(&c->inbuf, MAXBUFSIZE);
125
126         if(sizeof inbuf <= c->inbuf.len) {
127                 logger(DEBUG_ALWAYS, LOG_ERR, "Input buffer full for %s (%s)", c->name, c->hostname);
128                 return false;
129         }
130
131         inlen = recv(c->socket, inbuf, sizeof inbuf - c->inbuf.len, 0);
132
133         if(inlen <= 0) {
134                 if(!inlen || !errno) {
135                         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection closed by %s (%s)",
136                                            c->name, c->hostname);
137                 } else if(sockwouldblock(sockerrno))
138                         return true;
139                 else
140                         logger(DEBUG_ALWAYS, LOG_ERR, "Metadata socket read error for %s (%s): %s",
141                                    c->name, c->hostname, sockstrerror(sockerrno));
142                 return false;
143         }
144
145         if(c->allow_request == ID) {
146                 endp = memchr(bufp, '\n', inlen);
147                 if(endp)
148                         endp++;
149                 else
150                         endp = bufp + inlen;
151
152                 buffer_add(&c->inbuf, bufp, endp - bufp);
153
154                 inlen -= endp - bufp;
155                 bufp = endp;
156
157                 while(c->inbuf.len) {
158                         char *request = buffer_readline(&c->inbuf);
159                         if(request) {
160                                 bool result = receive_request(mesh, c, request);
161                                 if(!result)
162                                         return false;
163                                 continue;
164                         } else {
165                                 break;
166                         }
167                 }
168
169                 return true;
170         }
171
172         return sptps_receive_data(&c->sptps, bufp, inlen);
173 }