- /* Listen for inbound audio data */
- if(!(res = get_address(&sl, &prefs, &sockname)))
- exit(1);
- info("listening on %s", sockname);
- if((rtpfd = socket(res->ai_family,
- res->ai_socktype,
- res->ai_protocol)) < 0)
- fatal(errno, "error creating socket");
- if(bind(rtpfd, res->ai_addr, res->ai_addrlen) < 0)
- fatal(errno, "error binding socket to %s", sockname);
- if(multicast_group) {
- if((n = getaddrinfo(multicast_group, 0, &prefs, &res)))
- fatal(0, "getaddrinfo %s: %s", multicast_group, gai_strerror(n));
- switch(res->ai_family) {
- case PF_INET:
- mreq.imr_multiaddr = ((struct sockaddr_in *)res->ai_addr)->sin_addr;
- mreq.imr_interface.s_addr = 0; /* use primary interface */
- if(setsockopt(rtpfd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
- &mreq, sizeof mreq) < 0)
- fatal(errno, "error calling setsockopt IP_ADD_MEMBERSHIP");
- break;
- case PF_INET6:
- mreq6.ipv6mr_multiaddr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
- memset(&mreq6.ipv6mr_interface, 0, sizeof mreq6.ipv6mr_interface);
- if(setsockopt(rtpfd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
- &mreq6, sizeof mreq6) < 0)
- fatal(errno, "error calling setsockopt IPV6_JOIN_GROUP");
- break;
- default:
- fatal(0, "unsupported address family %d", res->ai_family);
+ disorder_info("version "VERSION" process ID %lu",
+ (unsigned long)getpid());
+ struct sockaddr *addr;
+ socklen_t addr_len;
+ if(!strcmp(sl.s[0], "-")) {
+ /* Pick address family to match known-working connectivity to the server */
+ 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(bestifa == NULL
+ || compare_interfaces(ifa, bestifa, family) > 0)
+ bestifa = ifa;
+ }
+ if(!bestifa)
+ disorder_fatal(0, "failed to select a network interface");
+ family = bestifa->ifa_addr->sa_family;
+ if((rtpfd = socket(family,
+ SOCK_DGRAM,
+ IPPROTO_UDP)) < 0)
+ disorder_fatal(errno, "error creating socket (family %d)", family);
+ /* Bind the address */
+ if(bind(rtpfd, bestifa->ifa_addr,
+ 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 {
+ /* 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));