chiark / gitweb /
lib/addr.c, etc.: Return plain addresses from `netaddress_resolve'.
[disorder] / lib / uaudio-rtp.c
index 2efd910b506d2cdbd4193cc5d872db02dcee34b4..dc1fd6ca6527096ac673f3c2102f58451b8782a9 100644 (file)
 #include "timeval.h"
 #include "configuration.h"
 
-/** @brief Bytes to send per network packet
- *
- * This is the maximum number of bytes we pass to write(2); to determine actual
- * packet sizes, add a UDP header and an IP header (and a link layer header if
- * it's the link layer size you care about).
- *
- * Don't make this too big or arithmetic will start to overflow.
- */
-#define NETWORK_BYTES (1500-8/*UDP*/-40/*IP*/-8/*conservatism*/)
+/** @brief Bytes to send per network packet */
+static int rtp_max_payload;
 
 /** @brief RTP payload type */
 static int rtp_payload;
 
-/** @brief RTP output socket */
+/** @brief RTP broadcast/multicast output socket */
 static int rtp_fd = -1;
 
-/** @brief RTP output socket (IPv6) */
-static int rtp_fd6;
+/** @brief RTP unicast output socket (IPv4) */
+static int rtp_fd4 = -1;
+
 /** @brief RTP unicast output socket (IPv6) */
 static int rtp_fd6 = -1;
 
@@ -106,6 +100,8 @@ static const char *const rtp_options[] = {
   "multicast-ttl",
   "multicast-loop",
   "rtp-mode",
+  "rtp-max-payload",
+  "rtp-mtu-discovery",
   NULL
 };
 
@@ -208,23 +204,23 @@ static size_t rtp_play(void *buffer, size_t nsamples, unsigned flags) {
     uaudio_schedule_sent(nsamples);
     return nsamples;
   }
-  if(rtp_mode == RTP_REQUEST) {
-    struct rtp_recipient *r;
-    struct msghdr m;
-    memset(&m, 0, sizeof m);
-    m.msg_iov = vec;
-    m.msg_iovlen = 2;
-    pthread_mutex_lock(&rtp_lock);
-    for(r = rtp_recipient_list; r; r = r->next) {
-      m.msg_name = &r->sa;
-      m.msg_namelen = r->sa.ss_family == AF_INET ? 
-        sizeof(struct sockaddr_in) : sizeof (struct sockaddr_in6);
-      sendmsg(r->sa.ss_family == AF_INET ? rtp_fd : rtp_fd6,
-              &m, MSG_DONTWAIT|MSG_NOSIGNAL);
-      // TODO similar error handling to other case?
-    }
-    pthread_mutex_unlock(&rtp_lock);
-  } else {
+  /* Send stuff to explicitly registerd unicast addresses unconditionally */
+  struct rtp_recipient *r;
+  struct msghdr m;
+  memset(&m, 0, sizeof m);
+  m.msg_iov = vec;
+  m.msg_iovlen = 2;
+  pthread_mutex_lock(&rtp_lock);
+  for(r = rtp_recipient_list; r; r = r->next) {
+    m.msg_name = &r->sa;
+    m.msg_namelen = r->sa.ss_family == AF_INET ? 
+      sizeof(struct sockaddr_in) : sizeof (struct sockaddr_in6);
+    sendmsg(r->sa.ss_family == AF_INET ? rtp_fd4 : rtp_fd6,
+            &m, MSG_DONTWAIT|MSG_NOSIGNAL);
+    // TODO similar error handling to other case?
+  }
+  pthread_mutex_unlock(&rtp_lock);
+  if(rtp_mode != RTP_REQUEST) {
     int written_bytes;
     do {
       written_bytes = writev(rtp_fd, vec, 2);
@@ -265,10 +261,15 @@ static void hack_send_buffer_size(int fd, const char *what) {
 }
 
 static void rtp_open(void) {
-  struct addrinfo *dres, *sres;
+  struct resolved *dres, *sres;
+  size_t ndres, nsres;
   static const int one = 1;
   struct netaddress dst[1], src[1];
   const char *mode;
+#ifdef IP_MTU_DISCOVER
+  const char *mtu_disc;
+  int opt;
+#endif
   
   /* Get the mode */
   mode = uaudio_get("rtp-mode", "auto");
@@ -287,22 +288,24 @@ static void rtp_open(void) {
                     "rtp-source-port",
                     src);
   if(dst->af != -1) {
-    dres = netaddress_resolve(dst, 0, IPPROTO_UDP);
-    if(!dres)
+    if(netaddress_resolve(dst, 0, SOCK_DGRAM, &dres, &ndres))
       exit(-1);
-  } else
+  } else {
     dres = 0;
+    ndres = 0;
+  }
   if(src->af != -1) {
-    sres = netaddress_resolve(src, 1, IPPROTO_UDP);
-    if(!sres)
+    if(netaddress_resolve(src, 0, SOCK_DGRAM, &sres, &nsres))
       exit(-1);
-  } else
+  } else {
     sres = 0;
+    nsres = 0;
+  }
   /* _AUTO inspects the destination address and acts accordingly */
   if(rtp_mode == RTP_AUTO) {
     if(!dres)
       rtp_mode = RTP_REQUEST;
-    else if(multicast(dres->ai_addr))
+    else if(multicast(dres->sa))
       rtp_mode = RTP_MULTICAST;
     else {
       struct ifaddrs *ifs;
@@ -315,7 +318,7 @@ static void rtp_open(void) {
          * for he same interface which _does_ have ifa_broadaddr though... */
         if((ifs->ifa_flags & IFF_BROADCAST)
            && ifs->ifa_broadaddr
-           && sockaddr_equal(ifs->ifa_broadaddr, dres->ai_addr))
+           && sockaddr_equal(ifs->ifa_broadaddr, dres->sa))
           break;
         ifs = ifs->ifa_next;
       }
@@ -325,29 +328,25 @@ static void rtp_open(void) {
         rtp_mode = RTP_UNICAST;
     }
   }
-  /* Create the socket */
+  rtp_max_payload = atoi(uaudio_get("rtp-max-payload", "-1"));
+  if(rtp_max_payload < 0)
+    rtp_max_payload = 1500 - 8/*UDP*/ - 40/*IP*/ - 8/*conservatism*/;
+  /* Create the sockets */
   if(rtp_mode != RTP_REQUEST) {
-    if((rtp_fd = socket(dres->ai_family,
-                        dres->ai_socktype,
-                        dres->ai_protocol)) < 0)
+    if((rtp_fd = socket(dres->sa->sa_family, SOCK_DGRAM, IPPROTO_UDP)) < 0)
       disorder_fatal(errno, "error creating RTP transmission socket");
-  } else {                              /* request mode slightly different */
-    if((rtp_fd = socket(AF_INET,
-                        SOCK_DGRAM,
-                        IPPROTO_UDP)) < 0)
-      disorder_fatal(errno, "error creating v4 RTP transmission socket");
-    if((rtp_fd6 = socket(AF_INET6,
-                         SOCK_DGRAM,
-                         IPPROTO_UDP)) < 0)
-      disorder_fatal(errno, "error creating v6 RTP transmission socket");
   }
+  if((rtp_fd4 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
+    disorder_fatal(errno, "error creating v4 RTP transmission socket");
+  if((rtp_fd6 = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0)
+    disorder_fatal(errno, "error creating v6 RTP transmission socket");
   /* Configure the socket according to the desired mode */
   switch(rtp_mode) {
   case RTP_MULTICAST: {
     /* Enable multicast options */
     const int ttl = atoi(uaudio_get("multicast-ttl", "1"));
     const int loop = !strcmp(uaudio_get("multicast-loop", "yes"), "yes");
-    switch(dres->ai_family) {
+    switch(dres->sa->sa_family) {
     case PF_INET: {
       if(setsockopt(rtp_fd, IPPROTO_IP, IP_MULTICAST_TTL,
                     &ttl, sizeof ttl) < 0)
@@ -367,21 +366,21 @@ static void rtp_open(void) {
       break;
     }
     default:
-      disorder_fatal(0, "unsupported address family %d", dres->ai_family);
+      disorder_fatal(0, "unsupported address family %d", dres->sa->sa_family);
     }
     disorder_info("multicasting on %s TTL=%d loop=%s", 
-                  format_sockaddr(dres->ai_addr), ttl, loop ? "yes" : "no");
+                  format_sockaddr(dres->sa), ttl, loop ? "yes" : "no");
     break;
   }
   case RTP_UNICAST: {
-    disorder_info("unicasting on %s", format_sockaddr(dres->ai_addr));
+    disorder_info("unicasting on %s", format_sockaddr(dres->sa));
     break;
   }
   case RTP_BROADCAST: {
     if(setsockopt(rtp_fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof one) < 0)
       disorder_fatal(errno, "error setting SO_BROADCAST on broadcast socket");
     disorder_info("broadcasting on %s", 
-                  format_sockaddr(dres->ai_addr));
+                  format_sockaddr(dres->sa));
     break;
   }
   case RTP_REQUEST: {
@@ -390,17 +389,32 @@ static void rtp_open(void) {
   }
   }
   /* Enlarge the socket buffers */
-  hack_send_buffer_size(rtp_fd, "master socket");
+  if (rtp_fd != -1) hack_send_buffer_size(rtp_fd, "master socket");
+  hack_send_buffer_size(rtp_fd4, "IPv4 on-demand socket");
+  hack_send_buffer_size(rtp_fd6, "IPv6 on-demand socket");
   /* We might well want to set additional broadcast- or multicast-related
    * options here */
   if(rtp_mode != RTP_REQUEST) {
-    if(sres && bind(rtp_fd, sres->ai_addr, sres->ai_addrlen) < 0)
+    if(sres && bind(rtp_fd, sres->sa, sres->len) < 0)
       disorder_fatal(errno, "error binding broadcast socket to %s", 
-                     format_sockaddr(sres->ai_addr));
-    if(connect(rtp_fd, dres->ai_addr, dres->ai_addrlen) < 0)
+                     format_sockaddr(sres->sa));
+    if(connect(rtp_fd, dres->sa, dres->len) < 0)
       disorder_fatal(errno, "error connecting broadcast socket to %s", 
-                     format_sockaddr(dres->ai_addr));
+                     format_sockaddr(dres->sa));
   }
+#ifdef IP_MTU_DISCOVER
+  mtu_disc = uaudio_get("rtp-mtu-discovery", "default");
+  do {
+    if(!strcmp(mtu_disc, "yes")) opt = IP_PMTUDISC_DO;
+    else if(!strcmp(mtu_disc, "no")) opt = IP_PMTUDISC_DONT;
+    else break;
+    if(setsockopt(rtp_fd4, IPPROTO_IP, IP_MTU_DISCOVER, &opt, sizeof opt))
+      disorder_fatal(errno, "error setting MTU discovery");
+    if(sres->sa->sa_family == AF_INET &&
+        setsockopt(rtp_fd, IPPROTO_IP, IP_MTU_DISCOVER, &opt, sizeof opt))
+      disorder_fatal(errno, "error setting MTU discovery");
+  } while (0);
+#endif
   if(config->rtp_verbose)
     disorder_info("RTP: prepared socket");
 }
@@ -439,7 +453,7 @@ static void rtp_start(uaudio_callback *callback,
                       userdata,
                       rtp_play,
                       256 / uaudio_sample_size,
-                      (NETWORK_BYTES - sizeof(struct rtp_header))
+                      (rtp_max_payload - sizeof(struct rtp_header))
                       / uaudio_sample_size,
                       0);
   if(config->rtp_verbose)
@@ -448,12 +462,9 @@ static void rtp_start(uaudio_callback *callback,
 
 static void rtp_stop(void) {
   uaudio_thread_stop();
-  close(rtp_fd);
-  rtp_fd = -1;
-  if(rtp_fd6 >= 0) {
-    close(rtp_fd6);
-    rtp_fd6 = -1;
-  }
+  if(rtp_fd >= 0) { close(rtp_fd); rtp_fd = -1; }
+  if(rtp_fd4 >= 0) { close(rtp_fd4); rtp_fd4 = -1; }
+  if(rtp_fd6 >= 0) { close(rtp_fd6); rtp_fd6 = -1; }
 }
 
 static void rtp_configure(void) {
@@ -469,6 +480,9 @@ static void rtp_configure(void) {
   snprintf(buffer, sizeof buffer, "%ld", config->multicast_ttl);
   uaudio_set("multicast-ttl", buffer);
   uaudio_set("multicast-loop", config->multicast_loop ? "yes" : "no");
+  snprintf(buffer, sizeof buffer, "%ld", config->rtp_max_payload);
+  uaudio_set("rtp-max-payload", buffer);
+  uaudio_set("rtp-mtu-discovery", config->rtp_mtu_discovery);
   if(config->rtp_verbose)
     disorder_info("RTP: configured");
 }