Small fixes.
[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.41 2003/08/12 14:48:12 guus Exp $
21 */
22
23 #include "system.h"
24
25 #include <openssl/evp.h>
26
27 #include "avl_tree.h"
28 #include "connection.h"
29 #include "logger.h"
30 #include "meta.h"
31 #include "net.h"
32 #include "protocol.h"
33 #include "system.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                 EVP_EncryptUpdate(c->outctx, outbuf, &outlen, buffer, length);
50                 bufp = outbuf;
51                 length = outlen;
52         } else
53                 bufp = buffer;
54
55         while(length) {
56                 result = send(c->socket, bufp, length, 0);
57                 if(result <= 0) {
58                         if(!errno || errno == EPIPE) {
59                                 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
60                                                    c->name, c->hostname);
61                         } else if(errno == EINTR)
62                                 continue;
63                         else
64                                 logger(LOG_ERR, _("Sending meta data to %s (%s) failed: %s"), c->name,
65                                            c->hostname, strerror(errno));
66                         return false;
67                 }
68                 bufp += result;
69                 length -= result;
70         }
71         
72         return true;
73 }
74
75 void broadcast_meta(connection_t *from, const char *buffer, int length)
76 {
77         avl_node_t *node;
78         connection_t *c;
79
80         cp();
81
82         for(node = connection_tree->head; node; node = node->next) {
83                 c = (connection_t *) node->data;
84
85                 if(c != from && c->status.active)
86                         send_meta(c, buffer, length);
87         }
88 }
89
90 bool receive_meta(connection_t *c)
91 {
92         int x;
93         socklen_t l = sizeof(x);
94         int oldlen, i;
95         int lenin, reqlen;
96         bool decrypted = false;
97         char inbuf[MAXBUFSIZE];
98
99         cp();
100
101         if(getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0) {
102                 logger(LOG_ERR, _("This is a bug: %s:%d: %d:%s %s (%s)"), __FILE__,
103                            __LINE__, c->socket, strerror(errno), c->name, c->hostname);
104                 return false;
105         }
106
107         if(x) {
108                 logger(LOG_ERR, _("Metadata socket error for %s (%s): %s"),
109                            c->name, c->hostname, strerror(x));
110                 return false;
111         }
112
113         /* Strategy:
114            - Read as much as possible from the TCP socket in one go.
115            - Decrypt it.
116            - Check if a full request is in the input buffer.
117            - If yes, process request and remove it from the buffer,
118            then check again.
119            - If not, keep stuff in buffer and exit.
120          */
121
122         lenin = recv(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen, 0);
123
124         if(lenin <= 0) {
125                 if(!lenin || !errno) {
126                         ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
127                                            c->name, c->hostname);
128                 } else if(errno == EINTR)
129                         return true;
130                 else
131                         logger(LOG_ERR, _("Metadata socket read error for %s (%s): %s"),
132                                    c->name, c->hostname, strerror(errno));
133
134                 return false;
135         }
136
137         oldlen = c->buflen;
138         c->buflen += lenin;
139
140         while(lenin) {
141                 /* Decrypt */
142
143                 if(c->status.decryptin && !decrypted) {
144                         EVP_DecryptUpdate(c->inctx, inbuf, &lenin, c->buffer + oldlen, lenin);
145                         memcpy(c->buffer + oldlen, inbuf, lenin);
146                         decrypted = true;
147                 }
148
149                 /* Are we receiving a TCPpacket? */
150
151                 if(c->tcplen) {
152                         if(c->tcplen <= c->buflen) {
153                                 receive_tcppacket(c, c->buffer, c->tcplen);
154
155                                 c->buflen -= c->tcplen;
156                                 lenin -= c->tcplen;
157                                 memmove(c->buffer, c->buffer + c->tcplen, c->buflen);
158                                 oldlen = 0;
159                                 c->tcplen = 0;
160                                 continue;
161                         } else {
162                                 break;
163                         }
164                 }
165
166                 /* Otherwise we are waiting for a request */
167
168                 reqlen = 0;
169
170                 for(i = oldlen; i < c->buflen; i++) {
171                         if(c->buffer[i] == '\n') {
172                                 c->buffer[i] = '\0';    /* replace end-of-line by end-of-string so we can use sscanf */
173                                 reqlen = i + 1;
174                                 break;
175                         }
176                 }
177
178                 if(reqlen) {
179                         c->reqlen = reqlen;
180                         if(!receive_request(c))
181                                 return false;
182
183                         c->buflen -= reqlen;
184                         lenin -= reqlen;
185                         memmove(c->buffer, c->buffer + reqlen, c->buflen);
186                         oldlen = 0;
187                         continue;
188                 } else {
189                         break;
190                 }
191         }
192
193         if(c->buflen >= MAXBUFSIZE) {
194                 logger(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
195                            c->name, c->hostname);
196                 return false;
197         }
198
199         c->last_ping_time = now;
200
201         return true;
202 }