1 /*
2
3 This file is a part of JRTPLIB
4 Copyright (c) 1999-2017 Jori Liesenborgs
5
6 Contact: jori.liesenborgs@gmail.com
7
8 This library was developed at the Expertise Centre for Digital Media
9 (http://www.edm.uhasselt.be), a research center of the Hasselt University
10 (http://www.uhasselt.be). The library is based upon work done for
11 my thesis at the School for Knowledge Technology (Belgium/The Netherlands).
12
13 Permission is hereby granted, free of charge, to any person obtaining a
14 copy of this software and associated documentation files (the "Software"),
15 to deal in the Software without restriction, including without limitation
16 the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 and/or sell copies of the Software, and to permit persons to whom the
18 Software is furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included
21 in all copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29 IN THE SOFTWARE.
30
31 */
32
33 /**
34 * \file rtpudpv6transmitter.h
35 */
36
37 #ifndef RTPUDPV6TRANSMITTER_H
38
39 #define RTPUDPV6TRANSMITTER_H
40
41 #include "rtpconfig.h"
42
43 #ifdef RTP_SUPPORT_IPV6
44
45 #include "rtptransmitter.h"
46 #include "rtpipv6destination.h"
47 #include "rtphashtable.h"
48 #include "rtpkeyhashtable.h"
49 #include "rtpsocketutil.h"
50 #include "rtpabortdescriptors.h"
51 #include <string.h>
52 #include <list>
53
54 #ifdef RTP_SUPPORT_THREAD
55 #include <jthread/jmutex.h>
56 #endif // RTP_SUPPORT_THREAD
57
58 #define RTPUDPV6TRANS_HASHSIZE 8317
59 #define RTPUDPV6TRANS_DEFAULTPORTBASE 5000
60
61 #define RTPUDPV6TRANS_RTPRECEIVEBUFFER 32768
62 #define RTPUDPV6TRANS_RTCPRECEIVEBUFFER 32768
63 #define RTPUDPV6TRANS_RTPTRANSMITBUFFER 32768
64 #define RTPUDPV6TRANS_RTCPTRANSMITBUFFER 32768
65
66 namespace jrtplib
67 {
68
69 /** Parameters for the UDP over IPv6 transmitter. */
70 class JRTPLIB_IMPORTEXPORT RTPUDPv6TransmissionParams : public RTPTransmissionParams
71 {
72 public:
73 RTPUDPv6TransmissionParams();
74
75 /** Sets the IP address which is used to bind the sockets to \c ip. */
SetBindIP(in6_addr ip)76 void SetBindIP(in6_addr ip) { bindIP = ip; }
77
78 /** Sets the multicast interface index. */
SetMulticastInterfaceIndex(unsigned int idx)79 void SetMulticastInterfaceIndex(unsigned int idx) { mcastifidx = idx; }
80
81 /** Sets the RTP portbase to \c pbase. This has to be an even number. */
SetPortbase(uint16_t pbase)82 void SetPortbase(uint16_t pbase) { portbase = pbase; }
83
84 /** Sets the multicast TTL to be used to \c mcastTTL. */
SetMulticastTTL(uint8_t mcastTTL)85 void SetMulticastTTL(uint8_t mcastTTL) { multicastTTL = mcastTTL; }
86
87 /** Passes a list of IP addresses which will be used as the local IP addresses. */
SetLocalIPList(std::list<in6_addr> & iplist)88 void SetLocalIPList(std::list<in6_addr> &iplist) { localIPs = iplist; }
89
90 /** Clears the list of local IP addresses.
91 * Clears the list of local IP addresses. An empty list will make the transmission component
92 * itself determine the local IP addresses.
93 */
ClearLocalIPList()94 void ClearLocalIPList() { localIPs.clear(); }
95
96 /** Returns the IP address which will be used to bind the sockets. */
GetBindIP()97 in6_addr GetBindIP() const { return bindIP; }
98
99 /** Returns the multicast interface index. */
GetMulticastInterfaceIndex()100 unsigned int GetMulticastInterfaceIndex() const { return mcastifidx; }
101
102 /** Returns the RTP portbase which will be used (default is 5000). */
GetPortbase()103 uint16_t GetPortbase() const { return portbase; }
104
105 /** Returns the multicast TTL which will be used (default is 1). */
GetMulticastTTL()106 uint8_t GetMulticastTTL() const { return multicastTTL; }
107
108 /** Returns the list of local IP addresses. */
GetLocalIPList()109 const std::list<in6_addr> &GetLocalIPList() const { return localIPs; }
110
111 /** Sets the RTP socket's send buffer size. */
SetRTPSendBuffer(int s)112 void SetRTPSendBuffer(int s) { rtpsendbuf = s; }
113
114 /** Sets the RTP socket's receive buffer size. */
SetRTPReceiveBuffer(int s)115 void SetRTPReceiveBuffer(int s) { rtprecvbuf = s; }
116
117 /** Sets the RTCP socket's send buffer size. */
SetRTCPSendBuffer(int s)118 void SetRTCPSendBuffer(int s) { rtcpsendbuf = s; }
119
120 /** Sets the RTCP socket's receive buffer size. */
SetRTCPReceiveBuffer(int s)121 void SetRTCPReceiveBuffer(int s) { rtcprecvbuf = s; }
122
123 /** If non null, the specified abort descriptors will be used to cancel
124 * the function that's waiting for packets to arrive; set to null (the default
125 * to let the transmitter create its own instance. */
SetCreatedAbortDescriptors(RTPAbortDescriptors * desc)126 void SetCreatedAbortDescriptors(RTPAbortDescriptors *desc) { m_pAbortDesc = desc; }
127
128 /** Returns the RTP socket's send buffer size. */
GetRTPSendBuffer()129 int GetRTPSendBuffer() const { return rtpsendbuf; }
130
131 /** Returns the RTP socket's receive buffer size. */
GetRTPReceiveBuffer()132 int GetRTPReceiveBuffer() const { return rtprecvbuf; }
133
134 /** Returns the RTCP socket's send buffer size. */
GetRTCPSendBuffer()135 int GetRTCPSendBuffer() const { return rtcpsendbuf; }
136
137 /** Returns the RTCP socket's receive buffer size. */
GetRTCPReceiveBuffer()138 int GetRTCPReceiveBuffer() const { return rtcprecvbuf; }
139
140 /** If non-null, this RTPAbortDescriptors instance will be used internally,
141 * which can be useful when creating your own poll thread for multiple
142 * sessions. */
GetCreatedAbortDescriptors()143 RTPAbortDescriptors *GetCreatedAbortDescriptors() const { return m_pAbortDesc; }
144 private:
145 uint16_t portbase;
146 in6_addr bindIP;
147 unsigned int mcastifidx;
148 std::list<in6_addr> localIPs;
149 uint8_t multicastTTL;
150 int rtpsendbuf, rtprecvbuf;
151 int rtcpsendbuf, rtcprecvbuf;
152
153 RTPAbortDescriptors *m_pAbortDesc;
154 };
155
RTPUDPv6TransmissionParams()156 inline RTPUDPv6TransmissionParams::RTPUDPv6TransmissionParams()
157 : RTPTransmissionParams(RTPTransmitter::IPv6UDPProto)
158 {
159 portbase = RTPUDPV6TRANS_DEFAULTPORTBASE;
160 for (int i = 0 ; i < 16 ; i++)
161 bindIP.s6_addr[i] = 0;
162
163 multicastTTL = 1;
164 mcastifidx = 0;
165 rtpsendbuf = RTPUDPV6TRANS_RTPTRANSMITBUFFER;
166 rtprecvbuf= RTPUDPV6TRANS_RTPRECEIVEBUFFER;
167 rtcpsendbuf = RTPUDPV6TRANS_RTCPTRANSMITBUFFER;
168 rtcprecvbuf = RTPUDPV6TRANS_RTCPRECEIVEBUFFER;
169
170 m_pAbortDesc = 0;
171 }
172
173 /** Additional information about the UDP over IPv6 transmitter. */
174 class JRTPLIB_IMPORTEXPORT RTPUDPv6TransmissionInfo : public RTPTransmissionInfo
175 {
176 public:
RTPUDPv6TransmissionInfo(std::list<in6_addr> iplist,SocketType rtpsock,SocketType rtcpsock,uint16_t rtpport,uint16_t rtcpport)177 RTPUDPv6TransmissionInfo(std::list<in6_addr> iplist, SocketType rtpsock, SocketType rtcpsock,
178 uint16_t rtpport, uint16_t rtcpport) : RTPTransmissionInfo(RTPTransmitter::IPv6UDPProto)
179 { localIPlist = iplist; rtpsocket = rtpsock; rtcpsocket = rtcpsock; m_rtpPort = rtpport; m_rtcpPort = rtcpport; }
180
~RTPUDPv6TransmissionInfo()181 ~RTPUDPv6TransmissionInfo() { }
182
183 /** Returns the list of IPv6 addresses the transmitter considers to be the local IP addresses. */
GetLocalIPList()184 std::list<in6_addr> GetLocalIPList() const { return localIPlist; }
185
186 /** Returns the socket descriptor used for receiving and transmitting RTP packets. */
GetRTPSocket()187 SocketType GetRTPSocket() const { return rtpsocket; }
188
189 /** Returns the socket descriptor used for receiving and transmitting RTCP packets. */
GetRTCPSocket()190 SocketType GetRTCPSocket() const { return rtcpsocket; }
191
192 /** Returns the port number that the RTP socket receives packets on. */
GetRTPPort()193 uint16_t GetRTPPort() const { return m_rtpPort; }
194
195 /** Returns the port number that the RTCP socket receives packets on. */
GetRTCPPort()196 uint16_t GetRTCPPort() const { return m_rtcpPort; }
197 private:
198 std::list<in6_addr> localIPlist;
199 SocketType rtpsocket,rtcpsocket;
200 uint16_t m_rtpPort, m_rtcpPort;
201 };
202
203 class JRTPLIB_IMPORTEXPORT RTPUDPv6Trans_GetHashIndex_IPv6Dest
204 {
205 public:
GetIndex(const RTPIPv6Destination & d)206 static int GetIndex(const RTPIPv6Destination &d) { in6_addr ip = d.GetIP(); return ((((uint32_t)ip.s6_addr[12])<<24)|(((uint32_t)ip.s6_addr[13])<<16)|(((uint32_t)ip.s6_addr[14])<<8)|((uint32_t)ip.s6_addr[15]))%RTPUDPV6TRANS_HASHSIZE; }
207 };
208
209 class JRTPLIB_IMPORTEXPORT RTPUDPv6Trans_GetHashIndex_in6_addr
210 {
211 public:
GetIndex(const in6_addr & ip)212 static int GetIndex(const in6_addr &ip) { return ((((uint32_t)ip.s6_addr[12])<<24)|(((uint32_t)ip.s6_addr[13])<<16)|(((uint32_t)ip.s6_addr[14])<<8)|((uint32_t)ip.s6_addr[15]))%RTPUDPV6TRANS_HASHSIZE; }
213 };
214
215 #define RTPUDPV6TRANS_HEADERSIZE (40+8)
216
217 /** An UDP over IPv6 transmitter.
218 * This class inherits the RTPTransmitter interface and implements a transmission component
219 * which uses UDP over IPv6 to send and receive RTP and RTCP data. The component's parameters
220 * are described by the class RTPUDPv6TransmissionParams. The functions which have an RTPAddress
221 * argument require an argument of RTPIPv6Address. The GetTransmissionInfo member function
222 * returns an instance of type RTPUDPv6TransmissionInfo.
223 */
224 class JRTPLIB_IMPORTEXPORT RTPUDPv6Transmitter : public RTPTransmitter
225 {
226 JRTPLIB_NO_COPY(RTPUDPv6Transmitter)
227 public:
228 RTPUDPv6Transmitter(RTPMemoryManager *mgr);
229 ~RTPUDPv6Transmitter();
230
231 int Init(bool treadsafe);
232 int Create(size_t maxpacksize,const RTPTransmissionParams *transparams);
233 void Destroy();
234 RTPTransmissionInfo *GetTransmissionInfo();
235 void DeleteTransmissionInfo(RTPTransmissionInfo *inf);
236
237 int GetLocalHostName(uint8_t *buffer,size_t *bufferlength);
238 bool ComesFromThisTransmitter(const RTPAddress *addr);
GetHeaderOverhead()239 size_t GetHeaderOverhead() { return RTPUDPV6TRANS_HEADERSIZE; }
240
241 int Poll();
242 int WaitForIncomingData(const RTPTime &delay,bool *dataavailable = 0);
243 int AbortWait();
244
245 int SendRTPData(const void *data,size_t len);
246 int SendRTCPData(const void *data,size_t len);
247
248 int AddDestination(const RTPAddress &addr);
249 int DeleteDestination(const RTPAddress &addr);
250 void ClearDestinations();
251
252 bool SupportsMulticasting();
253 int JoinMulticastGroup(const RTPAddress &addr);
254 int LeaveMulticastGroup(const RTPAddress &addr);
255 void LeaveAllMulticastGroups();
256
257 int SetReceiveMode(RTPTransmitter::ReceiveMode m);
258 int AddToIgnoreList(const RTPAddress &addr);
259 int DeleteFromIgnoreList(const RTPAddress &addr);
260 void ClearIgnoreList();
261 int AddToAcceptList(const RTPAddress &addr);
262 int DeleteFromAcceptList(const RTPAddress &addr);
263 void ClearAcceptList();
264 int SetMaximumPacketSize(size_t s);
265
266 bool NewDataAvailable();
267 RTPRawPacket *GetNextPacket();
268 #ifdef RTPDEBUG
269 void Dump();
270 #endif // RTPDEBUG
271 private:
272 int CreateLocalIPList();
273 bool GetLocalIPList_Interfaces();
274 void GetLocalIPList_DNS();
275 void AddLoopbackAddress();
276 void FlushPackets();
277 int PollSocket(bool rtp);
278 int ProcessAddAcceptIgnoreEntry(in6_addr ip,uint16_t port);
279 int ProcessDeleteAcceptIgnoreEntry(in6_addr ip,uint16_t port);
280 #ifdef RTP_SUPPORT_IPV6MULTICAST
281 bool SetMulticastTTL(uint8_t ttl);
282 #endif // RTP_SUPPORT_IPV6MULTICAST
283 bool ShouldAcceptData(in6_addr srcip,uint16_t srcport);
284 void ClearAcceptIgnoreInfo();
285
286 bool init;
287 bool created;
288 bool waitingfordata;
289 SocketType rtpsock,rtcpsock;
290 in6_addr bindIP;
291 unsigned int mcastifidx;
292 std::list<in6_addr> localIPs;
293 uint16_t portbase;
294 uint8_t multicastTTL;
295 RTPTransmitter::ReceiveMode receivemode;
296
297 uint8_t *localhostname;
298 size_t localhostnamelength;
299
300 RTPHashTable<const RTPIPv6Destination,RTPUDPv6Trans_GetHashIndex_IPv6Dest,RTPUDPV6TRANS_HASHSIZE> destinations;
301 #ifdef RTP_SUPPORT_IPV6MULTICAST
302 RTPHashTable<const in6_addr,RTPUDPv6Trans_GetHashIndex_in6_addr,RTPUDPV6TRANS_HASHSIZE> multicastgroups;
303 #endif // RTP_SUPPORT_IPV6MULTICAST
304 std::list<RTPRawPacket*> rawpacketlist;
305
306 bool supportsmulticasting;
307 size_t maxpacksize;
308
309 class PortInfo
310 {
311 public:
PortInfo()312 PortInfo() { all = false; }
313
314 bool all;
315 std::list<uint16_t> portlist;
316 };
317
318 RTPKeyHashTable<const in6_addr,PortInfo*,RTPUDPv6Trans_GetHashIndex_in6_addr,RTPUDPV6TRANS_HASHSIZE> acceptignoreinfo;
319 RTPAbortDescriptors m_abortDesc;
320 RTPAbortDescriptors *m_pAbortDesc;
321
322 #ifdef RTP_SUPPORT_THREAD
323 jthread::JMutex mainmutex,waitmutex;
324 int threadsafe;
325 #endif // RTP_SUPPORT_THREAD
326 };
327
328 } // end namespace
329
330 #endif // RTP_SUPPORT_IPV6
331
332 #endif // RTPUDPV6TRANSMITTER_H
333
334