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, uint8_t type, const char *buffer, size_t length) {
35 connection_t *c = handle;
38 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta_sptps() called with NULL pointer!");
42 buffer_add(&c->outbuf, buffer, length);
43 event_add(&c->outevent, NULL);
48 bool send_meta(connection_t *c, const char *buffer, int length) {
50 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta() called with NULL pointer!");
54 logger(DEBUG_META, LOG_DEBUG, "Sending %d bytes of metadata to %s (%s)", length,
55 c->name, c->hostname);
57 if(c->protocol_minor >= 2)
58 return sptps_send_record(&c->sptps, 0, buffer, length);
60 /* Add our data to buffer */
61 if(c->status.encryptout) {
62 size_t outlen = length;
64 if(!cipher_encrypt(&c->outcipher, buffer, length, buffer_prepare(&c->outbuf, length), &outlen, false) || outlen != length) {
65 logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting metadata to %s (%s)",
66 c->name, c->hostname);
71 buffer_add(&c->outbuf, buffer, length);
74 event_add(&c->outevent, NULL);
79 void broadcast_meta(connection_t *from, const char *buffer, int length) {
83 for(node = connection_tree->head; node; node = node->next) {
86 if(c != from && c->status.active)
87 send_meta(c, buffer, length);
91 bool receive_meta_sptps(void *handle, uint8_t type, const char *data, uint16_t length) {
92 connection_t *c = handle;
95 logger(DEBUG_ALWAYS, LOG_ERR, "receive_meta_sptps() called with NULL pointer!");
99 if(type == SPTPS_HANDSHAKE) {
100 if(c->allow_request == ACK)
109 /* Are we receiving a TCPpacket? */
112 if(length != c->tcplen)
114 receive_tcppacket(c, data, length);
119 /* Otherwise we are waiting for a request */
121 return receive_request(c, data);
124 bool receive_meta(connection_t *c) {
126 char inbuf[MAXBUFSIZE];
127 char *bufp = inbuf, *endp;
130 - Read as much as possible from the TCP socket in one go.
132 - Check if a full request is in the input buffer.
133 - If yes, process request and remove it from the buffer,
135 - If not, keep stuff in buffer and exit.
138 buffer_compact(&c->inbuf, MAXBUFSIZE);
140 if(sizeof inbuf <= c->inbuf.len) {
141 logger(DEBUG_ALWAYS, LOG_ERR, "Input buffer full for %s (%s)", c->name, c->hostname);
145 inlen = recv(c->socket, inbuf, sizeof inbuf - c->inbuf.len, 0);
148 if(!inlen || !errno) {
149 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection closed by %s (%s)",
150 c->name, c->hostname);
151 } else if(sockwouldblock(sockerrno))
154 logger(DEBUG_ALWAYS, LOG_ERR, "Metadata socket read error for %s (%s): %s",
155 c->name, c->hostname, sockstrerror(sockerrno));
160 if(c->protocol_minor >= 2)
161 return sptps_receive_data(&c->sptps, bufp, inlen);
163 if(!c->status.decryptin) {
164 endp = memchr(bufp, '\n', inlen);
170 buffer_add(&c->inbuf, bufp, endp - bufp);
172 inlen -= endp - bufp;
175 size_t outlen = inlen;
177 if(!cipher_decrypt(&c->incipher, bufp, inlen, buffer_prepare(&c->inbuf, inlen), &outlen, false) || inlen != outlen) {
178 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting metadata from %s (%s)",
179 c->name, c->hostname);
186 while(c->inbuf.len) {
187 /* Are we receiving a TCPpacket? */
190 char *tcpbuffer = buffer_read(&c->inbuf, c->tcplen);
192 if(proxytype == PROXY_SOCKS4 && c->allow_request == ID) {
193 if(tcpbuffer[0] == 0 && tcpbuffer[1] == 0x5a) {
194 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
196 logger(DEBUG_CONNECTIONS, LOG_ERR, "Proxy request rejected");
200 receive_tcppacket(c, tcpbuffer, c->tcplen);
208 /* Otherwise we are waiting for a request */
210 char *request = buffer_readline(&c->inbuf);
212 bool result = receive_request(c, request);