31a387a07a31931599b3cbab0306c830989051aa
[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.5 2000/10/21 11:52:06 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 <sys/socket.h>
30 #include <openssl/evp.h>
31
32 #include "net.h"
33 #include "system.h"
34
35 int send_meta(conn_list_t *cl, char *buffer, int length)
36 {
37   char outbuf[MAXBUFSIZE];
38   char *bufp;
39   int outlen;
40 cp
41   if(debug_lvl >= DEBUG_META)
42     syslog(LOG_DEBUG, _("Sending %d bytes of metadata to %s (%s): %s"), length,
43            cl->name, cl->hostname, buffer);
44
45   buffer[length-1]='\n';
46   
47   if(cl->status.encryptout)
48     {
49       EVP_EncryptUpdate(cl->cipher_outctx, outbuf, &outlen, buffer, length);
50       bufp = outbuf;
51     }
52   else
53     bufp = buffer;
54
55   if(write(cl->meta_socket, bufp, length) < 0)
56     {
57       syslog(LOG_ERR, _("Sending meta data to %s (%s) failed: %m"), cl->name, cl->hostname);
58       return -1;
59     }
60 cp
61   return 0;
62 }
63
64 int broadcast_meta(conn_list_t *cl, char *buffer, int length)
65 {
66   conn_list_t *p;
67 cp
68   for(p = conn_list; p != NULL; p = p->next)
69     if(p != cl && p->status.meta && p->status.active)
70       send_meta(p, buffer, length);
71 cp
72   return 0;
73 }
74
75 int receive_meta(conn_list_t *cl)
76 {
77   int x, l = sizeof(x);
78   int oldlen, i;
79   int lenin = 0;
80   char inbuf[MAXBUFSIZE];
81   char *bufp;
82 cp
83   if(getsockopt(cl->meta_socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
84     {
85       syslog(LOG_ERR, _("This is a bug: %s:%d: %d:%m %s (%s)"), __FILE__, __LINE__, cl->meta_socket,
86              cl->name, cl->hostname);
87       return -1;
88     }
89   if(x)
90     {
91       syslog(LOG_ERR, _("Metadata socket error for %s (%s): %s"),
92              cl->name, cl->hostname, strerror(x));
93       return -1;
94     }
95
96   if(cl->status.decryptin)
97     bufp = inbuf;
98   else
99     bufp = cl->buffer + cl->buflen;
100
101   lenin = read(cl->meta_socket, bufp, MAXBUFSIZE - cl->buflen);
102
103   if(lenin<=0)
104     {
105       if(errno==EINTR)
106         return 0;      
107       if(errno==0)
108         {
109           if(debug_lvl >= DEBUG_CONNECTIONS)
110             syslog(LOG_NOTICE, _("Connection closed by %s (%s)"),
111                 cl->name, cl->hostname);
112         }
113       else
114         syslog(LOG_ERR, _("Metadata socket read error for %s (%s): %m"),
115                cl->name, cl->hostname);
116       return -1;
117     }
118
119   if(cl->status.decryptin)
120     {
121       EVP_DecryptUpdate(cl->cipher_inctx, cl->buffer + cl->buflen, NULL, inbuf, lenin);
122     }
123     
124   oldlen = cl->buflen;
125   cl->buflen += lenin;
126
127   for(;;)
128     {
129       cl->reqlen = 0;
130
131       for(i = oldlen; i < cl->buflen; i++)
132         {
133           if(cl->buffer[i] == '\n')
134             {
135               cl->buffer[i] = 0;  /* replace end-of-line by end-of-string so we can use sscanf */
136               cl->reqlen = i + 1;
137               break;
138             }
139         }
140
141       if(cl->reqlen)
142         {
143           if(debug_lvl >= DEBUG_PROTOCOL)
144             syslog(LOG_DEBUG, _("Got request from %s (%s): %s"),
145                    cl->name, cl->hostname, cl->buffer);
146
147           if(receive_request(cl))
148             return -1;
149
150           cl->buflen -= cl->reqlen;
151           memmove(cl->buffer, cl->buffer + cl->reqlen, cl->buflen);
152           oldlen = 0;
153         }
154       else
155         {
156           break;
157         }
158     }
159
160   if(cl->buflen >= MAXBUFSIZE)
161     {
162       syslog(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
163              cl->name, cl->hostname);
164       return -1;
165     }
166
167   cl->last_ping_time = time(NULL);
168   cl->want_ping = 0;
169 cp  
170   return 0;
171 }