]> git.sesse.net Git - vlc/blob - modules/access/udp.c
Fix impossible out-of-bound strcmp
[vlc] / modules / access / udp.c
1 /*****************************************************************************
2  * udp.c: raw UDP & RTP input module
3  *****************************************************************************
4  * Copyright (C) 2001-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Tristan Leteurtre <tooney@via.ecp.fr>
9  *          Laurent Aimar <fenrir@via.ecp.fr>
10  *          Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
11  *
12  * Reviewed: 23 October 2003, Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27  *****************************************************************************/
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32 #include <stdlib.h>
33
34 #include <vlc/vlc.h>
35 #include <vlc_access.h>
36 #include <vlc_network.h>
37
38 #if defined (HAVE_NETINET_UDPLITE_H)
39 # include <netinet/udplite.h>
40 #elif defined (__linux__)
41 # define UDPLITE_SEND_CSCOV     10
42 # define UDPLITE_RECV_CSCOV     11
43 #endif
44
45 #ifndef IPPROTO_UDPLITE
46 # define IPPROTO_UDPLITE 136 /* from IANA */
47 #endif
48 #ifndef SOL_UDPLITE
49 # define SOL_UDPLITE IPPROTO_UDPLITE
50 #endif
51
52
53 /*****************************************************************************
54  * Module descriptor
55  *****************************************************************************/
56 #define CACHING_TEXT N_("Caching value in ms")
57 #define CACHING_LONGTEXT N_( \
58     "Caching value for UDP streams. This " \
59     "value should be set in milliseconds." )
60
61 #define AUTO_MTU_TEXT N_("Autodetection of MTU")
62 #define AUTO_MTU_LONGTEXT N_( \
63     "Automatically detect the line's MTU. This will increase the size if" \
64     " truncated packets are found" )
65
66 #define RTP_LATE_TEXT N_("RTP reordering timeout in ms")
67 #define RTP_LATE_LONGTEXT N_( \
68     "VLC reorders RTP packets. The input will wait for late packets at most "\
69     "the time specified here (in milliseconds)." )
70
71 static int  Open ( vlc_object_t * );
72 static void Close( vlc_object_t * );
73
74 vlc_module_begin();
75     set_shortname( _("UDP/RTP" ) );
76     set_description( _("UDP/RTP input") );
77     set_category( CAT_INPUT );
78     set_subcategory( SUBCAT_INPUT_ACCESS );
79
80     add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
81                  CACHING_LONGTEXT, VLC_TRUE );
82     add_integer( "rtp-late", 100, NULL, RTP_LATE_TEXT, RTP_LATE_LONGTEXT, VLC_TRUE );
83
84     add_bool( "udp-auto-mtu", 1, NULL,
85               AUTO_MTU_TEXT, AUTO_MTU_LONGTEXT, VLC_TRUE );
86
87     set_capability( "access2", 0 );
88     add_shortcut( "udp" );
89     add_shortcut( "udpstream" );
90     add_shortcut( "udp4" );
91     add_shortcut( "udp6" );
92     add_shortcut( "rtp" );
93     add_shortcut( "rtp4" );
94     add_shortcut( "rtp6" );
95     add_shortcut( "udplite" );
96     add_shortcut( "rtplite" );
97
98     set_callbacks( Open, Close );
99 vlc_module_end();
100
101 /*****************************************************************************
102  * Local prototypes
103  *****************************************************************************/
104 #define RTP_HEADER_LEN 12
105
106 static block_t *BlockUDP( access_t * );
107 static block_t *BlockRTP( access_t * );
108 static block_t *BlockChoose( access_t * );
109 static int Control( access_t *, int, va_list );
110
111 struct access_sys_t
112 {
113     int fd;
114
115     int i_mtu;
116     vlc_bool_t b_auto_mtu;
117
118     /* reorder rtp packets when out-of-sequence */
119     mtime_t i_rtp_late;
120     uint16_t i_last_seqno;
121     block_t *p_list;
122     block_t *p_end;
123 };
124
125 /*****************************************************************************
126  * Open: open the socket
127  *****************************************************************************/
128 static int Open( vlc_object_t *p_this )
129 {
130     access_t     *p_access = (access_t*)p_this;
131     access_sys_t *p_sys;
132
133     char *psz_name = strdup( p_access->psz_path );
134     char *psz_parser;
135     const char *psz_server_addr, *psz_bind_addr = "";
136     int  i_bind_port, i_server_port = 0;
137     int fam = AF_UNSPEC, proto = IPPROTO_UDP, cscov = 8;
138
139     if (strlen (p_access->psz_access) >= 3)
140     {
141         switch (p_access->psz_access[3])
142         {
143             case '4':
144                 fam = AF_INET;
145                 break;
146
147             case '6':
148                 fam = AF_INET6;
149                 break;
150         }
151         if (strcmp (p_access->psz_access + 3, "lite") == 0)
152             proto = IPPROTO_UDPLITE;
153    }
154    if (strncmp (p_access->psz_access, "rtp", 3) == 0)
155         /* Checksum coverage: RTP header is AT LEAST 12 bytes
156          * in addition to UDP header (8 bytes) */
157         cscov += 12;
158
159     i_bind_port = var_CreateGetInteger( p_access, "server-port" );
160
161     /* Parse psz_name syntax :
162      * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
163     psz_parser = strchr( psz_name, '@' );
164     if( psz_parser != NULL )
165     {
166         /* Found bind address and/or bind port */
167         *psz_parser++ = '\0';
168         psz_bind_addr = psz_parser;
169
170         if( *psz_parser == '[' )
171             /* skips bracket'd IPv6 address */
172             psz_parser = strchr( psz_parser, ']' );
173
174         if( psz_parser != NULL )
175         {
176             psz_parser = strchr( psz_parser, ':' );
177             if( psz_parser != NULL )
178             {
179                 *psz_parser++ = '\0';
180                 i_bind_port = atoi( psz_parser );
181             }
182         }
183     }
184
185     psz_server_addr = psz_name;
186     if( *psz_server_addr == '[' )
187         /* skips bracket'd IPv6 address */
188         psz_parser = strchr( psz_name, ']' );
189
190     if( psz_parser != NULL )
191     {
192         psz_parser = strchr( psz_parser, ':' );
193         if( psz_parser != NULL )
194         {
195             *psz_parser++ = '\0';
196             i_server_port = atoi( psz_parser );
197         }
198     }
199
200     msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
201              psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
202
203     /* Set up p_access */
204     access_InitFields( p_access );
205     ACCESS_SET_CALLBACKS( NULL, BlockChoose, Control, NULL );
206     p_access->info.b_prebuffered = VLC_FALSE;
207     MALLOC_ERR( p_access->p_sys, access_sys_t ); p_sys = p_access->p_sys;
208
209     p_sys->fd = net_Open( p_access, psz_bind_addr, i_bind_port,
210                           psz_server_addr, i_server_port, fam, SOCK_DGRAM, proto );
211     free (psz_name);
212     if( p_sys->fd == -1 )
213     {
214         msg_Err( p_access, "cannot open socket" );
215         free( p_sys );
216         return VLC_EGENERIC;
217     }
218
219     net_StopSend( p_sys->fd );
220
221 #ifdef UDPLITE_RECV_CSCOV
222     if (proto == IPPROTO_UDPLITE)
223         setsockopt (p_sys->fd, SOL_UDPLITE, UDPLITE_RECV_CSCOV, &cscov, sizeof (cscov));
224 #endif
225
226     /* FIXME */
227     p_sys->i_mtu = var_CreateGetInteger( p_access, "mtu" );
228     if( p_sys->i_mtu <= 1 )
229         p_sys->i_mtu  = 1500;   /* Avoid problem */
230
231     p_sys->b_auto_mtu = var_CreateGetBool( p_access, "udp-auto-mtu" );;
232
233     /* Update default_pts to a suitable value for udp access */
234     var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
235
236     /* RTP reordering for out-of-sequence packets */
237     p_sys->i_rtp_late = var_CreateGetInteger( p_access, "rtp-late" ) * 1000;
238     p_sys->i_last_seqno = 0;
239     p_sys->p_list = NULL;
240     p_sys->p_end = NULL;
241     return VLC_SUCCESS;
242 }
243
244 /*****************************************************************************
245  * Close: free unused data structures
246  *****************************************************************************/
247 static void Close( vlc_object_t *p_this )
248 {
249     access_t     *p_access = (access_t*)p_this;
250     access_sys_t *p_sys = p_access->p_sys;
251
252     block_ChainRelease( p_sys->p_list );
253     net_Close( p_sys->fd );
254     free( p_sys );
255 }
256
257 /*****************************************************************************
258  * Control:
259  *****************************************************************************/
260 static int Control( access_t *p_access, int i_query, va_list args )
261 {
262     access_sys_t *p_sys = p_access->p_sys;
263     vlc_bool_t   *pb_bool;
264     int          *pi_int;
265     int64_t      *pi_64;
266
267     switch( i_query )
268     {
269         /* */
270         case ACCESS_CAN_SEEK:
271         case ACCESS_CAN_FASTSEEK:
272         case ACCESS_CAN_PAUSE:
273         case ACCESS_CAN_CONTROL_PACE:
274             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
275             *pb_bool = VLC_FALSE;
276             break;
277         /* */
278         case ACCESS_GET_MTU:
279             pi_int = (int*)va_arg( args, int * );
280             *pi_int = p_sys->i_mtu;
281             break;
282
283         case ACCESS_GET_PTS_DELAY:
284             pi_64 = (int64_t*)va_arg( args, int64_t * );
285             *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
286             break;
287
288         /* */
289         case ACCESS_SET_PAUSE_STATE:
290         case ACCESS_GET_TITLE_INFO:
291         case ACCESS_SET_TITLE:
292         case ACCESS_SET_SEEKPOINT:
293         case ACCESS_SET_PRIVATE_ID_STATE:
294             return VLC_EGENERIC;
295
296         default:
297             msg_Warn( p_access, "unimplemented query in control" );
298             return VLC_EGENERIC;
299
300     }
301     return VLC_SUCCESS;
302 }
303
304 /*****************************************************************************
305  * BlockUDP:
306  *****************************************************************************/
307 static block_t *BlockUDP( access_t *p_access )
308 {
309     access_sys_t *p_sys = p_access->p_sys;
310     block_t      *p_block;
311
312     /* Read data */
313     p_block = block_New( p_access, p_sys->i_mtu );
314     p_block->i_buffer = net_Read( p_access, p_sys->fd, NULL,
315                                   p_block->p_buffer, p_sys->i_mtu,
316                                   VLC_FALSE );
317     if( p_block->i_buffer <= 0 )
318     {
319         block_Release( p_block );
320         return NULL;
321     }
322
323     if( (p_block->i_buffer >= p_sys->i_mtu) && p_sys->b_auto_mtu &&
324         p_sys->i_mtu < 32767 )
325     {
326         /* Increase by 100% */
327         p_sys->i_mtu *= 2;
328         msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
329     }
330
331     return p_block;
332 }
333
334 /*
335  * rtp_ChainInsert - insert a p_block in the chain and
336  * look at the sequence numbers.
337  */
338 static inline vlc_bool_t rtp_ChainInsert( access_t *p_access, block_t *p_block )
339 {
340     access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
341     block_t *p_prev = NULL;
342     block_t *p = p_sys->p_end;
343     uint16_t i_new = (uint16_t) p_block->i_dts;
344     uint16_t i_tmp = 0;
345
346     if( !p_sys->p_list )
347     {
348         p_sys->p_list = p_block;
349         p_sys->p_end = p_block;
350         return VLC_TRUE;
351     }
352     /* walk through the queue from top down since the new packet is in 
353     most cases just appended to the end */
354
355     for( ;; )
356     {
357         i_tmp = i_new - (uint16_t) p->i_dts;
358
359         if( !i_tmp )   /* trash duplicate */
360             break; 
361
362         if ( i_tmp < 32768 )
363         {   /* insert after this block ( i_new > p->i_dts ) */
364             p_block->p_next = p->p_next;
365             p->p_next = p_block;
366             p_block->p_prev = p;
367             if (p_prev)
368             {
369                 p_prev->p_prev = p_block;
370                 msg_Dbg(p_access, "RTP reordering: insert after %d, new %d", 
371                     (uint16_t) p->i_dts, i_new );
372             }
373             else 
374             {
375                 p_sys->p_end = p_block;
376             }
377             return VLC_TRUE;
378         }
379         if( p == p_sys->p_list )
380         {   /* we've reached bottom of chain */
381             i_tmp = p_sys->i_last_seqno - i_new;
382             if( !p_access->info.b_prebuffered || (i_tmp > 32767) )
383             {
384                 msg_Dbg(p_access, "RTP reordering: prepend %d before %d", 
385                         i_new, (uint16_t) p->i_dts );
386                 p_block->p_next = p;
387                 p->p_prev = p_block;
388                 p_sys->p_list = p_block;
389                 return VLC_TRUE;
390             }
391
392             if( !i_tmp )   /* trash duplicate */
393                 break;    
394
395             /* reordering failed - append the packet to the end of queue */
396             msg_Dbg(p_access, "RTP: sequence changed (or buffer too small) "
397                 "new: %d, buffer %d...%d", i_new, (uint16_t) p->i_dts, 
398                 (uint16_t) p_sys->p_end->i_dts);
399             p_sys->p_end->p_next = p_block;
400             p_block->p_prev = p_sys->p_end;
401             p_sys->p_end = p_block;
402             return VLC_TRUE;
403         }
404         p_prev = p;
405         p = p->p_prev;
406     }
407     block_Release( p_block );
408     return VLC_FALSE;
409 }
410
411 /*****************************************************************************
412  * BlockParseRTP/BlockRTP:
413  *****************************************************************************/
414 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
415 {
416     int      i_rtp_version;
417     int      i_CSRC_count;
418     int      i_payload_type;
419     int      i_skip = 0;
420     int      i_extension_flag = 0;
421     int      i_extension_length = 0;
422     uint16_t i_sequence_number = 0;
423
424     if( p_block == NULL )
425         return NULL;
426
427     if( p_block->i_buffer < RTP_HEADER_LEN )
428         goto trash;
429
430     /* Parse the header and make some verifications.
431      * See RFC 3550. */
432     i_rtp_version     = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
433     i_CSRC_count      = p_block->p_buffer[0] & 0x0F;
434     i_extension_flag  = p_block->p_buffer[0] & 0x10;
435     i_payload_type    = p_block->p_buffer[1] & 0x7F;
436     i_sequence_number = (p_block->p_buffer[2] << 8 ) + p_block->p_buffer[3];
437
438     if( i_rtp_version != 2 )
439         msg_Dbg( p_access, "RTP version is %u, should be 2", i_rtp_version );
440
441     if( i_payload_type == 14 || i_payload_type == 32)
442         i_skip = 4;
443     else if( i_payload_type !=  33 )
444         msg_Dbg( p_access, "unsupported RTP payload type (%u)", i_payload_type );
445     if( i_extension_flag )
446         i_extension_length = 4 +
447             4 * ( (p_block->p_buffer[14] << 8) + p_block->p_buffer[15] );
448
449     /* Skip header + CSRC extension field n*(32 bits) + extension */
450     i_skip += RTP_HEADER_LEN + 4*i_CSRC_count + i_extension_length;
451
452     if( i_skip >= p_block->i_buffer )
453         goto trash;
454
455     /* Return the packet without the RTP header, remember seqno in i_dts */
456     p_block->i_buffer -= i_skip;
457     p_block->p_buffer += i_skip;
458     p_block->i_pts = mdate();
459     p_block->i_dts = (mtime_t) i_sequence_number;
460
461 #if 0
462     /* Emulate packet loss */
463     if ( (i_sequence_number % 4000) == 0)
464     {
465         msg_Warn( p_access, "Emulating packet drop" );
466         block_Release( p_block );
467         return NULL;
468     }
469 #endif
470
471     return p_block;
472
473 trash:
474     msg_Warn( p_access, "received a too short packet for RTP" );
475     block_Release( p_block );
476     return NULL;
477 }
478
479 static block_t *BlockPrebufferRTP( access_t *p_access, block_t *p_block )
480 {
481     access_sys_t *p_sys = p_access->p_sys;
482     mtime_t   i_first = mdate();
483     int       i_count = 0;
484     block_t   *p = p_block;
485
486     for( ;; )
487     {
488         mtime_t i_date = mdate();
489
490         if( p && rtp_ChainInsert( p_access, p ))
491             i_count++;
492
493         /* Require at least 2 packets in the buffer */
494         if( i_count > 2 && (i_date - i_first) > p_sys->i_rtp_late )
495             break;
496
497         p = BlockParseRTP( p_access, BlockUDP( p_access ));
498         if( !p && (i_date - i_first) > p_sys->i_rtp_late ) 
499         {
500             msg_Err( p_access, "error in RTP prebuffering!" );
501             break;
502         }
503     }
504
505     msg_Dbg( p_access, "RTP: prebuffered %d packets", i_count - 1 );
506     p_access->info.b_prebuffered = VLC_TRUE;
507     p = p_sys->p_list;
508     p_sys->p_list = p_sys->p_list->p_next;
509     p_sys->i_last_seqno = (uint16_t) p->i_dts;
510     p->p_next = NULL;
511     return p;
512 }
513
514 static block_t *BlockRTP( access_t *p_access )
515 {
516     access_sys_t *p_sys = p_access->p_sys;
517     block_t *p;
518
519     while ( !p_sys->p_list || 
520              ( mdate() - p_sys->p_list->i_pts ) < p_sys->i_rtp_late )
521     {
522         p = BlockParseRTP( p_access, BlockUDP( p_access ));
523
524         if ( !p ) 
525             return NULL;
526
527         rtp_ChainInsert( p_access, p );
528     }
529
530     p = p_sys->p_list;
531     p_sys->p_list = p_sys->p_list->p_next;
532     p_sys->i_last_seqno++;
533     if( p_sys->i_last_seqno != (uint16_t) p->i_dts )
534     {
535         msg_Dbg( p_access, "RTP: packet(s) lost, expected %d, got %d",
536                  p_sys->i_last_seqno, (uint16_t) p->i_dts );
537         p_sys->i_last_seqno = (uint16_t) p->i_dts;
538     }
539     p->p_next = NULL;
540     return p;
541 }
542
543 /*****************************************************************************
544  * BlockChoose: decide between RTP and UDP
545  *****************************************************************************/
546 static block_t *BlockChoose( access_t *p_access )
547 {
548     block_t *p_block;
549     int     i_rtp_version;
550     int     i_CSRC_count;
551     int     i_payload_type;
552
553     if( ( p_block = BlockUDP( p_access ) ) == NULL )
554         return NULL;
555
556     if( p_block->p_buffer[0] == 0x47 )
557     {
558         msg_Dbg( p_access, "detected TS over raw UDP" );
559         p_access->pf_block = BlockUDP;
560         p_access->info.b_prebuffered = VLC_TRUE;
561         return p_block;
562     }
563
564     if( p_block->i_buffer < RTP_HEADER_LEN )
565         return p_block;
566
567     /* Parse the header and make some verifications.
568      * See RFC 3550. */
569
570     i_rtp_version  = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
571     i_CSRC_count   = ( p_block->p_buffer[0] & 0x0F );
572     i_payload_type = ( p_block->p_buffer[1] & 0x7F );
573
574     if( i_rtp_version != 2 )
575     {
576         msg_Dbg( p_access, "no supported RTP header detected" );
577         p_access->pf_block = BlockUDP;
578         p_access->info.b_prebuffered = VLC_TRUE;
579         return p_block;
580     }
581
582     switch( i_payload_type )
583     {
584         case 33:
585             msg_Dbg( p_access, "detected TS over RTP" );
586             p_access->psz_demux = strdup( "ts" );
587             break;
588
589         case 14:
590             msg_Dbg( p_access, "detected MPEG audio over RTP" );
591             p_access->psz_demux = strdup( "mpga" );
592             break;
593
594         case 32:
595             msg_Dbg( p_access, "detected MPEG video over RTP" );
596             p_access->psz_demux = strdup( "mpgv" );
597             break;
598
599         default:
600             msg_Dbg( p_access, "no RTP header detected" );
601             p_access->pf_block = BlockUDP;
602             p_access->info.b_prebuffered = VLC_TRUE;
603             return p_block;
604     }
605
606     if( !BlockParseRTP( p_access, p_block )) return NULL;
607
608     p_access->pf_block = BlockRTP;
609
610     return BlockPrebufferRTP( p_access, p_block );
611 }