+ /* If no address was given, pick something sensible based on the known-
+ * working connectivity to the server */
+ if(!node) {
+ int family = disorder_client_af(c);
+ /* Get a list of interfaces */
+ struct ifaddrs *ifa, *bestifa = NULL;
+ if(getifaddrs(&ifa) < 0)
+ disorder_fatal(errno, "error calling getifaddrs");
+ /* Try to pick a good one */
+ for(; ifa; ifa = ifa->ifa_next) {
+ if(!ifa->ifa_addr) continue;
+ if(bestifa == NULL
+ || compare_interfaces(ifa, bestifa, family) > 0)
+ bestifa = ifa;
+ }
+ if(!bestifa)
+ disorder_fatal(0, "failed to select a network interface");
+ sa = bestifa->ifa_addr;
+ switch(sa->sa_family) {
+ case AF_INET: ((struct sockaddr_in *)sa)->sin_port = 0; break;
+ case AF_INET6: ((struct sockaddr_in6 *)sa)->sin6_port = 0; break;
+ default: assert(!"unexpected address family");
+ }
+ prefs.ai_family = sa->sa_family;
+ }
+ /* If we have an address or port to resolve then do that now */
+ if (node || svc) {
+ struct addrinfo *ai;
+ char errbuf[1024];
+ int rc;
+ if((rc = getaddrinfo(node, svc, &prefs, &ai)))
+ disorder_fatal(0, "failed to resolve address `%s' and service `%s': %s",
+ node ? node : "-", svc ? svc : "-",
+ format_error(ec_getaddrinfo, rc,
+ errbuf, sizeof(errbuf)));
+ if(!sa)
+ sa = ai->ai_addr;
+ else {
+ assert(sa->sa_family == ai->ai_addr->sa_family);
+ switch(sa->sa_family) {
+ case AF_INET:
+ ((struct sockaddr_in *)sa)->sin_port =
+ ((struct sockaddr_in *)ai->ai_addr)->sin_port;
+ break;
+ case AF_INET6:
+ ((struct sockaddr_in6 *)sa)->sin6_port =
+ ((struct sockaddr_in6 *)ai->ai_addr)->sin6_port;
+ break;
+ default:
+ assert(!"unexpected address family");
+ }
+ }
+ }
+ if((rtpfd = socket(sa->sa_family, SOCK_DGRAM, IPPROTO_UDP)) < 0)
+ disorder_fatal(errno, "error creating socket (family %d)",
+ sa->sa_family);
+ /* Bind the address */
+ if(bind(rtpfd, sa,
+ sa->sa_family == AF_INET
+ ? sizeof (struct sockaddr_in) : sizeof (struct sockaddr_in6)) < 0)
+ disorder_fatal(errno, "error binding socket");
+ static struct sockaddr_storage bound_address;
+ addr = (struct sockaddr *)&bound_address;
+ addr_len = sizeof bound_address;
+ if(getsockname(rtpfd, addr, &addr_len) < 0)
+ disorder_fatal(errno, "error getting socket address");
+ /* Convert to string */
+ char addrname[128], portname[32];
+ if(getnameinfo(addr, addr_len,
+ addrname, sizeof addrname,
+ portname, sizeof portname,
+ NI_NUMERICHOST|NI_NUMERICSERV) < 0)
+ disorder_fatal(errno, "getnameinfo");
+ /* Ask for audio data */
+ if(disorder_rtp_request(c, addrname, portname)) exit(EXIT_FAILURE);
+ /* Report what we did */
+ disorder_info("listening on %s", format_sockaddr(addr));
+ } else {
+ if(sl.n > 2) disorder_fatal(0, "too many address components");
+ /* Look up address and port */
+ if(!(res = get_address(&sl, &prefs, &sockname)))
+ exit(1);
+ addr = res->ai_addr;
+ addr_len = res->ai_addrlen;
+ /* Create the socket */
+ if((rtpfd = socket(res->ai_family,
+ res->ai_socktype,
+ res->ai_protocol)) < 0)
+ disorder_fatal(errno, "error creating socket");
+ /* Allow multiple listeners */
+ xsetsockopt(rtpfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
+ is_multicast = multicast(addr);
+ /* The multicast and unicast/broadcast cases are different enough that they
+ * are totally split. Trying to find commonality between them causes more
+ * trouble that it's worth. */
+ if(is_multicast) {
+ /* Stash the multicast group address */
+ memcpy(&mgroup, addr, addr_len);
+ switch(res->ai_addr->sa_family) {
+ case AF_INET:
+ mgroup.in.sin_port = 0;
+ break;
+ case AF_INET6:
+ mgroup.in6.sin6_port = 0;
+ break;
+ default:
+ disorder_fatal(0, "unsupported address family %d",
+ (int)addr->sa_family);
+ }
+ /* Bind to to the multicast group address */
+ if(bind(rtpfd, addr, addr_len) < 0)
+ disorder_fatal(errno, "error binding socket to %s",
+ format_sockaddr(addr));
+ /* Add multicast group membership */
+ switch(mgroup.sa.sa_family) {
+ case PF_INET:
+ mreq.imr_multiaddr = mgroup.in.sin_addr;
+ mreq.imr_interface.s_addr = 0; /* use primary interface */
+ if(setsockopt(rtpfd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
+ &mreq, sizeof mreq) < 0)
+ disorder_fatal(errno, "error calling setsockopt IP_ADD_MEMBERSHIP");
+ break;
+ case PF_INET6:
+ mreq6.ipv6mr_multiaddr = mgroup.in6.sin6_addr;
+ memset(&mreq6.ipv6mr_interface, 0, sizeof mreq6.ipv6mr_interface);
+ if(setsockopt(rtpfd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
+ &mreq6, sizeof mreq6) < 0)
+ disorder_fatal(errno, "error calling setsockopt IPV6_JOIN_GROUP");
+ break;
+ default:
+ disorder_fatal(0, "unsupported address family %d", res->ai_family);
+ }
+ /* Report what we did */
+ disorder_info("listening on %s multicast group %s",
+ format_sockaddr(addr), format_sockaddr(&mgroup.sa));
+ } else {
+ /* Bind to 0/port */
+ switch(addr->sa_family) {
+ case AF_INET: {
+ struct sockaddr_in *in = (struct sockaddr_in *)addr;
+
+ memset(&in->sin_addr, 0, sizeof (struct in_addr));
+ break;
+ }
+ case AF_INET6: {
+ struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr;
+
+ memset(&in6->sin6_addr, 0, sizeof (struct in6_addr));
+ break;
+ }
+ default:
+ disorder_fatal(0, "unsupported family %d", (int)addr->sa_family);
+ }
+ if(bind(rtpfd, addr, addr_len) < 0)
+ disorder_fatal(errno, "error binding socket to %s",
+ format_sockaddr(addr));
+ /* Report what we did */
+ disorder_info("listening on %s", format_sockaddr(addr));
+ }
+ }