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