Introducing the Big Tinc Lock.
[tinc] / src / meta.c
1 /*
2     meta.c -- handle the meta communication
3     Copyright (C) 2000-2009 Guus Sliepen <guus@tinc-vpn.org>,
4                   2000-2005 Ivo Timmermans
5                   2006      Scott Lamb <slamb@slamb.org>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License along
18     with this program; if not, write to the Free Software Foundation, Inc.,
19     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include "system.h"
23
24 #include "splay_tree.h"
25 #include "cipher.h"
26 #include "connection.h"
27 #include "logger.h"
28 #include "meta.h"
29 #include "net.h"
30 #include "protocol.h"
31 #include "utils.h"
32 #include "xalloc.h"
33
34 bool send_meta(connection_t *c, const char *buffer, int length) {
35         if(!c) {
36                 logger(LOG_ERR, "send_meta() called with NULL pointer!");
37                 abort();
38         }
39
40         ifdebug(META) logger(LOG_DEBUG, "Sending %d bytes of metadata to %s (%s)", length,
41                            c->name, c->hostname);
42
43         /* Add our data to buffer */
44         if(c->status.encryptout) {
45                 char outbuf[length];
46                 size_t outlen = length;
47
48                 if(!cipher_encrypt(&c->outcipher, buffer, length, outbuf, &outlen, false) || outlen != length) {
49                         logger(LOG_ERR, "Error while encrypting metadata to %s (%s)",
50                                         c->name, c->hostname);
51                         return false;
52                 }
53                 
54                 write(c->socket, outbuf, length);
55         } else {
56                 write(c->socket, buffer, length);
57         }
58
59         return true;
60 }
61
62 void broadcast_meta(connection_t *from, const char *buffer, int length) {
63         splay_node_t *node;
64         connection_t *c;
65
66         for(node = connection_tree->head; node; node = node->next) {
67                 c = node->data;
68
69                 if(c != from && c->status.active)
70                         send_meta(c, buffer, length);
71         }
72 }
73
74 static bool process_meta(connection_t *c, char *reqbuf, int *len) {
75         while(*len) {
76                 if(c->tcplen) {
77                         if(c->tcplen > *len)
78                                 break;
79
80                         mutex_lock(&mutex);
81                         receive_tcppacket(c, reqbuf, c->tcplen);
82                         mutex_unlock(&mutex);
83
84                         memmove(reqbuf, reqbuf, *len - c->tcplen);
85                         *len -= c->tcplen;
86                 } else {
87                         char *end = memchr(reqbuf, '\n', *len);
88                         if(!end)
89                                 break;
90                         else
91                                 *end++ = 0;
92
93                         mutex_lock(&mutex);
94                         bool success = receive_request(c, reqbuf);
95                         mutex_unlock(&mutex);
96
97                         if(!success)
98                                 return false;
99
100                         memmove(reqbuf, end, *len - (end - reqbuf));
101                         *len -= end - reqbuf;
102                 }
103         }
104
105         return true;
106 }
107                         
108 bool receive_meta(connection_t *c) {
109         int inlen;
110         int reqlen = 0;
111         char inbuf[MAXBUFSIZE];
112         char reqbuf[MAXBUFSIZE];
113
114         /* Strategy:
115            - Read as much as possible from the TCP socket in one go.
116            - Decrypt it if necessary.
117            - Check if a full request is in the request buffer.
118            - If yes, process request and remove it from the buffer, then check again.
119            - If not, try to read more.
120          */
121
122         while(true) {
123                 inlen = recv(c->socket, inbuf, sizeof inbuf - reqlen, 0);
124
125                 if(inlen <= 0) {
126                         if(!inlen || !errno) {
127                                 ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection closed by %s (%s)",
128                                                    c->name, c->hostname);
129                         } else if(sockwouldblock(sockerrno))
130                                 continue;
131                         else
132                                 logger(LOG_ERR, "Metadata socket read error for %s (%s): %s",
133                                            c->name, c->hostname, sockstrerror(sockerrno));
134                         return false;
135                 }
136
137                 while(inlen) {
138                         if(!c->status.decryptin) {
139                                 char *end = memchr(inbuf, '\n', inlen);
140                                 if(!end)
141                                         end = inbuf + inlen;
142                                 else
143                                         end++;
144                                 memcpy(reqbuf + reqlen, inbuf, end - inbuf);
145                                 reqlen += end - inbuf;
146
147                                 if(!process_meta(c, reqbuf, &reqlen))
148                                         return false;
149
150                                 memmove(inbuf, end, inlen - (end - inbuf));
151                                 inlen -= end - inbuf;
152                         } else {
153                                 size_t outlen = inlen;
154
155                                 if(!cipher_decrypt(&c->incipher, inbuf, inlen, reqbuf + reqlen, &outlen, false) || inlen != outlen) {
156                                         logger(LOG_ERR, "Error while decrypting metadata from %s (%s)", c->name, c->hostname);
157                                         return false;
158                                 }
159
160                                 reqlen += inlen;
161                                 inlen = 0;
162
163                                 if(!process_meta(c, reqbuf, &reqlen))
164                                         return false;
165                         }
166                 }
167         }
168 }