live
GroupsockHelper.hh
Go to the documentation of this file.
1/**********
2This library is free software; you can redistribute it and/or modify it under
3the terms of the GNU Lesser General Public License as published by the
4Free Software Foundation; either version 3 of the License, or (at your
5option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
6
7This library is distributed in the hope that it will be useful, but WITHOUT
8ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
10more details.
11
12You should have received a copy of the GNU Lesser General Public License
13along with this library; if not, write to the Free Software Foundation, Inc.,
1451 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15**********/
16// "groupsock"
17// Copyright (c) 1996-2024 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#ifndef MSG_NOSIGNAL
29#define MSG_NOSIGNAL 0
30#endif
31
32int setupDatagramSocket(UsageEnvironment& env, Port port, int domain);
33int setupStreamSocket(UsageEnvironment& env, Port port, int domain,
34 Boolean makeNonBlocking = True, Boolean setKeepAlive = False);
35
37 int socket, unsigned char* buffer, unsigned bufferSize,
38 struct sockaddr_storage& fromAddress /*set only if we're a datagram socket*/);
39
41 int socket, struct sockaddr_storage const& addressAndPort,
42 u_int8_t ttlArg,
43 unsigned char* buffer, unsigned bufferSize);
44
46 int socket, struct sockaddr_storage const& addressAndPort,
47 unsigned char* buffer, unsigned bufferSize);
48 // An optimized version of "writeSocket" that omits the "setsockopt()" call to set the TTL.
49
50void ignoreSigPipeOnSocket(int socketNum);
51
52unsigned getSendBufferSize(UsageEnvironment& env, int socket);
53unsigned getReceiveBufferSize(UsageEnvironment& env, int socket);
55 int socket, unsigned requestedSize);
57 int socket, unsigned requestedSize);
59 int socket, unsigned requestedSize);
61 int socket, unsigned requestedSize);
62
64Boolean makeSocketBlocking(int sock, unsigned writeTimeoutInMilliseconds = 0);
65 // A "writeTimeoutInMilliseconds" value of 0 means: Don't timeout
67
69 struct sockaddr_storage const& groupAddress);
71 struct sockaddr_storage const& groupAddress);
72
73// source-specific multicast join/leave
75 struct sockaddr_storage const& groupAddress,
76 struct sockaddr_storage const& sourceFilterAddr);
78 struct sockaddr_storage const& groupAddress,
79 struct sockaddr_storage const& sourceFilterAddr);
80
81Boolean getSourcePort(UsageEnvironment& env, int socket, int domain, Port& port);
82
85
89 // returns True if we have either an IPv4 or an IPv6 address
90
91// IPv4 addresses of our sending and receiving interfaces. (By default, these
92// are INADDR_ANY (i.e., 0), specifying the default interface.)
95extern in6_addr ReceivingInterfaceAddr6;
96
97// Allocates a randomly-chosen IPv4 SSM (multicast) address:
99
100// Returns a simple "hh:mm:ss" string, for use in debugging output (e.g.)
101char const* timestampString();
102
103
104#ifdef HAVE_SOCKADDR_LEN
105#define SET_SOCKADDR_SIN_LEN(var) var.sin_len = sizeof var
106#define SET_SOCKADDR_SIN6_LEN(var) var.sin6_len = sizeof var
107#else
108#define SET_SOCKADDR_SIN_LEN(var)
109#define SET_SOCKADDR_SIN6_LEN(var)
110#endif
111
112#define MAKE_SOCKADDR_IN(var,adr,prt) /*adr,prt must be in network order*/\
113 struct sockaddr_in var;\
114 var.sin_family = AF_INET;\
115 var.sin_addr.s_addr = (adr);\
116 var.sin_port = (prt);\
117 SET_SOCKADDR_SIN_LEN(var);
118#define MAKE_SOCKADDR_IN6(var,adr,prt) /*adr,prt must be in network order*/\
119 struct sockaddr_in6 var;\
120 memset(&var, 0, sizeof var);\
121 var.sin6_family = AF_INET6;\
122 var.sin6_addr=adr;\
123 var.sin6_port = (prt);\
124 SET_SOCKADDR_SIN6_LEN(var);
125
126
127// By default, we create sockets with the SO_REUSE_* flag set.
128// If, instead, you want to create sockets without the SO_REUSE_* flags,
129// Then enclose the creation code with:
130// {
131// NoReuse dummy;
132// ...
133// }
134class NoReuse {
135public:
138
139private:
141};
142
143
144// Define the "UsageEnvironment"-specific "groupsockPriv" structure:
145
146struct _groupsockPriv { // There should be only one of these allocated
149};
150_groupsockPriv* groupsockPriv(UsageEnvironment& env); // allocates it if necessary
152
153
154#if (defined(__WIN32__) || defined(_WIN32)) && !defined(__MINGW32__)
155// For Windoze, we need to implement our own gettimeofday()
156extern int gettimeofday(struct timeval*, int*);
157#else
158#include <sys/time.h>
159#endif
160
161// The following are implemented in inet.c:
162extern "C" void our_srandom(int x);
163extern "C" long our_random();
164extern "C" u_int32_t our_random32(); // because "our_random()" returns a 31-bit number
165
166#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)
in6_addr ReceivingInterfaceAddr6
ipv4AddressBits chooseRandomIPv4SSMAddress(UsageEnvironment &env)
Boolean makeSocketNonBlocking(int sock)
Boolean socketJoinGroup(UsageEnvironment &env, int socket, struct sockaddr_storage const &groupAddress)
Boolean weHaveAnIPv6Address(UsageEnvironment &env)
char const * timestampString()
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)
ipv6AddressBits const & ourIPv6Address(UsageEnvironment &env)
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)
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()
_groupsockPriv * groupsockPriv(UsageEnvironment &env)
Boolean weHaveAnIPAddress(UsageEnvironment &env)
Boolean socketJoinGroupSSM(UsageEnvironment &env, int socket, struct sockaddr_storage const &groupAddress, struct sockaddr_storage const &sourceFilterAddr)
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