]> git.meshlink.io Git - meshlink/blob - src/meta.c
Get rid of ->hostname.
[meshlink] / src / meta.c
1 /*
2     meta.c -- handle the meta communication
3     Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>,
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "connection.h"
23 #include "logger.h"
24 #include "meshlink_internal.h"
25 #include "meta.h"
26 #include "net.h"
27 #include "protocol.h"
28 #include "utils.h"
29 #include "xalloc.h"
30
31 bool send_meta_sptps(void *handle, uint8_t type, const void *buffer, size_t length) {
32         connection_t *c = handle;
33         meshlink_handle_t *mesh = c->mesh;
34
35         if(!c) {
36                 logger(mesh, MESHLINK_ERROR, "send_meta_sptps() called with NULL pointer!");
37                 abort();
38         }
39
40         buffer_add(&c->outbuf, (const char *)buffer, length);
41         io_set(&mesh->loop, &c->io, IO_READ | IO_WRITE);
42
43         return true;
44 }
45
46 bool send_meta(meshlink_handle_t *mesh, connection_t *c, const char *buffer, int length) {
47         if(!c) {
48                 logger(mesh, MESHLINK_ERROR, "send_meta() called with NULL pointer!");
49                 abort();
50         }
51
52         logger(mesh, MESHLINK_DEBUG, "Sending %d bytes of metadata to %s", length, c->name);
53
54         if(c->allow_request == ID) {
55                 buffer_add(&c->outbuf, buffer, length);
56                 io_set(&mesh->loop, &c->io, IO_READ | IO_WRITE);
57                 return true;
58         }
59
60         return sptps_send_record(&c->sptps, 0, buffer, length);
61 }
62
63 void broadcast_meta(meshlink_handle_t *mesh, connection_t *from, const char *buffer, int length) {
64         for list_each(connection_t, c, mesh->connections)
65                 if(c != from && c->status.active)
66                         send_meta(mesh, c, buffer, length);
67 }
68
69 bool receive_meta_sptps(void *handle, uint8_t type, const void *data, uint16_t length) {
70         connection_t *c = handle;
71         meshlink_handle_t *mesh = c->mesh;
72         char *request = (char *)data;
73
74         if(!c) {
75                 logger(mesh, MESHLINK_ERROR, "receive_meta_sptps() called with NULL pointer!");
76                 abort();
77         }
78
79         if(type == SPTPS_HANDSHAKE) {
80                 if(c->allow_request == ACK)
81                         return send_ack(mesh, c);
82                 else
83                         return true;
84         }
85
86         if(!request)
87                 return true;
88
89         /* Are we receiving a TCPpacket? */
90
91         if(c->tcplen) {
92                 if(length != c->tcplen)
93                         return false;
94                 receive_tcppacket(mesh, c, request, length);
95                 c->tcplen = 0;
96                 return true;
97         }
98
99         /* Change newline to null byte, just like non-SPTPS requests */
100
101         if(request[length - 1] == '\n')
102                 request[length - 1] = 0;
103
104         /* Otherwise we are waiting for a request */
105
106         return receive_request(mesh, c, request);
107 }
108
109 bool receive_meta(meshlink_handle_t *mesh, connection_t *c) {
110         int inlen;
111         char inbuf[MAXBUFSIZE];
112
113         inlen = recv(c->socket, inbuf, sizeof(inbuf), 0);
114
115         if(inlen <= 0) {
116                 if(!inlen || !errno) {
117                         logger(mesh, MESHLINK_INFO, "Connection closed by %s", c->name);
118                 } else if(sockwouldblock(sockerrno))
119                         return true;
120                 else
121                         logger(mesh, MESHLINK_ERROR, "Metadata socket read error for %s: %s", c->name, sockstrerror(sockerrno));
122                 return false;
123         }
124
125         if(c->allow_request == ID) {
126                 buffer_add(&c->inbuf, inbuf, inlen);
127
128                 char *request = buffer_readline(&c->inbuf);
129
130                 if(request) {
131                         if(!receive_request(mesh, c, request) || c->allow_request == ID)
132                                 return false;
133
134                         int left = c->inbuf.len - c->inbuf.offset;
135                         if(left > 0) {
136                                 fprintf(stderr, "GOT A LITTLE MORE\n");
137                                 return sptps_receive_data(&c->sptps, buffer_read(&c->inbuf, left), left);
138                         } else
139                                 return true;
140                 }
141
142                 if(c->inbuf.len >= sizeof(inbuf)) {
143                         logger(mesh, MESHLINK_ERROR, "Input buffer full for %s", c->name);
144                         return false;
145                 } else
146                         return true;
147         }
148
149         return sptps_receive_data(&c->sptps, inbuf, inlen);
150 }