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