Remove forgotten braces.
[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.38 2003/07/29 22:59:00 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 "net.h"
31 #include "protocol.h"
32 #include "system.h"
33 #include "utils.h"
34
35 bool send_meta(connection_t *c, char *buffer, int length)
36 {
37         char *bufp;
38         int outlen;
39         char outbuf[MAXBUFSIZE];
40         int result;
41
42         cp();
43
44         ifdebug(META) logger(LOG_DEBUG, _("Sending %d bytes of metadata to %s (%s)"), length,
45                            c->name, c->hostname);
46
47         if(c->status.encryptout) {
48                 EVP_EncryptUpdate(c->outctx, outbuf, &outlen, buffer, length);
49                 bufp = outbuf;
50                 length = outlen;
51         } else
52                 bufp = buffer;
53
54         while(length) {
55                 result = send(c->socket, bufp, length, 0);
56                 if(result <= 0) {
57                         if(errno == EINTR)
58                                 continue;
59                         logger(LOG_ERR, _("Sending meta data to %s (%s) failed: %s"), c->name,
60                                    c->hostname, strerror(errno));
61                         return false;
62                 }
63                 bufp += result;
64                 length -= result;
65         }
66         
67         return true;
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 bool receive_meta(connection_t *c)
86 {
87         int x;
88         socklen_t l = sizeof(x);
89         int oldlen, i;
90         int lenin, reqlen;
91         bool decrypted = false;
92         char inbuf[MAXBUFSIZE];
93
94         cp();
95
96         if(getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0) {
97                 logger(LOG_ERR, _("This is a bug: %s:%d: %d:%s %s (%s)"), __FILE__,
98                            __LINE__, c->socket, strerror(errno), c->name, c->hostname);
99                 return false;
100         }
101
102         if(x) {
103                 logger(LOG_ERR, _("Metadata socket error for %s (%s): %s"),
104                            c->name, c->hostname, strerror(x));
105                 return false;
106         }
107
108         /* Strategy:
109            - Read as much as possible from the TCP socket in one go.
110            - Decrypt it.
111            - Check if a full request is in the input buffer.
112            - If yes, process request and remove it from the buffer,
113            then check again.
114            - If not, keep stuff in buffer and exit.
115          */
116
117         lenin = recv(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen, 0);
118
119         if(lenin <= 0) {
120                 if(lenin == 0) {
121                         ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
122                                            c->name, c->hostname);
123                 } else if(errno == EINTR)
124                         return true;
125                 else
126                         logger(LOG_ERR, _("Metadata socket read error for %s (%s): %s"),
127                                    c->name, c->hostname, strerror(errno));
128
129                 return false;
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, lenin);
140                         memcpy(c->buffer + oldlen, inbuf, lenin);
141                         decrypted = true;
142                 }
143
144                 /* Are we receiving a TCPpacket? */
145
146                 if(c->tcplen) {
147                         if(c->tcplen <= c->buflen) {
148                                 receive_tcppacket(c, c->buffer, c->tcplen);
149
150                                 c->buflen -= c->tcplen;
151                                 lenin -= c->tcplen;
152                                 memmove(c->buffer, c->buffer + c->tcplen, c->buflen);
153                                 oldlen = 0;
154                                 c->tcplen = 0;
155                                 continue;
156                         } else {
157                                 break;
158                         }
159                 }
160
161                 /* Otherwise we are waiting for a request */
162
163                 reqlen = 0;
164
165                 for(i = oldlen; i < c->buflen; i++) {
166                         if(c->buffer[i] == '\n') {
167                                 c->buffer[i] = '\0';    /* replace end-of-line by end-of-string so we can use sscanf */
168                                 reqlen = i + 1;
169                                 break;
170                         }
171                 }
172
173                 if(reqlen) {
174                         c->reqlen = reqlen;
175                         if(!receive_request(c))
176                                 return false;
177
178                         c->buflen -= reqlen;
179                         lenin -= reqlen;
180                         memmove(c->buffer, c->buffer + reqlen, c->buflen);
181                         oldlen = 0;
182                         continue;
183                 } else {
184                         break;
185                 }
186         }
187
188         if(c->buflen >= MAXBUFSIZE) {
189                 logger(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
190                            c->name, c->hostname);
191                 return false;
192         }
193
194         c->last_ping_time = now;
195
196         return true;
197 }