2 meta.c -- handle the meta communication
3 Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>,
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.
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.
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.
23 #include "connection.h"
31 bool send_meta_sptps(void *handle, uint8_t type, const char *buffer, size_t length) {
32 connection_t *c = handle;
35 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta_sptps() called with NULL pointer!");
39 buffer_add(&c->outbuf, buffer, length);
40 io_set(&c->io, IO_READ | IO_WRITE);
45 bool send_meta(connection_t *c, const char *buffer, int length) {
47 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta() called with NULL pointer!");
51 logger(DEBUG_META, LOG_DEBUG, "Sending %d bytes of metadata to %s (%s)", length,
52 c->name, c->hostname);
54 if(c->allow_request == ID) {
55 buffer_add(&c->outbuf, buffer, length);
56 io_set(&c->io, IO_READ | IO_WRITE);
60 return sptps_send_record(&c->sptps, 0, buffer, length);
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);
69 bool receive_meta_sptps(void *handle, uint8_t type, const char *data, uint16_t length) {
70 connection_t *c = handle;
73 logger(DEBUG_ALWAYS, LOG_ERR, "receive_meta_sptps() called with NULL pointer!");
77 if(type == SPTPS_HANDSHAKE) {
78 if(c->allow_request == ACK)
87 /* Are we receiving a TCPpacket? */
90 if(length != c->tcplen)
92 receive_tcppacket(c, data, length);
97 /* Change newline to null byte, just like non-SPTPS requests */
99 if(data[length - 1] == '\n')
100 ((char *)data)[length - 1] = 0;
102 /* Otherwise we are waiting for a request */
104 return receive_request(c, data);
107 bool receive_meta(connection_t *c) {
109 char inbuf[MAXBUFSIZE];
110 char *bufp = inbuf, *endp;
113 - Read as much as possible from the TCP socket in one go.
115 - Check if a full request is in the input buffer.
116 - If yes, process request and remove it from the buffer,
118 - If not, keep stuff in buffer and exit.
121 buffer_compact(&c->inbuf, MAXBUFSIZE);
123 if(sizeof inbuf <= c->inbuf.len) {
124 logger(DEBUG_ALWAYS, LOG_ERR, "Input buffer full for %s (%s)", c->name, c->hostname);
128 inlen = recv(c->socket, inbuf, sizeof inbuf - c->inbuf.len, 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))
137 logger(DEBUG_ALWAYS, LOG_ERR, "Metadata socket read error for %s (%s): %s",
138 c->name, c->hostname, sockstrerror(sockerrno));
142 if(c->allow_request == ID) {
143 endp = memchr(bufp, '\n', inlen);
149 buffer_add(&c->inbuf, bufp, endp - bufp);
151 inlen -= endp - bufp;
154 while(c->inbuf.len) {
155 char *request = buffer_readline(&c->inbuf);
157 bool result = receive_request(c, request);
169 return sptps_receive_data(&c->sptps, bufp, inlen);