]> git.sesse.net Git - cubemap/blobdiff - udpstream.cpp
Support setting TTL on outgoing UDP streams. Especially useful for multicast.
[cubemap] / udpstream.cpp
index 5f5d8468e3c19c68ebbe82219d668705f19e9876..74d6ef03ce7853658e8836bbb4a07256e32c9e5c 100644 (file)
@@ -2,7 +2,6 @@
 #include <sys/types.h>
 
 #include "log.h"
-#include "markpool.h"
 #include "udpstream.h"
 #include "util.h"
 
@@ -10,11 +9,8 @@
 #define SO_MAX_PACING_RATE 47
 #endif
 
-UDPStream::UDPStream(const sockaddr_in6 &dst, MarkPool *mark_pool, uint32_t pacing_rate)
-       : dst(dst),
-         mark_pool(mark_pool),
-         fwmark(0),
-         pacing_rate(pacing_rate)
+UDPStream::UDPStream(const sockaddr_in6 &dst, uint32_t pacing_rate, int ttl)
+       : dst(dst)
 {
        sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
        if (sock == -1) {
@@ -23,19 +19,28 @@ UDPStream::UDPStream(const sockaddr_in6 &dst, MarkPool *mark_pool, uint32_t paci
                return;
        }
 
-       if (mark_pool != NULL) {
-               fwmark = mark_pool->get_mark();
-               if (setsockopt(sock, SOL_SOCKET, SO_MARK, &fwmark, sizeof(fwmark)) == -1) {                          
-                       if (fwmark != 0) {
-                               log_perror("setsockopt(SO_MARK)");
-                       }
-               }
-       }
        if (setsockopt(sock, SOL_SOCKET, SO_MAX_PACING_RATE, &pacing_rate, sizeof(pacing_rate)) == -1) {
                if (pacing_rate != ~0U) {
                        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)");
+               }
+       }
 }
 
 UDPStream::~UDPStream()
@@ -43,9 +48,6 @@ UDPStream::~UDPStream()
        if (sock != -1) {
                safe_close(sock);
        }
-       if (mark_pool != NULL) {
-               mark_pool->release_mark(fwmark);
-       }
 }
 
 void UDPStream::send(const char *data, size_t bytes)