Added library link check and addressed review comments
[collectd.git] / src / libcollectdclient / server.c
1 /**
2  * Copyright 2017 Florian Forster
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  *
22  * Authors:
23  *   Florian octo Forster <octo at collectd.org>
24  **/
25
26 #include "config.h"
27
28 #if !defined(__GNUC__) || !__GNUC__
29 #define __attribute__(x) /**/
30 #endif
31
32 #include "collectd/lcc_features.h"
33 #include "collectd/network_parse.h" /* for lcc_network_parse_options_t */
34 #include "collectd/server.h"
35
36 #include <errno.h>
37 #include <net/if.h>
38 #include <netdb.h>
39 #include <netinet/in.h>
40 #include <string.h>
41 #include <sys/socket.h>
42 #include <sys/types.h>
43 #include <unistd.h>
44
45 #include <stdio.h>
46 #define DEBUG(...) printf(__VA_ARGS__)
47
48 static _Bool is_multicast(struct addrinfo const *ai) {
49   if (ai->ai_family == AF_INET) {
50     struct sockaddr_in *addr = (struct sockaddr_in *)ai->ai_addr;
51     return IN_MULTICAST(ntohl(addr->sin_addr.s_addr));
52   } else if (ai->ai_family == AF_INET6) {
53     struct sockaddr_in6 *addr = (struct sockaddr_in6 *)ai->ai_addr;
54     return IN6_IS_ADDR_MULTICAST(&addr->sin6_addr);
55   }
56   return 0;
57 }
58
59 static int server_multicast_join(lcc_listener_t *srv,
60                                  struct sockaddr_storage *group, int loop_back,
61                                  int ttl) {
62   if (group->ss_family == AF_INET) {
63     struct sockaddr_in *sa = (struct sockaddr_in *)group;
64
65     int status = setsockopt(srv->conn, IPPROTO_IP, IP_MULTICAST_LOOP,
66                             &loop_back, sizeof(loop_back));
67     if (status == -1) {
68       DEBUG("setsockopt(IP_MULTICAST_LOOP, %d) = %d\n", loop_back, errno);
69       return errno;
70     }
71
72     status =
73         setsockopt(srv->conn, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
74     if (status == -1)
75       return errno;
76
77 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
78     struct ip_mreqn mreq = {
79         .imr_address.s_addr = INADDR_ANY,
80         .imr_multiaddr.s_addr = sa->sin_addr.s_addr,
81         .imr_ifindex = if_nametoindex(srv->interface),
82     };
83 #else
84     struct ip_mreq mreq = {
85         .imr_multiaddr.s_addr = sa->sin_addr.s_addr,
86     };
87 #endif
88     status = setsockopt(srv->conn, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
89                         sizeof(mreq));
90     if (status == -1)
91       return errno;
92   } else if (group->ss_family == AF_INET6) {
93     struct sockaddr_in6 *sa = (struct sockaddr_in6 *)group;
94
95     int status = setsockopt(srv->conn, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
96                             &loop_back, sizeof(loop_back));
97     if (status == -1)
98       return errno;
99
100     status = setsockopt(srv->conn, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl,
101                         sizeof(ttl));
102     if (status == -1)
103       return errno;
104
105     struct ipv6_mreq mreq6 = {
106         .ipv6mr_interface = if_nametoindex(srv->interface),
107     };
108     memmove(&mreq6.ipv6mr_multiaddr, &sa->sin6_addr, sizeof(struct in6_addr));
109
110     status = setsockopt(srv->conn, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6,
111                         sizeof(mreq6));
112     if (status == -1)
113       return errno;
114   } else {
115     return EINVAL;
116   }
117
118   return 0;
119 }
120
121 static int server_bind_socket(lcc_listener_t *srv, struct addrinfo const *ai) {
122   /* allow multiple sockets to use the same PORT number */
123   if (setsockopt(srv->conn, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) ==
124       -1) {
125     return errno;
126   }
127
128   if (bind(srv->conn, ai->ai_addr, ai->ai_addrlen) == -1) {
129     return -1;
130   }
131
132   if (is_multicast(ai)) {
133     int status = server_multicast_join(srv, (void *)ai->ai_addr, /* loop = */ 1,
134                                        /* ttl = */ 16);
135     if (status != 0)
136       return status;
137   }
138
139   return 0;
140 }
141
142 static int server_open(lcc_listener_t *srv) {
143   struct addrinfo *res = NULL;
144   int status = getaddrinfo(srv->node ? srv->node : "::",
145                            srv->service ? srv->service : LCC_DEFAULT_PORT,
146                            &(struct addrinfo){
147                                .ai_flags = AI_ADDRCONFIG,
148                                .ai_family = AF_UNSPEC,
149                                .ai_socktype = SOCK_DGRAM,
150                            },
151                            &res);
152   if (status != 0)
153     return status;
154
155   for (struct addrinfo *ai = res; ai != NULL; ai = ai->ai_next) {
156     srv->conn = socket(ai->ai_family, ai->ai_socktype, 0);
157     if (srv->conn == -1)
158       continue;
159
160     status = server_bind_socket(srv, ai);
161     if (status != 0) {
162       close(srv->conn);
163       srv->conn = -1;
164       continue;
165     }
166
167     break;
168   }
169
170   freeaddrinfo(res);
171
172   if (srv->conn >= 0)
173     return 0;
174   return status != 0 ? status : -1;
175 }
176
177 int lcc_listen_and_write(lcc_listener_t srv) {
178   _Bool close_socket = 0;
179
180   if (srv.conn < 0) {
181     int status = server_open(&srv);
182     if (status != 0)
183       return status;
184     close_socket = 1;
185   }
186
187   if (srv.buffer_size == 0)
188     srv.buffer_size = LCC_NETWORK_BUFFER_SIZE;
189
190   if (srv.parser == NULL)
191     srv.parser = lcc_network_parse;
192
193   int ret = 0;
194   while (42) {
195     char buffer[srv.buffer_size];
196     ssize_t len = recv(srv.conn, buffer, sizeof(buffer), /* flags = */ 0);
197     if (len == -1) {
198       ret = errno;
199       break;
200     } else if (len == 0) {
201       break;
202     }
203
204     (void)srv.parser(buffer, (size_t)len, srv.parse_options);
205   }
206
207   if (close_socket) {
208     close(srv.conn);
209     srv.conn = -1;
210   }
211
212   return ret;
213 }