1 /*
2  * Copyright (C) 2004 Nathan Lutchansky <lutchann@litech.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18 
19 #include <sys/types.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/time.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <errno.h>
29 
30 #include <event.h>
31 #include <log.h>
32 #include <frame.h>
33 #include <rtp.h>
34 #include <conf_parse.h>
35 
36 static int rtp_port_start = 50000, rtp_port_end = 60000;
37 
38 static int rtcp_send( struct rtp_endpoint *ep );
39 
rtcp_fire(struct event_info * ei,void * d)40 static void rtcp_fire( struct event_info *ei, void *d )
41 {
42 	struct rtp_endpoint *ep = (struct rtp_endpoint *)d;
43 
44 	rtcp_send( ep );
45 }
46 
new_rtp_endpoint(int payload)47 struct rtp_endpoint *new_rtp_endpoint( int payload )
48 {
49 	struct rtp_endpoint *ep;
50 
51 	if( ! ( ep = (struct rtp_endpoint *)
52 			malloc( sizeof( struct rtp_endpoint ) ) ) )
53 		return NULL;
54 	ep->payload = payload;
55 	ep->max_data_size = 8192; /* default maximum */
56 	ep->ssrc = 0;
57 	random_bytes( (unsigned char *)&ep->ssrc, 4 );
58 	ep->start_timestamp = 0;
59 	random_bytes( (unsigned char *)&ep->start_timestamp, 4 );
60 	ep->last_timestamp = ep->start_timestamp;
61 	ep->seqnum = 0;
62 	random_bytes( (unsigned char *)&ep->seqnum, 2 );
63 	ep->packet_count = 0;
64 	ep->octet_count = 0;
65 	ep->rtcp_send_event = add_timer_event( 5000, 0, rtcp_fire, ep );
66 	set_event_enabled( ep->rtcp_send_event, 0 );
67 	ep->force_rtcp = 1;
68 	gettimeofday( &ep->last_rtcp_recv, NULL );
69 	ep->trans_type = 0;
70 
71 	return ep;
72 }
73 
del_rtp_endpoint(struct rtp_endpoint * ep)74 void del_rtp_endpoint( struct rtp_endpoint *ep )
75 {
76 	remove_event( ep->rtcp_send_event );
77 
78 	switch( ep->trans_type )
79 	{
80 	case RTP_TRANS_UDP:
81 		remove_event( ep->trans.udp.rtp_event );
82 		close( ep->trans.udp.rtp_fd );
83 		remove_event( ep->trans.udp.rtcp_event );
84 		close( ep->trans.udp.rtcp_fd );
85 		break;
86 	case RTP_TRANS_INTER:
87 		interleave_disconnect( ep->trans.inter.conn,
88 						ep->trans.inter.rtp_chan );
89 		interleave_disconnect( ep->trans.inter.conn,
90 						ep->trans.inter.rtcp_chan );
91 		break;
92 	}
93 
94 	free( ep );
95 }
96 
update_rtp_timestamp(struct rtp_endpoint * ep,int time_increment)97 void update_rtp_timestamp( struct rtp_endpoint *ep, int time_increment )
98 {
99 	ep->last_timestamp += time_increment;
100 	ep->last_timestamp &= 0xFFFFFFFF;
101 }
102 
udp_rtp_read(struct event_info * ei,void * d)103 static void udp_rtp_read( struct event_info *ei, void *d )
104 {
105 	struct rtp_endpoint *ep = (struct rtp_endpoint *)d;
106 	unsigned char buf[16384];
107 	int ret;
108 
109 	ret = read( ep->trans.udp.rtp_fd, buf, sizeof( buf ) );
110 	if( ret > 0 )
111 	{
112 		/* some SIP phones don't send RTCP */
113 		gettimeofday( &ep->last_rtcp_recv, NULL );
114 		return;
115 	} else if( ret < 0 )
116 		spook_log( SL_VERBOSE, "error on UDP RTP socket: %s",
117 			strerror( errno ) );
118 	else spook_log( SL_VERBOSE, "UDP RTP socket closed" );
119 	ep->session->teardown( ep->session, ep );
120 }
121 
udp_rtcp_read(struct event_info * ei,void * d)122 static void udp_rtcp_read( struct event_info *ei, void *d )
123 {
124 	struct rtp_endpoint *ep = (struct rtp_endpoint *)d;
125 	unsigned char buf[16384];
126 	int ret;
127 
128 	ret = read( ep->trans.udp.rtcp_fd, buf, sizeof( buf ) );
129 	if( ret > 0 )
130 	{
131 		spook_log( SL_DEBUG, "received RTCP packet from client" );
132 		gettimeofday( &ep->last_rtcp_recv, NULL );
133 		return;
134 	} else if( ret < 0 )
135 		spook_log( SL_VERBOSE, "error on UDP RTCP socket: %s",
136 			strerror( errno ) );
137 	else spook_log( SL_VERBOSE, "UDP RTCP socket closed" );
138 	ep->session->teardown( ep->session, ep );
139 }
140 
interleave_recv_rtcp(struct rtp_endpoint * ep,unsigned char * d,int len)141 void interleave_recv_rtcp( struct rtp_endpoint *ep, unsigned char *d, int len )
142 {
143 	spook_log( SL_DEBUG, "received RTCP packet from client" );
144 	gettimeofday( &ep->last_rtcp_recv, NULL );
145 }
146 
rtcp_send(struct rtp_endpoint * ep)147 static int rtcp_send( struct rtp_endpoint *ep )
148 {
149 	struct timeval now;
150 	unsigned char buf[16384];
151 	unsigned int ntp_sec, ntp_usec;
152 	struct iovec v[1];
153 
154 	gettimeofday( &now, NULL );
155 
156 	ep->force_rtcp = 0;
157 //	spook_log( SL_DEBUG, "sending RTCP packet" );
158 
159 	/* Grrr...  QuickTime apparently doesn't send RTCP over TCP */
160 	if( ep->trans_type != RTP_TRANS_INTER )
161 	{
162 		if( now.tv_sec - ep->last_rtcp_recv.tv_sec > 20 )
163 		{
164 			spook_log( SL_VERBOSE, "client timeout (no RTCP received in 20 seconds)" );
165 			ep->session->teardown( ep->session, ep );
166 			return -1;
167 		}
168 	}
169 
170 	ntp_sec = now.tv_sec + 0x83AA7E80;
171 	ntp_usec = (double)( (double)now.tv_usec * (double)0x4000000 ) / 15625.0;
172 
173 	//spook_log( SL_DEBUG, "ssrc=%u, ntp_sec=%u, ntp_usec=%u last_timestamp=%u packet_count=%d octet_count=%d",
174 	//		x->ssrc, ntp_sec, ntp_usec, x->last_timestamp, x->packet_count, x->octet_count );
175 
176 	buf[0] = 2 << 6; // version
177 	buf[1] = 200; // packet type is Sender Report
178 	PUT_16( buf + 2, 6 ); // length in words minus one
179 	PUT_32( buf + 4, ep->ssrc );
180 	PUT_32( buf + 8, ntp_sec );
181 	PUT_32( buf + 12, ntp_usec );
182 	PUT_32( buf + 16, ep->last_timestamp );
183 	PUT_32( buf + 20, ep->packet_count );
184 	PUT_32( buf + 24, ep->octet_count );
185 	buf[28] = ( 2 << 6 ) | 1; // version; source count = 1
186 	buf[29] = 202; // packet type is Source Description
187 	PUT_16( buf + 30, 4 ); // length in words minus one
188 	PUT_32( buf + 32, ep->ssrc );
189 	buf[36] = 0x01; // field type is CNAME
190 	buf[37] = 14; // text length
191 	memcpy( buf + 38, "Unnamed stream", 14 );
192 	switch( ep->trans_type )
193 	{
194 	case RTP_TRANS_UDP:
195 		if( send( ep->trans.udp.rtcp_fd, buf, 52, 0 ) < 0 )
196 			spook_log( SL_VERBOSE, "error sending UDP RTCP frame: %s",
197 					strerror( errno ) );
198 		else return 0;
199 		break;
200 	case RTP_TRANS_INTER:
201 		v[0].iov_base = buf;
202 		v[0].iov_len = 52;
203 		if( interleave_send( ep->trans.inter.conn,
204 				ep->trans.inter.rtcp_chan, v, 1 ) < 0 )
205 			spook_log( SL_VERBOSE, "error sending interleaved RTCP frame" );
206 		else return 0;
207 		break;
208 	}
209 	ep->session->teardown( ep->session, ep );
210 	return -1;
211 }
212 
connect_udp_endpoint(struct rtp_endpoint * ep,struct in_addr dest_ip,int dest_port,int * our_port)213 int connect_udp_endpoint( struct rtp_endpoint *ep,
214 		struct in_addr dest_ip, int dest_port, int *our_port )
215 {
216 	struct sockaddr_in rtpaddr, rtcpaddr;
217 	int port, success = 0, i, max_tries, rtpfd = -1, rtcpfd = -1;
218 
219 	rtpaddr.sin_family = rtcpaddr.sin_family = AF_INET;
220 	rtpaddr.sin_addr.s_addr = rtcpaddr.sin_addr.s_addr = 0;
221 
222 	port = rtp_port_start + random() % ( rtp_port_end - rtp_port_start );
223 	if( port & 0x1 ) ++port;
224 	max_tries = ( rtp_port_end - rtp_port_start + 1 ) / 2;
225 
226 	for( i = 0; i < max_tries; ++i )
227 	{
228 		if( port + 1 > rtp_port_end ) port = rtp_port_start;
229 		rtpaddr.sin_port = htons( port );
230 		rtcpaddr.sin_port = htons( port + 1 );
231 		if( rtpfd < 0 &&
232 			( rtpfd = socket( PF_INET, SOCK_DGRAM, 0 ) ) < 0 )
233 		{
234 			spook_log( SL_WARN, "unable to create UDP RTP socket: %s",
235 					strerror( errno ) );
236 			return -1;
237 		}
238 		if( rtcpfd < 0 &&
239 			( rtcpfd = socket( PF_INET, SOCK_DGRAM, 0 ) ) < 0 )
240 		{
241 			spook_log( SL_WARN, "unable to create UDP RTCP socket: %s",
242 					strerror( errno ) );
243 			close( rtpfd );
244 			return -1;
245 		}
246 		if( bind( rtpfd, (struct sockaddr *)&rtpaddr,
247 					sizeof( rtpaddr ) ) < 0 )
248 		{
249 			if( errno == EADDRINUSE )
250 			{
251 				port += 2;
252 				continue;
253 			}
254 			spook_log( SL_WARN, "strange error when binding RTP socket: %s",
255 					strerror( errno ) );
256 			close( rtpfd );
257 			close( rtcpfd );
258 			return -1;
259 		}
260 		if( bind( rtcpfd, (struct sockaddr *)&rtcpaddr,
261 					sizeof( rtcpaddr ) ) < 0 )
262 		{
263 			if( errno == EADDRINUSE )
264 			{
265 				close( rtpfd );
266 				rtpfd = -1;
267 				port += 2;
268 				continue;
269 			}
270 			spook_log( SL_WARN, "strange error when binding RTCP socket: %s",
271 					strerror( errno ) );
272 			close( rtpfd );
273 			close( rtcpfd );
274 			return -1;
275 		}
276 		success = 1;
277 		break;
278 	}
279 	if( ! success )
280 	{
281 		spook_log( SL_WARN, "ran out of UDP RTP ports!" );
282 		return -1;
283 	}
284 	rtpaddr.sin_family = rtcpaddr.sin_family = AF_INET;
285 	rtpaddr.sin_addr = rtcpaddr.sin_addr = dest_ip;
286 	rtpaddr.sin_port = htons( dest_port );
287 	rtcpaddr.sin_port = htons( dest_port + 1 );
288 	if( connect( rtpfd, (struct sockaddr *)&rtpaddr,
289 				sizeof( rtpaddr ) ) < 0 )
290 	{
291 		spook_log( SL_WARN, "strange error when connecting RTP socket: %s",
292 				strerror( errno ) );
293 		close( rtpfd );
294 		close( rtcpfd );
295 		return -1;
296 	}
297 	if( connect( rtcpfd, (struct sockaddr *)&rtcpaddr,
298 				sizeof( rtcpaddr ) ) < 0 )
299 	{
300 		spook_log( SL_WARN, "strange error when connecting RTCP socket: %s",
301 				strerror( errno ) );
302 		close( rtpfd );
303 		close( rtcpfd );
304 		return -1;
305 	}
306 	i = sizeof( rtpaddr );
307 	if( getsockname( rtpfd, (struct sockaddr *)&rtpaddr, &i ) < 0 )
308 	{
309 		spook_log( SL_WARN, "strange error from getsockname: %s",
310 				strerror( errno ) );
311 		close( rtpfd );
312 		close( rtcpfd );
313 		return -1;
314 	}
315 
316 	ep->max_data_size = 1400; /* good guess for preventing fragmentation */
317 	ep->trans_type = RTP_TRANS_UDP;
318 	sprintf( ep->trans.udp.sdp_addr, "IP4 %s",
319 				inet_ntoa( rtpaddr.sin_addr ) );
320 	ep->trans.udp.sdp_port = ntohs( rtpaddr.sin_port );
321 	ep->trans.udp.rtp_fd = rtpfd;
322 	ep->trans.udp.rtcp_fd = rtcpfd;
323 	ep->trans.udp.rtp_event = add_fd_event( rtpfd, 0, 0, udp_rtp_read, ep );
324 	ep->trans.udp.rtcp_event =
325 				add_fd_event( rtcpfd, 0, 0, udp_rtcp_read, ep );
326 
327 	*our_port = port;
328 
329 	return 0;
330 }
331 
connect_interleaved_endpoint(struct rtp_endpoint * ep,struct conn * conn,int rtp_chan,int rtcp_chan)332 void connect_interleaved_endpoint( struct rtp_endpoint *ep,
333 		struct conn *conn, int rtp_chan, int rtcp_chan )
334 {
335 	ep->trans_type = RTP_TRANS_INTER;
336 	ep->trans.inter.conn = conn;
337 	ep->trans.inter.rtp_chan = rtp_chan;
338 	ep->trans.inter.rtcp_chan = rtcp_chan;
339 }
340 
send_rtp_packet(struct rtp_endpoint * ep,struct iovec * v,int count,unsigned int timestamp,int marker)341 int send_rtp_packet( struct rtp_endpoint *ep, struct iovec *v, int count,
342 			unsigned int timestamp, int marker )
343 {
344 	unsigned char rtphdr[12];
345 	struct msghdr mh;
346 	int i;
347 
348 	ep->last_timestamp = ( ep->start_timestamp + timestamp )
349 					& 0xFFFFFFFF;
350 
351 //	spook_log( SL_DEBUG, "RTP: payload %d, seq %u, time %u, marker %d",
352 //		ep->payload, ep->seqnum, ep->last_timestamp, marker );
353 
354 	rtphdr[0] = 2 << 6; /* version */
355 	rtphdr[1] = ep->payload;
356 	if( marker ) rtphdr[1] |= 0x80;
357 	PUT_16( rtphdr + 2, ep->seqnum );
358 	PUT_32( rtphdr + 4, ep->last_timestamp );
359 	PUT_32( rtphdr + 8, ep->ssrc );
360 
361 	v[0].iov_base = rtphdr;
362 	v[0].iov_len = 12;
363 
364 	switch( ep->trans_type )
365 	{
366 	case RTP_TRANS_UDP:
367 		memset( &mh, 0, sizeof( mh ) );
368 		mh.msg_iov = v;
369 		mh.msg_iovlen = count;
370 		if( sendmsg( ep->trans.udp.rtp_fd, &mh, 0 ) < 0 )
371 		{
372 			spook_log( SL_VERBOSE, "error sending UDP RTP frame: %s",
373 					strerror( errno ) );
374 			ep->session->teardown( ep->session, ep );
375 			return -1;
376 		}
377 		break;
378 	case RTP_TRANS_INTER:
379 		if( interleave_send( ep->trans.inter.conn,
380 				ep->trans.inter.rtp_chan, v, count ) < 0 )
381 		{
382 			spook_log( SL_VERBOSE, "error sending interleaved RTP frame" );
383 			ep->session->teardown( ep->session, ep );
384 			return -1;
385 		}
386 		break;
387 	}
388 
389 	for( i = 0; i < count; ++i ) ep->octet_count += v[i].iov_len;
390 	++ep->packet_count;
391 
392 	if( ep->force_rtcp )
393 	{
394 		if( rtcp_send( ep ) < 0 ) return -1;
395 		set_event_enabled( ep->rtcp_send_event, 1 );
396 	}
397 
398 	ep->seqnum = ( ep->seqnum + 1 ) & 0xFFFF;
399 
400 	return 0;
401 }
402 
403 /********************* GLOBAL CONFIGURATION DIRECTIVES ********************/
404 
config_rtprange(int num_tokens,struct token * tokens,void * d)405 int config_rtprange( int num_tokens, struct token *tokens, void *d )
406 {
407 	rtp_port_start = tokens[1].v.num;
408 	rtp_port_end = tokens[2].v.num;
409 
410 	if( rtp_port_start & 0x1 ) ++rtp_port_start;
411 	if( ! ( rtp_port_end & 0x1 ) ) --rtp_port_end;
412 
413 	spook_log( SL_DEBUG, "RTP port range is %d-%d",
414 				rtp_port_start, rtp_port_end );
415 
416 	if( rtp_port_end - rtp_port_start + 1 < 8 )
417 	{
418 		spook_log( SL_ERR, "at least 8 ports are needed for RTP" );
419 		exit( 1 );
420 	}
421 
422 	return 0;
423 }
424