2 device.c -- multicast socket
3 Copyright (C) 2002-2005 Ivo Timmermans,
4 2002-2013 Guus Sliepen <guus@tinc-vpn.org>
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.
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.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 static const char *device_info = "multicast socket";
33 static struct addrinfo *ai = NULL;
34 static mac_t ignore_src = {0};
36 static bool setup_device(void) {
42 get_config_string(lookup_config(&config_tree, "Interface"), &iface);
44 if(!get_config_string(lookup_config(&config_tree, "Device"), &device)) {
45 logger(DEBUG_ALWAYS, LOG_ERR, "Device variable required for %s", device_info);
49 host = xstrdup(device);
50 space = strchr(host, ' ');
53 logger(DEBUG_ALWAYS, LOG_ERR, "Port number required for %s", device_info);
59 space = strchr(port, ' ');
66 ai = str2addrinfo(host, port, SOCK_DGRAM);
72 device_fd = socket(ai->ai_family, SOCK_DGRAM, IPPROTO_UDP);
75 logger(DEBUG_ALWAYS, LOG_ERR, "Creating socket failed: %s", sockstrerror(sockerrno));
80 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
83 static const int one = 1;
84 setsockopt(device_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
86 if(bind(device_fd, ai->ai_addr, ai->ai_addrlen)) {
87 logger(DEBUG_ALWAYS, LOG_ERR, "Can't bind to %s %s: %s", host, port, sockstrerror(sockerrno));
91 switch(ai->ai_family) {
92 #ifdef IP_ADD_MEMBERSHIP
96 struct sockaddr_in in;
97 memcpy(&in, ai->ai_addr, sizeof(in));
98 mreq.imr_multiaddr.s_addr = in.sin_addr.s_addr;
99 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
101 if(setsockopt(device_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *)&mreq, sizeof(mreq))) {
102 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot join multicast group %s %s: %s", host, port, sockstrerror(sockerrno));
106 #ifdef IP_MULTICAST_LOOP
107 setsockopt(device_fd, IPPROTO_IP, IP_MULTICAST_LOOP, (const void *)&one, sizeof(one));
109 #ifdef IP_MULTICAST_TTL
110 setsockopt(device_fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&ttl, sizeof(ttl));
116 #ifdef IPV6_JOIN_GROUP
119 struct ipv6_mreq mreq;
120 struct sockaddr_in6 in6;
121 memcpy(&in6, ai->ai_addr, sizeof(in6));
122 memcpy(&mreq.ipv6mr_multiaddr, &in6.sin6_addr, sizeof(mreq.ipv6mr_multiaddr));
123 mreq.ipv6mr_interface = in6.sin6_scope_id;
125 if(setsockopt(device_fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, (void *)&mreq, sizeof(mreq))) {
126 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot join multicast group %s %s: %s", host, port, sockstrerror(sockerrno));
130 #ifdef IPV6_MULTICAST_LOOP
131 setsockopt(device_fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (const void *)&one, sizeof(one));
133 #ifdef IPV6_MULTICAST_HOPS
134 setsockopt(device_fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (void *)&ttl, sizeof(ttl));
141 logger(DEBUG_ALWAYS, LOG_ERR, "Multicast for address family %x unsupported", ai->ai_family);
145 logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
152 closesocket(device_fd);
164 static void close_device(void) {
181 static bool read_packet(vpn_packet_t *packet) {
184 if((lenin = recv(device_fd, (void *)DATA(packet), MTU, 0)) <= 0) {
185 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
186 device, sockstrerror(sockerrno));
190 if(!memcmp(&ignore_src, DATA(packet) + 6, sizeof(ignore_src))) {
191 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Ignoring loopback packet of %ld bytes from %s", (long)lenin, device_info);
197 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s", packet->len,
203 static bool write_packet(vpn_packet_t *packet) {
204 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Writing packet of %d bytes to %s",
205 packet->len, device_info);
207 if(sendto(device_fd, (void *)DATA(packet), packet->len, 0, ai->ai_addr, ai->ai_addrlen) < 0) {
208 logger(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device,
209 sockstrerror(sockerrno));
213 memcpy(&ignore_src, DATA(packet) + 6, sizeof(ignore_src));
218 const devops_t multicast_devops = {
219 .setup = setup_device,
220 .close = close_device,
222 .write = write_packet,