]> git.sesse.net Git - cubemap/blobdiff - udpstream.cpp
Support setting outgoing interface for multicast.
[cubemap] / udpstream.cpp
index fc003561ef0bb7c5a75e3a126fc93c559100eca3..77476499a69a2e13b61d1708ef366f222359424d 100644 (file)
@@ -1,3 +1,4 @@
+#include <string.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 
@@ -9,9 +10,8 @@
 #define SO_MAX_PACING_RATE 47
 #endif
 
-UDPStream::UDPStream(const sockaddr_in6 &dst, uint32_t pacing_rate)
-       : dst(dst),
-         pacing_rate(pacing_rate)
+UDPStream::UDPStream(const sockaddr_in6 &dst, uint32_t pacing_rate, int ttl, int multicast_iface_index)
+       : dst(dst)
 {
        sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
        if (sock == -1) {
@@ -25,6 +25,35 @@ UDPStream::UDPStream(const sockaddr_in6 &dst, uint32_t pacing_rate)
                        log_perror("setsockopt(SO_MAX_PACING_RATE)");
                }
        }
+
+       if (ttl != -1) {
+               // Seemingly the IPv4 parameters are used for sending to IPv4,
+               // even on an AF_INET6 socket, so we need to set both.
+               if (setsockopt(sock, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) == -1) {
+                       log_perror("setsockopt(IP_TTL)");
+               }
+               if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) == -1) {
+                       log_perror("setsockopt(IP_MULTICAST_TTL)");
+               }
+               if (setsockopt(sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) == -1) {
+                       log_perror("setsockopt(IPV6_UNICAST_HOPS)");
+               }
+               if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) == -1) {
+                       log_perror("setsockopt(IPV6_MULTICAST_HOPS)");
+               }
+       }
+
+       if (multicast_iface_index != -1) {
+               ip_mreqn mr;
+               memset(&mr, 0, sizeof(mr));
+               mr.imr_ifindex = multicast_iface_index;
+               if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, &mr, sizeof(mr)) == -1) {
+                       log_perror("setsockopt(IP_MULTICAST_IF)");
+               }
+               if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_IF, &multicast_iface_index, sizeof(multicast_iface_index)) == -1) {
+                       log_perror("setsockopt(IPV6_MULTICAST_IF)");
+               }
+       }
 }
 
 UDPStream::~UDPStream()