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