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 return sptps_send_record(&c->sptps, 0, buffer, length);
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);
63 bool receive_meta_sptps(void *handle, uint8_t type, const char *data, uint16_t length) {
64 connection_t *c = handle;
67 logger(DEBUG_ALWAYS, LOG_ERR, "receive_meta_sptps() called with NULL pointer!");
71 if(type == SPTPS_HANDSHAKE) {
72 if(c->allow_request == ACK)
81 /* Are we receiving a TCPpacket? */
84 if(length != c->tcplen)
86 receive_tcppacket(c, data, length);
91 /* Change newline to null byte, just like non-SPTPS requests */
93 if(data[length - 1] == '\n')
94 ((char *)data)[length - 1] = 0;
96 /* Otherwise we are waiting for a request */
98 return receive_request(c, data);
101 bool receive_meta(connection_t *c) {
103 char inbuf[MAXBUFSIZE];
104 char *bufp = inbuf, *endp;
107 - Read as much as possible from the TCP socket in one go.
109 - Check if a full request is in the input buffer.
110 - If yes, process request and remove it from the buffer,
112 - If not, keep stuff in buffer and exit.
115 buffer_compact(&c->inbuf, MAXBUFSIZE);
117 if(sizeof inbuf <= c->inbuf.len) {
118 logger(DEBUG_ALWAYS, LOG_ERR, "Input buffer full for %s (%s)", c->name, c->hostname);
122 inlen = recv(c->socket, inbuf, sizeof inbuf - c->inbuf.len, 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))
131 logger(DEBUG_ALWAYS, LOG_ERR, "Metadata socket read error for %s (%s): %s",
132 c->name, c->hostname, sockstrerror(sockerrno));
136 return sptps_receive_data(&c->sptps, bufp, inlen);