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 | // "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 | |
28 | int setupDatagramSocket(UsageEnvironment& env, Port port); |
29 | int setupStreamSocket(UsageEnvironment& env, |
30 | Port port, Boolean makeNonBlocking = True, Boolean setKeepAlive = False); |
31 | |
32 | int readSocket(UsageEnvironment& env, |
33 | int socket, unsigned char* buffer, unsigned bufferSize, |
34 | struct sockaddr_in& fromAddress); |
35 | |
36 | Boolean 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 | |
41 | Boolean 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 | |
46 | void ignoreSigPipeOnSocket(int socketNum); |
47 | |
48 | unsigned getSendBufferSize(UsageEnvironment& env, int socket); |
49 | unsigned getReceiveBufferSize(UsageEnvironment& env, int socket); |
50 | unsigned setSendBufferTo(UsageEnvironment& env, |
51 | int socket, unsigned requestedSize); |
52 | unsigned setReceiveBufferTo(UsageEnvironment& env, |
53 | int socket, unsigned requestedSize); |
54 | unsigned increaseSendBufferTo(UsageEnvironment& env, |
55 | int socket, unsigned requestedSize); |
56 | unsigned increaseReceiveBufferTo(UsageEnvironment& env, |
57 | int socket, unsigned requestedSize); |
58 | |
59 | Boolean makeSocketNonBlocking(int sock); |
60 | Boolean makeSocketBlocking(int sock, unsigned writeTimeoutInMilliseconds = 0); |
61 | // A "writeTimeoutInMilliseconds" value of 0 means: Don't timeout |
62 | Boolean setSocketKeepAlive(int sock); |
63 | |
64 | Boolean socketJoinGroup(UsageEnvironment& env, int socket, |
65 | netAddressBits groupAddress); |
66 | Boolean socketLeaveGroup(UsageEnvironment&, int socket, |
67 | netAddressBits groupAddress); |
68 | |
69 | // source-specific multicast join/leave |
70 | Boolean socketJoinGroupSSM(UsageEnvironment& env, int socket, |
71 | netAddressBits groupAddress, |
72 | netAddressBits sourceFilterAddr); |
73 | Boolean socketLeaveGroupSSM(UsageEnvironment&, int socket, |
74 | netAddressBits groupAddress, |
75 | netAddressBits sourceFilterAddr); |
76 | |
77 | Boolean getSourcePort(UsageEnvironment& env, int socket, Port& port); |
78 | |
79 | netAddressBits 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.) |
83 | extern netAddressBits SendingInterfaceAddr; |
84 | extern netAddressBits ReceivingInterfaceAddr; |
85 | |
86 | // Allocates a randomly-chosen IPv4 SSM (multicast) address: |
87 | netAddressBits chooseRandomIPv4SSMAddress(UsageEnvironment& env); |
88 | |
89 | // Returns a simple "hh:mm:ss" string, for use in debugging output (e.g.) |
90 | char 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 | // } |
114 | class NoReuse { |
115 | public: |
116 | NoReuse(UsageEnvironment& env); |
117 | ~NoReuse(); |
118 | |
119 | private: |
120 | UsageEnvironment& fEnv; |
121 | }; |
122 | |
123 | |
124 | // Define the "UsageEnvironment"-specific "groupsockPriv" structure: |
125 | |
126 | struct _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 |
131 | void reclaimGroupsockPriv(UsageEnvironment& env); |
132 | |
133 | |
134 | #if (defined(__WIN32__) || defined(_WIN32)) && !defined(__MINGW32__) |
135 | // For Windoze, we need to implement our own gettimeofday() |
136 | extern int gettimeofday(struct timeval*, int*); |
137 | #else |
138 | #include <sys/time.h> |
139 | #endif |
140 | |
141 | // The following are implemented in inet.c: |
142 | extern "C" netAddressBits our_inet_addr(char const*); |
143 | extern "C" void our_srandom(int x); |
144 | extern "C" long our_random(); |
145 | extern "C" u_int32_t our_random32(); // because "our_random()" returns a 31-bit number |
146 | |
147 | #endif |
148 | |