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