Fix whitespace.
[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 "cipher.h"
25 #include "connection.h"
26 #include "logger.h"
27 #include "meta.h"
28 #include "net.h"
29 #include "protocol.h"
30 #include "utils.h"
31 #include "xalloc.h"
32
33 bool send_meta_sptps(void *handle, uint8_t type, const char *buffer, size_t length) {
34         connection_t *c = handle;
35
36         if(!c) {
37                 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta_sptps() called with NULL pointer!");
38                 abort();
39         }
40
41         buffer_add(&c->outbuf, buffer, length);
42         event_add(&c->outevent, NULL);
43
44         return true;
45 }
46
47 bool send_meta(connection_t *c, const char *buffer, int length) {
48         if(!c) {
49                 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta() called with NULL pointer!");
50                 abort();
51         }
52
53         logger(DEBUG_META, LOG_DEBUG, "Sending %d bytes of metadata to %s (%s)", length,
54                            c->name, c->hostname);
55
56         if(c->protocol_minor >= 2)
57                 return sptps_send_record(&c->sptps, 0, buffer, length);
58
59         /* Add our data to buffer */
60         if(c->status.encryptout) {
61                 size_t outlen = length;
62
63                 if(!cipher_encrypt(&c->outcipher, buffer, length, buffer_prepare(&c->outbuf, length), &outlen, false) || outlen != length) {
64                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting metadata to %s (%s)",
65                                         c->name, c->hostname);
66                         return false;
67                 }
68
69         } else {
70                 buffer_add(&c->outbuf, buffer, length);
71         }
72
73         event_add(&c->outevent, NULL);
74
75         return true;
76 }
77
78 void broadcast_meta(connection_t *from, const char *buffer, int length) {
79         for list_each(connection_t, c, connection_list)
80                 if(c != from && c->status.active)
81                         send_meta(c, buffer, length);
82 }
83
84 bool receive_meta_sptps(void *handle, uint8_t type, const char *data, uint16_t length) {
85         connection_t *c = handle;
86
87         if(!c) {
88                 logger(DEBUG_ALWAYS, LOG_ERR, "receive_meta_sptps() called with NULL pointer!");
89                 abort();
90         }
91
92         if(type == SPTPS_HANDSHAKE) {
93                 if(c->allow_request == ACK)
94                         return send_ack(c);
95                 else
96                         return true;
97         }
98
99         if(!data)
100                 return true;
101
102         /* Are we receiving a TCPpacket? */
103
104         if(c->tcplen) {
105                 if(length != c->tcplen)
106                         return false;
107                 receive_tcppacket(c, data, length);
108                 c->tcplen = 0;
109                 return true;
110         }
111
112         /* Otherwise we are waiting for a request */
113
114         return receive_request(c, data);
115 }
116
117 bool receive_meta(connection_t *c) {
118         int inlen;
119         char inbuf[MAXBUFSIZE];
120         char *bufp = inbuf, *endp;
121
122         /* Strategy:
123            - Read as much as possible from the TCP socket in one go.
124            - Decrypt it.
125            - Check if a full request is in the input buffer.
126            - If yes, process request and remove it from the buffer,
127            then check again.
128            - If not, keep stuff in buffer and exit.
129          */
130
131         buffer_compact(&c->inbuf, MAXBUFSIZE);
132
133         if(sizeof inbuf <= c->inbuf.len) {
134                 logger(DEBUG_ALWAYS, LOG_ERR, "Input buffer full for %s (%s)", c->name, c->hostname);
135                 return false;
136         }
137
138         inlen = recv(c->socket, inbuf, sizeof inbuf - c->inbuf.len, 0);
139
140         if(inlen <= 0) {
141                 if(!inlen || !errno) {
142                         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection closed by %s (%s)",
143                                            c->name, c->hostname);
144                 } else if(sockwouldblock(sockerrno))
145                         return true;
146                 else
147                         logger(DEBUG_ALWAYS, LOG_ERR, "Metadata socket read error for %s (%s): %s",
148                                    c->name, c->hostname, sockstrerror(sockerrno));
149                 return false;
150         }
151
152         do {
153                 if(c->protocol_minor >= 2)
154                         return sptps_receive_data(&c->sptps, bufp, inlen);
155
156                 if(!c->status.decryptin) {
157                         endp = memchr(bufp, '\n', inlen);
158                         if(endp)
159                                 endp++;
160                         else
161                                 endp = bufp + inlen;
162
163                         buffer_add(&c->inbuf, bufp, endp - bufp);
164
165                         inlen -= endp - bufp;
166                         bufp = endp;
167                 } else {
168                         size_t outlen = inlen;
169
170                         if(!cipher_decrypt(&c->incipher, bufp, inlen, buffer_prepare(&c->inbuf, inlen), &outlen, false) || inlen != outlen) {
171                                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting metadata from %s (%s)",
172                                            c->name, c->hostname);
173                                 return false;
174                         }
175
176                         inlen = 0;
177                 }
178
179                 while(c->inbuf.len) {
180                         /* Are we receiving a TCPpacket? */
181
182                         if(c->tcplen) {
183                                 char *tcpbuffer = buffer_read(&c->inbuf, c->tcplen);
184                                 if(tcpbuffer) {
185                                         if(proxytype == PROXY_SOCKS4 && c->allow_request == ID) {
186                                                 if(tcpbuffer[0] == 0 && tcpbuffer[1] == 0x5a) {
187                                                         logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
188                                                 } else {
189                                                         logger(DEBUG_CONNECTIONS, LOG_ERR, "Proxy request rejected");
190                                                         return false;
191                                                 }
192                                         } else
193                                                 receive_tcppacket(c, tcpbuffer, c->tcplen);
194                                         c->tcplen = 0;
195                                         continue;
196                                 } else {
197                                         break;
198                                 }
199                         }
200
201                         /* Otherwise we are waiting for a request */
202
203                         char *request = buffer_readline(&c->inbuf);
204                         if(request) {
205                                 bool result = receive_request(c, request);
206                                 if(!result)
207                                         return false;
208                                 continue;
209                         } else {
210                                 break;
211                         }
212                 }
213         } while(inlen);
214
215         return true;
216 }