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