multicastlisten.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. ** JNetLib
  3. ** Copyright (C) 2008 Nullsoft, Inc.
  4. ** Author: Ben Allison
  5. ** File: multicastlisten.cpp - JNL Multicast UDP listen implementation
  6. ** License: see jnetlib.h
  7. */
  8. #include "netinc.h"
  9. #include "util.h"
  10. #include "multicastlisten.h"
  11. #include "foundation/error.h"
  12. #include <new>
  13. int CreateMulticastListener(JNL_UDPConnection **connection, const char *mcast_ip, unsigned short port, size_t sendbufsize, size_t recvbufsize)
  14. {
  15. char portString[32] = {0};
  16. if (port)
  17. sprintf(portString, "%d", (int)port);
  18. addrinfo *res=0;
  19. addrinfo hints;
  20. memset(&hints, 0, sizeof(hints));
  21. hints.ai_family = AF_INET; /* IPv4 only for now until we get IPv6 multicast registration working */
  22. hints.ai_socktype = SOCK_DGRAM;
  23. hints.ai_flags = AI_NUMERICHOST | AI_PASSIVE;
  24. hints.ai_protocol = IPPROTO_UDP;
  25. if (getaddrinfo(NULL, port?portString:0, &hints, &res) == 0)
  26. {
  27. SOCKET m_socket = ::socket(res->ai_family,res->ai_socktype, res->ai_protocol);
  28. if (m_socket < 0)
  29. {
  30. freeaddrinfo(res);
  31. return NErr_Error;
  32. }
  33. else
  34. {
  35. SET_SOCK_BLOCK(m_socket,0);
  36. int bflag = 1;
  37. if (setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, (const char *)&bflag, sizeof(bflag)))
  38. {
  39. /*int err = ERRNO;
  40. err=err;
  41. printf("SO_REUSEADDR error: %d\n", err);*/
  42. }
  43. #if defined(__FreeBSD__) || defined(__APPLE__)
  44. bflag=1; // in case it magically got unset above
  45. setsockopt(m_socket, SOL_SOCKET, SO_REUSEPORT, (const char *)&bflag, sizeof(bflag));
  46. #endif
  47. if (::bind(m_socket, res->ai_addr, (int)res->ai_addrlen))
  48. {
  49. closesocket(m_socket);
  50. return NErr_Error;
  51. }
  52. else
  53. {
  54. // TODO: ipv6 with IPV6_ADD_MEMBERSHIP and ipv6_mreq
  55. sockaddr_in *ipv4 = (sockaddr_in *)res->ai_addr;
  56. /* join multicast group */
  57. ip_mreq ssdpMcastAddr;
  58. memset(&ssdpMcastAddr, 0, sizeof(ssdpMcastAddr));
  59. ssdpMcastAddr.imr_interface = ipv4->sin_addr;
  60. ssdpMcastAddr.imr_multiaddr.s_addr = inet_addr(mcast_ip);
  61. if (setsockopt(m_socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char *)&ssdpMcastAddr, sizeof(ssdpMcastAddr)))
  62. {
  63. closesocket(m_socket);
  64. freeaddrinfo(res);
  65. return NErr_Error;
  66. }
  67. /* Set multicast interface. */
  68. in_addr addr;
  69. memset(&addr, 0, sizeof(addr));
  70. addr = ipv4->sin_addr;
  71. if (setsockopt(m_socket, IPPROTO_IP, IP_MULTICAST_IF, (const char *)&addr, sizeof(addr)))
  72. {
  73. /*int err = ERRNO;
  74. err=err;
  75. printf("IP_MULTICAST_IF error: %d\n", err);*/
  76. /* This is probably not a critical error, so let's continue. */
  77. }
  78. /* set TTL to 4 */
  79. uint8_t ttl=4;
  80. if (setsockopt(m_socket, IPPROTO_IP, IP_MULTICAST_TTL, (const char *)&ttl, sizeof(ttl)))
  81. {
  82. /*int err = ERRNO;
  83. err=err;
  84. printf("IP_MULTICAST_TTL error: %d\n", err);*/
  85. /* This is probably not a critical error, so let's continue. */
  86. }
  87. int option = 1;
  88. if (setsockopt(m_socket, SOL_SOCKET, SO_BROADCAST, (const char *)&option, sizeof(option)) != 0)
  89. {
  90. closesocket(m_socket);
  91. freeaddrinfo(res);
  92. return NErr_Error;
  93. }
  94. }
  95. }
  96. freeaddrinfo(res);
  97. JNL_UDPConnection *c=new (std::nothrow) JNL_UDPConnection();
  98. if (!c)
  99. {
  100. closesocket(m_socket);
  101. return NErr_OutOfMemory;
  102. }
  103. c->open((int)m_socket, NULL, sendbufsize, recvbufsize);
  104. *connection = c;
  105. return NErr_Success;
  106. }
  107. else
  108. {
  109. return NErr_Error;
  110. }
  111. }