]> git.meshlink.io Git - meshlink/blob - src/meta.c
Don't use SPTPS to decrypt the ID request.
[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         if(c->allow_request == ID) {
55                 buffer_add(&c->outbuf, buffer, length);
56                 io_set(&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(connection_t *from, const char *buffer, int length) {
64         for list_each(connection_t, c, connection_list)
65                 if(c != from && c->status.active)
66                         send_meta(c, buffer, length);
67 }
68
69 bool receive_meta_sptps(void *handle, uint8_t type, const char *data, uint16_t length) {
70         connection_t *c = handle;
71
72         if(!c) {
73                 logger(DEBUG_ALWAYS, LOG_ERR, "receive_meta_sptps() called with NULL pointer!");
74                 abort();
75         }
76
77         if(type == SPTPS_HANDSHAKE) {
78                 if(c->allow_request == ACK)
79                         return send_ack(c);
80                 else
81                         return true;
82         }
83
84         if(!data)
85                 return true;
86
87         /* Are we receiving a TCPpacket? */
88
89         if(c->tcplen) {
90                 if(length != c->tcplen)
91                         return false;
92                 receive_tcppacket(c, data, length);
93                 c->tcplen = 0;
94                 return true;
95         }
96
97         /* Change newline to null byte, just like non-SPTPS requests */
98
99         if(data[length - 1] == '\n')
100                 ((char *)data)[length - 1] = 0;
101
102         /* Otherwise we are waiting for a request */
103
104         return receive_request(c, data);
105 }
106
107 bool receive_meta(connection_t *c) {
108         int inlen;
109         char inbuf[MAXBUFSIZE];
110         char *bufp = inbuf, *endp;
111
112         /* Strategy:
113            - Read as much as possible from the TCP socket in one go.
114            - Decrypt it.
115            - Check if a full request is in the input buffer.
116            - If yes, process request and remove it from the buffer,
117            then check again.
118            - If not, keep stuff in buffer and exit.
119          */
120
121         buffer_compact(&c->inbuf, MAXBUFSIZE);
122
123         if(sizeof inbuf <= c->inbuf.len) {
124                 logger(DEBUG_ALWAYS, LOG_ERR, "Input buffer full for %s (%s)", c->name, c->hostname);
125                 return false;
126         }
127
128         inlen = recv(c->socket, inbuf, sizeof inbuf - c->inbuf.len, 0);
129
130         if(inlen <= 0) {
131                 if(!inlen || !errno) {
132                         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection closed by %s (%s)",
133                                            c->name, c->hostname);
134                 } else if(sockwouldblock(sockerrno))
135                         return true;
136                 else
137                         logger(DEBUG_ALWAYS, LOG_ERR, "Metadata socket read error for %s (%s): %s",
138                                    c->name, c->hostname, sockstrerror(sockerrno));
139                 return false;
140         }
141
142         if(c->allow_request == ID) {
143                 endp = memchr(bufp, '\n', inlen);
144                 if(endp)
145                         endp++;
146                 else
147                         endp = bufp + inlen;
148
149                 buffer_add(&c->inbuf, bufp, endp - bufp);
150
151                 inlen -= endp - bufp;
152                 bufp = endp;
153
154                 while(c->inbuf.len) {
155                         char *request = buffer_readline(&c->inbuf);
156                         if(request) {
157                                 bool result = receive_request(c, request);
158                                 if(!result)
159                                         return false;
160                                 continue;
161                         } else {
162                                 break;
163                         }
164                 }
165
166                 return true;
167         }
168
169         return sptps_receive_data(&c->sptps, bufp, inlen);
170 }