2 meta.c -- handle the meta communication
3 Copyright (C) 2000-2009 Guus Sliepen <guus@tinc-vpn.org>,
4 2000-2005 Ivo Timmermans
5 2006 Scott Lamb <slamb@slamb.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "splay_tree.h"
26 #include "connection.h"
34 bool send_meta_sptps(void *handle, const char *buffer, size_t length) {
35 connection_t *c = handle;
38 logger(LOG_ERR, "send_meta_sptps() called with NULL pointer!");
42 logger(LOG_DEBUG, "send_meta_sptps(%s, %p, %zu)", c->name, buffer, length);
44 buffer_add(&c->outbuf, buffer, length);
45 event_add(&c->outevent, NULL);
50 bool send_meta(connection_t *c, const char *buffer, int length) {
52 logger(LOG_ERR, "send_meta() called with NULL pointer!");
56 ifdebug(META) logger(LOG_DEBUG, "Sending %d bytes of metadata to %s (%s)", length,
57 c->name, c->hostname);
59 if(c->protocol_minor >= 2)
60 return send_record(&c->sptps, 0, buffer, length);
62 /* Add our data to buffer */
63 if(c->status.encryptout) {
64 size_t outlen = length;
66 if(!cipher_encrypt(&c->outcipher, buffer, length, buffer_prepare(&c->outbuf, length), &outlen, false) || outlen != length) {
67 logger(LOG_ERR, "Error while encrypting metadata to %s (%s)",
68 c->name, c->hostname);
73 buffer_add(&c->outbuf, buffer, length);
76 event_add(&c->outevent, NULL);
81 void broadcast_meta(connection_t *from, const char *buffer, int length) {
85 for(node = connection_tree->head; node; node = node->next) {
88 if(c != from && c->status.active)
89 send_meta(c, buffer, length);
93 bool receive_meta_sptps(void *handle, uint8_t type, const char *data, uint16_t length) {
94 connection_t *c = handle;
97 logger(LOG_ERR, "receive_meta_sptps() called with NULL pointer!");
101 logger(LOG_DEBUG, "receive_meta_sptps(%s, %d, %p, %hu)", c->name, type, data, length);
103 if(type == SPTPS_HANDSHAKE) {
104 if(c->allow_request == ACK)
113 /* Are we receiving a TCPpacket? */
116 if(length != c->tcplen)
118 receive_tcppacket(c, data, length);
123 /* Otherwise we are waiting for a request */
125 return receive_request(c, data);
128 bool receive_meta(connection_t *c) {
130 char inbuf[MAXBUFSIZE];
131 char *bufp = inbuf, *endp;
134 - Read as much as possible from the TCP socket in one go.
136 - Check if a full request is in the input buffer.
137 - If yes, process request and remove it from the buffer,
139 - If not, keep stuff in buffer and exit.
142 buffer_compact(&c->inbuf, MAXBUFSIZE);
144 if(sizeof inbuf <= c->inbuf.len) {
145 logger(LOG_ERR, "Input buffer full for %s (%s)", c->name, c->hostname);
149 inlen = recv(c->socket, inbuf, sizeof inbuf - c->inbuf.len, 0);
152 if(!inlen || !errno) {
153 ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection closed by %s (%s)",
154 c->name, c->hostname);
155 } else if(sockwouldblock(sockerrno))
158 logger(LOG_ERR, "Metadata socket read error for %s (%s): %s",
159 c->name, c->hostname, sockstrerror(sockerrno));
164 if(c->protocol_minor >= 2) {
165 logger(LOG_DEBUG, "Receiving %d bytes of SPTPS data", inlen);
166 return receive_data(&c->sptps, bufp, inlen);
169 if(!c->status.decryptin) {
170 endp = memchr(bufp, '\n', inlen);
176 buffer_add(&c->inbuf, bufp, endp - bufp);
178 inlen -= endp - bufp;
181 size_t outlen = inlen;
182 ifdebug(META) logger(LOG_DEBUG, "Received encrypted %d bytes", inlen);
184 if(!cipher_decrypt(&c->incipher, bufp, inlen, buffer_prepare(&c->inbuf, inlen), &outlen, false) || inlen != outlen) {
185 logger(LOG_ERR, "Error while decrypting metadata from %s (%s)",
186 c->name, c->hostname);
193 while(c->inbuf.len) {
194 /* Are we receiving a TCPpacket? */
197 char *tcpbuffer = buffer_read(&c->inbuf, c->tcplen);
199 receive_tcppacket(c, tcpbuffer, c->tcplen);
207 /* Otherwise we are waiting for a request */
209 char *request = buffer_readline(&c->inbuf);
211 bool result = receive_request(c, request);