ecf875c7107338827a870810f505cbe142d465e5
[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.16 2001/03/12 23:58:19 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 outbuf[MAXBUFSIZE];
49   char *bufp;
50   int outlen;
51 cp
52   if(debug_lvl >= DEBUG_META)
53     syslog(LOG_DEBUG, _("Sending %d bytes of metadata to %s (%s): %s"), length,
54            cl->name, cl->hostname, buffer);
55
56   buffer[length-1]='\n';
57
58   if(cl->status.encryptout)
59     {
60       EVP_EncryptUpdate(cl->cipher_outctx, outbuf, &outlen, buffer, length);
61       bufp = outbuf;
62       length = outlen;
63     }
64   else
65       bufp = buffer;
66
67   if(write(cl->meta_socket, bufp, length) < 0)
68     {
69       syslog(LOG_ERR, _("Sending meta data to %s (%s) failed: %m"), cl->name, cl->hostname);
70       return -1;
71     }
72 cp
73   return 0;
74 }
75
76 void broadcast_meta(connection_t *cl, char *buffer, int length)
77 {
78   avl_node_t *node;
79   connection_t *p;
80 cp
81   for(node = connection_tree->head; node; node = node->next)
82     {
83       p = (connection_t *)node->data;
84       if(p != cl && p->status.meta && p->status.active)
85         send_meta(p, buffer, length);
86     }
87 cp
88 }
89
90 int receive_meta(connection_t *cl)
91 {
92   int x, l = sizeof(x);
93   int oldlen, i;
94   int lenin = 0;
95   char inbuf[MAXBUFSIZE];
96   char *bufp;
97   int decrypted = 0;
98 cp
99   if(getsockopt(cl->meta_socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
100     {
101       syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%m %s (%s)"), __FILE__, __LINE__, cl->meta_socket,
102              cl->name, cl->hostname);
103       return -1;
104     }
105   if(x)
106     {
107       syslog(LOG_ERR, _("Metadata socket error for %s (%s): %s"),
108              cl->name, cl->hostname, strerror(x));
109       return -1;
110     }
111
112   lenin = read(cl->meta_socket, cl->buffer + cl->buflen, MAXBUFSIZE - cl->buflen);
113
114   if(lenin<=0)
115     {
116       if(lenin==0)
117         {
118           if(debug_lvl >= DEBUG_CONNECTIONS)
119             syslog(LOG_NOTICE, _("Connection closed by %s (%s)"),
120                 cl->name, cl->hostname);
121         }
122       else
123         if(errno==EINTR)
124           return 0;      
125         else
126           syslog(LOG_ERR, _("Metadata socket read error for %s (%s): %m"),
127                  cl->name, cl->hostname);
128
129       return -1;
130     }
131
132   oldlen = cl->buflen;
133   cl->buflen += lenin;
134
135   while(lenin)
136     {
137       if(cl->status.decryptin && !decrypted)
138         {
139           EVP_DecryptUpdate(cl->cipher_inctx, inbuf, &lenin, cl->buffer + oldlen, lenin);
140           memcpy(cl->buffer + oldlen, inbuf, lenin);
141           decrypted = 1;
142         }
143
144       cl->reqlen = 0;
145
146       for(i = oldlen; i < cl->buflen; i++)
147         {
148           if(cl->buffer[i] == '\n')
149             {
150               cl->buffer[i] = 0;  /* replace end-of-line by end-of-string so we can use sscanf */
151               cl->reqlen = i + 1;
152               break;
153             }
154         }
155
156       if(cl->reqlen)
157         {
158           if(debug_lvl >= DEBUG_META)
159             syslog(LOG_DEBUG, _("Got request from %s (%s): %s"),
160                    cl->name, cl->hostname, cl->buffer);
161
162           if(receive_request(cl))
163             return -1;
164
165           cl->buflen -= cl->reqlen;
166           lenin -= cl->reqlen;
167           memmove(cl->buffer, cl->buffer + cl->reqlen, cl->buflen);
168           oldlen = 0;
169         }
170       else
171         {
172           break;
173         }
174     }
175
176   if(cl->buflen >= MAXBUFSIZE)
177     {
178       syslog(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
179              cl->name, cl->hostname);
180       return -1;
181     }
182
183   cl->last_ping_time = time(NULL);
184 cp  
185   return 0;
186 }