Moving files, first attempt at gcrypt compatibility, more interface
[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.4 2002/04/28 12:46:26 zarq Exp $
21 */
22
23 #include "config.h"
24 #include <utils.h>
25 #include <avl_tree.h>
26
27 #include <errno.h>
28 #include <unistd.h>
29 #include <string.h>
30 /* This line must be below the rest for FreeBSD */
31 #include <sys/types.h>
32 #include <sys/socket.h>
33
34 #include <openssl/evp.h>
35
36 #include "net.h"
37 #include "connection.h"
38 #include "system.h"
39 #include "protocol.h"
40 #include "logging.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 #ifdef USE_OPENSSL
55       EVP_EncryptUpdate(c->outctx, outbuf, &outlen, buffer, length);
56 #endif
57 #ifdef USE_GCRYPT
58       outlen = gcry_cipher_encrypt(c->outctx, outbuf, sizeof(outbuf), buffer, length);
59 #endif
60       bufp = outbuf;
61       length = outlen;
62     }
63   else
64       bufp = buffer;
65
66   if(write(c->socket, bufp, length) < 0)
67     {
68       syslog(LOG_ERR, _("Sending meta data to %s (%s) failed: %s"), c->name, c->hostname, strerror(errno));
69       return -1;
70     }
71 cp
72   return 0;
73 }
74
75 void broadcast_meta(connection_t *from, char *buffer, int length)
76 {
77   avl_node_t *node;
78   connection_t *c;
79 cp
80   for(node = connection_tree->head; node; node = node->next)
81     {
82       c = (connection_t *)node->data;
83       if(c != from && c->status.active)
84         send_meta(c, buffer, length);
85     }
86 cp
87 }
88
89 int receive_meta(connection_t *c)
90 {
91   int x, l = sizeof(x);
92   int oldlen, i;
93   int lenin, reqlen;
94   int decrypted = 0;
95   char inbuf[MAXBUFSIZE];
96 cp
97   if(getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
98     {
99       syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%s %s (%s)"), __FILE__, __LINE__, c->socket, strerror(errno),
100              c->name, c->hostname);
101       return -1;
102     }
103   if(x)
104     {
105       syslog(LOG_ERR, _("Metadata socket error for %s (%s): %s"),
106              c->name, c->hostname, strerror(x));
107       return -1;
108     }
109
110   /* Strategy:
111      - Read as much as possible from the TCP socket in one go.
112      - Decrypt it.
113      - Check if a full request is in the input buffer.
114        - If yes, process request and remove it from the buffer,
115          then check again.
116        - If not, keep stuff in buffer and exit.
117    */
118
119   lenin = read(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen);
120
121   if(lenin<=0)
122     {
123       if(lenin==0)
124         {
125           if(debug_lvl >= DEBUG_CONNECTIONS)
126             syslog(LOG_NOTICE, _("Connection closed by %s (%s)"),
127                 c->name, c->hostname);
128         }
129       else
130         if(errno==EINTR)
131           return 0;      
132         else
133           syslog(LOG_ERR, _("Metadata socket read error for %s (%s): %s"),
134                  c->name, c->hostname, strerror(errno));
135
136       return -1;
137     }
138
139   oldlen = c->buflen;
140   c->buflen += lenin;
141
142   while(lenin)
143     {
144       /* Decrypt */
145
146       if(c->status.decryptin && !decrypted)
147         {
148 #ifdef USE_OPENSSL
149           EVP_DecryptUpdate(c->inctx, inbuf, &lenin, c->buffer + oldlen, lenin);
150 #endif
151 #ifdef USE_GCRYPT
152           lenin = gcry_cipher_decrypt(c->inctx, inbuf, sizeof(inbuf), c->buffer + oldlen, lenin);
153 #endif
154           memcpy(c->buffer + oldlen, inbuf, lenin);
155           decrypted = 1;
156         }
157
158       /* Are we receiving a TCPpacket? */
159
160       if(c->tcplen)
161         {
162           if(c->tcplen <= c->buflen)
163             {
164               receive_tcppacket(c, c->buffer, c->tcplen);
165
166               c->buflen -= c->tcplen;
167               lenin -= c->tcplen;
168               memmove(c->buffer, c->buffer + c->tcplen, c->buflen);
169               oldlen = 0;
170               c->tcplen = 0;
171               continue;
172             }
173           else
174             {
175               break;
176             }
177         }
178
179       /* Otherwise we are waiting for a request */
180
181       reqlen = 0;
182
183       for(i = oldlen; i < c->buflen; i++)
184         {
185           if(c->buffer[i] == '\n')
186             {
187               c->buffer[i] = '\0';  /* replace end-of-line by end-of-string so we can use sscanf */
188               reqlen = i + 1;
189               break;
190             }
191         }
192
193       if(reqlen)
194         {
195           if(receive_request(c))
196             return -1;
197
198           c->buflen -= reqlen;
199           lenin -= reqlen;
200           memmove(c->buffer, c->buffer + reqlen, c->buflen);
201           oldlen = 0;
202           continue;
203         }
204       else
205         {
206           break;
207         }
208     }
209
210   if(c->buflen >= MAXBUFSIZE)
211     {
212       syslog(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
213              c->name, c->hostname);
214       return -1;
215     }
216
217   c->last_ping_time = now;
218 cp  
219   return 0;
220 }