fa475dfb758d32f3c69a4d201f0125c5d230bf9c
[tinc] / src / meta.c
1 /*
2     meta.c -- handle the meta communication
3     Copyright (C) 2000-2002 Guus Sliepen <guus@sliepen.eu.org>,
4                   2000-2002 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.29 2002/09/09 21:24:34 guus Exp $
21 */
22
23 #include "config.h"
24 #include <utils.h>
25 #include <avl_tree.h>
26
27 #include <errno.h>
28 #include <syslog.h>
29 #include <unistd.h>
30 #include <string.h>
31 /* This line must be below the rest for FreeBSD */
32 #include <sys/types.h>
33 #include <sys/socket.h>
34
35 #include <openssl/evp.h>
36
37 #include "net.h"
38 #include "connection.h"
39 #include "system.h"
40 #include "protocol.h"
41
42 int send_meta(connection_t * c, char *buffer, int length)
43 {
44         char *bufp;
45         int outlen;
46         char outbuf[MAXBUFSIZE];
47
48         cp();
49
50         if(debug_lvl >= DEBUG_META)
51                 syslog(LOG_DEBUG, _("Sending %d bytes of metadata to %s (%s)"), length,
52                            c->name, c->hostname);
53
54         if(c->status.encryptout) {
55                 EVP_EncryptUpdate(c->outctx, outbuf, &outlen, buffer, length);
56                 bufp = outbuf;
57                 length = outlen;
58         } else
59                 bufp = buffer;
60
61         if(write(c->socket, bufp, length) < 0) {
62                 syslog(LOG_ERR, _("Sending meta data to %s (%s) failed: %s"), c->name,
63                            c->hostname, strerror(errno));
64                 return -1;
65         }
66
67         return 0;
68 }
69
70 void broadcast_meta(connection_t * from, char *buffer, int length)
71 {
72         avl_node_t *node;
73         connection_t *c;
74
75         cp();
76
77         for(node = connection_tree->head; node; node = node->next) {
78                 c = (connection_t *) node->data;
79
80                 if(c != from && c->status.active)
81                         send_meta(c, buffer, length);
82         }
83 }
84
85 int receive_meta(connection_t * c)
86 {
87         int x, l = sizeof(x);
88         int oldlen, i;
89         int lenin, reqlen;
90         int decrypted = 0;
91         char inbuf[MAXBUFSIZE];
92
93         cp();
94
95         if(getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0) {
96                 syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%s %s (%s)"), __FILE__,
97                            __LINE__, c->socket, strerror(errno), c->name, c->hostname);
98                 return -1;
99         }
100
101         if(x) {
102                 syslog(LOG_ERR, _("Metadata socket error for %s (%s): %s"),
103                            c->name, c->hostname, strerror(x));
104                 return -1;
105         }
106
107         /* Strategy:
108            - Read as much as possible from the TCP socket in one go.
109            - Decrypt it.
110            - Check if a full request is in the input buffer.
111            - If yes, process request and remove it from the buffer,
112            then check again.
113            - If not, keep stuff in buffer and exit.
114          */
115
116         lenin = read(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen);
117
118         if(lenin <= 0) {
119                 if(lenin == 0) {
120                         if(debug_lvl >= DEBUG_CONNECTIONS)
121                                 syslog(LOG_NOTICE, _("Connection closed by %s (%s)"),
122                                            c->name, c->hostname);
123                 } else if(errno == EINTR)
124                         return 0;
125                 else
126                         syslog(LOG_ERR, _("Metadata socket read error for %s (%s): %s"),
127                                    c->name, c->hostname, strerror(errno));
128
129                 return -1;
130         }
131
132         oldlen = c->buflen;
133         c->buflen += lenin;
134
135         while(lenin) {
136                 /* Decrypt */
137
138                 if(c->status.decryptin && !decrypted) {
139                         EVP_DecryptUpdate(c->inctx, inbuf, &lenin, c->buffer + oldlen,
140                                                           lenin);
141                         memcpy(c->buffer + oldlen, inbuf, lenin);
142                         decrypted = 1;
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;
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 -1;
178
179                         c->buflen -= reqlen;
180                         lenin -= reqlen;
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                 syslog(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
191                            c->name, c->hostname);
192                 return -1;
193         }
194
195         c->last_ping_time = now;
196
197         return 0;
198 }