remove linux specific includes
[project/mdnsd.git] / interface.c
1 /*
2  * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
3  * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License version 2.1
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <sys/socket.h>
16 #include <sys/ioctl.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/utsname.h>
20 #include <net/if.h>
21 #include <arpa/inet.h>
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <errno.h>
28
29 #include <libubox/usock.h>
30 #include <libubox/uloop.h>
31 #include <libubox/avl-cmp.h>
32 #include <libubox/utils.h>
33 #include "interface.h"
34 #include "util.h"
35 #include "dns.h"
36 #include "announce.h"
37
38 int
39 interface_send_packet(struct interface *iface, struct iovec *iov, int iov_len)
40 {
41         static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_pktinfo)) / sizeof(size_t)) + 1];
42         static struct sockaddr_in a;
43         static struct msghdr m = {
44                 .msg_name = (struct sockaddr *) &a,
45                 .msg_namelen = sizeof(a),
46                 .msg_control = cmsg_data,
47                 .msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo)),
48         };
49         struct in_pktinfo *pkti;
50         struct cmsghdr *cmsg;
51         int fd = iface->fd.fd;
52
53         a.sin_family = AF_INET;
54         a.sin_port = htons(MCAST_PORT);
55         m.msg_iov = iov;
56         m.msg_iovlen = iov_len;
57
58         memset(cmsg_data, 0, sizeof(cmsg_data));
59         cmsg = CMSG_FIRSTHDR(&m);
60         cmsg->cmsg_len = m.msg_controllen;
61         cmsg->cmsg_level = IPPROTO_IP;
62         cmsg->cmsg_type = IP_PKTINFO;
63
64         pkti = (struct in_pktinfo*) CMSG_DATA(cmsg);
65         pkti->ipi_ifindex = iface->ifindex;
66
67         a.sin_addr.s_addr = inet_addr(MCAST_ADDR);
68
69         return sendmsg(fd, &m, 0);
70 }
71
72 static void interface_close(struct interface *iface)
73 {
74         if (iface->fd.fd < 0)
75                 return;
76
77         announce_free(iface);
78         uloop_fd_delete(&iface->fd);
79         close(iface->fd.fd);
80         iface->fd.fd = -1;
81 }
82
83 static void interface_free(struct interface *iface)
84 {
85         interface_close(iface);
86         free(iface);
87 }
88
89 static void
90 read_socket(struct uloop_fd *u, unsigned int events)
91 {
92         struct interface *iface = container_of(u, struct interface, fd);
93         static uint8_t buffer[8 * 1024];
94         int len;
95
96         if (u->eof) {
97                 interface_close(iface);
98                 uloop_timeout_set(&iface->reconnect, 1000);
99                 return;
100         }
101
102         len = read(u->fd, buffer, sizeof(buffer));
103         if (len < 1) {
104                 fprintf(stderr, "read failed: %s\n", strerror(errno));
105                 return;
106         }
107
108         dns_handle_packet(iface, buffer, len);
109 }
110
111 static int
112 interface_socket_setup(struct interface *iface)
113 {
114         struct ip_mreqn mreq;
115         uint8_t ttl = 255;
116         int yes = 1;
117         int no = 0;
118         struct sockaddr_in sa = { 0 };
119         int fd = iface->fd.fd;
120
121         sa.sin_family = AF_INET;
122         sa.sin_port = htons(MCAST_PORT);
123         inet_pton(AF_INET, MCAST_ADDR, &sa.sin_addr);
124
125         memset(&mreq, 0, sizeof(mreq));
126         mreq.imr_address.s_addr = iface->v4_addr.s_addr;
127         mreq.imr_multiaddr = sa.sin_addr;
128         mreq.imr_ifindex = iface->ifindex;
129
130         if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
131                 fprintf(stderr, "ioctl failed: IP_MULTICAST_TTL\n");
132
133         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
134                 fprintf(stderr, "ioctl failed: SO_REUSEADDR\n");
135
136         /* Some network drivers have issues with dropping membership of
137          * mcast groups when the iface is down, but don't allow rejoining
138          * when it comes back up. This is an ugly workaround
139          * -- this was copied from avahi --
140          */
141         setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
142
143         if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
144                 fprintf(stderr, "failed to join multicast group: %s\n", strerror(errno));
145                 close(fd);
146                 fd = -1;
147                 return -1;
148         }
149
150         if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0)
151                 fprintf(stderr, "ioctl failed: IP_RECVTTL\n");
152
153         if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0)
154                 fprintf(stderr, "ioctl failed: IP_PKTINFO\n");
155
156         if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &no, sizeof(no)) < 0)
157                 fprintf(stderr, "ioctl failed: IP_MULTICAST_LOOP\n");
158
159         return 0;
160 }
161
162 static void
163 reconnect_socket(struct uloop_timeout *timeout)
164 {
165         struct interface *iface = container_of(timeout, struct interface, reconnect);
166
167         iface->fd.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK, MCAST_ADDR, "5353");
168         if (iface->fd.fd < 0) {
169                 fprintf(stderr, "failed to add listener: %s\n", strerror(errno));
170                 goto retry;
171         }
172
173         if (interface_socket_setup(iface)) {
174                 iface->fd.fd = -1;
175                 goto retry;
176         }
177
178         uloop_fd_add(&iface->fd, ULOOP_READ);
179         dns_send_question(iface, "_services._dns-sd._udp.local", TYPE_PTR);
180         announce_init(iface);
181         return;
182
183 retry:
184         uloop_timeout_set(timeout, 1000);
185 }
186
187
188 static void interface_start(struct interface *iface)
189 {
190         iface->fd.cb = read_socket;
191         iface->reconnect.cb = reconnect_socket;
192         uloop_timeout_set(&iface->reconnect, 100);
193 }
194
195 static void
196 iface_update_cb(struct vlist_tree *tree, struct vlist_node *node_new,
197                 struct vlist_node *node_old)
198 {
199         struct interface *iface;
200
201         if (node_old) {
202                 iface = container_of(node_old, struct interface, node);
203                 interface_free(iface);
204         }
205
206         if (node_new) {
207                 iface = container_of(node_new, struct interface, node);
208                 interface_start(iface);
209         }
210 }
211
212 static int
213 get_iface_ipv4(struct interface *iface)
214 {
215         struct sockaddr_in *sin;
216         struct ifreq ir;
217         int sock, ret = -1;
218
219         sock = socket(AF_INET, SOCK_DGRAM, 0);
220         if (sock < 0)
221                 return -1;
222
223         memset(&ir, 0, sizeof(struct ifreq));
224         strncpy(ir.ifr_name, iface->name, sizeof(ir.ifr_name));
225
226         ret = ioctl(sock, SIOCGIFADDR, &ir);
227         if (ret < 0)
228                 goto out;
229
230         sin = (struct sockaddr_in *) &ir.ifr_addr;
231         memcpy(&iface->v4_addr, &sin->sin_addr, sizeof(iface->v4_addr));
232
233 out:
234         close(sock);
235         return ret;
236 }
237
238 int interface_add(const char *name)
239 {
240         struct interface *iface;
241         char *name_buf;
242
243         iface = calloc_a(sizeof(*iface),
244                 &name_buf, strlen(name) + 1);
245
246         iface->name = strcpy(name_buf, name);
247         iface->ifindex = if_nametoindex(name);
248         iface->fd.fd = -1;
249
250         if (iface->ifindex <= 0)
251                 goto error;
252
253         if (get_iface_ipv4(iface))
254                 goto error;
255
256         vlist_add(&interfaces, &iface->node, name);
257         return 0;
258
259 error:
260         free(iface);
261         return -1;
262 }
263
264 VLIST_TREE(interfaces, avl_strcmp, iface_update_cb, false, false);