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