TCPonly now works (in a relatively clean way too).
[tinc] / src / meta.c
1 /*
2     meta.c -- handle the meta communication
3     Copyright (C) 2000,2001 Guus Sliepen <guus@sliepen.warande.net>,
4                   2000,2001 Ivo Timmermans <itimmermans@bigfoot.com>
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.18 2001/05/25 11:54:28 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 <sys/signal.h>
30 #include <unistd.h>
31 #include <string.h>
32 /* This line must be below the rest for FreeBSD */
33 #include <sys/socket.h>
34
35 #ifdef HAVE_OPENSSL_EVP_H
36 # include <openssl/evp.h>
37 #else
38 # include <evp.h>
39 #endif
40
41 #include "net.h"
42 #include "connection.h"
43 #include "system.h"
44 #include "protocol.h"
45
46 int send_meta(connection_t *cl, char *buffer, int length)
47 {
48   char *bufp;
49   int outlen;
50   char outbuf[MAXBUFSIZE];
51 cp
52   if(debug_lvl >= DEBUG_META)
53     syslog(LOG_DEBUG, _("Sending %d bytes of metadata to %s (%s)"), length,
54            cl->name, cl->hostname);
55
56   if(cl->status.encryptout)
57     {
58       EVP_EncryptUpdate(cl->cipher_outctx, outbuf, &outlen, buffer, length);
59       bufp = outbuf;
60       length = outlen;
61     }
62   else
63       bufp = buffer;
64
65   if(write(cl->meta_socket, bufp, length) < 0)
66     {
67       syslog(LOG_ERR, _("Sending meta data to %s (%s) failed: %m"), cl->name, cl->hostname);
68       return -1;
69     }
70 cp
71   return 0;
72 }
73
74 void broadcast_meta(connection_t *cl, char *buffer, int length)
75 {
76   avl_node_t *node;
77   connection_t *p;
78 cp
79   for(node = connection_tree->head; node; node = node->next)
80     {
81       p = (connection_t *)node->data;
82       if(p != cl && p->status.meta && p->status.active)
83         send_meta(p, buffer, length);
84     }
85 cp
86 }
87
88 int receive_meta(connection_t *cl)
89 {
90   int x, l = sizeof(x);
91   int oldlen, i;
92   int lenin, reqlen;
93   int decrypted = 0;
94   char inbuf[MAXBUFSIZE];
95 cp
96   if(getsockopt(cl->meta_socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
97     {
98       syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%m %s (%s)"), __FILE__, __LINE__, cl->meta_socket,
99              cl->name, cl->hostname);
100       return -1;
101     }
102   if(x)
103     {
104       syslog(LOG_ERR, _("Metadata socket error for %s (%s): %s"),
105              cl->name, cl->hostname, strerror(x));
106       return -1;
107     }
108
109   /* Strategy:
110      - Read as much as possible from the TCP socket in one go.
111      - Decrypt it.
112      - Check if a full request is in the input buffer.
113        - If yes, process request and remove it from the buffer,
114          then check again.
115        - If not, keep stuff in buffer and exit.
116    */
117
118   lenin = read(cl->meta_socket, cl->buffer + cl->buflen, MAXBUFSIZE - cl->buflen);
119
120   if(lenin<=0)
121     {
122       if(lenin==0)
123         {
124           if(debug_lvl >= DEBUG_CONNECTIONS)
125             syslog(LOG_NOTICE, _("Connection closed by %s (%s)"),
126                 cl->name, cl->hostname);
127         }
128       else
129         if(errno==EINTR)
130           return 0;      
131         else
132           syslog(LOG_ERR, _("Metadata socket read error for %s (%s): %m"),
133                  cl->name, cl->hostname);
134
135       return -1;
136     }
137
138   oldlen = cl->buflen;
139   cl->buflen += lenin;
140
141   while(lenin)
142     {
143       /* Decrypt */
144
145       if(cl->status.decryptin && !decrypted)
146         {
147           EVP_DecryptUpdate(cl->cipher_inctx, inbuf, &lenin, cl->buffer + oldlen, lenin);
148           memcpy(cl->buffer + oldlen, inbuf, lenin);
149           decrypted = 1;
150         }
151
152       /* Are we receiving a TCPpacket? */
153
154       if(cl->tcplen)
155         {
156           if(cl->tcplen <= cl->buflen)
157             {
158               receive_tcppacket(cl, cl->buffer, cl->tcplen);
159
160               cl->buflen -= cl->tcplen;
161               lenin -= cl->tcplen;
162               memmove(cl->buffer, cl->buffer + cl->tcplen, cl->buflen);
163               oldlen = 0;
164               cl->tcplen = 0;
165               continue;
166             }
167           else
168             {
169               break;
170             }
171         }
172
173       /* Otherwise we are waiting for a request */
174
175       reqlen = 0;
176
177       for(i = oldlen; i < cl->buflen; i++)
178         {
179           if(cl->buffer[i] == '\n')
180             {
181               cl->buffer[i] = '\0';  /* replace end-of-line by end-of-string so we can use sscanf */
182               reqlen = i + 1;
183               break;
184             }
185         }
186
187       if(reqlen)
188         {
189           if(receive_request(cl))
190             return -1;
191
192           cl->buflen -= reqlen;
193           lenin -= reqlen;
194           memmove(cl->buffer, cl->buffer + reqlen, cl->buflen);
195           oldlen = 0;
196           continue;
197         }
198       else
199         {
200           break;
201         }
202     }
203
204   if(cl->buflen >= MAXBUFSIZE)
205     {
206       syslog(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
207              cl->name, cl->hostname);
208       return -1;
209     }
210
211   cl->last_ping_time = time(NULL);
212 cp  
213   return 0;
214 }