Update copyright notices.
[tinc] / src / meta.c
1 /*
2     meta.c -- handle the meta communication
3     Copyright (C) 2000-2005 Guus Sliepen <guus@tinc-vpn.org>,
4                   2000-2005 Ivo Timmermans <ivo@tinc-vpn.org>
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 "avl_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
36 bool send_meta(connection_t *c, const char *buffer, int length)
37 {
38         const char *bufp;
39         int outlen;
40         char outbuf[MAXBUFSIZE];
41         int result;
42
43         cp();
44
45         ifdebug(META) logger(LOG_DEBUG, _("Sending %d bytes of metadata to %s (%s)"), length,
46                            c->name, c->hostname);
47
48         if(c->status.encryptout) {
49                 result = EVP_EncryptUpdate(c->outctx, outbuf, &outlen, 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                 bufp = outbuf;
56                 length = outlen;
57         } else
58                 bufp = buffer;
59
60         while(length) {
61                 result = send(c->socket, bufp, length, 0);
62                 if(result <= 0) {
63                         if(!errno || errno == EPIPE) {
64                                 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
65                                                    c->name, c->hostname);
66                         } else if(errno == EINTR)
67                                 continue;
68                         else
69                                 logger(LOG_ERR, _("Sending meta data to %s (%s) failed: %s"), c->name,
70                                            c->hostname, strerror(errno));
71                         return false;
72                 }
73                 bufp += result;
74                 length -= result;
75         }
76         
77         return true;
78 }
79
80 void broadcast_meta(connection_t *from, const char *buffer, int length)
81 {
82         avl_node_t *node;
83         connection_t *c;
84
85         cp();
86
87         for(node = connection_tree->head; node; node = node->next) {
88                 c = node->data;
89
90                 if(c != from && c->status.active)
91                         send_meta(c, buffer, length);
92         }
93 }
94
95 bool receive_meta(connection_t *c)
96 {
97         int oldlen, i, result;
98         int lenin, lenout, reqlen;
99         bool decrypted = false;
100         char inbuf[MAXBUFSIZE];
101
102         cp();
103
104         /* Strategy:
105            - Read as much as possible from the TCP socket in one go.
106            - Decrypt it.
107            - Check if a full request is in the input buffer.
108            - If yes, process request and remove it from the buffer,
109            then check again.
110            - If not, keep stuff in buffer and exit.
111          */
112
113         lenin = recv(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen, 0);
114
115         if(lenin <= 0) {
116                 if(!lenin || !errno) {
117                         ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
118                                            c->name, c->hostname);
119                 } else if(errno == EINTR)
120                         return true;
121                 else
122                         logger(LOG_ERR, _("Metadata socket read error for %s (%s): %s"),
123                                    c->name, c->hostname, strerror(errno));
124
125                 return false;
126         }
127
128         oldlen = c->buflen;
129         c->buflen += lenin;
130
131         while(lenin > 0) {
132                 /* Decrypt */
133
134                 if(c->status.decryptin && !decrypted) {
135                         result = EVP_DecryptUpdate(c->inctx, inbuf, &lenout, c->buffer + oldlen, lenin);
136                         if(!result || lenout != lenin) {
137                                 logger(LOG_ERR, _("Error while decrypting metadata from %s (%s): %s"),
138                                                 c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
139                                 return false;
140                         }
141                         memcpy(c->buffer + oldlen, inbuf, lenin);
142                         decrypted = true;
143                 }
144
145                 /* Are we receiving a TCPpacket? */
146
147                 if(c->tcplen) {
148                         if(c->tcplen <= c->buflen) {
149                                 receive_tcppacket(c, c->buffer, c->tcplen);
150
151                                 c->buflen -= c->tcplen;
152                                 lenin -= c->tcplen - oldlen;
153                                 memmove(c->buffer, c->buffer + c->tcplen, c->buflen);
154                                 oldlen = 0;
155                                 c->tcplen = 0;
156                                 continue;
157                         } else {
158                                 break;
159                         }
160                 }
161
162                 /* Otherwise we are waiting for a request */
163
164                 reqlen = 0;
165
166                 for(i = oldlen; i < c->buflen; i++) {
167                         if(c->buffer[i] == '\n') {
168                                 c->buffer[i] = '\0';    /* replace end-of-line by end-of-string so we can use sscanf */
169                                 reqlen = i + 1;
170                                 break;
171                         }
172                 }
173
174                 if(reqlen) {
175                         c->reqlen = reqlen;
176                         if(!receive_request(c))
177                                 return false;
178
179                         c->buflen -= reqlen;
180                         lenin -= reqlen - oldlen;
181                         memmove(c->buffer, c->buffer + reqlen, c->buflen);
182                         oldlen = 0;
183                         continue;
184                 } else {
185                         break;
186                 }
187         }
188
189         if(c->buflen >= MAXBUFSIZE) {
190                 logger(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
191                            c->name, c->hostname);
192                 return false;
193         }
194
195         c->last_ping_time = now;
196
197         return true;
198 }