Fix some compiler warnings.
[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                 size_t outlen = length;
46
47                 if(!cipher_encrypt(&c->outcipher, buffer, length, buffer_prepare(&c->outbuf, length), &outlen, false) || outlen != length) {
48                         logger(LOG_ERR, "Error while encrypting metadata to %s (%s)",
49                                         c->name, c->hostname);
50                         return false;
51                 }
52
53         } else {
54                 buffer_add(&c->outbuf, buffer, length);
55         }
56
57         event_add(&c->outevent, NULL);
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 bool receive_meta(connection_t *c) {
75         int inlen;
76         char inbuf[MAXBUFSIZE];
77         char *bufp = inbuf, *endp;
78
79         /* Strategy:
80            - Read as much as possible from the TCP socket in one go.
81            - Decrypt it.
82            - Check if a full request is in the input buffer.
83            - If yes, process request and remove it from the buffer,
84            then check again.
85            - If not, keep stuff in buffer and exit.
86          */
87
88         inlen = recv(c->socket, inbuf, sizeof inbuf - c->inbuf.len, 0);
89
90         if(inlen <= 0) {
91                 if(!inlen || !errno) {
92                         ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection closed by %s (%s)",
93                                            c->name, c->hostname);
94                 } else if(sockwouldblock(sockerrno))
95                         return true;
96                 else
97                         logger(LOG_ERR, "Metadata socket read error for %s (%s): %s",
98                                    c->name, c->hostname, sockstrerror(sockerrno));
99                 return false;
100         }
101
102         do {
103                 if(!c->status.decryptin) {
104                         endp = memchr(bufp, '\n', inlen);
105                         if(endp)
106                                 endp++;
107                         else
108                                 endp = bufp + inlen;
109
110                         buffer_add(&c->inbuf, bufp, endp - bufp);
111
112                         inlen -= endp - bufp;
113                         bufp = endp;
114                 } else {
115                         size_t outlen = inlen;
116                         ifdebug(META) logger(LOG_DEBUG, "Received encrypted %d bytes", inlen);
117
118                         if(!cipher_decrypt(&c->incipher, bufp, inlen, buffer_prepare(&c->inbuf, inlen), &outlen, false) || inlen != outlen) {
119                                 logger(LOG_ERR, "Error while decrypting metadata from %s (%s)",
120                                            c->name, c->hostname);
121                                 return false;
122                         }
123
124                         inlen = 0;
125                 }
126
127                 while(c->inbuf.len) {
128                         /* Are we receiving a TCPpacket? */
129
130                         if(c->tcplen) {
131                                 char *tcpbuffer = buffer_read(&c->inbuf, c->tcplen);
132                                 if(tcpbuffer) {
133                                         receive_tcppacket(c, tcpbuffer, c->tcplen);
134                                         continue;
135                                 } else {
136                                         break;
137                                 }
138                         }
139
140                         /* Otherwise we are waiting for a request */
141
142                         char *request = buffer_readline(&c->inbuf);
143                         if(request) {
144                                 bool result = receive_request(c, request);
145                                 if(!result)
146                                         return false;
147                                 continue;
148                         } else {
149                                 break;
150                         }
151                 }
152         } while(inlen);
153
154         buffer_compact(&c->inbuf);
155
156         return true;
157 }