]> git.meshlink.io Git - meshlink/blob - src/meta.c
Remove support for the legacy protocol.
[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 "cipher.h"
23 #include "connection.h"
24 #include "logger.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 char *buffer, size_t length) {
32         connection_t *c = handle;
33
34         if(!c) {
35                 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta_sptps() called with NULL pointer!");
36                 abort();
37         }
38
39         buffer_add(&c->outbuf, buffer, length);
40         io_set(&c->io, IO_READ | IO_WRITE);
41
42         return true;
43 }
44
45 bool send_meta(connection_t *c, const char *buffer, int length) {
46         if(!c) {
47                 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta() called with NULL pointer!");
48                 abort();
49         }
50
51         logger(DEBUG_META, LOG_DEBUG, "Sending %d bytes of metadata to %s (%s)", length,
52                            c->name, c->hostname);
53
54         return sptps_send_record(&c->sptps, 0, buffer, length);
55 }
56
57 void broadcast_meta(connection_t *from, const char *buffer, int length) {
58         for list_each(connection_t, c, connection_list)
59                 if(c != from && c->status.active)
60                         send_meta(c, buffer, length);
61 }
62
63 bool receive_meta_sptps(void *handle, uint8_t type, const char *data, uint16_t length) {
64         connection_t *c = handle;
65
66         if(!c) {
67                 logger(DEBUG_ALWAYS, LOG_ERR, "receive_meta_sptps() called with NULL pointer!");
68                 abort();
69         }
70
71         if(type == SPTPS_HANDSHAKE) {
72                 if(c->allow_request == ACK)
73                         return send_ack(c);
74                 else
75                         return true;
76         }
77
78         if(!data)
79                 return true;
80
81         /* Are we receiving a TCPpacket? */
82
83         if(c->tcplen) {
84                 if(length != c->tcplen)
85                         return false;
86                 receive_tcppacket(c, data, length);
87                 c->tcplen = 0;
88                 return true;
89         }
90
91         /* Change newline to null byte, just like non-SPTPS requests */
92
93         if(data[length - 1] == '\n')
94                 ((char *)data)[length - 1] = 0;
95
96         /* Otherwise we are waiting for a request */
97
98         return receive_request(c, data);
99 }
100
101 bool receive_meta(connection_t *c) {
102         int inlen;
103         char inbuf[MAXBUFSIZE];
104         char *bufp = inbuf, *endp;
105
106         /* Strategy:
107            - Read as much as possible from the TCP socket in one go.
108            - Decrypt it.
109            - Check if a full request is in the input buffer.
110            - If yes, process request and remove it from the buffer,
111            then check again.
112            - If not, keep stuff in buffer and exit.
113          */
114
115         buffer_compact(&c->inbuf, MAXBUFSIZE);
116
117         if(sizeof inbuf <= c->inbuf.len) {
118                 logger(DEBUG_ALWAYS, LOG_ERR, "Input buffer full for %s (%s)", c->name, c->hostname);
119                 return false;
120         }
121
122         inlen = recv(c->socket, inbuf, sizeof inbuf - c->inbuf.len, 0);
123
124         if(inlen <= 0) {
125                 if(!inlen || !errno) {
126                         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection closed by %s (%s)",
127                                            c->name, c->hostname);
128                 } else if(sockwouldblock(sockerrno))
129                         return true;
130                 else
131                         logger(DEBUG_ALWAYS, LOG_ERR, "Metadata socket read error for %s (%s): %s",
132                                    c->name, c->hostname, sockstrerror(sockerrno));
133                 return false;
134         }
135
136         return sptps_receive_data(&c->sptps, bufp, inlen);
137 }