live
GroupsockHelper.hh
Go to the documentation of this file.
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 
7 This library is distributed in the hope that it will be useful, but WITHOUT
8 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
10 more details.
11 
12 You should have received a copy of the GNU Lesser General Public License
13 along with this library; if not, write to the Free Software Foundation, Inc.,
14 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15 **********/
16 // "groupsock"
17 // Copyright (c) 1996-2021 Live Networks, Inc. All rights reserved.
18 // Helper routines to implement 'group sockets'
19 // C++ header
20 
21 #ifndef _GROUPSOCK_HELPER_HH
22 #define _GROUPSOCK_HELPER_HH
23 
24 #ifndef _NET_ADDRESS_HH
25 #include "NetAddress.hh"
26 #endif
27 
28 int setupDatagramSocket(UsageEnvironment& env, Port port, int domain);
29 int setupStreamSocket(UsageEnvironment& env, Port port, int domain,
30  Boolean makeNonBlocking = True, Boolean setKeepAlive = False);
31 
33  int socket, unsigned char* buffer, unsigned bufferSize,
34  struct sockaddr_storage& fromAddress /*set only if we're a datagram socket*/);
35 
37  int socket, struct sockaddr_storage const& addressAndPort,
38  u_int8_t ttlArg,
39  unsigned char* buffer, unsigned bufferSize);
40 
42  int socket, struct sockaddr_storage const& addressAndPort,
43  unsigned char* buffer, unsigned bufferSize);
44  // An optimized version of "writeSocket" that omits the "setsockopt()" call to set the TTL.
45 
46 void ignoreSigPipeOnSocket(int socketNum);
47 
48 unsigned getSendBufferSize(UsageEnvironment& env, int socket);
49 unsigned getReceiveBufferSize(UsageEnvironment& env, int socket);
51  int socket, unsigned requestedSize);
53  int socket, unsigned requestedSize);
55  int socket, unsigned requestedSize);
57  int socket, unsigned requestedSize);
58 
60 Boolean makeSocketBlocking(int sock, unsigned writeTimeoutInMilliseconds = 0);
61  // A "writeTimeoutInMilliseconds" value of 0 means: Don't timeout
63 
65  struct sockaddr_storage const& groupAddress);
67  struct sockaddr_storage const& groupAddress);
68 
69 // source-specific multicast join/leave
71  struct sockaddr_storage const& groupAddress,
72  struct sockaddr_storage const& sourceFilterAddr);
74  struct sockaddr_storage const& groupAddress,
75  struct sockaddr_storage const& sourceFilterAddr);
76 
77 Boolean getSourcePort(UsageEnvironment& env, int socket, int domain, Port& port);
78 
81 
85  // returns True if we have either an IPv4 or an IPv6 address
86 
87 // IPv4 addresses of our sending and receiving interfaces. (By default, these
88 // are INADDR_ANY (i.e., 0), specifying the default interface.)
91 
92 // Allocates a randomly-chosen IPv4 SSM (multicast) address:
94 
95 // Returns a simple "hh:mm:ss" string, for use in debugging output (e.g.)
96 char const* timestampString();
97 
98 
99 #ifdef HAVE_SOCKADDR_LEN
100 #define SET_SOCKADDR_SIN_LEN(var) var.sin_len = sizeof var
101 #define SET_SOCKADDR_SIN6_LEN(var) var.sin6_len = sizeof var
102 #else
103 #define SET_SOCKADDR_SIN_LEN(var)
104 #define SET_SOCKADDR_SIN6_LEN(var)
105 #endif
106 
107 #define MAKE_SOCKADDR_IN(var,adr,prt) /*adr,prt must be in network order*/\
108  struct sockaddr_in var;\
109  var.sin_family = AF_INET;\
110  var.sin_addr.s_addr = (adr);\
111  var.sin_port = (prt);\
112  SET_SOCKADDR_SIN_LEN(var);
113 #define MAKE_SOCKADDR_IN6(var,prt) /*adr,prt must be in network order*/\
114  struct sockaddr_in6 var;\
115  memset(&var, 0, sizeof var);\
116  var.sin6_family = AF_INET6;\
117  var.sin6_port = (prt);\
118  SET_SOCKADDR_SIN6_LEN(var);
119 
120 
121 // By default, we create sockets with the SO_REUSE_* flag set.
122 // If, instead, you want to create sockets without the SO_REUSE_* flags,
123 // Then enclose the creation code with:
124 // {
125 // NoReuse dummy;
126 // ...
127 // }
128 class NoReuse {
129 public:
132 
133 private:
135 };
136 
137 
138 // Define the "UsageEnvironment"-specific "groupsockPriv" structure:
139 
140 struct _groupsockPriv { // There should be only one of these allocated
143 };
144 _groupsockPriv* groupsockPriv(UsageEnvironment& env); // allocates it if necessary
146 
147 
148 #if (defined(__WIN32__) || defined(_WIN32)) && !defined(__MINGW32__)
149 // For Windoze, we need to implement our own gettimeofday()
150 extern int gettimeofday(struct timeval*, int*);
151 #else
152 #include <sys/time.h>
153 #endif
154 
155 // The following are implemented in inet.c:
156 extern "C" void our_srandom(int x);
157 extern "C" long our_random();
158 extern "C" u_int32_t our_random32(); // because "our_random()" returns a 31-bit number
159 
160 #endif
const Boolean False
Definition: Boolean.hh:28
const Boolean True
Definition: Boolean.hh:31
unsigned char Boolean
Definition: Boolean.hh:25
unsigned setSendBufferTo(UsageEnvironment &env, int socket, unsigned requestedSize)
ipv4AddressBits chooseRandomIPv4SSMAddress(UsageEnvironment &env)
Boolean makeSocketNonBlocking(int sock)
Boolean socketJoinGroup(UsageEnvironment &env, int socket, struct sockaddr_storage const &groupAddress)
Boolean weHaveAnIPv6Address(UsageEnvironment &env)
Boolean writeSocket(UsageEnvironment &env, int socket, struct sockaddr_storage const &addressAndPort, u_int8_t ttlArg, unsigned char *buffer, unsigned bufferSize)
unsigned getReceiveBufferSize(UsageEnvironment &env, int socket)
Boolean socketLeaveGroup(UsageEnvironment &, int socket, struct sockaddr_storage const &groupAddress)
Boolean setSocketKeepAlive(int sock)
Boolean socketLeaveGroupSSM(UsageEnvironment &, int socket, struct sockaddr_storage const &groupAddress, struct sockaddr_storage const &sourceFilterAddr)
int setupStreamSocket(UsageEnvironment &env, Port port, int domain, Boolean makeNonBlocking=True, Boolean setKeepAlive=False)
ipv4AddressBits SendingInterfaceAddr
unsigned setReceiveBufferTo(UsageEnvironment &env, int socket, unsigned requestedSize)
Boolean weHaveAnIPv4Address(UsageEnvironment &env)
Boolean makeSocketBlocking(int sock, unsigned writeTimeoutInMilliseconds=0)
int setupDatagramSocket(UsageEnvironment &env, Port port, int domain)
_groupsockPriv * groupsockPriv(UsageEnvironment &env)
unsigned increaseReceiveBufferTo(UsageEnvironment &env, int socket, unsigned requestedSize)
ipv4AddressBits ourIPv4Address(UsageEnvironment &env)
int readSocket(UsageEnvironment &env, int socket, unsigned char *buffer, unsigned bufferSize, struct sockaddr_storage &fromAddress)
void reclaimGroupsockPriv(UsageEnvironment &env)
u_int32_t our_random32()
Boolean weHaveAnIPAddress(UsageEnvironment &env)
Boolean socketJoinGroupSSM(UsageEnvironment &env, int socket, struct sockaddr_storage const &groupAddress, struct sockaddr_storage const &sourceFilterAddr)
ipv6AddressBits const & ourIPv6Address(UsageEnvironment &env)
char const * timestampString()
Boolean getSourcePort(UsageEnvironment &env, int socket, int domain, Port &port)
void ignoreSigPipeOnSocket(int socketNum)
ipv4AddressBits ReceivingInterfaceAddr
long our_random()
unsigned increaseSendBufferTo(UsageEnvironment &env, int socket, unsigned requestedSize)
void our_srandom(int x)
unsigned getSendBufferSize(UsageEnvironment &env, int socket)
u_int32_t ipv4AddressBits
Definition: NetAddress.hh:38
u_int8_t ipv6AddressBits[16]
Definition: NetAddress.hh:39
NoReuse(UsageEnvironment &env)
UsageEnvironment & fEnv
HashTable * socketTable