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