- Integrate rbl trees into tinc.
[tinc] / src / meta.c
1 /*
2     meta.c -- handle the meta communication
3     Copyright (C) 2000 Guus Sliepen <guus@sliepen.warande.net>,
4                   2000 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.12 2000/11/20 19:12:12 guus Exp $
21 */
22
23 #include "config.h"
24 #include <utils.h>
25
26 #include <errno.h>
27 #include <syslog.h>
28 #include <sys/signal.h>
29 #include <unistd.h>
30 #include <string.h>
31 /* This line must be below the rest for FreeBSD */
32 #include <sys/socket.h>
33
34 #ifdef HAVE_OPENSSL_EVP_H
35 # include <openssl/evp.h>
36 #else
37 # include <evp.h>
38 #endif
39
40 #include "net.h"
41 #include "connection.h"
42 #include "system.h"
43 #include "protocol.h"
44
45 int send_meta(connection_t *cl, char *buffer, int length)
46 {
47   char outbuf[MAXBUFSIZE];
48   char *bufp;
49   int outlen;
50 cp
51   if(debug_lvl >= DEBUG_META)
52     syslog(LOG_DEBUG, _("Sending %d bytes of metadata to %s (%s): %s"), length,
53            cl->name, cl->hostname, buffer);
54
55   buffer[length-1]='\n';
56
57   if(cl->status.encryptout)
58     {
59       EVP_EncryptUpdate(cl->cipher_outctx, outbuf, &outlen, buffer, length);
60       bufp = outbuf;
61       length = outlen;
62     }
63   else
64       bufp = buffer;
65
66   if(write(cl->meta_socket, bufp, length) < 0)
67     {
68       syslog(LOG_ERR, _("Sending meta data to %s (%s) failed: %m"), cl->name, cl->hostname);
69       return -1;
70     }
71 cp
72   return 0;
73 }
74
75 void broadcast_meta(connection_t *cl, char *buffer, int length)
76 {
77   rbl_t *rbl;
78   connection_t *p;
79 cp
80   RBL_FOREACH(connection_tree, rbl)
81     {
82       p = (connection_t *)rbl->data;
83       if(p != cl && p->status.meta && p->status.active)
84         send_meta(p, buffer, length);
85     }
86 cp
87 }
88
89 int receive_meta(connection_t *cl)
90 {
91   int x, l = sizeof(x);
92   int oldlen, i;
93   int lenin = 0;
94   char inbuf[MAXBUFSIZE];
95   char *bufp;
96 cp
97   if(getsockopt(cl->meta_socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
98     {
99       syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%m %s (%s)"), __FILE__, __LINE__, cl->meta_socket,
100              cl->name, cl->hostname);
101       return -1;
102     }
103   if(x)
104     {
105       syslog(LOG_ERR, _("Metadata socket error for %s (%s): %s"),
106              cl->name, cl->hostname, strerror(x));
107       return -1;
108     }
109
110   if(cl->status.decryptin)
111     bufp = inbuf;
112   else
113     bufp = cl->buffer + cl->buflen;
114
115   lenin = read(cl->meta_socket, bufp, MAXBUFSIZE - cl->buflen);
116
117   if(lenin<=0)
118     {
119       if(errno==EINTR)
120         return 0;      
121       if(errno==0)
122         {
123           if(debug_lvl >= DEBUG_CONNECTIONS)
124             syslog(LOG_NOTICE, _("Connection closed by %s (%s)"),
125                 cl->name, cl->hostname);
126         }
127       else
128         syslog(LOG_ERR, _("Metadata socket read error for %s (%s): %m"),
129                cl->name, cl->hostname);
130       return -1;
131     }
132
133   if(cl->status.decryptin)
134     {
135       EVP_DecryptUpdate(cl->cipher_inctx, cl->buffer + cl->buflen, &lenin, inbuf, lenin);
136     }
137
138   oldlen = cl->buflen;
139   cl->buflen += lenin;
140
141   for(;;)
142     {
143       cl->reqlen = 0;
144
145       for(i = oldlen; i < cl->buflen; i++)
146         {
147           if(cl->buffer[i] == '\n')
148             {
149               cl->buffer[i] = 0;  /* replace end-of-line by end-of-string so we can use sscanf */
150               cl->reqlen = i + 1;
151               break;
152             }
153         }
154
155       if(cl->reqlen)
156         {
157           if(debug_lvl >= DEBUG_META)
158             syslog(LOG_DEBUG, _("Got request from %s (%s): %s"),
159                    cl->name, cl->hostname, cl->buffer);
160
161           if(receive_request(cl))
162             return -1;
163
164           cl->buflen -= cl->reqlen;
165           memmove(cl->buffer, cl->buffer + cl->reqlen, cl->buflen);
166           oldlen = 0;
167         }
168       else
169         {
170           break;
171         }
172     }
173
174   if(cl->buflen >= MAXBUFSIZE)
175     {
176       syslog(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
177              cl->name, cl->hostname);
178       return -1;
179     }
180
181   cl->last_ping_time = time(NULL);
182 cp  
183   return 0;
184 }