]> git.meshlink.io Git - meshlink/blob - src/meta.c
Remove everything GPL that is not copyright Guus Sliepen, update copyright statements.
[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->protocol_minor >= 2)
55                 return sptps_send_record(&c->sptps, 0, buffer, length);
56
57         /* Add our data to buffer */
58         if(c->status.encryptout) {
59                 size_t outlen = length;
60
61                 if(!cipher_encrypt(c->outcipher, buffer, length, buffer_prepare(&c->outbuf, length), &outlen, false) || outlen != length) {
62                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting metadata to %s (%s)",
63                                         c->name, c->hostname);
64                         return false;
65                 }
66         } else {
67                 buffer_add(&c->outbuf, buffer, length);
68         }
69
70         io_set(&c->io, IO_READ | IO_WRITE);
71
72         return true;
73 }
74
75 void broadcast_meta(connection_t *from, const char *buffer, int length) {
76         for list_each(connection_t, c, connection_list)
77                 if(c != from && c->status.active)
78                         send_meta(c, buffer, length);
79 }
80
81 bool receive_meta_sptps(void *handle, uint8_t type, const char *data, uint16_t length) {
82         connection_t *c = handle;
83
84         if(!c) {
85                 logger(DEBUG_ALWAYS, LOG_ERR, "receive_meta_sptps() called with NULL pointer!");
86                 abort();
87         }
88
89         if(type == SPTPS_HANDSHAKE) {
90                 if(c->allow_request == ACK)
91                         return send_ack(c);
92                 else
93                         return true;
94         }
95
96         if(!data)
97                 return true;
98
99         /* Are we receiving a TCPpacket? */
100
101         if(c->tcplen) {
102                 if(length != c->tcplen)
103                         return false;
104                 receive_tcppacket(c, data, length);
105                 c->tcplen = 0;
106                 return true;
107         }
108
109         /* Change newline to null byte, just like non-SPTPS requests */
110
111         if(data[length - 1] == '\n')
112                 ((char *)data)[length - 1] = 0;
113
114         /* Otherwise we are waiting for a request */
115
116         return receive_request(c, data);
117 }
118
119 bool receive_meta(connection_t *c) {
120         int inlen;
121         char inbuf[MAXBUFSIZE];
122         char *bufp = inbuf, *endp;
123
124         /* Strategy:
125            - Read as much as possible from the TCP socket in one go.
126            - Decrypt it.
127            - Check if a full request is in the input buffer.
128            - If yes, process request and remove it from the buffer,
129            then check again.
130            - If not, keep stuff in buffer and exit.
131          */
132
133         buffer_compact(&c->inbuf, MAXBUFSIZE);
134
135         if(sizeof inbuf <= c->inbuf.len) {
136                 logger(DEBUG_ALWAYS, LOG_ERR, "Input buffer full for %s (%s)", c->name, c->hostname);
137                 return false;
138         }
139
140         inlen = recv(c->socket, inbuf, sizeof inbuf - c->inbuf.len, 0);
141
142         if(inlen <= 0) {
143                 if(!inlen || !errno) {
144                         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection closed by %s (%s)",
145                                            c->name, c->hostname);
146                 } else if(sockwouldblock(sockerrno))
147                         return true;
148                 else
149                         logger(DEBUG_ALWAYS, LOG_ERR, "Metadata socket read error for %s (%s): %s",
150                                    c->name, c->hostname, sockstrerror(sockerrno));
151                 return false;
152         }
153
154         do {
155                 if(c->protocol_minor >= 2)
156                         return sptps_receive_data(&c->sptps, bufp, inlen);
157
158                 if(!c->status.decryptin) {
159                         endp = memchr(bufp, '\n', inlen);
160                         if(endp)
161                                 endp++;
162                         else
163                                 endp = bufp + inlen;
164
165                         buffer_add(&c->inbuf, bufp, endp - bufp);
166
167                         inlen -= endp - bufp;
168                         bufp = endp;
169                 } else {
170                         size_t outlen = inlen;
171
172                         if(!cipher_decrypt(c->incipher, bufp, inlen, buffer_prepare(&c->inbuf, inlen), &outlen, false) || inlen != outlen) {
173                                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting metadata from %s (%s)",
174                                            c->name, c->hostname);
175                                 return false;
176                         }
177
178                         inlen = 0;
179                 }
180
181                 while(c->inbuf.len) {
182                         /* Are we receiving a TCPpacket? */
183
184                         if(c->tcplen) {
185                                 char *tcpbuffer = buffer_read(&c->inbuf, c->tcplen);
186                                 if(!tcpbuffer)
187                                         break;
188
189                                 if(!c->node) {
190                                         if(c->outgoing && proxytype == PROXY_SOCKS4 && c->allow_request == ID) {
191                                                 if(tcpbuffer[0] == 0 && tcpbuffer[1] == 0x5a) {
192                                                         logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
193                                                 } else {
194                                                         logger(DEBUG_CONNECTIONS, LOG_ERR, "Proxy request rejected");
195                                                         return false;
196                                                 }
197                                         } else if(c->outgoing && proxytype == PROXY_SOCKS5 && c->allow_request == ID) {
198                                                 if(tcpbuffer[0] != 5) {
199                                                         logger(DEBUG_CONNECTIONS, LOG_ERR, "Invalid response from proxy server");
200                                                         return false;
201                                                 }
202                                                 if(tcpbuffer[1] == (char)0xff) {
203                                                         logger(DEBUG_CONNECTIONS, LOG_ERR, "Proxy request rejected: unsuitable authentication method");
204                                                         return false;
205                                                 }
206                                                 if(tcpbuffer[2] != 5) {
207                                                         logger(DEBUG_CONNECTIONS, LOG_ERR, "Invalid response from proxy server");
208                                                         return false;
209                                                 }
210                                                 if(tcpbuffer[3] == 0) {
211                                                         logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
212                                                 } else {
213                                                         logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request rejected");
214                                                         return false;
215                                                 }
216                                         } else {
217                                                 logger(DEBUG_CONNECTIONS, LOG_ERR, "c->tcplen set but c->node is NULL!");
218                                                 abort();
219                                         }
220                                 } else {
221                                         if(c->allow_request == ALL) {
222                                                 receive_tcppacket(c, tcpbuffer, c->tcplen);
223                                         } else {
224                                                 logger(DEBUG_CONNECTIONS, LOG_ERR, "Got unauthorized TCP packet from %s (%s)", c->name, c->hostname);
225                                                 return false;
226                                         }
227                                 }
228
229                                 c->tcplen = 0;
230                         }
231
232                         /* Otherwise we are waiting for a request */
233
234                         char *request = buffer_readline(&c->inbuf);
235                         if(request) {
236                                 bool result = receive_request(c, request);
237                                 if(!result)
238                                         return false;
239                                 continue;
240                         } else {
241                                 break;
242                         }
243                 }
244         } while(inlen);
245
246         return true;
247 }