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// "mTunnel" multicast access service
17// Copyright (c) 1996-2020 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
28int setupDatagramSocket(UsageEnvironment& env, Port port);
29int setupStreamSocket(UsageEnvironment& env,
30 Port port, Boolean makeNonBlocking = True, Boolean setKeepAlive = False);
31
32int readSocket(UsageEnvironment& env,
33 int socket, unsigned char* buffer, unsigned bufferSize,
34 struct sockaddr_in& fromAddress);
35
36Boolean writeSocket(UsageEnvironment& env,
37 int socket, struct in_addr address, portNumBits portNum/*network byte order*/,
38 u_int8_t ttlArg,
39 unsigned char* buffer, unsigned bufferSize);
40
41Boolean writeSocket(UsageEnvironment& env,
42 int socket, struct in_addr address, portNumBits portNum/*network byte order*/,
43 unsigned char* buffer, unsigned bufferSize);
44 // An optimized version of "writeSocket" that omits the "setsockopt()" call to set the TTL.
45
46void ignoreSigPipeOnSocket(int socketNum);
47
48unsigned getSendBufferSize(UsageEnvironment& env, int socket);
49unsigned getReceiveBufferSize(UsageEnvironment& env, int socket);
50unsigned setSendBufferTo(UsageEnvironment& env,
51 int socket, unsigned requestedSize);
52unsigned setReceiveBufferTo(UsageEnvironment& env,
53 int socket, unsigned requestedSize);
54unsigned increaseSendBufferTo(UsageEnvironment& env,
55 int socket, unsigned requestedSize);
56unsigned increaseReceiveBufferTo(UsageEnvironment& env,
57 int socket, unsigned requestedSize);
58
59Boolean makeSocketNonBlocking(int sock);
60Boolean makeSocketBlocking(int sock, unsigned writeTimeoutInMilliseconds = 0);
61 // A "writeTimeoutInMilliseconds" value of 0 means: Don't timeout
62Boolean setSocketKeepAlive(int sock);
63
64Boolean socketJoinGroup(UsageEnvironment& env, int socket,
65 netAddressBits groupAddress);
66Boolean socketLeaveGroup(UsageEnvironment&, int socket,
67 netAddressBits groupAddress);
68
69// source-specific multicast join/leave
70Boolean socketJoinGroupSSM(UsageEnvironment& env, int socket,
71 netAddressBits groupAddress,
72 netAddressBits sourceFilterAddr);
73Boolean socketLeaveGroupSSM(UsageEnvironment&, int socket,
74 netAddressBits groupAddress,
75 netAddressBits sourceFilterAddr);
76
77Boolean getSourcePort(UsageEnvironment& env, int socket, Port& port);
78
79netAddressBits ourIPAddress(UsageEnvironment& env); // in network order
80
81// IP addresses of our sending and receiving interfaces. (By default, these
82// are INADDR_ANY (i.e., 0), specifying the default interface.)
83extern netAddressBits SendingInterfaceAddr;
84extern netAddressBits ReceivingInterfaceAddr;
85
86// Allocates a randomly-chosen IPv4 SSM (multicast) address:
87netAddressBits chooseRandomIPv4SSMAddress(UsageEnvironment& env);
88
89// Returns a simple "hh:mm:ss" string, for use in debugging output (e.g.)
90char const* timestampString();
91
92
93#ifdef HAVE_SOCKADDR_LEN
94#define SET_SOCKADDR_SIN_LEN(var) var.sin_len = sizeof var
95#else
96#define SET_SOCKADDR_SIN_LEN(var)
97#endif
98
99#define MAKE_SOCKADDR_IN(var,adr,prt) /*adr,prt must be in network order*/\
100 struct sockaddr_in var;\
101 var.sin_family = AF_INET;\
102 var.sin_addr.s_addr = (adr);\
103 var.sin_port = (prt);\
104 SET_SOCKADDR_SIN_LEN(var);
105
106
107// By default, we create sockets with the SO_REUSE_* flag set.
108// If, instead, you want to create sockets without the SO_REUSE_* flags,
109// Then enclose the creation code with:
110// {
111// NoReuse dummy;
112// ...
113// }
114class NoReuse {
115public:
116 NoReuse(UsageEnvironment& env);
117 ~NoReuse();
118
119private:
120 UsageEnvironment& fEnv;
121};
122
123
124// Define the "UsageEnvironment"-specific "groupsockPriv" structure:
125
126struct _groupsockPriv { // There should be only one of these allocated
127 HashTable* socketTable;
128 int reuseFlag;
129};
130_groupsockPriv* groupsockPriv(UsageEnvironment& env); // allocates it if necessary
131void reclaimGroupsockPriv(UsageEnvironment& env);
132
133
134#if (defined(__WIN32__) || defined(_WIN32)) && !defined(__MINGW32__)
135// For Windoze, we need to implement our own gettimeofday()
136extern int gettimeofday(struct timeval*, int*);
137#else
138#include <sys/time.h>
139#endif
140
141// The following are implemented in inet.c:
142extern "C" netAddressBits our_inet_addr(char const*);
143extern "C" void our_srandom(int x);
144extern "C" long our_random();
145extern "C" u_int32_t our_random32(); // because "our_random()" returns a 31-bit number
146
147#endif
148