GroupsockHelper.hh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**********
  2. This library is free software; you can redistribute it and/or modify it under
  3. the terms of the GNU Lesser General Public License as published by the
  4. Free Software Foundation; either version 3 of the License, or (at your
  5. option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
  6. This library is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  9. more details.
  10. You should have received a copy of the GNU Lesser General Public License
  11. along with this library; if not, write to the Free Software Foundation, Inc.,
  12. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  13. **********/
  14. // "mTunnel" multicast access service
  15. // Copyright (c) 1996-2019 Live Networks, Inc. All rights reserved.
  16. // Helper routines to implement 'group sockets'
  17. // C++ header
  18. #ifndef _GROUPSOCK_HELPER_HH
  19. #define _GROUPSOCK_HELPER_HH
  20. #ifndef _NET_ADDRESS_HH
  21. #include "NetAddress.hh"
  22. #endif
  23. int setupDatagramSocket(UsageEnvironment& env, Port port);
  24. int setupStreamSocket(UsageEnvironment& env,
  25. Port port, Boolean makeNonBlocking = True, Boolean setKeepAlive = False);
  26. int readSocket(UsageEnvironment& env,
  27. int socket, unsigned char* buffer, unsigned bufferSize,
  28. struct sockaddr_in& fromAddress);
  29. Boolean writeSocket(UsageEnvironment& env,
  30. int socket, struct in_addr address, portNumBits portNum/*network byte order*/,
  31. u_int8_t ttlArg,
  32. unsigned char* buffer, unsigned bufferSize);
  33. Boolean writeSocket(UsageEnvironment& env,
  34. int socket, struct in_addr address, portNumBits portNum/*network byte order*/,
  35. unsigned char* buffer, unsigned bufferSize);
  36. // An optimized version of "writeSocket" that omits the "setsockopt()" call to set the TTL.
  37. void ignoreSigPipeOnSocket(int socketNum);
  38. unsigned getSendBufferSize(UsageEnvironment& env, int socket);
  39. unsigned getReceiveBufferSize(UsageEnvironment& env, int socket);
  40. unsigned setSendBufferTo(UsageEnvironment& env,
  41. int socket, unsigned requestedSize);
  42. unsigned setReceiveBufferTo(UsageEnvironment& env,
  43. int socket, unsigned requestedSize);
  44. unsigned increaseSendBufferTo(UsageEnvironment& env,
  45. int socket, unsigned requestedSize);
  46. unsigned increaseReceiveBufferTo(UsageEnvironment& env,
  47. int socket, unsigned requestedSize);
  48. Boolean makeSocketNonBlocking(int sock);
  49. Boolean makeSocketBlocking(int sock, unsigned writeTimeoutInMilliseconds = 0);
  50. // A "writeTimeoutInMilliseconds" value of 0 means: Don't timeout
  51. Boolean setSocketKeepAlive(int sock);
  52. Boolean socketJoinGroup(UsageEnvironment& env, int socket,
  53. netAddressBits groupAddress);
  54. Boolean socketLeaveGroup(UsageEnvironment&, int socket,
  55. netAddressBits groupAddress);
  56. // source-specific multicast join/leave
  57. Boolean socketJoinGroupSSM(UsageEnvironment& env, int socket,
  58. netAddressBits groupAddress,
  59. netAddressBits sourceFilterAddr);
  60. Boolean socketLeaveGroupSSM(UsageEnvironment&, int socket,
  61. netAddressBits groupAddress,
  62. netAddressBits sourceFilterAddr);
  63. Boolean getSourcePort(UsageEnvironment& env, int socket, Port& port);
  64. netAddressBits ourIPAddress(UsageEnvironment& env); // in network order
  65. // IP addresses of our sending and receiving interfaces. (By default, these
  66. // are INADDR_ANY (i.e., 0), specifying the default interface.)
  67. extern netAddressBits SendingInterfaceAddr;
  68. extern netAddressBits ReceivingInterfaceAddr;
  69. // Allocates a randomly-chosen IPv4 SSM (multicast) address:
  70. netAddressBits chooseRandomIPv4SSMAddress(UsageEnvironment& env);
  71. // Returns a simple "hh:mm:ss" string, for use in debugging output (e.g.)
  72. char const* timestampString();
  73. #ifdef HAVE_SOCKADDR_LEN
  74. #define SET_SOCKADDR_SIN_LEN(var) var.sin_len = sizeof var
  75. #else
  76. #define SET_SOCKADDR_SIN_LEN(var)
  77. #endif
  78. #define MAKE_SOCKADDR_IN(var,adr,prt) /*adr,prt must be in network order*/\
  79. struct sockaddr_in var;\
  80. var.sin_family = AF_INET;\
  81. var.sin_addr.s_addr = (adr);\
  82. var.sin_port = (prt);\
  83. SET_SOCKADDR_SIN_LEN(var);
  84. // By default, we create sockets with the SO_REUSE_* flag set.
  85. // If, instead, you want to create sockets without the SO_REUSE_* flags,
  86. // Then enclose the creation code with:
  87. // {
  88. // NoReuse dummy;
  89. // ...
  90. // }
  91. class NoReuse {
  92. public:
  93. NoReuse(UsageEnvironment& env);
  94. ~NoReuse();
  95. private:
  96. UsageEnvironment& fEnv;
  97. };
  98. // Define the "UsageEnvironment"-specific "groupsockPriv" structure:
  99. struct _groupsockPriv { // There should be only one of these allocated
  100. HashTable* socketTable;
  101. int reuseFlag;
  102. };
  103. _groupsockPriv* groupsockPriv(UsageEnvironment& env); // allocates it if necessary
  104. void reclaimGroupsockPriv(UsageEnvironment& env);
  105. #if (defined(__WIN32__) || defined(_WIN32)) && !defined(__MINGW32__)
  106. // For Windoze, we need to implement our own gettimeofday()
  107. extern int gettimeofday(struct timeval*, int*);
  108. #else
  109. #include <sys/time.h>
  110. #endif
  111. // The following are implemented in inet.c:
  112. extern "C" netAddressBits our_inet_addr(char const*);
  113. extern "C" void our_srandom(int x);
  114. extern "C" long our_random();
  115. extern "C" u_int32_t our_random32(); // because "our_random()" returns a 31-bit number
  116. #endif