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