Only show meta connection related debug messages when debug level >= 4
[tinc] / src / meta.c
1 /*
2     meta.c -- handle the meta communication
3     Copyright (C) 2000-2006 Guus Sliepen <guus@tinc-vpn.org>,
4                   2000-2005 Ivo Timmermans
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id$
21 */
22
23 #include "system.h"
24
25 #include <openssl/err.h>
26 #include <openssl/evp.h>
27
28 #include "splay_tree.h"
29 #include "connection.h"
30 #include "logger.h"
31 #include "meta.h"
32 #include "net.h"
33 #include "protocol.h"
34 #include "utils.h"
35 #include "xalloc.h"
36
37 bool send_meta(connection_t *c, const char *buffer, int length) {
38         int outlen;
39         int result;
40         cp();
41
42         ifdebug(META) logger(LOG_DEBUG, _("Sending %d bytes of metadata to %s (%s)"), length,
43                            c->name, c->hostname);
44
45         /* Add our data to buffer */
46         if(c->status.encryptout) {
47                 char outbuf[length];
48
49                 result = EVP_EncryptUpdate(c->outctx, (unsigned char *)outbuf, &outlen, (unsigned char *)buffer, length);
50                 if(!result || outlen != length) {
51                         logger(LOG_ERR, _("Error while encrypting metadata to %s (%s): %s"),
52                                         c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
53                         return false;
54                 }
55                 
56                 ifdebug(META) logger(LOG_DEBUG, _("Encrypted write %p %p %p %d"), c, c->buffer, outbuf, length);
57                 bufferevent_write(c->buffer, (void *)outbuf, length);
58                 ifdebug(META) logger(LOG_DEBUG, _("Done."));
59         } else {
60                 ifdebug(META) logger(LOG_DEBUG, _("Unencrypted write %p %p %p %d"), c, c->buffer, buffer, length);
61                 bufferevent_write(c->buffer, (void *)buffer, length);
62                 ifdebug(META) logger(LOG_DEBUG, _("Done."));
63         }
64
65         return true;
66 }
67
68 void broadcast_meta(connection_t *from, const char *buffer, int length) {
69         splay_node_t *node;
70         connection_t *c;
71
72         cp();
73
74         for(node = connection_tree->head; node; node = node->next) {
75                 c = node->data;
76
77                 if(c != from && c->status.active)
78                         send_meta(c, buffer, length);
79         }
80 }
81
82 bool receive_meta(connection_t *c) {
83         int result, inlen, outlen;
84         char inbuf[MAXBUFSIZE];
85         char *bufp = inbuf, *endp;
86
87         cp();
88
89         /* Strategy:
90            - Read as much as possible from the TCP socket in one go.
91            - Decrypt it.
92            - Check if a full request is in the input buffer.
93            - If yes, process request and remove it from the buffer,
94            then check again.
95            - If not, keep stuff in buffer and exit.
96          */
97
98         inlen = recv(c->socket, inbuf, sizeof inbuf, 0);
99
100         if(inlen <= 0) {
101                 logger(LOG_ERR, _("Receive callback called for %s (%s) but no data to receive: %s"), c->name, c->hostname, strerror(errno));
102                 return false;
103         }
104
105         do {
106                 if(!c->status.decryptin) {
107                         endp = memchr(bufp, '\n', inlen);
108                         if(endp)
109                                 endp++;
110                         else
111                                 endp = bufp + inlen;
112
113                         evbuffer_add(c->buffer->input, bufp, endp - bufp);
114
115                         inlen -= endp - bufp;
116                         bufp = endp;
117                 } else {
118                         ifdebug(META) logger(LOG_DEBUG, _("Received encrypted %d bytes"), inlen);
119                         evbuffer_expand(c->buffer->input, c->buffer->input->off + inlen);
120                         result = EVP_DecryptUpdate(c->inctx, (unsigned char *)c->buffer->input->buffer + c->buffer->input->off, &outlen, (unsigned char *)bufp, inlen);
121                         if(!result || outlen != inlen) {
122                                 logger(LOG_ERR, _("Error while decrypting metadata from %s (%s): %s"),
123                                            c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
124                                 return false;
125                         }
126                         c->buffer->input->off += inlen;
127
128                         inlen = 0;
129                 }
130
131                 while(c->buffer->input->off) {
132                         /* Are we receiving a TCPpacket? */
133
134                         if(c->tcplen) {
135                                 if(c->tcplen <= c->buffer->input->off) {
136                                         receive_tcppacket(c, (char *)c->buffer->input->buffer, c->tcplen);
137                                         evbuffer_drain(c->buffer->input, c->tcplen);
138                                         c->tcplen = 0;
139                                         continue;
140                                 } else {
141                                         break;
142                                 }
143                         }
144
145                         /* Otherwise we are waiting for a request */
146
147                         char *request = evbuffer_readline(c->buffer->input);
148                         if(request) {
149                                 receive_request(c, request);
150                                 free(request);
151                                 continue;
152                         } else {
153                                 break;
154                         }
155                 }
156         } while(inlen);
157
158         c->last_ping_time = time(NULL);
159
160         return true;
161 }