multicast.c 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. /*
  2. * Copyright (c) 1993 W. Richard Stevens. All rights reserved.
  3. * Permission to use or modify this software and its documentation only for
  4. * educational purposes and without fee is hereby granted, provided that
  5. * the above copyright notice appear in all copies. The author makes no
  6. * representations about the suitability of this software for any purpose.
  7. * It is provided "as is" without express or implied warranty.
  8. */
  9. #include "sock.h"
  10. void
  11. join_mcast(int fd, struct sockaddr_in *sin)
  12. {
  13. #ifdef IP_ADD_MEMBERSHIP /* only include if host supports mcasting */
  14. u_long inaddr;
  15. struct ip_mreq mreq;
  16. inaddr = sin->sin_addr.s_addr;
  17. if (IN_MULTICAST(inaddr) == 0)
  18. return; /* not a multicast address */
  19. mreq.imr_multiaddr.s_addr = inaddr;
  20. mreq.imr_interface.s_addr = htonl(INADDR_ANY); /* need way to change */
  21. if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
  22. sizeof(mreq)) == -1 )
  23. err_sys("IP_ADD_MEMBERSHIP error");
  24. if (verbose)
  25. fprintf(stderr, "multicast group joined\n");
  26. #endif /* IP_ADD_MEMBERSHIP */
  27. }