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